stream.c 49.3 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

void cdiPrintDefaults(void)
{
32
33
34
35
  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
36
37
38
39
40
41
42
}


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

43
  if ( CDI_Debug ) Message("debug level %d", level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

  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)
{
  int filetype = CDI_EUFTYPE;
  int fileID;
  int swap = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
  int version;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
  long recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
  char buffer[8];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
109
110
111
112

  fileID = fileOpen(filename, "r");

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
  if ( fileRead(fileID, buffer, 8) != 8 ) return (CDI_EUFTYPE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
121
122

  fileRewind(fileID);

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

  fileClose(fileID);

  *byteorder = getByteorder(swap);

  return (filetype);
}


195
196
197
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
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);
235
      if ( len && ( memcmp(argument, "filelist:", i) == 0 ||
236
		    memcmp(argument, "flist:", i) == 0 ) )
237
238
239
240
241
242
243
	{
	  for ( i = 0; i < len; ++i ) if ( pch[i] == ',' ) nfiles++;

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

	      if ( CDI_Debug )
247
		Message("Reading file names from %s", pch);
248
249
250
251
252
253
254
255

	      rewind(fp);

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

257
258
		  if ( nfiles >= max_fnames )
		    {
259
		      Warning("Too many input files (limit: %d)", max_fnames);
260
261
262
263
264
		      break;
		    }
		  fnames[nfiles] = strdupx(line);
		  nfiles++;
		}
265

266
267
	      fclose(fp);

268
	      if ( nfiles == 0 ) Error("No input file found in %s", pch);
269
270
271
272
	    }
	  else
	    {
	      char xline[65536];
273

274
275
	      strcpy(xline, pch);
	      for ( i = 0; i < len; i++ ) if ( xline[i] == ',' ) xline[i] = 0;
276

277
278
279
	      nfiles++;
	      if ( nfiles >= max_fnames )
		{
280
		  Warning("Too many input files (limit: %d)", max_fnames);
281
282
283
284
285
286
287
288
289
290
291
		  nfiles = max_fnames;
		}

	      i = 0;
	      for ( j = 0; j < nfiles; j++ )
		{
		  fnames[j] = strdupx(&xline[i]);
		  i += strlen(&xline[i]) + 1;
		}
	    }
	}
292
      else if ( len && memcmp(argument, "ls:", i) == 0 )
293
294
295
	{
	  char command[4096];
	  FILE *pfp;
296

297
298
299
300
	  strcpy(command, "ls ");
	  strcat(command, pch);

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

303
304
305
306
307
	  nfiles = 0;
	  while ( _readline_(pfp, line, MAX_LINE) )
	    {
	      if ( nfiles >= max_fnames )
		{
308
		  Warning("Too many input files (limit: %d)", max_fnames);
309
310
311
312
313
314
		  break;
		}
	      fnames[nfiles++] = strdupx(line);
	    }

	  pclose(pfp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
	  /*
316
317
	  for ( j = 0; j < nfiles; j++ )
	    fnames[j] = fnames[j];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
	  */
319
320
321
322
	}
    }

  num_fnames = nfiles;
323

324
325
326
  return (num_fnames);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
