[409] | 1 | /** \file |
---|
| 2 | Functions to write attributes. |
---|
| 3 | |
---|
| 4 | These functions read and write attributes. |
---|
| 5 | |
---|
| 6 | Copyright 2010 University Corporation for Atmospheric |
---|
| 7 | Research/Unidata. See \ref copyright file for more info. */ |
---|
| 8 | |
---|
| 9 | #include "ncdispatch.h" |
---|
| 10 | |
---|
| 11 | /** \name Writing Attributes |
---|
| 12 | |
---|
| 13 | Functions to write attributes. */ |
---|
| 14 | /*! \{ */ |
---|
| 15 | |
---|
| 16 | /*! |
---|
| 17 | \ingroup attributes |
---|
| 18 | Write a string attribute. |
---|
| 19 | |
---|
| 20 | The function nc_put_att_string adds or changes a variable attribute or |
---|
| 21 | global attribute of an open netCDF dataset. The string type is only |
---|
| 22 | available in netCDF-4/HDF5 files, when ::NC_CLASSIC_MODEL has not been |
---|
| 23 | used in nc_create(). |
---|
| 24 | |
---|
| 25 | \param ncid NetCDF or group ID, from a previous call to nc_open(), |
---|
| 26 | nc_create(), nc_def_grp(), or associated inquiry functions such as |
---|
| 27 | nc_inq_ncid(). |
---|
| 28 | |
---|
| 29 | \param varid Variable ID of the variable to which the attribute will |
---|
| 30 | be assigned or ::NC_GLOBAL for a global or group attribute. |
---|
| 31 | |
---|
| 32 | \param name Attribute \ref object_name. \ref attribute_conventions may |
---|
| 33 | apply. |
---|
| 34 | |
---|
| 35 | \param len Number of values provided for the attribute. |
---|
| 36 | |
---|
| 37 | \param value Pointer to one or more values. |
---|
| 38 | |
---|
| 39 | \returns ::NC_NOERR No error. |
---|
| 40 | \returns ::NC_EINVAL Trying to set global _FillValue. |
---|
| 41 | \returns ::NC_ENOTVAR Couldn't find varid. |
---|
| 42 | \returns ::NC_EBADTYPE Fill value and var must be same type. |
---|
| 43 | \returns ::NC_ENOMEM Out of memory |
---|
| 44 | \returns ::NC_ELATEFILL Fill values must be written while the file |
---|
| 45 | is still in initial define mode. |
---|
| 46 | */ |
---|
| 47 | int |
---|
| 48 | nc_put_att_string(int ncid, int varid, const char *name, |
---|
| 49 | size_t len, const char** value) |
---|
| 50 | { |
---|
| 51 | NC* ncp; |
---|
| 52 | int stat = NC_check_id(ncid, &ncp); |
---|
| 53 | if(stat != NC_NOERR) return stat; |
---|
| 54 | return ncp->dispatch->put_att(ncid, varid, name, NC_STRING, |
---|
| 55 | len, (void*)value, NC_STRING); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /*! |
---|
| 59 | \ingroup attributes |
---|
| 60 | Write a text attribute. |
---|
| 61 | |
---|
| 62 | Add or change a text attribute. If this attribute is new, |
---|
| 63 | or if the space required to store the attribute is greater than |
---|
| 64 | before, the netCDF dataset must be in define mode. |
---|
| 65 | |
---|
| 66 | Although it's possible to create attributes of all types, text and |
---|
| 67 | double attributes are adequate for most purposes. |
---|
| 68 | |
---|
| 69 | Use the nc_put_att function to create attributes of any type, |
---|
| 70 | including user-defined types. We recommend using the type safe |
---|
| 71 | versions of this function whenever possible. |
---|
| 72 | |
---|
| 73 | \param ncid NetCDF or group ID, from a previous call to nc_open(), |
---|
| 74 | nc_create(), nc_def_grp(), or associated inquiry functions such as |
---|
| 75 | nc_inq_ncid(). |
---|
| 76 | |
---|
| 77 | \param varid Variable ID of the variable to which the attribute will |
---|
| 78 | be assigned or ::NC_GLOBAL for a global attribute. |
---|
| 79 | |
---|
| 80 | \param name Attribute \ref object_name. \ref attribute_conventions may |
---|
| 81 | apply. |
---|
| 82 | |
---|
| 83 | \param len Number of values provided for the attribute. |
---|
| 84 | |
---|
| 85 | \param value Pointer to one or more values. |
---|
| 86 | |
---|
| 87 | \returns ::NC_NOERR No error. |
---|
| 88 | \returns ::NC_EINVAL Trying to set global _FillValue. |
---|
| 89 | \returns ::NC_ENOTVAR Couldn't find varid. |
---|
| 90 | \returns ::NC_EBADTYPE Fill value and var must be same type. |
---|
| 91 | \returns ::NC_ENOMEM Out of memory |
---|
| 92 | \returns ::NC_ELATEFILL Fill values must be written while the file |
---|
| 93 | is still in initial define mode. |
---|
| 94 | |
---|
| 95 | \note With netCDF-4 files, nc_put_att will notice if you are writing a |
---|
| 96 | _Fill_Value_ attribute, and will tell the HDF5 layer to use the |
---|
| 97 | specified fill value for that variable. |
---|
| 98 | |
---|
| 99 | \section Example |
---|
| 100 | |
---|
| 101 | Here is an example using nc_put_att_double() to add a variable |
---|
| 102 | attribute named valid_range for a netCDF variable named rh and |
---|
| 103 | nc_put_att_text() to add a global attribute named title to an existing |
---|
| 104 | netCDF dataset named foo.nc: |
---|
| 105 | |
---|
| 106 | \code |
---|
| 107 | #include <netcdf.h> |
---|
| 108 | ... |
---|
| 109 | int status; |
---|
| 110 | int ncid; |
---|
| 111 | int rh_id; |
---|
| 112 | static double rh_range[] = {0.0, 100.0}; |
---|
| 113 | static char title[] = "example netCDF dataset"; |
---|
| 114 | ... |
---|
| 115 | status = nc_open("foo.nc", NC_WRITE, &ncid); |
---|
| 116 | if (status != NC_NOERR) handle_error(status); |
---|
| 117 | ... |
---|
| 118 | status = nc_redef(ncid); |
---|
| 119 | if (status != NC_NOERR) handle_error(status); |
---|
| 120 | status = nc_inq_varid (ncid, "rh", &rh_id); |
---|
| 121 | if (status != NC_NOERR) handle_error(status); |
---|
| 122 | ... |
---|
| 123 | status = nc_put_att_double (ncid, rh_id, "valid_range", |
---|
| 124 | NC_DOUBLE, 2, rh_range); |
---|
| 125 | if (status != NC_NOERR) handle_error(status); |
---|
| 126 | status = nc_put_att_text (ncid, NC_GLOBAL, "title", |
---|
| 127 | strlen(title), title) |
---|
| 128 | if (status != NC_NOERR) handle_error(status); |
---|
| 129 | ... |
---|
| 130 | status = nc_enddef(ncid); |
---|
| 131 | if (status != NC_NOERR) handle_error(status); |
---|
| 132 | \endcode |
---|
| 133 | */ |
---|
| 134 | int |
---|
| 135 | nc_put_att_text(int ncid, int varid, const char *name, |
---|
| 136 | size_t len, const char *value) |
---|
| 137 | { |
---|
| 138 | NC* ncp; |
---|
| 139 | int stat = NC_check_id(ncid, &ncp); |
---|
| 140 | if(stat != NC_NOERR) return stat; |
---|
| 141 | return ncp->dispatch->put_att(ncid, varid, name, NC_CHAR, len, |
---|
| 142 | (void *)value, NC_CHAR); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /*! \} */ |
---|
| 146 | /*! |
---|
| 147 | \ingroup attributes |
---|
| 148 | Write an attribute. |
---|
| 149 | |
---|
| 150 | The function nc_put_att_ type adds or changes a variable attribute or |
---|
| 151 | global attribute of an open netCDF dataset. If this attribute is new, |
---|
| 152 | or if the space required to store the attribute is greater than |
---|
| 153 | before, the netCDF dataset must be in define mode. |
---|
| 154 | |
---|
| 155 | With netCDF-4 files, nc_put_att will notice if you are writing a |
---|
| 156 | _Fill_Value_ attribute, and will tell the HDF5 layer to use the |
---|
| 157 | specified fill value for that variable. |
---|
| 158 | |
---|
| 159 | Although it's possible to create attributes of all types, text and |
---|
| 160 | double attributes are adequate for most purposes. |
---|
| 161 | |
---|
| 162 | \param ncid NetCDF or group ID, from a previous call to nc_open(), |
---|
| 163 | nc_create(), nc_def_grp(), or associated inquiry functions such as |
---|
| 164 | nc_inq_ncid(). |
---|
| 165 | |
---|
| 166 | \param varid Variable ID of the variable to which the attribute will |
---|
| 167 | be assigned or ::NC_GLOBAL for a global or group attribute. |
---|
| 168 | |
---|
| 169 | \param name Attribute \ref object_name. \ref attribute_conventions may |
---|
| 170 | apply. |
---|
| 171 | |
---|
| 172 | \param xtype \ref data_type of the attribute. |
---|
| 173 | |
---|
| 174 | \param len Number of values provided for the attribute. |
---|
| 175 | |
---|
| 176 | \param value Pointer to one or more values. |
---|
| 177 | |
---|
| 178 | \returns ::NC_NOERR No error. |
---|
| 179 | \returns ::NC_EINVAL Trying to set global _FillValue. |
---|
| 180 | \returns ::NC_ENOTVAR Couldn't find varid. |
---|
| 181 | \returns ::NC_EBADTYPE Fill value and var must be same type. |
---|
| 182 | \returns ::NC_ENOMEM Out of memory |
---|
| 183 | \returns ::NC_ELATEFILL Fill values must be written while the file |
---|
| 184 | is still in initial define mode. |
---|
| 185 | |
---|
| 186 | \section Example |
---|
| 187 | |
---|
| 188 | Here is an example using nc_put_att_double() to add a variable |
---|
| 189 | attribute named valid_range for a netCDF variable named rh and |
---|
| 190 | nc_put_att_text() to add a global attribute named title to an existing |
---|
| 191 | netCDF dataset named foo.nc: |
---|
| 192 | |
---|
| 193 | \code |
---|
| 194 | #include <netcdf.h> |
---|
| 195 | ... |
---|
| 196 | int status; |
---|
| 197 | int ncid; |
---|
| 198 | int rh_id; |
---|
| 199 | static double rh_range[] = {0.0, 100.0}; |
---|
| 200 | static char title[] = "example netCDF dataset"; |
---|
| 201 | ... |
---|
| 202 | status = nc_open("foo.nc", NC_WRITE, &ncid); |
---|
| 203 | if (status != NC_NOERR) handle_error(status); |
---|
| 204 | ... |
---|
| 205 | status = nc_redef(ncid); |
---|
| 206 | if (status != NC_NOERR) handle_error(status); |
---|
| 207 | status = nc_inq_varid (ncid, "rh", &rh_id); |
---|
| 208 | if (status != NC_NOERR) handle_error(status); |
---|
| 209 | ... |
---|
| 210 | status = nc_put_att_double (ncid, rh_id, "valid_range", |
---|
| 211 | NC_DOUBLE, 2, rh_range); |
---|
| 212 | if (status != NC_NOERR) handle_error(status); |
---|
| 213 | status = nc_put_att_text (ncid, NC_GLOBAL, "title", |
---|
| 214 | strlen(title), title) |
---|
| 215 | if (status != NC_NOERR) handle_error(status); |
---|
| 216 | ... |
---|
| 217 | status = nc_enddef(ncid); |
---|
| 218 | if (status != NC_NOERR) handle_error(status); |
---|
| 219 | \endcode |
---|
| 220 | */ |
---|
| 221 | /*! \{*/ |
---|
| 222 | int |
---|
| 223 | nc_put_att(int ncid, int varid, const char *name, nc_type xtype, |
---|
| 224 | size_t len, const void *value) |
---|
| 225 | { |
---|
| 226 | NC* ncp; |
---|
| 227 | int stat = NC_check_id(ncid, &ncp); |
---|
| 228 | if(stat != NC_NOERR) return stat; |
---|
| 229 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 230 | value, xtype); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | int |
---|
| 234 | nc_put_att_schar(int ncid, int varid, const char *name, |
---|
| 235 | nc_type xtype, size_t len, const signed char *value) |
---|
| 236 | { |
---|
| 237 | NC *ncp; |
---|
| 238 | int stat = NC_check_id(ncid, &ncp); |
---|
| 239 | if(stat != NC_NOERR) return stat; |
---|
| 240 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 241 | (void *)value, NC_BYTE); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | int |
---|
| 245 | nc_put_att_uchar(int ncid, int varid, const char *name, |
---|
| 246 | nc_type xtype, size_t len, const unsigned char *value) |
---|
| 247 | { |
---|
| 248 | NC* ncp; |
---|
| 249 | int stat = NC_check_id(ncid, &ncp); |
---|
| 250 | if(stat != NC_NOERR) return stat; |
---|
| 251 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 252 | (void *)value, NC_UBYTE); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | int |
---|
| 256 | nc_put_att_short(int ncid, int varid, const char *name, |
---|
| 257 | nc_type xtype, size_t len, const short *value) |
---|
| 258 | { |
---|
| 259 | NC* ncp; |
---|
| 260 | int stat = NC_check_id(ncid, &ncp); |
---|
| 261 | if(stat != NC_NOERR) return stat; |
---|
| 262 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 263 | (void *)value, NC_SHORT); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | int |
---|
| 267 | nc_put_att_int(int ncid, int varid, const char *name, |
---|
| 268 | nc_type xtype, size_t len, const int *value) |
---|
| 269 | { |
---|
| 270 | NC* ncp; |
---|
| 271 | int stat = NC_check_id(ncid, &ncp); |
---|
| 272 | if(stat != NC_NOERR) return stat; |
---|
| 273 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 274 | (void *)value, NC_INT); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | int |
---|
| 278 | nc_put_att_long(int ncid, int varid, const char *name, |
---|
| 279 | nc_type xtype, size_t len, const long *value) |
---|
| 280 | { |
---|
| 281 | NC* ncp; |
---|
| 282 | int stat = NC_check_id(ncid, &ncp); |
---|
| 283 | if(stat != NC_NOERR) return stat; |
---|
| 284 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 285 | (void *)value, longtype); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | int |
---|
| 289 | nc_put_att_float(int ncid, int varid, const char *name, |
---|
| 290 | nc_type xtype, size_t len, const float *value) |
---|
| 291 | { |
---|
| 292 | NC* ncp; |
---|
| 293 | int stat = NC_check_id(ncid, &ncp); |
---|
| 294 | if(stat != NC_NOERR) return stat; |
---|
| 295 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 296 | (void *)value, NC_FLOAT); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | int |
---|
| 300 | nc_put_att_double(int ncid, int varid, const char *name, |
---|
| 301 | nc_type xtype, size_t len, const double *value) |
---|
| 302 | { |
---|
| 303 | NC* ncp; |
---|
| 304 | int stat = NC_check_id(ncid, &ncp); |
---|
| 305 | if(stat != NC_NOERR) return stat; |
---|
| 306 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 307 | (void *)value, NC_DOUBLE); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | int |
---|
| 311 | nc_put_att_ubyte(int ncid, int varid, const char *name, |
---|
| 312 | nc_type xtype, size_t len, const unsigned char *value) |
---|
| 313 | { |
---|
| 314 | NC* ncp; |
---|
| 315 | int stat = NC_check_id(ncid, &ncp); |
---|
| 316 | if(stat != NC_NOERR) return stat; |
---|
| 317 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 318 | (void *)value, NC_UBYTE); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | int |
---|
| 322 | nc_put_att_ushort(int ncid, int varid, const char *name, |
---|
| 323 | nc_type xtype, size_t len, const unsigned short *value) |
---|
| 324 | { |
---|
| 325 | NC* ncp; |
---|
| 326 | int stat = NC_check_id(ncid, &ncp); |
---|
| 327 | if(stat != NC_NOERR) return stat; |
---|
| 328 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 329 | (void *)value, NC_USHORT); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | int |
---|
| 333 | nc_put_att_uint(int ncid, int varid, const char *name, |
---|
| 334 | nc_type xtype, size_t len, const unsigned int *value) |
---|
| 335 | { |
---|
| 336 | NC* ncp; |
---|
| 337 | int stat = NC_check_id(ncid, &ncp); |
---|
| 338 | if(stat != NC_NOERR) return stat; |
---|
| 339 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 340 | (void *)value, NC_UINT); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | int |
---|
| 344 | nc_put_att_longlong(int ncid, int varid, const char *name, |
---|
| 345 | nc_type xtype, size_t len, |
---|
| 346 | const long long *value) |
---|
| 347 | { |
---|
| 348 | NC* ncp; |
---|
| 349 | int stat = NC_check_id(ncid, &ncp); |
---|
| 350 | if(stat != NC_NOERR) return stat; |
---|
| 351 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 352 | (void *)value, NC_INT64); |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | int |
---|
| 356 | nc_put_att_ulonglong(int ncid, int varid, const char *name, |
---|
| 357 | nc_type xtype, size_t len, |
---|
| 358 | const unsigned long long *value) |
---|
| 359 | { |
---|
| 360 | NC* ncp; |
---|
| 361 | int stat = NC_check_id(ncid, &ncp); |
---|
| 362 | if(stat != NC_NOERR) return stat; |
---|
| 363 | return ncp->dispatch->put_att(ncid, varid, name, xtype, len, |
---|
| 364 | (void *)value, NC_UINT64); |
---|
| 365 | } |
---|
| 366 | |
---|