-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathdfile.c
2162 lines (1839 loc) · 72.1 KB
/
dfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
*
* File create and open functions
*
* These functions end up calling functions in one of the dispatch
* layers (netCDF-4, dap server, etc).
*
* Copyright 2018 University Corporation for Atmospheric
* Research/Unidata. See COPYRIGHT file for more info.
*/
#include "config.h"
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* lseek() */
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#include "ncdispatch.h"
#include "netcdf_mem.h"
#include "ncwinpath.h"
#include "fbits.h"
#undef DEBUG
extern int NC_initialized; /**< True when dispatch table is initialized. */
/* User-defined formats. */
NC_Dispatch *UDF0_dispatch_table = NULL;
char UDF0_magic_number[NC_MAX_MAGIC_NUMBER_LEN + 1] = "";
NC_Dispatch *UDF1_dispatch_table = NULL;
char UDF1_magic_number[NC_MAX_MAGIC_NUMBER_LEN + 1] = "";
/**************************************************/
/** \defgroup datasets NetCDF File and Data I/O
NetCDF opens datasets as files or remote access URLs.
A netCDF dataset that has not yet been opened can only be referred to
by its dataset name. Once a netCDF dataset is opened, it is referred
to by a netCDF ID, which is a small non-negative integer returned when
you create or open the dataset. A netCDF ID is much like a file
descriptor in C or a logical unit number in FORTRAN. In any single
program, the netCDF IDs of distinct open netCDF datasets are
distinct. A single netCDF dataset may be opened multiple times and
will then have multiple distinct netCDF IDs; however at most one of
the open instances of a single netCDF dataset should permit
writing. When an open netCDF dataset is closed, the ID is no longer
associated with a netCDF dataset.
Functions that deal with the netCDF library include:
- Get version of library.
- Get error message corresponding to a returned error code.
The operations supported on a netCDF dataset as a single object are:
- Create, given dataset name and whether to overwrite or not.
- Open for access, given dataset name and read or write intent.
- Put into define mode, to add dimensions, variables, or attributes.
- Take out of define mode, checking consistency of additions.
- Close, writing to disk if required.
- Inquire about the number of dimensions, number of variables,
number of global attributes, and ID of the unlimited dimension, if
any.
- Synchronize to disk to make sure it is current.
- Set and unset nofill mode for optimized sequential writes.
- After a summary of conventions used in describing the netCDF
interfaces, the rest of this chapter presents a detailed description
of the interfaces for these operations.
*/
/**
* Add handling of user-defined format.
*
* User-defined formats allow users to write a library which can read
* their own proprietary format as if it were netCDF. This allows
* existing netCDF codes to work on non-netCDF data formats.
*
* User-defined formats work by specifying a netCDF dispatch
* table. The dispatch table is a struct of (mostly) C function
* pointers. It contains pointers to the key functions of the netCDF
* API. Once these functions are provided, and the dispatch table is
* specified, the netcdf-c library can read any format.
*
* @note Unlike the public netCDF API, the dispatch table may not be
* backward compatible between netCDF releases. Instead, it contains a
* dispatch version number. If this number is not correct (i.e. does
* not match the current dispatch table version), then ::NC_EINVAL
* will be returned.
*
* @param mode_flag NC_UDF0 or NC_UDF1
* @param dispatch_table Pointer to dispatch table to use for this user format.
* @param magic_number Magic number used to identify file. Ignored if
* NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_EINVAL Invalid input.
* @author Ed Hartnett
* @ingroup datasets
*/
int
nc_def_user_format(int mode_flag, NC_Dispatch *dispatch_table, char *magic_number)
{
/* Check inputs. */
if (mode_flag != NC_UDF0 && mode_flag != NC_UDF1)
return NC_EINVAL;
if (!dispatch_table)
return NC_EINVAL;
if (magic_number && strlen(magic_number) > NC_MAX_MAGIC_NUMBER_LEN)
return NC_EINVAL;
/* Check the version of the dispatch table provided. */
if (dispatch_table->dispatch_version != NC_DISPATCH_VERSION)
return NC_EINVAL;
/* Retain a pointer to the dispatch_table and a copy of the magic
* number, if one was provided. */
switch(mode_flag)
{
case NC_UDF0:
UDF0_dispatch_table = dispatch_table;
if (magic_number)
strncpy(UDF0_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
break;
case NC_UDF1:
UDF1_dispatch_table = dispatch_table;
if (magic_number)
strncpy(UDF1_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
break;
}
return NC_NOERR;
}
/**
* Inquire about user-defined format.
*
* @param mode_flag NC_UDF0 or NC_UDF1
* @param dispatch_table Pointer that gets pointer to dispatch table
* to use for this user format, or NULL if this user-defined format is
* not defined. Ignored if NULL.
* @param magic_number Pointer that gets magic number used to identify
* file, if one has been set. Magic number will be of max size
* NC_MAX_MAGIC_NUMBER_LEN. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_EINVAL Invalid input.
* @author Ed Hartnett
* @ingroup datasets
*/
int
nc_inq_user_format(int mode_flag, NC_Dispatch **dispatch_table, char *magic_number)
{
/* Check inputs. */
if (mode_flag != NC_UDF0 && mode_flag != NC_UDF1)
return NC_EINVAL;
switch(mode_flag)
{
case NC_UDF0:
if (dispatch_table)
*dispatch_table = UDF0_dispatch_table;
if (magic_number)
strncpy(magic_number, UDF0_magic_number, NC_MAX_MAGIC_NUMBER_LEN);
break;
case NC_UDF1:
if (dispatch_table)
*dispatch_table = UDF1_dispatch_table;
if (magic_number)
strncpy(magic_number, UDF1_magic_number, NC_MAX_MAGIC_NUMBER_LEN);
break;
}
return NC_NOERR;
}
/** \ingroup datasets
Create a new netCDF file.
This function creates a new netCDF dataset, returning a netCDF ID that
can subsequently be used to refer to the netCDF dataset in other
netCDF function calls. The new netCDF dataset opened for write access
and placed in define mode, ready for you to add dimensions, variables,
and attributes.
\param path The file name of the new netCDF dataset.
\param cmode The creation mode flag. The following flags are available:
NC_CLOBBER (overwrite existing file),
NC_NOCLOBBER (do not overwrite existing file),
NC_SHARE (limit write caching - netcdf classic files only),
NC_64BIT_OFFSET (create 64-bit offset file),
NC_64BIT_DATA (alias NC_CDF5) (create CDF-5 file),
NC_NETCDF4 (create netCDF-4/HDF5 file),
NC_CLASSIC_MODEL (enforce netCDF classic mode on netCDF-4/HDF5 files),
NC_DISKLESS (store data in memory), and
NC_PERSIST (force the NC_DISKLESS data from memory to a file),
NC_MMAP (use MMAP for NC_DISKLESS instead of NC_INMEMORY -- deprecated).
See discussion below.
\param ncidp Pointer to location where returned netCDF ID is to be
stored.
<h2>The cmode Flag</h2>
The cmode flag is used to control the type of file created, and some
aspects of how it may be used.
Setting NC_NOCLOBBER means you do not want to clobber (overwrite) an
existing dataset; an error (NC_EEXIST) is returned if the specified
dataset already exists.
The NC_SHARE flag is appropriate when one process may be writing the
dataset and one or more other processes reading the dataset
concurrently; it means that dataset accesses are not buffered and
caching is limited. Since the buffering scheme is optimized for
sequential access, programs that do not access data sequentially may
see some performance improvement by setting the NC_SHARE flag. This
flag is ignored for netCDF-4 files.
Setting NC_64BIT_OFFSET causes netCDF to create a 64-bit offset format
file, instead of a netCDF classic format file. The 64-bit offset
format imposes far fewer restrictions on very large (i.e. over 2 GB)
data files. See Large File Support.
Setting NC_64BIT_DATA (alias NC_CDF5) causes netCDF to create a CDF-5
file format that supports large files (i.e. over 2GB) and large
variables (over 2B array elements.). See Large File Support.
A zero value (defined for convenience as NC_CLOBBER) specifies the
default behavior: overwrite any existing dataset with the same file
name and buffer and cache accesses for efficiency. The dataset will be
in netCDF classic format. See NetCDF Classic Format Limitations.
Setting NC_NETCDF4 causes netCDF to create a HDF5/NetCDF-4 file.
Setting NC_CLASSIC_MODEL causes netCDF to enforce the classic data
model in this file. (This only has effect for netCDF-4/HDF5 files, as
CDF-1, 2 and 5 files always use the classic model.) When
used with NC_NETCDF4, this flag ensures that the resulting
netCDF-4/HDF5 file may never contain any new constructs from the
enhanced data model. That is, it cannot contain groups, user defined
types, multiple unlimited dimensions, or new atomic types. The
advantage of this restriction is that such files are guaranteed to
work with existing netCDF software.
Setting NC_DISKLESS causes netCDF to create the file only in
memory and to optionally write the final contents to the
correspondingly named disk file. This allows for the use of
files that have no long term purpose. Operating on an existing file
in memory may also be faster. The decision on whether
or not to "persist" the memory contents to a disk file is
described in detail in the file docs/inmemory.md, which is
definitive. By default, closing a diskless fill will cause it's
contents to be lost.
If NC_DISKLESS is going to be used for creating a large classic
file, it behooves one to use nc__create and specify an
appropriately large value of the initialsz parameter to avoid to
many extensions to the in-memory space for the file. This flag
applies to files in classic format and to file in extended
format (netcdf-4).
Note that nc_create(path,cmode,ncidp) is equivalent to the invocation of
nc__create(path,cmode,NC_SIZEHINT_DEFAULT,NULL,ncidp).
\returns ::NC_NOERR No error.
\returns ::NC_EEXIST Specifying a file name of a file that exists and also specifying NC_NOCLOBBER.
\returns ::NC_EPERM Attempting to create a netCDF file in a directory where you do not have permission to create files.
\returns ::NC_ENOMEM System out of memory.
\returns ::NC_ENFILE Too many files open.
\returns ::NC_EHDFERR HDF5 error (netCDF-4 files only).
\returns ::NC_EFILEMETA Error writing netCDF-4 file-level metadata in
HDF5 file. (netCDF-4 files only).
\returns ::NC_EDISKLESS if there was an error in creating the
in-memory file.
\note When creating a netCDF-4 file HDF5 error reporting is turned
off, if it is on. This doesn't stop the HDF5 error stack from
recording the errors, it simply stops their display to the user
through stderr.
<h1>Examples</h1>
In this example we create a netCDF dataset named foo.nc; we want the
dataset to be created in the current directory only if a dataset with
that name does not already exist:
@code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
...
status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
In this example we create a netCDF dataset named foo_large.nc. It will
be in the 64-bit offset format.
@code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
...
status = nc_create("foo_large.nc", NC_NOCLOBBER|NC_64BIT_OFFSET, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
In this example we create a netCDF dataset named foo_HDF5.nc. It will
be in the HDF5 format.
@code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
...
status = nc_create("foo_HDF5.nc", NC_NOCLOBBER|NC_NETCDF4, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
In this example we create a netCDF dataset named
foo_HDF5_classic.nc. It will be in the HDF5 format, but will not allow
the use of any netCDF-4 advanced features. That is, it will conform to
the classic netCDF-3 data model.
@code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
...
status = nc_create("foo_HDF5_classic.nc", NC_NOCLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
In this example we create an in-memory netCDF classic dataset named
diskless.nc whose content will be lost when nc_close() is called.
@code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
...
status = nc_create("diskless.nc", NC_DISKLESS, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
In this example we create a in-memory netCDF classic dataset named
diskless.nc and specify that it should be made persistent
in a file named diskless.nc when nc_close() is called.
@code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
...
status = nc_create("diskless.nc", NC_DISKLESS|NC_PERSIST, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
A variant of nc_create(), nc__create() (note the double underscore) allows
users to specify two tuning parameters for the file that it is
creating. */
int
nc_create(const char *path, int cmode, int *ncidp)
{
return nc__create(path,cmode,NC_SIZEHINT_DEFAULT,NULL,ncidp);
}
/**
* Create a netCDF file with some extra parameters controlling classic
* file caching.
*
* Like nc_create(), this function creates a netCDF file.
*
* @param path The file name of the new netCDF dataset.
* @param cmode The creation mode flag, the same as in nc_create().
* @param initialsz On some systems, and with custom I/O layers, it
* may be advantageous to set the size of the output file at creation
* time. This parameter sets the initial size of the file at creation
* time. This only applies to classic CDF-1, 2, and 5 files. The
* special value NC_SIZEHINT_DEFAULT (which is the value 0), lets the
* netcdf library choose a suitable initial size.
* @param chunksizehintp A pointer to the chunk size hint, which
* controls a space versus time tradeoff, memory allocated in the
* netcdf library versus number of system calls. Because of internal
* requirements, the value may not be set to exactly the value
* requested. The actual value chosen is returned by reference. Using
* a NULL pointer or having the pointer point to the value
* NC_SIZEHINT_DEFAULT causes the library to choose a default. How the
* system chooses the default depends on the system. On many systems,
* the "preferred I/O block size" is available from the stat() system
* call, struct stat member st_blksize. If this is available it is
* used. Lacking that, twice the system pagesize is used. Lacking a
* call to discover the system pagesize, we just set default bufrsize
* to 8192. The bufrsize is a property of a given open netcdf
* descriptor ncid, it is not a persistent property of the netcdf
* dataset. This only applies to classic files.
* @param ncidp Pointer to location where returned netCDF ID is to be
* stored.
*
* @note This function uses the same return codes as the nc_create()
* function.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_ENOMEM System out of memory.
* @returns ::NC_EHDFERR HDF5 error (netCDF-4 files only).
* @returns ::NC_EFILEMETA Error writing netCDF-4 file-level metadata in
* HDF5 file. (netCDF-4 files only).
* @returns ::NC_EDISKLESS if there was an error in creating the
* in-memory file.
*
* <h1>Examples</h1>
*
* In this example we create a netCDF dataset named foo_large.nc; we
* want the dataset to be created in the current directory only if a
* dataset with that name does not already exist. We also specify that
* bufrsize and initial size for the file.
*
* @code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
int intialsz = 2048;
int *bufrsize;
...
*bufrsize = 1024;
status = nc__create("foo.nc", NC_NOCLOBBER, initialsz, bufrsize, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
*
* @ingroup datasets
* @author Glenn Davis
*/
int
nc__create(const char *path, int cmode, size_t initialsz,
size_t *chunksizehintp, int *ncidp)
{
return NC_create(path, cmode, initialsz, 0,
chunksizehintp, 0, NULL, ncidp);
}
/** \ingroup datasets
Create a netCDF file with the contents stored in memory.
\param path Must be non-null, but otherwise only used to set the dataset name.
\param mode the mode flags; Note that this procedure uses a limited set of flags because it forcibly sets NC_INMEMORY.
\param initialsize (advisory) size to allocate for the created file
\param ncidp Pointer to location where returned netCDF ID is to be
stored.
\returns ::NC_NOERR No error.
\returns ::NC_ENOMEM Out of memory.
\returns ::NC_EDISKLESS diskless io is not enabled for fails.
\returns ::NC_EINVAL, etc. other errors also returned by nc_open.
<h1>Examples</h1>
In this example we use nc_create_mem() to create a classic netCDF dataset
named foo.nc. The initial size is set to 4096.
@code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
int mode = 0;
size_t initialsize = 4096;
...
status = nc_create_mem("foo.nc", mode, initialsize, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
*/
int
nc_create_mem(const char* path, int mode, size_t initialsize, int* ncidp)
{
if(mode & NC_MMAP) return NC_EINVAL;
mode |= NC_INMEMORY; /* Specifically, do not set NC_DISKLESS */
return NC_create(path, mode, initialsize, 0, NULL, 0, NULL, ncidp);
}
/**
* @internal Create a file with special (deprecated) Cray settings.
*
* @deprecated This function was used in the old days with the Cray at
* NCAR. The Cray is long gone, and this call is supported only for
* backward compatibility. Use nc_create() instead.
*
* @param path File name.
* @param cmode Create mode.
* @param initialsz Initial size of metadata region for classic files,
* ignored for other files.
* @param basepe Deprecated parameter from the Cray days.
* @param chunksizehintp A pointer to the chunk size hint. This only
* applies to classic files.
* @param ncidp Pointer that gets ncid.
*
* @return ::NC_NOERR No error.
* @author Glenn Davis
*/
int
nc__create_mp(const char *path, int cmode, size_t initialsz,
int basepe, size_t *chunksizehintp, int *ncidp)
{
return NC_create(path, cmode, initialsz, basepe,
chunksizehintp, 0, NULL, ncidp);
}
/**
* Open an existing netCDF file.
*
* This function opens an existing netCDF dataset for access. It
* determines the underlying file format automatically. Use the same
* call to open a netCDF classic or netCDF-4 file.
*
* @param path File name for netCDF dataset to be opened. When the dataset
* is located on some remote server, then the path may be an OPeNDAP URL
* rather than a file path.
* @param omode The open mode flag may include NC_WRITE (for read/write
* access) and NC_SHARE (see below) and NC_DISKLESS (see below).
* @param ncidp Pointer to location where returned netCDF ID is to be
* stored.
*
* <h2>Open Mode</h2>
*
* A zero value (or ::NC_NOWRITE) specifies the default behavior: open
* the dataset with read-only access, buffering and caching accesses
* for efficiency.
*
* Otherwise, the open mode is ::NC_WRITE, ::NC_SHARE, or
* ::NC_WRITE|::NC_SHARE. Setting the ::NC_WRITE flag opens the
* dataset with read-write access. ("Writing" means any kind of change
* to the dataset, including appending or changing data, adding or
* renaming dimensions, variables, and attributes, or deleting
* attributes.)
*
* The NC_SHARE flag is only used for netCDF classic
* files. It is appropriate when one process may be writing the
* dataset and one or more other processes reading the dataset
* concurrently; it means that dataset accesses are not buffered and
* caching is limited. Since the buffering scheme is optimized for
* sequential access, programs that do not access data sequentially
* may see some performance improvement by setting the NC_SHARE flag.
*
* This procedure may also be invoked with the NC_DISKLESS flag set in
* the omode argument if the file to be opened is a classic format
* file. For nc_open(), this flag applies only to files in classic
* format. If the file is of type NC_NETCDF4, then the NC_DISKLESS
* flag will be ignored.
*
* If NC_DISKLESS is specified, then the whole file is read completely
* into memory. In effect this creates an in-memory cache of the file.
* If the omode flag also specifies NC_PERSIST, then the in-memory cache
* will be re-written to the disk file when nc_close() is called. For
* some kinds of manipulations, having the in-memory cache can speed
* up file processing. But in simple cases, non-cached processing may
* actually be faster than using cached processing. You will need to
* experiment to determine if the in-memory caching is worthwhile for
* your application.
*
* Normally, NC_DISKLESS allocates space in the heap for storing the
* in-memory file. If, however, the ./configure flags --enable-mmap is
* used, and the additional omode flag NC_MMAP is specified, then the
* file will be opened using the operating system MMAP facility. This
* flag only applies to files in classic format. Extended format
* (netcdf-4) files will ignore the NC_MMAP flag.
*
* In most cases, using MMAP provides no advantage for just
* NC_DISKLESS. The one case where using MMAP is an advantage is when
* a file is to be opened and only a small portion of its data is to
* be read and/or written. In this scenario, MMAP will cause only the
* accessed data to be retrieved from disk. Without MMAP, NC_DISKLESS
* will read the whole file into memory on nc_open. Thus, MMAP will
* provide some performance improvement in this case.
*
* It is not necessary to pass any information about the format of the
* file being opened. The file type will be detected automatically by
* the netCDF library.
*
* If a the path is a DAP URL, then the open mode is read-only.
* Setting NC_WRITE will be ignored.
*
* As of version 4.3.1.2, multiple calls to nc_open with the same
* path will return the same ncid value.
*
* @note When opening a netCDF-4 file HDF5 error reporting is turned
* off, if it is on. This doesn't stop the HDF5 error stack from
* recording the errors, it simply stops their display to the user
* through stderr.
*
* nc_open()returns the value NC_NOERR if no errors
* occurred. Otherwise, the returned status indicates an
* error. Possible causes of errors include:
*
* Note that nc_open(path,omode,ncidp) is equivalent to the invocation
* of nc__open(path,omode,NC_SIZEHINT_DEFAULT,NULL,ncidp).
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EPERM Attempting to create a netCDF file in a directory where you do not have permission to open files.
* @returns ::NC_ENFILE Too many files open
* @returns ::NC_ENOMEM Out of memory.
* @returns ::NC_EHDFERR HDF5 error. (NetCDF-4 files only.)
* @returns ::NC_EDIMMETA Error in netCDF-4 dimension metadata. (NetCDF-4 files only.)
*
* <h1>Examples</h1>
*
* Here is an example using nc_open()to open an existing netCDF dataset
* named foo.nc for read-only, non-shared access:
*
* @code
* #include <netcdf.h>
* ...
* int status = NC_NOERR;
* int ncid;
* ...
* status = nc_open("foo.nc", 0, &ncid);
* if (status != NC_NOERR) handle_error(status);
* @endcode
* @ingroup datasets
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
*/
int
nc_open(const char *path, int omode, int *ncidp)
{
return NC_open(path, omode, 0, NULL, 0, NULL, ncidp);
}
/** \ingroup datasets
Open a netCDF file with extra performance parameters for the classic
library.
\param path File name for netCDF dataset to be opened. When DAP
support is enabled, then the path may be an OPeNDAP URL rather than a
file path.
\param omode The open mode flag may include NC_WRITE (for read/write
access) and NC_SHARE as in nc_open().
\param chunksizehintp A size hint for the classic library. Only
applies to classic files. See below for more
information.
\param ncidp Pointer to location where returned netCDF ID is to be
stored.
<h1>The chunksizehintp Parameter</h1>
The argument referenced by bufrsizehintp controls a space versus time
tradeoff, memory allocated in the netcdf library versus number of
system calls.
Because of internal requirements, the value may not be set to exactly
the value requested. The actual value chosen is returned by reference.
Using a NULL pointer or having the pointer point to the value
NC_SIZEHINT_DEFAULT causes the library to choose a default.
How the system chooses the default depends on the system. On
many systems, the "preferred I/O block size" is available from the
stat() system call, struct stat member st_blksize. If this is
available it is used. Lacking that, twice the system pagesize is used.
Lacking a call to discover the system pagesize, we just set default
bufrsize to 8192.
The bufrsize is a property of a given open netcdf descriptor ncid, it
is not a persistent property of the netcdf dataset.
\returns ::NC_NOERR No error.
\returns ::NC_ENOMEM Out of memory.
\returns ::NC_EHDFERR HDF5 error. (NetCDF-4 files only.)
\returns ::NC_EDIMMETA Error in netCDF-4 dimension metadata. (NetCDF-4
files only.)
*/
int
nc__open(const char *path, int omode,
size_t *chunksizehintp, int *ncidp)
{
/* this API is for non-parallel access.
* Note nc_open_par() also calls NC_open().
*/
return NC_open(path, omode, 0, chunksizehintp, 0, NULL, ncidp);
}
/** \ingroup datasets
Open a netCDF file with the contents taken from a block of memory.
\param path Must be non-null, but otherwise only used to set the dataset name.
\param omode the open mode flags; Note that this procedure uses a limited set of flags because it forcibly sets NC_INMEMORY.
\param size The length of the block of memory being passed.
\param memory Pointer to the block of memory containing the contents
of a netcdf file.
\param ncidp Pointer to location where returned netCDF ID is to be
stored.
\returns ::NC_NOERR No error.
\returns ::NC_ENOMEM Out of memory.
\returns ::NC_EDISKLESS diskless io is not enabled for fails.
\returns ::NC_EINVAL, etc. other errors also returned by nc_open.
<h1>Examples</h1>
Here is an example using nc_open_mem() to open an existing netCDF dataset
named foo.nc for read-only, non-shared access. It differs from the nc_open()
example in that it assumes the contents of foo.nc have been read into memory.
@code
#include <netcdf.h>
#include <netcdf_mem.h>
...
int status = NC_NOERR;
int ncid;
size_t size;
void* memory;
...
size = <compute file size of foo.nc in bytes>;
memory = malloc(size);
...
status = nc_open_mem("foo.nc", 0, size, memory, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
*/
int
nc_open_mem(const char* path, int omode, size_t size, void* memory, int* ncidp)
{
NC_memio meminfo;
/* Sanity checks */
if(memory == NULL || size < MAGIC_NUMBER_LEN || path == NULL)
return NC_EINVAL;
if(omode & (NC_WRITE|NC_MMAP))
return NC_EINVAL;
omode |= (NC_INMEMORY); /* Note: NC_INMEMORY and NC_DISKLESS are mutually exclusive*/
meminfo.size = size;
meminfo.memory = memory;
meminfo.flags = NC_MEMIO_LOCKED;
return NC_open(path, omode, 0, NULL, 0, &meminfo, ncidp);
}
/** \ingroup datasets
Open a netCDF file with the contents taken from a block of memory.
Similar to nc_open_mem, but with parameters. Warning: if you do
specify that the provided memory is locked, then <b>never</b>
pass in non-heap allocated memory. Additionally, if not locked,
then do not assume that the memory returned by nc_close_mem
is the same as passed to nc_open_memio. You <b>must</b> check
before attempting to free the original memory.
\param path Must be non-null, but otherwise only used to set the dataset name.
\param omode the open mode flags; Note that this procedure uses a limited set of flags because it forcibly sets NC_INMEMORY.
\param params controlling parameters
\param ncidp Pointer to location where returned netCDF ID is to be
stored.
\returns ::NC_NOERR No error.
\returns ::NC_ENOMEM Out of memory.
\returns ::NC_EDISKLESS diskless io is not enabled for fails.
\returns ::NC_EINVAL, etc. other errors also returned by nc_open.
<h1>Examples</h1>
Here is an example using nc_open_memio() to open an existing netCDF dataset
named foo.nc for read-only, non-shared access. It differs from the nc_open_mem()
example in that it uses a parameter block.
@code
#include <netcdf.h>
#include <netcdf_mem.h>
...
int status = NC_NOERR;
int ncid;
NC_memio params;
...
params.size = <compute file size of foo.nc in bytes>;
params.memory = malloc(size);
params.flags = <see netcdf_mem.h>
...
status = nc_open_memio("foo.nc", 0, ¶ms, &ncid);
if (status != NC_NOERR) handle_error(status);
@endcode
*/
int
nc_open_memio(const char* path, int omode, NC_memio* params, int* ncidp)
{
/* Sanity checks */
if(path == NULL || params == NULL)
return NC_EINVAL;
if(params->memory == NULL || params->size < MAGIC_NUMBER_LEN)
return NC_EINVAL;
if(omode & NC_MMAP)
return NC_EINVAL;
omode |= (NC_INMEMORY);
return NC_open(path, omode, 0, NULL, 0, params, ncidp);
}
/**
* @internal Open a netCDF file with extra parameters for Cray.
*
* @deprecated This function was used in the old days with the Cray at
* NCAR. The Cray is long gone, and this call is supported only for
* backward compatibility. Use nc_open() instead.
*
* @param path The file name of the new netCDF dataset.
* @param omode Open mode.
* @param basepe Deprecated parameter from the Cray days.
* @param chunksizehintp A pointer to the chunk size hint. This only
* applies to classic files.
* @param ncidp Pointer to location where returned netCDF ID is to be
* stored.
*
* @return ::NC_NOERR
* @author Glenn Davis
*/
int
nc__open_mp(const char *path, int omode, int basepe,
size_t *chunksizehintp, int *ncidp)
{
return NC_open(path, omode, basepe, chunksizehintp, 0, NULL, ncidp);
}
/** \ingroup datasets
Get the file pathname (or the opendap URL) which was used to
open/create the ncid's file.
\param ncid NetCDF ID, from a previous call to nc_open() or
nc_create().
\param pathlen Pointer where length of path will be returned. Ignored
if NULL.
\param path Pointer where path name will be copied. Space must already
be allocated. Ignored if NULL.
\returns ::NC_NOERR No error.
\returns ::NC_EBADID Invalid ncid passed.
*/
int
nc_inq_path(int ncid, size_t *pathlen, char *path)
{
NC* ncp;
int stat = NC_NOERR;
if ((stat = NC_check_id(ncid, &ncp)))
return stat;
if(ncp->path == NULL) {
if(pathlen) *pathlen = 0;
if(path) path[0] = '\0';
} else {
if (pathlen) *pathlen = strlen(ncp->path);
if (path) strcpy(path, ncp->path);
}
return stat;
}
/** \ingroup datasets
Put open netcdf dataset into define mode
The function nc_redef puts an open netCDF dataset into define mode, so
dimensions, variables, and attributes can be added or renamed and
attributes can be deleted.
For netCDF-4 files (i.e. files created with NC_NETCDF4 in the cmode in
their call to nc_create()), it is not necessary to call nc_redef()
unless the file was also created with NC_STRICT_NC3. For straight-up
netCDF-4 files, nc_redef() is called automatically, as needed.
For all netCDF-4 files, the root ncid must be used. This is the ncid
returned by nc_open() and nc_create(), and points to the root of the
hierarchy tree for netCDF-4 files.
\param ncid NetCDF ID, from a previous call to nc_open() or
nc_create().
\returns ::NC_NOERR No error.
\returns ::NC_EBADID Bad ncid.
\returns ::NC_EBADGRPID The ncid must refer to the root group of the
file, that is, the group returned by nc_open() or nc_create().
\returns ::NC_EINDEFINE Already in define mode.
\returns ::NC_EPERM File is read-only.
<h1>Example</h1>
Here is an example using nc_redef to open an existing netCDF dataset
named foo.nc and put it into define mode:
\code
#include <netcdf.h>
...
int status = NC_NOERR;
int ncid;
...
status = nc_open("foo.nc", NC_WRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
...
status = nc_redef(ncid);
if (status != NC_NOERR) handle_error(status);
\endcode
*/
int
nc_redef(int ncid)
{
NC* ncp;
int stat = NC_check_id(ncid, &ncp);
if(stat != NC_NOERR) return stat;
return ncp->dispatch->redef(ncid);
}
/** \ingroup datasets
Leave define mode
The function nc_enddef() takes an open netCDF dataset out of define
mode. The changes made to the netCDF dataset while it was in define
mode are checked and committed to disk if no problems
occurred. Non-record variables may be initialized to a "fill value" as
well with nc_set_fill(). The netCDF dataset is then placed in data
mode, so variable data can be read or written.
It's not necessary to call nc_enddef() for netCDF-4 files. With netCDF-4
files, nc_enddef() is called when needed by the netcdf-4 library. User
calls to nc_enddef() for netCDF-4 files still flush the metadata to
disk.
This call may involve copying data under some circumstances. For a
more extensive discussion see File Structure and Performance.
For netCDF-4/HDF5 format files there are some variable settings (the
compression, endianness, fletcher32 error correction, and fill value)
which must be set (if they are going to be set at all) between the
nc_def_var() and the next nc_enddef(). Once the nc_enddef() is called,
these settings can no longer be changed for a variable.
\param ncid NetCDF ID, from a previous call to nc_open() or
nc_create().
If you use a group id (in a netCDF-4/HDF5 file), the enddef
will apply to the entire file. That means the enddef will not just end
define mode in one group, but in the entire file.
\returns ::NC_NOERR no error
\returns ::NC_EBADID Invalid ncid passed.
<h1>Example</h1>
Here is an example using nc_enddef() to finish the definitions of a new
netCDF dataset named foo.nc and put it into data mode: