cdi_int.c 18.7 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>
Uwe Schulzweida's avatar
Uwe Schulzweida 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"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
16
17
18
19
#include "pio_util.h"
#include "namespace.h"
#include "resource_handle.h"
#include "pio_rpc.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20

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

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

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

35
int cdiIgnoreAttCoordinates = FALSE;
36
int cdiIgnoreValidRange     = FALSE;
37
int cdiSkipRecords          = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
int cdiInventoryMode        = 1;
39

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
46
47
48
49
50
double cdiDefaultMissval = -9.E33;

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

#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID


int CDI_Debug   = 0;    /* If set to 1, debugging           */

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

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


Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
static int    streamCompareP ( void * streamptr1, void * streamptr2 );
static void   streamDestroyP ( void * streamptr );
static void   streamPrintP   ( void * streamptr, FILE * fp );
#ifdef USE_MPI
static int    streamGetPackSize ( void * streamptr, MPI_Comm comm );
static void   streamPack        ( void * streamptr, void * buff, int size,
				  int * position, MPI_Comm comm );
static int    streamTxCode      ( void );
#endif

resOps streamOps = { streamCompareP, streamDestroyP, streamPrintP,
#ifdef USE_MPI
                     streamGetPackSize,
		     streamPack,
                     streamTxCode
#endif
};

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

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

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

  return (envValue);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
static
void cdiSetChunk(const char *chunkAlgo)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
  //char *pch;
  //size_t len = strlen(chunkAlgo);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
  int algo = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137

138
139
  if      ( strcmp("auto",  chunkAlgo)   == 0 ) algo = CHUNK_AUTO;
  else if ( strcmp("grid",  chunkAlgo)   == 0 ) algo = CHUNK_GRID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
  else if ( strcmp("lines", chunkAlgo)   == 0 ) algo = CHUNK_LINES;
141
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
144
145
146
147
148
149
150
  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;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
          algo = CHUNK_USER;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
154
155
        }
      else
        Warning("Invalid environment variable CDI_CHUNK_ALGO: %s", chunkAlgo);
    }
156
  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
  else
    Warning("Invalid environment variable CDI_CHUNK_ALGO: %s", chunkAlgo);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
160
161

  if ( algo != -1 )
    {
162
      cdiChunkType = algo;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
      if ( CDI_Debug ) Message("set ChunkAlgo to %s", chunkAlgo);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
169
170
171
172
173
174
175
176
177

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

  if ( ! Init_CDI )
    {
      Init_CDI = TRUE;

178
#if  defined  (HAVE_LIBCGRIBEX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
      gribFixZSE(1);   // 1: Fix ZeroShiftError of simple packed spherical harmonics
      gribSetConst(1); // 1: Don't pack constant fields on regular grids
181
182
#endif

183
184
185
186
187
188
      value = cdiGetenvInt("CDI_REGULARGRID");
      if ( value >= 0 ) cdiDataUnreduced = (int) value;

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

189
190
191
      value = cdiGetenvInt("CDI_HAVE_MISSVAL");
      if ( value >= 0 ) cdiHaveMissval = (int) value;

192
193
194
195
196
      value = cdiGetenvInt("CDI_LEVELTYPE");
      if ( value >= 0 ) cdiDefaultLeveltype = (int) value;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206
      envString = getenv("CDI_CHUNK_ALGO");
      if ( envString ) cdiSetChunk(envString);

207
208
      envString = getenv("SPLIT_LTYPE_105");
      if ( envString ) cdiSplitLtype105 = atoi(envString);
209
210
211

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

213
214
215
      envString = getenv("IGNORE_VALID_RANGE");
      if ( envString ) cdiIgnoreValidRange = atoi(envString);

216
217
218
219
220
221
222
      envString = getenv("CDI_SKIP_RECORDS");
      if ( envString )
	{
	  cdiSkipRecords = atoi(envString);
	  cdiSkipRecords = cdiSkipRecords > 0 ? cdiSkipRecords : 0;
	}

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
258
259
260
261
      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
262
263
264

      envString = getenv("STREAM_DEBUG");
      if ( envString ) STREAM_Debug = atoi(envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
267
268
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
char *strfiletype(int filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
270
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
272
  char *name;
  int size = (int) (sizeof(Filetypes)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
  return (name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
281
282
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
stream_t *stream_to_pointer(int idx)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
  return ( stream_t *) reshGetVal ( idx, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
287
}

Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
288
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
void streamDefaultValue ( stream_t * streamptr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
291
292
{
  int i;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
  streamptr->self              = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
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
  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;
326
  streamptr->sortname          = cdiSortName;
327
  streamptr->have_missval      = cdiHaveMissval;
328
  streamptr->comptype          = COMPRESS_NONE;
329
  streamptr->complevel         = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330
331
332

  basetimeInit(&streamptr->basetime);

333
334
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->xdimID[i]   = UNDEFID;
  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->ydimID[i]   = UNDEFID;
335
  for ( i = 0; i < MAX_ZAXES_PS; i++ ) streamptr->zaxisID[i]  = UNDEFID;
336
337
338
  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
339
340
341
342

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
  streamptr->gribContainers    = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
  streamptr->vlistIDorig       = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
347
348
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
stream_t *stream_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
352

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
  return streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361
362
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
void stream_delete_entry(stream_t *streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
365
366
{
  int idx;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
  xassert ( streamptr );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368

Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
371
  idx = streamptr->self;
  free ( streamptr );
  reshRemove ( idx, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373

  if ( STREAM_Debug )
374
    Message("Removed idx %d from stream list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
376
377
}


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


int streamSize(void)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
  return reshCountType ( &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
389
390
}


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


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

  cdiDefaultMissval = missval;
}


double cdiInqMissval(void)
{
  cdiInitialize();

428
  return (cdiDefaultMissval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
}


432
void vlist_check_contents(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
{
  int index, nzaxis, zaxisID;

  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
449
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450

Uwe Schulzweida's avatar
Uwe Schulzweida committed
451
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
453

  return (streamptr->fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
462
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
464

  if ( streamptr->tsteps == NULL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
466
467
    {
      int varID, nvars;
      int vlistID;
468

Uwe Schulzweida's avatar
Uwe Schulzweida committed
469
      vlistID = streamptr->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
470
471
472

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

      if ( varID == nvars )
	{
	  int taxisID;

	  taxisID = vlistInqTaxis(vlistID);
	  if ( taxisID == CDI_UNDEFID )
	    {
482
	      taxisID = taxisCreate(TAXIS_ABSOLUTE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
484
	      vlistDefTaxis(vlistID, taxisID);
	    }
485

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
498
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499
500

  streamptr->dimgroupID = dimgroupID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
501
502
503
504
505
}


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
508
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
510

  return (streamptr->dimgroupID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
511
512
513
514
515
}


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

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

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


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
  streamptr = ( stream_t *) reshGetVal ( streamID, &streamOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
543

  return (streamptr->accesstype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
544
545
546
547
548
549
550
551
} 


int streamInqNvars ( int streamID )
{
  stream_t * streamptr;
  streamptr = ( stream_t * ) reshGetVal ( streamID, &streamOps );
  return ( streamptr->nvars );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620


int  streamCompareP ( void * streamptr1, void * streamptr2 )
{
  stream_t * s1 = ( stream_t * ) streamptr1;
  stream_t * s2 = ( stream_t * ) streamptr2;
  int differ = -1;
  int equal  = 0;
  int len;

  xassert ( s1 );
  xassert ( s2 );

  if ( s1->filetype  != s2->filetype  ) return differ;
  if (  namespaceAdaptKey2 ( s1->vlistIDorig ) != 			     
	namespaceAdaptKey2 ( s2->vlistIDorig )) return differ;
  if ( s1->byteorder != s2->byteorder ) return differ;
  if ( s1->comptype  != s2->comptype  ) return differ;
  if ( s1->complevel != s2->complevel ) return differ; 

  if ( s1->filename )
    {
      len = strlen ( s1->filename ) + 1;
      if ( memcmp ( s1->filename, s2->filename, len )) 
	return differ;
    }
  else if ( s2->filename ) 
    return differ;

  return equal;
}


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

  xassert ( sp );

  id = sp->self;
  streamClose ( id );
}


void streamPrintP   ( void * streamptr, FILE * fp )
{
  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 );
621
  fprintf ( fp, "//svarinfo_t *vars;\n" );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
622
623
624
625
626
627
  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 );
628
  fprintf ( fp, "//  tsteps_t   *tsteps;\n" );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
630
631
632
633
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 );
  fprintf ( fp, "ztype         = %d\n", sp->comptype );
  fprintf ( fp, "zlevel        = %d\n", sp->complevel );
  fprintf ( fp, "curfile       = %d\n", sp->curfile );
  fprintf ( fp, "nfiles        = %d\n", sp->nfiles );
  fprintf ( fp, "//  char    **fnames;\n" );
652
  fprintf ( fp, "//  void    **gribContainers;\n" );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
653
654
655
656
657
658
659
660
661
662
663
664
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
  fprintf ( fp, "vlistIDorig   = %d\n", sp->vlistIDorig );
}


void streamGetIndexList ( int nstreams, int * streamIndexList )
{
  reshGetResHListOfType ( nstreams, streamIndexList, &streamOps );
}


#ifdef USE_MPI
static int
streamTxCode ( void )
{
  return STREAM;
}


int streamNint = 11 ;


static
int streamGetPackSize ( void * voidP, MPI_Comm comm )
{
  stream_t * streamP = ( stream_t * ) voidP;
  int packBufferSize = 0, size;

  xmpi ( MPI_Pack_size ( streamNint, MPI_INT, comm, &size ));
  packBufferSize += size;
  xmpi ( MPI_Pack_size ( 1, MPI_DOUBLE, comm, &size ));
  packBufferSize += 2 * size;


  xmpi ( MPI_Pack_size (( int ) strlen ( streamP->filename ) + 1,
			MPI_CHAR, comm, &size ));
  packBufferSize += size;
  xmpi ( MPI_Pack_size ( 1, MPI_DOUBLE, comm, &size ));
  packBufferSize += size;

  return packBufferSize;
}


static
void streamPack ( void * streamptr, void * packBuffer, int packBufferSize,
                  int * packBufferPos, MPI_Comm comm )
{
  stream_t * streamP = ( stream_t * ) streamptr;
  int intBuffer[streamNint];  
  double d;

  intBuffer[0]  = streamP->self;
  intBuffer[1]  = streamP->filetype;
  intBuffer[2]  =  ( int ) strlen ( streamP->filename ) + 1;
  intBuffer[3]  = streamP->vlistID;
  intBuffer[4]  = streamP->vlistIDorig;
  intBuffer[5]  = streamP->byteorder;
  intBuffer[6]  = streamP->comptype;
  intBuffer[7]  = streamP->complevel; 
  intBuffer[8]  = cdiDataUnreduced;
  intBuffer[9]  = cdiSortName;
  intBuffer[10] = cdiHaveMissval;
  
  xmpi ( MPI_Pack ( intBuffer, streamNint, MPI_INT, 
		    packBuffer, packBufferSize, packBufferPos, comm ));
  d = xchecksum ( DATATYPE_INT , streamNint, intBuffer );
  xmpi ( MPI_Pack ( &d, 1, MPI_DOUBLE,
		    packBuffer, packBufferSize, packBufferPos, comm ));

  xmpi ( MPI_Pack ( &cdiDefaultMissval, 1, MPI_DOUBLE,
		    packBuffer, packBufferSize, packBufferPos, comm ));

  xmpi ( MPI_Pack ( streamP->filename, intBuffer[2], MPI_CHAR, 
		    packBuffer, packBufferSize, packBufferPos, comm )); 
  d = xchecksum  ( DATATYPE_TXT, intBuffer[2], &streamP->filename );
  xmpi ( MPI_Pack ( &d, 1, MPI_DOUBLE,
		    packBuffer, packBufferSize, packBufferPos, comm ));
}

#endif
733
734
735
736
737
738
739
740
741
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */