NcFile::NcFile( const char* path, FileMode fmode, size_t* chunksizeptr, size_t initialsize, FileFormat fformat ) { NcError err(NcError::silent_nonfatal); // constructor must not fail int mode = NC_NOWRITE; the_fill_mode = Fill; int status; // If the user wants a 64-bit offset format, set that flag. if (fformat == Offset64Bits) mode |= NC_64BIT_OFFSET; #ifdef USE_NETCDF4 else if (fformat == Netcdf4) mode |= NC_NETCDF4; else if (fformat == Netcdf4Classic) mode |= NC_NETCDF4|NC_CLASSIC_MODEL; #endif switch (fmode) { case Write: mode |= NC_WRITE; /*FALLTHRU*/ case ReadOnly: // use netcdf-3 interface to permit specifying tuning parameter status = NcError::set_err( nc__open(path, mode, chunksizeptr, &the_id) ); if(status != NC_NOERR) { NcError::set_err(status); the_id = -1; } in_define_mode = 0; break; case New: mode |= NC_NOCLOBBER; /*FALLTHRU*/ case Replace: // use netcdf-3 interface to permit specifying tuning parameters status = NcError::set_err( nc__create(path, mode, initialsize, chunksizeptr, &the_id) ); if(status != NC_NOERR) { NcError::set_err(status); the_id = -1; } in_define_mode = 1; break; default: the_id = ncBad; in_define_mode = 0; break; } if (is_valid()) { dimensions = new NcDim*[NC_MAX_DIMS]; variables = new NcVar*[NC_MAX_VARS]; int i; for (i = 0; i < num_dims(); i++) dimensions[i] = new NcDim(this, i); for (i = 0; i < num_vars(); i++) variables[i] = new NcVar(this, i); globalv = new NcVar(this, ncGlobal); } else { dimensions = 0; variables = 0; globalv = 0; } }
int mpp_open(const char *file, int action) { char curfile[128]; char errmsg[512]; int ncid, status, istat, n, fid; size_t blksz=65536; /* write only from root pe. */ if(action == MPP_WRITE && mpp_pe() != mpp_root_pe() ) return -1; /*if file is not ended with .nc add .nc at the end. */ strcpy(curfile, file); if(strstr(curfile, ".nc") == NULL) strcat(curfile,".nc"); /* look through currently files to make sure the file is not in the list*/ fid = -1; for(n=0; n<nfiles; n++) { if(!strcmp(files[n].name, file)) { fid = n; break; } } if(fid > -1) { if(files[n].action == MPP_WRITE) { sprintf( errmsg, "mpp_io(mpp_open): %s is already created for write", file); mpp_error(errmsg); } if(files[n].status) { sprintf( errmsg, "mpp_io(mpp_open): %s is already opened", file); mpp_error(errmsg); } } else { fid = nfiles; nfiles++; if(nfiles > MAXFILE) mpp_error("mpp_io(mpp_open): nfiles is larger than MAXFILE, increase MAXFILE"); strcpy(files[fid].name, file); files[fid].nvar = 0; files[fid].var = (VarType *)malloc(MAXVAR*sizeof(VarType)); } switch (action) { case MPP_WRITE: switch (in_format) { case NC_FORMAT_NETCDF4: status = nc__create(curfile, NC_NETCDF4, 0, &blksz, &ncid); break; case NC_FORMAT_NETCDF4_CLASSIC: status = nc__create(curfile, NC_NETCDF4 | NC_CLASSIC_MODEL, 0, &blksz, &ncid); break; case NC_FORMAT_64BIT: status = nc__create(curfile, NC_CLOBBER | NC_64BIT_OFFSET, 0, &blksz, &ncid); break; case NC_FORMAT_CLASSIC: status = nc__create(curfile, NC_CLOBBER | NC_CLASSIC_MODEL, 0, &blksz, &ncid); break; default: sprintf(errmsg, "mpp_io(mpp_open): Unknown netCDF format"); mpp_error(errmsg); } break; case MPP_APPEND: status = nc_open(curfile, NC_WRITE, &ncid); break; case MPP_READ: status = nc_open(curfile,NC_NOWRITE, &ncid); istat = nc_inq_format(ncid,&in_format); break; default: sprintf(errmsg, "mpp_io(mpp_open): the action should be MPP_WRITE or MPP_READ when opening file %s", file); mpp_error(errmsg); } if(status != NC_NOERR) { sprintf(errmsg, "mpp_io(mpp_open): error in opening file %s", file); netcdf_error(errmsg, status); } files[fid].ncid = ncid; files[fid].status = 1; files[fid].action = action; return fid; }
int main(int argc, char **argv) { int status = NC_NOERR; int i,j,iv; unsigned int data[DATASIZE]; size_t start[1]; size_t count[1]; Tag tag = Create; int cmode = 0; int ncid; int dimids[1]; void* memory; int nvars; int varids[4096]; size_t varsize; size_t filesize; /* Get the specified var/file size */ if(argc > 1) { filesize = atol(argv[1]); } else { if(sizeof(size_t) == 4) filesize = 1000000000; else if(sizeof(size_t) == 8) filesize = 3000000000; else { fprintf(stderr,"Cannot compute filesize\n"); exit(1); } } /* Test that we can malloc that much space */ memory = malloc(filesize); if(memory == NULL) { fprintf(stderr,"Cannot malloc %lu bytes\n",(unsigned long)filesize); exit(1); } free(memory); if(argc > 2) { if(strcmp(argv[2],"create")==0) tag = Create; else if(strcmp(argv[2],"creatediskless")==0) tag = CreateDiskless; else if(strcmp(argv[2],"open")==0) tag = Open; else if(strcmp(argv[2],"opendiskless")==0) tag = OpenDiskless; else { fprintf(stderr,"illegal tag: %s",argv[2]); exit(1); } } else tag = Create; /* default */ switch (tag) { case Create: printf("\n*** Create file\n"); break; case CreateDiskless: printf("\n*** Create file diskless\n"); break; case Open: printf("\n*** Open file\n"); break; case OpenDiskless: printf("\n*** Open file diskless\n"); break; } switch (tag) { case Create: cmode = NC_CLOBBER; break; case CreateDiskless: cmode = NC_CLOBBER|NC_DISKLESS|NC_WRITE; break; case Open: cmode = 0; break; case OpenDiskless: cmode = NC_DISKLESS; break; } switch (tag) { case Create: case CreateDiskless: /* Try to alloc as much as possible initially */ if((status=nc__create(FILE_NAME, cmode, filesize, NULL, &ncid))) REPORT; if((status=nc_set_fill(ncid, NC_NOFILL, NULL))) REPORT; /* Only need 1 dimension */ if((status=nc_def_dim(ncid, "dim", DIMMAX, &dimids[0]))) REPORT; break; case Open: case OpenDiskless: if((status=nc_open(FILE_NAME, cmode, &ncid))) REPORT; if((status=nc_inq_dimid(ncid, "dim", &dimids[0]))) REPORT; break; } varsize = DIMMAX; nvars = filesize / varsize; assert((filesize % DIMMAX) == 0); assert(nvars < 4096); for(iv=0;iv<nvars;iv++) { char varname[32]; sprintf(varname,"var%d",iv); switch (tag) { case Create: case CreateDiskless: if((status=nc_def_var(ncid, varname, NC_BYTE, 1, &dimids[0], &varids[iv]))) REPORT; break; case Open: case OpenDiskless: if((status=nc_inq_varid(ncid, varname, &varids[iv]))) REPORT; break; } } if(tag == Create || tag == CreateDiskless) { if((status=nc_enddef(ncid))) REPORT; } for(iv=0;iv<nvars;iv++) { size_t pieces = varsize/CHUNKSIZE; switch (tag) { case Create: case CreateDiskless: /* Fill and put as integers */ for(i=0;i<pieces;i++) { start[0] = i*CHUNKSIZE; count[0] = CHUNKSIZE; for(j=0;j<DATASIZE;j++) data[j] = iv*((i*CHUNKSIZE)+j); if((status=nc_put_vara(ncid,varids[iv],start,count,(void*)data))) REPORT; } break; case Open: case OpenDiskless: /* Read the var contents and validate */ for(i=0;i<pieces;i++) { start[0] = i*CHUNKSIZE; count[0] = CHUNKSIZE; if((status=nc_get_vara(ncid,varids[iv],start,count,(void*)data))) REPORT; for(j=0;j<DATASIZE;j++) { unsigned int expected = iv*((i*CHUNKSIZE)+j); if(data[j] != expected) { printf("mismatch: iv=%d i=%u j=%u data=%u; should be %u\n", iv, i,j,data[j],expected); err++; } } } break; } } if((status=nc_close(ncid))) REPORT; SUMMARIZE_ERR; exit(0); return 0; }
int create_file(char *file_name, int fill_mode, size_t* sizehintp) { int i; int stat; /* return status */ int ncid; /* netCDF id */ /* dimension ids */ int lon_dim; int lat_dim; int lvl_dim; int time_dim; /* dimension lengths */ size_t lon_len = LON_LEN; size_t lat_len = LAT_LEN; size_t lvl_len = LVL_LEN; size_t time_len = TIME_LEN; /* variable ids */ int time_id; int lat_id; int lon_id; int lvl_id; int sfc_pres_id; int temp_scrn_id; int qsair_scrn_id; int topog_id; int mslp_id; int sfc_temp_id; int zonal_wnd_id; /* rank (number of dimensions) for each variable */ # define RANK_time 1 # define RANK_lat 1 # define RANK_lon 1 # define RANK_lvl 1 # define RANK_sfc_pres 3 # define RANK_temp_scrn 3 # define RANK_qsair_scrn 3 # define RANK_topog 3 # define RANK_mslp 3 # define RANK_sfc_temp 3 # define RANK_zonal_wnd 4 /* variable shapes */ int time_dims[RANK_time]; int lat_dims[RANK_lat]; int lon_dims[RANK_lon]; int lvl_dims[RANK_lvl]; int sfc_pres_dims[RANK_sfc_pres]; int temp_scrn_dims[RANK_temp_scrn]; int qsair_scrn_dims[RANK_qsair_scrn]; int topog_dims[RANK_topog]; int mslp_dims[RANK_mslp]; int sfc_temp_dims[RANK_sfc_temp]; int zonal_wnd_dims[RANK_zonal_wnd]; size_t zonal_wnd_start[RANK_zonal_wnd]; size_t zonal_wnd_count[RANK_zonal_wnd]; float zonal_wnd[LON_LEN*LAT_LEN*TIME_LEN]; int ii; int old_fill_mode; size_t default_initialsize = 0; /* To test bug on filesystem without large block size, we can get * the same effect by providing the desired value as sizehint to * nc__create() instead of calling nc_create() and getting the * block size reported by fstat */ stat = nc__create(file_name, NC_CLOBBER, default_initialsize, sizehintp, &ncid); check_err(stat,__LINE__,__FILE__); stat = nc_set_fill(ncid, fill_mode, &old_fill_mode); check_err(stat,__LINE__,__FILE__); /* define dimensions */ stat = nc_def_dim(ncid, "lon", lon_len, &lon_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "lat", lat_len, &lat_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "lvl", lvl_len, &lvl_dim); check_err(stat,__LINE__,__FILE__); stat = nc_def_dim(ncid, "time", time_len, &time_dim); check_err(stat,__LINE__,__FILE__); /* define variables */ time_dims[0] = time_dim; stat = nc_def_var(ncid, "time", NC_DOUBLE, RANK_time, time_dims, &time_id); check_err(stat,__LINE__,__FILE__); lat_dims[0] = lat_dim; stat = nc_def_var(ncid, "lat", NC_FLOAT, RANK_lat, lat_dims, &lat_id); check_err(stat,__LINE__,__FILE__); lon_dims[0] = lon_dim; stat = nc_def_var(ncid, "lon", NC_FLOAT, RANK_lon, lon_dims, &lon_id); check_err(stat,__LINE__,__FILE__); lvl_dims[0] = lvl_dim; stat = nc_def_var(ncid, "lvl", NC_FLOAT, RANK_lvl, lvl_dims, &lvl_id); check_err(stat,__LINE__,__FILE__); sfc_pres_dims[0] = time_dim; sfc_pres_dims[1] = lat_dim; sfc_pres_dims[2] = lon_dim; stat = nc_def_var(ncid, "sfc_pres", NC_FLOAT, RANK_sfc_pres, sfc_pres_dims, &sfc_pres_id); check_err(stat,__LINE__,__FILE__); temp_scrn_dims[0] = time_dim; temp_scrn_dims[1] = lat_dim; temp_scrn_dims[2] = lon_dim; stat = nc_def_var(ncid, "temp_scrn", NC_FLOAT, RANK_temp_scrn, temp_scrn_dims, &temp_scrn_id); check_err(stat,__LINE__,__FILE__); qsair_scrn_dims[0] = time_dim; qsair_scrn_dims[1] = lat_dim; qsair_scrn_dims[2] = lon_dim; stat = nc_def_var(ncid, "qsair_scrn", NC_FLOAT, RANK_qsair_scrn, qsair_scrn_dims, &qsair_scrn_id); check_err(stat,__LINE__,__FILE__); topog_dims[0] = time_dim; topog_dims[1] = lat_dim; topog_dims[2] = lon_dim; stat = nc_def_var(ncid, "topog", NC_FLOAT, RANK_topog, topog_dims, &topog_id); check_err(stat,__LINE__,__FILE__); mslp_dims[0] = time_dim; mslp_dims[1] = lat_dim; mslp_dims[2] = lon_dim; stat = nc_def_var(ncid, "mslp", NC_FLOAT, RANK_mslp, mslp_dims, &mslp_id); check_err(stat,__LINE__,__FILE__); sfc_temp_dims[0] = time_dim; sfc_temp_dims[1] = lat_dim; sfc_temp_dims[2] = lon_dim; stat = nc_def_var(ncid, "sfc_temp", NC_FLOAT, RANK_sfc_temp, sfc_temp_dims, &sfc_temp_id); check_err(stat,__LINE__,__FILE__); zonal_wnd_dims[0] = time_dim; zonal_wnd_dims[1] = lvl_dim; zonal_wnd_dims[2] = lat_dim; zonal_wnd_dims[3] = lon_dim; stat = nc_def_var(ncid, "zonal_wnd", NC_FLOAT, RANK_zonal_wnd, zonal_wnd_dims, &zonal_wnd_id); check_err(stat,__LINE__,__FILE__); /* leave define mode */ stat = nc_enddef (ncid); check_err(stat,__LINE__,__FILE__); { /* store time */ size_t time_start[RANK_time]; size_t time_count[RANK_time]; double time[TIME_LEN] = {1.}; time_len = 1; time_start[0] = 0; time_count[0] = time_len; stat = nc_put_vara_double(ncid, time_id, time_start, time_count, time); check_err(stat,__LINE__,__FILE__); } { /* store lat */ float lat[] = {90, 88.5, 87, 85.5, 84, 82.5, 81, 79.5, 78, 76.5, 75, 73.5, 72, 70.5, 69, 67.5, 66, 64.5, 63, 61.5, 60, 58.5, 57, 55.5, 54, 52.5, 51, 49.5, 48, 46.5, 45, 43.5, 42, 40.5, 39, 37.5, 36, 34.5, 33, 31.5, 30, 28.5, 27, 25.5, 24, 22.5, 21, 19.5, 18, 16.5, 15, 13.5, 12, 10.5, 9, 7.5, 6, 4.5, 3, 1.5, 0, -1.5, -3, -4.5, -6, -7.5, -9, -10.5, -12, -13.5, -15, -16.5, -18, -19.5, -21, -22.5, -24, -25.5, -27, -28.5, -30, -31.5, -33, -34.5, -36, -37.5, -39, -40.5, -42, -43.5, -45, -46.5, -48, -49.5, -51, -52.5, -54, -55.5, -57, -58.5, -60, -61.5, -63, -64.5, -66, -67.5, -69, -70.5, -72, -73.5, -75, -76.5, -78, -79.5, -81, -82.5, -84, -85.5, -87, -88.5, -90}; stat = nc_put_var_float(ncid, lat_id, lat); check_err(stat,__LINE__,__FILE__); } { /* store lon */ float lon[] = {0, 1.5, 3, 4.5, 6, 7.5, 9, 10.5, 12, 13.5, 15, 16.5, 18, 19.5, 21, 22.5, 24, 25.5, 27, 28.5, 30, 31.5, 33, 34.5, 36, 37.5, 39, 40.5, 42, 43.5, 45, 46.5, 48, 49.5, 51, 52.5, 54, 55.5, 57, 58.5, 60, 61.5, 63, 64.5, 66, 67.5, 69, 70.5, 72, 73.5, 75, 76.5, 78, 79.5, 81, 82.5, 84, 85.5, 87, 88.5, 90, 91.5, 93, 94.5, 96, 97.5, 99, 100.5, 102, 103.5, 105, 106.5, 108, 109.5, 111, 112.5, 114, 115.5, 117, 118.5, 120, 121.5, 123, 124.5, 126, 127.5, 129, 130.5, 132, 133.5, 135, 136.5, 138, 139.5, 141, 142.5, 144, 145.5, 147, 148.5, 150, 151.5, 153, 154.5, 156, 157.5, 159, 160.5, 162, 163.5, 165, 166.5, 168, 169.5, 171, 172.5, 174, 175.5, 177, 178.5, 180, 181.5, 183, 184.5, 186, 187.5, 189, 190.5, 192, 193.5, 195, 196.5, 198, 199.5, 201, 202.5, 204, 205.5, 207, 208.5, 210, 211.5, 213, 214.5, 216, 217.5, 219, 220.5, 222, 223.5, 225, 226.5, 228, 229.5, 231, 232.5, 234, 235.5, 237, 238.5, 240, 241.5, 243, 244.5, 246, 247.5, 249, 250.5, 252, 253.5, 255, 256.5, 258, 259.5, 261, 262.5, 264, 265.5, 267, 268.5, 270, 271.5, 273, 274.5, 276, 277.5, 279, 280.5, 282, 283.5, 285, 286.5, 288, 289.5, 291, 292.5, 294, 295.5, 297, 298.5, 300, 301.5, 303, 304.5, 306, 307.5, 309, 310.5, 312, 313.5, 315, 316.5, 318, 319.5, 321, 322.5, 324, 325.5, 327, 328.5, 330, 331.5, 333, 334.5, 336, 337.5, 339, 340.5, 342, 343.5, 345, 346.5, 348, 349.5, 351, 352.5, 354, 355.5, 357, 358.5}; stat = nc_put_var_float(ncid, lon_id, lon); check_err(stat,__LINE__,__FILE__); } { /* store lvl */ float lvl[] = {1000, 995, 990, 985, 975, 950, 925, 900, 875, 850, 800, 750, 700, 600, 500, 450, 400, 350, 300, 275, 250, 225, 200, 175, 150, 100, 70, 50, 30, 20, 10}; stat = nc_put_var_float(ncid, lvl_id, lvl); check_err(stat,__LINE__,__FILE__); } { /* store sfc_pres */ size_t sfc_pres_start[RANK_sfc_pres]; size_t sfc_pres_count[RANK_sfc_pres]; float sfc_pres[LON_LEN*LAT_LEN]; for(ii = 0; ii < LAT_LEN * LON_LEN; ii++) { sfc_pres[ii] = 6; } sfc_pres_start[0] = 0; sfc_pres_start[1] = 0; sfc_pres_start[2] = 0; sfc_pres_count[0] = time_len; sfc_pres_count[1] = lat_len; sfc_pres_count[2] = lon_len; stat = nc_put_vara_float(ncid, sfc_pres_id, sfc_pres_start, sfc_pres_count, sfc_pres); check_err(stat,__LINE__,__FILE__); } { /* store temp_scrn */ size_t temp_scrn_start[RANK_temp_scrn]; size_t temp_scrn_count[RANK_temp_scrn]; float temp_scrn[LON_LEN*LAT_LEN]; for(ii = 0; ii < LAT_LEN * LON_LEN; ii++) { temp_scrn[ii] = 11; } temp_scrn_start[0] = 0; temp_scrn_start[1] = 0; temp_scrn_start[2] = 0; temp_scrn_count[0] = time_len; temp_scrn_count[1] = lat_len; temp_scrn_count[2] = lon_len; stat = nc_put_vara_float(ncid, temp_scrn_id, temp_scrn_start, temp_scrn_count, temp_scrn); check_err(stat,__LINE__,__FILE__); } { /* store qsair_scrn */ size_t qsair_scrn_start[RANK_qsair_scrn]; size_t qsair_scrn_count[RANK_qsair_scrn]; float qsair_scrn[LON_LEN*LAT_LEN]; for(ii = 0; ii < LAT_LEN * LON_LEN; ii++) { qsair_scrn[ii] = 22; } qsair_scrn_start[0] = 0; qsair_scrn_start[1] = 0; qsair_scrn_start[2] = 0; qsair_scrn_count[0] = time_len; qsair_scrn_count[1] = lat_len; qsair_scrn_count[2] = lon_len; stat = nc_put_vara_float(ncid, qsair_scrn_id, qsair_scrn_start, qsair_scrn_count, qsair_scrn); check_err(stat,__LINE__,__FILE__); } { /* store topog */ size_t topog_start[RANK_topog]; size_t topog_count[RANK_topog]; float topog[LON_LEN*LAT_LEN]; for(ii = 0; ii < LAT_LEN * LON_LEN; ii++) { topog[ii] = 33; } topog_start[0] = 0; topog_start[1] = 0; topog_start[2] = 0; topog_count[0] = time_len; topog_count[1] = lat_len; topog_count[2] = lon_len; stat = nc_put_vara_float(ncid, topog_id, topog_start, topog_count, topog); check_err(stat,__LINE__,__FILE__); } { /* store mslp */ size_t mslp_start[RANK_mslp]; size_t mslp_count[RANK_mslp]; float mslp[LON_LEN*LAT_LEN]; for(ii = 0; ii < LAT_LEN * LON_LEN; ii++) { mslp[ii] = 44; } mslp_start[0] = 0; mslp_start[1] = 0; mslp_start[2] = 0; mslp_count[0] = time_len; mslp_count[1] = lat_len; mslp_count[2] = lon_len; stat = nc_put_vara_float(ncid, mslp_id, mslp_start, mslp_count, mslp); check_err(stat,__LINE__,__FILE__); } { /* store sfc_temp */ size_t sfc_temp_start[RANK_sfc_temp]; size_t sfc_temp_count[RANK_sfc_temp]; float sfc_temp[LON_LEN*LAT_LEN]; for(ii = 0; ii < LAT_LEN * LON_LEN; ii++) { sfc_temp[ii] = 55; } sfc_temp_start[0] = 0; sfc_temp_start[1] = 0; sfc_temp_start[2] = 0; sfc_temp_count[0] = time_len; sfc_temp_count[1] = lat_len; sfc_temp_count[2] = lon_len; stat = nc_put_vara_float(ncid, sfc_temp_id, sfc_temp_start, sfc_temp_count, sfc_temp); check_err(stat,__LINE__,__FILE__); } { /* store zonal_wnd */ /* Bug exposed when written in reverse order. */ for(i = LVL_LEN - 1; i>=0; i--) /* for(i = 0; i < LVL_LEN; i++) */ { int izw; for(izw = 0; izw < TIME_LEN * LAT_LEN * LON_LEN; izw++) { zonal_wnd[izw] = 100 + i; } zonal_wnd_start[0] = 0; zonal_wnd_start[1] = i; zonal_wnd_start[2] = 0; zonal_wnd_start[3] = 0; zonal_wnd_count[0] = time_len; zonal_wnd_count[1] = 1; zonal_wnd_count[2] = lat_len; zonal_wnd_count[3] = lon_len; stat = nc_put_vara_float(ncid, zonal_wnd_id, zonal_wnd_start, zonal_wnd_count, zonal_wnd); check_err(stat,__LINE__,__FILE__); } } stat = nc_close(ncid); check_err(stat,__LINE__,__FILE__); 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; #ifdef USE_PNETCDF MPI_Init(&argc, &argv); cmode |= (NC_PNETCDF); cmode |= (NC_64BIT_DATA); ret = nc_create_par(fname,cmode, MPI_COMM_WORLD, MPI_INFO_NULL, &id); #else ret = nc__create(fname,cmode, initialsz, &chunksz, &id); if(ret != NC_NOERR) { /* (void) fprintf(stderr, "trying again\n"); */ ret = nc__create(fname,cmode, initialsz, &chunksz, &id); } #endif 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; #ifdef USE_PNETCDF omode |= NC_PNETCDF; ret = nc_open_par(fname,omode, MPI_COMM_WORLD, MPI_INFO_NULL, &id); #else ret = nc__open(fname,omode, &chunksz, &id); #endif 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); */ #ifdef USE_PNETCDF MPI_Finalize(); #endif return 0; }
int nc_create(const char * path, int ioflags, int *ncid_ptr) { return nc__create(path, ioflags, 0, NULL, ncid_ptr); }