cdi_int.h 13 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
#ifndef  CDI_INT_H
#define  CDI_INT_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
3
4

#if defined (HAVE_CONFIG_H)
5
#include "config.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
#include <assert.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
9
#include <stdio.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
10
#include <stdbool.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
#include <string.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
#include <errno.h>
13
#include <limits.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
15
16
#include <math.h>
#include <sys/types.h>

17
#include "cdi.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
#include "cdf_config.h"
19

20
/* dummy use of unused parameters to silence compiler warnings */
21
#ifndef UNUSED
22
#define  UNUSED(x) (void)x
23
#endif
24

Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
#ifndef strdupx
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28
29
30
#ifndef strdup
char *strdup(const char *s);
#endif
#define strdupx  strdup
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
32
33
34
#define strdupx(s)			          \
({					      	  \
   const char *__old = (s);			  \
   size_t __len = strlen(__old) + 1;		  \
35
   char *__new = (char *) Malloc(__len);	  \
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
   (char *) memcpy(__new, __old, __len);	  \
})
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
40
#endif

41
42
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
#endif


46
#ifndef  ERROR_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
49
50
51
#  include "error.h"
#endif
#ifndef _BASETIME_H
#  include "basetime.h"
#endif
52
53
54
#ifndef _TIMEBASE_H
#  include "timebase.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
57
#ifndef  _TAXIS_H
#  include "taxis.h"
#endif
58
#ifndef  CDI_LIMITS_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
61
62
63
64
65
66
67
68
69
#  include "cdi_limits.h"
#endif
#ifndef  _SERVICE_H
#  include "service.h"
#endif
#ifndef  _EXTRA_H
#  include "extra.h"
#endif
#ifndef  _IEG_H
#  include "ieg.h"
#endif
70
71
72
#ifndef RESOURCE_HANDLE_H
#  include "resource_handle.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73

Thomas Jahns's avatar
Thomas Jahns committed
74

Deike Kleberg's avatar
Deike Kleberg committed
75
76
#define check_parg(arg)  if ( arg == 0 ) Warning("Argument '" #arg "' not allocated!")

Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
#if defined (__xlC__) /* performance problems on IBM */
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
78
79
80
81
#ifndef DBL_IS_NAN
#  define DBL_IS_NAN(x)     ((x) != (x))
#endif
#else
82
#ifndef DBL_IS_NAN
83
#if  defined  (HAVE_DECL_ISNAN)
84
#  define DBL_IS_NAN(x)     (isnan(x))
85
#elif  defined  (FP_NAN)
86
#  define DBL_IS_NAN(x)     (fpclassify(x) == FP_NAN)
87
#else
88
#  define DBL_IS_NAN(x)     ((x) != (x))
89
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92

93
94
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
95
#  define DBL_IS_EQUAL(x,y) (DBL_IS_NAN(x)||DBL_IS_NAN(y)?(DBL_IS_NAN(x)&&DBL_IS_NAN(y)):!(x < y || y < x))
96
97
#endif

98
99
100
101
102
#ifndef IS_EQUAL
#  define IS_NOT_EQUAL(x,y) (x < y || y < x)
#  define IS_EQUAL(x,y)     (!IS_NOT_EQUAL(x,y))
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
105
106
107
108
#define  FALSE  0
#define  TRUE   1

#define  TYPE_REC  0
#define  TYPE_VAR  1

109
110
111
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
113
typedef struct
{
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  void     *buffer;             /* gribapi, cgribex */
  size_t    buffersize;         /* gribapi, cgribex */
  off_t     position;           /* ieg */
  int       param;              /* srv */
  int       level;              /* ext, srv */
  int       date;               /* ext, srv */
  int       time;               /* srv */
  int       gridID;             /* ieg, ext */
  int       varID;              /* ieg */
  int       levelID;            /* ieg  */
  int       prec;               /* ext, srv */
  int       sec0[2];            /* cgribex */
  int       sec1[1024];         /* cgribex */
  int       sec2[4096];         /* cgribex */
  int       sec3[2];            /* cgribex */
  int       sec4[512];          /* cgribex */
  void     *exsep;              /* ieg, ext, srv */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
}
Record;

134
135
136
137
/* data structure specifying tile-related meta-data. structure
 * contains "-1" if this is no tile-variable. */
typedef struct {
  int
138
139
    tileindex,
    totalno_of_tileattr_pairs,
140
141
142
143
144
145
    tileClassification,
    numberOfTiles,
    numberOfAttributes,
    attribute;
} var_tile_t;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
148
149
150

typedef struct
{
  off_t     position;
  size_t    size;
151
  int       zip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
  int       param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
  int       ilevel;
  int       ilevel2;
  int       ltype;
Thomas Jahns's avatar
Thomas Jahns committed
156
  short     tsteptype;
157
158
159
160
161
162
163
164
#ifdef HIRLAM_EXTENSIONS
    // NOTE: tsteptype MUST be part of attributes used to compare variables!
    // Modern NWP models (HARMONIE, HIRLAM) use timeRangeIndicator to specify
    // if the field is instantanous or accumulated.
    // Both types are typically in the same GRIB-file.
    // (181; 105, 0, timeRangeIndicator=0) .. instantanous rain
    // (181; 105, 0, timeRangeIndicator=4) .. accumulated rain  .. both can be in the same grib file
#endif // HIRLAM_EXTENSIONS
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
  short     varID;
  short     levelID;
167
  short     used;
168
  char      varname[32]; /* needed for grib decoding with GRIB_API */
169
  var_tile_t tiles;      /* tile-related meta-data, currently for GRIB-API only. */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
record_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
173
174


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
  int      *recIDs;      /* IDs of non constant records           */
Thomas Jahns's avatar
Thomas Jahns committed
177
  int       recordSize;  /* number of allocated records           */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
180
181
182
  int       nrecs;       /* number of used records                */
                         /* tsID=0 nallrecs                       */
                         /* tsID>0 number of non constant records */
  int       nallrecs;    /* number of all records                 */
  int       curRecID;    /* current record ID                     */
183
  bool      next;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
  off_t     position;    /* timestep file position                */
185
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
}
187
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189
190
191


typedef struct {
  int       nlevs;
Thomas Jahns's avatar
Thomas Jahns committed
192
  int       subtypeIndex; /* corresponding tile in subtype_t structure (subtype->self) */
193
194
195
196
197
198
199
200
  int      *recordID;     /* record IDs: [nlevs] */
  int      *lindex;       /* level index */
} sleveltable_t;


typedef struct {
  int            ncvarid;
  int            subtypeSize;
201
202
203
  sleveltable_t *recordTable; // record IDs for each subtype
  bool           defmiss;     // true: if missval is defined in file
  bool           isUsed;
204
205
206

  int            gridID;
  int            zaxisID;
207
208
  int            tsteptype;   // TSTEP_*
  int            subtypeID;   // subtype ID, e.g. for tile-related meta-data (currently for GRIB-API only).
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
}
210
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
212
213
214
215
216
217
218
219
220


typedef struct {
  int       ilev;
  int       mlev;
  int       ilevID;
  int       mlevID;
}
VCT;

221
#ifdef HAVE_LIBNETCDF
222
223
enum {
  CDF_DIMID_X,
224
  CDF_DIMID_Y,
225
  CDF_VARID_X,
226
  CDF_VARID_Y,
227
  CDF_VARID_A,
228
229
  CDF_SIZE_ncIDs,
};
230
231
typedef struct {
  int gridID;
232
  int ncIDs[CDF_SIZE_ncIDs];
233
234
235
}
ncgrid_t;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
237

typedef struct {
238
239
240
241
242
243
244
  int         self;
  int         accesstype;   /* TYPE_REC or TYPE_VAR */
  int         accessmode;
  int         filetype;
  int         byteorder;
  int         fileID;
  int         filemode;
Thomas Jahns's avatar
Thomas Jahns committed
245
  int         nrecs;        /* number of records                  */
246
  size_t      numvals;
247
248
249
  char       *filename;
  Record     *record;
  svarinfo_t *vars;
Thomas Jahns's avatar
Thomas Jahns committed
250
  int         nvars;        /* number of variables                */
251
252
253
254
255
256
257
  int         varsAllocated;
  int         curTsID;      /* current timestep ID */
  int         rtsteps;      /* number of tsteps accessed       */
  long        ntsteps;      /* number of tsteps : only set if all records accessed */
  tsteps_t   *tsteps;
  int         tstepsTableSize;
  int         tstepsNextID;
258
  basetime_t  basetime;
259
260
  int         ncmode;
  int         vlistID;
261
#ifdef HAVE_LIBNETCDF
262
  ncgrid_t    ncgrid[MAX_GRIDS_PS];
263
  int         zaxisID[MAX_ZAXES_PS];	//Warning: synchronous array to vlist_to_pointer(vlistID)->zaxisIDs
264
  int         nczvarID[MAX_ZAXES_PS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
  VCT         vct;
#endif
267
268
269
270
271
272
273
  int         historyID;
  int         globalatts;
  int         localatts;
  int         unreduced;
  int         have_missval;
  int         comptype;      // compression type
  int         complevel;     // compression level
Uwe Schulzweida's avatar
Uwe Schulzweida committed
274
275
  bool        sortname;
  bool        sortparam;
276
277
278
279
280
281
282
#if defined (GRIBCONTAINER2D)
  void      **gribContainers;
#else
  void       *gribContainers;
#endif

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286


287
288
289
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

290
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
291
292
293
294
295
296
297
298
299
300
301

/* Data type specification for optional key/value pairs (GRIB) */
typedef enum {
  t_double = 0,
  t_int    = 1
} key_val_pair_datatype;

/* Data structure holding optional key/value pairs for GRIB */
typedef struct
{
  char*                  keyword;        /* keyword string */
302
  bool                   update;
303
304
305
306
307
308
  key_val_pair_datatype  data_type;      /* data type of this key/value pair */
  double                 dbl_val;        /* double value (data_type == t_double) */
  int                    int_val;        /* integer value (data_type == t_int) */
  int                    subtype_index;  /* tile index for this key-value pair */
} opt_key_val_pair_t;

309
310
311
312
313
314
315
//enum for differenciating between the different times that we handle
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

316

317
#define  CDI_FILETYPE_UNDEF          -1   /* Unknown/not yet defined file type */
318
319


320
extern int cdiDebugExt;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321
extern int CDI_Debug;      /* If set to 1, debuggig (default 0)            */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
extern int CDI_Recopt;
323
extern int cdiGribApiDebug;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
325
326
327
328
extern double cdiDefaultMissval;
extern int cdiDefaultInstID;
extern int cdiDefaultModelID;
extern int cdiDefaultTableID;
extern int cdiDefaultLeveltype;
329
extern int cdiDefaultCalendar;
330
//extern int cdiNcMissingValue;
331
extern int cdiNcChunksizehint;
332
extern int cdiChunkType;
333
extern int cdiSplitLtype105;
334
335
extern int cdiDataUnreduced;
extern int cdiSortName;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
336
extern int cdiSortParam;
337
extern int cdiHaveMissval;
338
339
340
extern bool cdiIgnoreAttCoordinates;
extern bool cdiCoordinatesLonLat;
extern bool cdiIgnoreValidRange;
341
342
343
344
extern int cdiSkipRecords;
extern int cdiConvention;
extern int cdiInventoryMode;
extern int CDI_Version_Info;
Thomas Jahns's avatar
Thomas Jahns committed
345
extern int CDI_cmor_mode;
346
extern int CDI_reduce_dim;
347
extern size_t CDI_netcdf_hdr_pad;
348
extern bool CDI_netcdf_lazy_grid_load;
349
350
extern int STREAM_Debug;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
351

Uwe Schulzweida's avatar
Uwe Schulzweida committed
352
353
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
354
355
356
357
358
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
359
  return (stream_t *)reshGetVal(idx, &streamOps);
360
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
361

362
363
364
365
366
367
static inline void
stream_check_ptr(const char *caller, stream_t *streamptr)
{
  if ( streamptr == NULL )
    Errorc("stream undefined!");
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368

Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
int     streamInqFileID(int streamID);

371
372
373
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374

375
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377
378
379
380

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
Thomas Jahns's avatar
Thomas Jahns committed
381
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID);
382
/* default implementation of the overridable function */
Thomas Jahns's avatar
Thomas Jahns committed
383
void    cdiStreamSetupVlist_(stream_t *streamptr, int vlistID);
384
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
385

386
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387

388
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389

390
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391

392
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393

394
void    cdi_create_records(stream_t *streamptr, int tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395

396
397
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

398
int     recordNewEntry(stream_t *streamptr, int tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399

400
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401

Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
405
406
407
408

void    cdiCheckZaxis(int zaxisID);

void    cdiDefAccesstype(int streamID, int type);
int     cdiInqAccesstype(int streamID);

Deike Kleberg's avatar
Deike Kleberg committed
409
410
int     getByteswap(int byteorder);

411
void cdiStreamGetIndexList(unsigned numIDs, int IDs[]);
Deike Kleberg's avatar
Deike Kleberg committed
412

413
414
void  cdiInitialize(void);

415
416
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
417

418
419
420
421
422
423
#define CDI_UNIT_PA   1
#define CDI_UNIT_HPA  2
#define CDI_UNIT_MM   3
#define CDI_UNIT_CM   4
#define CDI_UNIT_DM   5
#define CDI_UNIT_M    6
424

425
426
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
427
  int streamID, vlistID;
428
429
430
};

struct streamAssoc
431
432
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
433

434
int
435
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
436
437
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
438

439
440
441
442
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

443
444
void
cdiStreamDefVlist_(int streamID, int vlistID);
445
446

int
447
cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, size_t nmiss);
448

449
void
Thomas Jahns's avatar
Thomas Jahns committed
450
cdiStreamWriteVarChunk_(int streamID, int varID, int memtype,
451
                        const int rect[][2], const void *data, size_t nmiss);
452
void
453
454
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
455

Thomas Jahns's avatar
Thomas Jahns committed
456
457
int cdiStreamDefTimestep_(stream_t *streamptr, int tsID);

Thomas Jahns's avatar
Thomas Jahns committed
458
459
void cdiStreamSync_(stream_t *streamptr);

460
const char *cdiUnitNamePtr(int cdi_unit);
461

462
463
464
465
466
467
468
enum {
  /* 8192 is known to work on most systems (4096 isn't on Alpha) */
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

469
470
471
472
473
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

void zaxisDefLtype2(int zaxisID, int ltype2);
int  zaxisInqLtype2(int zaxisID);

474
475
476
477
478
479
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

480
481
void cdiDefTableID(int tableID);

482
483
484
void gridGenXvals(int xsize, double xfirst, double xlast, double xinc, double *xvals);
void gridGenYvals(int gridtype, int ysize, double yfirst, double ylast, double yinc, double *yvals);

485
486
487
488
489
490
491
static inline
void cdi_check_gridsize_int_limit(const char *format, size_t gridsize)
{
  if ( gridsize > INT_MAX ) Error("%s format grid size (%zu) limit exceeded (%zu)!", format, gridsize, INT_MAX);
}


492
493
494
495
#if defined (__cplusplus)
}
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
#endif  /* CDI_INT_H */
497
498
499
500
501
502
503
504
505
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */