cdi_int.h 12.8 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
21
22
23
24
25
// Base file types

#define  CDI_FILETYPE_GRIB        100   // File type GRIB
#define  CDI_FILETYPE_NETCDF      101   // File type NetCDF

// dummy use of unused parameters to silence compiler warnings
26
#ifndef  UNUSED
27
#define  UNUSED(x) (void)x
28
#endif
29

Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
#ifndef strdupx
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
32
33
34
35
#ifndef strdup
char *strdup(const char *s);
#endif
#define strdupx  strdup
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
38
39
#define strdupx(s)			          \
({					      	  \
   const char *__old = (s);			  \
   size_t __len = strlen(__old) + 1;		  \
40
   char *__new = (char *) Malloc(__len);	  \
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
42
   (char *) memcpy(__new, __old, __len);	  \
})
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
#endif

46
char *strToLower(char *str);
47
bool strStartsWith(const char *vstr, const char *cstr);
48

49
static inline bool
50
strIsEqual(const char *x, const char *y)
51
52
53
54
55
{
  return (*x == *y) && strcmp(x, y) == 0;
}


56
57
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59
60
#endif


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

Thomas Jahns's avatar
Thomas Jahns committed
89

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

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

108
109
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
110
#  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))
111
112
#endif

113
114
115
116
117
#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
118
119
120
121
122
123
#define  FALSE  0
#define  TRUE   1

#define  TYPE_REC  0
#define  TYPE_VAR  1

124
125
126
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
typedef struct
{
129
130
131
132
133
134
135
136
137
138
139
  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
140
141
  void     *exsep;              // ieg, ext, srv container
  void     *cgribexp;           // cgribex container
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
144
}
Record;

145
// data structure specifying tile-related meta-data. structure contains "-1" if this is no tile-variable.
146
147
typedef struct {
  int
148
149
    tileindex,
    totalno_of_tileattr_pairs,
150
151
152
153
154
155
    tileClassification,
    numberOfTiles,
    numberOfAttributes,
    attribute;
} var_tile_t;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
156

157
158
typedef struct {
  short perturbationNumber;
159
  short typeOfGeneratingProcess;
160
161
162
} VarScanKeys;

static inline void
163
varScanKeysInit(VarScanKeys *s)
164
{
165
  memset(s, 0, sizeof(VarScanKeys));
166
167
168
169
170
171
172
173
}

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

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


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
  record_t *records;
197
198
199
200
201
202
203
  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
204
  bool      next;
205
  off_t     position;    // timestep file position
206
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
}
208
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
210
211
212


typedef struct {
  int       nlevs;
213
214
215
  int       subtypeIndex; // corresponding tile in subtype_t structure (subtype->self)
  int      *recordID;     // record IDs: [nlevs]
  int      *lindex;       // level index
216
217
218
219
220
221
} sleveltable_t;


typedef struct {
  int            ncvarid;
  int            subtypeSize;
222
223
224
  sleveltable_t *recordTable; // record IDs for each subtype
  bool           defmiss;     // true: if missval is defined in file
  bool           isUsed;
225
226
227

  int            gridID;
  int            zaxisID;
228
229
  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
230
}
231
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235
236
237
238
239
240
241


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

242
#ifdef HAVE_LIBNETCDF
243
244
enum {
  CDF_DIMID_X,
245
  CDF_DIMID_Y,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
  CDF_DIMID_RP, // reducedPoints
247
  CDF_VARID_X,
248
  CDF_VARID_Y,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
  CDF_VARID_RP, // reducedPoints
250
  CDF_VARID_A,
251
252
  CDF_SIZE_ncIDs,
};
253
254
typedef struct {
  int gridID;
255
  int ncIDs[CDF_SIZE_ncIDs];
256
257
258
}
ncgrid_t;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260

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

304
305
306
307
  int         numWorker;
  int         nextRecID;
  int         cachedTsID;
  void       *jobs;
308
  void       *jobManager;
309

310
  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314


315
316
317
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

318
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
319

320
// Data type specification for optional key/value pairs (GRIB)
321
322
323
324
325
typedef enum {
  t_double = 0,
  t_int    = 1
} key_val_pair_datatype;

326
// Data structure holding optional key/value pairs for GRIB
327
328
typedef struct
{
329
  char*                  keyword;        // keyword string
330
  bool                   update;
331
332
333
334
  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
335
336
} opt_key_val_pair_t;

337
// enum for differenciating between the different times that we handle
338
339
340
341
342
343
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

344

345
#define  CDI_FILETYPE_UNDEF          -1   // Unknown/not yet defined file type
346
347


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
382

Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
384
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
385
386
387
388
389
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
390
  return (stream_t *)reshGetVal(idx, &streamOps);
391
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392

393
394
395
396
397
398
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
399

Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
int     streamInqFileID(int streamID);

402
403
404
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405

406
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
408
409
410
411

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
Thomas Jahns's avatar
Thomas Jahns committed
412
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID);
413
// default implementation of the overridable function
Thomas Jahns's avatar
Thomas Jahns committed
414
void    cdiStreamSetupVlist_(stream_t *streamptr, int vlistID);
415
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
416

417
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418

419
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420

421
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422

423
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
424

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

427
428
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

431
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432

Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
435
436
437
438
439

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
440
441
int     getByteswap(int byteorder);

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

444
445
void  cdiInitialize(void);

446
447
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
448

449
450
451
452
453
454
#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
455

456
457
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
458
  int streamID, vlistID;
459
460
461
};

struct streamAssoc
462
463
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
464

465
int
466
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
467
468
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
469

470
471
472
473
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

474
475
void
cdiStreamDefVlist_(int streamID, int vlistID);
476
477

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

480
void
Thomas Jahns's avatar
Thomas Jahns committed
481
cdiStreamWriteVarChunk_(int streamID, int varID, int memtype,
482
                        const int rect[][2], const void *data, size_t nmiss);
483
void
484
485
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
486

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

Thomas Jahns's avatar
Thomas Jahns committed
489
490
void cdiStreamSync_(stream_t *streamptr);

491
const char *cdiUnitNamePtr(int cdi_unit);
492

493
enum {
494
  // 8192 is known to work on most systems (4096 isn't on Alpha)
495
496
497
498
499
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

500
501
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

502
503
504
505
506
507
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

508
509
void cdiDefTableID(int tableID);

510
511
512
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);

513
514
515
516
517
518
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);
}

519
int cdiBaseFiletype(int filetype);
520

521
522
523
524
#if defined (__cplusplus)
}
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
#endif  /* CDI_INT_H */
526
527
528
529
530
531
532
533
534
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */