cdi_int.h 12.7 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
#endif


44
#ifndef  ERROR_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
46
47
48
49
#  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
#ifdef HAVE_LIBNETCDF
220
221
enum {
  CDF_DIMID_X,
222
  CDF_DIMID_Y,
223
  CDF_VARID_X,
224
  CDF_VARID_Y,
225
  CDF_VARID_A,
226
227
  CDF_SIZE_ncIDs,
};
228
229
typedef struct {
  int gridID;
230
  int ncIDs[CDF_SIZE_ncIDs];
231
232
233
}
ncgrid_t;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
235

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

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


285
286
287
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

288
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

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

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

314

315
#define  CDI_FILETYPE_UNDEF          -1   /* Unknown/not yet defined file type */
316
317


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
348

Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
351
352
353
354
355
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
356
  return (stream_t *)reshGetVal(idx, &streamOps);
357
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
358

359
360
361
362
363
364
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
365

Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
int     streamInqFileID(int streamID);

368
369
370
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371

372
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
374
375
376
377

void    streamDefineTaxis(int streamID);

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

383
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384

385
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
386

387
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388

389
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390

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

393
394
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

397
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
398

Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
402
403
404
405

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
406
407
int     getByteswap(int byteorder);

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

410
411
void  cdiInitialize(void);

412
413
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
414

415
416
417
418
419
420
#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
421

422
423
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
424
  int streamID, vlistID;
425
426
427
};

struct streamAssoc
428
429
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
430

431
int
432
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
433
434
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
435

436
437
438
439
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

440
441
void
cdiStreamDefVlist_(int streamID, int vlistID);
442
443
444
445

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

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

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

Thomas Jahns's avatar
Thomas Jahns committed
455
456
void cdiStreamSync_(stream_t *streamptr);

457
const char *cdiUnitNamePtr(int cdi_unit);
458

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

size_t cdiGetPageSize(bool largePageAlign);

466
467
468
469
470
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

471
472
473
474
475
476
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

477
478
void cdiDefTableID(int tableID);

479
480
481
482
483
484
485
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

486
#endif  /* _CDI_INT_H */
487
488
489
490
491
492
493
494
495
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */