Skip to content

Commit

Permalink
Merge pull request rogersce#5 from julienr/fortran_order
Browse files Browse the repository at this point in the history
Add fortran_order field to NpyArray to allow loading of Fortran-ordered ...
  • Loading branch information
rogersce committed May 16, 2013
2 parents 58b7ca8 + 474a229 commit b230586
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 6 additions & 5 deletions cnpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ template<> std::vector<char>& cnpy::operator+=(std::vector<char>& lhs, const cha
return lhs;
}

void cnpy::parse_npy_header(FILE* fp, unsigned int& word_size, unsigned int*& shape, unsigned int& ndims) {
void cnpy::parse_npy_header(FILE* fp, unsigned int& word_size, unsigned int*& shape, unsigned int& ndims, bool& fortran_order) {
char buffer[256];
fread(buffer,sizeof(char),11,fp);
std::string header = fgets(buffer,256,fp);
Expand All @@ -66,8 +66,7 @@ void cnpy::parse_npy_header(FILE* fp, unsigned int& word_size, unsigned int*& sh

//fortran order
loc1 = header.find("fortran_order")+16;
bool fortran_order = (header.substr(loc1,5) == "True" ? true : false);
assert(!fortran_order);
fortran_order = (header.substr(loc1,5) == "True" ? true : false);

//shape
loc1 = header.find("(");
Expand Down Expand Up @@ -121,14 +120,16 @@ void cnpy::parse_zip_footer(FILE* fp, unsigned short& nrecs, unsigned int& globa
cnpy::NpyArray load_the_npy_file(FILE* fp) {
unsigned int* shape;
unsigned int ndims, word_size;
cnpy::parse_npy_header(fp,word_size,shape,ndims);
bool fortran_order;
cnpy::parse_npy_header(fp,word_size,shape,ndims,fortran_order);
unsigned long long size = 1; //long long so no overflow when multiplying by word_size
for(int i = 0;i < ndims;i++) size *= shape[i];

cnpy::NpyArray arr;
arr.word_size = word_size;
arr.shape = std::vector<unsigned int>(shape,shape+ndims);
arr.data = new char[size*word_size];
arr.data = new char[size*word_size];
arr.fortran_order = fortran_order;
int nread = fread(arr.data,word_size,size,fp);
return arr;
}
Expand Down
7 changes: 5 additions & 2 deletions cnpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace cnpy {
char* data;
std::vector<unsigned int> shape;
unsigned int word_size;
bool fortran_order;
void destruct() {delete[] data;}
};

Expand All @@ -36,7 +37,7 @@ namespace cnpy {
char BigEndianTest();
char map_type(const std::type_info& t);
template<typename T> std::vector<char> create_npy_header(const T* data, const unsigned int* shape, const unsigned int ndims);
void parse_npy_header(FILE* fp,unsigned int& word_size, unsigned int*& shape, unsigned int& ndims);
void parse_npy_header(FILE* fp,unsigned int& word_size, unsigned int*& shape, unsigned int& ndims, bool& fortran_order);
void parse_zip_footer(FILE* fp, unsigned short& nrecs, unsigned int& global_header_size, unsigned int& global_header_offset);
npz_t npz_load(std::string fname);
NpyArray npz_load(std::string fname, std::string varname);
Expand Down Expand Up @@ -70,7 +71,9 @@ namespace cnpy {
//file exists. we need to append to it. read the header, modify the array size
unsigned int word_size, tmp_dims;
unsigned int* tmp_shape = 0;
parse_npy_header(fp,word_size,tmp_shape,tmp_dims);
bool fortran_order;
parse_npy_header(fp,word_size,tmp_shape,tmp_dims,fortran_order);
assert(!fortran_order);

if(word_size != sizeof(T)) {
std::cout<<"libnpy error: "<<fname<<" has word size "<<word_size<<" but npy_save appending data sized "<<sizeof(T)<<"\n";
Expand Down

0 comments on commit b230586

Please sign in to comment.