iterator.c 31.8 KB
Newer Older
1
#include "cdi.h"
2
#include "dmemory.h"
3
4
5
6
7
8
9
10
#include "iterator.h"
#include "iterator_fallback.h"
#include "iterator_grib.h"
#include "cdi_int.h"

#include <assert.h>
#include <ctype.h>

11
12
13
14
15
16
static const char kUnexpectedFileTypeMessage[]
  = "Internal error: Unexpected file type encountered in iterator.\n"
  "This is either due to an illegal memory access by the application\n"
  " or an internal logical error in CDI (unlikely, but possible).";
static const char kAdvancedString[] = "advanced";
static const char kUnadvancedString[] = "unadvanced";
17
18
19
20
21
22

//Returns a static string.
static const char* fileType2String(int fileType)
{
  switch(fileType)
    {
23
#ifdef HAVE_LIBGRIB_API
24
25
        case FILETYPE_GRB: return "CDI::Iterator::GRIB1";
        case FILETYPE_GRB2: return "CDI::Iterator::GRIB2";
26
27
#endif
#ifdef HAVE_LIBNETCDF
28
29
30
31
        case FILETYPE_NC: return "CDI::Iterator::NetCDF";
        case FILETYPE_NC2: return "CDI::Iterator::NetCDF2";
        case FILETYPE_NC4: return "CDI::Iterator::NetCDF4";
        case FILETYPE_NC4C: return "CDI::Iterator::NetCDF4C";
32
33
#endif
#ifdef HAVE_LIBSERVICE
34
        case FILETYPE_SRV: return "CDI::Iterator::SRV";
35
36
#endif
#ifdef HAVE_LIBEXTRA
37
        case FILETYPE_EXT: return "CDI::Iterator::EXT";
38
39
#endif
#ifdef HAVE_LIBIEG
40
        case FILETYPE_IEG: return "CDI::Iterator::IEG";
41
#endif
42
43
44
45
46

      default: return NULL;
    }
}

47
static int string2FileType(const char* fileType, const char **outRestString)
48
49
{
  //This first part unconditionally checks all known type strings, and only if the given string matches one of these strings, we use fileType2string() to check whether support for this type has been compiled in. This is to avoid throwing "invalid type string" errors when we just have a library version mismatch.
50
#define check(givenString, typeString, typeConstant) do \
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    { \
      if(givenString == strstr(givenString, typeString)) \
        { \
          if(outRestString) *outRestString = givenString + strlen(typeString); \
          if(fileType2String(typeConstant)) return typeConstant; \
          Error("Support for " typeString " not compiled in. Please check that the result of `cdiIterator_serialize()` is only passed to a `cdiIterator_deserialize()` implementation of the same CDI library version."); \
          return FILETYPE_UNDEF; \
        } \
    } while(0)
  check(fileType, "CDI::Iterator::GRIB1", FILETYPE_GRB);
  check(fileType, "CDI::Iterator::GRIB2", FILETYPE_GRB2);
  check(fileType, "CDI::Iterator::NetCDF", FILETYPE_NC);
  check(fileType, "CDI::Iterator::NetCDF2", FILETYPE_NC2);
  check(fileType, "CDI::Iterator::NetCDF4", FILETYPE_NC4);
  check(fileType, "CDI::Iterator::NetCDF4C", FILETYPE_NC4C);
  check(fileType, "CDI::Iterator::SRV", FILETYPE_SRV);
  check(fileType, "CDI::Iterator::EXT", FILETYPE_EXT);
  check(fileType, "CDI::Iterator::IEG", FILETYPE_IEG);
69
#undef check
70
71
72
73
74
75
76

  //If this point is reached, the given string does not seem to be produced by a cdiIterator_serialize() call.
  Error("The string \"%s\" does not start with a valid iterator type. Please check the source of this string.", fileType);
  *outRestString = fileType;
  return FILETYPE_UNDEF;
}

Thomas Jahns's avatar
Thomas Jahns committed
77
/*
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@Function cdiIterator_new
@Title Create an iterator for an input file

@Prototype CdiIterator* cdiIterator_new(const char* path)
@Parameter
    @item path Path to the file that is to be read.

@Result An iterator for the given file.

@Description
    Combined allocator and constructor for CdiIterator.

    The returned iterator does not point to the first field yet,
    it must first be advanced once before the first field can be introspected.
92
93
94
    This design decision has two benefits: 1. Empty files require no special
    cases, 2. Users can start a while(!cdiIterator_nextField(iterator)) loop
    right after the call to cdiIterator_new().
95
96
97
98
99
100
101
102
103
104
105
*/
CdiIterator* cdiIterator_new(const char* path)
{
  int trash;
  int filetype = cdiGetFiletype(path, &trash);
  switch(filetype)
    {
      case FILETYPE_UNDEF:
        Warning("Can't open file \"%s\": unknown format\n", path);
        return NULL;

106
#ifdef HAVE_LIBGRIB_API
107
108
109
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_new(path, filetype);
110
#endif
111

112
#ifdef HAVE_LIBNETCDF
113
114
115
116
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
117
118
#endif
#ifdef HAVE_LIBSERVICE
119
        case FILETYPE_SRV:
120
121
#endif
#ifdef HAVE_LIBEXTRA
122
        case FILETYPE_EXT:
123
124
#endif
#ifdef HAVE_LIBIEG
125
        case FILETYPE_IEG:
126
#endif
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
          return cdiFallbackIterator_new(path, filetype);

      default:
        Warning("the file \"%s\" is of type %s, but support for this format is not compiled in!", path, strfiletype(filetype));
        return NULL;
    }
}

void baseIterConstruct(CdiIterator* me, int filetype)
{
  me->filetype = filetype;
  me->isAdvanced = false;
}

const char* baseIter_constructFromString(CdiIterator* me, const char* description)
{
  const char* result = description;
  me->filetype = string2FileType(result, &result);
  assert(me->filetype != FILETYPE_UNDEF && "Please report this error.");        //This condition should have been checked for in a calling function.
  for(; *result && isspace(*result); result++);
  if(result == strstr(result, kAdvancedString))
    {
      me->isAdvanced = true;
150
      result += sizeof (kAdvancedString) - 1;
151
152
153
154
    }
  else if(result == strstr(result, kUnadvancedString))
    {
      me->isAdvanced = false;
155
      result += sizeof (kUnadvancedString) - 1;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    }
  else
    {
      Error("Invalid iterator description string \"%s\". Please check the origin of this string.", description);
      return NULL;
    }
  return result;
}

#define sanityCheck(me) do { \
    if(!me) xabort("NULL was passed to %s as an iterator. Please check the return value of cdiIterator_new().", __func__); \
    if(!me->isAdvanced) xabort("Calling %s is not allowed without calling cdiIterator_nextField() first.", __func__); \
} while(0)

Thomas Jahns's avatar
Thomas Jahns committed
170
/*
171
172
173
174
175
176
177
178
179
180
@Function cdiIterator_clone
@Title Make a copy of an iterator

@Prototype CdiIterator* cdiIterator_clone(CdiIterator* me)
@Parameter
    @item iterator The iterator to copy.

@Result The clone.

@Description
181
182
183
184
185
186
187
188
    Clones the given iterator. Make sure to call cdiIterator_delete() on both
    the copy and the original.

    This is not a cheap operation: Depending on the type of the file, it will
    either make a copy of the current field in memory (GRIB files), or reopen
    the file (all other file types). Use it sparingly. And if you do, try to
    avoid keeping too many clones around: their memory footprint is
    significant.
189
190
191
192
193
194
*/
CdiIterator* cdiIterator_clone(CdiIterator* me)
{
  sanityCheck(me);
  switch(me->filetype)
    {
195
#ifdef HAVE_LIBGRIB_API
196
197
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
198
199
          return cdiGribIterator_getSuper(cdiGribIterator_clone(me));
#endif
200

201
#ifdef HAVE_LIBNETCDF
202
203
204
205
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
206
207
#endif
#ifdef HAVE_LIBSERVICE
208
        case FILETYPE_SRV:
209
210
#endif
#ifdef HAVE_LIBEXTRA
211
        case FILETYPE_EXT:
212
213
#endif
#ifdef HAVE_LIBIEG
214
        case FILETYPE_IEG:
215
216
#endif
          return cdiFallbackIterator_getSuper(cdiFallbackIterator_clone(me));
217
218
219
220
221
222
223

      default:
        Error(kUnexpectedFileTypeMessage);
        return NULL;
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
224
/*
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
@Function cdiGribIterator_clone
@Title Gain access to GRIB specific functionality

@Prototype CdiGribIterator* cdiGribIterator_clone(CdiIterator* me)
@Parameter
    @item iterator The iterator to operate on.

@Result A clone that allows access to GRIB specific functionality, or NULL if the underlying file is not a GRIB file.

@Description
    Clones the given iterator iff the underlying file is a GRIB file, the returned iterator allows access to GRIB specific functionality.
    Make sure to check that the return value is not NULL, and to call cdiGribIterator_delete() on the copy.

    This is not a cheap operation: It will make a copy of the current field in memory. Use it sparingly. And if you do, try to avoid keeping too many clones around, their memory footprint is significant.
*/
CdiGribIterator* cdiGribIterator_clone(CdiIterator* me)
{
  sanityCheck(me);
  switch(me->filetype)
    {
245
#ifdef HAVE_LIBGRIB_API
246
247
248
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_makeClone(me);
249
#endif
250
251
252
253
254
255

      default:
        return NULL;
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
256
/*
257
258
259
260
261
262
263
264
265
266
@Function cdiIterator_serialize
@Title Serialize an iterator for sending it to another process

@Prototype char* cdiIterator_serialize(CdiIterator* me)
@Parameter
    @item iterator The iterator to operate on.

@Result A malloc'ed string that contains the full description of the iterator.

@Description
267
    Make sure to call Free() on the resulting string.
268
269
270
271
272
273
274
*/
char* cdiIterator_serialize(CdiIterator* me)
{
  if(!me) xabort("NULL was passed to %s as an iterator. Please check the return value of cdiIterator_new().", __func__); \
  char* subclassDescription = NULL;
  switch(me->filetype)
    {
275
#ifdef HAVE_LIBGRIB_API
276
277
278
279
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          subclassDescription = cdiGribIterator_serialize(me);
          break;
280
#endif
281

282
#ifdef HAVE_LIBNETCDF
283
284
285
286
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
287
288
#endif
#ifdef HAVE_LIBSERVICE
289
        case FILETYPE_SRV:
290
291
#endif
#ifdef HAVE_LIBEXTRA
292
        case FILETYPE_EXT:
293
294
#endif
#ifdef HAVE_LIBIEG
295
        case FILETYPE_IEG:
296
#endif
297
298
299
300
301
302
303
304
          subclassDescription = cdiFallbackIterator_serialize(me);
          break;

      default:
        Error(kUnexpectedFileTypeMessage);
        return NULL;
    }

305
306
  const char *ftypeStr = fileType2String(me->filetype),
    *advStr = me->isAdvanced ? kAdvancedString : kUnadvancedString;
307
  char* result = (char *) Malloc(strlen(ftypeStr) + 1 + strlen(advStr) + 1
308
309
                         + strlen(subclassDescription) + 1);
  sprintf(result, "%s %s %s", ftypeStr, advStr, subclassDescription);
310
  Free(subclassDescription);
311
312
313
  return result;
}

Thomas Jahns's avatar
Thomas Jahns committed
314
/*
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
@Function cdiIterator_deserialize
@Title Recreate an iterator from its textual description

@Prototype CdiIterator* cdiIterator_deserialize(const char* description)
@Parameter
    @item description The result of a call to cdiIterator_serialize().

@Result A clone of the original iterator.

@Description
    A pair of cdiIterator_serialize() and cdiIterator_deserialize() is functionally equivalent to a call to cdiIterator_clone()

    This function will reread the current field from disk, so don't expect immediate return.
*/
//This only checks the type of the iterator and calls the corresponding subclass function,
//the real deserialization is done in baseIter_constructFromString().
CdiIterator* cdiIterator_deserialize(const char* description)
{
  switch(string2FileType(description, NULL))
    {
335
#ifdef HAVE_LIBGRIB_API
336
337
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
338
339
          return cdiGribIterator_getSuper(cdiGribIterator_deserialize(description));
#endif
340

341
#ifdef HAVE_LIBNETCDF
342
343
344
345
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
346
347
#endif
#ifdef HAVE_LIBSERVICE
348
        case FILETYPE_SRV:
349
350
#endif
#ifdef HAVE_LIBEXTRA
351
        case FILETYPE_EXT:
352
353
#endif
#ifdef HAVE_LIBIEG
354
        case FILETYPE_IEG:
355
356
#endif
          return cdiFallbackIterator_getSuper(cdiFallbackIterator_deserialize(description));
357
358
359
360
361
362
363
364

      default:
        Error(kUnexpectedFileTypeMessage);
        return NULL;
    }
}


Thomas Jahns's avatar
Thomas Jahns committed
365
/*
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
@Function cdiIterator_print
@Title Print a textual description of the iterator to a stream

@Prototype void cdiIterator_print(CdiIterator* iterator, FILE* stream);
@Parameter
    @item iterator The iterator to print.
    @item stream The stream to print to.

@Description
    Use for debugging output.
*/
void cdiIterator_print(CdiIterator* me, FILE* stream)
{
  char* description = cdiIterator_serialize(me);
  fprintf(stream, "%s\n", description);
381
  Free(description);
382
383
384
}


Thomas Jahns's avatar
Thomas Jahns committed
385
/*
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
@Function cdiIterator_nextField
@Title Advance an iterator to the next field in the file

@Prototype int cdiIterator_nextField(CdiIterator* iterator)
@Parameter
    @item iterator The iterator to operate on.

@Result An error code. May be one of:
  * CDI_NOERR: The iterator has successfully been advanced to the next field.
  * CDI_EEOF: No more fields to read in this file.

@Description
    One call to cdiIterator_nextField() is required before the metadata of the first field can be examined.
    Usually, it will be used directly as the condition for a while() loop.
*/
int cdiIterator_nextField(CdiIterator* me)
{
  if(!me) xabort("NULL was passed in as an iterator. Please check the return value of cdiIterator_new().");
  me->isAdvanced = true;
  switch(me->filetype)
    {
407
#ifdef HAVE_LIBGRIB_API
408
409
410
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_nextField(me);
411
#endif
412

413
#ifdef HAVE_LIBNETCDF
414
415
416
417
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
418
419
#endif
#ifdef HAVE_LIBSERVICE
420
        case FILETYPE_SRV:
421
422
#endif
#ifdef HAVE_LIBEXTRA
423
        case FILETYPE_EXT:
424
425
#endif
#ifdef HAVE_LIBIEG
426
        case FILETYPE_IEG:
427
#endif
428
429
430
431
432
433
434
435
436
437
438
439
440
          return cdiFallbackIterator_nextField(me);

      default:
        Error(kUnexpectedFileTypeMessage);
        return CDI_EINVAL;
    }
}

static char* cdiIterator_inqTime(CdiIterator* me, bool getEndTime)
{
  sanityCheck(me);
  switch(me->filetype)
    {
441
#ifdef HAVE_LIBGRIB_API
442
443
444
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_inqTime(me, getEndTime);
445
#endif
446

447
#ifdef HAVE_LIBNETCDF
448
449
450
451
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
452
453
#endif
#ifdef HAVE_LIBSERVICE
454
        case FILETYPE_SRV:
455
456
#endif
#ifdef HAVE_LIBEXTRA
457
        case FILETYPE_EXT:
458
459
#endif
#ifdef HAVE_LIBIEG
460
        case FILETYPE_IEG:
461
#endif
462
463
464
465
466
467
468
469
          return cdiFallbackIterator_inqTime(me, getEndTime);

      default:
        Error(kUnexpectedFileTypeMessage);
        return NULL;
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
470
/*
471
472
473
474
475
476
477
478
479
480
481
482
483
484
@Function cdiIterator_inqStartTime
@Title Get the start time of a measurement

@Prototype char* cdiIterator_inqStartTime(CdiIterator* me)
@Parameter
    @item iterator The iterator to operate on.

@Result A malloc'ed string containing the (start) time of the current field in the format "YYYY-MM-DDTHH:MM:SS.mmm".

@Description
The returned time is either the time of the data (fields defined at a time point),
or the start time of an integration time range (statistical fields).

Converts the time to the ISO-8601 format and returns it in a newly allocated buffer.
485
The caller is responsible to Free() the resulting string.
486
487
488
489
490
491
492
493
494
495

If the file is a GRIB file, the calendar that is used to resolve the relative times is the proleptic calendar
as it is implemented by the standard C mktime() function.
This is due to the fact that GRIB-API version 1.12.3 still does not implement the calendar identification fields.
*/
char* cdiIterator_inqStartTime(CdiIterator* me)
{
  return cdiIterator_inqTime(me, false);
}

Thomas Jahns's avatar
Thomas Jahns committed
496
/*
497
498
499
500
501
502
503
504
505
506
507
508
509
510
@Function cdiIterator_inqEndTime
@Title Get the end time of a measurement

@Prototype char* cdiIterator_inqEndTime(CdiIterator* me)
@Parameter
    @item iterator The iterator to operate on.

@Result A malloc'ed string containing the end time of the current field in the format "YYYY-MM-DDTHH:MM:SS.mmm", or NULL if no such time is defined.

@Description
The returned time is the end time of an integration period if such a time exists (statistical fields).
Otherwise, NULL is returned.

Converts the time to the ISO-8601 format and returns it in a newly allocated buffer.
511
The caller is responsible to Free() the resulting string.
512
513
514
515
516
517
518
519
520
521

If the file is a GRIB file, the calendar that is used to resolve the relative times is the proleptic calendar
as it is implemented by the standard C mktime() function.
This is due to the fact that GRIB-API version 1.12.3 still does not implement the calendar identification fields.
*/
char* cdiIterator_inqEndTime(CdiIterator* me)
{
  return cdiIterator_inqTime(me, true);
}

Thomas Jahns's avatar
Thomas Jahns committed
522
/*
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
@Function cdiIterator_inqVTime
@Title Get the validity time of the current field

@Prototype char* cdiIterator_inqVTime(CdiIterator* me)
@Parameter
    @item iterator The iterator to operate on.

@Result A malloc'ed string containing the validity time of the current field in the format "YYYY-MM-DDTHH:MM:SS.mmm".

@Description
The returned time is the validity time as it is returned by taxisInqVtime(), only more precise.
That is, if the field is a time point, its time is returned,
if it is a statistical field with an integration period, the end time of the integration period is returned.

Converts the time to the ISO-8601 format and returns it in a newly allocated buffer.
538
The caller is responsible to Free() the resulting string.
539
540
541
542
543
544
545
546
547
548
549

If the file is a GRIB file, the calendar that is used to resolve the relative times is the proleptic calendar
as it is implemented by the standard C mktime() function.
This is due to the fact that GRIB-API version 1.12.3 still does not implement the calendar identification fields.
*/
char* cdiIterator_inqVTime(CdiIterator* me)
{
  char* result = cdiIterator_inqEndTime(me);
  return (result) ? result : cdiIterator_inqStartTime(me);
}

Thomas Jahns's avatar
Thomas Jahns committed
550
/*
551
552
553
@Function cdiIterator_inqLevelType
@Title Get the type of a level

554
@Prototype int cdiIterator_inqLevelType(CdiIterator* me, int levelSelector, char **outName = NULL, char **outLongName = NULL, char **outStdName = NULL, char **outUnit = NULL)
555
556
557
@Parameter
    @item iterator The iterator to operate on.
    @item levelSelector Zero for the top level, one for the bottom level
558
559
560
561
    @item outName Will be set to a Malloc()'ed string with the name of the level if not NULL.
    @item outLongName Will be set to a Malloc()'ed string with the long name of the level if not NULL.
    @item outStdName Will be set to a Malloc()'ed string with the standard name of the level if not NULL.
    @item outUnit Will be set to a Malloc()'ed string with the unit of the level if not NULL.
562
563
564
565
566
567
568

@Result An integer indicating the type of the level.

@Description
Find out some basic information about the given level, the levelSelector selects the function of the requested level.
If the requested level does not exist, this returns CDI_UNDEFID.
*/
569
int cdiIterator_inqLevelType(CdiIterator* me, int levelSelector, char **outName, char **outLongName, char **outStdName, char **outUnit)
570
571
572
573
{
  sanityCheck(me);
  switch(me->filetype)
    {
574
#ifdef HAVE_LIBGRIB_API
575
576
577
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_levelType(me, levelSelector, outName, outLongName, outStdName, outUnit);
578
#endif
579

580
#ifdef HAVE_LIBNETCDF
581
582
583
584
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
585
586
#endif
#ifdef HAVE_LIBSERVICE
587
        case FILETYPE_SRV:
588
589
#endif
#ifdef HAVE_LIBEXTRA
590
        case FILETYPE_EXT:
591
592
#endif
#ifdef HAVE_LIBIEG
593
        case FILETYPE_IEG:
594
#endif
595
596
597
598
599
600
601
602
          return cdiFallbackIterator_levelType(me, levelSelector, outName, outLongName, outStdName, outUnit);

      default:
        Error(kUnexpectedFileTypeMessage);
        return CDI_UNDEFID;
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
603
/*
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
@Function cdiIterator_inqLevel
@Title Get the value of the z-coordinate

@Prototype void cdiIterator_inqLevel(CdiIterator* me, int levelSelector, double* outValue1, double* outValue2 = NULL)
@Parameter
    @item iterator The iterator to operate on.
    @item levelSelector Zero for the top level, one for the bottom level
    @item outValue1 For "normal" levels this returns the value, for hybrid levels the first coordinate, for generalized levels the level number.
    @item outValue2 Zero for "normal" levels, for hybrid levels, this returns the second coordinate, for generalized levels the level count.

@Result An error code.

@Description
Returns the value of the z-coordinate, whatever that may be.
*/
int cdiIterator_inqLevel(CdiIterator* me, int levelSelector, double* outValue1, double* outValue2)
{
  sanityCheck(me);
  switch(me->filetype)
    {
624
#ifdef HAVE_LIBGRIB_API
625
626
627
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_level(me, levelSelector, outValue1, outValue2);
628
#endif
629

630
#ifdef HAVE_LIBNETCDF
631
632
633
634
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
635
636
#endif
#ifdef HAVE_LIBSERVICE
637
        case FILETYPE_SRV:
638
639
#endif
#ifdef HAVE_LIBEXTRA
640
        case FILETYPE_EXT:
641
642
#endif
#ifdef HAVE_LIBIEG
643
        case FILETYPE_IEG:
644
#endif
645
646
647
648
649
650
651
652
          return cdiFallbackIterator_level(me, levelSelector, outValue1, outValue2);

      default:
        Error(kUnexpectedFileTypeMessage);
        return CDI_EINVAL;
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
653
/*
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
@Function cdiIterator_inqLevelUuid
@Title Get the UUID of the z-axis used by this field

@Prototype int cdiIterator_inqLevelUuid(CdiIterator* me, int levelSelector, unsigned char (*outUuid)[16])
@Parameter
    @item iterator The iterator to operate on.
    @item outVgridNumber The number of the associated vertical grid description.
    @item outLevelCount The number of levels in the associated vertical grid description.
    @item outUuid A pointer to a user supplied buffer of 16 bytes to store the UUID in.

@Result An error code.

@Description
Returns identifying information for the external z-axis description. May only be called for generalized levels.
*/
669
int cdiIterator_inqLevelUuid(CdiIterator* me, int* outVgridNumber, int* outLevelCount, unsigned char outUuid[CDI_UUID_SIZE])
670
671
672
673
{
  sanityCheck(me);
  switch(me->filetype)
    {
674
#ifdef HAVE_LIBGRIB_API
675
676
677
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_zaxisUuid(me, outVgridNumber, outLevelCount, outUuid);
678
#endif
679

680
#ifdef HAVE_LIBNETCDF
681
682
683
684
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
685
686
#endif
#ifdef HAVE_LIBSERVICE
687
        case FILETYPE_SRV:
688
689
#endif
#ifdef HAVE_LIBEXTRA
690
        case FILETYPE_EXT:
691
692
#endif
#ifdef HAVE_LIBIEG
693
        case FILETYPE_IEG:
694
#endif
695
696
697
698
699
700
701
702
          return cdiFallbackIterator_zaxisUuid(me, outVgridNumber, outLevelCount, outUuid);

      default:
        Error(kUnexpectedFileTypeMessage);
        return CDI_ELIBNAVAIL;
    }
}

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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/*
@Function cdiIterator_inqTile
@Title Inquire the tile information for the current field

@Prototype int cdiIterator_inqTile(CdiIterator* me, int* outTileIndex, int* outTileAttribute)
@Parameter
    @item iterator The iterator to operate on.
    @item outTileIndex The index of the current tile, -1 if no tile information is available.
    @item outTileAttribute The attribute of the current tile, -1 if no tile information is available.

@Result An error code. CDI_EINVAL if there is no tile information associated with the current field.

@Description
Inquire the tile index and attribute for the current field.
*/
int cdiIterator_inqTile(CdiIterator* me, int* outTileIndex, int* outTileAttribute)
{
  sanityCheck(me);
  switch(me->filetype)
    {
      #ifdef HAVE_LIBGRIB_API
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_inqTile(me, outTileIndex, outTileAttribute);
      #endif

      #ifdef HAVE_LIBNETCDF
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
      #endif
      #ifdef HAVE_LIBSERVICE
        case FILETYPE_SRV:
      #endif
      #ifdef HAVE_LIBEXTRA
        case FILETYPE_EXT:
      #endif
      #ifdef HAVE_LIBIEG
        case FILETYPE_IEG:
      #endif
          return cdiFallbackIterator_inqTile(me, outTileIndex, outTileAttribute);

      default:
        Error(kUnexpectedFileTypeMessage);
        return CDI_ELIBNAVAIL;
    }
}

/**
@Function cdiIterator_inqTileCount
@Title Inquire the tile count and tile attribute counts for the current field

@Prototype int cdiIterator_inqTileCount(CdiIterator* me, int* outTileCount, int* outTileAttributeCount)
@Parameter
    @item iterator The iterator to operate on.
    @item outTileCount The number of tiles used for this variable, zero if no tile information is available.
    @item outTileAttributeCount The number of attributes available for the tile of this field, zero if no tile information is available.
          Note: This is not the global attribute count, which would be impossible to infer without reading the entire file if it's a GRIB file.

@Result An error code. CDI_EINVAL if there is no tile information associated with the current field.

@Description
Inquire the tile count and tile attribute counts for the current field.
*/
int cdiIterator_inqTileCount(CdiIterator* me, int* outTileCount, int* outTileAttributeCount)
{
  sanityCheck(me);
  switch(me->filetype)
    {
      #ifdef HAVE_LIBGRIB_API
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_inqTileCount(me, outTileCount, outTileAttributeCount);
      #endif

      #ifdef HAVE_LIBNETCDF
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
      #endif
      #ifdef HAVE_LIBSERVICE
        case FILETYPE_SRV:
      #endif
      #ifdef HAVE_LIBEXTRA
        case FILETYPE_EXT:
      #endif
      #ifdef HAVE_LIBIEG
        case FILETYPE_IEG:
      #endif
          return cdiFallbackIterator_inqTileCount(me, outTileCount, outTileAttributeCount);

      default:
        Error(kUnexpectedFileTypeMessage);
        return CDI_ELIBNAVAIL;
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
802
/*
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
@Function cdiIterator_inqParam
@Title Get discipline, category, and number

@Prototype CdiParam cdiIterator_inqParam(CdiIterator* iterator)
@Parameter
    @item iterator The iterator to operate on.

@Result A struct containing the requested information.

@Description
    Simple metadata inspection function.
*/
CdiParam cdiIterator_inqParam(CdiIterator* me)
{
  sanityCheck(me);
  return me->param;
}

Thomas Jahns's avatar
Thomas Jahns committed
821
/*
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
@Function cdiIterator_inqDatatype
@Title Get the datatype of the current field

@Prototype int cdiIterator_inqDatatype(CdiIterator* iterator)
@Parameter
    @item iterator The iterator to operate on.

@Result The datatype that is used to store this field on disk.

@Description
    Simple metadata inspection function.
*/
int cdiIterator_inqDatatype(CdiIterator* me)
{
  sanityCheck(me);
  return me->datatype;
}

Thomas Jahns's avatar
Thomas Jahns committed
840
/*
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
@Function cdiIterator_inqTsteptype
@Title Get the timestep type

@Prototype int cdiIterator_inqTsteptype(CdiIterator* iterator)
@Parameter
    @item iterator The iterator to operate on.

@Result The timestep type.

@Description
    Simple metadata inspection function.
*/
int cdiIterator_inqTsteptype(CdiIterator* me)
{
  sanityCheck(me);
  return me->timesteptype;
}

Thomas Jahns's avatar
Thomas Jahns committed
859
/*
860
861
862
863
864
865
866
@Function cdiIterator_inqVariableName
@Title Get the variable name of the current field

@Prototype char* cdiIterator_inqVariableName(CdiIterator* iterator)
@Parameter
    @item iterator The iterator to operate on.

867
@Result A pointer to a C-string containing the name. The storage for this string is allocated with Malloc(), and it is the responsibility of the caller to Free() it.
868
869
870

@Description
    Allocates a buffer to hold the string, copies the current variable name into this buffer, and returns the buffer.
871
    The caller is responsible to make the corresponding Free() call.
872
873
874
875
876
877
*/
char* cdiIterator_inqVariableName(CdiIterator* me)
{
  sanityCheck(me);
  switch(me->filetype)
    {
878
#ifdef HAVE_LIBGRIB_API
879
880
881
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          return cdiGribIterator_copyVariableName(me);
882
#endif
883

884
#ifdef HAVE_LIBNETCDF
885
886
887
888
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
889
890
#endif
#ifdef HAVE_LIBSERVICE
891
        case FILETYPE_SRV:
892
893
#endif
#ifdef HAVE_LIBEXTRA
894
        case FILETYPE_EXT:
895
896
#endif
#ifdef HAVE_LIBIEG
897
        case FILETYPE_IEG:
898
#endif
899
900
901
902
903
904
905
906
          return cdiFallbackIterator_copyVariableName(me);

      default:
        Error(kUnexpectedFileTypeMessage);
        return NULL;
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
907
/*
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
@Function cdiIterator_inqGridId
@Title Get the ID of the current grid

@Prototype int cdiIterator_inqGridId(CdiIterator* iterator)
@Parameter
    @item iterator The iterator to operate on.

@Result A gridId that can be used for further introspection.

@Description
    This provides access to the grid related metadata.
    The resulting ID is only valid until the next time cdiIterator_nextField() is called.
*/
int cdiIterator_inqGridId(CdiIterator* me)
{
  sanityCheck(me);
  return me->gridId;
}

Thomas Jahns's avatar
Thomas Jahns committed
927
/*
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
@Function cdiIterator_readField
@Title Read the whole field into a double buffer

@Prototype void cdiIterator_readField(CdiIterator* me, double* buffer, size_t* nmiss)
@Parameter
    @item iterator The iterator to operate on.
    @item buffer A pointer to the double array that the data should be written to.
    @item nmiss A pointer to a variable where the count of missing values will be stored. May be NULL.

@Description
    It is assumed that the caller first analyses the return value of cdiIterator_inqGridId to determine the required size of the buffer.
    Failing to do so results in undefined behavior. You have been warned.
*/
void cdiIterator_readField(CdiIterator* me, double* buffer, size_t* nmiss)
{
  sanityCheck(me);
  if(!buffer) xabort("NULL was passed in a buffer. Please provide a suitable buffer.");
  switch(me->filetype)
    {
947
#ifdef HAVE_LIBGRIB_API
948
949
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
950
951
          cdiGribIterator_readField(me, buffer, nmiss);
	  return;
952
#endif
953

954
#ifdef HAVE_LIBNETCDF
955
956
957
958
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
959
960
#endif
#ifdef HAVE_LIBSERVICE
961
        case FILETYPE_SRV:
962
963
#endif
#ifdef HAVE_LIBEXTRA
964
        case FILETYPE_EXT:
965
966
#endif
#ifdef HAVE_LIBIEG
967
        case FILETYPE_IEG:
968
#endif
969
970
          cdiFallbackIterator_readField(me, buffer, nmiss);
          return;
971
972
973
974
975
      default:
        Error(kUnexpectedFileTypeMessage);
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
976
/*
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
@Function cdiIterator_readFieldF
@Title Read the whole field into a double buffer

@Prototype void cdiIterator_readFieldF(CdiIterator* me, float* buffer, size_t* nmiss)
@Parameter
    @item iterator The iterator to operate on.
    @item buffer A pointer to the double array that the data should be written to.
    @item nmiss A pointer to a variable where the count of missing values will be stored. May be NULL.

@Description
    It is assumed that the caller first analyses the return value of cdiIterator_inqGridId to determine the required size of the buffer.
    Failing to do so results in undefined behavior. You have been warned.
*/
void cdiIterator_readFieldF(CdiIterator* me, float* buffer, size_t* nmiss)
{
  sanityCheck(me);
  if(!buffer) xabort("NULL was passed in a buffer. Please provide a suitable buffer.");
  switch(me->filetype)
    {
996
#ifdef HAVE_LIBGRIB_API
997
998
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
999
1000
          cdiGribIterator_readFieldF(me, buffer, nmiss);
	  return;
1001
#endif
1002

1003
#ifdef HAVE_LIBNETCDF
1004
1005
1006
1007
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
1008
1009
#endif
#ifdef HAVE_LIBSERVICE
1010
        case FILETYPE_SRV:
1011
1012
#endif
#ifdef HAVE_LIBEXTRA
1013
        case FILETYPE_EXT:
1014
1015
#endif
#ifdef HAVE_LIBIEG
1016
        case FILETYPE_IEG:
1017
#endif
1018
1019
          cdiFallbackIterator_readFieldF(me, buffer, nmiss);
          return; 
1020
1021
1022
1023
1024
      default:
        Error(kUnexpectedFileTypeMessage);
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
1025
/*
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
@Function cdiIterator_delete
@Title Destroy an iterator

@Prototype void cdiIterator_delete(CdiIterator* iterator)
@Parameter
    @item iterator The iterator to operate on.

@Description
    Combined destructor & deallocator.
*/
void cdiIterator_delete(CdiIterator* me)
{
  if(!me) xabort("NULL was passed in as an iterator. Please check the return value of cdiIterator_new().");
  switch(me->filetype)
    {
1041
#ifdef HAVE_LIBGRIB_API
1042
1043
1044
1045
        case FILETYPE_GRB:
        case FILETYPE_GRB2:
          cdiGribIterator_delete((CdiGribIterator*)me);
          break;
1046
#endif
1047

1048
#ifdef HAVE_LIBNETCDF
1049
1050
1051
1052
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
1053
1054
#endif
#ifdef HAVE_LIBSERVICE
1055
        case FILETYPE_SRV:
1056
1057
#endif
#ifdef HAVE_LIBEXTRA
1058
        case FILETYPE_EXT:
1059
1060
#endif
#ifdef HAVE_LIBIEG
1061
        case FILETYPE_IEG:
1062
#endif
1063
1064
1065
1066
1067
1068
1069
1070
          cdiFallbackIterator_delete(me);
          break;

      default:
        Error(kUnexpectedFileTypeMessage);
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
1071
1072
1073
1074
1075
void baseIterDestruct(CdiIterator* me)
{
  /*currently empty, but that's no reason not to call it*/
  (void)me;
}
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085

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