iterator_fallback.c 12.8 KB
Newer Older
1
2
3
4
5
6
7
8
#include "iterator_fallback.h"

#include "cdi.h"
#include "cdi_int.h"
#include "dmemory.h"
#include "vlist.h"      //Required for vlist_t, which we require because there is no safe function available to access a variable name.

#include <assert.h>
9
#include <limits.h>
10
11
#include <stdlib.h>

12
13
struct CdiFallbackIterator {
  CdiIterator super;
14
  int streamId, vlistId, subtypeId;
15
16
17
18
  char *path;   //needed for clone() & serialize()

  int variableCount, curVariable;
  int curLevelCount, curLevel;
19
  int curSubtypeCount, curSubtype;
20
21
22
23
24
25
26
27
28
  int curTimestep;
};

CdiIterator *cdiFallbackIterator_getSuper(CdiFallbackIterator *me)
{
  return &me->super;
}


29
//For more information on the condestruct() pattern, see comment in src/iterator_grib.c
30
static CdiFallbackIterator *cdiFallbackIterator_condestruct(CdiFallbackIterator *me, const char *path, int filetype)
31
32
33
{
  if(me) goto destruct;

34
  me = (CdiFallbackIterator *) Malloc(sizeof(*me));
35
36
37
38
39
40
41
42
  baseIterConstruct(&me->super, filetype);

  me->streamId = streamOpenRead(path);
  if(me->streamId == CDI_UNDEFID) goto destructSuper;
  me->vlistId = streamInqVlist(me->streamId);
  if(me->vlistId == CDI_UNDEFID) goto closeStream;
  me->variableCount = vlistNvars(me->vlistId);
  if(me->variableCount <= 0) goto closeStream;
43
44
  me->subtypeId = CDI_UNDEFID;   //Will be set in cdiFallbackIterator_nextField()
  me->curSubtypeCount = -1;      //Will be set in cdiFallbackIterator_nextField()
45
46
47
48
49
50
  me->curLevelCount = -1;        //Will be set in cdiFallbackIterator_nextField()

  //These values are chosen so that the natural increment at the start of cdiFallbackIterator_nextField() will correctly position us at the first slice.
  me->curTimestep = 0;
  if(streamInqTimestep(me->streamId, me->curTimestep) <= 0) goto closeStream;
  me->curVariable = 0;
51
  me->curSubtype = 0;
52
  me->curLevel = -1;
53
  me->path = strdup(path);
54
55
56
57
58
59
60
61
62
  if(!me->path) goto closeStream;

  return me;

// ^        constructor code        ^
// |                                |
// v destructor/error-cleanup code  v

destruct:
63
  Free(me->path);
64
65
66
67
closeStream:
  streamClose(me->streamId);
destructSuper:
  baseIterDestruct(&me->super);
68
  Free(me);
69
70
71
  return NULL;
}

72
CdiIterator *cdiFallbackIterator_new(const char *path, int filetype)
73
{
74
  return &cdiFallbackIterator_condestruct(NULL, path, filetype)->super;
75
76
}

77
78
//Fetches the info that is derived from the current variable. Most of this is published by the data members in the base class.
static void fetchVariableInfo(CdiFallbackIterator *me)
79
{
80
  //Fetch data that's published via base class data members.
81
82
83
84
85
  me->super.datatype = vlistInqVarDatatype(me->vlistId, me->curVariable);
  me->super.timesteptype = vlistInqVarTsteptype(me->vlistId, me->curVariable);
  me->super.gridId = vlistInqVarGrid(me->vlistId, me->curVariable);
  int param = vlistInqVarParam(me->vlistId, me->curVariable);
  cdiDecodeParam(param, &me->super.param.number, &me->super.param.category, &me->super.param.discipline);
86
87
88
89
90

  //Fetch the current level and subtype counts.
  me->curLevelCount = zaxisInqSize(vlistInqVarZaxis(me->vlistId, me->curVariable));
  me->subtypeId = vlistInqVarSubtype(me->vlistId, me->curVariable);
  me->curSubtypeCount = (me->subtypeId == CDI_UNDEFID) ? 1 : subtypeInqSize(me->subtypeId);
91
92
}

93
CdiFallbackIterator *cdiFallbackIterator_clone(CdiIterator *super)
94
{
Thomas Jahns's avatar
Thomas Jahns committed
95
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
96
97

  //Make another stream for this file. This yields an unadvanced iterator.
98
  CdiFallbackIterator *clone = cdiFallbackIterator_condestruct(NULL, me->path, me->super.filetype);
99
100
101
102
103
104
105
  if(!clone) return NULL;

  //Point the clone to the same position in the file.
  clone->variableCount = me->variableCount;
  clone->curVariable = me->curVariable;
  clone->curLevelCount = me->curLevelCount;
  clone->curLevel = me->curLevel;
106
107
  clone->curSubtypeCount = me->curSubtypeCount;
  clone->curSubtype = me->curSubtype;
108
109
110
  clone->curTimestep = me->curTimestep;

  clone->super.isAdvanced = super->isAdvanced;
111
  if(super->isAdvanced) fetchVariableInfo(clone);
112
113
114
115

  return clone;
}

116
char *cdiFallbackIterator_serialize(CdiIterator *super)
117
{
Thomas Jahns's avatar
Thomas Jahns committed
118
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
119

120
  char *escapedPath = cdiEscapeSpaces(me->path);
121
  char *result = (char *) Malloc(strlen(escapedPath)
122
123
                         + 7 * (3 * sizeof (int) * CHAR_BIT / 8 + 1) + 1);
  sprintf(result, "%s %d %d %d %d %d %d %d", escapedPath, me->variableCount, me->curVariable, me->curLevelCount, me->curLevel, me->curSubtypeCount, me->curSubtype, me->curTimestep);
124
  Free(escapedPath);
125
126
127
  return result;
}

128
CdiFallbackIterator *cdiFallbackIterator_deserialize(const char *description)
129
{
130
  CdiFallbackIterator *me = (CdiFallbackIterator *) Malloc(sizeof(*me));
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  if(!me) goto fail;

  description = baseIter_constructFromString(&me->super, description);

  while(*description == ' ') description++;
  me->path = cdiUnescapeSpaces(description, &description);
  if(!me->path) goto destructSuper;

  me->streamId = streamOpenRead(me->path);
  if(me->streamId == CDI_UNDEFID) goto freePath;
  me->vlistId = streamInqVlist(me->streamId);
  if(me->vlistId == CDI_UNDEFID) goto closeStream;

  //This reads one variable from the description string, does error checking, and advances the given string pointer.
145
#define decodeValue(variable, description) do \
146
    { \
147
      const char *savedStart = description; \
148
149
150
151
152
153
154
155
156
      long long decodedValue = strtoll(description, (char**)&description, 0);   /*The cast is a workaround for the wrong signature of strtoll().*/ \
      variable = (int)decodedValue; \
      if(savedStart == description) goto closeStream; \
      if((long long)decodedValue != (long long)variable) goto closeStream; \
    } while(0)
  decodeValue(me->variableCount, description);
  decodeValue(me->curVariable, description);
  decodeValue(me->curLevelCount, description);
  decodeValue(me->curLevel, description);
157
158
  decodeValue(me->curSubtypeCount, description);
  decodeValue(me->curSubtype, description);
159
  decodeValue(me->curTimestep, description);
160
#undef decodeValue
161
162

  if(streamInqTimestep(me->streamId, me->curTimestep) <= 0) goto closeStream;
163
  if(me->super.isAdvanced) fetchVariableInfo(me);
164
165
166
167
168
169

  return me;

closeStream:
  streamClose(me->streamId);
freePath:
170
  Free(me->path);
171
172
destructSuper:
  baseIterDestruct(&me->super);
173
  Free(me);
174
175
176
177
fail:
  return NULL;
}

178
static int advance(CdiFallbackIterator *me)
179
180
{
  me->curLevel++;
181
  if(me->curLevel >= me->curLevelCount)
182
183
    {
      me->curLevel = 0;
184
185
      me->curSubtype++;
      if(me->curSubtype >= me->curSubtypeCount)
186
        {
187
188
189
190
191
192
193
194
          me->curSubtype = 0;
          me->curVariable++;
          if(me->curVariable >= me->variableCount)
            {
              me->curVariable = 0;
              me->curTimestep++;
              if(streamInqTimestep(me->streamId, me->curTimestep) <= 0) return CDI_EEOF;
            }
195
196
197
198
199
        }
    }
  return CDI_NOERR;
}

