cdi_int.h 9.83 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
18
/* dummy use of unused parameters to silence compiler warnings */
#define  UNUSED(x) (void)x

Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
#ifndef strdupx
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20
21
22
23
24
#ifndef strdup
char *strdup(const char *s);
#endif
#define strdupx  strdup
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
28
29
30
31
#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
32
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
#endif

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


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

Thomas Jahns's avatar
Thomas Jahns committed
68
69
#include "cdi.h"

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

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

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

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

#define  TYPE_REC  0
#define  TYPE_VAR  1

105
106
107
#define  MEMTYPE_DOUBLE  1
#define  MEMTYPE_FLOAT   2

Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
109
typedef struct
{
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  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
127
128
129
130
131
132
133
134
}
Record;


typedef struct
{
  off_t     position;
  size_t    size;
135
  int       zip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
  int       param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
  int       ilevel;
  int       ilevel2;
  int       ltype;
140
  int       tsteptype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
143
  short     used;
  short     varID;
  short     levelID;
144
  char      varname[32]; /* needed for grib decoding with GRIB_API */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
record_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
148
149


typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
152
153
154
155
156
157
158
159
  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                */
160
  taxis_t   taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
}
162
tsteps_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
166
167
168
169
170
171
172
173
174


typedef struct {
  int       ncvarid;
  int       nlevs;
  int      *level;       /* record IDs */
  int      *lindex;      /* level index */
  int       defmiss;     /* TRUE if missval is defined in file */

  int       isUsed;
  int       gridID;
  int       zaxisID;
175
  int       tsteptype;   /* TSTEP_* */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
}
177
svarinfo_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
180
181
182
183
184
185
186
187
188
189


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


typedef struct {
190
191
192
193
194
195
196
197
198
199
  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
200
  int         nrecs;        /* number of records                  */
201
202
203
204
205
206
207
208
209
  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;
210
  basetime_t  basetime;
211
212
  int         ncmode;
  int         vlistID;
213
214
215
  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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  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;
234
  /* only used by MPI-parallelized version of library */
235
  int       ownerRank;    // MPI rank of owner process
236
237
238
239
240
  /* ---------------------------------- */
  /* Local change: 2013-02-18, FP (DWD) */
  /* ---------------------------------- */

  void *gh; // grib handle
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
stream_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245


extern int CDI_Debug;      /* If set to 1, debuggig (default 0)            */
246
extern int cdiGribApiDebug;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
249
250
251
extern double cdiDefaultMissval;
extern int cdiDefaultInstID;
extern int cdiDefaultModelID;
extern int cdiDefaultTableID;
extern int cdiDefaultLeveltype;
252
//extern int cdiNcMissingValue;
253
extern int cdiNcChunksizehint;
254
extern int cdiChunkType;
255
extern int cdiSplitLtype105;
256
257
258
259
260
extern int cdiDataUnreduced;
extern int cdiSortName;
extern int cdiHaveMissval;
extern int STREAM_Debug;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
261

Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
263
extern char *cdiPartabPath;
extern int   cdiPartabIntern;
264
265
266
267
268
269
270
extern const resOps streamOps;

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

272
273
274
275
276
277
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
278

Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
280
int     streamInqFileID(int streamID);

281
282
void    gridDefHasDims(int gridID, int hasdims);
int     gridInqHasDims(int gridID);
283
const char *gridNamePtr(int gridtype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
char   *zaxisNamePtr(int leveltype);
285
int     zaxisInqLevelID(int zaxisID, double level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286

287
void    streamCheckID(const char *caller, int streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
289
290
291
292

void    streamDefineTaxis(int streamID);

int     streamsNewEntry(int filetype);
void    streamsInitEntry(int streamID);
293
void    cdiStreamSetupVlist(stream_t *streamptr, int vlistID, int vlistIDorig);
294
int     stream_new_var(stream_t *streamptr, int gridID, int zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
295

296
int     tstepsNewEntry(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297

298
const char *strfiletype(int filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299

300
void    cdi_generate_vars(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301

302
void    vlist_check_contents(int vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303

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

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

308
void    cdiCreateTimesteps(stream_t *streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309

Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
void    recordInitEntry(record_t *record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
312
313
314
315
316
317
318

void    cdiCheckZaxis(int zaxisID);

void    cdiPrintDatatypes(void);

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

Deike Kleberg's avatar
Deike Kleberg committed
319
320
321
int     getByteswap(int byteorder);

int     streamSize ();
322
void    streamGetIndexList ( int, int * );
Deike Kleberg's avatar
Deike Kleberg committed
323

Uwe Schulzweida's avatar
Uwe Schulzweida committed
324

325
326
void  cdiInitialize(void);

327
328
void uuid2str(const unsigned char *uuid, char *uuidstr);
int str2uuid(const char *uuidstr, unsigned char *uuid);
Thomas Jahns's avatar
Thomas Jahns committed
329
330
331
static inline int
cdiUUIDIsNull(const unsigned char uuid[CDI_UUID_SIZE])
{
Thomas Jahns's avatar
Thomas Jahns committed
332
  static const unsigned char uuid_nil[CDI_UUID_SIZE];
Thomas Jahns's avatar
Thomas Jahns committed
333
334
335
  return !memcmp(uuid, uuid_nil, CDI_UUID_SIZE);
}

336

337
338
339
340
341
342
#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
343

344
345
346
347
348
349
350
struct streamAssoc
{
  int streamID, vlistID, vlistIDorig;
};

struct streamAssoc
streamUnpack(char * unpackBuffer, int unpackBufferSize,
351
             int * unpackBufferPos, int originNamespace, void *context);
352

353
354
int
cdiStreamOpenDefaultDelegate(const char *filename, const char *filemode,
355
356
                             int filetype, stream_t *streamptr,
                             int recordBufIsToBeCreated);
357
358
359
360
361
362
363
364
365
366

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
367
368
cdiStreamCloseDefaultDelegate(stream_t *streamptr,
                              int recordBufIsToBeDeleted);
369

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

Thomas Jahns's avatar
Thomas Jahns committed
372
373
void cdiStreamSync_(stream_t *streamptr);

374
375
char *cdiUnitNamePtr(int cdi_unit);

376
377
378
379
380
void zaxisGetIndexList(int nzaxis, int *zaxisIndexList);

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

381
#endif  /* _CDI_INT_H */
382
383
384
385
386
387
388
389
390
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */