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

#include <stdio.h>
#include <stdarg.h>
Thomas Jahns's avatar
Thomas Jahns committed
7
#include <string.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
9
10
11
12
13
14
#include <errno.h>
#include <math.h>
#include <ctype.h>

#include "dmemory.h"

#include "cdi.h"
15
#include "cdi_cksum.h"
16
#include "cdi_int.h"
17
18
19
20
#include "gribapi.h"
#ifdef HAVE_LIBNETCDF
#include "stream_cdf.h"
#endif
21
22
#include "pio_util.h"
#include "namespace.h"
23
#include "serialize.h"
24
#include "resource_handle.h"
25
#include "resource_unpack.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26

27
28
29
#if  defined  (HAVE_LIBCGRIBEX)
#include "cgribex.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30

31
32
extern int cdiPioSerialOpenFileMap(int streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
35
36
37
int cdiDefaultCalendar = CALENDAR_PROLEPTIC;

int cdiDefaultInstID   = CDI_UNDEFID;
int cdiDefaultModelID  = CDI_UNDEFID;
int cdiDefaultTableID  = CDI_UNDEFID;
38
//int cdiNcMissingValue  = CDI_UNDEFID;
39
int cdiNcChunksizehint = CDI_UNDEFID;
40
int cdiChunkType       = CHUNK_GRID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
int cdiSplitLtype105   = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
42

43
int cdiIgnoreAttCoordinates = FALSE;
44
int cdiIgnoreValidRange     = FALSE;
45
int cdiSkipRecords          = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
int cdiInventoryMode        = 1;
47

Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
50
char *cdiPartabPath   = NULL;
int   cdiPartabIntern = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
53
54
55
56
57
58
double cdiDefaultMissval = -9.E33;

char *Filetypes[] = {
  "UNKNOWN",
  "GRIB",
  "GRIB2",
  "netCDF",
  "netCDF2",
59
  "netCDF4",
60
  "netCDF4c",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
63
  "SERVICE",
  "EXTRA",
  "IEG",
64
  "HDF5",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66
67
68
69
70
};

#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID


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

73
static int  STREAM_Debug = 0;   /* If set to 1, debugging */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
76

int cdiDefaultLeveltype = -1;
static int cdiDataUnreduced = 0;
77
static int cdiSortName = 0;
78
static int cdiHaveMissval = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80


81
82
static int    streamCompareP ( void * streamptr1, void * streamptr2 );
static void   streamDestroyP ( void * streamptr );
83
static void   streamPrintP   ( void * streamptr, FILE * fp );
84
85
static int    streamGetPackSize ( void * streamptr, void *context);
static void   streamPack        ( void * streamptr, void * buff, int size, int * position, void *context );
Deike Kleberg's avatar
Deike Kleberg committed
86
static int    streamTxCode      ( void );
87

88
resOps streamOps = { streamCompareP, streamDestroyP, streamPrintP,
Deike Kleberg's avatar
Deike Kleberg committed
89
90
                     streamGetPackSize,
		     streamPack,
Deike Kleberg's avatar
Deike Kleberg committed
91
                     streamTxCode
92
};
93

Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
long cdiGetenvInt(char *envName)
{
  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;
118
119
		  Message("Invalid number string in %s: %s", envName, envString);
		  Warning("%s must comprise only digits [0-9].",envName);
120
		  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
123
124
125
126
127
		}
	      break;
	    }
	}

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

128
      if ( CDI_Debug ) Message("set %s to %ld", envName, envValue);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
131
132
133
    }

  return (envValue);
}

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
163
164
165
166
167
168
169
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
170
171
172
173
174
175
176
177
178
179
180

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

  if ( ! Init_CDI )
    {
      Init_CDI = TRUE;

181
182
183
184
185
#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

186
187
188
189
190
191
      value = cdiGetenvInt("CDI_REGULARGRID");
      if ( value >= 0 ) cdiDataUnreduced = (int) value;

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

192
193
194
      value = cdiGetenvInt("CDI_HAVE_MISSVAL");
      if ( value >= 0 ) cdiHaveMissval = (int) value;

195
196
197
198
199
      value = cdiGetenvInt("CDI_LEVELTYPE");
      if ( value >= 0 ) cdiDefaultLeveltype = (int) value;

      envString = getenv("CDI_MISSVAL");
      if ( envString ) cdiDefaultMissval = atof(envString);
200
      /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
      envString = getenv("NC_MISSING_VALUE");
202
      if ( envString ) cdiNcMissingValue = atoi(envString);
203
      */
204
205
206
      envString = getenv("NC_CHUNKSIZEHINT");
      if ( envString ) cdiNcChunksizehint = atoi(envString);

207
208
209
      envString = getenv("CDI_CHUNK_ALGO");
      if ( envString ) cdiSetChunk(envString);

210
211
      envString = getenv("SPLIT_LTYPE_105");
      if ( envString ) cdiSplitLtype105 = atoi(envString);
212
213
214

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

216
217
218
      envString = getenv("IGNORE_VALID_RANGE");
      if ( envString ) cdiIgnoreValidRange = atoi(envString);

219
220
221
222
223
224
225
      envString = getenv("CDI_SKIP_RECORDS");
      if ( envString )
	{
	  cdiSkipRecords = atoi(envString);
	  cdiSkipRecords = cdiSkipRecords > 0 ? cdiSkipRecords : 0;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
226
227
228
229
230
231
232
      envString = getenv("GRIB_INVENTORY_MODE");
      if ( envString )
	{
	  if ( strncmp(envString, "time", 4) == 0 )
	    {
	      cdiInventoryMode = 2;
	      if ( CDI_Debug )
233
		Message("Inventory mode was set to timestep!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
235
236
	    }
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
      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 )
254
	    Message("Default calendar set to %s!", envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
	}
256
#if  defined  (HAVE_LIBCGRIBEX)
257
      gribSetCalendar(cdiDefaultCalendar);
258
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259

Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
261
262
263
264
      envString = getenv("PARTAB_INTERN");
      if ( envString ) cdiPartabIntern = atoi(envString);

      envString = getenv("PARTAB_PATH");
      if ( envString ) cdiPartabPath = strdup(envString);
265
266
267

      envString = getenv("STREAM_DEBUG");
      if ( envString ) STREAM_Debug = atoi(envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
269
270
271
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
char *strfiletype(int filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
274
275
  char *name;
  int size = (int) (sizeof(Filetypes)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276

Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
278
279
  if ( filetype > 0 && filetype < size )
    name = Filetypes[filetype];
  else
280
    name = Filetypes[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
281

Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
  return (name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
284
285
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
stream_t *stream_to_pointer(int idx)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
{
288
  return ( stream_t *) reshGetVal ( idx, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
}

291
static
292
void streamDefaultValue ( stream_t * streamptr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
{
  int i;

296
  streamptr->self              = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  streamptr->accesstype        = UNDEFID;
  streamptr->accessmode        = 0;
  streamptr->filetype          = UNDEFID;
  streamptr->byteorder         = UNDEFID;
  streamptr->fileID            = 0;
  streamptr->dimgroupID        = UNDEFID;
  streamptr->filemode          = 0;
  streamptr->numvals           = 0;
  streamptr->filename          = NULL;
  streamptr->record            = NULL;
  streamptr->varsAllocated     = 0;
  streamptr->nrecs             = 0;
  streamptr->nvars             = 0;
  streamptr->vars              = NULL;
  streamptr->varinit           = 0;
  streamptr->ncmode            = 0;
  streamptr->curTsID           = UNDEFID;
  streamptr->rtsteps           = 0;
  streamptr->ntsteps           = UNDEFID;
  streamptr->numTimestep       = 0;
  streamptr->tsteps            = NULL;
  streamptr->tstepsTableSize   = 0;
  streamptr->tstepsNextID      = 0;
  streamptr->historyID         = UNDEFID;
  streamptr->vlistID           = UNDEFID;
  streamptr->globalatts        = 0;
  streamptr->localatts         = 0;
  streamptr->vct.ilev          = 0;
  streamptr->vct.mlev          = 0;
  streamptr->vct.ilevID        = UNDEFID;
  streamptr->vct.mlevID        = UNDEFID;
  streamptr->unreduced         = cdiDataUnreduced;
329
  streamptr->sortname          = cdiSortName;
330
  streamptr->have_missval      = cdiHaveMissval;
Deike Kleberg's avatar
Deike Kleberg committed
331
332
  streamptr->comptype          = COMPRESS_NONE;
  streamptr->complevel         = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
334
335

  basetimeInit(&streamptr->basetime);

336
337
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->xdimID[i]   = UNDEFID;
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->ydimID[i]   = UNDEFID;
338
  for ( i = 0; i < MAX_ZAXES_PS; i++ ) streamptr->zaxisID[i]  = UNDEFID;
339
340
341
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->ncxvarID[i] = UNDEFID;
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->ncyvarID[i] = UNDEFID;
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->ncavarID[i] = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
343
344
345

  streamptr->curfile           = 0;
  streamptr->nfiles            = 0;
  streamptr->fnames            = NULL;
346

347
  streamptr->gribContainers    = NULL;
348
  streamptr->vlistIDorig       = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
352
stream_t *stream_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355

Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
  cdiInitialize(); /* ***************** make MT version !!! */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357

358
359
360
  streamptr = (stream_t *) xmalloc(sizeof(stream_t));
  streamDefaultValue ( streamptr );
  streamptr->self = reshPut (( void * ) streamptr, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
361

362
  return streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
364
365
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
void stream_delete_entry(stream_t *streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
368
{
  int idx;
369

Deike Kleberg's avatar
Deike Kleberg committed
370
  xassert ( streamptr );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
372

  idx = streamptr->self;
373
  free ( streamptr );
374
  reshRemove ( idx, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
376

  if ( STREAM_Debug )
377
    Message("Removed idx %d from stream list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
379
380
}


381
void stream_check_ptr(const char *caller, stream_t *streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382
383
{
  if ( streamptr == NULL )
384
    Errorc("stream undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
385
386
387
}


388
389
390
391
392
int streamSize(void)
{
  return reshCountType ( &streamOps );
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
393

394
void cdiDefGlobal(const char *string, int val)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
{
396
  if ( strcmp(string, "REGULARGRID") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397
398
    {
      cdiDataUnreduced = val;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
    }
400
401
402
403
  else if ( strcmp(string, "SORTNAME") == 0 )
    {
      cdiSortName = val;
    }
404
405
406
407
  else if ( strcmp(string, "HAVE_MISSVAL") == 0 )
    {
      cdiHaveMissval = val;
    }
408
409
410
411
  else if ( strcmp(string, "NC_CHUNKSIZEHINT") == 0 )
    {
      cdiNcChunksizehint = val;
    }
412
413
  else
    {
414
      Warning("Unsupported global key: %s", string);
415
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
416
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
417
418


Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
421
422
423
424
425
426
427
428
429
430
void cdiDefMissval(double missval)
{
  cdiInitialize();

  cdiDefaultMissval = missval;
}


double cdiInqMissval(void)
{
  cdiInitialize();

431
  return (cdiDefaultMissval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
433
434
}


435
void vlist_check_contents(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
436
437
{
  int index, nzaxis, zaxisID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
438

Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
440
441
442
443
444
445
446
447
448
449
450
451
  nzaxis = vlistNzaxis(vlistID);

  for ( index = 0; index < nzaxis; index++ )
    {
      zaxisID = vlistZaxis(vlistID, index);
      if ( zaxisInqType(zaxisID) == ZAXIS_GENERIC )
	cdiCheckZaxis(zaxisID);
    }
}


int streamInqFileID(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
453

454
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455
456

  return (streamptr->fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
458
}

459
460
/* not used anymore */
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461
462
void streamDefineTaxis(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464

465
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
466
467

  if ( streamptr->tsteps == NULL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
469
470
    {
      int varID, nvars;
      int vlistID;
471

472
      vlistID = streamptr->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
474
475

      nvars = vlistNvars(vlistID);
      for ( varID = 0; varID < nvars; varID++ )
476
	if ( vlistInqVarTsteptype(vlistID, varID) == TSTEP_CONSTANT ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
477
478
479
480
481
482
483
484

      if ( varID == nvars )
	{
	  int taxisID;

	  taxisID = vlistInqTaxis(vlistID);
	  if ( taxisID == CDI_UNDEFID )
	    {
485
	      taxisID = taxisCreate(TAXIS_ABSOLUTE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
487
	      vlistDefTaxis(vlistID, taxisID);
	    }
488

Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
490
491
	  (void) streamDefTimestep(streamID, 0);
	}
      else
492
	Error("time axis undefined");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
494
    }
}
495
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
497
498

void streamDefDimgroupID(int streamID, int dimgroupID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500

501
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503

  streamptr->dimgroupID = dimgroupID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
505
506
507
508
}


int streamInqDimgroupID(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
510

511
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
513

  return (streamptr->dimgroupID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
516
517
518
}


void cdiDefAccesstype(int streamID, int type)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520

521
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
522

Uwe Schulzweida's avatar
Uwe Schulzweida committed
523
  if ( streamptr->accesstype == UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
524
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
      streamptr->accesstype = type;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
528
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
      if ( streamptr->accesstype != type )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
530
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
	  if ( streamptr->accesstype == TYPE_REC )
532
	    Error("Changing access type from REC to VAR not allowed!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
533
	  else
534
	    Error("Changing access type from VAR to REC not allowed!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
537
538
539
540
541
	}
    }
}


int cdiInqAccesstype(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543

544
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
546

  return (streamptr->accesstype);
Deike Kleberg's avatar
Deike Kleberg committed
547
548
549
550
551
552
553
554
} 


int streamInqNvars ( int streamID )
{
  stream_t * streamptr;
  streamptr = ( stream_t * ) reshGetVal ( streamID, &streamOps );
  return ( streamptr->nvars );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
}
556
557
558
559


int  streamCompareP ( void * streamptr1, void * streamptr2 )
{
Deike Kleberg's avatar
Deike Kleberg committed
560
561
562
563
564
565
  stream_t * s1 = ( stream_t * ) streamptr1;
  stream_t * s2 = ( stream_t * ) streamptr2;
  int differ = -1;
  int equal  = 0;
  int len;

Deike Kleberg's avatar
Deike Kleberg committed
566
567
  xassert ( s1 );
  xassert ( s2 );
Deike Kleberg's avatar
Deike Kleberg committed
568
569

  if ( s1->filetype  != s2->filetype  ) return differ;
570
  if (  namespaceAdaptKey2 ( s1->vlistIDorig ) !=
Deike Kleberg's avatar
Deike Kleberg committed
571
572
573
	namespaceAdaptKey2 ( s2->vlistIDorig )) return differ;
  if ( s1->byteorder != s2->byteorder ) return differ;
  if ( s1->comptype  != s2->comptype  ) return differ;
574
  if ( s1->complevel != s2->complevel ) return differ;
Deike Kleberg's avatar
Deike Kleberg committed
575
576
577
578

  if ( s1->filename )
    {
      len = strlen ( s1->filename ) + 1;
579
      if ( memcmp ( s1->filename, s2->filename, len ))
Deike Kleberg's avatar
Deike Kleberg committed
580
581
	return differ;
    }
582
  else if ( s2->filename )
Deike Kleberg's avatar
Deike Kleberg committed
583
    return differ;
584

Deike Kleberg's avatar
Deike Kleberg committed
585
  return equal;
586
587
588
589
590
}


void streamDestroyP ( void * streamptr )
{
591
592
593
  int id;
  stream_t * sp = ( stream_t * ) streamptr;

Deike Kleberg's avatar
Deike Kleberg committed
594
  xassert ( sp );
595
596
597

  id = sp->self;
  streamClose ( id );
598
599
600
}


601
void streamPrintP   ( void * streamptr, FILE * fp )
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
{
  stream_t * sp = ( stream_t * ) streamptr;

  if ( !sp ) return;

  fprintf ( fp, "#\n");
  fprintf ( fp, "# streamID %d\n", sp->self);
  fprintf ( fp, "#\n"); 
  fprintf ( fp, "self          = %d\n", sp->self );
  fprintf ( fp, "accesstype    = %d\n", sp->accesstype );
  fprintf ( fp, "accessmode    = %d\n", sp->accessmode ); 
  fprintf ( fp, "filetype      = %d\n", sp->filetype );
  fprintf ( fp, "byteorder     = %d\n", sp->byteorder );
  fprintf ( fp, "fileID        = %d\n", sp->fileID );
  fprintf ( fp, "dimgroupID    = %d\n", sp->dimgroupID );
  fprintf ( fp, "filemode      = %d\n", sp->filemode );
  fprintf ( fp, "//off_t numvals;\n" );
  fprintf ( fp, "filename      = %s\n", sp->filename );
  fprintf ( fp, "//Record   *record;\n" );
  fprintf ( fp, "nrecs         = %d\n", sp->nrecs );
  fprintf ( fp, "nvars         = %d\n", sp->nvars );
  fprintf ( fp, "varlocked     = %d\n", sp->varlocked );
624
  fprintf ( fp, "//svarinfo_t *vars;\n" );
625
626
627
628
629
630
  fprintf ( fp, "varsAllocated = %d\n", sp->varsAllocated );
  fprintf ( fp, "varinit       = %d\n", sp->varinit );
  fprintf ( fp, "curTsID       = %d\n", sp->curTsID );
  fprintf ( fp, "rtsteps       = %d\n", sp->rtsteps );
  fprintf ( fp, "//long ntsteps;\n" );
  fprintf ( fp, "numTimestep   = %d\n", sp->numTimestep );
631
  fprintf ( fp, "//  tsteps_t   *tsteps;\n" );
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  fprintf ( fp, "tstepsTableSize= %d\n", sp->tstepsTableSize );
  fprintf ( fp, "tstepsNextID  = %d\n", sp->tstepsNextID );
  fprintf ( fp, "//BaseTime  basetime;\n" );
  fprintf ( fp, "ncmode        = %d\n", sp->ncmode );
  fprintf ( fp, "vlistID       = %d\n", sp->vlistID );
  fprintf ( fp, "//  int       xdimID[MAX_GRIDS_PS];\n" );
  fprintf ( fp, "//  int       ydimID[MAX_GRIDS_PS];\n" );
  fprintf ( fp, "//  int       zaxisID[MAX_ZAXES_PS];\n" );
  fprintf ( fp, "//  int       ncxvarID[MAX_GRIDS_PS];\n" );
  fprintf ( fp, "//  int       ncyvarID[MAX_GRIDS_PS];\n" );
  fprintf ( fp, "//  int       ncavarID[MAX_GRIDS_PS];\n" );
  fprintf ( fp, "historyID     = %d\n", sp->historyID );
  fprintf ( fp, "globalatts    = %d\n", sp->globalatts );
  fprintf ( fp, "localatts     = %d\n", sp->localatts );
  fprintf ( fp, "//  VCT       vct;\n" );
  fprintf ( fp, "unreduced     = %d\n", sp->unreduced );
  fprintf ( fp, "sortname      = %d\n", sp->sortname );
  fprintf ( fp, "have_missval  = %d\n", sp->have_missval );
Deike Kleberg's avatar
Deike Kleberg committed
650
651
  fprintf ( fp, "ztype         = %d\n", sp->comptype );
  fprintf ( fp, "zlevel        = %d\n", sp->complevel );
652
653
654
  fprintf ( fp, "curfile       = %d\n", sp->curfile );
  fprintf ( fp, "nfiles        = %d\n", sp->nfiles );
  fprintf ( fp, "//  char    **fnames;\n" );
655
  fprintf ( fp, "//  void    **gribContainers;\n" );
Deike Kleberg's avatar
Deike Kleberg committed
656
  fprintf ( fp, "vlistIDorig   = %d\n", sp->vlistIDorig );
657
}
658
659


660
void streamGetIndexList ( int nstreams, int * streamIndexList )
Deike Kleberg's avatar
Deike Kleberg committed
661
{
662
  reshGetResHListOfType ( nstreams, streamIndexList, &streamOps );
Deike Kleberg's avatar
Deike Kleberg committed
663
664
}

665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
void
cdiStreamSetupVlist(stream_t *streamptr, int vlistID, int vlistIDorig)
{
  int nvars = vlistNvars(vlistID);
  streamptr->vlistID = vlistID;
  streamptr->vlistIDorig = vlistIDorig;
  for (int varID = 0; varID < nvars; varID++ )
    {
      int gridID  = vlistInqVarGrid(vlistID, varID);
      int zaxisID = vlistInqVarZaxis(vlistID, varID);
      stream_new_var(streamptr, gridID, zaxisID);
      if ( streamptr->have_missval )
        vlistDefVarMissval(vlistID, varID,
                           vlistInqVarMissval(vlistID, varID));
    }

681
  if (streamptr->filemode == 'w' )
682
683
684
685
686
687
    {
      if ( streamptr->filetype == FILETYPE_NC  ||
           streamptr->filetype == FILETYPE_NC2 ||
           streamptr->filetype == FILETYPE_NC4 ||
           streamptr->filetype == FILETYPE_NC4C )
        {
688
#ifdef HAVE_LIBNETCDF
689
          void (*myCdfDefVars)(stream_t *streamptr)
690
691
            = (void (*)(stream_t *))
            namespaceSwitchGet(NSSWITCH_CDF_STREAM_SETUP).func;
692
          myCdfDefVars(streamptr);
693
#endif
694
695
696
697
698
699
700
701
702
        }
      else if ( streamptr->filetype == FILETYPE_GRB  ||
                streamptr->filetype == FILETYPE_GRB2 )
        {
          gribContainersNew(streamptr);
        }
    }
}

Deike Kleberg's avatar
Deike Kleberg committed
703

704
static int
705
streamTxCode(void)
706
707
708
709
{
  return STREAM;
}

Deike Kleberg's avatar
Deike Kleberg committed
710

Deike Kleberg's avatar
Deike Kleberg committed
711
int streamNint = 11 ;
Deike Kleberg's avatar
Deike Kleberg committed
712
713


714
715
static int
streamGetPackSize(void * voidP, void *context)
716
{
Deike Kleberg's avatar
Deike Kleberg committed
717
  stream_t * streamP = ( stream_t * ) voidP;
718
719
  int packBufferSize
    = serializeGetSize(streamNint, DATATYPE_INT, context)
720
    + serializeGetSize(2, DATATYPE_UINT32, context)
721
722
723
    + serializeGetSize((int)strlen(streamP->filename) + 1,
                       DATATYPE_TXT, context)
    + serializeGetSize(1, DATATYPE_FLT64, context);
724
  return packBufferSize;
725
726
727
}


728
729
730
static void
streamPack(void * streamptr, void * packBuffer, int packBufferSize,
           int * packBufferPos, void *context)
731
{
732
  stream_t * streamP = ( stream_t * ) streamptr;
733
  int intBuffer[streamNint];
Deike Kleberg's avatar
Deike Kleberg committed
734
735
736

  intBuffer[0]  = streamP->self;
  intBuffer[1]  = streamP->filetype;
737
  intBuffer[2]  = (int)strlen(streamP->filename) + 1;
Deike Kleberg's avatar
Deike Kleberg committed
738
739
740
741
  intBuffer[3]  = streamP->vlistID;
  intBuffer[4]  = streamP->vlistIDorig;
  intBuffer[5]  = streamP->byteorder;
  intBuffer[6]  = streamP->comptype;
742
  intBuffer[7]  = streamP->complevel;
Deike Kleberg's avatar
Deike Kleberg committed
743
744
745
  intBuffer[8]  = cdiDataUnreduced;
  intBuffer[9]  = cdiSortName;
  intBuffer[10] = cdiHaveMissval;
746
747

  serializePack(intBuffer, streamNint, DATATYPE_INT, packBuffer, packBufferSize, packBufferPos, context);
748
749
  uint32_t d = cdiCheckSum(DATATYPE_INT, streamNint, intBuffer);
  serializePack(&d, 1, DATATYPE_UINT32, packBuffer, packBufferSize, packBufferPos, context);
750
751
752

  serializePack(&cdiDefaultMissval, 1, DATATYPE_FLT64, packBuffer, packBufferSize, packBufferPos, context);
  serializePack(streamP->filename, intBuffer[2], DATATYPE_TXT, packBuffer, packBufferSize, packBufferPos, context);
753
  d = cdiCheckSum(DATATYPE_TXT, intBuffer[2], &streamP->filename);
754
  serializePack(&d, 1, DATATYPE_UINT32, packBuffer, packBufferSize, packBufferPos, context);
755
}
756

757
758
759
760
761
struct streamAssoc
streamUnpack(char * unpackBuffer, int unpackBufferSize,
             int * unpackBufferPos, int nspTarget, void *context)
{
  int intBuffer[streamNint], streamID;
762
  uint32_t d;
763
764
765
766
767
  char filename[CDI_MAX_NAME];

  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  intBuffer, streamNint, DATATYPE_INT, context);
  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
768
                  &d, 1, DATATYPE_UINT32, context);
769
  xassert(cdiCheckSum(DATATYPE_INT, streamNint, intBuffer) == d);
770
771
772
773
774
775

  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  &cdiDefaultMissval, 1, DATATYPE_FLT64, context);
  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  &filename, intBuffer[2], DATATYPE_TXT, context);
  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
776
                  &d, 1, DATATYPE_UINT32, context);
777
  xassert(d == cdiCheckSum(DATATYPE_TXT, intBuffer[2], filename));
778
779
780
781
782
783
784
785
786
787
788
789
790
791
  streamID = streamOpenWrite ( filename, intBuffer[1] );
  xassert ( streamID >= 0 &&
            namespaceAdaptKey ( intBuffer[0], nspTarget ) == streamID );
  streamDefByteorder(streamID, intBuffer[5]);
  streamDefCompType(streamID, intBuffer[6]);
  streamDefCompLevel(streamID, intBuffer[7]);
  cdiDefGlobal("REGULARGRID", intBuffer[8]);
  cdiDefGlobal("SORTNAME", intBuffer[9]);
  cdiDefGlobal("HAVE_MISSVAL", intBuffer[10]);
  struct streamAssoc retval = { streamID, intBuffer[3], intBuffer[4] };
  return retval;
}


792
793
794
795
796
797
798
799
800
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */