File: hdf4file.c

package info (click to toggle)
netcdf-parallel 1%3A4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 105,352 kB
  • sloc: ansic: 229,114; sh: 11,180; yacc: 2,561; makefile: 1,390; lex: 1,173; xml: 173; awk: 2
file content (731 lines) | stat: -rw-r--r-- 20,478 bytes parent folder | download | duplicates (2)
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
/* Copyright 2018, UCAR/Unidata See netcdf/COPYRIGHT file for copying
 * and redistribution conditions.*/
/**
 * @file
 * @internal The HDF4 file functions. These provide a read-only
 * interface to HDF4 SD files.
 *
 * @author Ed Hartnett
 */

#include "config.h"
#include "nc4internal.h"
#include "hdf4dispatch.h"
#include <mfhdf.h>

#define NUM_TYPES 12 /**< Number of netCDF atomic types. */

extern int nc4_vararray_add(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var);

/** @internal These flags may not be set for open mode. */
static const int
ILLEGAL_OPEN_FLAGS = (NC_MMAP|NC_64BIT_OFFSET|NC_DISKLESS|NC_WRITE);

/** @internal NetCDF atomic type names. */
static const char*
nc_type_name_g[NUM_TYPES] = {"char", "byte", "short", "int", "float", "double",
                             "ubyte", "ushort", "uint", "int64", "uint64",
                             "string"};

/** @internal NetCDF atomic type sizes. */
static const size_t
nc_type_size_g[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), sizeof(char *)};

/**
 * @internal Recursively delete the data for a group (and everything
 * it contains) in our internal metadata store.
 *
 * @param grp Pointer to group info struct.
 *
 * @return ::NC_NOERR No error.
 * @author Ed Hartnett
 */
static int
hdf4_rec_grp_del(NC_GRP_INFO_T *grp)
{
    NC_VAR_INFO_T *var;
    int i;

    assert(grp);
    LOG((3, "%s: grp->name %s", __func__, grp->hdr.name));

    /* Delete all vars. */
    for (i = 0; i < ncindexsize(grp->vars); i++)
    {
        NC_VAR_HDF4_INFO_T *hdf4_var;

        var = (NC_VAR_INFO_T *)ncindexith(grp->vars, i);
        assert(var);
        LOG((4, "%s: deleting var %s", __func__, var->hdr.name));

        /* Get the HDF4 specific var metadata. */
        hdf4_var = (NC_VAR_HDF4_INFO_T *)var->format_var_info;

        /* Close HDF4 dataset associated with this var, unless it's a
         * scale. */
        if (hdf4_var->sdsid && SDendaccess(hdf4_var->sdsid) < 0)
            return NC_EHDFERR;
    }

    return NC_NOERR;
}

/**
 * @internal Given an HDF4 type, set a pointer to netcdf type.
 *
 * See http://www.hdfgroup.org/training/HDFtraining/UsersGuide/Fundmtls.fm3.html
 * for more information re: HDF4 types.
 *
 * @param h5 Pointer to HDF5 file info struct.
 * @param hdf4_typeid Type ID for hdf4 datatype.
 * @param xtype Pointer that gets netcdf type. Ignored if NULL.
 * @param endniannessp Pointer that gets endianness. Ignored if NULL.
 * @param type_sizep Pointer that gets type size. Ignored if NULL.
 * @param type_name Pointer that gets the type name. Ignored if NULL.
 *
 * @return ::NC_NOERR No error.
 * @author Ed Hartnett
 */
static int
hdf4_type_info(NC_FILE_INFO_T *h5, int32 hdf4_typeid, nc_type* xtypep,
               int *endiannessp, size_t *type_sizep, char *type_name)
{
    int t = 0;
    int endianness = NC_ENDIAN_BIG;
    nc_type xtype;

    assert(h5);

    switch(hdf4_typeid)
    {
    case DFNT_CHAR:
        xtype = NC_CHAR;
        t = 0;
        break;
    case DFNT_UCHAR:
    case DFNT_UINT8:
        xtype = NC_UBYTE;
        t = 6;
        break;
    case DFNT_LUINT8:
        xtype = NC_UBYTE;
        t = 6;
        endianness = NC_ENDIAN_LITTLE;
        break;
    case DFNT_INT8:
        xtype = NC_BYTE;
        t = 1;
        break;
    case DFNT_LINT8:
        xtype = NC_BYTE;
        t = 1;
        endianness = NC_ENDIAN_LITTLE;
        break;
    case DFNT_INT16:
        xtype = NC_SHORT;
        t = 2;
        break;
    case DFNT_LINT16:
        xtype = NC_SHORT;
        t = 2;
        endianness = NC_ENDIAN_LITTLE;
        break;
    case DFNT_UINT16:
        xtype = NC_USHORT;
        t = 7;
        break;
    case DFNT_LUINT16:
        xtype = NC_USHORT;
        t = 7;
        endianness = NC_ENDIAN_LITTLE;
        break;
    case DFNT_INT32:
        xtype = NC_INT;
        t = 3;
        break;
    case DFNT_LINT32:
        xtype = NC_INT;
        t = 3;
        endianness = NC_ENDIAN_LITTLE;
        break;
    case DFNT_UINT32:
        xtype = NC_UINT;
        t = 8;
        break;
    case DFNT_LUINT32:
        xtype = NC_UINT;
        t = 8;
        endianness = NC_ENDIAN_LITTLE;
        break;
    case DFNT_FLOAT32:
        xtype = NC_FLOAT;
        t = 4;
        break;
    case DFNT_LFLOAT32:
        xtype = NC_FLOAT;
        t = 4;
        endianness = NC_ENDIAN_LITTLE;
        break;
    case DFNT_FLOAT64:
        xtype = NC_DOUBLE;
        t = 5;
        break;
    case DFNT_LFLOAT64:
        xtype = NC_DOUBLE;
        t = 5;
        endianness = NC_ENDIAN_LITTLE;
        break;
    default:
        return NC_EBADTYPID;
    }

    /* Return results to caller. */
    if (xtypep)
        *xtypep = xtype;
    if (endiannessp)
        *endiannessp = endianness;
    if (type_sizep)
        *type_sizep = nc_type_size_g[t];
    if (type_name)
        strncpy(type_name, nc_type_name_g[t], NC_MAX_NAME);

    return NC_NOERR;
}

/**
 * @internal Set the type of a netCDF-4 variable.
 *
 * @param xtype A netcdf type.
 * @param endianness The endianness of the data.
 * @param type_size The size in bytes of one element of this type.
 * @param type_name A name for the type.
 * @param typep Pointer to a pointer that gets the TYPE_INFO_T struct.
 *
 * @return ::NC_NOERR No error.
 * @author Ed Hartnett
 */
static int
nc4_set_var_type(nc_type xtype, int endianness, size_t type_size, char *type_name,
                 NC_TYPE_INFO_T **typep)
{
    NC_TYPE_INFO_T *type;

    /* Check inputs. */
    assert(typep);

    /* Allocate space for the type info struct. */
    if (!(type = calloc(1, sizeof(NC_TYPE_INFO_T))))
        return NC_ENOMEM;
    if (!(type->hdr.name = strdup(type_name)))
    {
        free(type);
        return NC_ENOMEM;
    }
    type->hdr.sort = NCTYP;

    /* Determine the type class. */
    if (xtype == NC_FLOAT)
        type->nc_type_class = NC_FLOAT;
    else if (xtype == NC_DOUBLE)
        type->nc_type_class = NC_DOUBLE;
    else if (xtype == NC_CHAR)
        type->nc_type_class = NC_STRING;
    else
        type->nc_type_class = NC_INT;

    /* Set other type info values. */
    type->endianness = endianness;
    type->size = type_size;
    type->hdr.id = (size_t)xtype;
    type->hdr.hashkey = NC_hashmapkey(type->hdr.name, strlen(type->hdr.name));

    /* Return to caller. */
    *typep = type;

    return NC_NOERR;
}

/**
 * @internal Read an attribute from a HDF4 file.
 *
 * @param h5 Pointer to the file metadata struct.
 * @param var Pointer to variable metadata struct or NULL for global
 * attributes.
 * @param a Index of attribute to read.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_EHDFERR HDF4 error.
 * @return ::NC_EATTMETA Error reading HDF4 attribute.
 * @return ::NC_ENOMEM Out of memory.
 * @author Ed Hartnett
 */
static int
hdf4_read_att(NC_FILE_INFO_T *h5, NC_VAR_INFO_T *var, int a)
{
    NC_HDF4_FILE_INFO_T *hdf4_file;
    NC_ATT_INFO_T *att;
    NCindex *att_list;
    int32 att_data_type, att_count;
    size_t att_type_size;
    char name[NC_MAX_HDF4_NAME+1];
    int sd_id;
    int retval;

    LOG((3, "%s: a %d var %s", __func__, a, var ? var->hdr.name : "global"));

    /* Check inputs. */
    assert(h5 && h5->format_file_info);

    /* Get the HDF4 file info. */
    hdf4_file = h5->format_file_info;

    /* Decide what att list to use, global or from a var. */
    if (var)
    {
        NC_VAR_HDF4_INFO_T *hdf4_var;
        assert(var->format_var_info);
        att_list = var->att;
        hdf4_var = var->format_var_info;
        sd_id = hdf4_var->sdsid;
    } else {
        att_list = h5->root_grp->att;
        sd_id = hdf4_file->sdid;
    }

    /* Learn about this attribute. */
    if (SDattrinfo(sd_id, a, name, &att_data_type, &att_count))
        return NC_EATTMETA;

    /* Get information about the attribute type. */
    nc_type xtype;
    if ((retval = hdf4_type_info(h5, att_data_type, &xtype, NULL,
                                 &att_type_size, NULL)))
        return retval;

    /* Add to the end of the list of atts for this var. */
    if ((retval = nc4_att_list_add(att_list, name, &att)))
        return retval;
    att->nc_typeid = xtype;
    att->created = NC_TRUE;
    att->len = att_count;

    /* Allocate memory to hold the data. */
    if (att->len)
        if (!(att->data = malloc(att_type_size * att->len)))
            return NC_ENOMEM;

    /* Read the data. */
    if (SDreadattr(sd_id, a, att->data))
        return NC_EHDFERR;

    return NC_NOERR;
}

/**
 * @internal Read a HDF4 dimension. As new dimensions are found, add
 * them to the metadata list of dimensions.
 *
 * @param h5 Pointer to the file metadata struct.
 * @param var Pointer to variable metadata struct or NULL for global
 * attributes.
 * @param rec_dim_len Actual length of first dim for this SD.
 * @param d Dimension index for this SD.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_EHDFERR HDF4 error.
 * @return ::NC_EDIMMETA Error reading HDF4 dimension info.
 * @return ::NC_ENOMEM Out of memory.
 * @return ::NC_EMAXNAME Name too long.
 * @author Ed Hartnett
 */
static int
hdf4_read_dim(NC_FILE_INFO_T *h5, NC_VAR_INFO_T *var, int rec_dim_len, int d)
{
    NC_VAR_HDF4_INFO_T *hdf4_var;
    NC_DIM_INFO_T *dim = NULL;
    int32 dimid, dim_len, dim_data_type, dim_num_attrs;
    char dim_name[NC_MAX_NAME + 1];
    int i;
    int retval;

    assert(h5 && h5->format_file_info && var && var->format_var_info);
    hdf4_var = var->format_var_info;

    /* Get information about the dimension. */
    if ((dimid = SDgetdimid(hdf4_var->sdsid, d)) == FAIL)
        return NC_EDIMMETA;
    if (SDdiminfo(dimid, dim_name, &dim_len, &dim_data_type, &dim_num_attrs))
        return NC_EDIMMETA;
    if (strlen(dim_name) > NC_MAX_HDF4_NAME)
        return NC_EMAXNAME;

    /* Do we already have this dimension? HDF4 explicitly uses
     * the name to tell. */
    for (i = 0; i < ncindexsize(h5->root_grp->dim); i++)
    {
        dim = (NC_DIM_INFO_T*)ncindexith(h5->root_grp->dim, i);
        if (!strcmp(dim->hdr.name, dim_name))
            break;
        dim = NULL;
    }

    /* If we didn't find this dimension, add one. */
    if (!dim)
    {
        LOG((4, "adding dim %s for dataset %s", dim_name, var->hdr.name));
        if ((retval = nc4_dim_list_add(h5->root_grp, dim_name,
                                       (dim_len ? dim_len : rec_dim_len), -1, &dim)))
            return retval;
    }

    /* Tell the variable the id of this dimension. */
    var->dimids[d] = dim->hdr.id;
    var->dim[d] = dim;

    return NC_NOERR;
}

/**
 * @internal Create a new variable and insert int relevant lists
 *
 * @param grp the containing group
 * @param name the name for the new variable
 * @param ndims the rank of the new variable
 * @param format_var_info Pointer to format-specific var info struct.
 * @param var Pointer in which to return a pointer to the new var.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_ENOMEM Out of memory.
 * @author Ed Hartnett
 */
static int
nc4_var_list_add_full(NC_GRP_INFO_T* grp, const char* name, int ndims, nc_type xtype,
                      int endianness, size_t type_size, char *type_name, void *fill_value,
                      int contiguous, size_t *chunksizes, void *format_var_info,
                      NC_VAR_INFO_T **var)
{
    int d;
    int retval;

    /* Add the VAR_INFO_T struct to our list of vars. */
    if ((retval = nc4_var_list_add(grp, name, ndims, var)))
        return retval;
    (*var)->created = NC_TRUE;
    (*var)->written_to = NC_TRUE;
    (*var)->format_var_info = format_var_info;
    (*var)->atts_read = 1;

    /* Fill special type_info struct for variable type information. */
    if ((retval = nc4_set_var_type(xtype, endianness, type_size, type_name,
                                   &(*var)->type_info)))
        return retval;

    (*var)->type_info->rc++;

    /* Handle fill value, if provided. */
    if (fill_value)
    {
        if (!((*var)->fill_value = malloc(type_size)))
            return NC_ENOMEM;
        memcpy((*var)->fill_value, fill_value, type_size);
    }

    /* Var contiguous or chunked? */
    if (contiguous)
	(*var)->storage = NC_CONTIGUOUS;
    else
	(*var)->storage = NC_CHUNKED;

    /* Were chunksizes provided? */
    if (chunksizes)
    {
        if (!((*var)->chunksizes = malloc(ndims * sizeof(size_t))))
            return NC_ENOMEM;
        for (d = 0; d < ndims; d++)
            (*var)->chunksizes[d] = chunksizes[d];
    }

    return NC_NOERR;
}

/**
 * @internal Read a HDF4 variable, including its associated dimensions
 * and attributes.
 *
 * @param h5 Pointer to the file metadata struct.
 * @param v Index of variable to read.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_EHDFERR HDF4 error.
 * @return ::NC_EDIMMETA Error reading HDF4 dimension info.
 * @return ::NC_EVARMETA Error reading HDF4 dataset or att.
 * @return ::NC_EATTMETA Error reading HDF4 attribute.
 * @return ::NC_ENOMEM Out of memory.
 * @return ::NC_EMAXNAME Name too long.
 * @author Ed Hartnett
 */
static int
hdf4_read_var(NC_FILE_INFO_T *h5, int v)
{
    NC_HDF4_FILE_INFO_T *hdf4_file;
    NC_VAR_INFO_T *var;
    NC_VAR_HDF4_INFO_T *hdf4_var;
    HDF_CHUNK_DEF chunkdefs;
    int32 data_type, num_atts;
    int32 dimsize[NC_MAX_HDF4_DIMS];
    int32 rec_dim_len;
    int32 rank;
    int32 sdsid;
    int contiguous;
    int d, a;
    int flag;
    char name[NC_MAX_HDF4_NAME+1];
    int xtype;
    char type_name[NC_MAX_NAME + 1];
    int endianness;
    size_t type_size;
    void *fill_value;
    size_t *chunksizes = NULL;
    int retval;

    /* Check inputs. */
    assert(h5 && h5->format_file_info);

    /* Get HDF4 file metadata. */
    hdf4_file = h5->format_file_info;

    /* Open this dataset in HDF4 file. */
    if ((sdsid = SDselect(hdf4_file->sdid, v)) == FAIL)
        return  NC_EVARMETA;

    /* Learn about this dataset. */
    if (SDgetinfo(sdsid, name, &rank, dimsize, &data_type, &num_atts))
        return NC_EVARMETA;
    rec_dim_len = dimsize[0];

    /* Get chunking info from HDF4 file. */
    if (SDgetchunkinfo(sdsid, &chunkdefs, &flag))
        return NC_EVARMETA;

    /* Learn about the HDF4 type. */
    if ((retval = hdf4_type_info(h5, data_type, &xtype, &endianness, &type_size,
                                 type_name)))
        return retval;

    /* Get the fill value. */
    if (!(fill_value = malloc(type_size)))
        return NC_ENOMEM;
    if (SDgetfillvalue(sdsid, fill_value))
    {
        /* Whoops! No fill value! */
        free(fill_value);
        fill_value = NULL;
    }

    /* Is variable chunked or contiguous? */
    if (flag == HDF_NONE)
        contiguous = NC_TRUE;
    else if (flag & HDF_CHUNK)
    {
        contiguous = NC_FALSE;
        if (!(chunksizes = malloc(rank * sizeof(size_t))))
            return NC_ENOMEM;
        for (d = 0; d < rank; d++)
            chunksizes[d] = chunkdefs.chunk_lengths[d];
    }

    /* Malloc a struct to hold HDF4-specific variable
     * information. */
    if (!(hdf4_var = malloc(sizeof(NC_VAR_HDF4_INFO_T))))
        return NC_ENOMEM;

    /* Remember these values. */
    hdf4_var->hdf4_data_type = data_type;
    hdf4_var->sdsid = sdsid;

    /* Add a variable to metadata structures. */
    LOG((3, "adding var for HDF4 dataset %s, rank %d netCDF type %d", name,
         rank, xtype));
    retval = nc4_var_list_add_full(h5->root_grp, name, (int)rank,
                                   xtype, endianness, type_size, type_name,
                                   fill_value, contiguous, chunksizes, hdf4_var,
                                   &var);

    /* Free resources. */
    if (chunksizes)
        free(chunksizes);
    if (fill_value)
        free(fill_value);

    /* Did the add fail? */
    if (retval)
    {
        free(hdf4_var);
        return retval;
    }

    /* Find the variable's dimensions. */
    for (d = 0; d < var->ndims; d++)
        if ((retval = hdf4_read_dim(h5, var, rec_dim_len, d)))
            return retval;

    /* Read the variable's attributes. */
    for (a = 0; a < num_atts; a++)
        if ((retval = hdf4_read_att(h5, var, a)))
            return retval;

    return NC_NOERR;
}

/**
 * @internal Open a HDF4 SD file for read-only access.
 *
 * @param path The file name of the file.
 * @param mode The open mode flag.
 * @param basepe Ignored by this function.
 * @param chunksizehintp Ignored by this function.
 * @param parameters pointer to struct holding extra data (e.g. for
 * parallel I/O) layer. Ignored if NULL. Ignored by this function.
 * @param dispatch Pointer to the dispatch table for this file.
 * @param nc_file Pointer to an instance of NC. The ncid has already
 * been assigned, and is in nc_file->ext_ncid.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_EINVAL Invalid input.
 * @return ::NC_EHDFERR Error from HDF4 layer.
 * @return ::NC_ENOMEM Out of memory.
 * @author Ed Hartnett
 */
