コード例 #1
0
int main() {
  const char *path = "http://opendap.tudelft.nl/thredds/dodsC/data2/deltares/rijkswaterstaat/jarkus/profiles/transect.nc";
  int ncid;
  int status;
  status = nc_open (path, 0, &ncid);
  if (status != NC_NOERR) handle_error(status);
  // Get the lat variable
  int lat_id;
  status = nc_inq_varid (ncid, "lat", &lat_id);
  if (status != NC_NOERR) handle_error(status);

  // Get one element element of the 2d array lat
  static size_t start0[] = {0, 0}; /* start at first value */
  static size_t start1[] = {1, 1}; /* start at second value */
  static size_t count[] = {1, 1}; /* get one value*/
  double vals[1*1];   /* array to hold values */

  // Read the second value (takes a jiffy)
  status = nc_get_vara(ncid, lat_id, start1, count, vals);
  if (status != NC_NOERR) handle_error(status);
  printf("lat[1,1]:%f\n", vals[0]);

  // Takes very long (reads the whole lat array)
  status = nc_get_vara(ncid, lat_id, start0, count, vals);
  if (status != NC_NOERR) handle_error(status);
  printf("lat[0,0] read 1x1:%f\n", vals[0]);

  status = nc_close(ncid);
  if (status != NC_NOERR) handle_error(status);

  return 0;
}
コード例 #2
0
ファイル: v2i.c プロジェクト: stcorp/harp
int
ncvarget(
    int		ncid,
    int		varid,
    const long*	start,
    const long*	count, 
    void*	value
)
{
	NDIMS_DECL
	A_DECL(stp, size_t, ndims, start);
	A_DECL(cntp, size_t, ndims, count);
	A_INIT(stp, size_t, ndims, start);
	A_INIT(cntp, size_t, ndims, count);
	{
	const int status = nc_get_vara(ncid, varid, stp, cntp, value);
	A_FREE(cntp);
	A_FREE(stp);
	if(status != NC_NOERR)
	{
		nc_advise("ncvarget", status, "ncid %d; varid %d", ncid, varid);
		return -1;
	}
	}
	return 0;
}
コード例 #3
0
ファイル: example1.c プロジェクト: fischer-ncar/cime
/** @brief Check the output file.
 *
 *  Use netCDF to check that the output is as expected. 
 *
 * @param ntasks The number of processors running the example. 
 * @param filename The name of the example file to check. 
 *
 * @return 0 if example file is correct, non-zero otherwise. */
int check_file(int ntasks, char *filename) {
    
    int ncid;         /**< File ID from netCDF. */
    int ndims;        /**< Number of dimensions. */
    int nvars;        /**< Number of variables. */
    int ngatts;       /**< Number of global attributes. */
    int unlimdimid;   /**< ID of unlimited dimension. */
    size_t dimlen;    /**< Length of the dimension. */
    int natts;        /**< Number of variable attributes. */
    nc_type xtype;    /**< NetCDF data type of this variable. */
    int ret;          /**< Return code for function calls. */
    int dimids[NDIM]; /**< Dimension ids for this variable. */
    char dim_name[NC_MAX_NAME];   /**< Name of the dimension. */
    char var_name[NC_MAX_NAME];   /**< Name of the variable. */
    size_t start[NDIM];           /**< Zero-based index to start read. */
    size_t count[NDIM];           /**< Number of elements to read. */
    int buffer[DIM_LEN];          /**< Buffer to read in data. */
    int expected[DIM_LEN];        /**< Data values we expect to find. */
    
    /* Open the file. */
    if ((ret = nc_open(filename, 0, &ncid)))
	return ret;

    /* Check the metadata. */
    if ((ret = nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)))
	return ret;
    if (ndims != NDIM || nvars != 1 || ngatts != 0 || unlimdimid != -1)
	return ERR_BAD;
    if ((ret = nc_inq_dim(ncid, 0, dim_name, &dimlen)))
	return ret;
    if (dimlen != DIM_LEN || strcmp(dim_name, DIM_NAME))
	return ERR_BAD;
    if ((ret = nc_inq_var(ncid, 0, var_name, &xtype, &ndims, dimids, &natts)))
	return ret;
    if (xtype != NC_INT || ndims != NDIM || dimids[0] != 0 || natts != 0)
	return ERR_BAD;

    /* Use the number of processors to figure out what the data in the
     * file should look like. */
    int div = DIM_LEN/ntasks;
    for (int d = 0; d < DIM_LEN; d++)
	expected[d] = START_DATA_VAL + d/div;
    
    /* Check the data. */
    start[0] = 0;
    count[0] = DIM_LEN;
    if ((ret = nc_get_vara(ncid, 0, start, count, buffer)))
	return ret;
    for (int d = 0; d < DIM_LEN; d++)
	if (buffer[d] != expected[d])
	    return ERR_BAD;

    /* Close the file. */
    if ((ret = nc_close(ncid)))
	return ret;

    /* Everything looks good! */
    return 0;
}
コード例 #4
0
ファイル: InputNetCDFBuffer.cpp プロジェクト: sbfnk/LibBi
void bi::InputNetCDFBuffer::readTime(int ncVar, const long start,
    size_t* const len, real* const t) {
  /* pre-condition */
  BI_ASSERT(start >= 0);
  BI_ASSERT(len != NULL);
  BI_ASSERT(t != NULL);

  std::vector<size_t> offsets(2), counts(2);
  std::vector<int> dimids = nc_inq_vardimid(ncid, ncVar);
  real tnxt;
  int j = 0;
  size_t T;

  if (nsDim >= 0 && dimids[j] == nsDim) {
    /* optional ns dimension */
    offsets[j] = ns;
    counts[j] = 1;
    ++j;
  }
  BI_ASSERT(j < static_cast<int>(dimids.size()));
  T = nc_inq_dimlen(ncid, dimids[j]);
  offsets[j] = start;
  counts[j] = 1;
  //++j; // not here, need to hold ref to last offset

  /* may be multiple records with same time, keep reading until time changes */
  *len = 0;
  *t = 0.0;
  tnxt = 0.0;
  while (*t == tnxt && offsets[j] < T) {
    nc_get_vara(ncid, ncVar, offsets, counts, &tnxt);
    if (*len == 0) {
      *t = tnxt;
    }
    if (tnxt == *t) {
      ++offsets[j];
      ++(*len);
    }
  }
}
コード例 #5
0
ファイル: v2i.c プロジェクト: stcorp/harp
/*
 * Read one record's worth of data, except don't read from variables for which
 * the address of the data to be read is null.  Return -1 on error.  This is
 * the same as the ncrecget() in the library, except that can handle errors
 * better.
 */
int
nc_get_rec(
	int ncid,
	size_t recnum,
	void **datap)
{
    int status;
    int varid;
    int rvarids[MAX_NC_VARS];
    int nrvars;
    size_t start[MAX_NC_DIMS];
    size_t edges[MAX_NC_DIMS];

    status = numrecvars(ncid, &nrvars, rvarids);
    if(status != NC_NOERR)
	return status;

    if (nrvars == 0)
      return NC_NOERR;

    start[0] = recnum;
    for (varid = 1; varid < nrvars; varid++)
	start[varid] = 0;

    for (varid = 0; varid < nrvars; varid++) {
	if (datap[varid] != NULL) {
	    status = dimsizes(ncid, rvarids[varid], edges);
	    if(status != NC_NOERR)
		return status;
	    edges[0] = 1;		/* only 1 record's worth */
	    status = nc_get_vara(ncid, rvarids[varid], start, edges, datap[varid]);
	    if(status != NC_NOERR)
		return status;
	}
    }    
    return 0;
}
コード例 #6
0
ファイル: tst_fillbug.c プロジェクト: Federico2014/edg4x-rose
int
main(int argc, char **argv) 
{/* create file that caused seg fault in ncdump */

    int  ncid;  /* netCDF id */

    /* dimension ids */
    int Time_dim;
    int X_dim;
    int Y_dim;

    /* dimension lengths */
    size_t Time_len = NC_UNLIMITED;
    size_t X_len = 4;
    size_t Y_len = 3;

    /* variable ids */
    int Time_id;
    int P_id;

    /* rank (number of dimensions) for each variable */
#   define RANK_Time 1
#   define RANK_P 3

    /* variable shapes */
    int Time_dims[RANK_Time];
    int P_dims[RANK_P];

   printf("\n*** Testing preparation of fillbug test.\n");
   printf("*** creating fillbug test file %s...", FILENAME);

    /* enter define mode */
    if (nc_create(FILENAME, NC_CLOBBER|NC_NETCDF4, &ncid)) ERR;

    /* define dimensions */
    if (nc_def_dim(ncid, "Time", Time_len, &Time_dim)) ERR;
    if (nc_def_dim(ncid, "X", X_len, &X_dim)) ERR;
    if (nc_def_dim(ncid, "Y", Y_len, &Y_dim)) ERR;

    /* define variables */

    Time_dims[0] = Time_dim;
    if (nc_def_var(ncid, "Time", NC_DOUBLE, RANK_Time, Time_dims, &Time_id)) ERR;

    P_dims[0] = Time_dim;
    P_dims[1] = Y_dim;
    P_dims[2] = X_dim;
    if (nc_def_var(ncid, "P", NC_FLOAT, RANK_P, P_dims, &P_id)) ERR;

    /* leave define mode */
    if (nc_enddef (ncid)) ERR;

    {/* assign variable data */
    static double Time_data[1]={3.14159};
    static size_t Time_startset[1] = {0};
    static size_t Time_countset[1] = {1};
    if (nc_put_vara(ncid, Time_id, Time_startset, Time_countset, Time_data)) ERR;
    }

    if (nc_close(ncid)) ERR;

    /* Try to duplicate segfault ncdump gets by making the same calls
     * to the netCDF-4 library, in the same order.  This doesn't
     * result in the same segfault, so either we have missed a call
     * made by ncdump, or an earlier ncdump bug masks the real problem
     * until a call is made into the netCDF-4 library ...  */
    if (nc_open(FILENAME, NC_NOWRITE, &ncid)) ERR;
    
    {   		
	/* We declare local arrays with small constant sizes to avoid
	 * all the mallocs and frees used in ncdump.  For the example
	 * above, the fixed-size arrays are ample. */
	int format, ndims, nvars, ngatts, xdimid, ndims_grp, dimids_grp[3],
	    unlimids[1], d_grp, nunlim, nvars_grp, varids_grp[3], v_grp,
	    varid, varndims, vardims[3], varnatts, vartype, dimids[3], is_recvar,
	    vdims[3], id, ntypes, numgrps, atttype, nc_status;
	size_t dimsize, len, attlen;
	char dimname[20], varname[20];
	if ( nc_inq_format(ncid, &format)) ERR;
	ntypes = count_udtypes(ncid);
	if ( nc_inq_typeids(ncid, &ntypes, NULL) ) ERR;
	if ( nc_inq_format(ncid, &format)) ERR;
	if ( nc_inq_grps(ncid, &numgrps, NULL) ) ERR;
	if ( nc_inq_typeids(ncid, &ntypes, NULL) ) ERR;
	if ( nc_inq(ncid, &ndims, &nvars, &ngatts, &xdimid) ) ERR;
	if ( nc_inq_ndims(ncid, &ndims_grp) ) ERR;
	if ( nc_inq_dimids(ncid, 0, dimids_grp, 0) ) ERR;
	if ( nc_inq_unlimdims(ncid, &nunlim, NULL) ) ERR;
	if ( nc_inq_unlimdims(ncid, &nunlim, unlimids) ) ERR;
	for (d_grp = 0; d_grp < ndims_grp; d_grp++) {
	    int dimid = dimids_grp[d_grp];
	    if ( nc_inq_dim(ncid, dimid, dimname, &dimsize) ) ERR;
	}
	if ( nc_inq_format(ncid, &format) ) ERR;
	if ( nc_inq_varids(ncid, &nvars_grp, varids_grp) ) ERR;
	for (v_grp = 0; v_grp < nvars_grp; v_grp++) {
	    varid = varids_grp[v_grp];
	    if ( nc_inq_varndims(ncid, varid, &varndims) ) ERR;
	    if ( nc_inq_var(ncid, varid, varname, &vartype, 0, vardims, 
			    &varnatts) ) ERR;
	    for (id = 0; id < varndims; id++) {
		if ( nc_inq_dimname(ncid, vardims[id], dimname) ) ERR;
	    }
	}
	for (v_grp = 0; v_grp < nvars_grp; v_grp++) {
	    varid = varids_grp[v_grp];
	    if( nc_inq_varndims(ncid, varid, &varndims) ) ERR;
	    if( nc_inq_var(ncid, varid, varname, &vartype, 0, vardims, 
			   &varnatts) ) ERR;
	    {
		is_recvar = 0;
		if ( nc_inq_varndims(ncid, varid, &ndims) ) ERR;
		if (ndims > 0) {
		    int nunlimdims;
		    int recdimids[3];
		    int dim, recdim;
		    if ( nc_inq_vardimid(ncid, varid, dimids) ) ERR;
		    if ( nc_inq_unlimdims(ncid, &nunlimdims, NULL) ) ERR;
		    if ( nc_inq_unlimdims(ncid, NULL, recdimids) ) ERR;
		    for (dim = 0; dim < ndims && is_recvar == 0; dim++) {
			for(recdim = 0; recdim < nunlimdims; recdim++) {
			    if(dimids[dim] == recdimids[recdim]) {
				is_recvar = 1;
				break;
			    }		
			}
		    }
		}
	    }
	    for (id = 0; id < varndims; id++) {
		if( nc_inq_dimlen(ncid, vardims[id], &len) ) ERR;
		vdims[id] = len;
	    }
	    nc_status = nc_inq_att(ncid,varid,_FillValue,&atttype,&attlen);
	    nc_status = nc_inq_att(ncid, varid, "units", &atttype, &attlen);
	    nc_status = nc_inq_att(ncid, varid, "C_format", &atttype, &attlen);
	    if (varid == 0) {
		/* read Time variable */
		static double Time_data;
		static size_t cor[RANK_Time] = {0};
		static size_t edg[RANK_Time] = {1};
		if (nc_get_vara(ncid, varid, cor, edg, &Time_data)) ERR;
	    } else {
		/* read data slices from P variable, should get fill values */
		static float P_data[4];
		static size_t cor[RANK_P] = {0, 0, 0};
		static size_t edg[RANK_P] = {1, 1, 4};

		/* first slice retrieved OK */
		if (nc_get_vara(ncid, varid, cor, edg, P_data)) ERR;
		
		/* In ncdump, reading second slice gets seg fault in
		 * nc4_open_var_grp(), but this attempt to do all the
		 * same netCDF calls as ncdump can't duplicate the
		 * error, which would seem to implicate ncdump rather
		 * than HDF5 or netCDF-4 library ... */
		cor[1] = 1;
		if (nc_get_vara(ncid, varid, cor, edg, P_data)) ERR;
	    }
	}
    }
    if (nc_close(ncid)) ERR;
      
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
コード例 #7
0
ファイル: tst_string_data.c プロジェクト: mmase/wgrib2
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   
}
コード例 #8
0
ファイル: vardata.c プロジェクト: ArtisticCoding/libmesh
/* Output the data for a single variable, in NcML syntax.
 *  TODO: currently not called, need option for NcML with values ... */
int
vardatax(
     const ncvar_t *vp,		/* variable */
     size_t vdims[],		/* variable dimension sizes */
     int ncid,			/* netcdf id */
     int varid			/* variable id */
     )
{
    size_t *cor;	     /* corner coordinates */
    size_t *edg;	     /* edges of hypercube */
    size_t *add;	     /* "odometer" increment to next "row"  */
    void *vals;

    int id;
    int ir;
    size_t nels;
    size_t ncols;
    size_t nrows;
    int vrank = vp->ndims;

    cor = (size_t *) emalloc((vrank + 1) * sizeof(size_t));
    edg = (size_t *) emalloc((vrank + 1) * sizeof(size_t));
    add = (size_t *) emalloc((vrank + 1) * sizeof(size_t));

    nels = 1;
    for (id = 0; id < vrank; id++) {
	cor[id] = 0;
	edg[id] = 1;
	nels *= vdims[id];	/* total number of values for variable */
    }

    printf("    <values>\n     ");
    set_indent (7);

    if (vrank < 1) {
	ncols = 1;
    } else {
	ncols = vdims[vrank-1];	/* size of "row" along last dimension */
	edg[vrank-1] = vdims[vrank-1];
	for (id = 0; id < vrank; id++)
	  add[id] = 0;
	if (vrank > 1)
	  add[vrank-2] = 1;
    }
    nrows = nels/ncols;		/* number of "rows" */
    vals = emalloc(ncols * vp->tinfo->size);

    for (ir = 0; ir < nrows; ir++) {
	size_t corsav;
	bool_t lastrow;

	if (vrank > 0) {
	    corsav = cor[vrank-1];
	}
	lastrow = (bool_t)(ir == nrows-1);

	if (vrank > 0)
	    edg[vrank-1] = ncols;
	NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals) );
	/* Test if we should treat array of chars as a string  */
	if(vp->type == NC_CHAR &&
	   (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) {
	    pr_tvalsx(vp, ncols, 0, lastrow, (char *) vals);
	} else {
	    pr_any_valsx(vp, ncols, 0, lastrow, vals);
	}

	if (vrank > 0)
	    cor[vrank-1] += ncols;

	if (vrank > 0)
	  cor[vrank-1] = corsav;
	if (ir < nrows-1)
	  if (!upcorner(vdims,vp->ndims,cor,add))
	    error("vardata: odometer overflowed!");
	set_indent(2);
    }
    printf(" </values>\n");
    free(vals);
    free(cor);
    free(edg);
    free(add);
    return 0;
}
コード例 #9
0
ファイル: tst_chunks3.c プロジェクト: ArtisticCoding/libmesh
/* compare contiguous, chunked, and compressed performance */
int
main(int argc, char *argv[]) {

    int  stat;  /* return status */
    int  ncid;  /* netCDF id */
    int i, j, k;
    int dim1id, dim2id, dim3id;
    int varid_g;		  /* varid for contiguous */
    int varid_k;		  /* varid for chunked */
    int varid_x;		  /* varid for compressed */

    float *varxy, *varxz, *varyz;    /* 2D memory slabs used for I/O */
    int mm;
    size_t dims[] = {256, 256, 256}; /* default dim lengths */
    size_t chunks[] = {32, 32, 32}; /* default chunk sizes */
    size_t start[3], count[3];
    float contig_time, chunked_time, compressed_time, ratio;
    int deflate_level = 1;	/* default compression level, 9 is
				 * better and slower.  If negative,
				 * turn on shuffle filter also. */
    int shuffle = NC_NOSHUFFLE;
    size_t cache_size_def;
    size_t cache_hash_def;
    float cache_pre_def;
    size_t cache_size = 0;	    /* use library default */
    size_t cache_hash = 0;	    /* use library default */
    float cache_pre = -1.0f;	    /* use library default */

    /* rank (number of dimensions) for each variable */
#   define RANK_var1 3

    /* variable shapes */
    int var_dims[RANK_var1];

    TIMING_DECLS(TMsec) ;

    /* From args, get parameters for timing, including variable and
       chunk sizes.  Negative deflate level means also use shuffle
       filter. */
    parse_args(argc, argv, &deflate_level, &shuffle, dims,
	       chunks, &cache_size, &cache_hash, &cache_pre);

    /* get cache defaults, then set cache parameters that are not default */
    if((stat = nc_get_chunk_cache(&cache_size_def, &cache_hash_def,
				   &cache_pre_def)))
	ERR1(stat);
    if(cache_size == 0)
	cache_size = cache_size_def;
    if(cache_hash == 0)
	cache_hash = cache_hash_def;
    if(cache_pre == -1.0f)
	cache_pre = cache_pre_def;
    if((stat = nc_set_chunk_cache(cache_size, cache_hash, cache_pre)))
	ERR1(stat);
    printf("cache: %3.2f MBytes  %ld objs  %3.2f preempt, ",
	   cache_size/1.e6, cache_hash, cache_pre);

    if(deflate_level == 0) {
	printf("uncompressed        ");
    } else {
	printf("compression level %d", deflate_level);
    }
    if(shuffle == 1) {
	printf(", shuffled");
    }
    printf("\n\n");

    /* initialize 2D slabs for writing along each axis with phony data */
    varyz = (float *) emalloc(sizeof(float) * 1 * dims[1] * dims[2]);
    varxz = (float *) emalloc(sizeof(float) * dims[0] * 1 * dims[2]);
    varxy = (float *) emalloc(sizeof(float) * dims[0] * dims[1] * 1);
    mm = 0;
    for(j = 0; j < dims[1]; j++) {
	for(k = 0; k < dims[2]; k++) {
	    varyz[mm++] = k + dims[2]*j;
	}
    }
    mm = 0;
    for(i = 0; i < dims[0]; i++) {
	for(k = 0; k < dims[2]; k++) {
	    varxz[mm++] = k + dims[2]*i;
	}
    }
    mm = 0;
    for(i = 0; i < dims[0]; i++) {
	for(j = 0; j < dims[1]; j++) {
	    varxy[mm++] = j + dims[1]*i;
	}
    }

    if((stat = nc_create(FILENAME, NC_NETCDF4 | NC_CLASSIC_MODEL, &ncid)))
	ERR1(stat);

    /* define dimensions */
    if((stat = nc_def_dim(ncid, "dim1", dims[0], &dim1id)))
	ERR1(stat);
    if((stat = nc_def_dim(ncid, "dim2", dims[1], &dim2id)))
	ERR1(stat);
    if((stat = nc_def_dim(ncid, "dim3", dims[2], &dim3id)))
	ERR1(stat);

    /* define variables */
    var_dims[0] = dim1id;
    var_dims[1] = dim2id;
    var_dims[2] = dim3id;
    if((stat = nc_def_var(ncid, "var_contiguous", NC_FLOAT, RANK_var1,
			   var_dims, &varid_g)))
	ERR1(stat);
    if((stat = nc_def_var(ncid, "var_chunked", NC_FLOAT, RANK_var1,
			   var_dims, &varid_k)))
	ERR1(stat);
    if((stat = nc_def_var(ncid, "var_compressed", NC_FLOAT, RANK_var1,
			   var_dims, &varid_x)))
	ERR1(stat);

    if((stat = nc_def_var_chunking(ncid, varid_g, NC_CONTIGUOUS, 0)))
	ERR1(stat);

    if((stat = nc_def_var_chunking(ncid, varid_k, NC_CHUNKED, chunks)))
	ERR1(stat);

    if((stat = nc_def_var_chunking(ncid, varid_x, NC_CHUNKED, chunks)))
	ERR1(stat);

    if (deflate_level != 0) {
	if((stat = nc_def_var_deflate(ncid, varid_x, shuffle,
				       NC_COMPRESSED, deflate_level)))
	    ERR1(stat);
    }

    /* leave define mode */
    if((stat = nc_enddef (ncid)))
	ERR1(stat);

    /* write each variable one yz slab at a time */
    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    count[0] = 1;
    count[1] = dims[1];
    count[2] = dims[2];

    sprintf(time_mess,"  contiguous write %3ld %3ld %3ld",
	    1, dims[1], dims[2]);
    TIMING_START ;
    for(i = 0; i < dims[0]; i++) {
	start[0] = i;
	if((stat = nc_put_vara(ncid, varid_g, start, count, &varyz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    printf("\n");
    contig_time = TMsec;

    sprintf(time_mess,"  chunked    write %3ld %3ld %3ld  %3ld %3ld %3ld",
	    1, dims[1], dims[2], chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[0]; i++) {
	start[0] = i;
	if((stat = nc_put_vara(ncid, varid_k, start, count, &varyz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    chunked_time = TMsec;
    ratio = contig_time/chunked_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);

    sprintf(time_mess,"  compressed write %3ld %3ld %3ld  %3ld %3ld %3ld",
	    1, dims[1], dims[2], chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[0]; i++) {
	start[0] = i;
	if((stat = nc_put_vara(ncid, varid_x, start, count, &varyz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    compressed_time = TMsec;
    ratio = contig_time/compressed_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);
    printf("\n");

    /* write each variable one xz slab at a time */
    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    count[0] = dims[0];
    count[1] = 1;
    count[2] = dims[2];

    sprintf(time_mess,"  contiguous write %3ld %3ld %3ld",
	    dims[0], 1, dims[2]);
    TIMING_START ;
    for(i = 0; i < dims[1]; i++) {
	start[1] = i;
	if((stat = nc_put_vara(ncid, varid_g, start, count, &varxz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    printf("\n");
    contig_time = TMsec;

    sprintf(time_mess,"  chunked    write %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[1]; i++) {
	start[1] = i;
	if((stat = nc_put_vara(ncid, varid_k, start, count, &varxz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    chunked_time = TMsec;
    ratio = contig_time/chunked_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);

    sprintf(time_mess,"  compressed write %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[1]; i++) {
	start[1] = i;
	if((stat = nc_put_vara(ncid, varid_x, start, count, &varxz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    compressed_time = TMsec;
    ratio = contig_time/compressed_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);
    printf("\n");

    /* write each variable one xy slab at a time */
    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    count[0] = dims[0];
    count[1] = dims[1];
    count[2] = 1;

    sprintf(time_mess,"  contiguous write %3ld %3ld %3ld",
	    dims[0], dims[1], 1);
    TIMING_START ;
    for(i = 0; i < dims[2]; i++) {
	start[2] = i;
	if((stat = nc_put_vara(ncid, varid_g, start, count, &varxy[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    printf("\n");
    contig_time = TMsec;

    sprintf(time_mess,"  chunked    write %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[2]; i++) {
	start[2] = i;
	if((stat = nc_put_vara(ncid, varid_k, start, count, &varxy[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    chunked_time = TMsec;
    ratio = contig_time/chunked_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);

    sprintf(time_mess,"  compressed write %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[2]; i++) {
	start[2] = i;
	if((stat = nc_put_vara(ncid, varid_x, start, count, &varxy[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    compressed_time = TMsec;
    ratio = contig_time/compressed_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);
    printf("\n");

    /* read each variable one yz slab at a time */
    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    count[0] = 1;
    count[1] = dims[1];
    count[2] = dims[2];

    sprintf(time_mess,"  contiguous  read %3ld %3ld %3ld",
	    1, dims[1], dims[2]);
    TIMING_START ;
    for(i = 0; i < dims[0]; i++) {
	start[0] = i;
	if((stat = nc_get_vara(ncid, varid_g, start, count, &varyz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    printf("\n");
    contig_time = TMsec;

    sprintf(time_mess,"  chunked     read %3ld %3ld %3ld  %3ld %3ld %3ld",
	    1, dims[1], dims[2] , chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[0]; i++) {
	start[0] = i;
	if((stat = nc_get_vara(ncid, varid_k, start, count, &varyz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    chunked_time = TMsec;
    ratio = contig_time/chunked_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);

    sprintf(time_mess,"  compressed  read %3ld %3ld %3ld  %3ld %3ld %3ld",
	    1, dims[1], dims[2] , chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[0]; i++) {
	start[0] = i;
	if((stat = nc_get_vara(ncid, varid_x, start, count, &varyz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    compressed_time = TMsec;
    ratio = contig_time/compressed_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);
    printf("\n");

    /* read each variable one xz slab at a time */
    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    count[0] = dims[0];
    count[1] = 1;
    count[2] = dims[2];

    sprintf(time_mess,"  contiguous  read %3ld %3ld %3ld",
	    dims[0], 1, dims[2]);
    TIMING_START ;
    for(i = 0; i < dims[1]; i++) {
	start[1] = i;
	if((stat = nc_get_vara(ncid, varid_g, start, count, &varxz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    printf("\n");
    contig_time = TMsec;

    sprintf(time_mess,"  chunked     read %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[1]; i++) {
	start[1] = i;
	if((stat = nc_get_vara(ncid, varid_k, start, count, &varxz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    chunked_time = TMsec;
    ratio = contig_time/chunked_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);

    sprintf(time_mess,"  compressed  read %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], 1, dims[2], chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[1]; i++) {
	start[1] = i;
	if((stat = nc_get_vara(ncid, varid_x, start, count, &varxz[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    compressed_time = TMsec;
    ratio = contig_time/compressed_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);
    printf("\n");

    /* read variable one xy slab at a time */
    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    count[0] = dims[0];
    count[1] = dims[1];
    count[2] = 1;

    sprintf(time_mess,"  contiguous  read %3ld %3ld %3ld",
	    dims[0], dims[1], 1);
    TIMING_START ;
    for(i = 0; i < dims[2]; i++) {
	start[2] = i;
	if((stat = nc_get_vara(ncid, varid_g, start, count, &varxy[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    printf("\n");
    contig_time = TMsec;

    sprintf(time_mess,"  chunked     read %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[2]; i++) {
	start[2] = i;
	if((stat = nc_get_vara(ncid, varid_k, start, count, &varxy[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    chunked_time = TMsec;
    ratio = contig_time/chunked_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);

    sprintf(time_mess,"  compressed  read %3ld %3ld %3ld  %3ld %3ld %3ld",
	    dims[0], dims[1], 1, chunks[0], chunks[1], chunks[2]);
    TIMING_START ;
    for(i = 0; i < dims[2]; i++) {
	start[2] = i;
	if((stat = nc_get_vara(ncid, varid_x, start, count, &varxy[0])))
	    ERR1(stat);
    }
    TIMING_END(TMsec) ;
    compressed_time = TMsec;
    ratio = contig_time/compressed_time;
    if(ratio >= 1.0)
	printf(" %5.2g x faster\n", ratio);
    else
	printf(" %5.2g x slower\n", 1.0/ratio);

    if((stat = nc_close(ncid)))
	ERR1(stat);

    return 0;
}
コード例 #10
0
ファイル: vardata.c プロジェクト: ArtisticCoding/libmesh
/*  Print data values for variable varid.
 *
 * Recursive to handle possibility of variables with multiple
 * unlimited dimensions, for which the CDL syntax requires use of "{"
 * and "}" in data section to disambiguate the size of nested records
 * in a simple linear list of values.
 */
static int
print_rows(
    int level,          /* 0 at top-level, incremented for each recursive level */
    int ncid,		/* netcdf id */
    int varid,		/* variable id */
    const ncvar_t *vp,	/* variable */
    size_t ncols,	/* number of values in a row */
    int rank,	       	/* number of elements in following 3 arrays  */
    size_t vdims[],    	/* variable dimension sizes */
    size_t cor[],      	/* corner coordinates */
    size_t edg[],      	/* edges of hypercube */
    void *vals,   	/* allocated buffer for ncols values in a row */
    int marks_pending	/* number of pending closing "}" record markers */
    )
{
    int d0 = 0;
    size_t inc = 1;
    int i;
    bool_t mark_record = (level > 0 && is_unlim_dim(ncid, vp->dims[level]));
    safebuf_t *sb = sbuf_new();
    if (rank > 0)
	d0 = vdims[level];
    for(i = level + 1; i < rank; i++) {
	inc *= vdims[i];
    }
    if(mark_record) { /* the whole point of this recursion is printing these "{}" */
	lput("{");
	marks_pending++;	/* matching "}"s to emit after last "row" */
    }
    if(rank - level > 1) {     	/* this level is just d0 next levels */
	size_t *local_cor = emalloc((rank + 1) * sizeof(size_t));
	size_t *local_edg = emalloc((rank + 1) * sizeof(size_t));
	for(i = 0; i < rank; i++) {
	    local_cor[i] = cor[i];
	    local_edg[i] = edg[i];
	}
	local_cor[level] = 0;
	local_edg[level] = 1;
	for(i = 0; i < d0 - 1; i++) {
	    print_rows(level + 1, ncid, varid, vp, ncols, rank, vdims,
		       local_cor, local_edg, vals, 0);
	    local_cor[level] += 1;
	}
	print_rows(level + 1, ncid, varid, vp, ncols, rank, vdims,
		   local_cor, local_edg, vals, marks_pending);
	free(local_edg);
	free(local_cor);
    } else {			/* bottom out of recursion */
	char *valp = vals;
	bool_t lastrow;
	int j;
	if(formatting_specs.brief_data_cmnts && rank > 1) {
	    annotate_brief(vp, cor, vdims);
	}
	NC_CHECK(nc_get_vara(ncid, varid, cor, edg, (void *)valp));
	for(i=0; i < d0 - 1; i++) {
	    print_any_val(sb, vp, (void *)valp);
	    valp += vp->tinfo->size; /* next value according to type */
	    if (formatting_specs.full_data_cmnts) {
		printf("%s, ", sb->buf);
		annotate (vp, cor, i);
	    } else {
		sbuf_cat(sb, ", ");
		lput(sbuf_str(sb));
	    }
	}
	print_any_val(sb, vp, (void *)valp);
	/* determine if this is the last row */
	lastrow = true;
	for(j = 0; j < rank - 1; j++) {
	    if (cor[j] != vdims[j] - 1) {
		lastrow = false;
		break;
	    }
	}
	if (formatting_specs.full_data_cmnts) {
	    for (j = 0; j < marks_pending; j++) {
		sbuf_cat(sb, "}");
	    }
	    printf("%s", sbuf_str(sb));
	    lastdelim (0, lastrow);
	    annotate (vp, cor, i);
	} else {
	    for (j = 0; j < marks_pending; j++) {
		sbuf_cat(sb, "}");
	    }
	    lput(sbuf_str(sb));
	    lastdelim2 (0, lastrow);
	}
    }
    sbuf_free(sb);
    return NC_NOERR;
}
コード例 #11
0
ファイル: vardata.c プロジェクト: ArtisticCoding/libmesh
/* Output the data for a single variable, in CDL syntax. */
int
vardata(
     const ncvar_t *vp,		/* variable */
     size_t vdims[],		/* variable dimension sizes */
     int ncid,			/* netcdf id */
     int varid			/* variable id */
     )
{
    size_t *cor;	     /* corner coordinates */
    size_t *edg;	     /* edges of hypercube */
    size_t *add;	     /* "odometer" increment to next "row"  */
    void *vals;

    int id;
    int ir;
    size_t nels;
    size_t ncols;
    size_t nrows;
    int vrank = vp->ndims;

    cor = (size_t *) emalloc((1 + vrank) * sizeof(size_t));
    edg = (size_t *) emalloc((1 + vrank) * sizeof(size_t));
    add = (size_t *) emalloc((1 + vrank) * sizeof(size_t));

    nels = 1;
    if(vrank == 0) { /*scalar*/
	cor[0] = 0;
	edg[0] = 1;
    } else {
	for (id = 0; id < vrank; id++) {
	    cor[id] = 0;
	    edg[id] = 1;
	    nels *= vdims[id];	/* total number of values for variable */
	}
    }
    printf("\n");
    indent_out();
    printf(" ");
    print_name(vp->name);
    if (vrank <= 1) {
	printf(" = ");
	set_indent ((int)strlen(vp->name) + 4 + indent_get());
    } else {
	printf(" =\n  ");
	set_indent (2 + indent_get());
    }

    if (vrank == 0) {
	ncols = 1;
    } else {
	ncols = vdims[vrank-1];	/* size of "row" along last dimension */
	edg[vrank-1] = ncols;
	for (id = 0; id < vrank; id++)
	  add[id] = 0;
	if (vrank > 1)
	  add[vrank-2] = 1;
    }
    nrows = nels/ncols;		/* number of "rows" */
    vals = emalloc(ncols * vp->tinfo->size);

    /* Test if we should treat array of chars as a string  */
    if(vp->type == NC_CHAR && (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) {
	for (ir = 0; ir < nrows; ir++) {
	    if (vrank > 0) {
		if (formatting_specs.brief_data_cmnts != false && vrank > 1 && ncols > 0) {
		    annotate_brief(vp, cor, vdims);
		}
	    }
	    NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals));
	    pr_tvals(vp, ncols, (ir == nrows-1), (char *) vals, cor);
	    if (ir < nrows-1)
	      if (!upcorner(vdims, vp->ndims, cor, add))
	        error("vardata: odometer overflowed!");
	    set_indent(2);
	}
    } else {
	int level = 0;
	int rank = vp->ndims;
	int marks_pending = 0;
	NC_CHECK(print_rows(level, ncid, varid, vp, ncols, rank, vdims, cor, edg,
			    vals, marks_pending));
    }
    free(vals);
    free(cor);
    free(edg);
    free(add);

    return 0;
}
コード例 #12
0
int
main(int argc, char **argv)
{
   printf("\n*** Testing HDF4/NetCDF-4 interoperability...\n");
   printf("*** testing that netCDF can read a HDF4 file with some ints...");
   {
#define PRES_NAME "pres"
#define LAT_LEN 3
#define LON_LEN 2
#define DIMS_2 2

      int32 sd_id, sds_id;
      int32 dim_size[DIMS_2] = {LAT_LEN, LON_LEN};
      int32 start[DIMS_2] = {0, 0}, edge[DIMS_2] = {LAT_LEN, LON_LEN};
      int ncid, nvars_in, ndims_in, natts_in, unlimdim_in;
      size_t len_in;
      int data_out[LAT_LEN][LON_LEN], data_in[LAT_LEN][LON_LEN];
      size_t nstart[DIMS_2] = {0, 0}, ncount[DIMS_2] = {LAT_LEN, LON_LEN};
      size_t nindex[DIMS_2] = {0, 0};
      int scalar_data_in = 0;
      int i, j;

      /* Create some data. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    data_out[i][j] = j;

      /* Create a file with one SDS, containing our phony data. */
      sd_id = SDstart(FILE_NAME, DFACC_CREATE);
      sds_id = SDcreate(sd_id, PRES_NAME, DFNT_INT32, DIMS_2, dim_size);
      if (SDwritedata(sds_id, start, NULL, edge, (void *)data_out)) ERR;
      if (SDendaccess(sds_id)) ERR;
      if (SDend(sd_id)) ERR;

      /* Now open with netCDF and check the contents. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; 
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdim_in)) ERR; 
      if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdim_in != -1) ERR;
      if (nc_inq_dim(ncid, 0, NULL, &len_in)) ERR;
      if (len_in != LAT_LEN) ERR;
      if (nc_inq_dim(ncid, 1, NULL, &len_in)) ERR;
      if (len_in != LON_LEN) ERR;
      
      /* Read the data through a vara function from the netCDF API. */
      if (nc_get_vara(ncid, 0, nstart, ncount, data_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    if (data_in[i][j] != data_out[i][j]) ERR;

      /* Reset for next test. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    data_in[i][j] = -88;

      /* Read the data through a vara_int function from the netCDF API. */
      if (nc_get_vara_int(ncid, 0, nstart, ncount, data_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    if (data_in[i][j] != data_out[i][j]) ERR;

      /* Reset for next test. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    data_in[i][j] = -88;

      /* Read the data through a var_int function from the netCDF API. */
      if (nc_get_var_int(ncid, 0, data_in)) ERR;
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	    if (data_in[i][j] != data_out[i][j]) ERR;

      /* Read the data through a var1 function from the netCDF API. */
      for (i = 0; i < LAT_LEN; i++)
	 for (j = 0; j < LON_LEN; j++)
	 {
	    nindex[0] = i;
	    nindex[1] = j;
	    if (nc_get_var1(ncid, 0, nindex, &scalar_data_in)) ERR;
	    if (scalar_data_in != data_out[i][j]) ERR;
	    scalar_data_in = -88; /* reset */
	    if (nc_get_var1_int(ncid, 0, nindex, &scalar_data_in)) ERR;
	    if (scalar_data_in != data_out[i][j]) ERR;
	 }

      if (nc_close(ncid)) ERR; 
   }
   SUMMARIZE_ERR;
   printf("*** testing with a more complex HDF4 file...");
   {
#define Z_LEN 3
#define Y_LEN 2
#define X_LEN 5
#define DIMS_3 3
#define NUM_TYPES 8

      int32 sd_id, sds_id;
      int32 dim_size[DIMS_3] = {Z_LEN, Y_LEN, X_LEN};
      int dimids_in[DIMS_3];
      int ncid, nvars_in, ndims_in, natts_in, unlimdim_in;
      size_t len_in;
      nc_type type_in;
      int hdf4_type[NUM_TYPES] = {DFNT_FLOAT32, DFNT_FLOAT64, 
				  DFNT_INT8, DFNT_UINT8, DFNT_INT16, 
				  DFNT_UINT16, DFNT_INT32, DFNT_UINT32};
      int netcdf_type[NUM_TYPES] = {NC_FLOAT, NC_DOUBLE, 
				  NC_BYTE, NC_UBYTE, NC_SHORT, 
				  NC_USHORT, NC_INT, NC_UINT};
      char tmp_name[NC_MAX_NAME + 1], name_in[NC_MAX_NAME + 1];
      char dim_name[NC_MAX_NAME + 1][DIMS_3] = {"z", "y", "x"};
      int d, t;

      /* Create a HDF4 SD file. */
      sd_id = SDstart (FILE_NAME, DFACC_CREATE);

      /* Create some HDF4 datasets. */
      for (t = 0; t < NUM_TYPES; t++)
      {
	 sprintf(tmp_name, "hdf4_dataset_type_%d", t);
	 if ((sds_id = SDcreate(sd_id, tmp_name, hdf4_type[t], 
				DIMS_3, dim_size)) == FAIL) ERR;	    
	 /* Set up dimensions. By giving them the same names for each
	  * dataset, I am specifying that they are shared
	  * dimensions. */
	 for (d = 0; d < DIMS_3; d++)
	 {
	    int32 dimid;
	    if ((dimid = SDgetdimid(sds_id, d)) == FAIL) ERR;
	    if (SDsetdimname(dimid, dim_name[d])) ERR;
	 }
	 if (SDendaccess(sds_id)) ERR;
      }
      if (SDend(sd_id)) ERR;

      /* Open the file with netCDF and check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdim_in)) ERR;
      if (ndims_in != DIMS_3 || nvars_in != NUM_TYPES || natts_in != 0 || unlimdim_in != -1) ERR;
      if (nc_inq_dim(ncid, 0, NULL, &len_in)) ERR;
      if (len_in != Z_LEN) ERR;
      if (nc_inq_dim(ncid, 1, NULL, &len_in)) ERR;
      if (len_in != Y_LEN) ERR;
      if (nc_inq_dim(ncid, 2, NULL, &len_in)) ERR;
      if (len_in != X_LEN) ERR;
      for (t = 0; t < NUM_TYPES; t++)
      {
	 if (nc_inq_var(ncid, t, name_in, &type_in, &ndims_in, 
			dimids_in, &natts_in)) ERR;
	 if (type_in != netcdf_type[t] || ndims_in != DIMS_3 ||
	     dimids_in[0] != 0 || dimids_in[2] != 2 || dimids_in[2] != 2 || 
	     natts_in != 0) ERR;
      }
      
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
コード例 #13
0
int
main(int argc, char **argv)
{
    int status = NC_NOERR;
    int i,j,iv;
    unsigned int data[DATASIZE];
    size_t start[1];
    size_t count[1];
    Tag tag = Create; 
    int cmode = 0;
    int ncid;
    int dimids[1];
    void* memory;
    int nvars;
    int varids[4096];
    size_t varsize;
    size_t filesize;

    /* Get the specified var/file size */
    if(argc > 1) {
	filesize = atol(argv[1]);
    } else {
	if(sizeof(size_t) == 4)
	    filesize = 1000000000;
	else if(sizeof(size_t) == 8)
	    filesize = 3000000000;
	else {
	    fprintf(stderr,"Cannot compute filesize\n");
	    exit(1);
	}
    }

    /* Test that we can malloc that much space */
    memory = malloc(filesize);
    if(memory == NULL) {
        fprintf(stderr,"Cannot malloc %lu bytes\n",(unsigned long)filesize);
	exit(1);
    }
    free(memory);

    if(argc > 2) {
        if(strcmp(argv[2],"create")==0) tag = Create;
        else if(strcmp(argv[2],"creatediskless")==0) tag = CreateDiskless;
        else if(strcmp(argv[2],"open")==0) tag = Open;
        else if(strcmp(argv[2],"opendiskless")==0) tag = OpenDiskless;
	else {
	    fprintf(stderr,"illegal tag: %s",argv[2]);
	    exit(1);
	}
    } else
	tag = Create; /* default */
    
    switch (tag) {
    case Create: printf("\n*** Create file\n"); break;
    case CreateDiskless: printf("\n*** Create file diskless\n"); break;
    case Open: printf("\n*** Open file\n"); break;
    case OpenDiskless: printf("\n*** Open file diskless\n"); break;
    }

    switch (tag) {
    case Create:	  cmode = NC_CLOBBER; break;
    case CreateDiskless:  cmode = NC_CLOBBER|NC_DISKLESS|NC_WRITE; break;
    case Open:		  cmode = 0; break;
    case OpenDiskless:	  cmode = NC_DISKLESS; break;
    }

    switch (tag) {
    case Create:
    case CreateDiskless:
	/* Try to alloc as much as possible initially */
        if((status=nc__create(FILE_NAME, cmode, filesize, NULL, &ncid)))
	    REPORT;
        if((status=nc_set_fill(ncid, NC_NOFILL, NULL)))
	    REPORT;
	/* Only need 1 dimension */
        if((status=nc_def_dim(ncid, "dim", DIMMAX, &dimids[0])))
	    REPORT;
	break;
    case Open:
    case OpenDiskless:
        if((status=nc_open(FILE_NAME, cmode, &ncid)))
	    REPORT;
        if((status=nc_inq_dimid(ncid, "dim", &dimids[0])))
	    REPORT;
	break;
    }

    varsize = DIMMAX;
    nvars = filesize / varsize;
        assert((filesize % DIMMAX) == 0);
        assert(nvars < 4096);

    for(iv=0;iv<nvars;iv++) {
	char varname[32];
        sprintf(varname,"var%d",iv);
	switch (tag) {
        case Create:
        case CreateDiskless:
            if((status=nc_def_var(ncid, varname, NC_BYTE, 1, &dimids[0], &varids[iv])))
	        REPORT;
	break;
        case Open:
        case OpenDiskless:
            if((status=nc_inq_varid(ncid, varname, &varids[iv])))
	        REPORT;
	    break;
	}
    }

    if(tag == Create || tag == CreateDiskless) {
        if((status=nc_enddef(ncid)))
	    REPORT;
    }

    for(iv=0;iv<nvars;iv++) {
        size_t pieces = varsize/CHUNKSIZE;
        switch (tag) {
        case Create:
        case CreateDiskless:
	    /* Fill and put as integers */
	    for(i=0;i<pieces;i++) {
		start[0] = i*CHUNKSIZE;
		count[0] = CHUNKSIZE;
		for(j=0;j<DATASIZE;j++) data[j] = iv*((i*CHUNKSIZE)+j);
		if((status=nc_put_vara(ncid,varids[iv],start,count,(void*)data)))
		    REPORT;
	    }
	    break;
        case Open:
        case OpenDiskless:
	    /* Read the var contents and validate */
	    for(i=0;i<pieces;i++) {
		start[0] = i*CHUNKSIZE;
		count[0] = CHUNKSIZE;
		if((status=nc_get_vara(ncid,varids[iv],start,count,(void*)data)))
		    REPORT;
		for(j=0;j<DATASIZE;j++) {
		    unsigned int expected = iv*((i*CHUNKSIZE)+j);
	   	    if(data[j] != expected) {
		        printf("mismatch: iv=%d i=%u j=%u data=%u; should be %u\n",
				iv, i,j,data[j],expected);
		        err++;
		    }
		}
	    }
	    break;
	}
    }

    if((status=nc_close(ncid)))
	REPORT;

    SUMMARIZE_ERR;
    exit(0);
    return 0;
}
コード例 #14
0
int
main(int argc, char **argv)
{
   int mpi_namelen;
   char mpi_name[MPI_MAX_PROCESSOR_NAME];
   int mpi_size, mpi_rank;
   MPI_Comm comm = MPI_COMM_WORLD;
   MPI_Info info = MPI_INFO_NULL;
   double start_time = 0, total_time;
   int mpi_size_in;
#define NUM_TEST_TYPES 11
   nc_type test_type[NUM_TEST_TYPES] = {NC_BYTE, NC_CHAR, NC_SHORT, NC_INT, NC_FLOAT, NC_DOUBLE,
                                        NC_UBYTE, NC_USHORT, NC_UINT, NC_INT64, NC_UINT64};
   int tt, fv;
   int j, i, k, ret;

   /* Initialize MPI. */
   MPI_Init(&argc,&argv);
   MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
   MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
   MPI_Get_processor_name(mpi_name, &mpi_namelen);

   /* Must be able to evenly divide my slabs between processors. */
   if (NUM_SLABS % mpi_size)
   {
      if (!mpi_rank)
         printf("NUM_SLABS (%d) is not evenly divisible by mpi_size(%d)\n",
                NUM_SLABS, mpi_size);
      ERR;
   }

   if (!mpi_rank)
      printf("\n*** Testing parallel I/O some more.\n");

   /* Test for different fill value settings. */
   for (fv = 0; fv < NUM_FILL_TEST_RUNS; fv++)
   {
      /* Test for different netCDF types. */
      for (tt = 0; tt < NUM_TEST_TYPES; tt++)
      {
         char file_name[NC_MAX_NAME + 1];
         int fill_mode_in;
         void *data, *data_in;
         void *fill_value, *fill_value_in;
         size_t type_size;
         size_t write_start[NDIMS] = {0, 0, 1};
         size_t write_count[NDIMS] = {1, DIMSIZE, DIMSIZE - 1};
         size_t read_start[NDIMS] = {0, 0, 0};
         size_t read_count[NDIMS] = {1, DIMSIZE, DIMSIZE};
         int ncid, varid, dimids[NDIMS];
         int ndims_in, nvars_in, natts_in, unlimdimid_in;

         /* Fill values to be expected. */
         signed char byte_expected_fill_value;
         unsigned char char_expected_fill_value;
         short short_expected_fill_value;
         int int_expected_fill_value;
         float float_expected_fill_value;
         double double_expected_fill_value;
         unsigned char ubyte_expected_fill_value;
         unsigned short ushort_expected_fill_value;
         unsigned int uint_expected_fill_value;
         long long int int64_expected_fill_value;
         unsigned long long int uint64_expected_fill_value;

         /* Fill values used when writing. */
         signed char byte_fill_value = -TEST_VAL_42;
         unsigned char char_fill_value = 'x';
         short short_fill_value = TEST_VAL_42 * 100;
         int int_fill_value = TEST_VAL_42 * 1000;
         float float_fill_value = TEST_VAL_42 * 1000;
         double double_fill_value = TEST_VAL_42 * 1000;
         unsigned char ubyte_fill_value = TEST_VAL_42;
         unsigned short ushort_fill_value = TEST_VAL_42 * 100;
         unsigned int uint_fill_value = TEST_VAL_42 * 1000;
         long long int int64_fill_value = TEST_VAL_42 * 1000;
         unsigned long long int uint64_fill_value = TEST_VAL_42 * 1000;

         /* Fill values read in. */
         signed char byte_fill_value_in;
         unsigned char char_fill_value_in;
         short short_fill_value_in;
         int int_fill_value_in;
         float float_fill_value_in;
         double double_fill_value_in;
         unsigned char ubyte_fill_value_in;
         unsigned short ushort_fill_value_in;
         unsigned int uint_fill_value_in;
         long long int int64_fill_value_in;
         unsigned long long int uint64_fill_value_in;

         /* Data to write and read. */
         signed char byte_data[DIMSIZE * DIMSIZE], byte_data_in[DIMSIZE * DIMSIZE];
         unsigned char char_data[DIMSIZE * DIMSIZE], char_data_in[DIMSIZE * DIMSIZE];
         short short_data[DIMSIZE * DIMSIZE], short_data_in[DIMSIZE * DIMSIZE];
         int int_data[DIMSIZE * DIMSIZE], int_data_in[DIMSIZE * DIMSIZE];
         float float_data[DIMSIZE * DIMSIZE], float_data_in[DIMSIZE * DIMSIZE];
         double double_data[DIMSIZE * DIMSIZE], double_data_in[DIMSIZE * DIMSIZE];
         unsigned char ubyte_data[DIMSIZE * DIMSIZE], ubyte_data_in[DIMSIZE * DIMSIZE];
         unsigned short ushort_data[DIMSIZE * DIMSIZE], ushort_data_in[DIMSIZE * DIMSIZE];
         unsigned int uint_data[DIMSIZE * DIMSIZE], uint_data_in[DIMSIZE * DIMSIZE];
         long long int int64_data[DIMSIZE * DIMSIZE], int64_data_in[DIMSIZE * DIMSIZE];
         unsigned long long int uint64_data[DIMSIZE * DIMSIZE], uint64_data_in[DIMSIZE * DIMSIZE];

         if (!mpi_rank)
            printf("*** writing a %d x %d x %d file from %d processors for fill value test %d type %d...\n",
                   NUM_SLABS, DIMSIZE, DIMSIZE, mpi_size, fv, test_type[tt]);

         /* Initialize test data. */
         switch(test_type[tt])
         {
         case NC_BYTE:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               byte_data[i] = mpi_rank;
            data = byte_data;
            data_in = byte_data_in;
            byte_expected_fill_value = fv ? byte_fill_value : NC_FILL_BYTE;
            fill_value = &byte_expected_fill_value;
            fill_value_in = &byte_fill_value_in;
            break;
         case NC_CHAR:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               char_data[i] = mpi_rank;
            data = char_data;
            data_in = char_data_in;
            char_expected_fill_value = fv ? char_fill_value : NC_FILL_CHAR;
            fill_value = &char_expected_fill_value;
            fill_value_in = &char_fill_value_in;
            break;
         case NC_SHORT:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               short_data[i] = mpi_rank;
            data = short_data;
            data_in = short_data_in;
            short_expected_fill_value = fv ? short_fill_value : NC_FILL_SHORT;
            fill_value = &short_expected_fill_value;
            fill_value_in = &short_fill_value_in;
            break;
         case NC_INT:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               int_data[i] = mpi_rank;
            data = int_data;
            data_in = int_data_in;
            int_expected_fill_value = fv ? int_fill_value : NC_FILL_INT;
            fill_value = &int_expected_fill_value;
            fill_value_in = &int_fill_value_in;
            break;
         case NC_FLOAT:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               float_data[i] = mpi_rank;
            data = float_data;
            data_in = float_data_in;
            float_expected_fill_value = fv ? float_fill_value : NC_FILL_FLOAT;
            fill_value = &float_expected_fill_value;
            fill_value_in = &float_fill_value_in;
            break;
         case NC_DOUBLE:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               double_data[i] = mpi_rank;
            data = double_data;
            data_in = double_data_in;
            double_expected_fill_value = fv ? double_fill_value : NC_FILL_DOUBLE;
            fill_value = &double_expected_fill_value;
            fill_value_in = &double_fill_value_in;
            break;
         case NC_UBYTE:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               ubyte_data[i] = mpi_rank;
            data = ubyte_data;
            data_in = ubyte_data_in;
            ubyte_expected_fill_value = fv ? ubyte_fill_value : NC_FILL_UBYTE;
            fill_value = &ubyte_expected_fill_value;
            fill_value_in = &ubyte_fill_value_in;
            break;
         case NC_USHORT:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               ushort_data[i] = mpi_rank;
            data = ushort_data;
            data_in = ushort_data_in;
            ushort_expected_fill_value = fv ? ushort_fill_value : NC_FILL_USHORT;
            fill_value = &ushort_expected_fill_value;
            fill_value_in = &ushort_fill_value_in;
            break;
         case NC_UINT:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               uint_data[i] = mpi_rank;
            data = uint_data;
            data_in = uint_data_in;
            uint_expected_fill_value = fv ? uint_fill_value : NC_FILL_UINT;
            fill_value = &uint_expected_fill_value;
            fill_value_in = &uint_fill_value_in;
            break;
         case NC_INT64:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               int64_data[i] = mpi_rank;
            data = int64_data;
            data_in = int64_data_in;
            int64_expected_fill_value = fv ? int64_fill_value : NC_FILL_INT64;
            fill_value = &int64_expected_fill_value;
            fill_value_in = &int64_fill_value_in;
            break;
         case NC_UINT64:
            for (i = 0; i < DIMSIZE * DIMSIZE; i++)
               uint64_data[i] = mpi_rank;
            data = uint64_data;
            data_in = uint64_data_in;
            uint64_expected_fill_value = fv ? uint64_fill_value : NC_FILL_UINT64;
            fill_value = &uint64_expected_fill_value;
            fill_value_in = &uint64_fill_value_in;
            break;
         }

         /* Create a file name. */
         sprintf(file_name, "%s_type_%d_fv_%d.nc", TEST_NAME, test_type[tt], fv);

         /* Create a parallel netcdf-4 file. */
         if (nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid)) ERR;

         /* Get the type len. */
         if (nc_inq_type(ncid, test_type[tt], NULL, &type_size)) ERR;

         /* A global attribute holds the number of processors that created
          * the file. */
         if (nc_put_att_int(ncid, NC_GLOBAL, "num_processors", NC_INT, 1, &mpi_size)) ERR;

         /* Create three dimensions. */
         if (nc_def_dim(ncid, DIM1_NAME, NUM_SLABS, dimids)) ERR;
         if (nc_def_dim(ncid, DIM2_NAME, DIMSIZE, &dimids[1])) ERR;
         if (nc_def_dim(ncid, DIM3_NAME, DIMSIZE, &dimids[2])) ERR;

         /* Create one var. */
         if (nc_def_var(ncid, VAR_NAME, test_type[tt], NDIMS, dimids, &varid)) ERR;
         if (nc_put_att_int(ncid, varid, "var_num_processors", NC_INT, 1, &mpi_size)) ERR;
         if (fv == 1)
         {
            if (nc_def_var_fill(ncid, varid, NC_FILL, fill_value)) ERR;
            if (nc_inq_var_fill(ncid, varid, &fill_mode_in, fill_value_in)) ERR;
            if (fill_mode_in != NC_FILL) ERR;
            if (memcmp(fill_value_in, fill_value, type_size)) ERR;
         }
         else if (fv == 2)
         {
            if (nc_def_var_fill(ncid, varid, NC_NOFILL, NULL)) ERR;
            if (nc_inq_var_fill(ncid, varid, &fill_mode_in, NULL)) ERR;
            if (!fill_mode_in) ERR; /* nofill will be true */
         }

         /* Write metadata to file. */
         if (nc_enddef(ncid)) ERR;

         /* Change access mode to collective, then back to independent. */
         if (nc_var_par_access(ncid, varid, NC_COLLECTIVE)) ERR;
         if (nc_var_par_access(ncid, varid, NC_INDEPENDENT)) ERR;

         if (!mpi_rank)
            start_time = MPI_Wtime();

         /* Write all the slabs this process is responsible for. */
         for (i = 0; i < NUM_SLABS / mpi_size; i++)
         {
            write_start[0] = NUM_SLABS / mpi_size * mpi_rank + i;

            /* Write one slab of data. Due to start/count settings,
             * every 16th value will be a fill value. */
            if (nc_put_vara(ncid, varid, write_start, write_count, data)) ERR;
         }

         /* On rank 0, keep track of time. */
         if (!mpi_rank)
         {
            total_time = MPI_Wtime() - start_time;
            printf("%d\t%g\t%g\n", mpi_size, total_time, DIMSIZE * DIMSIZE * NUM_SLABS *
                   sizeof(int) / total_time);
         }

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

         /* Reopen the file and check it. */
         if ((ret = nc_open_par(file_name, NC_NOWRITE, comm, info, &ncid))) ERR;
         if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
         if (ndims_in != NDIMS || nvars_in != 1 || natts_in != 1 ||
             unlimdimid_in != -1) ERR;

         /* Check the attributes. */
         if (nc_get_att_int(ncid, NC_GLOBAL, "num_processors", &mpi_size_in)) ERR;
         if (mpi_size_in != mpi_size) ERR;
         if (nc_get_att_int(ncid, 0, "var_num_processors", &mpi_size_in)) ERR;
         if (mpi_size_in != mpi_size) ERR;
         if (fv == 1)
         {
            if (nc_inq_var_fill(ncid, varid, &fill_mode_in, fill_value_in)) ERR;
            if (fill_mode_in != NC_FILL) ERR;
            if (memcmp(fill_value_in, fill_value, type_size)) ERR;
         }

         /* Read all the slabs this process is responsible for. */
         for (i = 0; i < NUM_SLABS / mpi_size; i++)
         {
            read_start[0] = NUM_SLABS / mpi_size * mpi_rank + i;
            /* printf("mpi_rank %d i %d read_start[0] %ld\n", mpi_rank, i, read_start[0]); */

            /* Read one slab of data. */
            if (nc_get_vara(ncid, varid, read_start, read_count, data_in)) ERR;

            /* Check data.  For the third fill value test, fill is
             * turned off. So don't bother testing the values where k
             * is zero. */
            /* printf("mpi_rank %d fv %d i %d j %d k %d int_data_in[j * k] %d int_expected_fill_value %d " */
            /*        "expected_value %d\n", mpi_rank, fv, i, j, k, int_data_in[j * k], */
            /*        int_expected_fill_value, expected_value); */
            switch (test_type[tt])
            {
            case NC_BYTE:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (byte_data_in[j * DIMSIZE + k] != (signed char)(k ? mpi_rank : byte_expected_fill_value)) ERR;
               break;
            case NC_SHORT:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (short_data_in[j * DIMSIZE + k] != (short)(k ? mpi_rank : short_expected_fill_value)) ERR;
               break;
            case NC_INT:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (int_data_in[j * DIMSIZE + k] != (int)(k ? mpi_rank : int_expected_fill_value)) ERR;
               break;
            case NC_FLOAT:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (float_data_in[j * DIMSIZE + k] != (float)(k ? mpi_rank : float_expected_fill_value)) ERR;
               break;
            case NC_DOUBLE:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (double_data_in[j * DIMSIZE + k] != (double)(k ? mpi_rank : double_expected_fill_value)) ERR;
               break;
            case NC_UBYTE:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (ubyte_data_in[j * DIMSIZE + k] != (unsigned char)(k ? mpi_rank : ubyte_expected_fill_value)) ERR;
               break;
            case NC_USHORT:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (ushort_data_in[j * DIMSIZE + k] != (unsigned short)(k ? mpi_rank : ushort_expected_fill_value)) ERR;
               break;
            case NC_UINT:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (uint_data_in[j * DIMSIZE + k] != (unsigned int)(k ? mpi_rank : uint_expected_fill_value)) ERR;
               break;
            case NC_INT64:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (int64_data_in[j * DIMSIZE + k] != (long long int)(k ? mpi_rank : int64_expected_fill_value)) ERR;
               break;
            case NC_UINT64:
               for (j = 0; j < DIMSIZE; j++)
                  for (k = 0; k < DIMSIZE; k++)
                     if (fv < 2 || k)
                        if (uint64_data_in[j * DIMSIZE + k] != (unsigned long long int)(k ? mpi_rank : uint64_expected_fill_value)) ERR;
               break;
            }
         } /* next slab */

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

         if (!mpi_rank)
            SUMMARIZE_ERR;
      } /* next test type */
   } /* next fill value test run */

   /* Shut down MPI. */
   MPI_Finalize();

   if (!mpi_rank)
      FINAL_RESULTS;
   return 0;
}
コード例 #15
0
ファイル: vardata.c プロジェクト: mmase/wgrib2
/* Output the data for a single variable, in NcML syntax.
 *  TODO: currently not called, need option for NcML with values ... */
int
vardatax(
     const ncvar_t *vp,		/* variable */
     size_t vdims[],		/* variable dimension sizes */
     int ncid,			/* netcdf id */
     int varid,			/* variable id */
     const fspec_t *fsp	        /* formatting specs */
     )
{
    size_t cor[NC_MAX_DIMS];	/* corner coordinates */
    size_t edg[NC_MAX_DIMS];	/* edges of hypercube */
    size_t add[NC_MAX_DIMS];	/* "odometer" increment to next "row"  */
    size_t gulp;
    void *vals;

    int id;
    int ir;
    size_t nels;
    size_t ncols;
    size_t nrows;
    int vrank = vp->ndims;

    nels = 1;
    for (id = 0; id < vrank; id++) {
	cor[id] = 0;
	edg[id] = 1;
	nels *= vdims[id];	/* total number of values for variable */
    }

    printf("    <values>\n     ");
    set_indent (7);

    if (vrank < 1) {
	ncols = 1;
    } else {
	ncols = vdims[vrank-1];	/* size of "row" along last dimension */
	edg[vrank-1] = vdims[vrank-1];
	for (id = 0; id < vrank; id++)
	  add[id] = 0;
	if (vrank > 1)
	  add[vrank-2] = 1;
    }
    nrows = nels/ncols;		/* number of "rows" */
    gulp = ncols < VALBUFSIZ ? VALBUFSIZ : ncols;
    vals = emalloc(gulp * vp->tinfo->size);
    
    for (ir = 0; ir < nrows; ir++) {
	/*
	 * rather than just printing a whole row at once (which might
	 * exceed the capacity of some platforms), we break each row
	 * into smaller chunks, if necessary.
	 */
	size_t corsav;
	int left = (int)ncols;
	boolean lastrow;

	if (vrank > 0) {
	    corsav = cor[vrank-1];
	}
	lastrow = (boolean)(ir == nrows-1);
	while (left > 0) {
	    size_t toget = left < gulp ? left : gulp;
	    if (vrank > 0)
	      edg[vrank-1] = toget;
	    NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals) );

	    /* Test if we should treat array of chars as a string  */
	    if(vp->type == NC_CHAR && 
	       (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) {
	        pr_tvalsx(vp, toget, left > toget, lastrow, (char *) vals);
	    } else {
	        pr_any_valsx(vp, toget, left > toget, lastrow, vals);
	    }

	    left -= toget;
	    if (vrank > 0)
	      cor[vrank-1] += toget;
	}
	if (vrank > 0)
	  cor[vrank-1] = corsav;
	if (ir < nrows-1)
	  if (!upcorner(vdims,vp->ndims,cor,add))
	    error("vardata: odometer overflowed!");
	set_indent(2);
    }
    printf(" </values>\n");
    free(vals);
    return 0;
}
コード例 #16
0
ファイル: vardata.c プロジェクト: mmase/wgrib2
/* Output the data for a single variable, in CDL syntax. */
int
vardata(
     const ncvar_t *vp,		/* variable */
     size_t vdims[],		/* variable dimension sizes */
     int ncid,			/* netcdf id */
     int varid,			/* variable id */
     const fspec_t *fsp	        /* formatting specs */
     )
{
    size_t cor[NC_MAX_DIMS];	/* corner coordinates */
    size_t edg[NC_MAX_DIMS];	/* edges of hypercube */
    size_t add[NC_MAX_DIMS];	/* "odometer" increment to next "row"  */
    size_t gulp;
    void *vals;

    int id;
    int ir;
    size_t nels;
    size_t ncols;
    size_t nrows;
    int vrank = vp->ndims;

    nels = 1;
    for (id = 0; id < vrank; id++) {
	cor[id] = 0;
	edg[id] = 1;
	nels *= vdims[id];	/* total number of values for variable */
    }

    printf("\n");
    indent_out();
/* 	printf(" %s = ", vp->name); */
/*          or      */
/* 	printf(" %s =\n  ", vp->name); */
	printf(" ");
	print_name(vp->name);
    if (vrank <= 1) {
	printf(" = ");
	set_indent ((int)strlen(vp->name) + 4 + indent_get());
    } else {
	printf(" =\n  ");
	set_indent (2 + indent_get());
    }

    if (vrank < 1) {
	ncols = 1;
    } else {
	ncols = vdims[vrank-1];	/* size of "row" along last dimension */
	edg[vrank-1] = vdims[vrank-1];
	for (id = 0; id < vrank; id++)
	  add[id] = 0;
	if (vrank > 1)
	  add[vrank-2] = 1;
    }
    nrows = nels/ncols;		/* number of "rows" */
    gulp = ncols < VALBUFSIZ ? ncols : VALBUFSIZ;
    vals = emalloc(gulp * vp->tinfo->size);
    
    for (ir = 0; ir < nrows; ir++) {
	/*
	 * rather than just printing a whole row at once (which might
	 * exceed the capacity of some platforms), we break each row
	 * into smaller chunks, if necessary.
	 */
	size_t corsav = 0;
	int left = (int)ncols;
	boolean lastrow;

	if (vrank > 0) {
	    corsav = cor[vrank-1];
	    if (fsp->brief_data_cmnts != false
		&& vrank > 1
		&& left > 0) {	/* print brief comment with indices range */
/* 		printf("// %s(",vp->name); */
		printf("// ");
		printf(vp->name);
		printf("(");
		switch (fsp->data_lang) {
		  case LANG_C:
		    /* print brief comment with C variable indices */
		    for (id = 0; id < vrank-1; id++)
		      printf("%lu,", (unsigned long)cor[id]);
		    if (vdims[vrank-1] == 1)
		      printf("0");
		    else
		      printf(" 0-%lu", (unsigned long)vdims[vrank-1]-1);
		    break;
		  case LANG_F:
		    /* print brief comment with Fortran variable indices */
		    if (vdims[vrank-1] == 1)
		      printf("1");
		    else
		      printf("1-%lu ", (unsigned long)vdims[vrank-1]);
		    for (id = vrank-2; id >=0 ; id--) {
			printf(",%lu", (unsigned long)(1 + cor[id]));
		    }
		    break;
		}
		printf(")\n");
		indent_out();
		printf("    ");
		set_indent(4 + indent_get());
	    }
	}
	lastrow = (boolean)(ir == nrows-1);
	while (left > 0) {
	    size_t toget = left < gulp ? left : gulp;
	    if (vrank > 0)
	      edg[vrank-1] = toget;
	    NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals) );
	    /* Test if we should treat array of chars as a string  */
	    if(vp->type == NC_CHAR && 
	       (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) {
	        pr_tvals(vp, toget, left > toget, lastrow, (char *) vals, 
			 fsp, cor);
	    } else {
	        pr_any_vals(vp, toget, left > toget, lastrow, vals, fsp, cor);
	    }

	    left -= toget;
	    if (vrank > 0)
	      cor[vrank-1] += toget;
	}
	if (vrank > 0)
	  cor[vrank-1] = corsav;
	if (ir < nrows-1)
	  if (!upcorner(vdims,vp->ndims,cor,add))
	    error("vardata: odometer overflowed!");
	set_indent(2);
    }

    free(vals);
    return 0;
}
コード例 #17
0
ファイル: cfsubsets.c プロジェクト: Federico2014/edg4x-rose
int 
nccf_get_vara(int ncid, int varid, float *lat_range, int *nlat, float *lon_range, 
	      int *nlon, int lvl_index, int timestep, void *data)
{
   nc_type xtype[NDIMS];
   char name[NDIMS][NC_MAX_NAME];
   int did[NDIMS], vid[NDIMS];
   size_t len[NDIMS];
   size_t start[NDIMS], end[NDIMS];
   int ndims, dimids[MAX_DIMS];
   size_t dstart[NDIMS], dcount[NDIMS];
   int num[NDIMS];
   int found_lvl = 0, found_time = 0;
   int d;
   int ret;

   /* Get information about each coordinate axis. */
   if ((ret = nccf_inq_latitude(ncid, &len[LAT], &xtype[LAT], &did[LAT], &vid[LAT])))
      return ret;
   if ((ret = nccf_inq_longitude(ncid, &len[LON], &xtype[LON], &did[LON], &vid[LON])))
      return ret;

   /* Get the vertical level info, if there is one. */
   ret = nccf_inq_lvl(ncid, name[LVL], &len[LVL], &xtype[LVL], NULL, NULL, NULL, 
		      &did[LVL], &vid[LVL]);
   if (ret && ret != CF_ENOTFOUND)
      return ret;
   else if (ret == CF_NOERR)
      found_lvl++;

   /* Get the time info, if there is a time axis. */
   ret = nccf_inq_time(ncid, name[TIME], &len[TIME], &xtype[TIME], &did[TIME], 
		       &vid[TIME]);
   if (ret && ret != CF_ENOTFOUND)
      return ret;
   else if (ret == CF_NOERR)
      found_time++;

   /* Find the indixies along the latitude coordinate values to subset
    * along this dimension. */
   if ((ret = find_coord_idx(ncid, vid[LAT], len[LAT], lat_range, &num[LAT], 
			     &start[LAT], &end[LAT])))
      return ret;
   if (nlat)
      *nlat = num[LAT];

   /* Find the indixies along the longitude coordinate values to subset
    * along this dimension. */
   if ((ret = find_coord_idx(ncid, vid[LON], len[LON], lon_range, &num[LON], 
			     &start[LON], &end[LON])))
      return ret;
   if (nlon)
      *nlon = num[LON];

   /* If level indicies are not provided, take all levels. */
/*    if (!lvl_index) */
/*       num[LVL] = len[LVL]; */
/*    else */
/*       num[LVL] = 1; */
/*    if (nlvl) */
/*       *nlvl = num[LVL]; */

   if (found_time && timestep >= len[TIME])
      return CF_ERECTOOLARGE;

   /* If the user wants the data, get it for him. */
   if (lat_range && lon_range && data && num[LAT] && num[LON])
   {
      /* Find out about the dimensions of this data variable. */
      if ((ret = nc_inq_varndims(ncid, varid, &ndims)))
	 return ret;
      if (ndims > MAX_DIMS)
	 return CF_ENDIMS;
      if ((ret = nc_inq_vardimid(ncid, varid, dimids)))
	 return ret;

      /* Set the lat info. */
      for (d = 0; d < ndims; d++)
	 if (dimids[d] == did[LAT])
	    break;
      if (d == ndims)
	 return CF_ENODIM;
      dstart[d] = start[LAT];
      dcount[d] = num[LAT];

      /* Set the lon info. */
      for (d = 0; d < ndims; d++)
	 if (dimids[d] == did[LON])
	    break;
      if (d == ndims)
	 return CF_ENODIM;
      dstart[d] = start[LON];
      dcount[d] = num[LON];

      /* Set the level info. */
      if (found_lvl)
      {
	 for (d = 0; d < ndims; d++)
	    if (dimids[d] == did[LVL])
	       break;
	 if (d != ndims)
	 {
	    dstart[d] = lvl_index;
	    dcount[d] = 1;
	 }
      }

      /* Set the time info. */
      if (found_time)
      {
	 for (d = 0; d < ndims; d++)
	    if (dimids[d] == did[TIME])
	       break;
	 if (d != ndims)
	 {
	    dstart[d] = timestep;
	    dcount[d] = 1;
	 }
      }

      /* Now I've got everything needed to read a record of this data! */
      if ((ret = nc_get_vara(ncid, varid, dstart, dcount, data)))
	 return ret;
   }

   return CF_NOERR;
}
コード例 #18
0
ファイル: main.c プロジェクト: gsjaardema/netcdf-c
int
main(int argc, char **argv)
{
    int c, stat;
    int ncid;
    NChdr* hdr;

    init();

    opterr=1;
    while ((c = getopt(argc, argv, "dvau:D:l:p:c:t:R:")) != EOF) {
      switch(c) {
      case 'v': verbose=1; break;
      case 'd': debug=1; break;
      case 'D': debug=atoi(optarg); break;
      default: break;
      }
    }

    if(debug > 0) {ocdebug = debug;}

    argc -= optind;
    argv += optind;

    if(argc > 0 && fileurl == NULL)
	fileurl = nulldup(argv[0]);

    if(fileurl == NULL) fileurl = getenv("FILEURL");

    if(fileurl == NULL) {
	fprintf(stderr,"no file url specified\n");
	usage();
    }

    if(verbose) dumpflags();

    if(verbose) {fprintf(stdout,"initializing\n"); fflush(stdout);}

    stat = nc_open(fileurl,NC_NOWRITE,&ncid);
    check_err(stat);

    /* dump meta data */
    stat = dumpmetadata(ncid,&hdr);
    check_err(stat);

    /* Get data */
if(1)
    {
	int i,j,limited;
	size_t start[NC_MAX_VAR_DIMS];
	size_t count[NC_MAX_VAR_DIMS];
	/* Walk each variable */
	for(i=0;i<hdr->nvars;i++) {
	    Var* var = &hdr->vars[i];
	    size_t nelems = 1;
	    limited = 0;
	    for(j=0;j<var->ndims;j++) {
		start[j] = 0;
		assert(var->dimids[j] == hdr->dims[var->dimids[j]].dimid);
		count[j] = hdr->dims[var->dimids[j]].size;
		/* put a limit on each count */
	        if(count[j] > LIMIT) {count[j] = LIMIT; limited = 1;}
		nelems *= count[j];
	    }
	    if(nelems > 0) {
		size_t typesize = nctypesizeof(var->nctype);
		size_t memsize = typesize*nelems;
	        void* memory = malloc(memsize);
	        fprintf(stdout,"%s: size=%lu (%lu * %lu) ; ",
			var->name, (unsigned long)memsize, (unsigned long)typesize, (unsigned long)nelems);
		fflush(stdout);
	        stat = nc_get_vara(ncid,var->varid,start,count,memory);
	        check_err(stat);
		/* dump memory */
		switch (var->nctype){
		case NC_CHAR:
	            fprintf(stdout,"\""); fflush(stdout);
		    dumpchars(nelems,memory);
	            fprintf(stdout,"\"");
		    break;		    
		default:
		    for(j=0;j<nelems;j++) {
		        if(j > 0) fprintf(stdout," ");
		        dumpdata1(var->nctype,j,memory);
		    }
		}
		if(limited) fprintf(stdout,"...");
	        fprintf(stdout,"\n");
		free(memory);
	    } else
		fprintf(stdout,"%s: no data\n",var->name);
	}
        fprintf(stdout,"\n");	
    }

    nc_close(ncid);
    return 0;
}
コード例 #19
0
ファイル: bm_radar.c プロジェクト: Unidata/netcdf-c
/* Copy a netCDF file, changing cmode if desired. */
static
int copy_file(char *file_name_in, char *file_name_out, int cmode_out,
	      int *chunking, int *deflate)
{
   int ncid_in, ncid_out;
   int natts, nvars, ndims, unlimdimid;
   char name[NC_MAX_NAME + 1];
   size_t len;
   int a, v, d;

   if (nc_open(file_name_in, NC_NOWRITE, &ncid_in)) ERR;
   if (nc_create(file_name_out, cmode_out, &ncid_out)) ERR;
      
   if (nc_inq(ncid_in, &ndims, &nvars, &natts, &unlimdimid)) ERR;

   /* Copy dims. */
   for (d = 0; d < ndims; d++)
   {
      if (nc_inq_dim(ncid_in, d, name, &len)) ERR;
      if (nc_def_dim(ncid_out, name, len, NULL)) ERR;
   }

   /* Copy global atts. */
   for (a = 0; a < natts; a++)
   {
      if (nc_inq_attname(ncid_in, NC_GLOBAL, a, name)) ERR;
      if (nc_copy_att(ncid_in, NC_GLOBAL, name, ncid_out, NC_GLOBAL)) ERR;
   }

   /* Copy the variable metadata. */
   for (v = 0; v < nvars; v++)
   {
      char name[NC_MAX_NAME + 1];
      char att_name[NC_MAX_NAME + 1];
      nc_type xtype;
      int ndims, dimids[NC_MAX_VAR_DIMS], natts;
      int varid_out;
      int a;
      int retval = NC_NOERR;

      /* Learn about this var. */
      if ((retval = nc_inq_var(ncid_in, v, name, &xtype, &ndims, dimids, &natts)))
	 return retval;

      /* Create the output var. */
      if (nc_def_var(ncid_out, name, xtype, ndims, dimids, &varid_out)) ERR;

      /* Except for 1D vars, sent chunking and compression. */
      if (ndims != 1)
      {
	 if (chunking)
	    if (nc_def_var_chunking(ncid_out, v, NC_CHUNKED, chunking)) ERR;
	 if (deflate)
	    if (nc_def_var_deflate(ncid_out, v, NC_NOSHUFFLE, *deflate, *deflate)) ERR;
      }

      /* Copy the attributes. */
      for (a=0; a<natts; a++)
      {
	 if (nc_inq_attname(ncid_in, v, a, att_name)) ERR;
	 if (nc_copy_att(ncid_in, v, att_name, ncid_out, varid_out)) ERR;
      }
   }

   /* Copy the variable data. */
   for (v = 0; v < nvars; v++)
   {
      char name[NC_MAX_NAME + 1];
      nc_type xtype;
      int ndims, dimids[NC_MAX_VAR_DIMS], natts, real_ndims;
      int d;
      void *data = NULL;
      size_t *count = NULL, *start = NULL;
      size_t reclen = 1;
      size_t *dimlen = NULL;
      int retval = NC_NOERR;
      size_t type_size;
      char type_name[NC_MAX_NAME+1];

      /* Learn about this var. */
      if ((retval = nc_inq_var(ncid_in, v, name, &xtype, &ndims, dimids, &natts)))
	 return retval;

      /* Later on, we will need to know the size of this type. */
      if ((retval = nc_inq_type(ncid_in, xtype, type_name, &type_size)))
	 return retval;
      LOG((3, "type %s has size %d", type_name, type_size));

      /* Allocate memory for our start and count arrays. If ndims = 0
	 this is a scalar, which I will treat as a 1-D array with one
	 element. */
      real_ndims = ndims ? ndims : 1;
      if (!(start = nc_malloc(real_ndims * sizeof(size_t))))
	 BAIL(NC_ENOMEM);
      if (!(count = nc_malloc(real_ndims * sizeof(size_t))))
	 BAIL(NC_ENOMEM);

      /* The start array will be all zeros, except the first element,
	 which will be the record number. Count will be the dimension
	 size, except for the first element, which will be one, because
	 we will copy one record at a time. For this we need the var
	 shape. */
      if (!(dimlen = nc_malloc(real_ndims * sizeof(size_t))))
	 BAIL(NC_ENOMEM);

      /* Find out how much data. */
      for (d=0; d<ndims; d++)
      {
	 if ((retval = nc_inq_dimlen(ncid_in, dimids[d], &dimlen[d])))
	    BAIL(retval);
	 LOG((4, "nc_copy_var: there are %d data", dimlen[d]));
      }

      /* If this is really a scalar, then set the dimlen to 1. */
      if (ndims == 0)
	 dimlen[0] = 1;

      for (d=0; d<real_ndims; d++)
      {
	 start[d] = 0;
	 count[d] = d ? dimlen[d] : 1;
	 if (d) reclen *= dimlen[d];
      }

      /* If there are no records, we're done. */
      if (!dimlen[0])
	 goto exit;

      /* Allocate memory for one record. */
      if (!(data = nc_malloc(reclen * type_size)))
	 return NC_ENOMEM;
   
      /* Copy the var data one record at a time. */
      for (start[0]=0; !retval && start[0]<(size_t)dimlen[0]; start[0]++)
      {
	 if (nc_get_vara(ncid_in, v, start, count, data)) ERR;
	 if (nc_put_vara(ncid_out, v, start, count, data)) ERR;
      }
    
     exit:
      if (data) nc_free(data);
      if (dimlen) nc_free(dimlen);
      if (start) nc_free(start);
      if (count) nc_free(count);

   }

   if (nc_close(ncid_in)) ERR;
   if (nc_close(ncid_out)) ERR;

   return NC_NOERR;
}
コード例 #20
0
ファイル: tst_strings.c プロジェクト: dschwen/libmesh
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", "Van Buren",
			       "Harrison", "Tyler", "Polk", "Taylor",
			       "Fillmore", "Pierce", "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", "Van Buren",
			       "Harrison", "Tyler", "Polk", "Taylor",
			       "Fillmore", "Pierce", "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 && (data_in[i] == NULL || strcmp(data_in[i], data[i]))) ERR;
	 if (i >= SOME_PRES && (data_in[i] == NULL || 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 netcdf-4 strided string access...");
   {
#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];
      ptrdiff_t stride[NDIMS_PRES];
      char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
			       "Monroe", "Adams", "Jackson", "Van Buren",
			       "Harrison", "Tyler", "Polk", "Taylor",
			       "Fillmore", "Pierce", "Buchanan", "Lincoln"};
      char *data_in[NUM_PRES];
      int status;

      /* 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;
      stride[0] = 2;
      status = nc_put_vars_string(ncid, varid, start, count, stride, (const char **)data);
      if(status != NC_NOERR)
	  fprintf(stderr,"%s\n",nc_strerror(status));
      if (nc_close(ncid)) ERR;

      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_get_vars_string(ncid, varid, start, count, stride, data_in)) ERR;
      for (i = 0; i < NUM_PRES; i++)
      {
	 if (i < SOME_PRES && 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 variables with fill values...");
   {
#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[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[] = {""};
      char *my_string_fill[] = {"fill_string"};

#define NUM_DIM_COMBOS 4
      int dim_combo;

      for (dim_combo = 0; dim_combo < NUM_DIM_COMBOS; dim_combo++)
      {
         char filename[NC_MAX_NAME + 1];
         int dim_len = dim_combo ? NC_UNLIMITED : DHR_LEN;
         int expected_unlimdimid = dim_combo ? 0 : -1;
         char *default_fill = ((char *)"");
         char **string_fillp = dim_combo == 3 ? my_string_fill : &default_fill;
         char *data_in;

         sprintf(filename, "%s_dim_combo_%d.nc", TEST_NAME, dim_combo);
         if (nc_create(filename, NC_NETCDF4, &ncid)) ERR;

         /* Create an array of strings for the Universal Declaraion of Human Rights. */
         if (nc_def_dim(ncid, DIM_NAME1, dim_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;
         if (dim_combo == 3)
            if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, my_string_fill)) ERR;

         /* Check some stuff. */
         if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
         if (ndims != NDIMS || nvars != 2 || natts != 0 || unlimdimid != expected_unlimdimid) 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 (dim_combo)
         {
            size_t start[NDIMS], count[NDIMS] = {1};
            int counter = 1;
            
            /* Write one record at a time. */
            for (start[0] = 0; start[0] < DHR_LEN; start[0]++)
            {
               size_t new_start[NDIMS];
               size_t *my_startp = start;

               /* For dim_combo 2 or 3 skip every other record. */
               new_start[0] = start[0] + counter++;
               if (dim_combo >= 2)
                  my_startp = new_start;

               /* Write a record. */
               nc_put_vara_string(ncid, varid, my_startp, count, (const char **)&data[start[0]]);
            }
         }
         else
         {
            /* Write all records at once. */
            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(filename, NC_NOWRITE, &ncid)) ERR;
         if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
         if (ndims != NDIMS || nvars != 2 || natts != 0 || unlimdimid != expected_unlimdimid) 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;

         /* Check fill value stuff. */
         {
            int no_fill;
            char *fill_value_in[1];
            
            if (nc_inq_var_fill(ncid, varid, &no_fill, (char **)fill_value_in)) ERR;
            if (no_fill) ERR;
            if (strcmp(fill_value_in[0], *string_fillp)) ERR;
            if (nc_free_string(1, (char **)fill_value_in)) ERR;
         }

         if (dim_combo < 2)
         {
            char *data_in[DHR_LEN];
            
            /* Get the data in one read of the entire var. */
            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;
         }
         else
         {
            char *data_in;
            size_t start[NDIMS], count[NDIMS] = {1};
            int my_count = 0;
            
            /* Get the data one record at a time. Every other record,
             * starting with the first, is a fill value. */
            for (start[0] = 0; start[0] < DHR_LEN; start[0]++)
            {
               if (nc_get_vara(ncid, varid, start, count, &data_in)) ERR;
               if (start[0] % 2)
               {
                  if (strcmp(data_in, data[my_count++])) ERR;
               }
               else
               {
                  if (strcmp(data_in, *string_fillp)) ERR;
               }
               if (nc_free_string(1, &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, 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;
      } /* next dim_combo */
   }
   SUMMARIZE_ERR;

   printf("*** Testing a file that causes ncdump problems...");
   {
#define NUM_DIMS 2
#define DIM_0_NAME "dim_0"
#define DIM_1_NAME "dim_1"
#define DIM_1_LEN 2
#define FILE_NAME_NCDUMP "tst_strings_ncdump_problem.nc"
#define VAR_NAME_NCDUMP "var_string"
      int ncid, varid, dimid[NUM_DIMS];
      char *string_data[] = {"x"};
      int t;

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

      /* Create dims. */
      if (nc_def_dim(ncid, DIM_0_NAME, NC_UNLIMITED, &dimid[0])) ERR;
      if (nc_def_dim(ncid, DIM_1_NAME, DIM_1_LEN, &dimid[1])) ERR;
               
      /* Create a var. */
      if (nc_def_var(ncid, VAR_NAME_NCDUMP, NC_STRING, NUM_DIMS, dimid, &varid)) ERR;

      /* Check that you can't turn off fill mode for NC_STRING variables. */
      if (nc_def_var_fill(ncid, varid, NC_NOFILL, NULL) != NC_EINVAL) ERR;

      /* End define mode. */
      if (nc_enddef(ncid)) ERR;

      /* Write to each var. */
      for (t = 0; t < 1; t++)
      {
         size_t start[NUM_DIMS] = {1, 0};
         size_t count[NUM_DIMS] = {1, 1};

         if (nc_put_vara_string(ncid, varid, start, count, (const char **)string_data)) ERR;
      }
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}