200
int cdiFallbackIterator_nextField(CdiIterator *super)
201
{
Thomas Jahns's avatar
Thomas Jahns committed
202
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
203
204
205
  int result = advance(me);
  if(result) return result;

206
  if(!me->curLevel && !me->curSubtype) fetchVariableInfo(me);   //Check whether we are processing a new variable/timestep and fetch the information that may have changed in this case.
207
208
209
  return CDI_NOERR;
}

210
char *cdiFallbackIterator_inqTime(CdiIterator *super, bool getEndTime)
211
{
Thomas Jahns's avatar
Thomas Jahns committed
212
  CdiFallbackIterator *me = (CdiFallbackIterator *)(void *)super;
213
214
215
216
217
218
219
  if(getEndTime) return NULL;   //The stream interface does not export the start/end times of statistical fields, so we treat all data as point of time data, returning the validity time as the start time.
  int taxisId = vlistInqTaxis(me->vlistId);
  int date = taxisInqVdate(taxisId);
  int time = taxisInqVtime(taxisId);
  int year, month, day, hour, minute, second;
  cdiDecodeDate(date, &year, &month, &day);
  cdiDecodeTime(time, &hour, &minute, &second);
220
  char *result
221
    = (char *) Malloc(   4+1 +2+1 +2+1 +2+1 +2+1 +2+4+1);
222
  sprintf(result,     "%04d-%02d-%02dT%02d:%02d:%02d.000", year, month, day, hour, minute, second);
223
  return result;
224
225
}

226
int cdiFallbackIterator_levelType(CdiIterator *super, int levelSelector, char **outName, char **outLongName, char **outStdName, char **outUnit)
227
{
Thomas Jahns's avatar
Thomas Jahns committed
228
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
229
  int zaxisId = vlistInqVarZaxis(me->vlistId, me->curVariable);
Thomas Jahns's avatar
Thomas Jahns committed
230
  (void)levelSelector;
231
#define copyString(outPointer, function) do \
232
233
234
235
236
    { \
      if(outPointer) \
        { \
          char tempBuffer[CDI_MAX_NAME]; \
          function(zaxisId, tempBuffer); \
237
          *outPointer = strdup(tempBuffer); \
238
239
240
241
242
243
244
        } \
    } \
  while(0)
  copyString(outName, zaxisInqName);    //FIXME: zaxisInqName is unsafe.
  copyString(outLongName, zaxisInqLongname);    //FIXME: zaxisInqLongname is unsafe.
  copyString(outStdName, zaxisInqStdname);    //FIXME: zaxisInqStdname is unsafe.
  copyString(outUnit, zaxisInqUnits);    //FIXME: zaxisInqUnits is unsafe.
245
#undef copyString
246
247
248
  return zaxisInqLtype(zaxisId);
}

249
int cdiFallbackIterator_level(CdiIterator *super, int levelSelector, double *outValue1, double *outValue2)
250
{
Thomas Jahns's avatar
Thomas Jahns committed
251
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
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
  int zaxisId = vlistInqVarZaxis(me->vlistId, me->curVariable);

  //handle NULL pointers once and for all
  double trash;
  if(!outValue1) outValue1 = &trash;
  if(!outValue2) outValue2 = &trash;

  //get the level value
  if(levelSelector)
    {
      *outValue1 = (zaxisInqLbounds(zaxisId, NULL))
                 ? zaxisInqLbound(zaxisId, me->curLevel)
                 : zaxisInqLevel(zaxisId, me->curLevel);
    }
  else
    {
      *outValue1 = (zaxisInqUbounds(zaxisId, NULL))
                 ? zaxisInqUbound(zaxisId, me->curLevel)
                 : zaxisInqLevel(zaxisId, me->curLevel);
    }
  *outValue2 = 0.0;

  //if this is a hybrid zaxis, lookup the coordinates in the vertical coordinate table
  ssize_t intLevel = (ssize_t)(2**outValue1);
  if(0 <= intLevel && intLevel < zaxisInqVctSize(zaxisId) - 1)
    {
278
      const double *coordinateTable = zaxisInqVctPtr(zaxisId);
279
280
281
282
283
284
      *outValue1 = coordinateTable[intLevel];
      *outValue2 = coordinateTable[intLevel + 1];
    }
  return CDI_NOERR;
}

