[409] | 1 | /** \file |
---|
| 2 | Attribute functions |
---|
| 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 Getting Attributes |
---|
| 12 | |
---|
| 13 | Functions to get the values of attributes. |
---|
| 14 | */ |
---|
| 15 | /*! \{ */ |
---|
| 16 | |
---|
| 17 | /*! |
---|
| 18 | \ingroup attributes |
---|
| 19 | Get an attribute of any type. |
---|
| 20 | |
---|
| 21 | The nc_get_att() functions works for any type of attribute, and must |
---|
| 22 | be used to get attributes of user-defined type. We recommend that they |
---|
| 23 | type safe versions of this function be used where possible. |
---|
| 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 attribute's variable, or ::NC_GLOBAL |
---|
| 30 | for a global attribute. |
---|
| 31 | |
---|
| 32 | \param name Attribute \ref object_name. |
---|
| 33 | |
---|
| 34 | \param value Pointer to location for returned attribute value(s). All |
---|
| 35 | elements of the vector of attribute values are returned, so you must |
---|
| 36 | allocate enough space to hold them. Before using the value as a C |
---|
| 37 | string, make sure it is null-terminated. Call nc_inq_attlen() first to |
---|
| 38 | find out the length of the attribute. |
---|
| 39 | */ |
---|
| 40 | int |
---|
| 41 | nc_get_att(int ncid, int varid, const char *name, void *value) |
---|
| 42 | { |
---|
| 43 | NC* ncp; |
---|
| 44 | int stat = NC_NOERR; |
---|
| 45 | nc_type xtype; |
---|
| 46 | |
---|
| 47 | if ((stat = NC_check_id(ncid, &ncp))) |
---|
| 48 | return stat; |
---|
| 49 | |
---|
| 50 | /* Need to get the type */ |
---|
| 51 | if ((stat = nc_inq_atttype(ncid, varid, name, &xtype))) |
---|
| 52 | return stat; |
---|
| 53 | |
---|
| 54 | return ncp->dispatch->get_att(ncid, varid, name, value, xtype); |
---|
| 55 | } |
---|
| 56 | /*! \} */ |
---|
| 57 | |
---|
| 58 | /*! |
---|
| 59 | \ingroup attributes |
---|
| 60 | Get an attribute. |
---|
| 61 | |
---|
| 62 | This function gets an attribute from the netCDF file. The nc_get_att() |
---|
| 63 | function works with any type of data, including user defined types. |
---|
| 64 | |
---|
| 65 | \note The netCDF library reads all attributes into memory when the |
---|
| 66 | file is opened with nc_open(). Getting an attribute copies the value |
---|
| 67 | from the in-memory store, and does not incure any file I/O penalties. |
---|
| 68 | |
---|
| 69 | \param ncid NetCDF or group ID, from a previous call to nc_open(), |
---|
| 70 | nc_create(), nc_def_grp(), or associated inquiry functions such as |
---|
| 71 | nc_inq_ncid(). |
---|
| 72 | |
---|
| 73 | \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL |
---|
| 74 | for a global attribute. |
---|
| 75 | |
---|
| 76 | \param name Attribute \ref object_name. |
---|
| 77 | |
---|
| 78 | \param value Pointer to location for returned attribute value(s). All |
---|
| 79 | elements of the vector of attribute values are returned, so you must |
---|
| 80 | allocate enough space to hold them. If you don't know how much |
---|
| 81 | space to reserve, call nc_inq_attlen() first to find out the length of |
---|
| 82 | the attribute. |
---|
| 83 | |
---|
| 84 | <h1>Example</h1> |
---|
| 85 | |
---|
| 86 | Here is an example using nc_get_att_double() to determine the values |
---|
| 87 | of a variable attribute named valid_range for a netCDF variable named |
---|
| 88 | rh and using nc_get_att_text() to read a global attribute named title |
---|
| 89 | in an existing netCDF dataset named foo.nc. |
---|
| 90 | |
---|
| 91 | In this example, it is assumed that we don't know how many values will |
---|
| 92 | be returned, but that we do know the types of the attributes. Hence, |
---|
| 93 | to allocate enough space to store them, we must first inquire about |
---|
| 94 | the length of the attributes. |
---|
| 95 | |
---|
| 96 | \code |
---|
| 97 | #include <netcdf.h> |
---|
| 98 | ... |
---|
| 99 | int status; |
---|
| 100 | int ncid; |
---|
| 101 | int rh_id; |
---|
| 102 | int vr_len, t_len; |
---|
| 103 | double *vr_val; |
---|
| 104 | char *title; |
---|
| 105 | extern char *malloc() |
---|
| 106 | |
---|
| 107 | ... |
---|
| 108 | status = nc_open("foo.nc", NC_NOWRITE, &ncid); |
---|
| 109 | if (status != NC_NOERR) handle_error(status); |
---|
| 110 | ... |
---|
| 111 | status = nc_inq_varid (ncid, "rh", &rh_id); |
---|
| 112 | if (status != NC_NOERR) handle_error(status); |
---|
| 113 | ... |
---|
| 114 | status = nc_inq_attlen (ncid, rh_id, "valid_range", &vr_len); |
---|
| 115 | if (status != NC_NOERR) handle_error(status); |
---|
| 116 | status = nc_inq_attlen (ncid, NC_GLOBAL, "title", &t_len); |
---|
| 117 | if (status != NC_NOERR) handle_error(status); |
---|
| 118 | |
---|
| 119 | vr_val = (double *) malloc(vr_len * sizeof(double)); |
---|
| 120 | title = (char *) malloc(t_len + 1); |
---|
| 121 | |
---|
| 122 | status = nc_get_att_double(ncid, rh_id, "valid_range", vr_val); |
---|
| 123 | if (status != NC_NOERR) handle_error(status); |
---|
| 124 | status = nc_get_att_text(ncid, NC_GLOBAL, "title", title); |
---|
| 125 | if (status != NC_NOERR) handle_error(status); |
---|
| 126 | title[t_len] = '\0'; |
---|
| 127 | ... |
---|
| 128 | \endcode |
---|
| 129 | */ |
---|
| 130 | /*! \{ */ |
---|
| 131 | int |
---|
| 132 | nc_get_att_text(int ncid, int varid, const char *name, char *value) |
---|
| 133 | { |
---|
| 134 | NC* ncp; |
---|
| 135 | int stat = NC_check_id(ncid, &ncp); |
---|
| 136 | if(stat != NC_NOERR) return stat; |
---|
| 137 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_CHAR); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | int |
---|
| 141 | nc_get_att_schar(int ncid, int varid, const char *name, signed char *value) |
---|
| 142 | { |
---|
| 143 | NC* ncp; |
---|
| 144 | int stat = NC_check_id(ncid, &ncp); |
---|
| 145 | if(stat != NC_NOERR) return stat; |
---|
| 146 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_BYTE); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | int |
---|
| 150 | nc_get_att_uchar(int ncid, int varid, const char *name, unsigned char *value) |
---|
| 151 | { |
---|
| 152 | NC* ncp; |
---|
| 153 | int stat = NC_check_id(ncid, &ncp); |
---|
| 154 | if(stat != NC_NOERR) return stat; |
---|
| 155 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | int |
---|
| 159 | nc_get_att_short(int ncid, int varid, const char *name, short *value) |
---|
| 160 | { |
---|
| 161 | NC* ncp; |
---|
| 162 | int stat = NC_check_id(ncid, &ncp); |
---|
| 163 | if(stat != NC_NOERR) return stat; |
---|
| 164 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_SHORT); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | int |
---|
| 168 | nc_get_att_int(int ncid, int varid, const char *name, int *value) |
---|
| 169 | { |
---|
| 170 | NC* ncp; |
---|
| 171 | int stat = NC_check_id(ncid, &ncp); |
---|
| 172 | if(stat != NC_NOERR) return stat; |
---|
| 173 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | int |
---|
| 177 | nc_get_att_long(int ncid, int varid, const char *name, long *value) |
---|
| 178 | { |
---|
| 179 | NC* ncp; |
---|
| 180 | int stat = NC_check_id(ncid, &ncp); |
---|
| 181 | if(stat != NC_NOERR) return stat; |
---|
| 182 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, longtype); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | int |
---|
| 186 | nc_get_att_float(int ncid, int varid, const char *name, float *value) |
---|
| 187 | { |
---|
| 188 | NC* ncp; |
---|
| 189 | int stat = NC_check_id(ncid, &ncp); |
---|
| 190 | if(stat != NC_NOERR) return stat; |
---|
| 191 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_FLOAT); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | int |
---|
| 195 | nc_get_att_double(int ncid, int varid, const char *name, double *value) |
---|
| 196 | { |
---|
| 197 | NC* ncp; |
---|
| 198 | int stat = NC_check_id(ncid, &ncp); |
---|
| 199 | if(stat != NC_NOERR) return stat; |
---|
| 200 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_DOUBLE); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | int |
---|
| 204 | nc_get_att_ubyte(int ncid, int varid, const char *name, unsigned char *value) |
---|
| 205 | { |
---|
| 206 | NC* ncp; |
---|
| 207 | int stat = NC_check_id(ncid, &ncp); |
---|
| 208 | if(stat != NC_NOERR) return stat; |
---|
| 209 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | int |
---|
| 213 | nc_get_att_ushort(int ncid, int varid, const char *name, unsigned short *value) |
---|
| 214 | { |
---|
| 215 | NC* ncp; |
---|
| 216 | int stat = NC_check_id(ncid, &ncp); |
---|
| 217 | if(stat != NC_NOERR) return stat; |
---|
| 218 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_USHORT); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | int |
---|
| 222 | nc_get_att_uint(int ncid, int varid, const char *name, unsigned int *value) |
---|
| 223 | { |
---|
| 224 | NC* ncp; |
---|
| 225 | int stat = NC_check_id(ncid, &ncp); |
---|
| 226 | if(stat != NC_NOERR) return stat; |
---|
| 227 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | int |
---|
| 231 | nc_get_att_longlong(int ncid, int varid, const char *name, long long *value) |
---|
| 232 | { |
---|
| 233 | NC* ncp; |
---|
| 234 | int stat = NC_check_id(ncid, &ncp); |
---|
| 235 | if(stat != NC_NOERR) return stat; |
---|
| 236 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT64); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | int |
---|
| 240 | nc_get_att_ulonglong(int ncid, int varid, const char *name, unsigned long long *value) |
---|
| 241 | { |
---|
| 242 | NC *ncp; |
---|
| 243 | int stat = NC_check_id(ncid, &ncp); |
---|
| 244 | if(stat != NC_NOERR) return stat; |
---|
| 245 | return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT64); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | int |
---|
| 249 | nc_get_att_string(int ncid, int varid, const char *name, char **value) |
---|
| 250 | { |
---|
| 251 | NC *ncp; |
---|
| 252 | int stat = NC_check_id(ncid, &ncp); |
---|
| 253 | if(stat != NC_NOERR) return stat; |
---|
| 254 | return ncp->dispatch->get_att(ncid,varid,name,(void*)value, NC_STRING); |
---|
| 255 | } |
---|
| 256 | /*! \} */ |
---|