Seaspctl.c 7.15 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
      Seaspctl   seaspctl        Seasonal percentiles
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
23
24
*/


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


void *Seaspctl(void *argument)
{
35
  int timestat_date = TIMESTAT_MEAN;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
  int vdate1 = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
  int vdate2 = 0, vtime2 = 0;
  int vdate3 = 0, vtime3 = 0;
39
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
41
42
43
  int gridID, varID, levelID, recID;
  int tsID;
  int otsID;
  long nsets;
44
  int year, month, day, seas, seas0 = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
  int nmiss;
46
  int nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
  int newseas, oldmon = 0, newmon;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
  field_t **vars1 = NULL;
  field_t field;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
  HISTOGRAM_SET *hset = NULL;
52
  int season_start;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
55
56
57
58

  cdoInitialize(argument);

  cdoOperatorAdd("seaspctl", func_pctl, 0, NULL);

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

62
  season_start = get_season_start();
63

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

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

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

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

81
  int taxisID4 = taxisDuplicate(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
83
  vlistDefTaxis(vlistID4, taxisID4);

84
  int streamID4 = streamOpenWrite(cdoStreamName(3), cdoFiletype());
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
86
87

  streamDefVlist(streamID4, vlistID4);

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

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

94
95
96
97
98
  dtlist_type *dtlist = dtlist_new();
  dtlist_set_stat(dtlist, timestat_date);
  dtlist_set_calendar(dtlist, taxisInqCalendar(taxisID1));

  int gridsize = vlistGridsizeMax(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99

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

102
  vars1 = (field_t **) Malloc(nvars * sizeof(field_t *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
105
106
107
108
109
110
111
  hset = hsetCreate(nvars);

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

112
      vars1[varID] = (field_t*) Malloc(nlevels * sizeof(field_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
114
115
116
117
118
119
      hsetCreateVarLevels(hset, varID, nlevels, gridID);

      for ( levelID = 0; levelID < nlevels; levelID++ )
	{
	  vars1[varID][levelID].grid    = gridID;
	  vars1[varID][levelID].nmiss   = 0;
	  vars1[varID][levelID].missval = missval;
120
	  vars1[varID][levelID].ptr     = (double*) Malloc(gridsize * sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
123
124
125
126
127
128
129
	}
    }

  tsID    = 0;
  otsID   = 0;
  while ( TRUE )
    {
      nrecs = streamInqTimestep(streamID2, otsID);
      if ( nrecs != streamInqTimestep(streamID3, otsID) )
130
        cdoAbort("Number of records at time step %d of %s and %s differ!", otsID+1, cdoStreamName(1)->args, cdoStreamName(2)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
134
135
136
      
      vdate2 = taxisInqVdate(taxisID2);
      vtime2 = taxisInqVtime(taxisID2);
      vdate3 = taxisInqVdate(taxisID3);
      vtime3 = taxisInqVtime(taxisID3);
      if ( vdate2 != vdate3 || vtime2 != vtime3 )
137
        cdoAbort("Verification dates at time step %d of %s and %s differ!", otsID+1, cdoStreamName(1)->args, cdoStreamName(2)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
139
140
141
142
143
144

      for ( recID = 0; recID < nrecs; recID++ )
        {
          streamInqRecord(streamID2, &varID, &levelID);
	  streamReadRecord(streamID2, vars1[varID][levelID].ptr, &nmiss);
          vars1[varID][levelID].nmiss = nmiss;
        }
145

Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
148
149
150
151
152
153
154
155
156
      for ( recID = 0; recID < nrecs; recID++ )
        {
          streamInqRecord(streamID3, &varID, &levelID);
	  streamReadRecord(streamID3, field.ptr, &nmiss);
          field.nmiss   = nmiss;
          field.grid    = vars1[varID][levelID].grid;
	  field.missval = vars1[varID][levelID].missval;
	  
	  hsetDefVarLevelBounds(hset, varID, levelID, &vars1[varID][levelID], &field);
        }

157
158
      nsets   = 0;
      newseas = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
160
      while ( nrecs && (nrecs = streamInqTimestep(streamID1, tsID)) )
	{
161
162
163
	  dtlist_taxisInqTimestep(dtlist, taxisID1, nsets);
	  vdate1 = dtlist_get_vdate(dtlist, nsets);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
	  cdiDecodeDate(vdate1, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165

166
167
	  newmon = month;

168
169
170
	  if ( season_start == START_DEC && newmon == 12 ) newmon = 0;

          seas = month_to_season(month);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

	  if ( nsets == 0 )
	    {
	      seas0 = seas;
	      oldmon = newmon;
	    }

	  if ( newmon < oldmon ) newseas = TRUE;

	  if ( (seas != seas0) || newseas ) break;

	  oldmon = newmon;

	  for ( recID = 0; recID < nrecs; recID++ )
	    {
	      streamInqRecord(streamID1, &varID, &levelID);
	      if ( tsID == 0 )
		{
		  recVarID[recID]   = varID;
		  recLevelID[recID] = levelID;
		}
	      streamReadRecord(streamID1, vars1[varID][levelID].ptr, &nmiss);
	      vars1[varID][levelID].nmiss = nmiss;
	      
	      hsetAddVarLevelValues(hset, varID, levelID, &vars1[varID][levelID]);
	    }

	  nsets++;
	  tsID++;
	}

      if ( nrecs == 0 && nsets == 0 ) break;

      for ( varID = 0; varID < nvars; varID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
	  if ( vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
209
210
211
212
	  nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
	  
	  for ( levelID = 0; levelID < nlevels; levelID++ )
            hsetGetVarLevelPercentiles(&vars1[varID][levelID], hset, varID, levelID, pn);
	}

213
      dtlist_stat_taxisDefTimestep(dtlist, taxisID4, nsets);
214
      streamDefTimestep(streamID4, otsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
217
218
219
220

      for ( recID = 0; recID < nrecords; recID++ )
	{
	  varID   = recVarID[recID];
	  levelID = recLevelID[recID];

Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
	  if ( otsID && vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
222
223
224

	  streamDefRecord(streamID4, varID, levelID);
	  streamWriteRecord(streamID4, vars1[varID][levelID].ptr, vars1[varID][levelID].nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
226
227
	}

      if ( nrecs == 0 ) break;
228
      otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230
231
232
233
234
    }

  for ( varID = 0; varID < nvars; varID++ )
    {
      nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
      for ( levelID = 0; levelID < nlevels; levelID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
        Free(vars1[varID][levelID].ptr);
236
      Free(vars1[varID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
    }

239
  Free(vars1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
241
  hsetDestroy(hset);

242
243
  dtlist_delete(dtlist);

244
  if ( field.ptr ) Free(field.ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245

246
247
  if ( recVarID   ) Free(recVarID);
  if ( recLevelID ) Free(recLevelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
250
251
252
253
254
255

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

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
}