cdi_int.c 10.4 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <stdarg.h>
#include <ctype.h>

8
#include "cdf.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
9
#include "cdi.h"
10
#include "cdi_int.h"
11
12
#include "dmemory.h"
#include "file.h"
13
14
15
16
#include "gribapi.h"
#ifdef HAVE_LIBNETCDF
#include "stream_cdf.h"
#endif
17
#include "namespace.h"
18
#include "resource_handle.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19

20
21
22
#if  defined  (HAVE_LIBCGRIBEX)
#include "cgribex.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23

24
25
extern int cdiPioSerialOpenFileMap(int streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28
29
30
int cdiDefaultCalendar = CALENDAR_PROLEPTIC;

int cdiDefaultInstID   = CDI_UNDEFID;
int cdiDefaultModelID  = CDI_UNDEFID;
int cdiDefaultTableID  = CDI_UNDEFID;
31
//int cdiNcMissingValue  = CDI_UNDEFID;
32
int cdiNcChunksizehint = CDI_UNDEFID;
33
int cdiChunkType       = CHUNK_GRID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
int cdiSplitLtype105   = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35

36
int cdiIgnoreAttCoordinates = FALSE;
37
int cdiIgnoreValidRange     = FALSE;
38
int cdiSkipRecords          = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
int cdiInventoryMode        = 1;
40
size_t CDI_netcdf_hdr_pad   = 0UL;
41

Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
43
44
char *cdiPartabPath   = NULL;
int   cdiPartabIntern = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
46
double cdiDefaultMissval = -9.E33;

47
const char Filetypes[][9] = {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
50
51
52
  "UNKNOWN",
  "GRIB",
  "GRIB2",
  "netCDF",
  "netCDF2",
53
  "netCDF4",
54
  "netCDF4c",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
57
  "SERVICE",
  "EXTRA",
  "IEG",
58
  "HDF5",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
61
62
63
64
};

#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID


65
int CDI_Debug   = 0;    /* If set to 1, debugging           */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66

67
int cdiGribApiDebug     = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
int cdiDefaultLeveltype = -1;
69
70
71
int cdiDataUnreduced = 0;
int cdiSortName = 0;
int cdiHaveMissval = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
73


74
static long cdiGetenvInt(char *envName)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{
  char *envString;
  long envValue = -1;
  long fact = 1;

  envString = getenv(envName);

  if ( envString )
    {
      int loop, len;

      len = (int) strlen(envString);
      for ( loop = 0; loop < len; loop++ )
	{
	  if ( ! isdigit((int) envString[loop]) )
	    {
	      switch ( tolower((int) envString[loop]) )
		{
		case 'k':  fact = 1024;        break;
		case 'm':  fact = 1048576;     break;
		case 'g':  fact = 1073741824;  break;
		default:
		  fact = 0;
98
99
		  Message("Invalid number string in %s: %s", envName, envString);
		  Warning("%s must comprise only digits [0-9].",envName);
100
		  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
102
103
104
105
106
107
		}
	      break;
	    }
	}

      if ( fact ) envValue = fact*atol(envString);

108
      if ( CDI_Debug ) Message("set %s to %ld", envName, envValue);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
111
112
113
    }

  return (envValue);
}

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
static void
cdiPrintDefaults(void)
{
  fprintf(stderr, "default instID     :  %d\n"
          "default modelID    :  %d\n"
          "default tableID    :  %d\n"
          "default missval    :  %g\n", cdiDefaultInstID,
          cdiDefaultModelID, cdiDefaultTableID, cdiDefaultMissval);
}

void cdiPrintVersion(void)
{
  fprintf(stderr, "     CDI library version : %s\n", cdiLibraryVersion());
#if  defined  (HAVE_LIBCGRIBEX)
  fprintf(stderr, " CGRIBEX library version : %s\n", cgribexLibraryVersion());
#endif
#if  defined  (HAVE_LIBGRIB_API)
131
  fprintf(stderr, "GRIB_API library version : %s\n", gribapiLibraryVersionString());
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#endif
#if  defined  (HAVE_LIBNETCDF)
  fprintf(stderr, "  netCDF library version : %s\n", cdfLibraryVersion());
#endif
#if  defined  (HAVE_LIBHDF5)
  fprintf(stderr, "    HDF5 library version : %s\n", hdfLibraryVersion());
#endif
#if  defined  (HAVE_LIBSERVICE)
  fprintf(stderr, " SERVICE library version : %s\n", srvLibraryVersion());
#endif
#if  defined  (HAVE_LIBEXTRA)
  fprintf(stderr, "   EXTRA library version : %s\n", extLibraryVersion());
#endif
#if  defined  (HAVE_LIBIEG)
  fprintf(stderr, "     IEG library version : %s\n", iegLibraryVersion());
#endif
  fprintf(stderr, "    FILE library version : %s\n", fileLibraryVersion());
}

void cdiDebug(int level)
{
  if ( level == 1 || (level &  2) ) CDI_Debug = 1;

  if ( CDI_Debug ) Message("debug level %d", level);

  if ( level == 1 || (level &  4) ) memDebug(1);

  if ( level == 1 || (level &  8) ) fileDebug(1);

  if ( level == 1 || (level & 16) )
    {
163
#if  defined  (HAVE_LIBCGRIBEX)
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
      gribSetDebug(1);
#endif
#if  defined  (HAVE_LIBNETCDF)
      cdfDebug(1);
#endif
#if  defined  (HAVE_LIBSERVICE)
      srvDebug(1);
#endif
#if  defined  (HAVE_LIBEXTRA)
      extDebug(1);
#endif
#if  defined  (HAVE_LIBIEG)
      iegDebug(1);
#endif
    }

  if ( CDI_Debug )
    {
      cdiPrintDefaults();
      cdiPrintDatatypes();
    }
}


int cdiHaveFiletype(int filetype)
{
  int status = 0;

  switch (filetype)
    {
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:  { status = 1; break; }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:  { status = 1; break; }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:  { status = 1; break; }
#endif
#if  defined  (HAVE_LIBGRIB)
#if  defined  (HAVE_LIBGRIB_API) || defined  (HAVE_LIBCGRIBEX)
    case FILETYPE_GRB:  { status = 1; break; }
#endif
#if  defined  (HAVE_LIBGRIB_API)
    case FILETYPE_GRB2: { status = 1; break; }
#endif
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:   { status = 1; break; }
#if  defined  (HAVE_NETCDF2)
    case FILETYPE_NC2:  { status = 1; break; }
#endif
#if  defined  (HAVE_NETCDF4)
    case FILETYPE_NC4:  { status = 1; break; }
    case FILETYPE_NC4C: { status = 1; break; }
#endif
#endif
    default: { status = 0; break; }
    }

  return (status);
}

void cdiDefTableID(int tableID)
{
  cdiDefaultTableID = tableID;
  int modelID = cdiDefaultModelID = tableInqModel(tableID);
  cdiDefaultInstID = modelInqInstitut(modelID);
}

234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
static
void cdiSetChunk(const char *chunkAlgo)
{
  //char *pch;
  //size_t len = strlen(chunkAlgo);
  int algo = -1;

  if      ( strcmp("auto",  chunkAlgo)   == 0 ) algo = CHUNK_AUTO;
  else if ( strcmp("grid",  chunkAlgo)   == 0 ) algo = CHUNK_GRID;
  else if ( strcmp("lines", chunkAlgo)   == 0 ) algo = CHUNK_LINES;
  /*
  else if ( (pch = strstr(chunkAlgo,"x")) != 0 )
    {
      int ix, iy;
      ix = atoi(chunkAlgo);
      iy = atoi(pch+1);
      if ( ix > 0 && iy > 0 )
        {
          cdiChunkX = ix;
          cdiChunkY = iy;
          algo = CHUNK_USER;
        }
      else
        Warning("Invalid environment variable CDI_CHUNK_ALGO: %s", chunkAlgo);
    }
  */
  else
    Warning("Invalid environment variable CDI_CHUNK_ALGO: %s", chunkAlgo);

  if ( algo != -1 )
    {
      cdiChunkType = algo;
      if ( CDI_Debug ) Message("set ChunkAlgo to %s", chunkAlgo);
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
270
271
272
273
274
275
276
277
278
279
280

void cdiInitialize(void)
{
  static int Init_CDI = FALSE;
  char *envString;
  long value;

  if ( ! Init_CDI )
    {
      Init_CDI = TRUE;

281
282
283
284
285
#if  defined  (HAVE_LIBCGRIBEX)
      gribFixZSE(1);   // 1: Fix ZeroShiftError of simple packed spherical harmonics
      gribSetConst(1); // 1: Don't pack constant fields on regular grids
#endif

286
287
288
      value = cdiGetenvInt("CDI_DEBUG");
      if ( value >= 0 ) CDI_Debug = (int) value;

289
290
291
      value = cdiGetenvInt("CDI_GRIBAPI_DEBUG");
      if ( value >= 0 ) cdiGribApiDebug = (int) value;

292
293
294
295
296
297
      value = cdiGetenvInt("CDI_REGULARGRID");
      if ( value >= 0 ) cdiDataUnreduced = (int) value;

      value = cdiGetenvInt("CDI_SORTNAME");
      if ( value >= 0 ) cdiSortName = (int) value;

298
299
300
      value = cdiGetenvInt("CDI_HAVE_MISSVAL");
      if ( value >= 0 ) cdiHaveMissval = (int) value;

301
302
303
      value = cdiGetenvInt("CDI_LEVELTYPE");
      if ( value >= 0 ) cdiDefaultLeveltype = (int) value;

304
305
306
      value = cdiGetenvInt("CDI_NETCDF_HDR_PAD");
      if ( value >= 0 ) CDI_netcdf_hdr_pad = (size_t) value;

307
308
      envString = getenv("CDI_MISSVAL");
      if ( envString ) cdiDefaultMissval = atof(envString);
309
      /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
      envString = getenv("NC_MISSING_VALUE");
311
      if ( envString ) cdiNcMissingValue = atoi(envString);
312
      */
313
314
315
      envString = getenv("NC_CHUNKSIZEHINT");
      if ( envString ) cdiNcChunksizehint = atoi(envString);

316
317
318
      envString = getenv("CDI_CHUNK_ALGO");
      if ( envString ) cdiSetChunk(envString);

319
320
      envString = getenv("SPLIT_LTYPE_105");
      if ( envString ) cdiSplitLtype105 = atoi(envString);
321
322
323

      envString = getenv("IGNORE_ATT_COORDINATES");
      if ( envString ) cdiIgnoreAttCoordinates = atoi(envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324

325
326
327
      envString = getenv("IGNORE_VALID_RANGE");
      if ( envString ) cdiIgnoreValidRange = atoi(envString);

328
329
330
331
332
333
334
      envString = getenv("CDI_SKIP_RECORDS");
      if ( envString )
	{
	  cdiSkipRecords = atoi(envString);
	  cdiSkipRecords = cdiSkipRecords > 0 ? cdiSkipRecords : 0;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
      envString = getenv("CDI_INVENTORY_MODE");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
336
337
338
339
340
341
      if ( envString )
	{
	  if ( strncmp(envString, "time", 4) == 0 )
	    {
	      cdiInventoryMode = 2;
	      if ( CDI_Debug )
342
		Message("Inventory mode was set to timestep!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344
345
	    }
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
      envString = getenv("CDI_CALENDAR");
      if ( envString )
	{
	  if      ( strncmp(envString, "standard", 8) == 0 )
	    cdiDefaultCalendar = CALENDAR_STANDARD;
	  else if ( strncmp(envString, "proleptic", 9) == 0 )
	    cdiDefaultCalendar = CALENDAR_PROLEPTIC;
	  else if ( strncmp(envString, "360days", 7) == 0 )
	    cdiDefaultCalendar = CALENDAR_360DAYS;
	  else if ( strncmp(envString, "365days", 7) == 0 )
	    cdiDefaultCalendar = CALENDAR_365DAYS;
	  else if ( strncmp(envString, "366days", 7) == 0 )
	    cdiDefaultCalendar = CALENDAR_366DAYS;
	  else if ( strncmp(envString, "none", 4) == 0 )
	    cdiDefaultCalendar = CALENDAR_NONE;

	  if ( CDI_Debug )
363
	    Message("Default calendar set to %s!", envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
	}
365
#if  defined  (HAVE_LIBCGRIBEX)
366
      gribSetCalendar(cdiDefaultCalendar);
367
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368

Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
371
372
373
      envString = getenv("PARTAB_INTERN");
      if ( envString ) cdiPartabIntern = atoi(envString);

      envString = getenv("PARTAB_PATH");
      if ( envString ) cdiPartabPath = strdup(envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
376
377
    }
}


378
const char *strfiletype(int filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
{
380
  const char *name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
  int size = (int) (sizeof(Filetypes)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382

Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
384
385
  if ( filetype > 0 && filetype < size )
    name = Filetypes[filetype];
  else
386
    name = Filetypes[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387

Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
  return (name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
390
391
}


392
void cdiDefGlobal(const char *string, int val)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
{
394
395
396
397
398
  if      ( strcmp(string, "REGULARGRID")      == 0 ) cdiDataUnreduced = val;
  else if ( strcmp(string, "GRIBAPI_DEBUG")    == 0 ) cdiGribApiDebug = val;
  else if ( strcmp(string, "SORTNAME")         == 0 ) cdiSortName = val;
  else if ( strcmp(string, "HAVE_MISSVAL")     == 0 ) cdiHaveMissval = val;
  else if ( strcmp(string, "NC_CHUNKSIZEHINT") == 0 ) cdiNcChunksizehint = val;
399
  else if ( strcmp(string, "NETCDF_HDR_PAD")   == 0 ) CDI_netcdf_hdr_pad = (size_t) val;
400
  else Warning("Unsupported global key: %s", string);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
403


Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
405
406
407
408
409
410
411
412
413
414
415
void cdiDefMissval(double missval)
{
  cdiInitialize();

  cdiDefaultMissval = missval;
}


double cdiInqMissval(void)
{
  cdiInitialize();

416
  return (cdiDefaultMissval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
417
418
}

419
420
421
422
423
424
425
426
427
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */