dfile.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /** \file
  2. File create and open functions
  3. These functions end up calling functions in one of the dispatch layers
  4. (netCDF-4, dap server, etc).
  5. Copyright 2010 University Corporation for Atmospheric
  6. Research/Unidata. See COPYRIGHT file for more info.
  7. */
  8. #include "config.h"
  9. #include <stdlib.h>
  10. #ifdef HAVE_SYS_RESOURCE_H
  11. #include <sys/resource.h>
  12. #endif
  13. #ifdef HAVE_SYS_TYPES_H
  14. #include <sys/types.h>
  15. #endif
  16. #ifdef HAVE_SYS_STAT_H
  17. #include <sys/stat.h>
  18. #endif
  19. #ifdef HAVE_FCNTL_H
  20. #include <fcntl.h>
  21. #endif
  22. #include "ncdispatch.h"
  23. static int nc_initialized = 0;
  24. /** \defgroup datasets NetCDF Files
  25. NetCDF opens datasets as files or remote access URLs.
  26. A netCDF dataset that has not yet been opened can only be referred to
  27. by its dataset name. Once a netCDF dataset is opened, it is referred
  28. to by a netCDF ID, which is a small non-negative integer returned when
  29. you create or open the dataset. A netCDF ID is much like a file
  30. descriptor in C or a logical unit number in FORTRAN. In any single
  31. program, the netCDF IDs of distinct open netCDF datasets are
  32. distinct. A single netCDF dataset may be opened multiple times and
  33. will then have multiple distinct netCDF IDs; however at most one of
  34. the open instances of a single netCDF dataset should permit
  35. writing. When an open netCDF dataset is closed, the ID is no longer
  36. associated with a netCDF dataset.
  37. Functions that deal with the netCDF library include:
  38. - Get version of library.
  39. - Get error message corresponding to a returned error code.
  40. The operations supported on a netCDF dataset as a single object are:
  41. - Create, given dataset name and whether to overwrite or not.
  42. - Open for access, given dataset name and read or write intent.
  43. - Put into define mode, to add dimensions, variables, or attributes.
  44. - Take out of define mode, checking consistency of additions.
  45. - Close, writing to disk if required.
  46. - Inquire about the number of dimensions, number of variables,
  47. number of global attributes, and ID of the unlimited dimension, if
  48. any.
  49. - Synchronize to disk to make sure it is current.
  50. - Set and unset nofill mode for optimized sequential writes.
  51. - After a summary of conventions used in describing the netCDF
  52. interfaces, the rest of this chapter presents a detailed description
  53. of the interfaces for these operations.
  54. */
  55. /**@{*/
  56. size_t* NC_coord_zero;
  57. size_t* NC_coord_one;
  58. static void
  59. nc_local_initialize(void)
  60. {
  61. int i;
  62. NC_coord_zero = (size_t*)malloc(sizeof(size_t)*NC_MAX_VAR_DIMS);
  63. if(NC_coord_zero == NULL) abort();
  64. NC_coord_one = (size_t*)malloc(sizeof(size_t)*NC_MAX_VAR_DIMS);
  65. if(NC_coord_one == NULL) abort();
  66. for(i=0;i<NC_MAX_VAR_DIMS;i++) {
  67. NC_coord_one[i] = 1;
  68. NC_coord_zero[i] = 0;
  69. }
  70. }
  71. static int
  72. NC_check_file_type(const char *path, int use_parallel, void *mpi_info,
  73. int *cdf, int *hdf)
  74. {
  75. char magic[MAGIC_NUMBER_LEN];
  76. *hdf = 0; *cdf = 0;
  77. /* Get the 4-byte magic from the beginning of the file. Don't use posix
  78. * for parallel, use the MPI functions instead. */
  79. #ifdef USE_PARALLEL_MPIO
  80. if (use_parallel)
  81. {
  82. MPI_File fh;
  83. MPI_Status status;
  84. int retval;
  85. MPI_Comm comm = 0;
  86. MPI_Info info = 0;
  87. if(mpi_info != NULL) {
  88. comm = ((NC_MPI_INFO*)mpi_info)->comm;
  89. info = ((NC_MPI_INFO*)mpi_info)->info;
  90. }
  91. if((retval = MPI_File_open(comm, (char *)path, MPI_MODE_RDONLY,info,
  92. &fh)) != MPI_SUCCESS)
  93. return NC_EPARINIT;
  94. if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
  95. &status)) != MPI_SUCCESS)
  96. return NC_EPARINIT;
  97. if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
  98. return NC_EPARINIT;
  99. } else
  100. #endif /* USE_PARALLEL */
  101. {
  102. FILE *fp;
  103. int i;
  104. if(path == NULL || strlen(path)==0)
  105. return NC_EINVAL;
  106. if (!(fp = fopen(path, "r")))
  107. return errno;
  108. i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
  109. fclose(fp);
  110. if(i != 1)
  111. return errno;
  112. }
  113. /* Ignore the first byte for HDF */
  114. if(magic[1] == 'H' && magic[2] == 'D' && magic[3] == 'F')
  115. *hdf = 5;
  116. else if(magic[0] == '\016' && magic[1] == '\003'
  117. && magic[2] == '\023' && magic[3] == '\001')
  118. *hdf = 4;
  119. else if(magic[0] == 'C' && magic[1] == 'D' && magic[2] == 'F')
  120. {
  121. if(magic[3] == '\001')
  122. *cdf = 1; /* netcdf classic version 1 */
  123. else if(magic[3] == '\002')
  124. *cdf = 2; /* netcdf classic version 2 */
  125. }
  126. return NC_NOERR;
  127. }
  128. /** \ingroup datasets
  129. Create a new netCDF file.
  130. This function creates a new netCDF dataset, returning a netCDF ID that
  131. can subsequently be used to refer to the netCDF dataset in other
  132. netCDF function calls. The new netCDF dataset opened for write access
  133. and placed in define mode, ready for you to add dimensions, variables,
  134. and attributes.
  135. \param path The file name of the new netCDF dataset.
  136. \param cmode The creation mode flag. The following flags are
  137. available: NC_NOCLOBBER (do not overwrite existing file), NC_SHARE
  138. (limit write caching - netcdf classic files onlt), NC_64BIT_OFFSET
  139. (create 64-bit offset file), NC_NETCDF4 (create netCDF-4/HDF5 file),
  140. NC_CLASSIC_MODEL (enforce netCDF classic mode on netCDF-4/HDF5
  141. files), NC_DISKLESS (store data only in memory), NC_MMAP (use MMAP
  142. for NC_DISKLESS), and NC_WRITE.
  143. See discussion below.
  144. \param ncidp Pointer to location where returned netCDF ID is to be
  145. stored.
  146. <h2>The cmode Flag</h2>
  147. The cmode flag is used to control the type of file created, and some
  148. aspects of how it may be used.
  149. Setting NC_NOCLOBBER means you do not want to clobber (overwrite) an
  150. existing dataset; an error (NC_EEXIST) is returned if the specified
  151. dataset already exists.
  152. The NC_SHARE flag is appropriate when one process may be writing the
  153. dataset and one or more other processes reading the dataset
  154. concurrently; it means that dataset accesses are not buffered and
  155. caching is limited. Since the buffering scheme is optimized for
  156. sequential access, programs that do not access data sequentially may
  157. see some performance improvement by setting the NC_SHARE flag. This
  158. flag is ignored for netCDF-4 files.
  159. Setting NC_64BIT_OFFSET causes netCDF to create a 64-bit offset format
  160. file, instead of a netCDF classic format file. The 64-bit offset
  161. format imposes far fewer restrictions on very large (i.e. over 2 GB)
  162. data files. See Large File Support.
  163. A zero value (defined for convenience as NC_CLOBBER) specifies the
  164. default behavior: overwrite any existing dataset with the same file
  165. name and buffer and cache accesses for efficiency. The dataset will be
  166. in netCDF classic format. See NetCDF Classic Format Limitations.
  167. Setting NC_NETCDF4 causes netCDF to create a HDF5/NetCDF-4 file.
  168. Setting NC_CLASSIC_MODEL causes netCDF to enforce the classic data
  169. model in this file. (This only has effect for netCDF-4/HDF5 files, as
  170. classic and 64-bit offset files always use the classic model.) When
  171. used with NC_NETCDF4, this flag ensures that the resulting
  172. netCDF-4/HDF5 file may never contain any new constructs from the
  173. enhanced data model. That is, it cannot contain groups, user defined
  174. types, multiple unlimited dimensions, or new atomic types. The
  175. advantage of this restriction is that such files are guaranteed to
  176. work with existing netCDF software.
  177. Setting NC_DISKLESS causes netCDF to create the file only in memory.
  178. This allows for the use of files that have no long term purpose. Note that
  179. with one exception, the in-memory file is destroyed upon calling
  180. nc_close. If, however, the flag combination (NC_DISKLESS|NC_WRITE)
  181. is used, then at close, the contents of the memory file will be
  182. made persistent in the file path that was specified in the nc_create
  183. call. If NC_DISKLESS is going to be used for creating a large classic file,
  184. it behooves one to use either nc__create or nc_create_mp and specify
  185. an appropriately large value of the initialsz parameter to avoid
  186. to many extensions to the in-memory space for the file.
  187. This flag applies to files in classic format and to file in extended
  188. format (netcdf-4).
  189. Normally, NC_DISKLESS allocates space in the heap for
  190. storing the in-memory file. If, however, the ./configure
  191. flags --enable-mmap is used, and the additional mode flag
  192. NC_MMAP is specified, then the file will be created using
  193. the operating system MMAP facility.
  194. This flag only applies to files in classic format. Extended
  195. format (netcdf-4) files will ignore the NC_MMAP flag.
  196. Using NC_MMAP for nc_create is
  197. only included for completeness vis-a-vis nc_open. The
  198. ability to use MMAP is of limited use for nc_create because
  199. nc_create is going to create the file in memory anyway.
  200. Closing a MMAP'd file will be slightly faster, but not significantly.
  201. Note that nc_create(path,cmode,ncidp) is equivalent to the invocation of
  202. nc__create(path,cmode,NC_SIZEHINT_DEFAULT,NULL,ncidp).
  203. \returns ::NC_NOERR No error.
  204. \returns ::NC_ENOMEM System out of memory.
  205. \returns ::NC_EHDFERR HDF5 error (netCDF-4 files only).
  206. \returns ::NC_EFILEMETA Error writing netCDF-4 file-level metadata in
  207. HDF5 file. (netCDF-4 files only).
  208. \returns ::NC_EDISKLESS if there was an error in creating the
  209. in-memory file.
  210. \note When creating a netCDF-4 file HDF5 error reporting is turned
  211. off, if it is on. This doesn't stop the HDF5 error stack from
  212. recording the errors, it simply stops their display to the user
  213. through stderr.
  214. <h1>Examples</h1>
  215. In this example we create a netCDF dataset named foo.nc; we want the
  216. dataset to be created in the current directory only if a dataset with
  217. that name does not already exist:
  218. @code
  219. #include <netcdf.h>
  220. ...
  221. int status = NC_NOERR;
  222. int ncid;
  223. ...
  224. status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
  225. if (status != NC_NOERR) handle_error(status);
  226. @endcode
  227. In this example we create a netCDF dataset named foo_large.nc. It will
  228. be in the 64-bit offset format.
  229. @code
  230. #include <netcdf.h>
  231. ...
  232. int status = NC_NOERR;
  233. int ncid;
  234. ...
  235. status = nc_create("foo_large.nc", NC_NOCLOBBER|NC_64BIT_OFFSET, &ncid);
  236. if (status != NC_NOERR) handle_error(status);
  237. @endcode
  238. In this example we create a netCDF dataset named foo_HDF5.nc. It will
  239. be in the HDF5 format.
  240. @code
  241. #include <netcdf.h>
  242. ...
  243. int status = NC_NOERR;
  244. int ncid;
  245. ...
  246. status = nc_create("foo_HDF5.nc", NC_NOCLOBBER|NC_NETCDF4, &ncid);
  247. if (status != NC_NOERR) handle_error(status);
  248. @endcode
  249. In this example we create a netCDF dataset named
  250. foo_HDF5_classic.nc. It will be in the HDF5 format, but will not allow
  251. the use of any netCDF-4 advanced features. That is, it will conform to
  252. the classic netCDF-3 data model.
  253. @code
  254. #include <netcdf.h>
  255. ...
  256. int status = NC_NOERR;
  257. int ncid;
  258. ...
  259. status = nc_create("foo_HDF5_classic.nc", NC_NOCLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid);
  260. if (status != NC_NOERR) handle_error(status);
  261. @endcode
  262. In this example we create a in-memory netCDF classic dataset named
  263. diskless.nc whose content will be lost when nc_close() is called.
  264. @code
  265. #include <netcdf.h>
  266. ...
  267. int status = NC_NOERR;
  268. int ncid;
  269. ...
  270. status = nc_create("diskless.nc", NC_DISKLESS, &ncid);
  271. if (status != NC_NOERR) handle_error(status);
  272. @endcode
  273. In this example we create a in-memory netCDF classic dataset named
  274. diskless.nc and specify that it should be made persistent
  275. in a file named diskless.nc when nc_close() is called.
  276. @code
  277. #include <netcdf.h>
  278. ...
  279. int status = NC_NOERR;
  280. int ncid;
  281. ...
  282. status = nc_create("diskless.nc", NC_DISKLESS|NC_WRITE, &ncid);
  283. if (status != NC_NOERR) handle_error(status);
  284. @endcode
  285. A variant of nc_create(), nc__create() (note the double underscore) allows
  286. users to specify two tuning parameters for the file that it is
  287. creating. */
  288. int
  289. nc_create(const char *path, int cmode, int *ncidp)
  290. {
  291. return nc__create(path,cmode,NC_SIZEHINT_DEFAULT,NULL,ncidp);
  292. }
  293. /*!
  294. Create a netCDF file with some extra parameters controlling classic
  295. file cacheing.
  296. Like nc_create(), this function creates a netCDF file.
  297. \param path The file name of the new netCDF dataset.
  298. \param cmode The creation mode flag, the same as in nc_create().
  299. \param initialsz On some systems, and with custom I/O layers, it may
  300. be advantageous to set the size of the output file at creation
  301. time. This parameter sets the initial size of the file at creation
  302. time. This only applies to classic and 64-bit offset files.
  303. The special value NC_SIZEHINT_DEFAULT (which is the value 0),
  304. lets the netcdf library choose a suitable initial size.
  305. \param chunksizehintp A pointer to the chunk size hint,
  306. which controls a space versus time tradeoff, memory
  307. allocated in the netcdf library versus number of system
  308. calls. Because of internal requirements, the value may not
  309. be set to exactly the value requested. The actual value
  310. chosen is returned by reference. Using a NULL pointer or
  311. having the pointer point to the value NC_SIZEHINT_DEFAULT
  312. causes the library to choose a default. How the system
  313. chooses the default depends on the system. On many systems,
  314. the "preferred I/O block size" is available from the stat()
  315. system call, struct stat member st_blksize. If this is
  316. available it is used. Lacking that, twice the system
  317. pagesize is used. Lacking a call to discover the system
  318. pagesize, we just set default bufrsize to 8192. The bufrsize
  319. is a property of a given open netcdf descriptor ncid, it is
  320. not a persistent property of the netcdf dataset. This only
  321. applies to classic and 64-bit offset files.
  322. \param ncidp Pointer to location where returned netCDF ID is to be
  323. stored.
  324. \note This function uses the same return codes as the nc_create()
  325. function.
  326. <h1>Examples</h1>
  327. In this example we create a netCDF dataset named foo_large.nc; we want
  328. the dataset to be created in the current directory only if a dataset
  329. with that name does not already exist. We also specify that bufrsize
  330. and initial size for the file.
  331. \code
  332. #include <netcdf.h>
  333. ...
  334. int status = NC_NOERR;
  335. int ncid;
  336. int intialsz = 2048;
  337. int *bufrsize;
  338. ...
  339. *bufrsize = 1024;
  340. status = nc__create("foo.nc", NC_NOCLOBBER, initialsz, bufrsize, &ncid);
  341. if (status != NC_NOERR) handle_error(status);
  342. \endcode
  343. */
  344. int
  345. nc__create(const char *path, int cmode, size_t initialsz,
  346. size_t *chunksizehintp, int *ncidp)
  347. {
  348. return NC_create(path, cmode, initialsz, 0,
  349. chunksizehintp, 0, NULL, ncidp);
  350. }
  351. /**
  352. \internal
  353. \deprecated This function was used in the old days with the Cray at
  354. NCAR. The Cray is long gone, and this call is supported only for
  355. backward compatibility.
  356. */
  357. int
  358. nc__create_mp(const char *path, int cmode, size_t initialsz,
  359. int basepe, size_t *chunksizehintp, int *ncidp)
  360. {
  361. return NC_create(path, cmode, initialsz, basepe,
  362. chunksizehintp, 0, NULL, ncidp);
  363. }
  364. /**
  365. Open an existing netCDF file.
  366. This function opens an existing netCDF dataset for access. It
  367. determines the underlying file format automatically. Use the same call
  368. to open a netCDF classic, 64-bit offset, or netCDF-4 file.
  369. \param path File name for netCDF dataset to be opened. When DAP
  370. support is enabled, then the path may be an OPeNDAP URL rather than a
  371. file path.
  372. \param mode The mode flag may include NC_WRITE (for read/write
  373. access) and NC_SHARE (see below) and NC_DISKLESS (see below).
  374. \param ncidp Pointer to location where returned netCDF ID is to be
  375. stored.
  376. <h2>Open Mode</h2>
  377. A zero value (or NC_NOWRITE) specifies the default behavior: open the
  378. dataset with read-only access, buffering and caching accesses for
  379. efficiency.
  380. Otherwise, the open mode is NC_WRITE, NC_SHARE, or
  381. NC_WRITE|NC_SHARE. Setting the NC_WRITE flag opens the dataset with
  382. read-write access. ("Writing" means any kind of change to the dataset,
  383. including appending or changing data, adding or renaming dimensions,
  384. variables, and attributes, or deleting attributes.)
  385. The NC_SHARE flag is only used for netCDF classic and 64-bit offset
  386. files. It is appropriate when one process may be writing the dataset
  387. and one or more other processes reading the dataset concurrently; it
  388. means that dataset accesses are not buffered and caching is
  389. limited. Since the buffering scheme is optimized for sequential
  390. access, programs that do not access data sequentially may see some
  391. performance improvement by setting the NC_SHARE flag.
  392. This procedure may also be invoked with the NC_DISKLESS flag
  393. set in the mode argument if the file to be opened is a
  394. classic format file. For nc_open(), this flag applies only
  395. to files in classic format. If the file is of type
  396. NC_NETCDF4, then the NC_DISKLESS flag will be ignored.
  397. If NC_DISKLESS is specified, then the whole file is read completely into
  398. memory. In effect this creates an in-memory cache of the file.
  399. If the mode flag also specifies NC_WRITE, then the in-memory cache
  400. will be re-written to the disk file when nc_close() is called.
  401. For some kinds of manipulations, having the in-memory cache can
  402. speed up file processing. But in simple cases, non-cached
  403. processing may actually be faster than using cached processing.
  404. You will need to experiment to determine if the in-memory caching
  405. is worthwhile for your application.
  406. Normally, NC_DISKLESS allocates space in the heap for
  407. storing the in-memory file. If, however, the ./configure
  408. flags --enable-mmap is used, and the additional mode flag
  409. NC_MMAP is specified, then the file will be opened using
  410. the operating system MMAP facility.
  411. This flag only applies to files in classic format. Extended
  412. format (netcdf-4) files will ignore the NC_MMAP flag.
  413. In most cases, using MMAP provides no advantage
  414. for just NC_DISKLESS. The one case where using MMAP is an
  415. advantage is when a file is to be opened and only a small portion
  416. of its data is to be read and/or written.
  417. In this scenario, MMAP will cause only the accessed data to be
  418. retrieved from disk. Without MMAP, NC_DISKLESS will read the whole
  419. file into memory on nc_open. Thus, MMAP will provide some performance
  420. improvement in this case.
  421. It is not necessary to pass any information about the format of the
  422. file being opened. The file type will be detected automatically by the
  423. netCDF library.
  424. If a the path is a DAP URL, then the open mode is read-only.
  425. Setting NC_WRITE will be ignored.
  426. \note When opening a netCDF-4 file HDF5 error reporting is turned off,
  427. if it is on. This doesn't stop the HDF5 error stack from recording the
  428. errors, it simply stops their display to the user through stderr.
  429. nc_open()returns the value NC_NOERR if no errors occurred. Otherwise,
  430. the returned status indicates an error. Possible causes of errors
  431. include:
  432. Note that nc_open(path,cmode,ncidp) is equivalent to the invocation of
  433. nc__open(path,cmode,NC_SIZEHINT_DEFAULT,NULL,ncidp).
  434. \returns ::NC_NOERR No error.
  435. \returns ::NC_ENOMEM Out of memory.
  436. \returns ::NC_EHDFERR HDF5 error. (NetCDF-4 files only.)
  437. \returns ::NC_EDIMMETA Error in netCDF-4 dimension metadata. (NetCDF-4 files only.)
  438. <h1>Examples</h1>
  439. Here is an example using nc_open()to open an existing netCDF dataset
  440. named foo.nc for read-only, non-shared access:
  441. @code
  442. #include <netcdf.h>
  443. ...
  444. int status = NC_NOERR;
  445. int ncid;
  446. ...
  447. status = nc_open("foo.nc", 0, &ncid);
  448. if (status != NC_NOERR) handle_error(status);
  449. @endcode
  450. */
  451. int
  452. nc_open(const char *path, int mode, int *ncidp)
  453. {
  454. return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
  455. }
  456. /**
  457. Open a netCDF file with extra performance parameters for the classic
  458. library.
  459. \param path File name for netCDF dataset to be opened. When DAP
  460. support is enabled, then the path may be an OPeNDAP URL rather than a
  461. file path.
  462. \param mode The mode flag may include NC_WRITE (for read/write
  463. access) and NC_SHARE as in nc_open().
  464. \param chunksizehintp A size hint for the classic library. Only
  465. applies to classic and 64-bit offset files. See below for more
  466. information.
  467. \param ncidp Pointer to location where returned netCDF ID is to be
  468. stored.
  469. <h1>The chunksizehintp Parameter</h1>
  470. The argument referenced by bufrsizehintp controls a space versus time
  471. tradeoff, memory allocated in the netcdf library versus number of
  472. system calls.
  473. Because of internal requirements, the value may not be set to exactly
  474. the value requested. The actual value chosen is returned by reference.
  475. Using a NULL pointer or having the pointer point to the value
  476. NC_SIZEHINT_DEFAULT causes the library to choose a default.
  477. How the system chooses the default depends on the system. On
  478. many systems, the "preferred I/O block size" is available from the
  479. stat() system call, struct stat member st_blksize. If this is
  480. available it is used. Lacking that, twice the system pagesize is used.
  481. Lacking a call to discover the system pagesize, we just set default
  482. bufrsize to 8192.
  483. The bufrsize is a property of a given open netcdf descriptor ncid, it
  484. is not a persistent property of the netcdf dataset.
  485. \returns ::NC_NOERR No error.
  486. \returns ::NC_ENOMEM Out of memory.
  487. \returns ::NC_EHDFERR HDF5 error. (NetCDF-4 files only.)
  488. \returns ::NC_EDIMMETA Error in netCDF-4 dimension metadata. (NetCDF-4
  489. files only.)
  490. */
  491. int
  492. nc__open(const char *path, int mode,
  493. size_t *chunksizehintp, int *ncidp)
  494. {
  495. return NC_open(path, mode, 0, chunksizehintp, 0,
  496. NULL, ncidp);
  497. }
  498. /**
  499. \internal
  500. \deprecated This function was used in the old days with the Cray at
  501. NCAR. The Cray is long gone, and this call is supported only for
  502. backward compatibility.
  503. */
  504. int
  505. nc__open_mp(const char *path, int mode, int basepe,
  506. size_t *chunksizehintp, int *ncidp)
  507. {
  508. return NC_open(path, mode, basepe, chunksizehintp,
  509. 0, NULL, ncidp);
  510. }
  511. /**
  512. Get the file pathname (or the opendap URL) which was used to
  513. open/create the ncid's file.
  514. \param ncid NetCDF ID, from a previous call to nc_open() or
  515. nc_create().
  516. \param pathlen Pointer where length of path will be returned. Ignored
  517. if NULL.
  518. \param path Pointer where path name will be copied. Space must already
  519. be allocated. Ignored if NULL.
  520. \returns ::NC_NOERR No error.
  521. \returns ::NC_EBADID Invalid ncid passed.
  522. */
  523. int
  524. nc_inq_path(int ncid, size_t *pathlen, char *path)
  525. {
  526. NC* ncp;
  527. int stat = NC_NOERR;
  528. if ((stat = NC_check_id(ncid, &ncp)))
  529. return stat;
  530. if(ncp->path == NULL) {
  531. if(pathlen) *pathlen = 0;
  532. if(path) path[0] = '\0';
  533. } else {
  534. if (pathlen) *pathlen = strlen(ncp->path);
  535. if (path) strcpy(path, ncp->path);
  536. }
  537. return stat;
  538. }
  539. /**
  540. Put open netcdf dataset into define mode
  541. The function nc_redef puts an open netCDF dataset into define mode, so
  542. dimensions, variables, and attributes can be added or renamed and
  543. attributes can be deleted.
  544. For netCDF-4 files (i.e. files created with NC_NETCDF4 in the cmode in
  545. their call to nc_create()), it is not necessary to call nc_redef()
  546. unless the file was also created with NC_STRICT_NC3. For straight-up
  547. netCDF-4 files, nc_redef() is called automatically, as needed.
  548. For all netCDF-4 files, the root ncid must be used. This is the ncid
  549. returned by nc_open() and nc_create(), and points to the root of the
  550. hierarchy tree for netCDF-4 files.
  551. \param ncid NetCDF ID, from a previous call to nc_open() or
  552. nc_create().
  553. \returns ::NC_NOERR No error.
  554. \returns ::NC_EBADID Bad ncid.
  555. \returns ::NC_EBADGRPID The ncid must refer to the root group of the
  556. file, that is, the group returned by nc_open() or nc_create().
  557. \returns ::NC_EINDEFINE Already in define mode.
  558. \returns ::NC_EPERM File is read-only.
  559. <h1>Example</h1>
  560. Here is an example using nc_redef to open an existing netCDF dataset
  561. named foo.nc and put it into define mode:
  562. \code
  563. #include <netcdf.h>
  564. ...
  565. int status = NC_NOERR;
  566. int ncid;
  567. ...
  568. status = nc_open("foo.nc", NC_WRITE, &ncid);
  569. if (status != NC_NOERR) handle_error(status);
  570. ...
  571. status = nc_redef(ncid);
  572. if (status != NC_NOERR) handle_error(status);
  573. \endcode
  574. */
  575. int
  576. nc_redef(int ncid)
  577. {
  578. NC* ncp;
  579. int stat = NC_check_id(ncid, &ncp);
  580. if(stat != NC_NOERR) return stat;
  581. return ncp->dispatch->redef(ncid);
  582. }
  583. /**
  584. Leave define mode
  585. The function nc_enddef() takes an open netCDF dataset out of define
  586. mode. The changes made to the netCDF dataset while it was in define
  587. mode are checked and committed to disk if no problems
  588. occurred. Non-record variables may be initialized to a "fill value" as
  589. well with nc_set_fill(). The netCDF dataset is then placed in data
  590. mode, so variable data can be read or written.
  591. It's not necessary to call nc_enddef() for netCDF-4 files. With netCDF-4
  592. files, nc_enddef() is called when needed by the netcdf-4 library. User
  593. calls to nc_enddef() for netCDF-4 files still flush the metadata to
  594. disk.
  595. This call may involve copying data under some circumstances. For a
  596. more extensive discussion see File Structure and Performance.
  597. For netCDF-4/HDF5 format files there are some variable settings (the
  598. compression, endianness, fletcher32 error correction, and fill value)
  599. which must be set (if they are going to be set at all) between the
  600. nc_def_var() and the next nc_enddef(). Once the nc_enddef() is called,
  601. these settings can no longer be changed for a variable.
  602. \param ncid NetCDF ID, from a previous call to nc_open() or
  603. nc_create().
  604. If you use a group id (in a netCDF-4/HDF5 file), the enddef
  605. will apply to the entire file. That means the enddef will not just end
  606. define mode in one group, but in the entire file.
  607. \returns ::NC_NOERR no error
  608. \returns ::NC_EBADID Invalid ncid passed.
  609. <h1>Example</h1>
  610. Here is an example using nc_enddef() to finish the definitions of a new
  611. netCDF dataset named foo.nc and put it into data mode:
  612. \code
  613. #include <netcdf.h>
  614. ...
  615. int status = NC_NOERR;
  616. int ncid;
  617. ...
  618. status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
  619. if (status != NC_NOERR) handle_error(status);
  620. ... create dimensions, variables, attributes
  621. status = nc_enddef(ncid);
  622. if (status != NC_NOERR) handle_error(status);
  623. \endcode
  624. */
  625. int
  626. nc_enddef(int ncid)
  627. {
  628. int status = NC_NOERR;
  629. NC *ncp;
  630. status = NC_check_id(ncid, &ncp);
  631. if(status != NC_NOERR) return status;
  632. return ncp->dispatch->_enddef(ncid,0,1,0,1);
  633. }
  634. /**
  635. Leave define mode with performance tuning
  636. The function nc__enddef takes an open netCDF dataset out of define
  637. mode. The changes made to the netCDF dataset while it was in define
  638. mode are checked and committed to disk if no problems
  639. occurred. Non-record variables may be initialized to a "fill value" as
  640. well with nc_set_fill(). The netCDF dataset is then placed in data mode,
  641. so variable data can be read or written.
  642. This call may involve copying data under some circumstances. For a
  643. more extensive discussion see File Structure and Performance.
  644. \warning This function exposes internals of the netcdf version 1 file
  645. format. Users should use nc_enddef() in most circumstances. This
  646. function may not be available on future netcdf implementations.
  647. The classic netcdf file format has three sections, the "header"
  648. section, the data section for fixed size variables, and the data
  649. section for variables which have an unlimited dimension (record
  650. variables).
  651. The header begins at the beginning of the file. The index (offset) of
  652. the beginning of the other two sections is contained in the
  653. header. Typically, there is no space between the sections. This causes
  654. copying overhead to accrue if one wishes to change the size of the
  655. sections, as may happen when changing names of things, text attribute
  656. values, adding attributes or adding variables. Also, for buffered i/o,
  657. there may be advantages to aligning sections in certain ways.
  658. The minfree parameters allow one to control costs of future calls to
  659. nc_redef, nc_enddef() by requesting that minfree bytes be available at
  660. the end of the section.
  661. The align parameters allow one to set the alignment of the beginning
  662. of the corresponding sections. The beginning of the section is rounded
  663. up to an index which is a multiple of the align parameter. The flag
  664. value ALIGN_CHUNK tells the library to use the bufrsize (see above) as
  665. the align parameter. It has nothing to do with the chunking
  666. (multidimensional tiling) features of netCDF-4.
  667. The file format requires mod 4 alignment, so the align parameters are
  668. silently rounded up to multiples of 4. The usual call,
  669. \code
  670. nc_enddef(ncid);
  671. \endcode
  672. is equivalent to
  673. \code
  674. nc__enddef(ncid, 0, 4, 0, 4);
  675. \endcode
  676. The file format does not contain a "record size" value, this is
  677. calculated from the sizes of the record variables. This unfortunate
  678. fact prevents us from providing minfree and alignment control of the
  679. "records" in a netcdf file. If you add a variable which has an
  680. unlimited dimension, the third section will always be copied with the
  681. new variable added.
  682. \param ncid NetCDF ID, from a previous call to nc_open() or
  683. nc_create().
  684. \param h_minfree Sets the pad at the end of the "header" section.
  685. \param v_align Controls the alignment of the beginning of the data
  686. section for fixed size variables.
  687. \param v_minfree Sets the pad at the end of the data section for fixed
  688. size variables.
  689. \param r_align Controls the alignment of the beginning of the data
  690. section for variables which have an unlimited dimension (record
  691. variables).
  692. \returns ::NC_NOERR No error.
  693. \returns ::NC_EBADID Invalid ncid passed.
  694. */
  695. int
  696. nc__enddef(int ncid, size_t h_minfree, size_t v_align, size_t v_minfree,
  697. size_t r_align)
  698. {
  699. NC* ncp;
  700. int stat = NC_check_id(ncid, &ncp);
  701. if(stat != NC_NOERR) return stat;
  702. return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
  703. }
  704. /**
  705. Synchronize an open netcdf dataset to disk
  706. The function nc_sync() offers a way to synchronize the disk copy of a
  707. netCDF dataset with in-memory buffers. There are two reasons you might
  708. want to synchronize after writes:
  709. - To minimize data loss in case of abnormal termination, or
  710. - To make data available to other processes for reading immediately
  711. after it is written. But note that a process that already had the
  712. dataset open for reading would not see the number of records
  713. increase when the writing process calls nc_sync(); to accomplish this,
  714. the reading process must call nc_sync.
  715. This function is backward-compatible with previous versions of the
  716. netCDF library. The intent was to allow sharing of a netCDF dataset
  717. among multiple readers and one writer, by having the writer call
  718. nc_sync() after writing and the readers call nc_sync() before each
  719. read. For a writer, this flushes buffers to disk. For a reader, it
  720. makes sure that the next read will be from disk rather than from
  721. previously cached buffers, so that the reader will see changes made by
  722. the writing process (e.g., the number of records written) without
  723. having to close and reopen the dataset. If you are only accessing a
  724. small amount of data, it can be expensive in computer resources to
  725. always synchronize to disk after every write, since you are giving up
  726. the benefits of buffering.
  727. An easier way to accomplish sharing (and what is now recommended) is
  728. to have the writer and readers open the dataset with the NC_SHARE
  729. flag, and then it will not be necessary to call nc_sync() at
  730. all. However, the nc_sync() function still provides finer granularity
  731. than the NC_SHARE flag, if only a few netCDF accesses need to be
  732. synchronized among processes.
  733. It is important to note that changes to the ancillary data, such as
  734. attribute values, are not propagated automatically by use of the
  735. NC_SHARE flag. Use of the nc_sync() function is still required for this
  736. purpose.
  737. Sharing datasets when the writer enters define mode to change the data
  738. schema requires extra care. In previous releases, after the writer
  739. left define mode, the readers were left looking at an old copy of the
  740. dataset, since the changes were made to a new copy. The only way
  741. readers could see the changes was by closing and reopening the
  742. dataset. Now the changes are made in place, but readers have no
  743. knowledge that their internal tables are now inconsistent with the new
  744. dataset schema. If netCDF datasets are shared across redefinition,
  745. some mechanism external to the netCDF library must be provided that
  746. prevents access by readers during redefinition and causes the readers
  747. to call nc_sync before any subsequent access.
  748. When calling nc_sync(), the netCDF dataset must be in data mode. A
  749. netCDF dataset in define mode is synchronized to disk only when
  750. nc_enddef() is called. A process that is reading a netCDF dataset that
  751. another process is writing may call nc_sync to get updated with the
  752. changes made to the data by the writing process (e.g., the number of
  753. records written), without having to close and reopen the dataset.
  754. Data is automatically synchronized to disk when a netCDF dataset is
  755. closed, or whenever you leave define mode.
  756. \param ncid NetCDF ID, from a previous call to nc_open() or
  757. nc_create().
  758. \returns ::NC_NOERR No error.
  759. \returns ::NC_EBADID Invalid ncid passed.
  760. */
  761. int
  762. nc_sync(int ncid)
  763. {
  764. NC* ncp;
  765. int stat = NC_check_id(ncid, &ncp);
  766. if(stat != NC_NOERR) return stat;
  767. return ncp->dispatch->sync(ncid);
  768. }
  769. /**
  770. \internal
  771. Users no longer need to call this function, since it is called
  772. automatically by nc_close() in case the dataset is in define mode and
  773. something goes wrong with committing the changes. The function
  774. nc_abort() just closes the netCDF dataset, if not in define mode. If
  775. the dataset is being created and is still in define mode, the dataset
  776. is deleted. If define mode was entered by a call to nc_redef(), the
  777. netCDF dataset is restored to its state before definition mode was
  778. entered and the dataset is closed.
  779. \param ncid NetCDF ID, from a previous call to nc_open() or
  780. nc_create().
  781. \returns ::NC_NOERR No error.
  782. <h1>Example</h1>
  783. Here is an example using nc_abort to back out of redefinitions of a
  784. dataset named foo.nc:
  785. \code
  786. #include <netcdf.h>
  787. ...
  788. int ncid, status, latid;
  789. ...
  790. status = nc_open("foo.nc", NC_WRITE, &ncid);
  791. if (status != NC_NOERR) handle_error(status);
  792. ...
  793. status = nc_redef(ncid);
  794. if (status != NC_NOERR) handle_error(status);
  795. ...
  796. status = nc_def_dim(ncid, "lat", 18L, &latid);
  797. if (status != NC_NOERR) {
  798. handle_error(status);
  799. status = nc_abort(ncid);
  800. if (status != NC_NOERR) handle_error(status);
  801. }
  802. \endcode
  803. */
  804. int
  805. nc_abort(int ncid)
  806. {
  807. NC* ncp;
  808. int stat = NC_check_id(ncid, &ncp);
  809. if(stat != NC_NOERR) return stat;
  810. if(ncp->path != NULL) free(ncp->path);
  811. ncp->path = NULL;
  812. return ncp->dispatch->abort(ncid);
  813. }
  814. /**
  815. Close an open netCDF dataset
  816. If the dataset in define mode, nc_enddef() will be called before
  817. closing. (In this case, if nc_enddef() returns an error, nc_abort() will
  818. automatically be called to restore the dataset to the consistent state
  819. before define mode was last entered.) After an open netCDF dataset is
  820. closed, its netCDF ID may be reassigned to the next netCDF dataset
  821. that is opened or created.
  822. \param ncid NetCDF ID, from a previous call to nc_open() or nc_create().
  823. \returns ::NC_NOERR No error.
  824. \returns ::NC_EBADID Invalid id passed.
  825. \returns ::NC_EBADGRPID ncid did not contain the root group id of this
  826. file. (NetCDF-4 only).
  827. <h1>Example</h1>
  828. Here is an example using nc_close to finish the definitions of a new
  829. netCDF dataset named foo.nc and release its netCDF ID:
  830. \code
  831. #include <netcdf.h>
  832. ...
  833. int status = NC_NOERR;
  834. int ncid;
  835. ...
  836. status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
  837. if (status != NC_NOERR) handle_error(status);
  838. ... create dimensions, variables, attributes
  839. status = nc_close(ncid);
  840. if (status != NC_NOERR) handle_error(status);
  841. \endcode
  842. */
  843. int
  844. nc_close(int ncid)
  845. {
  846. NC* ncp;
  847. int stat = NC_check_id(ncid, &ncp);
  848. if(stat != NC_NOERR) return stat;
  849. return ncp->dispatch->close(ncid);
  850. }
  851. /**
  852. Change the fill-value mode to improve write performance.
  853. This function is intended for advanced usage, to optimize writes under
  854. some circumstances described below. The function nc_set_fill() sets the
  855. fill mode for a netCDF dataset open for writing and returns the
  856. current fill mode in a return parameter. The fill mode can be
  857. specified as either ::NC_FILL or ::NC_NOFILL. The default behavior
  858. corresponding to ::NC_FILL is that data is pre-filled with fill values,
  859. that is fill values are written when you create non-record variables
  860. or when you write a value beyond data that has not yet been
  861. written. This makes it possible to detect attempts to read data before
  862. it was written. For more information on the use of fill values see
  863. Fill Values. For information about how to define your own fill values
  864. see Attribute Conventions.
  865. The behavior corresponding to ::NC_NOFILL overrides the default behavior
  866. of prefilling data with fill values. This can be used to enhance
  867. performance, because it avoids the duplicate writes that occur when
  868. the netCDF library writes fill values that are later overwritten with
  869. data.
  870. A value indicating which mode the netCDF dataset was already in is
  871. returned. You can use this value to temporarily change the fill mode
  872. of an open netCDF dataset and then restore it to the previous mode.
  873. After you turn on ::NC_NOFILL mode for an open netCDF dataset, you must
  874. be certain to write valid data in all the positions that will later be
  875. read. Note that nofill mode is only a transient property of a netCDF
  876. dataset open for writing: if you close and reopen the dataset, it will
  877. revert to the default behavior. You can also revert to the default
  878. behavior by calling nc_set_fill() again to explicitly set the fill mode
  879. to ::NC_FILL.
  880. There are three situations where it is advantageous to set nofill
  881. mode:
  882. - Creating and initializing a netCDF dataset. In this case, you should
  883. set nofill mode before calling nc_enddef() and then write completely
  884. all non-record variables and the initial records of all the record
  885. variables you want to initialize.
  886. - Extending an existing record-oriented netCDF dataset. Set nofill
  887. mode after opening the dataset for writing, then append the
  888. additional records to the dataset completely, leaving no intervening
  889. unwritten records.
  890. - Adding new variables that you are going to initialize to an existing
  891. netCDF dataset. Set nofill mode before calling nc_enddef() then write
  892. all the new variables completely.
  893. If the netCDF dataset has an unlimited dimension and the last record
  894. was written while in nofill mode, then the dataset may be shorter than
  895. if nofill mode was not set, but this will be completely transparent if
  896. you access the data only through the netCDF interfaces.
  897. The use of this feature may not be available (or even needed) in
  898. future releases. Programmers are cautioned against heavy reliance upon
  899. this feature.
  900. \param ncid NetCDF ID, from a previous call to nc_open() or
  901. nc_create().
  902. \param fillmode Desired fill mode for the dataset, either ::NC_NOFILL or
  903. ::NC_FILL.
  904. \param old_modep Pointer to location for returned current fill mode of
  905. the dataset before this call, either ::NC_NOFILL or ::NC_FILL.
  906. \returns ::NC_NOERR No error.
  907. \returns ::NC_EBADID The specified netCDF ID does not refer to an open
  908. netCDF dataset.
  909. \returns ::NC_EPERM The specified netCDF ID refers to a dataset open for
  910. read-only access.
  911. \returns ::NC_EINVAL The fill mode argument is neither ::NC_NOFILL nor
  912. ::NC_FILL.
  913. <h1>Example</h1>
  914. Here is an example using nc_set_fill() to set nofill mode for subsequent
  915. writes of a netCDF dataset named foo.nc:
  916. \code
  917. #include <netcdf.h>
  918. ...
  919. int ncid, status, old_fill_mode;
  920. ...
  921. status = nc_open("foo.nc", NC_WRITE, &ncid);
  922. if (status != NC_NOERR) handle_error(status);
  923. ... write data with default prefilling behavior
  924. status = nc_set_fill(ncid, ::NC_NOFILL, &old_fill_mode);
  925. if (status != NC_NOERR) handle_error(status);
  926. ... write data with no prefilling
  927. \endcode
  928. */
  929. int
  930. nc_set_fill(int ncid, int fillmode, int *old_modep)
  931. {
  932. NC* ncp;
  933. int stat = NC_check_id(ncid, &ncp);
  934. if(stat != NC_NOERR) return stat;
  935. return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
  936. }
  937. /**
  938. \internal
  939. \deprecated This function was used in the old days with the Cray at
  940. NCAR. The Cray is long gone, and this call is supported only for
  941. backward compatibility.
  942. \returns ::NC_NOERR No error.
  943. \returns ::NC_EBADID Invalid ncid passed.
  944. */
  945. int
  946. nc_inq_base_pe(int ncid, int *pe)
  947. {
  948. NC* ncp;
  949. int stat = NC_check_id(ncid, &ncp);
  950. if(stat != NC_NOERR) return stat;
  951. return ncp->dispatch->inq_base_pe(ncid,pe);
  952. }
  953. /**
  954. \internal
  955. \deprecated This function was used in the old days with the Cray at
  956. NCAR. The Cray is long gone, and this call is supported only for
  957. backward compatibility.
  958. \returns ::NC_NOERR No error.
  959. \returns ::NC_EBADID Invalid ncid passed.
  960. */
  961. int
  962. nc_set_base_pe(int ncid, int pe)
  963. {
  964. NC* ncp;
  965. int stat = NC_check_id(ncid, &ncp);
  966. if(stat != NC_NOERR) return stat;
  967. return ncp->dispatch->set_base_pe(ncid,pe);
  968. }
  969. /**
  970. Inquire about the binary format of a netCDF file.
  971. This function returns the (rarely needed) format version.
  972. \param ncid NetCDF ID, from a previous call to nc_open() or
  973. nc_create().
  974. \param formatp Pointer to location for returned format version, one of
  975. NC_FORMAT_CLASSIC, NC_FORMAT_64BIT, NC_FORMAT_NETCDF4,
  976. NC_FORMAT_NETCDF4_CLASSIC.
  977. \returns ::NC_NOERR No error.
  978. \returns ::NC_EBADID Invalid ncid passed.
  979. */
  980. int
  981. nc_inq_format(int ncid, int *formatp)
  982. {
  983. NC* ncp;
  984. int stat = NC_check_id(ncid, &ncp);
  985. if(stat != NC_NOERR) return stat;
  986. return ncp->dispatch->inq_format(ncid,formatp);
  987. }
  988. /**
  989. Inquire about a file or group.
  990. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  991. nc_create(), nc_def_grp(), or associated inquiry functions such as
  992. nc_inq_ncid().
  993. \param ndimsp Pointer to location for returned number of dimensions
  994. defined for this netCDF dataset. Ignored if NULL.
  995. \param nvarsp Pointer to location for returned number of variables
  996. defined for this netCDF dataset. Ignored if NULL.
  997. \param nattsp Pointer to location for returned number of global
  998. attributes defined for this netCDF dataset. Ignored if NULL.
  999. \param unlimdimidp Pointer to location for returned ID of the
  1000. unlimited dimension, if there is one for this netCDF dataset. If no
  1001. unlimited length dimension has been defined, -1 is returned. Ignored
  1002. if NULL. If there are multiple unlimited dimensions (possible only
  1003. for netCDF-4 files), only a pointer to the first is returned, for
  1004. backward compatibility. If you want them all, use nc_inq_unlimids().
  1005. \returns ::NC_NOERR No error.
  1006. \returns ::NC_EBADID Invalid ncid passed.
  1007. <h1>Example</h1>
  1008. Here is an example using nc_inq to find out about a netCDF dataset
  1009. named foo.nc:
  1010. \code
  1011. #include <netcdf.h>
  1012. ...
  1013. int status, ncid, ndims, nvars, ngatts, unlimdimid;
  1014. ...
  1015. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  1016. if (status != NC_NOERR) handle_error(status);
  1017. ...
  1018. status = nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid);
  1019. if (status != NC_NOERR) handle_error(status);
  1020. \endcode
  1021. */
  1022. int
  1023. nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
  1024. {
  1025. NC* ncp;
  1026. int stat = NC_check_id(ncid, &ncp);
  1027. if(stat != NC_NOERR) return stat;
  1028. return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
  1029. }
  1030. int
  1031. nc_inq_nvars(int ncid, int *nvarsp)
  1032. {
  1033. NC* ncp;
  1034. int stat = NC_check_id(ncid, &ncp);
  1035. if(stat != NC_NOERR) return stat;
  1036. return ncp->dispatch->inq(ncid, NULL, nvarsp, NULL, NULL);
  1037. }
  1038. /**
  1039. Inquire about a type.
  1040. Given an ncid and a typeid, get the information about a type. This
  1041. function will work on any type, including atomic and any user defined
  1042. type, whether compound, opaque, enumeration, or variable length array.
  1043. For even more information about a user defined type nc_inq_user_type().
  1044. \param ncid The ncid for the group containing the type (ignored for
  1045. atomic types).
  1046. \param xtype The typeid for this type, as returned by nc_def_compound,
  1047. nc_def_opaque, nc_def_enum, nc_def_vlen, or nc_inq_var, or as found in
  1048. netcdf.h in the list of atomic types (NC_CHAR, NC_INT, etc.).
  1049. \param name If non-NULL, the name of the user defined type will be
  1050. copied here. It will be NC_MAX_NAME bytes or less. For atomic types,
  1051. the type name from CDL will be given.
  1052. \param size If non-NULL, the (in-memory) size of the type in bytes
  1053. will be copied here. VLEN type size is the size of nc_vlen_t. String
  1054. size is returned as the size of a character pointer. The size may be
  1055. used to malloc space for the data, no matter what the type.
  1056. \returns ::NC_NOERR No error.
  1057. \returns ::NC_EBADTYPE Bad typeid.
  1058. \returns ::NC_ENOTNC4 Seeking a user-defined type in a netCDF-3 file.
  1059. \returns ::NC_ESTRICTNC3 Seeking a user-defined type in a netCDF-4 file
  1060. for which classic model has been turned on.
  1061. \returns ::NC_EBADGRPID Bad group ID in ncid.
  1062. \returns ::NC_EBADID Type ID not found.
  1063. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  1064. <h1>Example</h1>
  1065. This example is from the test program tst_enums.c, and it uses all the
  1066. possible inquiry functions on an enum type.
  1067. \code
  1068. if (nc_inq_user_type(ncid, typeids[0], name_in, &base_size_in, &base_nc_type_in,
  1069. &nfields_in, &class_in)) ERR;
  1070. if (strcmp(name_in, TYPE_NAME) || base_size_in != sizeof(int) ||
  1071. base_nc_type_in != NC_INT || nfields_in != NUM_MEMBERS || class_in != NC_ENUM) ERR;
  1072. if (nc_inq_type(ncid, typeids[0], name_in, &base_size_in)) ERR;
  1073. if (strcmp(name_in, TYPE_NAME) || base_size_in != sizeof(int)) ERR;
  1074. if (nc_inq_enum(ncid, typeids[0], name_in, &base_nc_type, &base_size_in, &num_members)) ERR;
  1075. if (strcmp(name_in, TYPE_NAME) || base_nc_type != NC_INT || num_members != NUM_MEMBERS) ERR;
  1076. for (i = 0; i < NUM_MEMBERS; i++)
  1077. {
  1078. if (nc_inq_enum_member(ncid, typeid, i, name_in, &value_in)) ERR;
  1079. if (strcmp(name_in, member_name[i]) || value_in != member_value[i]) ERR;
  1080. if (nc_inq_enum_ident(ncid, typeid, member_value[i], name_in)) ERR;
  1081. if (strcmp(name_in, member_name[i])) ERR;
  1082. }
  1083. if (nc_close(ncid)) ERR;
  1084. \endcode
  1085. */
  1086. int
  1087. nc_inq_type(int ncid, nc_type xtype, char *name, size_t *size)
  1088. {
  1089. NC* ncp;
  1090. /* For compatibility, we need to allow inq about
  1091. atomic types, even if ncid is ill-defined */
  1092. if(xtype <= ATOMICTYPEMAX) {
  1093. if(xtype <= NC_NAT) return NC_EBADTYPE;
  1094. if(name) strncpy(name,NC_atomictypename(xtype),NC_MAX_NAME);
  1095. if(size) *size = NC_atomictypelen(xtype);
  1096. return NC_NOERR;
  1097. } else {
  1098. int stat = NC_check_id(ncid, &ncp);
  1099. if(stat != NC_NOERR) return NC_EBADTYPE; /* compatibility */
  1100. return ncp->dispatch->inq_type(ncid,xtype,name,size);
  1101. }
  1102. }
  1103. /**@}*/
  1104. /**
  1105. \internal
  1106. \ingroup dispatch
  1107. Create a file, calling the appropriate dispatch create call.
  1108. For create, we have the following pieces of information to use to
  1109. determine the dispatch table:
  1110. - table specified by override
  1111. - path
  1112. - cmode
  1113. \param path The file name of the new netCDF dataset.
  1114. \param cmode The creation mode flag, the same as in nc_create().
  1115. \param initialsz This parameter sets the initial size of the file at creation
  1116. time. This only applies to classic and 64-bit offset files.
  1117. \param basepe Deprecated parameter from the Cray days.
  1118. \param chunksizehintp A pointer to the chunk size hint. This only
  1119. applies to classic and 64-bit offset files.
  1120. \param useparallel Non-zero if parallel I/O is to be used on this
  1121. file.
  1122. \param mpi_info Pointer to MPI comm and info.
  1123. \param ncidp Pointer to location where returned netCDF ID is to be
  1124. stored.
  1125. \returns ::NC_NOERR No error.
  1126. */
  1127. int
  1128. NC_create(const char *path, int cmode, size_t initialsz,
  1129. int basepe, size_t *chunksizehintp, int useparallel,
  1130. void* mpi_info, int *ncidp)
  1131. {
  1132. int stat = NC_NOERR;
  1133. NC* ncp = NULL;
  1134. NC_Dispatch* dispatcher = NULL;
  1135. /* Need three pieces of information for now */
  1136. int model = 0; /* one of the NC_DISPATCH_XXX values */
  1137. int isurl = 0; /* dap or cdmremote or neither */
  1138. int xcmode = 0; /* for implied cmode flags */
  1139. extern int default_create_format;
  1140. /* Initialize the dispatch table. The function pointers in the
  1141. * dispatch table will depend on how netCDF was built
  1142. * (with/without netCDF-4, DAP, CDMREMOTE). */
  1143. if(!nc_initialized)
  1144. {
  1145. if ((stat = NC_initialize()))
  1146. return stat;
  1147. /* Do local initialization */
  1148. nc_local_initialize();
  1149. nc_initialized = 1;
  1150. }
  1151. if((isurl = NC_testurl(path)))
  1152. model = NC_urlmodel(path);
  1153. /* Look to the incoming cmode for hints */
  1154. if(model == 0) {
  1155. if(cmode & NC_NETCDF4 || cmode & NC_PNETCDF)
  1156. model = NC_DISPATCH_NC4;
  1157. }
  1158. if(model == 0) {
  1159. /* Check default format */
  1160. int format = default_create_format;
  1161. switch (format) {
  1162. #ifdef USE_NETCDF4
  1163. case NC_FORMAT_NETCDF4:
  1164. xcmode |= NC_NETCDF4;
  1165. model = NC_DISPATCH_NC4;
  1166. break;
  1167. case NC_FORMAT_NETCDF4_CLASSIC:
  1168. xcmode |= NC_CLASSIC_MODEL;
  1169. model = NC_DISPATCH_NC4;
  1170. break;
  1171. #endif
  1172. case NC_FORMAT_64BIT:
  1173. xcmode |= NC_64BIT_OFFSET;
  1174. /* fall thru */
  1175. case NC_FORMAT_CLASSIC:
  1176. default:
  1177. model = NC_DISPATCH_NC3;
  1178. break;
  1179. }
  1180. }
  1181. /* Add inferred flags */
  1182. cmode |= xcmode;
  1183. #ifdef USE_NETCDF4
  1184. if((cmode & NC_MPIIO && cmode & NC_MPIPOSIX))
  1185. return NC_EINVAL;
  1186. #endif
  1187. if (!(dispatcher = NC_get_dispatch_override()))
  1188. {
  1189. /* Figure out what dispatcher to use */
  1190. #ifdef USE_NETCDF4
  1191. #ifdef USE_CDMREMOTE
  1192. if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
  1193. dispatcher = NCCR_dispatch_table;
  1194. else
  1195. #endif
  1196. if(model == (NC_DISPATCH_NC4))
  1197. dispatcher = NC4_dispatch_table;
  1198. else
  1199. #endif /*USE_NETCDF4*/
  1200. #ifdef USE_DAP
  1201. if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
  1202. dispatcher = NCD3_dispatch_table;
  1203. else
  1204. #endif
  1205. if(model == (NC_DISPATCH_NC3))
  1206. dispatcher = NC3_dispatch_table;
  1207. else
  1208. return NC_ENOTNC;
  1209. }
  1210. if ((stat = dispatcher->create(path, cmode, initialsz, basepe, chunksizehintp,
  1211. useparallel, mpi_info, dispatcher, &ncp)))
  1212. return stat;
  1213. ncp->dispatch = dispatcher;
  1214. if(ncidp)
  1215. *ncidp = ncp->ext_ncid;
  1216. if (!(ncp->path = nulldup(path)))
  1217. return NC_ENOMEM;
  1218. return NC_NOERR;
  1219. }
  1220. /**
  1221. \internal
  1222. \ingroup dispatch
  1223. Open a netCDF file (or remote dataset) calling the appropriate
  1224. dispatch function.
  1225. For open, we have the following pieces of information to use to determine the dispatch table.
  1226. - table specified by override
  1227. - path
  1228. - cmode
  1229. - the contents of the file (if it exists), basically checking its magic number.
  1230. \returns ::NC_NOERR No error.
  1231. */
  1232. int
  1233. NC_open(const char *path, int cmode,
  1234. int basepe, size_t *chunksizehintp,
  1235. int useparallel, void* mpi_info,
  1236. int *ncidp)
  1237. {
  1238. int stat = NC_NOERR;
  1239. NC* ncp = NULL;
  1240. NC_Dispatch* dispatcher = NULL;
  1241. /* Need two pieces of information for now */
  1242. int model = 0;
  1243. int isurl = 0;
  1244. int cdfversion = 0;
  1245. int hdfversion = 0;
  1246. extern int default_create_format;
  1247. if(!nc_initialized) {
  1248. stat = NC_initialize();
  1249. if(stat) return stat;
  1250. /* Do local initialization */
  1251. nc_local_initialize();
  1252. nc_initialized = 1;
  1253. }
  1254. isurl = NC_testurl(path);
  1255. if(isurl)
  1256. model = NC_urlmodel(path);
  1257. if(!isurl) {
  1258. /* Look at the file if it exists */
  1259. stat = NC_check_file_type(path,useparallel,mpi_info,&cdfversion,&hdfversion);
  1260. if(stat == NC_NOERR) {
  1261. if(hdfversion != 0) {
  1262. model = NC_DISPATCH_NC4;
  1263. } else if(cdfversion != 0) {
  1264. model = NC_DISPATCH_NC3;
  1265. }
  1266. }
  1267. /* else ignore the file */
  1268. }
  1269. /* Look to the incoming cmode for hints */
  1270. if(model == 0) {
  1271. if(cmode & NC_NETCDF4 || cmode & NC_PNETCDF) model |= NC_DISPATCH_NC4;
  1272. }
  1273. if(model == 0) model = NC_DISPATCH_NC3; /* final default */
  1274. /* Force flag consistentcy */
  1275. if(model & NC_DISPATCH_NC4)
  1276. cmode |= NC_NETCDF4;
  1277. else if(model & NC_DISPATCH_NC3) {
  1278. cmode &= ~NC_NETCDF4; /* must be netcdf-3 */
  1279. if(cdfversion == 2) cmode |= NC_64BIT_OFFSET;
  1280. }
  1281. if((cmode & NC_MPIIO && cmode & NC_MPIPOSIX))
  1282. return NC_EINVAL;
  1283. /* override overrides any other table choice */
  1284. dispatcher = NC_get_dispatch_override();
  1285. if(dispatcher != NULL) goto havetable;
  1286. /* Figure out what dispatcher to use */
  1287. #if defined(USE_CDMREMOTE)
  1288. if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
  1289. dispatcher = NCCR_dispatch_table;
  1290. else
  1291. #endif
  1292. #if defined(USE_DAP)
  1293. if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
  1294. dispatcher = NCD3_dispatch_table;
  1295. else
  1296. #endif
  1297. #if defined(USE_NETCDF4)
  1298. if(model == (NC_DISPATCH_NC4))
  1299. dispatcher = NC4_dispatch_table;
  1300. else
  1301. #endif
  1302. if(model == (NC_DISPATCH_NC3))
  1303. dispatcher = NC3_dispatch_table;
  1304. else
  1305. return NC_ENOTNC;
  1306. havetable:
  1307. stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
  1308. useparallel, mpi_info, dispatcher, &ncp);
  1309. if(stat == NC_NOERR) {
  1310. ncp->dispatch = dispatcher;
  1311. if(ncidp) *ncidp = ncp->ext_ncid;
  1312. ncp->path = nulldup(path);
  1313. if(path == NULL) stat = NC_ENOMEM;
  1314. }
  1315. return stat;
  1316. }
  1317. /*Provide an internal function for generating pseudo file descriptors
  1318. for systems that are not file based (e.g. dap, memio).
  1319. */
  1320. /* Static counter for pseudo file descriptors (incremented) */
  1321. static int pseudofd = 0;
  1322. /* Create a pseudo file descriptor that does not
  1323. overlap real file descriptors
  1324. */
  1325. int
  1326. nc__pseudofd(void)
  1327. {
  1328. if(pseudofd == 0) {
  1329. int maxfd = 32767; /* default */
  1330. #ifdef HAVE_GETRLIMIT
  1331. struct rlimit rl;
  1332. if(getrlimit(RLIMIT_NOFILE,&rl) == 0) {
  1333. if(rl.rlim_max != RLIM_INFINITY)
  1334. maxfd = rl.rlim_max;
  1335. if(rl.rlim_cur != RLIM_INFINITY)
  1336. maxfd = rl.rlim_cur;
  1337. }
  1338. pseudofd = maxfd+1;
  1339. #endif
  1340. }
  1341. return pseudofd++;
  1342. }