cdi.c 28.5 KB
Newer Older
1
2
3
4
#if defined (HAVE_CONFIG_H)
#  include "../src/config.h"
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
#include <math.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12

13
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
int      vlistInqVarMissvalUsed(int vlistID, int varID);
15
#ifndef DBL_IS_NAN
16
#if  defined  (HAVE_DECL_ISNAN)
17
#  define DBL_IS_NAN(x)     (isnan(x))
18
#elif  defined  (FP_NAN)
19
#  define DBL_IS_NAN(x)     (fpclassify(x) == FP_NAN)
20
#else
21
#  define DBL_IS_NAN(x)     ((x) != (x))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
#endif
23
24
#endif

25
26
27
28
29
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
#  define DBL_IS_EQUAL(x,y) (DBL_IS_NAN(x)||DBL_IS_NAN(y)?(DBL_IS_NAN(x)&&DBL_IS_NAN(y)?1:0):!(x < y || y < x))
#endif

30
31
32
33
34
#ifndef IS_EQUAL
#  define IS_NOT_EQUAL(x,y) (x < y || y < x)
#  define IS_EQUAL(x,y)     (!IS_NOT_EQUAL(x,y))
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
35

Thomas Jahns's avatar
Thomas Jahns committed
36
37
38
39
40
41
42
static inline int
cdiUUIDIsNull(const unsigned char uuid[CDI_UUID_SIZE])
{
  static unsigned char uuid_nil[CDI_UUID_SIZE];
  return !memcmp(uuid, uuid_nil, CDI_UUID_SIZE);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
#include "printinfo.h"

45
void cdiDefTableID(int tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46

Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
49
50
51
52
53
54
int getopt(int argc, char *const argv[], const char *optstring);

extern char *optarg;
extern int optind, opterr, optopt;


char *Progname;

55
56
57
58
int DefaultFileType  = CDI_UNDEFID;
int DefaultDataType  = CDI_UNDEFID;
int DefaultByteorder = CDI_UNDEFID;

59
60
int comptype  = COMPRESS_NONE;  // Compression type
int complevel = 0;              // Compression level
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62


Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
64
static
void version(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
{
66
67
68
  int   filetypes[] = {FILETYPE_SRV, FILETYPE_EXT, FILETYPE_IEG, FILETYPE_GRB, FILETYPE_GRB2, FILETYPE_NC, FILETYPE_NC2, FILETYPE_NC4, FILETYPE_NC4C};
  char *typenames[] = {        "srv",        "ext",        "ieg",        "grb",        "grb2",        "nc",        "nc2",        "nc4",        "nc4c"};

69
  fprintf(stderr, "CDI version 1.8\n");
70
71
72
73
74
75
#if defined (COMPILER)
  fprintf(stderr, "Compiler: %s\n", COMPILER);
#endif
#if defined (COMP_VERSION)
  fprintf(stderr, " version: %s\n", COMP_VERSION);
#endif
76
77
78
79
80
81
82
83
84
85
#if defined (USER_NAME) && defined(HOST_NAME) && defined(SYSTEM_TYPE)
  fprintf(stderr, "Compiled: by %s on %s (%s) %s %s\n",
	  USER_NAME, HOST_NAME, SYSTEM_TYPE, __DATE__, __TIME__);
#endif

  fprintf(stderr, "filetype: ");
  for ( size_t i = 0; i < sizeof(filetypes)/sizeof(int); ++i )
    if ( cdiHaveFiletype(filetypes[i]) ) fprintf(stderr, "%s ", typenames[i]);
  fprintf(stderr, "\n");

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  fprintf(stderr, "    with:");
#if defined (HAVE_LIBPTHREAD)
  fprintf(stderr, " PTHREADS");
#endif
#if defined (_OPENMP)
  fprintf(stderr, " OpenMP");
#endif
#if  defined  (HAVE_NETCDF4)
  fprintf(stderr, " NC4");
#endif
#if  defined  (HAVE_LIBNC_DAP)
  fprintf(stderr, " OPeNDAP");
#endif
#if defined (HAVE_LIBSZ)
  fprintf(stderr, " SZ");
#endif
#if defined (HAVE_LIBZ)
  fprintf(stderr, " Z");
#endif
#if defined (HAVE_LIBJASPER)
  fprintf(stderr, " JASPER");
#endif
#if defined (HAVE_LIBPROJ)
  fprintf(stderr, " PROJ.4");
#endif
#if defined (HAVE_LIBDRMAA)
  fprintf(stderr, " DRMAA");
#endif
#if defined (HAVE_LIBCURL)
  fprintf(stderr, " CURL");
#endif
  fprintf(stderr, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
120
121
122
123
124
125
126
127
128
129
  cdiPrintVersion();
  fprintf(stderr, "\n");
/*
  1.0.0   6 Feb 2001 : initial version
  1.1.0  30 Jul 2003 : missing values implemented
  1.2.0   8 Aug 2003 : changes for CDI library version 0.7.0
  1.3.0  10 Feb 2004 : changes for CDI library version 0.7.9
  1.4.0   5 May 2004 : changes for CDI library version 0.8.1 (error handling)
  1.4.1  18 Sep 2004 : netCDF 2 support
  1.4.2  22 Mar 2005 : change level from int to double
  1.4.3  11 Apr 2005 : change date and time format to ISO
  1.5.0  22 Nov 2005 : IEG support
130
131
  1.5.1  21 Feb 2006 : added option -s for short info
  1.6.0   1 Aug 2006 : added option -z szip for SZIP compression of GRIB records
132
  1.6.1  27 Feb 2007 : short info with ltype for GENERIC zaxis
133
  1.6.2   3 Jan 2008 : changes for CDI library version 1.1.0 (compress)
134
  1.6.3  26 Mar 2008 : call streamDefTimestep also if ntsteps = 0 (buf fix)
135
136
137
  1.7.0  11 Apr 2008 : added option -z zip for deflate compression of netCDF4 variables
  1.7.1   1 Nov 2009 : added option -z jpeg for JPEG compression of GRIB2 records
  1.7.2  14 Nov 2012 : added optional compression level -z zip[_1-9]
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
139
140
*/
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
static
void usage(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
146
147
148
149
150
{
  char *name;
  int id;

  fprintf(stderr, "usage : %s  [Option]  [ifile]  [ofile]\n", Progname);

  fprintf(stderr, "\n");
  fprintf(stderr, "  Options:\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
  fprintf(stderr, "    -d             Print debugging information\n");
152
  fprintf(stderr, "    -f <format>    Format of the output file. (grb, grb2, nc, nc2, nc4, nc4c, src, ext or ieg)\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
  fprintf(stderr, "    -s             give short information if ofile is missing\n");
  fprintf(stderr, "    -t <table>     Parameter table name/file\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
156
157
158
159
160
161
  fprintf(stderr, "                   Predefined tables: ");
  for ( id = 0; id < tableInqNumber(); id++ )
    if ( (name = tableInqNamePtr(id)) )
      fprintf(stderr, " %s", name);
  fprintf(stderr, "\n");

  fprintf(stderr, "    -V             Print version number\n");
162
163
  fprintf(stderr, "    -z szip        SZIP compression of GRIB1 records\n");
  fprintf(stderr, "       jpeg        JPEG compression of GRIB2 records\n");
164
  fprintf(stderr, "        zip[_1-9]  Deflate compression of netCDF4 variables\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
  fprintf(stderr, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
  fprintf(stderr, "  Report bugs to <http://code.zmaw.de/projects/cdi>\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
169
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
static
171
void printInfo(int vdate, int vtime, char *varname, double level,
172
	       int datasize, int number, int nmiss, double missval, const double *data, int vardis)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
174
175
176
{
  static int rec = 0;
  int i, ivals = 0, imiss = 0;
  double arrmean, arrmin, arrmax;
177
  char vdatestr[32], vtimestr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178

Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
  if ( ! rec )
  {
181
    if ( vardis )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
      fprintf(stdout,
    "   Rec :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter name\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
/*   ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+ */
185
    else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
187
      fprintf(stdout,
    "   Rec :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter ID\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
/*   ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+ */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
  }

191
192
  date2str(vdate, vdatestr, sizeof(vdatestr));
  time2str(vtime, vtimestr, sizeof(vtimestr));
193

Uwe Schulzweida's avatar
Uwe Schulzweida committed
194
  fprintf(stdout, "%6d :%s %s %7g ", ++rec, vdatestr, vtimestr, level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195

Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
  fprintf(stdout, "%8d ", datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
198

  fprintf(stdout, "%7d :", nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
200

  if ( number == CDI_REAL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
203
204
    {
      if ( nmiss > 0 )
	{
	  arrmean = 0;
205
206
	  arrmin  =  1.e300;
	  arrmax  = -1.e300;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
	  for ( i = 0; i < datasize; i++ )
	    {
209
	      if ( !DBL_IS_EQUAL(data[i], missval) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
		{
		  if ( data[i] < arrmin ) arrmin = data[i];
		  if ( data[i] > arrmax ) arrmax = data[i];
		  arrmean += data[i];
		  ivals++;
		}
	    }
	  imiss = datasize - ivals;
	  datasize = ivals;
	}
      else
	{
	  arrmean = data[0];
	  arrmin  = data[0];
	  arrmax  = data[0];
	  for ( i = 1; i < datasize; i++ )
	    {
	      if ( data[i] < arrmin ) arrmin = data[i];
	      if ( data[i] > arrmax ) arrmax = data[i];
	      arrmean += data[i];
	    }
	}

      if ( datasize > 0 ) arrmean /= datasize;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
      fprintf(stdout, "%#12.5g%#12.5g%#12.5g", arrmin, arrmean, arrmax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  else
    {
      int nvals_r = 0, nvals_i = 0;
      double arrsum_r, arrsum_i, arrmean_r = 0, arrmean_i = 0;
      arrsum_r = 0;
      arrsum_i = 0;

      for ( i = 0; i < datasize; i++ )
	{
	  if ( !DBL_IS_EQUAL(data[i*2], missval) )
	    {
	      arrsum_r += data[i*2];
	      nvals_r++;
	    }
	  if ( !DBL_IS_EQUAL(data[i*2+1], missval) )
	    {
	      arrsum_i += data[i*2+1];
	      nvals_i++;
	    }
	}

258
259
      imiss = datasize - nvals_r;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
261
      if ( nvals_r > 0 ) arrmean_r = arrsum_r / nvals_r;
      if ( nvals_i > 0 ) arrmean_i = arrsum_i / nvals_i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
      fprintf(stdout, "  -  (%#12.5g,%#12.5g)  -", arrmean_r, arrmean_i);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
263
    }
264

Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
  fprintf(stdout, " : %-11s\n", varname);

267
268
  if ( imiss != nmiss && nmiss > 0 )
    fprintf(stdout, "Found %d of %d missing values!\n", imiss, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
}

271
#define MAXCHARS 82
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272

273
274
const char * tunit2str(int tunits)
{
275
276
277
278
279
280
281
282
283
284
285
286
  if      ( tunits == TUNIT_YEAR )       return ("years");
  else if ( tunits == TUNIT_MONTH )      return ("months");
  else if ( tunits == TUNIT_DAY )        return ("days");
  else if ( tunits == TUNIT_12HOURS )    return ("12hours");
  else if ( tunits == TUNIT_6HOURS )     return ("6hours");
  else if ( tunits == TUNIT_3HOURS )     return ("3hours");
  else if ( tunits == TUNIT_HOUR )       return ("hours");
  else if ( tunits == TUNIT_30MINUTES )  return ("30minutes");
  else if ( tunits == TUNIT_QUARTER )    return ("15minutes");
  else if ( tunits == TUNIT_MINUTE )     return ("minutes");
  else if ( tunits == TUNIT_SECOND )     return ("seconds");
  else                                   return ("unknown");
287
288
289
}


290
const char* calendar2str(int calendar)
291
292
293
294
295
296
297
298
299
{
  if      ( calendar == CALENDAR_STANDARD )  return ("standard");
  else if ( calendar == CALENDAR_PROLEPTIC ) return ("proleptic_gregorian");
  else if ( calendar == CALENDAR_360DAYS )   return ("360_day");
  else if ( calendar == CALENDAR_365DAYS )   return ("365_day");
  else if ( calendar == CALENDAR_366DAYS )   return ("366_day");
  else                                       return ("unknown");
}

300
static
301
void limit_string_length(char* string, size_t maxlen)
302
{
303
  string[maxlen-1] = 0;
304
305
306
307
308
  size_t len = strlen(string);

  if ( len > 10 )
    {
      for ( size_t i = 3; i < len; ++i )
309
	if ( string[i] == ' ' || string[i] == ',' || (i>10 && string[i] == '.') )
310
311
312
313
314
315
316
	  {
	    string[i] = 0;
	    break;
	  }
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
318
static
void printShortinfo(int streamID, int vlistID, int vardis)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319
320
321
{
  int varID;
  int gridsize = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
  int gridID, zaxisID, param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
  int vdate, vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
325
  int nvars, ntsteps;
  int levelsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326
  int tsteptype, taxisID;
327
  char tmpname[CDI_MAX_NAME];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
  char varname[CDI_MAX_NAME];
329
  const char *modelptr, *instptr;
330
  int datatype;
331
  int year, month, day, hour, minute, second;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
  char pstr[4];
333
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
334

335
336
      fprintf(stdout, "   File format");
      fprintf(stdout, " : ");
337
      printFiletype(streamID, vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338
339
340

      if ( vardis )
	fprintf(stdout,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
		"   Var : Institut Source   Ttype    Levels Num  Gridsize Num Dtype : Parameter name\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
343
      else
	fprintf(stdout,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
		"   Var : Institut Source   Ttype    Levels Num  Gridsize Num Dtype : Parameter ID\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
346
347
348
349

      nvars = vlistNvars(vlistID);

      for ( varID = 0; varID < nvars; varID++ )
	{
350
	  param   = vlistInqVarParam(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
352
353
354
355
	  gridID  = vlistInqVarGrid(vlistID, varID);
	  zaxisID = vlistInqVarZaxis(vlistID, varID);

	  fprintf(stdout, "%6d : ", varID + 1);

356
	  /* institute info */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
	  instptr = institutInqNamePtr(vlistInqVarInstitut(vlistID, varID));
358
	  strcpy(tmpname, "unknown");
359
360
	  if ( instptr ) strncpy(tmpname, instptr, CDI_MAX_NAME);
	  limit_string_length(tmpname, CDI_MAX_NAME);
361
	  fprintf(stdout, "%-8s ", tmpname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
362

363
	  /* source info */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
	  modelptr = modelInqNamePtr(vlistInqVarModel(vlistID, varID));
365
	  strcpy(tmpname, "unknown");
366
367
	  if ( modelptr ) strncpy(tmpname, modelptr, CDI_MAX_NAME);
	  limit_string_length(tmpname, CDI_MAX_NAME);
368
	  fprintf(stdout, "%-8s ", tmpname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369

370
	  /* tsteptype */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
	  tsteptype = vlistInqVarTsteptype(vlistID, varID);
372
373
	  if      ( tsteptype == TSTEP_CONSTANT ) fprintf(stdout, "%-8s ", "constant");
	  else if ( tsteptype == TSTEP_INSTANT  ) fprintf(stdout, "%-8s ", "instant");
374
375
	  else if ( tsteptype == TSTEP_INSTANT2 ) fprintf(stdout, "%-8s ", "instant");
	  else if ( tsteptype == TSTEP_INSTANT3 ) fprintf(stdout, "%-8s ", "instant");
376
377
	  else if ( tsteptype == TSTEP_MIN      ) fprintf(stdout, "%-8s ", "min");
	  else if ( tsteptype == TSTEP_MAX      ) fprintf(stdout, "%-8s ", "max");
378
	  else if ( tsteptype == TSTEP_AVG      ) fprintf(stdout, "%-8s ", "avg");
379
	  else if ( tsteptype == TSTEP_ACCUM    ) fprintf(stdout, "%-8s ", "accum");
380
381
	  else if ( tsteptype == TSTEP_RANGE    ) fprintf(stdout, "%-8s ", "range");
	  else if ( tsteptype == TSTEP_DIFF     ) fprintf(stdout, "%-8s ", "diff");
382
383
384
385
386
387
	  else                                    fprintf(stdout, "%-8s ", "unknown");

	  /* layer info */
	  levelsize = zaxisInqSize(zaxisID);
	  fprintf(stdout, "%6d ", levelsize);
	  fprintf(stdout, "%3d ", vlistZaxisIndex(vlistID, zaxisID) + 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388

389
390
391
392
	  /* grid info */
	  gridsize = gridInqSize(gridID);
	  fprintf(stdout, "%9d ", gridsize);
	  fprintf(stdout, "%3d ", vlistGridIndex(vlistID, gridID) + 1);
393

394
395
	  /* datatype */
	  datatype = vlistInqVarDatatype(vlistID, varID);
396
397
398
399
400
401
402
403
404
405
406
407
408
	  if      ( datatype == DATATYPE_PACK   ) strcpy(pstr, "P0");
	  else if ( datatype > 0 && datatype <= 32  ) sprintf(pstr, "P%d", datatype);
	  else if ( datatype == DATATYPE_CPX32  ) strcpy(pstr, "C32");
	  else if ( datatype == DATATYPE_CPX64  ) strcpy(pstr, "C64");
	  else if ( datatype == DATATYPE_FLT32  ) strcpy(pstr, "F32");
	  else if ( datatype == DATATYPE_FLT64  ) strcpy(pstr, "F64");
	  else if ( datatype == DATATYPE_INT8   ) strcpy(pstr, "I8");
	  else if ( datatype == DATATYPE_INT16  ) strcpy(pstr, "I16");
	  else if ( datatype == DATATYPE_INT32  ) strcpy(pstr, "I32");
	  else if ( datatype == DATATYPE_UINT8  ) strcpy(pstr, "U8");
	  else if ( datatype == DATATYPE_UINT16 ) strcpy(pstr, "U16");
	  else if ( datatype == DATATYPE_UINT32 ) strcpy(pstr, "U32");
	  else                                    strcpy(pstr, "-1");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
410
411

	  fprintf(stdout, " %-3s", pstr);

412
	  if ( vlistInqVarCompType(vlistID, varID) == COMPRESS_NONE )
413
	    fprintf(stdout, "  ");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
414
	  else
415
	    fprintf(stdout, "z ");
416

417
418
	  /* parameter info */
	  fprintf(stdout, ": ");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419

420
	  cdiParamToString(param, paramstr, sizeof(paramstr));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421

422
423
424
425
426
	  if (vardis)
            {
              vlistInqVarName(vlistID, varID, varname);
              fprintf(stdout, "%-11s", varname);
            }
427
	  else
428
	    fprintf(stdout, "%-11s", paramstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
432

	  fprintf(stdout, "\n");
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
434
435
      fprintf(stdout, "   Grid coordinates");
      fprintf(stdout, " :\n");

436
      printGridInfo(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
437

Uwe Schulzweida's avatar
Uwe Schulzweida committed
438
439
      fprintf(stdout, "   Vertical coordinates");
      fprintf(stdout, " :\n");
440

Uwe Schulzweida's avatar
Uwe Schulzweida committed
441
      printZaxisInfo(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
443
444
445
446
447
448

      taxisID = vlistInqTaxis(vlistID);
      ntsteps = vlistNtsteps(vlistID);

      if ( ntsteps != 0 )
	{
	  if ( ntsteps == CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
449
	    fprintf(stdout, "   Time coordinate :  unlimited steps\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450
	  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
451
	    fprintf(stdout, "   Time coordinate :  %d step%s\n", ntsteps, ntsteps == 1 ? "" : "s");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
453
454
455
456

	  if ( taxisID != CDI_UNDEFID )
	    {
	      if ( taxisInqType(taxisID) == TAXIS_RELATIVE )
		{
457
458
                  int calendar, tunits;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
460
461
		  vdate = taxisInqRdate(taxisID);
		  vtime = taxisInqRtime(taxisID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
462
463
		  cdiDecodeDate(vdate, &year, &month, &day);
		  cdiDecodeTime(vtime, &hour, &minute, &second);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464

465
466
		  fprintf(stdout, "     RefTime = %4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
			  year, month, day, hour, minute, second);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467

Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
		  tunits = taxisInqTunit(taxisID);
469
		  if ( tunits != CDI_UNDEFID )  fprintf(stdout, "  Units = %s", tunit2str(tunits));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
470

Uwe Schulzweida's avatar
Uwe Schulzweida committed
471
		  calendar = taxisInqCalendar(taxisID);
472
		  if ( calendar != CDI_UNDEFID )  fprintf(stdout, "  Calendar = %s", calendar2str(calendar));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473

Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
475
476
		  if ( taxisHasBounds(taxisID) )
		    fprintf(stdout, "  Bounds = true");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
477
478
479
480
		  fprintf(stdout, "\n");
		}
	    }

481
	  fprintf(stdout, "  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482

483
	  printTimesteps(streamID, taxisID, 0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
485
486
487
488
489

	  fprintf(stdout, "\n");
	}
}


490
491
492
493
#undef  IsBigendian
#define IsBigendian()  ( u_byteorder.c[sizeof(long) - 1] )


Uwe Schulzweida's avatar
Uwe Schulzweida committed
494
495
static
void setDefaultDataType(char *datatypestr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
{
497
  static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
498
  int nbits = -1;
499
  enum {D_UINT, D_INT, D_FLT, D_CPX};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  int dtype = -1;

  if      ( *datatypestr == 'i' || *datatypestr == 'I' )
    {
      dtype = D_INT;
      datatypestr++;
    }
  else if ( *datatypestr == 'u' || *datatypestr == 'U' )
    {
      dtype = D_UINT;
      datatypestr++;
    }
  else if ( *datatypestr == 'f' || *datatypestr == 'F' )
    {
      dtype = D_FLT;
      datatypestr++;
    }
517
518
519
520
521
  else if ( *datatypestr == 'c' || *datatypestr == 'C' )
    {
      dtype = D_CPX;
      datatypestr++;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
522
523
524

  if ( isdigit((int) *datatypestr) )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
526
      nbits = atoi(datatypestr);
      if ( nbits < 10 )
527
528
529
	datatypestr += 1;
      else
	datatypestr += 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
530

Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
      if ( dtype == -1 )
532
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
533
534
535
536
537
538
539
540
541
	  if      ( nbits > 0 && nbits < 32 ) DefaultDataType = nbits;
	  else if ( nbits == 32 )
	    {
	      if ( DefaultFileType == FILETYPE_GRB )
		DefaultDataType = DATATYPE_PACK32;
	      else
		DefaultDataType = DATATYPE_FLT32;
	    }
	  else if ( nbits == 64 ) DefaultDataType = DATATYPE_FLT64;
542
	  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
	    {
	      fprintf(stderr, "Unsupported number of bits %d!\n", nbits);
545
	      fprintf(stderr, "Use I8/I16/I32/F32/F64 for nc/nc2/nc4/nc4c; F32/F64 for grb2/srv/ext/ieg; P1 - P24 for grb/grb2.\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
	      exit(EXIT_FAILURE);
	    }
548
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549
550
      else
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
	  if ( dtype == D_INT )
	    {
	      if      ( nbits ==  8 ) DefaultDataType = DATATYPE_INT8;
	      else if ( nbits == 16 ) DefaultDataType = DATATYPE_INT16;
	      else if ( nbits == 32 ) DefaultDataType = DATATYPE_INT32;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype INT!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
	  /*
	  else if ( dtype == D_UINT )
	    {
	      if      ( nbits ==  8 ) DefaultDataType = DATATYPE_UINT8;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype UINT!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
	  */
	  else if ( dtype == D_FLT )
	    {
	      if      ( nbits == 32 ) DefaultDataType = DATATYPE_FLT32;
	      else if ( nbits == 64 ) DefaultDataType = DATATYPE_FLT64;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype FLT!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
583
584
585
586
587
588
589
590
591
592
	  else if ( dtype == D_CPX )
	    {
	      if      ( nbits == 32 ) DefaultDataType = DATATYPE_CPX32;
	      else if ( nbits == 64 ) DefaultDataType = DATATYPE_CPX64;
	      else
		{
		  fprintf(stderr, "Unsupported number of bits = %d for datatype CPX!\n", nbits);
		  exit(EXIT_FAILURE);
		}
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
593
594
595
	}
    }

596
  if ( *datatypestr != 0 )
597
    {
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
      if ( *datatypestr == 'l' || *datatypestr == 'L' )
	{
	  if ( IsBigendian() ) DefaultByteorder = CDI_LITTLEENDIAN;
	  datatypestr++;
	}
      else if ( *datatypestr == 'b' || *datatypestr == 'B' )
	{
	  if ( ! IsBigendian() ) DefaultByteorder = CDI_BIGENDIAN;
	  datatypestr++;
	}
      else
	{
	  fprintf(stderr, "Unsupported character in number of bytes: >%s< !\n", datatypestr);
	  exit(EXIT_FAILURE);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
613
614
615
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
617
static
void setDefaultFileType(char *filetypestr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
619
620
{
  if ( filetypestr )
    {
621
622
      char *ftstr = filetypestr;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
623
      if      ( memcmp(filetypestr, "grb2", 4)  == 0 ) { ftstr += 4; DefaultFileType = FILETYPE_GRB2;}
624
      else if ( memcmp(filetypestr, "grb1", 4)  == 0 ) { ftstr += 4; DefaultFileType = FILETYPE_GRB; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
625
626
      else if ( memcmp(filetypestr, "grb",  3)  == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_GRB; }
      else if ( memcmp(filetypestr, "nc2",  3)  == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_NC2; }
627
      else if ( memcmp(filetypestr, "nc4c", 4)  == 0 ) { ftstr += 4; DefaultFileType = FILETYPE_NC4C;}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
628
629
630
631
632
      else if ( memcmp(filetypestr, "nc4",  3)  == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_NC4; }
      else if ( memcmp(filetypestr, "nc",   2)  == 0 ) { ftstr += 2; DefaultFileType = FILETYPE_NC;  }
      else if ( memcmp(filetypestr, "srv",  3)  == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_SRV; }
      else if ( memcmp(filetypestr, "ext",  3)  == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_EXT; }
      else if ( memcmp(filetypestr, "ieg",  3)  == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_IEG; }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633
634
635
      else
	{
	  fprintf(stderr, "Unsupported filetype %s!\n", filetypestr);
636
	  fprintf(stderr, "Available filetypes: grb, grb2, nc, nc2, nc4, nc4c, srv, ext and ieg\n");
637
	  exit(EXIT_FAILURE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
638
639
	}

640
      if ( DefaultFileType != CDI_UNDEFID && *ftstr != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
641
	{
642
	  if ( *ftstr == '_' )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
643
	    {
644
	      ftstr++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
645

646
647
648
649
650
651
	      setDefaultDataType(ftstr);
	    }
	  else
	    {
	      fprintf(stderr, "Unexpected character >%c< in file type >%s<!\n", *ftstr, filetypestr);
	      fprintf(stderr, "Use format[_nbits] with:\n");
652
	      fprintf(stderr, "    format = grb, grb2, nc, nc2, nc4, nc4c, srv, ext or ieg\n");
653
	      fprintf(stderr, "    nbits  = 32/64 for grb2/nc/nc2/nc4/nc4c/srv/ext/ieg; 1 - 24 for grb/grb2\n");
654
	      exit(EXIT_FAILURE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
656
657
658
659
	    }
	}
    }
}

660
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
661
662
663
int handle_error(int cdiErrno, const char *fmt, ...)
{
  va_list args;
664

Uwe Schulzweida's avatar
Uwe Schulzweida committed
665
666
667
668
669
670
671
672
673
674
675
676
677
  va_start(args, fmt);

  printf("\n");
  vfprintf(stderr, fmt, args);
   fprintf(stderr, "\n");

  va_end(args);

  fprintf(stderr, "%s\n", cdiStringError(cdiErrno));

  return (cdiErrno);
}

678
static
679
680
681
682
683
void defineCompress(const char *arg)
{
  size_t len = strlen(arg);

  if      ( strncmp(arg, "szip", len) == 0 )
684
    {
685
      comptype = COMPRESS_SZIP;
686
    }
687
688
  else if ( strncmp(arg, "jpeg", len) == 0 )
    {
689
      comptype = COMPRESS_JPEG;
690
    }
691
  else if ( strncmp(arg, "gzip", len) == 0 )
692
    {
693
      comptype = COMPRESS_GZIP;
694
      complevel = 6;
695
    }
696
  else if ( strncmp(arg, "zip", 3) == 0 )
697
    {
698
      comptype = COMPRESS_ZIP;
699
700
701
702
      if ( len == 5 && arg[3] == '_' && isdigit(arg[4]) )
	complevel = atoi(&arg[4]);
      else
        complevel = 1;
703
    }
704
  else
705
    fprintf(stderr, "%s compression unsupported!\n", arg);
706
707
708
709
}



Uwe Schulzweida's avatar
Uwe Schulzweida committed
710
711
712
713
714
715
716
717
718
719
int main(int argc, char *argv[])
{
  int c;
  char *fname1 = NULL;
  char *fname2 = NULL;
  char *rTable = NULL;
  char *wTable = NULL;
  int Move = 0;
  int Record = 0;
  int Debug = 0;
Thomas Jahns's avatar
Thomas Jahns committed
720
  /* int Quiet  = 0; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
721
  int Vardis = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
722
723
  int Version = 0;
  int Longinfo = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
  int Shortinfo = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
725
726
  int varID;
  int itableID = CDI_UNDEFID, otableID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
727
  int Info = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728
  char varname[CDI_MAX_NAME];
729
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
730
731
732
733
734

  Progname = strrchr(argv[0], '/');
  if (Progname == 0) Progname = argv[0];
  else               Progname++;

735
  while ( (c = getopt(argc, argv, "b:f:t:w:z:cdhlMmqRrsvVZ")) != EOF )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
737
738
    {
      switch (c)
	{
739
740
741
	case 'b':
	  setDefaultDataType(optarg);
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
742
743
744
745
746
747
	case 'd':
	  Debug = 1;
	  break;
	case 'f':
	  setDefaultFileType(optarg);
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
749
750
	case 'h':
	  usage();
	  exit (0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
752
753
	case 'l':
	  Longinfo = 1;
	  break;
754
755
756
	case 'M':
	  cdiDefGlobal("HAVE_MISSVAL", 1);
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
757
758
759
760
	case 'm':
	  Move = 1;
	  break;
	case 'q':
Thomas Jahns's avatar
Thomas Jahns committed
761
	  /* Quiet = 1; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
762
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
764
765
766
767
768
	case 'R':
	  cdiDefGlobal("REGULARGRID", 1);
	  break;
	case 'r':
	  Record = 1;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
770
771
	case 's':
	  Shortinfo = 1;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
773
774
	case 't':
	  rTable = optarg;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
775
776
777
778
779
780
	case 'v':
	  Vardis = 1;
	  break;
	case 'V':
	  Version = 1;
	  break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
781
782
783
784
	case 'w':
	  wTable = optarg;
	  break;
	case 'z':
785
	  defineCompress(optarg);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
787
788
789
790
791
	  break;
	}
    }

  if ( optind < argc ) fname1 = argv[optind++];
  if ( optind < argc ) fname2 = argv[optind++];
792
  if ( optind < argc ) fprintf(stderr, "optind: %d argc: %d\n", optind, argc);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793
794
795
796
797
798
799
800

  if ( Debug || Version ) version();

  if ( Debug ) cdiDebug(Debug);

  if ( rTable )
    {
      itableID = tableInq(-1, 0, rTable);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
801
      if ( itableID != CDI_UNDEFID ) cdiDefTableID(itableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
      otableID = itableID;
    }

  if ( fname1 == NULL && ! (Debug || Version) )
    {
      usage();
      exit (0);
    }

  if ( fname1 )
    {
      double *data = NULL;
      double missval;
      double level;
      int nmiss;
817
      int number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
818
819
820
821
      int datasize = 0;
      int streamID1 = CDI_UNDEFID;
      int streamID2 = CDI_UNDEFID;
      int filetype;
822
      int gridID, zaxisID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
823
      int param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
824
825
826
827
      int vdate, vtime;
      int nrecs, nvars;
      int levelID, levelsize;
      int nts = 0;
828
      int gridsize = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
829
830
831
      int recID;
      int tsID;
      int ntsteps = 0;
832
      int taxisID1, taxisID2 = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
      int vlistID1, vlistID2 = CDI_UNDEFID;

      streamID1 = streamOpenRead(fname1);
      if ( streamID1 < 0 )
	return (handle_error(streamID1, "Open failed on %s", fname1));

      vlistID1 = streamInqVlist(streamID1);

      if ( Longinfo )
	{
	  int ngrids, nzaxis;
	  vlistPrint(vlistID1);
	  ngrids = vlistNgrids(vlistID1);
	  nzaxis = vlistNzaxis(vlistID1);
847
	  for ( gridID = 0; gridID < ngrids; gridID++ ) gridPrint(gridID, gridID, 1);
848
	  for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ ) zaxisPrint(zaxisID, zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
849
850
851
	}

      nvars   = vlistNvars(vlistID1);
852
      taxisID1 = vlistInqTaxis(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
853
854
      ntsteps = vlistNtsteps(vlistID1);

855
856
857
      if (Debug)
        fprintf(stderr, "nvars   = %d\n"
                "ntsteps = %d\n", nvars, ntsteps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858

859
860
861
862
863
864
      if ( fname2 )
        {
          vlistID2 = vlistDuplicate(vlistID1);
          taxisID2 = taxisDuplicate(taxisID1);
          vlistDefTaxis(vlistID2, taxisID2);
        }
865

Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
868
869
870
871
872
873
874
875
876
877
878
879
      for ( varID = 0; varID < nvars; varID++)
	{
	  gridID   = vlistInqVarGrid(vlistID1, varID);
	  gridsize = gridInqSize(gridID);
	  if ( gridsize > datasize ) datasize = gridsize;
	  if ( fname2 )
	    {
	      if ( DefaultDataType != CDI_UNDEFID )
		vlistDefVarDatatype(vlistID2, varID, DefaultDataType);
	    }
	}

      if ( fname2 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
880
	  Info = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
881
882
883
884
885
886
887
888
889
	  filetype = streamInqFiletype(streamID1);

	  if ( DefaultFileType != CDI_UNDEFID )
	    filetype = DefaultFileType;

	  streamID2 = streamOpenWrite(fname2, filetype);
	  if ( streamID2 < 0 )
	    return (handle_error(streamID2, "Open failed on %s", fname2));

890
891
892
	  if ( DefaultByteorder != CDI_UNDEFID )
	    streamDefByteorder(streamID2, DefaultByteorder);

893
	  if ( comptype != COMPRESS_NONE )
894
	    {
895
	      streamDefCompType(streamID2, comptype);
896
	      streamDefCompLevel(streamID2, complevel);
897
898
	    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
899
900
901
902
	  streamDefVlist(streamID2, vlistID2);

	  if ( otableID == CDI_UNDEFID ) otableID = itableID;
	}
903

Uwe Schulzweida's avatar
Uwe Schulzweida committed
904
      if ( vlistNumber(vlistID1) != CDI_REAL ) datasize *= 2;
Thomas Jahns's avatar
Thomas Jahns committed
905
      data = (double *) malloc((size_t)datasize * sizeof (double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
906
907
908
909

      /*
	nts = cdiInqTimeSize(streamID1);
      */
910
911
912
913
      if (Debug)
	printf("nts = %d\n"
               "streamID1 = %d, streamID2 = %d\n",
               nts, streamID1, streamID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914

Uwe Schulzweida's avatar
Uwe Schulzweida committed
915
916
917
918
919
920
      if ( Shortinfo )
	{
	  Info = 0;
	  printShortinfo(streamID1, vlistID1, Vardis);
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
921
      tsID = 0;
922
      if ( Info || fname2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
923
924
      while ( (nrecs = streamInqTimestep(streamID1, tsID)) > 0 )
	{
925
	  if ( fname2 /* && ntsteps != 0*/ )
926
927
928
929
930
931
            {
              taxisCopyTimestep(taxisID2, taxisID1);
              streamDefTimestep(streamID2, tsID);
            }
	  vdate = taxisInqVdate(taxisID1);
	  vtime = taxisInqVtime(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
932
933
934
935
936
937
938
939
940
941
942

	  if ( Debug )
	    fprintf(stdout, "tsID = %d nrecs = %d date = %d time = %d\n", tsID, nrecs, vdate, vtime);

	  if ( Record )
	    {
	      for ( recID = 0; recID < nrecs; recID++ )
		{
		  streamInqRecord(streamID1, &varID, &levelID);
		  streamReadRecord(streamID1, data, &nmiss);

943
		  number   = vlistInqVarNumber(vlistID1, varID);
944
945
946
		  gridID   = vlistInqVarGrid(vlistID1, varID);
		  zaxisID  = vlistInqVarZaxis(vlistID1, varID);
		  param    = vlistInqVarParam(vlistID1, varID);
947

948
		  cdiParamToString(param, paramstr, sizeof(paramstr));
949
950
951

		  if ( Vardis ) vlistInqVarName(vlistID1, varID, varname);
		  else          strcpy(varname, paramstr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
952
		  /*
953
954
		  printf("varID=%d, param=%d, gridID=%d, zaxisID=%d levelID=%d\n",
			 varID, param, gridID, zaxisID, levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
955
956
957
958
		  */
		  gridsize = gridInqSize(gridID);
		  level    = zaxisInqLevel(zaxisID, levelID);
		  missval  = vlistInqVarMissval(vlistID1, varID);
959

Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
		  if ( Info )
961
		    printInfo(vdate, vtime, varname, level, gridsize, number, nmiss, missval, data, Vardis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
962
963
964
965
966
967
968

		  if ( fname2 )
		    {
		      streamDefRecord(streamID2, varID, levelID);
		      if ( Move )
			streamCopyRecord(streamID2, streamID1);
		      else
969
			streamWriteRecord(streamID2, data, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
970
971
972
973
974
975
976
		    }
	      	}
	    }
	  else
	    {
	      for ( varID = 0; varID < nvars; varID++ )
		{
977
		  if ( vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT && tsID > 0 ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
978

979
		  number   = vlistInqVarNumber(vlistID1, varID);
980
981
982
		  gridID   = vlistInqVarGrid(vlistID1, varID);
		  zaxisID  = vlistInqVarZaxis(vlistID1, varID);
		  param    = vlistInqVarParam(vlistID1, varID);
983

984
		  cdiParamToString(param, paramstr, sizeof(paramstr));
985
986
987
988

		  if ( Vardis ) vlistInqVarName(vlistID1, varID, varname);
		  else          strcpy(varname, paramstr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
989
		  if ( Debug )
990
991
		    fprintf(stdout, "varID = %d param = %d gridID = %d zaxisID = %d\n",
			    varID, param, gridID, zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
992
993
994
995
996
997
998
999
1000
1001

		  gridsize = gridInqSize(gridID);
		  missval  = vlistInqVarMissval(vlistID1, varID);

		  levelsize = zaxisInqSize(zaxisID);
		  for ( levelID = 0; levelID < levelsize; levelID++ )
		    {
		      level = zaxisInqLevel(zaxisID, levelID);
		      streamReadVarSlice(streamID1, varID, levelID, data, &nmiss);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1002
		      if ( Info )
1003
			printInfo(vdate, vtime, varname, level, gridsize, number, nmiss, missval, data, Vardis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014

		      if ( fname2 )
			streamWriteVarSlice(streamID2, varID, levelID, data, nmiss);
		    }
		}
	    }
	  tsID++;
        }

      free(data);

1015
      /* fprintf(stderr, "%ld\n", (long) streamNvals(streamID1)); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1016

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1017
1018
1019
1020
      if ( fname2 )
	{
	  streamClose(streamID2);
	  vlistDestroy(vlistID2);
1021
	  taxisDestroy(taxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1022
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1023
1024
1025
1026
1027
1028
1029
1030
      streamClose(streamID1);
    }

  if ( wTable )
    tableWrite(wTable, itableID);

  return (0);
}
1031
1032
1033
1034
1035
1036
1037
1038
1039
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */