File: vardata.c

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (622 lines) | stat: -rw-r--r-- 19,708 bytes parent folder | download | duplicates (3)
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
/*********************************************************************
 *   Copyright 1993, UCAR/Unidata
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
 *********************************************************************/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "hdf.h"

#ifdef H4_HAVE_NETCDF
#include <netcdf.h>
#else
#include <hdf4_netcdf.h>
#endif

#include "ncdump.h"
#include "dumplib.h"
#include "vardata.h"

/*
 * Function from ncdump.c. "Fixes" variable names to remove spaces and other
 * "illegal" characters.
 */
extern char *sanitize_string(char *str, bool fix_str);

static void annotate(struct ncvar *vp, struct fspec *fsp, long cor[], long iel);

#define VD_STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)

/*
 * Print a row of variable values.  Makes sure output lines aren't too long
 * by judiciously inserting newlines.
 */
/* vp - variable */
/* len - number of values to print */
/* fmt
 * printf format used for each value.  If
 * nc_type is NC_CHAR and this is NULL,
 * character arrays will be printed as strings
 * enclosed in quotes.
 */
/* more
 * true if more data will follow, so add
 * trailing comma
 */
/* lastrow
 * true if this is the last row for this
 * variable, so terminate with ";" instead of
 * ","
 */
/* vals pointer to block of values */
static void
pr_vals(struct ncvar *vp, long len, char *fmt, bool more, bool lastrow, void *vals)
{
    long iel;
    union {
        char   *cp;
        short  *sp;
        nclong *lp;
        float  *fp;
        double *dp;
    } gp;
    char         *sp;
    unsigned char uc;
    float         fill_float;
    double        fill_double;
    char          sout[100]; /* temporary string for each encoded output */

    fill_float  = FILL_FLOAT; /* static initialization hits ultrix cc bug */
    fill_double = FILL_DOUBLE;
    switch (vp->type) {
        case NC_BYTE:
            gp.cp = (char *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                (void)sprintf(sout, fmt, *gp.cp++);
                (void)strcat(sout, ", ");
                lput(sout);
            }
            (void)sprintf(sout, fmt, *gp.cp++);
            lput(sout);
            break;
        case NC_CHAR:
            gp.cp = (char *)vals;
            if (fmt == 0 || VD_STREQ(fmt, "%s") || VD_STREQ(fmt, "")) { /* as string */
                printf("\"");
                /* adjust len so trailing nulls don't get printed */
                sp = gp.cp + len;
                while (len > 0 && *--sp == '\0')
                    len--;
                for (iel = 0; iel < len; iel++)
                    switch (uc = *gp.cp++ & 0377) {
                        case '\b':
                            printf("\\b");
                            break;
                        case '\f':
                            printf("\\f");
                            break;
                        case '\n': /* generate linebreaks after new-lines */
                            printf("\\n\",\n    \"");
                            break;
                        case '\r':
                            printf("\\r");
                            break;
                        case '\t':
                            printf("\\t");
                            break;
                        case '\v':
                            printf("\\v");
                            break;
                        case '\\':
                            printf("\\\\");
                            break;
                        case '\'':
                            printf("\\\'");
                            break;
                        case '\"':
                            printf("\\\"");
                            break;
                        default:
                            if (isprint(uc))
                                printf("%c", uc);
                            else
                                printf("\\%.3o", uc);
                            break;
                    }
                printf("\"");
            }
            else { /* use format from C_format attribute */
                for (iel = 0; iel < len - 1; iel++) {
                    (void)sprintf(sout, fmt, *gp.cp++);
                    (void)strcat(sout, ", ");
                    lput(sout);
                }
                (void)sprintf(sout, fmt, *gp.cp++);
                lput(sout);
            }
            break;
        case NC_SHORT:
            gp.sp = (short *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                (void)sprintf(sout, fmt, *gp.sp++);
                (void)strcat(sout, ", ");
                lput(sout);
            }
            (void)sprintf(sout, fmt, *gp.sp++);
            lput(sout);
            break;
        case NC_LONG:
            gp.lp = (nclong *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                (void)sprintf(sout, fmt, *gp.lp++);
                (void)strcat(sout, ", ");
                lput(sout);
            }
            (void)sprintf(sout, fmt, *gp.lp++);
            lput(sout);
            break;
        case NC_FLOAT:
            gp.fp = (float *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                if (*gp.fp >= fill_float)
                    (void)sprintf(sout, "FloatInf, ");
                else {
                    (void)sprintf(sout, fmt, (double)*gp.fp);
                    (void)strcat(sout, ", ");
                }
                lput(sout);
                gp.fp++;
            }
            if (*gp.fp >= fill_float)
                (void)sprintf(sout, "FloatInf");
            else
                (void)sprintf(sout, fmt, (double)*gp.fp);
            lput(sout);
            gp.fp++;
            break;
        case NC_DOUBLE:
            gp.dp = (double *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                if (*gp.dp >= fill_double)
                    (void)sprintf(sout, "DoubleInf, ");
                else {
                    (void)sprintf(sout, fmt, *gp.dp);
                    (void)strcat(sout, ", ");
                }
                lput(sout);
                gp.dp++;
            }
            if (*gp.dp >= fill_double)
                (void)sprintf(sout, "DoubleInf");
            else
                (void)sprintf(sout, fmt, *gp.dp);
            lput(sout);
            gp.dp++;
            break;
        default:
            error("pr_vals: bad type");
    }
    if (more) {
        lput(", ");
    }
    else {
        if (lastrow) {
            lput(" ;");
            lput("\n");
        }
        else {
            lput(",\n");
            lput("  ");
        }
    }
}

/*
 * print last delimiter in each line before annotation (, or ;)
 */
static void
lastdelim(bool more, bool lastrow)
{
    if (more) {
        printf(", ");
    }
    else {
        if (lastrow) {
            printf(";");
        }
        else {
            printf(",");
        }
    }
}

/*
 * Annotates a value in data section with var name and indices in comment
 */
/* vp  - variable */
/* fsp - formatting specs */
/* cor - corner coordinates */
/* iel - which element in current row */
static void
annotate(struct ncvar *vp, struct fspec *fsp, long cor[], long iel)
{
    int vrank = vp->ndims;
    int id;

    /* print indices according to data_lang */
    printf("  // %s(", vp->name);
    switch (fsp->data_lang) {
        case LANG_C:
            /* C variable indices */
            for (id = 0; id < vrank - 1; id++)
                printf("%d,", (int)cor[id]);
            printf("%d", (int)(cor[id] + iel));
            break;
        case LANG_F:
            /* Fortran variable indices */
            printf("%d", (int)(cor[vrank - 1] + iel + 1));
            for (id = vrank - 2; id >= 0; id--) {
                printf(",%d", (int)(1 + cor[id]));
            }
            break;
        case LANG_NONE:
        default:
            /* Might want to make this an error */
            break;
    }
    printf(")\n    ");
}

/*
 * Print a number of commented variable values, where the comments for each
 * value identify the variable, and each dimension index.
 */
/* vp - variable */
/* len - number of values to print */
/* fmt
 * printf format used for each value.  If
 * nc_type is NC_CHAR and this is NULL,
 * character arrays will be printed as strings
 * enclosed in quotes.
 */
/* more
 * true if more data for this row will follow,
 * so add trailing comma
 */
/* lastrow
 * true if this is the last row for this
 * variable, so terminate with ";" instead of
 * ","
 */
/* vals - pointer to block of values */
/* fsp  - formatting specs */
/* cor  - corner coordinates */
static void
pr_cvals(struct ncvar *vp, long len, char *fmt, bool more, bool lastrow, void *vals, struct fspec *fsp,
         long cor[])
{
    long iel;
    union {
        char   *cp;
        short  *sp;
        nclong *lp;
        float  *fp;
        double *dp;
    } gp;
    char         *sp;
    unsigned char uc;
    float         fill_float;
    double        fill_double;

    fill_float  = FILL_FLOAT; /* static initialization hits ultrix cc bug */
    fill_double = FILL_DOUBLE;
    switch (vp->type) {
        case NC_BYTE:
            gp.cp = (char *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                printf(fmt, *gp.cp++);
                printf(", ");
                annotate(vp, fsp, cor, iel);
            }
            printf(fmt, *gp.cp++);
            lastdelim(more, lastrow);
            annotate(vp, fsp, cor, iel);
            break;
        case NC_CHAR:
            gp.cp = (char *)vals;
            if (fmt == 0 || VD_STREQ(fmt, "%s") || VD_STREQ(fmt, "")) { /* as string */
                printf("\"");
                /* adjust len so trailing nulls don't get printed */
                sp = gp.cp + len;
                while (len > 0 && *--sp == '\0')
                    len--;
                for (iel = 0; iel < len; iel++)
                    switch (uc = *gp.cp++ & 0377) {
                        case '\b':
                            printf("\\b");
                            break;
                        case '\f':
                            printf("\\f");
                            break;
                        case '\n': /* generate linebreaks after new-lines */
                            printf("\\n\",\n    \"");
                            break;
                        case '\r':
                            printf("\\r");
                            break;
                        case '\t':
                            printf("\\t");
                            break;
                        case '\v':
                            printf("\\v");
                            break;
                        case '\\':
                            printf("\\\\");
                            break;
                        case '\'':
                            printf("\\\'");
                            break;
                        case '\"':
                            printf("\\\"");
                            break;
                        default:
                            if (isprint(uc))
                                printf("%c", uc);
                            else
                                printf("\\%.3o", uc);
                            break;
                    }
                printf("\"");
                annotate(vp, fsp, cor, 0);
            }
            else { /* use format from C_format attribute */
                for (iel = 0; iel < len - 1; iel++) {
                    printf(fmt, *gp.cp++);
                    printf(", ");
                    annotate(vp, fsp, cor, iel);
                }
                printf(fmt, *gp.cp++);
                lastdelim(more, lastrow);
                annotate(vp, fsp, cor, iel);
            }
            break;
        case NC_SHORT:
            gp.sp = (short *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                printf(fmt, *gp.sp++);
                printf(", ");
                annotate(vp, fsp, cor, iel);
            }
            printf(fmt, *gp.sp++);
            lastdelim(more, lastrow);
            annotate(vp, fsp, cor, iel);
            break;
        case NC_LONG:
            gp.lp = (nclong *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                printf(fmt, *gp.lp++);
                printf(", ");
                annotate(vp, fsp, cor, iel);
            }
            printf(fmt, *gp.lp++);
            lastdelim(more, lastrow);
            annotate(vp, fsp, cor, iel);
            break;
        case NC_FLOAT:
            gp.fp = (float *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                if (*gp.fp >= fill_float)
                    printf("FloatInf");
                else
                    printf(fmt, (double)*gp.fp);
                printf(",");
                annotate(vp, fsp, cor, iel);
                gp.fp++;
            }
            if (*gp.fp >= fill_float)
                printf("FloatInf");
            else
                printf(fmt, (double)*gp.fp);
            lastdelim(more, lastrow);
            annotate(vp, fsp, cor, iel);
            gp.fp++;
            break;
        case NC_DOUBLE:
            gp.dp = (double *)vals;
            for (iel = 0; iel < len - 1; iel++) {
                if (*gp.dp >= fill_double)
                    printf("DoubleInf");
                else {
                    printf(fmt, *gp.dp);
                }
                printf(",");
                annotate(vp, fsp, cor, iel);
                gp.dp++;
            }
            if (*gp.dp >= fill_double)
                printf("DoubleInf");
            else
                printf(fmt, *gp.dp);
            lastdelim(more, lastrow);
            annotate(vp, fsp, cor, iel);
            gp.dp++;
            break;
        default:
            error("pr_vals: bad type");
    }
}

/*
 * Updates a vector of ints, odometer style.  Returns 0 if odometer
 * overflowed, else 1.
 */
/* dims  - The "odometer" limits for each dimension  */
/* ndims - Number of dimensions */
/* odom  - The "odometer" vector to be updated */
/* add   - A vector to "add" to odom on each update */
static int
upcorner(long *dims, int ndims, long *odom, long *add)
{
    int id;
    int ret = 1;

    for (id = ndims - 1; id > 0; id--) {
        odom[id] += add[id];
        if (odom[id] >= dims[id]) {
            odom[id - 1]++;
            odom[id] -= dims[id];
        }
    }
    odom[0] += add[0];
    if (odom[0] >= dims[0])
        ret = 0;
    return ret;
}

