printinfo.c 23.6 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
// This file is used in CDI and CDO !!!

#ifdef HAVE_CONFIG_H
#include "../src/config.h"
#endif

#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>

#include <cdi.h>
#include "cdi_uuid.h"

#ifdef CDO

#include "text.h"
#include "datetime.h"
#include "compare.h"
#include "util.h"
#include "pstream_int.h"
#include "process_int.h"  //TODO: this include must not be here.
                          //refactoring needed (11.9.2018);
#define streamInqFiletype pstreamInqFiletype
#define streamInqByteorder pstreamInqByteorder
#define streamInqTimestep cdoStreamInqTimestep

#else

#include "cdi_int.h"

#endif


#include "printinfo.h"

static void
my_reset_text_color(FILE *fp)
{
  (void) fp;
#ifdef CDO
  reset_text_color(fp);
#endif
}

void
datetime2str(int64_t date, int time, char *datetimestr, int maxlen)
{
  int year, month, day;
  cdiDecodeDate(date, &year, &month, &day);
  int hour, minute, second;
  cdiDecodeTime(time, &hour, &minute, &second);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
  const int len = sprintf(datetimestr, DATE_FORMAT "T" TIME_FORMAT, year, month, day, hour, minute, second);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
57
58
59
60
61
62
63
  if (len > (maxlen - 1)) fprintf(stderr, "Internal problem (%s): sizeof input string is too small!\n", __func__);
}

void
date2str(int64_t date, char *datestr, int maxlen)
{
  int year, month, day;
  cdiDecodeDate(date, &year, &month, &day);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
  const int len = sprintf(datestr, DATE_FORMAT, year, month, day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66
67
68
69
70
71
72
73
  if (len > (maxlen - 1)) fprintf(stderr, "Internal problem (%s): sizeof input string is too small!\n", __func__);
}

void
time2str(int time, char *timestr, int maxlen)
{
  int hour, minute, second;
  cdiDecodeTime(time, &hour, &minute, &second);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
  const int len = sprintf(timestr, TIME_FORMAT, hour, minute, second);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  if (len > (maxlen - 1)) fprintf(stderr, "Internal problem (%s): sizeof input string is too small!\n", __func__);
}

const char *
comp_name(int comptype)
{
  if (comptype == CDI_COMPRESS_SZIP) return "szip";
  if (comptype == CDI_COMPRESS_ZIP) return "zip";
  if (comptype == CDI_COMPRESS_JPEG) return "jpeg";
  if (comptype == CDI_COMPRESS_AEC) return "aec";
  return " ";
}

void
printFiletype(int streamID, int vlistID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
  const int filetype = streamInqFiletype(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
118
119

  // clang-format off
  switch ( filetype )
    {
    case CDI_FILETYPE_GRB:  printf("GRIB");  break;
    case CDI_FILETYPE_GRB2: printf("GRIB2");  break;
    case CDI_FILETYPE_NC:   printf("NetCDF");  break;
    case CDI_FILETYPE_NC2:  printf("NetCDF2");  break;
    case CDI_FILETYPE_NC4:  printf("NetCDF4");  break;
    case CDI_FILETYPE_NC4C: printf("NetCDF4 classic");  break;
    case CDI_FILETYPE_NC5:  printf("NetCDF5");  break;
    case CDI_FILETYPE_SRV:  printf("SERVICE");  break;
    case CDI_FILETYPE_EXT:  printf("EXTRA");  break;
    case CDI_FILETYPE_IEG:  printf("IEG");  break;
    default: printf("  File format: unsupported filetype %d" , filetype);  break;
    }

  if ( filetype == CDI_FILETYPE_SRV || filetype == CDI_FILETYPE_EXT || filetype == CDI_FILETYPE_IEG )
    {
      switch ( streamInqByteorder(streamID) )
	{
	case CDI_BIGENDIAN:    printf("  BIGENDIAN");  break;
	case CDI_LITTLEENDIAN: printf("  LITTLEENDIAN");  break;
	default: printf("  byteorder: %d undefined", streamInqByteorder(streamID));  break;
	}
    }
  // clang-format on

Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
121
  const int nvars = vlistNvars(vlistID);
  const int comps[] = { CDI_COMPRESS_ZIP, CDI_COMPRESS_JPEG, CDI_COMPRESS_SZIP, CDI_COMPRESS_AEC };
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
124
125
  unsigned kk = 0;
  for (unsigned k = 0; k < sizeof(comps) / sizeof(int); ++k)
    for (int varID = 0; varID < nvars; varID++)
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
        const int comptype = vlistInqVarCompType(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        if (comptype == comps[k])
          {
            if (kk++ == 0)
              printf(" ");
            else
              printf("/");
            printf("%s", comp_name(comptype));
            break;
          }
      }

  printf("\n");
}

static void
print_xvals(int gridID, int dig)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
  const size_t xsize = gridInqXsize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147
148
149
150
  if (xsize > 0 && gridInqXvals(gridID, NULL))
    {
      char xname[CDI_MAX_NAME], xunits[CDI_MAX_NAME];
      gridInqXname(gridID, xname);
      gridInqXunits(gridID, xunits);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
152
153
      const double xfirst = gridInqXval(gridID, 0);
      const double xlast = gridInqXval(gridID, xsize - 1);
      const double xinc = gridInqXinc(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
      fprintf(stdout, "%33s : %.*g", xname, dig, xfirst);
      if (xsize > 1)
        {
          fprintf(stdout, " to %.*g", dig, xlast);
          if (IS_NOT_EQUAL(xinc, 0)) fprintf(stdout, " by %.*g", dig, xinc);
        }
      fprintf(stdout, " %s", xunits);
      if (gridIsCircular(gridID)) fprintf(stdout, "  circular");
      fprintf(stdout, "\n");
    }
}

static void
print_yvals(int gridID, int dig)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
  const size_t ysize = gridInqYsize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
173
174
175
  if (ysize > 0 && gridInqYvals(gridID, NULL))
    {
      char yname[CDI_MAX_NAME], yunits[CDI_MAX_NAME];
      gridInqYname(gridID, yname);
      gridInqYunits(gridID, yunits);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178
      const double yfirst = gridInqYval(gridID, 0);
      const double ylast = gridInqYval(gridID, ysize - 1);
      const double yinc = gridInqYinc(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
181
      fprintf(stdout, "%33s : %.*g", yname, dig, yfirst);
      if (ysize > 1)
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
          const int gridtype = gridInqType(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
          fprintf(stdout, " to %.*g", dig, ylast);
          if (IS_NOT_EQUAL(yinc, 0) && gridtype != GRID_GAUSSIAN && gridtype != GRID_GAUSSIAN_REDUCED)
            fprintf(stdout, " by %.*g", dig, yinc);
        }
      fprintf(stdout, " %s", yunits);
      fprintf(stdout, "\n");
    }
}

static void
print_xyvals2D(int gridID, int dig)
{
  if (gridInqXvals(gridID, NULL) && gridInqYvals(gridID, NULL))
    {
      char xname[CDI_MAX_NAME], yname[CDI_MAX_NAME], xunits[CDI_MAX_NAME], yunits[CDI_MAX_NAME];
      gridInqXname(gridID, xname);
      gridInqYname(gridID, yname);
      gridInqXunits(gridID, xunits);
      gridInqYunits(gridID, yunits);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
      const size_t gridsize = gridInqSize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
      double *xvals2D = (double *) malloc(gridsize * sizeof(double));
      double *yvals2D = (double *) malloc(gridsize * sizeof(double));

      gridInqXvals(gridID, xvals2D);
      gridInqYvals(gridID, yvals2D);

      double xfirst = xvals2D[0];
      double xlast = xvals2D[0];
      double yfirst = yvals2D[0];
      double ylast = yvals2D[0];
      for (size_t i = 1; i < gridsize; i++)
        {
          if (xvals2D[i] < xfirst) xfirst = xvals2D[i];
          if (xvals2D[i] > xlast) xlast = xvals2D[i];
          if (yvals2D[i] < yfirst) yfirst = yvals2D[i];
          if (yvals2D[i] > ylast) ylast = yvals2D[i];
        }

      double xinc = 0;
      double yinc = 0;
      int gridtype = gridInqType(gridID);
      if (gridtype == GRID_CURVILINEAR)
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
228
          const size_t xsize = gridInqXsize(gridID);
          const size_t ysize = gridInqYsize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230
231
232
233
234
235
236
237
238
239
240
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
          if (xsize > 1)
            {
              double *xvals = (double *) malloc((size_t) xsize * sizeof(double));
              for (size_t i = 0; i < xsize; ++i) xvals[i] = xvals2D[i];
              xinc = fabs(xvals[xsize - 1] - xvals[0]) / (xsize - 1);
              for (size_t i = 1; i < xsize; i++)
                if (fabs(fabs(xvals[i - 1] - xvals[i]) - xinc) > 0.01 * xinc)
                  {
                    xinc = 0;
                    break;
                  }
              free(xvals);
              if (IS_NOT_EQUAL(xinc, 0))
                {
                  for (size_t i = 1; i < ysize; i++)
                    if (IS_NOT_EQUAL(xvals2D[i * xsize], xvals2D[0])
                        || IS_NOT_EQUAL(xvals2D[(i + 1) * xsize - 1], xvals2D[xsize - 1]))
                      {
                        xinc = 0;
                        break;
                      }
                }
            }
          if (ysize > 1)
            {
              double *yvals = (double *) malloc((size_t) ysize * sizeof(double));
              for (size_t i = 0; i < ysize; ++i) yvals[i] = yvals2D[i * xsize];
              yinc = fabs(yvals[ysize - 1] - yvals[0]) / (ysize - 1);
              for (size_t i = 1; i < ysize; i++)
                if (fabs(fabs(yvals[i - 1] - yvals[i]) - yinc) > 0.01 * yinc)
                  {
                    yinc = 0;
                    break;
                  }
              free(yvals);
              if (IS_NOT_EQUAL(yinc, 0))
                {
                  for (size_t i = 1; i < xsize; i++)
                    if (IS_NOT_EQUAL(yvals2D[i], yvals2D[0])
                        || IS_NOT_EQUAL(yvals2D[(ysize - 1) * xsize + i], yvals2D[(ysize - 1) * xsize]))
                      {
                        yinc = 0;
                        break;
                      }
                }
            }
        }

      fprintf(stdout, "%33s : %.*g", xname, dig, xfirst);
      if (gridsize > 1) fprintf(stdout, " to %.*g", dig, xlast);
      if (IS_NOT_EQUAL(xinc, 0)) fprintf(stdout, " by %.*g", dig, xinc);
      fprintf(stdout, " %s", xunits);
      if (gridIsCircular(gridID)) fprintf(stdout, "  circular");
      fprintf(stdout, "\n");
      fprintf(stdout, "%33s : %.*g", yname, dig, yfirst);
      if (gridsize > 1) fprintf(stdout, " to %.*g", dig, ylast);
      if (IS_NOT_EQUAL(yinc, 0)) fprintf(stdout, " by %.*g", dig, yinc);
      fprintf(stdout, " %s", yunits);
      fprintf(stdout, "\n");

      free(xvals2D);
      free(yvals2D);
    }
}

static void
printGridNp(int gridtype, int gridID, size_t gridsize, size_t xsize, size_t ysize)
{

  size_t xysize = xsize * ysize;
  fprintf(stdout, "points=%zu", gridsize);
  if (gridtype == GRID_GAUSSIAN_REDUCED)
    fprintf(stdout, "  nlat=%zu", ysize);
  else if (xysize)
    fprintf(stdout, " (%zux%zu)", xsize, ysize);

  if (gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED) fprintf(stdout, "  np=%d", gridInqNP(gridID));
  my_reset_text_color(stdout);

  fprintf(stdout, "\n");
}

static void
printGridInfoKernel(int gridID, int index, bool lproj)
{
  int gridtype = gridInqType(gridID);

  if (lproj && gridtype != GRID_PROJECTION)
    fprintf(stderr, "Internal problem (%s): sub grid not equal GRID_PROJECTION!\n", __func__);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
319
320
321
322
  const int trunc = gridInqTrunc(gridID);
  const size_t gridsize = gridInqSize(gridID);
  const size_t xsize = gridInqXsize(gridID);
  const size_t ysize = gridInqYsize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

  // int prec     = gridInqDatatype(gridID);
  // int dig = (prec == CDI_DATATYPE_FLT64) ? 15 : 7;
  int dig = 7;
#ifdef CDO
  extern int CDO_flt_digits;
  dig = CDO_flt_digits;
#endif

  if (!lproj)
    {
      fprintf(stdout, "  %4d : ", index + 1);
#ifdef CDO
      set_text_color(stdout, RESET, BLUE);
#endif
      fprintf(stdout, "%-24s", gridNamePtr(gridtype));
      my_reset_text_color(stdout);
      fprintf(stdout, " : ");
    }

  if (gridtype == GRID_LONLAT || gridtype == GRID_PROJECTION || gridtype == GRID_GENERIC || gridtype == GRID_CHARXY
      || gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED)
    {
      if (!lproj)

        {
#ifdef CDO
          set_text_color(stdout, RESET, GREEN);
#endif
          printGridNp(gridtype, gridID, gridsize, xsize, ysize);
        }

      char name[CDI_MAX_NAME];
      name[0] = 0;
      cdiGridInqKeyStr(gridID, CDI_KEY_MAPNAME, CDI_MAX_NAME, name);
      if (gridtype == GRID_PROJECTION || name[0])
        {
          if (name[0] == 0) strcpy(name, "undefined");
#ifdef CDO
          set_text_color(stdout, RESET, BLUE);
#endif
          fprintf(stdout, "         %24s", "mapping");
          my_reset_text_color(stdout);
          fprintf(stdout, " : ");
#ifdef CDO
          set_text_color(stdout, RESET, GREEN);
#endif
          fprintf(stdout, "%s\n", name);
          my_reset_text_color(stdout);
        }

      print_xvals(gridID, dig);
      print_yvals(gridID, dig);

      if (gridInqXbounds(gridID, NULL) || gridInqYbounds(gridID, NULL))
        {
          fprintf(stdout, "%33s :", "available");
          if (gridInqXbounds(gridID, NULL) && gridInqYbounds(gridID, NULL)) fprintf(stdout, " cellbounds");
          if (gridHasArea(gridID)) fprintf(stdout, " area");
          if (gridInqMask(gridID, NULL)) fprintf(stdout, " mask");
          fprintf(stdout, "\n");
        }
    }
  else if (gridtype == GRID_SPECTRAL)
    {
#ifdef CDO
      set_text_color(stdout, RESET, GREEN);
#endif
      fprintf(stdout, "points=%zu  nsp=%zu  truncation=%d", gridsize, gridsize / 2, trunc);
      if (gridInqComplexPacking(gridID)) fprintf(stdout, "  complexPacking");
      my_reset_text_color(stdout);
      fprintf(stdout, "\n");
    }
  else if (gridtype == GRID_FOURIER)
    {
#ifdef CDO
      set_text_color(stdout, RESET, GREEN);
#endif
      fprintf(stdout, "points=%zu  nfc=%zu  truncation=%d\n", gridsize, gridsize / 2, trunc);
      my_reset_text_color(stdout);
    }
  else if (gridtype == GRID_GME)
    {
      int nd, ni, ni2, ni3;
      gridInqParamGME(gridID, &nd, &ni, &ni2, &ni3);
#ifdef CDO
      set_text_color(stdout, RESET, GREEN);
#endif
      fprintf(stdout, "points=%zu  nd=%d  ni=%d\n", gridsize, nd, ni);
      my_reset_text_color(stdout);
    }
  else if (gridtype == GRID_CURVILINEAR || gridtype == GRID_UNSTRUCTURED)
    {
#ifdef CDO
      set_text_color(stdout, RESET, GREEN);
#endif
      if (gridtype == GRID_CURVILINEAR)
        fprintf(stdout, "points=%zu (%zux%zu)", gridsize, xsize, ysize);
      else
        fprintf(stdout, "points=%zu", gridsize);

      if (gridtype == GRID_UNSTRUCTURED && gridInqNvertex(gridID) > 0) fprintf(stdout, "  nvertex=%d", gridInqNvertex(gridID));
      my_reset_text_color(stdout);

      fprintf(stdout, "\n");

      if (gridtype == GRID_UNSTRUCTURED)
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431
432
          const int number = gridInqNumber(gridID);
          const int position = gridInqPosition(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
          if (number > 0) fprintf(stdout, "%33s : number=%d  position=%d\n", "grid", number, position);

          if (gridInqReference(gridID, NULL))
            {
              char reference_link[8192];
              gridInqReference(gridID, reference_link);
              fprintf(stdout, "%33s : %s\n", "uri", reference_link);
            }
        }

      print_xyvals2D(gridID, dig);
    }
  else /* if ( gridtype == GRID_GENERIC ) */
    {
#ifdef CDO
      set_text_color(stdout, RESET, GREEN);
#endif
      if (ysize == 0)
        fprintf(stdout, "points=%zu\n", gridsize);
      else
        fprintf(stdout, "points=%zu (%zux%zu)\n", gridsize, xsize, ysize);
      my_reset_text_color(stdout);
    }

  if (gridtype == GRID_CURVILINEAR || gridtype == GRID_UNSTRUCTURED)
    {
      if (gridHasArea(gridID) || gridInqXbounds(gridID, NULL) || gridInqYbounds(gridID, NULL))
        {
          fprintf(stdout, "%33s :", "available");
          if (gridInqXbounds(gridID, NULL) && gridInqYbounds(gridID, NULL)) fprintf(stdout, " cellbounds");
          if (gridHasArea(gridID)) fprintf(stdout, " area");
          if (gridInqMask(gridID, NULL)) fprintf(stdout, " mask");
          fprintf(stdout, "\n");
        }
    }

  unsigned char uuidOfHGrid[CDI_UUID_SIZE];
  gridInqUUID(gridID, uuidOfHGrid);
  if (!cdiUUIDIsNull(uuidOfHGrid))
    {
      char uuidOfHGridStr[37];
      cdiUUID2Str(uuidOfHGrid, uuidOfHGridStr);
      if (uuidOfHGridStr[0] != 0 && strlen(uuidOfHGridStr) == 36)
        {
          fprintf(stdout, "%33s : %s\n", "uuid", uuidOfHGridStr);
        }
    }
}

void
printGridInfo(int vlistID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
  const int ngrids = vlistNgrids(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
487
  for (int index = 0; index < ngrids; index++)
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
      const int gridID = vlistGrid(vlistID, index);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
      printGridInfoKernel(gridID, index, false);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
      const int projID = gridInqProj(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491
492
493
494
495
      if (projID != CDI_UNDEFID) printGridInfoKernel(projID, index, true);
    }
}

static void
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
printZaxisBoundsInfo(const int zaxisID, const int dig, const int levelsize, const double zinc, const char *zunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
497
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
498
  fprintf(stdout, "%33s : ", "bounds");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499

Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
501
502
503
504
505
506
507
508
509
510
511
  double level1 = zaxisInqLbound(zaxisID, 0);
  double level2 = zaxisInqUbound(zaxisID, 0);
  fprintf(stdout, "%.*g-%.*g", dig, level1, dig, level2);
  if (levelsize > 1)
    {
      level1 = zaxisInqLbound(zaxisID, levelsize - 1);
      level2 = zaxisInqUbound(zaxisID, levelsize - 1);
      fprintf(stdout, " to %.*g-%.*g", dig, level1, dig, level2);
      if (IS_NOT_EQUAL(zinc, 0)) fprintf(stdout, " by %.*g", dig, zinc);
    }
  fprintf(stdout, " %s", zunits);
  fprintf(stdout, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
513
514
}

static void
Uwe Schulzweida's avatar
Uwe Schulzweida committed
515
printZaxisLevelInfo(const int levelsize, const int zaxisID, const int zaxistype, double zinc, const int dig, const char *zname, const char *zunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
516
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518
  double *levels = (double *) malloc((size_t) levelsize * sizeof(double));
  zaxisInqLevels(zaxisID, levels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519

Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
  if (!(zaxistype == ZAXIS_SURFACE && levelsize == 1 && fabs(levels[0]) <= 0))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
521
522
523
524
525
526
527
528
529
530
531
532
533
    {
      const double zfirst = levels[0];
      const double zlast = levels[levelsize - 1];
      if (levelsize > 2)
        {
          zinc = (levels[levelsize - 1] - levels[0]) / (levelsize - 1);
          for (int levelID = 2; levelID < levelsize; ++levelID)
            if (fabs(fabs(levels[levelID] - levels[levelID - 1]) - zinc) > 0.001 * zinc)
              {
                zinc = 0;
                break;
              }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
534

Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
537
538
539
540
541
542
543
      fprintf(stdout, "%33s : %.*g", zname, dig, zfirst);
      if (levelsize > 1)
        {
          fprintf(stdout, " to %.*g", dig, zlast);
          if (IS_NOT_EQUAL(zinc, 0)) fprintf(stdout, " by %.*g", dig, zinc);
        }
      fprintf(stdout, " %s", zunits);
      fprintf(stdout, "\n");
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
544

Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
  free(levels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
548
}

static void
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549
printZaxisHybridInfo(const int zaxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
550
551
552
553
{
  char psname[CDI_MAX_NAME];
  psname[0] = 0;
  cdiZaxisInqKeyStr(zaxisID, CDI_KEY_PSNAME, CDI_MAX_NAME, psname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
554
  const int vctsize = zaxisInqVctSize(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556
557
558
559
560
561
562
563
564
  if (vctsize || psname[0])
    {
      fprintf(stdout, "%33s :", "available");
      if (vctsize) fprintf(stdout, " vct");
      if (psname[0]) fprintf(stdout, "  ps: %s", psname);
      fprintf(stdout, "\n");
    }
}

static void
Uwe Schulzweida's avatar
Uwe Schulzweida committed
565
printZaxisGenericInfo(const int ltype, const int zaxistype, const char *zaxisname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566
567
568
569
570
571
572
573
574
575
576
577
{
  if (zaxistype == ZAXIS_GENERIC && ltype != 0)
    {
      fprintf(stdout, "%-12s (ltype=%3d)", zaxisname, ltype);
    }
  else
    {
      fprintf(stdout, "%-24s", zaxisname);
    }
}

static void
Uwe Schulzweida's avatar
Uwe Schulzweida committed
578
printZaxisReferenceInfo(const int zaxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
580
  const int number = zaxisInqNumber(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  if (number > 0)
    {
      fprintf(stdout, "%33s : ", "zaxis");
      fprintf(stdout, "number=%d\n", number);
    }

  unsigned char uuidOfVGrid[CDI_UUID_SIZE];
  zaxisInqUUID(zaxisID, uuidOfVGrid);
  if (!cdiUUIDIsNull(uuidOfVGrid))
    {
      char uuidOfVGridStr[37];
      cdiUUID2Str(uuidOfVGrid, uuidOfVGridStr);
      if (uuidOfVGridStr[0] != 0 && strlen(uuidOfVGridStr) == 36)
        {
          fprintf(stdout, "%33s : ", "uuid");
          fprintf(stdout, "%s\n", uuidOfVGridStr);
        }
    }
}

void
printZaxisInfo(int vlistID)
{
  char zaxisname[CDI_MAX_NAME], zname[CDI_MAX_NAME], zunits[CDI_MAX_NAME];

Uwe Schulzweida's avatar
Uwe Schulzweida committed
606
  const int nzaxis = vlistNzaxis(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
608
609
610
611
612
613
614
615
  for (int index = 0; index < nzaxis; index++)
    {
      int dig = 7;
#ifdef CDO
      extern int CDO_flt_digits;
      dig = CDO_flt_digits;
#endif
      //______________________________________________________--
      double zinc = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
617
618
619
      const int zaxisID = vlistZaxis(vlistID, index);
      const int zaxistype = zaxisInqType(zaxisID);
      const int ltype = zaxisInqLtype(zaxisID);
      const int levelsize = zaxisInqSize(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
      // int prec      = zaxisInqDatatype(zaxisID);
      // int dig = (prec == CDI_DATATYPE_FLT64) ? 15 : 7;

      zaxisName(zaxistype, zaxisname);
      zaxisInqName(zaxisID, zname);
      zaxisInqUnits(zaxisID, zunits);
      zunits[12] = 0;

      fprintf(stdout, "  %4d : ", vlistZaxisIndex(vlistID, zaxisID) + 1);
#ifdef CDO
      set_text_color(stdout, RESET, BLUE);
#endif
      printZaxisGenericInfo(ltype, zaxistype, zaxisname);

      my_reset_text_color(stdout);

      fprintf(stdout, " :");

#ifdef CDO
      set_text_color(stdout, RESET, GREEN);
#endif
      fprintf(stdout, " levels=%d", levelsize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
642
      const bool zscalar = (levelsize == 1) ? zaxisInqScalar(zaxisID) : false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
643
644
645
646
647
648
649
650
651
652
653
654
655
656
      if (zscalar) fprintf(stdout, "  scalar");
      my_reset_text_color(stdout);
      fprintf(stdout, "\n");

      if (zaxisInqLevels(zaxisID, NULL))
        {
          printZaxisLevelInfo(levelsize, zaxisID, zaxistype, zinc, dig, zname, zunits);
        }

      if (zaxisInqLbounds(zaxisID, NULL) && zaxisInqUbounds(zaxisID, NULL))
        {
          printZaxisBoundsInfo(zaxisID, dig, levelsize, zinc, zunits);
        }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
657
      if (zaxistype == ZAXIS_HYBRID) printZaxisHybridInfo(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
658

Uwe Schulzweida's avatar
Uwe Schulzweida committed
659
      if (zaxistype == ZAXIS_REFERENCE) printZaxisReferenceInfo(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
660
661
662
663
664
665
    }
}

void
printSubtypeInfo(int vlistID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
666
  const int nsubtypes = vlistNsubtypes(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
667
668
  for (int index = 0; index < nsubtypes; index++)
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
669
670
      const int subtypeID = vlistSubtype(vlistID, index);
      const int subtypesize = subtypeInqSize(subtypeID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
671
672
673
674
675
676
677
678
      // subtypePrint(subtypeID);
      fprintf(stdout, "  %4d : %-24s :", vlistSubtypeIndex(vlistID, subtypeID) + 1, "tiles");
      fprintf(stdout, " ntiles=%d", subtypesize);
      fprintf(stdout, "\n");
    }
}

static int
Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
printDateTime(int ntimeout, const int64_t vdate, const int vtime)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
{
  char vdatestr[32], vtimestr[32];

  if (ntimeout == 4)
    {
      ntimeout = 0;
      fprintf(stdout, "\n");
    }

  date2str(vdate, vdatestr, sizeof(vdatestr));
  time2str(vtime, vtimestr, sizeof(vtimestr));

  fprintf(stdout, " %s %s", vdatestr, vtimestr);

  return ++ntimeout;
}

#define NUM_TIMESTEP 60
#define MAX_DOTS 80

static int
printDot(int ndotout, int *nfact, int *ncout)
{
  // printf("ncout %d %d %d\n",*ncout, (*ncout)%(*nfact), *nfact);
  if ((*ncout) % (*nfact) == 0)
    {
      if (ndotout == MAX_DOTS)
        {
          *ncout = 0;
          ndotout = 0;
          fprintf(stdout, "\n   ");
          (*nfact) *= 10;
        }

      fprintf(stdout, ".");
      fflush(stdout);
      ndotout++;
    }

  (*ncout)++;

  return ndotout;
}

void
printTimesteps(int streamID, int taxisID, int verbose)
{
  int nrecs;
  struct datetime
  {
    int64_t vdate;
    int vtime;
    struct datetime *next;
  };
  struct datetime vdatetime[NUM_TIMESTEP];
  struct datetime *next_vdatetime = vdatetime;

  for (int i = 0; i < NUM_TIMESTEP - 1; ++i) vdatetime[i].next = &vdatetime[i + 1];
  vdatetime[NUM_TIMESTEP - 1].next = &vdatetime[0];

  int ntimeout = 0;
  int ndotout = 0;
  int nvdatetime = 0;
  int ncout = 0;
  int nfact = 1;
  int tsID = 0;

#ifdef CDO
  DateTimeList dtlist;
#endif
  while ((nrecs = streamInqTimestep(streamID, tsID)))
    {
#ifdef CDO
      dtlist.taxisInqTimestep(taxisID, 0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
755
      const int64_t vdate = dtlist.getVdate(0);
      const int vtime = dtlist.getVtime(0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
#else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
757
758
      const int64_t vdate = taxisInqVdate(taxisID);
      const int vtime = taxisInqVtime(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
#endif

      if (verbose || tsID < NUM_TIMESTEP)
        {
          ntimeout = printDateTime(ntimeout, vdate, vtime);
        }
      else
        {
          if (tsID == 2 * NUM_TIMESTEP) fprintf(stdout, "\n   ");
          if (tsID >= 2 * NUM_TIMESTEP) ndotout = printDot(ndotout, &nfact, &ncout);

          if (nvdatetime < NUM_TIMESTEP)
            {
              vdatetime[nvdatetime].vdate = vdate;
              vdatetime[nvdatetime].vtime = vtime;
              nvdatetime++;
            }
          else
            {
              next_vdatetime->vdate = vdate;
              next_vdatetime->vtime = vtime;
              next_vdatetime = next_vdatetime->next;
            }
        }

      tsID++;
    }

  if (nvdatetime)
    {
      fprintf(stdout, "\n");

      ntimeout = 0;
      int toff = 0;
      if (tsID > 2 * NUM_TIMESTEP)
        {
          toff = tsID % 4;
          if (toff > 0) toff = 4 - toff;
          for (int i = 0; i < toff; ++i) next_vdatetime = next_vdatetime->next;
        }
      for (int i = toff; i < nvdatetime; ++i)
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
801
802
          const int64_t vdate = next_vdatetime->vdate;
          const int vtime = next_vdatetime->vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
803
804
805
806
807
          ntimeout = printDateTime(ntimeout, vdate, vtime);
          next_vdatetime = next_vdatetime->next;
        }
    }
}