example1.cpp
Go to the documentation of this file.
1 #include"include/cnpy/cnpy.h"
2 #include<complex>
3 #include<cstdlib>
4 #include<iostream>
5 #include<map>
6 #include<string>
7 
8 const int Nx = 128;
9 const int Ny = 64;
10 const int Nz = 32;
11 
12 int main()
13 {
14  //set random seed so that result is reproducible (for testing)
15  srand(0);
16  //create random data
17  std::vector<std::complex<double>> data(Nx*Ny*Nz);
18  for(int i = 0;i < Nx*Ny*Nz;i++) data[i] = std::complex<double>(rand(),rand());
19 
20  //save it to file
21  cnpy::npy_save("arr1.npy",&data[0],{Nz,Ny,Nx},"w");
22 
23  //load it into a new array
24  cnpy::NpyArray arr = cnpy::npy_load("arr1.npy");
25  std::complex<double>* loaded_data = arr.data<std::complex<double>>();
26 
27  //make sure the loaded data matches the saved data
28  assert(arr.word_size == sizeof(std::complex<double>));
29  assert(arr.shape.size() == 3 && arr.shape[0] == Nz && arr.shape[1] == Ny && arr.shape[2] == Nx);
30  for(int i = 0; i < Nx*Ny*Nz;i++) assert(data[i] == loaded_data[i]);
31 
32  //append the same data to file
33  //npy array on file now has shape (Nz+Nz,Ny,Nx)
34  cnpy::npy_save("arr1.npy",&data[0],{Nz,Ny,Nx},"a");
35 
36  //now write to an npz file
37  //non-array variables are treated as 1D arrays with 1 element
38  double myVar1 = 1.2;
39  char myVar2 = 'a';
40  cnpy::npz_save("out.npz","myVar1",&myVar1,{1},"w"); //"w" overwrites any existing file
41  cnpy::npz_save("out.npz","myVar2",&myVar2,{1},"a"); //"a" appends to the file we created above
42  cnpy::npz_save("out.npz","arr1",&data[0],{Nz,Ny,Nx},"a"); //"a" appends to the file we created above
43 
44  //load a single var from the npz file
45  cnpy::NpyArray arr2 = cnpy::npz_load("out.npz","arr1");
46 
47  //load the entire npz file
48  cnpy::npz_t my_npz = cnpy::npz_load("out.npz");
49 
50  //check that the loaded myVar1 matches myVar1
51  cnpy::NpyArray arr_mv1 = my_npz["myVar1"];
52  double* mv1 = arr_mv1.data<double>();
53  assert(arr_mv1.shape.size() == 1 && arr_mv1.shape[0] == 1);
54  assert(mv1[0] == myVar1);
55 }
cnpy::npy_load
NpyArray npy_load(std::string fname)
Definition: cnpy.cpp:329
cnpy::NpyArray::data
T * data()
Definition: cnpy.h:37
cnpy::npz_t
std::map< std::string, NpyArray > npz_t
Definition: cnpy.h:63
cnpy::npy_save
void npy_save(std::string fname, const T *data, const std::vector< size_t > shape, std::string mode="w")
Definition: cnpy.h:88
cnpy::npz_load
npz_t npz_load(std::string fname)
Definition: cnpy.cpp:232
Nz
const int Nz
Definition: example1.cpp:10
cnpy::NpyArray
Definition: cnpy.h:24
main
int main()
Definition: example1.cpp:12
Ny
const int Ny
Definition: example1.cpp:9
Nx
const int Nx
Definition: example1.cpp:8
cnpy::NpyArray::shape
std::vector< size_t > shape
Definition: cnpy.h:57
cnpy.h
cnpy::npz_save
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")
Definition: cnpy.h:133


cnpy
Author(s): Carl Rogers, Peter Mitrano
autogenerated on Tue Mar 1 2022 23:57:46