zaxis_print.cc 4.44 KB
Newer Older
1
2
3
4
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2020 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
6
7
8
9
10
11
12
13
14
15
16
  See COPYING file for copying and redistribution conditions.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/
17
#include <cdi.h>
18

19
#include "cdi_uuid.h"
20
#include "cdo_options.h"
21
#include "process_int.h"
22

23
void cdoPrintAttributes(FILE *fp, int cdiID, int varID, int nblanks);
24

25
static void
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
printDblsPrefixAutoBrk(FILE *fp, int dig, const char prefix[], int nbyte0, size_t n, const double vals[], size_t extbreak)
27
28
29
{
  fputs(prefix, fp);
  int nbyte = nbyte0;
30
  for (size_t i = 0; i < n; i++)
31
    {
32
      if (nbyte > 80 || (i && i == extbreak))
33
34
35
36
37
38
39
40
41
        {
          fprintf(fp, "\n%*s", nbyte0, "");
          nbyte = nbyte0;
        }
      nbyte += fprintf(fp, "%.*g ", dig, vals[i]);
    }
  fputs("\n", fp);
}

42
static void
43
zaxisPrintKernel(int zaxisID, FILE *fp)
44
45
{
  char attstr[CDI_MAX_NAME];
46
47
48
49
  const int type = zaxisInqType(zaxisID);
  const int nlevels = zaxisInqSize(zaxisID);
  const int prec = zaxisInqDatatype(zaxisID);
  const size_t nvals = (size_t) zaxisInqLevels(zaxisID, nullptr);
50

51
  const int dig = (prec == CDI_DATATYPE_FLT64) ? Options::CDO_dbl_digits : Options::CDO_flt_digits;
52
53
54
55

  fprintf(fp, "zaxistype = %s\n", zaxisNamePtr(type));
  fprintf(fp, "size      = %d\n", nlevels);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
  if (nlevels == 1 && zaxisInqScalar(zaxisID)) fprintf(fp, "scalar    = true\n");
57

58
59
  int length = CDI_MAX_NAME;
  cdiInqKeyString(zaxisID, CDI_GLOBAL, CDI_KEY_NAME, attstr, &length);
60
  if (attstr[0]) fprintf(fp, "name      = %s\n", attstr);
61
62
  length = CDI_MAX_NAME;
  cdiInqKeyString(zaxisID, CDI_GLOBAL, CDI_KEY_LONGNAME, attstr, &length);
63
  if (attstr[0]) fprintf(fp, "longname  = \"%s\"\n", attstr);
64
65
  length = CDI_MAX_NAME;
  cdiInqKeyString(zaxisID, CDI_GLOBAL, CDI_KEY_UNITS, attstr, &length);
66
  if (attstr[0]) fprintf(fp, "units     = \"%s\"\n", attstr);
67

68
69
  std::vector<double> vals;
  if (nvals) vals.resize(nvals);
70

71
72
  if (nvals)
    {
73
      zaxisInqLevels(zaxisID, vals.data());
74
      static const char prefix[] = "levels    = ";
75
      printDblsPrefixAutoBrk(fp, dig, prefix, (int) sizeof(prefix) - 1, nvals, vals.data(), 0);
76
    }
77
  else if (type == ZAXIS_CHAR)
Fabian Wachsmann's avatar
Fabian Wachsmann committed
78
    {
79
      const int clen = zaxisInqCLen(zaxisID);
80
      char **cvals = nullptr;
Fabian Wachsmann's avatar
Fabian Wachsmann committed
81
      zaxisInqCVals(zaxisID, &cvals);
82
      fprintf(fp, "levels    = \n");
83
      for (int i = 0; i < nlevels; i++)
Fabian Wachsmann's avatar
Fabian Wachsmann committed
84
        {
Fabian Wachsmann's avatar
Fabian Wachsmann committed
85
          fprintf(fp, "     [%2d] = %.*s\n", i, clen, cvals[i]);
86
          free(cvals[i]);
Fabian Wachsmann's avatar
Fabian Wachsmann committed
87
        }
88
      if (cvals) free(cvals);
Fabian Wachsmann's avatar
Fabian Wachsmann committed
89
    }
90

Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
  if (zaxisInqLbounds(zaxisID, nullptr) && zaxisInqUbounds(zaxisID, nullptr))
92
93
    {
      {
94
        zaxisInqLbounds(zaxisID, vals.data());
95
        static const char prefix[] = "lbounds   = ";
96
        printDblsPrefixAutoBrk(fp, dig, prefix, (int) sizeof(prefix) - 1, nvals, vals.data(), 0);
97
98
99
      }

      {
100
        zaxisInqUbounds(zaxisID, vals.data());
101
        static const char prefix[] = "ubounds   = ";
102
        printDblsPrefixAutoBrk(fp, dig, prefix, (int) sizeof(prefix) - 1, nvals, vals.data(), 0);
103
104
105
      }
    }

106
  if (type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF)
107
    {
108
      const int vctsize = zaxisInqVctSize(zaxisID);
109
      if (vctsize)
110
        {
111
          fprintf(fp, "vctsize   = %d\n", vctsize);
112
113
          std::vector<double> vct(vctsize);
          zaxisInqVct(zaxisID, vct.data());
114
          static const char prefix[] = "vct       = ";
115
          printDblsPrefixAutoBrk(fp, dig, prefix, (int) sizeof(prefix) - 1, vctsize, vct.data(), vctsize / 2);
116
117
118
        }
    }

119
  if (type == ZAXIS_REFERENCE)
120
121
    {
      unsigned char uuid[CDI_UUID_SIZE];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
      length = CDI_UUID_SIZE;
123
124
      memset(uuid, 0, length);
      cdiInqKeyBytes(zaxisID, CDI_GLOBAL, CDI_KEY_UUID, uuid, &length);
125
      if (!cdiUUIDIsNull(uuid))
126
127
128
        {
          char uuidStr[37];
          cdiUUID2Str(uuid, uuidStr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
          if (uuidStr[0] != 0 && strlen(uuidStr) == 36) fprintf(fp, "uuid      = %s\n", uuidStr);
130
131
        }
    }
132

133
  cdoPrintAttributes(fp, zaxisID, CDI_GLOBAL, 0);
134
135
}

136
void
137
cdoPrintZaxis(int zaxisID)
138
{
139
  zaxisPrintKernel(zaxisID, stdout);
140
}