stream.c 53.4 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <ctype.h>

#include "dmemory.h"
#include "cdi.h"
#include "stream_int.h"
#include "cdf.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
#include "stream_grb.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
14
15
16
#include "stream_cdf.h"
#include "stream_srv.h"
#include "stream_ext.h"
#include "stream_ieg.h"
#include "file.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
17
#include "cgribex.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
#include "gribapi.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
20
21
22
23
#include "cdf.h"
#include "service.h"
#include "extra.h"
#include "ieg.h"
#include "vlist.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24
25
26
27
28
29
30
31
32
#include "resource_handle.h"
#include "pio_util.h"

#include "namespace.h"
#include "pio_interface.h"
#include "pio_rpc.h"
#include "pio_comm.h"

#include <string.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

34
35
#define  MAX_FNAMES  3

Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
38
extern resOps streamOps;


39
40
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
42
43

void cdiPrintDefaults(void)
{
44
45
46
47
  fprintf (stderr, "default instID     :  %d\n", cdiDefaultInstID);
  fprintf (stderr, "default modelID    :  %d\n", cdiDefaultModelID);
  fprintf (stderr, "default tableID    :  %d\n", cdiDefaultTableID);
  fprintf (stderr, "default missval    :  %g\n", cdiDefaultMissval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
50
51
52
53
54
}


void cdiDebug(int level)
{
  if ( level == 1 || level &  2 ) CDI_Debug = 1;

55
  if ( CDI_Debug ) Message("debug level %d", level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

  if ( level == 1 || level &  4 ) memDebug(1);

  if ( level == 1 || level &  8 ) fileDebug(1);

  if ( level == 1 || level & 16 )
    {
#if  defined  (HAVE_LIBGRIB)
      gribSetDebug(1);
#endif
#if  defined  (HAVE_LIBNETCDF)
      cdfDebug(1);
#endif
#if  defined  (HAVE_LIBSERVICE)
      srvDebug(1);
#endif
#if  defined  (HAVE_LIBEXTRA)
      extDebug(1);
#endif
#if  defined  (HAVE_LIBIEG)
      iegDebug(1);
#endif
    }

  if ( CDI_Debug )
    {
      cdiPrintDefaults();
      cdiPrintDatatypes();
    }
}


#undef  IsBigendian
#define IsBigendian()  ( u_byteorder.c[sizeof(long) - 1] )


92
93
static
int getByteorder(int byteswap)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
  int byteorder = -1;

  if ( IsBigendian() )
    {
      if ( byteswap ) byteorder = CDI_LITTLEENDIAN;
      else            byteorder = CDI_BIGENDIAN;
    }
  else
    {
      if ( byteswap ) byteorder = CDI_BIGENDIAN;
      else            byteorder = CDI_LITTLEENDIAN;
    }

  return (byteorder);
}


113
114
static
int getFiletype(const char *filename, int *byteorder)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
117
118
{
  int filetype = CDI_EUFTYPE;
  int fileID;
  int swap = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
  int version;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
  long recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
  char buffer[8];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
124
125
126

  fileID = fileOpen(filename, "r");

  if ( fileID == CDI_UNDEFID )
    {
127
      if ( memcmp(filename, "http:", 5) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
129
130
131
132
	return (FILETYPE_NC);
      else
	return (CDI_ESYSTEM);
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
  if ( fileRead(fileID, buffer, 8) != 8 ) return (CDI_EUFTYPE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136

  fileRewind(fileID);

137
  if ( memcmp(buffer, "GRIB", 4) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
140
141
142
      version = buffer[7];
      if ( version <= 1 )
	{
	  filetype = FILETYPE_GRB;
143
	  if ( CDI_Debug ) Message("found GRIB file = %s, version %d", filename, version);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147
      else if ( version == 2 )
	{
	  filetype = FILETYPE_GRB2;
148
	  if ( CDI_Debug ) Message("found GRIB2 file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
    }
151
  else if ( memcmp(buffer, "CDF\001", 4) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
    {
      filetype = FILETYPE_NC;
154
      if ( CDI_Debug ) Message("found CDF1 file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
    }
156
  else if ( memcmp(buffer, "CDF\002", 4) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
    {
      filetype = FILETYPE_NC2;
159
      if ( CDI_Debug ) Message("found CDF2 file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
    }
161
  else if ( memcmp(buffer+1, "HDF", 3) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
    {
163
      filetype = FILETYPE_NC4;
164
      if ( CDI_Debug ) Message("found HDF file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167
168
169
    }
#if  defined  (HAVE_LIBSERVICE)
  else if ( srvCheckFiletype(fileID, &swap) )
    {
      filetype = FILETYPE_SRV;
170
      if ( CDI_Debug ) Message("found SRV file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
172
173
174
175
176
    }
#endif
#if  defined  (HAVE_LIBEXTRA)
  else if ( extCheckFiletype(fileID, &swap) )
    {
      filetype = FILETYPE_EXT;
177
      if ( CDI_Debug ) Message("found EXT file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
180
181
182
183
    }
#endif
#if  defined  (HAVE_LIBIEG)
  else if ( iegCheckFiletype(fileID, &swap) )
    {
      filetype = FILETYPE_IEG;
184
      if ( CDI_Debug ) Message("found IEG file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
186
    }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
  else if ( gribCheckSeek(fileID, &recpos, &version) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
191
      if ( version <= 1 )
	{
	  filetype = FILETYPE_GRB;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
	  if ( CDI_Debug ) Message("found seeked GRIB file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194
      else if ( version == 2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
196
	{
	  filetype = FILETYPE_GRB2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
	  if ( CDI_Debug ) Message("found seeked GRIB2 file = %s", filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
200
201
202
203
204
205
206
207
208
    }

  fileClose(fileID);

  *byteorder = getByteorder(swap);

  return (filetype);
}


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
int _readline_(FILE *fp, char *line, int len)
{
  int ichar, ipos = 0;

  while ( (ichar = fgetc(fp)) != EOF )
    {
      if ( ichar == '\n' ) break;
      line[ipos++] = ichar;
      if ( ipos >= len )
        {
          fprintf(stderr, "readline Warning: end of line not found (maxlen = %d)!\n", len);
          break;
        }
    }
  line[ipos] = 0;

  if ( feof(fp) && ipos == 0 ) return (0);

  return (1);
}

#define  MAX_LINE  4096

int get_fnames(const char *argument, char *fnames[], int max_fnames)
{
  int num_fnames = 0;
  int len;
  int nfiles = 0;
  int i, j;
  const char *pch;
  char line[MAX_LINE];

  len = (int) strlen(argument);
  for ( i = 0; i < len; ++i )
    if ( argument[i] == ':' ) break;

  if ( i < len )
    {
      pch = &argument[i+1];
      len -= (i+1);
249
      if ( len && ( memcmp(argument, "filelist:", i) == 0 ||
250
		    memcmp(argument, "flist:", i) == 0 ) )
251
252
253
254
255
256
257
	{
	  for ( i = 0; i < len; ++i ) if ( pch[i] == ',' ) nfiles++;

	  if ( nfiles == 0 )
	    {
	      FILE *fp;
	      fp = fopen(pch, "r");
258
	      if ( fp == NULL ) Error("Open failed on %s", pch);
259
260

	      if ( CDI_Debug )
261
		Message("Reading file names from %s", pch);
262
263
264
265
266
267
268
269

	      rewind(fp);

	      nfiles = 0;
	      while ( _readline_(fp, line, MAX_LINE) )
		{
		  if ( line[0] == '#' || line[0] == '\0' ||
		       line[0] == ' ' ) continue;
270

271
272
		  if ( nfiles >= max_fnames )
		    {
273
		      Warning("Too many input files (limit: %d)", max_fnames);
274
275
276
277
278
		      break;
		    }
		  fnames[nfiles] = strdupx(line);
		  nfiles++;
		}
279

280
281
	      fclose(fp);

282
	      if ( nfiles == 0 ) Error("No input file found in %s", pch);
283
284
285
286
	    }
	  else
	    {
	      char xline[65536];
287

288
289
	      strcpy(xline, pch);
	      for ( i = 0; i < len; i++ ) if ( xline[i] == ',' ) xline[i] = 0;
290

291
292
293
	      nfiles++;
	      if ( nfiles >= max_fnames )
		{
294
		  Warning("Too many input files (limit: %d)", max_fnames);
295
296
297
298
299
300
301
302
303
304
305
		  nfiles = max_fnames;
		}

	      i = 0;
	      for ( j = 0; j < nfiles; j++ )
		{
		  fnames[j] = strdupx(&xline[i]);
		  i += strlen(&xline[i]) + 1;
		}
	    }
	}
306
      else if ( len && memcmp(argument, "ls:", i) == 0 )
307
308
309
	{
	  char command[4096];
	  FILE *pfp;
310

311
312
313
314
	  strcpy(command, "ls ");
	  strcat(command, pch);

	  pfp = popen(command, "r");
315
316
	  if ( pfp == NULL ) SysError("popen %s failed", command);

317
318
319
320
321
	  nfiles = 0;
	  while ( _readline_(pfp, line, MAX_LINE) )
	    {
	      if ( nfiles >= max_fnames )
		{
322
		  Warning("Too many input files (limit: %d)", max_fnames);
323
324
325
326
327
328
		  break;
		}
	      fnames[nfiles++] = strdupx(line);
	    }

	  pclose(pfp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
329
	  /*
330
331
	  for ( j = 0; j < nfiles; j++ )
	    fnames[j] = fnames[j];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
	  */
333
334
335
336
	}
    }

  num_fnames = nfiles;
337

338
339
340
  return (num_fnames);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
342
343
344
345
346
/*
@Function  streamInqFiletype
@Title     Get the filetype

@Prototype int streamInqFiletype(int streamID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349

@Description
350
The function @func{streamInqFiletype} returns the filetype of a stream.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
352

@Result
353
@func{streamInqFiletype} returns the type of the file format,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
one of the set of predefined CDI file format types.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
The valid CDI file format types are @func{FILETYPE_GRB}, @func{FILETYPE_GRB2}, @func{FILETYPE_NC}, @func{FILETYPE_NC2},
356
@func{FILETYPE_NC4}, @func{FILETYPE_NC4C}, @func{FILETYPE_SRV}, @func{FILETYPE_EXT} and @func{FILETYPE_IEG}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358
359
360
361

@EndFunction
*/
int streamInqFiletype(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
362
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
364

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365

366
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367

Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
  return (streamptr->filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
}


int getByteswap(int byteorder)
{
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
  int byteswap = 0;

  if ( IsBigendian() )
    {
      if ( byteorder == CDI_LITTLEENDIAN ) byteswap = TRUE;
    }
  else
    {
      if ( byteorder == CDI_BIGENDIAN ) byteswap = TRUE;
    }

  return (byteswap);
}

389
390
391
392
393
394
/*
@Function  streamDefByteorder
@Title     Define the byte order

@Prototype void streamDefByteorder(int streamID, int byteorder)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
396
397
398
399
400
401
402
403
404
    @Item  byteorder The byte order of a dataset, one of the CDI constants @func{CDI_BIGENDIAN} and
                     @func{CDI_LITTLEENDIAN}.

@Description
The function @func{streamDefByteorder} defines the byte order of a binary dataset
with the file format type @func{FILETYPE_SRV}, @func{FILETYPE_EXT} or @func{FILETYPE_IEG}.

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
406
void streamDefByteorder(int streamID, int byteorder)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409

Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411

412
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413

Uwe Schulzweida's avatar
Uwe Schulzweida committed
414
415
416
417
418
419
  if ( reshGetStatus ( streamID, &streamOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed.");
      return;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
421
  streamptr->byteorder = byteorder;
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
424
425
426
427

  switch (filetype)
    {
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
	srvrec_t *srvp = streamptr->record->srvp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
432
433
434
435
436
	srvp->byteswap = getByteswap(byteorder);

	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
437
	extrec_t *extp = streamptr->record->extp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
438
439
440
441
442
443
444
445
	extp->byteswap = getByteswap(byteorder);

	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
446
	iegrec_t *iegp = streamptr->record->iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
447
448
449
450
451
452
453
454
	iegp->byteswap = getByteswap(byteorder);

	break;
      }
#endif
    }
}

455
456
457
458
459
460
/*
@Function  streamInqByteorder
@Title     Get the byte order

@Prototype int streamInqByteorder(int streamID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}.
462
463
464
465
466
467
468
469
470
471
472

@Description
The function @func{streamInqByteorder} returns the byte order of a binary dataset
with the file format type @func{FILETYPE_SRV}, @func{FILETYPE_EXT} or @func{FILETYPE_IEG}.

@Result
@func{streamInqByteorder} returns the type of the byte order.
The valid CDI byte order types are @func{CDI_BIGENDIAN} and @func{CDI_LITTLEENDIAN}

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
474
int streamInqByteorder(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
475
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
476
477

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478

479
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480

Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
  return (streamptr->byteorder);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
483
484
485
486
}


char *streamFilesuffix(int filetype)
{
487
  static char *fileSuffix[] = {"", ".grb", ".g2", ".nc", ".nc2", ".nc4", ".nc4", ".srv", ".ext", ".ieg", ".h5"};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
  int size = (int) (sizeof(fileSuffix)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
490
491
492
493
494
495
496
497
498

  if ( filetype > 0 && filetype < size )
    return (fileSuffix[filetype]);
  else
    return (fileSuffix[0]);
}


char *streamFilename(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500

Uwe Schulzweida's avatar
Uwe Schulzweida committed
501
  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502

503
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
505

  return (streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
508
509
510
511
512
}


int cdiInqTimeSize(int streamID)
{
  int ntsteps;
  int tsID = 0, nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
513
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
516

517
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
518

Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
  ntsteps = streamptr->ntsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
521
522
523

  if ( ntsteps == CDI_UNDEFID )
    while ( (nrecs = streamInqTimestep(streamID, tsID++)) )

Uwe Schulzweida's avatar
Uwe Schulzweida committed
524
  ntsteps = streamptr->ntsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
526
527
528
529
530
531
532
533
534
535

  return (ntsteps);
}


int cdiInqContents(int streamID)
{
  int filetype;
  int vlistID;
  int taxisID;
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
536
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
538

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
539

540
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
541

Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
545
546
547

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
548
549
    case FILETYPE_GRB2:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
550
        status = grbInqContents(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        status = srvInqContents(streamID);
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        status = extInqContents(streamID);
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        status = iegInqContents(streamID);
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
578
    case FILETYPE_NC4:
579
    case FILETYPE_NC4C:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
580
581
582
583
584
585
586
587
      {
        status = cdfInqContents(streamID);
	break;
      }
#endif
    default:
      {
	if ( CDI_Debug )
588
	  Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
589
590
591
592
593
594
595
596
597
598

	status = CDI_ELIBNAVAIL;
      }
    }

  if ( status == 0 )
    {
      vlistID = streamInqVlist(streamID);
      taxisID = vlistInqTaxis(vlistID);
      if ( taxisID != -1 )
599
600
601
602
603
604
605
        {
          taxis_t *taxisptr1 = &streamptr->tsteps[0].taxis;
          taxis_t *taxisptr2 = taxisPtr(taxisID);
          ptaxisCopy(taxisptr2, taxisptr1);
          if ( taxisptr1->name     ) taxisptr2->name = taxisptr1->name;
          if ( taxisptr1->longname ) taxisptr2->longname = taxisptr1->longname;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
606
607
608
609
610
611
612
613
614
615
616
617
    }

  return (status);
}


int streamOpen(const char *filename, const char *filemode, int filetype)
{
  int fileID = CDI_UNDEFID;
  int streamID = CDI_ESYSTEM;
  int status;
  Record *record = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
  stream_t *streamptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
  int hasLocalFile = namespaceHasLocalFile ( namespaceGetActive ());
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
621

  if ( CDI_Debug )
622
    Message("Open %s mode %c file %s", strfiletype(filetype), (int) *filemode, filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
623
624
625

  if ( ! filename || ! filemode || filetype < 0 ) return (CDI_EINVAL);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
626
627
628
629
630
631
  xdebug("START, filename = %s, filemode = %s, filetype = %d, "
         "namespaceHasLocalFile(%d)=%s",
         filename, filemode, filetype, namespaceGetActive(),
	 namespaceHasLocalFile ( namespaceGetActive ()) ? "true":"false");

  if ( hasLocalFile )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
632
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633
634
      switch (filetype)
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
635
#if  defined  (HAVE_LIBGRIB)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
637
638
639
640
641
642
643
644
645
	case FILETYPE_GRB:
	case FILETYPE_GRB2:
	  {
	    fileID = gribOpen(filename, filemode);
            if ( fileID < 0 ) fileID = CDI_ESYSTEM;
            xdebug("%s OPENED, fileID=%d", filename, fileID);
	    record = (Record *) malloc(sizeof(Record));
	    record->buffer = NULL;
	    break;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
647
#endif
#if  defined  (HAVE_LIBSERVICE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
648
649
650
651
652
653
654
655
656
	case FILETYPE_SRV:
	  {
	    fileID = fileOpen(filename, filemode);
            if ( fileID < 0 ) fileID = CDI_ESYSTEM;
	    record = (Record *) malloc(sizeof(Record));
	    record->buffer = NULL;
	    record->srvp   = srvNew();
	    break;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
657
658
#endif
#if  defined  (HAVE_LIBEXTRA)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
659
660
661
662
663
664
665
666
667
	case FILETYPE_EXT:
	  {
	    fileID = fileOpen(filename, filemode);
            if ( fileID < 0 ) fileID = CDI_ESYSTEM;
	    record = (Record *) malloc(sizeof(Record));
	    record->buffer = NULL;
	    record->extp   = extNew();
	    break;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
668
669
#endif
#if  defined  (HAVE_LIBIEG)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
670
671
672
673
674
675
676
677
678
	case FILETYPE_IEG:
	  {
	    fileID = fileOpen(filename, filemode);
            if ( fileID < 0 ) fileID = CDI_ESYSTEM;
	    record = (Record *) malloc(sizeof(Record));
	    record->buffer = NULL;
	    record->iegp   = iegNew();
	    break;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
680
#endif
#if  defined  (HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
	case FILETYPE_NC:
	  {
	    fileID = cdfOpen(filename, filemode);
	    break;
	  }
	case FILETYPE_NC2:
	  {
	    fileID = cdfOpen64(filename, filemode);
	    break;
	  }
	case FILETYPE_NC4:
	case FILETYPE_NC4C:
	  {
	    fileID = cdf4Open(filename, filemode, &filetype);
	    break;
	  }
#endif
	default:
	  {
	    if ( CDI_Debug ) Message("%s support not compiled in!", strfiletype(filetype));
	    return (CDI_ELIBNAVAIL);
	  }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
704
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
#ifdef USE_MPI
  else if ( tolower ( * filemode ) == 'w' )
    {
      statusCode nspStatus = namespaceInqResStatus ();
      switch ( nspStatus )
        {
        case STAGE_DEFINITION:
          break;
        case STAGE_TIMELOOP:
          pioBufferFuncCall(STREAMOPEN, 2, filename, filetype);
          break;
        case STAGE_CLEANUP:
          xabort ( "TRANSITION TO IO PROCESSES ALREADY FINISHED." );
        break;
        default:
          xabort ( "INTERNAL ERROR" );
        }
    }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724

Uwe Schulzweida's avatar
Uwe Schulzweida committed
725
  if ( fileID < 0 && hasLocalFile )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
727
728
729
730
    {
      streamID = fileID;
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
731
      streamptr = stream_new_entry();
732
733
      streamID  = streamptr->self;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
734
      if ( streamID < 0 ) return (CDI_ELIMIT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
735

Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
737
738
739
740
      streamptr->record   = record;
      streamptr->filetype = filetype;
      streamptr->filemode = tolower(*filemode);
      streamptr->filename = strdupx(filename);
      streamptr->fileID   = fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
741

Uwe Schulzweida's avatar
Uwe Schulzweida committed
742
      if ( streamptr->filemode == 'r' )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
743
	{
744
	  vlist_t *vlistptr;
745
746
747
748
749
	  int vlistID;
	  vlistID = vlistCreate();
	  if ( vlistID < 0 ) return(CDI_ELIMIT);

	  streamptr->vlistID = vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
750
751
752
	  /* cdiReadByteorder(streamID); */
	  status = cdiInqContents(streamID);
	  if ( status < 0 ) return (status);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
753
	  vlistptr = vlist_to_pointer(streamptr->vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
755
756
	  vlistptr->ntsteps = streamNtsteps(streamID);
	}
    }
757

Uwe Schulzweida's avatar
Uwe Schulzweida committed
758
759
760
761
762
763
764
765
766
767
  return (streamID);
}


int streamOpenA(const char *filename, const char *filemode, int filetype)
{
  int fileID = CDI_UNDEFID;
  int streamID = CDI_ESYSTEM;
  int status;
  Record *record = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
768
  stream_t *streamptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
770

  if ( CDI_Debug )
771
    Message("Open %s mode %c file %s", strfiletype(filetype), (int) *filemode, filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
773
774
775
776
777
778

  if ( ! filename || ! filemode || filetype < 0 ) return (CDI_EINVAL);

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
779
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
      {
        fileID = gribOpen(filename, "r");
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	break;
      }
#endif
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        fileID = fileOpen(filename, "r");
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->srvp   = srvNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        fileID = fileOpen(filename, "r");
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->extp   = extNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        fileID = fileOpen(filename, "r");
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->iegp   = iegNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
      {
	fileID = cdfOpen(filename, "r");
	break;
      }
    case FILETYPE_NC2:
      {
	fileID = cdfOpen64(filename, "r");
	break;
      }
828
    case FILETYPE_NC4:
829
    case FILETYPE_NC4C:
830
      {
831
	fileID = cdf4Open(filename, "r", &filetype);
832
833
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834
835
836
#endif
    default:
      {
837
	if ( CDI_Debug ) Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
838
839
840
841
842
843
844
845
846
847
848
	return (CDI_ELIBNAVAIL);
      }
    }

  if ( fileID == CDI_UNDEFID || fileID == CDI_ELIBNAVAIL )
    {
      streamID = fileID;
      return (streamID);
    }
  else
    {
849
      vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
851
      streamptr = stream_new_entry();
      streamID = streamptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
852

Uwe Schulzweida's avatar
Uwe Schulzweida committed
853
854
855
856
857
      streamptr->record   = record;
      streamptr->filetype = filetype;
      streamptr->filemode = tolower(*filemode);
      streamptr->filename = strdupx(filename);
      streamptr->fileID   = fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858

Uwe Schulzweida's avatar
Uwe Schulzweida committed
859
      streamptr->vlistID = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
860
861
862
      /* cdiReadByteorder(streamID); */
      status = cdiInqContents(streamID);
      if ( status < 0 ) return (status);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
      vlistptr = vlist_to_pointer(streamptr->vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
864
865
      vlistptr->ntsteps = cdiInqTimeSize(streamID);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866

Uwe Schulzweida's avatar
Uwe Schulzweida committed
867
868
869
  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
870
871
872
873
874
875
    case FILETYPE_GRB:
    case FILETYPE_GRB2:
      {
	gribClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
#endif
#if  defined  (HAVE_LIBSERVICE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
878
879
880
881
882
    case FILETYPE_SRV:
      {
	fileClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
883
884
#endif
#if  defined  (HAVE_LIBEXTRA)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
885
886
887
888
889
    case FILETYPE_EXT:
      {
	fileClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
890
891
#endif
#if  defined  (HAVE_LIBIEG)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
892
893
894
895
896
    case FILETYPE_IEG:
      {
	fileClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
897
898
#endif
#if  defined  (HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
899
900
901
    case FILETYPE_NC:
    case FILETYPE_NC2:
    case FILETYPE_NC4:
902
    case FILETYPE_NC4C:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
903
904
905
906
      {
	cdfClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
907
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
908
909
    default:
      {
910
	if ( CDI_Debug ) Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
911
912
	return (CDI_ELIBNAVAIL);
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
914
915
916
917
918
    }

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
919
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
      {
        fileID = gribOpen(filename, filemode);
	break;
      }
#endif
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        fileID = fileOpen(filename, filemode);
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        fileID = fileOpen(filename, filemode);
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        fileID = fileOpen(filename, filemode);
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
      {
	fileID = cdfOpen(filename, filemode);
950
	streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
951
952
953
954
955
	break;
      }
    case FILETYPE_NC2:
      {
	fileID = cdfOpen64(filename, filemode);
956
	streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
957
958
	break;
      }
959
    case FILETYPE_NC4:
960
    case FILETYPE_NC4C:
961
      {
962
	fileID = cdf4Open(filename, filemode, &filetype);
963
	streamptr->ncmode = 2;
964
965
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
966
967
968
#endif
    default:
      {
969
	if ( CDI_Debug ) Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
970
971
972
973
974
975
976
	return (CDI_ELIBNAVAIL);
      }
    }

  if ( fileID == CDI_UNDEFID )
    streamID = CDI_UNDEFID;
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
977
    streamptr->fileID   = fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
978
979
980
981
982
983

  return (streamID);
}

/*
@Function  streamOpenRead
Uwe Schulzweida's avatar
Uwe Schulzweida committed
984
@Title     Open a dataset for reading
Uwe Schulzweida's avatar
Uwe Schulzweida committed
985

Uwe Schulzweida's avatar
Uwe Schulzweida committed
986
@Prototype int streamOpenRead(const char *path)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
987
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988
    @Item  path  The name of the dataset to be read.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
989
990

@Description
991
The function @func{streamOpenRead} opens an existing dataset for reading.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
992
993

@Result
994
Upon successful completion @func{streamOpenRead} returns an identifier to the
Uwe Schulzweida's avatar
Uwe Schulzweida committed
995
996
997
998
open stream. Otherwise, a negative number with the error status is returned.

@Errors
@List
Uwe Schulzweida's avatar
Uwe Schulzweida committed
999
1000
   @Item  CDI_ESYSTEM     Operating system error.
   @Item  CDI_EINVAL      Invalid argument.
For faster browsing, not all history is shown. View entire blame