forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfile.c
1996 lines (1636 loc) · 62.4 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 dfile.c
File create and open functions
These functions end up calling functions in one of the dispatch layers
(netCDF-4, dap server, etc).
Copyright 2010 University Corporation for Atmospheric
Research/Unidata. See COPYRIGHT file for more info.
*/
#include "config.h"
#include <stdlib.h>
#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
#include "ncdispatch.h"
#include "netcdf_mem.h"
#include "ncwinpath.h"
extern int NC_initialized;
extern int NC_finalized;
/** \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.
*/
/*!
Interpret the magic number found in the header of a netCDF file.
This function interprets the magic number/string contained in the header of a netCDF file and sets the appropriate NC_FORMATX flags.
@param[in] magic Pointer to a character array with the magic number block.
@param[out] model Pointer to an integer to hold the corresponding netCDF type.
@param[out] version Pointer to an integer to hold the corresponding netCDF version.
@param[in] use_parallel 1 if using parallel, 0 if not.
@return Returns an error code or 0 on success.
\internal
\ingroup datasets
*/
static int
NC_interpret_magic_number(char* magic, int* model, int* version, int use_parallel)
{
int status = NC_NOERR;
/* Look at the magic number */
/* Ignore the first byte for HDF */
#ifdef USE_NETCDF4
if(magic[1] == 'H' && magic[2] == 'D' && magic[3] == 'F') {
*model = NC_FORMATX_NC4;
*version = 5;
#ifdef USE_HDF4
} else if(magic[0] == '\016' && magic[1] == '\003'
&& magic[2] == '\023' && magic[3] == '\001') {
*model = NC_FORMATX_NC4;
*version = 4;
#endif
} else
#endif
if(magic[0] == 'C' && magic[1] == 'D' && magic[2] == 'F') {
if(magic[3] == '\001') {
*version = 1; /* netcdf classic version 1 */
*model = NC_FORMATX_NC3;
} else if(magic[3] == '\002') {
*version = 2; /* netcdf classic version 2 */
*model = NC_FORMATX_NC3;
} else if(magic[3] == '\005') {
*version = 5; /* cdf5 (including pnetcdf) file */
*model = NC_FORMATX_NC3;
} else
{status = NC_ENOTNC; goto done;}
} else
{status = NC_ENOTNC; goto done;}
done:
return status;
}
/*!
Given an existing file, figure out its format
and return that format value (NC_FORMATX_XXX)
in model arg.
*/
static int NC_check_file_type(const char *path, int flags, void *parameters,
int* model, int* version)
{
char magic[MAGIC_NUMBER_LEN];
int status = NC_NOERR;
int diskless = ((flags & NC_DISKLESS) == NC_DISKLESS);
int use_parallel = ((flags & NC_MPIIO) == NC_MPIIO);
int inmemory = (diskless && ((flags & NC_INMEMORY) == NC_INMEMORY));
*model = 0;
if(inmemory) {
NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
if(meminfo == NULL || meminfo->size < MAGIC_NUMBER_LEN)
{status = NC_EDISKLESS; goto done;}
memcpy(magic,meminfo->memory,MAGIC_NUMBER_LEN);
} else {/* presumably a real file */
/* Get the 4-byte magic from the beginning of the file. Don't use posix
* for parallel, use the MPI functions instead. */
#ifdef USE_PARALLEL
if (use_parallel) {
MPI_File fh;
MPI_Status mstatus;
int retval;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Info info = MPI_INFO_NULL;
if(parameters != NULL) {
comm = ((NC_MPI_INFO*)parameters)->comm;
info = ((NC_MPI_INFO*)parameters)->info;
}
if((retval = MPI_File_open(comm,(char*)path,MPI_MODE_RDONLY,info,
&fh)) != MPI_SUCCESS)
{status = NC_EPARINIT; goto done;}
if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
&mstatus)) != MPI_SUCCESS)
{status = NC_EPARINIT; goto done;}
if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
{status = NC_EPARINIT; goto done;}
} else
#endif /* USE_PARALLEL */
{
FILE *fp;
size_t i;
#ifdef HAVE_FILE_LENGTH_I64
__int64 file_len = 0;
#endif
if(path == NULL || strlen(path)==0)
{status = NC_EINVAL; goto done;}
if (!(fp = fopen(path, "r")))
{status = errno; goto done;}
#ifdef HAVE_SYS_STAT_H
/* The file must be at least MAGIC_NUMBER_LEN in size,
or otherwise the following fread will exhibit unexpected
behavior. */
/* Windows and fstat have some issues, this will work around that. */
#ifdef HAVE_FILE_LENGTH_I64
if((file_len = _filelengthi64(fileno(fp))) < 0) {
fclose(fp);
status = errno;
goto done;
}
if(file_len < MAGIC_NUMBER_LEN) {
fclose(fp);
status = NC_ENOTNC;
goto done;
}
else
{ int fno = fileno(fp);
if(!(fstat(fno,&st) == 0)) {
fclose(fp);
status = errno;
goto done;
}
if(st.st_size < MAGIC_NUMBER_LEN) {
fclose(fp);
status = NC_ENOTNC;
goto done;
}
}
#endif //HAVE_FILE_LENGTH_I64
#endif //HAVE_SYS_STAT_H
i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
fclose(fp);
if(i == 0)
{status = NC_ENOTNC; goto done;}
if(i != 1)
{status = errno; goto done;}
}
} /* !inmemory */
/* Look at the magic number */
status = NC_interpret_magic_number(magic,model,version,use_parallel);
done:
return status;
}
/** \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_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 only in memory),
NC_MMAP (use MMAP for NC_DISKLESS),
and NC_WRITE.
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.
Note that the flag NC_PNETCDF also exists as the combination of
NC_CDF5 or'd with NC_MPIIO to indicate that the pnetcdf library
should be used.
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
classic and 64-bit offset 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.
This allows for the use of files that have no long term purpose. Note that
with one exception, the in-memory file is destroyed upon calling
nc_close. If, however, the flag combination (NC_DISKLESS|NC_WRITE)
is used, then at close, the contents of the memory file will be
made persistent in the file path that was specified in the nc_create
call. If NC_DISKLESS is going to be used for creating a large classic file,
it behooves one to use either nc__create or nc_create_mp 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).
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 mode flag
NC_MMAP is specified, then the file will be created 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.
Using NC_MMAP for nc_create is
only included for completeness vis-a-vis nc_open. The
ability to use MMAP is of limited use for nc_create because
nc_create is going to create the file in memory anyway.
Closing a MMAP'd file will be slightly faster, but not significantly.
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_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.
\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 a 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_WRITE, &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 cacheing.
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 and 64-bit offset 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 and 64-bit offset 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.
<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
*/
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);
}
/**
\internal
\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.
*/
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);
}
/** \ingroup datasets
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, 64-bit offset, or netCDF-4 file.
\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 mode The 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 and 64-bit offset
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 mode 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 mode flag also specifies NC_WRITE, 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 mode 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,cmode,ncidp) is equivalent to the invocation of
nc__open(path,cmode,NC_SIZEHINT_DEFAULT,NULL,ncidp).
\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.)
<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
*/
int
nc_open(const char *path, int mode, int *ncidp)
{
return NC_open(path, mode, 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 mode The 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 and 64-bit offset 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 mode,
size_t *chunksizehintp, int *ncidp)
{
/* this API is for non-parallel access: TODO check for illegal cmode
* flags, such as NC_PNETCDF, NC_MPIIO, or NC_MPIPOSIX, before entering
* NC_open()? Note nc_open_par() also calls NC_open().
*/
return NC_open(path, mode, 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 mode the mode flags; Note that this procedure uses a limited set of flags because it forcibly sets NC_NOWRITE|NC_DISKLESS|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 mode, size_t size, void* memory, int* ncidp)
{
#ifdef USE_DISKLESS
NC_MEM_INFO meminfo;
/* Sanity checks */
if(memory == NULL || size < MAGIC_NUMBER_LEN || path == NULL)
return NC_EINVAL;
if(mode & (NC_WRITE|NC_MPIIO|NC_MPIPOSIX|NC_MMAP))
return NC_EINVAL;
mode |= (NC_INMEMORY|NC_DISKLESS);
meminfo.size = size;
meminfo.memory = memory;
return NC_open(path, mode, 0, NULL, 0, &meminfo, ncidp);
#else
return NC_EDISKLESS;
#endif
}
/**
\internal
\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.
*/
int
nc__open_mp(const char *path, int mode, int basepe,
size_t *chunksizehintp, int *ncidp)
{
return NC_open(path, mode, 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:
\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);
... create dimensions, variables, attributes
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
\endcode
*/
int
nc_enddef(int ncid)
{
int status = NC_NOERR;
NC *ncp;
status = NC_check_id(ncid, &ncp);
if(status != NC_NOERR) return status;
return ncp->dispatch->_enddef(ncid,0,1,0,1);
}
/** \ingroup datasets
Leave define mode with performance tuning
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.
This call may involve copying data under some circumstances. For a
more extensive discussion see File Structure and Performance.
\warning This function exposes internals of the netcdf version 1 file
format. Users should use nc_enddef() in most circumstances. This
function may not be available on future netcdf implementations.
The classic netcdf file format has three sections, the "header"
section, the data section for fixed size variables, and the data
section for variables which have an unlimited dimension (record
variables).
The header begins at the beginning of the file. The index (offset) of
the beginning of the other two sections is contained in the
header. Typically, there is no space between the sections. This causes
copying overhead to accrue if one wishes to change the size of the
sections, as may happen when changing names of things, text attribute
values, adding attributes or adding variables. Also, for buffered i/o,
there may be advantages to aligning sections in certain ways.
The minfree parameters allow one to control costs of future calls to
nc_redef, nc_enddef() by requesting that minfree bytes be available at
the end of the section.
The align parameters allow one to set the alignment of the beginning
of the corresponding sections. The beginning of the section is rounded
up to an index which is a multiple of the align parameter. The flag
value ALIGN_CHUNK tells the library to use the bufrsize (see above) as
the align parameter. It has nothing to do with the chunking
(multidimensional tiling) features of netCDF-4.
The file format requires mod 4 alignment, so the align parameters are
silently rounded up to multiples of 4. The usual call,
\code
nc_enddef(ncid);
\endcode
is equivalent to
\code