cdi_int.c 21.4 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_int.h"
16
17
18
19
#include "gribapi.h"
#ifdef HAVE_LIBNETCDF
#include "stream_cdf.h"
#endif
20
21
#include "pio_util.h"
#include "namespace.h"
22
#include "serialize.h"
23
#include "resource_handle.h"
24
25
26
27
#include "resource_unpack.h"
#ifdef USE_MPI
#include "pio_comm.h"
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28

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

33
34
extern int cdiPioSerialOpenFileMap(int streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
37
38
39
int cdiDefaultCalendar = CALENDAR_PROLEPTIC;

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

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

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

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

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

#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID


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

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

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


83
84
static int    streamCompareP ( void * streamptr1, void * streamptr2 );
static void   streamDestroyP ( void * streamptr );
85
static void   streamPrintP   ( void * streamptr, FILE * fp );
86
87
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
88
static int    streamTxCode      ( void );
89

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

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

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

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

  return (envValue);
}

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
170
171
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
172
173
174
175
176
177
178
179
180
181
182

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

  if ( ! Init_CDI )
    {
      Init_CDI = TRUE;

183
184
185
186
187
#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

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

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

194
195
196
      value = cdiGetenvInt("CDI_HAVE_MISSVAL");
      if ( value >= 0 ) cdiHaveMissval = (int) value;

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

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

209
210
211
      envString = getenv("CDI_CHUNK_ALGO");
      if ( envString ) cdiSetChunk(envString);

212
213
      envString = getenv("SPLIT_LTYPE_105");
      if ( envString ) cdiSplitLtype105 = atoi(envString);
214
215
216

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

218
219
220
      envString = getenv("IGNORE_VALID_RANGE");
      if ( envString ) cdiIgnoreValidRange = atoi(envString);

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

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

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

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

      envString = getenv("PARTAB_PATH");
      if ( envString ) cdiPartabPath = strdup(envString);
267
268
269

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


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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
  return (name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286
287
}


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

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

298
  streamptr->self              = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
329
330
  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;
331
  streamptr->sortname          = cdiSortName;
332
  streamptr->have_missval      = cdiHaveMissval;
Deike Kleberg's avatar
Deike Kleberg committed
333
334
  streamptr->comptype          = COMPRESS_NONE;
  streamptr->complevel         = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
337

  basetimeInit(&streamptr->basetime);

338
339
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->xdimID[i]   = UNDEFID;
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->ydimID[i]   = UNDEFID;
340
  for ( i = 0; i < MAX_ZAXES_PS; i++ ) streamptr->zaxisID[i]  = UNDEFID;
341
342
343
  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
344
345
346
347

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

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


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

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

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

364
  return streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365
366
367
}


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

Deike Kleberg's avatar
Deike Kleberg committed
372
  xassert ( streamptr );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
374

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

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


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


390
391
392
393
394
int streamSize(void)
{
  return reshCountType ( &streamOps );
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
395

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


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

  cdiDefaultMissval = missval;
}


double cdiInqMissval(void)
{
  cdiInitialize();

433
  return (cdiDefaultMissval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
435
436
}


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
441
442
443
444
445
446
447
448
449
450
451
452
453
  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
454
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455

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

  return (streamptr->fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
460
}

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

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

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

474
      vlistID = streamptr->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
475
476
477

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

      if ( varID == nvars )
	{
	  int taxisID;

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

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

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

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

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


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

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

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


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

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

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


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

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

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


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


int  streamCompareP ( void * streamptr1, void * streamptr2 )
{
Deike Kleberg's avatar
Deike Kleberg committed
562
563
564
565
566
567
  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
568
569
  xassert ( s1 );
  xassert ( s2 );
Deike Kleberg's avatar
Deike Kleberg committed
570
571

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

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

Deike Kleberg's avatar
Deike Kleberg committed
587
  return equal;
588
589
590
591
592
}


void streamDestroyP ( void * streamptr )
{
593
594
595
  int id;
  stream_t * sp = ( stream_t * ) streamptr;

Deike Kleberg's avatar
Deike Kleberg committed
596
  xassert ( sp );
597
598
599

  id = sp->self;
  streamClose ( id );
600
601
602
}


603
void streamPrintP   ( void * streamptr, FILE * fp )
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
{
  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 );
626
  fprintf ( fp, "//svarinfo_t *vars;\n" );
627
628
629
630
631
632
  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 );
633
  fprintf ( fp, "//  tsteps_t   *tsteps;\n" );
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  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
652
653
  fprintf ( fp, "ztype         = %d\n", sp->comptype );
  fprintf ( fp, "zlevel        = %d\n", sp->complevel );
654
655
656
  fprintf ( fp, "curfile       = %d\n", sp->curfile );
  fprintf ( fp, "nfiles        = %d\n", sp->nfiles );
  fprintf ( fp, "//  char    **fnames;\n" );
657
  fprintf ( fp, "//  void    **gribContainers;\n" );
Deike Kleberg's avatar
Deike Kleberg committed
658
  fprintf ( fp, "vlistIDorig   = %d\n", sp->vlistIDorig );
659
}
660
661


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

667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
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));
    }

  if (namespaceHasLocalFile(namespaceGetActive())
      && streamptr->filemode == 'w' )
    {
      if ( streamptr->filetype == FILETYPE_NC  ||
           streamptr->filetype == FILETYPE_NC2 ||
           streamptr->filetype == FILETYPE_NC4 ||
           streamptr->filetype == FILETYPE_NC4C )
        {
#if USE_MPI && defined  (HAVE_LIBNETCDF) && ! defined (HAVE_PARALLEL_NC4)
          int rank, rankOpen;
          if (commInqIOMode() == PIO_NONE
              || ((rank = commInqRankColl())
695
                  == (rankOpen = cdiPioSerialOpenFileMap(streamptr->self))))
696
#endif
697
#ifdef HAVE_LIBNETCDF
698
            cdfDefVars(streamptr);
699
#endif
700
701
702
703
704
705
706
707
708
        }
      else if ( streamptr->filetype == FILETYPE_GRB  ||
                streamptr->filetype == FILETYPE_GRB2 )
        {
          gribContainersNew(streamptr);
        }
    }
}

Deike Kleberg's avatar
Deike Kleberg committed
709

710
static int
711
streamTxCode(void)
712
713
714
715
{
  return STREAM;
}

Deike Kleberg's avatar
Deike Kleberg committed
716

Deike Kleberg's avatar
Deike Kleberg committed
717
int streamNint = 11 ;
Deike Kleberg's avatar
Deike Kleberg committed
718
719


720
721
static int
streamGetPackSize(void * voidP, void *context)
722
{
Deike Kleberg's avatar
Deike Kleberg committed
723
  stream_t * streamP = ( stream_t * ) voidP;
724
725
726
727
728
729
  int packBufferSize
    = serializeGetSize(streamNint, DATATYPE_INT, context)
    + serializeGetSize(2, DATATYPE_FLT64, context)
    + serializeGetSize((int)strlen(streamP->filename) + 1,
                       DATATYPE_TXT, context)
    + serializeGetSize(1, DATATYPE_FLT64, context);
730
  return packBufferSize;
731
732
733
}


734
735
736
static void
streamPack(void * streamptr, void * packBuffer, int packBufferSize,
           int * packBufferPos, void *context)
737
{
738
  stream_t * streamP = ( stream_t * ) streamptr;
739
  int intBuffer[streamNint];
Deike Kleberg's avatar
Deike Kleberg committed
740
741
742
743
  double d;

  intBuffer[0]  = streamP->self;
  intBuffer[1]  = streamP->filetype;
744
  intBuffer[2]  = (int)strlen(streamP->filename) + 1;
Deike Kleberg's avatar
Deike Kleberg committed
745
746
747
748
  intBuffer[3]  = streamP->vlistID;
  intBuffer[4]  = streamP->vlistIDorig;
  intBuffer[5]  = streamP->byteorder;
  intBuffer[6]  = streamP->comptype;
749
  intBuffer[7]  = streamP->complevel;
Deike Kleberg's avatar
Deike Kleberg committed
750
751
752
  intBuffer[8]  = cdiDataUnreduced;
  intBuffer[9]  = cdiSortName;
  intBuffer[10] = cdiHaveMissval;
753
754
755
756
757
758
759
760
761

  serializePack(intBuffer, streamNint, DATATYPE_INT, packBuffer, packBufferSize, packBufferPos, context);
  d = xchecksum(DATATYPE_INT, streamNint, intBuffer);
  serializePack(&d, 1, DATATYPE_FLT64, packBuffer, packBufferSize, packBufferPos, context);

  serializePack(&cdiDefaultMissval, 1, DATATYPE_FLT64, packBuffer, packBufferSize, packBufferPos, context);
  serializePack(streamP->filename, intBuffer[2], DATATYPE_TXT, packBuffer, packBufferSize, packBufferPos, context);
  d = xchecksum(DATATYPE_TXT, intBuffer[2], &streamP->filename);
  serializePack(&d, 1, DATATYPE_FLT64, packBuffer, packBufferSize, packBufferPos, context);
762
}
763

764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
struct streamAssoc
streamUnpack(char * unpackBuffer, int unpackBufferSize,
             int * unpackBufferPos, int nspTarget, void *context)
{
  int intBuffer[streamNint], streamID;
  double d;
  char filename[CDI_MAX_NAME];

  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  intBuffer, streamNint, DATATYPE_INT, context);
  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  &d, 1, DATATYPE_FLT64, context);
  xassert(xchecksum(DATATYPE_INT, streamNint, intBuffer ) == d);

  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  &cdiDefaultMissval, 1, DATATYPE_FLT64, context);
  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  &filename, intBuffer[2], DATATYPE_TXT, context);
  serializeUnpack(unpackBuffer, unpackBufferSize, unpackBufferPos,
                  &d, 1, DATATYPE_FLT64, context);
  xassert(d == xchecksum(DATATYPE_TXT, intBuffer[2], filename));
  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;
}


799
800
801
802
803
804
805
806
807
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */