dvar.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*! \file
  2. Functions for defining and inquiring about variables.
  3. Copyright 2010 University Corporation for Atmospheric
  4. Research/Unidata. See COPYRIGHT file for more info.
  5. */
  6. #include "ncdispatch.h"
  7. #include "netcdf_f.h"
  8. /** \defgroup variables Variables
  9. Variables hold multi-dimensional arrays of data.
  10. Variables for a netCDF dataset are defined when the dataset is
  11. created, while the netCDF dataset is in define mode. Other variables
  12. may be added later by reentering define mode. A netCDF variable has a
  13. name, a type, and a shape, which are specified when it is defined. A
  14. variable may also have values, which are established later in data
  15. mode.
  16. Ordinarily, the name, type, and shape are fixed when the variable is
  17. first defined. The name may be changed, but the type and shape of a
  18. variable cannot be changed. However, a variable defined in terms of
  19. the unlimited dimension can grow without bound in that dimension.
  20. A netCDF variable in an open netCDF dataset is referred to by a small
  21. integer called a variable ID.
  22. Variable IDs reflect the order in which variables were defined within
  23. a netCDF dataset. Variable IDs are 0, 1, 2,..., in the order in which
  24. the variables were defined. A function is available for getting the
  25. variable ID from the variable name and vice-versa.
  26. Attributes (see Attributes) may be associated with a variable to
  27. specify such properties as units.
  28. Operations supported on variables are:
  29. - Create a variable, given its name, data type, and shape.
  30. - Get a variable ID from its name.
  31. - Get a variable's name, data type, shape, and number of attributes
  32. from its ID.
  33. - Put a data value into a variable, given variable ID, indices, and value.
  34. - Put an array of values into a variable, given variable ID, corner
  35. indices, edge lengths, and a block of values.
  36. - Put a subsampled or mapped array-section of values into a variable,
  37. given variable ID, corner indices, edge lengths, stride vector,
  38. index mapping vector, and a block of values.
  39. - Get a data value from a variable, given variable ID and indices.
  40. - Get an array of values from a variable, given variable ID, corner
  41. indices, and edge lengths.
  42. - Get a subsampled or mapped array-section of values from a variable,
  43. given variable ID, corner indices, edge lengths, stride vector, and
  44. index mapping vector.
  45. - Rename a variable.
  46. \section language_types Language Types Corresponding to netCDF
  47. External Data Types
  48. NetCDF supported six atomic data types through version 3.6.0 (char,
  49. byte, short, int, float, and double). Starting with version 4.0, many
  50. new atomic and user defined data types are supported (unsigned int
  51. types, strings, compound types, variable length arrays, enums,
  52. opaque).
  53. The additional data types are only supported in netCDF-4/HDF5
  54. files. To create netCDF-4/HDF5 files, use the HDF5 flag in
  55. nc_create. (see nc_create).
  56. \section classic_types NetCDF-3 Classic and 64-Bit Offset Data Types
  57. NetCDF-3 classic and 64-bit offset files support 6 atomic data types,
  58. and none of the user defined datatype introduced in NetCDF-4.
  59. The following table gives the netCDF-3 external data types and the
  60. corresponding type constants for defining variables in the C
  61. interface:
  62. <table>
  63. <tr><td>Type</td><td>C define</td><td>Bits</td></tr>
  64. <tr><td>byte</td><td>NC_BYTE</td><td>8</td></tr>
  65. <tr><td>char</td><td>NC_CHAR</td><td>8</td></tr>
  66. <tr><td>short</td><td>NC_SHORT</td><td>16</td></tr>
  67. <tr><td>int</td><td>NC_INT</td><td>32</td></tr>
  68. <tr><td>float</td><td>NC_FLOAT</td><td>32</td></tr>
  69. <tr><td>double</td><td>NC_DOUBLE</td><td>64</td></tr>
  70. </table>
  71. The first column gives the netCDF external data type, which is the
  72. same as the CDL data type. The next column gives the corresponding C
  73. pre-processor macro for use in netCDF functions (the pre-processor
  74. macros are defined in the netCDF C header-file netcdf.h). The last
  75. column gives the number of bits used in the external representation of
  76. values of the corresponding type.
  77. \section netcdf_4_atomic NetCDF-4 Atomic Types
  78. NetCDF-4 files support all of the atomic data types from netCDF-3,
  79. plus additional unsigned integer types, 64-bit integer types, and a
  80. string type.
  81. <table>
  82. <tr><td>Type</td><td>C define</td><td>Bits
  83. <tr><td>byte</td><td>NC_BYTE</td><td>8</td></tr>
  84. <tr><td>unsigned byte </td><td>NC_UBYTE^</td><td> 8</td></tr>
  85. <tr><td>char </td><td>NC_CHAR </td><td>8</td></tr>
  86. <tr><td>short </td><td>NC_SHORT </td><td>16</td></tr>
  87. <tr><td>unsigned short </td><td>NC_USHORT^ </td><td>16</td></tr>
  88. <tr><td>int </td><td>NC_INT </td><td>32</td></tr>
  89. <tr><td>unsigned int </td><td>NC_UINT^ </td><td>32</td></tr>
  90. <tr><td>unsigned long long </td><td>NC_UINT64^ </td><td>64</td></tr>
  91. <tr><td>long long </td><td>NC_INT64^ </td><td>64</td></tr>
  92. <tr><td>float </td><td>NC_FLOAT </td><td>32</td></tr>
  93. <tr><td>double </td><td>NC_DOUBLE </td><td>64</td></tr>
  94. <tr><td>char ** </td><td>NC_STRING^ </td><td>string length + 1</td></tr>
  95. </table>
  96. ^This type was introduced in netCDF-4, and is not supported in netCDF
  97. classic or 64-bit offset format files, or in netCDF-4 files if they
  98. are created with the NC_CLASSIC_MODEL flags.
  99. */
  100. /** \name Defining Variables
  101. Use these functions to define variables.
  102. */
  103. /*! \{ */
  104. /**
  105. \ingroup variables
  106. Define a new variable.
  107. This function adds a new variable to an open netCDF dataset or group.
  108. It returns (as an argument) a variable ID, given the netCDF ID,
  109. the variable name, the variable type, the number of dimensions, and a
  110. list of the dimension IDs.
  111. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  112. nc_create(), nc_def_grp(), or associated inquiry functions such as
  113. nc_inq_ncid().
  114. \param name Variable \ref object_name.
  115. \param xtype \ref data_type of the variable.
  116. \param ndims Number of dimensions for the variable. For example, 2
  117. specifies a matrix, 1 specifies a vector, and 0 means the variable is
  118. a scalar with no dimensions. Must not be negative or greater than the
  119. predefined constant ::NC_MAX_VAR_DIMS.
  120. \param dimidsp Vector of ndims dimension IDs corresponding to the
  121. variable dimensions. For classic model netCDF files, if the ID of the
  122. unlimited dimension is included, it must be first. This argument is
  123. ignored if ndims is 0. For expanded model netCDF4/HDF5 files, there
  124. may be any number of unlimited dimensions, and they may be used in any
  125. element of the dimids array.
  126. \param varidp Pointer to location for the returned variable ID.
  127. \returns ::NC_NOERR No error.
  128. \returns ::NC_EBADID Bad ncid.
  129. \returns ::NC_ENOTINDEFINE Not in define mode.
  130. \returns ::NC_ESTRICTNC3 Attempting netcdf-4 operation on strict nc3 netcdf-4 file.
  131. \returns ::NC_EMAXVARS NC_MAX_VARS exceeded
  132. \returns ::NC_EBADTYPE Bad type.
  133. \returns ::NC_EINVAL Invalid input.
  134. \returns ::NC_ENAMEINUSE Name already in use.
  135. \returns ::NC_EPERM Attempt to create object in read-only file.
  136. \section Example
  137. Here is an example using nc_def_var to create a variable named rh of
  138. type double with three dimensions, time, lat, and lon in a new netCDF
  139. dataset named foo.nc:
  140. \code
  141. #include <netcdf.h>
  142. ...
  143. int status;
  144. int ncid;
  145. int lat_dim, lon_dim, time_dim;
  146. int rh_id;
  147. int rh_dimids[3];
  148. ...
  149. status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
  150. if (status != NC_NOERR) handle_error(status);
  151. ...
  152. status = nc_def_dim(ncid, "lat", 5L, &lat_dim);
  153. if (status != NC_NOERR) handle_error(status);
  154. status = nc_def_dim(ncid, "lon", 10L, &lon_dim);
  155. if (status != NC_NOERR) handle_error(status);
  156. status = nc_def_dim(ncid, "time", NC_UNLIMITED, &time_dim);
  157. if (status != NC_NOERR) handle_error(status);
  158. ...
  159. rh_dimids[0] = time_dim;
  160. rh_dimids[1] = lat_dim;
  161. rh_dimids[2] = lon_dim;
  162. status = nc_def_var (ncid, "rh", NC_DOUBLE, 3, rh_dimids, &rh_id);
  163. if (status != NC_NOERR) handle_error(status);
  164. \endcode
  165. */
  166. int
  167. nc_def_var(int ncid, const char *name, nc_type xtype,
  168. int ndims, const int *dimidsp, int *varidp)
  169. {
  170. NC* ncp;
  171. int stat = NC_NOERR;
  172. if ((stat = NC_check_id(ncid, &ncp)))
  173. return stat;
  174. return ncp->dispatch->def_var(ncid, name, xtype, ndims,
  175. dimidsp, varidp);
  176. }
  177. /*! \} */
  178. /** \name Rename a Variable
  179. Rename a variable.
  180. */
  181. /*! \{ */
  182. /** Rename a variable.
  183. \ingroup variables
  184. This function changes the name of a netCDF variable in an open netCDF
  185. file or group. You cannot rename a variable to have the name of any existing
  186. variable.
  187. For classic format, 64-bit offset format, and netCDF-4/HDF5 with
  188. classic mode, if the new name is longer than the old name, the netCDF
  189. dataset must be in define mode.
  190. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  191. nc_create(), nc_def_grp(), or associated inquiry functions such as
  192. nc_inq_ncid().
  193. \param varid Variable ID
  194. \param name New name of the variable.
  195. \returns ::NC_NOERR No error.
  196. \returns ::NC_EBADID Bad ncid.
  197. \returns ::NC_ENOTVAR Invalid variable ID.
  198. \returns ::NC_EBADNAME Bad name.
  199. \returns ::NC_EMAXNAME Name is too long.
  200. \returns ::NC_ENAMEINUSE Name in use.
  201. \returns ::NC_ENOMEM Out of memory.
  202. \section Example
  203. Here is an example using nc_rename_var to rename the variable rh to
  204. rel_hum in an existing netCDF dataset named foo.nc:
  205. \code
  206. #include <netcdf.h>
  207. ...
  208. int status;
  209. int ncid;
  210. int rh_id;
  211. ...
  212. status = nc_open("foo.nc", NC_WRITE, &ncid);
  213. if (status != NC_NOERR) handle_error(status);
  214. ...
  215. status = nc_redef(ncid);
  216. if (status != NC_NOERR) handle_error(status);
  217. status = nc_inq_varid (ncid, "rh", &rh_id);
  218. if (status != NC_NOERR) handle_error(status);
  219. status = nc_rename_var (ncid, rh_id, "rel_hum");
  220. if (status != NC_NOERR) handle_error(status);
  221. status = nc_enddef(ncid);
  222. if (status != NC_NOERR) handle_error(status);
  223. \endcode
  224. */
  225. int
  226. nc_rename_var(int ncid, int varid, const char *name)
  227. {
  228. NC* ncp;
  229. int stat = NC_check_id(ncid, &ncp);
  230. if(stat != NC_NOERR) return stat;
  231. return ncp->dispatch->rename_var(ncid, varid, name);
  232. }
  233. /*! \} */
  234. /** \internal
  235. \ingroup variables
  236. */
  237. int
  238. NC_is_recvar(int ncid, int varid, size_t* nrecs)
  239. {
  240. int status = NC_NOERR;
  241. int unlimid;
  242. int ndims;
  243. int dimset[NC_MAX_VAR_DIMS];
  244. status = nc_inq_unlimdim(ncid,&unlimid);
  245. if(status != NC_NOERR) return 0; /* no unlimited defined */
  246. status = nc_inq_varndims(ncid,varid,&ndims);
  247. if(status != NC_NOERR) return 0; /* no unlimited defined */
  248. if(ndims == 0) return 0; /* scalar */
  249. status = nc_inq_vardimid(ncid,varid,dimset);
  250. if(status != NC_NOERR) return 0; /* no unlimited defined */
  251. status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
  252. if(status != NC_NOERR) return 0;
  253. return (dimset[0] == unlimid ? 1: 0);
  254. }
  255. /* Ok to use NC pointers because
  256. all IOSP's will use that structure,
  257. but not ok to use e.g. NC_Var pointers
  258. because they may be different structure
  259. entirely.
  260. */
  261. /** \internal
  262. \ingroup variables
  263. Find the length of a type. This is how much space is required by the user, as in
  264. \code
  265. vals = malloc(nel * nctypelen(var.type));
  266. ncvarget(cdfid, varid, cor, edg, vals);
  267. \endcode
  268. */
  269. int
  270. nctypelen(nc_type type)
  271. {
  272. switch(type){
  273. case NC_CHAR :
  274. return((int)sizeof(char));
  275. case NC_BYTE :
  276. return((int)sizeof(signed char));
  277. case NC_SHORT :
  278. return(int)(sizeof(short));
  279. case NC_INT :
  280. return((int)sizeof(int));
  281. case NC_FLOAT :
  282. return((int)sizeof(float));
  283. case NC_DOUBLE :
  284. return((int)sizeof(double));
  285. /* These can occur in netcdf-3 code */
  286. case NC_UBYTE :
  287. return((int)sizeof(unsigned char));
  288. case NC_USHORT :
  289. return((int)(sizeof(unsigned short)));
  290. case NC_UINT :
  291. return((int)sizeof(unsigned int));
  292. case NC_INT64 :
  293. return((int)sizeof(signed long long));
  294. case NC_UINT64 :
  295. return((int)sizeof(unsigned long long));
  296. #ifdef USE_NETCDF4
  297. case NC_STRING :
  298. return((int)sizeof(char*));
  299. #endif /*USE_NETCDF4*/
  300. default:
  301. return -1;
  302. }
  303. }
  304. /** \internal
  305. \ingroup variables
  306. Find the length of a type. Redunant over nctypelen() above. */
  307. int
  308. NC_atomictypelen(nc_type xtype)
  309. {
  310. int sz = 0;
  311. switch(xtype) {
  312. case NC_NAT: sz = 0; break;
  313. case NC_BYTE: sz = sizeof(signed char); break;
  314. case NC_CHAR: sz = sizeof(char); break;
  315. case NC_SHORT: sz = sizeof(short); break;
  316. case NC_INT: sz = sizeof(int); break;
  317. case NC_FLOAT: sz = sizeof(float); break;
  318. case NC_DOUBLE: sz = sizeof(double); break;
  319. case NC_INT64: sz = sizeof(signed long long); break;
  320. case NC_UBYTE: sz = sizeof(unsigned char); break;
  321. case NC_USHORT: sz = sizeof(unsigned short); break;
  322. case NC_UINT: sz = sizeof(unsigned int); break;
  323. case NC_UINT64: sz = sizeof(unsigned long long); break;
  324. #ifdef USE_NETCDF4
  325. case NC_STRING: sz = sizeof(char*); break;
  326. #endif
  327. default: break;
  328. }
  329. return sz;
  330. }
  331. /** \internal
  332. \ingroup variables
  333. Get the type name. */
  334. char *
  335. NC_atomictypename(nc_type xtype)
  336. {
  337. char* nm = NULL;
  338. switch(xtype) {
  339. case NC_NAT: nm = "undefined"; break;
  340. case NC_BYTE: nm = "byte"; break;
  341. case NC_CHAR: nm = "char"; break;
  342. case NC_SHORT: nm = "short"; break;
  343. case NC_INT: nm = "int"; break;
  344. case NC_FLOAT: nm = "float"; break;
  345. case NC_DOUBLE: nm = "double"; break;
  346. case NC_INT64: nm = "int64"; break;
  347. case NC_UBYTE: nm = "ubyte"; break;
  348. case NC_USHORT: nm = "ushort"; break;
  349. case NC_UINT: nm = "uint"; break;
  350. case NC_UINT64: nm = "uint64"; break;
  351. #ifdef USE_NETCDF4
  352. case NC_STRING: nm = "string"; break;
  353. #endif
  354. default: break;
  355. }
  356. return nm;
  357. }
  358. /** \internal
  359. \ingroup variables
  360. Get the shape of a variable.
  361. */
  362. int
  363. NC_getshape(int ncid, int varid, int ndims, size_t* shape)
  364. {
  365. int dimids[NC_MAX_VAR_DIMS];
  366. int i;
  367. int status = NC_NOERR;
  368. if ((status = nc_inq_vardimid(ncid, varid, dimids)))
  369. return status;
  370. for(i = 0; i < ndims; i++)
  371. if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
  372. break;
  373. return status;
  374. }
  375. #ifdef USE_NETCDF4
  376. /** \ingroup variables
  377. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  378. nc_create(), nc_def_grp(), or associated inquiry functions such as
  379. nc_inq_ncid().
  380. \param varid Variable ID
  381. \param size The total size of the raw data chunk cache, in bytes.
  382. \param nelems The number of chunk slots in the raw data chunk cache.
  383. \param preemption The preemption, a value between 0 and 1 inclusive
  384. that indicates how much chunks that have been fully read are favored
  385. for preemption. A value of zero means fully read chunks are treated no
  386. differently than other chunks (the preemption is strictly LRU) while a
  387. value of one means fully read chunks are always preempted before other
  388. chunks.
  389. \returns ::NC_NOERR No error.
  390. \returns ::NC_EBADID Bad ncid.
  391. \returns ::NC_ENOTVAR Invalid variable ID.
  392. \returns ::NC_ESTRICTNC3 Attempting netcdf-4 operation on strict nc3 netcdf-4 file.
  393. \returns ::NC_EINVAL Invalid input
  394. */
  395. int
  396. nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
  397. float preemption)
  398. {
  399. NC* ncp;
  400. int stat = NC_check_id(ncid, &ncp);
  401. if(stat != NC_NOERR) return stat;
  402. return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
  403. nelems, preemption);
  404. }
  405. /** \ingroup variables
  406. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  407. nc_create(), nc_def_grp(), or associated inquiry functions such as
  408. nc_inq_ncid().
  409. \param varid Variable ID
  410. \param sizep The total size of the raw data chunk cache, in bytes,
  411. will be put here. \ref ignored_if_null.
  412. \param nelemsp The number of chunk slots in the raw data chunk cache
  413. hash table will be put here. \ref ignored_if_null.
  414. \param preemptionp The preemption will be put here. The preemtion
  415. value is between 0 and 1 inclusive and indicates how much chunks that
  416. have been fully read are favored for preemption. A value of zero means
  417. fully read chunks are treated no differently than other chunks (the
  418. preemption is strictly LRU) while a value of one means fully read
  419. chunks are always preempted before other chunks. \ref ignored_if_null.
  420. \returns ::NC_NOERR No error.
  421. \returns ::NC_EBADID Bad ncid.
  422. \returns ::NC_ENOTVAR Invalid variable ID.
  423. \returns ::NC_ESTRICTNC3 Attempting netcdf-4 operation on strict nc3 netcdf-4 file.
  424. \returns ::NC_EINVAL Invalid input
  425. */
  426. int
  427. nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
  428. float *preemptionp)
  429. {
  430. NC* ncp;
  431. int stat = NC_check_id(ncid, &ncp);
  432. if(stat != NC_NOERR) return stat;
  433. return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
  434. nelemsp, preemptionp);
  435. }
  436. /** \ingroup variables
  437. Free string space allocated by the library.
  438. When you read string type the library will allocate the storage space
  439. for the data. This storage space must be freed, so pass the pointer
  440. back to this function, when you're done with the data, and it will
  441. free the string memory.
  442. \param len The number of character arrays in the array.
  443. \param data The pointer to the data array.
  444. \returns ::NC_NOERR No error.
  445. */
  446. int
  447. nc_free_string(size_t len, char **data)
  448. {
  449. int i;
  450. for (i = 0; i < len; i++)
  451. free(data[i]);
  452. return NC_NOERR;
  453. }
  454. int
  455. nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
  456. {
  457. NC* ncp;
  458. int stat = NC_check_id(ncid,&ncp);
  459. if(stat != NC_NOERR) return stat;
  460. return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
  461. }
  462. int
  463. nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
  464. {
  465. NC* ncp;
  466. int stat = NC_check_id(ncid,&ncp);
  467. if(stat != NC_NOERR) return stat;
  468. return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
  469. }
  470. int
  471. nc_def_var_chunking(int ncid, int varid, int storage,
  472. const size_t *chunksizesp)
  473. {
  474. NC* ncp;
  475. int stat = NC_check_id(ncid, &ncp);
  476. if(stat != NC_NOERR) return stat;
  477. return ncp->dispatch->def_var_chunking(ncid, varid, storage,
  478. chunksizesp);
  479. }
  480. int
  481. nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
  482. {
  483. NC* ncp;
  484. int stat = NC_check_id(ncid,&ncp);
  485. if(stat != NC_NOERR) return stat;
  486. return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
  487. }
  488. int
  489. nc_def_var_endian(int ncid, int varid, int endian)
  490. {
  491. NC* ncp;
  492. int stat = NC_check_id(ncid,&ncp);
  493. if(stat != NC_NOERR) return stat;
  494. return ncp->dispatch->def_var_endian(ncid,varid,endian);
  495. }
  496. #endif /* USE_NETCDF4 */