cdi_int.h 12.6 KB
Newer Older
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>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
14
15
#include <math.h>
#include <sys/types.h>

16
17
#include "cdi.h"

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

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

39
40
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
42
43
44
45
46
47
48
49
#endif


#ifndef  _ERROR_H
#  include "error.h"
#endif
#ifndef _BASETIME_H
#  include "basetime.h"
#endif
50
51
52
#ifndef _TIMEBASE_H
#  include "timebase.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef  _TAXIS_H
#  include "taxis.h"
#endif
#ifndef  _CDI_LIMITS_H
#  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
68
69
70
#ifndef RESOURCE_HANDLE_H
#  include "resource_handle.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71

Thomas Jahns's avatar
Thomas Jahns committed
72

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

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

91
92
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
93
#  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))
94
95
#endif

96
97
98
99
100
#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
101
102
103
104
105
106
#define  FALSE  0
#define  TRUE   1

#define  TYPE_REC  0
#define  TYPE_VAR  1

107
108
109
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
111
typedef struct
{
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  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
129
130
131
}
Record;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
146
147
148

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


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
  int      *recIDs;      /* IDs of non constant records           */
Thomas Jahns's avatar
Thomas Jahns committed
175
  int       recordSize;  /* number of allocated records           */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
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                     */
  long      next;
  off_t     position;    /* timestep file position                */
183
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
}
185
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
187
188
189


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


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

  int            gridID;
  int            zaxisID;
205
206
  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
207
}
208
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
210
211
212
213
214
215
216
217
218


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

219
220
221
222
223
224
225
226
227
228
229
#ifdef HAVE_LIBNETCDF
typedef struct {
  int gridID;
  int xdimID;
  int ydimID;
  int xvarID;
  int yvarID;
  int avarID;
}
ncgrid_t;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
231

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

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
280


281
282
283
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

284
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302

/* 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 */
  int                    update;
  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;

303
304
305
306
307
308
309
//enum for differenciating between the different times that we handle
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

310

311
#define  CDI_FILETYPE_UNDEF          -1   /* Unknown/not yet defined file type */
312
313


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
343

Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
345
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
346
347
348
349
350
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
351
  return (stream_t *)reshGetVal(idx, &streamOps);
352
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353

354
355
356
357
358
359
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
360

Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
362
int     streamInqFileID(int streamID);

363
364
365
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366

367
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369
370
371
372

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
Thomas Jahns's avatar
Thomas Jahns committed
373
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID);
374
/* default implementation of the overridable function */
Thomas Jahns's avatar
Thomas Jahns committed
375
void    cdiStreamSetupVlist_(stream_t *streamptr, int vlistID);
376
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377

378
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379

380
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381

382
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
383

384
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
385

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

388
389
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

392
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393

Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398
399
400

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
401
402
int     getByteswap(int byteorder);

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

405
406
void  cdiInitialize(void);

407
408
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
409

410
411
412
413
414
415
#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
416

417
418
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
419
  int streamID, vlistID;
420
421
422
};

struct streamAssoc
423
424
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
425

426
int
427
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
428
429
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
430

431
432
433
434
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

435
436
void
cdiStreamDefVlist_(int streamID, int vlistID);
437
438
439
440

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

441
void
Thomas Jahns's avatar
Thomas Jahns committed
442
cdiStreamWriteVarChunk_(int streamID, int varID, int memtype,
443
444
                        const int rect[][2], const void *data, int nmiss);
void
445
446
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
447

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

Thomas Jahns's avatar
Thomas Jahns committed
450
451
void cdiStreamSync_(stream_t *streamptr);

452
const char *cdiUnitNamePtr(int cdi_unit);
453

454
455
456
457
458
459
460
enum {
  /* 8192 is known to work on most systems (4096 isn't on Alpha) */
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

461
462
463
464
465
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

466
467
468
469
470
471
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

472
473
void cdiDefTableID(int tableID);

474
475
476
477
478
479
480
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);

#if defined (__cplusplus)
}
#endif

481
#endif  /* _CDI_INT_H */
482
483
484
485
486
487
488
489
490
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */