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,
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
#if defined (GRIBCONTAINER2D)
  void      **gribContainers;
#else
  void       *gribContainers;
#endif

298
299
300
301
302
  int         numWorker;
  int         nextRecID;
  int         cachedTsID;
  void       *jobs;

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


308
309
310
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

311
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
312

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

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

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

337

338
#define  CDI_FILETYPE_UNDEF          -1   // Unknown/not yet defined file type
339
340


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
375

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

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

386
387
388
389
390
391
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
392

Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394
int     streamInqFileID(int streamID);

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

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

void    streamDefineTaxis(int streamID);

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

410
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411

412
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413

414
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415

416
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
417

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

420
421
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

424
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
425

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

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
433
434
int     getByteswap(int byteorder);

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

437
438
void  cdiInitialize(void);

439
440
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
441

442
443
444
445
446
447
#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
448

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

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

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

463
464
465
466
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

467
468
void
cdiStreamDefVlist_(int streamID, int vlistID);
469
470

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

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

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

Thomas Jahns's avatar
Thomas Jahns committed
482
483
void cdiStreamSync_(stream_t *streamptr);

484
const char *cdiUnitNamePtr(int cdi_unit);
485

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

size_t cdiGetPageSize(bool largePageAlign);

493
494
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

495
496
497
498
499
500
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

501
502
void cdiDefTableID(int tableID);

503
504
505
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);

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


513
514
515
516
#if defined (__cplusplus)
}
#endif

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