cdi_int.h 13.1 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
43
void str_tolower(char *str);
bool str_is_equal(const char *vstr, const char *cstr);

44
45
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
48
#endif


49
#ifndef  ERROR_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
51
52
53
54
#  include "error.h"
#endif
#ifndef _BASETIME_H
#  include "basetime.h"
#endif
55
#ifndef TIMEBASE_H
56
57
#  include "timebase.h"
#endif
58
#ifndef TAXIS_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
#  include "taxis.h"
#endif
61
#ifndef CDI_LIMITS_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
63
64
65
66
67
68
69
70
71
72
#  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
73
74
75
#ifndef RESOURCE_HANDLE_H
#  include "resource_handle.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76

Thomas Jahns's avatar
Thomas Jahns committed
77

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

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

96
97
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
98
#  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))
99
100
#endif

101
102
103
104
105
#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
106
107
108
109
110
111
#define  FALSE  0
#define  TRUE   1

#define  TYPE_REC  0
#define  TYPE_VAR  1

112
113
114
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
typedef struct
{
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  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
134
135
136
}
Record;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
151
152
153

typedef struct
{
  off_t     position;
  size_t    size;
154
  size_t    gridsize;
155
  int       zip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
  int       param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
159
  int       ilevel;
  int       ilevel2;
  int       ltype;
Thomas Jahns's avatar
Thomas Jahns committed
160
  short     tsteptype;
161
162
163
164
165
166
167
168
#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
169
170
  short     varID;
  short     levelID;
171
  short     used;
172
  char      varname[32]; /* needed for grib decoding with GRIB_API */
173
  var_tile_t tiles;      /* tile-related meta-data, currently for GRIB-API only. */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
record_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
  int      *recIDs;      /* IDs of non constant records           */
Thomas Jahns's avatar
Thomas Jahns committed
181
  int       recordSize;  /* number of allocated records           */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
184
185
186
  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                     */
187
  bool      next;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
  off_t     position;    /* timestep file position                */
189
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
}
191
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
193
194
195


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


typedef struct {
  int            ncvarid;
  int            subtypeSize;
205
206
207
  sleveltable_t *recordTable; // record IDs for each subtype
  bool           defmiss;     // true: if missval is defined in file
  bool           isUsed;
208
209
210

  int            gridID;
  int            zaxisID;
211
212
  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
213
}
214
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
217
218
219
220
221
222
223
224


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

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

typedef struct {
242
243
244
245
246
247
248
  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
249
  int         nrecs;        /* number of records                  */
250
  size_t      numvals;
251
252
253
  char       *filename;
  Record     *record;
  svarinfo_t *vars;
Thomas Jahns's avatar
Thomas Jahns committed
254
  int         nvars;        /* number of variables                */
255
256
257
258
259
260
261
  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;
262
  basetime_t  basetime;
263
264
  int         ncmode;
  int         vlistID;
265
#ifdef HAVE_LIBNETCDF
266
  ncgrid_t    ncgrid[MAX_GRIDS_PS];
267
  int         zaxisID[MAX_ZAXES_PS];	//Warning: synchronous array to vlist_to_pointer(vlistID)->zaxisIDs
268
  int         nczvarID[MAX_ZAXES_PS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
  VCT         vct;
#endif
271
272
273
274
275
276
277
  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
278
279
  bool        sortname;
  bool        sortparam;
280
281
282
283
284
285
286
#if defined (GRIBCONTAINER2D)
  void      **gribContainers;
#else
  void       *gribContainers;
#endif

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290


291
292
293
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

294
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
295
296
297
298
299
300
301
302
303
304
305

/* 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 */
306
  bool                   update;
307
308
309
310
311
312
  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;

313
314
315
316
317
318
319
//enum for differenciating between the different times that we handle
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

320

321
#define  CDI_FILETYPE_UNDEF          -1   /* Unknown/not yet defined file type */
322
323


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
355

Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
357
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
358
359
360
361
362
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
363
  return (stream_t *)reshGetVal(idx, &streamOps);
364
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365

366
367
368
369
370
371
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
372

Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
374
int     streamInqFileID(int streamID);

375
376
377
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378

379
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
381
382
383
384

void    streamDefineTaxis(int streamID);

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

390
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391

392
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393

394
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395

396
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397

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

400
401
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

404
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405

Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
408
409
410
411
412

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
413
414
int     getByteswap(int byteorder);

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

417
418
void  cdiInitialize(void);

419
420
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
421

422
423
424
425
426
427
#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
428

429
430
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
431
  int streamID, vlistID;
432
433
434
};

struct streamAssoc
435
436
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
437

438
int
439
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
440
441
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
442

443
444
445
446
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

447
448
void
cdiStreamDefVlist_(int streamID, int vlistID);
449
450

int
451
cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, size_t nmiss);
452

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

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

Thomas Jahns's avatar
Thomas Jahns committed
462
463
void cdiStreamSync_(stream_t *streamptr);

464
const char *cdiUnitNamePtr(int cdi_unit);
465

466
467
468
469
470
471
472
enum {
  /* 8192 is known to work on most systems (4096 isn't on Alpha) */
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

473
474
475
476
477
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

478
479
480
481
482
483
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

484
485
void cdiDefTableID(int tableID);

486
487
488
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);

489
490
491
492
493
494
495
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);
}


496
497
498
499
#if defined (__cplusplus)
}
#endif

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