void create_netcdf(char *filename, int num_vars, char *varname[num_vars])
{
    int ncid_wr, retval_wr;
    int vel_varid_wr;
    int Nt, Nx, Ny, Nz;
    int dimids[NDIMS];

    /* Create the file. */
    if ((retval_wr = nc_create(filename, NC_CLOBBER, &ncid_wr)))
       ERR(retval_wr);

    /* Define the dimensions. The record dimension is defined to have
     * unlimited length - it can grow as needed.*/
    if ((retval_wr = nc_def_dim(ncid_wr, "Ny", N_HR, &Ny)))
        ERR(retval_wr);
    if ((retval_wr = nc_def_dim(ncid_wr, "Nz", N_HR, &Nz)))
        ERR(retval_wr);
    if ((retval_wr = nc_def_dim(ncid_wr, "Nt", NC_UNLIMITED, &Nt)))
        ERR(retval_wr);

    /* Define the netCDF variables for the data. */
    dimids[0] = Nt;
    dimids[1] = Nx;
    dimids[2] = Ny;
    dimids[3] = Nz;

    for (int i = 0; i<num_vars; i++)
    {
        if ((retval_wr = nc_def_var(ncid_wr, varname[i], NC_FLOAT, NDIMS, dimids, &vel_varid_wr)))
            ERR(retval_wr);
    }

    /* End define mode (SHOULD NOT FORGET THIS!). */
    if ((retval_wr = nc_enddef(ncid_wr)))
        ERR(retval_wr);

    /* Close the file. */
    if ((retval_wr = nc_close(ncid_wr)))
        ERR(retval_wr);
    printf("\n *** SUCCESS creating file: %s!\n", filename);
}
Ejemplo n.º 2
0
int
main()
{
   int ncid, spockid, kirkid, dimids[NUMDIMS];
   double val_in, val_out = 999.99;
   size_t index[NUMDIMS] = {1};
   int i, res; 

   /* Create the netCDF classic format file. */
   if ((res = nc_create("example.nc", NC_CLOBBER, &ncid)))
      BAIL(res);

   /* Turn off fill mode to speed things up. */
   if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
       BAIL(res);

   /* Define dimension. */
   if ((res = nc_def_dim(ncid, "longdim", DIM_LEN, dimids)))
      BAIL(res);
   
   /* Define two variables. */
   if ((res = nc_def_var(ncid, "spock", NC_DOUBLE, NUMDIMS, 
			 dimids, &spockid)))
      BAIL(res);
   if ((res = nc_def_var(ncid, "kirk", NC_DOUBLE, NUMDIMS, 
			 dimids, &kirkid)))
      BAIL(res);
   
   /* We're finished defining metadata. */
   if ((res = nc_enddef(ncid)))
      BAIL(res);

   if ((res = nc_put_var1_double(ncid, spockid, index, &val_out)))
      BAIL(res);

   /* We're done! */
   if ((res = nc_close(ncid)))
      BAIL(res);

   return 0;
}
Ejemplo n.º 3
0
static void
test_large_byte_var(const char *testfile) {
    int ncid, varid, dimids[NUMDIMS];
    size_t index[2] = {0, 0};
    int cflag = NC_CLOBBER;
    signed char vals[DIM2];
    signed char char_val_in;
    int i, j;

    if (nc_create(testfile, cflag, &ncid)) ERR;
    if (nc_set_fill(ncid, NC_NOFILL, NULL)) ERR;
    if (nc_def_dim(ncid, "dim1", DIM1, &dimids[0])) ERR;
    if (nc_def_dim(ncid, "dim2", DIM2, &dimids[1])) ERR;
    if (nc_def_var(ncid, "var", NC_BYTE, NUMDIMS, dimids, &varid)) ERR;
    if (nc_enddef(ncid)) ERR;

    for (i=0; i<DIM1; i++) 
    {
	size_t start[NUMDIMS];
	size_t count[NUMDIMS];
	start[0] = i;
	start[1] = 0;
	count[0] = 1;
	count[1] = DIM2;
	for (j=0; j < DIM2; j++) {
	    vals[j] = (i+9)*(j+11); /* note vals[j] is 99 when i==0 and j==0 */
	}
	if (nc_put_vara_schar(ncid, varid, start, count, vals)) 
	{
	    ERR;
	    break;
	}
    }
    if (nc_close(ncid)) ERR;
    if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
    if (nc_inq_varid(ncid, "var", &varid)) ERR;
    if (nc_get_var1_schar(ncid, varid, index, &char_val_in)) ERR;
    if (char_val_in != 99)	/* see above, the value written when i==0, j==0 */
	ERR;
    if (nc_close(ncid)) ERR;
}
Ejemplo n.º 4
0
int dump_file3(const float *data, int docompression, int usedefdim)
{
   int ncmode, ncid, dimids[NDIMS], var;
   size_t start[NDIMS] = {0, 0, 0}, count[NDIMS] = {X_LEN, Y_LEN, Z_LEN};
   ptrdiff_t stride[NDIMS] = {1, 1, 1};

   ncmode = NC_CLOBBER|NC_NETCDF4;

   if (nc_create(FILE_NAME, ncmode, &ncid)) ERR_RET;
   if (nc_def_dim(ncid, "time", X_LEN, &dimids[0])) ERR_RET;
   if (nc_def_dim(ncid, "lat", Y_LEN, &dimids[1])) ERR_RET;
   if (nc_def_dim(ncid, "lon", Z_LEN, &dimids[2])) ERR_RET;
   if (nc_def_var(ncid, "test", NC_FLOAT, NDIMS, dimids, &var)) ERR_RET;
   if (docompression)
      if (nc_def_var_deflate(ncid, var, 1, 1, 1)) ERR_RET;
   if (nc_enddef(ncid)) ERR_RET;
   if (nc_put_vars_float(ncid, var, start, count, stride, data)) ERR_RET;
   if (nc_close(ncid)) ERR_RET;

   return 0;
}
Ejemplo n.º 5
0
int dump_file2(const float *data, int docompression, int usedefdim)
{
   int ncid, dimids[NDIMS], var;
   size_t start[NDIMS] = {0, 0, 0};
   size_t count[NDIMS] = {1, 1, Z_LEN};

   if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR_RET;
   if (nc_def_dim(ncid, "time", X_LEN, &dimids[0])) ERR_RET;
   if (nc_def_dim(ncid, "lat", Y_LEN, &dimids[1])) ERR_RET;
   if (nc_def_dim(ncid, "lon", Z_LEN, &dimids[2])) ERR_RET;
   if (nc_def_var(ncid, "test", NC_FLOAT, NDIMS, dimids, &var)) ERR_RET;
   if (docompression)
     if (nc_def_var_deflate(ncid, var, 1, 1, 1)) ERR_RET;
   if (nc_enddef(ncid)) ERR_RET;
   for (start[0] = 0; start[0] < X_LEN; start[0]++)
      for (start[1] = 0; start[1] < Y_LEN; start[1]++)
	 if (nc_put_vara_float(ncid, var, start, count, data)) ERR_RET;
   if (nc_close(ncid)) ERR_RET;

   return 0;
}
Ejemplo n.º 6
0
int main(int argc, char *argv[])
{
    int i, err, nerrs=0, ncid, dimid[2], varid[2];
    short buf[10];
    size_t start, count;

    err = nc_create(FILE_NAME, NC_CLOBBER|NC_64BIT_DATA, &ncid); ERR;
    err = nc_def_dim(ncid, "dim0", NC_MAX_UINT, &dimid[0]); ERR
    err = nc_def_dim(ncid, "dim1", 10,          &dimid[1]); ERR

    /* define one small variable after one big variable */
    err = nc_def_var(ncid, "var_big",   NC_SHORT, 1, &dimid[0], &varid[0]); ERR
    err = nc_def_var(ncid, "var_small", NC_SHORT, 1, &dimid[1], &varid[1]); ERR
    err = nc_set_fill(ncid, NC_NOFILL, NULL); ERR
    err = nc_enddef(ncid); ERR

    /* write to var_big in location overlapping with var_small when using
     * netCDF 4.4.x or prior */
    start = NC_MAX_UINT/sizeof(short);
    count = 10;
    for (i=0; i<10; i++) buf[i] = i;
    err = nc_put_vara_short(ncid, varid[0], &start, &count, buf); ERR

    /* write var_small */
    for (i=0; i<10; i++) buf[i] = -1;
    err = nc_put_var_short(ncid, varid[1], buf); ERR

    /* read back var_big and check contents */
    for (i=0; i<10; i++) buf[i] = -1;
    err = nc_get_vara_short(ncid, varid[0], &start, &count,buf); ERR
    for (i=0; i<10; i++) {
        if (buf[i] != i) {
            printf("Error at buf[%d] expect %d but got %hd\n",i,i,buf[i]);
            nerrs++;
        }
    }
    err = nc_close(ncid); ERR

    return (nerrs > 0);
}
Ejemplo n.º 7
0
int
main(int argc, char **argv)
{
   printf("\n*** Testing netcdf-4 string type.\n");
   printf("*** testing very simple string attribute...");
   {
#define ATT_LEN 1
      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] = {"An appeaser is one who feeds a crocodile — "
                             "hoping it will eat him last. "
                             "Here are some non-ASCII characters: "
                             "\x00\xAA\xBB\xFF"};

      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;
   FINAL_RESULTS;
}
Ejemplo n.º 8
0
static int
test_large_byte_var(const char *testfile) {
    int ncid, varid, dimids[NUMDIMS];
    size_t index[NUMDIMS] = {0, 0};
    signed char vals[DIM2];
    signed char char_val_in;
    size_t start[NUMDIMS], count[NUMDIMS];
    int j;

    if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
    if (nc_set_fill(ncid, NC_NOFILL, NULL)) ERR;
    if (nc_def_dim(ncid, "dim1", DIM1, &dimids[0])) ERR;
    if (nc_def_dim(ncid, "dim2", DIM2, &dimids[1])) ERR;
    if (nc_def_var(ncid, "var", NC_BYTE, NUMDIMS, dimids, &varid)) ERR;
    if (nc_enddef(ncid)) ERR;

    for (j = 0; j < DIM2; j++) {
       vals[j] = 9 * (j + 11); /* note vals[j] is 99 when j==0 */
    }
    start[1] = 0;
    count[0] = 1;
    count[1] = DIM2;
    for (start[0] = 0; start[0] < DIM1; start[0]++) {
	if (nc_put_vara_schar(ncid, varid, start, count, vals))
	{
	    ERR;
	    break;
	}
    }

    if (nc_close(ncid)) ERR;
    if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
    if (nc_inq_varid(ncid, "var", &varid)) ERR;
    if (nc_get_var1_schar(ncid, varid, index, &char_val_in)) ERR;
    if (char_val_in != 99)	/* see above, the value written when start[0]==0, j==0 */
	ERR;
    if (nc_close(ncid)) ERR;
    return 0;
}
Ejemplo n.º 9
0
static void
test_small_atts(const char *testfile)
{
   int ncid;
   char att[MAX_LEN + 1], att_in[MAX_LEN + 1], source[MAX_LEN + 1] = "0123456";
   int ndims, nvars, natts, unlimdimid;
   size_t len_in;
   int t, l, f;

   
   /* Run this with and without fill mode. */
   for (f = 0; f < 2; f++)
   {
      /* Create small files with an attribute that grows by one each
       * time. */
      for (t = 1; t < MAX_LEN; t++)
      {
	 /* Create null-terminated text string of correct length. */
	 strncpy(att, source, t);
	 
	 /* Create a file with one attribute. */
	 if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;
	 if (nc_put_att_text(ncid, NC_GLOBAL, ATT_NAME, t + 1, att)) ERR;
	 if (f && nc_set_fill(ncid, NC_NOFILL, NULL)) ERR;
	 if (nc_close(ncid)) ERR;
	 
	 /* Reopen the file and check it. */
	 if (nc_open(testfile, 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_attlen(ncid, NC_GLOBAL, ATT_NAME, &len_in)) ERR;
	 if (len_in != t + 1) ERR;
	 if (nc_get_att_text(ncid, NC_GLOBAL, ATT_NAME, att_in));
	 l = strlen(att_in);
	 if (strncmp(att_in, att, t)) ERR;
	 if (nc_close(ncid)) ERR; 
      }
   }
}
Ejemplo n.º 10
0
static void
test_transform(const char *testfile)
{
   int ncid, transform_varid;
   int nvars, ndims, natts, unlimdimid;
   char name_in[NC_MAX_NAME + 1];
   char transform_name_in[NC_MAX_NAME + 1], transform_type_in[NC_MAX_NAME + 1];
   size_t type_len, name_len;

   /* Create a file. */
   if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;

   /* Create a transform. */
   if (nccf_def_transform(ncid, TRANSFORM_NAME, TRANSFORM_TYPE1, 
			  TRANSFORM_NAME, &transform_varid)) ERR;
   if (transform_varid != 0) ERR;
   if (nccf_inq_transform(ncid, transform_varid, name_in, &type_len, 
			  transform_type_in, &name_len, 
			  transform_name_in)) ERR;
   if (strcmp(name_in, TRANSFORM_NAME) || type_len != strlen(TRANSFORM_TYPE1) + 1 ||
       strcmp(transform_type_in, TRANSFORM_TYPE1) || name_len != strlen(TRANSFORM_NAME) + 1 ||
       strcmp(transform_name_in, TRANSFORM_NAME)) ERR;
   
   /* Write the file. */
   if (nc_close(ncid)) ERR;

   /* Reopen the file and check it. */
   if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
   if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
   if (ndims != 0 && nvars != 1 && natts != 0 && unlimdimid != -1) ERR;
   if (nccf_inq_transform(ncid, 0, name_in, &type_len, transform_type_in, 
			&name_len, transform_name_in)) ERR;
   if (strcmp(name_in, TRANSFORM_NAME) || type_len != strlen(TRANSFORM_TYPE1) + 1 ||
       strcmp(transform_type_in, TRANSFORM_TYPE1) || name_len != strlen(TRANSFORM_NAME) + 1 ||
       strcmp(transform_name_in, TRANSFORM_NAME)) ERR;

   if (nc_close(ncid)) ERR; 
}
Ejemplo n.º 11
0
static void
test_system(const char *testfile)
{
   int ncid, dimid, axis_varid, system_varid, dimids[1] = {0};
   int axes_varids[1] = {0};
   int nvars, ndims, natts, unlimdimid;
   char name_in[NC_MAX_NAME + 1];
   int naxes_in, axes_varids_in[1];

   /* Create a file. */
   if (nc_create(testfile, NC_CLOBBER, &ncid)) ERR;

   /* Create an dimension and a coordinate var to go with it. */
   if (nc_def_dim(ncid, DIM1_NAME, DIM_LEN, &dimid)) ERR;
   if (nc_def_var(ncid, DIM1_NAME, NC_FLOAT, 1, dimids, &axis_varid)) ERR;
   if (axis_varid != 0) ERR;
   if (nccf_def_coord_system(ncid, COORD_SYSTEM, 1, axes_varids, &system_varid)) ERR;
   if (system_varid != 1) ERR;
   if (nccf_inq_coord_system(ncid, 1, name_in, &naxes_in, axes_varids_in)) ERR;
   if (strcmp(name_in, COORD_SYSTEM) || naxes_in != 1 || 
       axes_varids_in[0] != axes_varids[0]) ERR;


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

   /* Reopen the file and check it. */
   if (nc_open(testfile, NC_NOWRITE, &ncid)) ERR;
   if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
   if (ndims != 1 && nvars != 2 && natts != 0 && unlimdimid != -1) ERR;
   if (nccf_inq_coord_system(ncid, 1, name_in, &naxes_in, 
			   axes_varids_in)) ERR;
   if (strcmp(name_in, COORD_SYSTEM) || naxes_in != 1 || 
       axes_varids_in[0] != axes_varids[0]) ERR;

   if (nc_close(ncid)) ERR; 
}
Ejemplo n.º 12
0
int
main(int argc, char **argv)
{
    int ncid, dimid, fvarid, dvarid;
    float fvals[NVALS], fvals_in[NVALS];
    double dvals[NVALS], dvals_in[NVALS];


    float fnan = NC_FNAN;//(NC_INFINITE-NC_INFINITE);//0.f/0.f;
    double dnan = NC_DNAN;//(NC_INFINITE-NC_INFINITE);//0.0/0.0;
    float fpinf = NC_FPINF;//NC_INFINITE;//1.0f/0.0f;
    float fninf = -fpinf;
    double dpinf = NC_DPINF;//NC_INFINITE;//1.0/0.0;
    double dninf = -dpinf;
    nc_type att_type;
    size_t att_len;
    float att_fvals[NVALS];
    double att_dvals[NVALS];

    printf("\n*** Testing NaN\n");
    printf("*** creating NaN test file %s...", FILE8_NAME);
    if (nc_create(FILE8_NAME, NC_CLOBBER, &ncid)) ERR;

    if (nc_def_dim(ncid, DIM_NAME, NVALS, &dimid)) ERR;

    if (nc_def_var(ncid, F_NAME, NC_FLOAT, NDIMS, &dimid, &fvarid)) ERR;
    if (nc_def_var(ncid, D_NAME, NC_DOUBLE, NDIMS, &dimid, &dvarid)) ERR;

    fvals[0] = fninf;
    fvals[1] = fnan;
    fvals[2] = fpinf;
    dvals[0] = dninf;
    dvals[1] = dnan;
    dvals[2] = dpinf;

    /* Create float and double attributes */
    if (nc_put_att_float(ncid, fvarid, FV_NAME, NC_FLOAT, FV_NVALS, &fnan)) ERR;
    if (nc_put_att_float(ncid, fvarid, ATT_NAME, NC_FLOAT, NVALS, fvals)) ERR;
    if (nc_put_att_double(ncid, dvarid, FV_NAME, NC_DOUBLE, FV_NVALS, &dnan)) ERR;
    if (nc_put_att_double(ncid, dvarid, ATT_NAME, NC_DOUBLE, NVALS, dvals)) ERR;

    if (nc_enddef(ncid)) ERR;

    /* Write float and double data */
   if (nc_put_var_float(ncid, fvarid, fvals)) ERR;
   if (nc_put_var_double(ncid, dvarid, dvals)) ERR;

   if (nc_close(ncid)) ERR;

   /* Check it out. */

   /* Reopen the file. */
   if (nc_open(FILE8_NAME, NC_NOWRITE, &ncid)) ERR;
   if (nc_inq_varid(ncid, F_NAME, &fvarid)) ERR;
   if (nc_inq_varid(ncid, D_NAME, &dvarid)) ERR;
   /* Check the values of the float attributes */
   if (nc_inq_att(ncid, fvarid, FV_NAME, &att_type, &att_len)) ERR;
   if (att_type != NC_FLOAT || att_len != FV_NVALS) ERR;
   if (nc_get_att_float(ncid, fvarid, FV_NAME, att_fvals)) ERR;
   if (!isnan(att_fvals[0])) ERR;
   if (nc_get_att_float(ncid, fvarid, ATT_NAME, att_fvals)) ERR;
   if (!(isinf(att_fvals[0]) && att_fvals[0] < 0)) ERR;
   if (!isnan(att_fvals[1])) ERR;
   if (!(isinf(att_fvals[2]) && att_fvals[2] > 0)) ERR;
   /* Check the values of double attributes */
   if (nc_inq_att(ncid, dvarid, FV_NAME, &att_type, &att_len)) ERR;
   if (att_type != NC_DOUBLE || att_len != FV_NVALS) ERR;
   if (nc_get_att_double(ncid, dvarid, FV_NAME, att_dvals)) ERR;
   if (!isnan(att_dvals[0])) ERR;
   if (nc_get_att_double(ncid, dvarid, ATT_NAME, att_dvals)) ERR;
   if (!(isinf(att_dvals[0]) && att_dvals[0] < 0)) ERR;
   if (!isnan(att_dvals[1])) ERR;
   if (!(isinf(att_dvals[2]) && att_dvals[2] > 0)) ERR;
   /* Check values of float data */
   if (nc_get_var_float(ncid, fvarid, fvals_in)) ERR;
   if (!(isinf(fvals_in[0]) && fvals_in[0] < 0)) ERR;
   if (!isnan(fvals_in[1])) ERR;
   if (!(isinf(fvals_in[2]) && fvals_in[2] > 0)) ERR;
   /* Check values of double data */
   if (nc_get_var_double(ncid, dvarid, dvals_in)) ERR;
   if (!(isinf(dvals_in[0]) && dvals_in[0] < 0)) ERR;
   if (!isnan(dvals_in[1])) ERR;
   if (!(isinf(dvals_in[2]) && dvals_in[2] > 0)) ERR;

   if (nc_close(ncid)) ERR;

   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Ejemplo n.º 13
0
int
main(int argc, char **argv)
{

   printf("\n*** Testing netcdf-4 variable chunking.\n");
   printf("**** testing that fixed vars with filter end up being chunked, with good sizes...");
   {

      int ncid;
      int nvars, ndims, ngatts, unlimdimid;
      int contig;
      int ndims_in, natts_in, dimids_in;
      int small_dimid, medium_dimid, large_dimid;
      int small_varid, medium_varid, large_varid;
      char var_name_in[NC_MAX_NAME + 1];
      size_t chunksize_in[NDIMS1];
      nc_type xtype_in;

      /* Create a netcdf-4 file with three dimensions. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN, &small_dimid)) ERR;
      if (nc_def_dim(ncid, D_MEDIUM, D_MEDIUM_LEN, &medium_dimid)) ERR;
      if (nc_def_dim(ncid, D_LARGE, D_LARGE_LEN, &large_dimid)) ERR;

      /* Add three vars, with filters to force chunking. */
      if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
      if (nc_def_var_deflate(ncid, small_varid, 0, 1, 4)) ERR;
      if (nc_def_var(ncid, V_MEDIUM, NC_INT64, NDIMS1, &medium_dimid, &medium_varid)) ERR;
      if (nc_def_var_deflate(ncid, medium_varid, 1, 0, 0)) ERR;
      if (nc_def_var(ncid, V_LARGE, NC_INT64, NDIMS1, &large_dimid, &large_varid)) ERR;
      if (nc_def_var_fletcher32(ncid, large_varid, 1)) ERR;
      if (nc_close(ncid)) ERR;

      /* Open the file and check. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
      if (nvars != 3 || ndims != 3 || ngatts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, &dimids_in, &natts_in)) ERR;
      if (strcmp(var_name_in, V_SMALL) || xtype_in != NC_INT64 || ndims_in != 1 ||
	  natts_in != 0) ERR;
      
      /* Make sure chunking sizes are what we expect. */
      if (nc_inq_var_chunking(ncid, small_varid, &contig, chunksize_in)) ERR;
      if (contig || chunksize_in[0] != D_SMALL_LEN) ERR;
      if (nc_inq_var_chunking(ncid, medium_varid, &contig, chunksize_in)) ERR;
      if (contig || chunksize_in[0] * sizeof(long long) > DEFAULT_CHUNK_SIZE) ERR;
      if (nc_inq_var_chunking(ncid, large_varid, &contig, chunksize_in)) ERR;
      if (contig || chunksize_in[0] * sizeof(long long) > DEFAULT_CHUNK_SIZE) ERR;

      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** testing default chunksizes...");
   {
      int nvars, ndims, ngatts, unlimdimid;
      int contig;
#define NUM_DIM 4
#define NUM_TYPE 2
      int ncid;
      int dim_len[NUM_DIM] = {NC_UNLIMITED, 100, 1000, 2000};
      size_t chunksize_in[NUM_DIM];
      int type_id[NUM_TYPE] = {NC_BYTE, NC_INT};
      int dimid[NUM_DIM], varid[NUM_TYPE];
      char dim_name[NC_MAX_NAME + 1], var_name[NC_MAX_NAME + 1];
      int d, t;

      /* Create a netcdf-4 file with NUM_DIM dimensions. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      for (d = 0; d < NUM_DIM; d++)
      {
	 sprintf(dim_name, "dim_%d", dim_len[d]);
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
	 printf("creating dim %s\n", dim_name);
#endif
	 if (nc_def_dim(ncid, dim_name, dim_len[d], &dimid[d])) ERR;
      }

      for (t = 0; t < NUM_TYPE; t++)
      {
	 sprintf(var_name, "var_%d", type_id[t]);
	 if (nc_def_var(ncid, var_name, type_id[t], NUM_DIM, dimid, &varid[t])) ERR;
	 if (nc_inq_var_chunking(ncid, varid[t], &contig, chunksize_in)) ERR;
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
	 printf("chunksizes for %d x %d x %d x %d var: %d x %d x %d x %d (=%d)\n", 
		dim_len[0], dim_len[1], dim_len[2], dim_len[3], 
		(int)chunksize_in[0], (int)chunksize_in[1], (int)chunksize_in[2], 
		(int)chunksize_in[3], 
		(int)(chunksize_in[0] * chunksize_in[1] * chunksize_in[2] * chunksize_in[3]));
#endif
      }

      if (nc_close(ncid)) ERR;

      /* Open the file and check. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
      if (nvars != NUM_TYPE || ndims != NUM_DIM || ngatts != 0 || unlimdimid != 0) ERR;
      
      for (t = 0; t < NUM_TYPE; t++)
      {
	 sprintf(var_name, "var_%d", type_id[t]);
	 if (nc_inq_var_chunking(ncid, varid[t], &contig, chunksize_in)) ERR;
	 if (contig) ERR;
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
	 printf("chunksizes for %d x %d x %d x %d var: %d x %d x %d x %d (=%d)\n", 
		dim_len[0], dim_len[1], dim_len[2], dim_len[3], 
		(int)chunksize_in[0], (int)chunksize_in[1], (int)chunksize_in[2], 
		(int)chunksize_in[3],
		(int)(chunksize_in[0] * chunksize_in[1] * chunksize_in[2] * chunksize_in[3]));
#endif
      }

      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** testing that chunking works on classic mode files...");
   {
#define D_SMALL_LEN2 66
      int ncid;
      int nvars, ndims, ngatts, unlimdimid;
      int contig;
      int ndims_in, natts_in, dimids_in;
      int small_dimid, medium_dimid, large_dimid;
      int small_varid, medium_varid, large_varid;
      char var_name_in[NC_MAX_NAME + 1];
      size_t chunks[1], chunksize_in;
      nc_type xtype_in;

      /* Create a netcdf-4 file with three dimensions. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN2, &small_dimid)) ERR;
      if (nc_def_dim(ncid, D_MEDIUM, D_MEDIUM_LEN, &medium_dimid)) ERR;
      if (nc_def_dim(ncid, D_LARGE, D_LARGE_LEN, &large_dimid)) ERR;

      /* Add three vars. */
      if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
      if (nc_def_var_chunking(ncid, small_varid, 1, NULL)) ERR;

      if (nc_def_var(ncid, V_MEDIUM, NC_INT64, NDIMS1, &medium_dimid, &medium_varid)) ERR;
      chunks[0] = D_MEDIUM_LEN / 100;
      if (nc_def_var_chunking(ncid, medium_varid, 0, chunks)) ERR;
      if (nc_def_var_deflate(ncid, medium_varid, 1, 0, 0)) ERR;

      if (nc_def_var(ncid, V_LARGE, NC_INT64, NDIMS1, &large_dimid, &large_varid)) ERR;
      chunks[0] = D_LARGE_LEN / 1000;
      if (nc_def_var_chunking(ncid, large_varid, 0, chunks)) ERR;
      if (nc_def_var_fletcher32(ncid, large_varid, 1)) ERR;
      if (nc_close(ncid)) ERR;

      /* Open the file and check. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
      if (nvars != 3 || ndims != 3 || ngatts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, &dimids_in, &natts_in)) ERR;
      if (strcmp(var_name_in, V_SMALL) || xtype_in != NC_INT64 || ndims_in != 1 ||
	  natts_in != 0) ERR;
      
      /* Make sure chunking settings are what we expect. */
      if (nc_inq_var_chunking(ncid, small_varid, &contig, &chunksize_in)) ERR;
      if (!contig) ERR;
      if (nc_inq_var_chunking(ncid, medium_varid, &contig, &chunksize_in)) ERR;
      if (contig || chunksize_in != D_MEDIUM_LEN / 100) ERR;
      if (nc_inq_var_chunking(ncid, large_varid, &contig, &chunksize_in)) ERR;
      if (contig || chunksize_in != D_LARGE_LEN / 1000) ERR;

      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** testing many chunking and contiguous variables...");
   {
#define NDIMS_3 3
#define NUM_PLANS 30
#define D_SNEAKINESS "sneakiness"
#define D_SNEAKINESS_LEN 5
#define D_CLEVERNESS "clevernesss"
#define D_CLEVERNESS_LEN 3
#define D_EFFECTIVENESS "effectiveness"
#define D_EFFECTIVENESS_LEN 2

      int ncid, dimids[NDIMS_3], varid[NUM_PLANS];
      size_t chunksize[NDIMS_3] = {D_SNEAKINESS_LEN, D_CLEVERNESS_LEN, 
				   D_EFFECTIVENESS_LEN};
      char plan_name[NC_MAX_NAME + 1];
      int contig;
      size_t chunksize_in[NDIMS_3];
      int i, j;

      /* Create a netcdf-4 file with three dimensions. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, D_SNEAKINESS, D_SNEAKINESS_LEN, &dimids[0])) ERR;
      if (nc_def_dim(ncid, D_CLEVERNESS, D_CLEVERNESS_LEN, &dimids[1])) ERR;
      if (nc_def_dim(ncid, D_EFFECTIVENESS, D_EFFECTIVENESS_LEN, &dimids[2])) ERR;

      /* Oh that tricky Cardinal Richelieu, he had many plans! */
      for (i = 0; i < NUM_PLANS; i++)
      {
	 sprintf(plan_name, "Richelieu_sneaky_plan_%d", i);
	 if (nc_def_var(ncid, plan_name, i % (NC_STRING - 1) + 1, NDIMS_3, 
			dimids, &varid[i])) ERR;
	 if (i % 2 && nc_def_var_chunking(ncid, varid[i], 0, chunksize)) ERR;
      }
      
      /* Check the chunking. */
      for (i = 0; i < NUM_PLANS; i++)
      {
	 if (nc_inq_var_chunking(ncid, varid[i], &contig, chunksize_in)) ERR;
	 if (i % 2)
	 {
	    for (j = 0; j < NDIMS_3; j++)
	       if (chunksize_in[j] != chunksize[j]) ERR;
	 } 
	 else
	    if (!contig) ERR;
      }
      if (nc_close(ncid)) ERR;

      /* Open the file and check. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
      /* Check the chunking. */
      for (i = 0; i < NUM_PLANS; i++)
      {
	 if (nc_inq_var_chunking(ncid, varid[i], &contig, chunksize_in)) ERR;
	 if (i % 2)
	 {
	    for (j = 0; j < NDIMS_3; j++)
	       if (chunksize_in[j] != chunksize[j]) ERR;
	 } 
	 else
	    if (!contig) ERR;
      }
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** testing that too large chunksizes fail...");
   {
#define D_SMALL_LEN2 66
      int stat = NC_NOERR;
      int ncid;
      int nvars, ndims, ngatts, unlimdimid;
      int contig;
      int ndims_in, natts_in, dimids_in;
      int small_dimid, medium_dimid, large_dimid;
      int small_varid;
      char var_name_in[NC_MAX_NAME + 1];
      size_t chunks[1], chunksize_in;
      nc_type xtype_in;

      /* Create a netcdf-4 file with three dimensions. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN2, &small_dimid)) ERR;

      /* Add one var. */
      if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;

      /* Attempt to set too large chunksizes */
      chunks[0] = D_SMALL_LEN2 + 1;
      stat = nc_def_var_chunking(ncid, small_varid, NC_CHUNKED, chunks);
      if(stat != NC_EBADCHUNK) {
	printf("Return code is '%s', expected NC_BADCHUNK",nc_strerror(stat));
	ERR;
      }
      /* try agains with proper chunksize */
      chunks[0] = D_SMALL_LEN2;
      stat = nc_def_var_chunking(ncid, small_varid, NC_CHUNKED, chunks);
      if(stat != NC_NOERR) {
	printf("Return code is '%s', expected NC_NOERR",nc_strerror(stat));
	ERR;
      }
      if (nc_abort(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Ejemplo n.º 14
0
int
main(int argc, char **argv)
{
    printf("\n*** Testing 'Fileinfo attributes.\n");

    {
	hid_t fileid;
	hid_t fcplid;
	hid_t scalar_spaceid;

        printf("*** creating test file using HDF5 directly %s...", HDFFILE);

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

        /* Set creation ordering for file, so we can revise its contents later */
        if((fcplid = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
        if(H5Pset_link_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
        if(H5Pset_attr_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;

        /* Create new file, using default properties */
        if((fileid = H5Fcreate(HDFFILE, H5F_ACC_TRUNC, fcplid, H5P_DEFAULT)) < 0) ERR;
        /* Close file creation property list */
        if(H5Pclose(fcplid) < 0) ERR;

        /* Add attributes to root group */
        {
            hid_t scalar_spaceid = -1;
            hid_t attid = -1;

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

            /* Create attribute with native integer datatype on object */
            if((attid = H5Acreate2(fileid, INT_ATT_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR;
            if(H5Aclose(attid) < 0) ERR;

            /* Clean up objects created */
            if(H5Sclose(scalar_spaceid) < 0) ERR;
        }

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

    {
	int root, grpid, varid, stat, natts, id;
	int data = 17;
	const char* sdata = "text";
	char ncprops[8192];
	size_t len;
	int dimid;
        nc_type xtype;
	char name[NC_MAX_NAME];

        printf("\n*** creating netcdf-4 test file using netCDF %s...", NC4FILE);

	if(nc_create(NC4FILE,NC_WRITE|NC_CLOBBER|NC_NETCDF4,&root)!=0) ERR;
	/* Create global attribute */
	if(nc_put_att_int(root,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
	/* Create global variable */
	if(nc_def_var(root,INT_VAR_NAME,NC_INT,0,NULL,&varid)!=0) ERR;
	/* Create attribute on var */
	if(nc_put_att_int(root,varid,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
	/* Create global subgroup */
	if(nc_def_grp(root,GROUPNAME,&grpid)!=0) ERR;
	/* Create global attribute in the group */
	if(nc_put_att_int(grpid,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
	/* Create _NCProperties as var attr and as subgroup attribute */
	if(nc_put_att_text(grpid,NC_GLOBAL,NCPROPS,strlen(sdata),sdata)!=0) ERR;
	if(nc_put_att_text(root,varid,NCPROPS,strlen(sdata),sdata)!=0) ERR;
	/* Create var + dimension to cause e.g. dimscales to appear */
	if(nc_def_dim(root,DIMNAME,(size_t)4,&dimid)!=0) ERR;
	if(nc_def_var(root,DIMNAME,NC_INT,1,&dimid,&varid)!=0) ERR; /* same name */
	/* Close, then re-open */
	if(nc_close(root)) ERR;
	if(nc_open(NC4FILE,NC_WRITE|NC_NETCDF4,&root)!=0) ERR;

	/* Is all invisible attributes actually invisible vis-a-vis nc_inq? */
        if(nc_inq(root,NULL,NULL,&natts,NULL)!=0) ERR;
	if(natts != 1) ERR;

	/* Now, fiddle with the NCPROPS attribute */

	/* Get its metadata */
	if(nc_inq_att(root,NC_GLOBAL,NCPROPS,&xtype,&len)!=0) ERR;
	if(xtype != NC_CHAR) ERR;

	/* Read in two ways */
	if(nc_get_att_text(root,NC_GLOBAL,NCPROPS,ncprops)!=0) ERR;
	if(strlen(ncprops) != len) ERR;

	/* Attempt to get attribute metadata piecemeal; some will fail */
	id = -1;
	stat = nc_inq_attid(root,NC_GLOBAL,NCPROPS,&id);
	if(stat == NC_NOERR) ERR;
	stat = nc_inq_attname(root,NC_GLOBAL,id,name);
	if(stat == NC_NOERR) ERR;
	if(nc_inq_atttype(root,NC_GLOBAL,NCPROPS,&xtype)!=0) ERR;
	if(xtype != NC_CHAR) ERR;
	if(nc_inq_attlen(root,NC_GLOBAL,NCPROPS,&len)!=0) ERR;
	if(len != strlen(ncprops)) ERR;

	/*Overwrite _NCProperties root attribute; should fail */
	stat = nc_put_att_text(root,NC_GLOBAL,NCPROPS,strlen(sdata),sdata);
	if(stat == NC_NOERR) ERR;

	/* Delete; should fail */
	stat = nc_del_att(root,NC_GLOBAL,NCPROPS);
        if(stat != NC_ENOTATT) ERR;

	/* Ditto _SuperblockVersion */

	/* Get its metadata */
	if(nc_inq_att(root,NC_GLOBAL,SUPERBLOCKATT,&xtype,&len)!=0) ERR;
	if(xtype != NC_INT) ERR;
	if(len != 1) ERR;

	if(nc_get_att_int(root,NC_GLOBAL,SUPERBLOCKATT,&data)!=0) ERR;

	/* Attempt to get attribute metadata piecemeal */
	stat = nc_inq_attid(root,NC_GLOBAL,SUPERBLOCKATT,&id);
	if(stat == NC_NOERR) ERR;
	stat = nc_inq_attname(root,NC_GLOBAL,id,name);
	if(stat == NC_NOERR) ERR;
	if(nc_inq_atttype(root,NC_GLOBAL,SUPERBLOCKATT,&xtype)!=0) ERR;
	if(xtype != NC_INT) ERR;
	if(nc_inq_attlen(root,NC_GLOBAL,SUPERBLOCKATT,&len)!=0) ERR;
	if(len != 1) ERR;

	/*Overwrite; should fail */
	stat = nc_put_att_int(root,NC_GLOBAL,NCPROPS,NC_INT,1,&data);
	if(stat == NC_NOERR) ERR;

	/* Delete; should fail */
	stat = nc_del_att(root,NC_GLOBAL,SUPERBLOCKATT);
        if(stat == NC_NOERR) ERR;

	/* Ditto _IsNetcdf4 */

	/* Get its metadata */
	if(nc_inq_att(root,NC_GLOBAL,ISNETCDF4ATT,&xtype,&len)!=0) ERR;
	if(xtype != NC_INT) ERR;
	if(len != 1) ERR;

	if(nc_get_att_int(root,NC_GLOBAL,ISNETCDF4ATT,&data)!=0) ERR;

	/* Attempt to get attribute metadata piecemeal */
	stat = nc_inq_attid(root,NC_GLOBAL,ISNETCDF4ATT,&id);
	if(stat == NC_NOERR) ERR;
	stat = nc_inq_attname(root,NC_GLOBAL,id,name);
	if(stat == NC_NOERR) ERR;
	if(nc_inq_atttype(root,NC_GLOBAL,ISNETCDF4ATT,&xtype)!=0) ERR;
	if(xtype != NC_INT) ERR;
	if(nc_inq_attlen(root,NC_GLOBAL,ISNETCDF4ATT,&len)!=0) ERR;
	if(len != 1) ERR;

	/*Overwrite; should fail */
	stat = nc_put_att_int(root,NC_GLOBAL,ISNETCDF4ATT,NC_INT,1,&data);
	if(stat == NC_NOERR) ERR;

	/* Delete; should fail */
	stat = nc_del_att(root,NC_GLOBAL,ISNETCDF4ATT);
        if(stat == NC_NOERR) ERR;

	if(nc_close(root)!=0) ERR;
    }

    SUMMARIZE_ERR;
    FINAL_RESULTS;
}
Ejemplo n.º 15
0
/* See ncd3dispatch.c for other version */
int
NCD3_open(const char * path, int mode,
               int basepe, size_t *chunksizehintp,
 	       int useparallel, void* mpidata,
               NC_Dispatch* dispatch, NC** ncpp)
{
    NCerror ncstat = NC_NOERR;
    OCerror ocstat = OC_NOERR;
    NC* drno = NULL;
    NCDAPCOMMON* dapcomm = NULL;
    const char* value;
    char* tmpname = NULL;

    if(!nc3dinitialized) nc3dinitialize();

    if(path == NULL)
	return NC_EDAPURL;
    if(dispatch == NULL) PANIC("NC3D_open: no dispatch table");

    /* Setup our NC and NCDAPCOMMON state*/
    drno = (NC*)calloc(1,sizeof(NC));
    if(drno == NULL) {ncstat = NC_ENOMEM; goto done;}

    /* compute an ncid */
    ncstat = add_to_NCList(drno);
    if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}

    dapcomm = (NCDAPCOMMON*)calloc(1,sizeof(NCDAPCOMMON));
    if(dapcomm == NULL) {ncstat = NC_ENOMEM; goto done;}

    drno->dispatch = dispatch;
    drno->dispatchdata = dapcomm;
    dapcomm->controller = (NC*)drno;

    dapcomm->cdf.separator = ".";
    dapcomm->cdf.smallsizelimit = DFALTSMALLLIMIT;
    dapcomm->cdf.cache = createnccache();

#ifdef HAVE_GETRLIMIT
    { struct rlimit rl;
      if(getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
	dapcomm->cdf.cache->cachecount = (size_t)(rl.rlim_cur / 2);
      }
    }
#endif

#ifdef OCCOMPILEBYDEFAULT
    /* set the compile flag by default */
    dapcomm->oc.rawurltext = (char*)emalloc(strlen(path)+strlen("[compile]")+1);
    strcpy(dapcomm->oc.rawurltext,"[compile]");
    strcat(dapcomm->oc.rawurltext, path);    
#else
    dapcomm->oc.rawurltext = strdup(path);
#endif

    nc_uriparse(dapcomm->oc.rawurltext,&dapcomm->oc.url);

    /* parse the client parameters */
    nc_uridecodeparams(dapcomm->oc.url);

    if(!constrainable34(dapcomm->oc.url))
	SETFLAG(dapcomm->controls,NCF_UNCONSTRAINABLE);

    /* Use libsrc code for storing metadata */
    tmpname = nulldup(PSEUDOFILE);
    /* Now, use the file to create the netcdf file */
    if(sizeof(size_t) == sizeof(unsigned int))
	ncstat = nc_create(tmpname,NC_CLOBBER,&drno->substrate);
    else
	ncstat = nc_create(tmpname,NC_CLOBBER|NC_64BIT_OFFSET,&drno->substrate);
    if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}

    /* free the filename so it will automatically go away*/
    unlink(tmpname);
    nullfree(tmpname);

    /* Avoid fill */
    nc_set_fill(drno->substrate,NC_NOFILL,NULL);

    dapcomm->oc.dapconstraint = (DCEconstraint*)dcecreate(CES_CONSTRAINT);
    dapcomm->oc.dapconstraint->projections = nclistnew();
    dapcomm->oc.dapconstraint->selections = nclistnew();

    /* Parse constraints to make sure they are syntactically correct */
    ncstat = parsedapconstraints(dapcomm,dapcomm->oc.url->constraint,dapcomm->oc.dapconstraint);
    if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}

    /* Complain if we are unconstrainable but have constraints */
    if(FLAGSET(dapcomm->controls,NCF_UNCONSTRAINABLE)) {
	if(dapcomm->oc.url->constraint != NULL
	   && strlen(dapcomm->oc.url->constraint) > 0) {
	    nclog(NCLOGWARN,"Attempt to constrain an unconstrainable data source: %s",
		   dapcomm->oc.url->constraint);
	}
    }

    /* Construct a url for oc minus any parameters */
    dapcomm->oc.urltext = nc_uribuild(dapcomm->oc.url,NULL,NULL,
				(NC_URIALL ^ NC_URICONSTRAINTS));

    /* Pass to OC */
    ocstat = oc_open(dapcomm->oc.urltext,&dapcomm->oc.conn);
    if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto done;}

    nullfree(dapcomm->oc.urltext); /* clean up */
    dapcomm->oc.urltext = NULL;

    /* process control client parameters */
    applyclientparamcontrols3(dapcomm);

    /* Turn on logging; only do this after oc_open*/
    if((value = paramvalue34(dapcomm,"log")) != NULL) {
	ncloginit();
        ncsetlogging(1);
        nclogopen(value);
	oc_loginit();
        oc_setlogging(1);
        oc_logopen(value);
    }

    /* fetch and build the (almost) unconstrained DDS for use as
       template */
    ncstat = fetchtemplatemetadata3(dapcomm);
    if(ncstat != NC_NOERR) goto done;

    /* fetch and build the constrained DDS */
    ncstat = fetchconstrainedmetadata3(dapcomm);
    if(ncstat != NC_NOERR) goto done;

#ifdef DEBUG2
fprintf(stderr,"constrained dds: %s\n",dumptree(dapcomm->cdf.ddsroot));
#endif


    /* The following actions are (mostly) WRT to the constrained tree */

    /* Accumulate useful nodes sets  */
    ncstat = computecdfnodesets3(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* Fix grids */
    ncstat = fixgrids3(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* Locate and mark usable sequences */
    ncstat = sequencecheck3(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* suppress variables not in usable sequences */
    ncstat = suppressunusablevars3(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* apply client parameters */
    ncstat = applyclientparams34(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* Add (as needed) string dimensions*/
    ncstat = addstringdims(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    if(nclistlength(dapcomm->cdf.seqnodes) > 0) {
	/* Build the sequence related dimensions */
        ncstat = defseqdims(dapcomm);
        if(ncstat) {THROWCHK(ncstat); goto done;}
    }

    /* Define the dimsetplus and dimsetall lists */
    ncstat = definedimsets3(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* Re-compute the dimension names*/
    ncstat = computecdfdimnames34(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* Deal with zero size dimensions */
    ncstat = fixzerodims3(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* Attempt to use the DODS_EXTRA info to turn
       one of the dimensions into unlimited.
       Assume computecdfdimnames34 has already been called.
    */
    ncstat = defrecorddim3(dapcomm);
    if(ncstat) {THROWCHK(ncstat); goto done;}
    if(dapcomm->cdf.recorddimname != NULL
       && nclistlength(dapcomm->cdf.seqnodes) > 0) {
	/*nclog(NCLOGWARN,"unlimited dimension specified, but sequences exist in DDS");*/
	PANIC("unlimited dimension specified, but sequences exist in DDS");	
    }

    /* Re-compute the var names*/
    ncstat = computecdfvarnames3(dapcomm,dapcomm->cdf.ddsroot,dapcomm->cdf.varnodes);
    if(ncstat) {THROWCHK(ncstat); goto done;}

    /* Transfer data from the unconstrained DDS data to the unconstrained DDS */
    ncstat = dimimprint3(dapcomm);
    if(ncstat) goto done;

    /* Process the constraints to map to the constrained CDF tree */
    /* (must follow fixgrids3 */
    ncstat = mapconstraints3(dapcomm->oc.dapconstraint,dapcomm->cdf.ddsroot);
    if(ncstat != NC_NOERR) goto done;

    /* Canonicalize the constraint */
    ncstat = fixprojections(dapcomm->oc.dapconstraint->projections);
    if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}

    /* Fill in segment information */
    ncstat = qualifyconstraints3(dapcomm->oc.dapconstraint);
    if(ncstat != NC_NOERR) goto done;

    /* using the modified constraint, rebuild the constraint string */
    if(FLAGSET(dapcomm->controls,NCF_UNCONSTRAINABLE)) {
	/* ignore all constraints */
	dapcomm->oc.urltext = nc_uribuild(dapcomm->oc.url,NULL,NULL,0);
    } else {
	char* constraintstring = buildconstraintstring3(dapcomm->oc.dapconstraint);
        nc_urisetconstraints(dapcomm->oc.url,constraintstring);
	nullfree(constraintstring);
        dapcomm->oc.urltext = nc_uribuild(dapcomm->oc.url,NULL,NULL,NC_URICONSTRAINTS);
    }

#ifdef DEBUG
fprintf(stderr,"ncdap3: final constraint: %s\n",dapcomm->oc.url->constraint);
#endif

    /* Estimate the variable sizes */
    estimatevarsizes3(dapcomm);

    /* Build the meta data */
    ncstat = buildncstructures3(dapcomm);
    if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}

    /* Do any necessary data prefetch */
    if(FLAGSET(dapcomm->controls,NCF_PREFETCH)) {
        ncstat = prefetchdata3(dapcomm);
        if(ncstat != NC_NOERR) {
            del_from_NCList((NC*)drno); /* undefine here */
	    {THROWCHK(ncstat); goto done;}
	}
    }

    {
        /* Mark as no longer writable and no longer indef;
           requires breaking abstraction  */
	NC* nc;
        ncstat = NC_check_id(drno->substrate, &nc);
        /* Mark as no longer writeable */
        fClr(nc->nciop->ioflags, NC_WRITE);
        /* Mark as no longer indef;
           (do NOT use nc_enddef until diskless is working)*/
	fSet(nc->flags, NC_INDEF);	
    }

    if(ncpp) *ncpp = (NC*)drno;

    return ncstat;

done:
    if(drno != NULL) NCD3_abort(drno->ext_ncid);
    if(ocstat != OC_NOERR) ncstat = ocerrtoncerr(ocstat);
    return THROW(ncstat);
}
Ejemplo n.º 16
0
int
main(int argc, char **argv)
{
   int ncid;
   int dimid, varid;
   char name_in[NC_MAX_NAME+1];
   int class_in;
   size_t size_in;
   char *value_in;
   nc_type att_type;
   size_t att_len;

   int i;

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

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

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

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


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

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

   if (nc_close(ncid)) ERR; 

   SUMMARIZE_ERR;
#endif /* EXTRA_TESTS */
   FINAL_RESULTS;
#ifdef USE_PARALLEL
   MPI_Finalize();
#endif   
}
Ejemplo n.º 17
0
int
main() {			/* create nc_sync.nc */

    int  ncid;			/* netCDF id */

    /* dimension ids */
    int dim0_dim;
    int dim1_dim;
    int dim2_dim;
    int dim3_dim;

    /* dimension lengths */
    size_t dim0_len = 53;
    size_t dim1_len = 67;
    size_t dim2_len = 30;
    size_t dim3_len = NC_UNLIMITED;

    /* variable ids */
    int var0_id;
    int var1_id;
    int var2_id;
    int var3_id;
    int var4_id;
    int var5_id;
    int var6_id;
    int var7_id;
    int var8_id;
    int var9_id;
    int var10_id;
    int var11_id;
    int var12_id;
    int var13_id;
    int var14_id;
    int var15_id;
    int var16_id;
    int var17_id;
    int var18_id;
    int var19_id;
    int var20_id;
    int var21_id;
    int var22_id;
    int var23_id;
    int var24_id;
    int var25_id;
    int var26_id;
    int var27_id;
    int var28_id;
    int var29_id;
    int var30_id;
    int var31_id;
    int var32_id;
    int var33_id;
    int var34_id;
    int var35_id;
    int var36_id;

    /* rank (number of dimensions) for each variable */
#  define RANK_var0 4
#  define RANK_var1 4
#  define RANK_var2 4
#  define RANK_var3 4
#  define RANK_var4 4
#  define RANK_var5 4
#  define RANK_var6 4
#  define RANK_var7 4
#  define RANK_var8 4
#  define RANK_var9 4
#  define RANK_var10 4
#  define RANK_var11 4
#  define RANK_var12 4
#  define RANK_var13 4
#  define RANK_var14 4
#  define RANK_var15 4
#  define RANK_var16 4
#  define RANK_var17 4
#  define RANK_var18 4
#  define RANK_var19 4
#  define RANK_var20 4
#  define RANK_var21 4
#  define RANK_var22 4
#  define RANK_var23 4
#  define RANK_var24 4
#  define RANK_var25 4
#  define RANK_var26 4
#  define RANK_var27 4
#  define RANK_var28 4
#  define RANK_var29 4
#  define RANK_var30 4
#  define RANK_var31 4
#  define RANK_var32 4
#  define RANK_var33 4
#  define RANK_var34 4
#  define RANK_var35 4
#  define RANK_var36 4

    /* variable shapes */
    int var0_dims[RANK_var0];
    int var1_dims[RANK_var1];
    int var2_dims[RANK_var2];
    int var3_dims[RANK_var3];
    int var4_dims[RANK_var4];
    int var5_dims[RANK_var5];
    int var6_dims[RANK_var6];
    int var7_dims[RANK_var7];
    int var8_dims[RANK_var8];
    int var9_dims[RANK_var9];
    int var10_dims[RANK_var10];
    int var11_dims[RANK_var11];
    int var12_dims[RANK_var12];
    int var13_dims[RANK_var13];
    int var14_dims[RANK_var14];
    int var15_dims[RANK_var15];
    int var16_dims[RANK_var16];
    int var17_dims[RANK_var17];
    int var18_dims[RANK_var18];
    int var19_dims[RANK_var19];
    int var20_dims[RANK_var20];
    int var21_dims[RANK_var21];
    int var22_dims[RANK_var22];
    int var23_dims[RANK_var23];
    int var24_dims[RANK_var24];
    int var25_dims[RANK_var25];
    int var26_dims[RANK_var26];
    int var27_dims[RANK_var27];
    int var28_dims[RANK_var28];
    int var29_dims[RANK_var29];
    int var30_dims[RANK_var30];
    int var31_dims[RANK_var31];
    int var32_dims[RANK_var32];
    int var33_dims[RANK_var33];
    int var34_dims[RANK_var34];
    int var35_dims[RANK_var35];
    int var36_dims[RANK_var36];

    /* enter define mode */
    int stat = nc_create("nc_sync.nc", NC_CLOBBER, &ncid);
    check_err(stat,__LINE__,__FILE__);

    /* define dimensions */
    stat = nc_def_dim(ncid, "dim0", dim0_len, &dim0_dim);
    check_err(stat,__LINE__,__FILE__);
    stat = nc_def_dim(ncid, "dim1", dim1_len, &dim1_dim);
    check_err(stat,__LINE__,__FILE__);
    stat = nc_def_dim(ncid, "dim2", dim2_len, &dim2_dim);
    check_err(stat,__LINE__,__FILE__);
    stat = nc_def_dim(ncid, "dim3", dim3_len, &dim3_dim);
    check_err(stat,__LINE__,__FILE__);

    /* define variables */

    var0_dims[0] = dim3_dim;
    var0_dims[1] = dim2_dim;
    var0_dims[2] = dim1_dim;
    var0_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var0", NC_DOUBLE, RANK_var0, var0_dims, &var0_id);
    check_err(stat,__LINE__,__FILE__);

    var1_dims[0] = dim3_dim;
    var1_dims[1] = dim2_dim;
    var1_dims[2] = dim1_dim;
    var1_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var1", NC_DOUBLE, RANK_var1, var1_dims, &var1_id);
    check_err(stat,__LINE__,__FILE__);

    var2_dims[0] = dim3_dim;
    var2_dims[1] = dim2_dim;
    var2_dims[2] = dim1_dim;
    var2_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var2", NC_DOUBLE, RANK_var2, var2_dims, &var2_id);
    check_err(stat,__LINE__,__FILE__);

    var3_dims[0] = dim3_dim;
    var3_dims[1] = dim2_dim;
    var3_dims[2] = dim1_dim;
    var3_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var3", NC_DOUBLE, RANK_var3, var3_dims, &var3_id);
    check_err(stat,__LINE__,__FILE__);

    var4_dims[0] = dim3_dim;
    var4_dims[1] = dim2_dim;
    var4_dims[2] = dim1_dim;
    var4_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var4", NC_DOUBLE, RANK_var4, var4_dims, &var4_id);
    check_err(stat,__LINE__,__FILE__);

    var5_dims[0] = dim3_dim;
    var5_dims[1] = dim2_dim;
    var5_dims[2] = dim1_dim;
    var5_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var5", NC_DOUBLE, RANK_var5, var5_dims, &var5_id);
    check_err(stat,__LINE__,__FILE__);

    var6_dims[0] = dim3_dim;
    var6_dims[1] = dim2_dim;
    var6_dims[2] = dim1_dim;
    var6_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var6", NC_DOUBLE, RANK_var6, var6_dims, &var6_id);
    check_err(stat,__LINE__,__FILE__);

    var7_dims[0] = dim3_dim;
    var7_dims[1] = dim2_dim;
    var7_dims[2] = dim1_dim;
    var7_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var7", NC_DOUBLE, RANK_var7, var7_dims, &var7_id);
    check_err(stat,__LINE__,__FILE__);

    var8_dims[0] = dim3_dim;
    var8_dims[1] = dim2_dim;
    var8_dims[2] = dim1_dim;
    var8_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var8", NC_DOUBLE, RANK_var8, var8_dims, &var8_id);
    check_err(stat,__LINE__,__FILE__);

    var9_dims[0] = dim3_dim;
    var9_dims[1] = dim2_dim;
    var9_dims[2] = dim1_dim;
    var9_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var9", NC_DOUBLE, RANK_var9, var9_dims, &var9_id);
    check_err(stat,__LINE__,__FILE__);

    var10_dims[0] = dim3_dim;
    var10_dims[1] = dim2_dim;
    var10_dims[2] = dim1_dim;
    var10_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var10", NC_DOUBLE, RANK_var10, var10_dims, &var10_id);
    check_err(stat,__LINE__,__FILE__);

    var11_dims[0] = dim3_dim;
    var11_dims[1] = dim2_dim;
    var11_dims[2] = dim1_dim;
    var11_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var11", NC_DOUBLE, RANK_var11, var11_dims, &var11_id);
    check_err(stat,__LINE__,__FILE__);

    var12_dims[0] = dim3_dim;
    var12_dims[1] = dim2_dim;
    var12_dims[2] = dim1_dim;
    var12_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var12", NC_DOUBLE, RANK_var12, var12_dims, &var12_id);
    check_err(stat,__LINE__,__FILE__);

    var13_dims[0] = dim3_dim;
    var13_dims[1] = dim2_dim;
    var13_dims[2] = dim1_dim;
    var13_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var13", NC_DOUBLE, RANK_var13, var13_dims, &var13_id);
    check_err(stat,__LINE__,__FILE__);

    var14_dims[0] = dim3_dim;
    var14_dims[1] = dim2_dim;
    var14_dims[2] = dim1_dim;
    var14_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var14", NC_DOUBLE, RANK_var14, var14_dims, &var14_id);
    check_err(stat,__LINE__,__FILE__);

    var15_dims[0] = dim3_dim;
    var15_dims[1] = dim2_dim;
    var15_dims[2] = dim1_dim;
    var15_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var15", NC_DOUBLE, RANK_var15, var15_dims, &var15_id);
    check_err(stat,__LINE__,__FILE__);

    var16_dims[0] = dim3_dim;
    var16_dims[1] = dim2_dim;
    var16_dims[2] = dim1_dim;
    var16_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var16", NC_DOUBLE, RANK_var16, var16_dims, &var16_id);
    check_err(stat,__LINE__,__FILE__);

    var17_dims[0] = dim3_dim;
    var17_dims[1] = dim2_dim;
    var17_dims[2] = dim1_dim;
    var17_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var17", NC_DOUBLE, RANK_var17, var17_dims, &var17_id);
    check_err(stat,__LINE__,__FILE__);

    var18_dims[0] = dim3_dim;
    var18_dims[1] = dim2_dim;
    var18_dims[2] = dim1_dim;
    var18_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var18", NC_DOUBLE, RANK_var18, var18_dims, &var18_id);
    check_err(stat,__LINE__,__FILE__);

    var19_dims[0] = dim3_dim;
    var19_dims[1] = dim2_dim;
    var19_dims[2] = dim1_dim;
    var19_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var19", NC_DOUBLE, RANK_var19, var19_dims, &var19_id);
    check_err(stat,__LINE__,__FILE__);

    var20_dims[0] = dim3_dim;
    var20_dims[1] = dim2_dim;
    var20_dims[2] = dim1_dim;
    var20_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var20", NC_DOUBLE, RANK_var20, var20_dims, &var20_id);
    check_err(stat,__LINE__,__FILE__);

    var21_dims[0] = dim3_dim;
    var21_dims[1] = dim2_dim;
    var21_dims[2] = dim1_dim;
    var21_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var21", NC_DOUBLE, RANK_var21, var21_dims, &var21_id);
    check_err(stat,__LINE__,__FILE__);

    var22_dims[0] = dim3_dim;
    var22_dims[1] = dim2_dim;
    var22_dims[2] = dim1_dim;
    var22_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var22", NC_DOUBLE, RANK_var22, var22_dims, &var22_id);
    check_err(stat,__LINE__,__FILE__);

    var23_dims[0] = dim3_dim;
    var23_dims[1] = dim2_dim;
    var23_dims[2] = dim1_dim;
    var23_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var23", NC_DOUBLE, RANK_var23, var23_dims, &var23_id);
    check_err(stat,__LINE__,__FILE__);

    var24_dims[0] = dim3_dim;
    var24_dims[1] = dim2_dim;
    var24_dims[2] = dim1_dim;
    var24_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var24", NC_DOUBLE, RANK_var24, var24_dims, &var24_id);
    check_err(stat,__LINE__,__FILE__);

    var25_dims[0] = dim3_dim;
    var25_dims[1] = dim2_dim;
    var25_dims[2] = dim1_dim;
    var25_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var25", NC_DOUBLE, RANK_var25, var25_dims, &var25_id);
    check_err(stat,__LINE__,__FILE__);

    var26_dims[0] = dim3_dim;
    var26_dims[1] = dim2_dim;
    var26_dims[2] = dim1_dim;
    var26_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var26", NC_DOUBLE, RANK_var26, var26_dims, &var26_id);
    check_err(stat,__LINE__,__FILE__);

    var27_dims[0] = dim3_dim;
    var27_dims[1] = dim2_dim;
    var27_dims[2] = dim1_dim;
    var27_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var27", NC_DOUBLE, RANK_var27, var27_dims, &var27_id);
    check_err(stat,__LINE__,__FILE__);

    var28_dims[0] = dim3_dim;
    var28_dims[1] = dim2_dim;
    var28_dims[2] = dim1_dim;
    var28_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var28", NC_DOUBLE, RANK_var28, var28_dims, &var28_id);
    check_err(stat,__LINE__,__FILE__);

    var29_dims[0] = dim3_dim;
    var29_dims[1] = dim2_dim;
    var29_dims[2] = dim1_dim;
    var29_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var29", NC_DOUBLE, RANK_var29, var29_dims, &var29_id);
    check_err(stat,__LINE__,__FILE__);

    var30_dims[0] = dim3_dim;
    var30_dims[1] = dim2_dim;
    var30_dims[2] = dim1_dim;
    var30_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var30", NC_DOUBLE, RANK_var30, var30_dims, &var30_id);
    check_err(stat,__LINE__,__FILE__);

    var31_dims[0] = dim3_dim;
    var31_dims[1] = dim2_dim;
    var31_dims[2] = dim1_dim;
    var31_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var31", NC_DOUBLE, RANK_var31, var31_dims, &var31_id);
    check_err(stat,__LINE__,__FILE__);

    var32_dims[0] = dim3_dim;
    var32_dims[1] = dim2_dim;
    var32_dims[2] = dim1_dim;
    var32_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var32", NC_DOUBLE, RANK_var32, var32_dims, &var32_id);
    check_err(stat,__LINE__,__FILE__);

    var33_dims[0] = dim3_dim;
    var33_dims[1] = dim2_dim;
    var33_dims[2] = dim1_dim;
    var33_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var33", NC_DOUBLE, RANK_var33, var33_dims, &var33_id);
    check_err(stat,__LINE__,__FILE__);

    var34_dims[0] = dim3_dim;
    var34_dims[1] = dim2_dim;
    var34_dims[2] = dim1_dim;
    var34_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var34", NC_DOUBLE, RANK_var34, var34_dims, &var34_id);
    check_err(stat,__LINE__,__FILE__);

    var35_dims[0] = dim3_dim;
    var35_dims[1] = dim2_dim;
    var35_dims[2] = dim1_dim;
    var35_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var35", NC_DOUBLE, RANK_var35, var35_dims, &var35_id);
    check_err(stat,__LINE__,__FILE__);

    var36_dims[0] = dim3_dim;
    var36_dims[1] = dim2_dim;
    var36_dims[2] = dim1_dim;
    var36_dims[3] = dim0_dim;
    stat = nc_def_var(ncid, "var36", NC_DOUBLE, RANK_var36, var36_dims, &var36_id);
    check_err(stat,__LINE__,__FILE__);

    /* leave define mode */
    stat = nc_enddef (ncid);
    check_err(stat,__LINE__,__FILE__);

    nc_sync_sub(ncid);

    stat = nc_close(ncid);
    check_err(stat,__LINE__,__FILE__);

    return 0;
}
Ejemplo n.º 18
0
int
main(int argc, char **argv)
{
nc_set_log_level(0);
   printf("\n*** Testing netcdf-4 dimensions even more.\n");
   printf("*** testing netcdf-4 dimension inheritance...");
   {
#define FILE_NAME "tst_dims3.nc"
#define RANK_time 1
#define GRP_NAME  "G"
#define TIME_NAME "time"
#define VAR2_NAME "z"
#define TIME_RANK 1
#define NUM_TIMES 2
      int ncid, grpid;
      int time_dim, time_dim_in;
      int time_var, z_var;
      size_t len;
      int time_data[NUM_TIMES] = {1, 2} ;
      size_t time_startset[TIME_RANK] = {0} ;
      size_t time_countset[TIME_RANK] = {NUM_TIMES} ;

      /* Create file with unlimited dim and associated coordinate
       * variable in root group, another variable that uses unlimited
       * dim in subgroup. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_grp(ncid, GRP_NAME, &grpid)) ERR;
      if (nc_def_dim(ncid, TIME_NAME, NC_UNLIMITED, &time_dim)) ERR;
      if (nc_def_var(ncid, TIME_NAME, NC_INT, TIME_RANK, &time_dim, 
		     &time_var)) ERR;
      if (nc_def_var(grpid, VAR2_NAME, NC_INT, TIME_RANK, &time_dim, 
		     &z_var)) ERR;
      if (nc_enddef(ncid)) ERR;

      /* Assign data to time variable, creating two times */
      if (nc_put_vara(ncid, time_dim, time_startset, time_countset, 
		      time_data)) ERR;

      /* Check the dim len from the root group */
      if (nc_inq_dimlen(ncid, time_dim, &len)) ERR;
      if (len != NUM_TIMES) ERR;

      /* Check the dim len from the sub group */
      if (nc_inq_dimlen(grpid, time_dim, &len)) ERR;
      if (len != NUM_TIMES) ERR;
      if (nc_close(ncid)) ERR;

      /* Now check how many times there are from the subgroup */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_ncid(ncid, GRP_NAME, &grpid)) ERR;
      if (nc_inq_dimid(ncid, TIME_NAME, &time_dim)) ERR;

      /* Check the dim len from the root group */
      if (nc_inq_dimlen(ncid, time_dim, &len)) ERR;
      if (len != NUM_TIMES) ERR;

      /* Check the dim len from the sub group */
      if (nc_inq_dimlen(grpid, time_dim, &len)) ERR;
      if (len != NUM_TIMES) ERR;

      /* Find the dimension by name. */
      if (nc_inq_dimid(grpid, TIME_NAME, &time_dim_in)) ERR;
      if (time_dim_in != time_dim) ERR;

      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** testing a scalar coordinate dimension...");
   {
      int ncid, dimid, varid, stat;
      float data = 42.5;
      
      /* Create a scalar coordinate dimension. The only reason that
       * the user can ever possibly have for doing this is just
       * because they like to make life difficult for poor, poor
       * netCDF programmers, trapped in this horrible place, in a
       * Rocky Mountain valley, drenched in sunlight, with a stream
       * quietly gurgling, deer feeding on the grasses, and all those
       * damn birds chirping! */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR_RET;
      if (nc_def_dim(ncid, "scalar", 0, &dimid)) ERR_RET;
      if (nc_def_var(ncid, "scalar", NC_FLOAT, 0, &dimid, &varid)) ERR_RET;
      if (nc_put_var_float(ncid, varid, &data)) ERR_RET;
      if (nc_close(ncid))
	ERR_RET;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Ejemplo n.º 19
0
/*
 * NETCDF_open()
 * Open the trajectory specified by the filename and accessMode in trajInfo as 
 * a NETCDF traj.
 * Return 0 on success, 1 on failure
 */
int NETCDF_open(coordinateInfo *trajInfo) {
#ifdef BINTRAJ
  int err,ncid;

// NOTE: Put in a check, only open if coord is unknown or netcdf
  if (prnlev>0) fprintf(stdout,"[%i] NETCDF_open(): Opening %s\n",
                        worldrank,trajInfo->filename);

  switch (trajInfo->accessMode) {
    case 0: // Read 
#     ifdef MPI
      err = ncmpi_open(MPI_COMM_WORLD, trajInfo->filename, NC_NOWRITE, MPI_INFO_NULL, &ncid);
      /* This next line is a test. Apparently it puts the netcdf file in an
       * independent I/O mode. Not sure if it is bad to always put here. 
       * Originally this call was only made from ptrajPreprocess...
       */
      if (err == NC_NOERR)
        err = ncmpi_begin_indep_data(ncid);
#     else
      err = nc_open(trajInfo->filename, NC_NOWRITE, &ncid);
#     endif
    break;
    case 1: // Write
      //omode=NC_WRITE; 
#     ifdef MPI
      err = ncmpi_create(MPI_COMM_WORLD, trajInfo->filename, NC_64BIT_OFFSET, MPI_INFO_NULL, &ncid);
      if (err == NC_NOERR)
        ncmpi_begin_indep_data(ncid);
#     else
      err = nc_create(trajInfo->filename, NC_64BIT_OFFSET, &ncid);
#     endif
    break;
    case 2: // Append
      printfone("Appending of NETCDF files is not supported.\n");
      return 1;
      break;
  }

  /* If opening succeeded and memory hasnt been allocated already
   *  initialize necessary data structure.
   * NOTE: Should this be in NETCDF_setup? If so ncid would have to 
   *       be its own variable in coordinateInfo.
   * NOTE: If this is an output file trajInfo->type has already been set.
   *       Not a huge problem but is a bit circular. TRAJOUT should eventually
   *       only set trajInfo->isNetcdf.
   */
  if (err == NC_NOERR) {
    trajInfo->type = COORD_AMBER_NETCDF;
    if (trajInfo->NCInfo==NULL) {
      trajInfo->NCInfo = (netcdfTrajectoryInfo *) safe_malloc(sizeof(netcdfTrajectoryInfo));
      INITIALIZE_netcdfTrajectoryInfo( trajInfo->NCInfo );
    }
    trajInfo->NCInfo->currentFrame = worldrank;
    // Always set NCID since it can change depending on when file is opened
    trajInfo->NCInfo->ncid = ncid;
    if (prnlev>0) fprintf(stdout,"NETCDF_open(): %s has been assigned ncid of %i\n",
                          trajInfo->filename,ncid);
    return 0;
  }

  // If we are here an error occured. Print the error message before exiting.
  fprintf(stdout,"Error: NETCDF_open(): Could not open %s with accessMode %i\n",
          trajInfo->filename,trajInfo->accessMode);
  fprintf(stdout,"%s\n",nc_strerror(err));
#endif
  // If no BINTRAJ always fail
  return 1;
}
Ejemplo n.º 20
0
int main() {

  double *data_out;       // buffer
  int nx = 1/DX;          // hyperslab and output buffer dimensions 
  int ny = 1/DY;
  // int i, j;
 
  // Allocate memory for data
  if((data_out = (double *)malloc(STEPS * nx * ny * sizeof(double))) == NULL)
    printf("Error malloc matrix data_out[%d]\n",nx * ny);

  // Create NetCDF file. NC_CLOBBER tells NetCDF to overwrite this file, if it already exists
  int ncid, retval;
  // if( retval = nc_create( NC_FILE_NAME_NETCDF, NC_CLOBBER|NC_NETCDF4, &ncid ) ) {
  if( retval = nc_create( NC_FILE_NAME_NETCDF, NC_CLOBBER, &ncid ) ) {
    ERR_NETCDF(retval);
  }

  // Define the x and y dimensions. NetCDF will hand back and ID for each.
  int x_dimid, y_dimid, t_dimid;
  if( retval = nc_def_dim( ncid, "x", nx, &x_dimid ) ) {
    ERR_NETCDF(retval);
  }
  if( retval = nc_def_dim( ncid, "y", ny, &y_dimid ) ) {
    ERR_NETCDF(retval);
  }
  
  // Define the t dimension at NetCDF.
  if( retval = nc_def_dim( ncid, "t", NC_UNLIMITED, &t_dimid ) ) {
    ERR_NETCDF(retval);
  }
  
  // Define coordinate variables for x and y at NetCDF
  int x_varid, y_varid, t_varid;
  if( retval = nc_def_var( ncid, "x", NC_DOUBLE, 1, &x_dimid, &x_varid ) ) {
    ERR_NETCDF(retval);
  }
  if( retval = nc_def_var( ncid, "y", NC_DOUBLE, 1, &y_dimid, &y_varid ) ) {
    ERR_NETCDF(retval);
  }
  if( retval = nc_def_var( ncid, "t", NC_DOUBLE, 1, &t_dimid, &t_varid ) ) {
    ERR_NETCDF(retval);
  }
  
  // Define the nc-variable to store temperature data. The t dimension should be the one which varies more slowly at NetCDF.
  int varid;
  int dimids[3] = { t_dimid, x_dimid, y_dimid };
  if( retval = nc_def_var( ncid, "temperature", NC_DOUBLE, 3, dimids, &varid )){
    ERR_NETCDF(retval);
  }

  // Write x, y, t and temperature units at NetCDF
  char * space_units = "meters";
  char * time_units = "seconds since start of the experiment";
  char * temp_units = "kelvin";
  if( retval = nc_put_att_text( ncid, x_varid, "units", strlen(space_units), space_units ) ) {
    ERR_NETCDF(retval);
  }
  if( retval = nc_put_att_text( ncid, y_varid, "units", strlen(space_units), space_units ) ) {
    ERR_NETCDF(retval);
  }
  if( retval = nc_put_att_text( ncid, t_varid, "units", strlen(time_units), time_units ) ) {
    ERR_NETCDF(retval);
  }
  if( retval = nc_put_att_text( ncid, varid, "units", strlen(temp_units), temp_units ) ) {
    ERR_NETCDF(retval);
  }
  double scale_factor = 300.0;
  if( retval = nc_put_att_double( ncid, varid, "scale_factor", NC_DOUBLE, 1, &scale_factor ) ) {
    ERR_NETCDF(retval);
  }

  // End define mode: this tells NetCDF that we are done defining metadata at NetCDF
  if( retval = nc_enddef( ncid ) ) {
    ERR_NETCDF(retval);
  }

  // Write x coordinates at NetCDF
  size_t pos;
  for( pos = 0; pos < nx; ++pos ) {
    double x = DX*pos;
    if( retval = nc_put_var1_double( ncid, x_varid, &pos, &x ) ) {
      ERR_NETCDF(retval);
    }
  }
  
  // Write y coordinates at NetCDF
  for( pos = 0; pos < ny; ++pos ) {
    double y = DY*pos;
    if( retval = nc_put_var1_double( ncid, y_varid, &pos, &y ) ) {
      ERR_NETCDF(retval);
    }
  }

  // Open an existing HDF5 file for Output buffer
  hid_t file_id = H5Fopen(H5_FILE_NAME_HDF5, H5F_ACC_RDONLY, H5P_DEFAULT);
  if( file_id < 0 ) { ERR_HDF5; }

  // Open an existing HDF5 dataset
  hid_t tempD = H5Dopen(file_id, "temperature", H5P_DEFAULT);
  if( tempD < 0 ) { ERR_HDF5; }

  // Returns an identifier for a copy of the dataspace for a dataset HDF5
  hid_t tempSel  = H5Dget_space (tempD);    /* dataspace handle */
  if( tempSel < 0 ) { ERR_HDF5; }

  // Returns the number of dimensions in the HDF5 dataspace if successful; otherwise returns a negative value
  int rank;
  rank  = H5Sget_simple_extent_ndims (tempSel);

  // Retrieves dataspace dimension size and maximum size HDF5
  hsize_t dims_out[2];    // HDF5 dataset dimensions
  hid_t status_n  = H5Sget_simple_extent_dims (tempSel, dims_out, NULL);
  if( status_n < 0 ) { ERR_HDF5; }
  
  // Display the number of dimensions in the HDF5 dataspace and the dataspace dimension size and maximum size 
  printf("\nRank: %d\nDimensions: %lu x %lu \n", rank, (unsigned long)(dims_out[0]), (unsigned long)(dims_out[1]));

  // Define hyperslab in the dataset HDF5
  hsize_t sel_offset_in[4] = {0,0,0,SECTION}; // The temperature value for the last interaction (STEPS) is chosen
  hsize_t sel_length_in[4] = {STEPS, nx, ny, 1};   
  H5Sselect_hyperslab( tempSel, H5S_SELECT_SET, sel_offset_in, NULL, sel_length_in, NULL );
  if( tempSel < 0 ) { ERR_HDF5; }

  // Define the memory dataspace HDF5
  hsize_t memSdim[4]={STEPS,nx,ny};
  hid_t memS = H5Screate_simple( 3, memSdim, NULL );
  if( memS < 0 ) { ERR_HDF5; }

  // Define memory HDF5 hyperslab
  hsize_t sel_offset_out[3] = {0,0,0};
  hsize_t sel_length_out[3] = {STEPS, nx, ny};
  H5Sselect_hyperslab( memS, H5S_SELECT_SET, sel_offset_out, NULL, sel_length_out, NULL );
  if( memS < 0 ) { ERR_HDF5; }

  // Read dataset tempD data from HDF5 hyperslab in the file into the hyperslab in memory
  hsize_t status = H5Dread (tempD, H5T_NATIVE_DOUBLE, memS, tempSel, H5P_DEFAULT, data_out);
  if( status < 0 ) { ERR_HDF5; }
  
  // printf ("Data:\n ");
  // for( i = 0; i < nx; ++i ) {
  //   for( j = 0; j < ny; ++j ) {
  //     printf("%f ", data_out[i*ny+j]);
  //   }
  //   printf("\n ");
  // }
  // printf("\n");

  // Write the data to the NETCDF file
  size_t corner_vector[3] = {0,0,0};
  size_t edge_lengths[3] = {STEPS, nx, ny};
  if(retval = nc_put_vara_double(ncid, varid, corner_vector, edge_lengths, data_out)){
    ERR_NETCDF(retval);
  }
  pos = 0;
  double tval = 0;
  if( retval = nc_put_var1_double( ncid, t_varid, &pos, &tval ) ) {
    ERR_NETCDF(retval);
  }

  // Close the HDF5 memspace
  if( H5Sclose( memS ) <  0 ) { ERR_HDF5; }

  // Close the HDF5 dataspace
  if( H5Sclose( tempSel ) <  0 ) { ERR_HDF5; }

  // Close the HDF5 dataset
  if( H5Dclose( tempD ) <  0 ) { ERR_HDF5; }

  // Close the HDF5 file
  if( H5Fclose( file_id ) <  0 ) { ERR_HDF5; }

  // Close the file. This frees up any internal NetCDF resources associated with the file, and flushes any buffers
  if( retval = nc_close( ncid ) ) {
    ERR_NETCDF(retval);
  }

  // Free memory
  free(data_out);

  return 0;
  
}
Ejemplo n.º 21
0
int
main(int argc, char **argv)
{
   int ncid;
   size_t size_in;
   nc_type xtype;
   unsigned char data[DIM_LEN][BASE_SIZE], data_in[DIM_LEN][BASE_SIZE];
   int i, j;

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

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

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

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

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

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

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

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

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

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

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

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

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

      if (nc_close(ncid)) ERR;

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

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

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

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

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

   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Ejemplo n.º 22
0
/*
 * Generate C code for creating netCDF from in-memory structure.
 */
void
gen_netcdf(const char *filename)
{
    int stat, ncid;
    int idim, ivar, iatt;
    int ndims, nvars, natts, ngatts;

#ifdef USE_NETCDF4
    int ntyps, ngrps, igrp;
#endif

    Bytebuffer* databuf = bbNew();

    ndims = listlength(dimdefs);
    nvars = listlength(vardefs);
    natts = listlength(attdefs);
    ngatts = listlength(gattdefs);
#ifdef USE_NETCDF4
    ntyps = listlength(typdefs);
    ngrps = listlength(grpdefs);
#endif /*USE_NETCDF4*/

    /* create netCDF file, uses NC_CLOBBER mode */
    cmode_modifier |= NC_CLOBBER;
#ifdef USE_NETCDF4
    if(!usingclassic)
        cmode_modifier |= NC_NETCDF4;
#endif

    stat = nc_create(filename, cmode_modifier, &ncid);
    check_err(stat,__LINE__,__FILE__);

    /* ncid created above is also root group*/
    rootgroup->ncid = ncid;

#ifdef USE_NETCDF4
    /* Define the group structure */
    /* walking grdefs list will do a preorder walk of all defined groups*/
    for(igrp=0;igrp<ngrps;igrp++) {
	Symbol* gsym = (Symbol*)listget(grpdefs,igrp);
	if(gsym == rootgroup) continue; /* ignore root group*/
	stat = nc_def_grp(gsym->container->ncid,gsym->name,&gsym->ncid);
	check_err(stat,__LINE__,__FILE__);
    }
#endif

#ifdef USE_NETCDF4
    /* Define the types*/
    if (ntyps > 0) {
	int ityp;
	for(ityp = 0; ityp < ntyps; ityp++) {
	    Symbol* tsym = (Symbol*)listget(typdefs,ityp);
	    genbin_deftype(tsym);
	}
    }
#endif

    /* define dimensions from info in dims array */
    if (ndims > 0) {
        for(idim = 0; idim < ndims; idim++) {
            Symbol* dsym = (Symbol*)listget(dimdefs,idim);
	    stat = nc_def_dim(dsym->container->ncid,
			      dsym->name,
			      (dsym->dim.isunlimited?NC_UNLIMITED:dsym->dim.declsize),
			      &dsym->ncid);
	    check_err(stat,__LINE__,__FILE__);
       }
    }

    /* define variables from info in vars array */
    if (nvars > 0) {
	for(ivar = 0; ivar < nvars; ivar++) {
            Symbol* vsym = (Symbol*)listget(vardefs,ivar);
	    if (vsym->typ.dimset.ndims > 0) {	/* a dimensioned variable */
		/* construct a vector of dimension ids*/
		int dimids[NC_MAX_VAR_DIMS];
		for(idim=0;idim<vsym->typ.dimset.ndims;idim++)
		    dimids[idim] = vsym->typ.dimset.dimsyms[idim]->ncid;
		stat = nc_def_var(vsym->container->ncid,
				  vsym->name,
			          vsym->typ.basetype->ncid,
		        	  vsym->typ.dimset.ndims,
				  dimids,
				  &vsym->ncid);
	    } else { /* a scalar */
		stat = nc_def_var(vsym->container->ncid,
				  vsym->name,
			          vsym->typ.basetype->ncid,
		        	  vsym->typ.dimset.ndims,
				  NULL,
				  &vsym->ncid);
	    }
	    check_err(stat,__LINE__,__FILE__);
	}
    }

#ifdef USE_NETCDF4
    /* define special variable properties */
    if(nvars > 0) {
	for(ivar = 0; ivar < nvars; ivar++) {
            Symbol* var = (Symbol*)listget(vardefs,ivar);
	    genbin_definespecialattributes(var);
	}
    }
#endif /*USE_NETCDF4*/

/* define global attributes */
    if(ngatts > 0) {
	for(iatt = 0; iatt < ngatts; iatt++) {
	    Symbol* gasym = (Symbol*)listget(gattdefs,iatt);
	    genbin_defineattr(gasym);
	}
    }

    /* define per-variable attributes */
    if(natts > 0) {
	for(iatt = 0; iatt < natts; iatt++) {
	    Symbol* asym = (Symbol*)listget(attdefs,iatt);
	    genbin_defineattr(asym);
	}
    }

    if (nofill_flag) {
	stat = nc_set_fill(rootgroup->ncid, NC_NOFILL, 0);
	check_err(stat,__LINE__,__FILE__);
    }

    /* leave define mode */
    stat = nc_enddef(rootgroup->ncid);
    check_err(stat,__LINE__,__FILE__);

    if(!header_only) {
        /* Load values into those variables with defined data */
        if(nvars > 0) {
            for(ivar = 0; ivar < nvars; ivar++) {
                Symbol* vsym = (Symbol*)listget(vardefs,ivar);
                if(vsym->data != NULL) {
                    bbClear(databuf);
                    genbin_definevardata(vsym);
                }
            }
        }
    }
    bbFree(databuf);
}
Ejemplo n.º 23
0
int
main(int argc, char **argv) 
{			/* create tst_classic_fills.nc */
   printf("\n*** Testing fill values.\n");
   printf("*** testing read of string record var with no data...");
   {
#define STRING_VAR_NAME "blood_toil_tears_sweat"
#define NDIMS_STRING 1
#define DATA_START 1 /* Real data here. */

      int  ncid, varid, dimid, varid_in;
      const char *data_out[1] = {
	 "We have before us an ordeal of the most grievous kind. We have before "
	 "us many, many long months of struggle and of suffering. You ask, what "
	 "is our policy? I can say: It is to wage war, by sea, land and air, "
	 "with all our might and with all the strength that God can give us; to "
	 "wage war against a monstrous tyranny, never surpassed in the dark, "
	 "lamentable catalogue of human crime. That is our policy. You ask, what "
	 "is our aim? "
	 "I can answer in one word: It is victory, victory at all costs, victory "
	 "in spite of all terror, victory, however long and hard the road may "
	 "be; for without victory, there is no survival."};
      char *data_in;
      size_t index = DATA_START;

      /* Create file with a 1D string var. Set its fill value to the
       * empty string. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, "sentence", NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, 
		     &dimid, &varid)) ERR;

      /* Check it out. */
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;

      /* Write one string, leaving some blank records which will then
       * get the fill value. */
      if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR;

      /* Get all the data from the variable. */
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

   }
   SUMMARIZE_ERR;
   printf("*** testing read of string record var w/fill-value with no data...");
   {
#undef STRING_VAR_NAME
#define STRING_VAR_NAME "I_Have_A_Dream"
#undef NDIMS_STRING
#define NDIMS_STRING 1
#define FILLVALUE_LEN 1 /* There is 1 string, the empty one. */
#undef DATA_START
#define DATA_START 2 /* Real data here. */

      int  ncid, varid, dimid, varid_in;
      const char *missing_val[FILLVALUE_LEN] = {""};
      const char *missing_val_in[FILLVALUE_LEN];
      const char *data_out[1] = {
	 "With this faith, we will be able to hew out of the mountain of "
	 "despair a stone of hope. With this faith, we will be able to "
	 "transform the jangling discords of our nation into a beautiful "
	 "symphony of brotherhood. With this faith, we will be able to work "
	 "together, to pray together, to struggle together, to go to jail "
	 "together, to stand up for freedom together, knowing that we will "
	 "be free one day."};
      char *data_in;
      size_t index = DATA_START;

      /* Create file with a 1D string var. Set its fill value to the
       * empty string. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, "sentence", NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, 
		     &dimid, &varid)) ERR;
      if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN,
	missing_val)) ERR;

      /* Check it out. */
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue",
      			    (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR;

      /* Write one string, leaving some blank records which will then
       * get the fill value. */
      if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR;

      /* Get all the data from the variable. */
      index = 0;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
      index = 1;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
      index = DATA_START;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue",
      			    (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR;

      /* Get all the data from the variable. */
/* As of HDF5-1.8.12, reading from an unwritten chunk in a dataset with a
 *      variable-length datatype and a fill-value set will error, instead
 *      of retrieving the fill-value. -QAK
 */
#ifdef NOT_YET
      index = 0;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
      index = 1;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
#endif /* NOT_YET */
      index = DATA_START;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

   }
   SUMMARIZE_ERR;
/*    printf("*** testing empty fill values of a string var..."); */
/*    { */
/* #define STRING_VAR_NAME "The_String" */
/* #define NDIMS_STRING 1 */
/* #define FILLVALUE_LEN 1 /\* There is 1 string, the empty one. *\/ */
/* #define DATA_START 2 /\* Real data here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       const char *missing_val[FILLVALUE_LEN] = {""}; */
/*       const char *missing_val_in[FILLVALUE_LEN]; */
/*       const char *data_out[1] = {"The evil that men do lives after them; " */
/* 				 "the good is oft interred with their bones."}; */
/*       char **data_in; */
/*       size_t index = DATA_START; */
/*       int i; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN, missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR; */

/*       /\* Write one string, leaving some blank records which will then */
/*        * get the fill value. *\/ */
/*       if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR; */

/*       /\* Get all the data from the variable. *\/ */
/*       if (!(data_in = malloc((DATA_START + 1) * sizeof(char *)))) ERR; */
/*       if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */

/*       /\* First there should be fill values, then the data value we */
/*        * wrote. *\/ */
/*       for (i = 0; i < DATA_START; i++) */
/* 	 if (strcmp(data_in[i], "")) ERR; */
/*       if (strcmp(data_in[DATA_START], data_out[0])) ERR; */

/*       /\* Close everything up. Don't forget to free the string! *\/ */
/*       if (nc_free_string(DATA_START + 1, data_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, read data, and check values again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       /\*if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in[0])) ERR;*\/ */
/*       if (nc_close(ncid)) ERR; */
/*        free(data_in);  */
/*    } */

/*    SUMMARIZE_ERR; */
/*    printf("*** testing non-empty fill values of a string var..."); */
/*    { */
/* #define STRING_VAR_NAME2 "CASSIUS" */
/* #define FILLVALUE_LEN2 1 /\* There is 1 string in the fillvalue array. *\/ */
/* #define DATA_START2 9 /\* Real data starts here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       const char *missing_val[FILLVALUE_LEN2] = {"I know that virtue to be in you, Brutus"}; */
/*       const char *missing_val_in[FILLVALUE_LEN2]; */
/*       const char *data_out[1] = {"The evil that men do lives after them; " */
/* 				 "the good is oft interred with their bones."}; */
/*       char **data_in; */
/*       size_t index = DATA_START2; */
/*       int i; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * a non-empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME2, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN2, missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR; */

/*       /\* Write one string, leaving some blank records which will then */
/*        * get the fill value. *\/ */
/*       if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR; */

/*       /\* Get all the data from the variable. *\/ */
/*       if (!(data_in = malloc((DATA_START2 + 1) * sizeof(char *)))) ERR; */
/*       if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */

/*       /\* First there should be fill values, then the data value we */
/*        * wrote. *\/ */
/*       for (i = 0; i < DATA_START2; i++) */
/* 	 if (strcmp(data_in[i], missing_val[0])) ERR; */
/*       if (strcmp(data_in[DATA_START2], data_out[0])) ERR; */

/*       /\* Close everything up. *\/ */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, read data, and check values again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */
/*       free(data_in); */
/*    } */
/*    printf("*** testing read of string record var with no data..."); */
/*    { */
/* #define STRING_VAR_NAME "Moon_Is_A_Harsh_Mistress" */
/* #define NDIMS_STRING 1 */
/* #define FILLVALUE_LEN 1 /\* There is 1 string, the empty one. *\/ */
/* #define DATA_START 2 /\* Real data here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       char *missing_val[FILLVALUE_LEN] = {""}; */
/*       char *missing_val_in; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "Lunar_Years", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING,  */
/* 		     &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN,  */
/* 			    missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", &missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in)) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, &missing_val_in)) ERR; */

/*       /\* Get all the data from the variable. There is none! *\/ */
/*       if (nc_get_var_string(ncid, varid_in, NULL)) ERR; */
      
/*       /\* Close file. *\/ */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, and check again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", &missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in)) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, &missing_val_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */
/*    } */
/*    SUMMARIZE_ERR; */
   FINAL_RESULTS;
}
Ejemplo n.º 24
0
// ADD-BY-LEETEN 08/06/2011-BEGIN
/////////////////////////////////////////////////////////////////
void
ITLRandomField::_CreateNetCdf
(
		const char *szPath,
		const char *szFilenamePrefix
)
{
	char szNetCdfPathFilename[1024];
	#ifndef	WITH_PNETCDF	// ADD-BY-LEETEN 08/12/2011
	sprintf(szNetCdfPathFilename, "%s/%s.rank_%d.nc", szPath, szFilenamePrefix, iRank);

    // Create the file.
    ASSERT_NETCDF(nc_create(
    		szNetCdfPathFilename,
    		NC_CLOBBER,
    		&iNcId));

    // ADD-BY-LEETEN 08/12/2011-BEGIN
	#else	// #ifndef	WITH_PNETCDF
	sprintf(szNetCdfPathFilename, "%s/%s.nc", szPath, szFilenamePrefix);
    ASSERT_NETCDF(ncmpi_create(
    		MPI_COMM_WORLD,
    		szNetCdfPathFilename,
    		NC_CLOBBER,
    		MPI_INFO_NULL,
    		&iNcId));
	#endif	// #ifndef	WITH_PNETCDF
    // ADD-BY-LEETEN 08/12/2011-END

    // find the maximal block dim
    int piBlockDimMaxLengths[CBlock::MAX_DIM];
	for(int d = 0; d < CBlock::MAX_DIM; d++)
	{
		piBlockDimMaxLengths[d] = 0;
	}
    for(int b = 0; b < (int)pcBlockInfo.USize(); b++)
    	for(int d = 0; d < CBlock::MAX_DIM; d++)
    		piBlockDimMaxLengths[d] = max(piBlockDimMaxLengths[d], pcBlockInfo[b].piDimLengths[d]);

    // ADD-BY-LEETEN 08/12/2011-BEGIN
	// collect the max. length of all dim.
#if	0	// MOD-BY-LEETEN 08/29/2011-FROM:
	for(int d = 0; d < CBlock::MAX_DIM; d++)
	{
		int iTemp = piBlockDimMaxLengths[d];
		ASSERT_OR_LOG(MPI_SUCCESS == MPI_Reduce(&iTemp, &piBlockDimMaxLengths[d], 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD), "");
		if( 0 == iRank )
			ASSERT_OR_LOG(MPI_SUCCESS == MPI_Bcast(&piBlockDimMaxLengths[d], 1, MPI_INT, 0, MPI_COMM_WORLD), "");
	}
#else   // MOD-BY-LEETEN 08/29/2011-TO:
	ASSERT_OR_LOG(MPI_SUCCESS == MPI_Allreduce(MPI_IN_PLACE, &piBlockDimMaxLengths[0], CBlock::MAX_DIM, MPI_INT, MPI_MAX, MPI_COMM_WORLD), "");
#endif  // MOD-BY-LEETEN 08/29/2011-END

	#ifndef	WITH_PNETCDF	// ADD-BY-LEETEN 08/12/2011
	for(int d = 0; d < CBlock::MAX_DIM; d++)
		ASSERT_NETCDF(nc_def_dim(
						iNcId,
						pszNcDimNames[d],
						piBlockDimMaxLengths[d],
						&piNcDimIds[d]));

    // Define the block dimension
	ASSERT_NETCDF(nc_def_dim(
    				iNcId,
    				pszNcDimNames[NC_DIM_BLOCK],
    				IGetNrOfBlocks(),
    				&piNcDimIds[NC_DIM_BLOCK]));

	// Define the time dimension with unlimited length
    ASSERT_NETCDF(nc_def_dim(
    				iNcId,
    				pszNcDimNames[NC_DIM_GLOBAL_TIME],
    				NC_UNLIMITED,
    				&piNcDimIds[NC_DIM_GLOBAL_TIME]));

    // Define the variables for the coordinates
	for(int d = 0; d < CBlock::MAX_DIM; d++)
	{
		int piBlockDims[3];
		piBlockDims[0] = piNcDimIds[NC_DIM_GLOBAL_TIME];
		piBlockDims[1] = piNcDimIds[NC_DIM_BLOCK];
		piBlockDims[2] = piNcDimIds[d];

		ASSERT_NETCDF(nc_def_var(
						iNcId,
						pszNcDimNames[d],
						NC_DOUBLE,
						sizeof(piBlockDims) / sizeof(piBlockDims[0]),
						piBlockDims,
						&piNcDimVarIds[d]));
	}

	// define the variable for time stamp
	ASSERT_NETCDF(nc_def_var(
			iNcId,
			pszNcDimNames[NC_DIM_GLOBAL_TIME],
			NC_INT,
			1,
			&piNcDimIds[NC_DIM_GLOBAL_TIME],
	     	&iNcTimeVarId));

	// define the variable for all data components
	for(int c = 0; c < this->IGetNrOfDataComponents(); c++)
	{
		int piBlockDims[6];
		piBlockDims[0] = piNcDimIds[NC_DIM_GLOBAL_TIME];
		piBlockDims[1] = piNcDimIds[NC_DIM_BLOCK];
		piBlockDims[2] = piNcDimIds[NC_DIM_T];
		piBlockDims[3] = piNcDimIds[NC_DIM_Z];
		piBlockDims[4] = piNcDimIds[NC_DIM_Y];
		piBlockDims[5] = piNcDimIds[NC_DIM_X];

		ASSERT_NETCDF(nc_def_var(
						iNcId,
						this->CGetDataComponent(c).szName,
						NC_DOUBLE,
						sizeof(piBlockDims) / sizeof(piBlockDims[0]),
						piBlockDims,
						&this->CGetDataComponent(c).iVarId));
	}

	// define the varaibles for all random variables
	for(int r = 0; r < this->IGetNrOfRandomVariables(); r++)
	{
	  CRandomVariable& cRv = this->CGetRandomVariable(r);
	  int piBlockDims[6];
	  piBlockDims[0] = piNcDimIds[NC_DIM_GLOBAL_TIME];
	  piBlockDims[1] = piNcDimIds[NC_DIM_BLOCK];
	  piBlockDims[2] = piNcDimIds[NC_DIM_T];
	  piBlockDims[3] = piNcDimIds[NC_DIM_Z];
	  piBlockDims[4] = piNcDimIds[NC_DIM_Y];
	  piBlockDims[5] = piNcDimIds[NC_DIM_X];

	  ASSERT_NETCDF(nc_def_var(
				   iNcId,
				   cRv.szName,
				   NC_FLOAT,
				   sizeof(piBlockDims) / sizeof(piBlockDims[0]),
				   piBlockDims,
				   &cRv.iVarId));
	}

	// finish the definition mode
	ASSERT_NETCDF(nc_enddef(
			iNcId));

	// ADD-BY-LEETEN 08/12/2011-BEGIN
	#else	// #ifndef	WITH_PNETCDF
	for(int d = 0; d < CBlock::MAX_DIM; d++)
	{
		ASSERT_NETCDF(ncmpi_def_dim(
						iNcId,
						pszNcDimNames[d],
						piBlockDimMaxLengths[d],
						&piNcDimIds[d]));
	}

    // Define the block dimension
	ASSERT_NETCDF(ncmpi_def_dim(
    				iNcId,
    				pszNcDimNames[NC_DIM_BLOCK],
				// MOD-BY-LEETEN 08/30/2011-FROM:
    				  // IGetNrOfBlocks(),
				// TO:
				iNrOfGlobalBlocks,
				// MOD-BY-LEETEN 08/30/2011-END
    				&piNcDimIds[NC_DIM_BLOCK]));

    // Define the time dimension with unlimited length
    ASSERT_NETCDF(ncmpi_def_dim(
    				iNcId,
    				pszNcDimNames[NC_DIM_GLOBAL_TIME],
    				NC_UNLIMITED,
    				&piNcDimIds[NC_DIM_GLOBAL_TIME]));

    // Define the variables for the coordinates
	for(int d = 0; d < CBlock::MAX_DIM; d++)
	{
		int piBlockDims[3];
		piBlockDims[0] = piNcDimIds[NC_DIM_GLOBAL_TIME];
		piBlockDims[1] = piNcDimIds[NC_DIM_BLOCK];
		piBlockDims[2] = piNcDimIds[d];

		ASSERT_NETCDF(ncmpi_def_var(
						iNcId,
						pszNcDimNames[d],
						NC_DOUBLE,
						sizeof(piBlockDims) / sizeof(piBlockDims[0]),
						piBlockDims,
						&piNcDimVarIds[d]));
	}

	// define the variable for time stamp
	ASSERT_NETCDF(ncmpi_def_var(
			iNcId,
			pszNcDimNames[NC_DIM_GLOBAL_TIME],
			NC_INT,
			1,
			&piNcDimIds[NC_DIM_GLOBAL_TIME],
	     	&iNcTimeVarId));

	// define the variable for all data components
	for(int c = 0; c < this->IGetNrOfDataComponents(); c++)
	{
		int piBlockDims[6];
		piBlockDims[0] = piNcDimIds[NC_DIM_GLOBAL_TIME];
		piBlockDims[1] = piNcDimIds[NC_DIM_BLOCK];
		piBlockDims[2] = piNcDimIds[NC_DIM_T];
		piBlockDims[3] = piNcDimIds[NC_DIM_Z];
		piBlockDims[4] = piNcDimIds[NC_DIM_Y];
		piBlockDims[5] = piNcDimIds[NC_DIM_X];

		ASSERT_NETCDF(ncmpi_def_var(
						iNcId,
						this->CGetDataComponent(c).szName,
						NC_DOUBLE,
						sizeof(piBlockDims) / sizeof(piBlockDims[0]),
						piBlockDims,
						&this->CGetDataComponent(c).iVarId));
	}

	// define the varaibles for all random variables
	for(int r = 0; r < this->IGetNrOfRandomVariables(); r++)
	{
	  CRandomVariable& cRv = this->CGetRandomVariable(r);
	  int piBlockDims[6];
	  piBlockDims[0] = piNcDimIds[NC_DIM_GLOBAL_TIME];
	  piBlockDims[1] = piNcDimIds[NC_DIM_BLOCK];
	  piBlockDims[2] = piNcDimIds[NC_DIM_T];
	  piBlockDims[3] = piNcDimIds[NC_DIM_Z];
	  piBlockDims[4] = piNcDimIds[NC_DIM_Y];
	  piBlockDims[5] = piNcDimIds[NC_DIM_X];

	  ASSERT_NETCDF(ncmpi_def_var(
				   iNcId,
				   cRv.szName,
				   NC_FLOAT,
				   sizeof(piBlockDims) / sizeof(piBlockDims[0]),
				   piBlockDims,
				   &cRv.iVarId));
	}

	// finish the definition mode
	ASSERT_NETCDF(ncmpi_enddef(
			iNcId));
	#endif	// #ifndef	WITH_PNETCDF
	// ADD-BY-LEETEN 08/12/2011-END
	// enter the data mode...
}
Ejemplo n.º 25
0
int main(int argc, char **argv)
{
    struct timeval start_time, end_time, diff_time;
    double sec;
    int nitem = 10000;		/* default number of objects of each type */
    int i;
    int ncid;
    int data[] = {42};
    int g, grp, numgrp;
    char gname[16];
    int v, var, numvar, vn, vleft, nvars;
    
    if(argc > 2) { 	/* Usage */
	printf("NetCDF performance test, writing many groups and variables.\n");
	printf("Usage:\t%s [N]\n", argv[0]);
	printf("\tN: number of objects\n");
	return(0);
    }
    for(i = 1; i < argc; i++) {
	nitem = atoi(argv[i]);
    }

    /*  create new file */
    if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
    if (gettimeofday(&start_time, NULL))
	ERR;
    /* create N groups, printing time after every 1000 */
    numgrp = nitem;
    for(g = 1; g < numgrp + 1; g++) {
	sprintf(gname, "group%d", g);
	if (nc_def_grp(ncid, gname, &grp)) ERR;
	if (nc_def_var(grp, "var", NC_INT, 0, NULL, &var)) ERR;
	if(nc_enddef (grp)) ERR;
	if(nc_put_var(grp, var, data)) ERR;
	if(g%1000 == 0) {		/* only print every 1000th group name */
	    if (gettimeofday(&end_time, NULL)) ERR;
	    if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
	    sec = diff_time.tv_sec + 1.0e-6 * diff_time.tv_usec;
	    printf("%s\t%.3g sec\n", gname, sec);
	}
    }
    nc_close(ncid);
    
    /*  create new file */
    if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
    /* create N variables, printing time after every 1000.  Because only
     * NC_MAX_VARS are permitted per group, create the necessary number
     * of groups to hold nitem variables. */
    numvar = nitem;
    v = 1;
    numgrp = (numvar - 1) / NC_MAX_VARS + 1;
    vleft = numvar - (NC_MAX_VARS * (numgrp - 1));
    if (gettimeofday(&start_time, NULL))
	ERR;
    
    for(g = 1; g < numgrp + 1; g++) {
	sprintf(gname, "group%d", g);
	if (nc_def_grp(ncid, gname, &grp)) ERR;
	nvars = g < numgrp ? NC_MAX_VARS : vleft; /* leftovers on last time through */
	for(vn = 1; vn < nvars + 1; vn++) {
	    int var;
	    char vname[20];
	    sprintf(vname, "variable%d", v);
	    if(nc_def_var(grp, vname, NC_INT, 0, NULL, &var)) ERR;
	    if(nc_put_var(grp, var, data)) ERR;
	    if(v%1000 == 0) {		/* only print every 1000th variable name */
		if (gettimeofday(&end_time, NULL)) ERR;
		if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
		sec = diff_time.tv_sec + 1.0e-6 * diff_time.tv_usec;
		printf("%s/%s\t%.3g sec\n", gname, vname, sec);
	    }
	    v++;
	}
    }
    nc_close(ncid);
    return(0);
}
Ejemplo n.º 26
0
int
main(int argc, char **argv)
{
    int ncid, spockid, kirkid, dimids[NUMDIMS];
    int int_val_in, int_val_out = 99;
    double double_val_in, double_val_out = 1.79769313486230e+308; /* from ncx.h */
    size_t index[2] = {QTR_CLASSIC_MAX-1, 0};

    /* These are for the revolutionary generals tests. */
    int cromwellid, collinsid, washingtonid;
    int napoleanid, dimids_gen[4], dimids_gen1[4];

    /* All create modes will be anded to this. All tests will be run
       twice, with and without NC_SHARE.*/
    int cmode_run;
    int cflag = NC_CLOBBER;

    int res; 

    printf("\n*** Testing large files, quickly.\n");

    for (cmode_run=0; cmode_run<2; cmode_run++)
    {
 	/* On second pass, try using NC_SHARE. */
	if (cmode_run == 1) 
	{
	    cflag |= NC_SHARE;
	    printf("*** Turned on NC_SHARE for subsequent tests.\n");
	}

	/* Create a netCDF 64-bit offset format file. Write a value. */
	printf("*** Creating %s for 64-bit offset large file test...", FILE_NAME);
	if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid)))
	    ERR;

	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "longdim", QTR_CLASSIC_MAX, dimids)))
	    ERR;
	if ((res = nc_def_var(ncid, "spock", NC_DOUBLE, NUMDIMS, 
			      dimids, &spockid)))
	    ERR;
	if ((res = nc_def_var(ncid, "kirk", NC_DOUBLE, NUMDIMS, 
			      dimids, &kirkid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	if ((res = nc_put_var1_double(ncid, kirkid, index, &double_val_out)))
	    ERR;
	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");

	/* How about a meteorological data file about the weather
	   experience by various generals of revolutionary armies? 

	   This has 3 dims, 4 vars. The dimensions are such that this will
	   (just barely) not fit in a classic format file. The first three
	   vars are cromwell, 536870911 bytes, washington, 2*536870911
	   bytes, and napolean, 536870911 bytes. That's a grand total of
	   2147483644 bytes. Recall our magic limit for the combined size
	   of all fixed vars: 2 GiB - 4 bytes, or 2147483644. So you would
	   think these would exactly fit, unless you realized that
	   everything is rounded to a 4 byte boundary, so you need to add
	   some bytes for that (how many?), and that pushes us over the
	   limit.
      
	   We will create this file twice, once to ensure it succeeds (with
	   64-bit offset format), and once to make sure it fails (with
	   classic format). Then some variations to check record var
	   boundaries. 
	*/
	printf("*** Now a 64-bit offset, large file, fixed var test...");
	if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor", 
			      QTR_CLASSIC_MAX, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      QTR_CLASSIC_MAX, &dimids_gen[1])))
	    ERR;
	if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[2])))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 1, &dimids_gen[0],
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 1, &dimids_gen[1], 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 1, &dimids_gen[0], 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	printf("ok\n");

	/* Write a value or two just for fun. */
	/*index[0] = QTR_CLASSIC_MAX - 296;
	if ((res = nc_put_var1_int(ncid, napoleanid, index, &int_val_out)))
	    ERR;
	if ((res = nc_get_var1_int(ncid, napoleanid, index, &int_val_in)))
	    ERR;
	if (int_val_in != int_val_out)
	BAIL2;*/
	printf("*** Now writing some values...");
	index[0] = QTR_CLASSIC_MAX - 295;
	if ((res = nc_put_var1_int(ncid, napoleanid, index, &int_val_out)))
	    ERR;
	if ((res = nc_get_var1_int(ncid, napoleanid, index, &int_val_in)))
	    ERR;
	if (int_val_in != int_val_out)
	   ERR;

	index[0] = QTR_CLASSIC_MAX - 1;
	if ((res = nc_put_var1_int(ncid, napoleanid, index, &int_val_out)))
	    ERR;
	if ((res = nc_get_var1_int(ncid, napoleanid, index, &int_val_in)))
	    ERR;
	if (int_val_in != int_val_out)
	    ERR;

	index[0] = QTR_CLASSIC_MAX - 1;
	if ((res = nc_put_var1_int(ncid, washingtonid, index, &int_val_out)))
	    ERR;
	if ((res = nc_get_var1_int(ncid, washingtonid, index, &int_val_in)))
	    ERR;
	if (int_val_in != int_val_out)
	    ERR;

	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");

	/* This time it should fail, because we're trying to cram this into
	   a classic format file. nc_enddef will detect our violations and
	   give an error. We've*/
	printf("*** Now a classic file which will fail...");
	if ((res = nc_create(FILE_NAME, cflag, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor", 
			      QTR_CLASSIC_MAX, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      QTR_CLASSIC_MAX, &dimids_gen[1])))
	    ERR;
	if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[2])))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 1, &dimids_gen[0],
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 1, &dimids_gen[1], 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 1, &dimids_gen[0], 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)) != NC_EVARSIZE)
	    ERR;
	if ((res = nc_close(ncid)) != NC_EVARSIZE)
	    ERR;
	printf("ok\n");

	/* This will create some max sized 64-bit offset format fixed vars. */
	printf("*** Now a 64-bit offset, simple fixed var create test...");
	if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor", 
			      MAX_CLASSIC_BYTES, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_SHORT, 1, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_SHORT, 1, dimids_gen, 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, dimids_gen, 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");

	/* This will exceed the 64-bit offset format limits for one of the
	   fixed vars. */
	printf("*** Now a 64-bit offset, over-sized file that will fail...");
	if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	/* max dim size is MAX_CLASSIC_BYTES. */
	if ((res = nc_def_dim(ncid, "revolutionary_fervor", 
			      MAX_CLASSIC_BYTES, dimids_gen)))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 1, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 1, dimids_gen, 
			      &washingtonid)))
	    if ((res = nc_enddef(ncid)) != NC_EVARSIZE)
		ERR;
	if ((res = nc_close(ncid)) != NC_EVARSIZE)
	    ERR;
	printf("ok\n");

	/* Now let's see about record vars. First create a 64-bit offset
	   file with three rec variables, each with the same numbers as
	   defined above for the fixed var tests. This should all work. */
	printf("*** Now a 64-bit offset, record var file...");
	if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor", 
			      QTR_CLASSIC_MAX, &dimids_gen[1])))
	    ERR;
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      QTR_CLASSIC_MAX, &dimids_gen[2])))
	    ERR;
	if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[3])))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen, 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen, 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");

	/* Now try this record file in classic format. It should fail and
	   the enddef. Too many bytes in the first record.*/
	printf("*** Now a classic file that's too big and will fail...");
	if ((res = nc_create(FILE_NAME, cflag, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor", 
			      QTR_CLASSIC_MAX, &dimids_gen[1])))
	    ERR;
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      QTR_CLASSIC_MAX, &dimids_gen[2])))
	    ERR;
	if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[3])))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen, 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen, 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)) != NC_EVARSIZE)
	    ERR;
	if ((res = nc_close(ncid)) != NC_EVARSIZE)
	    ERR;
	printf("ok\n");

	/* Now try this record file in classic format. It just barely
	   passes at the enddef. Almost, but not quite, too many bytes in
	   the first record. Since I'm adding a fixed variable (Collins), 
	   I don't get the last record size exemption. */ 
	printf("*** Now a classic file with recs and one fixed will fail...");
	if ((res = nc_create(FILE_NAME, cflag, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor", 
			      MAX_CLASSIC_BYTES, &dimids_gen[1])))
	    ERR;
	if ((res = nc_def_dim(ncid, "ruthlessness", 100, &dimids_gen[2])))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_BYTE, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 1, &dimids_gen[2], 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");

	/* Try a classic file with several records, and the last record var
	   with a record size greater than our magic number of 2 GiB - 4
	   bytes. We'll start with just one oversized record var. This
	   should work. Cromwell has been changed to NC_DOUBLE, and that
	   increases his size to 2147483644 (the max dimension size) times
	   8, or about 16 GB per record. Zowie! (Mind you, Cromwell
	   certainly had a great deal of revolutionary fervor.)
	*/ 
	printf("*** Now a classic file with one large rec var...");
	if ((res = nc_create(FILE_NAME, cflag, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor",  
			      MAX_CLASSIC_BYTES, &dimids_gen[1])))  
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	index[0] = 0;
	index[1] = MAX_CLASSIC_BYTES - 1;
	if ((res = nc_put_var1_double(ncid, cromwellid, index, &double_val_out)))
	    ERR;
	if ((res = nc_get_var1_double(ncid, cromwellid, index, &double_val_in)))
	    ERR;
	if (double_val_in != double_val_out)
	    ERR;
	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");
   
	/* This is a classic format file with an extra-large last record
	   var. */
	printf("*** Now a classic file with extra-large last record var...");
	if ((res = nc_create(FILE_NAME, cflag, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor",  
			      MAX_CLASSIC_BYTES, &dimids_gen[1])))  
	    ERR;
	dimids_gen1[0] = dimids_gen[0];
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      5368, &dimids_gen1[1])))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen1, 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, 
			      &collinsid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	index[0] = 0;
	index[1] = MAX_CLASSIC_BYTES - 1;
	if ((res = nc_put_var1_double(ncid, cromwellid, index, &double_val_out)))
	    ERR;
	if ((res = nc_get_var1_double(ncid, cromwellid, index, &double_val_in)))
	    ERR;
	if (double_val_in != double_val_out)
	    ERR;
	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");

	/* This is a classic format file with an extra-large second to last
	   record var. But this time it won't work, because the size
	   exemption only applies to the last record var. Note that one
	   dimension is small (5000). */
	printf("*** Now a classic file xtra-large 2nd to last var that will fail...");
	if ((res = nc_create(FILE_NAME, cflag, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor",  
			      MAX_CLASSIC_BYTES, &dimids_gen[1])))  
	    ERR;
	dimids_gen1[0] = dimids_gen[0];
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      5000, &dimids_gen1[1])))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen1, 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)) != NC_EVARSIZE)
	    ERR;
	if ((res = nc_close(ncid)) != NC_EVARSIZE)
	    ERR;
	printf("ok\n");

	/* Now try an extra large second to last ver with 64-bit
	   offset. This won't work either, because the cromwell var is so
	   large. It exceeds the 4GiB - 4 byte per record limit for record
	   vars. */
	printf("*** Now a 64-bit offset file with too-large rec var that will fail...");
	if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor",  
			      MAX_CLASSIC_BYTES, &dimids_gen[1])))  
	    ERR;
	dimids_gen1[0] = dimids_gen[0];
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      5368, &dimids_gen1[1])))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_BYTE, 2, dimids_gen1, 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_DOUBLE, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)) != NC_EVARSIZE)
	    ERR;
	if ((res = nc_close(ncid)) != NC_EVARSIZE)
	    ERR;
	printf("ok\n");

	/* A 64-bit offset record file that just fits... */
	printf("*** Now a 64 bit-offset file that just fits...");
	if ((res = nc_create(FILE_NAME, cflag|NC_64BIT_OFFSET, &ncid)))
	    ERR;
	if ((res = nc_set_fill(ncid, NC_NOFILL, NULL)))
	    ERR;
	if ((res = nc_def_dim(ncid, "political_trouble", 
			      NC_UNLIMITED, &dimids_gen[0])))
	    ERR;
	if ((res = nc_def_dim(ncid, "revolutionary_fervor",  
			      MAX_CLASSIC_BYTES, &dimids_gen[1])))  
	    ERR;
	dimids_gen1[0] = dimids_gen[0];
	if ((res = nc_def_dim(ncid, "post_revoultionary_hangover", 
			      MAX_CLASSIC_BYTES, &dimids_gen1[1])))
	    ERR;
	if ((res = nc_def_var(ncid, "Washington", NC_SHORT, 2, dimids_gen1, 
			      &washingtonid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Napolean", NC_SHORT, 2, dimids_gen1, 
			      &napoleanid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Cromwell", NC_SHORT, 2, dimids_gen,
			      &cromwellid)))
	    ERR;
	if ((res = nc_def_var(ncid, "Collins", NC_DOUBLE, 2, dimids_gen1, 
			      &collinsid)))
	    ERR;
	if ((res = nc_enddef(ncid)))
	    ERR;
	index[0] = 0;
	index[1] = MAX_CLASSIC_BYTES - 1;
	if ((res = nc_put_var1_int(ncid, cromwellid, index, &int_val_out)))
	    ERR;
	if ((res = nc_get_var1_int(ncid, cromwellid, index, &int_val_in)))
	    ERR;
	if (int_val_in != int_val_out)
	    ERR;
	if ((res = nc_close(ncid)))
	    ERR;
	printf("ok\n");
    } /* end of cmode run */

    /* Wow! Everything worked! */
    printf("\n*** All large file tests were successful.\n");

    /* Delete the huge data file we created. */
    (void) remove(FILE_NAME); 

    printf("*** Success ***\n");

    return 0;
}
Ejemplo n.º 27
0
/* 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;
}
Ejemplo n.º 28
0
static GDALDataset *
GMTCreateCopy( const char * pszFilename, GDALDataset *poSrcDS,
               int bStrict, CPL_UNUSED char ** papszOptions,
               CPL_UNUSED GDALProgressFunc pfnProgress,
               CPL_UNUSED void * pProgressData )
{
/* -------------------------------------------------------------------- */
/*      Figure out general characteristics.                             */
/* -------------------------------------------------------------------- */
    nc_type nc_datatype;
    GDALRasterBand *poBand;
    int nXSize, nYSize;

    CPLMutexHolderD(&hNCMutex);

    if( poSrcDS->GetRasterCount() != 1 )
    {
        CPLError( CE_Failure, CPLE_AppDefined,
                  "Currently GMT export only supports 1 band datasets." );
        return NULL;
    }

    poBand = poSrcDS->GetRasterBand(1);

    nXSize = poSrcDS->GetRasterXSize();
    nYSize = poSrcDS->GetRasterYSize();
    
    if( poBand->GetRasterDataType() == GDT_Int16 )
        nc_datatype = NC_SHORT;
    else if( poBand->GetRasterDataType() == GDT_Int32 )
        nc_datatype = NC_INT;
    else if( poBand->GetRasterDataType() == GDT_Float32 )
        nc_datatype = NC_FLOAT;
    else if( poBand->GetRasterDataType() == GDT_Float64 )
        nc_datatype = NC_DOUBLE;
    else if( bStrict )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "Band data type %s not supported in GMT, giving up.",
                  GDALGetDataTypeName( poBand->GetRasterDataType() ) );
        return NULL;
    }
    else if( poBand->GetRasterDataType() == GDT_Byte )
        nc_datatype = NC_SHORT;
    else if( poBand->GetRasterDataType() == GDT_UInt16 )
        nc_datatype = NC_INT;
    else if( poBand->GetRasterDataType() == GDT_UInt32 )
        nc_datatype = NC_INT;
    else 
        nc_datatype = NC_FLOAT;
    