int
NC_HDF4_open(const char *path, int mode, int basepe, size_t *chunksizehintp,
             void *parameters, const NC_Dispatch *dispatch, int ncid)
{
    NC_FILE_INFO_T *h5;
    NC_HDF4_FILE_INFO_T *hdf4_file;
    NC *nc;
    int32 num_datasets, num_gatts;
    int32 sdid;
    int v, a;
    int retval;

    /* Check inputs. */
    assert(path);

    LOG((1, "%s: path %s mode %d params %x", __func__, path, mode, parameters));

    /* Find pointer to NC. */
    if ((retval = NC_check_id(ncid, &nc)))
        return retval;

    /* Check the mode for validity */
    if (mode & ILLEGAL_OPEN_FLAGS)
        return NC_EINVAL;

    /* Open the file and initialize SD interface. */
    if ((sdid = SDstart(path, DFACC_READ)) == FAIL)
        return NC_EHDFERR;

    /* Learn how many datasets and global atts we have. */
    if (SDfileinfo(sdid, &num_datasets, &num_gatts))
        return NC_EHDFERR;

    /* Add necessary structs to hold netcdf-4 file data. */
    if ((retval = nc4_file_list_add(ncid, path, mode, (void **)&h5)))
        return retval;
    assert(h5 && h5->root_grp);
    h5->no_write = NC_TRUE;
    h5->root_grp->atts_read = 1;

    /* Allocate data to hold HDF4 specific file data. */
    if (!(hdf4_file = malloc(sizeof(NC_HDF4_FILE_INFO_T))))
        return NC_ENOMEM;
    h5->format_file_info = hdf4_file;
    hdf4_file->sdid = sdid;

    /* Read the global atts. */
    for (a = 0; a < num_gatts; a++)
        if ((retval = hdf4_read_att(h5, NULL, a)))
            break;

    /* Read each dataset. */
    if (!retval)
        for (v = 0; v < num_datasets; v++)
            if ((retval = hdf4_read_var(h5, v)))
                break;

    /* If there is an error, free resources. */
    if (retval)
        free(hdf4_file);

#ifdef LOGGING
    /* This will print out the names, types, lens, etc of the vars and
       atts in the file, if the logging level is 2 or greater. */
    log_metadata_nc(h5);
#endif

    return retval;
}

/**
 * @internal Abort (close) the HDF4 file.
 *
 * @param ncid File ID.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_EBADID Bad ncid.
 * @return ::NC_EHDFERR Error from HDF4 layer.
 * @author Ed Hartnett
 */
int
NC_HDF4_abort(int ncid)
{
    return NC_HDF4_close(ncid, NULL);
}

/**
 * @internal Close the HDF4 file.
 *
 * @param ncid File ID.
 * @param ignore Ignore this pointer.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_EBADID Bad ncid.
 * @return ::NC_EHDFERR Error from HDF4 layer.
 * @author Ed Hartnett
 */
int
NC_HDF4_close(int ncid, void *ignore)
{
    NC_GRP_INFO_T *grp;
    NC *nc;
    NC_FILE_INFO_T *h5;
    NC_HDF4_FILE_INFO_T *hdf4_file;
    int retval;

    LOG((1, "%s: ncid 0x%x", __func__, ncid));

    /* Find our metadata for this file. */
    if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
        return retval;
    assert(nc && h5 && grp && !grp->parent);

    /* Clean up HDF4 specific allocations. */
    if ((retval = hdf4_rec_grp_del(h5->root_grp)))
        return retval;

    /* Close hdf4 file and free HDF4 file info. */
    hdf4_file = (NC_HDF4_FILE_INFO_T *)h5->format_file_info;
    if (SDend(hdf4_file->sdid))
        return NC_EHDFERR;
    free(hdf4_file);

    /* Free the NC_FILE_INFO_T struct. */
    if ((retval = nc4_nc4f_list_del(h5)))
        return retval;

    return NC_NOERR;
}