void CONetCDF4::writeData_(int _grpid, int _varid, const std::vector<std::size_t> & _sstart, const std::vector<std::size_t> & _scount, const int * _data) { CheckError(nc_put_vara_int(_grpid, _varid, &(_sstart[0]), &(_scount[0]), _data)); }
int ncd_wdset(int ncid,char *path,char *name, void *val, \ enum ADIOS_TYPES type, int rank, \ struct adios_bp_dimension_struct *dims) { int i,valid,retval; char fullname[100]; char dimname[100]; fullname[0]='\0'; char *result=NULL; int dimids[2]; dimids[1]=0; strcpy(fullname,name); if(start[0]==0) { retval=nc_redef(ncid); sprintf(dimname,"%s_%d",name,0); retval=nc_inq_unlimdim(ncid,&dimids[1]); if(dimids[1]==-1)retval=nc_def_dim(ncid,"timesteps",NC_UNLIMITED,&dimids[0]); retval=nc_def_dim(ncid,dimname,dims[0].local_bound,&dimids[1]); if(type==adios_real) retval=nc_def_var(ncid,fullname,NC_FLOAT,2,dimids,&valid); if(type==adios_integer) retval=nc_def_var(ncid,fullname,NC_INT,2,dimids,&valid); if(type==adios_long) retval=nc_def_var(ncid,fullname,NC_LONG,2,dimids,&valid); if(type==adios_double) { retval=nc_def_var(ncid,fullname,NC_DOUBLE,2,dimids,&valid); ERR(retval); } //printf("\t RANK=%d, DIMS:%s[0]=%d dimids[1]=%d\n",rank,dimname,dims[0].local_bound,dimids[1]); retval=nc_enddef(ncid); } else retval=nc_inq_varid (ncid, fullname, &valid); start[1]=0;//dims[0].local_bound; count[0]=1; count[1]=dims[0].local_bound; if(type==adios_double) retval=nc_put_vara_double(ncid,valid,start,count,val); if(type==adios_real) retval=nc_put_vara_float(ncid,valid,start,count,val); if(type==adios_integer) retval=nc_put_vara_int(ncid,valid,start,count,val); if(type==adios_long) retval=nc_put_vara_long(ncid,valid,start,count,val); //#if DEBUG //printf("create dataset:%s\n",fullname); //printf("start:%dx%d. count:%dx%d\n",start[0],start[1],count[0],count[1]); //printf("-------------------\n"); //#endif return; }
// Traj_AmberNetcdf::writeReservoir() TODO: Make Frame const& int Traj_AmberNetcdf::writeReservoir(int set, Frame const& frame, double energy, int bin) { start_[0] = ncframe_; start_[1] = 0; start_[2] = 0; count_[0] = 1; count_[1] = Ncatom(); count_[2] = 3; // Coords DoubleToFloat(Coord_, frame.xAddress()); if (checkNCerr(nc_put_vara_float(ncid_,coordVID_,start_,count_,Coord_)) ) { mprinterr("Error: Netcdf writing reservoir coords %i\n",set); return 1; } // Velo if (velocityVID_ != -1) { if (frame.vAddress() == 0) { // TODO: Make it so this can NEVER happen. mprinterr("Error: Reservoir expects velocities, but no velocities in frame.\n"); return 1; } DoubleToFloat(Coord_, frame.vAddress()); if (checkNCerr(nc_put_vara_float(ncid_,velocityVID_,start_,count_,Coord_)) ) { mprinterr("Error: Netcdf writing reservoir velocities %i\n",set); return 1; } } // Eptot, bins if ( checkNCerr( nc_put_vara_double(ncid_,eptotVID_,start_,count_,&energy)) ) { mprinterr("Error: Writing eptot.\n"); return 1; } if (binsVID_ != -1) { if ( checkNCerr( nc_put_vara_int(ncid_,binsVID_,start_,count_,&bin)) ) { mprinterr("Error: Writing bins.\n"); return 1; } } // Write box if (cellLengthVID_ != -1) { count_[1] = 3; count_[2] = 0; if (checkNCerr(nc_put_vara_double(ncid_,cellLengthVID_,start_,count_,frame.bAddress())) ) { mprinterr("Error: Writing cell lengths.\n"); return 1; } if (checkNCerr(nc_put_vara_double(ncid_,cellAngleVID_,start_,count_, frame.bAddress()+3)) ) { mprinterr("Error: Writing cell angles.\n"); return 1; } } nc_sync(ncid_); // Necessary after every write?? ++ncframe_; return 0; }
/* test extending variables */ int test_pio_extend(int flag){ int rank, procs; int ncFile; int ncDimPart; int ncDimVrtx; int ncVarVrtx; int dimsVrtx[2]; size_t start[2]; size_t count[2]; int vertices[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &procs); /* Create netcdf file */ if (nc_create_par("test.nc", NC_NETCDF4 | NC_MPIIO, MPI_COMM_WORLD, MPI_INFO_NULL, &ncFile)) ERR; /* Create netcdf dimensions */ if (nc_def_dim(ncFile, "partitions", procs, &ncDimPart)) ERR; if (nc_def_dim(ncFile, "vertices", NC_UNLIMITED, &ncDimVrtx)) ERR; /* Create netcdf variables */ dimsVrtx[0] = ncDimPart; dimsVrtx[1] = ncDimVrtx; if (nc_def_var(ncFile, "vertex", NC_INT, 2, dimsVrtx, &ncVarVrtx)) ERR; /* Start writing data */ if (nc_enddef(ncFile)) ERR; /* Set access mode */ if (nc_var_par_access(ncFile, ncVarVrtx, flag)) ERR; /* Write vertices */ start[0] = rank; start[1] = 0; count[0] = 1; count[1] = rank; if (nc_put_vara_int(ncFile, ncVarVrtx, start, count, vertices)) ERR; /* Close netcdf file */ if (nc_close(ncFile)) ERR; return 0; }
int ncd_wscalar(int ncid,char *path,char *name, void *val, \ enum ADIOS_TYPES type) { int i,valid,retval; char fullname[100]; char dimname[100]; fullname[0]='\0'; char *result=NULL; int dimids[1]; dimids[0]=0; strcpy(fullname,name); if(start[0]==0) { retval=nc_redef(ncid); sprintf(dimname,"%s_%d",name,0); retval=nc_inq_unlimdim(ncid,&dimids[0]); if(dimids[0]==-1)retval=nc_def_dim(ncid,"timesteps",NC_UNLIMITED,&dimids[0]); if(type==adios_real) retval=nc_def_var(ncid,fullname,NC_FLOAT,1,dimids,&valid); else if(type==adios_double) retval=nc_def_var(ncid,fullname,NC_DOUBLE,1,dimids,&valid); else if(type==adios_integer) retval=nc_def_var(ncid,fullname,NC_INT,1,dimids,&valid); else if(type==adios_long) retval=nc_def_var(ncid,fullname,NC_LONG,1,dimids,&valid); retval=nc_enddef(ncid); } else retval=nc_inq_varid (ncid, fullname, &valid); if(type==adios_real) retval=nc_put_vara_float(ncid,valid,start_scalar,count_scalar,val); else if(type==bp_double) retval=nc_put_vara_double(ncid,valid,start_scalar,count_scalar,val); else if(type==adios_integer) retval=nc_put_vara_int(ncid,valid,start_scalar,count_scalar,val); else if(type==adios_long) retval=nc_put_vara_long(ncid,valid,start_scalar,count_scalar,val); ERR(retval); return 0; }
int ncd_wdset1(int ncid,char *path,char *name, void *val, \ enum ADIOS_TYPES type, int rank, \ struct adios_bp_dimension_struct *dims) { int i,valid,retval; char fullname[100]; char dimname[100]; fullname[0]='\0'; char *result=NULL; int dimids[1]; strcpy(fullname,name); retval=nc_redef(ncid); sprintf(dimname,"%s_%d",name,0); retval=nc_def_dim(ncid,dimname,dims[0].local_bound,&dimids[1]); if(type==adios_real) retval=nc_def_var(ncid,fullname,NC_FLOAT,1,dimids,&valid); if(type==adios_integer) retval=nc_def_var(ncid,fullname,NC_INT,1,dimids,&valid); if(type==adios_long) retval=nc_def_var(ncid,fullname,NC_LONG,1,dimids,&valid); if(type==adios_double) { retval=nc_def_var(ncid,fullname,NC_DOUBLE,1,dimids,&valid); ERR(retval); } retval=nc_enddef(ncid); size_t a[1],b[1]; a[0]=0; b[0]=dims[0].local_bound; if(type==adios_double) retval=nc_put_vara_double(ncid,valid,a,b,val); if(type==adios_real) retval=nc_put_vara_float(ncid,valid,a,b,val); if(type==adios_integer) retval=nc_put_vara_int(ncid,valid,a,b,val); if(type==adios_long) retval=nc_put_vara_long(ncid,valid,a,b,val); return; }
void ITLRandomField::_AddTimeStamp( int iTimeStamp ) { // ADD-BY-LEETEN 09/01/2011-BEGIN if( iNcId >= 0 ) { // write the time stamp #ifndef WITH_PNETCDF // ADD-BY-LEETEN 08/12/2011 size_t uStart = viTimeStamps.size(); size_t uCount = 1; ASSERT_NETCDF(nc_put_vara_int( iNcId, iNcTimeVarId, &uStart, &uCount, &iTimeStamp)); // ADD-BY-LEETEN 08/12/2011-BEGIN #else // #ifndef WITH_PNETCDF MPI_Offset uStart = viTimeStamps.size(); MPI_Offset uCount = 1; ASSERT_NETCDF(ncmpi_begin_indep_data(iNcId)); if( 0 == iRank ) ASSERT_NETCDF(ncmpi_put_vara_int( iNcId, iNcTimeVarId, &uStart, &uCount, &iTimeStamp)); ASSERT_NETCDF(ncmpi_end_indep_data(iNcId)); #endif // #ifndef WITH_PNETCDF // ADD-BY-LEETEN 08/12/2011-END } // ADD-BY-LEETEN 09/01/2011-END viTimeStamps.push_back(iTimeStamp); }
/********************************************************************* void mpp_put_var_value_block(int fid, int vid, const size_t *start, const size_t *nread, void *data) read part of var data, the part is defined by start and nread. *********************************************************************/ void mpp_put_var_value_block(int fid, int vid, const size_t *start, const size_t *nwrite, const void *data) { int status; char errmsg[512]; if( mpp_pe() != mpp_root_pe() ) return; if(fid<0 || fid >=nfiles) mpp_error("mpp_io(mpp_put_var_value_block): invalid fid number, fid should be " "a nonnegative integer that less than nfiles"); if(vid<0 || vid >=files[fid].nvar) mpp_error("mpp_io(mpp_put_var_value_block): invalid vid number, vid should be " "a nonnegative integer that less than nvar"); switch(files[fid].var[vid].type) { case NC_DOUBLE:case NC_FLOAT: status = nc_put_vara_double(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; case NC_INT: status = nc_put_vara_int(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; case NC_SHORT: status = nc_put_vara_short(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; case NC_CHAR: status = nc_put_vara_text(files[fid].ncid, files[fid].var[vid].fldid, start, nwrite, data); break; default: sprintf(errmsg, "mpp_io(mpp_put_var_value_block): field %s in file %s has an invalid type, " "the type should be NC_DOUBLE, NC_FLOAT, NC_INT, NC_SHORT or NC_CHAR", files[fid].var[vid].name, files[fid].name ); mpp_error(errmsg); } if(status != NC_NOERR) { sprintf(errmsg, "mpp_io(mpp_put_var_value_block): Error in putting value of variable %s from file %s", files[fid].var[vid].name, files[fid].name ); netcdf_error(errmsg, status); } }; /* mpp_put_var_value_block */
void R_nc_put_vara_int( int *ncid, int *varid, int *start, int *count, int *data, int *retval ) { int i, ndims, err; size_t s_start[MAX_NC_DIMS], s_count[MAX_NC_DIMS]; /* Get # of dims for this var */ err = nc_inq_ndims( *ncid, &ndims ); if( err != NC_NOERR ) REprintf( "Error on nc_inq_ndims call in R_nc_put_vara_int: %s\n", nc_strerror(*retval) ); /* Copy over from ints to size_t */ for( i=0; i<ndims; i++ ) { s_start[i] = (size_t)start[i]; s_count[i] = (size_t)count[i]; } *retval = nc_put_vara_int(*ncid, *varid, s_start, s_count, data ); if( *retval != NC_NOERR ) REprintf( "Error in R_nc_put_vara_int: %s\n", nc_strerror(*retval) ); }
void sdatio_write_variable_private(struct sdatio_file * sfile, struct sdatio_variable * svar, size_t * counts, size_t * starts, void * address){ int retval; /*if (sfile->is_parallel){}*/ /*else {*/ switch (svar->type){ case (SDATIO_INT): DEBUG_MESS("Writing an integer\n"); if ((retval = nc_put_vara_int(sfile->nc_file_id, svar->nc_id, starts, counts, address))) ERR(retval); break; case (SDATIO_FLOAT): DEBUG_MESS("Writing a float\n"); /*if ((retval = nc_put_var_double(sfile->nc_file_id, svar->nc_id, address))) ERR(retval);*/ if ((retval = nc_put_vara_float(sfile->nc_file_id, svar->nc_id, starts, counts, address))) ERR(retval); break; case (SDATIO_DOUBLE): DEBUG_MESS("Writing a double\n"); /*if ((retval = nc_put_var_double(sfile->nc_file_id, svar->nc_id, address))) ERR(retval);*/ if ((retval = nc_put_vara_double(sfile->nc_file_id, svar->nc_id, starts, counts, address))) ERR(retval); break; } /*}*/ sfile->data_written = 1; }
void nc_put_vara(TYPE type,int ncid,int varid,const size_t start[],const size_t count[],const void *dp){ switch(type){ case(BYTE): nc_put_vara_uchar(ncid,varid,start,count,(const unsigned char *)dp); break; case(SHORT): nc_put_vara_short(ncid,varid,start,count,(const short *)dp); break; case(INT): nc_put_vara_int(ncid,varid,start,count,(const int *)dp); break; case(LONG): nc_put_vara_long(ncid,varid,start,count,(const long *)dp); break; case(FLOAT): nc_put_vara_float(ncid,varid,start,count,(const float *)dp); break; case(DOUBLE): nc_put_vara_double(ncid,varid,start,count,(const double *)dp); break; default: printf("unkown types in nc_put_vara()!\n"); } }
// Define function to output NetCDF dependent variables void NcOutput(int const time) { // Declare where to start writing data for each dimension size_t const startArray[INDEPENDENT_VAR_COUNT] = { time, // Start at the current time step 0, 0 // Start at coordinate (0, 0) }; // Declare how much data to write in each dimension size_t const countArray[INDEPENDENT_VAR_COUNT] = { 1, // Write one time step of data ENVIRONMENT_LENGTH, ENVIRONMENT_LENGTH // Write all positions in the // environment }; // Output the C array into the NetCDF variable TryNc(nc_put_vara_int(NcId, // NetCDF dataset ID StateVarId, // NetCDF variable ID startArray, // Where to start writing data in each // dimension countArray, // How much data to write in each // dimension &(Environment[0][0]))); // C array of data to output to // the NetCDF variable }
// NetcdfFile::NC_create() int NetcdfFile::NC_create(std::string const& Name, NCTYPE type, int natomIn, CoordinateInfo const& coordInfo, std::string const& title) { if (Name.empty()) return 1; int dimensionID[NC_MAX_VAR_DIMS]; int NDIM; nc_type dataType; if (ncdebug_>1) mprintf("DEBUG: NC_create: %s natom=%i V=%i F=%i box=%i temp=%i time=%i\n", Name.c_str(),natomIn,(int)coordInfo.HasVel(), (int)coordInfo.HasForce(),(int)coordInfo.HasBox(), (int)coordInfo.HasTemp(),(int)coordInfo.HasTime()); if ( checkNCerr( nc_create( Name.c_str(), NC_64BIT_OFFSET, &ncid_) ) ) return 1; ncatom_ = natomIn; ncatom3_ = ncatom_ * 3; // Set number of dimensions based on file type switch (type) { case NC_AMBERENSEMBLE: NDIM = 4; dataType = NC_FLOAT; break; case NC_AMBERTRAJ: NDIM = 3; dataType = NC_FLOAT; break; case NC_AMBERRESTART: NDIM = 2; dataType = NC_DOUBLE; break; default: mprinterr("Error: NC_create (%s): Unrecognized type (%i)\n",Name.c_str(),(int)type); return 1; } if (type == NC_AMBERENSEMBLE) { // Ensemble dimension for ensemble if (coordInfo.EnsembleSize() < 1) { mprinterr("Internal Error: NetcdfFile: ensembleSize < 1\n"); return 1; } if ( checkNCerr(nc_def_dim(ncid_, NCENSEMBLE, coordInfo.EnsembleSize(), &ensembleDID_)) ) { mprinterr("Error: Defining ensemble dimension.\n"); return 1; } dimensionID[1] = ensembleDID_; } ncframe_ = 0; if (type == NC_AMBERTRAJ || type == NC_AMBERENSEMBLE) { // Frame dimension for traj if ( checkNCerr( nc_def_dim( ncid_, NCFRAME, NC_UNLIMITED, &frameDID_)) ) { mprinterr("Error: Defining frame dimension.\n"); return 1; } // Since frame is UNLIMITED, it must be lowest dim. dimensionID[0] = frameDID_; } // Time variable and units if (coordInfo.HasTime()) { if ( checkNCerr( nc_def_var(ncid_, NCTIME, dataType, NDIM-2, dimensionID, &timeVID_)) ) { mprinterr("Error: Defining time variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text(ncid_, timeVID_, "units", 10, "picosecond")) ) { mprinterr("Error: Writing time VID units.\n"); return 1; } } // Spatial dimension and variable if ( checkNCerr( nc_def_dim( ncid_, NCSPATIAL, 3, &spatialDID_)) ) { mprinterr("Error: Defining spatial dimension.\n"); return 1; } dimensionID[0] = spatialDID_; if ( checkNCerr( nc_def_var( ncid_, NCSPATIAL, NC_CHAR, 1, dimensionID, &spatialVID_)) ) { mprinterr("Error: Defining spatial variable.\n"); return 1; } // Atom dimension if ( checkNCerr( nc_def_dim( ncid_, NCATOM, ncatom_, &atomDID_)) ) { mprinterr("Error: Defining atom dimension.\n"); return 1; } // Setup dimensions for Coords/Velocity // NOTE: THIS MUST BE MODIFIED IF NEW TYPES ADDED if (type == NC_AMBERENSEMBLE) { dimensionID[0] = frameDID_; dimensionID[1] = ensembleDID_; dimensionID[2] = atomDID_; dimensionID[3] = spatialDID_; } else if (type == NC_AMBERTRAJ) { dimensionID[0] = frameDID_; dimensionID[1] = atomDID_; dimensionID[2] = spatialDID_; } else { dimensionID[0] = atomDID_; dimensionID[1] = spatialDID_; } // Coord variable if ( checkNCerr( nc_def_var( ncid_, NCCOORDS, dataType, NDIM, dimensionID, &coordVID_)) ) { mprinterr("Error: Defining coordinates variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, coordVID_, "units", 8, "angstrom")) ) { mprinterr("Error: Writing coordinates variable units.\n"); return 1; } // Velocity variable if (coordInfo.HasVel()) { if ( checkNCerr( nc_def_var( ncid_, NCVELO, dataType, NDIM, dimensionID, &velocityVID_)) ) { mprinterr("Error: Defining velocities variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, velocityVID_, "units", 19, "angstrom/picosecond")) ) { mprinterr("Error: Writing velocities variable units.\n"); return 1; } if ( checkNCerr( nc_put_att_double( ncid_, velocityVID_, "scale_factor", NC_DOUBLE, 1, &Constants::AMBERTIME_TO_PS)) ) { mprinterr("Error: Writing velocities scale factor.\n"); return 1; } } // Force variable if (coordInfo.HasForce()) { if ( checkNCerr( nc_def_var( ncid_, NCFRC, dataType, NDIM, dimensionID, &frcVID_)) ) { mprinterr("Error: Defining forces variable\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, frcVID_, "units", 25, "kilocalorie/mole/angstrom")) ) { mprinterr("Error: Writing forces variable units.\n"); return 1; } } // Replica Temperature if (coordInfo.HasTemp()) { // NOTE: Setting dimensionID should be OK for Restart, will not be used. dimensionID[0] = frameDID_; if ( NC_defineTemperature( dimensionID, NDIM-2 ) ) return 1; } // Replica indices int remDimTypeVID = -1; if (coordInfo.HasReplicaDims()) { // Define number of replica dimensions remd_dimension_ = coordInfo.ReplicaDimensions().Ndims(); int remDimDID = -1; if ( checkNCerr(nc_def_dim(ncid_, NCREMD_DIMENSION, remd_dimension_, &remDimDID)) ) { mprinterr("Error: Defining replica indices dimension.\n"); return 1; } dimensionID[0] = remDimDID; // For each dimension, store the type if ( checkNCerr(nc_def_var(ncid_, NCREMD_DIMTYPE, NC_INT, 1, dimensionID, &remDimTypeVID)) ) { mprinterr("Error: Defining replica dimension type variable.\n"); return 1; } // Need to store the indices of replica in each dimension each frame // NOTE: THIS MUST BE MODIFIED IF NEW TYPES ADDED if (type == NC_AMBERENSEMBLE) { dimensionID[0] = frameDID_; dimensionID[1] = ensembleDID_; dimensionID[2] = remDimDID; } else if (type == NC_AMBERTRAJ) { dimensionID[0] = frameDID_; dimensionID[1] = remDimDID; } else { dimensionID[0] = remDimDID; } if (checkNCerr(nc_def_var(ncid_, NCREMD_INDICES, NC_INT, NDIM-1, dimensionID, &indicesVID_))) { mprinterr("Error: Defining replica indices variable ID.\n"); return 1; } // TODO: Determine if groups are really necessary for restarts. If not, // remove from AmberNetcdf.F90. } // Box Info if (coordInfo.HasBox()) { // Cell Spatial if ( checkNCerr( nc_def_dim( ncid_, NCCELL_SPATIAL, 3, &cell_spatialDID_)) ) { mprinterr("Error: Defining cell spatial dimension.\n"); return 1; } dimensionID[0] = cell_spatialDID_; if ( checkNCerr( nc_def_var(ncid_, NCCELL_SPATIAL, NC_CHAR, 1, dimensionID, &cell_spatialVID_))) { mprinterr("Error: Defining cell spatial variable.\n"); return 1; } // Cell angular if ( checkNCerr( nc_def_dim( ncid_, NCLABEL, NCLABELLEN, &labelDID_)) ) { mprinterr("Error: Defining label dimension.\n"); return 1; } if ( checkNCerr( nc_def_dim( ncid_, NCCELL_ANGULAR, 3, &cell_angularDID_)) ) { mprinterr("Error: Defining cell angular dimension.\n"); return 1; } dimensionID[0] = cell_angularDID_; dimensionID[1] = labelDID_; if ( checkNCerr( nc_def_var( ncid_, NCCELL_ANGULAR, NC_CHAR, 2, dimensionID, &cell_angularVID_)) ) { mprinterr("Error: Defining cell angular variable.\n"); return 1; } // Setup dimensions for Box // NOTE: This must be modified if more types added int boxdim; if (type == NC_AMBERENSEMBLE) { dimensionID[0] = frameDID_; dimensionID[1] = ensembleDID_; boxdim = 2; } else if (type == NC_AMBERTRAJ) { dimensionID[0] = frameDID_; boxdim = 1; } else { boxdim = 0; } dimensionID[boxdim] = cell_spatialDID_; if ( checkNCerr( nc_def_var( ncid_, NCCELL_LENGTHS, NC_DOUBLE, NDIM-1, dimensionID, &cellLengthVID_)) ) { mprinterr("Error: Defining cell length variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, cellLengthVID_, "units", 8, "angstrom")) ) { mprinterr("Error: Writing cell length variable units.\n"); return 1; } dimensionID[boxdim] = cell_angularDID_; if ( checkNCerr( nc_def_var( ncid_, NCCELL_ANGLES, NC_DOUBLE, NDIM-1, dimensionID, &cellAngleVID_)) ) { mprinterr("Error: Defining cell angle variable.\n"); return 1; } if ( checkNCerr( nc_put_att_text( ncid_, cellAngleVID_, "units", 6, "degree")) ) { mprinterr("Error: Writing cell angle variable units.\n"); return 1; } } // Attributes if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"title",title.size(),title.c_str())) ) { mprinterr("Error: Writing title.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"application",5,"AMBER")) ) { mprinterr("Error: Writing application.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"program",7,"cpptraj")) ) { mprinterr("Error: Writing program.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"programVersion", NETCDF_VERSION_STRLEN, NETCDF_VERSION_STRING)) ) { mprinterr("Error: Writing program version.\n"); return 1; } // TODO: Make conventions a static string bool errOccurred = false; if ( type == NC_AMBERENSEMBLE ) errOccurred = checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"Conventions",13,"AMBERENSEMBLE")); else if ( type == NC_AMBERTRAJ ) errOccurred = checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"Conventions",5,"AMBER")); else errOccurred = checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"Conventions",12,"AMBERRESTART")); if (errOccurred) { mprinterr("Error: Writing conventions.\n"); return 1; } if (checkNCerr(nc_put_att_text(ncid_,NC_GLOBAL,"ConventionVersion",3,"1.0")) ) { mprinterr("Error: Writing conventions version.\n"); return 1; } // Set fill mode if (checkNCerr(nc_set_fill(ncid_, NC_NOFILL, dimensionID))) { mprinterr("Error: NetCDF setting fill value.\n"); return 1; } // End netcdf definitions if (checkNCerr(nc_enddef(ncid_))) { mprinterr("NetCDF error on ending definitions."); return 1; } // Specify spatial dimension labels start_[0] = 0; count_[0] = 3; char xyz[3]; xyz[0] = 'x'; xyz[1] = 'y'; xyz[2] = 'z'; if (checkNCerr(nc_put_vara_text(ncid_, spatialVID_, start_, count_, xyz))) { mprinterr("Error on NetCDF output of spatial VID 'x', 'y' and 'z'"); return 1; } if ( coordInfo.HasBox() ) { xyz[0] = 'a'; xyz[1] = 'b'; xyz[2] = 'c'; if (checkNCerr(nc_put_vara_text(ncid_, cell_spatialVID_, start_, count_, xyz))) { mprinterr("Error on NetCDF output of cell spatial VID 'a', 'b' and 'c'"); return 1; } char abc[15] = { 'a', 'l', 'p', 'h', 'a', 'b', 'e', 't', 'a', ' ', 'g', 'a', 'm', 'm', 'a' }; start_[0] = 0; start_[1] = 0; count_[0] = 3; count_[1] = NCLABELLEN; if (checkNCerr(nc_put_vara_text(ncid_, cell_angularVID_, start_, count_, abc))) { mprinterr("Error on NetCDF output of cell angular VID 'alpha', 'beta ' and 'gamma'"); return 1; } } // Store the type of each replica dimension. if (coordInfo.HasReplicaDims()) { ReplicaDimArray const& remdDim = coordInfo.ReplicaDimensions(); start_[0] = 0; count_[0] = remd_dimension_; int* tempDims = new int[ remd_dimension_ ]; for (int i = 0; i < remd_dimension_; ++i) tempDims[i] = remdDim[i]; if (checkNCerr(nc_put_vara_int(ncid_, remDimTypeVID, start_, count_, tempDims))) { mprinterr("Error: writing replica dimension types.\n"); delete[] tempDims; return 1; } delete[] tempDims; } return 0; }
/* test different hyperslab settings */ int test_pio_hyper(int flag){ /* MPI stuff. */ int mpi_size, mpi_rank; int res = NC_NOERR; MPI_Comm comm = MPI_COMM_WORLD; MPI_Info info = MPI_INFO_NULL; /* Netcdf-4 stuff. */ int ncid; int nvid; int rvid; int j, i; /* two dimensional integer data test */ int dimids[NDIMS1]; size_t start[NDIMS1], count[NDIMS1]; int *data; int *tempdata; int *rdata; int *temprdata; int count_atom; /* Initialize MPI. */ MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); if(mpi_size == 1) return 0; /* Create a parallel netcdf-4 file. */ /* nc_set_log_level(NC_TURN_OFF_LOGGING); */ /* nc_set_log_level(4);*/ if (nc_create_par(file_name, facc_type, comm, info, &ncid)) ERR; /* The case is two dimensional variables, no unlimited dimension */ /* Create two dimensions. */ if (nc_def_dim(ncid, "d1", DIMSIZE2, dimids)) ERR; if (nc_def_dim(ncid, "d2", DIMSIZE, &dimids[1])) ERR; /* Create one var. */ if (nc_def_var(ncid, "v1", NC_INT, NDIMS1, dimids, &nvid)) ERR; if (nc_enddef(ncid)) ERR; /* hyperslab illustration for 3-processor case -------- |aaaacccc| |aaaacccc| |bbbb | |bbbb | -------- */ /* odd number of processors should be treated differently */ if(mpi_size%2 != 0) { count_atom = DIMSIZE*2/(mpi_size+1); if(mpi_rank <= mpi_size/2) { start[0] = 0; start[1] = mpi_rank*count_atom; count[0] = DIMSIZE2/2; count[1] = count_atom; } else { start[0] = DIMSIZE2/2; start[1] = (mpi_rank-mpi_size/2-1)*count_atom; count[0] = DIMSIZE2/2; count[1] = count_atom; } } else { count_atom = DIMSIZE*2/mpi_size; if(mpi_rank < mpi_size/2) { start[0] = 0; start[1] = mpi_rank*count_atom; count[0] = DIMSIZE2/2; count[1] = count_atom; } else { start[0] = DIMSIZE2/2; start[1] = (mpi_rank-mpi_size/2)*count_atom; count[0] = DIMSIZE2/2; count[1] = count_atom; } } if (nc_var_par_access(ncid, nvid, flag)) ERR; data = malloc(sizeof(int)*count[1]*count[0]); tempdata = data; for (j=0; j<count[0];j++){ for (i=0; i<count[1]; i++){ *tempdata = mpi_rank*(j+1); tempdata ++; } } if (nc_put_vara_int(ncid, nvid, start, count, data)) ERR; free(data); /* Close the netcdf file. */ if (nc_close(ncid)) ERR; if (nc_open_par(file_name, facc_type_open, comm, info, &ncid)) ERR; /* Inquiry the variable */ if (nc_inq_varid(ncid, "v1", &rvid)) ERR; if (nc_var_par_access(ncid, rvid, flag)) ERR; rdata = malloc(sizeof(int)*count[1]*count[0]); /* Read the data with the same slab settings */ if (nc_get_vara_int(ncid, rvid, start, count, rdata)) ERR; temprdata = rdata; for (j=0; j<count[0];j++){ for (i=0; i<count[1]; i++){ if(*temprdata != mpi_rank*(j+1)) { res = -1; break; } temprdata++; } } free(rdata); if(res == -1) ERR_RET; /* Close the netcdf file. */ if (nc_close(ncid)) ERR; return 0; }
int test_pio_attr(int flag) { /* MPI stuff. */ int mpi_size, mpi_rank; MPI_Comm comm = MPI_COMM_WORLD; MPI_Info info = MPI_INFO_NULL; /* Netcdf-4 stuff. */ int ncid; int nvid; int j, i; double rh_range[2]; static char title[] = "parallel attr to netCDF"; nc_type st_type,vr_type; size_t vr_len,st_len; size_t orivr_len; double *vr_val; char *st_val; /* two dimensional integer data*/ int dimids[NDIMS1]; size_t start[NDIMS1]; size_t count[NDIMS1]; int *data; int *tempdata; /* Initialize MPI. */ MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); /* Create a parallel netcdf-4 file. */ /* nc_set_log_level(NC_TURN_OFF_LOGGING); */ /* nc_set_log_level(3);*/ if (nc_create_par(file_name, facc_type, comm, info, &ncid)) ERR; /* Create a 2-D variable so that an attribute can be added. */ if (nc_def_dim(ncid, "d1", DIMSIZE2, dimids)) ERR; if (nc_def_dim(ncid, "d2", DIMSIZE, &dimids[1])) ERR; /* Create one var. */ if (nc_def_var(ncid, "v1", NC_INT, NDIMS1, dimids, &nvid)) ERR; orivr_len = 2; rh_range[0] = 1.0; rh_range[1] = 1000.0; /* Write attributes of a variable */ if (nc_put_att_double (ncid, nvid, "valid_range", NC_DOUBLE, orivr_len, rh_range)) ERR; if (nc_put_att_text (ncid, nvid, "title", strlen(title), title)) ERR; /* Write global attributes */ if (nc_put_att_double (ncid, NC_GLOBAL, "g_valid_range", NC_DOUBLE, orivr_len, rh_range)) ERR; if (nc_put_att_text (ncid, NC_GLOBAL, "g_title", strlen(title), title)) ERR; if (nc_enddef(ncid)) ERR; /* Set up slab for this process. */ start[0] = 0; start[1] = mpi_rank * DIMSIZE/mpi_size; count[0] = DIMSIZE2; count[1] = DIMSIZE/mpi_size; /* Access parallel */ if (nc_var_par_access(ncid, nvid, flag)) ERR; /* Allocating data */ data = malloc(sizeof(int)*count[1]*count[0]); tempdata = data; for(j = 0; j < count[0]; j++) for (i = 0; i < count[1]; i++) { *tempdata = mpi_rank * (j + 1); tempdata++; } if (nc_put_vara_int(ncid, nvid, start, count, data)) ERR; free(data); /* Close the netcdf file. */ if (nc_close(ncid)) ERR; /* Read attributes */ if (nc_open_par(file_name, facc_type_open, comm, info, &ncid)) ERR; /* Set up slab for this process. */ start[0] = 0; start[1] = mpi_rank * DIMSIZE/mpi_size; count[0] = DIMSIZE2; count[1] = DIMSIZE/mpi_size; /* Inquiry variable */ if (nc_inq_varid(ncid, "v1", &nvid)) ERR; /* Access parallel */ if (nc_var_par_access(ncid, nvid, flag)) ERR; /* Inquiry attribute */ if (nc_inq_att (ncid, nvid, "valid_range", &vr_type, &vr_len)) ERR; /* check stuff */ if(vr_type != NC_DOUBLE || vr_len != orivr_len) ERR; vr_val = (double *) malloc(vr_len * sizeof(double)); /* Get variable attribute values */ if (nc_get_att_double(ncid, nvid, "valid_range", vr_val)) ERR; /* Check variable attribute value */ for(i = 0; i < vr_len; i++) if (vr_val[i] != rh_range[i]) ERR_RET; free(vr_val); /* Inquiry global attribute */ if (nc_inq_att (ncid, NC_GLOBAL, "g_valid_range", &vr_type, &vr_len)) ERR; /* Check stuff. */ if(vr_type != NC_DOUBLE || vr_len != orivr_len) ERR; /* Obtain global attribute value */ vr_val = (double *) malloc(vr_len * sizeof(double)); if (nc_get_att_double(ncid, NC_GLOBAL, "g_valid_range", vr_val)) ERR; /* Check global attribute value */ for(i = 0; i < vr_len; i++) if (vr_val[i] != rh_range[i]) ERR_RET; free(vr_val); /* Inquiry string attribute of a variable */ if (nc_inq_att (ncid, nvid, "title", &st_type, &st_len)) ERR; /* check string attribute length */ if(st_len != strlen(title)) ERR_RET; /* Check string attribute type */ if(st_type != NC_CHAR) ERR_RET; /* Allocate meory for string attribute */ st_val = (char *) malloc(st_len * (sizeof(char))); /* Obtain variable string attribute value */ if (nc_get_att_text(ncid, nvid,"title", st_val)) ERR; /*check string value */ if(strncmp(st_val,title,st_len)) { free(st_val); ERR_RET; } free(st_val); /*Inquiry global attribute */ if (nc_inq_att (ncid, NC_GLOBAL, "g_title", &st_type, &st_len)) ERR; /* check attribute length*/ if(st_len != strlen(title)) ERR_RET; /*check attribute type*/ if(st_type != NC_CHAR) ERR_RET; /* obtain global string attribute value */ st_val = (char*)malloc(st_len*sizeof(char)); if (nc_get_att_text(ncid, NC_GLOBAL,"g_title", st_val)) ERR; /* check attribute value */ if(strncmp(st_val,title,st_len)){ free(st_val); ERR_RET; } free(st_val); /* Close the netcdf file. */ if (nc_close(ncid)) ERR; return 0; }
/* Both read and write will be tested */ int test_pio(int flag) { /* MPI stuff. */ int mpi_size, mpi_rank; MPI_Comm comm = MPI_COMM_WORLD; MPI_Info info = MPI_INFO_NULL; /* Netcdf-4 stuff. */ int ncid; int nvid,uvid; int rvid; unsigned m,k,j,i; /* two dimensional integer data test */ int dimids[NDIMS1]; size_t start[NDIMS1]; size_t count[NDIMS1]; int *data; int *tempdata; int *rdata; int *temprdata; /* four dimensional integer data test, time dimension is unlimited.*/ int dimuids[NDIMS2]; size_t ustart[NDIMS2]; size_t ucount[NDIMS2]; int *udata; int *tempudata; int *rudata; int *temprudata; /* Initialize MPI. */ MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); /* Create a parallel netcdf-4 file. */ if (nc_create_par(file_name, facc_type, comm, info, &ncid)) ERR; /* The first case is two dimensional variables, no unlimited dimension */ /* Create two dimensions. */ if (nc_def_dim(ncid, "d1", DIMSIZE2, dimids)) ERR; if (nc_def_dim(ncid, "d2", DIMSIZE, &dimids[1])) ERR; /* Create one var. */ if (nc_def_var(ncid, "v1", NC_INT, NDIMS1, dimids, &nvid)) ERR; if (nc_enddef(ncid)) ERR; /* Set up slab for this process. */ start[0] = 0; start[1] = mpi_rank * DIMSIZE/mpi_size; count[0] = DIMSIZE2; count[1] = DIMSIZE/mpi_size; /* start parallel netcdf4 */ if (nc_var_par_access(ncid, nvid, flag)) ERR; if (!(data = malloc(sizeof(int)*count[1]*count[0]))) ERR; tempdata = data; for (j = 0; j < count[0]; j++){ for (i = 0; i < count[1]; i++) { *tempdata = mpi_rank * (j + 1); tempdata++; } } /* Write two dimensional integer data */ if (nc_put_vara_int(ncid, nvid, start, count, data)) ERR; free(data); /* Case 2: create four dimensional integer data, one dimension is unlimited. */ /* Create four dimensions. */ if (nc_def_dim(ncid, "ud1", NC_UNLIMITED, dimuids)) ERR; if (nc_def_dim(ncid, "ud2", DIMSIZE3, &dimuids[1])) ERR; if (nc_def_dim(ncid, "ud3", DIMSIZE2, &dimuids[2])) ERR; if (nc_def_dim(ncid, "ud4", DIMSIZE, &dimuids[3])) ERR; /* Create one var. */ if (nc_def_var(ncid, "uv1", NC_INT, NDIMS2, dimuids, &uvid)) ERR; if (nc_enddef(ncid)) ERR; /* Set up selection parameters */ ustart[0] = 0; ustart[1] = 0; ustart[2] = 0; ustart[3] = DIMSIZE*mpi_rank/mpi_size; ucount[0] = TIMELEN; ucount[1] = DIMSIZE3; ucount[2] = DIMSIZE2; ucount[3] = DIMSIZE/mpi_size; /* Access parallel */ if (nc_var_par_access(ncid, uvid, flag)) ERR; /* Create phony data. */ if (!(udata = malloc(ucount[0]*ucount[1]*ucount[2]*ucount[3]*sizeof(int)))) ERR; tempudata = udata; for( m=0; m<ucount[0];m++) for( k=0; k<ucount[1];k++) for (j=0; j<ucount[2];j++) for (i=0; i<ucount[3]; i++) { *tempudata = (1+mpi_rank)*2*(j+1)*(k+1)*(m+1); tempudata++; } /* Write slabs of phoney data. */ if (NC_INDEPENDENT == flag) { int res; res = nc_put_vara_int(ncid, uvid, ustart, ucount, udata); if(res != NC_ECANTEXTEND) ERR; } else { if (nc_put_vara_int(ncid, uvid, ustart, ucount, udata)) ERR; } free(udata); /* Close the netcdf file. */ if (nc_close(ncid)) ERR; if (nc_open_par(file_name, facc_type_open, comm, info, &ncid)) ERR; /* Case 1: read two-dimensional variables, no unlimited dimension */ /* Set up slab for this process. */ start[0] = 0; start[1] = mpi_rank * DIMSIZE/mpi_size; count[0] = DIMSIZE2; count[1] = DIMSIZE/mpi_size; if (nc_inq_varid(ncid, "v1", &rvid)) ERR; if (nc_var_par_access(ncid, rvid, flag)) ERR; if (!(rdata = malloc(sizeof(int)*count[1]*count[0]))) ERR; if (nc_get_vara_int(ncid, rvid, start, count, rdata)) ERR; temprdata = rdata; for (j=0; j<count[0];j++){ for (i=0; i<count[1]; i++){ if(*temprdata != mpi_rank*(j+1)) { ERR_RET; break; } temprdata++; } } free(rdata); /* Case 2: read four dimensional data, one dimension is unlimited. */ /* set up selection parameters */ ustart[0] = 0; ustart[1] = 0; ustart[2] = 0; ustart[3] = DIMSIZE*mpi_rank/mpi_size; ucount[0] = TIMELEN; ucount[1] = DIMSIZE3; ucount[2] = DIMSIZE2; ucount[3] = DIMSIZE/mpi_size; /* Inquiry the data */ /* (NOTE: This variable isn't written out, when access is independent) */ if (NC_INDEPENDENT != flag) { if (nc_inq_varid(ncid, "uv1", &rvid)) ERR; /* Access the parallel */ if (nc_var_par_access(ncid, rvid, flag)) ERR; if (!(rudata = malloc(ucount[0]*ucount[1]*ucount[2]*ucount[3]*sizeof(int)))) ERR; temprudata = rudata; /* Read data */ if (nc_get_vara_int(ncid, rvid, ustart, ucount, rudata)) ERR; for(m = 0; m < ucount[0]; m++) for(k = 0; k < ucount[1]; k++) for(j = 0; j < ucount[2]; j++) for(i = 0; i < ucount[3]; i++) { if(*temprudata != (1+mpi_rank)*2*(j+1)*(k+1)*(m+1)) ERR_RET; temprudata++; } free(rudata); } /* Close the netcdf file. */ if (nc_close(ncid)) ERR; return 0; }
static GDALDataset * GMTCreateCopy( const char * pszFilename, GDALDataset *poSrcDS, int bStrict, CPL_UNUSED char ** papszOptions, CPL_UNUSED GDALProgressFunc pfnProgress, CPL_UNUSED void * pProgressData ) { /* -------------------------------------------------------------------- */ /* Figure out general characteristics. */ /* -------------------------------------------------------------------- */ nc_type nc_datatype; GDALRasterBand *poBand; int nXSize, nYSize; CPLMutexHolderD(&hNCMutex); if( poSrcDS->GetRasterCount() != 1 ) { CPLError( CE_Failure, CPLE_AppDefined, "Currently GMT export only supports 1 band datasets." ); return NULL; } poBand = poSrcDS->GetRasterBand(1); nXSize = poSrcDS->GetRasterXSize(); nYSize = poSrcDS->GetRasterYSize(); if( poBand->GetRasterDataType() == GDT_Int16 ) nc_datatype = NC_SHORT; else if( poBand->GetRasterDataType() == GDT_Int32 ) nc_datatype = NC_INT; else if( poBand->GetRasterDataType() == GDT_Float32 ) nc_datatype = NC_FLOAT; else if( poBand->GetRasterDataType() == GDT_Float64 ) nc_datatype = NC_DOUBLE; else if( bStrict ) { CPLError( CE_Failure, CPLE_AppDefined, "Band data type %s not supported in GMT, giving up.", GDALGetDataTypeName( poBand->GetRasterDataType() ) ); return NULL; } else if( poBand->GetRasterDataType() == GDT_Byte ) nc_datatype = NC_SHORT; else if( poBand->GetRasterDataType() == GDT_UInt16 ) nc_datatype = NC_INT; else if( poBand->GetRasterDataType() == GDT_UInt32 ) nc_datatype = NC_INT; else nc_datatype = NC_FLOAT; /* -------------------------------------------------------------------- */ /* Establish bounds from geotransform. */ /* -------------------------------------------------------------------- */ double adfGeoTransform[6]; double dfXMax, dfYMin; poSrcDS->GetGeoTransform( adfGeoTransform ); if( adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0 ) { CPLError( bStrict ? CE_Failure : CE_Warning, CPLE_AppDefined, "Geotransform has rotational coefficients not supported in GMT." ); if( bStrict ) return NULL; } dfXMax = adfGeoTransform[0] + adfGeoTransform[1] * nXSize; dfYMin = adfGeoTransform[3] + adfGeoTransform[5] * nYSize; /* -------------------------------------------------------------------- */ /* Create base file. */ /* -------------------------------------------------------------------- */ int cdfid, err; err = nc_create (pszFilename, NC_CLOBBER,&cdfid); if( err != NC_NOERR ) { CPLError( CE_Failure, CPLE_AppDefined, "nc_create(%s): %s", pszFilename, nc_strerror( err ) ); return NULL; } /* -------------------------------------------------------------------- */ /* Define the dimensions and so forth. */ /* -------------------------------------------------------------------- */ int side_dim, xysize_dim, dims[1]; int x_range_id, y_range_id, z_range_id, inc_id, nm_id, z_id; nc_def_dim(cdfid, "side", 2, &side_dim); nc_def_dim(cdfid, "xysize", (int) (nXSize * nYSize), &xysize_dim); dims[0] = side_dim; nc_def_var (cdfid, "x_range", NC_DOUBLE, 1, dims, &x_range_id); nc_def_var (cdfid, "y_range", NC_DOUBLE, 1, dims, &y_range_id); nc_def_var (cdfid, "z_range", NC_DOUBLE, 1, dims, &z_range_id); nc_def_var (cdfid, "spacing", NC_DOUBLE, 1, dims, &inc_id); nc_def_var (cdfid, "dimension", NC_LONG, 1, dims, &nm_id); dims[0] = xysize_dim; nc_def_var (cdfid, "z", nc_datatype, 1, dims, &z_id); /* -------------------------------------------------------------------- */ /* Assign attributes. */ /* -------------------------------------------------------------------- */ double default_scale = 1.0; double default_offset = 0.0; int default_node_offset = 1; // pixel is area nc_put_att_text (cdfid, x_range_id, "units", 7, "meters"); nc_put_att_text (cdfid, y_range_id, "units", 7, "meters"); nc_put_att_text (cdfid, z_range_id, "units", 7, "meters"); nc_put_att_double (cdfid, z_id, "scale_factor", NC_DOUBLE, 1, &default_scale ); nc_put_att_double (cdfid, z_id, "add_offset", NC_DOUBLE, 1, &default_offset ); nc_put_att_int (cdfid, z_id, "node_offset", NC_LONG, 1, &default_node_offset ); nc_put_att_text (cdfid, NC_GLOBAL, "title", 1, ""); nc_put_att_text (cdfid, NC_GLOBAL, "source", 1, ""); /* leave define mode */ nc_enddef (cdfid); /* -------------------------------------------------------------------- */ /* Get raster min/max. */ /* -------------------------------------------------------------------- */ double adfMinMax[2]; GDALComputeRasterMinMax( (GDALRasterBandH) poBand, FALSE, adfMinMax ); /* -------------------------------------------------------------------- */ /* Set range variables. */ /* -------------------------------------------------------------------- */ size_t start[2], edge[2]; double dummy[2]; int nm[2]; start[0] = 0; edge[0] = 2; dummy[0] = adfGeoTransform[0]; dummy[1] = dfXMax; nc_put_vara_double(cdfid, x_range_id, start, edge, dummy); dummy[0] = dfYMin; dummy[1] = adfGeoTransform[3]; nc_put_vara_double(cdfid, y_range_id, start, edge, dummy); dummy[0] = adfGeoTransform[1]; dummy[1] = -adfGeoTransform[5]; nc_put_vara_double(cdfid, inc_id, start, edge, dummy); nm[0] = nXSize; nm[1] = nYSize; nc_put_vara_int(cdfid, nm_id, start, edge, nm); nc_put_vara_double(cdfid, z_range_id, start, edge, adfMinMax); /* -------------------------------------------------------------------- */ /* Write out the image one scanline at a time. */ /* -------------------------------------------------------------------- */ double *padfData; int iLine; padfData = (double *) CPLMalloc( sizeof(double) * nXSize ); edge[0] = nXSize; for( iLine = 0; iLine < nYSize; iLine++ ) { start[0] = iLine * nXSize; poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1, padfData, nXSize, 1, GDT_Float64, 0, 0, NULL ); err = nc_put_vara_double( cdfid, z_id, start, edge, padfData ); if( err != NC_NOERR ) { CPLError( CE_Failure, CPLE_AppDefined, "nc_put_vara_double(%s): %s", pszFilename, nc_strerror( err ) ); nc_close (cdfid); return( NULL ); } } CPLFree( padfData ); /* -------------------------------------------------------------------- */ /* Close file, and reopen. */ /* -------------------------------------------------------------------- */ nc_close (cdfid); /* -------------------------------------------------------------------- */ /* Re-open dataset, and copy any auxiliary pam information. */ /* -------------------------------------------------------------------- */ GDALPamDataset *poDS = (GDALPamDataset *) GDALOpen( pszFilename, GA_ReadOnly ); if( poDS ) poDS->CloneInfo( poSrcDS, GCIF_PAM_DEFAULT ); return poDS; }
/* * Write a hypercube of numeric values into a netCDF variable of an open * netCDF file. */ static void c_ncvpt ( int ncid, /* netCDF ID */ int varid, /* variable ID */ const size_t* start, /* multidimensional index of hypercube corner */ const size_t* count, /* multidimensional hypercube edge lengths */ const void* value, /* block of data values to be written */ int* rcode /* returned error code */ ) { int status; nc_type datatype; if ((status = nc_inq_vartype(ncid, varid, &datatype)) == 0) { switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_put_vara_schar(ncid, varid, start, count, (const signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_put_vara_short(ncid, varid, start, count, (const short*)value); # elif NF_INT1_IS_C_INT status = nc_put_vara_int(ncid, varid, start, count, (const int*)value); # elif NF_INT1_IS_C_LONG status = nc_put_vara_long(ncid, varid, start, count, (const long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_put_vara_short(ncid, varid, start, count, (const short*)value); # elif NF_INT2_IS_C_INT status = nc_put_vara_int(ncid, varid, start, count, (const int*)value); # elif NF_INT2_IS_C_LONG status = nc_put_vara_long(ncid, varid, start, count, (const long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_put_vara_int(ncid, varid, start, count, (const int*)value); # elif NF_INT_IS_C_LONG status = nc_put_vara_long(ncid, varid, start, count, (const long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_put_vara_float(ncid, varid, start, count, (const float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_put_vara_double(ncid, varid, start, count, (const double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_put_vara_float(ncid, varid, start, count, (const float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_put_vara_double(ncid, varid, start, count, (const double*)value); # endif break; } } if (status == 0) *rcode = 0; else { nc_advise("NCVPT", status, ""); *rcode = ncerr; } }
int ex_put_partial_node_set (int exoid, ex_entity_id node_set_id, int64_t start_node_num, int64_t num_nodes, const void_int *node_set_node_list) { int dimid, node_list_id, node_set_id_ndx, status; size_t num_nodes_in_set, start[1], count[1]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* first check if any node sets are specified */ if ((status = nc_inq_dimid (exoid, DIM_NUM_NS, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: no node sets specified in file id %d", exoid); ex_err("ex_put_partial_node_set",errmsg,exerrval); return (EX_FATAL); } /* Lookup index of node set id in VAR_NS_IDS array */ if ((node_set_id_ndx = ex_id_lkup(exoid, EX_NODE_SET, node_set_id)) < 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: no data allowed for NULL node set %"PRId64" in file id %d", node_set_id, exoid); ex_err("ex_put_partial_node_set",errmsg,EX_NULLENTITY); return (EX_WARN); } else { sprintf(errmsg, "Error: failed to locate node set id %"PRId64" in VAR_NS_IDS array in file id %d", node_set_id, exoid); ex_err("ex_put_partial_node_set",errmsg,exerrval); return (EX_FATAL); } } /* inquire id's of previously defined dimensions */ if ((status = nc_inq_dimid (exoid, DIM_NUM_NOD_NS(node_set_id_ndx), &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate number of nodes in set %"PRId64" in file id %d", node_set_id, exoid); ex_err("ex_put_partial_node_set",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, &num_nodes_in_set)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of nodes in set %"PRId64" in file id %d", node_set_id, exoid); ex_err("ex_put_partial_node_set",errmsg,exerrval); return (EX_FATAL); } /* Check input parameters for a valid range of numbers */ if (start_node_num < 0 || start_node_num > num_nodes_in_set) { fprintf(stderr, "ERROR: Invalid input to function ex_get_partial_node_set!\n"); return -1; } if (num_nodes < 0) { fprintf(stderr, "ERROR: Invalid number of nodes in nodes set!\n"); return -1; } /* start_node_num now starts at 1, not 0 */ if ((start_node_num + num_nodes - 1) > num_nodes_in_set) { fprintf(stderr, "ERROR: request larger than number of nodes in set!\n"); return -1; } /* inquire if variable for node set node list has been defined */ if ((status = nc_inq_varid (exoid, VAR_NODE_NS(node_set_id_ndx), &node_list_id)) != NC_NOERR) { /* variable doesn't exist */ exerrval = status; sprintf(errmsg, "Error: failed to locate node set %"PRId64" node list in file id %d", node_set_id, exoid); ex_err("ex_put_partial_node_set",errmsg,exerrval); return (EX_FATAL); } /* write out the node list array */ start[0] = --start_node_num; count[0] = num_nodes; if (count[0] == 0) start[0] = 0; if (ex_int64_status(exoid) & EX_BULK_INT64_API) { status = nc_put_vara_longlong(exoid, node_list_id, start, count, node_set_node_list); } else { status = nc_put_vara_int(exoid, node_list_id, start, count, node_set_node_list); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store node set %"PRId64" node list in file id %d", node_set_id, exoid); ex_err("ex_put_partial_node_set",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); }
/* Write the grid to the netcdf file fname */ void WriteGridnetcdf(char *fname, int gridno) { int i, j, cnt; int *ellist; int ncid; /* netCDF id */ /* dimension ids */ int nmnp_dim; int nmel_dim; int elements_dim; /* dimension lengths */ size_t nmnp_len; size_t nmel_len; size_t elements_len; size_t start[2], count[2]; /* variable ids */ int x_id; int y_id; int z_id; int elements_id; int stat; stat = nc_open(fname, NC_WRITE, &ncid); check_err(stat, __LINE__, __FILE__); stat = nc_inq_dimid(ncid, "nmel", &nmel_dim); stat = nc_inq_dim(ncid, nmel_dim, "nmel", &nmel_len); stat = nc_inq_dimid(ncid, "nmnp", &nmnp_dim); stat = nc_inq_dim(ncid, nmnp_dim, "nmnp", &nmnp_len); stat = nc_inq_dimid(ncid, "elements", &elements_dim); stat = nc_inq_dim(ncid, elements_dim, "elements", &elements_len); stat = nc_inq_varid(ncid, "x", &x_id); stat = nc_inq_varid(ncid, "y", &y_id); stat = nc_inq_varid(ncid, "z", &z_id); stat = nc_inq_varid(ncid, "elements", &elements_id); ellist = (int *) malloc(elements_len * sizeof(int)); if (ellist == NULL) { stat = nc_close(ncid); return; } start[0] = 0; count[0] = nmnp_len; stat = nc_put_vara_double(ncid, x_id, start, count, grid[gridno].xord); stat = nc_put_vara_double(ncid, y_id, start, count, grid[gridno].yord); stat = nc_put_vara_double(ncid, z_id, start, count, grid[gridno].depth); cnt = 0; for (i = 0; i < grid[gridno].nmel; i++) { ellist[cnt++] = grid[gridno].icon[i].nn; for (j = 0; j < grid[gridno].icon[i].nn; j++) { ellist[cnt++] = grid[gridno].icon[i].nl[j]; } } /* * Oops, number of items in elements differs between the grid and the * file, bail out. */ if (cnt != elements_len) { free(ellist); stat = nc_close(ncid); return; } start[0] = 0; count[0] = cnt; stat = nc_put_vara_int(ncid, elements_id, start, count, ellist); free(ellist); stat = nc_close(ncid); check_err(stat, __LINE__, __FILE__); }
void ITLRandomField::_CloseNetCdf ( ) { // MOD-BY-LEETEN 09/01/2011-FROM: // if( iNcId > 0 ) // TO: if( iNcId >= 0 ) // MOD-BY-LEETEN 09/01/2011-END { // write the time stamp TBuffer<int> piTemp; piTemp.alloc(this->IGetNrOfTimeStamps()); for(int t = 0; t < (int)piTemp.USize(); t++) piTemp[t] = this->viTimeStamps[t]; #ifndef WITH_PNETCDF // ADD-BY-LEETEN 08/12/2011 #if 0 // DEL-BY-LEETEN 09/01/2011-BEGIN // since the time step wil lbe written earlier, this part can be removed size_t uStart = 0; size_t uCount = piTemp.USize(); ASSERT_NETCDF(nc_put_vara_int( iNcId, iNcTimeVarId, &uStart, &uCount, &piTemp[0])); #endif // DEL-BY-LEETEN 09/01/2011-END /* Close the file. */ ASSERT_NETCDF(nc_close(iNcId)); // ADD-BY-LEETEN 08/12/2011-BEGIN #else // #ifndef WITH_PNETCDF #if 0 // DEL-BY-LEETEN 09/01/2011-BEGIN MPI_Offset uStart = 0; MPI_Offset uCount = piTemp.USize(); ASSERT_NETCDF(ncmpi_begin_indep_data(iNcId)); if( 0 == iRank ) ASSERT_NETCDF(ncmpi_put_vara_int( iNcId, iNcTimeVarId, &uStart, &uCount, &piTemp[0])); ASSERT_NETCDF(ncmpi_end_indep_data(iNcId)); #endif // DEL-BY-LEETEN 09/01/2011-END /* Close the file. */ ASSERT_NETCDF(ncmpi_close(iNcId)); #endif // #ifndef WITH_PNETCDF // ADD-BY-LEETEN 08/12/2011-END // MOD-BY-LEETEN 09/01/2011-FROM: // iNcId = 0; // TO: iNcId = -1; // MOD-BY-LEETEN 09/01/2011-END } };
int ex_put_n_elem_conn (int exoid, ex_entity_id elem_blk_id, int64_t start_elem_num, int64_t num_elems, const void_int *connect) { int numelbdim, nelnoddim, connid, elem_blk_id_ndx, status; size_t num_elem_this_blk, num_nod_per_elem, start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* Determine index of elem_blk_id in VAR_ID_EL_BLK array */ if ((elem_blk_id_ndx = ex_id_lkup(exoid, EX_ELEM_BLOCK, elem_blk_id)) == -1) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: connectivity array not allowed for NULL element block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_n_elem_conn",errmsg,EX_MSG); return (EX_WARN); } else { sprintf(errmsg, "Error: failed to locate element block id %"PRId64" in %s array in file id %d", elem_blk_id,VAR_ID_EL_BLK, exoid); ex_err("ex_put_n_elem_conn",errmsg,exerrval); return (EX_FATAL); } } /* inquire id's of previously defined dimensions */ if ((status = nc_inq_dimid (exoid, DIM_NUM_EL_IN_BLK(elem_blk_id_ndx), &numelbdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate number of elements in block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_n_elem_conn",errmsg, exerrval); return(EX_FATAL); } if ((status = nc_inq_dimlen(exoid, numelbdim, &num_elem_this_blk)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of elements in block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_n_elem_conn",errmsg,exerrval); return(EX_FATAL); } if ((status = nc_inq_dimid (exoid, DIM_NUM_NOD_PER_EL(elem_blk_id_ndx), &nelnoddim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate number of nodes/elem in block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_n_elem_conn",errmsg,exerrval); return(EX_FATAL); } if ((status = nc_inq_dimlen (exoid, nelnoddim, &num_nod_per_elem)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of nodes/elem in block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_n_elem_conn",errmsg,exerrval); return(EX_FATAL); } if ((status = nc_inq_varid (exoid, VAR_CONN(elem_blk_id_ndx), &connid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate connectivity array for element block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_n_elem_conn",errmsg, exerrval); return(EX_FATAL); } /* do some error checking */ if (num_elem_this_blk < (start_elem_num + num_elems - 1)) { exerrval = status; sprintf(errmsg, "Error: requested connectivity from too many elements in block, %"PRId64, elem_blk_id); ex_err("ex_put_n_elem_conn",errmsg, exerrval); return(EX_FATAL); } /* write out the connectivity array */ start[0] = --start_elem_num; start[1] = 0; count[0] = num_elems; count[1] = num_nod_per_elem; if (ex_int64_status(exoid) & EX_BULK_INT64_API) { status = nc_put_vara_longlong(exoid, connid, start, count, connect); } else { status = nc_put_vara_int(exoid, connid, start, count, connect); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to write connectivity array for block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_n_elem_conn",errmsg, exerrval); return(EX_FATAL); } return (EX_NOERR); }
// Traj_NcEnsemble::writeArray() // TODO RemdValues int Traj_NcEnsemble::writeArray(int set, FramePtrArray const& Farray) { # ifdef HAS_PNETCDF MPI_Offset pstart_[4]; MPI_Offset pcount_[4]; # define start_ pstart_ # define count_ pcount_ # endif start_[0] = ncframe_; // Frame start_[2] = 0; // Atoms start_[3] = 0; // XYZ count_[0] = 1; // Frame count_[1] = 1; // Ensemble count_[3] = 3; // XYZ for (int member = ensembleStart_; member != ensembleEnd_; member++) { //rprintf("DEBUG: Writing set %i, member %i\n", set+1, member); # ifdef MPI Frame* frm = Farray[0]; # else Frame* frm = Farray[member]; # endif start_[1] = member; // Ensemble count_[2] = Ncatom(); // Atoms // Write Coords //DebugIndices(); // DEBUG DoubleToFloat(Coord_, frm->xAddress()); # ifdef HAS_PNETCDF if (ncmpi_put_vara_float_all(ncid_, coordVID_, start_, count_, Coord_)) # else if (NC::CheckErr(nc_put_vara_float(ncid_, coordVID_, start_, count_, Coord_))) # endif { mprinterr("Error: Netcdf Writing coords frame %i\n", set+1); return 1; } // Write velocity. if (velocityVID_ != -1) { DoubleToFloat(Coord_, frm->vAddress()); # ifdef HAS_PNETCDF if (ncmpi_put_vara_float_all(ncid_, velocityVID_, start_, count_, Coord_)) # else if (NC::CheckErr(nc_put_vara_float(ncid_, velocityVID_, start_, count_, Coord_)) ) # endif { mprinterr("Error: Netcdf writing velocity frame %i\n", set+1); return 1; } } // Write box if (cellLengthVID_ != -1) { count_[2] = 3; # ifdef HAS_PNETCDF if (ncmpi_put_vara_double_all(ncid_,cellLengthVID_,start_,count_,frm->bAddress())) # else if (NC::CheckErr(nc_put_vara_double(ncid_,cellLengthVID_,start_,count_,frm->bAddress())) ) # endif { mprinterr("Error: Writing cell lengths frame %i.\n", set+1); return 1; } # ifdef HAS_PNETCDF if (ncmpi_put_vara_double_all(ncid_,cellAngleVID_,start_,count_,frm->bAddress()+3)) # else if (NC::CheckErr(nc_put_vara_double(ncid_,cellAngleVID_,start_,count_,frm->bAddress()+3))) # endif { mprinterr("Error: Writing cell angles frame %i.\n", set+1); return 1; } } // Write temperature if (TempVID_!=-1) { # ifdef HAS_PNETCDF if (ncmpi_put_vara_double_all(ncid_,TempVID_,start_,count_,frm->tAddress())) # else if (NC::CheckErr(nc_put_vara_double(ncid_,TempVID_,start_,count_,frm->tAddress()))) # endif { mprinterr("Error: Writing temperature frame %i.\n", set+1); return 1; } } // Write indices if (indicesVID_ != -1) { count_[2] = remd_dimension_; # ifdef HAS_PNETCDF if (ncmpi_put_vara_int_all(ncid_,indicesVID_,start_,count_,frm->iAddress())) # else if (NC::CheckErr(nc_put_vara_int(ncid_,indicesVID_,start_,count_,frm->iAddress()))) # endif { mprinterr("Error: Writing indices frame %i.\n", set+1); return 1; } } } # ifdef HAS_PNETCDF //ncmpi_sync(ncid_); # else nc_sync(ncid_); // Necessary after every write?? # endif ++ncframe_; # ifdef HAS_PNETCDF // DEBUG # undef start_ # undef count_ # endif return 0; }
int main(int argc, char **argv) { printf("\n*** Testing netcdf-4 group functions.\n"); printf("*** testing use of unlimited dim in parent group..."); { #define NDIMS_IN_VAR 1 #define NDIMS_IN_FILE 2 #define BABE_LIMIT 3 #define DIM_NAME1 "Influence" #define DIM_NAME2 "Babe_Factor" #define VAR_NAME1 "Court_of_Star_Chamber" #define VAR_NAME2 "Justice_of_the_Peace" #define VAR_NAME3 "Bosworth_Field" int ncid, dimid1, dimid2, varid1, varid2, varid3, henry_vii_id; int grpid_in, varid_in1, varid_in2, varid_in3; nc_type xtype_in; int ndims_in, dimids_in[NDIMS_IN_FILE], dimid1_in, natts; char name_in[NC_MAX_NAME + 1]; size_t len_in, index[NDIMS_IN_VAR] = {0}; long long value = NC_FILL_INT64 + 1, value_in; /* Create a file with an unlimited dim and a limited, used by * variables in child groups. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_dim(ncid, DIM_NAME1, NC_UNLIMITED, &dimid1)) ERR; if (nc_def_dim(ncid, DIM_NAME2, BABE_LIMIT, &dimid2)) ERR; if (nc_def_grp(ncid, HENRY_VII, &henry_vii_id)) ERR; if (nc_def_var(henry_vii_id, VAR_NAME1, NC_INT64, NDIMS_IN_VAR, &dimid1, &varid1)) ERR; if (nc_def_var(henry_vii_id, VAR_NAME2, NC_INT64, NDIMS_IN_VAR, &dimid1, &varid2)) ERR; if (nc_def_var(henry_vii_id, VAR_NAME3, NC_INT64, NDIMS_IN_VAR, &dimid2, &varid3)) ERR; /* Check it out. Find the group by name. */ if (nc_inq_ncid(ncid, HENRY_VII, &grpid_in)) ERR; /* Ensure that dimensions in parent are visible and correct. */ if (nc_inq_dimids(grpid_in, &ndims_in, dimids_in, 1)) ERR; if (ndims_in != NDIMS_IN_FILE || dimids_in[0] != dimid1 || dimids_in[1] != dimid2) ERR; if (nc_inq_dim(grpid_in, dimids_in[0], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME1) || len_in != 0) ERR; if (nc_inq_dim(grpid_in, dimids_in[1], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME2) || len_in != BABE_LIMIT) ERR; /* Check the vars in the group. */ if (nc_inq_varid(grpid_in, VAR_NAME1, &varid_in1)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME2, &varid_in2)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME3, &varid_in3)) ERR; if (varid_in1 != varid1 || varid_in2 != varid2 || varid_in3 != varid3) ERR; if (nc_inq_var(grpid_in, varid1, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME1) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid2, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME2) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid3, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME3) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid2 || natts != 0) ERR; /* Write one value to one variable. */ if (nc_put_var1_longlong(grpid_in, varid_in1, index, &value)) ERR; /* Read one value from the second unlim dim variable. It should * be the fill value. */ if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Read one value from the variable with limited dim. It should * be the fill value. */ if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Attempt to read beyond end of dimensions to generate error. */ index[0] = BABE_LIMIT; if (nc_get_var1_longlong(grpid_in, varid_in1, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_close(ncid)) ERR; /* Check it out again. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Find the group by name. */ if (nc_inq_ncid(ncid, HENRY_VII, &grpid_in)) ERR; /* Ensure that dimensions in parent are visible and correct. */ if (nc_inq_dimids(grpid_in, &ndims_in, dimids_in, 1)) ERR; if (ndims_in != NDIMS_IN_FILE || dimids_in[0] != dimid1 || dimids_in[1] != dimid2) ERR; if (nc_inq_dim(grpid_in, dimids_in[0], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME1) || len_in != 1) ERR; if (nc_inq_dim(grpid_in, dimids_in[1], name_in, &len_in)) ERR; if (strcmp(name_in, DIM_NAME2) || len_in != BABE_LIMIT) ERR; /* Check the vars in the group. */ if (nc_inq_varid(grpid_in, VAR_NAME1, &varid_in1)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME2, &varid_in2)) ERR; if (nc_inq_varid(grpid_in, VAR_NAME3, &varid_in3)) ERR; if (varid_in1 != varid1 || varid_in2 != varid2 || varid_in3 != varid3) ERR; if (nc_inq_var(grpid_in, varid1, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME1) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid2, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME2) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid1 || natts != 0) ERR; if (nc_inq_var(grpid_in, varid3, name_in, &xtype_in, &ndims_in, &dimid1_in, &natts)) ERR; if (strcmp(name_in, VAR_NAME3) || xtype_in != NC_INT64 || ndims_in != NDIMS_IN_VAR || dimid1_in != dimid2 || natts != 0) ERR; /* Read one value from the second unlim dim variable. It should * be the fill value. */ index[0] = 0; if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Read one value from the variable with limited dim. It should * be the fill value. */ if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in)) ERR; if (value_in != NC_FILL_INT64) ERR; /* Attempt to read beyond end of dimensions to generate error. */ index[0] = BABE_LIMIT; if (nc_get_var1_longlong(grpid_in, varid_in1, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in2, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_get_var1_longlong(grpid_in, varid_in3, index, &value_in) != NC_EINVALCOORDS) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing groups and unlimited dimensions..."); { int ncid; int henry_vii_id; int tudor_id; int dimids_in[MAX_SIBLING_GROUPS], ndims_in; int num_grps; int dimid, dynasty, varid; size_t len_in; int natts_in; int grpids_in[10]; nc_type xtype_in; char name_in[NC_MAX_NAME + 1]; int data_out[DIM1_LEN] = {0, 2, 6}, data_in[DIM1_LEN]; size_t start[1] = {0}, count[1] = {3}; int j; /* Create one group, with one var, which has one dimension, which is unlimited. */ if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; if (nc_def_grp(ncid, DYNASTY, &tudor_id)) ERR; if (nc_def_dim(tudor_id, DIM1_NAME, NC_UNLIMITED, &dimid)) ERR; if (nc_def_grp(tudor_id, HENRY_VII, &henry_vii_id)) ERR; if (nc_def_var(henry_vii_id, VAR1_NAME, NC_INT, 1, &dimid, &varid)) ERR; if (nc_put_vara_int(henry_vii_id, varid, start, count, data_out)) ERR; if (nc_close(ncid)) ERR; /* Now check the file to see if the dimension and variable are * there. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq_grps(ncid, &num_grps, &dynasty)) ERR; if (num_grps != 1) ERR; if (nc_inq_grps(dynasty, &num_grps, grpids_in)) ERR; if (num_grps != 1) ERR; if (nc_inq_dim(grpids_in[0], 0, name_in, &len_in)) ERR; if (strcmp(name_in, DIM1_NAME) || len_in != DIM1_LEN) ERR; if (nc_inq_var(grpids_in[0], 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR; if (strcmp(name_in, VAR1_NAME) || xtype_in != NC_INT || ndims_in != 1 || dimids_in[0] != 0 || natts_in != 0) ERR; if (nc_get_vara_int(grpids_in[0], 0, start, count, data_in)) ERR; for (j=0; j<DIM1_LEN; j++) if (data_in[j] != data_out[j]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int extract_unary_1D(int mpi_rank,int mpi_size, int ncid,int vlid,int ncidout,int vlidout,int ndims,nc_type vtype,size_t *shape,size_t *begins,size_t *ends, ptrdiff_t *strides,size_t preLen,size_t *outLen){ int i,j,res; size_t *divider=(size_t *)malloc(sizeof(size_t)*ndims); //input divider size_t *dividerOut=(size_t *)malloc(sizeof(size_t)*ndims); // output divider size_t *start=(size_t*)malloc(sizeof(size_t)*ndims); //start position for reading element from input file size_t *count=(size_t*)malloc(sizeof(size_t)*ndims); size_t *countOut=(size_t *)malloc(sizeof(size_t)*ndims); size_t *startOut=(size_t*)malloc(sizeof(size_t)*ndims); //start position for writing element to output file size_t *shapeOut=(size_t*)malloc(sizeof(size_t)*ndims); //output dimension shape size_t startOut0; size_t countOut0; int len0=1; int lenOut=1; for(i=0;i<ndims;++i){ shapeOut[i]=(ends[i]-begins[i])/strides[i]+1; lenOut*=shapeOut[i]; if(i>0){ startOut[i]=0; countOut[i]=shapeOut[i]; start[i]=begins[i]; count[i]=ends[i]-begins[i]+1; len0*=ends[i]-begins[i]+1; } } if(outLen!=NULL) *outLen=lenOut; if(shapeOut[0]>=mpi_size){ startOut0=mpi_rank*(shapeOut[0]/mpi_size); if(mpi_rank!=mpi_size-1){ countOut0=(shapeOut[0]/mpi_size); }else{ countOut0=shapeOut[0]-startOut0; } }else{ if(mpi_rank<shapeOut[0]){ startOut0=mpi_rank; countOut0=1; }else{ return 0; } } int dataEnd=lenOut/shapeOut[0]; void* data=(void*)malloc(sizeof(double)*len0); void* dataOut=(void*)malloc(sizeof(double)*dataEnd); size_t* poses=(size_t*)malloc(sizeof(size_t)*dataEnd); getDivider(ndims,count,divider); getDivider(ndims,countOut,dividerOut); transfer_pos(poses,ndims-1,strides,dataEnd,dividerOut,divider); printf("mpi_rank %d,countOut0 %d\n",mpi_rank,countOut0); for(i=0;i<countOut0;++i){ startOut[0]=startOut0+i+preLen/len0; countOut[0]=1; start[0]=begins[0]+(startOut0+i)*strides[0]; count[0]=1; switch(vtype){ case NC_BYTE: if((res=nc_get_vara_uchar(ncid,vlid,start,count,(unsigned char *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_uchar(ncidout,vlidout,startOut,countOut,(unsigned char *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((unsigned char *)dataOut)[j]=((unsigned char *)data)[poses[j]]; } if((res=nc_put_vara_uchar(ncidout,vlidout,startOut,countOut,(unsigned char *)dataOut))) BAIL(res); } break; case NC_CHAR: if((res=nc_get_vara_schar(ncid,vlid,start,count,(signed char *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_schar(ncidout,vlidout,startOut,countOut,(signed char *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((signed char *)dataOut)[j]=((signed char *)data)[poses[j]]; } if((res=nc_put_vara_schar(ncidout,vlidout,startOut,countOut,(signed char *)dataOut))) BAIL(res); } break; case NC_SHORT: if((res=nc_get_vara_short(ncid,vlid,start,count,data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_short(ncidout,vlidout,startOut,countOut,(short *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((short *)dataOut)[j]=((short *)data)[poses[j]]; } if((res=nc_put_vara_short(ncidout,vlidout,startOut,countOut,(short *)dataOut))) BAIL(res); } break; case NC_INT: if((res=nc_get_vara_int(ncid,vlid,start,count,(int *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_int(ncidout,vlidout,startOut,countOut,(int *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((int *)dataOut)[j]=((int *)data)[poses[j]]; } if((res=nc_put_vara_int(ncidout,vlidout,startOut,countOut,(int *)dataOut))) BAIL(res); } break; case NC_FLOAT: if((res=nc_get_vara_float(ncid,vlid,start,count,data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_float(ncidout,vlidout,startOut,countOut,(float *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((float *)dataOut)[j]=((float *)data)[poses[j]]; } if((res=nc_put_vara_float(ncidout,vlidout,startOut,countOut,(float *)dataOut))) BAIL(res); } break; case NC_DOUBLE: if((res=nc_get_vara_double(ncid,vlid,start,count,(double *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_double(ncidout,vlidout,startOut,countOut,(double *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((double *)dataOut)[j]=((double *)data)[poses[j]]; } if((res=nc_put_vara_double(ncidout,vlidout,startOut,countOut,(double *)dataOut))) BAIL(res); } break; default: printf("Unknown data type\n"); } } /*free resourses*/ free(divider); free(dividerOut); free(start); free(startOut); free(shapeOut); free(data); free(dataOut); free(poses); return 0; }
int ex_put_processor_node_maps(int exoid, void_int *node_mapi, void_int *node_mapb, void_int *node_mape, int proc_id ) { const char *func_name="ex_put_processor_node_maps"; int status, varid, dimid; char ftype[2]; size_t start[1], count[1]; int64_t varidx[2]; int nmstat; char errmsg[MAX_ERR_LENGTH]; /*-----------------------------Execution begins-----------------------------*/ exerrval = 0; /* clear error code */ /* Get the file type */ if (ex_get_file_type(exoid, ftype) != EX_NOERR) { exerrval = EX_MSG; sprintf(errmsg, "Error: unable to find file type for file ID %d", exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* Get the status of this node map */ if ((status = nc_inq_varid(exoid, VAR_INT_N_STAT, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" from file ID %d", VAR_INT_N_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (ftype[0] == 'p') start[0] = 0; else start[0] = proc_id; if ((status = nc_get_var1_int(exoid, varid, start, &nmstat)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get status for \"%s\" from file %d", VAR_INT_N_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* Write out the internal node-number map */ if (nmstat == 1) { /* get the index */ if (ex_get_idx(exoid, VAR_NODE_MAP_INT_IDX, varidx, proc_id) == -1) { exerrval = status; sprintf(errmsg, "Error: failed to find index variable, \"%s\", in file ID %d", VAR_NODE_MAP_INT_IDX, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* check if I need to get the dimension */ if (varidx[1] == -1) { if ((status = nc_inq_dimid(exoid, DIM_NUM_INT_NODES, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find dimension ID for \"%s\" in file ID %d", DIM_NUM_INT_NODES, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, count)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find length of dimension \"%s\" in file ID %d", DIM_NUM_INT_NODES, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } varidx[1] = count[0]; } if ((status = nc_inq_varid(exoid, VAR_NODE_MAP_INT, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" in file ID %d", VAR_NODE_MAP_INT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } start[0] = varidx[0]; count[0] = varidx[1] - varidx[0]; if (ex_int64_status(exoid) & EX_MAPS_INT64_API) { status = nc_put_vara_longlong(exoid, varid, start, count, node_mapi); } else { status = nc_put_vara_int(exoid, varid, start, count, node_mapi); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to output variable \"%s\" in file ID %d", VAR_NODE_MAP_INT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } } /* End "if (nmstat == 1)" */ /* Get the status of this node map */ if ((status = nc_inq_varid(exoid, VAR_BOR_N_STAT, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" from file ID %d", VAR_BOR_N_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (ftype[0] == 'p') start[0] = 0; else start[0] = proc_id; if ((status = nc_get_var1_int(exoid, varid, start, &nmstat)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get status for \"%s\" from file %d", VAR_BOR_N_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (nmstat == 1) { /* Write out the border node-number map */ /* get the index */ if (ex_get_idx(exoid, VAR_NODE_MAP_BOR_IDX, varidx, proc_id) == -1) { exerrval = status; sprintf(errmsg, "Error: failed to find index variable, \"%s\", in file ID %d", VAR_NODE_MAP_BOR_IDX, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* check if I need to get the dimension */ if (varidx[1] == -1) { if ((status = nc_inq_dimid(exoid, DIM_NUM_BOR_NODES, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find dimension ID for \"%s\" in file ID %d", DIM_NUM_BOR_NODES, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, count)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find length of dimension \"%s\" in file ID %d", DIM_NUM_BOR_NODES, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } varidx[1] = count[0]; } if ((status = nc_inq_varid(exoid, VAR_NODE_MAP_BOR, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" in file ID %d", VAR_NODE_MAP_BOR, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* Output the map */ start[0] = varidx[0]; count[0] = varidx[1] - varidx[0]; if (ex_int64_status(exoid) & EX_MAPS_INT64_API) { status = nc_put_vara_longlong(exoid, varid, start, count, node_mapb); } else { status = nc_put_vara_int(exoid, varid, start, count, node_mapb); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to output variable \"%s\" in file ID %d", VAR_NODE_MAP_BOR, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } } /* End "if (nmstat == 1)" */ /* Get the status of this node map */ if ((status = nc_inq_varid(exoid, VAR_EXT_N_STAT, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" from file ID %d", VAR_EXT_N_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (ftype[0] == 'p') start[0] = 0; else start[0] = proc_id; if ((status = nc_get_var1_int(exoid, varid, start, &nmstat)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get status for \"%s\" from file %d", VAR_EXT_N_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (nmstat == 1) { /* Write out the external node-number map */ if (ex_get_idx(exoid, VAR_NODE_MAP_EXT_IDX, varidx, proc_id) == -1) { exerrval = status; sprintf(errmsg, "Error: failed to find index variable, \"%s\", in file ID %d", VAR_NODE_MAP_EXT_IDX, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* check if I need to get the dimension */ if (varidx[1] == -1) { if ((status = nc_inq_dimid(exoid, DIM_NUM_EXT_NODES, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find dimension ID for \"%s\" in file ID %d", DIM_NUM_EXT_NODES, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, count)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find length of dimension \"%s\" in file ID %d", DIM_NUM_EXT_NODES, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } varidx[1] = count[0]; } if ((status = nc_inq_varid(exoid, VAR_NODE_MAP_EXT, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" in file ID %d", VAR_NODE_MAP_EXT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* Output the map */ start[0] = varidx[0]; count[0] = varidx[1] - varidx[0]; if (ex_int64_status(exoid) & EX_MAPS_INT64_API) { status = nc_put_vara_longlong(exoid, varid, start, count, node_mape); } else { status = nc_put_vara_int(exoid, varid, start, count, node_mape); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to output variable \"%s\" in file ID %d", VAR_NODE_MAP_EXT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } } /* End "if (nmstat == 1)" */ return (EX_NOERR); }
/* This function use the first dimension to parallel and reads and writes all needed data only once, there is no iteration. Stride is not supported */ int extract_unary_1D_all(int mpi_rank,int mpi_size, int ncid,int vlid,int ncidout,int vlidout,int ndims,nc_type vtype,size_t *shape,size_t *begins,size_t *ends,size_t preLen,size_t *outLen){ int i,res; size_t *start=(size_t*)malloc(sizeof(size_t)*ndims); //start position for reading element from input file size_t *countOut=(size_t *)malloc(sizeof(size_t)*ndims); size_t *startOut=(size_t*)malloc(sizeof(size_t)*ndims); //start position for writing element to output file size_t *shapeOut=(size_t*)malloc(sizeof(size_t)*ndims); //output dimension shape size_t startOut0; size_t countOut0; int len0=1; int lenOut=1; for(i=0;i<ndims;++i){ /* shapeOut[0]=(ends[0]-begins[0])/strides[0]+1;*/ shapeOut[i]=ends[i]-begins[i]+1; lenOut*=shapeOut[i]; if(i>0){ startOut[i]=0; countOut[i]=shapeOut[i]; start[i]=begins[i]; len0*=ends[i]-begins[i]+1; } } if(outLen!=NULL) *outLen=lenOut; if(shapeOut[0]>=mpi_size){ startOut0=mpi_rank*(shapeOut[0]/mpi_size); if(mpi_rank!=mpi_size-1){ countOut0=(shapeOut[0]/mpi_size); }else{ countOut0=shapeOut[0]-startOut0; } }else{ if(mpi_rank<shapeOut[0]){ startOut0=mpi_rank; countOut0=1; }else{ return 0; } } int dataEnd=countOut0*len0; printf("mpi_rank %d,countOut0 %d\n",mpi_rank,countOut0); startOut[0]=startOut0+preLen/len0; countOut[0]=countOut0; /* start[0]=begins[0]+startOut0*strides[0];*/ start[0]=begins[0]+startOut0; switch(vtype){ case NC_BYTE: { unsigned char* dataOut=(unsigned char *)malloc(sizeof(unsigned char)*dataEnd); if((res=nc_get_vara_uchar(ncid,vlid,start,countOut,dataOut))) BAIL(res); if((res=nc_put_vara_uchar(ncidout,vlidout,startOut,countOut,(unsigned char *)dataOut))) BAIL(res); free(dataOut); } break; case NC_CHAR: { char* dataOut=(char *)malloc(sizeof(char)*dataEnd); if((res=nc_get_vara_schar(ncid,vlid,start,countOut,(signed char *)dataOut))) BAIL(res); if((res=nc_put_vara_schar(ncidout,vlidout,startOut,countOut,(signed char *)dataOut))) BAIL(res); free(dataOut); } break; case NC_SHORT: { short *dataOut=(short *)malloc(sizeof(short)*dataEnd); if((res=nc_get_vara_short(ncid,vlid,start,countOut,(short *)dataOut))) BAIL(res); if((res=nc_put_vara_short(ncidout,vlidout,startOut,countOut,(short *)dataOut))) BAIL(res); free(dataOut); } break; case NC_INT: { int * dataOut=(int *)malloc(sizeof(int)*dataEnd); if((res=nc_get_vara_int(ncid,vlid,start,countOut,(int *)dataOut))) BAIL(res); if((res=nc_put_vara_int(ncidout,vlidout,startOut,countOut,(int *)dataOut))) BAIL(res); free(dataOut); } break; case NC_FLOAT: { float * dataOut=(float *)malloc(sizeof(float)*dataEnd); if((res=nc_get_vara_float(ncid,vlid,start,countOut,(float *)dataOut))) BAIL(res); if((res=nc_put_vara_float(ncidout,vlidout,startOut,countOut,(float *)dataOut))) BAIL(res); free(dataOut); } break; case NC_DOUBLE: { double* dataOut=(double *)malloc(sizeof(double)*dataEnd); if((res=nc_get_vara_double(ncid,vlid,start,countOut,dataOut))) BAIL(res); if((res=nc_put_vara_double(ncidout,vlidout,startOut,countOut,(double *)dataOut))) BAIL(res); free(dataOut); } break; default: printf("Unknown data type\n"); } free(start); free(startOut); free(shapeOut); free(countOut); return 0; }
int ex_put_node_cmap(int exoid, ex_entity_id map_id, void_int *node_ids, void_int *proc_ids, int processor ) { const char *func_name="ex_put_node_cmap"; int map_idx, varid, dimid, status; size_t start[1], count[1], ret_val; int64_t varidx[2]; int value; char errmsg[MAX_ERR_LENGTH]; /*-----------------------------Execution begins-----------------------------*/ exerrval = 0; /* clear error code */ /* get the index for the comm map information variables */ if (ex_get_idx(exoid, VAR_N_COMM_INFO_IDX, varidx, processor) == -1) { sprintf(errmsg, "Error: failed to find index variable, \"%s\", in file ID %d", VAR_N_COMM_INFO_IDX, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* Get the index for this map_id */ if ((map_idx=ne_id_lkup(exoid, VAR_N_COMM_IDS, varidx, map_id)) == -1) { sprintf(errmsg, "Error: failed to find index for variable \"%s\" in file ID %d", VAR_N_COMM_IDS, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* * Find out if this is a NULL comm map by checking it's entry in * the status vector. */ if ((status = nc_inq_varid(exoid, VAR_N_COMM_STAT, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" in file ID %d", VAR_N_COMM_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } start[0] = map_idx; if ((status = nc_get_var1_int(exoid, varid, start, &value)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: unable to get variable \"%s\" from file ID %d", VAR_N_COMM_STAT, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (value == 0) return(EX_NOERR); /* NULL set */ /* now I need to get the comm map data index */ if (ex_get_idx(exoid, VAR_N_COMM_DATA_IDX, varidx, map_idx) == -1) { exerrval = status; sprintf(errmsg, "Error: failed to find index variable, \"%s\", in file ID %d", VAR_N_COMM_DATA_IDX, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* check if I need to get the dimension of the cmap data */ if (varidx[1] == -1) { /* Get the size of the comm maps */ if ((status = nc_inq_dimid(exoid, DIM_NCNT_CMAP, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get dimension ID for \"%s\" in file ID %d", DIM_NCNT_CMAP, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, &ret_val)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find length of dimension \"%s\" in file ID %d", DIM_NCNT_CMAP, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } varidx[1] = ret_val; } /* "if (varidx[1]==-1)" */ start[0] = varidx[0]; count[0] = varidx[1] - varidx[0]; /* Output the node IDs for this comm map */ if ((status = nc_inq_varid(exoid, VAR_N_COMM_NIDS, &varid )) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" in file ID %d", VAR_N_COMM_NIDS, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (ex_int64_status(exoid) & EX_BULK_INT64_API) { status = nc_put_vara_longlong(exoid, varid, start, count, node_ids); } else { status = nc_put_vara_int(exoid, varid, start, count, node_ids); } if (status != NC_NOERR) { fprintf(stderr, "Start, Count = %lu\t%lu\n", (unsigned long)start[0], (unsigned long)count[0]); exerrval = status; sprintf(errmsg, "Error: failed to output vector \"%s\" in file ID %d", VAR_N_COMM_NIDS, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } /* Output the processor IDs for this map */ if ((status = nc_inq_varid(exoid, VAR_N_COMM_PROC, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to find variable ID for \"%s\" in file ID %d", VAR_N_COMM_PROC, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } if (ex_int64_status(exoid) & EX_BULK_INT64_API) { status = nc_put_vara_longlong(exoid, varid, start, count, proc_ids); } else { status = nc_put_vara_int(exoid, varid, start, count, proc_ids); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to output variable \"%s\" in file ID %d", VAR_N_COMM_PROC, exoid); ex_err(func_name, errmsg, exerrval); return (EX_FATAL); } return (EX_NOERR); }
int main(int argc,char *argv[]) { struct DataMap *ptr; struct DataMapScalar *sx,*sy; struct DataMapArray *ax,*ay; size_t index[256]; size_t start[256]; size_t count[256]; int s; unsigned char vbflg=0; unsigned char help=0; unsigned char option=0; unsigned char zflg=0; FILE *fp=NULL; gzFile zfp=0; FILE *mapfp; int n,c,x; int ncid; int block=0; int varid; int strsze; char **strptr; char *tmpbuf=NULL; OptionAdd(&opt,"-help",'x',&help); OptionAdd(&opt,"-option",'x',&option); OptionAdd(&opt,"vb",'x',&vbflg); OptionAdd(&opt,"z",'x',&zflg); if (argc>1) { arg=OptionProcess(1,argc,argv,&opt,NULL); if (help==1) { OptionPrintInfo(stdout,hlpstr); exit(0); } if (option==1) { OptionDump(stdout,&opt); exit(0); } if (zflg) { zfp=gzopen(argv[arg],"r"); if (zfp==0) { fprintf(stderr,"File not found.\n"); exit(-1); } } else { fp=fopen(argv[arg],"r"); if (fp==NULL) { fprintf(stderr,"File not found.\n"); exit(-1); } } } else { OptionPrintInfo(stdout,errstr); exit(-1); } /* load the map */ mapfp=fopen(argv[arg+1],"r"); loadmap(mapfp); fclose(mapfp); s=nc_open(argv[arg+2],NC_WRITE,&ncid); if (s !=NC_NOERR) { fprintf(stderr,"Error opening CDF file.\n"); exit(-1); } block=0; while (1) { if (zflg) ptr=DataMapReadZ(zfp); else ptr=DataMapFread(fp); if (ptr==NULL) break; for (c=0;c<ptr->snum;c++) { sx=ptr->scl[c]; for (n=0;n<snum;n++) { sy=sptr[n]; if (strcmp(sx->name,sy->name) !=0) continue; if (sx->type !=sy->type) continue; break; } if (n !=snum) { /* mapped variable */ s=nc_inq_varid(ncid,cdfsname[n],&varid); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } index[0]=block; switch (sx->type) { case DATACHAR: s=nc_put_var1_text(ncid,varid,index,sx->data.cptr); break; case DATASHORT: s=nc_put_var1_short(ncid,varid,index,sx->data.sptr); break; case DATAINT: s=nc_put_var1_int(ncid,varid,index,sx->data.iptr); break; case DATAFLOAT: s=nc_put_var1_float(ncid,varid,index,sx->data.fptr); break; case DATADOUBLE: s=nc_put_var1_double(ncid,varid,index,sx->data.dptr); break; case DATASTRING: start[0]=block; start[1]=0; count[0]=1; count[1]=strlen(*((char **) sx->data.vptr))+1; s=nc_put_vara_text(ncid,varid,start,count, *((char **) sx->data.vptr)); break; } if (s !=NC_NOERR) { fprintf(stderr,"Error writing CDF file (%d).\n",s); exit(-1); } } } for (c=0;c<ptr->anum;c++) { ax=ptr->arr[c]; for (n=0;n<anum;n++) { ay=aptr[n]; if (strcmp(ax->name,ay->name) !=0) continue; if (ax->type !=ay->type) continue; if (ax->dim !=ay->dim) continue; break; } if (n !=anum) { /* mapped variable */ s=nc_inq_varid(ncid,cdfaname[n],&varid); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } start[0]=block; count[0]=1; n=1; for (x=0;x<ax->dim;x++) { start[1+x]=0; count[1+x]=ax->rng[x]; n=n*ax->rng[x]; } if (ax->type==DATASTRING) { int ndims; int dimids[NC_MAX_VAR_DIMS]; size_t dimlen; s=nc_inq_varndims(ncid,varid,&ndims); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } s=nc_inq_vardimid(ncid,varid,dimids); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } if (ndims-2!=ax->dim) { fprintf(stderr,"Error matching dimensions.\n"); exit(-1); } s=nc_inq_dimlen(ncid,dimids[ndims-1],&dimlen); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } strsze=dimlen; tmpbuf=malloc(n*strsze); if (tmpbuf==NULL) { fprintf(stderr,"Failed to allocate buffer.\n"); exit(-1); } memset(tmpbuf,0,n*strsze); start[1+ax->dim]=0; count[1+ax->dim]=strsze; strptr=(char **) ax->data.vptr; for (x=0;x<n;x++) strncpy(tmpbuf+x*strsze,strptr[x],strsze); } switch (ax->type) { case DATACHAR: s=nc_put_vara_text(ncid,varid,start,count,ax->data.cptr); break; case DATASHORT: s=nc_put_vara_short(ncid,varid,start,count,ax->data.sptr); break; case DATAINT: s=nc_put_vara_int(ncid,varid,start,count,ax->data.iptr); break; case DATAFLOAT: s=nc_put_vara_float(ncid,varid,start,count,ax->data.fptr); break; case DATADOUBLE: s=nc_put_vara_double(ncid,varid,start,count,ax->data.dptr); break; case DATASTRING: s=nc_put_vara_text(ncid,varid,start,count,tmpbuf); break; } if (tmpbuf !=NULL) { free(tmpbuf); tmpbuf=NULL; } if (s !=NC_NOERR) { fprintf(stderr,"Error writing CDF file (%d).\n",s); exit(-1); } } } DataMapFree(ptr); block++; } nc_close(ncid); if (zflg) gzclose(zfp); else fclose(fp); return 0; }
int main(int argc, char **argv) { /* MPI stuff. */ int mpi_namelen; char mpi_name[MPI_MAX_PROCESSOR_NAME]; int mpi_size, mpi_rank; MPI_Comm comm = MPI_COMM_WORLD; MPI_Info info = MPI_INFO_NULL; double start_time = 0, total_time; /* Netcdf-4 stuff. */ int ncid, varid, dimids[NDIMS]; size_t start[NDIMS] = {0, 0, 0}; size_t count[NDIMS] = {1, DIMSIZE, DIMSIZE}; int data[DIMSIZE * DIMSIZE], data_in[DIMSIZE * DIMSIZE]; int j, i; char file_name[NC_MAX_NAME + 1]; int ndims_in, nvars_in, natts_in, unlimdimid_in; #ifdef USE_MPE int s_init, e_init, s_define, e_define, s_write, e_write, s_close, e_close; #endif /* USE_MPE */ /* Initialize MPI. */ MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); MPI_Get_processor_name(mpi_name, &mpi_namelen); /*printf("mpi_name: %s size: %d rank: %d\n", mpi_name, mpi_size, mpi_rank);*/ /* Must be able to evenly divide my slabs between processors. */ if (NUM_SLABS % mpi_size != 0) { if (!mpi_rank) printf("NUM_SLABS (%d) is not evenly divisible by mpi_size(%d)\n", NUM_SLABS, mpi_size); ERR; } #ifdef USE_MPE MPE_Init_log(); s_init = MPE_Log_get_event_number(); e_init = MPE_Log_get_event_number(); s_define = MPE_Log_get_event_number(); e_define = MPE_Log_get_event_number(); s_write = MPE_Log_get_event_number(); e_write = MPE_Log_get_event_number(); s_close = MPE_Log_get_event_number(); e_close = MPE_Log_get_event_number(); s_open = MPE_Log_get_event_number(); e_open = MPE_Log_get_event_number(); MPE_Describe_state(s_init, e_init, "Init", "red"); MPE_Describe_state(s_define, e_define, "Define", "yellow"); MPE_Describe_state(s_write, e_write, "Write", "green"); MPE_Describe_state(s_close, e_close, "Close", "purple"); MPE_Describe_state(s_open, e_open, "Open", "blue"); MPE_Start_log(); MPE_Log_event(s_init, 0, "start init"); #endif /* USE_MPE */ /* if (!mpi_rank) */ /* { */ /* printf("\n*** Testing parallel I/O some more.\n"); */ /* printf("*** writing a %d x %d x %d file from %d processors...\n", */ /* NUM_SLABS, DIMSIZE, DIMSIZE, mpi_size); */ /* } */ /* We will write the same slab over and over. */ for (i = 0; i < DIMSIZE * DIMSIZE; i++) data[i] = mpi_rank; #ifdef USE_MPE MPE_Log_event(e_init, 0, "end init"); MPE_Log_event(s_define, 0, "start define file"); #endif /* USE_MPE */ /* Create a parallel netcdf-4 file. */ sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); if (nc_create_par(file_name, NC_PNETCDF, comm, info, &ncid)) ERR; /* A global attribute holds the number of processors that created * the file. */ if (nc_put_att_int(ncid, NC_GLOBAL, "num_processors", NC_INT, 1, &mpi_size)) ERR; /* Create three dimensions. */ if (nc_def_dim(ncid, DIM1_NAME, NUM_SLABS, dimids)) ERR; if (nc_def_dim(ncid, DIM2_NAME, DIMSIZE, &dimids[1])) ERR; if (nc_def_dim(ncid, DIM3_NAME, DIMSIZE, &dimids[2])) ERR; /* Create one var. */ if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIMS, dimids, &varid)) ERR; /* Write metadata to file. */ if (nc_enddef(ncid)) ERR; #ifdef USE_MPE MPE_Log_event(e_define, 0, "end define file"); if (mpi_rank) sleep(mpi_rank); #endif /* USE_MPE */ /* if (nc_var_par_access(ncid, varid, NC_COLLECTIVE)) ERR;*/ if (nc_var_par_access(ncid, varid, NC_INDEPENDENT)) ERR; if (!mpi_rank) start_time = MPI_Wtime(); /* Write all the slabs this process is responsible for. */ for (i = 0; i < NUM_SLABS / mpi_size; i++) { start[0] = NUM_SLABS / mpi_size * mpi_rank + i; #ifdef USE_MPE MPE_Log_event(s_write, 0, "start write slab"); #endif /* USE_MPE */ /* Write one slab of data. */ if (nc_put_vara_int(ncid, varid, start, count, data)) ERR; #ifdef USE_MPE MPE_Log_event(e_write, 0, "end write file"); #endif /* USE_MPE */ } if (!mpi_rank) { total_time = MPI_Wtime() - start_time; /* printf("num_proc\ttime(s)\n");*/ printf("%d\t%g\t%g\n", mpi_size, total_time, DIMSIZE * DIMSIZE * NUM_SLABS * sizeof(int) / total_time); } #ifdef USE_MPE MPE_Log_event(s_close, 0, "start close file"); #endif /* USE_MPE */ /* Close the netcdf file. */ if (nc_close(ncid)) ERR; #ifdef USE_MPE MPE_Log_event(e_close, 0, "end close file"); #endif /* USE_MPE */ /* Reopen the file and check it. */ if (nc_open_par(file_name, NC_NOWRITE, comm, info, &ncid)) ERR; if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR; if (ndims_in != NDIMS || nvars_in != 1 || natts_in != 1 || unlimdimid_in != -1) ERR; /* Read all the slabs this process is responsible for. */ for (i = 0; i < NUM_SLABS / mpi_size; i++) { start[0] = NUM_SLABS / mpi_size * mpi_rank + i; #ifdef USE_MPE MPE_Log_event(s_read, 0, "start read slab"); #endif /* USE_MPE */ /* Read one slab of data. */ if (nc_get_vara_int(ncid, varid, start, count, data_in)) ERR; /* Check data. */ for (j = 0; j < DIMSIZE * DIMSIZE; j++) if (data_in[j] != mpi_rank) { ERR; break; } #ifdef USE_MPE MPE_Log_event(e_read, 0, "end read file"); #endif /* USE_MPE */ } #ifdef USE_MPE MPE_Log_event(s_close, 0, "start close file"); #endif /* USE_MPE */ /* Close the netcdf file. */ if (nc_close(ncid)) ERR; #ifdef USE_MPE MPE_Log_event(e_close, 0, "end close file"); #endif /* USE_MPE */ /* Delete this large file. */ remove(file_name); /* Shut down MPI. */ MPI_Finalize(); /* if (!mpi_rank) */ /* { */ /* SUMMARIZE_ERR; */ /* FINAL_RESULTS; */ /* } */ return total_err; }