Vertintml.cc 21.5 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  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.
*/

18
19
20
21
22
23
24
/*
   This module contains the following operators:

      Vertint    ml2pl           Model to pressure level interpolation
      Vertint    ml2hl           Model to height level interpolation
*/

Ralf Mueller's avatar
Ralf Mueller committed
25
#include <cdi.h>
Oliver Heidmann's avatar
Oliver Heidmann committed
26

27
#include "cdo_options.h"
28
#include "process_int.h"
29
#include "cdo_vlist.h"
30
#include "field_vinterp.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
#include "stdnametable.h"
32
#include "constants.h"
33
#include "util_string.h"
34
#include "const.h"
35
#include "cdo_zaxis.h"
36
#include "param_conversion.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37

38
39
static bool
zaxis_is_hybrid(int zaxistype)
40
41
42
43
{
  return (zaxistype == ZAXIS_HYBRID || zaxistype == ZAXIS_HYBRID_HALF);
}

44
static void
45
change_hybrid_zaxis(int vlistID1, int vlistID2, int nvct, double *vct, int zaxisID2, int nhlevf, int nhlevh)
46
{
47
  const auto nzaxis = vlistNzaxis(vlistID1);
48
  for (int iz = 0; iz < nzaxis; ++iz)
49
    {
50
51
52
      const auto zaxisID = vlistZaxis(vlistID1, iz);
      const auto nlevel = zaxisInqSize(zaxisID);
      const auto zaxistype = zaxisInqType(zaxisID);
53

54
      if (zaxis_is_hybrid(zaxistype) && (nlevel == nhlevh || nlevel == nhlevf))
55
        {
56
          const auto nvct2 = zaxisInqVctSize(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
          if (nvct2 == nvct && memcmp(vct, zaxisInqVctPtr(zaxisID), nvct * sizeof(double)) == 0)
58
            vlistChangeZaxisIndex(vlistID2, iz, zaxisID2);
59
        }
60
61
62
    }
}

63
64
void *
Vertintml(void *process)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
{
66
  ModelMode mode(ModelMode::UNDEF);
67
68
69
70
71
72
73
74
75
76
  enum
  {
    func_pl,
    func_hl
  };
  enum
  {
    type_lin,
    type_log
  };
77
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
  int varID, levelID;
79
80
  bool sgeopot_needed = false;
  bool extrapolate = false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
  int sgeopotID = -1, geopotID = -1, tempID = -1, psID = -1, lnpsID = -1, gheightID = -1;
82
  char paramstr[32];
83
  char varname[CDI_MAX_NAME], stdname[CDI_MAX_NAME];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
85
  gribcode_t gribcodes;
  memset(&gribcodes, 0, sizeof(gribcode_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86

87
  cdoInitialize(process);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88

89
  // clang-format off
90
91
92
93
94
95
96
97
  const auto ML2PL     = cdoOperatorAdd("ml2pl",     func_pl, type_lin, "pressure levels in pascal");
  const auto ML2PLX    = cdoOperatorAdd("ml2plx",    func_pl, type_lin, "pressure levels in pascal");
  const auto ML2HL     = cdoOperatorAdd("ml2hl",     func_hl, type_lin, "height levels in meter");
  const auto ML2HLX    = cdoOperatorAdd("ml2hlx",    func_hl, type_lin, "height levels in meter");
  const auto ML2PL_LP  = cdoOperatorAdd("ml2pl_lp",  func_pl, type_log, "pressure levels in pascal");
  const auto ML2PLX_LP = cdoOperatorAdd("ml2plx_lp", func_pl, type_log, "pressure levels in pascal");
  const auto ML2HL_LP  = cdoOperatorAdd("ml2hl_lp",  func_hl, type_log, "height levels in meter");
  const auto ML2HLX_LP = cdoOperatorAdd("ml2hlx_lp", func_hl, type_log, "height levels in meter");
98
  // clang-format on
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99

100
  const auto operatorID = cdoOperatorID();
101
102
  const bool useHightLevel = cdoOperatorF1(operatorID) == func_hl;
  const bool useLogType = cdoOperatorF2(operatorID) == type_log;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103

Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
  if (operatorID == ML2PL || operatorID == ML2HL || operatorID == ML2PL_LP || operatorID == ML2HL_LP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
    {
106
      const char *envstr = getenv("EXTRAPOLATE");
107
108
109
110
111
      if (envstr && isdigit((int) envstr[0]))
        {
          if (atoi(envstr) == 1) extrapolate = true;
          if (extrapolate) cdoPrint("Extrapolation of missing values enabled!");
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
  else if (operatorID == ML2PLX || operatorID == ML2HLX || operatorID == ML2PLX_LP || operatorID == ML2HLX_LP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
    {
115
      extrapolate = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
119

  operatorInputArg(cdoOperatorEnter(operatorID));

120
  Varray<double> plev;
121
  if (operatorArgc() == 1 && cdoOperatorArgv(0).compare("default") == 0)
122
    {
123
      if (useHightLevel)
124
        plev = { 10, 50, 100, 500, 1000, 5000, 10000, 15000, 20000, 25000, 30000 };
125
      else
126
        plev = { 100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000 };
127
128
129
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
      plev = cdoArgvToFlt(cdoGetOperArgv());
131
    }
132

133
134
  int nplev = plev.size();

135
  const auto streamID1 = cdoOpenRead(0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136

137
138
  const auto vlistID1 = cdoStreamInqVlist(streamID1);
  const auto vlistID2 = vlistDuplicate(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139

140
141
  const auto taxisID1 = vlistInqTaxis(vlistID1);
  const auto taxisID2 = taxisDuplicate(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
  vlistDefTaxis(vlistID2, taxisID2);

144
  const auto gridsize = vlist_check_gridsize(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145

146
  const int zaxistype = useHightLevel ? ZAXIS_HEIGHT : ZAXIS_PRESSURE;
147
  const auto zaxisIDp = zaxisCreate(zaxistype, nplev);
148
  zaxisDefLevels(zaxisIDp, plev.data());
149

150
151
152
  int nvct = 0;
  int zaxisIDh = -1;
  int nhlev = 0, nhlevf = 0, nhlevh = 0;
153
154
  Varray<double> vct;
  vlist_read_vct(vlistID1, &zaxisIDh, &nvct, &nhlev, &nhlevf, &nhlevh, vct);
155

156
  change_hybrid_zaxis(vlistID1, vlistID2, nvct, vct.data(), zaxisIDp, nhlevf, nhlevh);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157

158
  int psvarID = -1;
159
  bool linvertvct = false;
160
  if (!vct.empty() && nvct && nvct % 2 == 0)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
    {
162
      psvarID = vlist_get_psvarid(vlistID1, zaxisIDh);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163

164
      int i;
165
166
167
      for (i = nvct / 2 + 1; i < nvct; i++)
        if (vct[i] > vct[i - 1]) break;
      if (i == nvct) linvertvct = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
169
    }

170
  if (Options::cdoVerbose) cdoPrint("linvertvct = %d", (int) linvertvct);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171

172
  if (linvertvct)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
    {
174
      Varray<double> vctbuf(nvct);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
      for (int i = 0; i < nvct; ++i) vctbuf[i] = vct[i];
176
      for (int i = 0; i < nvct / 2; i++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
        {
178
179
          vct[nvct / 2 - 1 - i] = vctbuf[i];
          vct[nvct - 1 - i] = vctbuf[i + nvct / 2];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
181
182
        }
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
184
185
  VarList varList1, varList2;
  varListInit(varList1, vlistID1);
  varListInit(varList2, vlistID2);
186
187
  varListSetUniqueMemtype(varList1);
  const auto memtype = varList1[0].memType;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188

189
  const auto nvars = vlistNvars(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190

191
  std::vector<bool> vars(nvars), varinterp(nvars);
192
  std::vector<std::vector<size_t>> varnmiss(nvars);
193
  Field3DVector vardata1(nvars), vardata2(nvars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194

195
  const int maxlev = nhlevh > nplev ? nhlevh : nplev;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196

197
  std::vector<size_t> pnmiss;
198
  if (!extrapolate) pnmiss.resize(nplev);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199

Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
  // check levels
201
  if (zaxisIDh != -1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
    {
203
      const auto nlev = zaxisInqSize(zaxisIDh);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
      if (nlev != nhlev) cdoAbort("Internal error, wrong number of hybrid level!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
    }

207
  std::vector<int> vert_index;
208
209

  Field3D full_press, half_press;
210
  if (zaxisIDh != -1 && gridsize > 0)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
    {
212
      vert_index.resize(gridsize * nplev);
213
214
215
216
217
218
219
220
221
222
223
224

      CdoVar var3Df, var3Dh;

      var3Df.gridsize = gridsize;
      var3Df.nlevels = nhlevf;
      var3Df.memType = memtype;
      full_press.init(var3Df);

      var3Dh.gridsize = gridsize;
      var3Dh.nlevels = nhlevh;
      var3Dh.memType = memtype;
      half_press.init(var3Dh);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
226
    }
  else
227
    cdoWarning("No 3D variable with hybrid sigma pressure coordinate found!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228

229
  if (useHightLevel)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
    {
231
      Varray<double> phlev(nplev);
232
      height2pressure(&phlev[0], plev.data(), nplev);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233

234
      if (Options::cdoVerbose)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
        for (int i = 0; i < nplev; ++i) cdoPrint("level = %d   height = %g   pressure = %g", i + 1, plev[i], phlev[i]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236

237
      plev = phlev;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
239
    }

240
  if (useLogType)
241
    for (int k = 0; k < nplev; k++) plev[k] = std::log(plev[k]);
242

243
  bool useTable = false;
244
  for (varID = 0; varID < nvars; varID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
    {
246
      const int tableNum = tableInqNum(vlistInqVarTable(vlistID1, varID));
247
248
249
250
251
      if (tableNum > 0 && tableNum != 255)
        {
          useTable = true;
          break;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
253
    }

254
  if (Options::cdoVerbose && useTable) cdoPrint("Using code tables!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255

256
  for (varID = 0; varID < nvars; varID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
    {
258
259
260
      const auto gridID = varList1[varID].gridID;
      const auto zaxisID = varList1[varID].zaxisID;
      const auto zaxistype = zaxisInqType(zaxisID);
261
      const auto nlevels = varList1[varID].nlevels;
262
263
      const auto instNum = institutInqCenter(vlistInqVarInstitut(vlistID1, varID));
      const auto tableNum = tableInqNum(vlistInqVarTable(vlistID1, varID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264

265
      auto code = vlistInqVarCode(vlistID1, varID);
266

267
      const auto param = vlistInqVarParam(vlistID1, varID);
268
      cdiParamToString(param, paramstr, sizeof(paramstr));
269
      int pnum, pcat, pdis;
270
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
      if (pdis >= 0 && pdis < 255) code = -1;

      if (useTable)
        {
          if (tableNum == 2)
            {
              mode = ModelMode::WMO;
              wmo_gribcodes(&gribcodes);
            }
          else if (tableNum == 128 || tableNum == 0)
            {
              mode = ModelMode::ECHAM;
              echam_gribcodes(&gribcodes);
            }
          //  KNMI: HIRLAM model version 7.2 uses tableNum=1    (LAMH_D11*)
286
287
          //  KNMI: HARMONIE model version 36 uses tableNum=1   (grib*) (opreational NWP version) 
          //  KNMI: HARMONIE model version 38 uses tableNum=253 (grib,grib_md) and tableNum=1 (grib_sfx) (research version)
288
289
290
291
292
293
          else if (tableNum == 1 || tableNum == 253)
            {
              mode = ModelMode::HIRLAM;
              hirlam_harmonie_gribcodes(&gribcodes);
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
      else
295
296
297
298
299
        {
          mode = ModelMode::ECHAM;
          echam_gribcodes(&gribcodes);
        }

300
      if (Options::cdoVerbose)
301
302
        {
          vlistInqVarName(vlistID1, varID, varname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
304
          cdoPrint("Mode=%d  Center=%d  TableNum=%d  Code=%d  Param=%s  Varname=%s  varID=%d", mode, instNum, tableNum, code,
                   paramstr, varname, varID);
305
        }
306

307
      if (code <= 0 || code == 255)
308
309
        {
          vlistInqVarName(vlistID1, varID, varname);
310
          cstrToLowerCase(varname);
311
312

          vlistInqVarStdname(vlistID1, varID, stdname);
313
          cstrToLowerCase(stdname);
314
315
316
317

          code = echamcode_from_stdname(stdname);
          if (code == -1)
            {
318
              //                                       ECHAM                         ECMWF
319
              // clang-format off
320
              if      (sgeopotID == -1 && (cstrIsEqual(varname, "geosp") || cstrIsEqual(varname, "z"))) code = gribcodes.geopot;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321
322
323
324
              else if (tempID == -1    && (cstrIsEqual(varname, "st") || cstrIsEqual(varname, "t"))) code = gribcodes.temp;
              else if (psID == -1      && (cstrIsEqual(varname, "aps") || cstrIsEqual(varname, "sp"))) code = gribcodes.ps;
              else if (lnpsID == -1    && (cstrIsEqual(varname, "lsp") || cstrIsEqual(varname, "lnsp"))) code = gribcodes.lsp;
              else if (geopotID == -1  && cstrIsEqual(stdname, "geopotential_full")) code = gribcodes.geopot;
325
              // else if (cstrIsEqual(varname, "geopoth")) code = 156;
326
              // clang-format on
327
328
329
330
331
            }
        }

      if (mode == ModelMode::ECHAM)
        {
332
          // clang-format off
333
334
335
336
337
338
          if      (code == gribcodes.geopot  && nlevels == 1) sgeopotID = varID;
          else if (code == gribcodes.geopot  && nlevels == nhlevf) geopotID = varID;
          else if (code == gribcodes.temp    && nlevels == nhlevf) tempID = varID;
          else if (code == gribcodes.ps      && nlevels == 1) psID = varID;
          else if (code == gribcodes.lsp     && nlevels == 1) lnpsID = varID;
          else if (code == gribcodes.gheight && nlevels == nhlevf) gheightID = varID;
339
          // clang-format on
340
341
342
        }
      else if (mode == ModelMode::WMO || mode == ModelMode::HIRLAM)
        {
343
          // clang-format off
344
345
346
347
          if      (code == gribcodes.geopot && nlevels == 1) sgeopotID = varID;
          else if (code == gribcodes.geopot && nlevels == nhlevf) geopotID = varID;
          else if (code == gribcodes.temp   && nlevels == nhlevf) tempID = varID;
          else if (code == gribcodes.ps     && nlevels == 1) psID = varID;
348
          // clang-format on
349
        }
350

Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
      if (gridInqType(gridID) == GRID_SPECTRAL && zaxis_is_hybrid(zaxistype)) cdoAbort("Spectral data on model level unsupported!");
352

Uwe Schulzweida's avatar
Uwe Schulzweida committed
353
      if (gridInqType(gridID) == GRID_SPECTRAL) cdoAbort("Spectral data unsupported!");
354

355
356
      if (varID == gheightID) varList1[varID].nlevels = nlevels + 1;
      vardata1[varID].init(varList1[varID]);
357

358
      // varinterp[varID] = ( zaxis_is_hybrid(zaxistype) && zaxisIDh != -1 && nlevels == nhlev );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
      varinterp[varID]
360
          = (zaxisID == zaxisIDh || (zaxis_is_hybrid(zaxistype) && zaxisIDh != -1 && (nlevels == nhlevh || nlevels == nhlevf)));
361

362
      if (varinterp[varID])
363
        {
364
          varnmiss[varID].resize(maxlev, 0);
365
          vardata2[varID].init(varList2[varID]);
366
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
      else
368
        {
369
          if (zaxis_is_hybrid(zaxistype) && zaxisIDh != -1 && nlevels > 1)
370
371
            {
              vlistInqVarName(vlistID1, varID, varname);
372
              cdoWarning("Parameter %d has wrong number of levels, skipped! (param=%s nlevel=%d)", varID + 1, varname, nlevels);
373
374
            }

375
          varnmiss[varID].resize(nlevels);
376
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
378
    }

379
  if (Options::cdoVerbose)
380
381
    {
      cdoPrint("Found:");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382
383
      if (tempID != -1) cdoPrint("  %s -> %s", var_stdname(air_temperature), cdoVlistInqVarName(vlistID1, tempID, varname));
      if (psID != -1) cdoPrint("  %s -> %s", var_stdname(surface_air_pressure), cdoVlistInqVarName(vlistID1, psID, varname));
384
      if (lnpsID != -1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
385
        cdoPrint("  LOG(%s) -> %s", var_stdname(surface_air_pressure), cdoVlistInqVarName(vlistID1, lnpsID, varname));
386
      if (sgeopotID != -1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
        cdoPrint("  %s -> %s", var_stdname(surface_geopotential), cdoVlistInqVarName(vlistID1, sgeopotID, varname));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
      if (geopotID != -1) cdoPrint("  %s -> %s", var_stdname(geopotential), cdoVlistInqVarName(vlistID1, geopotID, varname));
389
      if (gheightID != -1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
        cdoPrint("  %s -> %s", var_stdname(geopotential_height), cdoVlistInqVarName(vlistID1, gheightID, varname));
391
392
    }

393
  if (tempID != -1 || gheightID != -1) sgeopot_needed = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394

395
  if (zaxisIDh != -1 && gheightID != -1 && tempID == -1)
396
    cdoAbort("Temperature not found, needed for vertical interpolation of geopotheight!");
397

398
  int presID = lnpsID;
399
  if (psvarID != -1) presID = psvarID;
400

401
  if (zaxisIDh != -1 && presID == -1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
    {
403
404
      if (psID == -1)
        cdoAbort("%s not found!", var_stdname(surface_air_pressure));
405
      else
406
        presID = psID;
407
408
    }

409
  if (Options::cdoVerbose)
410
    {
411
      if (presID == lnpsID)
412
        cdoPrint("using LOG(%s) from %s", var_stdname(surface_air_pressure), varList1[presID].name);
413
      else
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
        cdoPrint("using %s from %s", var_stdname(surface_air_pressure), varList1[presID].name);
    }

  Field ps_prog;
  ps_prog.init(varList1[presID]);

  Field sgeopot;
  if (zaxisIDh != -1 && sgeopot_needed)
    {
      sgeopot.init(varList1[presID]);
      if (sgeopotID == -1)
        {
          if (extrapolate)
            {
              if (geopotID == -1)
                cdoWarning("%s not found - set to zero!", var_stdname(surface_geopotential));
              else
                cdoPrint("%s not found - using bottom layer of %s!", var_stdname(surface_geopotential), var_stdname(geopotential));
            }
          fieldFill(sgeopot, 0.0);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
435
436
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
437
  // check VCT
438
  if (zaxisIDh != -1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
440
    {
      double suma = 0, sumb = 0;
441
442
      for (int i = 0; i < nhlevh; ++i) suma += vct[i];
      for (int i = 0; i < nhlevh; ++i) sumb += vct[i + nhlevh];
443
      if (!(suma > 0 || sumb > 0)) cdoWarning("VCT is empty!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
445
    }

446
  const auto streamID2 = cdoOpenWrite(1);
447
  cdoDefVlist(streamID2, vlistID2);
448

Uwe Schulzweida's avatar
Uwe Schulzweida committed
449
  int tsID = 0;
450
  while ((nrecs = cdoStreamInqTimestep(streamID1, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
451
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
      for (varID = 0; varID < nvars; ++varID) vars[varID] = false;
453

Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
      taxisCopyTimestep(taxisID2, taxisID1);
455
      cdoDefTimestep(streamID2, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456

457
458
      for (int recID = 0; recID < nrecs; recID++)
        {
459
          cdoInqRecord(streamID1, &varID, &levelID);
460

461
          const auto zaxisID = varList1[varID].zaxisID;
462
463
          const auto nlevels = varList1[varID].nlevels;
          if (linvertvct && zaxisIDh != -1 && zaxisID == zaxisIDh) levelID = nlevels - 1 - levelID;
464

465
          cdoReadRecord(streamID1, vardata1[varID], levelID, &varnmiss[varID][levelID]);
466
467
468
469
470
471
472
473
474

          vars[varID] = true;
        }

      if (zaxisIDh != -1)
        {
          if (sgeopot_needed)
            {
              if (sgeopotID != -1)
475
                fieldCopy(vardata1[sgeopotID], sgeopot);
476
              else if (geopotID != -1)
477
                fieldCopy(vardata1[geopotID], nhlevf - 1, sgeopot);
478

479
              // check range of surface geopot
480
481
              if (extrapolate && (sgeopotID != -1 || geopotID != -1))
                {
482
                  const auto minmax = fieldMinMax(sgeopot);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
                  if (minmax.first < MIN_FIS || minmax.second > MAX_FIS)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
                    cdoWarning("Surface geopotential out of range (min=%g max=%g) [timestep:%d]!", minmax.first, minmax.second,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
                               tsID + 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
487
488
                  if (gridsize > 1 && minmax.first >= 0 && minmax.second <= 9000 && IS_NOT_EQUAL(minmax.first, minmax.second))
                    cdoWarning("Surface geopotential has an unexpected range (min=%g max=%g) [timestep:%d]!", minmax.first,
                               minmax.second, tsID + 1);
489
490
491
492
                }
            }

          if (presID == lnpsID)
493
494
495
496
497
498
            {
              if (memtype == MemType::Float)
                for (size_t i = 0; i < gridsize; i++) ps_prog.vec_f[i] = std::exp(vardata1[lnpsID].vec_f[i]);
              else
                for (size_t i = 0; i < gridsize; i++) ps_prog.vec_d[i] = std::exp(vardata1[lnpsID].vec_d[i]);
            }
499
          else if (presID != -1)
500
            fieldCopy(vardata1[presID], ps_prog);
501

502
          // check range of ps_prog
503
          const auto minmax = fieldMinMax(ps_prog);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
505
          if (minmax.first < MIN_PS || minmax.second > MAX_PS)
            cdoWarning("Surface pressure out of range (min=%g max=%g) [timestep:%d]!", minmax.first, minmax.second, tsID + 1);
506

507
508
509
510
          if (memtype == MemType::Float)
            presh(full_press.vec_f.data(), half_press.vec_f.data(), vct.data(), ps_prog.vec_f.data(), nhlevf, gridsize);
          else
            presh(full_press.vec_d.data(), half_press.vec_d.data(), vct.data(), ps_prog.vec_d.data(), nhlevf, gridsize);
511
512
513

          if (useLogType)
            {
514
515
516
517
518
519
520
521
522
523
524
525
              if (memtype == MemType::Float)
                {
                  for (size_t i = 0; i < gridsize; i++) ps_prog.vec_f[i] = std::log(ps_prog.vec_f[i]);
                  for (size_t ki = 0; ki < nhlevh * gridsize; ki++) half_press.vec_f[ki] = std::log(half_press.vec_f[ki]);
                  for (size_t ki = 0; ki < nhlevf * gridsize; ki++) full_press.vec_f[ki] = std::log(full_press.vec_f[ki]);
                }
              else
                {
                  for (size_t i = 0; i < gridsize; i++) ps_prog.vec_d[i] = std::log(ps_prog.vec_d[i]);
                  for (size_t ki = 0; ki < nhlevh * gridsize; ki++) half_press.vec_d[ki] = std::log(half_press.vec_d[ki]);
                  for (size_t ki = 0; ki < nhlevf * gridsize; ki++) full_press.vec_d[ki] = std::log(full_press.vec_d[ki]);
                }
526
527
            }

528
          genind(vert_index, plev, full_press, gridsize);
529

530
          if (!extrapolate) genindmiss(vert_index, plev, gridsize, ps_prog, pnmiss);
531
532
533
534
535
536
537
538
        }

      for (varID = 0; varID < nvars; varID++)
        {
          if (vars[varID])
            {
              if (varinterp[varID])
                {
539
540
541
                  const auto nlevels = varList1[varID].nlevels;
                  if (nlevels != nhlevf && nlevels != nhlevh)
                    cdoAbort("Number of hybrid level differ from full/half level (param=%s)!", varList1[varID].name);
542

543
                  for (levelID = 0; levelID < nlevels; levelID++)
544
                    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
                      if (varnmiss[varID][levelID]) cdoAbort("Missing values unsupported for this operator!");
546
547
548
549
                    }

                  if (varID == tempID)
                    {
550
                      if (nlevels == nhlevh) cdoAbort("Temperature on half level unsupported!");
551

Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
                      if (useLogType && extrapolate) cdoAbort("Log. extrapolation of temperature unsupported!");
553

554
555
                      vertical_interp_T(nlevels, full_press, half_press, vardata1[varID], vardata2[varID], sgeopot,
                                        vert_index, plev, gridsize);
556
557
558
                    }
                  else if (varID == gheightID)
                    {
559
560
561
562
                      if (memtype == MemType::Float)
                        for (size_t i = 0; i < gridsize; ++i) vardata1[varID].vec_f[gridsize * nlevels + i] = sgeopot.vec_f[i] / PlanetGrav;
                      else
                        for (size_t i = 0; i < gridsize; ++i) vardata1[varID].vec_d[gridsize * nlevels + i] = sgeopot.vec_d[i] / PlanetGrav;
563

564
565
                      vertical_interp_Z(nlevels, full_press, half_press, vardata1[varID], vardata2[varID], vardata1[tempID], sgeopot,
                                        vert_index, plev, gridsize);
566
567
568
                    }
                  else
                    {
569
                      vertical_interp_X(nlevels, full_press, half_press, vardata1[varID], vardata2[varID], vert_index, plev, gridsize);
570
571
                    }

572
                  if (!extrapolate) varrayCopy(nplev, pnmiss, varnmiss[varID]);
573
574
                }

575
576
              const auto nlevels = varList2[varID].nlevels;
              for (levelID = 0; levelID < nlevels; levelID++)
577
                {
578
                  cdoDefRecord(streamID2, varID, levelID);
579
                  cdoWriteRecord(streamID2, varinterp[varID] ? vardata2[varID] : vardata1[varID], levelID, varnmiss[varID][levelID]);
580
581
582
                }
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
583
584
585
586

      tsID++;
    }

587
588
  cdoStreamClose(streamID2);
  cdoStreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
589
590
591

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
592
  return nullptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
593
}