1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/* This is part of the netCDF package. Copyright 2018 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use.
This program sets up HDF5 files that contain scalar attributes and
variables, of both string and numeric datatypes. ncdump should handle
all of these.
Russ Rew
*/
#include <nc_tests.h>
#include "err_macros.h"
#include <hdf5.h>
#define FILE_NAME "tst_h_scalar.nc"
#define VSTR_ATT1_NAME "vstratt1"
#define VSTR_ATT2_NAME "vstratt2"
#define VSTR_ATT3_NAME "vstratt3"
#define VSTR_ATT4_NAME "vstratt4"
#define VSTR_VAR1_NAME "vstrvar1"
#define VSTR_VAR2_NAME "vstrvar2"
#define VSTR_VAR3_NAME "vstrvar3"
#define VSTR_VAR4_NAME "vstrvar4"
#define FSTR_ATT_NAME "fstratt"
#define FSTR_VAR_NAME "fstrvar"
#define INT_ATT_NAME "intatt"
#define INT_VAR_NAME "intvar"
int
add_attrs(hid_t objid)
{
hid_t scalar_spaceid = -1;
hid_t vlstr_typeid = -1, fixstr_typeid = -1;
char *vlstr;
hid_t attid = -1;
/* Create scalar dataspace */
if ((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR_GOTO;
/* Create string datatypes */
if ((vlstr_typeid = H5Tcreate(H5T_STRING, (size_t)H5T_VARIABLE)) < 0) ERR_GOTO;
if ((fixstr_typeid = H5Tcreate(H5T_STRING, (size_t)10)) < 0) ERR_GOTO;
/* Create attribute with VL string datatype on object */
if ((attid = H5Acreate2(objid, VSTR_ATT1_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO;
/* No write, use fill value */
if (H5Aclose(attid) < 0) ERR_GOTO;
/* Create attribute with VL string datatype on object */
if ((attid = H5Acreate2(objid, VSTR_ATT2_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO;
vlstr = NULL;
if (H5Awrite(attid, vlstr_typeid, &vlstr) < 0) ERR_GOTO;
if (H5Aclose(attid) < 0) ERR_GOTO;
/* Create attribute with VL string datatype on object */
if ((attid = H5Acreate2(objid, VSTR_ATT3_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO;
vlstr = malloc(10);
*vlstr = '\0';
if (H5Awrite(attid, vlstr_typeid, &vlstr) < 0) ERR_GOTO;
if (H5Aclose(attid) < 0) ERR_GOTO;
/* Create attribute with VL string datatype on object */
if ((attid = H5Acreate2(objid, VSTR_ATT4_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO;
strcpy(vlstr, "foo");
if (H5Awrite(attid, vlstr_typeid, &vlstr) < 0) ERR_GOTO;
free(vlstr);
if (H5Aclose(attid) < 0) ERR_GOTO;
/* Create attribute with fixed-length string datatype on object */
if ((attid = H5Acreate2(objid, FSTR_ATT_NAME, fixstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO;
if (H5Aclose(attid) < 0) ERR_GOTO;
/* Create attribute with native integer datatype on object */
if ((attid = H5Acreate2(objid, INT_ATT_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO;
if (H5Aclose(attid) < 0) ERR_GOTO;
/* Clean up objects created */
if (H5Sclose(scalar_spaceid) < 0) ERR_GOTO;
if (H5Tclose(vlstr_typeid) < 0) ERR_GOTO;
if (H5Tclose(fixstr_typeid) < 0) ERR_GOTO;
return(0);
error:
H5E_BEGIN_TRY {
H5Aclose(attid);
H5Sclose(scalar_spaceid);
H5Tclose(vlstr_typeid);
H5Tclose(fixstr_typeid);
} H5E_END_TRY;
return(-1);
}
int
main()
{
printf("\n*** Create file with datasets & attributes that have scalar dataspaces...");
{
hid_t fileid;
hid_t fcplid;
hid_t dsetid;
hid_t dcplid;
hid_t scalar_spaceid;
hid_t vlstr_typeid, fixstr_typeid;
/* 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(FILE_NAME, H5F_ACC_TRUNC, fcplid, H5P_DEFAULT)) < 0) ERR;
/* Close file creation property list */
if (H5Pclose(fcplid) < 0) ERR;
/* Create variable-length string datatype */
if ((vlstr_typeid = H5Tcreate(H5T_STRING, (size_t)H5T_VARIABLE)) < 0) ERR;
/* Create fixed-length string datatype */
if ((fixstr_typeid = H5Tcreate(H5T_STRING, (size_t)10)) < 0) ERR;
/* Set creation ordering for dataset, so we can revise its contents later */
if ((dcplid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
if (H5Pset_attr_creation_order(dcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
/* Create scalar dataset with VL string datatype */
if ((dsetid = H5Dcreate2(fileid, VSTR_VAR1_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR;
/* Add attributes to dataset */
if (add_attrs(dsetid) < 0) ERR;
/* Close VL string dataset */
if (H5Dclose(dsetid) < 0) ERR;
/* Create scalar dataset with fixed-length string datatype */
if ((dsetid = H5Dcreate2(fileid, FSTR_VAR_NAME, fixstr_typeid, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR;
/* Add attributes to dataset */
if (add_attrs(dsetid) < 0) ERR;
/* Close fixed-length string dataset */
if (H5Dclose(dsetid) < 0) ERR;
/* Create scalar dataset with native integer datatype */
if ((dsetid = H5Dcreate2(fileid, INT_VAR_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR;
/* Add attributes to dataset */
if (add_attrs(dsetid) < 0) ERR;
/* Close native integer dataset */
if (H5Dclose(dsetid) < 0) ERR;
/* Add attributes to root group */
if (add_attrs(fileid) < 0) ERR;
/* Close dataset creation property list */
if (H5Pclose(dcplid) < 0) ERR;
/* Close string datatypes */
if (H5Tclose(vlstr_typeid) < 0) ERR;
if (H5Tclose(fixstr_typeid) < 0) ERR;
/* Close rest */
if (H5Sclose(scalar_spaceid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
}
SUMMARIZE_ERR;
printf("*** Revise file through netCDF-4 API...");
{
int ncid, varid;
char *vlstr;
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
/* Define new VL string variable */
if (nc_def_var(ncid, VSTR_VAR2_NAME , NC_STRING, 0, NULL, &varid)) ERR;
/* Write to the variable */
vlstr = NULL;
if (nc_put_var(ncid, varid, &vlstr)) ERR;
/* Define new VL string variable */
if (nc_def_var(ncid, VSTR_VAR3_NAME , NC_STRING, 0, NULL, &varid)) ERR;
/* Write to the variable */
vlstr = malloc(10);
*vlstr = '\0';
if (nc_put_var(ncid, varid, &vlstr)) ERR;
/* Define new VL string variable */
if (nc_def_var(ncid, VSTR_VAR4_NAME , NC_STRING, 0, NULL, &varid)) ERR;
/* Write to the variable */
strcpy(vlstr, "foo");
if (nc_put_var(ncid, varid, &vlstr)) ERR;
free(vlstr);
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}
|