Ydaypctl.cc 7.03 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"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
#include "cdo_vlist.h"
#include "datetime.h"
29
#include "functs.h"
30
#include "process_int.h"
31
#include "param_conversion.h"
32
#include "percentiles_hist.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

34
35
void *
Ydaypctl(void *process)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
  constexpr int MaxDays = 373;
38
  int nrecs;
39
  size_t nmiss;
40
41
42
  int64_t vdates1[MaxDays] = { 0 }, vdates2[MaxDays] = { 0 };
  int vtimes1[MaxDays] = { 0 };
  long nsets[MaxDays] = { 0 };
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
  FieldVector2D vars1[MaxDays];
  HistogramSet hsets[MaxDays];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45

46
  cdoInitialize(process);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
  cdoOperatorAdd("ydaypctl", func_pctl, 0, nullptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49

  operatorInputArg("percentile number");
50
  const auto pn = parameter2double(cdoOperatorArgv(0));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51

52
53
54
  const auto streamID1 = cdoOpenRead(0);
  const auto streamID2 = cdoOpenRead(1);
  const auto streamID3 = cdoOpenRead(2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55

Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
58
59
  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
60

Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
  vlistCompare(vlistID1, vlistID2, CMP_ALL);
  vlistCompare(vlistID1, vlistID3, CMP_ALL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63

Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
66
  const auto taxisID1 = vlistInqTaxis(vlistID1);
  const auto taxisID2 = vlistInqTaxis(vlistID2);
  const auto taxisID3 = vlistInqTaxis(vlistID3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
  /* TODO - check that time axes 2 and 3 are equal */

Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
  const auto taxisID4 = taxisDuplicate(taxisID1);
70
  if (taxisHasBounds(taxisID4)) taxisDeleteBounds(taxisID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
72
  vlistDefTaxis(vlistID4, taxisID4);

73
  const auto streamID4 = cdoOpenWrite(3);
74
  cdoDefVlist(streamID4, vlistID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75

Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
  const auto nvars = vlistNvars(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77

Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
  const auto maxrecs = vlistNrecs(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
  std::vector<RecordInfo> recList(maxrecs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80

Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82
83
  VarList varList1;
  varListInit(varList1, vlistID1);

84
  const auto gridsizemax = vlistGridsizeMax(vlistID1);
85

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
  Field field;
87
  field.resize(gridsizemax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88

89
  int tsID = 0;
90
  while ((nrecs = cdoStreamInqTimestep(streamID2, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
    {
92
      if (nrecs != cdoStreamInqTimestep(streamID3, tsID))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
        cdoAbort("Number of records at time step %d of %s and %s differ!", tsID + 1, cdoGetStreamName(1), cdoGetStreamName(2));
94

95
      const auto vdate = taxisInqVdate(taxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
      const auto vtime = taxisInqVtime(taxisID2);
97
98

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
      const auto dayoy = decodeDayOfYear(vdate);
      if (dayoy < 0 || dayoy >= MaxDays) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
107

      vdates2[dayoy] = vdate;

108
      if (!vars1[dayoy].size())
109
        {
110
          fieldsFromVlist(vlistID1, vars1[dayoy], FIELD_VEC);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
          hsets[dayoy].create(nvars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112

Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
          for (int varID = 0; varID < nvars; varID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
            hsets[dayoy].createVarLevels(varID, varList1[varID].nlevels, varList1[varID].gridsize);
115
116
117
        }

      for (int recID = 0; recID < nrecs; recID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
          int varID, levelID;
120
          cdoInqRecord(streamID2, &varID, &levelID);
121
          cdoReadRecord(streamID2, vars1[dayoy][varID][levelID].vec_d.data(), &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
          vars1[dayoy][varID][levelID].nmiss = nmiss;
        }
124

125
      for (int recID = 0; recID < nrecs; recID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
          int varID, levelID;
128
          cdoInqRecord(streamID3, &varID, &levelID);
129
          cdoReadRecord(streamID3, field.vec_d.data(), &field.nmiss);
130
131
132
          field.size = varList1[varID].gridsize;
          field.grid = varList1[varID].gridID;
          field.missval = varList1[varID].missval;
133

Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
          hsets[dayoy].defVarLevelBounds(varID, levelID, vars1[dayoy][varID][levelID], field);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
        }
136

Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
      tsID++;
    }
139

Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
  tsID = 0;
141
  while ((nrecs = cdoStreamInqTimestep(streamID1, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
    {
143
      const auto vdate = taxisInqVdate(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
      const auto vtime = taxisInqVtime(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
      const auto dayoy = decodeDayOfYear(vdate);
      if (dayoy < 0 || dayoy >= MaxDays) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
151
152

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
      if (!vars1[dayoy].size()) cdoAbort("No data for day %d in %s and %s", dayoy, cdoGetStreamName(1), cdoGetStreamName(2));
155
156
157

      for (int recID = 0; recID < nrecs; recID++)
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
          int varID, levelID;
159
          cdoInqRecord(streamID1, &varID, &levelID);
160
161
162

          if (tsID == 0)
            {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
              recList[recID].varID = varID;
              recList[recID].levelID = levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
              recList[recID].lconst = varList1[varID].timetype == TIME_CONSTANT;
166
167
            }

168
          cdoReadRecord(streamID1, vars1[dayoy][varID][levelID].vec_d.data(), &nmiss);
169
170
          vars1[dayoy][varID][levelID].nmiss = nmiss;

171
          hsets[dayoy].addSubVarLevelValues(varID, levelID, vars1[dayoy][varID][levelID], 1, FIELD_VEC);
172
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
174
175
176
177

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

178
  int otsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
  for (int dayoy = 0; dayoy < MaxDays; dayoy++)
180
    if (nsets[dayoy])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
        if (decodeMonthAndDay(vdates1[dayoy]) != decodeMonthAndDay(vdates2[dayoy]))
183
184
          cdoAbort("Verification dates for the day %d of %s and %s are different!", dayoy, cdoGetStreamName(0),
                   cdoGetStreamName(1));
185

Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
        for (int varID = 0; varID < nvars; varID++)
187
          {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
            if (varList1[varID].timetype == TIME_CONSTANT) continue;
189

Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
            const auto nlevels = varList1[varID].nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
            for (int levelID = 0; levelID < nlevels; levelID++)
192
              hsets[dayoy].getVarLevelPercentiles(vars1[dayoy][varID][levelID], varID, levelID, pn, FIELD_VEC);
193
194
195
196
          }

        taxisDefVdate(taxisID4, vdates1[dayoy]);
        taxisDefVtime(taxisID4, vtimes1[dayoy]);
197
        cdoDefTimestep(streamID4, otsID);
198

199
        for (int recID = 0; recID < maxrecs; recID++)
200
          {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
            if (otsID && recList[recID].lconst) continue;
202

Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
204
            const auto varID = recList[recID].varID;
            const auto levelID = recList[recID].levelID;
205
            cdoDefRecord(streamID4, varID, levelID);
206
            cdoWriteRecord(streamID4, vars1[dayoy][varID][levelID].vec_d.data(), vars1[dayoy][varID][levelID].nmiss);
207
208
209
          }

        otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
211
      }

212
213
214
215
  cdoStreamClose(streamID4);
  cdoStreamClose(streamID3);
  cdoStreamClose(streamID2);
  cdoStreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
218

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
  return nullptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
}