mirror of
https://github.com/Unidata/netcdf-c.git
synced 2025-04-06 18:00:24 +08:00
created function create_dim_wo_var()
This commit is contained in:
parent
a89f9ddeb9
commit
42c64598dc
153
libhdf5/nc4hdf.c
153
libhdf5/nc4hdf.c
@ -1707,6 +1707,92 @@ write_var(NC_VAR_INFO_T *var, NC_GRP_INFO_T *grp, nc_bool_t write_dimid)
|
||||
return NC_NOERR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Write a HDF5 dataset which is a dimension without a
|
||||
* coordinate variable. This is a special 1-D dataset.
|
||||
*
|
||||
* @param dim Pointer to dim info struct.
|
||||
* @param grp Pointer to group info struct.
|
||||
* @param write_dimid
|
||||
*
|
||||
* @returns ::NC_NOERR No error.
|
||||
* @returns ::NC_EPERM Read-only file.
|
||||
* @returns ::NC_EHDFERR HDF5 returned error.
|
||||
* @author Ed Hartnett
|
||||
*/
|
||||
static int
|
||||
create_dim_wo_var(NC_DIM_INFO_T *dim)
|
||||
{
|
||||
NC_HDF5_DIM_INFO_T *hdf5_dim;
|
||||
NC_HDF5_GRP_INFO_T *hdf5_grp;
|
||||
hid_t spaceid = -1, create_propid = -1;
|
||||
hsize_t dims[1], max_dims[1], chunk_dims[1] = {1};
|
||||
char dimscale_wo_var[NC_MAX_NAME];
|
||||
int retval;
|
||||
|
||||
LOG((4, "%s: creating dim %s", __func__, dim->hdr.name));
|
||||
|
||||
/* Sanity check */
|
||||
assert(!dim->coord_var);
|
||||
|
||||
/* Get HDF5-specific dim and group info. */
|
||||
hdf5_grp = (NC_HDF5_GRP_INFO_T *)dim->container->format_grp_info;
|
||||
hdf5_dim = (NC_HDF5_DIM_INFO_T *)dim->format_dim_info;
|
||||
|
||||
/* Create a property list. */
|
||||
if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Turn off recording of times associated with this object. */
|
||||
if (H5Pset_obj_track_times(create_propid, 0) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Set size of dataset to size of dimension. */
|
||||
dims[0] = dim->len;
|
||||
max_dims[0] = dim->len;
|
||||
|
||||
/* If this dimension scale is unlimited (i.e. it's an unlimited
|
||||
* dimension), then set up chunking, with a chunksize of 1. */
|
||||
if (dim->unlimited)
|
||||
{
|
||||
max_dims[0] = H5S_UNLIMITED;
|
||||
if (H5Pset_chunk(create_propid, 1, chunk_dims) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
}
|
||||
|
||||
/* Set up space. */
|
||||
if ((spaceid = H5Screate_simple(1, dims, max_dims)) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Turn on creation-order tracking. */
|
||||
if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED|
|
||||
H5P_CRT_ORDER_INDEXED) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Create the dataset that will be the dimension scale. */
|
||||
LOG((4, "%s: about to H5Dcreate1 a dimscale dataset %s", __func__,
|
||||
dim->hdr.name));
|
||||
if ((hdf5_dim->hdf_dimscaleid = H5Dcreate2(hdf5_grp->hdf_grpid, dim->hdr.name,
|
||||
H5T_IEEE_F32BE, spaceid,
|
||||
H5P_DEFAULT, create_propid,
|
||||
H5P_DEFAULT)) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Indicate that this is a scale. Also indicate that not
|
||||
* be shown to the user as a variable. It is hidden. It is
|
||||
* a DIM WITHOUT A VARIABLE! */
|
||||
sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, (int)dim->len);
|
||||
if (H5DSset_scale(hdf5_dim->hdf_dimscaleid, dimscale_wo_var) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
exit:
|
||||
if (spaceid > 0 && H5Sclose(spaceid) < 0)
|
||||
BAIL2(NC_EHDFERR);
|
||||
if (create_propid > 0 && H5Pclose(create_propid) < 0)
|
||||
BAIL2(NC_EHDFERR);
|
||||
return NC_NOERR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Write a dimension.
|
||||
*
|
||||
@ -1722,76 +1808,21 @@ write_var(NC_VAR_INFO_T *var, NC_GRP_INFO_T *grp, nc_bool_t write_dimid)
|
||||
static int
|
||||
write_dim(NC_DIM_INFO_T *dim, NC_GRP_INFO_T *grp, nc_bool_t write_dimid)
|
||||
{
|
||||
hid_t spaceid = -1, create_propid = -1;
|
||||
NC_HDF5_GRP_INFO_T *hdf5_grp;
|
||||
NC_HDF5_DIM_INFO_T *hdf5_dim;
|
||||
hsize_t *new_size = NULL;
|
||||
int retval = NC_NOERR;
|
||||
|
||||
assert(dim && dim->format_dim_info && grp && grp->format_grp_info);
|
||||
|
||||
/* Get HDF5-specific dim and group info. */
|
||||
hdf5_dim = (NC_HDF5_DIM_INFO_T *)dim->format_dim_info;
|
||||
hdf5_grp = (NC_HDF5_GRP_INFO_T *)grp->format_grp_info;
|
||||
|
||||
/* If there's no dimscale dataset for this dim, create one,
|
||||
* and mark that it should be hidden from netCDF as a
|
||||
* variable. (That is, it should appear as a dimension
|
||||
* without an associated variable.) */
|
||||
if (!hdf5_dim->hdf_dimscaleid)
|
||||
{
|
||||
hsize_t dims[1], max_dims[1], chunk_dims[1] = {1};
|
||||
char dimscale_wo_var[NC_MAX_NAME];
|
||||
|
||||
LOG((4, "%s: creating dim %s", __func__, dim->hdr.name));
|
||||
|
||||
/* Sanity check */
|
||||
assert(!dim->coord_var);
|
||||
|
||||
/* Create a property list. If this dimension scale is
|
||||
* unlimited (i.e. it's an unlimited dimension), then set
|
||||
* up chunking, with a chunksize of 1. */
|
||||
if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Turn off recording of times associated with this object. */
|
||||
if (H5Pset_obj_track_times(create_propid,0)<0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
dims[0] = dim->len;
|
||||
max_dims[0] = dim->len;
|
||||
if (dim->unlimited)
|
||||
{
|
||||
max_dims[0] = H5S_UNLIMITED;
|
||||
if (H5Pset_chunk(create_propid, 1, chunk_dims) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
}
|
||||
|
||||
/* Set up space. */
|
||||
if ((spaceid = H5Screate_simple(1, dims, max_dims)) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Turn on creation-order tracking. */
|
||||
if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED|
|
||||
H5P_CRT_ORDER_INDEXED) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Create the dataset that will be the dimension scale. */
|
||||
LOG((4, "%s: about to H5Dcreate1 a dimscale dataset %s", __func__,
|
||||
dim->hdr.name));
|
||||
if ((hdf5_dim->hdf_dimscaleid = H5Dcreate2(hdf5_grp->hdf_grpid, dim->hdr.name,
|
||||
H5T_IEEE_F32BE, spaceid,
|
||||
H5P_DEFAULT, create_propid,
|
||||
H5P_DEFAULT)) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
|
||||
/* Indicate that this is a scale. Also indicate that not
|
||||
* be shown to the user as a variable. It is hidden. It is
|
||||
* a DIM WITHOUT A VARIABLE! */
|
||||
sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, (int)dim->len);
|
||||
if (H5DSset_scale(hdf5_dim->hdf_dimscaleid, dimscale_wo_var) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
}
|
||||
if ((retval = create_dim_wo_var(dim)))
|
||||
BAIL(retval);
|
||||
|
||||
/* Did we extend an unlimited dimension? */
|
||||
if (dim->extended)
|
||||
@ -1806,6 +1837,7 @@ write_dim(NC_DIM_INFO_T *dim, NC_GRP_INFO_T *grp, nc_bool_t write_dimid)
|
||||
if (v1)
|
||||
{
|
||||
NC_HDF5_VAR_INFO_T *hdf5_v1;
|
||||
hsize_t *new_size;
|
||||
int d1;
|
||||
|
||||
hdf5_v1 = (NC_HDF5_VAR_INFO_T *)v1->format_var_info;
|
||||
@ -1821,6 +1853,7 @@ write_dim(NC_DIM_INFO_T *dim, NC_GRP_INFO_T *grp, nc_bool_t write_dimid)
|
||||
}
|
||||
if (H5Dset_extent(hdf5_v1->hdf_datasetid, new_size) < 0)
|
||||
BAIL(NC_EHDFERR);
|
||||
free(new_size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1833,12 +1866,6 @@ write_dim(NC_DIM_INFO_T *dim, NC_GRP_INFO_T *grp, nc_bool_t write_dimid)
|
||||
BAIL(retval);
|
||||
|
||||
exit:
|
||||
if (spaceid > 0 && H5Sclose(spaceid) < 0)
|
||||
BAIL2(NC_EHDFERR);
|
||||
if (create_propid > 0 && H5Pclose(create_propid) < 0)
|
||||
BAIL2(NC_EHDFERR);
|
||||
if (new_size)
|
||||
free(new_size);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
@ -34,6 +34,9 @@ See \ref copyright file for more info.
|
||||
#define NDIM1 1
|
||||
#define NDIM3 3
|
||||
#define NUM_ENDDEF_SETTINGS 2
|
||||
#define D1_NAME "d1"
|
||||
#define D2_NAME "d2"
|
||||
#define TMP_NAME "t1"
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
@ -44,195 +47,228 @@ main(int argc, char **argv)
|
||||
|
||||
fprintf(stderr,"*** Testing more renames\n");
|
||||
|
||||
for (format = 0; format < NUM_FORMATS; format++)
|
||||
/* for (format = 0; format < NUM_FORMATS; format++) */
|
||||
/* { */
|
||||
/* fprintf(stderr,"*** test renaming 3 dimensions with format %d...", */
|
||||
/* formats[format]); */
|
||||
/* { */
|
||||
/* char filename[NC_MAX_NAME + 1]; */
|
||||
/* int ncid, dimid[NDIM3]; */
|
||||
/* int dimid_in; */
|
||||
/* int enddef_setting; */
|
||||
|
||||
/* if (nc_set_default_format(formats[format], NULL)) ERR; */
|
||||
|
||||
/* for (enddef_setting = 0; enddef_setting < NUM_ENDDEF_SETTINGS; */
|
||||
/* enddef_setting++) */
|
||||
/* { */
|
||||
/* sprintf(filename, "%s_%d_%d.nc", TEST_NAME, formats[format], */
|
||||
/* enddef_setting); */
|
||||
|
||||
/* /\* Create file with three dims. *\/ */
|
||||
/* if (nc_create(filename, 0, &ncid)) ERR; */
|
||||
/* if (nc_def_dim(ncid, LAT, DIM1_LEN, &dimid[0])) ERR; */
|
||||
/* if (nc_def_dim(ncid, LON, DIM1_LEN, &dimid[1])) ERR; */
|
||||
/* if (nc_def_dim(ncid, LEV, DIM1_LEN, &dimid[2])) ERR; */
|
||||
|
||||
/* if (enddef_setting) */
|
||||
/* { */
|
||||
/* if (nc_enddef(ncid)) ERR; */
|
||||
/* if (nc_redef(ncid)) ERR; */
|
||||
/* } */
|
||||
|
||||
/* /\* Rename the dimensions. *\/ */
|
||||
/* if (nc_rename_dim(ncid, 0, DIM_X)) ERR; */
|
||||
/* if (nc_rename_dim(ncid, 1, DIM_Y)) ERR; */
|
||||
/* if (nc_rename_dim(ncid, 2, DIM_Z)) ERR; */
|
||||
|
||||
/* /\* Close the file. *\/ */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
|
||||
/* /\* Reopen the file and check. *\/ */
|
||||
/* if (nc_open(filename, NC_NOWRITE, &ncid)) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_X, &dimid_in)) ERR; */
|
||||
/* if (dimid_in != 0) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_Y, &dimid_in)) ERR; */
|
||||
/* if (dimid_in != 1) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_Z, &dimid_in)) ERR; */
|
||||
/* if (dimid_in != 2) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
/* } /\* next enddef setting *\/ */
|
||||
/* } */
|
||||
/* SUMMARIZE_ERR; */
|
||||
|
||||
/* fprintf(stderr,"*** test renaming 3 dims with coord data format %d...", */
|
||||
/* formats[format]); */
|
||||
/* { */
|
||||
/* char filename[NC_MAX_NAME + 1]; */
|
||||
/* int ncid, dimid[NDIM3], varid[NDIM3]; */
|
||||
/* int dimid_in; */
|
||||
/* int lat_data[DIM1_LEN] = {0, 1, 2, 3}; */
|
||||
/* int lon_data[DIM1_LEN] = {0, 10, 20, 30}; */
|
||||
/* int lev_data[DIM1_LEN] = {0, 100, 200, 300}; */
|
||||
|
||||
/* if (nc_set_default_format(formats[format], NULL)) ERR; */
|
||||
|
||||
/* sprintf(filename, "%s_data_%d.nc", TEST_NAME, formats[format]); */
|
||||
|
||||
/* /\* Create file with three dims. *\/ */
|
||||
/* if (nc_create(filename, 0, &ncid)) ERR; */
|
||||
/* if (nc_def_dim(ncid, LAT, DIM1_LEN, &dimid[0])) ERR; */
|
||||
/* if (nc_def_dim(ncid, LON, DIM1_LEN, &dimid[1])) ERR; */
|
||||
/* if (nc_def_dim(ncid, LEV, DIM1_LEN, &dimid[2])) ERR; */
|
||||
|
||||
/* /\* Define coordinate data vars. *\/ */
|
||||
/* if (nc_def_var(ncid, LAT, NC_INT, NDIM1, &dimid[0], &varid[0])) ERR; */
|
||||
/* if (nc_def_var(ncid, LON, NC_INT, NDIM1, &dimid[1], &varid[1])) ERR; */
|
||||
/* if (nc_def_var(ncid, LEV, NC_INT, NDIM1, &dimid[2], &varid[2])) ERR; */
|
||||
|
||||
/* if (nc_enddef(ncid)) ERR; */
|
||||
|
||||
/* if (nc_put_var(ncid, 0, lat_data)) ERR; */
|
||||
/* if (nc_put_var(ncid, 1, lon_data)) ERR; */
|
||||
/* if (nc_put_var(ncid, 2, lev_data)) ERR; */
|
||||
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
/* if (nc_open(filename, NC_WRITE, &ncid)) ERR; */
|
||||
/* if (nc_redef(ncid)) ERR; */
|
||||
|
||||
/* /\* Rename the dimensions. *\/ */
|
||||
/* if (nc_rename_dim(ncid, 0, DIM_X)) ERR; */
|
||||
/* if (nc_rename_dim(ncid, 1, DIM_Y)) ERR; */
|
||||
/* if (nc_rename_dim(ncid, 2, DIM_Z)) ERR; */
|
||||
|
||||
/* /\* Close the file. *\/ */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
|
||||
/* /\* Reopen the file and check. *\/ */
|
||||
/* if (nc_open(filename, NC_NOWRITE, &ncid)) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_X, &dimid_in)) ERR; */
|
||||
/* if (dimid_in != 0) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_Y, &dimid_in)) ERR; */
|
||||
/* if (dimid_in != 1) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_Z, &dimid_in)) ERR; */
|
||||
/* if (dimid_in != 2) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
/* } */
|
||||
/* SUMMARIZE_ERR; */
|
||||
|
||||
/* } /\* next format *\/ */
|
||||
|
||||
/* #define FILE_NAME1 "tst_dims_foo1.nc" */
|
||||
/* #define DIM_NAME "lat_T42" */
|
||||
/* #define VAR_NAME DIM_NAME */
|
||||
/* #define DIM_NAME2 "lat" */
|
||||
/* #define VAR_NAME2 DIM_NAME2 */
|
||||
/* #define RANK_lat_T42 1 */
|
||||
/* fprintf(stderr,"*** test renaming with sync..."); */
|
||||
/* { */
|
||||
/* int ncid, dimid, varid; */
|
||||
/* char file_name[NC_MAX_NAME + 1]; */
|
||||
/* char name[NC_MAX_NAME + 1]; */
|
||||
|
||||
/* /\* Create file with dim and associated coordinate var. *\/ */
|
||||
/* sprintf(file_name, "%s_sync.nc", TEST_NAME); */
|
||||
/* nc_set_log_level(4); */
|
||||
/* if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; */
|
||||
/* if (nc_def_dim(ncid, DIM_NAME_END, DIM1_LEN, &dimid)) ERR; */
|
||||
/* if (nc_def_var(ncid, DIM_NAME_END, NC_INT, NDIM1, &dimid, &varid)) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
|
||||
/* if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; */
|
||||
/* if (nc_def_dim(ncid, DIM_NAME_START, DIM1_LEN, &dimid)) ERR; */
|
||||
/* if (nc_def_var(ncid, DIM_NAME_END, NC_INT, NDIM1, &dimid, &varid)) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
|
||||
/* if (nc_open(file_name, NC_WRITE, &ncid)) ERR; */
|
||||
/* if (nc_rename_dim(ncid, dimid, DIM_NAME_END)) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
|
||||
/* /\* Reopen file and check, *\/ */
|
||||
/* if (nc_open(file_name, NC_WRITE, &ncid)) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_NAME_END, &dimid)) ERR; */
|
||||
/* if (nc_inq_varid(ncid, DIM_NAME_END, &varid)) ERR; */
|
||||
/* if (nc_inq_dimname(ncid, dimid, name)) ERR; */
|
||||
/* if (strcmp(name, DIM_NAME_END)) ERR; */
|
||||
/* if (nc_inq_varname(ncid, varid, name)) ERR; */
|
||||
/* if (strcmp(name, DIM_NAME_END)) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
/* } */
|
||||
/* SUMMARIZE_ERR; */
|
||||
/* fprintf(stderr,"*** test renaming with sync..."); */
|
||||
/* { */
|
||||
/* int ncid, dimid, varid; */
|
||||
/* char file_name[NC_MAX_NAME + 1]; */
|
||||
/* char name[NC_MAX_NAME + 1]; */
|
||||
|
||||
/* /\* Create file with dim and associated coordinate var. *\/ */
|
||||
/* sprintf(file_name, "%s_sync.nc", TEST_NAME); */
|
||||
/* if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR; */
|
||||
/* if (nc_def_dim(ncid, DIM_NAME_START, DIM1_LEN, &dimid)) ERR; */
|
||||
/* if (nc_def_var(ncid, VAR_NAME_START, NC_INT, NDIM1, &dimid, &varid)) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
|
||||
/* nc_set_log_level(4); */
|
||||
/* /\* Open the file and rename the var. *\/ */
|
||||
/* if (nc_open(file_name, NC_WRITE, &ncid)) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_NAME_START, &dimid)) ERR; */
|
||||
/* if (nc_inq_varid(ncid, VAR_NAME_START, &varid)) ERR; */
|
||||
/* if (nc_rename_var(ncid, varid, VAR_NAME_END)) ERR; */
|
||||
|
||||
/* /\* Sync to disk. Now the file has one dim and one var. The dim */
|
||||
/* * is a dimscale only dataset, and the var is a dataset with a */
|
||||
/* * dimscale attached pointing to the dim. *\/ */
|
||||
/* /\* if (nc_sync(ncid)) ERR; *\/ */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
/* if (nc_open(file_name, NC_WRITE, &ncid)) ERR; */
|
||||
/* /\* Now rename the dim to the same name as the var. After this */
|
||||
/* * there will be one dataset, called DIM_NAME_END, which will be */
|
||||
/* * a dimscale. *\/ */
|
||||
/* if (nc_rename_dim(ncid, dimid, DIM_NAME_END)) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
|
||||
/* /\* Reopen file and check, *\/ */
|
||||
/* if (nc_open(file_name, NC_WRITE, &ncid)) ERR; */
|
||||
/* if (nc_inq_dimid(ncid, DIM_NAME_END, &dimid)) ERR; */
|
||||
/* if (nc_inq_varid(ncid, VAR_NAME_END, &varid)) ERR; */
|
||||
/* if (nc_inq_dimname(ncid, dimid, name)) ERR; */
|
||||
/* if (strcmp(name, DIM_NAME_END)) ERR; */
|
||||
/* if (nc_inq_varname(ncid, varid, name)) ERR; */
|
||||
/* if (strcmp(name, VAR_NAME_END)) ERR; */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
/* } */
|
||||
/* SUMMARIZE_ERR; */
|
||||
fprintf(stderr,"*** test renaming two coord vars...");
|
||||
{
|
||||
fprintf(stderr,"*** test renaming 3 dimensions with format %d...",
|
||||
formats[format]);
|
||||
{
|
||||
char filename[NC_MAX_NAME + 1];
|
||||
int ncid, dimid[NDIM3];
|
||||
int dimid_in;
|
||||
int enddef_setting;
|
||||
|
||||
if (nc_set_default_format(formats[format], NULL)) ERR;
|
||||
|
||||
for (enddef_setting = 0; enddef_setting < NUM_ENDDEF_SETTINGS;
|
||||
enddef_setting++)
|
||||
{
|
||||
sprintf(filename, "%s_%d_%d.nc", TEST_NAME, formats[format],
|
||||
enddef_setting);
|
||||
|
||||
/* Create file with three dims. */
|
||||
if (nc_create(filename, 0, &ncid)) ERR;
|
||||
if (nc_def_dim(ncid, LAT, DIM1_LEN, &dimid[0])) ERR;
|
||||
if (nc_def_dim(ncid, LON, DIM1_LEN, &dimid[1])) ERR;
|
||||
if (nc_def_dim(ncid, LEV, DIM1_LEN, &dimid[2])) ERR;
|
||||
|
||||
if (enddef_setting)
|
||||
{
|
||||
if (nc_enddef(ncid)) ERR;
|
||||
if (nc_redef(ncid)) ERR;
|
||||
}
|
||||
|
||||
/* Rename the dimensions. */
|
||||
if (nc_rename_dim(ncid, 0, DIM_X)) ERR;
|
||||
if (nc_rename_dim(ncid, 1, DIM_Y)) ERR;
|
||||
if (nc_rename_dim(ncid, 2, DIM_Z)) ERR;
|
||||
|
||||
/* Close the file. */
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
/* Reopen the file and check. */
|
||||
if (nc_open(filename, NC_NOWRITE, &ncid)) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_X, &dimid_in)) ERR;
|
||||
if (dimid_in != 0) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_Y, &dimid_in)) ERR;
|
||||
if (dimid_in != 1) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_Z, &dimid_in)) ERR;
|
||||
if (dimid_in != 2) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
} /* next enddef setting */
|
||||
}
|
||||
SUMMARIZE_ERR;
|
||||
|
||||
fprintf(stderr,"*** test renaming 3 dims with coord data format %d...",
|
||||
formats[format]);
|
||||
{
|
||||
char filename[NC_MAX_NAME + 1];
|
||||
int ncid, dimid[NDIM3], varid[NDIM3];
|
||||
int dimid_in;
|
||||
int lat_data[DIM1_LEN] = {0, 1, 2, 3};
|
||||
int lon_data[DIM1_LEN] = {0, 10, 20, 30};
|
||||
int lev_data[DIM1_LEN] = {0, 100, 200, 300};
|
||||
|
||||
if (nc_set_default_format(formats[format], NULL)) ERR;
|
||||
|
||||
sprintf(filename, "%s_data_%d.nc", TEST_NAME, formats[format]);
|
||||
|
||||
/* Create file with three dims. */
|
||||
if (nc_create(filename, 0, &ncid)) ERR;
|
||||
if (nc_def_dim(ncid, LAT, DIM1_LEN, &dimid[0])) ERR;
|
||||
if (nc_def_dim(ncid, LON, DIM1_LEN, &dimid[1])) ERR;
|
||||
if (nc_def_dim(ncid, LEV, DIM1_LEN, &dimid[2])) ERR;
|
||||
|
||||
/* Define coordinate data vars. */
|
||||
if (nc_def_var(ncid, LAT, NC_INT, NDIM1, &dimid[0], &varid[0])) ERR;
|
||||
if (nc_def_var(ncid, LON, NC_INT, NDIM1, &dimid[1], &varid[1])) ERR;
|
||||
if (nc_def_var(ncid, LEV, NC_INT, NDIM1, &dimid[2], &varid[2])) ERR;
|
||||
|
||||
if (nc_enddef(ncid)) ERR;
|
||||
|
||||
if (nc_put_var(ncid, 0, lat_data)) ERR;
|
||||
if (nc_put_var(ncid, 1, lon_data)) ERR;
|
||||
if (nc_put_var(ncid, 2, lev_data)) ERR;
|
||||
|
||||
if (nc_close(ncid)) ERR;
|
||||
if (nc_open(filename, NC_WRITE, &ncid)) ERR;
|
||||
if (nc_redef(ncid)) ERR;
|
||||
|
||||
/* Rename the dimensions. */
|
||||
if (nc_rename_dim(ncid, 0, DIM_X)) ERR;
|
||||
if (nc_rename_dim(ncid, 1, DIM_Y)) ERR;
|
||||
if (nc_rename_dim(ncid, 2, DIM_Z)) ERR;
|
||||
|
||||
/* Close the file. */
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
/* Reopen the file and check. */
|
||||
if (nc_open(filename, NC_NOWRITE, &ncid)) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_X, &dimid_in)) ERR;
|
||||
if (dimid_in != 0) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_Y, &dimid_in)) ERR;
|
||||
if (dimid_in != 1) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_Z, &dimid_in)) ERR;
|
||||
if (dimid_in != 2) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
}
|
||||
SUMMARIZE_ERR;
|
||||
|
||||
} /* next format */
|
||||
|
||||
#define FILE_NAME1 "tst_dims_foo1.nc"
|
||||
#define DIM_NAME "lat_T42"
|
||||
#define VAR_NAME DIM_NAME
|
||||
#define DIM_NAME2 "lat"
|
||||
#define VAR_NAME2 DIM_NAME2
|
||||
#define RANK_lat_T42 1
|
||||
fprintf(stderr,"*** test renaming with sync...");
|
||||
{
|
||||
int ncid, dimid, varid;
|
||||
char file_name[NC_MAX_NAME + 1];
|
||||
char name[NC_MAX_NAME + 1];
|
||||
|
||||
/* Create file with dim and associated coordinate var. */
|
||||
sprintf(file_name, "%s_sync.nc", TEST_NAME);
|
||||
nc_set_log_level(4);
|
||||
if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
|
||||
if (nc_def_dim(ncid, DIM_NAME_END, DIM1_LEN, &dimid)) ERR;
|
||||
if (nc_def_var(ncid, DIM_NAME_END, NC_INT, NDIM1, &dimid, &varid)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
|
||||
if (nc_def_dim(ncid, DIM_NAME_START, DIM1_LEN, &dimid)) ERR;
|
||||
if (nc_def_var(ncid, DIM_NAME_END, NC_INT, NDIM1, &dimid, &varid)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
|
||||
if (nc_rename_dim(ncid, dimid, DIM_NAME_END)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
/* Reopen file and check, */
|
||||
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_NAME_END, &dimid)) ERR;
|
||||
if (nc_inq_varid(ncid, DIM_NAME_END, &varid)) ERR;
|
||||
if (nc_inq_dimname(ncid, dimid, name)) ERR;
|
||||
if (strcmp(name, DIM_NAME_END)) ERR;
|
||||
if (nc_inq_varname(ncid, varid, name)) ERR;
|
||||
if (strcmp(name, DIM_NAME_END)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
}
|
||||
SUMMARIZE_ERR;
|
||||
fprintf(stderr,"*** test renaming with sync...");
|
||||
{
|
||||
int ncid, dimid, varid;
|
||||
int ncid, dimid1, dimid2, varid1, varid2;
|
||||
char file_name[NC_MAX_NAME + 1];
|
||||
char name[NC_MAX_NAME + 1];
|
||||
|
||||
/* Create file with dim and associated coordinate var. */
|
||||
sprintf(file_name, "%s_sync.nc", TEST_NAME);
|
||||
if (nc_create(file_name, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
|
||||
if (nc_def_dim(ncid, DIM_NAME_START, DIM1_LEN, &dimid)) ERR;
|
||||
if (nc_def_var(ncid, VAR_NAME_START, NC_INT, NDIM1, &dimid, &varid)) ERR;
|
||||
if (nc_def_dim(ncid, D1_NAME, DIM1_LEN, &dimid1)) ERR;
|
||||
if (nc_def_dim(ncid, D2_NAME, DIM1_LEN, &dimid2)) ERR;
|
||||
if (nc_def_var(ncid, D1_NAME, NC_INT, NDIM1, &dimid1, &varid1)) ERR;
|
||||
if (nc_def_var(ncid, D2_NAME, NC_INT, NDIM1, &dimid2, &varid2)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
/* Open the file and rename the vars. */
|
||||
nc_set_log_level(4);
|
||||
/* Open the file and rename the var. */
|
||||
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_NAME_START, &dimid)) ERR;
|
||||
if (nc_inq_varid(ncid, VAR_NAME_START, &varid)) ERR;
|
||||
if (nc_rename_var(ncid, varid, VAR_NAME_END)) ERR;
|
||||
|
||||
/* Sync to disk. Now the file has one dim and one var. The dim
|
||||
* is a dimscale only dataset, and the var is a dataset with a
|
||||
* dimscale attached pointing to the dim. */
|
||||
/* if (nc_sync(ncid)) ERR; */
|
||||
if (nc_close(ncid)) ERR;
|
||||
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
|
||||
/* Now rename the dim to the same name as the var. After this
|
||||
* there will be one dataset, called DIM_NAME_END, which will be
|
||||
* a dimscale. */
|
||||
if (nc_rename_dim(ncid, dimid, DIM_NAME_END)) ERR;
|
||||
if (nc_rename_var(ncid, varid1, TMP_NAME)) ERR;
|
||||
/* if (nc_rename_var(ncid, varid2, D1_NAME)) ERR; */
|
||||
if (nc_close(ncid)) ERR;
|
||||
|
||||
/* Reopen file and check, */
|
||||
if (nc_open(file_name, NC_WRITE, &ncid)) ERR;
|
||||
if (nc_inq_dimid(ncid, DIM_NAME_END, &dimid)) ERR;
|
||||
if (nc_inq_varid(ncid, VAR_NAME_END, &varid)) ERR;
|
||||
if (nc_inq_dimname(ncid, dimid, name)) ERR;
|
||||
if (strcmp(name, DIM_NAME_END)) ERR;
|
||||
if (nc_inq_varname(ncid, varid, name)) ERR;
|
||||
if (strcmp(name, VAR_NAME_END)) ERR;
|
||||
if (nc_close(ncid)) ERR;
|
||||
/* if (nc_open(file_name, NC_WRITE, &ncid)) ERR; */
|
||||
/* /\* if (nc_inq_dimid(ncid, DIM_NAME_END, &dimid)) ERR; *\/ */
|
||||
/* /\* if (nc_inq_varid(ncid, VAR_NAME_END, &varid)) ERR; *\/ */
|
||||
/* /\* if (nc_inq_dimname(ncid, dimid, name)) ERR; *\/ */
|
||||
/* /\* if (strcmp(name, DIM_NAME_END)) ERR; *\/ */
|
||||
/* /\* if (nc_inq_varname(ncid, varid, name)) ERR; *\/ */
|
||||
/* /\* if (strcmp(name, VAR_NAME_END)) ERR; *\/ */
|
||||
/* if (nc_close(ncid)) ERR; */
|
||||
}
|
||||
SUMMARIZE_ERR;
|
||||
FINAL_RESULTS;
|
||||
|
Loading…
x
Reference in New Issue
Block a user