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
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,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
  CDF_DIMID_RP, // reducedPoints
242
  CDF_VARID_X,
243
  CDF_VARID_Y,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
  CDF_VARID_RP, // reducedPoints
245
  CDF_VARID_A,
246
247
  CDF_SIZE_ncIDs,
};
248
249
typedef struct {
  int gridID;
250
  int ncIDs[CDF_SIZE_ncIDs];
251
252
253
}
ncgrid_t;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
255

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

299
300
301
302
  int         numWorker;
  int         nextRecID;
  int         cachedTsID;
  void       *jobs;
303
  void       *jobManager;
304

305
  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
307
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
309


310
311
312
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

313
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
314

315
// Data type specification for optional key/value pairs (GRIB)
316
317
318
319
320
typedef enum {
  t_double = 0,
  t_int    = 1
} key_val_pair_datatype;

321
// Data structure holding optional key/value pairs for GRIB
322
323
typedef struct
{
324
  char*                  keyword;        // keyword string
325
  bool                   update;
326
327
328
329
  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
330
331
} opt_key_val_pair_t;

332
// enum for differenciating between the different times that we handle
333
334
335
336
337
338
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

339

340
#define  CDI_FILETYPE_UNDEF          -1   // Unknown/not yet defined file type
341
342


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
377

Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
379
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
380
381
382
383
384
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
385
  return (stream_t *)reshGetVal(idx, &streamOps);
386
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387

388
389
390
391
392
393
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
394

Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
int     streamInqFileID(int streamID);

397
398
399
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400

401
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
403
404
405
406

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
Thomas Jahns's avatar
Thomas Jahns committed
407
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID);
408
// default implementation of the overridable function
Thomas Jahns's avatar
Thomas Jahns committed
409
void    cdiStreamSetupVlist_(stream_t *streamptr, int vlistID);
410
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411

412
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413

414
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415

416
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
417

418
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419

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

422
423
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

426
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427

Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
432
433
434

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
435
436
int     getByteswap(int byteorder);

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

439
440
void  cdiInitialize(void);

441
442
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
443

444
445
446
447
448
449
#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
450

451
452
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
453
  int streamID, vlistID;
454
455
456
};

struct streamAssoc
457
458
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
459

460
int
461
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
462
463
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
464

465
466
467
468
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

469
470
void
cdiStreamDefVlist_(int streamID, int vlistID);
471
472

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

475
void
Thomas Jahns's avatar
Thomas Jahns committed
476
cdiStreamWriteVarChunk_(int streamID, int varID, int memtype,
477
                        const int rect[][2], const void *data, size_t nmiss);
478
void
479
480
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
481

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

Thomas Jahns's avatar
Thomas Jahns committed
484
485
void cdiStreamSync_(stream_t *streamptr);

486
const char *cdiUnitNamePtr(int cdi_unit);
487

488
enum {
489
  // 8192 is known to work on most systems (4096 isn't on Alpha)
490
491
492
493
494
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

495
496
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

497
498
499
500
501
502
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

503
504
void cdiDefTableID(int tableID);

505
506
507
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);

508
509
510
511
512
513
514
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);
}


515
516
517
518
#if defined (__cplusplus)
}
#endif

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