Ydaypctl.cc 7.32 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) 2006 Brockmann Consult
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  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.
*/

/*
   This module contains the following operators:

      Ydaypctl   ydaypctl        Multi-year daily percentiles
*/

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

26
#include "cdo_options.h"
27
#include "functs.h"
28
#include "process_int.h"
29
#include "cdo_vlist.h"
30
#include "param_conversion.h"
31
#include "percentiles_hist.h"
32
#include "percentiles.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
35

constexpr int MAX_DAYS = 373;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36

37
int getmonthday(int64_t date);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38

39
40
void *
Ydaypctl(void *process)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
42
{
  int varID;
43
  int year, month, day;
44
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
  int levelID;
46
  size_t nmiss;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
49
50
  int64_t vdates1[MAX_DAYS], vdates2[MAX_DAYS];
  int vtimes1[MAX_DAYS];
  long nsets[MAX_DAYS];
  FieldVector2D vars1[MAX_DAYS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
  HistogramSet hsets[MAX_DAYS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52

53
  cdoInitialize(process);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
  cdoOperatorAdd("ydaypctl", func_pctl, 0, nullptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56

  operatorInputArg("percentile number");
57
  const auto pn = parameter2double(cdoOperatorArgv(0));
58
  percentile_check_number(pn);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59

Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
  for (int dayoy = 0; dayoy < MAX_DAYS; dayoy++) nsets[dayoy] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61

62
63
64
  const auto streamID1 = cdoOpenRead(0);
  const auto streamID2 = cdoOpenRead(1);
  const auto streamID3 = cdoOpenRead(2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65

Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
67
68
69
  const auto vlistID1 = cdoStreamInqVlist(streamID1);
  const auto vlistID2 = cdoStreamInqVlist(streamID2);
  const auto vlistID3 = cdoStreamInqVlist(streamID3);
  const auto vlistID4 = vlistDuplicate(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70

Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
72
  vlistCompare(vlistID1, vlistID2, CMP_ALL);
  vlistCompare(vlistID1, vlistID3, CMP_ALL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73

Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
76
  const auto taxisID1 = vlistInqTaxis(vlistID1);
  const auto taxisID2 = vlistInqTaxis(vlistID2);
  const auto taxisID3 = vlistInqTaxis(vlistID3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
78
  /* TODO - check that time axes 2 and 3 are equal */

79
  const int taxisID4 = taxisDuplicate(taxisID1);
80
  if (taxisHasBounds(taxisID4)) taxisDeleteBounds(taxisID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82
  vlistDefTaxis(vlistID4, taxisID4);

83
  const auto streamID4 = cdoOpenWrite(3);
84
  cdoDefVlist(streamID4, vlistID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
  const auto nvars = vlistNvars(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87

Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
  const auto maxrecs = vlistNrecs(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
  std::vector<RecordInfo> recList(maxrecs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90

91
  const auto gridsizemax = vlistGridsizeMax(vlistID1);
92

Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
  Field field;
94
  field.resize(gridsizemax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95

96
  int tsID = 0;
97
  while ((nrecs = cdoStreamInqTimestep(streamID2, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
    {
99
      if (nrecs != cdoStreamInqTimestep(streamID3, tsID))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
        cdoAbort("Number of records at time step %d of %s and %s differ!", tsID + 1, cdoGetStreamName(1), cdoGetStreamName(2));
101

102
      const auto vdate = taxisInqVdate(taxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
      const auto vtime = taxisInqVtime(taxisID2);
104
105

      if (vdate != taxisInqVdate(taxisID3))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
        cdoAbort("Verification dates at time step %d of %s and %s differ!", tsID + 1, cdoGetStreamName(1), cdoGetStreamName(2));
107

108
      if (Options::cdoVerbose) cdoPrint("process timestep: %d %d %d", tsID + 1, vdate, vtime);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109

Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
      cdiDecodeDate(vdate, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111

112
      const int dayoy = (month >= 1 && month <= 12) ? (month - 1) * 31 + day : 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
      if (dayoy < 0 || dayoy >= MAX_DAYS) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116

      vdates2[dayoy] = vdate;

117
      if (!vars1[dayoy].size())
118
        {
119
          fieldsFromVlist(vlistID1, vars1[dayoy], FIELD_VEC);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
          hsets[dayoy].create(nvars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121

122
123
          for (varID = 0; varID < nvars; varID++)
            {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125
126
              auto gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
              auto nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
              hsets[dayoy].createVarLevels(varID, nlevels, gridsize);
127
128
129
130
            }
        }

      for (int recID = 0; recID < nrecs; recID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
        {
132
          cdoInqRecord(streamID2, &varID, &levelID);
133
          cdoReadRecord(streamID2, vars1[dayoy][varID][levelID].vec.data(), &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
          vars1[dayoy][varID][levelID].nmiss = nmiss;
        }
136

137
      for (int recID = 0; recID < nrecs; recID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
        {
139
          cdoInqRecord(streamID3, &varID, &levelID);
140
          cdoReadRecord(streamID3, field.vec.data(), &field.nmiss);
141
142
143
          field.grid = vars1[dayoy][varID][levelID].grid;
          field.missval = vars1[dayoy][varID][levelID].missval;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
          hsets[dayoy].defVarLevelBounds(varID, levelID, vars1[dayoy][varID][levelID], field);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
        }
146

Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
148
      tsID++;
    }
149

Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
  tsID = 0;
151
  while ((nrecs = cdoStreamInqTimestep(streamID1, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
    {
153
      const auto vdate = taxisInqVdate(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
      const auto vtime = taxisInqVtime(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155

156
      if (Options::cdoVerbose) cdoPrint("process timestep: %d %d %d", tsID + 1, vdate, vtime);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157

Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
      cdiDecodeDate(vdate, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159

160
      const int dayoy = (month >= 1 && month <= 12) ? (month - 1) * 31 + day : 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
      if (dayoy < 0 || dayoy >= MAX_DAYS) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
163
164

      vdates1[dayoy] = vdate;
      vtimes1[dayoy] = vtime;
165

Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
      if (!vars1[dayoy].size()) cdoAbort("No data for day %d in %s and %s", dayoy, cdoGetStreamName(1), cdoGetStreamName(2));
167
168
169

      for (int recID = 0; recID < nrecs; recID++)
        {
170
          cdoInqRecord(streamID1, &varID, &levelID);
171
172
173

          if (tsID == 0)
            {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
175
176
              recList[recID].varID = varID;
              recList[recID].levelID = levelID;
              recList[recID].lconst = vlistInqVarTimetype(vlistID1, varID) == TIME_CONSTANT;
177
178
            }

179
          cdoReadRecord(streamID1, vars1[dayoy][varID][levelID].vec.data(), &nmiss);
180
181
          vars1[dayoy][varID][levelID].nmiss = nmiss;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
          hsets[dayoy].addVarLevelValues(varID, levelID, vars1[dayoy][varID][levelID]);
183
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
185
186
187
188

      nsets[dayoy]++;
      tsID++;
    }

189
  int otsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
  for (int dayoy = 0; dayoy < MAX_DAYS; dayoy++)
191
    if (nsets[dayoy])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
      {
193
        if (getmonthday(vdates1[dayoy]) != getmonthday(vdates2[dayoy]))
194
195
          cdoAbort("Verification dates for the day %d of %s and %s are different!", dayoy, cdoGetStreamName(0),
                   cdoGetStreamName(1));
196
197
198
199
200

        for (varID = 0; varID < nvars; varID++)
          {
            if (vlistInqVarTimetype(vlistID1, varID) == TIME_CONSTANT) continue;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
            const auto nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
202
            for (levelID = 0; levelID < nlevels; levelID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
              hsets[dayoy].getVarLevelPercentiles(vars1[dayoy][varID][levelID], varID, levelID, pn);
204
205
206
207
          }

        taxisDefVdate(taxisID4, vdates1[dayoy]);
        taxisDefVtime(taxisID4, vtimes1[dayoy]);
208
        cdoDefTimestep(streamID4, otsID);
209

210
        for (int recID = 0; recID < maxrecs; recID++)
211
          {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
            if (otsID && recList[recID].lconst) continue;
213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
215
            const auto varID = recList[recID].varID;
            const auto levelID = recList[recID].levelID;
216
            cdoDefRecord(streamID4, varID, levelID);
217
            cdoWriteRecord(streamID4, vars1[dayoy][varID][levelID].vec.data(), vars1[dayoy][varID][levelID].nmiss);
218
219
220
          }

        otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
      }

223
224
225
226
  cdoStreamClose(streamID4);
  cdoStreamClose(streamID3);
  cdoStreamClose(streamID2);
  cdoStreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
228
229

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
  return nullptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
231
}