Timpctl.c 6.89 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

  Copyright (C) 2006 Brockmann Consult
  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:

      Timpctl    timpctl         Time percentiles
      Hourpctl   hourpctl        Hourly percentiles
      Daypctl    daypctl         Daily percentiles
      Monpctl    monpctl         Monthly percentiles
      Yearpctl   yearpctl        Yearly percentiles
*/


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


36
37
static
void timpctl(int operatorID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
{
39
  int timestat_date = TIMESTAT_MEAN;
40
  char indate1[DATE_LEN+1], indate2[DATE_LEN+1];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
42
43
  int vdate1 = 0, vtime1 = 0;
  int vdate2 = 0, vtime2 = 0;
  int vdate3 = 0, vtime3 = 0;
44
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
46
47
48
49
  int gridID, varID, levelID, recID;
  int tsID;
  int otsID;
  long nsets;
  int nmiss;
50
  int nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
  field_t **vars1 = NULL;
  field_t field;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
55
  HISTOGRAM_SET *hset = NULL;
  
  operatorInputArg("percentile number");
56
  double pn = parameter2double(operatorArgv()[0]);
57
  percentile_check_number(pn);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58

59
  int cmplen = DATE_LEN - cdoOperatorF2(operatorID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60

61
62
63
  int streamID1 = streamOpenRead(cdoStreamName(0));
  int streamID2 = streamOpenRead(cdoStreamName(1));
  int streamID3 = streamOpenRead(cdoStreamName(2));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
  
65
66
67
68
  int vlistID1 = streamInqVlist(streamID1);
  int vlistID2 = streamInqVlist(streamID2);
  int vlistID3 = streamInqVlist(streamID3);
  int vlistID4 = vlistDuplicate(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69

Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
71
  vlistCompare(vlistID1, vlistID2, CMP_ALL);
  vlistCompare(vlistID1, vlistID3, CMP_ALL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
  
73
  if ( cdoOperatorF2(operatorID) == 16 ) vlistDefNtsteps(vlistID4, 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74

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

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

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

  streamDefVlist(streamID4, vlistID4);

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

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

93
94
95
96
97
  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
98

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

102
  vars1 = field_malloc(vlistID1, FIELD_PTR);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
105
106
107
108
109
110
111
112
113
114
115
  hset = hsetCreate(nvars);
  
  for ( varID = 0; varID < nvars; varID++ )
    {
      gridID   = vlistInqVarGrid(vlistID1, varID);
      nlevels  = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));

      hsetCreateVarLevels(hset, varID, nlevels, gridID);
    }

  tsID    = 0;
  otsID   = 0;
  while ( TRUE )
116
    {      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
      nrecs = streamInqTimestep(streamID2, otsID);
      if ( nrecs != streamInqTimestep(streamID3, otsID) )
119
        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
120
121
122
123
124
125

      vdate2 = taxisInqVdate(taxisID2);
      vtime2 = taxisInqVtime(taxisID2);
      vdate3 = taxisInqVdate(taxisID3);
      vtime3 = taxisInqVtime(taxisID3);
      if ( vdate2 != vdate3 || vtime2 != vtime3 )
126
        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
127
128
129
130
131
132
133
      
      for ( recID = 0; recID < nrecs; recID++ )
        {
          streamInqRecord(streamID2, &varID, &levelID);
	  streamReadRecord(streamID2, vars1[varID][levelID].ptr, &nmiss);
          vars1[varID][levelID].nmiss = nmiss;
        }
134

Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
138
139
140
141
142
143
144
145
      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);
        }
          
146
      nsets = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
148
      while ( nrecs && (nrecs = streamInqTimestep(streamID1, tsID)) )
	{
149
150
151
	  dtlist_taxisInqTimestep(dtlist, taxisID1, nsets);
	  vdate1 = dtlist_get_vdate(dtlist, nsets);
	  vtime1 = dtlist_get_vtime(dtlist, nsets);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152

153
154
155
156
	  if ( nsets == 0 ) SET_DATE(indate2, vdate1, vtime1);
	  SET_DATE(indate1, vdate1, vtime1);

	  if ( DATE_IS_NEQ(indate1, indate2, cmplen) ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

	  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
180
	  if ( vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
183
184
185
186
	  nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
	  
	  for ( levelID = 0; levelID < nlevels; levelID++ )
            hsetGetVarLevelPercentiles(&vars1[varID][levelID], hset, varID, levelID, pn);
	}

187
      dtlist_stat_taxisDefTimestep(dtlist, taxisID4, nsets);
188
      streamDefTimestep(streamID4, otsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
191
192
193
194

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
	  if ( otsID && vlistInqVarTsteptype(vlistID1, varID) == TSTEP_CONSTANT ) continue;
196
197
198

	  streamDefRecord(streamID4, varID, levelID);
	  streamWriteRecord(streamID4, vars1[varID][levelID].ptr, vars1[varID][levelID].nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
200
201
	}

      if ( nrecs == 0 ) break;
202
      otsID++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
204
    }

205
  field_free(vars1, vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
  hsetDestroy(hset);
207
208

  dtlist_delete(dtlist);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
  
210
  if ( field.ptr ) Free(field.ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211

212
213
  if ( recVarID   ) Free(recVarID);
  if ( recLevelID ) Free(recLevelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
215
216
217
218
219
220
221
222
223
224
225
226

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

void *Timpctl(void *argument)
{
  int operatorID;
  
  cdoInitialize(argument);

227
228
229
230
231
  cdoOperatorAdd("timpctl",  func_pctl, 31, NULL);
  cdoOperatorAdd("yearpctl", func_pctl, 10, NULL);
  cdoOperatorAdd("monpctl",  func_pctl,  8, NULL);
  cdoOperatorAdd("daypctl",  func_pctl,  6, NULL);
  cdoOperatorAdd("hourpctl", func_pctl,  4, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235

  operatorID = cdoOperatorID();
  
  timpctl(operatorID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236

Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
}