cdi_int.h 12.1 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;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
156
157
  short     used;
  short     varID;
  short     levelID;
158
  char      varname[32]; /* needed for grib decoding with GRIB_API */
159
  var_tile_t tiles;      /* tile-related meta-data, currently for GRIB-API only. */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
record_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
163
164


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
  int      *recIDs;      /* IDs of non constant records           */
Thomas Jahns's avatar
Thomas Jahns committed
167
  int       recordSize;  /* number of allocated records           */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
169
170
171
172
173
174
  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                */
175
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
}
177
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
180
181


typedef struct {
  int       nlevs;
Thomas Jahns's avatar
Thomas Jahns committed
182
  int       subtypeIndex; /* corresponding tile in subtype_t structure (subtype->self) */
183
184
185
186
187
188
189
190
  int      *recordID;     /* record IDs: [nlevs] */
  int      *lindex;       /* level index */
} sleveltable_t;


typedef struct {
  int            ncvarid;
  int            subtypeSize;
191
192
193
  sleveltable_t *recordTable; // record IDs for each subtype
  bool           defmiss;     // true: if missval is defined in file
  bool           isUsed;
194
195
196

  int            gridID;
  int            zaxisID;
197
198
  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
199
}
200
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
203
204
205
206
207
208
209
210


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

211
212
213
214
215
216
217
218
219
220
221
#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
222
223

typedef struct {
224
225
226
227
228
229
230
  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
231
  int         nrecs;        /* number of records                  */
232
233
234
235
  off_t       numvals;
  char       *filename;
  Record     *record;
  svarinfo_t *vars;
Thomas Jahns's avatar
Thomas Jahns committed
236
  int         nvars;        /* number of variables                */
237
238
239
240
241
242
243
  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;
244
  basetime_t  basetime;
245
246
  int         ncmode;
  int         vlistID;
247
#ifdef HAVE_LIBNETCDF
248
  ncgrid_t    ncgrid[MAX_GRIDS_PS];
249
  int         zaxisID[MAX_ZAXES_PS];	//Warning: synchronous array to vlist_to_pointer(vlistID)->zaxisIDs
250
  int         nczvarID[MAX_ZAXES_PS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
  VCT         vct;
#endif
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  int         historyID;
  int         globalatts;
  int         localatts;
  int         unreduced;
  int         sortname;
  int         have_missval;
  int         comptype;      // compression type
  int         complevel;     // compression level
#if defined (GRIBCONTAINER2D)
  void      **gribContainers;
#else
  void       *gribContainers;
#endif

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
270
271


272
273
274
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

275
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

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

294
295
296
297
298
299
300
//enum for differenciating between the different times that we handle
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

301

302
#define  CDI_FILETYPE_UNDEF          -1   /* Unknown/not yet defined file type */
303
304


Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
extern int CDI_Debug;      /* If set to 1, debuggig (default 0)            */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
extern int CDI_Recopt;
307
extern int cdiGribApiDebug;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
309
310
311
312
extern double cdiDefaultMissval;
extern int cdiDefaultInstID;
extern int cdiDefaultModelID;
extern int cdiDefaultTableID;
extern int cdiDefaultLeveltype;
313
extern int cdiDefaultCalendar;
314
//extern int cdiNcMissingValue;
315
extern int cdiNcChunksizehint;
316
extern int cdiChunkType;
317
extern int cdiSplitLtype105;
318
319
320
extern int cdiDataUnreduced;
extern int cdiSortName;
extern int cdiHaveMissval;
321
322
323
324
325
326
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
327
extern int CDI_cmor_mode;
328
extern size_t CDI_netcdf_hdr_pad;
329
extern bool CDI_netcdf_lazy_grid_load;
330
331
extern int STREAM_Debug;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
332

Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
334
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
335
336
337
338
339
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
340
  return (stream_t *)reshGetVal(idx, &streamOps);
341
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342

343
344
345
346
347
348
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
349

Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
351
int     streamInqFileID(int streamID);

352
353
354
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355

356
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358
359
360
361

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
Thomas Jahns's avatar
Thomas Jahns committed
362
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID);
363
/* default implementation of the overridable function */
Thomas Jahns's avatar
Thomas Jahns committed
364
void    cdiStreamSetupVlist_(stream_t *streamptr, int vlistID);
365
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366

367
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368

369
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370

371
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372

373
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374

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

377
378
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

381
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382

Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
385
386
387
388
389
390
391

void    cdiCheckZaxis(int zaxisID);

void    cdiPrintDatatypes(void);

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

Deike Kleberg's avatar
Deike Kleberg committed
392
393
int     getByteswap(int byteorder);

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

396
397
void  cdiInitialize(void);

398
399
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
400

401
402
403
404
405
406
#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
407

408
409
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
410
  int streamID, vlistID;
411
412
413
};

struct streamAssoc
414
415
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
416

417
int
418
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
419
420
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
421

422
423
424
425
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

426
427
void
cdiStreamDefVlist_(int streamID, int vlistID);
428
429
430
431

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

432
void
Thomas Jahns's avatar
Thomas Jahns committed
433
cdiStreamWriteVarChunk_(int streamID, int varID, int memtype,
434
435
                        const int rect[][2], const void *data, int nmiss);
void
436
437
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
438

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

Thomas Jahns's avatar
Thomas Jahns committed
441
442
void cdiStreamSync_(stream_t *streamptr);

443
const char *cdiUnitNamePtr(int cdi_unit);
444

445
446
447
448
449
450
451
enum {
  /* 8192 is known to work on most systems (4096 isn't on Alpha) */
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

452
453
454
455
456
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

457
458
459
460
461
462
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

463
464
void cdiDefTableID(int tableID);

465
466
467
468
469
470
471
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

472
#endif  /* _CDI_INT_H */
473
474
475
476
477
478
479
480
481
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */