Ydaypctl.cc 7.43 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
24
  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
25
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
#include "cdo.h"
27
#include "cdo_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
#include "pstream.h"
29
#include "percentiles_hist.h"
30
#include "percentiles.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
32
33

#define  NDAY       373

34
int getmonthday(int date);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
37
38
39
40
41

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

  cdoInitialize(argument);
  cdoOperatorAdd("ydaypctl", func_pctl, 0, NULL);

  operatorInputArg("percentile number");
56
57
  double pn = parameter2double(operatorArgv()[0]);
  percentile_check_number(pn);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59
60
61
62
63
64
65

  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    {
      vars1[dayoy] = NULL;
      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 = pstreamInqVlist(streamID1);
  int vlistID2 = pstreamInqVlist(streamID2);
  int vlistID3 = pstreamInqVlist(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
91
  int nvars    = vlistNvars(vlistID1);
  int nrecords = vlistNrecs(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92

93
94
  int *recVarID   = (int*) Malloc(nrecords*sizeof(int));
  int *recLevelID = (int*) Malloc(nrecords*sizeof(int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95

96
97
  int gridsize = vlistGridsizeMax(vlistID1);

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

102
  int tsID = 0;
103
  while ( (nrecs = pstreamInqTimestep(streamID2, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
    {
105
      if ( nrecs != pstreamInqTimestep(streamID3, tsID) )
106
        cdoAbort("Number of records at time step %d of %s and %s differ!", tsID+1, cdoGetStreamName(1).c_str(), cdoStreamName(2));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
109
110
      
      vdate = taxisInqVdate(taxisID2);
      vtime = taxisInqVtime(taxisID2);
      
111
      if ( vdate != taxisInqVdate(taxisID3) )
112
        cdoAbort("Verification dates at time step %d of %s and %s differ!", tsID+1, cdoGetStreamName(1).c_str(), cdoStreamName(2));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
114
115
        
      if ( cdoVerbose ) cdoPrint("process timestep: %d %d %d", tsID+1, vdate, vtime);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
      cdiDecodeDate(vdate, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
119
120
121
122
123
124
125
126
127
128
129

      if ( month >= 1 && month <= 12 )
	dayoy = (month-1)*31 + day;
      else
	dayoy = 0;

      if ( dayoy < 0 || dayoy >= NDAY )
	cdoAbort("Day %d out of range!", dayoy);

      vdates2[dayoy] = vdate;

      if ( vars1[dayoy] == NULL )
	{
130
	  vars1[dayoy] = field_malloc(vlistID1, FIELD_PTR);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
134
135
136
137
138
139
140
141
          hsets[dayoy] = hsetCreate(nvars);

	  for ( varID = 0; varID < nvars; varID++ )
	    {
	      gridID   = vlistInqVarGrid(vlistID1, varID);
	      nlevels  = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));

              hsetCreateVarLevels(hsets[dayoy], varID, nlevels, gridID);
	    }
	}
      
142
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
        {
144
145
          pstreamInqRecord(streamID2, &varID, &levelID);
	  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
151
          pstreamInqRecord(streamID3, &varID, &levelID);
	  pstreamReadRecord(streamID3, field.ptr, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
154
155
156
157
158
159
160
161
162
          field.nmiss   = nmiss;
          field.grid    = vars1[dayoy][varID][levelID].grid;
	  field.missval = vars1[dayoy][varID][levelID].missval;
	  
	  hsetDefVarLevelBounds(hsets[dayoy], varID, levelID, &vars1[dayoy][varID][levelID], &field);
        }
      
      tsID++;
    }
  
  tsID = 0;
163
  while ( (nrecs = pstreamInqTimestep(streamID1, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
165
166
167
168
169
    {
      vdate = taxisInqVdate(taxisID1);
      vtime = taxisInqVtime(taxisID1);

      if ( cdoVerbose ) cdoPrint("process timestep: %d %d %d", tsID+1, vdate, vtime);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
      cdiDecodeDate(vdate, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
172
173
174
175
176
177
178
179
180
181
182
183

      if ( month >= 1 && month <= 12 )
	dayoy = (month-1)*31 + day;
      else
	dayoy = 0;

      if ( dayoy < 0 || dayoy >= NDAY )
	cdoAbort("Day %d out of range!", dayoy);
	
      vdates1[dayoy] = vdate;
      vtimes1[dayoy] = vtime;
      
      if ( vars1[dayoy] == NULL )
184
        cdoAbort("No data for day %d in %s and %s", dayoy, cdoGetStreamName(1).c_str(), cdoStreamName(2));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
        
186
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
	{
188
	  pstreamInqRecord(streamID1, &varID, &levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189

190
191
192
193
194
	  if ( tsID == 0 )
	    {
	      recVarID[recID]   = varID;
	      recLevelID[recID] = levelID;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195

196
	  pstreamReadRecord(streamID1, vars1[dayoy][varID][levelID].ptr, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
198
199
200
201
202
203
204
205
	  vars1[dayoy][varID][levelID].nmiss = nmiss;
	      
	  hsetAddVarLevelValues(hsets[dayoy], varID, levelID, &vars1[dayoy][varID][levelID]);
	}

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

206
  int otsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
209
  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    if ( nsets[dayoy] )
      {
210
211
        if ( getmonthday(vdates1[dayoy]) !=  getmonthday(vdates2[dayoy]) )
          cdoAbort("Verification dates for the day %d of %s and %s are different!",
212
                   dayoy, cdoGetStreamName(0).c_str(), cdoStreamName(1));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
214
215
        
	for ( varID = 0; varID < nvars; varID++ )
	  {
216
	    if ( vlistInqVarTimetype(vlistID1, varID) == TIME_CONSTANT ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
219
220
221
222
223
224
	    nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
	      
	    for ( levelID = 0; levelID < nlevels; levelID++ )
	      hsetGetVarLevelPercentiles(&vars1[dayoy][varID][levelID], hsets[dayoy], varID, levelID, pn);
	  }

	taxisDefVdate(taxisID4, vdates1[dayoy]);
	taxisDefVtime(taxisID4, vtimes1[dayoy]);
225
	pstreamDefTimestep(streamID4, otsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226

227
	for ( int recID = 0; recID < nrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
230
231
	  {
	    varID    = recVarID[recID];
	    levelID  = recLevelID[recID];

232
	    if ( otsID && vlistInqVarTimetype(vlistID1, varID) == TIME_CONSTANT ) continue;
233

234
235
	    pstreamDefRecord(streamID4, varID, levelID);
	    pstreamWriteRecord(streamID4, vars1[dayoy][varID][levelID].ptr, vars1[dayoy][varID][levelID].nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
	  }
237
238

	otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
240
241
242
243
244
      }

  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    {
      if ( vars1[dayoy] != NULL )
	{
245
	  field_free(vars1[dayoy], vlistID1); 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
247
248
249
	  hsetDestroy(hsets[dayoy]);
	}
    }

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

252
253
  if ( recVarID   ) Free(recVarID);
  if ( recLevelID ) Free(recLevelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254

255
256
257
258
  pstreamClose(streamID4);
  pstreamClose(streamID3);
  pstreamClose(streamID2);
  pstreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260
261

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
263
}