Ydaypctl.cc 7.49 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_int.h"
27
#include "cdo_options.h"
28
#include "functs.h"
29
#include "process_int.h"
30
#include "cdo_vlist.h"
31
#include "param_conversion.h"
32

33
#include "percentiles_hist.h"
34
#include "percentiles.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35

36
#define NDAY 373
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37

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

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

56
  cdoInitialize(process);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
  cdoOperatorAdd("ydaypctl", func_pctl, 0, nullptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59

  operatorInputArg("percentile number");
60
  const double pn = parameter2double(operatorArgv()[0]);
61
  percentile_check_number(pn);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62

63
  for (int dayoy = 0; dayoy < NDAY; dayoy++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
      hsets[dayoy] = nullptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
67
68
      nsets[dayoy] = 0;
    }

69
70
71
  const auto streamID1 = cdoOpenRead(0);
  const auto streamID2 = cdoOpenRead(1);
  const auto streamID3 = cdoOpenRead(2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72

73
74
75
76
  const int vlistID1 = cdoStreamInqVlist(streamID1);
  const int vlistID2 = cdoStreamInqVlist(streamID2);
  const int vlistID3 = cdoStreamInqVlist(streamID3);
  const int vlistID4 = vlistDuplicate(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77

Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
  vlistCompare(vlistID1, vlistID2, CMP_ALL);
  vlistCompare(vlistID1, vlistID3, CMP_ALL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80

81
82
83
  const int taxisID1 = vlistInqTaxis(vlistID1);
  const int taxisID2 = vlistInqTaxis(vlistID2);
  const int taxisID3 = vlistInqTaxis(vlistID3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
85
  /* TODO - check that time axes 2 and 3 are equal */

86
  const int taxisID4 = taxisDuplicate(taxisID1);
87
  if (taxisHasBounds(taxisID4)) taxisDeleteBounds(taxisID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
89
  vlistDefTaxis(vlistID4, taxisID4);

90
  const auto streamID4 = cdoOpenWrite(3);
91
  cdoDefVlist(streamID4, vlistID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92

93
  const int nvars = vlistNvars(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94

95
  const int maxrecs = vlistNrecs(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
  std::vector<RecordInfo> recList(maxrecs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97

98
  const size_t gridsizemax = vlistGridsizeMax(vlistID1);
99

Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
  Field field;
101
  field.resize(gridsizemax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102

103
  int tsID = 0;
104
  while ((nrecs = cdoStreamInqTimestep(streamID2, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
    {
106
      if (nrecs != cdoStreamInqTimestep(streamID3, tsID))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
        cdoAbort("Number of records at time step %d of %s and %s differ!", tsID + 1, cdoGetStreamName(1), cdoGetStreamName(2));
108

109
      const auto vdate = taxisInqVdate(taxisID2);
110
      const int vtime = taxisInqVtime(taxisID2);
111
112

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

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

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

119
      const int dayoy = (month >= 1 && month <= 12) ? (month - 1) * 31 + day : 0;
120
      if (dayoy < 0 || dayoy >= NDAY) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
123

      vdates2[dayoy] = vdate;

124
      if (!vars1[dayoy].size())
125
        {
126
          fieldsFromVlist(vlistID1, vars1[dayoy], FIELD_VEC);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
          hsets[dayoy] = hsetCreate(nvars);

129
130
131
132
          for (varID = 0; varID < nvars; varID++)
            {
              gridID = vlistInqVarGrid(vlistID1, varID);
              nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134

              hsetCreateVarLevels(hsets[dayoy], varID, nlevels, gridID);
135
136
137
138
            }
        }

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

145
      for (int recID = 0; recID < nrecs; recID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
        {
147
          cdoInqRecord(streamID3, &varID, &levelID);
148
          cdoReadRecord(streamID3, field.vec.data(), &field.nmiss);
149
150
151
          field.grid = vars1[dayoy][varID][levelID].grid;
          field.missval = vars1[dayoy][varID][levelID].missval;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
          hsetDefVarLevelBounds(hsets[dayoy], varID, levelID, &vars1[dayoy][varID][levelID], &field);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
        }
154

Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
156
      tsID++;
    }
157

Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
  tsID = 0;
159
  while ((nrecs = cdoStreamInqTimestep(streamID1, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
    {
161
      const auto vdate = taxisInqVdate(taxisID1);
162
      const int vtime = taxisInqVtime(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163

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

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

168
      const int dayoy = (month >= 1 && month <= 12) ? (month - 1) * 31 + day : 0;
169
      if (dayoy < 0 || dayoy >= NDAY) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
      if (!vars1[dayoy].size()) cdoAbort("No data for day %d in %s and %s", dayoy, cdoGetStreamName(1), cdoGetStreamName(2));
175
176
177

      for (int recID = 0; recID < nrecs; recID++)
        {
178
          cdoInqRecord(streamID1, &varID, &levelID);
179
180
181

          if (tsID == 0)
            {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
184
              recList[recID].varID = varID;
              recList[recID].levelID = levelID;
              recList[recID].lconst = vlistInqVarTimetype(vlistID1, varID) == TIME_CONSTANT;
185
186
            }

187
          cdoReadRecord(streamID1, vars1[dayoy][varID][levelID].vec.data(), &nmiss);
188
189
          vars1[dayoy][varID][levelID].nmiss = nmiss;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
          hsetAddVarLevelValues(hsets[dayoy], varID, levelID, &vars1[dayoy][varID][levelID]);
191
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
193
194
195
196

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

197
  int otsID = 0;
198
  for (int dayoy = 0; dayoy < NDAY; dayoy++)
199
    if (nsets[dayoy])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
      {
201
        if (getmonthday(vdates1[dayoy]) != getmonthday(vdates2[dayoy]))
202
203
          cdoAbort("Verification dates for the day %d of %s and %s are different!", dayoy, cdoGetStreamName(0),
                   cdoGetStreamName(1));
204
205
206
207
208
209
210

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

            for (levelID = 0; levelID < nlevels; levelID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
              hsetGetVarLevelPercentiles(&vars1[dayoy][varID][levelID], hsets[dayoy], varID, levelID, pn);
212
213
214
215
          }

        taxisDefVdate(taxisID4, vdates1[dayoy]);
        taxisDefVtime(taxisID4, vtimes1[dayoy]);
216
        cdoDefTimestep(streamID4, otsID);
217

218
        for (int recID = 0; recID < maxrecs; recID++)
219
          {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
            if (otsID && recList[recID].lconst) continue;
221

Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
223
            const int varID = recList[recID].varID;
            const int levelID = recList[recID].levelID;
224
            cdoDefRecord(streamID4, varID, levelID);
225
            cdoWriteRecord(streamID4, vars1[dayoy][varID][levelID].vec.data(), vars1[dayoy][varID][levelID].nmiss);
226
227
228
          }

        otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230
      }

231
  for (int dayoy = 0; dayoy < NDAY; dayoy++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
    {
233
      if (vars1[dayoy].size())
234
235
236
        {
          hsetDestroy(hsets[dayoy]);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
    }

239
240
241
242
  cdoStreamClose(streamID4);
  cdoStreamClose(streamID3);
  cdoStreamClose(streamID2);
  cdoStreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
  return nullptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
}