// Test reading/writing netcdf files #include "IO/netcdf.h" #include "common/MPI_Helpers.h" #include "common/Communication.h" #include "common/UnitTest.h" #include "ProfilerApp.h" void load( const std::string& ); void test_NETCDF( UnitTest& ut ) { const int rank = comm_rank( MPI_COMM_WORLD ); const int size = comm_size( MPI_COMM_WORLD ); int nprocx = 2; int nprocy = 2; int nprocz = 2; RankInfoStruct info( rank, nprocx, nprocy, nprocz ); Array data( 4, 5, 6 ); for (size_t i=0; i dim = { (int) data.size(0)*nprocx, (int) data.size(1)*nprocy, (int) data.size(2)*nprocz }; int fid = netcdf::open( filename, netcdf::CREATE, MPI_COMM_WORLD ); auto dims = netcdf::defDim( fid, {"X", "Y", "Z"}, dim ); netcdf::write( fid, "tmp", dims, data, info ); netcdf::close( fid ); MPI_Barrier( MPI_COMM_WORLD ); // Read the contents of the file we created fid = netcdf::open( filename, netcdf::READ ); Array tmp = netcdf::getVar( fid, "tmp" ); if ( (int)tmp.size(0)!=dim[0] || (int)tmp.size(1)!=dim[1] || (int)tmp.size(2)!=dim[2] ) { ut.failure("Array sizes do not match"); return; } bool pass = true; for (size_t i=0; i size ) { printf(" Reading variable %s (%i",name.c_str(),(int)size[0]); for (size_t i=1; i vars = netcdf::getVarNames( fid ); for (size_t i=0; i tmp = netcdf::getVar( fid, vars[i] ); else if ( type == netcdf::SHORT ) Array tmp = netcdf::getVar( fid, vars[i] ); else Array tmp = netcdf::getVar( fid, vars[i] ); } std::vector attr = netcdf::getAttNames( fid ); for (size_t i=0; i tmp = netcdf::getAtt( fid, attr[i] ); else Array tmp = netcdf::getAtt( fid, attr[i] ); } netcdf::close( fid ); } int main(int argc, char **argv) { // Initialize MPI MPI_Init(&argc,&argv); int rank = comm_rank(MPI_COMM_WORLD); UnitTest ut; PROFILE_START("Main"); // Test reading existing netcdf files if ( rank==0 ) { for (int i=1; i