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 ex_get_time (int exoid, int time_step, 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_get_time",errmsg,exerrval); return (EX_FATAL); } /* read time value */ start[0] = --time_step; if (ex_comp_ws(exoid) == 4) { status = nc_get_var1_float(exoid, varid, start, time_value); } else { status = nc_get_var1_double(exoid, varid, start, time_value); } if (status != NC_NOERR) { exerrval = status; sprintf(errmsg, "Error: failed to get time value in file id %d", exoid); ex_err("ex_get_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; }
sta_struct *decode_fsl2(int cdfid, long miss, int *iret) { int ndims,nvars,natts,nunlim; int tmpint[20]; int ier,i,j,unlimsiz; int wmoStaNum_id,wmoStaNum,staName_id; char staName[20]; int staLat_id,staLon_id,staElev_id,timeObs_id,levels_id; int uwnd_id,vwnd_id,wwnd_id,uv_qual_id,w_qual_id,levelMode_id; int sigma_uv_id,sigma_w_id; int sfc_sped_id,sfc_drct_id,sfc_pres_id,sfc_temp_id,sfc_relh_id,sfc_rain_id; float staLat,staLon,staElev,level; float uwnd,vwnd,wwnd,sigma_uv,sigma_w; int uv_qual,w_qual,levelMode; float sfc_sped,sfc_drct,sfc_pres,sfc_temp,sfc_relh,sfc_rain; double timeObs; nc_type xtype; int nvdims,nvatts,time_interval; size_t dimsiz,var_i[5],vc[5],namelen; float ufill,vfill,wfill; float pfill,tfill,dfill,sfill,rfill,rrfill; float fmiss,e; time_t obs_time; char timestr[80],*atttext; int year,month,day,hour,minute; struct tm *gmt_time=NULL,new_time; sta_struct *stadat,*head=NULL; prof_data *plev,*plast; udebug("decoding fsl2\0"); fmiss = (float)miss; ier = nc_inq(cdfid,&ndims,&nvars,&natts,&nunlim); ier = nc_inq_atttype(cdfid,NC_GLOBAL,"avgTimePeriod",&xtype); if(xtype == NC_CHAR) { ier = nc_inq_attlen(cdfid,NC_GLOBAL,"avgTimePeriod",&namelen); udebug("AvgTimPeriod name len is %d",namelen); atttext = (char *)malloc(namelen + 1); ier = nc_get_att_text(cdfid,NC_GLOBAL,"avgTimePeriod",atttext); sscanf(atttext,"%d",tmpint); udebug("AvgTimPeriod type is NC_CHAR %s VAL %d",atttext,tmpint[0]); free(atttext); } else { ier = nc_get_att_int(cdfid,NC_GLOBAL,"avgTimePeriod",tmpint); } udebug("AvgTimPeriod is %d\0",tmpint[0]); time_interval = tmpint[0]; ier = 0; ier += nc_inq_varid(cdfid,"wmoStaNum",&wmoStaNum_id); ier += nc_inq_varid(cdfid,"staName",&staName_id); ier += nc_inq_varid(cdfid,"staLat",&staLat_id); ier += nc_inq_varid(cdfid,"staLon",&staLon_id); ier += nc_inq_varid(cdfid,"staElev",&staElev_id); ier += nc_inq_varid(cdfid,"timeObs",&timeObs_id); ier += nc_inq_varid(cdfid,"levels",&levels_id); ier += nc_inq_varid(cdfid,"uComponent",&uwnd_id); ier += nc_inq_varid(cdfid,"vComponent",&vwnd_id); ier += nc_inq_varid(cdfid,"wComponent",&wwnd_id); ier += nc_get_att_float(cdfid,uwnd_id,"_FillValue",&ufill); ier += nc_get_att_float(cdfid,vwnd_id,"_FillValue",&vfill); ier += nc_get_att_float(cdfid,wwnd_id,"_FillValue",&wfill); ier += nc_inq_varid(cdfid,"uvQualityCode",&uv_qual_id); ier += nc_inq_varid(cdfid,"wQualityCode",&w_qual_id); ier += nc_inq_varid(cdfid,"windSpeedStdDev",&sigma_uv_id); ier += nc_inq_varid(cdfid,"wStdDev",&sigma_w_id); ier += nc_inq_varid(cdfid,"levelMode",&levelMode_id); ier += nc_inq_varid(cdfid,"windSpeedSfc",&sfc_sped_id); ier += nc_inq_varid(cdfid,"windDirSfc",&sfc_drct_id); ier += nc_inq_varid(cdfid,"pressure",&sfc_pres_id); ier += nc_inq_varid(cdfid,"temperature",&sfc_temp_id); ier += nc_inq_varid(cdfid,"relHumidity",&sfc_relh_id); ier += nc_inq_varid(cdfid,"rainRate",&sfc_rain_id); ier += nc_get_att_float(cdfid,sfc_sped_id,"_FillValue",&sfill); ier += nc_get_att_float(cdfid,sfc_drct_id,"_FillValue",&dfill); ier += nc_get_att_float(cdfid,sfc_pres_id,"_FillValue",&pfill); ier += nc_get_att_float(cdfid,sfc_temp_id,"_FillValue",&tfill); ier += nc_get_att_float(cdfid,sfc_relh_id,"_FillValue",&rfill); ier += nc_get_att_float(cdfid,sfc_rain_id,"_FillValue",&rrfill); if(ier != 0) { uerror("could not get station information\0"); *iret = -1; return(NULL); } ier = nc_inq_vardimid(cdfid,staName_id,tmpint); ier += nc_inq_dimlen(cdfid,tmpint[1],&namelen); tmpint[0] = 0;tmpint[1] = 0; ier += nc_inq_var(cdfid,wmoStaNum_id,NULL, &xtype, &nvdims, tmpint, &nvatts); ier += nc_inq_dimlen(cdfid,tmpint[0],&dimsiz); if(ier == 0) unlimsiz = dimsiz; for(i=0;i<unlimsiz;i++) { var_i[0] = i; var_i[1] = 0; vc[0] = 1; vc[1] = namelen-1; memset(staName,'\0',20); ier = nc_get_vara_text(cdfid,staName_id,var_i,vc,staName); ier = nc_get_var1_int(cdfid,wmoStaNum_id,var_i,&wmoStaNum); ier = nc_get_var1_float(cdfid,staLat_id,var_i,&staLat); ier = nc_get_var1_float(cdfid,staLon_id,var_i,&staLon); ier = nc_get_var1_float(cdfid,staElev_id,var_i,&staElev); ier = nc_get_var1_float(cdfid,sfc_sped_id,var_i,&sfc_sped); ier = nc_get_var1_float(cdfid,sfc_drct_id,var_i,&sfc_drct); ier = nc_get_var1_float(cdfid,sfc_pres_id,var_i,&sfc_pres); ier = nc_get_var1_float(cdfid,sfc_temp_id,var_i,&sfc_temp); ier = nc_get_var1_float(cdfid,sfc_relh_id,var_i,&sfc_relh); ier = nc_get_var1_float(cdfid,sfc_rain_id,var_i,&sfc_rain); ier = nc_get_var1_double(cdfid,timeObs_id,var_i,&timeObs); obs_time = (time_t) timeObs; gmt_time = gmtime(&obs_time); new_time = *gmt_time; timestr[0] = '\0'; strftime(timestr,80,"%Y %m %d %H %M",&new_time); sscanf(timestr,"%d %d %d %d %d",&year,&month,&day,&hour,&minute); udebug("Station %3d %8d %s = %6.2f %7.2f %5.0f %s\0",i,wmoStaNum, staName,staLat,staLon,staElev,timestr); stadat = (sta_struct *)malloc(sizeof(sta_struct)); if(stadat == NULL) { uerror("Could not allocate station data structure\0"); exit(-2); } stadat->wmoStaNum = wmoStaNum; stadat->staName = (char *)malloc(strlen(staName)+1); strcpy(stadat->staName,staName); stadat->staLat = staLat; stadat->staLon = staLon; stadat->staElev = staElev; stadat->timeObs = timeObs; stadat->year = year; stadat->month = month; stadat->day = day; stadat->hour = hour; stadat->minute = minute; stadat->time_interval = time_interval; stadat->pdata = NULL; stadat->rdata = NULL; stadat->sfc_pres = fmiss; stadat->sfc_temp = fmiss; stadat->sfc_sped = fmiss; stadat->sfc_drct = fmiss; stadat->sfc_relh = fmiss; stadat->sfc_rain_rate = fmiss; stadat->sfc_rain_amt = fmiss; stadat->sfc_dwpc = fmiss; if(sfc_pres != pfill) stadat->sfc_pres = sfc_pres; if(sfc_temp != tfill) stadat->sfc_temp = sfc_temp - 273.15; if(sfc_sped != sfill) stadat->sfc_sped = sfc_sped; if(sfc_drct != dfill) stadat->sfc_drct = sfc_drct; if(sfc_relh != rfill) stadat->sfc_relh = sfc_relh; if(sfc_rain != rrfill) stadat->sfc_rain_rate = sfc_rain; if((stadat->sfc_temp != fmiss)&&(stadat->sfc_relh != fmiss)) { VAPOR_PRES(stadat->sfc_temp+273.15,&e); e = e * (stadat->sfc_relh / 100.); t_from_e(e,&stadat->sfc_dwpc); stadat->sfc_dwpc = stadat->sfc_dwpc - 273.15; } ier = nc_inq_var(cdfid,levels_id,NULL, &xtype, &nvdims, tmpint, &nvatts); if(ier == 0) { ier = nc_inq_dimlen(cdfid,tmpint[0],&dimsiz); stadat->numlevs = dimsiz; plast = stadat->pdata; for(j=0;j<stadat->numlevs;j++) { var_i[0] = j; ier = nc_get_var1_float(cdfid,levels_id,var_i,&level); ier = nc_get_var1_int(cdfid,levelMode_id,var_i,&levelMode); var_i[0] = i; var_i[1] = j; ier = nc_get_var1_float(cdfid,uwnd_id,var_i,&uwnd); ier = nc_get_var1_float(cdfid,vwnd_id,var_i,&vwnd); ier = nc_get_var1_float(cdfid,wwnd_id,var_i,&wwnd); ier = nc_get_var1_int(cdfid,uv_qual_id,var_i,&uv_qual); ier = nc_get_var1_int(cdfid,w_qual_id,var_i,&w_qual); ier = nc_get_var1_float(cdfid,sigma_uv_id,var_i,&sigma_uv); ier = nc_get_var1_float(cdfid,sigma_w_id,var_i,&sigma_w); plev = (prof_data *)malloc(sizeof(prof_data)); if(plev != NULL) { plev->level = level; if(uwnd == ufill) uwnd = fmiss; if(vwnd == vfill) vwnd = fmiss; if(wwnd == wfill) wwnd = fmiss; if(uv_qual != 0) { uwnd = fmiss; vwnd = fmiss; } if(w_qual != 0) wwnd = fmiss; if((uwnd == fmiss)||(vwnd == fmiss)) sigma_uv = fmiss; if(wwnd == fmiss) sigma_w = fmiss; plev->u = uwnd; plev->v = vwnd; plev->w = wwnd; plev->sigma_uv = sigma_uv; plev->sigma_w = sigma_w; plev->levmode = levelMode; plev->nextlev = NULL; if(plast == NULL) stadat->pdata = plev; else plast->nextlev = plev; plast = plev; } } } else stadat->numlevs = 0; stadat->next = head; head = stadat; } return(head); }
/* * Get a single numeric value from a variable of an open netCDF file. */ static void c_ncvgt1 ( int ncid, /* netCDF ID */ int varid, /* variable ID */ const size_t* indices,/* multidim index of data to be read */ void* value, /* pointer to data value to be read */ 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_get_var1_schar(ncid, varid, indices, (signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_get_var1_short(ncid, varid, indices, (short*)value); # elif NF_INT1_IS_C_INT status = nc_get_var1_int(ncid, varid, indices, (int*)value); # elif NF_INT1_IS_C_LONG status = nc_get_var1_long(ncid, varid, indices, (long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_get_var1_short(ncid, varid, indices, (short*)value); # elif NF_INT2_IS_C_INT status = nc_get_var1_int(ncid, varid, indices, (int*)value); # elif NF_INT2_IS_C_LONG status = nc_get_var1_long(ncid, varid, indices, (long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_get_var1_int(ncid, varid, indices, (int*)value); # elif NF_INT_IS_C_LONG status = nc_get_var1_long(ncid, varid, indices, (long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_get_var1_float(ncid, varid, indices, (float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_get_var1_double(ncid, varid, indices, (double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_get_var1_float(ncid, varid, indices, (float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_get_var1_double(ncid, varid, indices, (double*)value); # endif break; } } if (status == 0) *rcode = 0; else { nc_advise("NCVGT1", 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() */
/*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; }
int Ensemble::getTimes(std::string &str) { // reading netcdf files int ncid, dimid, varid, status; size_t recSize, len; size_t index=0; double val ; int retVal=0; std::string begStr; std::string endStr; for( size_t i=0 ; i < member.size() ; ++i ) { if( nc_open( member[i]->getFile().c_str(), 0, &ncid) ) { isInvalid = true; member[i]->state = "could not open file"; continue; } nc_inq_unlimdim(ncid, &dimid) ; if( dimid == -1 ) // try for a regular dimension time nc_inq_dimid(ncid, "time", &dimid); // no unlimited variable found; this is not // neccessarily an error. if( dimid == -1 ) { isNoRec = true; nc_close(ncid); if( sz == 1 ) return 0; // a single file else retVal=4; isInvalid = true; member[i]->state += "missing time dimension"; continue; } if( (status = nc_inq_dimlen(ncid, dimid, &recSize) ) ) { isInvalid = true; member[i]->state = "could not read time dimension"; nc_close(ncid); continue; } if( status == 0 && recSize == 0 ) { // this could be wrong // or just a fixed variable with time dimension defined. isNoRec = true; nc_close(ncid); if( sz == 1 ) return 0; isInvalid = true; member[i]->state = "no time values available"; continue; } if( (status = nc_inq_varid(ncid, "time", &varid) ) ) { isNoRec = true; nc_close(ncid); if( sz == 1 ) return 0; member[i]->state = "missing time variable"; continue; } // reference calendar std::vector<std::string> vs; if( getAttText(ncid, varid, "calendar", vs) ) { // failure member[i]->state = "missing or invalid calendar attribute"; nc_close(ncid); continue; } else // successful reading of calendar member[i]->refDate.setCalendar(vs[0]) ; // reference date if( getAttText(ncid, varid, "units", vs) ) { // failure member[i]->state = "missing or invalid units attribute"; nc_close(ncid); continue; } else { if( vs[0].find("%Y") < std::string::npos ) member[i]->refDate.setFormattedDate(); else member[i]->refDate = vs[0] ; } // time values: fist and last index=0; if( (status = nc_get_var1_double(ncid, varid, &index, &val) ) ) { if( sz > 1 ) { isInvalid = true; member[i]->state = "could not read first time value"; nc_close(ncid); continue; } } // any reasonable values available? nc_type var_type ; status = nc_inq_vartype(ncid, varid, &var_type) ; // return value type is double double fV; int noFill=0; int statusF = nc_inq_var_fill(ncid, varid, &noFill, &fV); if( noFill != 1 && (statusF || val == fV) ) { isInvalid = true; member[i]->state = "first time value equals _FillValue"; nc_close(ncid); continue; } if(member[i]->refDate.isValid(val)) member[i]->setBegin( member[i]->refDate.getDate(val) ); else { isInvalid=true; retVal=5; member[i]->putState("invalid data, found " + hdhC::double2String(val), false); nc_close(ncid); continue; } index=recSize-1; if( (status = nc_get_var1_double(ncid, varid, &index, &val) ) ) { if( sz > 1 ) { isInvalid = true; member[i]->putState("could not read last time value"); nc_close(ncid); } } if( noFill != 1 && (statusF || val == fV) ) { isInvalid = true; member[i]->putState("last time value equals _FillValue"); nc_close(ncid); continue; } if(member[i]->refDate.isValid(val)) member[i]->setEnd( member[i]->refDate.getDate(val) ); else { isInvalid=true; retVal=5; member[i]->putState("invalid data, found " + hdhC::double2String(val), false); } nc_close(ncid); } // any serious conditions if( isInvalid ) { // only files with annotations enablePrintOnlyMarked(); str = getOutput(); if( retVal ) return retVal; else return 3 ; // unspecific error } // get modification times of files for(size_t i=0 ; i < member.size() ; ++i ) member[i]->getModificationTime() ; // sorted vector of pointers of dates sortDate(); return 0; }
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; }
int read_field(int fn, int lev, char var_name[], double **array, size_t *nlay, size_t *nx, size_t *ny, double st[2]) { static int ncid; size_t start[MAX_VAR_DIMS] = {0}, count[MAX_VAR_DIMS]; int i, varid, ndims, dimids[MAX_VAR_DIMS], recdim, status; ncid = ncInid[fn]; if (ncid < 0) { printf("ERROR: Required file (%d) for reading %s is not open.\n",fn,var_name); exit(-1); } // printf("Inquire unlimdim of %s - id %d.\n",in_file,ncid); status = nc_inq_unlimdim(ncid,&recdim); if (status != NC_NOERR) handle_error(status,var_name,status); // printf("Done opening %s.\n",in_file); status = nc_inq_varid(ncid,var_name,&varid); if (status != NC_NOERR) handle_error(status,var_name,status); status = nc_inq_varndims(ncid,varid,&ndims); if (status != NC_NOERR) handle_error(status,var_name,status); status = nc_inq_vardimid(ncid,varid,dimids); if (status != NC_NOERR) handle_error(status,var_name,status); // printf("Get %d dim lengths %s.\n",ndims,in_file); for (i=0;i<ndims;i++) { status = nc_inq_dimlen(ncid,dimids[i],&count[i]); if (status != NC_NOERR) handle_error(status,var_name,i); } if (dimids[0] == recdim) { if (count[0] < lev+1) return -1; start[0] = lev; count[0] = 1; } else if (lev > 0) return -1; for (i=ndims-2;i<ndims;i++) { char dimname[NC_MAX_NAME]; int varid; size_t index = 0; status = nc_inq_dimname(ncid,dimids[i],dimname); if (status != NC_NOERR) handle_error(status,var_name,i); status = nc_inq_varid(ncid, dimname, &varid); if (status != NC_NOERR) handle_error(status,dimname,status); status = nc_get_var1_double(ncid, varid, &index, &st[i-ndims+2]); if (status != NC_NOERR) handle_error(status,dimname,status); } // printf("Done getting info about %s.\n",var_name); if (*array == NULL) { size_t sz = 1; for (i=0;i<ndims;i++) if (count[i] > 1) sz *= count[i]+1; // printf("Attempting to allocate array of size %ld.\n",(long) sz); *array = (double *) calloc(sz, sizeof(double)); if (*array == NULL) printf("Unable to allocate array of size %ld for %s.\n",(long) sz,var_name); } *nlay = count[ndims-3]; *nx = count[ndims-1]; *ny = count[ndims-2]; { size_t sz = 1; for (i=0;i<ndims;i++) if (count[i] > 1) sz *= count[i]; // printf("Attempting to read array of size %ld.\n",(long) sz); } status = nc_get_vara_double(ncid,varid,start,count,*array); if (status != NC_NOERR) return -1; return 0; }
void im_rcdf ( char imgfil[], int *kx, int *ky, int *isorc, int *itype, int *idate, int *itime, int *iproj, float *clat, float *clon, float *xmin, float *xmax, float *ymin, float *ymax, int *iret ) /************************************************************************ * im_rcdf * * * * This subroutine opens a NetCDF file and reads the header information * * and returns it to the calling function. * * * * im_rcdf ( imgfil, kx, ky, isorc, itype, idate, itime, iproj, * * clat, clon, xmin, xmax, ymin, ymax, iret ) * * * * Input parameters: * * imgfil[] char Name of image file * * * * Output parameters: * * *kx int x dimension of image * * *ky int y dimension of image * * *isorc int * * *itype int type of channel * * *idate int date * * *itime int time * * *iproj int projection type * * *clat float central latitude * * *clon float central longitude * * *xmin float x - minimum for image * * *xmax float x - maximum for image * * *ymin float y - minimum for image * * *ymax float y - maximum for image * * *iret int return value * * * * * ** * * Log: * * S. Jacobs/NCEP 6/99 Created * * R. Curtis/EAI 8/00 Updated and implemented in GEMPAK * * T. Piper/GSC 9/00 Modified for Lambert Conformal * * A. Hardy/GSC 7/01 Modified to use the channel table * ***********************************************************************/ { int ncid, iy, im, id, ih, in, is; size_t lenp, jx, jy; double dsec; time_t isec; struct tm *tarr; char chanl[81]; /*---------------------------------------------------------------------*/ *iret = 0; /* * Open the NetCDF file. */ nc_open ( imgfil, NC_NOWRITE, &ncid ); /* * Get the x and y dimensions of the image. */ nc_inq_dimlen ( ncid, 0, &jy ); nc_inq_dimlen ( ncid, 1, &jx ); *kx = (int) jx; *ky = (int) jy; /* * Get the date/time. It is stored as the number of seconds * since 00:00:00 UTC, January 1, 1970. */ nc_get_var1_double ( ncid, 1, 0, &dsec ); isec = (time_t) dsec; tarr = gmtime ( &isec ); iy = (*tarr).tm_year + 1900; im = (*tarr).tm_mon + 1; id = (*tarr).tm_mday; ih = (*tarr).tm_hour; in = (*tarr).tm_min; is = (*tarr).tm_sec; *idate = iy * 10000 + im * 100 + id; *itime = ih * 10000 + in * 100 + is; /* * Get the channel information. */ nc_inq_attlen ( ncid, NC_GLOBAL, "channel", &lenp ); nc_get_att_text ( ncid, NC_GLOBAL, "channel", chanl ); chanl[lenp] = CHNULL; /* * ITYPE Channel String(s) * ----------------------------- * 1 VIS * 2 3.9 micron (IR2) * 4 6.7 micron (WV) * 8 11 micron (IR) * 16 12 micron (IR5) */ im_chtb ( chanl, itype, iret, strlen(chanl) ); /* * Get the projection and central lat/lon. */ nc_get_att_int ( ncid, NC_GLOBAL, "projIndex", iproj ); nc_get_att_float ( ncid, NC_GLOBAL, "centralLat", clat ); nc_get_att_float ( ncid, NC_GLOBAL, "centralLon", clon ); /* * If the center longitude is east of -100, assume that the image * is from GOES-8. Otherwise, assume that it is from GOES-9. */ if ( *clon > -100.0 ) { *isorc = 70; } else { *isorc = 72; } /* * Get the corner lat/lons. The values are for the upper left * and lower right corners. */ nc_get_att_float ( ncid, NC_GLOBAL, "xMin", xmin ); nc_get_att_float ( ncid, NC_GLOBAL, "xMax", xmax ); nc_get_att_float ( ncid, NC_GLOBAL, "yMin", ymin ); nc_get_att_float ( ncid, NC_GLOBAL, "yMax", ymax ); /* * Close the NetCDF file. */ nc_close ( ncid ); }
//This is the write routine void write_topo(s_nc_input_t* topo, s_nc_result* mask){ /***********************************************************************************************/ int status; size_t *i, *j, *k; size_t *m, *n; //read variable double floor; size_t sizeofarea; size_t landarea; size_t waterarea; size_t source_index[2]; // n, m. size_t source_index_window[4]; //[0],[1] m; [2],[3] n. m = &source_index[S_LON]; //lon n = &source_index[S_LAT]; //lat //write variable byte_t msk; size_t target_index[3]; // k, j, i. size_t target_index_window[6]; //[0],[1] k; [2],[3] j; [4],[5] i. double target_edge_coord[4]; //[0],[1] lon; [2],[3] lat. double depth; i = &target_index[T_LON]; //lon j = &target_index[T_LAT]; //lat k = &target_index[T_DEP]; //depth //Defines the window indexes of the target. set_target_index_window(mask, target_index_window); printf("Building "); ///////////////////////////////////////////////////////////////////////// #ifdef TEST_1_DEPTH_OFF for( (*k) = target_index_window[0]; (*k) < target_index_window[1]; (*k)++){ #else (*k)=0; #endif //Gets the depth. depth = get_depth(mask, target_index[T_DEP]); for( (*j) = target_index_window[2]; (*j) < target_index_window[3]; (*j)++){ for( (*i) = target_index_window[4]; (*i) < target_index_window[5]; (*i)++){ ///////////////////////////////////////////////////////////////////////// //Defines the coordinates window. set_target_edge_coord(mask, target_index, target_edge_coord); //Defines the window indexes of the source. set_source_index_window(topo, source_index_window, target_edge_coord); //Initializes the area of land and water. landarea = 0; waterarea= 0; //Gets the size of the inner area. sizeofarea = get_area(source_index_window); //read value ... if(source_index_window[0] < source_index_window[1]){/***************************/ //___________________________________________________________________________ ///////////////////////////////////////////////////////////////////////////// for( (*n) = source_index_window[2]; (*n) < source_index_window[3]; (*n)++){ for( (*m) = source_index_window[0]; (*m) < source_index_window[1]; (*m)++){ ///////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- status = nc_get_var1_double(topo->file.ncid, topo->r.id, source_index, &floor); CDF_ERROR if( floor > depth ) landarea++; else waterarea++; //___________________________________________________________________________ //////////////////////////////////////////////////////////////////////////// } } //////////////////////////////////////////////////////////////////////////// //-------------------------------------------------------------------------- }/******************************************************************************/ else{/**************************************************************************/ //___________________________________________________________________________ ///////////////////////////////////////////////////////////////////////////// for( (*n) = source_index_window[2]; (*n) < source_index_window[3]; (*n)++){ ///////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- //___________________________________________________________________________ ///////////////////////////////////////////////////////////////////////////// for( (*m) = source_index_window[0]; (*m) < topo->x.length; (*m)++){ ///////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- status = nc_get_var1_double(topo->file.ncid, topo->r.id, source_index, &floor); CDF_ERROR if( floor > depth ) landarea++; else waterarea++; //___________________________________________________________________________ //////////////////////////////////////////////////////////////////////////// } ///////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- //___________________________________________________________________________ ///////////////////////////////////////////////////////////////////////////// for( (*m) = 0; (*m) < source_index_window[1]; (*m)++){ ///////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- status = nc_get_var1_double(topo->file.ncid, topo->r.id, source_index, &floor); CDF_ERROR if( floor > depth ) landarea++; else waterarea++; //___________________________________________________________________________ ///////////////////////////////////////////////////////////////////////////// } ///////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- //___________________________________________________________________________ ///////////////////////////////////////////////////////////////////////////// } ///////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- }/******************************************************************************/ //perform treatment to value if( landarea > waterarea ) msk = LAND; else msk = WATER; //write mask value status = nc_put_var1_short(mask->file.ncid, mask->m.id, target_index, &msk); CDF_ERROR ///////////////////////////////////////////////////////////////////////// } } printf("."); #ifdef TEST_1_DEPTH_OFF }