/* vp    - variable */
/* vdims - variable dimension sizes */
/* ncid  - netcdf id */
/* varid - variable id */
/* fsp   - formatting specs */
int
vardata(struct ncvar *vp, long vdims[], int ncid, int varid, struct fspec *fsp)
{
    long cor[H4_MAX_VAR_DIMS]; /* corner coordinates */
    long edg[H4_MAX_VAR_DIMS]; /* edges of hypercube */
    long add[H4_MAX_VAR_DIMS]; /* "odometer" increment to next "row"  */
#define VALBUFSIZ 8192
    double vals[VALBUFSIZ / sizeof(double)]; /* aligned buffer */
    int    gulp = VALBUFSIZ / nctypelen(vp->type);

    int   id;
    int   ir;
    long  nels;
    long  ncols;
    long  nrows;
    int   vrank = vp->ndims;
    char *fixed_var;
    int   ret = 0, err_code = 0;

    /* printf format used to print each value */
    const char *fmt = get_fmt(ncid, varid, vp->type);

    nels = 1;
    for (id = 0; id < vrank; id++) {
        cor[id] = 0;
        edg[id] = 1;
        nels *= vdims[id]; /* total number of values for variable */
    }

    fixed_var = sanitize_string(vp->name, fsp->fix_str);

    if (vrank <= 1) {
        printf("\n %s = ", fixed_var);
        set_indent(strlen(fixed_var) + 4);
    }
    else {
        printf("\n %s =\n  ", fixed_var);
        set_indent(2);
    }

    if (vrank < 1) {
        ncols = 1;
    }
    else {
        ncols          = vdims[vrank - 1]; /* size of "row" along last dimension */
        edg[vrank - 1] = vdims[vrank - 1];
        for (id = 0; id < vrank; id++)
            add[id] = 0;
        if (vrank > 1)
            add[vrank - 2] = 1;
    }
    nrows = nels / ncols; /* number of "rows" */

    for (ir = 0; ir < nrows; ir++) {
        /*
         * rather than just printing a whole row at once (which might exceed
         * the capacity of MSDOS platforms, for example), we break each row
         * into smaller chunks, if necessary.
         */
        long corsav = 0;
        long left   = ncols;
        bool lastrow;

        if (vrank > 0) {
            corsav = cor[vrank - 1];
            if (fsp->brief_data_cmnts != false && vrank > 1 &&
                left > 0) { /* print brief comment with indices range */
                printf("// %s(", fixed_var);
                switch (fsp->data_lang) {
                    case LANG_C:
                        /* print brief comment with C variable indices */
                        for (id = 0; id < vrank - 1; id++)
                            printf("%d,", (int)cor[id]);
                        if (vdims[vrank - 1] == 1)
                            printf("0");
                        else
                            printf(" 0-%d", (int)vdims[vrank - 1] - 1);
                        break;
                    case LANG_F:
                        /* print brief comment with Fortran variable indices */
                        if (vdims[vrank - 1] == 1)
                            printf("1");
                        else
                            printf("1-%d ", (int)vdims[vrank - 1]);
                        for (id = vrank - 2; id >= 0; id--) {
                            printf(",%d", (int)(1 + cor[id]));
                        }
                        break;
                    case LANG_NONE:
                    default:
                        /* Might want to make this an error */
                        break;
                }
                printf(")\n    ");
                set_indent(4);
            }
        }
        lastrow = (ir == nrows - 1) ? true : false;
        while (left > 0) {
            long toget = left < gulp ? left : gulp;
            if (vrank > 0)
                edg[vrank - 1] = toget;

            /* ncvarget was casted to (void), thus ncdump misinformed users
               that the reading succeeded even though the data was corrupted and
               reading in fact failed (HDFFR-1468.)  Now when ncvarget fails,
               break out of the while loop and set error code to indicate this
               failure. -BMR, 2015/01/19 */
            ret = ncvarget(ncid, varid, cor, edg, (void *)vals);
            if (ret == -1) {
                err_code = ERR_READFAIL; /* to be returned to caller to
                                            indicate that ncvarget fails */
                break;
            }

            if (fsp->full_data_cmnts)
                pr_cvals(vp, toget, fmt, left > toget, lastrow, (void *)vals, fsp, cor);
            else
                pr_vals(vp, toget, fmt, left > toget, lastrow, (void *)vals);
            left -= toget;
            if (vrank > 0)
                cor[vrank - 1] += toget;
        }
        /* No failure occurs */
        if (ret >= 0) {
            if (vrank > 0)
                cor[vrank - 1] = corsav;
            if (ir < nrows - 1)
                if (!upcorner(vdims, vp->ndims, cor, add))
                    error("vardata: odometer overflowed!");
            set_indent(2);
        }
    }

    free(fixed_var);
    return (err_code);
    /* Previously, it was "return 0;"  If this function is revised, this
       return statement may be changed appropriately. (HDFFR-1468) */
}