Example #1
0
int
main(int argc, char **argv)
{
   printf("\n*** Testing nofill mode.\n");
   printf("*** Create file in nofill mode using nc_create()...");
   {
       int ncid, varid;
      int data[TIME_LEN];

      if (create_file(FILE_NAME, NC_NOFILL)) ERR;
      if (nc_open(FILE_NAME, 0, &ncid)) ERR;
      if (nc_inq_varid(ncid, "time", &varid)) ERR;
      if (nc_get_var(ncid, varid, data)) ERR;
      if (data[0] != 1) ERR; /* value overwritten by zero due to NOFILL bug */
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Example #2
0
int
main(int argc, char **argv)
{
   int ncid;
   size_t size_in;
   nc_type xtype;
   unsigned char data[DIM_LEN][BASE_SIZE], data_in[DIM_LEN][BASE_SIZE];
   int i, j;

   printf("\n*** Testing netcdf-4 opaque type.\n");

   for (i=0; i<DIM_LEN; i++)
      for (j=0; j<BASE_SIZE; j++)
	 data[i][j] = 0;

   printf("*** testing scalar opaque variable...");
   {
      int varid;
      char name_in[NC_MAX_NAME+1];
      size_t nfields_in, base_size_in;
      nc_type base_nc_type_in, var_type;
      int class_in;
      char var_name[NC_MAX_NAME+1];
      int  nvars, natts, ndims, unlimdimid;

      /* Create a file that has an opaque variable. */
      if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
      if (nc_def_opaque(ncid, BASE_SIZE, TYPE_NAME, &xtype)) ERR;
      if (nc_inq_user_type(ncid, xtype, name_in, &base_size_in, &base_nc_type_in, &nfields_in, &class_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE || 
	  base_nc_type_in != 0 || nfields_in != 0 || class_in != NC_OPAQUE) ERR;
      if (nc_inq_opaque(ncid, xtype, name_in, &base_size_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE) ERR;
      if (nc_def_var(ncid, VAR_NAME, xtype, 0, NULL, &varid)) ERR; 
      if (nc_put_var(ncid, varid, &data[0])) ERR; 
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, NULL, &natts)) ERR;
      if (ndims != 0 || strcmp(var_name, VAR_NAME) || natts != 0) ERR;
      if (nc_get_var(ncid, 0, &data_in[0])) ERR;
      for (j = 0; j < BASE_SIZE; j++)
	 if (data_in[0][j] != data[0][j]) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** testing opaque variable...");
   {
      int dimid, varid, dimids[] = {0};
      char name_in[NC_MAX_NAME+1];
      nc_type base_nc_type_in, var_type;
      size_t nfields_in, base_size_in;
      int class_in;
      char var_name[NC_MAX_NAME+1];
      int  nvars, natts, ndims, unlimdimid, dimids_var[1];

      /* Create a file that has an opaque variable. */
      if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
      if (nc_def_opaque(ncid, BASE_SIZE, TYPE_NAME, &xtype)) ERR;
      if (nc_inq_user_type(ncid, xtype, name_in, &base_size_in, &base_nc_type_in, &nfields_in, &class_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE ||
	  base_nc_type_in != 0 || nfields_in != 0 || class_in != NC_OPAQUE) ERR;
      if (nc_inq_opaque(ncid, xtype, name_in, &base_size_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE) ERR;
      if (nc_def_dim(ncid, DIM_NAME, DIM_LEN, &dimid)) ERR;
      if (nc_def_var(ncid, VAR_NAME, xtype, 1, dimids, &varid)) ERR;
      if (nc_put_var(ncid, varid, data)) ERR;
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
      if (ndims != 1 || strcmp(var_name, VAR_NAME) ||
	  dimids_var[0] != dimids[0] || natts != 0) ERR;
      if (nc_get_var(ncid, 0, data_in)) ERR;
      for (i=0; i<DIM_LEN; i++)
 	 for (j=0; j<BASE_SIZE; j++)
 	    if (data_in[i][j] != data[i][j]) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** testing *really* simple opaque attribute...");
   {

      /* Create a file that has an opaque attribute. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_opaque(ncid, BASE_SIZE, TYPE_NAME, &xtype)) ERR;

      /* Write an att. */
      if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, xtype, DIM_LEN, data)) ERR;
      if (nc_close(ncid)) ERR;

      /* Reopen. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** testing opaque attribute...");
   {
      char name_in[NC_MAX_NAME+1];
      nc_type base_nc_type_in;
      size_t base_size_in;
      size_t nfields_in;
      int class_in;

      /* Create a file that has an opaque attribute. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      if (nc_def_opaque(ncid, BASE_SIZE, TYPE_NAME, &xtype)) ERR;

      /* Check it out. */
      if (nc_inq_user_type(ncid, xtype, name_in, &base_size_in, &base_nc_type_in,
			   &nfields_in, &class_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE ||
	  base_nc_type_in != 0 || nfields_in != 0 || class_in != NC_OPAQUE) ERR;
      if (nc_inq_opaque(ncid, xtype, name_in, &base_size_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE) ERR;

      /* Write an att. */
      if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, xtype, DIM_LEN, data)) ERR;

      if (nc_close(ncid)) ERR;

      /* Reopen. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      /* Check it out. */
      if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &xtype, &size_in)) ERR;
      if (size_in != DIM_LEN) ERR;
      if (nc_inq_user_type(ncid, xtype, name_in, &base_size_in, &base_nc_type_in, &nfields_in, &class_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE ||
	  base_nc_type_in != 0 || nfields_in != 0 || class_in != NC_OPAQUE) ERR;
      if (nc_inq_opaque(ncid, xtype, name_in, &base_size_in)) ERR;
      if (strcmp(name_in, TYPE_NAME) || base_size_in != BASE_SIZE) ERR;
      if (nc_get_att(ncid, NC_GLOBAL, ATT_NAME, data_in)) ERR;
      for (i=0; i<DIM_LEN; i++)
 	 for (j=0; j<BASE_SIZE; j++)
 	    if (data_in[i][j] != data[i][j]) ERR;

      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** testing 3 opaque types...");
   {
#define TYPE_SIZE1 20
#define NUM_TYPES 3

      char name_in[NC_MAX_NAME+1];
      nc_type base_nc_type_in;
      size_t nfields_in, base_size_in;
      nc_type otid[3];
      int class_in;
      char type_name[NUM_TYPES][NC_MAX_NAME + 1] = {"o1", "o2", "o3"};
      int  nvars, natts, ndims, unlimdimid;
      int ntypes, typeids[NUM_TYPES];
      int i;

      /* Create a file that has three opaque types. */
      if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
      for (i = 0; i < NUM_TYPES; i++)
      {
	 if (nc_def_opaque(ncid, TYPE_SIZE1, type_name[i], &otid[i])) ERR;
	 if (nc_inq_user_type(ncid, otid[i], name_in, &base_size_in, &base_nc_type_in, &nfields_in, &class_in)) ERR;
	 if (strcmp(name_in, type_name[i]) || base_size_in != TYPE_SIZE1 ||
	     base_nc_type_in != 0 || nfields_in != 0 || class_in != NC_OPAQUE) ERR;
	 if (nc_inq_opaque(ncid, otid[i], name_in, &base_size_in)) ERR;
	 if (strcmp(name_in, type_name[i]) || base_size_in != TYPE_SIZE1) ERR;
      }
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != 0 || nvars != 0 || natts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_typeids(ncid, &ntypes, typeids)) ERR;
      if (ntypes != NUM_TYPES) ERR;
      for (i = 0; i < NUM_TYPES; i++)
      {
	 if (nc_inq_user_type(ncid, otid[i], name_in, &base_size_in, &base_nc_type_in, &nfields_in, &class_in)) ERR;
	 if (strcmp(name_in, type_name[i]) || base_size_in != TYPE_SIZE1 ||
	     base_nc_type_in != 0 || nfields_in != 0 || class_in != NC_OPAQUE) ERR;
	 if (nc_inq_opaque(ncid, otid[i], name_in, &base_size_in)) ERR;
	 if (strcmp(name_in, type_name[i]) || base_size_in != TYPE_SIZE1) ERR;
      }
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Example #3
0
// TODO: Support to only set parcels within limited region.
void ParcelManager::
init(const Mesh &mesh) {
    Field<int> numParcelPerCell;
    numParcelPerCell.create("rp", "1", "parcel refinement", mesh, CENTER, mesh.domain().numDim());
    if (ConfigManager::hasKey("lasm", "parcel_refine_file_path")) {
        auto filePath = ConfigManager::getValue<string>("lasm", "parcel_refine_file_path");
        int ncId, ret, varId;
        int *buffer = new int[mesh.totalNumGrid(CENTER)];
        ret = nc_open(filePath.c_str(), NC_NOWRITE, &ncId);
        CHECK_NC_OPEN(ret, filePath);
        ret = nc_inq_varid(ncId, "rp", &varId);
        CHECK_NC_INQ_VARID(ret, filePath, "rp");
        ret = nc_get_var(ncId, varId, buffer);
        CHECK_NC_GET_VAR(ret, filePath, "rp");
        ret = nc_close(ncId);
        CHECK_NC_CLOSE(ret, filePath);
        for (uword i = 0; i < mesh.totalNumGrid(CENTER); ++i) {
            numParcelPerCell(i) = buffer[i];
        }
        delete [] buffer;
    } else {
        for (uword i = 0; i < mesh.totalNumGrid(CENTER); ++i) {
            numParcelPerCell(i) = 1;
        }
    }
    uvec n(mesh.domain().numDim());
    SpaceCoord x(mesh.domain().numDim());
    vec dx(mesh.domain().numDim());
    this->mesh = &mesh;
    TimeLevelIndex<2> timeIdx;
    int id = 0;
#ifdef LASM_USE_RLL_MESH
    vec areas(mesh.numGrid(1, FULL));
    vec areaWeights(mesh.numGrid(1, FULL));
    for (uword j = mesh.js(FULL); j <= mesh.je(FULL); ++j) {
        int cellIdx = mesh.wrapIndex(CENTER, mesh.is(FULL), j, mesh.ks(FULL));
        areas[j] = mesh.cellVolume(cellIdx);
        areaWeights[j] = exp(-0.1*fabs(mesh.sinLat(FULL, j)));
    }
    double maxArea = areas.max();
    vec size(mesh.domain().numDim());
    for (uword k = mesh.ks(FULL); k <= mesh.ke(FULL); ++k) {
        if (mesh.domain().numDim() == 3) {
            size[2] = mesh.gridInterval(2, HALF, k);
        }
        for (uword j = mesh.js(FULL)+1; j < mesh.je(FULL); ++j) {
            size[1] = mesh.gridInterval(1, HALF, j)*mesh.domain().radius();
            int numReducedLon = mesh.numGrid(0, FULL)/
                std::max(1, static_cast<int>(maxArea*areaWeights[j]/areas[j]));
            double dlon = PI2/numReducedLon;
            for (uword i = 0; i < numReducedLon; ++i) {
                double lon = i*dlon;
                if (mesh.domain().numDim() == 2) {
                    x.set(lon, mesh.lat(FULL, j));
                } else if (mesh.domain().numDim() == 3) {
                    x.set(lon, mesh.lat(FULL, j), mesh.lev(FULL, k));
                }
                x.transformToCart(mesh.domain());
                size[0] = dlon*mesh.domain().radius()*mesh.cosLat(FULL, j);
                Parcel *parcel = new Parcel;
                parcel->init(id++, 0);
                parcel->x(timeIdx) = x;
                parcel->meshIndex(timeIdx).locate(mesh, x);
                parcel->skeletonPoints().init(mesh, size);
                parcel->updateDeformMatrix(timeIdx);
                parcel->tracers().init();
                _parcels.push_back(parcel);
            }
        }
    }
#else
    for (uword i = 0; i < mesh.totalNumGrid(CENTER); ++i) {
        n.fill(pow(numParcelPerCell(i), 1.0/mesh.domain().numDim()));
        const SpaceCoord &x0 = mesh.gridCoord(CENTER, i);
        dx = mesh.cellSize(CENTER, i)/n;
        for (uword j = 0; j < numParcelPerCell(i); ++j) {
            subdivide(n, dx, j, x0, x);
            Parcel *parcel = new Parcel;
            parcel->init(id++, 0);
            parcel->x(timeIdx) = x;
            parcel->meshIndex(timeIdx).locate(mesh, x);
            parcel->skeletonPoints().init(mesh, dx);
            parcel->volume(timeIdx) = mesh.cellVolume(i)/numParcelPerCell(i);
            parcel->updateDeformMatrix(timeIdx);
            parcel->resetSkeletonPoints(timeIdx, mesh);
            parcel->tracers().init();
            _parcels.push_back(parcel);
        }
    }
#endif
} // init
Example #4
0
int
main(int argc, char **argv)
{
   int ncid;
   int dimid, varid;
   char name_in[NC_MAX_NAME+1];
   int class_in;
   size_t size_in;
   char *value_in;
   nc_type att_type;
   size_t att_len;

   int i;

   int var_dims[VAR4_RANK];
   const char *desc_data[DIM4_LEN] = {
       "first string", "second string", "third string", "", "last string"
   };
   const char *missing_val[ATT4_LEN] = {""};
   char *strings_in[DIM4_LEN];
   
#ifdef USE_PARALLEL
   MPI_Init(&argc, &argv);
#endif

#ifdef EXTRA_TESTS
   printf("*** creating strings test file %s...", FILE4_NAME);
   if (nc_create(FILE4_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR;
   
   /* Declare a line dimension */
   if (nc_def_dim(ncid, DIM4_NAME, DIM4_LEN, &dimid)) ERR;
   
   /* Declare a string variable */
   var_dims[0] = dimid;
   if (nc_def_var(ncid, VAR4_NAME, NC_STRING, VAR4_RANK, var_dims, &varid)) ERR;
   
   /* Create and write a variable attribute of string type */
   if (nc_put_att_string(ncid, varid, ATT4_NAME, ATT4_LEN, missing_val)) ERR;
   if (nc_enddef(ncid)) ERR;
   
   /* Store some data of string type */
   if(nc_put_var(ncid, varid, desc_data)) ERR;
   
   /* Write the file. */
   if (nc_close(ncid)) ERR;
   
   /* Check it out. */
   if (nc_open(FILE4_NAME, NC_NOWRITE, &ncid)) ERR;
   if (nc_inq_varid(ncid, VAR4_NAME, &varid)) ERR;
   if (nc_inq_att(ncid, varid, ATT4_NAME, &att_type, &att_len)) ERR;
   if (att_type != NC_STRING || att_len != ATT4_LEN) ERR;
   if (nc_get_att_string(ncid, varid, ATT4_NAME, strings_in)) ERR;
   
   if (strcmp(strings_in[0], *missing_val) != 0) ERR;
   /* string atts should be explicitly freed when done with them */
   nc_free_string(ATT4_LEN, strings_in);
   
   if(nc_get_var_string(ncid, varid, strings_in)) ERR;
   for (i = 0; i < DIM4_LEN; i++) {
       if (strcmp(strings_in[i], desc_data[i]) != 0) ERR;
   }
   nc_free_string(DIM4_LEN, strings_in);

   /* Try reading strings in with typeless generic interface also */
   if(nc_get_var(ncid, varid, strings_in)) ERR;

   for (i = 0; i < DIM4_LEN; i++) {
       if (strcmp(strings_in[i], desc_data[i]) != 0) ERR;
   }
   nc_free_string(DIM4_LEN, strings_in);


   /* Try reading strings in with typeless generic array interface also */
   {
       size_t cor[VAR4_RANK], edg[VAR4_RANK];
       cor[0] = 0;
       edg[0] = DIM4_LEN;
       if(nc_get_vara(ncid, varid, cor, edg, strings_in)) ERR;

       for (i = 0; i < DIM4_LEN; i++) {
	   if (strcmp(strings_in[i], desc_data[i]) != 0) ERR;
       }
       nc_free_string(DIM4_LEN, strings_in);
   }

   if (nc_close(ncid)) ERR; 

   SUMMARIZE_ERR;
#endif /* EXTRA_TESTS */
   FINAL_RESULTS;
#ifdef USE_PARALLEL
   MPI_Finalize();
#endif   
}
Example #5
0
int
main(int argc, char **argv)
{
   printf("\n*** Testing netcdf-4 dimensions.\n");
   printf("*** Testing that netcdf-4 dimids inq works on netcdf-3 file...");
   {
      int ncid, dimid;
      int ndims_in, dimids_in[MAX_DIMS];

      /* Create a netcdf-3 file with one dim. */
      if (nc_create(FILE_NAME, 0, &ncid)) ERR;
      if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &dimid)) ERR;
      if (nc_close(ncid)) ERR;

      /* Open the file and make sure nc_inq_dimids yeilds correct
       * result. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_unlimdims(ncid, &ndims_in, dimids_in)) ERR;
      if (ndims_in != 0) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing that netcdf-4 dimids inq works on more complex netcdf-3 file...");
   {
      int ncid, dimid;
      int lon_dimid;
      int ndims_in, dimids_in[MAX_DIMS];

      /* Create a netcdf-3 file with three dim. */
      if (nc_create(FILE_NAME, 0, &ncid)) ERR;
      if (nc_def_dim(ncid, LEVEL_NAME, NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &dimid)) ERR;
      if (nc_def_dim(ncid, LON_NAME, LON_LEN, &lon_dimid)) ERR;
      if (nc_close(ncid)) ERR;

      /* Open the file and make sure nc_inq_dimids yeilds correct
       * result. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 3 || dimids_in[0] != 0 || dimids_in[1] != 1 ||
	 dimids_in[2] != 2) ERR;
      if (nc_inq_unlimdims(ncid, &ndims_in, dimids_in)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** Testing file with just one dimension...");
   {
      int ncid, dimid;
      int ndims_in, dimids_in[MAX_DIMS];
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int dimid_in;

      /* Create a file with one dim and nothing else. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &dimid)) ERR;

      /* Check out what we've got. */
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1) ERR;
      if (nc_inq_dimid(ncid, LAT_NAME, &dimid_in)) ERR;
      if (dimid_in != 0) ERR;
      if (nc_inq_dimname(ncid, 0, name_in)) ERR;
      if (strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dimlen(ncid, 0, &len_in)) ERR;
      if (len_in != LAT_LEN) ERR;
      if (nc_inq_unlimdims(ncid, &ndims_in, dimids_in)) ERR;
      if (ndims_in != 0) ERR;
      if (nc_close(ncid)) ERR;

      /* Reopen and check it out again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1) ERR;
      if (nc_inq_dimid(ncid, LAT_NAME, &dimid_in)) ERR;
      if (dimid_in != 0) ERR;
      if (nc_inq_dimname(ncid, 0, name_in)) ERR;
      if (strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dimlen(ncid, 0, &len_in)) ERR;
      if (len_in != LAT_LEN) ERR;
      if (nc_inq_unlimdims(ncid, &ndims_in, dimids_in)) ERR;
      if (ndims_in != 0) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** Testing renaming of one dimension...");
   {
      int ncid, dimid, dimid_in;
      char name_in[NC_MAX_NAME + 1];
      size_t len_in;
      int ndims_in, dimids_in[MAX_DIMS];

      /* Reopen the file with one dim, and change the name of the dim. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
      if (nc_rename_dim(ncid, 0, BUBBA)) ERR;

      /* Check out what we've got. */
      dimid = 0;
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != LAT_LEN || strcmp(name_in, BUBBA)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1) ERR;
      if (dimids_in[0] != 0) ERR;
      if (nc_inq_dimid(ncid, BUBBA, &dimid_in)) ERR;
      if (dimid_in != 0) ERR;
      if (nc_inq_dimname(ncid, 0, name_in)) ERR;
      if (strcmp(name_in, BUBBA)) ERR;
      if (nc_inq_dimlen(ncid, 0, &len_in)) ERR;
      if (len_in != LAT_LEN) ERR;
      if (nc_close(ncid)) ERR;

      /* Reopen and check out what we've got again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != LAT_LEN || strcmp(name_in, BUBBA)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_dimid(ncid, BUBBA, &dimid_in)) ERR;
      if (dimid_in != 0) ERR;
      if (nc_inq_dimname(ncid, 0, name_in)) ERR;
      if (strcmp(name_in, BUBBA)) ERR;
      if (nc_inq_dimlen(ncid, 0, &len_in)) ERR;
      if (len_in != LAT_LEN) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing renaming dimensions and vars...");
   {
#define FILE_NAME1 "foo1.nc"
#define FILE_NAME2 "foo2.nc"
#define FILE_NAME3 "foo3.nc"
#define FILE_NAME4 "foo4.nc"
#define DIM_NAME "lat_T42"
#define VAR_NAME DIM_NAME
#define DIM_NAME2 "lat"
#define VAR_NAME2 DIM_NAME2
#define RANK_lat_T42 1
      int  ncid, varid, dimid;
      int lat_T42_dim;
      size_t lat_T42_len = 3;
      int lat_T42_id;
      int lat_T42_dims[RANK_lat_T42];
      char name[NC_MAX_NAME + 1];

    /* =========== */
    /* Sub-test #1 */
    /* =========== */
      /* create file with dimension and associated coordinate variable */
      if (nc_create(FILE_NAME1, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, lat_T42_len, &lat_T42_dim)) ERR;
      lat_T42_dims[0] = lat_T42_dim;
      if (nc_def_var(ncid, VAR_NAME, NC_INT, RANK_lat_T42, lat_T42_dims, &lat_T42_id)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, rename coordinate dimension and then associated variable */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_dimid(ncid, DIM_NAME, &dimid)) ERR;
      if (nc_inq_varid(ncid, VAR_NAME, &varid)) ERR;
      if (nc_rename_dim(ncid, dimid, DIM_NAME2)) ERR;
      if (nc_rename_var(ncid, varid, VAR_NAME2)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, check coordinate dimension and associated variable names */
      /* Should be just like they created the file with DIM_NAME2 & VAR_NAME2 to
       *  begin with.
       */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_dimid(ncid, DIM_NAME2, &dimid)) ERR;
      if (nc_inq_varid(ncid, VAR_NAME2, &varid)) ERR;
      if (nc_inq_dimname(ncid, dimid, name)) ERR;
      if (strcmp(name, DIM_NAME2)) ERR;
      if (nc_inq_varname(ncid, varid, name)) ERR;
      if (strcmp(name, VAR_NAME2)) ERR;
      if (nc_close(ncid)) ERR;


    /* =========== */
    /* Sub-test #2 */
    /* =========== */
      /* create file with dimension and associated coordinate variable */
      if (nc_create(FILE_NAME1, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, lat_T42_len, &lat_T42_dim)) ERR;
      lat_T42_dims[0] = lat_T42_dim;
      if (nc_def_var(ncid, VAR_NAME, NC_INT, RANK_lat_T42, lat_T42_dims, &lat_T42_id)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, just rename coordinate dimension */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_dimid(ncid, DIM_NAME, &dimid)) ERR;
      if (nc_rename_dim(ncid, dimid, DIM_NAME2)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, check coordinate dimension and associated variable names */
      /* Should be just like the file was created with DIM_NAME2 to begin with */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_dimid(ncid, DIM_NAME2, &dimid)) ERR;
      if (nc_inq_varid(ncid, VAR_NAME, &varid)) ERR;
      if (nc_inq_dimname(ncid, dimid, name)) ERR;
      if (strcmp(name, DIM_NAME2)) ERR;
      if (nc_inq_varname(ncid, varid, name)) ERR;
      if (strcmp(name, VAR_NAME)) ERR;
      if (nc_close(ncid)) ERR;


    /* =========== */
    /* Sub-test #3 */
    /* =========== */
      /* create file with dimension and associated coordinate variable */
      if (nc_create(FILE_NAME1, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, lat_T42_len, &lat_T42_dim)) ERR;
      lat_T42_dims[0] = lat_T42_dim;
      if (nc_def_var(ncid, VAR_NAME, NC_INT, RANK_lat_T42, lat_T42_dims, &lat_T42_id)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, just rename variable */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_varid(ncid, VAR_NAME, &varid)) ERR;
      if (nc_rename_var(ncid, varid, VAR_NAME2)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, check coordinate dimension and associated variable names */
      /* Should be just like the file was created with VAR_NAME2 to begin with */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_dimid(ncid, DIM_NAME, &dimid)) ERR;
      if (nc_inq_varid(ncid, VAR_NAME2, &varid)) ERR;
      if (nc_inq_dimname(ncid, dimid, name)) ERR;
      if (strcmp(name, DIM_NAME)) ERR;
      if (nc_inq_varname(ncid, varid, name)) ERR;
      if (strcmp(name, VAR_NAME2)) ERR;
      if (nc_close(ncid)) ERR;


    /* =========== */
    /* Sub-test #4 */
    /* =========== */
      /* create file with dimension and associated coordinate variable */
      if (nc_create(FILE_NAME1, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, lat_T42_len, &lat_T42_dim)) ERR;
      lat_T42_dims[0] = lat_T42_dim;
      if (nc_def_var(ncid, VAR_NAME, NC_INT, RANK_lat_T42, lat_T42_dims, &lat_T42_id)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, rename associated variable and then coordinate dimension */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_dimid(ncid, DIM_NAME, &dimid)) ERR;
      if (nc_inq_varid(ncid, VAR_NAME, &varid)) ERR;
      if (nc_rename_var(ncid, varid, VAR_NAME2)) ERR;
      if (nc_rename_dim(ncid, dimid, DIM_NAME2)) ERR;
      if (nc_close(ncid)) ERR;


      /* reopen file, check coordinate dimension and associated variable names */
      /* Should be just like they created the file with DIM_NAME2 & VAR_NAME2 to
       *  begin with.
       */
      if (nc_open(FILE_NAME1, NC_WRITE, &ncid)) ERR;
      if (nc_inq_dimid(ncid, DIM_NAME2, &dimid)) ERR;
      if (nc_inq_varid(ncid, VAR_NAME2, &varid)) ERR;
      if (nc_inq_dimname(ncid, dimid, name)) ERR;
      if (strcmp(name, DIM_NAME2)) ERR;
      if (nc_inq_varname(ncid, varid, name)) ERR;
      if (strcmp(name, VAR_NAME2)) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing file with just one unlimited dimension...");
   {
      int ncid, dimid;
      int ndims_in, dimids_in[MAX_DIMS];
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int unlimdimid_in;
      int nunlimdims_in;

      /* Create a file with one unlimied dim and nothing else. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, LEVEL_NAME, NC_UNLIMITED, &dimid)) ERR;

      /* Check it out before the enddef. */
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != NC_UNLIMITED || strcmp(name_in, LEVEL_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_unlimdim(ncid, &unlimdimid_in)) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_inq_unlimdims(ncid, &nunlimdims_in, &unlimdimid_in)) ERR;
      if (nunlimdims_in != 1 || unlimdimid_in != 0) ERR;

      /* Automatically enddef and close. */
      if (nc_close(ncid)) ERR;

      /* Reopen and check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != NC_UNLIMITED || strcmp(name_in, LEVEL_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_unlimdim(ncid, &unlimdimid_in)) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_inq_unlimdims(ncid, &nunlimdims_in, &unlimdimid_in)) ERR;
      if (nunlimdims_in != 1 || unlimdimid_in != 0) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
#define ROMULUS "Romulus"
#define REMUS "Remus"
#define DIMS2 2
   printf("*** Testing file with two unlimited dimensions...");
   {
      int ncid, dimid[DIMS2];
      int ndims_in, dimids_in[DIMS2];
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int unlimdimid_in[DIMS2];
      int nunlimdims_in;

      /* Create a file with two unlimited dims and nothing else. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, REMUS, NC_UNLIMITED, &dimid[0])) ERR;
      if (nc_def_dim(ncid, ROMULUS, NC_UNLIMITED, &dimid[1])) ERR;

      /* Check it out before the enddef. */
      if (nc_inq_dim(ncid, dimid[0], name_in, &len_in)) ERR;
      if (len_in != NC_UNLIMITED || strcmp(name_in, REMUS)) ERR;
      if (nc_inq_dim(ncid, dimid[1], name_in, &len_in)) ERR;
      if (len_in != NC_UNLIMITED || strcmp(name_in, ROMULUS)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 2 || dimids_in[0] != 0 || dimids_in[1] != 1) ERR;
      if (nc_inq_unlimdim(ncid, &unlimdimid_in[0])) ERR;
      if (unlimdimid_in[0] != 0) ERR;
      if (nc_inq_unlimdims(ncid, &nunlimdims_in, unlimdimid_in)) ERR;
      if (nunlimdims_in != 2 || unlimdimid_in[0] != 0 || unlimdimid_in[1] != 1) ERR;

      /* Automatically enddef and close. */
      if (nc_close(ncid)) ERR;

      /* Reopen and check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing ordering of dimensions...");
   {
#define A_NAME "a"
#define B_NAME "b"
#define A_LEN 50
#define B_LEN 92
#define A_DIMID 1
#define B_DIMID 0

      int ncid;
      int ndims_in, dimids_in[MAX_DIMS];
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int dimid_a, dimid_b;

      /* Create a file with two dims and nothing else. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, B_NAME, B_LEN, &dimid_b)) ERR;
      if (nc_def_dim(ncid, A_NAME, A_LEN, &dimid_a)) ERR;
      if (dimid_b != B_DIMID || dimid_a != A_DIMID) ERR;

      /* Check out what we've got. */
      if (nc_inq_dim(ncid, dimid_a, name_in, &len_in)) ERR;
      if (len_in != A_LEN || strcmp(name_in, A_NAME)) ERR;
      if (nc_inq_dim(ncid, dimid_b, name_in, &len_in)) ERR;
      if (len_in != B_LEN || strcmp(name_in, B_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 2 || dimids_in[0] != 0 || dimids_in[1] != 1) ERR;

      if (nc_close(ncid)) ERR;

      /* Reopen and check it out again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      if (nc_inq_dim(ncid, B_DIMID, name_in, &len_in)) ERR;
      if (len_in != B_LEN || strcmp(name_in, B_NAME)) ERR;
      if (nc_inq_dim(ncid, A_DIMID, name_in, &len_in)) ERR;
      if (len_in != A_LEN || strcmp(name_in, A_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 2 || dimids_in[0] != 0 ||
	  dimids_in[1] != 1) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing file with just one unlimited dimension and one var...");
   {
      int ncid, dimid, dimids[MAX_DIMS];
      int level_varid;
      int natts_in, ndims_in, dimids_in[MAX_DIMS];
      nc_type xtype_in;
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int unlimdimid_in;
      size_t start[MAX_DIMS], count[MAX_DIMS];
      int varid_in, nvars_in, nunlimdims_in;
      unsigned long long uint64_data[1] = {42};

      /* Create a file with one unlimied dim and nothing else. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, LEVEL_NAME, NC_UNLIMITED, &dimid)) ERR;
      if (dimid != 0) ERR;
      dimids[0] = dimid;
      if (nc_def_var(ncid, LEVEL_NAME, NC_UINT64, 1, dimids, &level_varid)) ERR;
      if (level_varid != 0) ERR;

      /* Check it out before enddef. */
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != 0 || strcmp(name_in, LEVEL_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_unlimdim(ncid, &unlimdimid_in)) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_inq_unlimdims(ncid, &nunlimdims_in, &unlimdimid_in)) ERR;
      if (nunlimdims_in != 1 || unlimdimid_in != 0) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
      if (nc_inq_varid(ncid, LEVEL_NAME, &varid_in)) ERR;
      if (varid_in != 0) ERR;
      if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LEVEL_NAME) || xtype_in != NC_UINT64 || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;
      if (nc_inq_varname(ncid, 0, name_in)) ERR;
      if (strcmp(name_in, LEVEL_NAME)) ERR;

      /* Now write one record of data to the var. */
      start[0] = 0;
      count[0] = 1;
      if (nc_put_vara_ulonglong(ncid, 0, start, count, uint64_data)) ERR;

      /* Check dimension informaiton again. Now the length of this
       * dimension should be one. */
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != 1 || strcmp(name_in, LEVEL_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_dimlen(ncid, 0, &len_in)) ERR;
      if (len_in != 1) ERR;
      if (nc_inq_unlimdim(ncid, &unlimdimid_in)) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_inq_unlimdims(ncid, &nunlimdims_in, &unlimdimid_in)) ERR;
      if (nunlimdims_in != 1 || unlimdimid_in != 0) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
      if (nc_inq_varid(ncid, LEVEL_NAME, &varid_in)) ERR;
      if (varid_in != 0) ERR;
      if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LEVEL_NAME) || xtype_in != NC_UINT64 || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;
      if (nc_inq_varname(ncid, 0, name_in)) ERR;
      if (strcmp(name_in, LEVEL_NAME)) ERR;

      /* Close the file. */
      if (nc_close(ncid)) ERR;

      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != 1 || strcmp(name_in, LEVEL_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_unlimdim(ncid, &unlimdimid_in)) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_inq_unlimdims(ncid, &nunlimdims_in, &unlimdimid_in)) ERR;
      if (nunlimdims_in != 1 || unlimdimid_in != 0) ERR;
      if (unlimdimid_in != 0) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
      if (nc_inq_varid(ncid, LEVEL_NAME, &varid_in)) ERR;
      if (varid_in != 0) ERR;
      if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LEVEL_NAME) || xtype_in != NC_UINT64 || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;
      if (nc_inq_varname(ncid, 0, name_in)) ERR;
      if (strcmp(name_in, LEVEL_NAME)) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing adding a coordinate var to file with dimension...");
   {
      int ncid, dimid, dimids[MAX_DIMS];
      int natts_in, ndims_in, dimids_in[MAX_DIMS];
      nc_type xtype_in;
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int unlimdimid_in;
      int nvars_in, dim5_varid;

      /* Create a file with one dim and nothing else. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM5_NAME, DIM5_LEN, &dimid)) ERR;

      /* Check out what we've got. */
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 0 || natts_in != 0 ||
	 unlimdimid_in != -1) ERR;
      if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
      if (len_in != DIM5_LEN || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;

      if (nc_close(ncid)) ERR;

      /* Reopen and check it out again. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 0 || natts_in != 0 ||
	 unlimdimid_in != -1) ERR;
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != DIM5_LEN || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;

      /* Add a coordinate var for this dimension. */
      dimids[0] = 0;
      if (nc_def_var(ncid, DIM5_NAME, NC_FLOAT, 1, dimids, &dim5_varid)) ERR;

      /* Check it out. */
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || natts_in != 0 ||
	 unlimdimid_in != -1) ERR;
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != DIM5_LEN || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, DIM5_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;

      if (nc_close(ncid)) ERR;

      /* Reopen and check it out again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || natts_in != 0 ||
	 unlimdimid_in != -1) ERR;
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != DIM5_LEN || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, DIM5_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;

      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing adding a coordinate var to file with unlimited dimension...");
   {
      int ncid, dimid, dimids[MAX_DIMS];
      int natts_in, ndims_in, dimids_in[MAX_DIMS];
      nc_type xtype_in;
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int unlimdimid_in;
      size_t start[MAX_DIMS], count[MAX_DIMS], index[MAX_DIMS];
      unsigned short data[2] = {42, 24}, data_in[2];
      int nvars_in, hp_varid, dim5_varid;

      /* Create a file with one dim and one var. This time make
       * it an unlimited dim. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM5_NAME, NC_UNLIMITED, &dimid)) ERR;
      if (dimid != 0) ERR;
      dimids[0] = dimid;
      if (nc_def_var(ncid, HP_NAME, NC_USHORT, 1, dimids, &hp_varid)) ERR;
      if (hp_varid != 0) ERR;

      /* Check out what we've got. */
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || natts_in != 0 ||
	 unlimdimid_in != 0) ERR;
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != 0 || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, HP_NAME) || xtype_in != NC_USHORT || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;

      /* Add a record to the HP variable. */
      start[0] = 0;
      count[0] = 1;
      if (nc_put_vara(ncid, hp_varid, start, count, data)) ERR;

      /* Check to ensure dimension grew. */
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != 1 || strcmp(name_in, DIM5_NAME)) ERR;

      /* Reread the value just written. */
      index[0] = 0;
      if (nc_get_var1_ushort(ncid, hp_varid, index, data_in)) ERR;
      if (data_in[0] != data[0]) ERR;

      if (nc_close(ncid)) ERR;

      /* Reopen and check it out again. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || natts_in != 0 ||
	 unlimdimid_in != 0) ERR;
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != 1 || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;

      /* Add a coordinate var for this dimension. */
      dimids[0] = 0;
      if (nc_def_var(ncid, DIM5_NAME, NC_FLOAT, 1, dimids, &dim5_varid)) ERR;

      /* Check it out. */
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 2 || natts_in != 0 ||
	 unlimdimid_in != 0) ERR;
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != 1 || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_var(ncid, dim5_varid, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, DIM5_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;

      if (nc_close(ncid)) ERR;

      /* Reopen and check it out again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 2 || natts_in != 0 ||
	 unlimdimid_in != 0) ERR;
      if (nc_inq_dim(ncid, 0, name_in, &len_in)) ERR;
      if (len_in != 1 || strcmp(name_in, DIM5_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1 || dimids_in[0] != 0) ERR;
      if (nc_inq_var(ncid, dim5_varid, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, DIM5_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != 0 || natts_in != 0) ERR;

      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Creating file with 1 data var, 2 dims, and 2 coord. vars...");

   {
      int ncid, dimids[MAX_DIMS];
      int lat_dimid, lon_dimid, lat_varid, lon_varid;
      int pres_varid;
      char name_in[NC_MAX_NAME + 1];
      int natts_in, ndims_in, dimids_in[MAX_DIMS];
      nc_type xtype_in;
      size_t len_in;
      float lat[LAT_LEN], lon[LON_LEN];
      float lat_in[LAT_LEN], lon_in[LON_LEN];
      double pres[LAT_LEN][LON_LEN], pres_in[LAT_LEN][LON_LEN];
      int i, j;

      /* Lats and lons suitable for some South American data. */
      for (lat[0] = 40.0, i = 1; i < LAT_LEN; i++)
	 lat[i] = lat[i - 1] + .5;
      for (lon[0] = 20.0, i = 1; i < LON_LEN; i++)
	 lon[i] = lon[i - 1] + 1.5;

      /* Some phoney 2D pressure data. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    pres[i][j] = 1013.1 + j;

      /* Create a file. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* Define lat and lon dimensions, with associated variables. */
      if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &lat_dimid)) ERR;
      dimids[0] = lat_dimid;
      if (nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, dimids, &lat_varid)) ERR;
      if (nc_def_dim(ncid, LON_NAME, LON_LEN, &lon_dimid)) ERR;
      dimids[0] = lon_dimid;
      if (nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, dimids, &lon_varid)) ERR;

      /* Define a 2D variable called pressure, with NC_DOUBLE on a lat
       * lon grid. */
      dimids[0] = lat_dimid;
      dimids[1] = lon_dimid;
      if (nc_def_var(ncid, PRES_NAME, NC_DOUBLE, 2, dimids, &pres_varid)) ERR;

      /* Check our dimensions. */
      if (nc_inq_dim(ncid, lat_dimid, name_in, &len_in)) ERR;
      if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dim(ncid, lon_dimid, name_in, &len_in)) ERR;
      if (len_in != LON_LEN || strcmp(name_in, LON_NAME)) ERR;
      if (nc_inq_var(ncid, lat_varid, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LAT_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != lat_dimid || natts_in != 0) ERR;
      if (nc_inq_var(ncid, lon_varid, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LON_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != lon_dimid || natts_in != 0) ERR;

      /* Check our data variable. */
      if (nc_inq_var(ncid, pres_varid, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, PRES_NAME) || xtype_in != NC_DOUBLE || ndims_in != 2 ||
	  dimids_in[0] != lat_dimid || dimids_in[1] != lon_dimid || natts_in != 0) ERR;

      /* Write our latitude and longitude values. This writes all
       * metadata to disk too. */
      if (nc_put_var_float(ncid, lat_varid, lat)) ERR;
      if (nc_put_var_float(ncid, lon_varid, lon)) ERR;

      /* Write our 2D pressure values. */
      if (nc_put_var_double(ncid, pres_varid, (double *)pres)) ERR;

      /* Check our latitude and longitude values. */
      if (nc_get_var(ncid, lat_varid, lat_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 if (lat[i] != lat_in[i]) ERR;
      if (nc_get_var_float(ncid, lon_varid, lon_in)) ERR;
      for (i = 0; i < LON_LEN; i++)
	 if (lon[i] != lon_in[i]) ERR;

      /* Check our pressure values. */
      if (nc_get_var_double(ncid, pres_varid, (double *)pres_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    if (pres[i][j] != pres_in[i][j]) ERR;

      /* Close the file. */
      if (nc_close(ncid)) ERR;

      /* Reopen the file and check it out again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      /* Check our dimensions. */
      if (nc_inq_dim(ncid, lat_dimid, name_in, &len_in)) ERR;
      if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dim(ncid, lon_dimid, name_in, &len_in)) ERR;
      if (len_in != LON_LEN || strcmp(name_in, LON_NAME)) ERR;
      if (nc_inq_var(ncid, lat_varid, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LAT_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != lat_dimid || natts_in != 0) ERR;
      if (nc_inq_var(ncid, lon_varid, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LON_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != lon_dimid || natts_in != 0) ERR;

      /* Check our data variable. */
      if (nc_inq_var(ncid, pres_varid, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, PRES_NAME) || xtype_in != NC_DOUBLE || ndims_in != 2 ||
	  dimids_in[0] != lat_dimid || dimids_in[1] != lon_dimid || natts_in != 0) ERR;

      /* Check our latitude and longitude values. */
      if (nc_get_var(ncid, lat_varid, lat_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 if (lat[i] != lat_in[i]) ERR;
      if (nc_get_var_float(ncid, lon_varid, lon_in)) ERR;
      for (i = 0; i < LON_LEN; i++)
	 if (lon[i] != lon_in[i]) ERR;

      /* Check our pressure values. */
      if (nc_get_var_double(ncid, pres_varid, (double *)pres_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    if (pres[i][j] != pres_in[i][j]) ERR;

      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Creating file with 3 data vars, 4 dims, and 2 coord. vars...");

   {
      int ncid, lat_dimid, dimids[MAX_DIMS];
      int level_dimid, time_dimid, elev_varid;
      int lat_varid, lon_dimid, lon_varid, pres_varid, hp_varid;
      double pres[LAT_LEN][LON_LEN][LEVEL_LEN][TIME_LEN];
      double pres_in[LAT_LEN][LON_LEN][LEVEL_LEN][TIME_LEN];
      unsigned short hp[LAT_LEN][LON_LEN][TIME_LEN];
      unsigned short hp_in[LAT_LEN][LON_LEN][TIME_LEN];
      unsigned long long elev[LAT_LEN][LON_LEN], elev_in[LAT_LEN][LON_LEN];
      size_t start[4], count[4];
      int nvars, ndims, natts, unlimdimid;
      int natts_in, ndims_in, dimids_in[MAX_DIMS];
      nc_type xtype_in;
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      float lat[LAT_LEN], lon[LON_LEN];
      float lat_in[LAT_LEN], lon_in[LON_LEN];
      int i, j, k, l;

      /* Some phony 4D pressure data. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    for (k = 0; k < LEVEL_LEN; k++)
	       for (l = 0; l <TIME_LEN; l++)
		  pres[i][j][k][l] = 1013.1 + j;

      /* Some phony 3D hp data. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    for (l = 0; l <TIME_LEN; l++)
	       hp[i][j][l] = 100 + l;

      /* Some phony 2D elevaton data. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    elev[i][j] = 1010101022223333ULL  + i + j;

      /* Some phony 1D lats and lons. */
      for (i = 0; i < LAT_LEN; i++)
	 lat[i] = i * 5.;
      for (i = 0; i < LON_LEN; i++)
	 lon[i] = i * 5.;

      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* Define lat, lon, level, and timestep dimensions, with
       * associated coordinate variables for lat and lon only. Time is
       * an unlimited dimension. */
      if (nc_def_dim(ncid, LAT_NAME, LAT_LEN, &lat_dimid)) ERR;
      if (lat_dimid != LAT_DIMID) ERR;
      dimids[0] = lat_dimid;
      if (nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, dimids, &lat_varid)) ERR;
      if (lat_varid != LAT_VARID) ERR;
      if (nc_def_dim(ncid, LON_NAME, LON_LEN, &lon_dimid)) ERR;
      if (lon_dimid != LON_DIMID) ERR;
      dimids[0] = lon_dimid;
      if (nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, dimids, &lon_varid)) ERR;
      if (lon_varid != LON_VARID) ERR;
      if (nc_def_dim(ncid, LEVEL_NAME, LEVEL_LEN, &level_dimid)) ERR;
      if (level_dimid != LEVEL_DIMID) ERR;
      if (nc_def_dim(ncid, TIME_NAME, NC_UNLIMITED, &time_dimid)) ERR;
      if (time_dimid != TIME_DIMID) ERR;

      /* Define a 4D NC_DOUBLE variable called pressure. */
      dimids[0] = lat_dimid;
      dimids[1] = lon_dimid;
      dimids[2] = level_dimid;
      dimids[3] = time_dimid;
      if (nc_def_var(ncid, PRES_NAME, NC_DOUBLE, 4, dimids, &pres_varid)) ERR;
      if (pres_varid != PRES_VARID) ERR;

      /* Define a 2D variable for surface elevation. Use NC_INT64
       * because our units for this is Angstroms from Earth's
       * Center. */
      if (nc_def_var(ncid, ELEV_NAME, NC_INT64, 2, dimids, &elev_varid)) ERR;
      if (elev_varid != ELEV_VARID) ERR;

      /* Define a 3D NC_USHORT variable to store the number of Harry
       * Potter books in this grid square at this time (ignore HP
       * books in airplanes, dirigibles, hot air balloons, space
       * capsules, hang-gliders, parachutes, and flying on brooms). */
      dimids[2] = time_dimid;
      if (nc_def_var(ncid, HP_NAME, NC_USHORT, 3, dimids, &hp_varid)) ERR;
      if (hp_varid != HP_VARID) ERR;

      /* Did all our stuff make it into the internal metadata model
       * intact? */
      /* Check our dimensions. */
      if (nc_inq_dim(ncid, LAT_DIMID, name_in, &len_in)) ERR;
      if (len_in != LAT_LEN || strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dim(ncid, LON_DIMID, name_in, &len_in)) ERR;
      if (len_in != LON_LEN || strcmp(name_in, LON_NAME)) ERR;
      if (nc_inq_dim(ncid, LEVEL_DIMID, name_in, &len_in)) ERR;
      if (len_in != LEVEL_LEN || strcmp(name_in, LEVEL_NAME)) ERR;
      if (nc_inq_dim(ncid, TIME_DIMID, name_in, &len_in)) ERR;
      if (len_in != 0 || strcmp(name_in, TIME_NAME)) ERR;

      /* Check our coordinate variables. */
      if (nc_inq_var(ncid, LAT_VARID, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LAT_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != LAT_DIMID || natts_in != 0) ERR;
      if (nc_inq_var(ncid, LON_VARID, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, LON_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
	  dimids_in[0] != LON_DIMID || natts_in != 0) ERR;

      /* Check our data variables. */
      if (nc_inq_var(ncid, PRES_VARID, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, PRES_NAME) || xtype_in != NC_DOUBLE || ndims_in != 4 ||
	  dimids_in[0] != LAT_DIMID || dimids_in[1] != LON_DIMID ||
	  dimids_in[2] != LEVEL_DIMID || dimids_in[3] != TIME_DIMID ||
	  natts_in != 0) ERR;
      if (nc_inq_var(ncid, ELEV_VARID, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, ELEV_NAME) || xtype_in != NC_INT64 || ndims_in != 2 ||
	  dimids_in[0] != LAT_DIMID || dimids_in[1] != LON_DIMID ||
	  natts_in != 0) ERR;
      if (nc_inq_var(ncid, HP_VARID, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (strcmp(name_in, HP_NAME) || xtype_in != NC_USHORT || ndims_in != 3 ||
	  dimids_in[0] != LAT_DIMID || dimids_in[1] != LON_DIMID ||
	  dimids_in[2] != TIME_DIMID || natts_in != 0) ERR;

      /* Write our latitude and longitude values. This writes all
       * metadata to disk too. */
      if (nc_put_var_float(ncid, lat_varid, lat)) ERR;
      if (nc_put_var_float(ncid, lon_varid, lon)) ERR;

      /* Write our 4D pressure, elevation, and hp data. But this
       * should do nothing for pressure and hp, because these are
       * record vars, and nc_put_var_* doesn't do anything to record
       * vars, because it doesn't know how big the var is supposed to
       * be. */
      if (nc_put_var_double(ncid, pres_varid, (double *)pres)) ERR;
      if (nc_put_var_ulonglong(ncid, elev_varid, (unsigned long long *)elev)) ERR;
      if (nc_put_var_ushort(ncid, hp_varid, (unsigned short *)hp)) ERR;

      /* Check our latitude and longitude values. */
      if (nc_get_var(ncid, lat_varid, lat_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 if (lat[i] != lat_in[i]) ERR;
      if (nc_get_var_float(ncid, lon_varid, lon_in)) ERR;
      for (i = 0; i < LON_LEN; i++)
	 if (lon[i] != lon_in[i]) ERR;

      /* Make sure our pressure and hp variables are still
       * empty. get_var calls will return no error, but fetch no
       * data. */
      if (nc_inq_var(ncid, pres_varid, name_in, &xtype_in, &ndims_in,
		     dimids_in, &natts_in)) ERR;
      if (nc_inq_dim(ncid, dimids_in[3], NULL, &len_in)) ERR;
      if (len_in != 0) ERR;
      memset(pres_in, 0, sizeof(pres_in));
      if (nc_get_var_double(ncid, pres_varid, (double *)pres_in)) ERR;

      /* Check our pressure values. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    for (k = 0; k < LEVEL_LEN; k++)
	       for (l = 0; l <TIME_LEN; l++)
		  if (0 != pres_in[i][j][k][l]) ERR;

      if (nc_inq_var(ncid, hp_varid, NULL, NULL, &ndims_in,
		     dimids_in, NULL)) ERR;
      if (nc_inq_dim(ncid, dimids_in[2], NULL, &len_in)) ERR;
      if (len_in != 0) ERR;
      memset(hp_in, 0, sizeof(hp_in));
      if (nc_get_var_ushort(ncid, hp_varid, (unsigned short *)hp_in)) ERR;

      /* Check our hp values. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    for (l = 0; l <TIME_LEN; l++)
	       if (0 != hp_in[i][j][l]) ERR;

      /* Now use nc_put_vara to really write pressure and hp
       * data. Write TIME_LEN (4) records of each. */
      start[0] = start[1] = start[2] = start[3] = 0;
      count[0] = LAT_LEN;
      count[1] = LON_LEN;
      count[2] = LEVEL_LEN;
      count[3] = TIME_LEN;
      if (nc_put_vara(ncid, pres_varid, start, count,
		      (double *)pres)) ERR;
      count[2] = TIME_LEN;
      if (nc_put_vara(ncid, hp_varid, start, count,
		      (double *)hp)) ERR;

      /* Check our pressure values. */
      if (nc_get_var_double(ncid, pres_varid, (double *)pres_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    for (k = 0; k < LEVEL_LEN; k++)
	       for (l = 0; l <TIME_LEN; l++)
		  if (pres[i][j][k][l] != pres_in[i][j][k][l]) ERR;

      /* Check our elevation values. */
      if (nc_get_var_ulonglong(ncid, elev_varid, (unsigned long long *)elev_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    if (elev[i][j] != elev_in[i][j]) ERR;

      /* Check our hp values. */
      if (nc_get_var_ushort(ncid, hp_varid, (unsigned short *)hp_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    for (l = 0; l <TIME_LEN; l++)
	       if (hp[i][j][l] != hp_in[i][j][l]) ERR;

      /* Close the file. */
      if (nc_close(ncid)) ERR;

      /* Reopen the file and check it out again. At the moment, we
       * can't count on the varids and dimids being the same. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != 4 || nvars != 5 || natts != 0) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** Testing file with dims and only some coordinate vars...");
#define NDIMS_EX 4
#define NLAT 6
#define NLON 12
#define LAT_NAME_EX "latitude"
#define LON_NAME_EX "longitude"
#define NREC 2
#define REC_NAME "time"
#define LVL_NAME "level"
#define NLVL 2

/* Names of things. */
#define PRES_NAME "pressure"
#define TEMP_NAME "temperature"
#define UNITS "units"
#define DEGREES_EAST "degrees_east"
#define DEGREES_NORTH "degrees_north"

/* There are 4 vars, two for data, two for coordinate data. */
#define NVARS_EX 4

/* These are used to construct some example data. */
#define SAMPLE_PRESSURE 900
#define SAMPLE_TEMP 9.0
#define START_LAT 25.0
#define START_LON -125.0

/* For the units attributes. */
#define UNITS "units"
#define PRES_UNITS "hPa"
#define TEMP_UNITS "celsius"
#define LAT_UNITS "degrees_north"
#define LON_UNITS "degrees_east"
#define MAX_ATT_LEN 80
   {
      /* IDs for the netCDF file, dimensions, and variables. */
      int ncid, lon_dimid, lat_dimid;
      int lon_varid;
      int ndims_in;
      int dimid[NDIMS_EX];
      char dim_name_in[NDIMS_EX][NC_MAX_NAME];
      int i;

      /* Create the file. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* Define the dimensions. */
      if (nc_def_dim(ncid, LAT_NAME_EX, NLAT, &lat_dimid)) ERR;
      if (nc_def_dim(ncid, LON_NAME_EX, NLON, &lon_dimid)) ERR;

      /* Define a coordinate var. */
      if (nc_def_var(ncid, LON_NAME_EX, NC_FLOAT, 1, &lon_dimid,
			       &lon_varid)) ERR;

      /* Close the file. */
      if (nc_close(ncid)) ERR;

      if (nc_open(FILE_NAME, 0, &ncid)) ERR;

      /* Check dimensions. */
      ndims_in = 0;
      if (nc_inq_dimids(ncid, &ndims_in, dimid, 0)) ERR;
      if (ndims_in != 2) ERR;
      for (i = 0; i < 2; i++)
      {
	 if (dimid[i] != i) ERR;
	 if (nc_inq_dimname(ncid, i, dim_name_in[i])) ERR;
      }
      if (strcmp(dim_name_in[0], LAT_NAME_EX) ||
	  strcmp(dim_name_in[1], LON_NAME_EX)) ERR;

      /* Close the file. */
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** Testing file with just one very long dimension...");
   {
#define VERY_LONG_LEN (size_t)4500000000LL
      int ncid, dimid;
      int ndims_in, dimids_in[MAX_DIMS];
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int varid_in;

      if (SIZEOF_SIZE_T == 8)
      {
	 /* Create a file with one dim and nothing else. */
	 if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
	 if (nc_def_dim(ncid, LAT_NAME, VERY_LONG_LEN, &dimid)) ERR;

	 /* Check it out. */
	 if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
	 if (len_in != ((SIZEOF_SIZE_T == 8) ? VERY_LONG_LEN : NC_MAX_UINT) ||
	     strcmp(name_in, LAT_NAME)) ERR;
	 if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
	 if (ndims_in != 1) ERR;
	 if (nc_inq_dimid(ncid, LAT_NAME, &varid_in)) ERR;
	 if (varid_in != 0) ERR;
	 if (nc_inq_dimname(ncid, 0, name_in)) ERR;
	 if (strcmp(name_in, LAT_NAME)) ERR;
	 if (nc_inq_dimlen(ncid, 0, &len_in)) ERR;
	 if (len_in != ((SIZEOF_SIZE_T == 8) ? VERY_LONG_LEN : NC_MAX_UINT)) ERR;
	 if (nc_inq_unlimdims(ncid, &ndims_in, dimids_in)) ERR;
	 if (ndims_in != 0) ERR;
	 if (nc_close(ncid)) ERR;

	 /* Reopen and check it out again. */
	 if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
	 /* Check it out. */
	 if (nc_inq_dim(ncid, dimid, name_in, &len_in)) ERR;
	 if (len_in != ((SIZEOF_SIZE_T == 8) ? VERY_LONG_LEN : NC_MAX_UINT) ||
	     strcmp(name_in, LAT_NAME)) ERR;
	 if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
	 if (ndims_in != 1) ERR;
	 if (nc_inq_dimid(ncid, LAT_NAME, &varid_in)) ERR;
	 if (varid_in != 0) ERR;
	 if (nc_inq_dimname(ncid, 0, name_in)) ERR;
	 if (strcmp(name_in, LAT_NAME)) ERR;
	 if (nc_inq_dimlen(ncid, 0, &len_in)) ERR;
	 if (len_in != ((SIZEOF_SIZE_T == 8) ? VERY_LONG_LEN : NC_MAX_UINT)) ERR;
	 if (nc_inq_unlimdims(ncid, &ndims_in, dimids_in)) ERR;
	 if (ndims_in != 0) ERR;
	 if (nc_close(ncid)) ERR;
      }
   }
   SUMMARIZE_ERR;
   printf("*** Testing reference file with just one very long dimension...");
   {
#define REF_FILE_NAME "ref_tst_dims.nc"
      int ncid, dimid = 0;
      int ndims_in, dimids_in[MAX_DIMS];
      size_t len_in;
      char name_in[NC_MAX_NAME + 1];
      int varid_in;
      char file_in[NC_MAX_NAME + 1];
      int ret;

      strcpy(file_in, "");
      if (getenv("srcdir"))
      {
	 strcat(file_in, getenv("srcdir"));
	 strcat(file_in, "/");
      }
      strcat(file_in, REF_FILE_NAME);

      /* Reopen and check it out again. */
      if (nc_open(file_in, NC_NOWRITE, &ncid)) ERR;

      /* Check it out. */
      ret = nc_inq_dim(ncid, dimid, name_in, &len_in);
      if ((SIZEOF_SIZE_T >= 8 && ret) ||
	  (SIZEOF_SIZE_T < 8 && ret != NC_EDIMSIZE)) ERR;
      if (SIZEOF_SIZE_T < 8)
      {
	 if (len_in != NC_MAX_UINT) ERR;
      }
      else
      {
	 if (len_in != VERY_LONG_LEN) ERR;
      }
      if (strcmp(name_in, LAT_NAME)) ERR;
      if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
      if (ndims_in != 1) ERR;
      if (nc_inq_dimid(ncid, LAT_NAME, &varid_in)) ERR;
      if (varid_in != 0) ERR;
      if (nc_inq_unlimdims(ncid, &ndims_in, dimids_in)) ERR;
      if (ndims_in != 0) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Example #6
0
/* Given an ncid, check the file to make sure it has all the objects I
 * expect. */
int
check_nc4_file(int ncid)
{
   int varid, dimid, attnum, grpid, grpid2, grpid3, numgrps;
   int numtypes, enum_typeid, comp_typeid;
   int class_in;
   size_t att_len, size_in, num_mem, nfields_in;
   nc_type att_type, base_type_in;
   char name_in[NC_MAX_NAME + 1], strings_in[NC_MAX_NAME + 1], value;

   /* Check the group. */
   if (nc_inq_grps(ncid, &numgrps, &grpid)) ERR;
   if (numgrps != 1) ERR;
   name_in[0] = 0;
   if (nc_inq_grpname(grpid, name_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR;

   /* Check the variable. */
   if (nc_inq_varid(grpid, name_utf8, &varid)) ERR;
   if (nc_inq_varname(grpid, varid, name_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR;
   if (nc_inq_varid(grpid, norm_utf8, &varid)) ERR;
   name_in[0] = 0;
   if (nc_inq_varname(grpid, varid, name_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR;
   if (nc_get_var(grpid, varid, strings_in)) ERR;
   if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR;
   strings_in[0] = '\0'; /* Reset my string buffer. */

   /* Check the dimension. */
   if (nc_inq_dimid(grpid, name_utf8, &dimid)) ERR;
   if (nc_inq_dimname(grpid, dimid, name_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR;
   if (nc_inq_dimid(grpid, norm_utf8, &dimid)) ERR;
   if (nc_inq_dimname(grpid, dimid, name_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR;

   /* Check the attribute.  We don't normalize data or attribute
    * values, so get exactly what was put for the value, but
    * normalized values for names. */
   if (nc_inq_attid(grpid, varid, norm_utf8, &attnum)) ERR;
   if (attnum) ERR;
   attnum = 99; /* Reset. */
   if (nc_inq_attid(grpid, varid, name_utf8, &attnum)) ERR;
   if (attnum) ERR;
   if (nc_inq_att(grpid, varid, norm_utf8, &att_type, &att_len)) ERR;
   if (att_type != NC_CHAR || att_len != sizeof(name_utf8)) ERR;
   if (nc_get_att_text(grpid, varid, norm_utf8, strings_in)) ERR;
   if (strncmp(name_utf8, strings_in, sizeof(name_utf8))) ERR;

   /* Check the enum type. */
   if (nc_inq_grps(grpid, &numgrps, &grpid2)) ERR;
   if (numgrps != 1) ERR;
   if (nc_inq_typeids(grpid2, &numtypes, &enum_typeid)) ERR;
   if (numtypes != 1) ERR;
   if (nc_inq_user_type(grpid2, enum_typeid, name_in, &size_in, &base_type_in,
			       &nfields_in, &class_in)) ERR;
   if (strncmp(norm_utf8, name_in, strlen(norm_utf8)) || size_in != 1 ||
       base_type_in != NC_BYTE || nfields_in != 1 || class_in != NC_ENUM) ERR;
   name_in[0] = size_in = base_type_in = 0;
   if (nc_inq_enum(grpid2, enum_typeid, name_in, &base_type_in, &size_in, &num_mem)) ERR;
   if (strncmp(norm_utf8, name_in, strlen(norm_utf8)) || size_in != 1 ||
       base_type_in != NC_BYTE || num_mem != 1) ERR;
   if (nc_inq_enum_member(grpid2, enum_typeid, 0, name_in, &value)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || value != ENUM_VALUE) ERR;

   /* Check the compound type. */
   if (nc_inq_grps(grpid2, &numgrps, &grpid3)) ERR;
   if (numgrps != 1) ERR;
   if (nc_inq_typeids(grpid3, &numtypes, &comp_typeid)) ERR;
   if (numtypes != 1) ERR;
   name_in[0] = 0;
   if (nc_inq_user_type(grpid3, comp_typeid, name_in, &size_in, &base_type_in,
			&nfields_in, &class_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || size_in != sizeof(struct comp) ||
       base_type_in != NC_NAT || nfields_in != 1 || class_in != NC_COMPOUND) ERR;
   size_in = nfields_in = 999;
   if (nc_inq_compound(grpid3, comp_typeid, name_in, &size_in, &nfields_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8)) || size_in != sizeof(struct comp) ||
       nfields_in != 1) ERR;
   name_in[0] = 0;
   if (nc_inq_compound_fieldname(grpid3, comp_typeid, 0, name_in)) ERR;
   if (strncmp(norm_utf8, name_in, sizeof(norm_utf8))) ERR;
   return NC_NOERR;
}
Example #7
0
int
main(int argc, char **argv)
{
   printf("\n*** Testing HDF5/NetCDF-4 interoperability...\n");
   printf("*** testing HDF5 compatibility...");
   {
#define GRPA_NAME "grpa"
#define VAR_NAME "vara"
#define NDIMS 2
      int nrowCur = 7;               /* current size */
      int ncolCur = 3;
      int nrowMax = nrowCur + 0;     /* maximum size */
      int ncolMax = ncolCur + 0;

      hid_t xdimId;
      hid_t ydimId;
      hsize_t xscaleDims[1];
      hsize_t yscaleDims[1];
      hid_t xdimSpaceId, spaceId;
      hid_t fileId;
      hid_t fapl;
      hsize_t curDims[2];
      hsize_t maxDims[2];
      hid_t dataTypeId, dsPropertyId, grpaId, grpaPropId, dsId;
      hid_t ydimSpaceId;
      const char * dimNameBase
	 = "This is a netCDF dimension but not a netCDF variable.";
      char dimNameBuf[1000];
      char *varaName = "/grpa/vara";
      short amat[nrowCur][ncolCur];
      int ii, jj;

      xscaleDims[0] = nrowCur;
      yscaleDims[0] = ncolCur;
      if ((xdimSpaceId = H5Screate_simple(1, xscaleDims, NULL)) < 0) ERR;

      /* With the SEMI close degree, the HDF5 file close will fail if
       * anything is left open. */
      if ((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
      if (H5Pset_fclose_degree(fapl, H5F_CLOSE_SEMI)) ERR;

      /* Create file */
      if((fileId = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC,
			     H5Pcreate(H5P_FILE_CREATE), fapl)) < 0) ERR;
      if (H5Pclose(fapl) < 0) ERR;

      /* Create data space */
      curDims[0] = nrowCur;
      curDims[1] = ncolCur;
      maxDims[0] = nrowMax;
      maxDims[1] = ncolMax;
      if ((spaceId = H5Screate_simple(2, curDims, maxDims)) < 0) ERR;

      if ((dataTypeId = H5Tcopy(H5T_NATIVE_SHORT)) < 0) ERR;

      if ((dsPropertyId = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;

      if ((grpaPropId = H5Pcreate(H5P_GROUP_CREATE)) < 0) ERR;
      if ((grpaId = H5Gcreate2(fileId, GRPA_NAME, H5P_DEFAULT,
			       grpaPropId, H5P_DEFAULT)) < 0) ERR;
      if (H5Pclose(grpaPropId) < 0) ERR;

      /* Create vara dataset */
      if ((dsId = H5Dcreate2(fileId, varaName, dataTypeId, spaceId,
			     H5P_DEFAULT, dsPropertyId,
			     H5P_DEFAULT)) < 0) ERR;

      if (H5Pclose(dsPropertyId) < 0) ERR;
      if (H5Tclose(dataTypeId) < 0) ERR;
      if ((ydimSpaceId = H5Screate_simple(1, yscaleDims, NULL)) < 0) ERR;

      /* Create xdim dimension dataset */
      if ((xdimId = H5Dcreate2(fileId, "/xdim", H5T_IEEE_F32BE,
			       xdimSpaceId, H5P_DEFAULT, H5P_DEFAULT,
			       H5P_DEFAULT)) < 0) ERR;

      if (H5Sclose(xdimSpaceId) < 0) ERR;

      /* Create ydim dimension dataset */
      if ((ydimId = H5Dcreate2(fileId, "/ydim", H5T_IEEE_F32BE,
			       ydimSpaceId, H5P_DEFAULT, H5P_DEFAULT,
			       H5P_DEFAULT)) < 0) ERR;
      if (H5Sclose(ydimSpaceId) < 0) ERR;

      /* Create xdim scale */
      sprintf(dimNameBuf, "%s%10d", dimNameBase, nrowCur);
      if (H5DSset_scale(xdimId, dimNameBuf) < 0) ERR;

      /* Create ydim scale */
      sprintf(dimNameBuf, "%s%10d", dimNameBase, ncolCur);
      if (H5DSset_scale(ydimId, dimNameBuf) < 0) ERR;

      /* Attach dimension scales to the dataset */
      if (H5DSattach_scale(dsId, xdimId, 0) < 0) ERR;

      if (H5DSattach_scale(dsId, ydimId, 1) < 0) ERR;

      /* Close stuff. */
      if (H5Dclose(xdimId) < 0) ERR;
      if (H5Dclose(ydimId) < 0) ERR;
      if (H5Dclose(dsId) < 0) ERR;
      if (H5Gclose(grpaId) < 0) ERR;
      if (H5Sclose(spaceId) < 0) ERR;
      if (H5Fclose(fileId) < 0) ERR;

      /* Create some data */
      for (ii = 0; ii < nrowCur; ii++)
	 for (jj = 0; jj < ncolCur; jj++)
	    amat[ii][jj] = 100 * ii + jj;

      /* Re-open file */
      if ((fileId = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
      if ((grpaId = H5Gopen2(fileId, GRPA_NAME, H5P_DEFAULT)) < 0) ERR;
      if ((dsId = H5Dopen2(grpaId, varaName,  H5P_DEFAULT)) < 0) ERR;

      /* Write dataset */
      if (H5Dwrite(dsId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
		   amat) < 0) ERR;

      /* Write dimension values for both xdim, ydim */
      {
      short xydimMat[ nrowCur >= ncolCur ? nrowCur : ncolCur];
      for (ii = 0; ii < nrowCur; ii++)
	 xydimMat[ii] = 0;    /*#### 100 * ii; */

      /* Write xdim */
      if ((xdimId = H5Dopen2(fileId, "/xdim", H5P_DEFAULT)) < 0) ERR;
      if (H5Dwrite(xdimId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL,
		   H5P_DEFAULT, xydimMat) < 0) ERR;
      if (H5Dclose(xdimId) < 0) ERR;

      /* Write ydim */
      if ((ydimId = H5Dopen2(fileId, "/ydim", H5P_DEFAULT)) < 0) ERR;
      if (H5Dwrite(ydimId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL,
		   H5P_DEFAULT, xydimMat) < 0) ERR;
      if (H5Dclose(ydimId) < 0) ERR;
      }

      if (H5Dclose(dsId) < 0) ERR;
      if (H5Gclose(grpaId) < 0) ERR;
      if (H5Fclose(fileId) < 0) ERR;

      {
	 int ncid, grpid, nvars, ngatts, ndims, unlimdimid, ngrps;
	 char name_in[NC_MAX_NAME + 1];
	 nc_type xtype_in;
	 int ndims_in, natts_in, dimid_in[NDIMS];

/*	 nc_set_log_level(5);*/
	 if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
	 if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
	 if (ndims != 2 || nvars != 0 || ngatts != 0 || unlimdimid != -1) ERR;
	 if (nc_inq_grps(ncid, &ngrps, &grpid)) ERR;
	 if (ngrps != 1) ERR;
	 if (nc_inq(grpid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
	 if (ndims != 0 || nvars != 1 || ngatts != 0 || unlimdimid != -1) ERR;

	 if (nc_inq_var(grpid, 0, name_in, &xtype_in, &ndims_in, dimid_in,
			&natts_in)) ERR;
	 if (strcmp(name_in, VAR_NAME) || xtype_in != NC_SHORT || ndims_in != NDIMS ||
	     dimid_in[0] != 0 || dimid_in[1] != 1 || natts_in != 0) ERR;

	 if (nc_close(ncid)) ERR;
      }
   }
   SUMMARIZE_ERR;
#ifdef USE_SZIP
   printf("*** testing HDF5 compatibility with szip...");
   {

#define DEFLATE_LEVEL 9
#define MAX_NAME 100
#define NUM_CD_ELEM 10
/* HDF5 defines this... */
#define DEFLATE_NAME "deflate"
#define DIM1_LEN 3000
#define GRP_NAME "George_Washington"
#define BATTLE_RECORD "Battle_Record"

      hid_t fileid, grpid, spaceid, datasetid;
      int data_out[DIM1_LEN], data_in[DIM1_LEN];
      hsize_t dims[1] = {DIM1_LEN};
      hid_t propid;
      char name_in[MAX_NAME + 1];
      int ncid, ndims_in, nvars_in, ngatts_in, unlimdimid_in, ngrps_in;
      int nc_grpid;
      int dimid_in[1], natts_in;

      nc_type xtype_in;
      int i;

      for (i = 0; i < DIM1_LEN; i++)
	 data_out[i] = i;

      /* Open file and create group. */
      if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
			      H5P_DEFAULT)) < 0) ERR;
      if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR;

      /* Write an array of bools, with szip compression. */
      if ((propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
      if (H5Pset_layout(propid, H5D_CHUNKED)) ERR;
      if (H5Pset_chunk(propid, 1, dims)) ERR;
      if (H5Pset_szip(propid, H5_SZIP_EC_OPTION_MASK, 32)) ERR;
      if ((spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR;
      if ((datasetid = H5Dcreate(grpid, BATTLE_RECORD, H5T_NATIVE_INT,
				 spaceid, propid)) < 0) ERR;
      if (H5Dwrite(datasetid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
		   data_out) < 0) ERR;
      if (H5Dclose(datasetid) < 0 ||
	  H5Pclose(propid) < 0 ||
	  H5Sclose(spaceid) < 0 ||
	  H5Gclose(grpid) < 0 ||
	  H5Fclose(fileid) < 0)
	 ERR;

      /* Open the file with netCDF and check it. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 0 || nvars_in != 0 || ngatts_in != 0 || unlimdimid_in != -1) ERR;
      if (nc_inq_grps(ncid, &ngrps_in, &nc_grpid)) ERR;
      if (ngrps_in != 1) ERR;
      if (nc_inq(nc_grpid, &ndims_in, &nvars_in, &ngatts_in, &unlimdimid_in)) ERR;
      if (ndims_in != 1 || nvars_in != 1 || ngatts_in != 0 || unlimdimid_in != -1) ERR;

      /* Check the variable. */
      if (nc_inq_var(nc_grpid, 0, name_in, &xtype_in, &ndims_in, dimid_in,
      		     &natts_in)) ERR;
      if (strcmp(name_in, BATTLE_RECORD) || xtype_in != NC_INT || ndims_in != 1 ||
      	  dimid_in[0] != 0 || natts_in != 0) ERR;

      /* Check the data. */
      if (nc_get_var(nc_grpid, 0, data_in)) ERR;
      for (i = 0; i < DIM1_LEN; i++)
	 if (data_in[i] != data_out[i]) ERR;

      if (nc_close(ncid)) ERR;

   }
   SUMMARIZE_ERR;
#endif /* USE_SZIP */
   FINAL_RESULTS;
}
Example #8
0
int
main()
{
    printf("\n*** Creating file with datasets & attributes that have scalar dataspaces...");
    {
	hid_t fileid;
        hid_t fcplid;
	hid_t dsetid;
        hid_t dcplid;
	hid_t scalar_spaceid;
        hid_t vlstr_typeid, fixstr_typeid;
	hid_t attid;

        /* Create scalar dataspace */
	if ((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;

        /* Set creation ordering for file, so we can revise its contents later */
        if ((fcplid = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
        if (H5Pset_link_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
        if (H5Pset_attr_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
	
	/* Create new file, using default properties */
	if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcplid, H5P_DEFAULT)) < 0) ERR;
	
        /* Close file creation property list */
        if (H5Pclose(fcplid) < 0) ERR;


        /* Create variable-length string datatype */
        if ((vlstr_typeid = H5Tcreate(H5T_STRING, (size_t)H5T_VARIABLE)) < 0) ERR;

        /* Create fixed-length string datatype */
        if ((fixstr_typeid = H5Tcreate(H5T_STRING, (size_t)10)) < 0) ERR;


        /* Set creation ordering for dataset, so we can revise its contents later */
        if ((dcplid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
        if (H5Pset_attr_creation_order(dcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;

	
        /* Create scalar dataset with VL string datatype */
        if ((dsetid = H5Dcreate2(fileid, VSTR_VAR1_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR;

        /* Add attributes to dataset */
        if (add_attrs(dsetid) < 0) ERR;

        /* Close VL string dataset */
        if (H5Dclose(dsetid) < 0) ERR;


        /* Create scalar dataset with fixed-length string datatype */
        if ((dsetid = H5Dcreate2(fileid, FSTR_VAR_NAME, fixstr_typeid, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR;

        /* Add attributes to dataset */
        if (add_attrs(dsetid) < 0) ERR;

        /* Close fixed-length string dataset */
        if (H5Dclose(dsetid) < 0) ERR;


        /* Create scalar dataset with native integer datatype */
        if ((dsetid = H5Dcreate2(fileid, INT_VAR_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR;

        /* Add attributes to dataset */
        if (add_attrs(dsetid) < 0) ERR;

        /* Close native integer dataset */
        if (H5Dclose(dsetid) < 0) ERR;


        /* Add attributes to root group */
        if (add_attrs(fileid) < 0) ERR;


        /* Close dataset creation property list */
        if (H5Pclose(dcplid) < 0) ERR;

        /* Close string datatypes */
        if (H5Tclose(vlstr_typeid) < 0) ERR;
        if (H5Tclose(fixstr_typeid) < 0) ERR;


        /* Close rest */
	if (H5Sclose(scalar_spaceid) < 0) ERR;
	if (H5Fclose(fileid) < 0) ERR;
    }
    SUMMARIZE_ERR;

    printf("*** Checking accessing file through netCDF-4 API...");
    {
	int ncid, varid;
        size_t len;
        nc_type type;
        int ndims;
        char *vlstr;
        int x;

	if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

        /* Check the global attributes are OK */
        if (check_attrs(ncid, NC_GLOBAL) < 0) ERR;

        /* Verify that the VL string dataset is present and OK */
	if (nc_inq_varid(ncid, VSTR_VAR1_NAME, &varid)) ERR;
        if (varid != 0) ERR;
	if (nc_inq_vartype(ncid, varid, &type)) ERR;
	if (type != NC_STRING) ERR;
        if (nc_inq_varndims(ncid, varid, &ndims)) ERR;
        if (ndims != 0) ERR;
        vlstr = NULL;
        if (nc_get_var(ncid, varid, &vlstr)) ERR;
        if (NULL != vlstr) ERR;

        /* Check the variable's attributes are OK */
        if (check_attrs(ncid, varid) < 0) ERR;

        /* Verify that the fixed-length string dataset is present and OK */
	if (nc_inq_varid(ncid, FSTR_VAR_NAME, &varid)) ERR;
        if (varid != 1) ERR;
	if (nc_inq_vartype(ncid, varid, &type)) ERR;
	if (type != NC_STRING) ERR;
        if (nc_inq_varndims(ncid, varid, &ndims)) ERR;
        if (ndims != 0) ERR;
        vlstr = NULL;
        if (nc_get_var(ncid, varid, &vlstr)) ERR;
        if ('\0' != *vlstr) ERR;
        free(vlstr);

        /* Check the variable's attributes are OK */
        if (check_attrs(ncid, varid) < 0) ERR;

        /* Verify that the integer dataset is present and OK */
	if (nc_inq_varid(ncid, INT_VAR_NAME, &varid)) ERR;
        if (varid != 2) ERR;
	if (nc_inq_vartype(ncid, varid, &type)) ERR;
	if (type != NC_INT) ERR;
        if (nc_inq_varndims(ncid, varid, &ndims)) ERR;
        if (ndims != 0) ERR;
        x = -1;
        if (nc_get_var(ncid, varid, &x)) ERR;
        if (0 != x) ERR;

        /* Check the variable's attributes are OK */
        if (check_attrs(ncid, varid) < 0) ERR;

	if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;

    printf("*** Checking revising file through netCDF-4 API...");
    {
	int ncid, varid;
        char *vlstr;

	if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
        
        /* Write to the VL string variable */
	if (nc_inq_varid(ncid, VSTR_VAR1_NAME, &varid)) ERR;
        vlstr = NULL;
        if (nc_put_var(ncid, varid, &vlstr)) ERR;

        vlstr = malloc(10);
        *vlstr = '\0';
        if (nc_put_var(ncid, varid, &vlstr)) ERR;

        strcpy(vlstr, "foo");
        if (nc_put_var(ncid, varid, &vlstr)) ERR;
        free(vlstr);


        /* Write to a VL string attribute */
        vlstr = NULL;
        if (nc_put_att(ncid, varid, VSTR_ATT1_NAME, NC_STRING, 1, &vlstr)) ERR;

        vlstr = malloc(10);
        *vlstr = '\0';
        if (nc_put_att(ncid, varid, VSTR_ATT1_NAME, NC_STRING, 1, &vlstr)) ERR;

        strcpy(vlstr, "foo");
        if (nc_put_att(ncid, varid, VSTR_ATT1_NAME, NC_STRING, 1, &vlstr)) ERR;
        free(vlstr);


        /* Define a new VL string variable */
        if (nc_def_var(ncid, VSTR_VAR2_NAME , NC_STRING, 0, NULL, &varid)) ERR;

        /* Write to the variable's fill-value */
        vlstr = NULL;
        if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, &vlstr)) ERR;

        vlstr = malloc(10);
        *vlstr = '\0';
        if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, &vlstr)) ERR;

        strcpy(vlstr, "foo");
        if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, &vlstr)) ERR;
        free(vlstr);


	if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;

    FINAL_RESULTS;
}
Example #9
0
int
main(int argc, char **argv)
{
    printf("\n*** Testing netcdf-4 string type.\n");
    printf("*** testing string attribute...");
    {
#define ATT_LEN_1 1
#define MOUNTAIN_RANGE "mountain_range"
        size_t att_len;
        int ndims, nvars, natts, unlimdimid;
        nc_type att_type;
        int ncid, i;
        char *data_in[ATT_LEN_1] = {NULL};
        char *data[ATT_LEN_1] = {"R"};

        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
        if (nc_put_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, NC_STRING, ATT_LEN_1, data)) ERR;
        if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
        if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
        if (nc_inq_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, &att_type, &att_len)) ERR;
        if (att_type != NC_STRING || att_len != ATT_LEN_1) ERR;
        if (nc_close(ncid)) ERR;

        /* Check it out. */
        if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
        if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
        if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
        if (nc_inq_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, &att_type, &att_len)) ERR;
        if (att_type != NC_STRING || att_len != ATT_LEN_1) ERR;
        if (nc_get_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, data_in)) ERR;
        for (i = 0; i < att_len; i++)
            if (strcmp(data_in[i], data[i])) ERR;
        if (nc_free_string(ATT_LEN_1, (char **)data_in)) ERR;
        if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;
    printf("*** testing string attribute...");
    {
#define ATT_LEN 9
        size_t att_len;
        int ndims, nvars, natts, unlimdimid;
        nc_type att_type;
        int ncid, i;
        char *data_in[ATT_LEN];
        char *data[ATT_LEN] = {"Let but your honour know",
                               "Whom I believe to be most strait in virtue",
                               "That, in the working of your own affections",
                               "Had time cohered with place or place with wishing",
                               "Or that the resolute acting of your blood",
                               "Could have attain'd the effect of your own purpose",
                               "Whether you had not sometime in your life",
                               "Err'd in this point which now you censure him",
                               "And pull'd the law upon you."
                              };

        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
        if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, NC_STRING, ATT_LEN, data)) ERR;
        if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
        if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
        if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &att_type, &att_len)) ERR;
        if (att_type != NC_STRING || att_len != ATT_LEN) ERR;
        if (nc_close(ncid)) ERR;

        /* Check it out. */
        if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
        if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
        if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
        if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &att_type, &att_len)) ERR;
        if (att_type != NC_STRING || att_len != ATT_LEN) ERR;
        if (nc_get_att(ncid, NC_GLOBAL, ATT_NAME, data_in)) ERR;
        for (i = 0; i < att_len; i++)
            if (strcmp(data_in[i], data[i])) ERR;
        if (nc_free_string(att_len, (char **)data_in)) ERR;
        if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;
    printf("*** testing string var functions...");
    {
#define MOBY_LEN 16
        int ncid, varid, dimids[NDIMS];
        char *data[] = {"Perhaps a very little thought will now enable you to account for ",
                        "those repeated whaling disasters--some few of which are casually ",
                        "chronicled--of this man or that man being taken out of the boat by ",
                        "the line, and lost.",
                        "For, when the line is darting out, to be seated then in the boat, ",
                        "is like being seated in the midst of the manifold whizzings of a ",
                        "steam-engine in full play, when every flying beam, and shaft, and wheel, ",
                        "is grazing you.",
                        "It is worse; for you cannot sit motionless in the heart of these perils, ",
                        "because the boat is rocking like a cradle, and you are pitched one way and ",
                        "the other, without the slightest warning;",
                        "But why say more?",
                        "All men live enveloped in whale-lines.",
                        "All are born with halters round their necks; but it is only when caught ",
                        "in the swift, sudden turn of death, that mortals realize the silent, subtle, ",
                        "ever-present perils of life."
                       };
        char *data_in[MOBY_LEN];
        int i;

        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
        if (nc_def_dim(ncid, DIM_NAME, MOBY_LEN, dimids)) ERR;
        if (nc_def_var(ncid, VAR_NAME, NC_STRING, NDIMS, dimids, &varid)) ERR;
        if (nc_put_var_string(ncid, varid, (const char **)data)) ERR;
        if (nc_close(ncid)) ERR;

        /* Check it out. */
        if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
        if (nc_get_var_string(ncid, varid, data_in)) ERR;
        for (i=0; i<MOBY_LEN; i++)
            if (strcmp(data_in[i], data[i])) ERR;
        if (nc_free_string(MOBY_LEN, (char **)data_in)) ERR;
        if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;
    printf("*** testing string attributes...");
    {
#define SOME_PRES 16
#define NDIMS_PRES 1
#define ATT2_NAME "presidents"

        int ncid, i;
        char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
                                 "Monroe", "Adams", "Jackson", "VanBuren",
                                 "Harrison", "Tyler", "Polk", "Tayor",
                                 "Fillmore", "Peirce", "Buchanan", "Lincoln"
                                };
        char *data_in[SOME_PRES];

        /* Create a file with string attribute. */
        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
        if (nc_put_att_string(ncid, NC_GLOBAL, ATT2_NAME, SOME_PRES, (const char **)data)) ERR;
        if (nc_close(ncid)) ERR;

        /* Check it out. */
        if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
        if (nc_get_att_string(ncid, NC_GLOBAL, ATT2_NAME, (char **)data_in)) ERR;
        for (i=0; i < SOME_PRES; i++)
            if (strcmp(data_in[i], data[i])) ERR;

        /* Must free your data! */
        if (nc_free_string(SOME_PRES, (char **)data_in)) ERR;

        if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;
    printf("*** testing string fill value...");
    {
#define NUM_PRES 43
#define SOME_PRES 16
#define NDIMS_PRES 1
#define VAR_NAME_P "presidents"
        int ncid, varid, i, dimids[NDIMS_PRES];
        size_t start[NDIMS_PRES], count[NDIMS_PRES];
        char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
                                 "Monroe", "Adams", "Jackson", "VanBuren",
                                 "Harrison", "Tyler", "Polk", "Tayor",
                                 "Fillmore", "Peirce", "Buchanan", "Lincoln"
                                };
        char *data_in[NUM_PRES];

        /* Create a file with NUM_PRES strings, and write SOME_PRES of
         * them. */
        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
        if (nc_def_dim(ncid, DIM_NAME, NUM_PRES, dimids)) ERR;
        if (nc_def_var(ncid, VAR_NAME_P, NC_STRING, NDIMS_PRES, dimids, &varid)) ERR;
        start[0] = 0;
        count[0] = SOME_PRES;
        if (nc_put_vara_string(ncid, varid, start, count, (const char **)data)) ERR;
        if (nc_close(ncid)) ERR;

        /* Check it out. */
        if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
        if (nc_get_var_string(ncid, varid, data_in)) ERR;
        for (i = 0; i < NUM_PRES; i++)
        {
            if (i < SOME_PRES && strcmp(data_in[i], data[i])) ERR;
            if (i >= SOME_PRES && strcmp(data_in[i], "")) ERR;
        }

        /* Must free your data! */
        if (nc_free_string(NUM_PRES, (char **)data_in)) ERR;

        if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;
    printf("*** testing long string variable...");
    {
#define VAR_NAME2 "empty"
#define ATT_NAME2 "empty"
#define DHR_LEN 30
#define DIM_NAME1 "article"
#define VAR_NAME1 "universal_declaration_of_human_rights"
        int var_dimids[NDIMS];
        int ndims, nvars, natts, unlimdimid;
        nc_type var_type;
        char var_name[NC_MAX_NAME + 1];
        int var_natts, var_ndims;
        int ncid, varid, i, dimids[NDIMS], varid2;
        char *data_in[DHR_LEN];
        char *data[DHR_LEN] = {
            "All human beings are born free and equal in dignity and rights. "
            "They are endowed with reason and "
            "conscience and should act towards one another in a "
            "spirit of brotherhood.",
            "Everyone is entitled to all the rights and freedoms set "
            "forth in this Declaration, without distinction of "
            "any kind, such as race, colour, sex, language, religion, "
            "political or other opinion, national or social "
            "origin, property, birth or other status. Furthermore, no "
            "distinction shall be made on the basis of the "
            "political, jurisdictional or international status of the "
            "country or territory to which a person belongs, "
            "whether it be independent, trust, non-self-governing or "
            "under any other limitation of sovereignty.",
            "Everyone has the right to life, liberty and security of person.",
            "No one shall be held in slavery or servitude; slavery and the "
            "slave trade shall be prohibited in all their forms.",
            "No one shall be subjected to torture or to cruel, "
            "inhuman or degrading treatment or punishment.",
            "Everyone has the right to recognition everywhere as "
            "a person before the law.",
            "All are equal before the law and are entitled without "
            "any discrimination to equal protection of the law. All are "
            "entitled to equal protection against any discrimination in "
            "violation of this Declaration and against any incitement "
            "to such discrimination.",
            "Everyone has the right to an effective remedy by the "
            "competent national tribunals for acts violating the "
            "fundamental rights granted "
            "him by the constitution or by law.",
            "No one shall be subjected to arbitrary arrest, detention or exile.",
            "Everyone is entitled in full equality to a fair and public "
            "hearing by an independent and impartial tribunal, in the "
            "determination of his rights and obligations and of any "
            "criminal charge against him.",
            "(1) Everyone charged with a penal offence has the right "
            "to be presumed innocent until proved guilty according to law in a "
            "public trial at which he has had all the guarantees "
            "necessary for his defence."
            "(2) No one shall be held guilty of any penal offence "
            "on account of any act or omission which did not constitute a penal "
            "offence, under national or international law, at the time "
            "when it was committed. Nor shall a heavier penalty be imposed "
            "than the one that was applicable at the time the penal "
            "offence was committed.",
            "No one shall be subjected to arbitrary interference with "
            "his privacy, family, home or correspondence, nor to attacks upon "
            "his honour and reputation. Everyone has the right to the "
            "protection of the law against such interference or attacks.",
            "(1) Everyone has the right to freedom of movement and "
            "residence within the borders of each state."
            "(2) Everyone has the right to leave any country, "
            "including his own, and to return to his country.",
            "(1) Everyone has the right to seek and to enjoy in "
            "other countries asylum from persecution. "
            "(2) This right may not be invoked in the case of prosecutions "
            "genuinely arising from non-political crimes or from acts "
            "contrary to the purposes and principles of the United Nations.",
            "(1) Everyone has the right to a nationality. (2) No one shall "
            "be arbitrarily deprived of his nationality nor denied the "
            "right to change his nationality.",
            "(1) Men and women of full age, without any limitation "
            "due to race, nationality or religion, have the right "
            "to marry and to found a family. "
            "They are entitled to equal rights as to marriage, "
            "during marriage and at its dissolution. (2) Marriage "
            "shall be entered into only with the free and full "
            "consent of the intending spouses. (3) The family is "
            "the natural and fundamental group unit of society and "
            "is entitled to protection by society and the State.",
            "(1) Everyone has the right to own property alone as "
            "well as in association with others. (2) No one shall "
            "be arbitrarily deprived of his property.",
            "Everyone has the right to freedom of thought, conscience "
            "and religion; this right includes freedom to change "
            "his religion or belief, and freedom, "
            "either alone or in community with others and in "
            "public or private, to manifest his religion or "
            "belief in teaching, practice, worship and observance.",
            "Everyone has the right to freedom of opinion and "
            "expression; this right includes freedom to hold "
            "opinions without interference and to seek, receive "
            "and impart information and ideas through any media "
            "and regardless of frontiers.",
            "(1) Everyone has the right to freedom of peaceful assembly "
            "and association. (2) No one may be compelled to belong to "
            "an association.",
            "(1) Everyone has the right to take part in the government "
            "of his country, directly or through freely chosen representatives. "
            "(2) Everyone has the right of equal access to public "
            "service in his country. (3) The will of the people "
            "shall be the basis of the authority of government; "
            "this will shall be expressed in periodic and genuine "
            "elections which shall be by universal and equal suffrage "
            "and shall be held by secret vote or by "
            "equivalent free voting procedures.",
            "Everyone, as a member of society, has the right to "
            "social security and is entitled to realization, "
            "through national effort and international co-operation "
            "and in accordance with the organization and resources of "
            "each State, of the economic, social and cultural rights "
            "indispensable for his dignity and the free "
            "development of his personality.",
            "(1) Everyone has the right to work, to free choice "
            "of employment, to just and favourable conditions of "
            "work and to protection against unemployment. "
            "(2) Everyone, without any discrimination, has the right "
            "to equal pay for equal work. (3) Everyone who works "
            "has the right to just and favourable "
            "remuneration ensuring for himself and his family an "
            "existence worthy of human dignity, and supplemented, "
            "if necessary, by other means of social protection."
            "(4) Everyone has the right to form and to join trade "
            "unions for the protection of his interests.",
            "Everyone has the right to rest and leisure, including "
            "reasonable limitation of working hours and periodic "
            "holidays with pay.",
            "(1) Everyone has the right to a standard of living "
            "adequate for the health and well-being of himself "
            "and of his family, including food, clothing, housing "
            "and medical care and necessary social services, and "
            "the right to security in the event of unemployment, "
            "sickness, disability, widowhood, old age or other lack "
            "of livelihood in circumstances beyond his control. "
            "(2) Motherhood and childhood are entitled to special "
            "care and assistance. All children, whether "
            "born in or out of wedlock, shall enjoy the same "
            "social protection.",
            "(1) Everyone has the right to education. Education "
            "shall be free, at least in the elementary and "
            "fundamental stages. Elementary education shall be compulsory. "
            "Technical and professional education shall be made generally "
            "available and higher education "
            "shall be equally accessible to all on the basis of merit. "
            "(2) Education shall be directed to the full development of "
            "the human personality and to the strengthening of respect for "
            "human rights and fundamental freedoms. "
            "It shall promote understanding, tolerance and friendship among "
            "all nations, racial or religious groups, and shall further the "
            "activities of the United Nations "
            "for the maintenance of peace. (3) Parents have a prior right to "
            "choose the kind of education that shall be given to their children.",
            "(1) Everyone has the right freely to participate in the cultural "
            "life of the community, to enjoy the arts and to share in scientific "
            "advancement and its benefits. "
            "(2) Everyone has the right to the protection of the moral "
            "and material interests resulting from any scientific, "
            "literary or artistic production of which he is the author.",
            "Everyone is entitled to a social and international order in "
            "which the rights and freedoms set forth in this Declaration "
            "can be fully realized.",
            "(1) Everyone has duties to the community in which alone "
            "the free and full development of his personality is possible. "
            "(2) In the exercise of his rights and "
            "freedoms, everyone shall be subject only to such limitations "
            "as are determined by law solely for the purpose "
            "of securing due recognition and respect for the rights "
            "and freedoms of others and of meeting the just requirements "
            "of morality, public order and the general welfare in a "
            "democratic society. (3) These rights and freedoms may in no "
            "case be exercised contrary to the purposes and "
            "principles of the United Nations.",
            "Nothing in this Declaration may be interpreted as implying "
            "for any State, group or person any right to engage in any "
            "activity or to perform any act aimed at the destruction of "
            "any of the rights and freedoms set forth herein."
        };
        char *empty_string[] = {""};

        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

        /* Create an array of strings for the Universal Declaraion of Human Rights. */
        if (nc_def_dim(ncid, DIM_NAME1, DHR_LEN, dimids)) ERR;
        if (nc_def_var(ncid, VAR_NAME1, NC_STRING, NDIMS, dimids, &varid)) ERR;

        /* Create a scalar variable for the empty string. */
        if (nc_def_var(ncid, VAR_NAME2, NC_STRING, 0, NULL, &varid2)) ERR;

        /* Check some stuff. */
        if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
        if (ndims != NDIMS || nvars != 2 || natts != 0 || unlimdimid != -1) ERR;
        if (nc_inq_var(ncid, varid, var_name, &var_type, &var_ndims,
                       var_dimids, &var_natts)) ERR;
        if (var_type != NC_STRING || strcmp(var_name, VAR_NAME1) || var_ndims != NDIMS ||
                var_dimids[0] != dimids[0]) ERR;

        /* Write the universal declaraion of human rights. */
        if (nc_put_var(ncid, varid, data)) ERR;

        /* Write an empty string with an empty attribute. */
        if (nc_put_var(ncid, varid2, empty_string)) ERR;
        if (nc_put_att(ncid, varid2, ATT_NAME2, NC_STRING, 0, empty_string)) ERR;

        /* Close the file. */
        if (nc_close(ncid)) ERR;

        /* Check it out. */
        if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
        if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
        if (ndims != NDIMS || nvars != 2 || natts != 0 || unlimdimid != -1) ERR;

        /* Check declaration. */
        if (nc_inq_varid(ncid, VAR_NAME1, &varid)) ERR;
        if (nc_inq_var(ncid, varid, var_name, &var_type, &var_ndims,
                       var_dimids, &var_natts)) ERR;
        if (var_type != NC_STRING || strcmp(var_name, VAR_NAME1) || var_ndims != NDIMS ||
                var_dimids[0] != dimids[0]) ERR;
        if (nc_get_var(ncid, varid, data_in)) ERR;
        for (i = 0; i < DHR_LEN; i++)
            if (strcmp(data_in[i], data[i])) ERR;
        if (nc_free_string(DHR_LEN, data_in)) ERR;

        /* Check the empty var and att. */
        if (nc_inq_varid(ncid, VAR_NAME2, &varid)) ERR;
        if (nc_get_var(ncid, varid, data_in)) ERR;
        if (strcmp(data_in[0], empty_string[0])) ERR;
        if (nc_free_string(1, data_in)) ERR;
        if (nc_get_att(ncid, varid, ATT_NAME2, NULL)) ERR;
        if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;
    FINAL_RESULTS;
}
Example #10
0
psurface3d
read_nc_surface3d(const char *filename)
{
#ifdef USE_NETCDF
  psurface3d gr;
  real(*x)[3];
  uint(*e)[2];
  uint(*t)[3];
  uint(*s)[3];
  uint      vertices;
  uint      edges;
  uint      triangles;
  int       res, nc_in;
  int       x_id, e_id, t_id, s_id;
  int       vertices_id, edges_id, triangles_id;
  size_t    dim;

  /* Open CDF file */
  res = nc_open(filename, NC_NOWRITE, &nc_in);
  nc_handle_error(res);

  /* Obtain dimensions */
  res = nc_inq_dimid(nc_in, "vertices", &vertices_id);
  nc_handle_error(res);
  res = nc_inq_dimid(nc_in, "edges", &edges_id);
  nc_handle_error(res);
  res = nc_inq_dimid(nc_in, "triangles", &triangles_id);
  nc_handle_error(res);

  /* Get values of dimensions */
  res = nc_inq_dimlen(nc_in, vertices_id, &dim);
  nc_handle_error(res);
  vertices = dim;
  res = nc_inq_dimlen(nc_in, edges_id, &dim);
  nc_handle_error(res);
  edges = dim;
  res = nc_inq_dimlen(nc_in, triangles_id, &dim);
  nc_handle_error(res);
  triangles = dim;

  /* Create surface3d object */
  gr = new_surface3d(vertices, edges, triangles);
  x = gr->x;
  e = gr->e;
  t = gr->t;
  s = gr->s;

  /* Obtain variables */
  res = nc_inq_varid(nc_in, "x", &x_id);
  nc_handle_error(res);
  res = nc_inq_varid(nc_in, "e", &e_id);
  nc_handle_error(res);
  res = nc_inq_varid(nc_in, "t", &t_id);
  nc_handle_error(res);
  res = nc_inq_varid(nc_in, "s", &s_id);
  nc_handle_error(res);

  /* Read variables */
  res = nc_get_var(nc_in, x_id, x);
  res = nc_get_var(nc_in, e_id, e);
  res = nc_get_var(nc_in, t_id, t);
  res = nc_get_var(nc_in, s_id, s);

  /* Close file */
  res = nc_close(nc_in);
  nc_handle_error(res);

  return gr;

#else
  (void) filename;

  (void) printf("Sorry, no NetCDF support.\n");
  return 0;
#endif
}
Example #11
0
int mov_session::open(QString openFilename)
{
    int ierr,ncid,i,nrow;
    int dimid_ntimeseries,nTimeseries;
    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;
    const char * mydatachar[1];
    double mydatadouble[1];
    int mydataint[1];
    QMessageBox::StandardButton reply;
    QString filelocation,filename,series_name,color,type;
    QString coldstartstring,stationfile,stationfilepath;
    QString BaseFile,NewFile,TempFile,BaseDir;
    double unitconvert,xshift,yshift;
    size_t temp_size_t;
    size_t start[1];
    QDate tempstartdate,tempenddate;
    QString tempstring;
    QColor CellColor;
    QDateTime ColdStart;
    //ADCNC NetCDFData;
    //ADCASCII ADCData;
    bool continueToLoad,hasCheckInfo;
    Qt::CheckState checkState;


    QFile Session(openFilename);
    if(!Session.exists())
    {
        emit sessionError("File not found");
        return 1;
    }

    //Open the netCDF file
    ierr = mov_generic::NETCDF_ERR(nc_open(openFilename.toUtf8(),NC_NOWRITE,&ncid));
    if(ierr!=NC_NOERR)return 1;

    //Read some of the basics from the file (dimensions, variable IDs)
    ierr = mov_generic::NETCDF_ERR(nc_inq_dimid(ncid,"ntimeseries",&dimid_ntimeseries));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_filename",&varid_filename));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_colors",&varid_colors));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_names",&varid_names));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_filetype",&varid_type));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_units",&varid_units));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_xshift",&varid_xshift));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_yshift",&varid_yshift));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_coldstartdate",&varid_coldstart));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_stationfile",&varid_stationfile));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_plottitle",&varid_plottitle));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_xlabel",&varid_xlabel));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_ylabel",&varid_ylabel));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_precision",&varid_precision));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_startdate",&varid_startdate));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_enddate",&varid_enddate));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_ymin",&varid_ymin));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_ymax",&varid_ymax));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_autodate",&varid_autodate));
    if(ierr!=NC_NOERR)return 1;

    ierr = mov_generic::NETCDF_ERR(nc_inq_varid(ncid,"timeseries_autoy",&varid_autoy));
    if(ierr!=NC_NOERR)return 1;

    ierr = nc_inq_varid(ncid,"timeseries_checkState",&varid_checkState);
    if(ierr!=NC_NOERR)
        hasCheckInfo = false;
    else
        hasCheckInfo = true;

    //Read the scalar variables
    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_plottitle,&mydatachar));
    if(ierr!=NC_NOERR)return 1;
    this->plotTitleWidget->setText(QString(mydatachar[0]));

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_xlabel,&mydatachar));
    if(ierr!=NC_NOERR)return 1;
    this->xLabelWidget->setText(QString(mydatachar[0]));

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_ylabel,&mydatachar));
    if(ierr!=NC_NOERR)return 1;
    this->yLabelWidget->setText(QString(mydatachar[0]));

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_startdate,&mydatachar));
    if(ierr!=NC_NOERR)return 1;
    tempstring = QString(mydatachar[0]);
    tempstartdate = QDateTime::fromString(tempstring,"yyyy-MM-dd hh:mm:ss").date();
    this->startDateEdit->setDate(tempstartdate);

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_enddate,&mydatachar));
    if(ierr!=NC_NOERR)return 1;
    tempstring = QString(mydatachar[0]);
    tempenddate = QDateTime::fromString(tempstring,"yyyy-MM-dd hh:mm:ss").date();
    this->endDateEdit->setDate(tempenddate);

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_ymin,&mydatadouble));
    if(ierr!=NC_NOERR)return 1;
    this->yMinSpinBox->setValue(mydatadouble[0]);

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_ymax,&mydatadouble));
    if(ierr!=NC_NOERR)return 1;
    this->yMaxSpinBox->setValue(mydatadouble[0]);

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_autodate,&mydataint));
    if(ierr!=NC_NOERR)return 1;
    if(mydataint[0]==0)
        this->checkAllData->setChecked(false);
    else
        this->checkAllData->setChecked(true);

    ierr = mov_generic::NETCDF_ERR(nc_get_var(ncid,varid_autoy,&mydataint));
    if(ierr!=NC_NOERR)return 1;
    if(mydataint[0]==0)
        this->checkYAuto->setChecked(false);
    else
        this->checkYAuto->setChecked(true);

    //Next, read in the data and add rows to the table
    ierr = mov_generic::NETCDF_ERR(nc_inq_dimlen(ncid,dimid_ntimeseries,&temp_size_t));
    if(ierr!=NC_NOERR)return 1;
    nTimeseries = static_cast<int>(temp_size_t);
    nrow = 0;

    //Get the location we are currently working in
    mov_generic::splitPath(openFilename,TempFile,this->currentDirectory);

    for(i=0;i<nTimeseries;i++)
    {
        start[0] = static_cast<size_t>(i);

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_filename,start,&mydatachar));
        if(ierr!=NC_NOERR)return 1;
        filelocation = QString(mydatachar[0]);
        mov_generic::splitPath(filelocation,filename,TempFile);

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_names,start,&mydatachar));
        if(ierr!=NC_NOERR)return 1;
        series_name = QString(mydatachar[0]);

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_type,start,&mydatachar));
        if(ierr!=NC_NOERR)return 1;
        type = QString(mydatachar[0]);

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_colors,start,&mydatachar));
        if(ierr!=NC_NOERR)return 1;
        color = QString(mydatachar[0]);

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_units,start,&mydatadouble));
        if(ierr!=NC_NOERR)return 1;
        unitconvert = mydatadouble[0];

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_coldstart,start,&mydatachar));
        if(ierr!=NC_NOERR)return 1;
        coldstartstring = QString(mydatachar[0]);

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_xshift,start,&mydatadouble));
        if(ierr!=NC_NOERR)return 1;
        xshift = mydatadouble[0];

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_yshift,start,&mydatadouble));
        if(ierr!=NC_NOERR)return 1;
        yshift = mydatadouble[0];

        ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_stationfile,start,&mydatachar));
        if(ierr!=NC_NOERR)return 1;
        stationfilepath = QString(mydatachar[0]);
        mov_generic::splitPath(stationfilepath,stationfile,TempFile);

        if(hasCheckInfo)
        {
            ierr = mov_generic::NETCDF_ERR(nc_get_var1(ncid,varid_checkState,start,&mydataint));
            if(mydataint[0]==1)
                checkState = Qt::Checked;
            else
                checkState = Qt::Unchecked;
        }
        else
            checkState = Qt::Checked;

        continueToLoad = false;

        filelocation = this->currentDirectory+"/"+filelocation;
        mov_generic::splitPath(filelocation,BaseFile,BaseDir);


        QFile myfile(filelocation);
        if(!myfile.exists())
        {
            //The file wasn't found where we think it should be. Give the
            //user a chance to specify an alternate data directory
            if(this->alternateFolder==NULL)
            {
                //If we haven't previously specified an alternate folder, inform the user and
                //ask if they want to specify.
                reply = QMessageBox::question((QWidget*)parent(),"File not found",
                    "Data file not found in default location. Would you like to specify another?");
                if(reply==QMessageBox::Yes)
                {

                    //Get an alternate location
                    //this->alternateFolder = QFileDialog::getExistingDirectory(0,"Select Folder",this->previousDirectory);
                    this->alternateFolder = QFileDialog::getExistingDirectory((QWidget*)parent(),"Test",this->previousDirectory);
                    NewFile = this->alternateFolder+"/"+BaseFile;
                    QFile myfile(NewFile);
                    if(!myfile.exists())
                    {
                        continueToLoad = false;
                        emit sessionError("The file "+BaseFile+" was not found and has been skipped.");
                    }
                    else
                    {
                        continueToLoad = true;
                        filelocation = NewFile;
                    }
                }
                else
                    continueToLoad = false;
            }
            else
            {
                //Start by trying the previously specified alternate folder
                NewFile = this->alternateFolder+"/"+BaseFile;
                QFile myfile(NewFile);
                if(!myfile.exists())
                {
                    reply = QMessageBox::question((QWidget*)parent(),"File not found",
                        "File not found in default location. Would you like to specify another?");
                    if(reply==QMessageBox::Yes)
                    {
                        //Get an alternate location
                        this->alternateFolder = QFileDialog::getExistingDirectory((QWidget*)parent(),"Select Folder",this->previousDirectory);
                        NewFile = this->alternateFolder+"/"+BaseFile;
                        QFile myfile(NewFile);
                        if(!myfile.exists())
                        {
                            continueToLoad = false;
                            emit sessionError("The file "+BaseFile+" was not found and has been skipped.");
                        }
                        else
                        {
                            continueToLoad = true;
                            filelocation = NewFile;
                        }
                    }
                    else
                        continueToLoad = false;
                }
                else
                {
                    continueToLoad = true;
                    filelocation = NewFile;
                }
            }
        }
        else
        {
            continueToLoad = true;
        }

        if(type == "ADCIRC")
        {
            mov_generic::splitPath(stationfilepath,BaseFile,BaseDir);
            stationfilepath = this->currentDirectory+"/"+stationfilepath;
            QFile myfile(stationfilepath);
            if(!myfile.exists())
            {
                //The file wasn't found where we think it should be. Give the
                //user a chance to specify an alternate data directory
                if(this->alternateFolder==NULL)
                {
                    //If we haven't previously specified an alternate folder, inform the user and
                    //ask if they want to specify.
                    reply = QMessageBox::question((QWidget*)parent(),"File not found",
                        "Station file not found in default location. Would you like to specify another?");
                    if(reply==QMessageBox::Yes)
                    {
                        //Get an alternate location
                        this->alternateFolder = QFileDialog::getExistingDirectory((QWidget*)parent(),"Select Folder",this->previousDirectory);
                        NewFile = this->alternateFolder+"/"+BaseFile;
                        QFile myfile(NewFile);
                        if(!myfile.exists())
                        {
                            continueToLoad = false;
                            emit sessionError("The file "+BaseFile+" was not found and has been skipped.");
                        }
                        else
                        {
                            continueToLoad = true;
                            stationfilepath = NewFile;
                        }
                    }
                    else
                        continueToLoad = false;
                }
                else
                {
                    //Start by trying the previously specified alternate folder
                    NewFile = this->alternateFolder+"/"+BaseFile;
                    QFile myfile(NewFile);
                    if(!myfile.exists())
                    {
                        reply = QMessageBox::question((QWidget*)parent(),"File not found",
                            "File not found in default location. Would you like to specify another?");
                        if(reply==QMessageBox::Yes)
                        {
                            //Get an alternate location
                            this->alternateFolder = QFileDialog::getExistingDirectory((QWidget*)parent(),"Select Folder",this->previousDirectory);
                            NewFile = this->alternateFolder+"/"+BaseFile;
                            QFile myfile(NewFile);
                            if(!myfile.exists())
                            {
                                continueToLoad = false;
                                emit sessionError("The file "+BaseFile+" was not found and has been skipped.");
                            }
                            else
                            {
                                continueToLoad = true;
                                stationfilepath = NewFile;
                            }
                        }
                        else
                            continueToLoad = false;
                    }
                    else
                    {
                        continueToLoad = true;
                        stationfilepath = NewFile;
                    }
                }
            }
            else
            {
                continueToLoad = true;
            }
        }

        if(continueToLoad)
        {
            //Build the table
            nrow = nrow + 1;
            this->tableWidget->setRowCount(nrow);
            this->tableWidget->setRowCount(nrow);
            this->tableWidget->setItem(nrow-1,0,new QTableWidgetItem(filename));
            this->tableWidget->setItem(nrow-1,1,new QTableWidgetItem(series_name));
            this->tableWidget->setItem(nrow-1,2,new QTableWidgetItem(color));
            this->tableWidget->setItem(nrow-1,3,new QTableWidgetItem(QString::number(unitconvert)));
            this->tableWidget->setItem(nrow-1,4,new QTableWidgetItem(QString::number(xshift)));
            this->tableWidget->setItem(nrow-1,5,new QTableWidgetItem(QString::number(yshift)));
            this->tableWidget->setItem(nrow-1,6,new QTableWidgetItem(filelocation));
            this->tableWidget->setItem(nrow-1,7,new QTableWidgetItem((coldstartstring)));
            this->tableWidget->setItem(nrow-1,8,new QTableWidgetItem(type));
            this->tableWidget->setItem(nrow-1,9,new QTableWidgetItem(stationfile));
            this->tableWidget->setItem(nrow-1,10,new QTableWidgetItem(stationfilepath));
            CellColor.setNamedColor(color);
            this->tableWidget->item(nrow-1,2)->setBackgroundColor(CellColor);
            this->tableWidget->item(nrow-1,0)->setCheckState(checkState);
            ColdStart = QDateTime::fromString(coldstartstring,"yyyy-MM-dd hh:mm:ss");
        }
    }
    ierr = mov_generic::NETCDF_ERR(nc_close(ncid));
    if(ierr!=NC_NOERR)return 1;

    return 0;
}
Example #12
0
int
main(int argc, char **argv)
{
#ifdef USE_PARALLEL
   MPI_Init(&argc, &argv);
#endif

   printf("\n*** Testing netcdf-4 string type.\n");
   printf("*** testing string variable...");
   {
      int var_dimids[NDIMS];
      int ndims, nvars, natts, unlimdimid;
      nc_type var_type;
      char var_name[NC_MAX_NAME + 1];
      int var_natts, var_ndims;
      int ncid, varid, i, dimids[NDIMS];
      char *data_in[DIM_LEN];
      char *data[DIM_LEN] = {"Let but your honour know",
			     "Whom I believe to be most strait in virtue", 
			     "That, in the working of your own affections", 
			     "Had time cohered with place or place with wishing", 
			     "Or that the resolute acting of your blood",
			     "Could have attain'd the effect of your own purpose",
			     "Whether you had not sometime in your life",
			     "Err'd in this point which now you censure him", 
			     "And pull'd the law upon you."};
   
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, DIM_LEN, dimids)) ERR;
      if (nc_def_var(ncid, VAR_NAME, NC_STRING, NDIMS, dimids, &varid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != NDIMS || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_var(ncid, varid, var_name, &var_type, &var_ndims,
		     var_dimids, &var_natts)) ERR;
      if (var_type != NC_STRING || strcmp(var_name, VAR_NAME) || var_ndims != NDIMS ||
	  var_dimids[0] != dimids[0]) ERR;
      if (nc_put_var(ncid, varid, data)) ERR;
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != NDIMS || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_var(ncid, varid, var_name, &var_type, &var_ndims,
		     var_dimids, &var_natts)) ERR;
      if (var_type != NC_STRING || strcmp(var_name, VAR_NAME) || var_ndims != NDIMS ||
	  var_dimids[0] != dimids[0]) ERR;
      if (nc_get_var(ncid, varid, data_in)) ERR;
      for (i=0; i<DIM_LEN; i++)
	 if (strcmp(data_in[i], data[i])) ERR;
      for (i = 0; i < DIM_LEN; i++)
	 free(data_in[i]);
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** testing string attribute...");
   {
      
      size_t att_len;
      int ndims, nvars, natts, unlimdimid;
      nc_type att_type;
      int ncid, i;
      char *data_in[DIM_LEN];
      char *data[DIM_LEN] = {"Let but your honour know",
			     "Whom I believe to be most strait in virtue", 
			     "That, in the working of your own affections", 
			     "Had time cohered with place or place with wishing", 
			     "Or that the resolute acting of your blood",
			     "Could have attain'd the effect of your own purpose",
			     "Whether you had not sometime in your life",
			     "Err'd in this point which now you censure him", 
			     "And pull'd the law upon you."};
   

      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, NC_STRING, DIM_LEN, data)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
      if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &att_type, &att_len)) ERR;
      if (att_type != NC_STRING || att_len != DIM_LEN) ERR;
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
      if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &att_type, &att_len)) ERR;
      if (att_type != NC_STRING || att_len != DIM_LEN) ERR;
      if (nc_get_att(ncid, NC_GLOBAL, ATT_NAME, data_in)) ERR; 
      for (i = 0; i < att_len; i++)
	 if (strcmp(data_in[i], data[i])) ERR;
      if (nc_free_string(att_len, (char **)data_in)) ERR;
      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** testing string var functions...");

   {
#define MOBY_LEN 16
      int ncid, varid, i, dimids[NDIMS];
      char *data[] = {"Perhaps a very little thought will now enable you to account for ",
		      "those repeated whaling disasters--some few of which are casually ",
		      "chronicled--of this man or that man being taken out of the boat by ",
		      "the line, and lost.",
		      "For, when the line is darting out, to be seated then in the boat, ",
		      "is like being seated in the midst of the manifold whizzings of a ",
		      "steam-engine in full play, when every flying beam, and shaft, and wheel, ",
		      "is grazing you.",
		      "It is worse; for you cannot sit motionless in the heart of these perils, ",
		      "because the boat is rocking like a cradle, and you are pitched one way and ",
		      "the other, without the slightest warning;",
		      "But why say more?",
		      "All men live enveloped in whale-lines.",
		      "All are born with halters round their necks; but it is only when caught ",
		      "in the swift, sudden turn of death, that mortals realize the silent, subtle, ",
		      "ever-present perils of life."};
      char *data_in[MOBY_LEN];

      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, MOBY_LEN, dimids)) ERR;
      if (nc_def_var(ncid, VAR_NAME, NC_STRING, NDIMS, dimids, &varid)) ERR;
      if (nc_put_var_string(ncid, varid, (const char **)data)) ERR;
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
     if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
     if (nc_get_var_string(ncid, varid, data_in)) ERR;
     for (i=0; i<MOBY_LEN; i++)
	if (strcmp(data_in[i], data[i])) ERR;
     if (nc_free_string(MOBY_LEN, (char **)data_in)) ERR;
     if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;

   printf("*** testing string attributes...");
   {
#define SOME_PRES 16
#define NDIMS_PRES 1
#define ATT2_NAME "presidents"

      int ncid, i;
      char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
			       "Monroe", "Adams", "Jackson", "VanBuren",
			       "Harrison", "Tyler", "Polk", "Tayor", 
			       "Fillmore", "Peirce", "Buchanan", "Lincoln"};
      char *data_in[SOME_PRES];

      /* Create a file with string attribute. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_put_att_string(ncid, NC_GLOBAL, ATT2_NAME, SOME_PRES, (const char **)data)) ERR;
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_get_att_string(ncid, NC_GLOBAL, ATT2_NAME, (char **)data_in)) ERR;
      for (i=0; i < SOME_PRES; i++)
	 if (strcmp(data_in[i], data[i])) ERR;
      
      /* Must free your data! */
      if (nc_free_string(SOME_PRES, (char **)data_in)) ERR;

      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** testing string fill value...");

   {
#define NUM_PRES 43
#define SOME_PRES 16
#define NDIMS_PRES 1

      int ncid, varid, i, dimids[NDIMS_PRES];
      size_t start[NDIMS_PRES], count[NDIMS_PRES];
      char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
			       "Monroe", "Adams", "Jackson", "VanBuren",
			       "Harrison", "Tyler", "Polk", "Tayor", 
			       "Fillmore", "Peirce", "Buchanan", "Lincoln"};
      char *data_in[NUM_PRES];

      /* Create a file with NUM_PRES strings, and write SOME_PRES of
       * them. */
      /*      nc_set_log_level(4);*/
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, NUM_PRES, dimids)) ERR;
      if (nc_def_var(ncid, VAR_NAME, NC_STRING, NDIMS_PRES, dimids, &varid)) ERR;
      start[0] = 0;
      count[0] = SOME_PRES;
      if (nc_put_vara_string(ncid, varid, start, count, (const char **)data)) ERR;
      if (nc_close(ncid)) ERR;
      
      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_get_var_string(ncid, varid, data_in)) ERR;
      for (i=0; i < NUM_PRES; i++)
      {
	 if (i < SOME_PRES && strcmp(data_in[i], data[i])) ERR;
	 if (i >= SOME_PRES && strcmp(data_in[i], "")) ERR;
      }
      
      /* Must free your data! */
      if (nc_free_string(SOME_PRES, (char **)data_in)) ERR;

      if (nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   FINAL_RESULTS;
#ifdef USE_PARALLEL
   MPI_Finalize();
#endif   
}
void *mthreadLoadImg(void *arg) {
    thread_args *t_args = (thread_args*)arg;
    float ****storage = t_args->storage;
    float ****count = t_args->count;
    int start_i = t_args->start_i;
    int stop_i = t_args->stop_i;
    int num_rows = t_args->num_rows;
    int num_columns = t_args->num_columns;
    pthread_mutex_t *fopen_lock = t_args->fopen_lock;
    char **warp_list = t_args->warp_list;
    int retval;
    int ncid;
    size_t i,j;
    int file_i;
    sir_head *head = t_args->head;
    // netcdf vars
    int lon_varid, lat_varid, time_varid, sm_varid, rsize_varid;
    int loc_dimid, obs_dimid;
    size_t loc_len,obs_len;
    // netcdf storage
    double *lon, *lat, *time;
    long long *rsize;
    int8_t *sm;
    // time storage
    struct tm *tm;



    for (file_i = start_i; file_i <= stop_i; file_i++) {
        printf("Processing file %d of %d\n",file_i,935);
        pthread_mutex_lock(fopen_lock);

        if ((retval = nc_open(warp_list[file_i], NC_NOWRITE, &ncid))) {
            ERR(retval);
            continue;
        }
        // get the dimids and the lengths of the dimensions
        if ((retval = nc_inq_dimid (ncid, "locations", &loc_dimid))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_inq_dimid (ncid, "obs", &obs_dimid))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_inq_dimlen (ncid, loc_dimid, &loc_len))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_inq_dimlen (ncid, obs_dimid, &obs_len))) {
            ERR(retval);
            continue;
        }
        // allocate space for all of the variables based on dim length
        lon = malloc(sizeof(double)*loc_len);
        lat = malloc(sizeof(double)*loc_len);
        time = malloc(sizeof(double)*obs_len);
        rsize = malloc(sizeof(int64_t)*loc_len);
        sm = malloc(sizeof(int8_t)*obs_len);

        // where to store the time conversions
        tm = malloc(sizeof(struct tm)*obs_len);

        memset(lon,0,sizeof(double)*loc_len);
        memset(lat,0,sizeof(double)*loc_len);
        memset(time,0,sizeof(double)*obs_len);
        memset(rsize,0,sizeof(int64_t)*loc_len);
        memset(sm,0,sizeof(int8_t)*obs_len);
        memset(tm,0,sizeof(struct tm)*obs_len);

        /* Get the varid of the data variable, based on its name. */
        if ((retval = nc_inq_varid(ncid, "lon", &lon_varid))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_inq_varid(ncid, "lat", &lat_varid))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_inq_varid(ncid, "time", &time_varid))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_inq_varid(ncid, "row_size", &rsize_varid))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_inq_varid(ncid, "sm", &sm_varid))) {
            ERR(retval);
            continue;
        }
        /* read values from netCDF variable */
        if ((retval = nc_get_var_double(ncid, lon_varid, lon))) {
            ERR(retval);
            continue;
        }
        /* read values from netCDF variable */
        if ((retval = nc_get_var_double(ncid, lat_varid, lat))) {
            ERR(retval);
            continue;
        }
        /* read values from netCDF variable */
        if ((retval = nc_get_var_double(ncid, time_varid, time))) {
            ERR(retval);
            continue;
        }
        /* read values from netCDF variable */
        if ((retval = nc_get_var_longlong(ncid, rsize_varid, rsize))) {
            ERR(retval);
            continue;
        }
        /* read values from netCDF variable */
        if ((retval = nc_get_var(ncid, sm_varid, sm))) {
            ERR(retval);
            continue;
        }
        if ((retval = nc_close(ncid))) {
            ERR(retval);
            continue;
        }

        pthread_mutex_unlock(fopen_lock);

        // now load the image into the storage array if necessary
        int use_file = 0;
        float x,y;
        int x_int, y_int;
        for (i = 0; i < loc_len; i++) {
            sir_latlon2pix(lon[i],lat[i],&x,&y,head);
            x_int = (int)x;
            y_int = (int)y;
            if ( x_int < num_columns && x_int >= 0 && y_int < num_rows && y_int >= 0) {
                use_file = 1;
                break;
            }
        }

        if (!use_file) {
            // free data
            free(lon);
            free(lat);
            free(time);
            free(rsize);
            free(sm);
            free(tm);
            continue;
        }

        // if I'm here, then there are measurements that need to be stored
        // first convert the time to doy and year
        // time is stored as a double -- number of days since 1900-1-1 00:00:00
        double cur_time;
        for (i = 0; i < obs_len; i++) {
            cur_time = time[i];
            // first convert to seconds
            // (1 day * 24 hours/day * 60 min/hr * 60 sec/min) = 84600
            cur_time = cur_time * 86400;
            // convert from seconds since 1900 to unix time
            // note that accd to time protocol in RFC 868, this is 2208988800
            cur_time = cur_time - 2208988800;

            // here, cast from double to time_t and use gmtime_r to convert to year/doy
            time_t cur_time_conv = (time_t) cur_time;
            gmtime_r(&cur_time_conv, &tm[i]);
            // note that tm_year is years since 1900, so adjust to true year
            tm[i].tm_year = tm[i].tm_year + 1900;
            assert(tm[i].tm_year-(YEAR_START)>=0);
        }
        // now all the times should be correct in the tm struct
        // parse through the measurements

        size_t ind = 0;
        int affected_days[3] = {-1,-1,-1};
        int k;
        for (i = 0; i < loc_len; i++) {
            sir_latlon2pix(lon[i],lat[i],&x,&y,head);
            x_int = (int)x;
            y_int = (int)y;
            // if lat/lon are in im bounds
            if ( x_int < num_columns && x_int >= 0 && y_int < num_rows && y_int >= 0) {
                for (j = 0; j < rsize[i]; j++) {

                    if (tm[ind].tm_yday%2 == 0) {
                        affected_days[0] = tm[ind].tm_yday-3;
                        affected_days[1] = tm[ind].tm_yday-1;
                        affected_days[2] = -1;
                    }
                    else {
                        affected_days[0] = tm[ind].tm_yday-4;
                        affected_days[1] = tm[ind].tm_yday-2;
                        affected_days[2] = tm[ind].tm_yday;
                    }
                    // convert affected days to a 1:2:365 index

                    for (k = 0; k < 3; k++) {
                        if (affected_days[k] < 0)
                            continue;
                        affected_days[k] = affected_days[k]/2;
                        // shorthand
                        float *st = &storage[y_int][x_int][affected_days[k]][tm[ind].tm_year-YEAR_START];
                        float *ct = &count[y_int][x_int][affected_days[k]][tm[ind].tm_year-YEAR_START];
                        // do running average
                        *st = *st + (((float)sm[ind]) - *st) / *ct;
                        (*ct)++;
                    }
                    ind++;
                }
            }
            else {
                ind += rsize[i];
            }
        }
        // free data
        free(lon);
        free(lat);
        free(time);
        free(rsize);
        free(sm);
        free(tm);
    }
    return NULL;
}
Example #14
0
pcluster
read_cdfpart_cluster(int nc_file, const char *prefix)
{
  pcluster  t;
  uint     *idx;
  size_t    clusters, clusteridx;
  size_t    coeffs, coeffidx;
  char      dimname[NC_MAX_NAME + 1];
  char     *buf;
  int       bufsize;
  int       nc_sons, nc_size, nc_idx, nc_coeff;
  int       nc_clusters, nc_coeffs, nc_totalsize, nc_dim;
  size_t    dim, totalsize;
  int       result;

  /* Prepare buffer for prefixed names */
  bufsize = strlen(prefix) + 16;
  buf = (char *) allocmem(sizeof(char) * bufsize);

  /* Get "clusters" dimension */
  prefix_name(buf, bufsize, prefix, "clusters");
  result = nc_inq_dimid(nc_file, buf, &nc_clusters);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_clusters, dimname, &clusters);
  assert(result == NC_NOERR);

  /* Get "coeffs" dimension */
  prefix_name(buf, bufsize, prefix, "coeffs");
  result = nc_inq_dimid(nc_file, buf, &nc_coeffs);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_coeffs, dimname, &coeffs);
  assert(result == NC_NOERR);

  /* Get "totalsize" dimension */
  prefix_name(buf, bufsize, prefix, "totalsize");
  result = nc_inq_dimid(nc_file, buf, &nc_totalsize);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_totalsize, dimname, &totalsize);
  assert(result == NC_NOERR);

  /* Get "dim" dimension */
  prefix_name(buf, bufsize, prefix, "dim");
  result = nc_inq_dimid(nc_file, buf, &nc_dim);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_dim, dimname, &dim);
  assert(result == NC_NOERR);

  /* Get "sons" variable */
  prefix_name(buf, bufsize, prefix, "sons");
  result = nc_inq_varid(nc_file, buf, &nc_sons);
  assert(result == NC_NOERR);

  /* Get "size" variable */
  prefix_name(buf, bufsize, prefix, "size");
  result = nc_inq_varid(nc_file, buf, &nc_size);
  assert(result == NC_NOERR);

  /* Get "idx" variable */
  prefix_name(buf, bufsize, prefix, "idx");
  result = nc_inq_varid(nc_file, buf, &nc_idx);
  assert(result == NC_NOERR);

  /* Get "coeff" variable */
  prefix_name(buf, bufsize, prefix, "coeff");
  result = nc_inq_varid(nc_file, buf, &nc_coeff);
  assert(result == NC_NOERR);

  /* Read index */
  idx = (uint *) allocmem(sizeof(uint) * totalsize);
  nc_get_var(nc_file, nc_idx, idx);

  /* Read coefficients from NetCDF variables */
  clusteridx = 0;
  coeffidx = 0;
  t = read_cdf_part(nc_file, clusters, coeffs,
		    nc_sons, nc_size, nc_coeff,
		    idx, dim, &clusteridx, &coeffidx);
  assert(clusteridx == clusters);
  assert(coeffidx == coeffs);

  /* Clean up */
  freemem(buf);

  return t;
}
Example #15
0
pcluster
read_cdf_cluster(const char *name)
{
  pcluster  t;
  uint     *idx;
  size_t    clusters, clusteridx;
  size_t    coeffs, coeffidx;
  char      dimname[NC_MAX_NAME + 1];
  int       nc_file, nc_sons, nc_size, nc_idx, nc_coeff;
  int       nc_clusters, nc_coeffs, nc_totalsize, nc_dim;
  size_t    dim, totalsize;
  int       result;

  /* Open NetCDF file */
  result = nc_open(name, NC_NOWRITE, &nc_file);
  assert(result == NC_NOERR);

  /* Get "clusters" dimension */
  result = nc_inq_dimid(nc_file, "clusters", &nc_clusters);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_clusters, dimname, &clusters);
  assert(result == NC_NOERR);

  /* Get "coeffs" dimension */
  result = nc_inq_dimid(nc_file, "coeffs", &nc_coeffs);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_coeffs, dimname, &coeffs);
  assert(result == NC_NOERR);

  /* Get "totalsize" dimension */
  result = nc_inq_dimid(nc_file, "totalsize", &nc_totalsize);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_totalsize, dimname, &totalsize);
  assert(result == NC_NOERR);

  /* Get "dim" dimension */
  result = nc_inq_dimid(nc_file, "dim", &nc_dim);
  assert(result == NC_NOERR);
  result = nc_inq_dim(nc_file, nc_dim, dimname, &dim);
  assert(result == NC_NOERR);

  /* Get "sons" variable */
  result = nc_inq_varid(nc_file, "sons", &nc_sons);
  assert(result == NC_NOERR);

  /* Get "size" variable */
  result = nc_inq_varid(nc_file, "size", &nc_size);
  assert(result == NC_NOERR);

  /* Get "idx" variable */
  result = nc_inq_varid(nc_file, "idx", &nc_idx);
  assert(result == NC_NOERR);

  /* Get "coeff" variable */
  result = nc_inq_varid(nc_file, "coeff", &nc_coeff);
  assert(result == NC_NOERR);

  /* Read index */
  idx = (uint *) allocmem(sizeof(uint) * totalsize);
  nc_get_var(nc_file, nc_idx, idx);

  /* Read coefficients from NetCDF variables */
  clusteridx = 0;
  coeffidx = 0;
  t = read_cdf_part(nc_file, clusters, coeffs,
		    nc_sons, nc_size, nc_coeff,
		    idx, dim, &clusteridx, &coeffidx);
  assert(clusteridx == clusters);
  assert(coeffidx == coeffs);

  /* Close NetCDF file */
  nc_close(nc_file);

  return t;
}
Example #16
0
int
main()
{
   /* There will be netCDF IDs for the file, each group, and each
    * variable. */
   int ncid, varid1, varid2, grp1id, grp2id;

   unsigned long long data_in[NX][NY];

   /* Loop indexes, and error handling. */
   int x, y, retval;

   /* The following struct is written as a compound type. */
   struct s1
   {
         int i1;
         int i2;
   };
   struct s1 compound_data[NX][NY];

   /* Open the file. NC_NOWRITE tells netCDF we want read-only access
    * to the file.*/
   if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
      ERR(retval);

   /* Get the group ids of our two groups. */
   if ((retval = nc_inq_ncid(ncid, "grp1", &grp1id)))
      ERR(retval);
   if ((retval = nc_inq_ncid(ncid, "grp2", &grp2id)))
      ERR(retval);

   /* Get the varid of the uint64 data variable, based on its name, in
    * grp1. */
   if ((retval = nc_inq_varid(grp1id, "data", &varid1)))
      ERR(retval);

   /* Read the data. */
   if ((retval = nc_get_var_ulonglong(grp1id, varid1, &data_in[0][0])))
      ERR(retval);

   /* Get the varid of the compound data variable, based on its name,
    * in grp2. */
   if ((retval = nc_inq_varid(grp2id, "data", &varid2)))
      ERR(retval);

   /* Read the data. */
   if ((retval = nc_get_var(grp2id, varid2, &compound_data[0][0])))
      ERR(retval);

   /* Check the data. */
   for (x = 0; x < NX; x++)
      for (y = 0; y < NY; y++)
      {
         if (data_in[x][y] != x * NY + y ||
             compound_data[x][y].i1 != 42 ||
             compound_data[x][y].i2 != -42)
            return ERRCODE;
      }

   /* Close the file, freeing all resources. */
   if ((retval = nc_close(ncid)))
      ERR(retval);

   printf("*** SUCCESS reading example file %s!\n", FILE_NAME);
   return 0;
}