Ydaypctl.cc 7.57 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 "param_conversion.h"
28

29
#include "percentiles_hist.h"
30
#include "percentiles.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31

32
#define NDAY 373
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

34
int getmonthday(int64_t date);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35

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

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

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

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

67
68
69
  int streamID1 = cdoStreamOpenRead(cdoStreamName(0));
  int streamID2 = cdoStreamOpenRead(cdoStreamName(1));
  int streamID3 = cdoStreamOpenRead(cdoStreamName(2));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70

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

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

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

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

88
  int streamID4 = cdoStreamOpenWrite(cdoStreamName(3));
89
  cdoDefVlist(streamID4, vlistID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90

91
  int nvars = vlistNvars(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92

93
  int maxrecs = vlistNrecs(vlistID1);
94
  std::vector<RecordInfo> recinfo(maxrecs);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95

96
  size_t gridsize = vlistGridsizeMax(vlistID1);
97

Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
  Field field;
99
  fieldInit(field);
100
  field.ptr = (double *) Malloc(gridsize * sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
      vdate = taxisInqVdate(taxisID2);
      vtime = taxisInqVtime(taxisID2);
111
112

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

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

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

120
121
      if (month >= 1 && month <= 12)
        dayoy = (month - 1) * 31 + day;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
      else
123
        dayoy = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124

125
      if (dayoy < 0 || dayoy >= NDAY) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128

      vdates2[dayoy] = vdate;

129
      if (!vars1[dayoy].size())
130
        {
131
          fieldsFromVlist(vlistID1, vars1[dayoy], FIELD_PTR);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
133
          hsets[dayoy] = hsetCreate(nvars);

134
135
136
137
          for (varID = 0; varID < nvars; varID++)
            {
              gridID = vlistInqVarGrid(vlistID1, varID);
              nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
139

              hsetCreateVarLevels(hsets[dayoy], varID, nlevels, gridID);
140
141
142
143
            }
        }

      for (int recID = 0; recID < nrecs; recID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
        {
145
146
          cdoInqRecord(streamID2, &varID, &levelID);
          cdoReadRecord(streamID2, vars1[dayoy][varID][levelID].ptr, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
148
          vars1[dayoy][varID][levelID].nmiss = nmiss;
        }
149
      for (int recID = 0; recID < nrecs; recID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
        {
151
152
          cdoInqRecord(streamID3, &varID, &levelID);
          cdoReadRecord(streamID3, field.ptr, &nmiss);
153
154
155
156
          field.nmiss = nmiss;
          field.grid = vars1[dayoy][varID][levelID].grid;
          field.missval = vars1[dayoy][varID][levelID].missval;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
          hsetDefVarLevelBounds(hsets[dayoy], varID, levelID, &vars1[dayoy][varID][levelID], &field);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
        }
159

Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
161
      tsID++;
    }
162

Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
  tsID = 0;
164
  while ((nrecs = cdoStreamInqTimestep(streamID1, tsID)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167
168
    {
      vdate = taxisInqVdate(taxisID1);
      vtime = taxisInqVtime(taxisID1);

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

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

173
174
      if (month >= 1 && month <= 12)
        dayoy = (month - 1) * 31 + day;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
      else
176
177
178
        dayoy = 0;

      if (dayoy < 0 || dayoy >= NDAY) cdoAbort("Day %d out of range!", dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
181

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

183
      if (!vars1[dayoy].size())
184
        cdoAbort("No data for day %d in %s and %s", dayoy, cdoGetStreamName(1), cdoGetStreamName(2));
185
186
187

      for (int recID = 0; recID < nrecs; recID++)
        {
188
          cdoInqRecord(streamID1, &varID, &levelID);
189
190
191

          if (tsID == 0)
            {
192
193
194
              recinfo[recID].varID = varID;
              recinfo[recID].levelID = levelID;
              recinfo[recID].lconst = vlistInqVarTimetype(vlistID1, varID) == TIME_CONSTANT;
195
196
            }

197
          cdoReadRecord(streamID1, vars1[dayoy][varID][levelID].ptr, &nmiss);
198
199
          vars1[dayoy][varID][levelID].nmiss = nmiss;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
          hsetAddVarLevelValues(hsets[dayoy], varID, levelID, &vars1[dayoy][varID][levelID]);
201
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
203
204
205
206

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

207
  int otsID = 0;
208
209
  for (dayoy = 0; dayoy < NDAY; dayoy++)
    if (nsets[dayoy])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
      {
211
        if (getmonthday(vdates1[dayoy]) != getmonthday(vdates2[dayoy]))
212
213
          cdoAbort("Verification dates for the day %d of %s and %s are different!", dayoy, cdoGetStreamName(0),
                   cdoGetStreamName(1));
214
215
216
217
218
219
220

        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
221
              hsetGetVarLevelPercentiles(&vars1[dayoy][varID][levelID], hsets[dayoy], varID, levelID, pn);
222
223
224
225
          }

        taxisDefVdate(taxisID4, vdates1[dayoy]);
        taxisDefVtime(taxisID4, vtimes1[dayoy]);
226
        cdoDefTimestep(streamID4, otsID);
227

228
        for (int recID = 0; recID < maxrecs; recID++)
229
          {
230
            if (otsID && recinfo[recID].lconst) continue;
231

232
233
            int varID = recinfo[recID].varID;
            int levelID = recinfo[recID].levelID;
234
235
            cdoDefRecord(streamID4, varID, levelID);
            cdoWriteRecord(streamID4, vars1[dayoy][varID][levelID].ptr, vars1[dayoy][varID][levelID].nmiss);
236
237
238
          }

        otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
240
      }

241
  for (dayoy = 0; dayoy < NDAY; dayoy++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
    {
243
      if (vars1[dayoy].size())
244
        {
245
          fieldsFree(vlistID1, vars1[dayoy]);
246
247
          hsetDestroy(hsets[dayoy]);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
    }

250
  if (field.ptr) Free(field.ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251

252
253
254
255
  cdoStreamClose(streamID4);
  cdoStreamClose(streamID3);
  cdoStreamClose(streamID2);
  cdoStreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
257
258

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
  return nullptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
}