stream.c 46.5 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
24
#include "cdf.h"
#include "service.h"
#include "extra.h"
#include "ieg.h"
#include "vlist.h"

25
26
27
28
#define  MAX_FNAMES  3

FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

void cdiPrintDefaults(void)
{
  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); 
}


void cdiDebug(int level)
{
  static char func[] = "cdiDebug";

  if ( level == 1 || level &  2 ) CDI_Debug = 1;

  if ( CDI_Debug ) Message(func, "debug level %d", level);

  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] )


static int getByteorder(int byteswap)
{
  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);
}


static int getFiletype(const char *filename, int *byteorder)
{
  static char func[] = "getFiletype";
  int filetype = CDI_EUFTYPE;
  int fileID;
  int swap = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
  int version;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
  long recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
  char buffer[8];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
112
113
114
115

  fileID = fileOpen(filename, "r");

  if ( fileID == CDI_UNDEFID )
    {
116
      if ( memcmp(filename, "http:", 5) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
119
120
121
	return (FILETYPE_NC);
      else
	return (CDI_ESYSTEM);
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
  if ( fileRead(fileID, buffer, 8) != 8 ) return (CDI_EUFTYPE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125

  fileRewind(fileID);

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

  fileClose(fileID);

  *byteorder = getByteorder(swap);

  return (filetype);
}


198
199
200
201
202
203
204
205
206
207
208
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
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)
{
  static char func[] = "get_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);
239
240
      if ( len && ( memcmp(argument, "filelist:", i) == 0 || 
		    memcmp(argument, "flist:", i) == 0 ) )
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
	{
	  for ( i = 0; i < len; ++i ) if ( pch[i] == ',' ) nfiles++;

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

	      if ( CDI_Debug )
		Message(func, "Reading file names from %s", pch);

	      rewind(fp);

	      nfiles = 0;
	      while ( _readline_(fp, line, MAX_LINE) )
		{
		  if ( line[0] == '#' || line[0] == '\0' ||
		       line[0] == ' ' ) continue;
		  
		  if ( nfiles >= max_fnames )
		    {
		      Warning(func, "Too many input files (limit: %d)", max_fnames);
		      break;
		    }
		  fnames[nfiles] = strdupx(line);
		  nfiles++;
		}
	      
	      fclose(fp);

	      if ( nfiles == 0 ) Error(func, "No input file found in %s", pch);
	    }
	  else
	    {
	      char xline[65536];
	      	      
	      strcpy(xline, pch);
	      for ( i = 0; i < len; i++ ) if ( xline[i] == ',' ) xline[i] = 0;
	      
	      nfiles++;
	      if ( nfiles >= max_fnames )
		{
		  Warning(func, "Too many input files (limit: %d)", max_fnames);
		  nfiles = max_fnames;
		}

	      i = 0;
	      for ( j = 0; j < nfiles; j++ )
		{
		  fnames[j] = strdupx(&xline[i]);
		  i += strlen(&xline[i]) + 1;
		}
	    }
	}
296
      else if ( len && memcmp(argument, "ls:", i) == 0 )
297
298
299
300
301
302
303
304
	{
	  char command[4096];
	  FILE *pfp;
	  
	  strcpy(command, "ls ");
	  strcat(command, pch);

	  pfp = popen(command, "r");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
	  if ( pfp == NULL )
306
307
308
309
310
311
312
313
314
315
316
317
318
319
	    SysError(func, "popen %s failed", command);
	  
	  nfiles = 0;
	  while ( _readline_(pfp, line, MAX_LINE) )
	    {
	      if ( nfiles >= max_fnames )
		{
		  Warning(func, "Too many input files (limit: %d)", max_fnames);
		  break;
		}
	      fnames[nfiles++] = strdupx(line);
	    }

	  pclose(pfp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320
	  /*
321
322
	  for ( j = 0; j < nfiles; j++ )
	    fnames[j] = fnames[j];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
	  */
324
325
326
327
328
329
330
331
332
	}
    }

  num_fnames = nfiles;
  
  return (num_fnames);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
334
335
336
337
338
/*
@Function  streamInqFiletype
@Title     Get the filetype

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

@Description
342
The function @func{streamInqFiletype} returns the filetype of a stream.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344

@Result
345
@func{streamInqFiletype} returns the type of the file format,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
one of the set of predefined CDI file format types.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
The valid CDI file format types are @func{FILETYPE_GRB}, @func{FILETYPE_GRB2}, @func{FILETYPE_NC}, @func{FILETYPE_NC2},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
@func{FILETYPE_NC4}, @func{FILETYPE_SRV}, @func{FILETYPE_EXT} and @func{FILETYPE_IEG}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351
352
353
354

@EndFunction
*/
int streamInqFiletype(int streamID)
{
  static char func[] = "streamInqFiletype";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
357

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360

Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
  return (streamptr->filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
}


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);
}


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
@Function  streamDefByteorder
@Title     Define the byte order

@Prototype void streamDefByteorder(int streamID, int byteorder)
@Parameter
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}
    @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
399
400
401
402
void streamDefByteorder(int streamID, int byteorder)
{
  static char func[] = "streamDefByteorder";
  int filetype, fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
408
409
410
411
  stream_check_ptr(func, streamptr);

  streamptr->byteorder = byteorder;
  filetype = streamptr->filetype;
  fileID   = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412
413
414
415
416
417

  switch (filetype)
    {
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
	srvrec_t *srvp = streamptr->record->srvp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
421
422
423
424
425
426
	srvp->byteswap = getByteswap(byteorder);

	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
	extrec_t *extp = streamptr->record->extp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
429
430
431
432
433
434
435
	extp->byteswap = getByteswap(byteorder);

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

	break;
      }
#endif
    }
}


446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
@Function  streamInqByteorder
@Title     Get the byte order

@Prototype int streamInqByteorder(int streamID)
@Parameter
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}

@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
464
465
466
int streamInqByteorder(int streamID)
{
  static char func[] = "streamInqByteorder";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
469

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
471
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
472

Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
  return (streamptr->byteorder);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
475
476
477
478
}


char *streamFilesuffix(int filetype)
{
479
  static char *fileSuffix[] = {"", ".grb", ".g2", ".nc", ".nc2", ".nc4", ".srv", ".ext", ".ieg", ".h5"};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480
  int size = (int) (sizeof(fileSuffix)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
482
483
484
485
486
487
488
489
490
491

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


char *streamFilename(int streamID)
{
  static char func[] = "streamFilename";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
497
498
  stream_check_ptr(func, streamptr);

  return (streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499
500
501
502
503
504
505
506
}


int cdiInqTimeSize(int streamID)
{
  static char func[] = "cdiInqTimeSize";
  int ntsteps;
  int tsID = 0, nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
507
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
508
509

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
511
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512

Uwe Schulzweida's avatar
Uwe Schulzweida committed
513
  ntsteps = streamptr->ntsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
516
517

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
518
  ntsteps = streamptr->ntsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
520
521
522
523
524
525
526
527
528
529
530

  return (ntsteps);
}


int cdiInqContents(int streamID)
{
  static char func[] = "cdiInqContents";
  int filetype;
  int vlistID;
  int taxisID;
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
532
533

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
536

Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538
539
540
541
542

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
    case FILETYPE_GRB2:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
        status = grbInqContents(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
548
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#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:
573
    case FILETYPE_NC4:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
575
576
577
578
579
580
581
      {
        status = cdfInqContents(streamID);
	break;
      }
#endif
    default:
      {
	if ( CDI_Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
582
	  Message(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
583
584
585
586
587
588
589
590
591
592

	status = CDI_ELIBNAVAIL;
      }
    }

  if ( status == 0 )
    {
      vlistID = streamInqVlist(streamID);
      taxisID = vlistInqTaxis(vlistID);
      if ( taxisID != -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
593
	ptaxisCopy(taxisPtr(taxisID), &streamptr->tsteps[0].taxis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
595
596
597
598
599
600
601
602
603
604
605
606
    }

  return (status);
}


int streamOpen(const char *filename, const char *filemode, int filetype)
{
  static char func[] = "streamOpen";
  int fileID = CDI_UNDEFID;
  int streamID = CDI_ESYSTEM;
  int status;
  Record *record = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
  stream_t *streamptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
608
609
610
611
612
613
614
615
616
617

  if ( CDI_Debug )
    Message(func, "Open %s mode %c file %s", strfiletype(filetype), (int) *filemode, filename);

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

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
      {
        fileID = gribOpen(filename, filemode);
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	break;
      }
#endif
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        fileID = fileOpen(filename, filemode);
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->srvp   = srvNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        fileID = fileOpen(filename, filemode);
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->extp   = extNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        fileID = fileOpen(filename, filemode);
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->iegp   = iegNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
      {
	fileID = cdfOpen(filename, filemode);
	break;
      }
    case FILETYPE_NC2:
      {
	fileID = cdfOpen64(filename, filemode);
	break;
      }
667
668
669
670
671
672
    case FILETYPE_NC4:
      {
	fileID = cdf4Open(filename, filemode);
	break;
      }
 #endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
673
674
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
	if ( CDI_Debug ) Message(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
676
677
678
679
680
681
682
683
684
685
	return (CDI_ELIBNAVAIL);
      }
    }

  if ( fileID < 0 )
    {
      streamID = fileID;
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
686
687
      streamptr = stream_new_entry();
      streamID = streamptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
688

Uwe Schulzweida's avatar
Uwe Schulzweida committed
689
690
691
692
693
      streamptr->record   = record;
      streamptr->filetype = filetype;
      streamptr->filemode = tolower(*filemode);
      streamptr->filename = strdupx(filename);
      streamptr->fileID   = fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
694

Uwe Schulzweida's avatar
Uwe Schulzweida committed
695
      if ( streamptr->filemode == 'r' )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
696
	{
697
	  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
698
	  streamptr->vlistID = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
699
700
701
	  /* cdiReadByteorder(streamID); */
	  status = cdiInqContents(streamID);
	  if ( status < 0 ) return (status);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
702
	  vlistptr = vlist_to_pointer(streamptr->vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
	  vlistptr->ntsteps = streamNtsteps(streamID);
	}
    }
 
  return (streamID);
}


int streamOpenA(const char *filename, const char *filemode, int filetype)
{
  static char func[] = "streamOpenA";
  int fileID = CDI_UNDEFID;
  int streamID = CDI_ESYSTEM;
  int status;
  Record *record = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
  stream_t *streamptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
719
720
721
722
723
724
725
726
727
728

  if ( CDI_Debug )
    Message(func, "Open %s mode %c file %s", strfiletype(filetype), (int) *filemode, filename);

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

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
729
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
774
775
776
777
      {
        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;
      }
778
779
780
781
782
    case FILETYPE_NC4:
      {
	fileID = cdf4Open(filename, "r");
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
784
785
#endif
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
	if ( CDI_Debug ) Message(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
787
788
789
790
791
792
793
794
795
796
797
	return (CDI_ELIBNAVAIL);
      }
    }

  if ( fileID == CDI_UNDEFID || fileID == CDI_ELIBNAVAIL )
    {
      streamID = fileID;
      return (streamID);
    }
  else
    {
798
      vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
799
800
      streamptr = stream_new_entry();
      streamID = streamptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
801

Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
803
804
805
806
      streamptr->record   = record;
      streamptr->filetype = filetype;
      streamptr->filemode = tolower(*filemode);
      streamptr->filename = strdupx(filename);
      streamptr->fileID   = fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
807

Uwe Schulzweida's avatar
Uwe Schulzweida committed
808
      streamptr->vlistID = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
809
810
811
      /* cdiReadByteorder(streamID); */
      status = cdiInqContents(streamID);
      if ( status < 0 ) return (status);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
      vlistptr = vlist_to_pointer(streamptr->vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813
814
815
816
817
818
      vlistptr->ntsteps = cdiInqTimeSize(streamID);
    }
 
  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
819
820
821
822
823
824
    case FILETYPE_GRB:
    case FILETYPE_GRB2:
      {
	gribClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
826
#endif
#if  defined  (HAVE_LIBSERVICE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
827
828
829
830
831
    case FILETYPE_SRV:
      {
	fileClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
832
833
#endif
#if  defined  (HAVE_LIBEXTRA)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834
835
836
837
838
    case FILETYPE_EXT:
      {
	fileClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
839
840
#endif
#if  defined  (HAVE_LIBIEG)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
841
842
843
844
845
    case FILETYPE_IEG:
      {
	fileClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
846
847
#endif
#if  defined  (HAVE_LIBNETCDF)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
848
849
850
851
852
853
854
    case FILETYPE_NC:
    case FILETYPE_NC2:
    case FILETYPE_NC4:
      {
	cdfClose(fileID);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
855
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
856
857
858
859
860
    default:
      {
	if ( CDI_Debug ) Message(func, "%s support not compiled in!", strfiletype(filetype));
	return (CDI_ELIBNAVAIL);
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
862
863
864
865
866
    }

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
867
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
      {
        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);
898
	streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
899
900
901
902
903
	break;
      }
    case FILETYPE_NC2:
      {
	fileID = cdfOpen64(filename, filemode);
904
	streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
905
906
	break;
      }
907
908
909
    case FILETYPE_NC4:
      {
	fileID = cdf4Open(filename, filemode);
910
	streamptr->ncmode = 2;
911
912
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
914
915
#endif
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
916
	if ( CDI_Debug ) Message(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
917
918
919
920
921
922
923
	return (CDI_ELIBNAVAIL);
      }
    }

  if ( fileID == CDI_UNDEFID )
    streamID = CDI_UNDEFID;
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
924
    streamptr->fileID   = fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
925
926
927
928
929
930
931

  return (streamID);
}


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
934
@Prototype int streamOpenRead(const char *path)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
935
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
936
    @Item  path  The name of the dataset to be read
Uwe Schulzweida's avatar
Uwe Schulzweida committed
937
938

@Description
939
The function @func{streamOpenRead} opens an existing dataset for reading.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
941

@Result
942
Upon successful completion @func{streamOpenRead} returns an identifier to the
Uwe Schulzweida's avatar
Uwe Schulzweida committed
943
944
945
946
947
948
949
open stream. Otherwise, a negative number with the error status is returned.

@Errors
@List
   @Item  CDI_ESYSTEM     Operating system error
   @Item  CDI_EINVAL      Invalid argument
   @Item  CDI_EUFILETYPE  Unsupported file type
Uwe Schulzweida's avatar
Uwe Schulzweida committed
950
   @Item  CDI_ELIBNAVAIL  Library support not compiled in
Uwe Schulzweida's avatar
Uwe Schulzweida committed
951
952
953
@EndList

@Example
954
955
Here is an example using @func{streamOpenRead} to open an existing netCDF
file named @func{foo.nc} for reading:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
956
957

@Source
Uwe Schulzweida's avatar
Uwe Schulzweida committed
958
#include "cdi.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
960
961
962
963
964
965
966
967
   ...
int streamID;
   ...
streamID = streamOpenRead("foo.nc");
if ( streamID < 0 ) handle_error(streamID);
   ...
@EndSource
@EndFunction
*/
968
int streamOpenRead(const char *filenames)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
969
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
970
  static char func[] = "streamOpenRead";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
971
972
  int filetype, byteorder;
  int streamID;
973
974
975
  int num_fnames;
  char *fnames[MAX_FNAMES];
  const char *filename;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
976
  stream_t *streamptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
977

978
979
980
981
982
983
984
985
986
987
988
  num_fnames = get_fnames(filenames, fnames, MAX_FNAMES);

  if ( num_fnames == 0 )
    filename = filenames;
  else
    {
      int i; 
      for ( i = 0; i < num_fnames; ++i) printf("fnames: %d %s\n", i, fnames[i]);
      filename = fnames[0];
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
989
990
991
992
993
994
  filetype = getFiletype(filename, &byteorder);

  if ( filetype < 0 ) return (filetype);

  streamID = streamOpen(filename, "r", filetype);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
995
996
997
998
  if ( streamID >= 0 )
    {
      streamptr = stream_to_pointer(streamID);
      streamptr->byteorder = byteorder;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
999
1000
1001
1002
1003
1004
1005
1006
1007

      if ( num_fnames > 0 )
	{
	  int i;
	  streamptr->nfiles = num_fnames;
	  streamptr->fnames = (char **) malloc(num_fnames*sizeof(char *));
	  for ( i = 0; i < num_fnames; ++i )
	    streamptr->fnames[i] = fnames[i];
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1008
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1009
1010
1011
1012
1013
1014
1015
1016
1017

  return (streamID);
}


int streamOpenAppend(const char *filename)
{
  int filetype, byteorder;
  int streamID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1019
1020
1021
1022
1023
1024
1025

  filetype = getFiletype(filename, &byteorder);

  if ( filetype < 0 ) return (filetype);

  streamID = streamOpenA(filename, "a", filetype);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1026
1027
1028
  streamptr = stream_to_pointer(streamID);

  streamptr->byteorder = byteorder;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1029
1030
1031
1032
1033
1034
1035

  return (streamID);
}


/*
@Function  streamOpenWrite
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1036
@Title     Create a new dataset
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1037

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1038
@Prototype int streamOpenWrite(const char *path, int filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1039
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1040
    @Item  path      The name of the new dataset
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1041
    @Item  filetype  The type of the file format, one of the set of predefined CDI file format types.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1042
                     The valid CDI file format types are @func{FILETYPE_GRB}, @func{FILETYPE_GRB2}, @func{FILETYPE_NC},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1043
1044
                     @func{FILETYPE_NC2}, @func{FILETYPE_NC4}, @func{FILETYPE_SRV},
                     @func{FILETYPE_EXT} and @func{FILETYPE_IEG}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1045
1046

@Description
1047
The function @func{streamOpenWrite} creates a new datset.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1048
@Result
1049
Upon successful completion @func{streamOpenWrite} returns an identifier to the
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1050
1051
1052
1053
1054
1055
1056
open stream. Otherwise, a negative number with the error status is returned.

@Errors
@List
   @Item  CDI_ESYSTEM     Operating system error
   @Item  CDI_EINVAL      Invalid argument
   @Item  CDI_EUFILETYPE  Unsupported file type
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1057
   @Item  CDI_ELIBNAVAIL  Library support not compiled in
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1058
1059
1060
@EndList

@Example
1061
1062
Here is an example using @func{streamOpenWrite} to create a new netCDF file 
named @func{foo.nc} for writing:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1063
1064

@Source
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065
#include "cdi.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
   ...
int streamID;
   ...
streamID = streamOpenWrite("foo.nc", FILETYPE_NC);
if ( streamID < 0 ) handle_error(streamID);
   ...
@EndSource
@EndFunction
*/
int streamOpenWrite(const char *filename, int filetype)
{
  return (streamOpen(filename, "w", filetype));
}


/*
@Function  streamClose
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1083
@Title     Close an open dataset
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1084
1085
1086

@Prototype  void streamClose(int streamID)
@Parameter
1087
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1088
1089

@Description
1090
The function @func{streamClose} closes an open dataset.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100

@EndFunction
*/
void streamClose(int streamID)
{
  static char func[] = "streamClose";
  int filetype;
  int fileID;
  int index;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1101
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1102
1103

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1105
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1106
1107

  if ( CDI_Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1108
    Message(func, "fileID = %d filename = %s", streamID, streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1109

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1110
1111
  fileID   = streamptr->fileID;
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1112
1113
1114
  vlistID  = streamInqVlist(streamID);

  if ( fileID == CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115
    Warning(func, "File %s not open!", streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1116
1117
1118
1119
1120
  else
    switch (filetype)
      {
#if  defined  (HAVE_LIBGRIB)
      case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1121
      case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1122
1123
	{
	  gribClose(fileID);
1124
	  if ( streamptr->gribHandleNums )
1125
	    {
1126
1127
1128
1129
1130
1131
1132
	      int i;
	      for ( i = 0; i < streamptr->gribHandleNums; ++i )
		{
		  gribHandleDelete(streamptr->gribHandle[0]);
		  streamptr->gribHandle[i] = NULL;
		}
	      free(streamptr->gribHandle);
1133
	      streamptr->gribHandle = NULL;
1134
	      streamptr->gribHandleNums = 0;
1135
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1136
1137
1138
1139
1140
1141
1142
	  break;
	}
#endif
#if  defined  (HAVE_LIBSERVICE)
      case FILETYPE_SRV:
	{
	  fileClose(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1143
	  srvDelete(streamptr->record->srvp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1144
1145
1146
1147
1148
1149
1150
	  break;
	}
#endif
#if  defined  (HAVE_LIBEXTRA)
      case FILETYPE_EXT:
	{
	  fileClose(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1151
	  extDelete(streamptr->record->extp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1152
1153
1154
1155
1156
1157
1158
	  break;
	}
#endif
#if  defined  (HAVE_LIBIEG)
      case FILETYPE_IEG:
	{
	  fileClose(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1159
	  iegDelete(streamptr->record->iegp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1160
1161
1162
1163
1164
1165
	  break;
	}
#endif
#if  defined  (HAVE_LIBNETCDF)
      case FILETYPE_NC:
      case FILETYPE_NC2:
1166
      case FILETYPE_NC4:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1167
1168
1169
1170
1171
1172
1173
	{
	  cdfClose(fileID);
	  break;
	}
#endif
      default:
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1174
	  Error(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1175
1176
1177
1178
	  break;
	}
      }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1179
  if ( streamptr->record )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1180
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1181
1182
      if ( streamptr->record->buffer )
	free(streamptr->record->buffer);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1183

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1184
      free(streamptr->record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1185
1186
    }  

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1187
1188
  streamptr->filetype = 0;
  if ( streamptr->filename ) free(streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1189

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1190
  for ( index = 0; index < streamptr->nvars; index++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1191
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1192
1193
1194
1195
      if ( streamptr->vars[index].level )
	free(streamptr->vars[index].level);
      if ( streamptr->vars[index].lindex )
	free(streamptr->vars[index].lindex);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1196
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1197
  free(streamptr->vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1198

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1199
  for ( index = 0; index < streamptr->ntsteps; ++index )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1200
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1201
1202
1203
1204
      if ( streamptr->tsteps[index].records )
	free(streamptr->tsteps[index].records);
      if ( streamptr->tsteps[index].recIDs )
	free(streamptr->tsteps[index].recIDs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1205
1206
    }
    
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1207
  if ( streamptr->tsteps ) free(streamptr->tsteps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1208

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1209
1210
1211
1212
1213
1214
1215
1216
  if ( streamptr->nfiles > 0 )
    {
      for ( index = 0; index < streamptr->nfiles; ++index )
	free(streamptr->fnames[index]);

      free(streamptr->fnames);
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1217
1218
  if ( vlistID != -1 )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1219
1220
1221
1222
1223
1224
      if ( streamptr->filemode != 'w' )
	if ( vlistInqTaxis(vlistID) != -1 )
	  {
	    taxisDestroy(vlistInqTaxis(vlistID));
	  }

1225
      vlistDestroy(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1226
    }
Uwe Schulzweida's avatar
bug fix    
Uwe Schulzweida committed
1227
1228

  stream_delete_entry(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1229
1230
1231
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/*
@Function  streamSync
@Title     Synchronize an Open Dataset to Disk

@Prototype  void streamSync(int streamID)
@Parameter
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}

@Description
The function @func{streamSync} offers a way to synchronize the disk copy of a dataset with in-memory buffers.

@EndFunction
*/
void streamSync(int streamID)
{
  static char func[] = "streamSync";
  int filetype;
  int fileID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1251
  int nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1252
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1253
1254
1255
1256
1257
1258
1259
1260

  streamptr = stream_to_pointer(streamID);

  stream_check_ptr(func, streamptr);

  fileID   = streamptr->fileID;
  filetype = streamptr->filetype;
  vlistID  = streamInqVlist(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1261
  nvars    = vlistNvars(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1262
1263
1264

  if ( fileID == CDI_UNDEFID )
    Warning(func, "File %s not open!", streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1265
1266
1267
1268
  else if ( vlistID == CDI_UNDEFID )
    Warning(func, "Vlist undefined for file %s!", streamptr->filename);
  else if ( nvars == 0 )
    Warning(func, "No variables defined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
  else
    {
      if ( streamptr->filemode == 'w' || streamptr->filemode == 'a' )
	{
	  switch (filetype)
	    {
#if  defined  (HAVE_LIBNETCDF)
	    case FILETYPE_NC:
	    case FILETYPE_NC2:
	    case FILETYPE_NC4:
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1280
1281
		void cdf_sync(int ncid);
		if ( streamptr->ncmode == 2 ) cdf_sync(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1282
1283
1284
1285
1286
		break;
	      }
#endif
	    default:
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1287
		fileFlush(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1288
1289
1290
1291
1292
1293
1294
1295
		break;
	      }
	    }
	}
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1296
1297
/*
@Function  streamDefTimestep
1298
@Title     Define time step
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1299
1300
1301

@Prototype int streamDefTimestep(int streamID, int tsID)
@Parameter
1302
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1303
1304
1305
    @Item  tsID      Timestep identifier

@Description
1306
The function @func{streamDefTimestep} defines the time step of a stream.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1307
1308

@Result
1309
@func{streamDefTimestep} returns the number of records of the time step.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1310
1311
1312
1313
1314
1315
1316
1317
1318

@EndFunction
*/
int streamDefTimestep(int streamID, int tsID)
{
  static char func[] = "streamDefTimestep";
  int newtsID;
  int taxisID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1319
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1320
1321

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1322
1323
1324
1325

  if ( CDI_Debug )
    Message(func, "streamID = %d  tsID = %d", streamID, tsID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1326
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1327
1328
1329
1330
1331
1332

  vlistID = streamInqVlist(streamID);
  taxisID = vlistInqTaxis(vlistID);
  if ( taxisID == CDI_UNDEFID )
    {
      Warning(func, "taxisID undefined for fileID = %d! Using absolute time axis.", streamID);
1333
      taxisID = taxisCreate(TAXIS_ABSOLUTE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1334
1335
1336
1337
1338
1339
1340
1341
      vlistDefTaxis(vlistID, taxisID);
    }

  newtsID = tstepsNewEntry(streamID);

  if ( tsID != newtsID )
    Error(func, "Internal problem: tsID = %d newtsID = %d", tsID, newtsID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1342
  streamptr->curTsID = tsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1343

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1344
  ptaxisCopy(&streamptr->tsteps[tsID].taxis, taxisPtr(taxisID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1345

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1346
  streamptr->ntsteps = tsID + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1347

1348
1349
1350
  if ( (streamptr->filetype == FILETYPE_NC  ||
	streamptr->filetype == FILETYPE_NC2 ||
	streamptr->filetype == FILETYPE_NC4)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1351
1352
1353
1354
1355
       && vlistHasTime(vlistID) )
    cdfDefTimestep(streamID, tsID);

  cdiCreateRecords(streamID, tsID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1356
  return (streamptr->ntsteps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1357
1358
1359
1360
1361
}


/*
@Function  streamInqTimestep
1362
@Title     Get time step
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1363
1364
1365

@Prototype int streamInqTimestep(int streamID, int tsID)
@Parameter
1366
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1367
1368
1369
    @Item  tsID      Timestep identifier

@Description
1370
The function @func{streamInqTimestep} returns the time step of a stream.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1371
1372

@Result
1373
@func{streamInqTimestep} returns the number of records of the time step.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383

@EndFunction
*/
int streamInqTimestep(int streamID, int tsID)
{
  static char func[] = "streamInqTimestep";
  int filetype;
  int nrecs = 0;
  int taxisID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1384
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1385
1386

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1388
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1389
1390
1391

  vlistID = streamInqVlist(streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1392
  if ( tsID < streamptr->rtsteps )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1393
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1394
1395
1396
      streamptr->curTsID = tsID;
      nrecs = streamptr->tsteps[tsID].nrecs;
      streamptr->tsteps[tsID].curRecID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1397
1398
1399
      taxisID = vlistInqTaxis(vlistID);
      if ( taxisID == -1 )
	Error(func, "Timestep undefined for fileID = %d", streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1400
      ptaxisCopy(taxisPtr(taxisID), &streamptr->tsteps[tsID].taxis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1401
1402
1403
1404

      return (nrecs);
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1405
  if ( tsID >= streamptr->ntsteps && streamptr->ntsteps != CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1406
1407
1408
1409
    {
      return (0);
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1410
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1411
1412
1413
1414
1415
1416
1417
1418

  if ( CDI_Debug )
    Message(func, "streamID = %d  tsID = %d  filetype = %d", streamID, tsID, filetype);

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1419
1420
    case FILETYPE_GRB2:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1421
        nrecs = grbInqTimestep(streamID, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1422
1423
1424
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        nrecs = srvInqTimestep(streamID, tsID);
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        nrecs = extInqTimestep(streamID, tsID);
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        nrecs = iegInqTimestep(streamID, tsID);
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
1449
    case FILETYPE_NC4:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1450
1451
1452
1453
1454
1455
1456
      {
        nrecs = cdfInqTimestep(streamID, tsID);
	break;
      }
#endif
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1457
	Error(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1458
1459
1460
1461
1462
1463
1464
1465
	break;
      }
    }

  taxisID = vlistInqTaxis(vlistID);
  if ( taxisID == -1 )
    Error(func, "Timestep undefined for fileID = %d", streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1466
  ptaxisCopy(taxisPtr(taxisID), &streamptr->tsteps[tsID].taxis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477

  return (nrecs);
}


/*
@Function  streamReadVar
@Title     Read a variable

@Prototype void streamReadVar(int streamID, int varID, double *data, int *nmiss)
@Parameter
1478
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1479
    @Item  varID     Variable identifier
1480
    @Item  data      Pointer to the location into which the data value is read
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1481
1482
    @Item  nmiss     Number of missing values

1483
1484
1485
@Description
The function streamReadVar reads all the values of one time step of a variable
from an open dataset.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1486
1487
1488
1489
1490
1491
@EndFunction
*/
void streamReadVar(int streamID, int varID, double *data, int *nmiss)
{
  static char func[] = "streamReadVar";
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1492
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1493
1494

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1495
1496
1497
1498

  if ( CDI_Debug )
    Message(func, "streamID = %d  varID = %d", streamID, varID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1499
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1500

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1501
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1502
1503
1504
1505
1506
1507
1508

  *nmiss = 0;

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1509
1510
    case FILETYPE_GRB2:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1511
        grbReadVarDP(streamID, varID, data, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1512
1513
1514
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV: