File: minc_compat.c

package info (click to toggle)
minc 2.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,160 kB
  • sloc: ansic: 82,507; sh: 10,666; yacc: 1,187; perl: 612; makefile: 586; lex: 319
file content (387 lines) | stat: -rw-r--r-- 8,721 bytes parent folder | download | duplicates (4)
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
#if HAVE_CONFIG_H
#include "config.h"
#endif

#if MINC2                       /* Ignore if not MINC2 */
/* minc_compat.c
 * 
 * this code exists to provide a dispatch layer between the MI2* low-level
 * calls and their respective HDF5 and NetCDF implementations.
 *
 * Since each of these calls uses exactly one file descriptor, the logic is 
 * simple: we just apply the correct operation based on a quick determination
 * of whether this is an HDF5 handle or a NetCDF file descriptor.
 */
#define _MI2_FORCE_NETCDF_
#include "minc_private.h"
#include "hdf_convenience.h"

/* */
MNCAPI int
MI2varname(int fd, int varid, char *varnm)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varname(fd, varid, varnm));
    }
    else {
        return (nc_inq_varname(fd, varid, varnm));
    }
}

/* */
MNCAPI int
MI2varid(int fd, const char *varnm)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varid(fd, varnm));
    }
    else {
        return (ncvarid(fd, varnm));
    }
}

/* */
MNCAPI int
MI2attinq(int fd, int varid, const char *attnm, nc_type *type_ptr,
          int *length_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_attinq(fd, varid, attnm, type_ptr, length_ptr));
    }
    else {
        int status;
        int oldncopts = ncopts;
        ncopts = 0;
        status = ncattinq(fd, varid, attnm, type_ptr, length_ptr);

        ncopts = oldncopts;
        if (status != 1 && oldncopts != 0) {
            fprintf(stderr,
                    _("ncattinq: ncid %d: varid: %d: Attribute '%s' not found"),
                    fd, varid, attnm);
	      
        }
        return (status);
    }
}

MNCAPI int
MI2attname(int fd, int varid, int attid, char *name)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_attname(fd, varid, attid, name));
    }
    else {
        return (ncattname(fd, varid, attid, name));
    }
}

/* */
MNCAPI int
MI2inquire(int fd, int *ndims_ptr, int *nvars_ptr, int *natts_ptr,
           int *unlimdim_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_inquire(fd, ndims_ptr, nvars_ptr, natts_ptr, unlimdim_ptr));
    }
    else {
        return (ncinquire(fd, ndims_ptr, nvars_ptr, natts_ptr, unlimdim_ptr));
    }
}

/* */
MNCAPI int
MI2varinq(int fd, int varid, char *varnm_ptr, nc_type *type_ptr,
          int *ndims_ptr, int *dims_ptr, int *natts_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varinq(fd, varid, varnm_ptr, type_ptr, ndims_ptr, 
                           dims_ptr, natts_ptr));
    }
    else {
        return (ncvarinq(fd, varid, varnm_ptr, type_ptr, ndims_ptr, 
                         dims_ptr, natts_ptr));
    }
}

/* */
MNCAPI int
MI2dimid(int fd, const char *dimnm)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_dimid(fd, dimnm));
    }
    else {
        return (ncdimid(fd, dimnm));
    }
}

/* */
MNCAPI int
MI2diminq(int fd, int dimid, char *dimnm_ptr, long *len_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_diminq(fd, dimid, dimnm_ptr, len_ptr));
    }
    else {
        return (ncdiminq(fd, dimid, dimnm_ptr, len_ptr));
    }
}

/* */
MNCAPI int
MI2dimdef(int fd, const char *dimnm, long length)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_dimdef(fd, dimnm, length));
    }
    else {
        return (ncdimdef(fd, dimnm, length));
    }
}

/* */
MNCAPI int
MI2attget(int fd, int varid, const char *attnm, void *value)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_attget(fd, varid, attnm, value));
    }
    else {
        return (ncattget(fd, varid, attnm, value));
    }
}

/* */
MNCAPI int
MI2attput(int fd, int varid, const char *attnm, nc_type val_typ, 
          int val_len, void *val_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_attput(fd, varid, attnm, val_typ, val_len, val_ptr));
    }
    else {
        int old_ncopts = ncopts;
        int result;
        ncopts = 0;
        result = ncattput(fd, varid, attnm, val_typ, val_len, val_ptr);
        ncopts = old_ncopts;
        return (result);
    }
}

/* */
MNCAPI int
MI2endef(int fd)
{
    if (MI2_ISH5OBJ(fd)) {
        return (MI_NOERROR);    /* Just a stub, HDF5 doesn't do this! */
    }
    else {
        return (ncendef(fd));
    }
}  

/* */
MNCAPI int
MI2vardef(int fd, const char *varnm, nc_type vartype, int ndims,
          const int *dimids)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_vardef(fd, varnm, vartype, ndims, dimids));
    }
    else {
        return (ncvardef(fd, varnm, vartype, ndims, dimids));
    }
}

/* */
MNCAPI int
MI2varget(int fd, int varid, const long *start_ptr, 
          const long *count_ptr, void *val_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varget(fd, varid, start_ptr, count_ptr, val_ptr));
    }
    else {
        return (ncvarget(fd, varid, start_ptr, count_ptr, val_ptr));
    }
}

/* */
MNCAPI int
MI2varput(int fd, int varid, const long *start_ptr,
          const long *count_ptr, const void *val_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varput(fd, varid, start_ptr, count_ptr, val_ptr));
    }
    else {
        return (ncvarput(fd, varid, start_ptr, count_ptr, val_ptr));
    }
}

/* */
MNCAPI int
MI2varput1(int fd, int varid, const long *mindex_ptr,
           const void *val_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varput1(fd, varid, mindex_ptr, val_ptr));
    }
    else {
        return (ncvarput1(fd, varid, mindex_ptr, val_ptr));
    }
}

MNCAPI int
MI2attdel(int fd, int varid, const char *attnm)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_attdel(fd, varid, attnm));
    }
    else {
        return (ncattdel(fd, varid, attnm));
    }
}


/* */
MNCAPI int
MI2dimrename(int fd, int dimid, const char *new_name)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_dimrename(fd, dimid, new_name));
    }
    else {
        return (ncdimrename(fd, dimid, new_name));
    }
}

MNCAPI int
MI2varputg(int fd, int varid, const long *startp, const long *countp,
           const long *stridep, const long *imapp, const void *valp)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varputg(fd, varid, startp, countp, stridep, imapp, valp));
    }
    else {
        return (ncvarputg(fd, varid, startp, countp, stridep, imapp, valp));
    }
}

MNCAPI int
MI2attcopy(int infd, int invarid, const char *name, int outfd, 
           int outvarid)
{
    if (!MI2_ISH5OBJ(infd) && !MI2_ISH5OBJ(outfd)) {
        /* Trivial case. */
        return (ncattcopy(infd, invarid, name, outfd, outvarid));
    }
    else {
        /* Complex case.  Using our own compatibility layer functions lets us
         * handle all three other possible combinations of infd & outfd types.
         */
        nc_type att_type;
        int att_length;
        void *val_ptr;
        int status;

        status = MI2attinq(infd, invarid, name, &att_type, &att_length);
        if (status == MI_ERROR) {
            return (MI_ERROR);
        }

        /* Special case for att_type == NC_CHAR && att_length == 0 
         */
        if (att_type == NC_CHAR && att_length == 0) {
            val_ptr = malloc(1);
            if (val_ptr == NULL) {
                return (MI_ERROR);
            }
            *(char *)val_ptr = '\0';
            att_length = 1;
            status = MI_NOERROR;
        }
        else {
            val_ptr = malloc(MI2typelen(att_type) * att_length);
            if (val_ptr == NULL) {
                return (MI_ERROR);
            }

            status = MI2attget(infd, invarid, name, val_ptr);
        }

        if (status != MI_ERROR) {
            status = MI2attput(outfd, outvarid, name, att_type, att_length,
                               val_ptr);
        }
    
        free(val_ptr);
        return (status);
    }
}

MNCAPI int
MI2typelen(int type_id)
{
    switch (type_id) {
    case NC_BYTE:
    case NC_CHAR:
        return (1);
    case NC_SHORT:
        return (2);
    case NC_INT:
    case NC_FLOAT:
        return (4);
    case NC_DOUBLE:
        return (8);
    default:
        break;
    }
    fprintf(stderr, _("Unknown type %d"), type_id);
    return (-1);
}

MNCAPI int
MI2redef(int fd)
{
    if (MI2_ISH5OBJ(fd)) {
        /* Do nothing, since there is no equivalent in HDF5. */
        return (MI_NOERROR);
    }
    else {
        return (ncredef(fd));
    }
}

MNCAPI int
MI2sync(int fd)
{
    if (MI2_ISH5OBJ(fd)) {
        /* Commit the (entire) file to disk. */
        if (H5Fflush(fd, H5F_SCOPE_GLOBAL) < 0) {
            return (MI_ERROR);
        }
        else {
            return (MI_NOERROR);
        }
    }
    else {
        return (ncsync(fd));
    }
}

MNCAPI int
MI2setfill(int fd, int fillmode)
{
    if (MI2_ISH5OBJ(fd)) {
        /* TODO: ??? */
        return (MI_NOERROR);
    }
    else {
        return (ncsetfill(fd, fillmode));
    }
}

#endif /* MINC2 defined */