Ydaypctl.cc 7.4 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"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
32

#define  NDAY       373

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

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

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

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

  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    {
      vars1[dayoy] = NULL;
      hsets[dayoy] = NULL;
      nsets[dayoy] = 0;
    }

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

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

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

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

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

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

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

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

95
96
  int gridsize = vlistGridsizeMax(vlistID1);

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

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

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

      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 )
	{
129
	  vars1[dayoy] = field_malloc(vlistID1, FIELD_PTR);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
131
132
133
134
135
136
137
138
139
140
          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);
	    }
	}
      
141
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
        {
143
144
          pstreamInqRecord(streamID2, &varID, &levelID);
	  pstreamReadRecord(streamID2, vars1[dayoy][varID][levelID].ptr, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
          vars1[dayoy][varID][levelID].nmiss = nmiss;
        }
147
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
        {
149
150
          pstreamInqRecord(streamID3, &varID, &levelID);
	  pstreamReadRecord(streamID3, field.ptr, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
152
153
154
155
156
157
158
159
160
161
          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;
162
  while ( (nrecs = pstreamInqTimestep(streamID1, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
166
167
168
    {
      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
169
      cdiDecodeDate(vdate, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
173
174
175
176
177
178
179
180
181
182

      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 )
183
        cdoAbort("No data for day %d in %s and %s", dayoy, cdoStreamName(1)->args, cdoStreamName(2)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
        
185
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
	{
187
	  pstreamInqRecord(streamID1, &varID, &levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188

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

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

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

205
  int otsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
207
208
  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    if ( nsets[dayoy] )
      {
209
210
211
        if ( getmonthday(vdates1[dayoy]) !=  getmonthday(vdates2[dayoy]) )
          cdoAbort("Verification dates for the day %d of %s and %s are different!",
                   dayoy, cdoStreamName(0)->args, cdoStreamName(1)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
213
214
        
	for ( varID = 0; varID < nvars; varID++ )
	  {
215
	    if ( vlistInqVarTimetype(vlistID1, varID) == TIME_CONSTANT ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
218
219
220
221
222
223
	    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]);
224
	pstreamDefTimestep(streamID4, otsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225

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

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

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

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

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

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

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

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

  cdoFinish();

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