// 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; }
/** * Write double array data into corresponding variable name */ void store(Property<std::vector<double> > *v) { int retval; int varid; const char *name = v->getName().c_str(); size_t start[2], count[2]; /** * Specify index where the first data will be written */ start[0] = index; start[1] = 0; /** * Specify the number of values that will be written in each dimension */ count[0] = 1; count[1] = v->get().size(); retval = nc_inq_varid(ncid, name, &varid); if (retval) log(Error) << "Could not get variable id of " << name << ", error " << retval <<endlog(); retval = nc_put_vara_double(ncid, varid, start, count, &(v->get().front())); if(retval) log(Error) << "Could not write variable " << name << ", error " << retval <<endlog(); }
int ex_put_varid_var(int exoid, int time_step, int varid, int num_entity, const void *var_vals) { size_t start[2], count[2]; int status; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ start[0] = --time_step; start[1] = 0; count[0] = 1; count[1] = num_entity; if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, varid, start, count, var_vals); } else { status = nc_put_vara_double(exoid, varid, start, count, var_vals); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store variables with varid %d in file id %d", varid, exoid); ex_err("ex_put_varid_var",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); }
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); }
void CONetCDF4::writeData_(int _grpid, int _varid, const std::vector<std::size_t> & _sstart, const std::vector<std::size_t> & _scount, const double * _data) { CheckError(nc_put_vara_double(_grpid, _varid, &(_sstart[0]), &(_scount[0]), _data)); }
void write_field(int lev, int v, double *array) { int status; size_t start[MAX_VAR_DIMS] = {0}; start[0] = lev; status = nc_put_vara_double(ncOutid,var_id[v],start,var_count[v],array); if (status != NC_NOERR) handle_error(status,"Writing field",v); }
// 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; }
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; }
void R_nc4_put_vara_double( int *ncid, int *varid, int *start, int *count, double *data, int *retval ) { int i, ndims, err, verbose; size_t s_start[MAX_NC_DIMS], s_count[MAX_NC_DIMS]; char varname[MAX_NC_NAME]; verbose = 0; if( verbose ) { err = nc_inq_varname( *ncid, *varid, varname ); Rprintf( "R_nc4_put_vara_double: entering with ncid=%d, varid=%d (varname=%s)\n", *ncid, *varid, varname ); } /* Get # of dims for this var */ err = nc_inq_varndims( *ncid, *varid, &ndims ); if( err != NC_NOERR ) Rprintf( "Error on nc_inq_varndims call in R_nc4_put_vara_double: %s\n", nc_strerror(*retval) ); if( verbose ) Rprintf( "R_nc4_put_vara_double: for this var ndims=%d\n", ndims ); /* 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]; } if( verbose ) { Rprintf( "R_nc4_put_vara_double: about to write with start=" ); for( i=0; i<ndims; i++ ) Rprintf("%d ", s_start[i] ); Rprintf( " count=" ); for( i=0; i<ndims; i++ ) Rprintf("%d ", s_count[i] ); Rprintf( "\n" ); } *retval = nc_put_vara_double(*ncid, *varid, s_start, s_count, data ); if( *retval != NC_NOERR ) Rprintf( "Error in R_nc4_put_vara_double: %s\n", nc_strerror(*retval) ); if( verbose ) Rprintf( "R_nc4_put_vara_double: returning with errval=%d\n", *retval ); }
int ex_put_nodal_varid_var(int exoid, int time_step, int nodal_var_index, int num_nodes, int varid, const void *nodal_var_vals) { int status; size_t start[3], count[3]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ if (ex_large_model(exoid) == 0) { start[0] = --time_step; start[1] = --nodal_var_index; start[2] = 0; count[0] = 1; count[1] = 1; count[2] = num_nodes; } else { start[0] = --time_step; start[1] = 0; count[0] = 1; count[1] = num_nodes; } if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, varid, start, count, nodal_var_vals); } else { status = nc_put_vara_double(exoid, varid, start, count, nodal_var_vals); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store nodal variables in file id %d", exoid); ex_err("ex_put_nodal_varid_var",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); }
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 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 write_netcdf(char *filename, char *varname, size_t *start, size_t *count, double *snaps) { int ncid_wr, retval_wr; int vel_varid_wr; /* Open the file. NC_WRITE tells netCDF we want read-only access to the file.*/ if ((retval_wr = nc_open(filename, NC_WRITE, &ncid_wr))) ERR(retval_wr); /* Get variable*/ if ((retval_wr = nc_inq_varid(ncid_wr, varname, &vel_varid_wr))) ERR(retval_wr);; /* Put variable*/ if ((retval_wr = nc_put_vara_double(ncid_wr, vel_varid_wr, start, count, &snaps[0]))) ERR(retval_wr); /* Close the file. */ if ((retval_wr = nc_close(ncid_wr))) ERR(retval_wr); printf("\n *** SUCCESS writing variables \"%s\" to \"%s\"!\n", varname, filename); }
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"); } }
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; }
/* 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; }
// 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 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; }
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; }
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; }
int ex_put_partial_var(int exoid, int time_step, ex_entity_type var_type, int var_index, ex_entity_id obj_id, int64_t start_index, int64_t num_entities, const void *var_vals) { int varid, dimid, time_dim, numobjdim, dims[2], obj_id_ndx; size_t num_obj; size_t num_obj_var; size_t num_entity; size_t start[2], count[2]; int * obj_var_truth_tab; int status; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ #define EX_LOOK_UP_VAR(VOBJID, VVAR, VOBJTAB, DNUMOBJ, DNUMOBJVAR) \ /* Determine index of obj_id in VOBJID array */ \ obj_id_ndx = ex_id_lkup(exoid, var_type, obj_id); \ if (exerrval != 0) { \ if (exerrval == EX_NULLENTITY) { \ snprintf(errmsg, MAX_ERR_LENGTH, \ "Warning: no variables allowed for NULL block %" PRId64 " in file id %d", obj_id, \ exoid); \ ex_err("ex_put_partial_var", errmsg, EX_NULLENTITY); \ return (EX_WARN); \ } \ else { \ snprintf(errmsg, MAX_ERR_LENGTH, \ "ERROR: failed to locate %s id %" PRId64 " in %s array in file id %d", \ ex_name_of_object(var_type), obj_id, VOBJID, exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ return (EX_FATAL); \ } \ } \ \ if ((status = nc_inq_varid(exoid, VVAR(var_index, obj_id_ndx), &varid)) != NC_NOERR) { \ if (status == NC_ENOTVAR) /* variable doesn't exist, create it! */ \ { \ /* check for the existance of an TNAME variable truth table */ \ if (nc_inq_varid(exoid, VOBJTAB, &varid) == NC_NOERR) { \ /* find out number of TNAMEs and TNAME variables */ \ status = ex_get_dimension(exoid, DNUMOBJ, ex_name_of_object(var_type), &num_obj, &dimid, \ "ex_put_partial_var"); \ if (status != NC_NOERR) \ return status; \ \ status = ex_get_dimension(exoid, DNUMOBJVAR, ex_name_of_object(var_type), &num_obj_var, \ &dimid, "ex_put_partial_var"); \ if (status != NC_NOERR) \ return status; \ \ if (!(obj_var_truth_tab = malloc(num_obj * num_obj_var * sizeof(int)))) { \ exerrval = EX_MEMFAIL; \ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to allocate memory for %s variable " \ "truth table in file id %d", \ ex_name_of_object(var_type), exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ return (EX_FATAL); \ } \ \ /* read in the TNAME variable truth table */ \ if ((status = nc_get_var_int(exoid, varid, obj_var_truth_tab)) != NC_NOERR) { \ exerrval = status; \ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get truth table from file id %d", \ exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ return (EX_FATAL); \ } \ \ if (obj_var_truth_tab[num_obj_var * (obj_id_ndx - 1) + var_index - 1] == 0L) { \ free(obj_var_truth_tab); \ exerrval = EX_BADPARAM; \ snprintf(errmsg, MAX_ERR_LENGTH, \ "ERROR: Invalid %s variable %d, %s %" PRId64 " in file id %d", \ ex_name_of_object(var_type), var_index, ex_name_of_object(var_type), obj_id, \ exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ return (EX_FATAL); \ } \ free(obj_var_truth_tab); \ } \ \ if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) { \ exerrval = status; \ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d", \ exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ goto error_ret; /* exit define mode and return */ \ } \ \ ex_get_dimension(exoid, ex_dim_num_entries_in_object(var_type, obj_id_ndx), \ ex_name_of_object(var_type), &num_entity, &numobjdim, \ "ex_put_partial_var"); \ \ /* variable doesn't exist so put file into define mode */ \ if ((status = nc_redef(exoid)) != NC_NOERR) { \ exerrval = status; \ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", \ exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ return (EX_FATAL); \ } \ \ /* define netCDF variable to store TNAME variable values */ \ dims[0] = time_dim; \ dims[1] = numobjdim; \ if ((status = nc_def_var(exoid, VVAR(var_index, obj_id_ndx), nc_flt_code(exoid), 2, dims, \ &varid)) != NC_NOERR) { \ exerrval = status; \ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define %s variable %d in file id %d", \ ex_name_of_object(var_type), var_index, exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ goto error_ret; \ } \ ex_compress_variable(exoid, varid, 2); \ \ /* leave define mode */ \ \ if ((status = nc_enddef(exoid)) != NC_NOERR) { \ exerrval = status; \ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete %s variable %s definition " \ "to file id %d", \ ex_name_of_object(var_type), VVAR(var_index, obj_id_ndx), exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ return (EX_FATAL); \ } \ } \ else { \ exerrval = status; \ snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s variable %s in file id %d", \ ex_name_of_object(var_type), VVAR(var_index, obj_id_ndx), exoid); \ ex_err("ex_put_partial_var", errmsg, exerrval); \ return (EX_FATAL); \ } \ } switch (var_type) { case EX_GLOBAL: if (num_entities <= 0) { exerrval = EX_MSG; snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no global variables specified for file id %d", exoid); ex_err("ex_put_partial_var", errmsg, exerrval); return (EX_WARN); } /* inquire previously defined variable */ if ((status = nc_inq_varid(exoid, VAR_GLO_VAR, &varid)) != NC_NOERR) { if (status == NC_ENOTVAR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no global variables defined in file id %d", exoid); ex_err("ex_put_partial_var", errmsg, exerrval); } else { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get global variables parameters in file id %d", exoid); ex_err("ex_put_partial_var", errmsg, exerrval); } return (EX_FATAL); } break; case EX_NODAL: return ex_put_partial_nodal_var_int(exoid, time_step, var_index, start_index, num_entities, var_vals); break; case EX_EDGE_BLOCK: EX_LOOK_UP_VAR(VAR_ID_ED_BLK, VAR_EDGE_VAR, VAR_EBLK_TAB, DIM_NUM_ED_BLK, DIM_NUM_EDG_VAR); break; case EX_FACE_BLOCK: EX_LOOK_UP_VAR(VAR_ID_FA_BLK, VAR_FACE_VAR, VAR_FBLK_TAB, DIM_NUM_FA_BLK, DIM_NUM_FAC_VAR); break; case EX_ELEM_BLOCK: EX_LOOK_UP_VAR(VAR_ID_EL_BLK, VAR_ELEM_VAR, VAR_ELEM_TAB, DIM_NUM_EL_BLK, DIM_NUM_ELE_VAR); break; case EX_NODE_SET: EX_LOOK_UP_VAR(VAR_NS_IDS, VAR_NS_VAR, VAR_NSET_TAB, DIM_NUM_NS, DIM_NUM_NSET_VAR); break; case EX_EDGE_SET: EX_LOOK_UP_VAR(VAR_ES_IDS, VAR_ES_VAR, VAR_ESET_TAB, DIM_NUM_ES, DIM_NUM_ESET_VAR); break; case EX_FACE_SET: EX_LOOK_UP_VAR(VAR_FS_IDS, VAR_FS_VAR, VAR_FSET_TAB, DIM_NUM_FS, DIM_NUM_FSET_VAR); break; case EX_SIDE_SET: EX_LOOK_UP_VAR(VAR_SS_IDS, VAR_SS_VAR, VAR_SSET_TAB, DIM_NUM_SS, DIM_NUM_SSET_VAR); break; case EX_ELEM_SET: EX_LOOK_UP_VAR(VAR_ELS_IDS, VAR_ELS_VAR, VAR_ELSET_TAB, DIM_NUM_ELS, DIM_NUM_ELSET_VAR); break; default: exerrval = EX_MSG; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: invalid variable type (%d) specified for file id %d", var_type, exoid); ex_err("ex_put_partial_var", errmsg, exerrval); return (EX_FATAL); } /* store element variable values */ start[0] = --time_step; start[1] = start_index - 1; if (var_type == EX_GLOBAL) { /* global variables may be written * - all at once (by setting var_index to 1 and num_entries_this_obj to * num_glob, or * - one at a time (by setting var_index to the desired index and * num_entries_this_obj to 1. */ count[0] = var_index; } else { count[0] = 1; } count[1] = num_entities; if (count[1] == 0) { start[1] = 0; } if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, varid, start, count, var_vals); } else { status = nc_put_vara_double(exoid, varid, start, count, var_vals); } if (status != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s %" PRId64 " variable %d in file id %d", ex_name_of_object(var_type), obj_id, var_index, exoid); ex_err("ex_put_partial_var", errmsg, exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (nc_enddef(exoid) != NC_NOERR) /* exit define mode */ { snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d", exoid); ex_err("ex_put_partial_var", errmsg, exerrval); } return (EX_FATAL); }
int ex_put_coord (int exoid, const void *x_coor, const void *y_coor, const void *z_coor) { int status; int coordid; int coordidx, coordidy, coordidz; int numnoddim, ndimdim; size_t i, num_nod, num_dim, start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire id's of previously defined dimensions */ if ((status = nc_inq_dimid(exoid, DIM_NUM_NODES, &numnoddim)) != NC_NOERR) { /* If not found, then this file is storing 0 nodes. Return immediately */ return (EX_NOERR); } if ((status = nc_inq_dimlen(exoid, numnoddim, &num_nod)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: inquire failed to return number of nodes in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_inq_dimid(exoid, DIM_NUM_DIM, &ndimdim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate number of dimensions in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, ndimdim, &num_dim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of dimensions in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } /* write out the coordinates */ if (ex_large_model(exoid) == 0) { if ((status = nc_inq_varid(exoid, VAR_COORD, &coordid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } for (i=0; i<num_dim; i++) { char *which = 0; start[0] = i; start[1] = 0; count[0] = 1; count[1] = num_nod; if (i == 0 && x_coor != NULL) { which = "X"; if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, coordid, start, count, x_coor); } else { status = nc_put_vara_double(exoid, coordid, start, count, x_coor); } } else if (i == 1 && y_coor != NULL) { which = "Y"; if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, coordid, start, count, y_coor); } else { status = nc_put_vara_double(exoid, coordid, start, count, y_coor); } } else if (i == 2 && z_coor != NULL) { which = "Z"; if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, coordid, start, count, z_coor); } else { status = nc_put_vara_double(exoid, coordid, start, count, z_coor); } } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to put %s coord array in file id %d", which, exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } } else { if ((status = nc_inq_varid(exoid, VAR_COORD_X, &coordidx)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate x nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } if (num_dim > 1) { if ((status = nc_inq_varid(exoid, VAR_COORD_Y, &coordidy)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate y nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } else { coordidy = 0; } if (num_dim > 2) { if ((status = nc_inq_varid(exoid, VAR_COORD_Z, &coordidz)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate z nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } else { coordidz = 0; } /* write out the coordinates */ for (i=0; i<num_dim; i++) { const void *coor = NULL; char *which = NULL; if (i == 0) { coor = x_coor; which = "X"; coordid = coordidx; } else if (i == 1) { coor = y_coor; which = "Y"; coordid = coordidy; } else if (i == 2) { coor = z_coor; which = "Z"; coordid = coordidz; } if (coor != NULL && coordid != 0) { if (ex_comp_ws(exoid) == 4) { status = nc_put_var_float(exoid, coordid, coor); } else { status = nc_put_var_double(exoid, coordid, coor); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to put %s coord array in file id %d", which, exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } } } return (EX_NOERR); }
int main() { double *data_out; // buffer int nx = 1/DX; // hyperslab and output buffer dimensions int ny = 1/DY; // int i, j; // Allocate memory for data if((data_out = (double *)malloc(STEPS * nx * ny * sizeof(double))) == NULL) printf("Error malloc matrix data_out[%d]\n",nx * ny); // Create NetCDF file. NC_CLOBBER tells NetCDF to overwrite this file, if it already exists int ncid, retval; // if( retval = nc_create( NC_FILE_NAME_NETCDF, NC_CLOBBER|NC_NETCDF4, &ncid ) ) { if( retval = nc_create( NC_FILE_NAME_NETCDF, NC_CLOBBER, &ncid ) ) { ERR_NETCDF(retval); } // Define the x and y dimensions. NetCDF will hand back and ID for each. int x_dimid, y_dimid, t_dimid; if( retval = nc_def_dim( ncid, "x", nx, &x_dimid ) ) { ERR_NETCDF(retval); } if( retval = nc_def_dim( ncid, "y", ny, &y_dimid ) ) { ERR_NETCDF(retval); } // Define the t dimension at NetCDF. if( retval = nc_def_dim( ncid, "t", NC_UNLIMITED, &t_dimid ) ) { ERR_NETCDF(retval); } // Define coordinate variables for x and y at NetCDF int x_varid, y_varid, t_varid; if( retval = nc_def_var( ncid, "x", NC_DOUBLE, 1, &x_dimid, &x_varid ) ) { ERR_NETCDF(retval); } if( retval = nc_def_var( ncid, "y", NC_DOUBLE, 1, &y_dimid, &y_varid ) ) { ERR_NETCDF(retval); } if( retval = nc_def_var( ncid, "t", NC_DOUBLE, 1, &t_dimid, &t_varid ) ) { ERR_NETCDF(retval); } // Define the nc-variable to store temperature data. The t dimension should be the one which varies more slowly at NetCDF. int varid; int dimids[3] = { t_dimid, x_dimid, y_dimid }; if( retval = nc_def_var( ncid, "temperature", NC_DOUBLE, 3, dimids, &varid )){ ERR_NETCDF(retval); } // Write x, y, t and temperature units at NetCDF char * space_units = "meters"; char * time_units = "seconds since start of the experiment"; char * temp_units = "kelvin"; if( retval = nc_put_att_text( ncid, x_varid, "units", strlen(space_units), space_units ) ) { ERR_NETCDF(retval); } if( retval = nc_put_att_text( ncid, y_varid, "units", strlen(space_units), space_units ) ) { ERR_NETCDF(retval); } if( retval = nc_put_att_text( ncid, t_varid, "units", strlen(time_units), time_units ) ) { ERR_NETCDF(retval); } if( retval = nc_put_att_text( ncid, varid, "units", strlen(temp_units), temp_units ) ) { ERR_NETCDF(retval); } double scale_factor = 300.0; if( retval = nc_put_att_double( ncid, varid, "scale_factor", NC_DOUBLE, 1, &scale_factor ) ) { ERR_NETCDF(retval); } // End define mode: this tells NetCDF that we are done defining metadata at NetCDF if( retval = nc_enddef( ncid ) ) { ERR_NETCDF(retval); } // Write x coordinates at NetCDF size_t pos; for( pos = 0; pos < nx; ++pos ) { double x = DX*pos; if( retval = nc_put_var1_double( ncid, x_varid, &pos, &x ) ) { ERR_NETCDF(retval); } } // Write y coordinates at NetCDF for( pos = 0; pos < ny; ++pos ) { double y = DY*pos; if( retval = nc_put_var1_double( ncid, y_varid, &pos, &y ) ) { ERR_NETCDF(retval); } } // Open an existing HDF5 file for Output buffer hid_t file_id = H5Fopen(H5_FILE_NAME_HDF5, H5F_ACC_RDONLY, H5P_DEFAULT); if( file_id < 0 ) { ERR_HDF5; } // Open an existing HDF5 dataset hid_t tempD = H5Dopen(file_id, "temperature", H5P_DEFAULT); if( tempD < 0 ) { ERR_HDF5; } // Returns an identifier for a copy of the dataspace for a dataset HDF5 hid_t tempSel = H5Dget_space (tempD); /* dataspace handle */ if( tempSel < 0 ) { ERR_HDF5; } // Returns the number of dimensions in the HDF5 dataspace if successful; otherwise returns a negative value int rank; rank = H5Sget_simple_extent_ndims (tempSel); // Retrieves dataspace dimension size and maximum size HDF5 hsize_t dims_out[2]; // HDF5 dataset dimensions hid_t status_n = H5Sget_simple_extent_dims (tempSel, dims_out, NULL); if( status_n < 0 ) { ERR_HDF5; } // Display the number of dimensions in the HDF5 dataspace and the dataspace dimension size and maximum size printf("\nRank: %d\nDimensions: %lu x %lu \n", rank, (unsigned long)(dims_out[0]), (unsigned long)(dims_out[1])); // Define hyperslab in the dataset HDF5 hsize_t sel_offset_in[4] = {0,0,0,SECTION}; // The temperature value for the last interaction (STEPS) is chosen hsize_t sel_length_in[4] = {STEPS, nx, ny, 1}; H5Sselect_hyperslab( tempSel, H5S_SELECT_SET, sel_offset_in, NULL, sel_length_in, NULL ); if( tempSel < 0 ) { ERR_HDF5; } // Define the memory dataspace HDF5 hsize_t memSdim[4]={STEPS,nx,ny}; hid_t memS = H5Screate_simple( 3, memSdim, NULL ); if( memS < 0 ) { ERR_HDF5; } // Define memory HDF5 hyperslab hsize_t sel_offset_out[3] = {0,0,0}; hsize_t sel_length_out[3] = {STEPS, nx, ny}; H5Sselect_hyperslab( memS, H5S_SELECT_SET, sel_offset_out, NULL, sel_length_out, NULL ); if( memS < 0 ) { ERR_HDF5; } // Read dataset tempD data from HDF5 hyperslab in the file into the hyperslab in memory hsize_t status = H5Dread (tempD, H5T_NATIVE_DOUBLE, memS, tempSel, H5P_DEFAULT, data_out); if( status < 0 ) { ERR_HDF5; } // printf ("Data:\n "); // for( i = 0; i < nx; ++i ) { // for( j = 0; j < ny; ++j ) { // printf("%f ", data_out[i*ny+j]); // } // printf("\n "); // } // printf("\n"); // Write the data to the NETCDF file size_t corner_vector[3] = {0,0,0}; size_t edge_lengths[3] = {STEPS, nx, ny}; if(retval = nc_put_vara_double(ncid, varid, corner_vector, edge_lengths, data_out)){ ERR_NETCDF(retval); } pos = 0; double tval = 0; if( retval = nc_put_var1_double( ncid, t_varid, &pos, &tval ) ) { ERR_NETCDF(retval); } // Close the HDF5 memspace if( H5Sclose( memS ) < 0 ) { ERR_HDF5; } // Close the HDF5 dataspace if( H5Sclose( tempSel ) < 0 ) { ERR_HDF5; } // Close the HDF5 dataset if( H5Dclose( tempD ) < 0 ) { ERR_HDF5; } // Close the HDF5 file if( H5Fclose( file_id ) < 0 ) { ERR_HDF5; } // Close the file. This frees up any internal NetCDF resources associated with the file, and flushes any buffers if( retval = nc_close( ncid ) ) { ERR_NETCDF(retval); } // Free memory free(data_out); return 0; }
int ex_put_partial_side_set_df(int exoid, ex_entity_id side_set_id, int64_t start_num, int64_t num_df_to_get, void *side_set_dist_fact) { int status; int dimid, side_set_id_ndx; int dist_id; size_t num_df_in_set, start[1], count[1]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* first check if any side sets are specified */ if ((status = nc_inq_dimid(exoid, DIM_NUM_SS, &dimid)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no side sets specified in file id %d", exoid); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } /* Lookup index of side set id in VAR_SS_IDS array */ if ((side_set_id_ndx = ex_id_lkup(exoid, EX_SIDE_SET, side_set_id)) < 0) { if (exerrval == EX_NULLENTITY) { snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no data allowed for NULL side set %" PRId64 " in file id %d", side_set_id, exoid); ex_err("ex_put_side_set_fact", errmsg, EX_NULLENTITY); return (EX_WARN); } snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate side set id %" PRId64 " in VAR_SS_IDS array in file id %d", side_set_id, exoid); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } /* inquire id's of previously defined dimension and variable */ if ((status = nc_inq_dimid(exoid, DIM_NUM_DF_SS(side_set_id_ndx), &dimid)) != NC_NOERR) { if (status == NC_EBADDIM) { exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no dist factors defined for side set %" PRId64 " in file id %d", side_set_id, exoid); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_WARN); } exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate number of dist factors in side set %" PRId64 " in file id %d", side_set_id, exoid); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, &num_df_in_set)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of dist factors in side set %" PRId64 " in file id %d", side_set_id, exoid); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } /* Check input parameters for a valid range of numbers */ if (start_num < 0 || (num_df_to_get > 0 && start_num > num_df_in_set)) { exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid input"); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } if (num_df_to_get < 0) { exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid number of df's to put!"); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } /* start_num now starts at 1, not 0 */ if ((start_num + num_df_to_get) > num_df_in_set + 1) { exerrval = EX_BADPARAM; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: request larger than number of df's in set!"); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_varid(exoid, VAR_FACT_SS(side_set_id_ndx), &dist_id)) != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate dist factors list for side set %" PRId64 " in file id %d", side_set_id, exoid); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } /* write out the distribution factors array */ start[0] = --start_num; count[0] = num_df_to_get; if (num_df_to_get == 0) { start[0] = 0; } if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, dist_id, start, count, side_set_dist_fact); } else { status = nc_put_vara_double(exoid, dist_id, start, count, side_set_dist_fact); } if (status != NC_NOERR) { exerrval = status; snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store dist factors for side set %" PRId64 " in file id %d", side_set_id, exoid); ex_err("ex_put_partial_side_set_df", errmsg, exerrval); return (EX_FATAL); } return (EX_NOERR); }
int ex_put_elem_var_slab (int exoid, int time_step, int elem_var_index, ex_entity_id elem_blk_id, int64_t start_pos, int64_t num_vals, void *elem_var_vals) { int status; int varid, dimid,time_dim, numelbdim, dims[2], elem_blk_id_ndx; size_t num_elem_blk, num_elem_var, start[2], count[2]; int *elem_var_tab; 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)) < 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: no variables allowed for NULL block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_elem_var_slab", errmsg, EX_NULLENTITY); return (EX_WARN); } 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_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_varid (exoid, VAR_ELEM_VAR(elem_var_index, elem_blk_id_ndx), &varid)) != NC_NOERR) { if (status == NC_ENOTVAR) { /* variable doesn't exist, create it! */ /* inquire previously defined dimensions */ /* check for the existance of an element variable truth table */ if ((status = nc_inq_varid (exoid, VAR_ELEM_TAB, &varid)) == NC_NOERR) { /* find out number of element blocks and element variables */ if ((status = nc_inq_dimid (exoid, DIM_NUM_EL_BLK, &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate number of element blocks in file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, &num_elem_blk)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of element blocks in file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimid (exoid, DIM_NUM_ELE_VAR, &dimid)) != NC_NOERR) { exerrval = EX_BADPARAM; sprintf(errmsg, "Error: no element variables stored in file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } if ((status = nc_inq_dimlen(exoid, dimid, &num_elem_var)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get number of element variables in file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } if (!(elem_var_tab = (int *)malloc(num_elem_blk*num_elem_var*sizeof(int)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for element variable truth table in file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } /* read in the element variable truth table */ if ((status = nc_get_var_int(exoid, varid, elem_var_tab)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get truth table from file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } if (elem_var_tab[num_elem_var*(elem_blk_id_ndx-1)+elem_var_index-1] == 0L) { free(elem_var_tab); exerrval = EX_BADPARAM; sprintf(errmsg, "Error: Invalid element variable %d, block %"PRId64" in file id %d", elem_var_index, elem_blk_id, exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } free(elem_var_tab); } if ((status = nc_inq_dimid (exoid, DIM_TIME, &time_dim)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate time dimension in file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); goto error_ret; /* exit define mode and return */ } if ((status = nc_inq_dimid(exoid, DIM_NUM_EL_IN_BLK(elem_blk_id_ndx), &numelbdim)) != NC_NOERR) { if (status == NC_EBADDIM) { exerrval = status; sprintf(errmsg, "Error: number of elements in element block %"PRId64" not defined in file id %d", elem_blk_id, exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); } else { exerrval = status; sprintf(errmsg, "Error: failed to locate number of elements in element block %"PRId64" in file id %d", elem_blk_id, exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); } goto error_ret; } /* variable doesn't exist so put file into define mode */ if ((status = nc_redef (exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to put file id %d into define mode", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } /* define netCDF variable to store element variable values */ dims[0] = time_dim; dims[1] = numelbdim; if ((status = nc_def_var(exoid, VAR_ELEM_VAR(elem_var_index, elem_blk_id_ndx), nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to define element variable %d in file id %d", elem_var_index, exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); goto error_ret; } ex_compress_variable(exoid, varid, 2); /* leave define mode */ if ((status = nc_enddef(exoid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to complete element variable %s definition to file id %d", VAR_ELEM_VAR(elem_var_index, elem_blk_id_ndx), exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } } else { exerrval = status; sprintf(errmsg, "Error: failed to locate element variable %s in file id %d", VAR_ELEM_VAR(elem_var_index, elem_blk_id_ndx),exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } } /* store element variable values */ start[0] = --time_step; start[1] = --start_pos; count[0] = 1; count[1] = num_vals; if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, varid, start, count, elem_var_vals); } else { status = nc_put_vara_double(exoid, varid, start, count, elem_var_vals); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store element variable %d in file id %d", elem_var_index, exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (nc_enddef (exoid) != NC_NOERR) /* exit define mode */ { sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_put_elem_var_slab", errmsg, exerrval); } return (EX_FATAL); }
/* * 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_set_dist_fact (int exoid, ex_entity_type set_type, ex_entity_id set_id, int64_t offset, int64_t num_to_put, const void *set_dist_fact) { int status; int dimid, set_id_ndx; int dist_id; size_t start[1], count[1]; char errmsg[MAX_ERR_LENGTH]; char* factptr = NULL; exerrval = 0; /* clear error code */ /* first check if any sets are specified */ if ((status = nc_inq_dimid(exoid, ex_dim_num_objects(set_type), &dimid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: no %ss specified in file id %d", ex_name_of_object(set_type), exoid); ex_err("ex_put_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } /* Lookup index of set id in VAR_*S_IDS array */ set_id_ndx = ex_id_lkup(exoid,set_type,set_id); if (exerrval != 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: no data allowed for NULL %s %"PRId64" in file id %d", ex_name_of_object(set_type), set_id,exoid); ex_err("ex_put_set_fact",errmsg,EX_NULLENTITY); return (EX_WARN); } else { sprintf(errmsg, "Error: failed to locate %s id %"PRId64" in VAR_*S_IDS array in file id %d", ex_name_of_object(set_type), set_id,exoid); ex_err("ex_put_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } } /* setup more pointers based on set_type */ if (set_type == EX_NODE_SET) { /* note we are using DIM_NUM_NODE_NS instead of DIM_NUM_DF_NS */ factptr = VAR_FACT_NS(set_id_ndx); } else if (set_type == EX_EDGE_SET) { factptr = VAR_FACT_ES(set_id_ndx); } else if (set_type == EX_FACE_SET) { factptr = VAR_FACT_FS(set_id_ndx); } else if (set_type == EX_SIDE_SET) { factptr = VAR_FACT_SS(set_id_ndx); } if (set_type == EX_ELEM_SET) { factptr = VAR_FACT_ELS(set_id_ndx); } /* find id of distribution factors variable */ if ((status = nc_inq_varid(exoid, factptr, &dist_id)) != NC_NOERR) { /* this test is only needed for node set because we're using DIM_NUM_NOD_NS instead of DIM_NUM_DF_NS*/ if (status == NC_ENOTVAR) { exerrval = EX_BADPARAM; sprintf(errmsg, "Warning: no dist factors defined for %s %"PRId64" in file id %d", ex_name_of_object(set_type), set_id, exoid); ex_err("ex_put_set_dist_fact",errmsg,exerrval); return (EX_WARN); } else { exerrval = status; sprintf(errmsg, "Error: failed to locate dist factors list for %s %"PRId64" in file id %d", ex_name_of_object(set_type), set_id,exoid); ex_err("ex_put_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } } start[0] = offset-1; count[0] = num_to_put; if (num_to_put == 0) start[0] = 0; /* write out the distribution factors array */ if (ex_comp_ws(exoid) == 4) { status = nc_put_vara_float(exoid, dist_id, start, count, set_dist_fact); } else { status = nc_put_vara_double(exoid, dist_id, start, count, set_dist_fact); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store dist factors for %s %"PRId64" in file id %d", ex_name_of_object(set_type), set_id,exoid); ex_err("ex_put_partial_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); }
void ITLRandomField::_DumpData2NetCdf ( ) { for(int b = 0; b < IGetNrOfBlocks(); b++) { const CBlock& cBlock = this->CGetBlock(b); int iNrOfCells = 1; for(int d = 0; d < CBlock::MAX_DIM; d++) iNrOfCells *= cBlock.piDimLengths[d]; TBuffer<double> pdTemp; pdTemp.alloc(iNrOfCells); #ifndef WITH_PNETCDF // ADD-BY-LEETEN 09/01/2011 size_t puStart[6]; size_t puCount[6]; // ADD-BY-LEETEN 09/01/2011-BEGIN #else // #ifndef WITH_PNETCDF MPI_Offset puStart[6]; MPI_Offset puCount[6]; MPI_Offset puStride[6]; for(int d = 0; d < sizeof(puStride)/sizeof(puStride[0]); d ++) puStride[d] = 1; #endif // #ifndef WITH_PNETCDF // ADD-BY-LEETEN 09/01/2011-END // time puStart[0] = this->IGetNrOfTimeStamps() - 1; puCount[0] = 1; // block #ifndef WITH_PNETCDF // ADD-BY-LEETEN 09/01/2011 puStart[1] = (size_t)b; // ADD-BY-LEETEN 09/01/2011-BEGIN #else // #ifndef WITH_PNETCDF puStart[1] = (size_t)CGetBlock(b).iGlobalId; #endif // #ifndef WITH_PNETCDF // ADD-BY-LEETEN 09/01/2011-END puCount[1] = 1; for(int d = 0; d < CBlock::MAX_DIM; d++) { puStart[2+d] = 0; puCount[2+d] = (size_t)cBlock.piDimLengths[CBlock::MAX_DIM - 1 - d]; } for(int c = 0; c < IGetNrOfDataComponents(); c++) { const CDataComponent& cDataComponent = CGetDataComponent(c); const CArray &cArray = this->CGetArray(b, c); const double *pdData = cArray.pdData; int iBase = cArray.iBase; int iStep = cArray.iStep; for(int v = 0; v < iNrOfCells; v++) { double dValue = pdData[iBase + v * iStep]; pdTemp[v] = cDataComponent.cRange.DClamp(dValue); } #ifndef WITH_PNETCDF // ADD-BY-LEETEN 08/12/2011 // dump the geometry of the given dim. ASSERT_NETCDF(nc_put_vara_double( iNcId, cDataComponent.iVarId, puStart, puCount, &pdTemp[0])); // ADD-BY-LEETEN 08/12/2011-BEGIN #else // #ifndef WITH_PNETCDF // MOD-BY-LEETEN 09/01/2011-FROM: // LOG_ERROR(fprintf(stderr, "PNetCDF is not fully supported yet.")) // TO: ASSERT_NETCDF(ncmpi_put_vars_double_all( iNcId, cDataComponent.iVarId, puStart, puCount, puStride, &pdTemp[0])); // MOD-BY-LEETEN 09/01/2011-END #endif // #ifndef WITH_PNETCDF // ADD-BY-LEETEN 08/12/2011-END } } }