Consecstat.c 10 KB
Newer Older
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
29
30
31
32
33
34
35
36
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

  Copyright (C) 2003-2010 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
  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:

      Consectstep  consecsum  For each timestep, the current number of 
                              onsecutive timsteps is counted
      Consectstep  consects   For each period of consecutive timesteps, only
                              count its lenght + last contributing timesteps

   =============================================================================
   Created:  04/08/2010 11:58:01 AM
    Author:  Ralf Mueller (ram), ralf.mueller@zmaw.de
   Company:  Max-Planck-Institute for Meteorology
   =============================================================================
 */

#if defined (_OPENMP)
#  include <omp.h>
#endif

Ralf Mueller's avatar
Ralf Mueller committed
37
#include <cdi.h>
38
39
40
41
42
43
44
45
46
47
#include "cdo.h"
#include "cdo_int.h"
#include "field.h"
#include "pstream.h"

enum {CONSECSUM, CONSECTS};
#define SWITCHWARN "Hit default case!This should never happen (%s).\n"

static void selEndOfPeriod(field_t *periods, field_t history, field_t current, int isLastTimestep)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
  static const char *func = "selEndOfPeriod";
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  long   i, len;
  double pmissval = periods->missval;
  double  *parray = periods->ptr;
  double hmissval = history.missval;
  double  *harray = history.ptr;
  double cmissval = current.missval;
  double  *carray = current.ptr;

  len = gridInqSize(periods->grid);
  if ( len != gridInqSize(current.grid) || (gridInqSize(current.grid) != gridInqSize(history.grid)) )
    cdoAbort("Fields have different gridsize (%s)", func);

  if (!isLastTimestep)
  {
    if ( current.nmiss > 0 || history.nmiss > 0 )
    {
#if defined (_OPENMP)
66
#pragma omp parallel for default(shared)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#endif
      for ( i = 0; i < len; i++ )
      {
        if ( !DBL_IS_EQUAL(harray[i], hmissval) )
        {
          if ( !DBL_IS_EQUAL(carray[i], cmissval) )
          {
            parray[i] = ( DBL_IS_EQUAL(carray[i], 0.0)  && IS_NOT_EQUAL(harray[i], 0.0) ) ? harray[i] : pmissval;
          }
          else /* DBL_IS_EQUAL(carray[i], cmissval) */
          {
            parray[i] = ( IS_NOT_EQUAL(harray[i], 0.0) ) ? harray[i] : pmissval;
          }
        }
        else /* DBL_IS_EQUAL(harray[i], hmissval) */
        {
          parray[i] = pmissval;
        }
      }
    }
    else
    {
#if defined (_OPENMP)
90
#pragma omp parallel for default(shared)
91
92
93
94
95
96
97
98
99
100
#endif
      for ( i = 0; i < len; i++ )
        parray[i] = ( DBL_IS_EQUAL(carray[i], 0.0)  && IS_NOT_EQUAL(harray[i], 0.0) ) ? harray[i] : pmissval;
    }
  }
  else
  {
    if ( current.nmiss > 0 )
    {
#if defined (_OPENMP)
101
#pragma omp parallel for default(shared)
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#endif
      for ( i = 0; i < len; i++ )
        if ( !DBL_IS_EQUAL(carray[i], cmissval) )
        {
          parray[i] = ( DBL_IS_EQUAL(carray[i], 0.0) ) ? pmissval : carray[i];
        }
        else /* DBL_IS_EQUAL(carray[i], cmissval) */
        {
          parray[i] = pmissval;
        }
    }
    else
    {
#if defined (_OPENMP)
116
#pragma omp parallel for default(shared)
117
118
119
120
121
122
123
124
125
126
127
128
129
#endif
      for ( i = 0; i < len; i++ )
        parray[i] = ( DBL_IS_EQUAL(carray[i], 0.0) ) ? pmissval : carray[i];
    }
  }

  periods->nmiss = 0;
  for ( i = 0; i < len; i++ )
    if ( DBL_IS_EQUAL(parray[i], pmissval) ) periods->nmiss++;
}

void *Consecstat (void *argument)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
  static const char *func = "Consecstat";
131
132
133
134
135
  int operatorID;

  int i;
  int istreamID, itaxisID, ivlistID, itsID;
  int ostreamID, otaxisID, ovlistID, otsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
  int vdate = 0, vtime = 0;
137
138
139
140
141
142
  int histvdate = 0, histvtime = 0;
  int recID, nrecs;
  int varID, nvars;
  int levelID, nlevels; 
  int gridID, gridsize;
  double missval;
Ralf Mueller's avatar
Ralf Mueller committed
143
  double refval = 0.0;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

  field_t **vars = NULL, **hist = NULL, **periods = NULL;
  field_t field;

  cdoInitialize(argument);
  cdoOperatorAdd("consecsum",CONSECSUM, 0, "refval");
  cdoOperatorAdd("consects" ,CONSECTS , 0, NULL);
  operatorID = cdoOperatorID();

  if ( operatorID == CONSECSUM )
    if ( operatorArgc() > 0 ) refval = atof(operatorArgv()[0]);

  istreamID = streamOpenRead(cdoStreamName(0));
  if ( istreamID < 0 ) cdiError(istreamID, "Open failed on %s", cdoStreamName(0));

  ivlistID = streamInqVlist(istreamID);
  itaxisID = vlistInqTaxis(ivlistID);
  ovlistID = vlistDuplicate(ivlistID);
  otaxisID = taxisDuplicate(itaxisID);
  vlistDefTaxis(ovlistID, otaxisID);

  field.ptr = (double *) malloc(vlistGridsizeMax(ovlistID)*sizeof(double));
  nvars     = vlistNvars(ivlistID);
167
168
  vars      = (field_t **) malloc(nvars*sizeof(field_t *));
  hist      = (field_t **) malloc(nvars*sizeof(field_t *));
169
  if ( operatorID == CONSECTS )
170
    periods   = (field_t **) malloc(nvars*sizeof(field_t *));
171
172

  for ( varID = 0; varID < nvars; varID++ )
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
206
  {
    gridID      = vlistInqVarGrid(ovlistID, varID);
    gridsize    = gridInqSize(gridID);
    missval     = vlistInqVarMissval(ovlistID, varID);
    nlevels     = zaxisInqSize(vlistInqVarZaxis(ovlistID, varID));

    vars[varID] = (field_t *) malloc(nlevels*sizeof(field_t));
    if ( operatorID == CONSECTS )
    {
      periods[varID] = (field_t *) malloc(nlevels*sizeof(field_t));
      hist[varID]    = (field_t *) malloc(nlevels*sizeof(field_t));
    }


    for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      vars[varID][levelID].grid    = gridID;
      vars[varID][levelID].missval = missval;
      vars[varID][levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
      vars[varID][levelID].nmiss   = 0;
      if ( operatorID == CONSECTS )
      {
        hist[varID][levelID].grid       = gridID;
        hist[varID][levelID].missval    = missval;
        hist[varID][levelID].ptr        = (double *) malloc(gridsize*sizeof(double));
        hist[varID][levelID].nmiss      = 0;
        periods[varID][levelID].grid    = gridID;
        periods[varID][levelID].missval = missval;
        periods[varID][levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
        periods[varID][levelID].nmiss   = 0;
      }
    }
    vlistDefVarUnits(ovlistID, varID, "steps"); /* TODO */
  }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

  ostreamID = streamOpenWrite(cdoStreamName(1), cdoFiletype());
  if ( ostreamID < 0 ) cdiError(ostreamID, "Open failed on %s", cdoStreamName(1));

  streamDefVlist(ostreamID, ovlistID);

  itsID = 0;
  otsID = 0;
  while ( (nrecs = streamInqTimestep(istreamID, itsID)) )
  {
    vdate = taxisInqVdate(itaxisID);
    vtime = taxisInqVtime(itaxisID);
    switch (operatorID)
    {
      case CONSECSUM:
        taxisDefVdate(otaxisID, vdate);
        taxisDefVtime(otaxisID, vtime);
        streamDefTimestep(ostreamID, otsID);
        break;
      case CONSECTS:
        if (itsID != 0 )
        {
          taxisDefVdate(otaxisID, histvdate);
          taxisDefVtime(otaxisID, histvtime);
          streamDefTimestep(ostreamID, otsID-1);
        }
        break;
      default:
        printf (SWITCHWARN,func);
    }

    for ( recID = 0; recID < nrecs; recID++ )
    {
      streamInqRecord(istreamID, &varID, &levelID);
      streamReadRecord(istreamID, field.ptr, &field.nmiss);
      field.grid    = vlistInqVarGrid(ovlistID, varID);
      field.missval = vlistInqVarMissval(ovlistID, varID);
      farsumtr(&vars[varID][levelID], field, refval);
      switch (operatorID)
      {
        case CONSECSUM:
          streamDefRecord(ostreamID, varID, levelID);
          streamWriteRecord(ostreamID, vars[varID][levelID].ptr, vars[varID][levelID].nmiss);
          break;
        case CONSECTS:
          if ( itsID != 0 )
          {
            selEndOfPeriod(&periods[varID][levelID], hist[varID][levelID], vars[varID][levelID], FALSE);
            streamDefRecord(ostreamID, varID, levelID);
            streamWriteRecord(ostreamID, periods[varID][levelID].ptr, periods[varID][levelID].nmiss);
          }
258
259
260
261
262
#if defined (_OPENMP)
#pragma omp parallel for default(shared) schedule(static)
          for ( i = 0; i < gridInqSize(vars[varID][levelID].grid); i++ )
            hist[varID][levelID].ptr[i] = vars[varID][levelID].ptr[i];
#else
263
264
265
          memcpy(hist[varID][levelID].ptr,
                 vars[varID][levelID].ptr,
                 gridInqSize(vars[varID][levelID].grid)*sizeof(double));
266
#endif
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
          break;
        default:
          printf (SWITCHWARN,func);
      }
    }
    histvdate = vdate;
    histvtime = vtime;
    itsID++;
    otsID++;
  }

  if ( operatorID == CONSECTS ) /* Save the last timestep */
  {
    taxisDefVdate(otaxisID, vdate);
    taxisDefVtime(otaxisID, vtime);
    streamDefTimestep(ostreamID, otsID-1);
    for ( varID = 0; varID < nvars; varID++ )
    {
      nlevels = zaxisInqSize(vlistInqVarZaxis(ovlistID, varID));
      for ( levelID = 0; levelID < nlevels; levelID++ )
      {
        selEndOfPeriod(&periods[varID][levelID], hist[varID][levelID], vars[varID][levelID], TRUE);
        streamDefRecord(ostreamID, varID, levelID);
        streamWriteRecord(ostreamID, periods[varID][levelID].ptr, periods[varID][levelID].nmiss);
      }
    }
  }
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  
  for ( varID = 0; varID < nvars; varID++ )
  {
    nlevels = zaxisInqSize(vlistInqVarZaxis(ovlistID, varID));
    for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      if ( vars[varID][levelID].ptr ) free(vars[varID][levelID].ptr);
      if ( operatorID == CONSECTS )
      {
        if ( hist[varID][levelID].ptr ) free(hist[varID][levelID].ptr);
        if ( periods[varID][levelID].ptr ) free(periods[varID][levelID].ptr);
      }
    }
    free(vars[varID]);
    if ( operatorID == CONSECTS )
    {
      free(hist[varID]);
      free(periods[varID]);
    }
  }
  if ( vars )    free(vars);
  if ( hist )    free(hist);
  if ( periods ) free(periods);
317
318
319
320
321
322
323
324

  streamClose(istreamID);
  streamClose(ostreamID);

  cdoFinish();

  return (0);
}