Yseaspctl.cc 7.48 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
  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:

Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
      Yseaspctl  yseaspctl       Multi-year seasonal percentiles
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
23
*/

Ralf Mueller's avatar
Ralf Mueller committed
24
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
#include "cdo.h"
26
#include "cdo_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
#include "pstream.h"
28
#include "percentiles_hist.h"
29
#include "util.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
32

#define  NSEAS       4

33
34
35
36
37
38
39
40
41
42
typedef struct {
  int vdate;
  int vtime;
}
date_time_t;

void set_date(int vdate_new, int vtime_new, date_time_t *datetime);

int getmonthday(int date);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
43

Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
46
47
48
void *Yseaspctl(void *argument)
{
  int varID;
  int gridID;
  int vdate, vtime;
49
  int year, month, day, seas;
50
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
  int levelID;
  int nmiss;
53
  int nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
  long nsets[NSEAS];
55
  date_time_t datetime1[NSEAS], datetime2[NSEAS];
56
  field_type **vars1[NSEAS];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
59
60
61
62
  HISTOGRAM_SET *hsets[NSEAS];

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

  operatorInputArg("percentile number");
63
  double pn = parameter2double(operatorArgv()[0]);
64
  percentile_check_number(pn);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66
67
68
69
70

  for ( seas = 0; seas < NSEAS; seas++ )
    {
      vars1[seas] = NULL;
      hsets[seas] = NULL;
      nsets[seas] = 0;
71
72
73
74
      datetime1[seas].vdate = 0;
      datetime1[seas].vtime = 0;
      datetime2[seas].vdate = 0;
      datetime2[seas].vtime = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
    }

77
78
79
  int streamID1 = streamOpenRead(cdoStreamName(0));
  int streamID2 = streamOpenRead(cdoStreamName(1));
  int streamID3 = streamOpenRead(cdoStreamName(2));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80

81
82
83
84
  int vlistID1 = streamInqVlist(streamID1);
  int vlistID2 = streamInqVlist(streamID2);
  int vlistID3 = streamInqVlist(streamID3);
  int vlistID4 = vlistDuplicate(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
  vlistCompare(vlistID1, vlistID2, CMP_ALL);
  vlistCompare(vlistID1, vlistID3, CMP_ALL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88

89
90
91
  int taxisID1 = vlistInqTaxis(vlistID1);
  int taxisID2 = vlistInqTaxis(vlistID2);
  int taxisID3 = vlistInqTaxis(vlistID3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92
93
  /* TODO - check that time axes 2 and 3 are equal */

94
  int taxisID4 = taxisDuplicate(taxisID1);
95
  if ( taxisHasBounds(taxisID4) ) taxisDeleteBounds(taxisID4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
97
  vlistDefTaxis(vlistID4, taxisID4);

98
  int streamID4 = streamOpenWrite(cdoStreamName(3), cdoFiletype());
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
100
101

  streamDefVlist(streamID4, vlistID4);

102
103
  int nvars    = vlistNvars(vlistID1);
  int nrecords = vlistNrecs(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104

105
106
  int *recVarID   = (int*) Malloc(nrecords*sizeof(int));
  int *recLevelID = (int*) Malloc(nrecords*sizeof(int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107

108
109
  int gridsize = vlistGridsizeMax(vlistID1);

110
  field_type field;
111
  field_init(&field);
112
  field.ptr = (double*) Malloc(gridsize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113

114
  int tsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
  while ( (nrecs = streamInqTimestep(streamID2, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
117
    {
      if ( nrecs != streamInqTimestep(streamID3, tsID) )
118
        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
119
120
121
122
      
      vdate = taxisInqVdate(taxisID2);
      vtime = taxisInqVtime(taxisID2);
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
      if ( vdate != taxisInqVdate(taxisID3) )
124
        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
125
126
127
        
      if ( cdoVerbose ) cdoPrint("process timestep: %d %d %d", tsID+1, vdate, vtime);

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

130
      seas = month_to_season(month);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131

132
      set_date(vdate, vtime, &datetime2[seas]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
135

      if ( vars1[seas] == NULL )
	{
136
	  vars1[seas] = field_malloc(vlistID1, FIELD_PTR);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140
141
142
143
144
145
146
147
          hsets[seas] = hsetCreate(nvars);

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

              hsetCreateVarLevels(hsets[seas], varID, nlevels, gridID);
	    }
	}
      
148
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
151
152
153
        {
          streamInqRecord(streamID2, &varID, &levelID);
	  streamReadRecord(streamID2, vars1[seas][varID][levelID].ptr, &nmiss);
          vars1[seas][varID][levelID].nmiss = nmiss;
        }
154
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        {
          streamInqRecord(streamID3, &varID, &levelID);
	  streamReadRecord(streamID3, field.ptr, &nmiss);
          field.nmiss   = nmiss;
          field.grid    = vars1[seas][varID][levelID].grid;
	  field.missval = vars1[seas][varID][levelID].missval;
	  
	  hsetDefVarLevelBounds(hsets[seas], varID, levelID, &vars1[seas][varID][levelID], &field);
        }
      
      tsID++;
    }

  tsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
  while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
173
    {
      vdate = taxisInqVdate(taxisID1);
      vtime = taxisInqVtime(taxisID1);
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
      cdiDecodeDate(vdate, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
176
177
178
179
180
181
182
183
184
      if ( month < 0 || month > 16 )
	cdoAbort("Month %d out of range!", month);

      if ( month <= 12 )
	seas = (month % 12) / 3;
      else
	seas = month - 13;
      if ( seas < 0 || seas > 3 )
	cdoAbort("Season %d out of range!", seas+1);

185
      set_date(vdate, vtime, &datetime1[seas]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
187

      if ( vars1[seas] == NULL )
188
        cdoAbort("No data for season %d in %s and %s", seas, cdoStreamName(1)->args, cdoStreamName(2)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189

190
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
193
	{
	  streamInqRecord(streamID1, &varID, &levelID);

194
195
196
197
198
	  if ( tsID == 0 )
	    {
	      recVarID[recID]   = varID;
	      recLevelID[recID] = levelID;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
200
201
202
203
204
205
206
207
208
209

	  streamReadRecord(streamID1, vars1[seas][varID][levelID].ptr, &nmiss);
	  vars1[seas][varID][levelID].nmiss = nmiss;
	  
	  hsetAddVarLevelValues(hsets[seas], varID, levelID, &vars1[seas][varID][levelID]);
	}

      nsets[seas]++;
      tsID++;
    }

210
  int otsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
212
213
  for ( seas = 0; seas < NSEAS; seas++ )
    if ( nsets[seas] )
      {
214
        if ( getmonthday(datetime1[seas].vdate) != getmonthday(datetime2[seas].vdate) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
          cdoAbort("Verification dates for the season %d of %s and %s are different!",
                   seas, cdoStreamName(0)->args, cdoStreamName(1)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
219

	for ( varID = 0; varID < nvars; varID++ )
	  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
	    if ( vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
223
224
225
226
	    nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
	    
	    for ( levelID = 0; levelID < nlevels; levelID++ )
	      hsetGetVarLevelPercentiles(&vars1[seas][varID][levelID], hsets[seas], varID, levelID, pn);
	  }

227
228
	taxisDefVdate(taxisID4, datetime1[seas].vdate);
	taxisDefVtime(taxisID4, datetime1[seas].vtime);
229
	streamDefTimestep(streamID4, otsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230

231
	for ( int recID = 0; recID < nrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235
	  {
	    varID    = recVarID[recID];
	    levelID  = recLevelID[recID];

Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
	    if ( otsID && vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
237
238
239

	    streamDefRecord(streamID4, varID, levelID);
	    streamWriteRecord(streamID4, vars1[seas][varID][levelID].ptr, vars1[seas][varID][levelID].nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
	  }
241
242

	otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245
246
247
248
      }

  for ( seas = 0; seas < NSEAS; seas++ )
    {
      if ( vars1[seas] != NULL )
	{
249
	  field_free(vars1[seas], vlistID1); 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
251
252
253
	  hsetDestroy(hsets[seas]);
	}
    }

254
  if ( field.ptr ) Free(field.ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255

256
257
  if ( recVarID   ) Free(recVarID);
  if ( recLevelID ) Free(recLevelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
259
260
261
262
263
264
265

  streamClose(streamID4);
  streamClose(streamID3);
  streamClose(streamID2);
  streamClose(streamID1);

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
266
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267
}