test_cdf_read.c 4.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#include "cdi.h"

11
12
13
static void
printAtts(int vlistID);

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int main(int argc, const char **argv)
{
  const char *fname = "test.nc";
  int countMissingValues = 1;
  /* todo: handle optional arguments here to increase test coverage */
  if (argc)
    fname = argv[1];

  int streamID = streamOpenRead(fname);
  if (streamID < 0)
    {
      fprintf(stderr, "Open failed for file %s: %s\n",
              fname, cdiStringError(streamID));
      return EXIT_FAILURE;
    }
  int vlistID = streamInqVlist(streamID);
  size_t nVars = (size_t)vlistNvars(vlistID);

  double *buf = NULL;
  size_t bufSize = 0;
  size_t allNmissSum = 0;

36
37
  printAtts(vlistID);

38
39
40
41
  for (int tsID = 0; streamInqTimestep(streamID, tsID); ++tsID)
    {
      for (size_t varID = 0; varID < nVars; ++varID)
        {
42
          size_t memSize = (size_t)vlistInqVarSize(vlistID, (int)varID)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
            * sizeof (double);
          int nmiss;
          if (memSize > bufSize)
            {
              double *temp = realloc(buf, memSize);
              if (!temp)
                {
                  perror("read buffer reallocation failed");
                  return EXIT_FAILURE;
                }
              buf = temp;
            }
          streamReadVar(streamID, (int)varID, buf, &nmiss);
          allNmissSum += (size_t)nmiss;
        }
      ++tsID;
    }
  if (countMissingValues)
    printf("missing values count = %zu\n", allNmissSum);
  streamClose(streamID);
  return EXIT_SUCCESS;
}

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
static void
printAtts(int vlistID)
{
  size_t nVars = (size_t)vlistNvars(vlistID);
  static const char globDesc[] = "global",
    varDescPrefix[] = "variable ";
  size_t digitsPerInt = 9;
  size_t bufSize = digitsPerInt + sizeof (varDescPrefix);
  void *restrict buf = malloc(bufSize);
  if (!buf)
    {
      perror("attribute buffer resize failed");
      exit(EXIT_FAILURE);
    }
  for (size_t varIdx = 0; varIdx <= nVars; ++varIdx)
    {
      int nAtts, attType, attLen;
      char attName[CDI_MAX_NAME + 1];
      int varID = (int)varIdx - 1;
      vlistInqNatts(vlistID, varID, &nAtts);
      for (size_t attIdx = 0; attIdx < (size_t)nAtts; attIdx++ )
        {
          int rc = vlistInqAtt(vlistID, varID, (int)attIdx,
                               attName, &attType, &attLen);
          {
            const char *varDesc = varIdx > 0
              ? (sprintf(buf, "%s%d", varDescPrefix,
                         vlistInqVarCode(vlistID, varID)), buf)
              : globDesc;
            printf("%s attribute \"%s\", value: ",
                   varDesc, attName);
          }
          if (attLen < 0)
            goto attGetFail;
          size_t elemSize = 0;
          switch (attType)
            {
            case DATATYPE_TXT:
              elemSize = 1;
              break;
            case DATATYPE_FLT:
              elemSize = sizeof (double);
              break;
            case DATATYPE_INT:
              elemSize = sizeof (int);
              break;
            }

          size_t attSize = elemSize * ((size_t)attLen + 1);
          if (attSize > bufSize)
            {
              if (!(buf = realloc(buf, attSize)))
                {
                  perror("attribute buffer resize failed");
                  exit(EXIT_FAILURE);
                }
            }

          switch (attType)
            {
            case DATATYPE_TXT:
              rc = vlistInqAttTxt(vlistID, (int)varID, attName,
                                  attLen, buf);
              if (rc == CDI_NOERR)
                printf("\"%.*s\"", attLen, (char *)buf);
              break;
            case DATATYPE_FLT:
              rc = vlistInqAttFlt(vlistID, (int)varID, attName,
                                  attLen + 1, buf);
              if (rc == CDI_NOERR && attLen)
                {
                  const double *restrict dp = buf;
                  printf("%10g", dp[0]);
                  for (size_t i = 1; i < (size_t)attLen; ++i)
                    printf(", %10g", dp[i]);
                }
              break;
            case DATATYPE_INT:
              rc = vlistInqAttInt(vlistID, (int)varID, attName,
                                  attLen + 1, buf);
              if (rc == CDI_NOERR && attLen)
                {
                  const int *restrict ip = buf;
                  printf("%d", ip[0]);
                  for (size_t i = 1; i < (size_t)attLen; ++i)
                    printf(", %d", ip[i]);
                }
              break;
            }

          if (rc == CDI_NOERR)
            {
              putchar('\n');
            }
          else
            {
              attGetFail:
              puts("error retrieving value");
            }
        }
    }
  free(buf);
}


171
172
173
174
175
176
177
178
179
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */