cdi_int.h 12.6 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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
4
#ifdef 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
char *strToLower(char *str);
42
bool strStartsWith(const char *vstr, const char *cstr);
43

44
static inline bool
45
46
47
48
49
50
strIsEqual(const char *x, const char *y) 
{
  return (*x == *y) && strcmp(x, y) == 0;
}


51
52
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
55
#endif


56
#ifndef  ERROR_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
59
60
61
#  include "error.h"
#endif
#ifndef _BASETIME_H
#  include "basetime.h"
#endif
62
#ifndef TIMEBASE_H
63
64
#  include "timebase.h"
#endif
65
#ifndef TAXIS_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
67
#  include "taxis.h"
#endif
68
#ifndef CDI_LIMITS_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
70
71
72
73
74
75
76
77
78
79
#  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
80
81
82
#ifndef RESOURCE_HANDLE_H
#  include "resource_handle.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83

Thomas Jahns's avatar
Thomas Jahns committed
84

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
#if defined (__xlC__) /* performance problems on IBM */
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
88
89
90
91
#ifndef DBL_IS_NAN
#  define DBL_IS_NAN(x)     ((x) != (x))
#endif
#else
92
#ifndef DBL_IS_NAN
93
#if  defined  (HAVE_DECL_ISNAN)
94
#  define DBL_IS_NAN(x)     (isnan(x))
95
#elif  defined  (FP_NAN)
96
#  define DBL_IS_NAN(x)     (fpclassify(x) == FP_NAN)
97
#else
98
#  define DBL_IS_NAN(x)     ((x) != (x))
99
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102

103
104
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
105
#  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))
106
107
#endif

108
109
110
111
112
#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
113
114
115
116
117
118
#define  FALSE  0
#define  TRUE   1

#define  TYPE_REC  0
#define  TYPE_VAR  1

119
120
121
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
typedef struct
{
124
125
126
127
128
129
130
131
132
133
134
  void     *buffer;             // gribapi, cgribex
  size_t    buffersize;         // gribapi, cgribex
  off_t     position;           // file position
  int       param;
  int       level;
  int       date;
  int       time;
  int       gridID;
  int       varID;
  int       levelID;
  int       prec;               // ext, srv
135
136
  void     *exsep;              // ieg, ext, srv container
  void     *cgribexp;           // cgribex container
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
}
Record;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
151

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
typedef struct {
  short perturbationNumber;
  //short typeOfGeneratingProcess;
} VarScanKeys;

static inline void
varScanKeysInit(VarScanKeys *scanKeys)
{
  memset(scanKeys, 0, sizeof(VarScanKeys));
}

static inline bool
varScanKeysIsEqual(const VarScanKeys *s1, const VarScanKeys *s2)
{
  return memcmp(s1, s2, sizeof(VarScanKeys)) == 0;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
170
171
172
typedef struct
{
  off_t     position;
  size_t    size;
173
  size_t    gridsize;
174
  int       zip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
  int       param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178
  int       ilevel;
  int       ilevel2;
  int       ltype;
Thomas Jahns's avatar
Thomas Jahns committed
179
  short     tsteptype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
181
  short     varID;
  short     levelID;
182
  short     used;
183
  char      varname[32]; // needed for grib decoding with GRIB_API
184
  VarScanKeys scanKeys;
185
  var_tile_t tiles;      // tile-related meta-data, currently for GRIB-API only.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
record_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189
190


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
  record_t *records;
192
193
194
195
196
197
198
  int      *recIDs;      // IDs of non constant records
  int       recordSize;  // number of allocated records
  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
199
  bool      next;
200
  off_t     position;    // timestep file position
201
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
}
203
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206
207


typedef struct {
  int       nlevs;
208
209
210
  int       subtypeIndex; // corresponding tile in subtype_t structure (subtype->self)
  int      *recordID;     // record IDs: [nlevs]
  int      *lindex;       // level index
211
212
213
214
215
216
} sleveltable_t;


typedef struct {
  int            ncvarid;
  int            subtypeSize;
217
218
219
  sleveltable_t *recordTable; // record IDs for each subtype
  bool           defmiss;     // true: if missval is defined in file
  bool           isUsed;
220
221
222

  int            gridID;
  int            zaxisID;
223
224
  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
225
}
226
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
228
229
230
231
232
233
234
235
236


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

237
#ifdef HAVE_LIBNETCDF
238
239
enum {
  CDF_DIMID_X,
240
  CDF_DIMID_Y,
241
  CDF_VARID_X,
242
  CDF_VARID_Y,
243
  CDF_VARID_A,
244
245
  CDF_SIZE_ncIDs,
};
246
247
typedef struct {
  int gridID;
248
  int ncIDs[CDF_SIZE_ncIDs];
249
250
251
}
ncgrid_t;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
253

typedef struct {
254
  int         self;
255
  int         accesstype;   // TYPE_REC or TYPE_VAR
256
257
258
259
260
  int         accessmode;
  int         filetype;
  int         byteorder;
  int         fileID;
  int         filemode;
261
  int         nrecs;        // number of records
262
  size_t      numvals;
263
264
265
  char       *filename;
  Record     *record;
  svarinfo_t *vars;
266
  int         nvars;        // number of variables
267
  int         varsAllocated;
268
269
270
  int         curTsID;      // current timestep ID
  int         rtsteps;      // number of tsteps accessed
  long        ntsteps;      // number of tsteps : only set if all records accessed
271
272
273
  tsteps_t   *tsteps;
  int         tstepsTableSize;
  int         tstepsNextID;
274
  basetime_t  basetime;
275
276
  int         ncmode;
  int         vlistID;
277
#ifdef HAVE_LIBNETCDF
278
  ncgrid_t    ncgrid[MAX_GRIDS_PS];
279
  int         zaxisID[MAX_ZAXES_PS];	// Warning: synchronous array to vlist_to_pointer(vlistID)->zaxisIDs
280
  int         nczvarID[MAX_ZAXES_PS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
281
282
  VCT         vct;
#endif
283
284
285
286
287
288
289
  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
290
291
  bool        sortname;
  bool        sortparam;
292
293
294
295
296
297
298
#if defined (GRIBCONTAINER2D)
  void      **gribContainers;
#else
  void       *gribContainers;
#endif

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
300
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
302


303
304
305
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

306
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
307

308
// Data type specification for optional key/value pairs (GRIB)
309
310
311
312
313
typedef enum {
  t_double = 0,
  t_int    = 1
} key_val_pair_datatype;

314
// Data structure holding optional key/value pairs for GRIB
315
316
typedef struct
{
317
  char*                  keyword;        // keyword string
318
  bool                   update;
319
320
321
322
  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
323
324
} opt_key_val_pair_t;

325
// enum for differenciating between the different times that we handle
326
327
328
329
330
331
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

332

333
#define  CDI_FILETYPE_UNDEF          -1   // Unknown/not yet defined file type
334
335


336
extern int cdiDebugExt;
337
extern int CDI_Debug;      // If set to 1, debuggig (default 0)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338
extern int CDI_Recopt;
339
340
extern bool CDI_gribapi_debug;
extern bool CDI_gribapi_grib1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
extern double CDI_default_missval;
342
extern double CDI_grid_missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344
345
346
extern int cdiDefaultInstID;
extern int cdiDefaultModelID;
extern int cdiDefaultTableID;
extern int cdiDefaultLeveltype;
347
extern int cdiDefaultCalendar;
348
//extern int cdiNcMissingValue;
349
extern int cdiNcChunksizehint;
350
extern int cdiChunkType;
351
extern int cdiSplitLtype105;
352
353
extern int cdiDataUnreduced;
extern int cdiSortName;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
extern int cdiSortParam;
355
extern int cdiHaveMissval;
356
357
358
extern bool cdiIgnoreAttCoordinates;
extern bool cdiCoordinatesLonLat;
extern bool cdiIgnoreValidRange;
359
extern int cdiSkipRecords;
360
extern int CDI_convention;
361
extern int CDI_inventory_mode;
362
extern int CDO_version_info;
363
extern int CDI_read_cell_corners;
Thomas Jahns's avatar
Thomas Jahns committed
364
extern int CDI_cmor_mode;
365
extern int CDI_reduce_dim;
366
extern size_t CDI_netcdf_hdr_pad;
367
extern bool CDI_netcdf_lazy_grid_load;
368
369
extern int STREAM_Debug;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
370

Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
372
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
373
374
375
376
377
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
378
  return (stream_t *)reshGetVal(idx, &streamOps);
379
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380

381
382
383
384
385
386
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
387

Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
389
int     streamInqFileID(int streamID);

390
391
392
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393

394
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398
399

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
Thomas Jahns's avatar
Thomas Jahns committed
400
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID);
401
// default implementation of the overridable function
Thomas Jahns's avatar
Thomas Jahns committed
402
void    cdiStreamSetupVlist_(stream_t *streamptr, int vlistID);
403
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404

405
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406

407
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408

409
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
410

411
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412

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

415
416
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

419
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420

Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
424
425
426
427

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
428
429
int     getByteswap(int byteorder);

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

432
433
void  cdiInitialize(void);

434
435
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
436

437
438
439
440
441
442
#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
443

444
445
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
446
  int streamID, vlistID;
447
448
449
};

struct streamAssoc
450
451
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
452

453
int
454
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
455
456
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
457

458
459
460
461
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

462
463
void
cdiStreamDefVlist_(int streamID, int vlistID);
464
465

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

468
void
Thomas Jahns's avatar
Thomas Jahns committed
469
cdiStreamWriteVarChunk_(int streamID, int varID, int memtype,
470
                        const int rect[][2], const void *data, size_t nmiss);
471
void
472
473
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
474

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

Thomas Jahns's avatar
Thomas Jahns committed
477
478
void cdiStreamSync_(stream_t *streamptr);

479
const char *cdiUnitNamePtr(int cdi_unit);
480

481
enum {
482
  // 8192 is known to work on most systems (4096 isn't on Alpha)
483
484
485
486
487
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

488
489
490
491
492
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

493
494
495
496
497
498
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

499
500
void cdiDefTableID(int tableID);

501
502
503
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);

504
505
506
507
508
509
510
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);
}


511
512
513
514
#if defined (__cplusplus)
}
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
515
#endif  /* CDI_INT_H */
516
517
518
519
520
521
522
523
524
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */