cdi_int.h 12.5 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
typedef struct {
  short perturbationNumber;
154
  short typeOfGeneratingProcess;
155
156
157
} VarScanKeys;

static inline void
158
varScanKeysInit(VarScanKeys *s)
159
{
160
  memset(s, 0, sizeof(VarScanKeys));
161
162
163
164
165
166
167
168
}

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

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


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

307
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
308

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

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

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

333

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


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
371

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

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

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

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

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

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

void    streamDefineTaxis(int streamID);

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

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

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

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

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

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

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

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

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

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

void    cdiCheckZaxis(int zaxisID);

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

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

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

433
434
void  cdiInitialize(void);

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

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

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

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

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

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

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

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

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

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

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

480
const char *cdiUnitNamePtr(int cdi_unit);
481

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

size_t cdiGetPageSize(bool largePageAlign);

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

491
492
493
494
495
496
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

497
498
void cdiDefTableID(int tableID);

499
500
501
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);

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


509
510
511
512
#if defined (__cplusplus)
}
#endif

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