Ydaypctl.cc 7.58 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 "pstream_int.h"
28
#include "percentiles_hist.h"
29
#include "percentiles.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30

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

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

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

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

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

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

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

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

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

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

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

87
  int streamID4 = cdoStreamOpenWrite(cdoStreamName(3), cdoFiletype());
88
  pstreamDefVlist(streamID4, vlistID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89

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

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

95
  size_t gridsize = vlistGridsizeMax(vlistID1);
96

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

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

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

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

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

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

      vdates2[dayoy] = vdate;

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

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

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

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

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

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

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

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

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

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

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

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

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

      for (int recID = 0; recID < nrecs; recID++)
        {
          pstreamInqRecord(streamID1, &varID, &levelID);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
          pstreamReadRecord(streamID1, vars1[dayoy][varID][levelID].ptr, &nmiss);
197
198
          vars1[dayoy][varID][levelID].nmiss = nmiss;

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

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

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

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

        taxisDefVdate(taxisID4, vdates1[dayoy]);
        taxisDefVtime(taxisID4, vtimes1[dayoy]);
        pstreamDefTimestep(streamID4, otsID);

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

231
232
            int varID = recinfo[recID].varID;
            int levelID = recinfo[recID].levelID;
233
            pstreamDefRecord(streamID4, varID, levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
            pstreamWriteRecord(streamID4, vars1[dayoy][varID][levelID].ptr, vars1[dayoy][varID][levelID].nmiss);
235
236
237
          }

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

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

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

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

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
}