285
int cdiFallbackIterator_zaxisUuid(CdiIterator *super, int *outVgridNumber, int *outLevelCount, unsigned char outUuid[CDI_UUID_SIZE])
286
{
Thomas Jahns's avatar
Thomas Jahns committed
287
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
288
289
290
291
  int zaxisId = vlistInqVarZaxis(me->vlistId, me->curVariable);
  if(zaxisInqLtype(zaxisId) != ZAXIS_HYBRID) return CDI_EINVAL;
  if(outVgridNumber) *outVgridNumber = zaxisInqNumber(zaxisId);
  if(outLevelCount) *outLevelCount = zaxisInqNlevRef(zaxisId);
292
  if(outUuid) zaxisInqUUID(zaxisId, outUuid);
293
294
295
  return CDI_NOERR;
}

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
int cdiFallbackIterator_inqTile(CdiIterator *super, int *outTileIndex, int *outTileAttribute)
{
  CdiFallbackIterator *me = (CdiFallbackIterator*)super;
  if(!outTileIndex) outTileIndex = &(int){0};
  if(!outTileAttribute) outTileAttribute = &(int){0};

  int error = CDI_NOERR;
  if(subtypeInqAttribute(me->subtypeId, me->curSubtype, "tileIndex", outTileIndex)) error = CDI_EINVAL;
  if(subtypeInqAttribute(me->subtypeId, me->curSubtype, "tileAttribute", outTileAttribute)) error = CDI_EINVAL;
  if(error) *outTileIndex = *outTileAttribute = -1; //Guarantee defined values in case of an error.
  return error;
}

int cdiFallbackIterator_inqTileCount(CdiIterator *super, int *outTileCount, int *outTileAttributeCount)
{
  CdiFallbackIterator *me = (CdiFallbackIterator*)super;
  if(!outTileCount) outTileCount = &(int){0};
  if(!outTileAttributeCount) outTileAttributeCount = &(int){0};

  int error = CDI_NOERR;
  if(subtypeInqAttribute(me->subtypeId, me->curSubtype, "numberOfTiles", outTileCount)) error = CDI_EINVAL;
  if(subtypeInqAttribute(me->subtypeId, me->curSubtype, "numberOfTileAttributes", outTileAttributeCount)) error = CDI_EINVAL;
  if(error) *outTileCount = *outTileAttributeCount = -1; //Guarantee defined values in case of an error.
  return CDI_NOERR;
}

322
char *cdiFallbackIterator_copyVariableName(CdiIterator *super)
323
{
Thomas Jahns's avatar
Thomas Jahns committed
324
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
325
326
327
  return vlistCopyVarName(me->vlistId, me->curVariable);
}

328
void cdiFallbackIterator_readField(CdiIterator *super, double *buffer, size_t *nmiss)
329
{
Thomas Jahns's avatar
Thomas Jahns committed
330
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
331
332
  int missingValues = 0;
  streamReadVarSlice(me->streamId, me->curVariable, me->curLevel, buffer, &missingValues);
333
  if(nmiss) *nmiss = (size_t)missingValues;
334
335
}

336
void cdiFallbackIterator_readFieldF(CdiIterator *super, float *buffer, size_t *nmiss)
337
{
Thomas Jahns's avatar
Thomas Jahns committed
338
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
339
340
  int missingValues = 0;
  streamReadVarSliceF(me->streamId, me->curVariable, me->curLevel, buffer, &missingValues);
341
  if(nmiss) *nmiss = (size_t)missingValues;
342
343
}

344
void cdiFallbackIterator_delete(CdiIterator *super)
345
{
Thomas Jahns's avatar
Thomas Jahns committed
346
  CdiFallbackIterator *me = (CdiFallbackIterator*)(void *)super;
347
  cdiFallbackIterator_condestruct(me, NULL, 0);
348
}
349
350
351
352
353
354
355
356
357
358

/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */