cdi_int.h 11.3 KB
Newer Older
1
2
#ifndef _CDI_INT_H
#define _CDI_INT_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
3
4
5
6
7

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#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>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
14
15
#include <math.h>
#include <sys/types.h>

16
17
#include "cdi.h"

18
19
20
/* dummy use of unused parameters to silence compiler warnings */
#define  UNUSED(x) (void)x

Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
#ifndef strdupx
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
23
24
25
26
#ifndef strdup
char *strdup(const char *s);
#endif
#define strdupx  strdup
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
30
31
32
33
#define strdupx(s)			          \
({					      	  \
   const char *__old = (s);			  \
   size_t __len = strlen(__old) + 1;		  \
   char *__new = (char *) malloc(__len);	  \
   (char *) memcpy(__new, __old, __len);	  \
})
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
#endif

37
38
#ifndef  M_PI
#define  M_PI        3.14159265358979323846  /* pi */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
40
41
42
43
44
45
46
47
#endif


#ifndef  _ERROR_H
#  include "error.h"
#endif
#ifndef _BASETIME_H
#  include "basetime.h"
#endif
48
49
50
#ifndef _TIMEBASE_H
#  include "timebase.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef  _TAXIS_H
#  include "taxis.h"
#endif
#ifndef  _CDI_LIMITS_H
#  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
66
67
68
#ifndef RESOURCE_HANDLE_H
#  include "resource_handle.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69

Thomas Jahns's avatar
Thomas Jahns committed
70

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
#if defined (__xlC__) /* performance problems on IBM */
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
74
75
76
77
#ifndef DBL_IS_NAN
#  define DBL_IS_NAN(x)     ((x) != (x))
#endif
#else
78
#ifndef DBL_IS_NAN
79
#if  defined  (HAVE_DECL_ISNAN)
80
#  define DBL_IS_NAN(x)     (isnan(x))
81
#elif  defined  (FP_NAN)
82
#  define DBL_IS_NAN(x)     (fpclassify(x) == FP_NAN)
83
#else
84
#  define DBL_IS_NAN(x)     ((x) != (x))
85
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88

89
90
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
91
#  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))
92
93
#endif

94
95
96
97
98
99
#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
100
101
102
103
104
105
#define  FALSE  0
#define  TRUE   1

#define  TYPE_REC  0
#define  TYPE_VAR  1

106
107
108
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
typedef struct
{
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  void     *buffer;             /* gribapi, cgribex */
  size_t    buffersize;         /* gribapi, cgribex */
  off_t     position;           /* ieg */
  int       param;              /* srv */
  int       level;              /* ext, srv */
  int       date;               /* ext, srv */
  int       time;               /* srv */
  int       gridID;             /* ieg, ext */
  int       varID;              /* ieg */
  int       levelID;            /* ieg  */
  int       prec;               /* ext, srv */
  int       sec0[2];            /* cgribex */
  int       sec1[1024];         /* cgribex */
  int       sec2[4096];         /* cgribex */
  int       sec3[2];            /* cgribex */
  int       sec4[512];          /* cgribex */
  void     *exsep;              /* ieg, ext, srv */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
129
130
}
Record;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
146
147

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


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167
168
169
170
171
172
173
  int       recordSize;  /* number of allocated records           */
  int      *recIDs;      /* IDs of non constant 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                     */
  long      next;
  off_t     position;    /* timestep file position                */
174
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
}
176
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
178
179
180


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


typedef struct {
  int            ncvarid;
  int            subtypeSize;
  sleveltable_t *recordTable; /* record IDs for each subtype */
  int            defmiss;     /* TRUE if missval is defined in file */

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


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


typedef struct {
212
213
214
215
216
217
218
219
220
221
  int         self;
  int         accesstype;   /* TYPE_REC or TYPE_VAR */
  int         accessmode;
  int         filetype;
  int         byteorder;
  int         fileID;
  int         filemode;
  off_t       numvals;
  char       *filename;
  Record     *record;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
  int         nrecs;        /* number of records                  */
223
224
225
226
227
228
229
230
231
  int         nvars;        /* number of variables                */
  svarinfo_t *vars;
  int         varsAllocated;
  int         curTsID;      /* current timestep ID */
  int         rtsteps;      /* number of tsteps accessed       */
  long        ntsteps;      /* number of tsteps : only set if all records accessed */
  tsteps_t   *tsteps;
  int         tstepsTableSize;
  int         tstepsNextID;
232
  basetime_t  basetime;
233
234
  int         ncmode;
  int         vlistID;
235
236
237
  int         xdimID[MAX_GRIDS_PS];	//Warning: synchronous array to vlist_to_pointer(vlistID)->gridIDs
  int         ydimID[MAX_GRIDS_PS];	//Warning: synchronous array to vlist_to_pointer(vlistID)->gridIDs
  int         zaxisID[MAX_ZAXES_PS];	//Warning: synchronous array to vlist_to_pointer(vlistID)->zaxisIDs
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  int         ncxvarID[MAX_GRIDS_PS];
  int         ncyvarID[MAX_GRIDS_PS];
  int         ncavarID[MAX_GRIDS_PS];
  int         historyID;
  int         globalatts;
  int         localatts;
  VCT         vct;
  int         unreduced;
  int         sortname;
  int         have_missval;
  int         comptype;      // compression type
  int         complevel;     // compression level
#if defined (GRIBCONTAINER2D)
  void      **gribContainers;
#else
  void       *gribContainers;
#endif
  int         vlistIDorig;
256
  /* only used by MPI-parallelized version of library */
257
  int       ownerRank;    // MPI rank of owner process
258
259

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
263


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Length of optional keyword/value pair list */
#define MAX_OPT_GRIB_ENTRIES 500


/* Data type specification for optional key/value pairs (GRIB) */
typedef enum {
  t_double = 0,
  t_int    = 1
} key_val_pair_datatype;

/* Data structure holding optional key/value pairs for GRIB */
typedef struct
{
  char*                  keyword;        /* keyword string */
  int                    update;
  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 */
} opt_key_val_pair_t;




Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
extern int CDI_Debug;      /* If set to 1, debuggig (default 0)            */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
extern int CDI_Recopt;
290
extern int cdiGribApiDebug;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
291
292
293
294
295
extern double cdiDefaultMissval;
extern int cdiDefaultInstID;
extern int cdiDefaultModelID;
extern int cdiDefaultTableID;
extern int cdiDefaultLeveltype;
296
//extern int cdiNcMissingValue;
297
extern int cdiNcChunksizehint;
298
extern int cdiChunkType;
299
extern int cdiSplitLtype105;
300
301
302
303
304
extern int cdiDataUnreduced;
extern int cdiSortName;
extern int cdiHaveMissval;
extern int STREAM_Debug;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
305

Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
308
309
310
311
312
extern const resOps streamOps;

static inline stream_t *
stream_to_pointer(int idx)
{
313
  return (stream_t *)reshGetVal(idx, &streamOps);
314
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315

316
317
318
319
320
321
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
322

Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
int     streamInqFileID(int streamID);

325
326
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
327
const char *gridNamePtr(int gridtype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
char   *zaxisNamePtr(int leveltype);
329
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330

331
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
333
334
335
336

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
337
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID, int vlistIDorig);
338
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID, int tilesetID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339

340
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341

342
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343

344
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345

346
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347

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

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

352
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353

Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357
358
359
360
361
362

void    cdiCheckZaxis(int zaxisID);

void    cdiPrintDatatypes(void);

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

Deike Kleberg's avatar
Deike Kleberg committed
363
364
int     getByteswap(int byteorder);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
367

368
369
void  cdiInitialize(void);

370
371
void uuid2str(const unsigned char uuid[], char uuidstr[]);
int str2uuid(const char *uuidstr, unsigned char uuid[]);
372

373
static inline int cdiUUIDIsNull(const unsigned char uuid[])
Thomas Jahns's avatar
Thomas Jahns committed
374
{
375
376
377
378
  int isNull = 1;
  for (size_t i = 0; i < CDI_UUID_SIZE; ++i)
    isNull &= (uuid[i] == 0);
  return isNull;
Thomas Jahns's avatar
Thomas Jahns committed
379
380
381
}


382
383
char *cdiEscapeSpaces(const char *string);
char *cdiUnescapeSpaces(const char *string, const char **outStringEnd);
384

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

392
393
394
395
396
397
struct streamAssoc
{
  int streamID, vlistID, vlistIDorig;
};

struct streamAssoc
398
399
streamUnpack(char *unpackBuffer, int unpackBufferSize,
             int *unpackBufferPos, int originNamespace, void *context);
400

401
402
int
cdiStreamOpenDefaultDelegate(const char *filename, const char *filemode,
403
404
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
405
406
407
408
409
410
411
412
413
414

void
cdiStreamDefVlist_(int streamID, int vlistID);
void
cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data,
                   int nmiss);
void
cdiStreamwriteVarChunk_(int streamID, int varID, int memtype,
                        const int rect[][2], const void *data, int nmiss);
void
415
416
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
417

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

Thomas Jahns's avatar
Thomas Jahns committed
420
421
void cdiStreamSync_(stream_t *streamptr);

422
423
char *cdiUnitNamePtr(int cdi_unit);

424
425
426
427
428
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

429
#endif  /* _CDI_INT_H */
430
431
432
433
434
435
436
437
438
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */