-
Notifications
You must be signed in to change notification settings - Fork 259
/
nc4file.c
3392 lines (2961 loc) · 96.4 KB
/
nc4file.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
The netCDF-4 file functions.
This file is part of netcdf-4, a netCDF-like interface for HDF5, or
a HDF5 backend for netCDF, depending on your point of view.
Copyright 2003, University Corporation for Atmospheric Research. See
COPYRIGHT file for copying and redistribution conditions.
*/
#include "config.h"
#include <errno.h> /* netcdf functions sometimes return system errors */
#include "nc.h"
#include "nc4internal.h"
#include "nc4dispatch.h"
#include <H5DSpublic.h>
#ifdef USE_HDF4
#include <mfhdf.h>
#endif
#if 0 /*def USE_PNETCDF*/
#include <pnetcdf.h>
#endif
/* This is to track opened HDF5 objects to make sure they are
* closed. */
#ifdef EXTRA_TESTS
extern int num_plists;
extern int num_spaces;
#endif /* EXTRA_TESTS */
#define MIN_DEFLATE_LEVEL 0
#define MAX_DEFLATE_LEVEL 9
/* These are the special attributes added by the HDF5 dimension scale
* API. They will be ignored by netCDF-4. */
#define REFERENCE_LIST "REFERENCE_LIST"
#define CLASS "CLASS"
#define DIMENSION_LIST "DIMENSION_LIST"
#define NAME "NAME"
/* Forward */
static int NC4_enddef(int ncid);
static int nc4_rec_read_types(NC_GRP_INFO_T *grp);
static int nc4_rec_read_vars(NC_GRP_INFO_T *grp);
static int close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5, int abort);
#ifdef IGNORE
/* This extern points to the pointer that holds the list of open
* netCDF files. */
extern NC *nc_file;
#endif
/* These are the default chunk cache sizes for HDF5 files created or
* opened with netCDF-4. */
size_t nc4_chunk_cache_size = CHUNK_CACHE_SIZE;
size_t nc4_chunk_cache_nelems = CHUNK_CACHE_NELEMS;
float nc4_chunk_cache_preemption = CHUNK_CACHE_PREEMPTION;
/* To turn off HDF5 error messages, I have to catch an early
invocation of a netcdf function. */
static int virgin = 1;
/* For performance, fill this array only the first time, and keep it
* in global memory for each further use. */
#define NUM_TYPES 12
static hid_t native_type_constant[NUM_TYPES];
static char nc_type_name[NUM_TYPES][NC_MAX_NAME + 1] = {"char", "byte", "short", "int", "float",
"double", "ubyte", "ushort", "uint",
"int64", "uint64", "string"};
int nc4_free_global_hdf_string_typeid();
/* Set chunk cache size. Only affects files opened/created *after* it
* is called. */
int
nc_set_chunk_cache(size_t size, size_t nelems, float preemption)
{
if (preemption < 0 || preemption > 1)
return NC_EINVAL;
nc4_chunk_cache_size = size;
nc4_chunk_cache_nelems = nelems;
nc4_chunk_cache_preemption = preemption;
return NC_NOERR;
}
/* Get chunk cache size. Only affects files opened/created *after* it
* is called. */
int
nc_get_chunk_cache(size_t *sizep, size_t *nelemsp, float *preemptionp)
{
if (sizep)
*sizep = nc4_chunk_cache_size;
if (nelemsp)
*nelemsp = nc4_chunk_cache_nelems;
if (preemptionp)
*preemptionp = nc4_chunk_cache_preemption;
return NC_NOERR;
}
/* Required for fortran to avoid size_t issues. */
int
nc_set_chunk_cache_ints(int size, int nelems, int preemption)
{
if (size <= 0 || nelems <= 0 || preemption < 0 || preemption > 100)
return NC_EINVAL;
nc4_chunk_cache_size = size;
nc4_chunk_cache_nelems = nelems;
nc4_chunk_cache_preemption = (float)preemption / 100;
return NC_NOERR;
}
int
nc_get_chunk_cache_ints(int *sizep, int *nelemsp, int *preemptionp)
{
if (sizep)
*sizep = (int)nc4_chunk_cache_size;
if (nelemsp)
*nelemsp = (int)nc4_chunk_cache_nelems;
if (preemptionp)
*preemptionp = (int)(nc4_chunk_cache_preemption * 100);
return NC_NOERR;
}
/* This will return the length of a netcdf data type in bytes. */
int
nc4typelen(nc_type type)
{
switch(type){
case NC_BYTE:
case NC_CHAR:
case NC_UBYTE:
return 1;
case NC_USHORT:
case NC_SHORT:
return 2;
case NC_FLOAT:
case NC_INT:
case NC_UINT:
return 4;
case NC_DOUBLE:
case NC_INT64:
case NC_UINT64:
return 8;
}
return -1;
}
/* Given a filename, check to see if it is a HDF5 file. */
#define MAGIC_NUMBER_LEN 4
#define NC_HDF5_FILE 1
#define NC_HDF4_FILE 2
static int
nc_check_for_hdf(const char *path, int use_parallel, MPI_Comm comm, MPI_Info info,
int *hdf_file)
{
char blob[MAGIC_NUMBER_LEN];
assert(hdf_file && path);
LOG((3, "nc_check_for_hdf: path %s", path));
/* HDF5 function handles possible user block at beginning of file */
if(H5Fis_hdf5(path))
{
*hdf_file = NC_HDF5_FILE;
} else {
/* Get the 4-byte blob 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 status;
int retval;
if ((retval = MPI_File_open(comm, (char *)path, MPI_MODE_RDONLY,
info, &fh)) != MPI_SUCCESS)
return NC_EPARINIT;
if ((retval = MPI_File_read(fh, blob, MAGIC_NUMBER_LEN, MPI_CHAR,
&status)) != MPI_SUCCESS)
return NC_EPARINIT;
if ((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
return NC_EPARINIT;
}
else
#endif /* USE_PARALLEL */
{
FILE *fp;
if (!(fp = fopen(path, "r")) ||
fread(blob, MAGIC_NUMBER_LEN, 1, fp) != 1) {
if(fp) fclose(fp);
return errno;
}
fclose(fp);
}
/* Check for HDF4. */
if (!strncmp(blob, "\016\003\023\001", MAGIC_NUMBER_LEN))
*hdf_file = NC_HDF4_FILE;
else
*hdf_file = 0;
}
return NC_NOERR;
}
/* Create a HDF5/netcdf-4 file. */
static int
nc4_create_file(const char *path, int cmode, MPI_Comm comm, MPI_Info info,
NC *nc)
{
hid_t fcpl_id, fapl_id = -1;
unsigned flags;
FILE *fp;
int retval = NC_NOERR;
NC_HDF5_FILE_INFO_T* nc4_info = NULL;
#ifdef USE_PARALLEL
int comm_duped = 0; /* Whether the MPI Communicator was duplicated */
int info_duped = 0; /* Whether the MPI Info object was duplicated */
#else /* !USE_PARALLEL */
int persist = 0; /* Should diskless try to persist its data into file?*/
#endif
assert(nc);
if(cmode & NC_DISKLESS)
flags = H5F_ACC_TRUNC;
else if(cmode & NC_NOCLOBBER)
flags = H5F_ACC_EXCL;
else
flags = H5F_ACC_TRUNC;
LOG((3, "nc4_create_file: path %s mode 0x%x", path, cmode));
assert(nc && path);
/* If this file already exists, and NC_NOCLOBBER is specified,
return an error. */
if (cmode & NC_DISKLESS) {
#ifndef USE_PARALLEL
if(cmode & NC_WRITE)
persist = 1;
#endif
} else if ((cmode & NC_NOCLOBBER) && (fp = fopen(path, "r"))) {
fclose(fp);
return NC_EEXIST;
}
/* Add necessary structs to hold netcdf-4 file data. */
if ((retval = nc4_nc4f_list_add(nc, path, (NC_WRITE | cmode))))
BAIL(retval);
nc4_info = NC4_DATA(nc);
assert(nc4_info && nc4_info->root_grp);
#if 0 /*def USE_PNETCDF*/
if (cmode & NC_PNETCDF)
return NC_NOERR;
#endif
/* Need this access plist to control how HDF5 handles open onjects
* on file close. (Setting H5F_CLOSE_SEMI will cause H5Fclose to
* fail if there are any open objects in the file. */
if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
BAIL(NC_EHDFERR);
#ifdef EXTRA_TESTS
num_plists++;
#endif
#ifdef EXTRA_TESTS
if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
BAIL(NC_EHDFERR);
#else
if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
BAIL(NC_EHDFERR);
#endif /* EXTRA_TESTS */
#ifdef USE_PARALLEL
/* If this is a parallel file create, set up the file creation
property list. */
if ((cmode & NC_MPIIO) || (cmode & NC_MPIPOSIX))
{
nc4_info->parallel++;
if (cmode & NC_MPIIO) /* MPI/IO */
{
LOG((4, "creating parallel file with MPI/IO"));
if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
BAIL(NC_EPARINIT);
}
else /* MPI/POSIX */
{
LOG((4, "creating parallel file with MPI/posix"));
if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
BAIL(NC_EPARINIT);
}
/* Keep copies of the MPI Comm & Info objects */
if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
BAIL(NC_EMPI);
comm_duped++;
if (MPI_INFO_NULL != info)
{
if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
BAIL(NC_EMPI);
info_duped++;
}
else
{
/* No dup, just copy it. */
nc4_info->info = info;
}
}
#else /* only set cache for non-parallel... */
if(cmode & NC_DISKLESS) {
if (H5Pset_fapl_core(fapl_id, 4096, persist))
BAIL(NC_EDISKLESS);
}
if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
nc4_chunk_cache_preemption) < 0)
BAIL(NC_EHDFERR);
LOG((4, "nc4_create_file: set HDF raw chunk cache to size %d nelems %d preemption %f",
nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
#endif /* USE_PARALLEL */
if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
BAIL(NC_EHDFERR);
/* Create the property list. */
if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0)
BAIL(NC_EHDFERR);
#ifdef EXTRA_TESTS
num_plists++;
#endif
/* Set latest_format in access propertly list and
* H5P_CRT_ORDER_TRACKED in the creation property list. This turns
* on HDF5 creation ordering. */
if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
H5P_CRT_ORDER_INDEXED)) < 0)
BAIL(NC_EHDFERR);
if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
H5P_CRT_ORDER_INDEXED)) < 0)
BAIL(NC_EHDFERR);
/* Create the file. */
if ((nc4_info->hdfid = H5Fcreate(path, flags, fcpl_id, fapl_id)) < 0)
/*Change the return error from NC_EFILEMETADATA to
System error EACCES because that is the more likely problem */
BAIL(EACCES);
/* Open the root group. */
if ((nc4_info->root_grp->hdf_grpid = H5Gopen2(nc4_info->hdfid, "/",
H5P_DEFAULT)) < 0)
BAIL(NC_EFILEMETA);
/* Release the property lists. */
if (H5Pclose(fapl_id) < 0 ||
H5Pclose(fcpl_id) < 0)
BAIL(NC_EHDFERR);
#ifdef EXTRA_TESTS
num_plists--;
num_plists--;
#endif
/* Define mode gets turned on automatically on create. */
nc4_info->flags |= NC_INDEF;
return NC_NOERR;
exit: /*failure exit*/
#ifdef USE_PARALLEL
if (comm_duped) MPI_Comm_free(&nc4_info->comm);
if (info_duped) MPI_Info_free(&nc4_info->info);
#endif
#ifdef EXTRA_TESTS
num_plists--;
#endif
if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
if(!nc4_info) return retval;
close_netcdf4_file(nc4_info,1); /* treat like abort */
#if 0
if (nc4_info->hdfid > 0) H5Fclose(nc4_info->hdfid);
if (nc4_info->root_grp) {
free(nc4_info->root_grp->name);
free(nc4_info->root_grp);
}
free(nc4_info);
#endif
return retval;
}
/** \ingroup netcdf4
Create a netCDF-4/HDF5 file.
\param path The file name of the new file.
\param cmode The creation mode flag.
\param initialsz Ignored by this function.
\param basepe Ignored by this function.
\param chunksizehintp Ignored by this function.
\param use_parallel 0 for sequential, non-zero for parallel I/O.
\param mpidata pointer to struct holdind data for parallel I/O
layer. Ignored if NULL.
\param dispatch Pointer to the dispatch table for this file.
\param nc_file Pointer to an instance of NC.
\return NC_INVAL Invalid input (check cmode).
*/
int
NC4_create(const char* path, int cmode, size_t initialsz, int basepe,
size_t *chunksizehintp, int use_parallel, void *mpidata,
NC_Dispatch *dispatch, NC* nc_file)
{
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Info info = MPI_INFO_NULL;
int res;
assert(nc_file && path);
LOG((1, "nc4_create_file: path %s cmode 0x%x comm %d info %d",
path, cmode, comm, info));
#ifdef USE_PARALLEL
if (mpidata)
{
comm = ((NC_MPI_INFO *)mpidata)->comm;
info = ((NC_MPI_INFO *)mpidata)->info;
}
#endif /* USE_PARALLEL */
/* If this is our first file, turn off HDF5 error messages. */
if (virgin)
{
if (H5Eset_auto(NULL, NULL) < 0)
LOG((0, "Couldn't turn off HDF5 error messages!"));
LOG((1, "HDF5 error messages have been turned off."));
virgin = 0;
}
/* Check the cmode for validity. */
if (cmode & ~(NC_NOCLOBBER | NC_64BIT_OFFSET
| NC_NETCDF4 | NC_CLASSIC_MODEL
| NC_SHARE | NC_MPIIO | NC_MPIPOSIX | NC_LOCK | NC_PNETCDF
| NC_DISKLESS
| NC_WRITE /* to support diskless persistence */
)
|| (cmode & NC_MPIIO && cmode & NC_MPIPOSIX)
|| (cmode & NC_64BIT_OFFSET && cmode & NC_NETCDF4)
|| (cmode & (NC_MPIIO | NC_MPIPOSIX) && cmode & NC_DISKLESS)
)
return NC_EINVAL;
cmode |= NC_NETCDF4;
/* Apply default create format. */
if (nc_get_default_format() == NC_FORMAT_64BIT)
cmode |= NC_64BIT_OFFSET;
else if (nc_get_default_format() == NC_FORMAT_NETCDF4_CLASSIC)
cmode |= NC_CLASSIC_MODEL;
LOG((2, "cmode after applying default format: 0x%x", cmode));
nc_file->int_ncid = nc_file->ext_ncid;
res = nc4_create_file(path, cmode, comm, info, nc_file);
#if 0 /*def USE_PNETCDF*/
if (cmode & NC_PNETCDF)
{
NC_HDF5_FILE_INFO_T* nc4_info;
nc4_info = NC4_DATA(nc_file);
assert(nc4_info);
nc4_info->pnetcdf_file++;
res = ncmpi_create(comm, path, cmode, info, &(nc_file->int_ncid));
}
#endif /* USE_PNETCDF */
return res;
}
/* This function is called by read_dataset when a dimension scale
* dataset is encountered. It reads in the dimension data (creating a
* new NC_DIM_INFO_T object), and also checks to see if this is a
* dimension without a variable - that is, a coordinate dimension
* which does not have any coordinate data. */
static int
read_scale(NC_GRP_INFO_T *grp, hid_t datasetid, const char *obj_name,
hsize_t scale_size, hsize_t max_scale_size,
int *dim_without_var)
{
/*char *start_of_len;*/
char dimscale_name_att[NC_MAX_NAME + 1] = "";
hid_t attid = 0;
int max_len;
int retval;
/* Add a dimension for this scale. */
if ((retval = nc4_dim_list_add(&grp->dim)))
return retval;
/* Assign dimid and increment number of dimensions. */
grp->dim->dimid = grp->nc4_info->next_dimid++;
grp->ndims++;
/* Does this dataset have a hidden attribute that tells us its
* dimid? If so, read it. */
H5E_BEGIN_TRY {
if ((attid = H5Aopen_by_name(datasetid, ".", NC_DIMID_ATT_NAME,
H5P_DEFAULT, H5P_DEFAULT)) > 0)
{
if (H5Aread(attid, H5T_NATIVE_INT, &grp->dim->dimid) < 0)
return NC_EHDFERR;
if (H5Aclose(attid) < 0)
return NC_EHDFERR;
}
} H5E_END_TRY;
max_len = strlen(obj_name) > NC_MAX_NAME ? NC_MAX_NAME : strlen(obj_name);
if (!(grp->dim->name = malloc((max_len + 1) * sizeof(char))))
return NC_ENOMEM;
strncpy(grp->dim->name, obj_name, max_len + 1);
if (SIZEOF_SIZE_T < 8 && scale_size > NC_MAX_UINT)
{
grp->dim->len = NC_MAX_UINT;
grp->dim->too_long = 1;
}
else
grp->dim->len = scale_size;
grp->dim->hdf_dimscaleid = datasetid;
/* If the dimscale has an unlimited dimension, then this dimension
* is unlimited. */
if (max_scale_size == H5S_UNLIMITED)
grp->dim->unlimited++;
/* If the scale name is set to DIM_WITHOUT_VARIABLE, then this is a
* dimension, but not a variable. (If get_scale_name returns an
* error, just move on, there's no NAME.) */
if (H5DSget_scale_name(datasetid, dimscale_name_att, NC_MAX_NAME) >= 0)
{
if (!strncmp(dimscale_name_att, DIM_WITHOUT_VARIABLE,
strlen(DIM_WITHOUT_VARIABLE)))
{
if (grp->dim->unlimited)
{
size_t len = 0, *lenp = &len;
if ((retval = nc4_find_dim_len(grp, grp->dim->dimid, &lenp)))
return retval;
grp->dim->len = *lenp;
}
(*dim_without_var)++;
}
}
return NC_NOERR;
}
/* This function reads the hacked in coordinates attribute I use for
* multi-dimensional coordinates. */
static int
read_coord_dimids(NC_VAR_INFO_T *var)
{
hid_t coord_att_typeid = -1, coord_attid = -1, spaceid = -1;
int ret = 0;
/* There is a hidden attribute telling us the ids of the
* dimensions that apply to this multi-dimensional coordinate
* variable. Read it. */
if ((coord_attid = H5Aopen_name(var->hdf_datasetid, COORDINATES)) < 0) ret++;
if (!ret && (coord_att_typeid = H5Aget_type(coord_attid)) < 0) ret++;
if (!ret && H5Aread(coord_attid, coord_att_typeid, var->dimids) < 0) ret++;
LOG((4, "dimscale %s is multidimensional and has coords", var->name));
/* How many dimensions are there? */
if ((spaceid = H5Aget_space(coord_attid)) < 0) ret++;
#ifdef EXTRA_TESTS
num_spaces++;
#endif
if (H5Sget_simple_extent_npoints(spaceid) < 0) ret++;
/* Set my HDF5 IDs free! */
if (spaceid >= 0 && H5Sclose(spaceid) < 0) ret++;
#ifdef EXTRA_TESTS
num_spaces--;
#endif
if (coord_att_typeid >= 0 && H5Tclose(coord_att_typeid) < 0) ret++;
if (coord_attid >= 0 && H5Aclose(coord_attid) < 0) ret++;
return ret ? NC_EATTMETA : NC_NOERR;
}
/* This function is called when reading a file's metadata for each
* dimension scale attached to a variable.*/
static herr_t
dimscale_visitor(hid_t did, unsigned dim, hid_t dsid,
void *dimscale_hdf5_objids)
{
H5G_stat_t statbuf;
/* Get more info on the dimscale object.*/
if (H5Gget_objinfo(dsid, ".", 1, &statbuf) < 0)
return -1;
/* Pass this information back to caller. */
/* (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno = statbuf.fileno;
(*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno = statbuf.objno;*/
(*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[0] = statbuf.fileno[0];
(*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[1] = statbuf.fileno[1];
(*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[0] = statbuf.objno[0];
(*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[1] = statbuf.objno[1];
return 0;
}
/* Given an HDF5 type, set a pointer to netcdf type. */
static int
get_netcdf_type(NC_HDF5_FILE_INFO_T *h5, hid_t native_typeid,
nc_type *xtype)
{
NC_TYPE_INFO_T *type;
hid_t class;
htri_t is_str, equal = 0;
assert(h5 && xtype);
if ((class = H5Tget_class(native_typeid)) < 0)
return NC_EHDFERR;
/* H5Tequal doesn't work with H5T_C_S1 for some reason. But
* H5Tget_class will return H5T_STRING if this is a string. */
if (class == H5T_STRING)
{
if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
return NC_EHDFERR;
if (is_str)
*xtype = NC_STRING;
else
*xtype = NC_CHAR;
return NC_NOERR;
}
else if (class == H5T_INTEGER || class == H5T_FLOAT)
{
/* For integers and floats, we don't have to worry about
* endianness if we compare native types. */
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SCHAR)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_BYTE;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SHORT)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_SHORT;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_INT)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_INT;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_FLOAT)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_FLOAT;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_DOUBLE)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_DOUBLE;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UCHAR)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_UBYTE;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_USHORT)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_USHORT;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UINT)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_UINT;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_LLONG)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_INT64;
return NC_NOERR;
}
if ((equal = H5Tequal(native_typeid, H5T_NATIVE_ULLONG)) < 0)
return NC_EHDFERR;
if (equal)
{
*xtype = NC_UINT64;
return NC_NOERR;
}
}
/* Maybe we already know about this type. */
if (!equal)
if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
{
*xtype = type->nc_typeid;
return NC_NOERR;
}
*xtype = NC_NAT;
return NC_EBADTYPID;
}
/* Given an HDF5 type, set a pointer to netcdf type_info struct,
* either an existing one (for user-defined types) or a newly created
* one. */
static int
get_type_info2(NC_HDF5_FILE_INFO_T *h5, hid_t datasetid,
nc_type *xtype, NC_TYPE_INFO_T **type_info)
{
NC_TYPE_INFO_T *type;
htri_t is_str, equal = 0;
hid_t class, native_typeid, hdf_typeid;
#if 0
nc_type my_nc_type = 0;
#endif
H5T_order_t order;
int endianness;
nc_type nc_type_constant[NUM_TYPES] = {NC_CHAR, NC_BYTE, NC_SHORT, NC_INT, NC_FLOAT,
NC_DOUBLE, NC_UBYTE, NC_USHORT, NC_UINT,
NC_INT64, NC_UINT64, NC_STRING};
int type_size[NUM_TYPES] = {sizeof(char), sizeof(char), sizeof(short),
sizeof(int), sizeof(float), sizeof(double),
sizeof(unsigned char), sizeof(unsigned short),
sizeof(unsigned int), sizeof(long long),
sizeof(unsigned long long), 0};
int t;
assert(h5 && xtype && type_info);
/* Because these N5T_NATIVE_* constants are actually function calls
* (!) in H5Tpublic.h, I can't initialize this array in the usual
* way, because at least some C compilers (like Irix) complain
* about calling functions when defining constants. So I have to do
* it like this. Note that there's no native types for char or
* string. Those are handled later. */
if (!native_type_constant[1])
{
native_type_constant[1] = H5T_NATIVE_SCHAR;
native_type_constant[2] = H5T_NATIVE_SHORT;
native_type_constant[3] = H5T_NATIVE_INT;
native_type_constant[4] = H5T_NATIVE_FLOAT;
native_type_constant[5] = H5T_NATIVE_DOUBLE;
native_type_constant[6] = H5T_NATIVE_UCHAR;
native_type_constant[7] = H5T_NATIVE_USHORT;
native_type_constant[8] = H5T_NATIVE_UINT;
native_type_constant[9] = H5T_NATIVE_LLONG;
native_type_constant[10] = H5T_NATIVE_ULLONG;
}
/* Get the HDF5 typeid - we'll need it later. */
if ((hdf_typeid = H5Dget_type(datasetid)) < 0)
return NC_EHDFERR;
/* Get the native typeid. Will be equivalent to hdf_typeid when
* creating but not necessarily when reading, a variable. */
if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
return NC_EHDFERR;
/* Is this type an integer, string, compound, or what? */
if ((class = H5Tget_class(native_typeid)) < 0)
return NC_EHDFERR;
/* Is this an atomic type? */
if (class == H5T_STRING || class == H5T_INTEGER || class == H5T_FLOAT)
{
/* Allocate a phony NC_TYPE_INFO_T struct to hold type info. */
if (!(*type_info = calloc(1, sizeof(NC_TYPE_INFO_T))))
return NC_ENOMEM;
(*type_info)->class = class;
/* H5Tequal doesn't work with H5T_C_S1 for some reason. But
* H5Tget_class will return H5T_STRING if this is a string. */
if (class == H5T_STRING)
{
if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
return NC_EHDFERR;
/* Make sure fixed-len strings will work like variable-len strings */
if (is_str || H5Tget_size(hdf_typeid) > 1)
t = NUM_TYPES - 1;
else
t = 0;
}
else if (class == H5T_INTEGER || class == H5T_FLOAT)
{
for (t = 1; t < NUM_TYPES - 1; t++)
{
if ((equal = H5Tequal(native_typeid, native_type_constant[t])) < 0)
return NC_EHDFERR;
if (equal)
{
#if 0
my_nc_type = nc_type_constant[t];
#endif
break;
}
}
/* Find out about endianness. */
if (class == H5T_INTEGER)
{
if ((order = H5Tget_order(hdf_typeid)) < 0)
return NC_EHDFERR;
if (order == H5T_ORDER_LE)
endianness = NC_ENDIAN_LITTLE;
else if (order == H5T_ORDER_BE)
endianness = NC_ENDIAN_BIG;
else /* don't support H5T_ORDER_VAX, H5T_ORDER_MIXED, H5T_ORDER_NONE */
return NC_EBADTYPE;
/* Copy this into the type_info struct. */
(*type_info)->endianness = endianness;
}
}
*xtype = nc_type_constant[t];
(*type_info)->nc_typeid = nc_type_constant[t];
(*type_info)->size = type_size[t];
if (!((*type_info)->name = malloc((strlen(nc_type_name[t]) + 1) * sizeof(char))))
return NC_ENOMEM;
strcpy((*type_info)->name, nc_type_name[t]);
(*type_info)->class = class;
(*type_info)->hdf_typeid = hdf_typeid;
(*type_info)->native_typeid = native_typeid;
(*type_info)->close_hdf_typeid = 1;
return NC_NOERR;
}
else
{
/* This is a user-defined type. */
if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
{
*xtype = type->nc_typeid;
*type_info = type;
}
/* The type entry in the array of user-defined types already has
* an open data typeid (and native typeid), so close the ones we
* opened above. */
if (H5Tclose(native_typeid) < 0)
return NC_EHDFERR;
if (H5Tclose(hdf_typeid) < 0)
return NC_EHDFERR;
if (type)
return NC_NOERR;
}
*xtype = NC_NAT;
return NC_EBADTYPID;
}
/* Read an attribute. */
static int
read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
{
hid_t spaceid = 0, file_typeid = 0;
hsize_t dims[1] = {0}; /* netcdf attributes always 1-D. */
int retval = NC_NOERR;
size_t type_size;
int att_ndims;
hssize_t att_npoints;
H5T_class_t att_class;
int fixed_len_string = 0;
size_t fixed_size = 0;
assert(att->name);
LOG((5, "read_hdf5_att: att->attnum %d att->name %s "
"att->xtype %d att->len %d", att->attnum, att->name,
att->xtype, att->len));
/* Get type of attribute in file. */
if ((file_typeid = H5Aget_type(attid)) < 0)
return NC_EATTMETA;
if ((att->native_typeid = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0)
BAIL(NC_EHDFERR);
if ((att_class = H5Tget_class(att->native_typeid)) < 0)
BAIL(NC_EATTMETA);
if (att_class == H5T_STRING && !H5Tis_variable_str(att->native_typeid))
{
fixed_len_string++;
if (!(fixed_size = H5Tget_size(att->native_typeid)))
BAIL(NC_EATTMETA);
}
if ((retval = get_netcdf_type(grp->nc4_info, att->native_typeid, &(att->xtype))))
BAIL(retval);
/* Get len. */
if ((spaceid = H5Aget_space(attid)) < 0)
BAIL(NC_EATTMETA);
#ifdef EXTRA_TESTS
num_spaces++;
#endif
if ((att_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
BAIL(NC_EATTMETA);
if ((att_npoints = H5Sget_simple_extent_npoints(spaceid)) < 0)
BAIL(NC_EATTMETA);
/* If both att_ndims and att_npoints are zero, then this is a
* zero length att. */
if (att_ndims == 0 && att_npoints == 0)
{
dims[0] = 0;
}
else if (att->xtype == NC_STRING) {
dims[0] = att_npoints;
}
else if (att->xtype == NC_CHAR)
{
/* NC_CHAR attributes are written as a scalar in HDF5, of type
* H5T_C_S1, of variable length. */
if (att_ndims == 0)
{
if (!(dims[0] = H5Tget_size(file_typeid)))
BAIL(NC_EATTMETA);
}
else
{
/* This is really a string type! */
att->xtype = NC_STRING;
dims[0] = att_npoints;
}
}
else
{
H5S_class_t space_class;
/* All netcdf attributes are scalar or 1-D only. */
if (att_ndims > 1)
BAIL(NC_EATTMETA);
/* Check class of HDF5 dataspace */
if ((space_class = H5Sget_simple_extent_type(spaceid)) < 0)
BAIL(NC_EATTMETA);
/* Check for NULL HDF5 dataspace class (should be weeded out earlier) */
if (H5S_NULL == space_class)
BAIL(NC_EATTMETA);
/* check for SCALAR HDF5 dataspace class */
if (H5S_SCALAR == space_class)
dims[0] = 1;
else /* Must be "simple" dataspace */
{
/* Read the size of this attribute. */
if (H5Sget_simple_extent_dims(spaceid, dims, NULL) < 0)
BAIL(NC_EATTMETA);
}
}
/* Tell the user what the length if this attribute is. */
att->len = dims[0];
/* Allocate some memory if the len is not zero, and read the
attribute. */
if (dims[0])
{
if ((retval = nc4_get_typelen_mem(grp->nc4_info, att->xtype, 0,
&type_size)))
return retval;
if (att_class == H5T_VLEN)
{
if (!(att->vldata = malloc((unsigned int)(att->len * sizeof(hvl_t)))))
BAIL(NC_ENOMEM);
if (H5Aread(attid, att->native_typeid, att->vldata) < 0)
BAIL(NC_EATTMETA);
}
else if (att->xtype == NC_STRING)
{
if (!(att->stdata = calloc(att->len, sizeof(char *))))
BAIL(NC_ENOMEM);
/* For a fixed length HDF5 string, the read requires
* contiguous memory. Meanwhile, the netCDF API requires that
* nc_free_string be called on string arrays, which would not
* work if one contiguous memory block were used. So here I
* convert the contiguous block of strings into an array of