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); }
NCstate NCdsHandleGContCLStats(NCdsHandleGCont_t *gCont, size_t tStep, size_t level) { int status; size_t row, col, obsNum = 0, index[2], ncidx; double val, weight = 1.0, sumWeight = 0.0; double min = HUGE_VAL, max = -HUGE_VAL, avg = 0.0, stdDev = 0.0; for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) { val = gCont->Data[gCont->ColNum * row + col]; if (_NCdsHandleGContTestNodata(gCont, val)) continue; sumWeight += weight; avg = avg + val * weight; min = min < val ? min : val; max = max > val ? max : val; stdDev = stdDev + val * val * weight; obsNum++; } if (obsNum > 0) { avg = avg / sumWeight; stdDev = stdDev / sumWeight; stdDev = stdDev - avg * avg; stdDev = sqrt(stdDev); } else switch (gCont->GType) { default: CMmsgPrint(CMmsgAppError, "Invalid NetCDF type in: %s %d", __FILE__, __LINE__); return (NCfailed); case NC_BYTE: case NC_SHORT: case NC_INT: avg = stdDev = min = max = (double) gCont->FillValue.Int; break; case NC_FLOAT: case NC_DOUBLE: avg = stdDev = min = max = gCont->FillValue.Float; break; } ncidx = gCont->NCindex[tStep]; index[0] = tStep - gCont->NCoffset[ncidx]; index[1] = level; if ((gCont->MeanIds[ncidx] != NCundefined) && ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->MeanIds[ncidx], index, &avg)) != NC_NOERR)) NCprintNCError (status, "NCdsHandleGContCLStats"); if ((gCont->MinIds[ncidx] != NCundefined) && ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->MinIds[ncidx], index, &min)) != NC_NOERR)) NCprintNCError (status, "NCdsHandleGContCLStats"); if ((gCont->MaxIds[ncidx] != NCundefined) && ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->MaxIds[ncidx], index, &max)) != NC_NOERR)) NCprintNCError (status, "NCdsHandleGContCLStats"); if ((gCont->StdIds[ncidx] != NCundefined) && ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->StdIds[ncidx], index, &stdDev)) != NC_NOERR)) NCprintNCError (status, "NCdsHandleGContCLStats"); return (NCsucceeded); }
void copy_time_vals(size_t lev) { int status, i; double time_io; for (i=0;i<2;i++) if ((time_in_id[i] >= 0) && (time_out_id[i] >= 0)) { status = nc_get_var1_double(ncInid[0],time_in_id[i],&lev,&time_io); if (status != NC_NOERR) handle_error(status,"Reading time",lev); status = nc_put_var1_double(ncOutid,time_out_id[i],&lev,&time_io); if (status != NC_NOERR) handle_error(status,"Writing time",lev); } }
int main() { int ncid, spockid, kirkid, dimids[NUMDIMS]; double val_in, val_out = 999.99; size_t index[NUMDIMS] = {1}; int i, res; /* Create the netCDF classic format file. */ if ((res = nc_create("example.nc", NC_CLOBBER, &ncid))) BAIL(res); /* Turn off fill mode to speed things up. */ if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) BAIL(res); /* Define dimension. */ if ((res = nc_def_dim(ncid, "longdim", DIM_LEN, dimids))) BAIL(res); /* Define two variables. */ if ((res = nc_def_var(ncid, "spock", NC_DOUBLE, NUMDIMS, dimids, &spockid))) BAIL(res); if ((res = nc_def_var(ncid, "kirk", NC_DOUBLE, NUMDIMS, dimids, &kirkid))) BAIL(res); /* We're finished defining metadata. */ if ((res = nc_enddef(ncid))) BAIL(res); if ((res = nc_put_var1_double(ncid, spockid, index, &val_out))) BAIL(res); /* We're done! */ if ((res = nc_close(ncid))) BAIL(res); return 0; }
/** * Write double data to corresponding variable name */ void store(Property<double> *v) { int retval; int varid; double value = v->get(); std::string sname = composeName(v->getName()); /** * Get netcdf variable ID from name */ retval = nc_inq_varid(ncid, sname.c_str(), &varid); if (retval) log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog(); /** * Write a single data value */ retval = nc_put_var1_double(ncid, varid, &index, &value); if(retval) log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog(); }
int ex_put_time (int exoid, int time_step, const void *time_value) { int status; int varid; size_t start[1]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire previously defined variable */ if ((status = nc_inq_varid(exoid, VAR_WHOLE_TIME, &varid)) != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to locate time variable in file id %d", exoid); ex_err("ex_put_time",errmsg,exerrval); return (EX_FATAL); } /* store time value */ start[0] = --time_step; if (ex_comp_ws(exoid) == 4) { status = nc_put_var1_float(exoid, varid, start, time_value); } else { status = nc_put_var1_double(exoid, varid, start, time_value); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to store time value in file id %d", exoid); ex_err("ex_put_time",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); }
int extract_unary_single(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 *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 int lenOut=1; for(i=0;i<ndims;++i){ shapeOut[i]=(ends[i]-begins[i])/strides[i]+1; lenOut*=shapeOut[i]; } if(outLen!=NULL) *outLen=lenOut; getDivider(ndims,shape,divider); getDivider(ndims,shapeOut,dividerOut); /* decide element boundary for each mpi process */ size_t beginOut; size_t endOut; if(lenOut>=mpi_size){ beginOut=mpi_rank*(lenOut/mpi_size); if(mpi_rank!=mpi_size-1) endOut=(mpi_rank+1)*(lenOut/mpi_size); else endOut=lenOut; }else{ //mpi_size is bigger than lenOut if(mpi_rank<lenOut){ beginOut=mpi_rank; endOut=mpi_rank+1; }else{ beginOut=0; endOut=0; } } printf("mpi_rank %d, beginOut %d, endOut %d\n",mpi_rank,beginOut,endOut); void *data=malloc(sizeof(double)); size_t rem,remIn; for(i=beginOut;i<endOut;++i){ rem=i+preLen; remIn=i; for(j=0;j<ndims;++j){ startOut[j]=rem/dividerOut[j]; start[j]=begins[j]+(remIn/dividerOut[j])*strides[j]; rem=rem%dividerOut[j]; remIn=remIn%dividerOut[j]; } switch(vtype){ case NC_BYTE: if((res=nc_get_var1_uchar(ncid,vlid,start,data))) BAIL(res); if((res=nc_put_var1_uchar(ncidout,vlidout,startOut,(unsigned char *)data))) BAIL(res); break; case NC_CHAR: if((res=nc_get_var1_schar(ncid,vlid,start,(signed char *)data))) BAIL(res); if((res=nc_put_var1_schar(ncidout,vlidout,startOut,(signed char *)data))) BAIL(res); break; case NC_SHORT: if((res=nc_get_var1_short(ncid,vlid,start,data))) BAIL(res); if((res=nc_put_var1_short(ncidout,vlidout,startOut,(short *)data))) BAIL(res); break; case NC_INT: if((res=nc_get_var1_int(ncid,vlid,start,(int *)data))) BAIL(res); if((res=nc_put_var1_int(ncidout,vlidout,startOut,(int *)data))) BAIL(res); break; case NC_FLOAT: if((res=nc_get_var1_float(ncid,vlid,start,data))) BAIL(res); if((res=nc_put_var1_float(ncidout,vlidout,startOut,(float *)data))) BAIL(res); break; case NC_DOUBLE: if((res=nc_get_var1_double(ncid,vlid,start,data))) BAIL(res); if((res=nc_put_var1_double(ncidout,vlidout,startOut,(double *)data))) BAIL(res); break; default: printf("Unknown data type\n"); } } /*free resourses*/ free(divider); free(dividerOut); free(start); free(startOut); free(shapeOut); free(data); return 0; }
int main(int argc, char **argv) { int ncid, spockid, kirkid, dimids[NUMDIMS]; int int_val_in, int_val_out = 99; double double_val_in, double_val_out = 1.79769313486230e+308; /* from ncx.h */ size_t index[2] = {QTR_CLASSIC_MAX-1, 0}; /* These are for the revolutionary generals tests. */ int cromwellid, collinsid, washingtonid; int napoleanid, dimids_gen[4], dimids_gen1[4]; /* All create modes will be anded to this. All tests will be run twice, with and without NC_SHARE.*/ int cmode_run; int cflag = NC_CLOBBER; int res; printf("\n*** Testing large files, quickly.\n"); for (cmode_run=0; cmode_run<2; cmode_run++) { /* On second pass, try using NC_SHARE. */ if (cmode_run == 1) { cflag |= NC_SHARE; printf("*** Turned on NC_SHARE for subsequent tests.\n"); } /* Create a netCDF 64-bit offset format file. Write a value. */ printf("*** Creating %s for 64-bit offset large file test...", FILE_NAME); if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "longdim", QTR_CLASSIC_MAX, dimids))) ERR; if ((res = nc_def_var(ncid, "spock", NC_DOUBLE, NUMDIMS, dimids, &spockid))) ERR; if ((res = nc_def_var(ncid, "kirk", NC_DOUBLE, NUMDIMS, dimids, &kirkid))) ERR; if ((res = nc_enddef(ncid))) ERR; if ((res = nc_put_var1_double(ncid, kirkid, index, &double_val_out))) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); /* How about a meteorological data file about the weather experience by various generals of revolutionary armies? This has 3 dims, 4 vars. The dimensions are such that this will (just barely) not fit in a classic format file. The first three vars are cromwell, 536870911 bytes, washington, 2*536870911 bytes, and napolean, 536870911 bytes. That's a grand total of 2147483644 bytes. Recall our magic limit for the combined size of all fixed vars: 2 GiB - 4 bytes, or 2147483644. So you would think these would exactly fit, unless you realized that everything is rounded to a 4 byte boundary, so you need to add some bytes for that (how many?), and that pushes us over the limit. We will create this file twice, once to ensure it succeeds (with 64-bit offset format), and once to make sure it fails (with classic format). Then some variations to check record var boundaries. */ printf("*** Now a 64-bit offset, large file, fixed var test..."); if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", QTR_CLASSIC_MAX, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", QTR_CLASSIC_MAX, &dimids_gen[1]))) ERR; if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[2]))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 1, &dimids_gen[0], &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 1, &dimids_gen[1], &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 1, &dimids_gen[0], &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], &collinsid))) ERR; if ((res = nc_enddef(ncid))) ERR; printf("ok\n"); /* Write a value or two just for fun. */ /*index[0] = QTR_CLASSIC_MAX - 296; if ((res = nc_put_var1_int(ncid, napoleanid, index, &int_val_out))) ERR; if ((res = nc_get_var1_int(ncid, napoleanid, index, &int_val_in))) ERR; if (int_val_in != int_val_out) BAIL2;*/ printf("*** Now writing some values..."); index[0] = QTR_CLASSIC_MAX - 295; if ((res = nc_put_var1_int(ncid, napoleanid, index, &int_val_out))) ERR; if ((res = nc_get_var1_int(ncid, napoleanid, index, &int_val_in))) ERR; if (int_val_in != int_val_out) ERR; index[0] = QTR_CLASSIC_MAX - 1; if ((res = nc_put_var1_int(ncid, napoleanid, index, &int_val_out))) ERR; if ((res = nc_get_var1_int(ncid, napoleanid, index, &int_val_in))) ERR; if (int_val_in != int_val_out) ERR; index[0] = QTR_CLASSIC_MAX - 1; if ((res = nc_put_var1_int(ncid, washingtonid, index, &int_val_out))) ERR; if ((res = nc_get_var1_int(ncid, washingtonid, index, &int_val_in))) ERR; if (int_val_in != int_val_out) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); /* This time it should fail, because we're trying to cram this into a classic format file. nc_enddef will detect our violations and give an error. We've*/ printf("*** Now a classic file which will fail..."); if ((res = nc_create(FILE_NAME, cflag, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", QTR_CLASSIC_MAX, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", QTR_CLASSIC_MAX, &dimids_gen[1]))) ERR; if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[2]))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 1, &dimids_gen[0], &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 1, &dimids_gen[1], &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 1, &dimids_gen[0], &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], &collinsid))) ERR; if ((res = nc_enddef(ncid)) != NC_EVARSIZE) ERR; if ((res = nc_close(ncid)) != NC_EVARSIZE) ERR; printf("ok\n"); /* This will create some max sized 64-bit offset format fixed vars. */ printf("*** Now a 64-bit offset, simple fixed var create test..."); if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, &dimids_gen[0]))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_SHORT, 1, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_SHORT, 1, dimids_gen, &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, dimids_gen, &collinsid))) ERR; if ((res = nc_enddef(ncid))) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); /* This will exceed the 64-bit offset format limits for one of the fixed vars. */ printf("*** Now a 64-bit offset, over-sized file that will fail..."); if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; /* max dim size is MAX_CLASSIC_BYTES. */ if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, dimids_gen))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 1, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 1, dimids_gen, &washingtonid))) if ((res = nc_enddef(ncid)) != NC_EVARSIZE) ERR; if ((res = nc_close(ncid)) != NC_EVARSIZE) ERR; printf("ok\n"); /* Now let's see about record vars. First create a 64-bit offset file with three rec variables, each with the same numbers as defined above for the fixed var tests. This should all work. */ printf("*** Now a 64-bit offset, record var file..."); if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", QTR_CLASSIC_MAX, &dimids_gen[1]))) ERR; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", QTR_CLASSIC_MAX, &dimids_gen[2]))) ERR; if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[3]))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen, &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen, &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], &collinsid))) ERR; if ((res = nc_enddef(ncid))) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); /* Now try this record file in classic format. It should fail and the enddef. Too many bytes in the first record.*/ printf("*** Now a classic file that's too big and will fail..."); if ((res = nc_create(FILE_NAME, cflag, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", QTR_CLASSIC_MAX, &dimids_gen[1]))) ERR; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", QTR_CLASSIC_MAX, &dimids_gen[2]))) ERR; if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[3]))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen, &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen, &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], &collinsid))) ERR; if ((res = nc_enddef(ncid)) != NC_EVARSIZE) ERR; if ((res = nc_close(ncid)) != NC_EVARSIZE) ERR; printf("ok\n"); /* Now try this record file in classic format. It just barely passes at the enddef. Almost, but not quite, too many bytes in the first record. Since I'm adding a fixed variable (Collins), I don't get the last record size exemption. */ printf("*** Now a classic file with recs and one fixed will fail..."); if ((res = nc_create(FILE_NAME, cflag, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, &dimids_gen[1]))) ERR; if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[2]))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], &collinsid))) ERR; if ((res = nc_enddef(ncid))) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); /* Try a classic file with several records, and the last record var with a record size greater than our magic number of 2 GiB - 4 bytes. We'll start with just one oversized record var. This should work. Cromwell has been changed to NC_DOUBLE, and that increases his size to 2147483644 (the max dimension size) times 8, or about 16 GB per record. Zowie! (Mind you, Cromwell certainly had a great deal of revolutionary fervor.) */ printf("*** Now a classic file with one large rec var..."); if ((res = nc_create(FILE_NAME, cflag, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, &dimids_gen[1]))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_enddef(ncid))) ERR; index[0] = 0; index[1] = MAX_CLASSIC_BYTES - 1; if ((res = nc_put_var1_double(ncid, cromwellid, index, &double_val_out))) ERR; if ((res = nc_get_var1_double(ncid, cromwellid, index, &double_val_in))) ERR; if (double_val_in != double_val_out) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); /* This is a classic format file with an extra-large last record var. */ printf("*** Now a classic file with extra-large last record var..."); if ((res = nc_create(FILE_NAME, cflag, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, &dimids_gen[1]))) ERR; dimids_gen1[0] = dimids_gen[0]; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 5368, &dimids_gen1[1]))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen1, &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, &collinsid))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_enddef(ncid))) ERR; index[0] = 0; index[1] = MAX_CLASSIC_BYTES - 1; if ((res = nc_put_var1_double(ncid, cromwellid, index, &double_val_out))) ERR; if ((res = nc_get_var1_double(ncid, cromwellid, index, &double_val_in))) ERR; if (double_val_in != double_val_out) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); /* This is a classic format file with an extra-large second to last record var. But this time it won't work, because the size exemption only applies to the last record var. Note that one dimension is small (5000). */ printf("*** Now a classic file xtra-large 2nd to last var that will fail..."); if ((res = nc_create(FILE_NAME, cflag, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, &dimids_gen[1]))) ERR; dimids_gen1[0] = dimids_gen[0]; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 5000, &dimids_gen1[1]))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen1, &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, &collinsid))) ERR; if ((res = nc_enddef(ncid)) != NC_EVARSIZE) ERR; if ((res = nc_close(ncid)) != NC_EVARSIZE) ERR; printf("ok\n"); /* Now try an extra large second to last ver with 64-bit offset. This won't work either, because the cromwell var is so large. It exceeds the 4GiB - 4 byte per record limit for record vars. */ printf("*** Now a 64-bit offset file with too-large rec var that will fail..."); if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, &dimids_gen[1]))) ERR; dimids_gen1[0] = dimids_gen[0]; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 5368, &dimids_gen1[1]))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen1, &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, &collinsid))) ERR; if ((res = nc_enddef(ncid)) != NC_EVARSIZE) ERR; if ((res = nc_close(ncid)) != NC_EVARSIZE) ERR; printf("ok\n"); /* A 64-bit offset record file that just fits... */ printf("*** Now a 64 bit-offset file that just fits..."); if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid))) ERR; if ((res = nc_set_fill(ncid, NC_NOFILL, NULL))) ERR; if ((res = nc_def_dim(ncid, "political_trouble", NC_UNLIMITED, &dimids_gen[0]))) ERR; if ((res = nc_def_dim(ncid, "revolutionary_fervor", MAX_CLASSIC_BYTES, &dimids_gen[1]))) ERR; dimids_gen1[0] = dimids_gen[0]; if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", MAX_CLASSIC_BYTES, &dimids_gen1[1]))) ERR; if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, &washingtonid))) ERR; if ((res = nc_def_var(ncid, "Napolean", NC_SHORT, 2, dimids_gen1, &napoleanid))) ERR; if ((res = nc_def_var(ncid, "Cromwell", NC_SHORT, 2, dimids_gen, &cromwellid))) ERR; if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, &collinsid))) ERR; if ((res = nc_enddef(ncid))) ERR; index[0] = 0; index[1] = MAX_CLASSIC_BYTES - 1; if ((res = nc_put_var1_int(ncid, cromwellid, index, &int_val_out))) ERR; if ((res = nc_get_var1_int(ncid, cromwellid, index, &int_val_in))) ERR; if (int_val_in != int_val_out) ERR; if ((res = nc_close(ncid))) ERR; printf("ok\n"); } /* end of cmode run */ /* Wow! Everything worked! */ printf("\n*** All large file tests were successful.\n"); /* Delete the huge data file we created. */ (void) remove(FILE_NAME); printf("*** Success ***\n"); return 0; }
/* * Put a single numeric data value into a variable of an open netCDF. */ static void c_ncvpt1 ( int ncid, /* netCDF ID */ int varid, /* variable ID */ const size_t* indices,/* multidim index of data to be written */ const void* value, /* pointer to data value 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_var1_schar(ncid, varid, indices, (const signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_put_var1_short(ncid, varid, indices, (const short*)value); # elif NF_INT1_IS_C_INT status = nc_put_var1_int(ncid, varid, indices, (const int*)value); # elif NF_INT1_IS_C_LONG status = nc_put_var1_long(ncid, varid, indices, (const long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_put_var1_short(ncid, varid, indices, (const short*)value); # elif NF_INT2_IS_C_INT status = nc_put_var1_int(ncid, varid, indices, (const int*)value); # elif NF_INT2_IS_C_LONG status = nc_put_var1_long(ncid, varid, indices, (const long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_put_var1_int(ncid, varid, indices, (const int*)value); # elif NF_INT_IS_C_LONG status = nc_put_var1_long(ncid, varid, indices, (const long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_put_var1_float(ncid, varid, indices, (const float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_put_var1_double(ncid, varid, indices, (const double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_put_var1_float(ncid, varid, indices, (const float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_put_var1_double(ncid, varid, indices, (const double*)value); # endif break; } } if (status == 0) *rcode = 0; else { nc_advise("NCVPT1", status, ""); *rcode = ncerr; } }
/*! \internal */ int cpy_var_val(int in_id,int out_id,char *var_nm) /* int in_id: input netCDF input-file ID int out_id: input netCDF output-file ID char *var_nm: input variable name */ { /* Routine to copy the variable data from an input netCDF file * to an output netCDF file. */ int *dim_id; int idx; int nbr_dim; int var_in_id; int var_out_id; size_t *dim_cnt; size_t *dim_sz; size_t *dim_srt; size_t var_sz=1L; nc_type var_type_in, var_type_out; void *void_ptr = NULL; /* Get the var_id for the requested variable from both files. */ (void)nc_inq_varid(in_id, var_nm, &var_in_id); (void)nc_inq_varid(out_id,var_nm, &var_out_id); /* Get the number of dimensions for the variable. */ (void)nc_inq_vartype( out_id, var_out_id, &var_type_out); (void)nc_inq_varndims(out_id, var_out_id, &nbr_dim); (void)nc_inq_vartype( in_id, var_in_id, &var_type_in); (void)nc_inq_varndims(in_id, var_in_id, &nbr_dim); /* Allocate space to hold the dimension IDs */ dim_cnt = malloc(nbr_dim*sizeof(size_t)); dim_id=malloc(nbr_dim*sizeof(int)); dim_sz=malloc(nbr_dim*sizeof(size_t)); dim_srt=malloc(nbr_dim*sizeof(size_t)); /* Get the dimension IDs from the input file */ (void)nc_inq_vardimid(in_id, var_in_id, dim_id); /* Get the dimension sizes and names from the input file */ for(idx=0;idx<nbr_dim;idx++){ /* NB: For the unlimited dimension, ncdiminq() returns the maximum value used so far in writing data for that dimension. Thus if you read the dimension sizes from the output file, then the ncdiminq() returns dim_sz=0 for the unlimited dimension until a variable has been written with that dimension. This is the reason for always reading the input file for the dimension sizes. */ (void)nc_inq_dimlen(in_id,dim_id[idx],dim_cnt+idx); /* Initialize the indicial offset and stride arrays */ dim_srt[idx]=0L; var_sz*=dim_cnt[idx]; } /* end loop over dim */ /* Allocate enough space to hold the variable */ if (var_sz > 0) void_ptr=malloc(var_sz * type_size(var_type_in)); /* Get the variable */ /* if variable is float or double, convert if necessary */ if(nbr_dim==0){ /* variable is a scalar */ if (var_type_in == NC_INT && var_type_out == NC_INT) { nc_get_var1_int(in_id, var_in_id, 0L, void_ptr); nc_put_var1_int(out_id, var_out_id, 0L, void_ptr); } else if (var_type_in == NC_INT64 && var_type_out == NC_INT64) { nc_get_var1_longlong(in_id, var_in_id, 0L, void_ptr); nc_put_var1_longlong(out_id, var_out_id, 0L, void_ptr); } else if (var_type_in == NC_FLOAT) { nc_get_var1_float(in_id, var_in_id, 0L, void_ptr); nc_put_var1_float(out_id, var_out_id, 0L, void_ptr); } else if (var_type_in == NC_DOUBLE) { nc_get_var1_double(in_id, var_in_id, 0L, void_ptr); nc_put_var1_double(out_id, var_out_id, 0L, void_ptr); } else if (var_type_in == NC_CHAR) { nc_get_var1_text(in_id, var_in_id, 0L, void_ptr); nc_put_var1_text(out_id, var_out_id, 0L, void_ptr); } else { assert(1==0); } } else { /* variable is a vector */ if (var_type_in == NC_INT && var_type_out == NC_INT) { (void)nc_get_var_int(in_id, var_in_id, void_ptr); (void)nc_put_var_int(out_id, var_out_id, void_ptr); } else if (var_type_in == NC_INT64 && var_type_out == NC_INT64) { (void)nc_get_var_longlong(in_id, var_in_id, void_ptr); (void)nc_put_var_longlong(out_id, var_out_id, void_ptr); } else if (var_type_in == NC_FLOAT) { (void)nc_get_var_float(in_id, var_in_id, void_ptr); (void)nc_put_var_float(out_id, var_out_id, void_ptr); } else if (var_type_in == NC_DOUBLE) { (void)nc_get_var_double(in_id, var_in_id, void_ptr); (void)nc_put_var_double(out_id, var_out_id, void_ptr); } else if (var_type_in == NC_CHAR) { (void)nc_get_var_text(in_id, var_in_id, void_ptr); (void)nc_put_var_text(out_id, var_out_id, void_ptr); } else { assert(1==0); } } /* end if variable is an array */ /* Free the space that held the dimension IDs */ (void)free(dim_cnt); (void)free(dim_id); (void)free(dim_sz); (void)free(dim_srt); /* Free the space that held the variable */ (void)free(void_ptr); return(EX_NOERR); } /* end cpy_var_val() */
void define_nc_dim_var(int ncid, int dim_latlon, int *time_dim, int *time_var, int *x_dim, int *y_dim, double dx, double dy, double verf_utime, double ref_utime, double time_step, int time_step_type, double date0, int time_ind, double *lat_data, double *lon_data) { char *name, *str, *lname, *units; char ref_date[50]; int dimids[2]; int lat_var, lon_var, y_var, x_var; int ref_time_type; double fill_value = FILL_VALUE_DOUBLE; float f_fill_value = FILL_VALUE_FLOAT; /* the variable need by put_nc_dim_var */ double ttime; int i, j; double *test_ll; size_t start[2], count[2]; size_t x_len, y_len; size_t dimlens[2]; // create coordinate variables... if (dim_latlon == 1) { dimids[0] = *y_dim; netcdf_func( nc_def_var (ncid, "latitude", NC_DOUBLE, 1, dimids, &lat_var) ); dimids[0] = *x_dim; netcdf_func( nc_def_var (ncid, "longitude", NC_DOUBLE, 1, dimids, &lon_var) ); /*#ifdef DEBUG_NC fprintf(stderr,"netcdf:create_nc_dims: latitude(dim,var,ny)=%d %d %d\n",*y_dim,lat_var,ny); fprintf(stderr,"netcdf:create_nc_dims: longitude(dim,var,nx)=%d %d %d\n",*x_dim,lon_var,nx); #endif*/ } else if(dim_latlon == 2) { dimids[0] = *y_dim; netcdf_func( nc_def_var (ncid, "y", NC_DOUBLE, 1, dimids, &y_var) ); str = "y coordinate of projection"; // str = "projection_y_coordinate"; //it is standard_name, //but could require grid_mapping definition that is //projection-dependant; could do it later; //do not need lat-lon values in this case as these would be //computed in processing software (CF convention) netcdf_func( nc_put_att_text(ncid, y_var, "long_name", strlen(str), str) ); str = "projection_y_coordinate"; netcdf_func( nc_put_att_text(ncid, y_var, "standard_name", strlen(str), str) ); dimids[0] = *x_dim; netcdf_func( nc_def_var (ncid, "x", NC_DOUBLE, 1, dimids, &x_var) ); str = "x coordinate of projection"; netcdf_func( nc_put_att_text(ncid, x_var, "long_name", strlen(str), str) ); str = "projection_x_coordinate"; netcdf_func( nc_put_att_text(ncid, x_var, "standard_name", strlen(str), str) ); str = "m"; netcdf_func( nc_put_att_text(ncid, y_var, "units", strlen(str), str) ); netcdf_func( nc_put_att_text(ncid, x_var, "units", strlen(str), str) ); netcdf_func( nc_put_att_double(ncid, x_var, "grid_spacing", NC_DOUBLE, 1, &dx) ); netcdf_func( nc_put_att_double(ncid, y_var, "grid_spacing", NC_DOUBLE, 1, &dy) ); dimids[0] = *y_dim; dimids[1] = *x_dim; netcdf_func( nc_def_var (ncid, "latitude", NC_DOUBLE, 2, dimids, &lat_var) ); netcdf_func( nc_def_var (ncid, "longitude", NC_DOUBLE, 2, dimids, &lon_var) ); /*#ifdef DEBUG_NC fprintf(stderr,"netcdf:create_nc_dims: y(dim,var,ny)=%d %d %d\n",*y_dim,y_var,ny); fprintf(stderr,"netcdf:create_nc_dims: x(dim,var,nx)=%d %d %d\n",*x_dim,x_var,nx); fprintf(stderr,"netcdf:create_nc_var (2D): latitude(xdim,ydim,var)=%d %d %d\n",*y_dim,*x_dim,lat_var); fprintf(stderr,"netcdf:create_nc_var (2D): longitude(xdim,ydim,var)=%d %d %d\n",*y_dim,*x_dim,lon_var); #endif*/ } else print_error("netcdf:create_nc_dims: %s","unsupported lat-lon dimension"); str = "degrees_north"; netcdf_func( nc_put_att_text(ncid, lat_var, "units", strlen(str), str) ); str = "degrees_east"; netcdf_func( nc_put_att_text(ncid, lon_var, "units", strlen(str), str) ); str = "latitude"; netcdf_func( nc_put_att_text(ncid, lat_var, "long_name", strlen(str), str) ); str = "longitude"; netcdf_func( nc_put_att_text(ncid, lon_var, "long_name", strlen(str), str) ); /* time settings */ dimids[0] = *time_dim; netcdf_func(nc_def_var (ncid, "time", NC_DOUBLE, 1, dimids, time_var)); netcdf_func(nc_put_att_double(ncid, *time_var, "_FillValue", NC_DOUBLE, 1, &fill_value)); str = "seconds since 1970-01-01 00:00:00.0 0:00"; netcdf_func( nc_put_att_text(ncid, *time_var, "units", strlen(str), str) ); //str = "verification time generated by function verftime()"; //netcdf_func( nc_put_att_text(ncid, *time_var, "long_name", strlen(str), str) ); /* nc_ref_time is reference time value, nc_ref_time_type is the reference time type: 0 - undefined 1 - analyses, all for the same reference date, could be succeded by forecasts 2 - analyses, but for different reference date/time (time serie) 3 - forecasts from the same reference date/time For the type 0 or 2 nc_ref_time keeps first field reference date/time */ if (fabs(ref_utime - verf_utime) < TM_TOLERANCE ) { ref_time_type = 1; str = "analyses, reference date is fixed"; } else { ref_time_type = 3; str = "forecast or accumulated, reference date is fixed"; } netcdf_func( nc_put_att_double(ncid, *time_var, "reference_time", NC_DOUBLE, 1, &ref_utime) ); netcdf_func( nc_put_att_int(ncid, *time_var, "reference_time_type", NC_INT, 1, &ref_time_type) ); my_get_unixdate(ref_utime, ref_date); netcdf_func( nc_put_att_text(ncid, *time_var, "reference_date", strlen(ref_date), ref_date) ); netcdf_func( nc_put_att_text(ncid, *time_var, "reference_time_description", strlen(str), str) ); /* write time step attributes, could be -1 (undefined) for first or single time step, then update */ if ( time_step_type ) { str="user"; } else { str="auto"; } netcdf_func( nc_put_att_text(ncid, *time_var, "time_step_setting", strlen(str), str) ); netcdf_func( nc_put_att_double(ncid, *time_var, "time_step", NC_DOUBLE, 1, &time_step) ); #ifdef DEBUG_NC fprintf(stderr,"netcdf:create_nc_dims: time(dim,var)=%d %d, size unlimited\n",*time_dim,*time_var); fprintf(stderr,"netcdf:create_nc_dims: time_step=%.1lf time_step_type=%d (%s)\n", time_step,time_step_type,str); #endif netcdf_func( nc_enddef(ncid) ); /* * put var */ // populate coordinate variables... start[0] = 0; netcdf_func( nc_put_var1_double(ncid, *time_var, start, &date0) ); if ( time_ind == 1 ) { start[0] = 1; netcdf_func( nc_put_var1_double(ncid, *time_var, start, &verf_utime) ); } else if (time_ind > 1) { for (j=1; j <= time_ind; j++) { start[0] = j; ttime = date0 + (time_step)*j; netcdf_func( nc_put_var1_double(ncid, *time_var, start, &ttime) ); } } else if (time_ind < 0) { fatal_error("netcdf:create_nc_dims: %s","negative time index"); } netcdf_func(nc_inq_dimlen(ncid, *x_dim, &x_len)); netcdf_func(nc_inq_dimlen(ncid, *y_dim, &y_len)); i = MAX(x_len, y_len); test_ll = (double*) malloc(i*sizeof(double)); if (!test_ll) fatal_error("netcdf:create_nc_dims: %s", "error doing malloc of test_ll"); if (dim_latlon == 1) { set_hyperslab(count, start, &y_len, 1); for (i=0; i < count[0] ; i++) test_ll[i] = lat_data[(i+start[0])*x_len]; //start[0] = 0; count[0] = y_len; netcdf_func( nc_put_vara_double(ncid, lat_var, start, count, test_ll) ); set_hyperslab(count, start, &x_len, 1); for (i=0; i<count[0]; i++) test_ll[i] = lon_data[i+start[0]]; //start[0] = 0; count[0] = x_len; netcdf_func( nc_put_vara_double(ncid, lon_var, start, count, test_ll) ); } else { set_hyperslab(count, start, &y_len, 1); for (i=0; i<count[0] ; i++) test_ll[i] = dy*(i+start[0]); //start[0] = 0; count[0] = y_len; netcdf_func( nc_put_vara_double(ncid, y_var, start, count, test_ll) ); set_hyperslab(count, start, &x_len, 1); for (i=0; i<count[0]; i++) test_ll[i] = dx*(i+start[0]); //start[0] = 0; count[0] = x_len; netcdf_func( nc_put_vara_double(ncid, x_var, start, count, test_ll) ); //start[0] = 0; count[0] = y_len; //start[1] = 0; count[1] = x_len; dimlens[0] = y_len; dimlens[1] = x_len; set_hyperslab(count, start, dimlens, 2); netcdf_func( nc_put_vara_double(ncid, lat_var, start, count, &(lat_data[start[0] * x_len + start[1]])) ); netcdf_func( nc_put_vara_double(ncid, lon_var, start, count, &(lon_data[start[0] * x_len + start[1]])) ); } free(test_ll); }
int mov_session::save() { int ierr,ncid,i; int dimid_ntimeseries,dimid_one; int varid_filename,varid_colors,varid_units,varid_names; int varid_xshift,varid_yshift,varid_type,varid_coldstart; int varid_stationfile,varid_plottitle,varid_xlabel,varid_ylabel; int varid_startdate,varid_enddate,varid_precision,varid_ymin,varid_ymax; int varid_autodate,varid_autoy,varid_checkState; int dims_1d[1]; int nTimeseries; QString relPath,TempFile,Directory,tempString; QByteArray tempByte; size_t start[1]; size_t iu; double mydatadouble[1]; int mydataint[1]; const char * mydatastring[1]; QFile Session(this->sessionFileName); QVector<QString> filenames_ts; QVector<QString> filetype_ts; QVector<QString> colors_ts; QVector<double> units_ts; QVector<QString> seriesname_ts; QVector<double> xshift_ts; QVector<double> yshift_ts; QVector<QString> date_ts; QVector<QString> stationfile_ts; QVector<int> checkStates_ts; //Remove the old file if(Session.exists()) Session.remove(); //Get the path of the session file so we can save a relative path later mov_generic::splitPath(this->sessionFileName,TempFile,Directory); QDir CurrentDir(Directory); ierr = mov_generic::NETCDF_ERR(nc_create(this->sessionFileName.toUtf8(),NC_NETCDF4,&ncid)); if(ierr!=NC_NOERR)return 1; //Start setting up the definitions nTimeseries = this->tableWidget->rowCount(); filenames_ts.resize(nTimeseries); colors_ts.resize(nTimeseries); units_ts.resize(nTimeseries); seriesname_ts.resize(nTimeseries); xshift_ts.resize(nTimeseries); yshift_ts.resize(nTimeseries); date_ts.resize(nTimeseries); stationfile_ts.resize(nTimeseries); filetype_ts.resize(nTimeseries); checkStates_ts.resize(nTimeseries); for(i=0;i<nTimeseries;i++) { filenames_ts[i] = this->tableWidget->item(i,6)->text(); seriesname_ts[i] = this->tableWidget->item(i,1)->text(); colors_ts[i] = this->tableWidget->item(i,2)->text(); units_ts[i] = this->tableWidget->item(i,3)->text().toDouble(); xshift_ts[i] = this->tableWidget->item(i,4)->text().toDouble(); yshift_ts[i] = this->tableWidget->item(i,5)->text().toDouble(); date_ts[i] = this->tableWidget->item(i,7)->text(); filetype_ts[i] = this->tableWidget->item(i,8)->text(); stationfile_ts[i] = this->tableWidget->item(i,10)->text(); if(this->tableWidget->item(i,0)->checkState()==Qt::Checked) checkStates_ts[i] = 1; else checkStates_ts[i] = 0; } ierr = mov_generic::NETCDF_ERR(nc_def_dim(ncid,"ntimeseries",static_cast<size_t>(nTimeseries),&dimid_ntimeseries)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_dim(ncid,"one",1,&dimid_one)); if(ierr!=NC_NOERR)return 1; //Arrays dims_1d[0] = dimid_ntimeseries; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_filename",NC_STRING,1,dims_1d,&varid_filename)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_colors",NC_STRING,1,dims_1d,&varid_colors)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_names",NC_STRING,1,dims_1d,&varid_names)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_filetype",NC_STRING,1,dims_1d,&varid_type)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_coldstartdate",NC_STRING,1,dims_1d,&varid_coldstart)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_stationfile",NC_STRING,1,dims_1d,&varid_stationfile)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_xshift",NC_DOUBLE,1,dims_1d,&varid_xshift)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_yshift",NC_DOUBLE,1,dims_1d,&varid_yshift)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_units",NC_DOUBLE,1,dims_1d,&varid_units)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_checkState",NC_INT,1,dims_1d,&varid_checkState)); if(ierr!=NC_NOERR)return 1; //Scalars dims_1d[0] = dimid_one; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_plottitle",NC_STRING,1,dims_1d,&varid_plottitle)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_xlabel",NC_STRING,1,dims_1d,&varid_xlabel)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_ylabel",NC_STRING,1,dims_1d,&varid_ylabel)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_precision",NC_INT,1,dims_1d,&varid_precision)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_startdate",NC_STRING,1,dims_1d,&varid_startdate)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_enddate",NC_STRING,1,dims_1d,&varid_enddate)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_ymin",NC_DOUBLE,1,dims_1d,&varid_ymin)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_ymax",NC_DOUBLE,1,dims_1d,&varid_ymax)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_autodate",NC_INT,1,dims_1d,&varid_autodate)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_autoy",NC_INT,1,dims_1d,&varid_autoy)); if(ierr!=NC_NOERR)return 1; ierr = mov_generic::NETCDF_ERR(nc_enddef(ncid)); if(ierr!=NC_NOERR)return 1; tempByte = this->plotTitleWidget->text().toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_plottitle,mydatastring)); if(ierr!=NC_NOERR)return 1; tempByte = this->xLabelWidget->text().toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_xlabel,mydatastring)); if(ierr!=NC_NOERR)return 1; tempByte = this->yLabelWidget->text().toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_ylabel,mydatastring)); if(ierr!=NC_NOERR)return 1; tempByte = this->startDateEdit->dateTime().toString("yyyy-MM-dd hh:mm:ss").toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_startdate,mydatastring)); if(ierr!=NC_NOERR)return 1; tempByte = this->endDateEdit->dateTime().toString("yyyy-MM-dd hh:mm:ss").toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_enddate,mydatastring)); if(ierr!=NC_NOERR)return 1; mydataint[0] = 3; ierr = mov_generic::NETCDF_ERR(nc_put_var_int(ncid,varid_precision,mydataint)); if(ierr!=NC_NOERR)return 1; mydatadouble[0] = this->yMinSpinBox->value(); ierr = mov_generic::NETCDF_ERR(nc_put_var_double(ncid,varid_ymin,mydatadouble)); if(ierr!=NC_NOERR)return 1; mydatadouble[0] = this->yMaxSpinBox->value(); ierr = mov_generic::NETCDF_ERR(nc_put_var_double(ncid,varid_ymax,mydatadouble)); if(ierr!=NC_NOERR)return 1; if(this->checkAllData->isChecked()) mydataint[0] = 1; else mydataint[0] = 0; ierr = mov_generic::NETCDF_ERR(nc_put_var_int(ncid,varid_autodate,mydataint)); if(ierr!=NC_NOERR)return 1; if(this->checkYAuto->isChecked()) mydataint[0] = 1; else mydataint[0] = 0; ierr = mov_generic::NETCDF_ERR(nc_put_var_int(ncid,varid_autoy,mydataint)); if(ierr!=NC_NOERR)return 1; for(iu=0;iu<static_cast<unsigned int>(nTimeseries);iu++) { start[0] = iu; relPath = CurrentDir.relativeFilePath(filenames_ts[iu]); tempByte = relPath.toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_filename,start,mydatastring)); if(ierr!=NC_NOERR)return 1; mydatastring[0] = NULL; tempString = seriesname_ts[iu]; tempByte = tempString.toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_names,start,mydatastring)); if(ierr!=NC_NOERR)return 1; mydatastring[0] = NULL; tempByte = colors_ts[iu].toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_colors,start,mydatastring)); if(ierr!=NC_NOERR)return 1; mydatastring[0] = NULL; tempByte = date_ts[iu].toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_coldstart,start,mydatastring)); if(ierr!=NC_NOERR)return 1; mydatastring[0] = NULL; relPath = CurrentDir.relativeFilePath(stationfile_ts[iu]); tempByte = relPath.toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_stationfile,start,mydatastring)); if(ierr!=NC_NOERR)return 1; mydatastring[0] = NULL; tempByte = filetype_ts[iu].toUtf8(); mydatastring[0] = tempByte.data(); ierr = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_type,start,mydatastring)); if(ierr!=NC_NOERR)return 1; mydatastring[0] = NULL; mydatadouble[0] = xshift_ts[iu]; ierr = mov_generic::NETCDF_ERR(nc_put_var1_double(ncid,varid_xshift,start,mydatadouble)); if(ierr!=NC_NOERR)return 1; mydatadouble[0] = yshift_ts[iu]; ierr = mov_generic::NETCDF_ERR(nc_put_var1_double(ncid,varid_yshift,start,mydatadouble)); if(ierr!=NC_NOERR)return 1; mydatadouble[0] = units_ts[iu]; ierr = mov_generic::NETCDF_ERR(nc_put_var1_double(ncid,varid_units,start,mydatadouble)); if(ierr!=NC_NOERR)return 1; mydataint[0] = checkStates_ts[iu]; ierr = mov_generic::NETCDF_ERR(nc_put_var1_int(ncid,varid_checkState,start,mydataint)); if(ierr!=NC_NOERR)return 1; } ierr = mov_generic::NETCDF_ERR(nc_close(ncid)); if(ierr!=NC_NOERR)return 1; }
int main(int argc, char **argv) { char file_name[NC_MAX_NAME + 1]; #ifdef USE_PARALLEL MPI_Init(&argc, &argv); #endif printf("\n*** Testing really large files in netCDF-4/HDF5 format, quickly.\n"); printf("*** Testing create of simple, but large, file..."); { #define DIM_NAME "Time_in_nanoseconds" #define NUMDIMS 1 #define NUMVARS 4 int ncid, dimids[NUMDIMS], varid[NUMVARS], chunksize[NUMDIMS]; char var_name[NUMVARS][NC_MAX_NAME + 1] = {"England", "Scotland", "Ireland", "Wales"}; size_t index[NUMDIMS] = {QTR_CLASSIC_MAX-1}; int ndims, nvars, natts, unlimdimid; nc_type xtype; char name_in[NC_MAX_NAME + 1]; size_t len; double pi = 3.1459, pi_in; int i; /* Create a netCDF netCDF-4/HDF5 format file, with 4 vars. */ sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME); if (nc_create(file_name, NC_NETCDF4, &ncid)) ERR; if (nc_set_fill(ncid, NC_NOFILL, NULL)) ERR; if (nc_def_dim(ncid, DIM_NAME, QTR_CLASSIC_MAX, dimids)) ERR; chunksize[0] = MEGABYTE/DOUBLE_SIZE; for (i = 0; i < NUMVARS; i++) { if (nc_def_var(ncid, var_name[i], NC_DOUBLE, NUMDIMS, dimids, &varid[i])) ERR; if (nc_def_var_chunking(ncid, i, 0, chunksize)) ERR; } if (nc_enddef(ncid)) ERR; for (i = 0; i < NUMVARS; i++) if (nc_put_var1_double(ncid, i, index, &pi)) ERR; if (nc_close(ncid)) ERR; /* Reopen and check the file. */ if (nc_open(file_name, 0, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; if (ndims != NUMDIMS || nvars != NUMVARS || natts != 0 || unlimdimid != -1) ERR; if (nc_inq_dimids(ncid, &ndims, dimids, 1)) ERR; if (ndims != 1 || dimids[0] != 0) ERR; if (nc_inq_dim(ncid, 0, name_in, &len)) ERR; if (strcmp(name_in, DIM_NAME) || len != QTR_CLASSIC_MAX) ERR; for (i = 0; i < NUMVARS; i++) { if (nc_inq_var(ncid, i, name_in, &xtype, &ndims, dimids, &natts)) ERR; if (strcmp(name_in, var_name[i]) || xtype != NC_DOUBLE || ndims != 1 || dimids[0] != 0 || natts != 0) ERR; if (nc_get_var1_double(ncid, i, index, &pi_in)) ERR; if (pi_in != pi) ERR; } if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; #ifdef USE_PARALLEL MPI_Finalize(); #endif FINAL_RESULTS; }
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); }
int main(int argc,char *argv[]) { struct DataMap *ptr; struct DataMapScalar *sx,*sy; struct DataMapArray *ax,*ay; size_t index[256]; size_t start[256]; size_t count[256]; int s; unsigned char vbflg=0; unsigned char help=0; unsigned char option=0; unsigned char zflg=0; FILE *fp=NULL; gzFile zfp=0; FILE *mapfp; int n,c,x; int ncid; int block=0; int varid; int strsze; char **strptr; char *tmpbuf=NULL; OptionAdd(&opt,"-help",'x',&help); OptionAdd(&opt,"-option",'x',&option); OptionAdd(&opt,"vb",'x',&vbflg); OptionAdd(&opt,"z",'x',&zflg); if (argc>1) { arg=OptionProcess(1,argc,argv,&opt,NULL); if (help==1) { OptionPrintInfo(stdout,hlpstr); exit(0); } if (option==1) { OptionDump(stdout,&opt); exit(0); } if (zflg) { zfp=gzopen(argv[arg],"r"); if (zfp==0) { fprintf(stderr,"File not found.\n"); exit(-1); } } else { fp=fopen(argv[arg],"r"); if (fp==NULL) { fprintf(stderr,"File not found.\n"); exit(-1); } } } else { OptionPrintInfo(stdout,errstr); exit(-1); } /* load the map */ mapfp=fopen(argv[arg+1],"r"); loadmap(mapfp); fclose(mapfp); s=nc_open(argv[arg+2],NC_WRITE,&ncid); if (s !=NC_NOERR) { fprintf(stderr,"Error opening CDF file.\n"); exit(-1); } block=0; while (1) { if (zflg) ptr=DataMapReadZ(zfp); else ptr=DataMapFread(fp); if (ptr==NULL) break; for (c=0;c<ptr->snum;c++) { sx=ptr->scl[c]; for (n=0;n<snum;n++) { sy=sptr[n]; if (strcmp(sx->name,sy->name) !=0) continue; if (sx->type !=sy->type) continue; break; } if (n !=snum) { /* mapped variable */ s=nc_inq_varid(ncid,cdfsname[n],&varid); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } index[0]=block; switch (sx->type) { case DATACHAR: s=nc_put_var1_text(ncid,varid,index,sx->data.cptr); break; case DATASHORT: s=nc_put_var1_short(ncid,varid,index,sx->data.sptr); break; case DATAINT: s=nc_put_var1_int(ncid,varid,index,sx->data.iptr); break; case DATAFLOAT: s=nc_put_var1_float(ncid,varid,index,sx->data.fptr); break; case DATADOUBLE: s=nc_put_var1_double(ncid,varid,index,sx->data.dptr); break; case DATASTRING: start[0]=block; start[1]=0; count[0]=1; count[1]=strlen(*((char **) sx->data.vptr))+1; s=nc_put_vara_text(ncid,varid,start,count, *((char **) sx->data.vptr)); break; } if (s !=NC_NOERR) { fprintf(stderr,"Error writing CDF file (%d).\n",s); exit(-1); } } } for (c=0;c<ptr->anum;c++) { ax=ptr->arr[c]; for (n=0;n<anum;n++) { ay=aptr[n]; if (strcmp(ax->name,ay->name) !=0) continue; if (ax->type !=ay->type) continue; if (ax->dim !=ay->dim) continue; break; } if (n !=anum) { /* mapped variable */ s=nc_inq_varid(ncid,cdfaname[n],&varid); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } start[0]=block; count[0]=1; n=1; for (x=0;x<ax->dim;x++) { start[1+x]=0; count[1+x]=ax->rng[x]; n=n*ax->rng[x]; } if (ax->type==DATASTRING) { int ndims; int dimids[NC_MAX_VAR_DIMS]; size_t dimlen; s=nc_inq_varndims(ncid,varid,&ndims); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } s=nc_inq_vardimid(ncid,varid,dimids); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } if (ndims-2!=ax->dim) { fprintf(stderr,"Error matching dimensions.\n"); exit(-1); } s=nc_inq_dimlen(ncid,dimids[ndims-1],&dimlen); if (s !=NC_NOERR) { fprintf(stderr,"Error accessing CDF file.\n"); exit(-1); } strsze=dimlen; tmpbuf=malloc(n*strsze); if (tmpbuf==NULL) { fprintf(stderr,"Failed to allocate buffer.\n"); exit(-1); } memset(tmpbuf,0,n*strsze); start[1+ax->dim]=0; count[1+ax->dim]=strsze; strptr=(char **) ax->data.vptr; for (x=0;x<n;x++) strncpy(tmpbuf+x*strsze,strptr[x],strsze); } switch (ax->type) { case DATACHAR: s=nc_put_vara_text(ncid,varid,start,count,ax->data.cptr); break; case DATASHORT: s=nc_put_vara_short(ncid,varid,start,count,ax->data.sptr); break; case DATAINT: s=nc_put_vara_int(ncid,varid,start,count,ax->data.iptr); break; case DATAFLOAT: s=nc_put_vara_float(ncid,varid,start,count,ax->data.fptr); break; case DATADOUBLE: s=nc_put_vara_double(ncid,varid,start,count,ax->data.dptr); break; case DATASTRING: s=nc_put_vara_text(ncid,varid,start,count,tmpbuf); break; } if (tmpbuf !=NULL) { free(tmpbuf); tmpbuf=NULL; } if (s !=NC_NOERR) { fprintf(stderr,"Error writing CDF file (%d).\n",s); exit(-1); } } } DataMapFree(ptr); block++; } nc_close(ncid); if (zflg) gzclose(zfp); else fclose(fp); return 0; }
int main() { 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; }
/*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; }