/* -------------------------------------------------------------------- */
/*      Establish bounds from geotransform.                             */
/* -------------------------------------------------------------------- */
    double adfGeoTransform[6];
    double dfXMax, dfYMin;

    poSrcDS->GetGeoTransform( adfGeoTransform );
    
    if( adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0 )
    {
        CPLError( bStrict ? CE_Failure : CE_Warning, CPLE_AppDefined, 
                  "Geotransform has rotational coefficients not supported in GMT." );
        if( bStrict )
            return NULL;
    }

    dfXMax = adfGeoTransform[0] + adfGeoTransform[1] * nXSize;
    dfYMin = adfGeoTransform[3] + adfGeoTransform[5] * nYSize;
    
/* -------------------------------------------------------------------- */
/*      Create base file.                                               */
/* -------------------------------------------------------------------- */
    int cdfid, err;

    err = nc_create (pszFilename, NC_CLOBBER,&cdfid);
    if( err != NC_NOERR )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "nc_create(%s): %s", 
                  pszFilename, nc_strerror( err ) );
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*      Define the dimensions and so forth.                             */
/* -------------------------------------------------------------------- */
    int side_dim, xysize_dim, dims[1];
    int x_range_id, y_range_id, z_range_id, inc_id, nm_id, z_id;

    nc_def_dim(cdfid, "side", 2, &side_dim);
    nc_def_dim(cdfid, "xysize", (int) (nXSize * nYSize), &xysize_dim);

    dims[0]		= side_dim;
    nc_def_var (cdfid, "x_range", NC_DOUBLE, 1, dims, &x_range_id);
    nc_def_var (cdfid, "y_range", NC_DOUBLE, 1, dims, &y_range_id);
    nc_def_var (cdfid, "z_range", NC_DOUBLE, 1, dims, &z_range_id);
    nc_def_var (cdfid, "spacing", NC_DOUBLE, 1, dims, &inc_id);
    nc_def_var (cdfid, "dimension", NC_LONG, 1, dims, &nm_id);

    dims[0]		= xysize_dim;
    nc_def_var (cdfid, "z", nc_datatype, 1, dims, &z_id);

/* -------------------------------------------------------------------- */
/*      Assign attributes.                                              */
/* -------------------------------------------------------------------- */
    double default_scale = 1.0;
    double default_offset = 0.0;
    int default_node_offset = 1; // pixel is area

    nc_put_att_text (cdfid, x_range_id, "units", 7, "meters");
    nc_put_att_text (cdfid, y_range_id, "units", 7, "meters");
    nc_put_att_text (cdfid, z_range_id, "units", 7, "meters");

    nc_put_att_double (cdfid, z_id, "scale_factor", NC_DOUBLE, 1, 
                       &default_scale );
    nc_put_att_double (cdfid, z_id, "add_offset", NC_DOUBLE, 1, 
                       &default_offset );

    nc_put_att_int (cdfid, z_id, "node_offset", NC_LONG, 1, 
                    &default_node_offset );
    nc_put_att_text (cdfid, NC_GLOBAL, "title", 1, "");
    nc_put_att_text (cdfid, NC_GLOBAL, "source", 1, "");
	
    /* leave define mode */
    nc_enddef (cdfid);

/* -------------------------------------------------------------------- */
/*      Get raster min/max.                                             */
/* -------------------------------------------------------------------- */
    double adfMinMax[2];
    GDALComputeRasterMinMax( (GDALRasterBandH) poBand, FALSE, adfMinMax );
	
/* -------------------------------------------------------------------- */
/*      Set range variables.                                            */
/* -------------------------------------------------------------------- */
    size_t start[2], edge[2];
    double dummy[2];
    int nm[2];
	
    start[0] = 0;
    edge[0] = 2;
    dummy[0] = adfGeoTransform[0];
    dummy[1] = dfXMax;
    nc_put_vara_double(cdfid, x_range_id, start, edge, dummy);

    dummy[0] = dfYMin;
    dummy[1] = adfGeoTransform[3];
    nc_put_vara_double(cdfid, y_range_id, start, edge, dummy);

    dummy[0] = adfGeoTransform[1];
    dummy[1] = -adfGeoTransform[5];
    nc_put_vara_double(cdfid, inc_id, start, edge, dummy);

    nm[0] = nXSize;
    nm[1] = nYSize;
    nc_put_vara_int(cdfid, nm_id, start, edge, nm);

    nc_put_vara_double(cdfid, z_range_id, start, edge, adfMinMax);

/* -------------------------------------------------------------------- */
/*      Write out the image one scanline at a time.                     */
/* -------------------------------------------------------------------- */
    double *padfData;
    int  iLine;

    padfData = (double *) CPLMalloc( sizeof(double) * nXSize );

    edge[0] = nXSize;
    for( iLine = 0; iLine < nYSize; iLine++ )
    {
        start[0] = iLine * nXSize;
        poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1, 
                          padfData, nXSize, 1, GDT_Float64, 0, 0, NULL );
        err = nc_put_vara_double( cdfid, z_id, start, edge, padfData );
        if( err != NC_NOERR )
        {
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "nc_put_vara_double(%s): %s", 
                      pszFilename, nc_strerror( err ) );
            nc_close (cdfid);
            return( NULL );
        }
    }
    
    CPLFree( padfData );

/* -------------------------------------------------------------------- */
/*      Close file, and reopen.                                         */
/* -------------------------------------------------------------------- */
    nc_close (cdfid);

/* -------------------------------------------------------------------- */
/*      Re-open dataset, and copy any auxiliary pam information.         */
/* -------------------------------------------------------------------- */
    GDALPamDataset *poDS = (GDALPamDataset *)
        GDALOpen( pszFilename, GA_ReadOnly );

    if( poDS )
        poDS->CloneInfo( poSrcDS, GCIF_PAM_DEFAULT );

    return poDS;
}
Ejemplo n.º 29
0
int main(int argc,char *argv[])
{
  float *sfgrd, *emrf;
  long nsf;
  char *fname, *p, *e, foutname[PATH_MAX], line[256];
  int i, ncres, fres, ncid, idim, ivar[2];
  FILE *fp;
  char units[2][16] = { "cm-1", "1" };

  fprintf(stdout, "Convert Emissivity ASCII file to NetCDF file\n");

  if (argc < 2)
  {
    fprintf(stderr,"Not enough arguments. Need name of input file.\n");
    fprintf(stderr,"Usage :\n");
    fprintf(stderr,"%s filename\n",argv[0]);
    return -1;
  }
  fname = strdup(argv[1]);
  fp = fopen(fname,"r");
  if ( fp == NULL )
  {
    fprintf(stderr,"Cannot open file %s\n",fname);
    return -2;
  }
  p = strrchr(fname, '/');
  if ( p == NULL ) p = fname;
  else p = p+1;
  e = strrchr(fname, '.');
  if ( e != NULL ) *e = 0;
  sprintf(foutname,"%s.nc", p);
  ncres = nc_create(foutname,NC_CLOBBER,&ncid);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Cannot open output file %s\n",foutname);
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -3;
  }
  fres = fscanf(fp, "%ld\n", &nsf);
  if ( fres != 1 )
  {
    fprintf(stderr,"Error reading number of hinge points from input file\n");
    return -4;
  }
  if ( nsf < 0 || nsf > 1000000000 )
  {
    fprintf(stderr,"Error reading nsf from input file: %ld\n",nsf);
    return -4;
  }
  sfgrd = (float *) malloc(nsf*sizeof(float));
  emrf = (float *) malloc(nsf*sizeof(float));
  if ( sfgrd == NULL || emrf == NULL )
  {
    fprintf(stderr,"Error malloc data space: %ld",nsf*2*sizeof(float));
    return -5;
  }
  ncres = nc_def_dim(ncid,"nspectral",nsf,&idim);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error creating dim nspectral\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -6;
  }
  ncres = nc_def_var(ncid,"SfGrd",NC_FLOAT,1,&idim,ivar);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error creating SfGrd var\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -6;
  }
  ncres = nc_def_var(ncid,"EmRf",NC_FLOAT,1,&idim,ivar+1);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error creating EmRf var\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -6;
  }
  ncres = nc_put_att_text(ncid,ivar[0],"units",strlen(units[0])+1,units[0]);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error adding units\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -6;
  }
  ncres = nc_put_att_text(ncid,ivar[1],"units",strlen(units[1])+1,units[1]);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error adding units\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -6;
  }
  ncres = nc_enddef(ncid);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error finalizing outout file\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -7;
  }
  for ( i = 0; i < nsf; i ++ )
  {
    fres = fscanf(fp,"%f %f\n",sfgrd+i,emrf+i);
    if ( fres != 2 )
    {
      fprintf(stderr,"Error reading input data at record %d\n",i);
      return -8;
    }
  }
  ncres = nc_put_var_float(ncid,ivar[0],sfgrd);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error write var SfGrd\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -9;
  }
  ncres = nc_put_var_float(ncid,ivar[1],emrf);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error write var EmRf\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -9;
  }
  (void) fclose(fp);
  ncres = nc_close(ncid);
  if ( ncres != NC_NOERR )
  {
    fprintf(stderr,"Error closing output file!\n");
    fprintf(stderr,"%s\n",nc_strerror(ncres));
    return -10;
  }
  return 0;
}
Ejemplo n.º 30
0
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;
}