327
328
329
330
331
332
/*
@Function  streamInqFiletype
@Title     Get the filetype

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

@Description
336
The function @func{streamInqFiletype} returns the filetype of a stream.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
338

@Result
339
@func{streamInqFiletype} returns the type of the file format,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
one of the set of predefined CDI file format types.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
The valid CDI file format types are @func{FILETYPE_GRB}, @func{FILETYPE_GRB2}, @func{FILETYPE_NC}, @func{FILETYPE_NC2},
342
@func{FILETYPE_NC4}, @func{FILETYPE_NC4C}, @func{FILETYPE_SRV}, @func{FILETYPE_EXT} and @func{FILETYPE_IEG}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344
345
346
347

@EndFunction
*/
int streamInqFiletype(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350

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

352
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353

Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
  return (streamptr->filetype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
}


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

375
376
377
378
379
380
/*
@Function  streamDefByteorder
@Title     Define the byte order

@Prototype void streamDefByteorder(int streamID, int byteorder)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
382
383
384
385
386
387
388
389
390
    @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
391
392
393
void streamDefByteorder(int streamID, int byteorder)
{
  int filetype, fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395

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

398
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
400
401
402

  streamptr->byteorder = byteorder;
  filetype = streamptr->filetype;
  fileID   = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
405
406
407
408

  switch (filetype)
    {
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
	srvrec_t *srvp = streamptr->record->srvp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
411
412
413
414
415
416
417
	srvp->byteswap = getByteswap(byteorder);

	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
	extrec_t *extp = streamptr->record->extp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
421
422
423
424
425
426
	extp->byteswap = getByteswap(byteorder);

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

	break;
      }
#endif
    }
}

436
437
438
439
440
441
/*
@Function  streamInqByteorder
@Title     Get the byte order

@Prototype int streamInqByteorder(int streamID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead} or @fref{streamOpenWrite}.
443
444
445
446
447
448
449
450
451
452
453

@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
454
455
int streamInqByteorder(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
458

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

460
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461

Uwe Schulzweida's avatar
Uwe Schulzweida committed
462
  return (streamptr->byteorder);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
464
465
466
467
}


char *streamFilesuffix(int filetype)
{
468
  static char *fileSuffix[] = {"", ".grb", ".g2", ".nc", ".nc2", ".nc4", ".nc4", ".srv", ".ext", ".ieg", ".h5"};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
469
  int size = (int) (sizeof(fileSuffix)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
470
471
472
473
474
475
476
477
478
479

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


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

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

484
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486

  return (streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488
489
490
491
492
493
}


int cdiInqTimeSize(int streamID)
{
  int ntsteps;
  int tsID = 0, nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
496

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

498
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499

Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
  ntsteps = streamptr->ntsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
501
502
503
504

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
505
  ntsteps = streamptr->ntsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
508
509
510
511
512
513
514
515
516

  return (ntsteps);
}


int cdiInqContents(int streamID)
{
  int filetype;
  int vlistID;
  int taxisID;
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
518
519

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

521
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
522

Uwe Schulzweida's avatar
Uwe Schulzweida committed
523
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
524
525
526
527
528

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
530
    case FILETYPE_GRB2:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
        status = grbInqContents(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
532
533
534
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#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:
559
    case FILETYPE_NC4:
560
    case FILETYPE_NC4C:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
561
562
563
564
565
566
567
568
      {
        status = cdfInqContents(streamID);
	break;
      }
#endif
    default:
      {
	if ( CDI_Debug )
569
	  Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
570
571
572
573
574
575
576
577
578
579

	status = CDI_ELIBNAVAIL;
      }
    }

  if ( status == 0 )
    {
      vlistID = streamInqVlist(streamID);
      taxisID = vlistInqTaxis(vlistID);
      if ( taxisID != -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
580
	ptaxisCopy(taxisPtr(taxisID), &streamptr->tsteps[0].taxis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
582
583
584
585
586
587
588
589
590
591
592
    }

  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
593
  stream_t *streamptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
595

  if ( CDI_Debug )
596
    Message("Open %s mode %c file %s", strfiletype(filetype), (int) *filemode, filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
597
598
599
600
601
602
603

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

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
604
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
605
606
      {
        fileID = gribOpen(filename, filemode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
        if ( fileID < 0 ) fileID = CDI_ESYSTEM;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
608
609
610
611
612
613
614
615
616
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	break;
      }
#endif
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        fileID = fileOpen(filename, filemode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
617
        if ( fileID < 0 ) fileID = CDI_ESYSTEM;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
619
620
621
622
623
624
625
626
627
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->srvp   = srvNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        fileID = fileOpen(filename, filemode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
628
        if ( fileID < 0 ) fileID = CDI_ESYSTEM;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
630
631
632
633
634
635
636
637
638
	record = (Record *) malloc(sizeof(Record));
	record->buffer = NULL;
	record->extp   = extNew();
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        fileID = fileOpen(filename, filemode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
639
        if ( fileID < 0 ) fileID = CDI_ESYSTEM;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
	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;
      }
657
    case FILETYPE_NC4:
658
    case FILETYPE_NC4C:
659
      {
660
	fileID = cdf4Open(filename, filemode, &filetype);
661
662
663
	break;
      }
 #endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
664
665
    default:
      {
666
	if ( CDI_Debug ) Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
667
668
669
670
671
672
673
674
675
676
	return (CDI_ELIBNAVAIL);
      }
    }

  if ( fileID < 0 )
    {
      streamID = fileID;
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
677
      streamptr = stream_new_entry();
678
679
      streamID  = streamptr->self;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
680
      if ( streamID < 0 ) return (CDI_ELIMIT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
681

Uwe Schulzweida's avatar
Uwe Schulzweida committed
682
683
684
685
686
      streamptr->record   = record;
      streamptr->filetype = filetype;
      streamptr->filemode = tolower(*filemode);
      streamptr->filename = strdupx(filename);
      streamptr->fileID   = fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
687

Uwe Schulzweida's avatar
Uwe Schulzweida committed
688
      if ( streamptr->filemode == 'r' )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
689
	{
690
	  vlist_t *vlistptr;
691
692
693
694
695
	  int vlistID;
	  vlistID = vlistCreate();
	  if ( vlistID < 0 ) return(CDI_ELIMIT);

	  streamptr->vlistID = vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
696
697
698
	  /* cdiReadByteorder(streamID); */
	  status = cdiInqContents(streamID);
	  if ( status < 0 ) return (status);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
699
	  vlistptr = vlist_to_pointer(streamptr->vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
700
701
702
	  vlistptr->ntsteps = streamNtsteps(streamID);
	}
    }
703

Uwe Schulzweida's avatar
Uwe Schulzweida committed
704
705
706
707
708
709
710
711
712
713
  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
714
  stream_t *streamptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
715
716

  if ( CDI_Debug )
717
    Message("Open %s mode %c file %s", strfiletype(filetype), (int) *filemode, filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
719
720
721
722
723
724

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

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
725
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
727
728
729
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
      {
        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;
      }
774
    case FILETYPE_NC4:
775
    case FILETYPE_NC4C:
776
      {
777
	fileID = cdf4Open(filename, "r", &filetype);
778
779
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
781
782
#endif
    default:
      {
783
	if ( CDI_Debug ) Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
784
785
786
787
788
789
790
791
792
793
794
	return (CDI_ELIBNAVAIL);
      }
    }

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

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

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

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
865
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
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
      {
        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);
896
	streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
897
898
899
900
901
	break;
      }
    case FILETYPE_NC2:
      {
	fileID = cdfOpen64(filename, filemode);
902
	streamptr->ncmode = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
903
904
	break;
      }
905
    case FILETYPE_NC4:
906
    case FILETYPE_NC4C:
907
      {
908
	fileID = cdf4Open(filename, filemode, &filetype);
909
	streamptr->ncmode = 2;
910
911
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
912
913
914
#endif
    default:
      {
915
	if ( CDI_Debug ) Message("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
916
917
918
919
920
921
922
	return (CDI_ELIBNAVAIL);
      }
    }

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

  return (streamID);
}

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

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

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

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

@Errors
@List
Uwe Schulzweida's avatar
Uwe Schulzweida committed
945
946
947
948
   @Item  CDI_ESYSTEM     Operating system error.
   @Item  CDI_EINVAL      Invalid argument.
   @Item  CDI_EUFILETYPE  Unsupported file type.
   @Item  CDI_ELIBNAVAIL  Library support not compiled in.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
949
950
951
@EndList

@Example
952
953
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
954
955

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

975
976
  cdiInitialize();

977
  //num_fnames = get_fnames(filenames, fnames, MAX_FNAMES);
978
979
980
981
982
983

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

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

  if ( filetype < 0 ) return (filetype);

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

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

      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
1007
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1008
1009
1010
1011
1012
1013
1014
1015
1016

  return (streamID);
}


int streamOpenAppend(const char *filename)
{
  int filetype, byteorder;
  int streamID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1017
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018

1019
1020
  cdiInitialize();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1021
1022
1023
1024
1025
1026
  filetype = getFiletype(filename, &byteorder);

  if ( filetype < 0 ) return (filetype);

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

1027
1028
1029
1030
1031
  if ( streamID >= 0 )
    {
      streamptr = stream_to_pointer(streamID);
      streamptr->byteorder = byteorder;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1032
1033
1034
1035
1036
1037

  return (streamID);
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1040
@Prototype int streamOpenWrite(const char *path, int filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1041
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1042
    @Item  path      The name of the new dataset.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1043
    @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
1044
                     The valid CDI file format types are @func{FILETYPE_GRB}, @func{FILETYPE_GRB2}, @func{FILETYPE_NC},
1045
                     @func{FILETYPE_NC2}, @func{FILETYPE_NC4}, @func{FILETYPE_NC4C}, @func{FILETYPE_SRV},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1046
                     @func{FILETYPE_EXT} and @func{FILETYPE_IEG}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1047
1048

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

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

@Example
1063
1064
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
1065
1066

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1081
1082
1083
1084
1085
  return (streamOpen(filename, "w", filetype));
}

/*
@Function  streamClose
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1086
@Title     Close an open dataset
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1087
1088
1089

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

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

@EndFunction
*/
void streamClose(int streamID)
{
  int filetype;
  int fileID;
  int index;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1103
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1104
1105

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

1107
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1108
1109

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1112
1113
  fileID   = streamptr->fileID;
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
1114
  vlistID  = streamptr->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115
1116

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1171
  if ( streamptr->record )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1172
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1173
1174
      if ( streamptr->record->buffer )
	free(streamptr->record->buffer);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1175

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1176
      free(streamptr->record);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1177
1178
    }  

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1179
1180
  streamptr->filetype = 0;
  if ( streamptr->filename ) free(streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1181

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1182
  for ( index = 0; index < streamptr->nvars; index++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1183
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1184
1185
1186
1187
      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
1188
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1189
  free(streamptr->vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1190

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1191
  for ( index = 0; index < streamptr->ntsteps; ++index )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1192
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1193
1194
1195
1196
      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
1197
1198
    }
    
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1199
  if ( streamptr->tsteps ) free(streamptr->tsteps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1200

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1201
1202
1203
1204
1205
1206
1207
1208
  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
1209
1210
  if ( vlistID != -1 )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1211
1212
1213
1214
1215
1216
      if ( streamptr->filemode != 'w' )
	if ( vlistInqTaxis(vlistID) != -1 )
	  {
	    taxisDestroy(vlistInqTaxis(vlistID));
	  }

1217
      vlistDestroy(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1218
    }
Uwe Schulzweida's avatar
bug fix    
Uwe Schulzweida committed
1219
1220

  stream_delete_entry(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1221
1222
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1223
1224
1225
1226
1227
1228
/*
@Function  streamSync
@Title     Synchronize an Open Dataset to Disk

@Prototype  void streamSync(int streamID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1229
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240

@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)
{
  int filetype;
  int fileID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1241
  int nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1242
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1243
1244
1245

  streamptr = stream_to_pointer(streamID);

1246
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1247
1248
1249
1250

  fileID   = streamptr->fileID;
  filetype = streamptr->filetype;
  vlistID  = streamInqVlist(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1251
  nvars    = vlistNvars(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1252
1253

  if ( fileID == CDI_UNDEFID )
1254
    Warning("File %s not open!", streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1255
  else if ( vlistID == CDI_UNDEFID )
1256
    Warning("Vlist undefined for file %s!", streamptr->filename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1257
  else if ( nvars == 0 )
1258
    Warning("No variables defined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  else
    {
      if ( streamptr->filemode == 'w' || streamptr->filemode == 'a' )
	{
	  switch (filetype)
	    {
#if  defined  (HAVE_LIBNETCDF)
	    case FILETYPE_NC:
	    case FILETYPE_NC2:
	    case FILETYPE_NC4:
1269
	    case FILETYPE_NC4C:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1270
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1271
1272
		void cdf_sync(int ncid);
		if ( streamptr->ncmode == 2 ) cdf_sync(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1273
1274
1275
1276
1277
		break;
	      }
#endif
	    default:
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1278
		fileFlush(fileID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1279
1280
1281
1282
1283
1284
1285
		break;
	      }
	    }
	}
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1286
1287
/*
@Function  streamDefTimestep
1288
@Title     Define time step
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1289
1290
1291

@Prototype int streamDefTimestep(int streamID, int tsID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1292
    @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1293
    @Item  tsID      Timestep identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1294
1295

@Description
1296
The function @func{streamDefTimestep} defines the time step of a stream.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1297
1298

@Result
1299
@func{streamDefTimestep} returns the number of records of the time step.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1300
1301
1302
1303
1304
1305
1306
1307

@EndFunction
*/
int streamDefTimestep(int streamID, int tsID)
{
  int newtsID;
  int taxisID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1308
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1309
1310

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

  if ( CDI_Debug )
1313
    Message("streamID = %d  tsID = %d", streamID, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1314

1315
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1316
1317
1318
1319
1320

  vlistID = streamInqVlist(streamID);
  taxisID = vlistInqTaxis(vlistID);
  if ( taxisID == CDI_UNDEFID )
    {
1321
      Warning("taxisID undefined for fileID = %d! Using absolute time axis.", streamID);