30 unsigned char x[] = {1,0};
31 short y = *(
short*) x;
32 return y == 1 ?
'<' :
'>';
38 if(t ==
typeid(
float) )
return 'f';
39 if(t ==
typeid(
double) )
return 'd';
40 if(t ==
typeid(
long double) )
return 'd';
42 if(t ==
typeid(
int) )
return 'i';
43 if(t ==
typeid(
char) )
return 'i';
44 if(t ==
typeid(
short) )
return 'i';
45 if(t ==
typeid(
long) )
return 'i';
46 if(t ==
typeid(
long long) )
return 'i';
48 if(t ==
typeid(
unsigned char) )
return 'u';
49 if(t ==
typeid(
unsigned short) )
return 'u';
50 if(t ==
typeid(
unsigned long) )
return 'u';
51 if(t ==
typeid(
unsigned long long) )
return 'u';
52 if(t ==
typeid(
unsigned int) )
return 'u';
54 if(t ==
typeid(
bool) )
return 'b';
56 if(t ==
typeid(std::complex<float>) )
return 'c';
57 if(t ==
typeid(std::complex<double>) )
return 'c';
58 if(t ==
typeid(std::complex<long double>) )
return 'c';
63 void split(vector<string>& strs,
string& input,
const string& pat)
66 pch = strtok( &input[0], pat.c_str() );
69 strs.push_back(
string(pch ) );
70 pch = strtok( NULL, pat.c_str() );
114 while( ( ch = fgetc( fp )) != EOF )
118 header.push_back( ch );
131 ,
const size_t data_len,
const size_t numcols
137 FILE* fp = fopen( filename.c_str(),
"r+b" );
146 size_t shapePos = header.find(
"'shape':" );
147 size_t lbrac = header.find(
'(', shapePos );
148 size_t rbrac = header.find(
')', lbrac );
149 assert( lbrac > shapePos );
150 assert( rbrac > lbrac );
152 string prefixHeader = header.substr( 0, lbrac + 1 );
153 string postfixHeader = header.substr( rbrac );
155 string shapeStr = header.substr( lbrac + 1, rbrac - lbrac - 1);
157 vector<string> tokens;
158 split( tokens, shapeStr,
"," );
160 string newShape =
"";
161 for (
size_t i = 0; i < tokens.size(); i++)
162 newShape +=
moose::toString( atoi( tokens[i].c_str() ) + data_len/numcols ) +
",";
164 string newHeader = prefixHeader + newShape + postfixHeader +
"\n";
165 if( newHeader.size() < header.size() )
167 cout <<
"Warn: Modified header can not be smaller than old header" << endl;
171 fseek(fp, 0, SEEK_SET);
172 fwrite( newHeader.c_str(),
sizeof(char), newHeader.size(), fp );
string toString(double x)
Convert a given value to string.
char map_type(const std::type_info &t)
void split(vector< string > &strs, string &input, const string &pat)
static char __pre__[__pre__size__]
bool is_valid_numpy_file(FILE *fp)
Check if a numpy file is sane or not.
static const unsigned int __pre__size__
void parse_header(FILE *fp, string &header)
Parser header from a numpy file. Store it in vector.
void showWarn(string msg)
void change_shape_in_header(const string &filename, const size_t data_len, const size_t numcols)
Change shape in numpy header.