cdi_int.h 12.1 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 strIsEqual(const char *vstr, const char *cstr);
43

44
45
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
48
#endif


49
#ifndef  ERROR_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
51
52
53
54
#  include "error.h"
#endif
#ifndef _BASETIME_H
#  include "basetime.h"
#endif
55
#ifndef TIMEBASE_H
56
57
#  include "timebase.h"
#endif
58
#ifndef TAXIS_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
#  include "taxis.h"
#endif
61
#ifndef CDI_LIMITS_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
63
64
65
66
67
68
69
70
71
72
#  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
73
74
75
#ifndef RESOURCE_HANDLE_H
#  include "resource_handle.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76

Thomas Jahns's avatar
Thomas Jahns committed
77

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
#if defined (__xlC__) /* performance problems on IBM */
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
81
82
83
84
#ifndef DBL_IS_NAN
#  define DBL_IS_NAN(x)     ((x) != (x))
#endif
#else
85
#ifndef DBL_IS_NAN
86
#if  defined  (HAVE_DECL_ISNAN)
87
#  define DBL_IS_NAN(x)     (isnan(x))
88
#elif  defined  (FP_NAN)
89
#  define DBL_IS_NAN(x)     (fpclassify(x) == FP_NAN)
90
#else
91
#  define DBL_IS_NAN(x)     ((x) != (x))
92
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95

96
97
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
98
#  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))
99
100
#endif

101
102
103
104
105
#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
106
107
108
109
110
111
#define  FALSE  0
#define  TRUE   1

#define  TYPE_REC  0
#define  TYPE_VAR  1

112
113
114
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
typedef struct
{
117
118
119
120
121
122
123
124
125
126
127
  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
128
129
  void     *exsep;              // ieg, ext, srv container
  void     *cgribexp;           // cgribex container
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
131
132
}
Record;

133
134
135
136
/* data structure specifying tile-related meta-data. structure
 * contains "-1" if this is no tile-variable. */
typedef struct {
  int
137
138
    tileindex,
    totalno_of_tileattr_pairs,
139
140
141
142
143
144
    tileClassification,
    numberOfTiles,
    numberOfAttributes,
    attribute;
} var_tile_t;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147
148
149

typedef struct
{
  off_t     position;
  size_t    size;
150
  size_t    gridsize;
151
  int       zip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
  int       param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
  int       ilevel;
  int       ilevel2;
  int       ltype;
156
  short     perturbationNumber;
Thomas Jahns's avatar
Thomas Jahns committed
157
  short     tsteptype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
159
  short     varID;
  short     levelID;
160
  short     used;
161
162
  char      varname[32]; // needed for grib decoding with GRIB_API
  var_tile_t tiles;      // tile-related meta-data, currently for GRIB-API only.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
record_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
  record_t *records;
169
170
171
172
173
174
175
  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
176
  bool      next;
177
  off_t     position;    // timestep file position
178
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
}
180
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
183
184


typedef struct {
  int       nlevs;
185
186
187
  int       subtypeIndex; // corresponding tile in subtype_t structure (subtype->self)
  int      *recordID;     // record IDs: [nlevs]
  int      *lindex;       // level index
188
189
190
191
192
193
} sleveltable_t;


typedef struct {
  int            ncvarid;
  int            subtypeSize;
194
195
196
  sleveltable_t *recordTable; // record IDs for each subtype
  bool           defmiss;     // true: if missval is defined in file
  bool           isUsed;
197
198
199

  int            gridID;
  int            zaxisID;
200
201
  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
202
}
203
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206
207
208
209
210
211
212
213


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

214
#ifdef HAVE_LIBNETCDF
215
216
enum {
  CDF_DIMID_X,
217
  CDF_DIMID_Y,
218
  CDF_VARID_X,
219
  CDF_VARID_Y,
220
  CDF_VARID_A,
221
222
  CDF_SIZE_ncIDs,
};
223
224
typedef struct {
  int gridID;
225
  int ncIDs[CDF_SIZE_ncIDs];
226
227
228
}
ncgrid_t;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230

typedef struct {
231
  int         self;
232
  int         accesstype;   // TYPE_REC or TYPE_VAR
233
234
235
236
237
  int         accessmode;
  int         filetype;
  int         byteorder;
  int         fileID;
  int         filemode;
238
  int         nrecs;        // number of records
239
  size_t      numvals;
240
241
242
  char       *filename;
  Record     *record;
  svarinfo_t *vars;
243
  int         nvars;        // number of variables
244
  int         varsAllocated;
245
246
247
  int         curTsID;      // current timestep ID
  int         rtsteps;      // number of tsteps accessed
  long        ntsteps;      // number of tsteps : only set if all records accessed
248
249
250
  tsteps_t   *tsteps;
  int         tstepsTableSize;
  int         tstepsNextID;
251
  basetime_t  basetime;
252
253
  int         ncmode;
  int         vlistID;
254
#ifdef HAVE_LIBNETCDF
255
  ncgrid_t    ncgrid[MAX_GRIDS_PS];
256
  int         zaxisID[MAX_ZAXES_PS];	// Warning: synchronous array to vlist_to_pointer(vlistID)->zaxisIDs
257
  int         nczvarID[MAX_ZAXES_PS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
259
  VCT         vct;
#endif
260
261
262
263
264
265
266
  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
267
268
  bool        sortname;
  bool        sortparam;
269
270
271
272
273
274
275
#if defined (GRIBCONTAINER2D)
  void      **gribContainers;
#else
  void       *gribContainers;
#endif

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279


280
281
282
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500

283
enum cdi_convention {CDI_CONVENTION_ECHAM, CDI_CONVENTION_CF};
284

285
// Data type specification for optional key/value pairs (GRIB)
286
287
288
289
290
typedef enum {
  t_double = 0,
  t_int    = 1
} key_val_pair_datatype;

291
// Data structure holding optional key/value pairs for GRIB
292
293
typedef struct
{
294
  char*                  keyword;        // keyword string
295
  bool                   update;
296
297
298
299
  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
300
301
} opt_key_val_pair_t;

302
// enum for differenciating between the different times that we handle
303
304
305
306
307
308
typedef enum {
  kCdiTimeType_referenceTime,
  kCdiTimeType_startTime,
  kCdiTimeType_endTime
} CdiTimeType;

309

310
#define  CDI_FILETYPE_UNDEF          -1   // Unknown/not yet defined file type
311
312


313
extern int cdiDebugExt;
314
extern int CDI_Debug;      // If set to 1, debuggig (default 0)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
extern int CDI_Recopt;
316
317
extern bool CDI_gribapi_debug;
extern bool CDI_gribapi_grib1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
extern double CDI_default_missval;
319
extern double CDI_grid_missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320
321
322
323
extern int cdiDefaultInstID;
extern int cdiDefaultModelID;
extern int cdiDefaultTableID;
extern int cdiDefaultLeveltype;
324
extern int cdiDefaultCalendar;
325
//extern int cdiNcMissingValue;
326
extern int cdiNcChunksizehint;
327
extern int cdiChunkType;
328
extern int cdiSplitLtype105;
329
330
extern int cdiDataUnreduced;
extern int cdiSortName;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
extern int cdiSortParam;
332
extern int cdiHaveMissval;
333
334
335
extern bool cdiIgnoreAttCoordinates;
extern bool cdiCoordinatesLonLat;
extern bool cdiIgnoreValidRange;
336
extern int cdiSkipRecords;
337
extern int CDI_convention;
338
extern int CDI_inventory_mode;
339
extern int CDO_version_info;
340
extern int CDI_read_cell_corners;
Thomas Jahns's avatar
Thomas Jahns committed
341
extern int CDI_cmor_mode;
342
extern int CDI_reduce_dim;
343
extern size_t CDI_netcdf_hdr_pad;
344
extern bool CDI_netcdf_lazy_grid_load;
345
346
extern int STREAM_Debug;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
347

Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
350
351
352
353
354
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
355
  return (stream_t *)reshGetVal(idx, &streamOps);
356
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357

358
359
360
361
362
363
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
364

Uwe Schulzweida's avatar
Uwe Schulzweida committed
365
366
int     streamInqFileID(int streamID);

367
368
369
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370

371
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
374
375
376

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
Thomas Jahns's avatar
Thomas Jahns committed
377
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID);
378
// default implementation of the overridable function
Thomas Jahns's avatar
Thomas Jahns committed
379
void    cdiStreamSetupVlist_(stream_t *streamptr, int vlistID);
380
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381

382
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
383

384
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
385

386
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387

388
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389

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

392
393
void streamFCopyRecord(stream_t *streamptr2, stream_t *streamptr1, const char *container_name);

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

396
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397

Uwe Schulzweida's avatar
Uwe Schulzweida committed
398
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
400
401
402
403
404

void    cdiCheckZaxis(int zaxisID);

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

Deike Kleberg's avatar
Deike Kleberg committed
405
406
int     getByteswap(int byteorder);

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

409
410
void  cdiInitialize(void);

411
412
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
413

414
415
416
417
418
419
#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
420

421
422
struct streamAssoc
{
Thomas Jahns's avatar
Thomas Jahns committed
423
  int streamID, vlistID;
424
425
426
};

struct streamAssoc
427
428
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
429

430
int
431
cdiStreamOpenDefaultDelegate(const char *filename, char filemode,
432
433
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
434

435
436
437
438
int
streamOpenID(const char *filename, char filemode, int filetype,
             int resH);

439
440
void
cdiStreamDefVlist_(int streamID, int vlistID);
441
442

int
443
cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, size_t nmiss);
444

445
void
Thomas Jahns's avatar
Thomas Jahns committed
446
cdiStreamWriteVarChunk_(int streamID, int varID, int memtype,
447
                        const int rect[][2], const void *data, size_t nmiss);
448
void
449
450
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
451

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

Thomas Jahns's avatar
Thomas Jahns committed
454
455
void cdiStreamSync_(stream_t *streamptr);

456
const char *cdiUnitNamePtr(int cdi_unit);
457

458
enum {
459
  // 8192 is known to work on most systems (4096 isn't on Alpha)
460
461
462
463
464
  commonPageSize = 8192,
};

size_t cdiGetPageSize(bool largePageAlign);

465
466
467
468
469
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

470
471
472
473
474
475
#ifdef __cplusplus
extern "C" {
#endif

// functions used in CDO !!!

476
477
void cdiDefTableID(int tableID);

478
479
480
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);

481
482
483
484
485
486
487
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);
}


488
489
490
491
#if defined (__cplusplus)
}
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
#endif  /* CDI_INT_H */
493
494
495
496
497
498
499
500
501
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */