/** * Writes the unknwons to a netCDF-file (-> constructor) with respect to the boundary sizes. * * boundarySize[0] == left * boundarySize[1] == right * boundarySize[2] == bottom * boundarySize[3] == top * * @param i_h water heights at a given time step. * @param i_hu momentums in x-direction at a given time step. * @param i_hv momentums in y-direction at a given time step. * @param i_boundarySize size of the boundaries. * @param i_time simulation time of the time step. */ void io::NetCdfWriter::writeTimeStep( const Float2D &i_h, const Float2D &i_hu, const Float2D &i_hv, float i_time) { if (timeStep == 0) // Write bathymetry writeVarTimeIndependent(b, bVar); //write i_time nc_put_var1_float(dataFile, timeVar, &timeStep, &i_time); //write water height writeVarTimeDependent(i_h, hVar); //write momentum in x-direction writeVarTimeDependent(i_hu, huVar); //write momentum in y-direction writeVarTimeDependent(i_hv, hvVar); //Write everything to the file nc_sync(dataFile); // Increment timeStep for next call timeStep++; }
NcBool NcFile::sync( void ) { if (!data_mode()) return 0; if (NcError::set_err( nc_sync(the_id) ) != NC_NOERR) return 0; int i; for (i = 0; i < num_dims(); i++) { if (dimensions[i]->is_valid()) { dimensions[i]->sync(); } else { // someone else added a new dimension dimensions[i] = new NcDim(this,i); } } for (i = 0; i < num_vars(); i++) { if (variables[i]->is_valid()) { variables[i]->sync(); } else { // someone else added a new variable variables[i] = new NcVar(this,i); } } return 1; }
NCstate NCdsHandleGContSaveCache(NCdsHandleGCont_t *gCont, size_t tStep, size_t level) { int status; size_t i = 0, start[4], count[4]; size_t ncidx; if (NCdsHandleGContCLStats(gCont, tStep, level) != NCsucceeded) return (NCfailed); ncidx = gCont->NCindex[tStep]; start[i] = tStep - gCont->NCoffset[ncidx]; count[i++] = 1; if (gCont->LVarIds[ncidx] != NCundefined) { start[i] = level; count[i++] = 1; } start[i] = (size_t) 0; count[i++] = gCont->RowNum; start[i] = (size_t) 0; count[i++] = gCont->ColNum; if ((status = nc_put_vara_double(gCont->NCIds[ncidx], gCont->GVarIds[ncidx], start, count, gCont->Data)) != NC_NOERR) { NCprintNCError (status, "NCdsHandleGContSaveCache"); } if ((gCont->TVarIds[ncidx] != NCundefined) && ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->TVarIds[ncidx], start, gCont->Times + tStep)) != NC_NOERR)) { NCprintNCError (status, "NCdsHandleGContSaveCache"); } if ((gCont->LVarIds[ncidx] != NCundefined) && ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->LVarIds[ncidx], start + 1, gCont->Levels + level)) != NC_NOERR)) { NCprintNCError (status, "NCdsHandleGContSaveCache"); } if ((status = nc_sync(gCont->NCIds[ncidx])) != NC_NOERR) { NCprintNCError (status, "NCdsHandleGContLoadCache"); return (NCfailed); } return (NCsucceeded); }
/** * Create a netCdf-file * Any existing file will be replaced. * * @param i_baseName base name of the netCDF-file to which the data will be written to. * @param i_nX number of cells in the horizontal direction. * @param i_nY number of cells in the vertical direction. * @param i_dX cell size in x-direction. * @param i_dY cell size in y-direction. * @param i_originX * @param i_originY */ io::NetCdfWriter::NetCdfWriter( const std::string &i_baseName, const Float2D &i_b, int i_nX, int i_nY, float i_dX, float i_dY, float i_originX, float i_originY) : io::Writer(i_baseName + ".nc", i_b,{{1, 1, 1, 1}}, i_nX, i_nY), flush(0) { int status; status = nc_create(fileName.c_str(), NC_NETCDF4, &dataFile); //check if the netCDF-file open command succeeded. if (status != NC_NOERR) { assert(false); return; } //dimensions int l_timeDim, l_xDim, l_yDim; nc_def_dim(dataFile, "time", NC_UNLIMITED, &l_timeDim); nc_def_dim(dataFile, "x", nX, &l_xDim); nc_def_dim(dataFile, "y", nY, &l_yDim); //variables (TODO: add rest of CF-1.5) int l_xVar, l_yVar; nc_def_var(dataFile, "time", NC_FLOAT, 1, &l_timeDim, &timeVar); ncPutAttText(timeVar, "long_name", "Time"); ncPutAttText(timeVar, "units", "seconds since simulation start"); // the word "since" is important for the paraview reader nc_def_var(dataFile, "x", NC_FLOAT, 1, &l_xDim, &l_xVar); nc_def_var(dataFile, "y", NC_FLOAT, 1, &l_yDim, &l_yVar); //variables, fastest changing index is on the right (C syntax), will be mirrored by the library int dims[] = {l_timeDim, l_yDim, l_xDim}; nc_def_var(dataFile, "b", NC_FLOAT, 3, dims, &bVar); //set attributes to match CF-1.5 convention ncPutAttText(NC_GLOBAL, "Conventions", "CF-1.5"); ncPutAttText(NC_GLOBAL, "title", "Computed tsunami solution"); ncPutAttText(NC_GLOBAL, "history", "SWE"); ncPutAttText(NC_GLOBAL, "institution", "Technische Universitaet Muenchen, Department of Informatics, Chair of Scientific Computing"); ncPutAttText(NC_GLOBAL, "source", "Bathymetry and displacement data."); ncPutAttText(NC_GLOBAL, "references", "http://www5.in.tum.de/SWE"); ncPutAttText(NC_GLOBAL, "comment", "SWE is free software and licensed under the GNU General Public License. Remark: In general this does not hold for the used input data."); //setup grid size float gridPosition = i_originX + (float).5 * i_dX; for(size_t i = 0; i < nX; i++) { nc_put_var1_float(dataFile, l_xVar, &i, &gridPosition); gridPosition += i_dX; } gridPosition = i_originY + (float).5 * i_dY; for(size_t j = 0; j < nY; j++) { nc_put_var1_float(dataFile, l_yVar, &j, &gridPosition); gridPosition += i_dY; } nc_sync(dataFile); }
void sdatio_sync(struct sdatio_file * sfile){ int retval; /*if (sfile->is_parallel){}*/ /*else {*/ if ((retval = nc_sync(sfile->nc_file_id))) ERR(retval); /*}*/ }
/** * Writes the unknwons to a netCDF-file (-> constructor) with respect to the boundary sizes. * * boundarySize[0] == left * boundarySize[1] == right * boundarySize[2] == bottom * boundarySize[3] == top * * @param i_h water heights at a given time step. * @param i_hu momentums in x-direction at a given time step. * @param i_hv momentums in y-direction at a given time step. * @param i_boundarySize size of the boundaries. * @param i_time simulation time of the time step. */ void io::NetCdfWriter::writeTimeStep( const Float2D &i_h, const Float2D &i_hu, const Float2D &i_hv, float i_time, bool i_writeBathymetry) { /* * Prints the arrays for(int row = 0; row < i_h.getRows(); row++) for(int col = 0; col < i_h.getCols(); col++) { std::cout << "Row " << row << ", Column " << col << std::endl; std::cout << i_h[row][col] << " " << i_hu[row][col] << " " << i_hv[row][col] << std::endl; } */ if (timeStep == 0 && i_writeBathymetry) // Write bathymetry writeVarTimeIndependent(b, bVar); //write i_time nc_put_var1_float(dataFile, timeVar, &timeStep, &i_time); //write water height writeVarTimeDependent(i_h, hVar); //write momentum in x-direction writeVarTimeDependent(i_hu, huVar); //write momentum in y-direction writeVarTimeDependent(i_hv, hvVar); // Increment timeStep for next call timeStep++; if (flush > 0 && timeStep % flush == 0) nc_sync(dataFile); nc_sync(dataFile); #ifndef NDEBUG std:string text = "Wrote to file "; std::ostringstream buff; buff << dataFile; tools::Logger::logger.printString(text + buff.str()); #endif }
NCstate NCdsHandleGContLoadCache(NCdsHandleGCont_t *gCont, size_t sTStep, size_t nTSteps, size_t sLevel, size_t nLevels) { int status; size_t start[4], count[4]; size_t ncidx, tStep, level, row, col; double val; count[0] = 1; if (gCont->LVarIds[0] == NCundefined) { start[1] = (size_t) 0; count[1] = gCont->RowNum; start[2] = (size_t) 0; count[2] = gCont->ColNum; } else { count[1] = 1; start[2] = (size_t) 0; count[2] = gCont->RowNum; start[3] = (size_t) 0; count[3] = gCont->ColNum; } for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) { gCont->ObsNum[gCont->ColNum * row + col] = (size_t) 0; gCont->Data[gCont->ColNum * row + col] = (double) 0.0; } for (tStep = sTStep; tStep < sTStep + nTSteps; tStep++) for (level = sLevel; level < sLevel + nLevels; level++) { ncidx = gCont->NCindex[tStep]; start[0] = tStep - gCont->NCoffset[ncidx]; if (gCont->LVarIds[ncidx] != NCundefined) start[1] = level; if ((status = nc_sync(gCont->NCIds[ncidx])) != NC_NOERR) { NCprintNCError (status, "NCdsHandleGContLoadCache"); return (NCfailed); } if ((status = nc_get_vara_double(gCont->NCIds[ncidx], gCont->GVarIds[ncidx], start, count, gCont->AuxData)) != NC_NOERR) { NCprintNCError (status, "NCdsHandleGContLoadCache"); return (NCfailed); } for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) if (_NCdsHandleGContTestNodata(gCont, (val = gCont->AuxData[gCont->ColNum * row + col])) == false) { gCont->Data[gCont->ColNum * row + col] += val; gCont->ObsNum[gCont->ColNum * row + col] += 1; } } for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) if (gCont->ObsNum[gCont->ColNum * row + col] == 0) NCdsHandleGContSetFill(gCont, row, col); else if (gCont->ObsNum[gCont->ColNum * row + col] > 1) gCont->Data[gCont->ColNum * row + col] /= (double) gCont->ObsNum[gCont->ColNum * row + col]; return (NCsucceeded); }
// 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; }
int ncsync(int ncid) { const int status = nc_sync(ncid); if(status != NC_NOERR) { nc_advise("ncsync", status, "ncid %d", ncid); return -1; } return 0; }
/*! * updates and then closes an open EXODUS II file */ int ex_close (int exoid) { char errmsg[MAX_ERR_LENGTH]; int status; exerrval = 0; /* clear error code */ if ((status = nc_sync(exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg,"Error: failed to update file id %d",exoid); ex_err("ex_close",errmsg,exerrval); return(EX_FATAL); } /* Check header size. Will print message if too big... */ ex_header_size(exoid); if ((status = nc_close (exoid)) == NC_NOERR) { ex_conv_exit(exoid); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_NODE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_SIDE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_NODE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_MAP)); ex_rm_stat_ptr (exoid, &exoII_ed); ex_rm_stat_ptr (exoid, &exoII_fa); ex_rm_stat_ptr (exoid, &exoII_eb); ex_rm_stat_ptr (exoid, &exoII_ns); ex_rm_stat_ptr (exoid, &exoII_es); ex_rm_stat_ptr (exoid, &exoII_fs); ex_rm_stat_ptr (exoid, &exoII_ss); ex_rm_stat_ptr (exoid, &exoII_els); ex_rm_stat_ptr (exoid, &exoII_nm); ex_rm_stat_ptr (exoid, &exoII_edm); ex_rm_stat_ptr (exoid, &exoII_fam); ex_rm_stat_ptr (exoid, &exoII_em); } else { exerrval = status; sprintf(errmsg, "Error: failed to close file id %d",exoid); ex_err("ex_close",errmsg, status); return(EX_FATAL); } return(EX_NOERR); }
int main() { /* create nc_enddef.nc */ int ncid; /* netCDF id */ /* dimension ids */ int dim_dim; /* dimension lengths */ size_t dim_len = 1; /* variable ids */ int var_id; /* rank (number of dimensions) for each variable */ # define RANK_var 1 /* variable shapes */ int var_dims[RANK_var]; /* enter define mode */ int stat = nc_create("nc_enddef.nc", NC_CLOBBER, &ncid); check_err(stat,__LINE__,__FILE__); /* define dimensions */ stat = nc_def_dim(ncid, "dim", dim_len, &dim_dim); check_err(stat,__LINE__,__FILE__); /* define variables */ var_dims[0] = dim_dim; stat = nc_def_var(ncid, "var", NC_DOUBLE, RANK_var, var_dims, &var_id); check_err(stat,__LINE__,__FILE__); /* leave define mode */ stat = nc_enddef (ncid); check_err(stat,__LINE__,__FILE__); { /* store var */ static double var[] = {1.}; stat = nc_put_var_double(ncid, var_id, var); check_err(stat,__LINE__,__FILE__); } stat = nc_sync(ncid); check_err(stat,__LINE__,__FILE__); stat = nc_close(ncid); check_err(stat,__LINE__,__FILE__); return 0; }
int ex_update(int exoid) { char errmsg[MAX_ERR_LENGTH]; int status; exerrval = 0; /* clear error code */ if ((status = nc_sync(exoid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to update file id %d", exoid); ex_err("ex_update", errmsg, exerrval); return (EX_FATAL); } return (EX_NOERR); }
void io::NetCdfWriter::writeTimeStep( const Float2D &i_h, const Float2D &i_hu, const Float2D &i_hv, const Float2D &i_b, float i_time) { //write i_time nc_put_var1_float(dataFile, timeVar, &timeStep, &i_time); //write water height writeVarTimeDependent(i_h, hVar); //write momentum in x-direction writeVarTimeDependent(i_hu, huVar); //write momentum in y-direction writeVarTimeDependent(i_hv, hvVar); //write bathymetry writeVarTimeDependent(i_b, bVar); // Increment timeStep for next call timeStep++; if (flush > 0 && timeStep % flush == 0) nc_sync(dataFile); nc_sync(dataFile); #ifndef NDEBUG std:string text = "Wrote to file "; std::ostringstream buff; buff << dataFile; tools::Logger::logger.printString(text + buff.str()); #endif }
/*! This function makes a synchronisation of the disk copy of a netCDF dataset. \param [in] ncid Id groupd(or File Id) \return Status code */ int CNetCdfInterface::sync(int ncid) { int status = nc_sync(ncid); if (NC_NOERR != status) { StdString errormsg(nc_strerror(status)); StdStringStream sstr; sstr << "Error when calling function nc_sync(ncid)" << std::endl; sstr << errormsg << std::endl; sstr << "Unable to make a synchronization of a netCDF file with id: " << ncid << std::endl; StdString e = sstr.str(); throw CNetCdfException(e); } return status; }
// Traj_AmberNetcdf::writeFrame() int Traj_AmberNetcdf::writeFrame(int set, double *X, double *V, double *box, double T) { DoubleToFloat(Coord_, X); // Write coords start_[0] = ncframe_; start_[1] = 0; start_[2] = 0; count_[0] = 1; count_[1] = Ncatom(); count_[2] = 3; if (checkNCerr(nc_put_vara_float(ncid_,coordVID_,start_,count_,Coord_)) ) { mprinterr("Error: Netcdf Writing frame %i\n",set); return 1; } // Write box if (cellLengthVID_ != -1) { count_[1] = 3; count_[2] = 0; if (checkNCerr(nc_put_vara_double(ncid_,cellLengthVID_,start_,count_,box)) ) { mprinterr("Error: Writing cell lengths.\n"); return 1; } if (checkNCerr(nc_put_vara_double(ncid_,cellAngleVID_,start_,count_, box+3)) ) { mprinterr("Error: Writing cell angles.\n"); return 1; } } // Write temperature if (TempVID_!=-1) { if ( checkNCerr( nc_put_vara_double(ncid_,TempVID_,start_,count_,&T)) ) { mprinterr("Error: Writing temperature.\n"); return 1; } } nc_sync(ncid_); // Necessary after every write?? ++ncframe_; return 0; }
void ex_update_max_name_length(int exoid, int length) { int status; int db_length = 0; /* Get current value of the maximum_name_length attribute... */ if ((status = nc_get_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &db_length)) != NC_NOERR) { char errmsg[MAX_ERR_LENGTH]; exerrval = status; sprintf(errmsg, "Error: failed to update 'max_name_length' attribute in file id %d", exoid); ex_err("ex_update_max_name_length",errmsg,exerrval); } if (length > db_length) { /* Update with new value... */ nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &length); nc_sync(exoid); } }
/*! The function ex_close() updates and then closes an open exodus file. \return In case of an error, ex_close() returns a negative number; a warning will return a positive number. Possible causes of errors include: - data file not properly opened with call to ex_create() or ex_open() \param exoid exodus file ID returned from a previous call to ex_create() or ex_open(). The following code segment closes an open exodus file: \code int error,exoid; error = ex_close (exoid); \endcode */ int ex_close (int exoid) { char errmsg[MAX_ERR_LENGTH]; int status; int parent_id = 0; exerrval = 0; /* clear error code */ /* * NOTE: If using netcdf-4, exoid must refer to the root group. * Need to determine whether there are any groups and if so, * call ex_rm_file_item and ex_rm_stat_ptr on each group. */ #if defined(ENABLE_NETCDF4) /* nc_inq_grp_parent() will return NC_ENOGRP error if exoid * refers to the root group (which is what we want) */ if ((status = nc_inq_grp_parent(exoid, &parent_id)) != NC_ENOGRP) { exerrval = EX_NOTROOTID; sprintf(errmsg,"Error: file id %d does not refer to root group.",exoid); ex_err("ex_close",errmsg,exerrval); return(EX_FATAL); } #endif if ((status = nc_sync(exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg,"Error: failed to update file id %d",exoid); ex_err("ex_close",errmsg,exerrval); return(EX_FATAL); } if ((status = nc_close (exoid)) == NC_NOERR) { ex_conv_exit(exoid); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_NODE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_SIDE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_NODE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_MAP)); ex_rm_stat_ptr (exoid, &exoII_ed); ex_rm_stat_ptr (exoid, &exoII_fa); ex_rm_stat_ptr (exoid, &exoII_eb); ex_rm_stat_ptr (exoid, &exoII_ns); ex_rm_stat_ptr (exoid, &exoII_es); ex_rm_stat_ptr (exoid, &exoII_fs); ex_rm_stat_ptr (exoid, &exoII_ss); ex_rm_stat_ptr (exoid, &exoII_els); ex_rm_stat_ptr (exoid, &exoII_nm); ex_rm_stat_ptr (exoid, &exoII_edm); ex_rm_stat_ptr (exoid, &exoII_fam); ex_rm_stat_ptr (exoid, &exoII_em); } else { exerrval = status; sprintf(errmsg, "Error: failed to close file id %d",exoid); ex_err("ex_close",errmsg, status); return(EX_FATAL); } return(EX_NOERR); }
int nc_sync_sub(int ncid) { int everythingOK = 1; FILE* pipe; /* IPC pipe to child process */ setbuf(stdout, NULL); /* unbuffer stdout */ /* * Execute the child process. */ pipe = popen("nc_sync_child", "w"); if (pipe == NULL) { perror("popen() error"); everythingOK = 0; } else { double var[DIM2][DIM1][DIM0]; setbuf(pipe, NULL); /* unbuffer IPC pipe */ /* * Initialize the variable array. */ { int i = 0; int n = DIM0*DIM1*DIM2; double *dp = (double*)var; for (i = 0; i < n; i++) *dp++ = i; } /* * Write the variable array. */ { int i3; int ncerr; size_t start[NDIM] = {0, 0, 0, 0}; size_t count[NDIM] = {1, DIM2, DIM1, DIM0}; /* * Loop over the unlimited dimension. */ for (i3 = 0; everythingOK && i3 < DIM3; ++i3) { int ivar; start[0] = i3; printf("PARENT: Writing %d\n", i3); fflush(stdout); /* * Loop over the variables. */ for (ivar = 0; everythingOK && ivar < NVAR; ++ivar) { ncerr = nc_put_vara_double( ncid, ivar, start, count, (double*)var); if (ncerr != NC_NOERR) { fprintf( stderr, "nc_put_vara_double() error: %s\n", nc_strerror(ncerr)); everythingOK = 0; } } if (everythingOK) { /* * Synchronize the netCDF file. */ puts("PARENT: Calling nc_sync()"); fflush(stdout); ncerr = nc_sync(ncid); if (ncerr != NC_NOERR) { fprintf( stderr, "nc_sync() error: %s\n", nc_strerror(ncerr)); everythingOK = 0; } else { /* * Notify the child process. */ puts("PARENT: Notifying child"); fflush(stdout); if (fwrite(&i3, sizeof(i3), 1, pipe) != 1) { perror("fwrite() error"); everythingOK = 0; } } } /* variables written */ } /* unlimited dimension loop */ } /* write block */ fclose(pipe); { int status; pid_t pid; pid = wait(&status); } } /* successful popen() */ return everythingOK ? 0 : 1; }
void write_cdfpart_cluster(pccluster t, int nc_file, const char *prefix) { size_t clusters, clusteridx; size_t coeffs, coeffidx; char *buf; int bufsize; int nc_sons, nc_size, nc_idx, nc_coeff; int nc_clusters, nc_coeffs, nc_totalsize, nc_dim; int result; /* Prepare buffer for prefixed names */ bufsize = strlen(prefix) + 16; buf = (char *) allocmem(sizeof(char) * bufsize); /* Count number of clusters and coefficients */ clusters = 0; coeffs = 0; write_count(t, &clusters, &coeffs); /* Switch NetCDF file to define mode */ result = nc_redef(nc_file); assert(result == NC_NOERR || result == NC_EINDEFINE); /* Define "clusters" dimension */ prefix_name(buf, bufsize, prefix, "clusters"); result = nc_def_dim(nc_file, buf, clusters, &nc_clusters); assert(result == NC_NOERR); /* Define "coeffs" dimension */ prefix_name(buf, bufsize, prefix, "coeffs"); result = nc_def_dim(nc_file, buf, coeffs, &nc_coeffs); assert(result == NC_NOERR); /* Define "totalsize" dimension */ prefix_name(buf, bufsize, prefix, "totalsize"); result = nc_def_dim(nc_file, buf, t->size, &nc_totalsize); assert(result == NC_NOERR); /* Define "dim" dimension */ prefix_name(buf, bufsize, prefix, "dim"); result = nc_def_dim(nc_file, buf, t->dim, &nc_dim); assert(result == NC_NOERR); /* Define "sons" variable */ prefix_name(buf, bufsize, prefix, "sons"); result = nc_def_var(nc_file, buf, NC_INT, 1, &nc_clusters, &nc_sons); assert(result == NC_NOERR); /* Define "size" variable */ prefix_name(buf, bufsize, prefix, "size"); result = nc_def_var(nc_file, buf, NC_INT, 1, &nc_clusters, &nc_size); assert(result == NC_NOERR); /* Define "idx" variable */ prefix_name(buf, bufsize, prefix, "idx"); result = nc_def_var(nc_file, buf, NC_INT, 1, &nc_totalsize, &nc_idx); assert(result == NC_NOERR); /* Define "coeff" variable */ prefix_name(buf, bufsize, prefix, "coeff"); result = nc_def_var(nc_file, buf, NC_DOUBLE, 1, &nc_coeffs, &nc_coeff); assert(result == NC_NOERR); /* Finish NetCDF define mode */ result = nc_enddef(nc_file); assert(result == NC_NOERR); /* Write index to NetCDF variable */ result = nc_put_var(nc_file, nc_idx, t->idx); /* Write coefficiencs to NetCDF variables */ clusteridx = 0; coeffidx = 0; write_cdf(t, clusters, coeffs, &clusteridx, &coeffidx, nc_file, nc_sons, nc_size, nc_coeff); assert(clusteridx == clusters); assert(coeffidx == coeffs); /* Clean up */ nc_sync(nc_file); freemem(buf); }
NCdsHandle_t *NCdsHandleCreate (const char *pattern, const char *name, int dncid, NCtimeStep tsMode, utUnit *tUnitIn, double sTime, double eTime) { size_t i, j, strLen, strOffset, ncNum = 0, index; char *str [] = { "{year}", "{month}", "{day}", "{hour}", "{minute}", "{second}" }; char *searchStr, *subStr, **fileNames = (char **) NULL, tsUnitStr [NCsizeString]; int *ncids = (int *) NULL, tvarid, status; int endYear, endMonth, year, month, day, hour, minute; float second; double time = 0.0, scale, offset; utUnit tUnitOut; NCdsHandle_t *dsh; if ((searchStr = malloc (strlen (pattern) + 1)) == (char *) NULL) { CMmsgPrint (CMmsgSysError, "Memory allocation error in: NCdsHandleCreate ()!"); return ((NCdsHandle_t *) NULL); } if ((utCalendar (eTime,tUnitIn,&endYear,&endMonth,&day,&hour,&minute,&second) != 0) || (utCalendar (sTime,tUnitIn,&year, &month, &day,&hour,&minute,&second) != 0)) { CMmsgPrint (CMmsgAppError, "Calender scanning error in:%s %d",__FILE__,__LINE__); goto ABORT; } switch (tsMode) { case NCtimeYear: sprintf (tsUnitStr,"years since %04d-01-01 00:00 0.0",year); break; case NCtimeMonth: sprintf (tsUnitStr,"months since %04d-%02d-01 00:00 0.0",year, month); break; case NCtimeDay: sprintf (tsUnitStr,"days since %04d-%02d-%02d 00:00 0.0",year, month, day); break; case NCtimeHour: sprintf (tsUnitStr,"hours since %04d-%02d-%02d %02d:00 0.0",year, month, day, hour); break; case NCtimeMinute: sprintf (tsUnitStr,"minutes since %04d-%02d-%02d %02d:%02d 0.0", year, month, day, hour, minute); break; case NCtimeSecond: sprintf (tsUnitStr,"seconds since %04d-%02d-%02d %02d:%02d %.1f", year, month, day, hour, minute, second); break; } if (tsMode > NCtimeMonth) { if ((utScan (tsUnitStr, &tUnitOut) != 0) || (utConvert (tUnitIn, &tUnitOut, &scale, &offset) != 0)) { CMmsgPrint (CMmsgAppError, "Time unit scanning error in: %s %d",__FILE__,__LINE__); goto ABORT; } sTime = sTime * scale + offset; eTime = eTime * scale + offset; } else { sTime = 0.0; eTime = tsMode > NCtimeYear ? (endYear - year) * 12 + (endMonth - month + 1) : (double) (year - endYear); } do { if (tsMode > NCtimeMonth) { if (utCalendar (sTime + time,&tUnitOut,&year,&month,&day,&hour,&minute,&second) != 0) { CMmsgPrint (CMmsgAppError, "Time unit scaning error in: %s %d",__FILE__,__LINE__); goto ABORT; } } strcpy (searchStr, pattern); for (i = 0;i < tsMode; ++i) if ((subStr = strstr (searchStr,str [i])) == (char *) NULL) break; else { strOffset = strlen (str [i]); strLen = strlen (subStr) - strOffset; switch (i) { case NCtimeYear: sprintf (subStr,"%04d", year); subStr += 4; strOffset -= 4; break; case NCtimeMonth: sprintf (subStr,"%02d", month); subStr += 2; strOffset -= 2; break; case NCtimeDay: sprintf (subStr,"%02d", day); subStr += 2; strOffset -= 2; break; case NCtimeHour: sprintf (subStr,"%02d", hour); subStr += 2; strOffset -= 2; break; case NCtimeMinute: sprintf (subStr,"%02d", minute); subStr += 2; strOffset -= 2; break; case NCtimeSecond: sprintf (subStr,"%04.1f",second); subStr += 4; strOffset -= 4; break; } for (j = 0;j <= strLen; j++) subStr [j] = subStr [j + strOffset]; } if ((ncNum == 0) || (strcmp (fileNames [ncNum - 1], searchStr) != 0)) { if (((fileNames = (char **) realloc (fileNames, (ncNum + 1) * sizeof (char *))) == (char **) NULL) || ((ncids = (int *) realloc (ncids, (ncNum + 1) * sizeof (int))) == (int *) NULL)) { CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); goto ABORT; } else ncNum++; if ((fileNames [ncNum - 1] = (char *) malloc (strlen (searchStr) + 1)) == (char *) NULL) { CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); goto ABORT; } strcpy (fileNames [ncNum - 1], searchStr); if (((ncids [ncNum - 1] = NCfileCreate (fileNames [ncNum - 1], dncid)) == NCfailed) || (NCfileVarAdd (ncids [ncNum - 1], name, NC_FLOAT, NC_DOUBLE, NC_FLOAT) == NCfailed) || (NCfileSetTimeUnit (ncids [ncNum - 1], tsUnitStr) == NCfailed) || (NCfileSetMissingVal (ncids [ncNum - 1], -9999.0) == NCfailed) || ((tvarid = NCdataGetTVarId (ncids [ncNum - 1])) == NCfailed)) goto ABORT; index = 0; } if ((status = nc_put_var1_double (ncids [ncNum - 1],tvarid,&index, &time)) != NC_NOERR) { NCprintNCError (status,"NCdsHandleCreate"); goto ABORT; } index++; if (tsMode == NCtimeMonth) { month++; if (month > 12) { month = 1; year++;} } if (tsMode == NCtimeYear) { year++; } time = time + 1.0; } while ((sTime + time) < eTime); for (i = 0;i < ncNum; i++) nc_sync (ncids [i]); if ((dsh = NCdsHandleOpenByIds (ncids, ncNum)) == (NCdsHandle_t *) NULL) goto ABORT; for (i = 0;i < ncNum; i++) free (fileNames [i]); utClear (&tUnitOut); free (fileNames); free (ncids); free (searchStr); return (dsh); ABORT: for (i = 0;i < ncNum;i++) { nc_abort (ncids [i]); unlink (fileNames [i]); if (fileNames [i] != (char *) NULL) free (fileNames [i]); } utClear (&tUnitOut); if (fileNames != (char **) NULL) free (fileNames); free (searchStr); return ((NCdsHandle_t *) NULL); }
/*ARGSUSED*/ int main(int argc, char *argv[]) { int cmode=NC_CLOBBER, omode, ret; int id; char buf[256]; #ifdef SYNCDEBUG char *str = "one"; #endif int ii; size_t ui; const struct tcdfvar *tvp = testvars; union getret got; const size_t initialsz = 8192; size_t chunksz = 8192; size_t align = 8192/32; MPI_Init(&argc, &argv); /* cmode |= NC_PNETCDF |NC_64BIT_OFFSET; */ cmode != NC_PNETCDF |NC_64BIT_DATA; ret = nc_create_par(fname,cmode, MPI_COMM_WORLD, MPI_INFO_NULL, &id); if(ret != NC_NOERR) { fprintf(stderr,"Error %s in file %s at line %d\n",nc_strerror(ret),__FILE__,__LINE__); exit(ret); } assert( nc_put_att_text(id, NC_GLOBAL, "TITLE", 12, "another name") == NC_NOERR); assert( nc_get_att_text(id, NC_GLOBAL, "TITLE", buf) == NC_NOERR); /* (void) printf("title 1 \"%s\"\n", buf); */ assert( nc_put_att_text(id, NC_GLOBAL, "TITLE", strlen(fname), fname) == NC_NOERR); assert( nc_get_att_text(id, NC_GLOBAL, "TITLE", buf) == NC_NOERR); buf[strlen(fname)] = 0; /* (void) printf("title 2 \"%s\"\n", buf); */ assert( strcmp(fname, buf) == 0); createtestdims(id, NUM_DIMS, sizes, dim_names); testdims(id, NUM_DIMS, sizes, dim_names); createtestvars(id, testvars, NUM_TESTVARS); { int ifill = -1; double dfill = -9999; assert( nc_put_att_int(id, Long_id, _FillValue, NC_INT, 1, &ifill) == NC_NOERR); assert( nc_put_att_double(id, Double_id, _FillValue, NC_DOUBLE, 1, &dfill) == NC_NOERR); } #ifdef REDEF assert( nc__enddef(id, 0, align, 0, 2*align) == NC_NOERR ); assert( nc_put_var1_int(id, Long_id, indices[3], &birthday) == NC_NOERR ); fill_seq(id); assert( nc_redef(id) == NC_NOERR ); /* assert( nc_rename_dim(id,2, "a long dim name") == NC_NOERR); */ #endif assert( nc_rename_dim(id,1, "IXX") == NC_NOERR); assert( nc_inq_dim(id, 1, buf, &ui) == NC_NOERR); /* (void) printf("dimrename: %s\n", buf); */ assert( nc_rename_dim(id,1, dim_names[1]) == NC_NOERR); #ifdef ATTRX assert( nc_rename_att(id, 1, "UNITS", "units") == NC_NOERR); assert( nc_del_att(id, 4, "FIELDNAM")== NC_NOERR); assert( nc_del_att(id, 2, "SCALEMIN")== NC_NOERR); assert( nc_del_att(id, 2, "SCALEMAX")== NC_NOERR); #endif /* ATTRX */ assert( nc__enddef(id, 0, align, 0, 2*align) == NC_NOERR ); #ifndef REDEF fill_seq(id); assert( nc_put_var1_int(id, Long_id, indices[3], &birthday)== NC_NOERR ); #endif assert( nc_put_vara_schar(id, Byte_id, s_start, s_edges, (signed char *)sentence) == NC_NOERR); assert( nc_put_var1_schar(id, Byte_id, indices[6], (signed char *)(chs+1)) == NC_NOERR); assert( nc_put_var1_schar(id, Byte_id, indices[5], (signed char *)chs) == NC_NOERR); assert( nc_put_vara_text(id, Char_id, s_start, s_edges, sentence) == NC_NOERR); assert( nc_put_var1_text(id, Char_id, indices[6], (chs+1)) == NC_NOERR) ; assert( nc_put_var1_text(id, Char_id, indices[5], chs) == NC_NOERR); assert( nc_put_var1_short(id, Short_id, indices[4], shs) == NC_NOERR); assert( nc_put_var1_float(id, Float_id, indices[2], &e) == NC_NOERR); assert( nc_put_var1_double(id, Double_id, indices[1], &zed) == NC_NOERR); assert( nc_put_var1_double(id, Double_id, indices[0], &pinot) == NC_NOERR); #ifdef SYNCDEBUG (void) printf("Hit Return to sync\n"); gets(str); nc_sync(id,0); (void) printf("Sync done. Hit Return to continue\n"); gets(str); #endif /* SYNCDEBUG */ ret = nc_close(id); /* (void) printf("nc_close ret = %d\n\n", ret); */ /* * read it */ omode = NC_NOWRITE; omode = NC_NOWRITE | NC_PNETCDF; if(ret != NC_NOERR) { (void) printf("Could not open %s: %s\n", fname, nc_strerror(ret)); exit(1); } /* (void) printf("reopen id = %d for filename %s\n", */ /* id, fname); */ /* NC */ /* (void) printf("NC "); */ assert( nc_inq(id, &(cdesc->num_dims), &(cdesc->num_vars), &(cdesc->num_attrs), &(cdesc->xtendim) ) == NC_NOERR); assert((size_t) cdesc->num_dims == num_dims); assert(cdesc->num_attrs == 1); assert(cdesc->num_vars == NUM_TESTVARS); /* (void) printf("done\n"); */ /* GATTR */ /* (void) printf("GATTR "); */ assert( nc_inq_attname(id, NC_GLOBAL, 0, adesc->mnem) == 0); assert(strcmp("TITLE",adesc->mnem) == 0); assert( nc_inq_att(id, NC_GLOBAL, adesc->mnem, &(adesc->type), &(adesc->len))== NC_NOERR); assert( adesc->type == NC_CHAR ); assert( adesc->len == strlen(fname) ); assert( nc_get_att_text(id, NC_GLOBAL, "TITLE", buf)== NC_NOERR); buf[adesc->len] = 0; assert( strcmp(fname, buf) == 0); /* VAR */ /* (void) printf("VAR "); */ assert( cdesc->num_vars == NUM_TESTVARS ); for(ii = 0; ii < cdesc->num_vars; ii++, tvp++ ) { int jj; assert( nc_inq_var(id, ii, vdesc->mnem, &(vdesc->type), &(vdesc->ndims), vdesc->dims, &(vdesc->num_attrs)) == NC_NOERR); if(strcmp(tvp->mnem , vdesc->mnem) != 0) { (void) printf("attr %d mnem mismatch %s, %s\n", ii, tvp->mnem, vdesc->mnem); continue; } if(tvp->type != vdesc->type) { (void) printf("attr %d type mismatch %d, %d\n", ii, (int)tvp->type, (int)vdesc->type); continue; } for(jj = 0; jj < vdesc->ndims; jj++ ) { if(tvp->dims[jj] != vdesc->dims[jj] ) { (void) printf( "inconsistent dim[%d] for variable %d: %d != %d\n", jj, ii, tvp->dims[jj], vdesc->dims[jj] ); continue; } } /* VATTR */ /* (void) printf("VATTR\n"); */ for(jj=0; jj<vdesc->num_attrs; jj++ ) { assert( nc_inq_attname(id, ii, jj, adesc->mnem) == NC_NOERR); if( strcmp(adesc->mnem, reqattr[jj]) != 0 ) { (void) printf("var %d attr %d mismatch %s != %s\n", ii, jj, adesc->mnem, reqattr[jj] ); break; } } if( nc_inq_att(id, ii, reqattr[0], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_CHAR ); assert( adesc->len == strlen(tvp->units) ); assert( nc_get_att_text(id,ii,reqattr[0],buf)== NC_NOERR); buf[adesc->len] = 0; assert( strcmp(tvp->units, buf) == 0); } if( nc_inq_att(id, ii, reqattr[1], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len == 1 ); assert( nc_get_att_double(id, ii, reqattr[1], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->validmin); } if( nc_inq_att(id, ii, reqattr[2], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len == 1 ); assert( nc_get_att_double(id, ii, reqattr[2], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->validmax); } if( nc_inq_att(id, ii, reqattr[3], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len ==1 ); assert( nc_get_att_double(id, ii, reqattr[3], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->scalemin); } if( nc_inq_att(id, ii, reqattr[4], &(adesc->type), &(adesc->len)) != -1) { assert( adesc->type == NC_DOUBLE ); assert( adesc->len == 1 ); assert( nc_get_att_double(id, ii, reqattr[4], &got.dbl)== NC_NOERR); chkgot(adesc->type, got, tvp->scalemax); } if( nc_inq_att(id, ii, reqattr[5], &(adesc->type), &(adesc->len))== NC_NOERR) { assert( adesc->type == NC_CHAR ); assert( adesc->len == strlen(tvp->fieldnam) ); assert( nc_get_att_text(id,ii,reqattr[5],buf)== NC_NOERR); buf[adesc->len] = 0; assert( strcmp(tvp->fieldnam, buf) == 0); } } /* (void) printf("fill_seq "); */ check_fill_seq(id); /* (void) printf("Done\n"); */ assert( nc_get_var1_double(id, Double_id, indices[0], &got.dbl)== NC_NOERR); /* (void) printf("got val = %f\n", got.dbl ); */ assert( nc_get_var1_double(id, Double_id, indices[1], &got.dbl)== NC_NOERR); /* (void) printf("got val = %f\n", got.dbl ); */ assert( nc_get_var1_float(id, Float_id, indices[2], &got.fl[0])== NC_NOERR); /* (void) printf("got val = %f\n", got.fl[0] ); */ assert( nc_get_var1_int(id, Long_id, indices[3], &got.in[0])== NC_NOERR); /* (void) printf("got val = %d\n", got.in[0] ); */ assert( nc_get_var1_short(id, Short_id, indices[4], &got.sh[0])== NC_NOERR); /* (void) printf("got val = %d\n", got.sh[0] ); */ assert( nc_get_var1_text(id, Char_id, indices[5], &got.by[0]) == NC_NOERR); /* (void) printf("got NC_CHAR val = %c (0x%02x) \n", */ /* got.by[0] , got.by[0]); */ assert( nc_get_var1_text(id, Char_id, indices[6], &got.by[0]) == NC_NOERR); /* (void) printf("got NC_CHAR val = %c (0x%02x) \n", */ /* got.by[0], got.by[0] ); */ (void) memset(buf,0,sizeof(buf)); assert( nc_get_vara_text(id, Char_id, s_start, s_edges, buf) == NC_NOERR); /* (void) printf("got NC_CHAR val = \"%s\"\n", buf); */ assert( nc_get_var1_schar(id, Byte_id, indices[5], (signed char *)&got.by[0])== NC_NOERR); /* (void) printf("got val = %c (0x%02x) \n", got.by[0] , got.by[0]); */ assert( nc_get_var1_schar(id, Byte_id, indices[6], (signed char *)&got.by[0])== NC_NOERR); /* (void) printf("got val = %c (0x%02x) \n", got.by[0], got.by[0] ); */ (void) memset(buf,0,sizeof(buf)); assert( nc_get_vara_schar(id, Byte_id, s_start, s_edges, (signed char *)buf)== NC_NOERR ); /* (void) printf("got val = \"%s\"\n", buf); */ { double dbuf[NUM_RECS * SIZE_1 * SIZE_2]; assert(nc_get_var_double(id, Float_id, dbuf) == NC_NOERR); /* (void) printf("got vals = %f ... %f\n", dbuf[0], */ /* dbuf[NUM_RECS * SIZE_1 * SIZE_2 -1] ); */ } ret = nc_close(id); /* (void) printf("re nc_close ret = %d\n", ret); */ MPI_Finalize(); return 0; }
bool CNetCDFTraj::WriteSnapshot(CAmberRestart* p_snap) { if( Mode != AMBER_TRAJ_WRITE ){ ES_ERROR("illegal mode, it should be AMBER_TRAJ_WRITE"); return(false); } if( p_snap == NULL ){ INVALID_ARGUMENT("p_snap == NULL"); } if( p_snap->GetTopology() == NULL ) { ES_ERROR("snapshot does not have assigned topology"); return(false); } if( p_snap->GetNumberOfAtoms() != ActualAtoms ) { CSmallString error; error << "inconsistent number of atoms, trajectory: " << ActualAtoms; error << " topology: " << p_snap->GetNumberOfAtoms(); ES_ERROR(error); return(false); } bool has_box = p_snap->GetTopology()->BoxInfo.GetType() != AMBER_BOX_NONE; if( has_box != HasBox ) { CSmallString error; error << "topology and snapshot has different info about box presence"; ES_ERROR(error); return(false); } if( CoordinateVID < 0 ) { CSmallString error; error << "WriteHeader must be called before WriteSnapshot"; ES_ERROR(error); return(false); } if( Coordinates == NULL ) { CSmallString error; error << "Coordinates are NULL"; ES_ERROR(error); return(false); } int j = 0; for(int i=0; i < ActualAtoms; i++){ CPoint pos = p_snap->GetPosition(i); Coordinates[j++] = pos.x; Coordinates[j++] = pos.y; Coordinates[j++] = pos.z; } int err; size_t start[3]; size_t count[3]; start[0] = CurrentSnapshot; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = ActualAtoms; count[2] = 3; err = nc_put_vara_float(NCID, CoordinateVID, start, count,Coordinates); if( err != NC_NOERR ){ CSmallString error; error << "unable to write coordinates (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } if( HasBox ) { start[0] = CurrentSnapshot; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = 3; count[2] = 0; CPoint box = p_snap->GetBox(); CellLength[0] = box.x; CellLength[1] = box.y; CellLength[2] = box.z; err = nc_put_vara_double(NCID, CellLengthVID, start, count, CellLength); if( err != NC_NOERR ){ CSmallString error; error << "unable to write cell lengths (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } CPoint ang = p_snap->GetAngles(); CellAngle[0] = ang.x; CellAngle[1] = ang.y; CellAngle[2] = ang.z; err = nc_put_vara_double(NCID, CellAngleVID, start, count, CellAngle); if( err != NC_NOERR ){ CSmallString error; error << "unable to write cell angles (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } } start[0] = CurrentSnapshot; count[0] = 1; float time = CurrentSnapshot; err = nc_put_vara_float(NCID, TimeVID, start, count, &time); if( err != NC_NOERR ){ CSmallString error; error << "unable to write time (" << nc_strerror(err) << ")"; ES_ERROR(error); return(false); } CurrentSnapshot++; TotalSnapshots++; nc_sync(NCID); return(true); }
/** * @name PIOc_sync */ int PIOc_sync (int ncid) { int ierr; int msg; int mpierr; iosystem_desc_t *ios; file_desc_t *file; wmulti_buffer *wmb, *twmb; ierr = PIO_NOERR; file = pio_get_file_from_id(ncid); if(file == NULL) return PIO_EBADID; ios = file->iosystem; msg = PIO_MSG_SYNC; if(ios->async_interface && ! ios->ioproc){ if(ios->compmaster) mpierr = MPI_Send(&msg, 1,MPI_INT, ios->ioroot, 1, ios->union_comm); mpierr = MPI_Bcast(&(file->fh),1, MPI_INT, 0, ios->intercomm); } if((file->mode & PIO_WRITE)){ // cn_buffer_report( *ios, true); wmb = &(file->buffer); while(wmb != NULL){ // printf("%s %d %d %d\n",__FILE__,__LINE__,wmb->ioid, wmb->validvars); if(wmb->validvars>0){ flush_buffer(ncid, wmb, true); } twmb = wmb; wmb = wmb->next; if(twmb == &(file->buffer)){ twmb->ioid=-1; twmb->next=NULL; }else{ brel(twmb); } } flush_output_buffer(file, true, 0); if(ios->ioproc){ switch(file->iotype){ #ifdef _NETCDF #ifdef _NETCDF4 case PIO_IOTYPE_NETCDF4P: ierr = nc_sync(file->fh);; break; case PIO_IOTYPE_NETCDF4C: #endif case PIO_IOTYPE_NETCDF: if(ios->io_rank==0){ ierr = nc_sync(file->fh);; } break; #endif #ifdef _PNETCDF case PIO_IOTYPE_PNETCDF: ierr = ncmpi_sync(file->fh);; break; #endif default: ierr = iotype_error(file->iotype,__FILE__,__LINE__); } } ierr = check_netcdf(file, ierr, __FILE__,__LINE__); } return ierr; }
// Traj_AmberNetcdf::writeFrame() int Traj_AmberNetcdf::writeFrame(int set, Frame const& frameOut) { DoubleToFloat(Coord_, frameOut.xAddress()); // Write coords start_[0] = ncframe_; start_[1] = 0; start_[2] = 0; count_[0] = 1; count_[1] = Ncatom(); count_[2] = 3; if (checkNCerr(nc_put_vara_float(ncid_,coordVID_,start_,count_,Coord_)) ) { mprinterr("Error: Netcdf Writing coords frame %i\n", set+1); return 1; } // Write velocity. FIXME: Should check in setup if (CoordInfo().HasVel() && frameOut.HasVelocity()) { DoubleToFloat(Coord_, frameOut.vAddress()); if (checkNCerr(nc_put_vara_float(ncid_, velocityVID_, start_, count_, Coord_)) ) { mprinterr("Error: Netcdf writing velocity frame %i\n", set+1); return 1; } } // Write box if (cellLengthVID_ != -1) { count_[1] = 3; count_[2] = 0; if (checkNCerr(nc_put_vara_double(ncid_,cellLengthVID_,start_,count_,frameOut.bAddress())) ) { mprinterr("Error: Writing cell lengths frame %i.\n", set+1); return 1; } if (checkNCerr(nc_put_vara_double(ncid_,cellAngleVID_,start_,count_, frameOut.bAddress()+3)) ) { mprinterr("Error: Writing cell angles frame %i.\n", set+1); return 1; } } // Write temperature if (TempVID_!=-1) { if ( checkNCerr( nc_put_vara_double(ncid_,TempVID_,start_,count_,frameOut.tAddress())) ) { mprinterr("Error: Writing temperature frame %i.\n", set+1); return 1; } } // Write time if (timeVID_ != -1) { float tVal = (float)frameOut.Time(); if ( checkNCerr( nc_put_vara_float(ncid_,timeVID_,start_,count_,&tVal)) ) { mprinterr("Error: Writing time frame %i.\n", set+1); return 1; } } // Write indices if (indicesVID_ != -1) { count_[1] = remd_dimension_; if ( checkNCerr(nc_put_vara_int(ncid_,indicesVID_,start_,count_,frameOut.iAddress())) ) { mprinterr("Error: Writing indices frame %i.\n", set+1); return 1; } } nc_sync(ncid_); // Necessary after every write?? ++ncframe_; return 0; }
/** * Create a netCdf-file * Any existing file will be replaced. * * @param i_baseName base name of the netCDF-file to which the data will be written to. * @param i_nX number of cells in the horizontal direction. * @param i_nY number of cells in the vertical direction. * @param i_dX cell size in x-direction. * @param i_dY cell size in y-direction. * @param i_originX * @param i_originY * @param i_flush If > 0, flush data to disk every i_flush write operation * @param i_dynamicBathymetry */ io::NetCdfWriter::NetCdfWriter( const std::string &i_baseName, const Float2D &i_b, const BoundarySize &i_boundarySize, int i_nX, int i_nY, float i_dX, float i_dY, float i_originX, float i_originY, unsigned int i_flush, size_t contTimestep, unsigned int compression) : //const bool &i_dynamicBathymetry) : //!TODO io::Writer(i_baseName + ".nc", i_b, i_boundarySize, i_nX, i_nY, contTimestep), flush(i_flush), compress(compression) { int status; if(contTimestep) { status = nc_open(fileName.c_str(), NC_WRITE, &dataFile); //check if the netCDF-file open command succeeded. if (status != NC_NOERR) { assert(false); return; } size_t l_length; if(status = nc_inq_varid(dataFile, "time", &timeVar)) ERR(status); if(status = nc_inq_varid(dataFile, "h", &hVar)) ERR(status); if(status = nc_inq_varid(dataFile, "hu", &huVar)) ERR(status); if(status = nc_inq_varid(dataFile, "hv", &hvVar)) ERR(status); if(status = nc_inq_varid(dataFile, "b", &bVar)) ERR(status); //if(status = nc_inq_dimlen(dataFile, timeVar, &timeStep)) ERR(status); } else { //create a netCDF-file, an existing file will be replaced status = nc_create(fileName.c_str(), NC_NETCDF4, &dataFile); //check if the netCDF-file creation constructor succeeded. if (status != NC_NOERR) { assert(false); return; } // std::cout << "i_nX, i_nY, i_dX, i_dY: " << nX << ", " << nY << ", " << i_dX << ", " << i_dY << std::endl; adjust(nX, nY, i_dX, i_dY, compress); // std::cout << "i_nX, i_nY, i_dX, i_dY: " << nX << ", " << nY << ", " << i_dX << ", " << i_dY << std::endl; #ifdef PRINT_NETCDFWRITER_INFORMATION std::cout << " *** io::NetCdfWriter::createNetCdfFile" << std::endl; std::cout << " created/replaced: " << fileName << std::endl; std::cout << " dimensions(nx, ny): " << nX << ", " << nY << std::endl; std::cout << " cell width(dx,dy): " << i_dX << ", " << i_dY << std::endl; std::cout << " origin(x,y): " << i_originX << ", " << i_originY << std::endl; #endif //dimensions int l_timeDim, l_xDim, l_yDim; nc_def_dim(dataFile, "time", NC_UNLIMITED, &l_timeDim); nc_def_dim(dataFile, "x", nX, &l_xDim); nc_def_dim(dataFile, "y", nY, &l_yDim); //variables (TODO: add rest of CF-1.5) int l_xVar, l_yVar; nc_def_var(dataFile, "time", NC_FLOAT, 1, &l_timeDim, &timeVar); ncPutAttText(timeVar, "long_name", "Time"); ncPutAttText(timeVar, "units", "seconds since simulation start"); // the word "since" is important for the paraview reader nc_def_var(dataFile, "x", NC_FLOAT, 1, &l_xDim, &l_xVar); nc_def_var(dataFile, "y", NC_FLOAT, 1, &l_yDim, &l_yVar); //variables, fastest changing index is on the right (C syntax), will be mirrored by the library int dims[] = {l_timeDim, l_yDim, l_xDim}; nc_def_var(dataFile, "h", NC_FLOAT, 3, dims, &hVar); nc_def_var(dataFile, "hu", NC_FLOAT, 3, dims, &huVar); nc_def_var(dataFile, "hv", NC_FLOAT, 3, dims, &hvVar); nc_def_var(dataFile, "b", NC_FLOAT, 3, dims, &bVar); //set attributes to match CF-1.5 convention ncPutAttText(NC_GLOBAL, "Conventions", "CF-1.5"); ncPutAttText(NC_GLOBAL, "title", "Computed tsunami solution"); ncPutAttText(NC_GLOBAL, "history", "SWE"); ncPutAttText(NC_GLOBAL, "institution", "Technische Universitaet Muenchen, Department of Informatics, Chair of Scientific Computing"); ncPutAttText(NC_GLOBAL, "source", "Bathymetry and displacement data."); ncPutAttText(NC_GLOBAL, "references", "http://www5.in.tum.de/SWE"); ncPutAttText(NC_GLOBAL, "comment", "SWE is free software and licensed under the GNU General Public License. Remark: In general this does not hold for the used input data."); //setup grid size float gridPosition = i_originX + (float).5 * i_dX; for(size_t i = 0; i < nX; i++) { nc_put_var1_float(dataFile, l_xVar, &i, &gridPosition); gridPosition += i_dX; } gridPosition = i_originY + (float).5 * i_dY; for(size_t j = 0; j < nY; j++) { nc_put_var1_float(dataFile, l_yVar, &j, &gridPosition); gridPosition += i_dY; } nc_sync(dataFile); } }
// 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; }
bi::NetCDFBuffer::~NetCDFBuffer() { nc_sync(ncid); nc_close(ncid); }
/*! The function ex_close() updates and then closes an open exodus file. \return In case of an error, ex_close() returns a negative number; a warning will return a positive number. Possible causes of errors include: - data file not properly opened with call to ex_create() or ex_open() \param exoid exodus file ID returned from a previous call to ex_create() or ex_open(). The following code segment closes an open exodus file: ~~~{.c} int error,exoid; error = ex_close (exoid); ~~~ */ int ex_close(int exoid) { char errmsg[MAX_ERR_LENGTH]; int status; int status1; int status2; #if NC_HAS_HDF5 int parent_id = 0; #endif EX_FUNC_ENTER(); ex_check_valid_file_id(exoid, __func__); /* * NOTE: If using netcdf-4, exoid must refer to the root group. * Need to determine whether there are any groups and if so, * call ex_rm_file_item and ex_rm_stat_ptr on each group. */ #if NC_HAS_HDF5 /* nc_inq_grp_parent() will return NC_ENOGRP error if exoid * refers to the root group (which is what we want) */ if ((status = nc_inq_grp_parent(exoid, &parent_id)) != NC_ENOGRP) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file id %d does not refer to root group.", exoid); ex_err(__func__, errmsg, EX_NOTROOTID); EX_FUNC_LEAVE(EX_FATAL); } #endif if ((status1 = nc_sync(exoid)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to update file id %d", exoid); ex_err(__func__, errmsg, status1); } if ((status2 = nc_close(exoid)) != NC_NOERR) { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to close file id %d", exoid); ex_err(__func__, errmsg, status2); } /* Even if we have failures above due to nc_sync() or nc_close(), we still need to clean up our * internal datastructures. */ ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_BLOCK)); ex_rm_file_item(exoid, ex_get_counter_list(EX_NODE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_SIDE_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_SET)); ex_rm_file_item(exoid, ex_get_counter_list(EX_NODE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_EDGE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_FACE_MAP)); ex_rm_file_item(exoid, ex_get_counter_list(EX_ELEM_MAP)); ex_rm_stat_ptr(exoid, &exoII_ed); ex_rm_stat_ptr(exoid, &exoII_fa); ex_rm_stat_ptr(exoid, &exoII_eb); ex_rm_stat_ptr(exoid, &exoII_ns); ex_rm_stat_ptr(exoid, &exoII_es); ex_rm_stat_ptr(exoid, &exoII_fs); ex_rm_stat_ptr(exoid, &exoII_ss); ex_rm_stat_ptr(exoid, &exoII_els); ex_rm_stat_ptr(exoid, &exoII_nm); ex_rm_stat_ptr(exoid, &exoII_edm); ex_rm_stat_ptr(exoid, &exoII_fam); ex_rm_stat_ptr(exoid, &exoII_em); ex_conv_exit(exoid); status = EX_NOERR; if (status1 != NC_NOERR || status2 != NC_NOERR) { status = EX_FATAL; } EX_FUNC_LEAVE(status); }