cdi_int.c 20 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 "resource_handle.h"
23
#include "pio_rpc.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30
31
32
33
int cdiDefaultCalendar = CALENDAR_PROLEPTIC;

int cdiDefaultInstID   = CDI_UNDEFID;
int cdiDefaultModelID  = CDI_UNDEFID;
int cdiDefaultTableID  = CDI_UNDEFID;
34
//int cdiNcMissingValue  = CDI_UNDEFID;
35
int cdiNcChunksizehint = CDI_UNDEFID;
36
int cdiChunkType       = CHUNK_GRID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
int cdiSplitLtype105   = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38

39
int cdiIgnoreAttCoordinates = FALSE;
40
int cdiIgnoreValidRange     = FALSE;
41
int cdiSkipRecords          = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
int cdiInventoryMode        = 1;
43

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
49
50
51
52
53
54
double cdiDefaultMissval = -9.E33;

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

#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID


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

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

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


77
78
static int    streamCompareP ( void * streamptr1, void * streamptr2 );
static void   streamDestroyP ( void * streamptr );
79
static void   streamPrintP   ( void * streamptr, FILE * fp );
80
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
81
82
83
84
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 );
85
#endif
86

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

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

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

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

  return (envValue);
}

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

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

  if ( ! Init_CDI )
    {
      Init_CDI = TRUE;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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


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

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

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

  basetimeInit(&streamptr->basetime);

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

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

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


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

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

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

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


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

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

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

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


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


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
394

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


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

  cdiDefaultMissval = missval;
}


double cdiInqMissval(void)
{
  cdiInitialize();

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


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

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

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

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

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

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

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

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

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

      if ( varID == nvars )
	{
	  int taxisID;

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

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

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

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

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


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

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

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


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

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

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


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

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

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


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


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

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

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

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


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

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

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


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


661
void streamGetIndexList ( int nstreams, int * streamIndexList )
Deike Kleberg's avatar
Deike Kleberg committed
662
{
663
  reshGetResHListOfType ( nstreams, streamIndexList, &streamOps );
Deike Kleberg's avatar
Deike Kleberg committed
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
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())
694
                  == (rankOpen = cdiPioSerialOpenFileMap(streamptr->self))))
695
696
697
698
699
700
701
702
703
704
705
#endif
            cdfDefVars(streamptr);
        }
      else if ( streamptr->filetype == FILETYPE_GRB  ||
                streamptr->filetype == FILETYPE_GRB2 )
        {
          gribContainersNew(streamptr);
        }
    }
}

Deike Kleberg's avatar
Deike Kleberg committed
706

707
#ifdef USE_MPI
708
static int
709
streamTxCode ( void )
710
711
712
713
{
  return STREAM;
}

Deike Kleberg's avatar
Deike Kleberg committed
714

Deike Kleberg's avatar
Deike Kleberg committed
715
int streamNint = 11 ;
Deike Kleberg's avatar
Deike Kleberg committed
716
717


718
static
Deike Kleberg's avatar
Deike Kleberg committed
719
int streamGetPackSize ( void * voidP, MPI_Comm comm )
720
{
Deike Kleberg's avatar
Deike Kleberg committed
721
  stream_t * streamP = ( stream_t * ) voidP;
722
723
  int packBufferSize = 0, size;

Deike Kleberg's avatar
Deike Kleberg committed
724
  xmpi ( MPI_Pack_size ( streamNint, MPI_INT, comm, &size ));
725
  packBufferSize += size;
Deike Kleberg's avatar
Deike Kleberg committed
726
727
  xmpi ( MPI_Pack_size ( 1, MPI_DOUBLE, comm, &size ));
  packBufferSize += 2 * size;
728
729


Deike Kleberg's avatar
Deike Kleberg committed
730
731
  xmpi ( MPI_Pack_size (( int ) strlen ( streamP->filename ) + 1,
			MPI_CHAR, comm, &size ));
732
  packBufferSize += size;
Deike Kleberg's avatar
Deike Kleberg committed
733
  xmpi ( MPI_Pack_size ( 1, MPI_DOUBLE, comm, &size ));
734
735
736
  packBufferSize += size;

  return packBufferSize;
737
738
739
}


740
static
Deike Kleberg's avatar
Deike Kleberg committed
741
void streamPack ( void * streamptr, void * packBuffer, int packBufferSize,
742
                  int * packBufferPos, MPI_Comm comm )
743
{
744
  stream_t * streamP = ( stream_t * ) streamptr;
Deike Kleberg's avatar
Deike Kleberg committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  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 ));
774
}
775
776

#endif
777
778
779
780
781
782
783
784
785
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */