Sent this to Ed last weekend, but I thought maybe someone on the list
might have some insights:
Hi Ed: I found a bug in one of the compound data type examples. The
attached patch fixes it. I'm puzzled by one thing though. Your example
for writing an array of ints in a compound type was this:
/* Create a file with a compound type which contains an array of
* int. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct dim_rec), "SFDimRec",
&typeid)) ERR;
if (nc_insert_compound(ncid, typeid, "starfleet_id",
HOFFSET(struct dim_rec, starfleet_id), NC_INT)) ERR;
if (nc_insert_array_compound(ncid, typeid, "abilities",
HOFFSET(struct dim_rec, abilities), NC_INT, 1,
dim_sizes)) ERR;
if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR;
if (nc_def_var(ncid, "dimension_data", typeid, 1, dimids, &varid))
ERR;
if (nc_put_var(ncid, varid, dim_data_out)) ERR;
if (nc_close(ncid)) ERR;
/* Open the file and take a look. */
{
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR;
/* if (strcmp(name, "starbase_13") || ndims != 1 || natts != 0 ||
dimids[0] != 0) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 5 || size != sizeof(struct sf_med_rec) ||
strcmp(name, "SFMedRec")) ERR;
if (nc_get_var(ncid, varid, med_data_in)) ERR;
for (i=0; i<DIM_LEN; i++)
if (med_data_in[i].num_heads != med_data_out[i].num_heads ||
med_data_in[i].num_arms != med_data_out[i].num_arms ||
med_data_in[i].num_toes != med_data_out[i].num_toes ||
med_data_in[i].ago != med_data_out[i].ago ||
med_data_in[i].num_hairs != med_data_out[i].num_hairs) ERR;*/
if (nc_close(ncid)) ERR;
}
}
I changed the latter part of it to this
{
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR;
if (strcmp(name, "dimension_data") || ndims != 1 || natts != 0 ||
dimids[0] != 0) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 2 || size != sizeof(struct dim_rec) || strcmp(name,
"SFDimRec")) ERR;
if (nc_get_var(ncid, varid, dim_data_in)) ERR;
for (i=0; i<DIM_LEN; i++)
{if (dim_data_in[i].starfleet_id != dim_data_out[i].starfleet_id)
ERR;
for (j = 0; j < NUM_DIMENSIONS; j++)
if (dim_data_in[i].abilities[j] != dim_data_out[i].abilities[j])
ERR;
}
if (nc_close(ncid)) ERR;
}
}
This works fine. However, if I insert
ierr = nc_inq_compound_field(ncid, xtype, 1, name, &offset,
&field_xtype, &field_ndims, &field_sizes);
printf("%s %d %d %d name type ndims
dim1\n",name,field_xtype,field_ndims,field_sizes[0]);
I get
abilities 0 0 0 name type ndims dim1
I would have expected '4 1 7', since the field is an 1-d array of ints
with length 7. I suspect this is a bug in the implementation of arrays
as compound type fields, but I may be missing something.
-Jeff
--
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/CDC1 FAX : (303)497-6449
325 Broadway Web : http://www.cdc.noaa.gov/~jsw
Boulder, CO, USA 80305-3328 Office: Skaggs Research Cntr 1D-124
------------------------------------------------------------------------
--- tst_compounds.c.orig Sat Mar 4 14:47:54 2006
+++ tst_compounds.c.new Sat Mar 4 14:47:51 2006
@@ -62,7 +62,7 @@
int starfleet_id;
int abilities[NUM_DIMENSIONS];
};
- struct dim_rec dim_data_out[DIM_LEN];
+ struct dim_rec dim_data_out[DIM_LEN], dim_data_in[DIM_LEN];
/* StarFleet Human Resources Department has data records for all
* employees. */
@@ -281,16 +281,15 @@
{
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR;
-/* if (strcmp(name, "starbase_13") || ndims != 1 || natts != 0 ||
dimids[0] != 0) ERR;
+ if (strcmp(name, "dimension_data") || ndims != 1 || natts != 0 ||
dimids[0] != 0) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
- if (nfields != 5 || size != sizeof(struct sf_med_rec) || strcmp(name,
"SFMedRec")) ERR;
- if (nc_get_var(ncid, varid, med_data_in)) ERR;
+ if (nfields != 2 || size != sizeof(struct dim_rec) || strcmp(name,
"SFDimRec")) ERR;
+ if (nc_get_var(ncid, varid, dim_data_in)) ERR;
for (i=0; i<DIM_LEN; i++)
- if (med_data_in[i].num_heads != med_data_out[i].num_heads ||
- med_data_in[i].num_arms != med_data_out[i].num_arms ||
- med_data_in[i].num_toes != med_data_out[i].num_toes ||
- med_data_in[i].ago != med_data_out[i].ago ||
- med_data_in[i].num_hairs != med_data_out[i].num_hairs) ERR;*/
+ {if (dim_data_in[i].starfleet_id != dim_data_out[i].starfleet_id) ERR;
+ for (j = 0; j < NUM_DIMENSIONS; j++)
+ if (dim_data_in[i].abilities[j] != dim_data_out[i].abilities[j])
ERR;
+ }
if (nc_close(ncid)) ERR;
}
}
--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@xxxxxxxx
325 Broadway Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg