cdi_int.c 11 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;
39
int cdiConvention           = CDI_CONVENTION_ECHAM;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
int cdiInventoryMode        = 1;
41
int CDI_Version_Info        = 1;
42
size_t CDI_netcdf_hdr_pad   = 0UL;
43

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
double cdiDefaultMissval = -9.E33;

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

#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID


67
int CDI_Debug   = 0;    /* If set to 1, debugging           */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
int CDI_Recopt = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69

70
int cdiGribApiDebug     = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
int cdiDefaultLeveltype = -1;
72
73
74
int cdiDataUnreduced = 0;
int cdiSortName = 0;
int cdiHaveMissval = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76


77
static long cdiGetenvInt(char *envName)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{
  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;
101
102
		  Message("Invalid number string in %s: %s", envName, envString);
		  Warning("%s must comprise only digits [0-9].",envName);
103
		  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
107
108
109
110
		}
	      break;
	    }
	}

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

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

  return (envValue);
}

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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)
134
  fprintf(stderr, "GRIB_API library version : %s\n", gribapiLibraryVersionString());
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
163
164
165
#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) )
    {
166
#if  defined  (HAVE_LIBCGRIBEX)
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
234
235
236
      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);
}

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
270
271
272
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
273
274
275
276

void cdiInitialize(void)
{
  static int Init_CDI = FALSE;
277
  char *envstr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279
280
281
282
283
  long value;

  if ( ! Init_CDI )
    {
      Init_CDI = TRUE;

284
285
286
287
288
#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

289
290
291
      value = cdiGetenvInt("CDI_DEBUG");
      if ( value >= 0 ) CDI_Debug = (int) value;

292
293
294
      value = cdiGetenvInt("CDI_GRIBAPI_DEBUG");
      if ( value >= 0 ) cdiGribApiDebug = (int) value;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
295
296
297
      value = cdiGetenvInt("CDI_RECOPT");
      if ( value >= 0 ) CDI_Recopt = (int) value;

298
299
300
301
302
303
      value = cdiGetenvInt("CDI_REGULARGRID");
      if ( value >= 0 ) cdiDataUnreduced = (int) value;

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

304
305
306
      value = cdiGetenvInt("CDI_HAVE_MISSVAL");
      if ( value >= 0 ) cdiHaveMissval = (int) value;

307
308
309
      value = cdiGetenvInt("CDI_LEVELTYPE");
      if ( value >= 0 ) cdiDefaultLeveltype = (int) value;

310
311
312
      value = cdiGetenvInt("CDI_NETCDF_HDR_PAD");
      if ( value >= 0 ) CDI_netcdf_hdr_pad = (size_t) value;

313
314
      envstr = getenv("CDI_MISSVAL");
      if ( envstr ) cdiDefaultMissval = atof(envstr);
315
      /*
316
317
      envstr = getenv("NC_MISSING_VALUE");
      if ( envstr ) cdiNcMissingValue = atoi(envstr);
318
      */
319
320
      envstr = getenv("NC_CHUNKSIZEHINT");
      if ( envstr ) cdiNcChunksizehint = atoi(envstr);
321

322
323
      envstr = getenv("CDI_CHUNK_ALGO");
      if ( envstr ) cdiSetChunk(envstr);
324

325
326
      envstr = getenv("SPLIT_LTYPE_105");
      if ( envstr ) cdiSplitLtype105 = atoi(envstr);
327

328
329
      envstr = getenv("IGNORE_ATT_COORDINATES");
      if ( envstr ) cdiIgnoreAttCoordinates = atoi(envstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330

331
332
      envstr = getenv("IGNORE_VALID_RANGE");
      if ( envstr ) cdiIgnoreValidRange = atoi(envstr);
333

334
335
      envstr = getenv("CDI_SKIP_RECORDS");
      if ( envstr )
336
	{
337
	  cdiSkipRecords = atoi(envstr);
338
339
340
	  cdiSkipRecords = cdiSkipRecords > 0 ? cdiSkipRecords : 0;
	}

341
342
      envstr = getenv("CDI_CONVENTION");
      if ( envstr )
343
	{
344
	  if ( strcmp(envstr, "CF") == 0 || strcmp(envstr, "cf") == 0 )
345
346
347
348
349
350
351
	    {
	      cdiConvention = CDI_CONVENTION_CF;
	      if ( CDI_Debug )
		Message("CDI convention was set to CF!");
	    }
	}

352
353
      envstr = getenv("CDI_INVENTORY_MODE");
      if ( envstr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
	{
355
	  if ( strncmp(envstr, "time", 4) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
357
358
	    {
	      cdiInventoryMode = 2;
	      if ( CDI_Debug )
359
		Message("Inventory mode was set to timestep!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361
362
	    }
	}

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
      envstr = getenv("CDI_VERSION_INFO");
      if ( envstr )
        {
          int ival = atoi(envstr);
          if ( ival == 0 || ival == 1 )
            {
              CDI_Version_Info = ival;
              if ( CDI_Debug )
                Message("CDI_Version_Info = %s", envstr);
            }
        }


      envstr = getenv("CDI_CALENDAR");
      if ( envstr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
	{
379
	  if      ( strncmp(envstr, "standard", 8) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
	    cdiDefaultCalendar = CALENDAR_STANDARD;
381
	  else if ( strncmp(envstr, "proleptic", 9) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382
	    cdiDefaultCalendar = CALENDAR_PROLEPTIC;
383
	  else if ( strncmp(envstr, "360days", 7) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
	    cdiDefaultCalendar = CALENDAR_360DAYS;
385
	  else if ( strncmp(envstr, "365days", 7) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
386
	    cdiDefaultCalendar = CALENDAR_365DAYS;
387
	  else if ( strncmp(envstr, "366days", 7) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
	    cdiDefaultCalendar = CALENDAR_366DAYS;
389
	  else if ( strncmp(envstr, "none", 4) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
392
	    cdiDefaultCalendar = CALENDAR_NONE;

	  if ( CDI_Debug )
393
	    Message("Default calendar set to %s!", envstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
	}
395
#if  defined  (HAVE_LIBCGRIBEX)
396
      gribSetCalendar(cdiDefaultCalendar);
397
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
398

399
400
      envstr = getenv("PARTAB_INTERN");
      if ( envstr ) cdiPartabIntern = atoi(envstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401

402
403
      envstr = getenv("PARTAB_PATH");
      if ( envstr ) cdiPartabPath = strdup(envstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
405
406
407
    }
}


408
const char *strfiletype(int filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
{
410
  const char *name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411
  int size = (int) (sizeof(Filetypes)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412

Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414
415
  if ( filetype > 0 && filetype < size )
    name = Filetypes[filetype];
  else
416
    name = Filetypes[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
417

Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
  return (name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
421
}


422
void cdiDefGlobal(const char *string, int val)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
423
{
424
425
426
427
428
  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;
429
  else if ( strcmp(string, "NETCDF_HDR_PAD")   == 0 ) CDI_netcdf_hdr_pad = (size_t) val;
430
  else Warning("Unsupported global key: %s", string);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
433


Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
435
436
437
438
439
440
441
442
443
444
445
void cdiDefMissval(double missval)
{
  cdiInitialize();

  cdiDefaultMissval = missval;
}


double cdiInqMissval(void)
{
  cdiInitialize();

446
  return (cdiDefaultMissval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
447
448
}

449
450
451
452
453
454
455
456
457
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */
458