Beispiel #1
0
/* 
 * copy a user-defined opaque type in the group igrp to the group ogrp
 */
static int
copy_opaque_type(int igrp, nc_type itype, int ogrp)
{
    int stat = NC_NOERR; 
    nc_type otype;
    char name[NC_MAX_NAME];
    size_t size;

    NC_CHECK(nc_inq_opaque(igrp, itype, name, &size));
    NC_CHECK(nc_def_opaque(ogrp, size, name, &otype));

    return stat;
}
Beispiel #2
0
int
main(int argc, char **argv)
{
   int ncid;
   size_t size_in;
   nc_type xtype;
   unsigned char data[DIM_LEN][BASE_SIZE], data_in[DIM_LEN][BASE_SIZE];
   int i, j;

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

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

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

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

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

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

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

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

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

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

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

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

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

      if (nc_close(ncid)) ERR;

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

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

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

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

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

   SUMMARIZE_ERR;
   FINAL_RESULTS;
}
Beispiel #3
0
static int
NC_meta_create_type1(int ncid, nc_type xtype,
		    MetaNode* root, MetaNode** typenodep)
{
    int ncstat = NC_NOERR;
    int i;
    MetaNode* node;
    nc_meta metatype = NC_NAT;

    /* Determine the top-level type for xtype */
    ncstat = mapnctype(ncid,xtype,&metatype);
    if(ncstat != NC_NOERR) goto done;    

    node = NC_meta_alloc(metatype);
    node->ncid = ncid;
    node->xtype = xtype;

    switch (node->nodeclass) {

    case NC_ATOMIC:
	ncstat = nc_inq_type(ncid,xtype,node->name,&node->size);
	if(ncstat != NC_NOERR) goto fail;
	break;

    case NC_OPAQUE:
	ncstat = nc_inq_opaque(ncid,xtype,node->name,&node->size);
	if(ncstat != NC_NOERR) goto fail;
	break;

    case NC_ENUM:
	ncstat = nc_inq_enum(ncid,xtype,node->name,&node->basetype,
				 &node->size,&node->nelems);
	if(ncstat != NC_NOERR) goto fail;
	/* Now, create and fill in the enum constants */
        node->econsts  = (struct MetaEconst*)calloc(node->nelems,
                                                    sizeof(struct MetaEconst));
	if(node->econsts == NULL) {ncstat = NC_ENOMEM;goto fail;}
	for(i=0;i<node->nelems;i++) {
	    struct MetaEconst* econst = node->consts+i;
	    ncstat = nc_inq_enum_member(ncid,xtype,i,
                                           econst->name,
                                           (void*)econst->value);
  	    if(ncstat != NC_NOERR) goto fail;
        }
	break;

    case NC_COMPOUND:
	ncstat = nc_inq_compound(ncid,xtype,node->name,&node->size,
				 &node->nelems);
	if(ncstat != NC_NOERR) goto fail;
	/* Now, create and fill in the fields */
        node->compound.fields = nc_meta_allocn(NC_FIELD,node->nelems);
	if(node->compound.fields == NULL) {ncstat = NC_ENOMEM;goto fail;}
	for(i=0;i<node->nelems;i++) {
	    MetaNode* field = node->compound.fields+i;
	    nc_type basetype;
	    ncstat = nc_inq_compound_field(ncid,xtype,i,
                                           field->name,
                                           &field->offset,
                                           &basetype,
					   &field->ndims,
					   field->dims);
  	    if(ncstat != NC_NOERR) goto fail;
	    /* create basetype */
	    ncstat = NC_meta_create_type(ncid,basetype,root,&field->basetype);
  	    if(ncstat != NC_NOERR) goto fail;
        }
	break;

    case NC_VLEN: {
        nc_type basetype;
	ncstat = nc_inq_vlen(ncid,xtype,node->name,&node->size,&basetype);
	if(ncstat != NC_NOERR) goto fail;
	/* create basetype */
	ncstat = NC_meta_create_type(ncid,basetype,root,&node->basetype);
  	if(ncstat != NC_NOERR) goto fail;
	} break;

    case NC_GROUP: {
	nc_type* typeids;
	ncstat = nc_inq_typeids(ncid,&node->nelems,NULL);
	if(ncstat != NC_NOERR) goto fail;
	typeids = (nc_type*)calloc(node->nelems,sizeof(nc_type));
	if(typeids == NULL) {ncstat = NC_ENOMEM; goto fail;}
	ncstat = nc_inq_typeids(ncid,&node->nelems,typeids);
	if(ncstat != NC_NOERR) goto fail;
	break;

    default: abort();
    }        
}

static int
mapnctype(int ncid, nc_type xtype, nc_meta* metatypep)
{
    int ncstat = NC_NOERR;
    if(metatypep == NULL) goto done;
    if(xtype <= NC_MAX_ATOMIC_TYPE) goto done;
    ncstat = nc_inq_user_type(ncid,xtype,NULL,NULL,NULL,NULL,metatypep);
done:
    return ncstat;
}