File: npy_read.c

package info (click to toggle)
c2x 2.40.e%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,136 kB
  • sloc: ansic: 22,418; makefile: 56; sh: 1
file content (170 lines) | stat: -rw-r--r-- 4,190 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Read a numpy array file, single density only, as floats or doubles 
* Cope with Fortran or C ordering, and either endianness */

/* MJR 12/2020 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include "c2xsf.h"



void npy_read(FILE* infile, struct grid *gptr){
/* NB "fortran" is an optionally-reserved word in C99, so fortrn here */
  int i,j,k,hdr_len,version,float_len,ndata,fortrn,data_little_endian;
  char magic[6]={0x93,'N','U','M','P','Y'};
  unsigned char buff[6];
  char *hdr,*cptr;
  void *data;
  double *dptr;

  data=NULL;
  
  if (fread(buff,1,6,infile)!=6)
    error_exit("Failed to read magic number");

  if (strncmp((char*)buff,magic,6))
    error_exit("Wrong magic number for .npy file");

  fread(buff,1,2,infile);
  version=buff[0];

  if (version==1){
    fread(buff,1,2,infile);
    hdr_len=buff[0]+(buff[1]<<8);
  }
  else{
    fread(buff,1,4,infile);
    hdr_len=buff[0]+(buff[1]<<8)+(buff[2]<<16)+(buff[3]<<24);
  }

  hdr=malloc(hdr_len+1);
  if (!hdr) error_exit("Malloc error for header");
  fread(hdr,1,hdr_len,infile);
  hdr[hdr_len]=0;

  cptr=strstr(hdr,"'shape'");
  if (!cptr) error_exit("Failed to find array shape");
  cptr=strchr(cptr,'(');
  if (!cptr) error_exit("Failed to parse array shape");
  cptr++;
  if (sscanf(cptr,"%d, %d, %d",gptr->size,gptr->size+1,gptr->size+2)!=3){
    fprintf(stderr,"%s\n",cptr);
    error_exit("Failed to parse dimensions from array shape");
  }
  ndata=gptr->size[0]*gptr->size[1]*gptr->size[2];
  
  cptr=strstr(hdr,"'descr'");
  if (!cptr) error_exit("Failed to find array datatype");
  cptr=strchr(cptr,':');
  if (!cptr) error_exit("Failed to parse array datatype");
  cptr++;
  while(*cptr==' ') cptr++;
  if ((*cptr!='\'')&&(*cptr!='"')){
    fprintf(stderr,"%s\n",cptr);
    error_exit("Failed to parse datatype");
  }
  cptr++;
  /* Ignore endianness for now */
  data_little_endian=self_little_endian(); /* start by assuming native */
  if (*cptr=='=')
    cptr++;
  else if (*cptr=='<') {
    data_little_endian=1;
    cptr++;
  }
  else if (*cptr=='>') {
    data_little_endian=0;
    cptr++;
  }
  
  if (*cptr!='f') error_exit("Unexpected datatype in descr");
  cptr++;
  float_len=4;
  sscanf(cptr,"%d",&float_len);
  if ((float_len!=4)&&(float_len!=8))
    error_exit("Unexpected float len in descr");

  fortrn=0;
  cptr=strstr(hdr,"'fortrn_order'");
  if (cptr){
    cptr=strchr(cptr,':');
    if (cptr){
      cptr++;
      while(*cptr==' ') cptr++;
      if (!strncmp(cptr,"True",4)) fortrn=1;
      else if (!strncmp(cptr,"False",5)) fortrn=0;
      else error_exit("Failed to parse fortrn_order");
    }
  }
  
  if (debug>1)
    fprintf(stderr,"Reading numpy array of %s size %dx%dx%d\n",
	    (float_len==4)?"floats":"doubles",
	    gptr->size[0],gptr->size[1],gptr->size[2]);
  
  data=malloc(float_len*ndata);
  if (!data) error_exit("Malloc error for data");
  
  if (fread(data,float_len,ndata,infile)!=ndata)
    error_exit("Read error reading array data");

  if (data_little_endian!=self_little_endian()){
    if (debug) fprintf(stderr,"Reversing endianness\n");
    if (float_len==4) reverse4n(data,ndata);
    else reverse8n(data,ndata);
  }
  
  gptr=grid_new(gptr);

  if ((!fortrn)&&(float_len==8)){
    gptr->data=(double*)data;
    return;
  }

  gptr->data=malloc(ndata*sizeof(double));
  if (!gptr->data) error_exit("Malloc error for data");

  
  if (float_len==8){
    dptr=gptr->data;
    for(i=0;i<gptr->size[0];i++)
      for(j=0;j<gptr->size[1];j++)
	for(k=0;k<gptr->size[2];k++)
	  *(dptr++)=((double*)data)[i+j*gptr->size[0]+
				    k*gptr->size[0]*gptr->size[1]];
  }
  else{
    if (!fortrn)
      for(i=0;i<ndata;i++)
	gptr->data[i]=((float*)data)[i];
    else{
      dptr=gptr->data;
      for(i=0;i<gptr->size[0];i++)
	for(j=0;j<gptr->size[1];j++)
	  for(k=0;k<gptr->size[2];k++)
	    *(dptr++)=((float*)data)[i+j*gptr->size[0]+
				     k*gptr->size[0]*gptr->size[1]];

    }
  }
  
  free(data);
  
}

int self_little_endian(){
  const float x=1;
  unsigned char *ptr;

  ptr=(unsigned char*)(&x);
  if (*ptr==0) return 1;
  if (*ptr==63) return 0;

  fprintf(stderr,"Endianness check failed. Assuming little\n");
  return 1;
  
  return 1;
}