Previous: Using Compound Types Next: Using Variable Length Types, Continued Table of contents Frames User guide
2007 Unidata NetCDF Workshop for Developers and Data Providers > Using NetCDF-4 Features, Part 2

13.5 Using Variable Length Types
The variable length type can hold arrays of variable length.

 

   int ncid, typeid;
   nc_vlen_t data[DIM_LEN], data_in[DIM_LEN];
   size_t size_in, len_in;
   nc_type base_nc_type_in;
   char name_in[NC_MAX_NAME + 1];
   int *phoney, class_in;
   int i, j;

   /* Create phoney data. */
   for (i=0; i < DIM_LEN; i++)
   {
      if (!(phoney = malloc(sizeof(int) * i+1)))
	 return NC_ENOMEM;
      for (j=0; j < i+1; j++)
	 phoney[j] = -99;
      data[i].p = phoney;
      data[i].len = i+1;
   }

   {
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_vlen(ncid, "name1", NC_INT, &typeid)) ERR;
      if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, typeid, DIM_LEN, data)) ERR;
      if (nc_close(ncid)) ERR;

      /* Check it out. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_get_att(ncid, NC_GLOBAL, ATT_NAME, data_in)) ERR;
      for (i=0; i < DIM_LEN; i++)
	 for (j=0; j < data_in[i].len; j++)
	    if (*((int *)data_in[i].p) != *((int *)data[i].p))
	    {
	       printf("*((int *)data_in[%d].p = %d (0x%x)\n", i, *((int *)data_in[i].p),
		      *((int *)data_in[i].p));
	       ERR;
	    }

      if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &typeid, &len_in)) ERR;
      if (len_in != DIM_LEN) ERR;
      if (nc_inq_vlen(ncid, typeid, name_in, &size_in, &base_nc_type_in)) ERR;
      if (base_nc_type_in != NC_INT || (size_in != 4 && strcmp(name_in, VLEN_NAME))) ERR;
      if (nc_inq_user_type(ncid, typeid, name_in, &size_in, &base_nc_type_in, NULL, &class_in)) ERR;
      if (base_nc_type_in != NC_INT || (size_in != 4 && strcmp(name_in, VLEN_NAME))) ERR;
      if (nc_close(ncid)) ERR;
   }

 


Previous: Using Compound Types Next: Using Variable Length Types, Continued Table of contents Frames User guide
2007 Unidata NetCDF Workshop for Developers and Data Providers > Using NetCDF-4 Features, Part 2