Functions
cnpy.cxx File Reference
#include "WireCellUtil/cnpy.h"
#include <complex>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <stdint.h>
#include <stdexcept>
#include <regex>

Go to the source code of this file.

Functions

cnpy::NpyArray load_the_npy_file (FILE *fp)
 
cnpy::NpyArray load_the_npz_array (FILE *fp, uint32_t compr_bytes, uint32_t uncompr_bytes)
 

Function Documentation

cnpy::NpyArray load_the_npy_file ( FILE *  fp)

Definition at line 195 of file cnpy.cxx.

195  {
196  std::vector<size_t> shape;
197  size_t word_size;
198  bool fortran_order;
199  cnpy::parse_npy_header(fp,word_size,shape,fortran_order);
200 
201  cnpy::NpyArray arr(shape, word_size, fortran_order);
202  size_t nread = fread(arr.data<char>(),1,arr.num_bytes(),fp);
203  if(nread != arr.num_bytes())
204  throw std::runtime_error("load_the_npy_file: failed fread");
205  return arr;
206 }
void parse_npy_header(FILE *fp, size_t &word_size, std::vector< size_t > &shape, bool &fortran_order)
Definition: cnpy.cxx:107
cnpy::NpyArray load_the_npz_array ( FILE *  fp,
uint32_t  compr_bytes,
uint32_t  uncompr_bytes 
)

Definition at line 208 of file cnpy.cxx.

208  {
209 
210  std::vector<unsigned char> buffer_compr(compr_bytes);
211  std::vector<unsigned char> buffer_uncompr(uncompr_bytes);
212  size_t nread = fread(&buffer_compr[0],1,compr_bytes,fp);
213  if(nread != compr_bytes)
214  throw std::runtime_error("load_the_npy_file: failed fread");
215 
216  z_stream d_stream;
217 
218  d_stream.zalloc = Z_NULL;
219  d_stream.zfree = Z_NULL;
220  d_stream.opaque = Z_NULL;
221  d_stream.avail_in = 0;
222  d_stream.next_in = Z_NULL;
223  inflateInit2(&d_stream, -MAX_WBITS);
224 
225  d_stream.avail_in = compr_bytes;
226  d_stream.next_in = &buffer_compr[0];
227  d_stream.avail_out = uncompr_bytes;
228  d_stream.next_out = &buffer_uncompr[0];
229 
230  inflate(&d_stream, Z_FINISH);
231  inflateEnd(&d_stream);
232 
233  std::vector<size_t> shape;
234  size_t word_size;
235  bool fortran_order;
236  cnpy::parse_npy_header(&buffer_uncompr[0],word_size,shape,fortran_order);
237 
238  cnpy::NpyArray array(shape, word_size, fortran_order);
239 
240  size_t offset = uncompr_bytes - array.num_bytes();
241  memcpy(array.data<unsigned char>(),&buffer_uncompr[0]+offset,array.num_bytes());
242 
243  return array;
244 }
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
void parse_npy_header(FILE *fp, size_t &word_size, std::vector< size_t > &shape, bool &fortran_order)
Definition: cnpy.cxx:107