ecacore.c 51.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
  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.
*/

#include <assert.h>
#include <string.h>

Ralf Mueller's avatar
Ralf Mueller committed
21
#include <cdi.h>
22
23
#include "cdo.h"
#include "cdo_int.h"
24
#include "grid.h"
25
26
#include "pstream.h"
#include "dtypes.h"
27
28
29
#if defined (_OPENMP)
#include <omp.h>
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
#include "ecacore.h"
31
32
33
34
35
36
37
38
39
40
41
42
#include "ecautil.h"

#define FIRST_VAR_ID 0

#define IS_NOT_SET(x) (x == NULL)
#define IS_SET(x)     (x != NULL)


void eca1(const ECA_REQUEST_1 *request)
{
  const int operatorID = cdoOperatorID();
  
43
44
  int cmplen;
  char indate1[DATE_LEN+1], indate2[DATE_LEN+1];
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  int gridsize;
  int ivdate = 0, ivtime = 0;
  int ovdate = 0, ovtime = 0;
  int nrecs, nrecords;
  int gridID, zaxisID, varID, levelID, recID;
  int itsID;
  int otsID;
  long nsets;
  int i;
  int istreamID, ostreamID;
  int ivlistID, ovlistID, itaxisID, otaxisID;
  int nlevels;
  int *recVarID, *recLevelID;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
  field_t *var12 = NULL, *samp1 = NULL, *samp2 = NULL, *var13 = NULL, *var21 = NULL, *var23 = NULL, *var;
  field_t field1, field2;
61
  
62
  cmplen = DATE_LEN - cdoOperatorF2(operatorID);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

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

  ivlistID = streamInqVlist(istreamID);
  ovlistID = vlistCreate();
  
  gridID  = vlistInqVarGrid(ivlistID, FIRST_VAR_ID);
  zaxisID = vlistInqVarZaxis(ivlistID, FIRST_VAR_ID);
  varID   = vlistDefVar(ovlistID, gridID, zaxisID, TIME_VARIABLE);
  
  if ( IS_SET(request->var1.name) )
    vlistDefVarName(ovlistID, varID, request->var1.name);
  if ( IS_SET(request->var1.longname) ) 
    vlistDefVarLongname(ovlistID, varID, request->var1.longname);
  if ( IS_SET(request->var1.units) ) 
    vlistDefVarUnits(ovlistID, varID, request->var1.units);

  if ( IS_SET(request->var2.h2) || IS_SET(request->var2.h3) )
    {
      varID = vlistDefVar(ovlistID, gridID, zaxisID, TIME_VARIABLE);
  
      if ( IS_SET(request->var2.name) ) 
        vlistDefVarName(ovlistID, varID, request->var2.name);
      if ( IS_SET(request->var2.longname) ) 
        vlistDefVarLongname(ovlistID, varID, request->var2.longname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
90
      if ( IS_SET(request->var2.units) ) 
        vlistDefVarUnits(ovlistID, varID, request->var2.units);
91
92
    }
    
93
  if ( cdoOperatorF2(operatorID) == 16 ) vlistDefNtsteps(ovlistID, 1);
94
95

  itaxisID = vlistInqTaxis(ivlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
  otaxisID = taxisCreate(TAXIS_RELATIVE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
  taxisDefTunit(otaxisID, TUNIT_MINUTE);
98
  taxisDefCalendar(otaxisID, CALENDAR_PROLEPTIC);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
100
  taxisDefRdate(otaxisID, 19550101);
  taxisDefRtime(otaxisID, 0);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  vlistDefTaxis(ovlistID, otaxisID);

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

  streamDefVlist(ostreamID, ovlistID);

  nrecords   = vlistNrecs(ivlistID);
  recVarID   = (int *) malloc(nrecords*sizeof(int));
  recLevelID = (int *) malloc(nrecords*sizeof(int));

  gridsize = gridInqSize(gridID);

  field1.ptr = (double *) malloc(gridsize*sizeof(double));
  if ( IS_SET(request->var2.h2) || IS_SET(request->var2.h3) ) 
    field2.ptr = (double *) malloc(gridsize*sizeof(double));
  else
    field2.ptr = NULL;

  nlevels = zaxisInqSize(zaxisID);
  missval = vlistInqVarMissval(ivlistID, FIRST_VAR_ID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
  var12 = (field_t *) malloc(nlevels*sizeof(field_t));
  samp1 = (field_t *) malloc(nlevels*sizeof(field_t));
  samp2 = (field_t *) malloc(nlevels*sizeof(field_t));
126
  if ( IS_SET(request->var1.f3) ) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
    var13 = (field_t *) malloc(nlevels*sizeof(field_t));
128
129
    
  if ( IS_SET(request->var2.h2) ) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
    var21 = (field_t *) malloc(nlevels*sizeof(field_t));
131
  if ( IS_SET(request->var2.h3) ) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
    var23 = (field_t *) malloc(nlevels*sizeof(field_t));
133
134
135
136
137
138
139
140
141
142
143
      
  for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      var12[levelID].grid    = gridID;
      var12[levelID].nmiss   = 0;
      var12[levelID].missval = missval;
      var12[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));

      samp1[levelID].grid    = gridID;
      samp1[levelID].nmiss   = 0;
      samp1[levelID].missval = missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
146
147
148
149
      samp1[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));

      samp2[levelID].grid    = gridID;
      samp2[levelID].nmiss   = 0;
      samp2[levelID].missval = missval;
      samp2[levelID].ptr     = NULL;
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

      if ( IS_SET(request->var1.f3) )
        {
          var13[levelID].grid    = gridID;
          var13[levelID].nmiss   = 0;
          var13[levelID].missval = missval;
          var13[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
        }
      if ( IS_SET(request->var2.h2) )
        {
          var21[levelID].grid    = gridID;
          var21[levelID].nmiss   = 0;
          var21[levelID].missval = missval;
          var21[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
        }
      if ( IS_SET(request->var2.h3) )
        {
          var23[levelID].grid    = gridID;
          var23[levelID].nmiss   = 0;
          var23[levelID].missval = missval;
          var23[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
        }
    }

  itsID   = 0;
  otsID   = 0;
  while ( TRUE )
    {
      nsets = 0;
      while ( (nrecs = streamInqTimestep(istreamID, itsID)) > 0 )
        {
          ivdate = taxisInqVdate(itaxisID);
          ivtime = taxisInqVtime(itaxisID);

184
185
	  if ( nsets == 0 ) SET_DATE(indate2, ivdate, ivtime);
	  SET_DATE(indate1, ivdate, ivtime);
186

187
	  if ( DATE_IS_NEQ(indate1, indate2, cmplen) ) break;
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

          for ( recID = 0; recID < nrecs; recID++ )
            {
              streamInqRecord(istreamID, &varID, &levelID);

              if ( itsID == 0 )
                {
                  recVarID[recID]   = varID;
                  recLevelID[recID] = levelID;
                }
              if ( varID != FIRST_VAR_ID ) continue;

              if ( nsets == 0 )
                {
                  for ( i = 0; i < gridsize; i++ )
                    {
                      var12[levelID].ptr[i] = missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
207
                      samp1[levelID].ptr[i] = missval;
                      if ( IS_SET(samp2[levelID].ptr) )
                        samp2[levelID].ptr[i] = 0.0;
208
209
210
211
212
213
214
215
                      if ( IS_SET(request->var1.f3) ) 
                        var13[levelID].ptr[i] = missval;
                      if ( IS_SET(request->var2.h2) )
                        var21[levelID].ptr[i] = missval;
                      if ( IS_SET(request->var2.h3) )
                        var23[levelID].ptr[i] = missval;
                    }
                  var12[levelID].nmiss = gridsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
                  samp1[levelID].nmiss = gridsize;
217
218
219
220
221
222
223
224
225
226
227
                  if ( IS_SET(request->var1.f3) )
                    var13[levelID].nmiss = gridsize;
                  if ( IS_SET(request->var2.h2) )
                    var21[levelID].nmiss = gridsize; 
                  if ( IS_SET(request->var2.h3) )
                    var23[levelID].nmiss = gridsize; 
                }

              streamReadRecord(istreamID, field1.ptr, &field1.nmiss);
              field1.grid    = var12[levelID].grid;
              field1.missval = var12[levelID].missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
              
              farnum(&samp1[levelID], field1);
230
231
232
233
234
235
236
237
238
239
240
241

              if ( IS_SET(request->var2.h2) )
                {
                  memcpy(field2.ptr, field1.ptr, gridsize*sizeof(double));
                  field2.nmiss   = field1.nmiss;
                  field2.grid    = field1.grid;
                  field2.missval = field1.missval;
                }
		
              if ( IS_SET(request->var1.f1) ) 
                request->var1.f1(&field1, request->var1.f1arg);
              
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
              if ( field1.nmiss > 0 || IS_SET(samp2[levelID].ptr) )
243
                {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
                  if ( IS_NOT_SET(samp2[levelID].ptr) )
245
                    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
                      samp2[levelID].ptr = (double *) malloc(gridsize*sizeof(double));
247
                      for ( i = 0; i < gridsize; i++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
                        samp2[levelID].ptr[i] = nsets;
249
250
251
252
253
                    }
                  for ( i = 0; i < gridsize; i++ )
                    {
                      if ( DBL_IS_EQUAL(field1.ptr[i], field1.missval) )
                        continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
                      samp2[levelID].ptr[i]++;
255
256
257
                    }
                }
              
258
              if ( IS_NOT_EQUAL(request->var1.mulc, 0.0) )
259
                farcmul(&field1, request->var1.mulc);
260
              if ( IS_NOT_EQUAL(request->var1.addc, 0.0) )
261
262
263
264
265
266
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
                farcadd(&field1, request->var1.addc);
                
              request->var1.f2(&var12[levelID], field1);
              
              if ( IS_SET(request->var2.h2) || IS_SET(request->var2.h3) )
                { 
                  /* if h2 is null, use the output of f2 as input for h1 */
                  if ( IS_NOT_SET(request->var2.h2) )
                    {
                      memcpy(field2.ptr, var12[levelID].ptr, gridsize*sizeof(double));
                      field2.nmiss   = var12[levelID].nmiss;
                      field2.grid    = var12[levelID].grid;
                      field2.missval = var12[levelID].missval;
                    }
                  
                  if ( IS_SET(request->var2.h1) ) 
                    request->var2.h1(&field2, request->var2.h1arg);
                        
                  if ( IS_NOT_SET(request->var2.h2) )    
                    request->var2.h3(&var23[levelID], field2);
                  else
                    {
                      request->var2.h2(&var21[levelID], field2);
                      if ( IS_SET(request->var2.h3) )
                        request->var2.h3(&var23[levelID], var21[levelID]);
                    }
                }

              if ( IS_SET(request->var1.f3) )
                request->var1.f3(&var13[levelID], var12[levelID]);
            }

          ovdate = ivdate;
          ovtime = ivtime;
          nsets++;
          itsID++;
        }

      if ( nrecs == 0 && nsets == 0 ) break;
      
      if ( request->var1.epilog == MEAN || request->var1.epilog == PERCENT_OF_TIME )
        for ( levelID = 0; levelID < nlevels; levelID++ )
          {
            if ( IS_SET(request->var1.f3) )
              var = &var13[levelID];
            else 
              var = &var12[levelID];
              
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
            if ( IS_NOT_SET(samp2[levelID].ptr) )
310
311
              farcdiv(var, nsets);
            else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
              fardiv(var, samp2[levelID]);
313
314
315
316
317
318
319
              
            if ( request->var1.epilog == PERCENT_OF_TIME )
              farcmul(var, 100.0);
          }

      taxisDefVdate(otaxisID, ovdate);
      taxisDefVtime(otaxisID, ovtime);
320
      streamDefTimestep(ostreamID, otsID);
321

322
323
324
325
326
327
328
329
330
      if ( otsID && vlistInqVarTime(ivlistID, FIRST_VAR_ID) == TIME_CONSTANT ) continue;

      varID = 0;
      for ( levelID = 0; levelID < nlevels; levelID++ )
	{
	  if ( IS_SET(request->var1.f3) )
	    var = &var13[levelID];
	  else 
	    var = &var12[levelID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
              
332
	  farsel(var, samp1[levelID]);
333
               
334
335
336
337
338
339
340
341
342
343
344
345
	  streamDefRecord(ostreamID, varID, levelID);
	  streamWriteRecord(ostreamID, var->ptr, var->nmiss);
	}
      if ( IS_SET(request->var2.h2) || IS_SET(request->var2.h3) )
	{
	  varID = 1;
	  for ( levelID = 0; levelID < nlevels; levelID++ )
	    {
	      if ( IS_SET(request->var2.h3) )
		var = &var23[levelID];
	      else 
		var = &var21[levelID];
346
                  
347
	      farsel(var, samp1[levelID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
                  
349
350
351
	      streamDefRecord(ostreamID, varID, levelID);
	      streamWriteRecord(ostreamID, var->ptr, var->nmiss);
	    }
352
353
354
        }

      if ( nrecs == 0 ) break;
355
      otsID++;
356
357
358
359
360
    }

  for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      free(var12[levelID].ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
362
      free(samp1[levelID].ptr);
      if ( IS_SET(samp2[levelID].ptr) ) free(samp2[levelID].ptr);
363
364
365
    }  
  free(var12);
  free(samp1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
  free(samp2);
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  
  if ( IS_SET(var13) ) 
    {
      for ( levelID = 0; levelID < nlevels; levelID++ )
        free(var13[levelID].ptr);
      free(var13);
    }
  if ( IS_SET(var21) ) 
    {
      for ( levelID = 0; levelID < nlevels; levelID++ )
        free(var21[levelID].ptr);
      free(var21);
    }
  if ( IS_SET(var23) ) 
    {
      for ( levelID = 0; levelID < nlevels; levelID++ )
        free(var23[levelID].ptr);
      free(var23);
    }
    
  if ( IS_SET(field1.ptr) ) free(field1.ptr);
  if ( IS_SET(field2.ptr) ) free(field2.ptr);

  if ( IS_SET(recVarID) )   free(recVarID);
  if ( IS_SET(recLevelID) ) free(recLevelID);

  streamClose(ostreamID);
  streamClose(istreamID);
}


void eca2(const ECA_REQUEST_2 *request)
{
  const int operatorID = cdoOperatorID();
  
402
403
  int cmplen;
  char indate1[DATE_LEN+1], indate2[DATE_LEN+1];
404
  int gridsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
  int ivdate = 0, ivtime = 0;
406
407
408
409
410
411
412
413
414
415
416
  int ovdate = 0, ovtime = 0;
  int nrecs, nrecords;
  int gridID, zaxisID, varID, levelID, recID;
  int itsID;
  int otsID;
  long nsets;
  int i;
  int istreamID1, istreamID2, ostreamID;
  int ivlistID1, ivlistID2, ovlistID, itaxisID1, itaxisID2, otaxisID;
  int nlevels;
  int *recVarID, *recLevelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
417
  double missval1, missval2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
419
  field_t *var14 = NULL, *samp1 = NULL, *samp2 = NULL, *samp3 = NULL, *total = NULL, *var15 = NULL, *var22 = NULL, *var;
  field_t field1, field2;
420
  
421
  cmplen = DATE_LEN - cdoOperatorF2(operatorID);
422
423
424
425
426
427
428
429
430
431

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

  ivlistID1 = streamInqVlist(istreamID1);
  ivlistID2 = streamInqVlist(istreamID2);
  ovlistID  = vlistCreate();
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
433
  vlistCompare(ivlistID1, ivlistID2, CMP_HRD);
  vlistCompare(ivlistID1, ivlistID2, CMP_SFT);
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  
  gridID  = vlistInqVarGrid(ivlistID1, FIRST_VAR_ID);
  zaxisID = vlistInqVarZaxis(ivlistID1, FIRST_VAR_ID);
  varID   = vlistDefVar(ovlistID, gridID, zaxisID, TIME_VARIABLE);
  
  if ( IS_SET(request->var1.name) ) 
    vlistDefVarName(ovlistID, varID, request->var1.name);
  if ( IS_SET(request->var1.longname) ) 
    vlistDefVarLongname(ovlistID, varID, request->var1.longname);
  if ( IS_SET(request->var1.units) ) 
    vlistDefVarUnits(ovlistID, varID, request->var1.units);

  if ( IS_SET(request->var2.h2) )
    {
      varID = vlistDefVar(ovlistID, gridID, zaxisID, TIME_VARIABLE);
  
      if ( IS_SET(request->var2.name) ) 
        vlistDefVarName(ovlistID, varID, request->var2.name);
      if ( IS_SET(request->var2.longname) ) 
        vlistDefVarLongname(ovlistID, varID, request->var2.longname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
      if ( IS_SET(request->var2.units) ) 
        vlistDefVarUnits(ovlistID, varID, request->var2.units);
456
457
    }

458
  if ( cdoOperatorF2(operatorID) == 16 ) vlistDefNtsteps(ovlistID, 1);
459
460
461

  itaxisID1 = vlistInqTaxis(ivlistID1);
  itaxisID2 = vlistInqTaxis(ivlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
462
  otaxisID = taxisCreate(TAXIS_RELATIVE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
  taxisDefTunit(otaxisID, TUNIT_MINUTE);
464
  taxisDefCalendar(otaxisID, CALENDAR_PROLEPTIC);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
466
  taxisDefRdate(otaxisID, 19550101);
  taxisDefRtime(otaxisID, 0);
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  vlistDefTaxis(ovlistID, otaxisID);

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

  streamDefVlist(ostreamID, ovlistID);

  nrecords   = vlistNrecs(ivlistID1);
  recVarID   = (int *) malloc(nrecords*sizeof(int));
  recLevelID = (int *) malloc(nrecords*sizeof(int));

  gridsize = gridInqSize(gridID);

  field1.ptr = (double *) malloc(gridsize*sizeof(double));
  field2.ptr = (double *) malloc(gridsize*sizeof(double));

  nlevels = zaxisInqSize(zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
485
486
  missval1 = vlistInqVarMissval(ivlistID1, FIRST_VAR_ID);
  missval2 = vlistInqVarMissval(ivlistID2, FIRST_VAR_ID);
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488
489
490
  var14 = (field_t *) malloc(nlevels*sizeof(field_t));
  samp1 = (field_t *) malloc(nlevels*sizeof(field_t));
  samp2 = (field_t *) malloc(nlevels*sizeof(field_t));
  samp3 = (field_t *) malloc(nlevels*sizeof(field_t));
491
492
  
  if ( request->var1.epilog == PERCENT_OF_TOTAL_AMOUNT )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
    total  = (field_t *) malloc(nlevels*sizeof(field_t));
494
  if ( IS_SET(request->var1.f5) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
    var15 = (field_t *) malloc(nlevels*sizeof(field_t));
496
  if ( IS_SET(request->var2.h2) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
497
    var22 = (field_t *) malloc(nlevels*sizeof(field_t));
498
499
500
501
502
      
  for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      var14[levelID].grid    = gridID;
      var14[levelID].nmiss   = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
503
      var14[levelID].missval = missval1;
504
505
506
507
      var14[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
      
      samp1[levelID].grid    = gridID;
      samp1[levelID].nmiss   = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
508
509
510
511
512
513
514
515
516
517
518
519
      samp1[levelID].missval = missval1;
      samp1[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));

      samp2[levelID].grid    = gridID;
      samp2[levelID].nmiss   = 0;
      samp2[levelID].missval = missval1;
      samp2[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));

      samp3[levelID].grid    = gridID;
      samp3[levelID].nmiss   = 0;
      samp3[levelID].missval = missval1;
      samp3[levelID].ptr     = NULL;
520
521
522
523
524
      
      if ( request->var1.epilog == PERCENT_OF_TOTAL_AMOUNT )
        {
          total[levelID].grid    = gridID;
          total[levelID].nmiss   = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
          total[levelID].missval = missval1;
526
527
528
529
530
531
          total[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
        }
      if ( IS_SET(request->var1.f5) )
        {
          var15[levelID].grid    = gridID;
          var15[levelID].nmiss   = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
532
          var15[levelID].missval = missval1;
533
534
535
536
537
538
          var15[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
        }
      if ( IS_SET(request->var2.h2) )
        {
          var22[levelID].grid    = gridID;
          var22[levelID].nmiss   = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
539
          var22[levelID].missval = missval1;
540
541
542
543
544
545
546
547
548
549
550
551
552
          var22[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
        }
    }

  itsID   = 0;
  otsID   = 0;
  while ( TRUE )
    {
      nsets = 0;
      while ( (nrecs = streamInqTimestep(istreamID1, itsID)) > 0 )
        {
	  if ( !streamInqTimestep(istreamID2, itsID) )
	    cdoAbort("Input streams have different number of time steps!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
553
554
555
556

          ivdate = taxisInqVdate(itaxisID1);
          ivtime = taxisInqVtime(itaxisID1);
          
557
558
	  if ( nsets == 0 ) SET_DATE(indate2, ivdate, ivtime);
	  SET_DATE(indate1, ivdate, ivtime);
559

560
	  if ( DATE_IS_NEQ(indate1, indate2, cmplen) ) break;
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577

          for ( recID = 0; recID < nrecs; recID++ )
            {
              streamInqRecord(istreamID1, &varID, &levelID);
              streamInqRecord(istreamID2, &varID, &levelID);

              if ( itsID == 0 )
                {
                  recVarID[recID]   = varID;
                  recLevelID[recID] = levelID;
                }
              if ( varID != FIRST_VAR_ID ) continue;

              if ( nsets == 0 )
                {
                  for ( i = 0; i < gridsize; i++ )
                    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
578
579
580
581
582
                      var14[levelID].ptr[i] = missval1;
                      samp1[levelID].ptr[i] = missval1;
                      samp2[levelID].ptr[i] = missval1;
                      if ( IS_SET(samp3[levelID].ptr) )
                        samp3[levelID].ptr[i] = 0.0;
583
584
585
                      if ( request->var1.epilog == PERCENT_OF_TOTAL_AMOUNT )
                        total[levelID].ptr[i] = 0.0;
                      if ( IS_SET(request->var1.f5) ) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
586
                        var15[levelID].ptr[i] = missval1;
587
                      if ( IS_SET(request->var2.h2) ) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
                        var22[levelID].ptr[i] = missval1;
589
590
                    }
                  var14[levelID].nmiss = gridsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
592
                  samp1[levelID].nmiss = gridsize;
                  samp2[levelID].nmiss = gridsize;
593
594
595
596
597
598
599
600
601
                  if ( request->var1.epilog == PERCENT_OF_TOTAL_AMOUNT )
                    total[levelID].nmiss = gridsize;
                  if ( IS_SET(request->var1.f5) )
                    var15[levelID].nmiss = gridsize;
                  if ( IS_SET(request->var2.h2) )
                    var22[levelID].nmiss = gridsize;
                }

              streamReadRecord(istreamID1, field1.ptr, &field1.nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
603
604
              field1.grid    = gridID;
              field1.missval = missval1;
              
605
              streamReadRecord(istreamID2, field2.ptr, &field2.nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
606
607
608
609
610
              field2.grid    = gridID;
              field2.missval = missval2;

              farnum(&samp1[levelID], field1);
              farnum(&samp2[levelID], field2);
611
612
613
614
615
616
617
618
619
620

              if ( request->var1.epilog == PERCENT_OF_TOTAL_AMOUNT )
                farsum(&total[levelID], field1);
                             
              if ( IS_SET(request->var1.f1) ) 
                request->var1.f1(&field1, request->var1.f1arg);
              
              if ( IS_SET(request->var1.f2) )
                request->var1.f2(&field2, request->var1.f2arg);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
621
              if ( field1.nmiss > 0 || IS_SET(samp3[levelID].ptr) )
622
                {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
623
                  if ( IS_NOT_SET(samp3[levelID].ptr) )
624
                    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
625
                      samp3[levelID].ptr = (double *) malloc(gridsize*sizeof(double));
626
                      for ( i = 0; i < gridsize; i++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
627
                        samp3[levelID].ptr[i] = nsets;
628
629
630
631
632
                    }
                  for ( i = 0; i < gridsize; i++ )
                    {
                      if ( DBL_IS_EQUAL(field1.ptr[i], field1.missval) )
                        continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633
                      samp3[levelID].ptr[i]++;
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
                    }
                }
              
              request->var1.f3(&field1, field2);
              request->var1.f4(&var14[levelID], field1);

              if ( IS_SET(request->var2.h2) )
                {
                  memcpy(field2.ptr, var14[levelID].ptr, gridsize*sizeof(double));
                  field2.nmiss   = var14[levelID].nmiss;
                  field2.grid    = var14[levelID].grid;
                  field2.missval = var14[levelID].missval;

                  if ( IS_SET(request->var2.h1) )
                    request->var2.h1(&field2, request->var2.h1arg);
                    
                  request->var2.h2(&var22[levelID], field2);
                }

              if ( IS_SET(request->var1.f5) )
                request->var1.f5(&var15[levelID], var14[levelID], request->var1.f5arg);              
            }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
657
658
          ovdate = ivdate;
          ovtime = ivtime;
659
660
661
662
663
664
665
666
667
668
669
670
671
672
          nsets++;
          itsID++;
        }

      if ( nrecs == 0 && nsets == 0 ) break;
      
      if ( request->var1.epilog == MEAN || request->var1.epilog == PERCENT_OF_TIME )
        for ( levelID = 0; levelID < nlevels; levelID++ )
          {
	    if ( IS_SET(request->var1.f5) )
	      var = &var15[levelID];
	    else
	      var = &var14[levelID];
	  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
673
            if ( IS_NOT_SET(samp3[levelID].ptr) )
674
675
              farcdiv(var, nsets);
            else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
676
              fardiv(var, samp3[levelID]);
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694

	    if ( request->var1.epilog == PERCENT_OF_TIME )
	      farcmul(var, 100.0);
          }
      else if ( request->var1.epilog == PERCENT_OF_TOTAL_AMOUNT )
        for ( levelID = 0; levelID < nlevels; levelID++ )
          {
	    if ( IS_SET(request->var1.f5) )
	      var = &var15[levelID];
	    else
	      var = &var14[levelID];
	  
            fardiv(var, total[levelID]);
	    farcmul(var, 100.0);
          }

      taxisDefVdate(otaxisID, ovdate);
      taxisDefVtime(otaxisID, ovtime);
695
      streamDefTimestep(ostreamID, otsID);
696

697
698
699
700
701
702
703
704
705
      if ( otsID && vlistInqVarTime(ivlistID1, FIRST_VAR_ID) == TIME_CONSTANT ) continue;

      varID = 0;
      for ( levelID = 0; levelID < nlevels; levelID++ )
	{
	  if ( IS_SET(request->var1.f5) )
	    var = &var15[levelID];
	  else 
	    var = &var14[levelID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
              
707
708
	  farsel(var, samp1[levelID]);
	  farsel(var, samp2[levelID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
709
              
710
711
712
713
714
715
716
717
718
719
720
721
	  streamDefRecord(ostreamID, varID, levelID);
	  streamWriteRecord(ostreamID, var->ptr, var->nmiss);
	}
      if ( IS_SET(request->var2.h2) )
	{
	  varID = 1;
	  for ( levelID = 0; levelID < nlevels; levelID++ )
	    {
	      var = &var22[levelID];
              
	      farsel(var, samp1[levelID]);
	      farsel(var, samp2[levelID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
722
                  
723
724
725
	      streamDefRecord(ostreamID, varID, levelID);
	      streamWriteRecord(ostreamID, var->ptr, var->nmiss);
	    }
726
727
728
        }

      if ( nrecs == 0 ) break;
729
      otsID++;
730
731
732
733
734
    }

  for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      free(var14[levelID].ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
735
736
737
      free(samp1[levelID].ptr);
      free(samp2[levelID].ptr);
      if ( IS_SET(samp3[levelID].ptr) ) free(samp3[levelID].ptr);
738
739
740
    }
  free(var14);
  free(samp1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
741
742
  free(samp2);
  free(samp3);
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
  
  if ( IS_SET(total) ) 
    {
      for ( levelID = 0; levelID < nlevels; levelID++ )
        free(total[levelID].ptr);
      free(total);
    }
  if ( IS_SET(var15) ) 
    {
      for ( levelID = 0; levelID < nlevels; levelID++ )
        free(var15[levelID].ptr);
      free(var15);
    }
  if ( IS_SET(var22) ) 
    {
      for ( levelID = 0; levelID < nlevels; levelID++ )
        free(var22[levelID].ptr);
      free(var22);
    }
  
  if ( IS_SET(field1.ptr) ) free(field1.ptr);
  if ( IS_SET(field2.ptr) ) free(field2.ptr);

  if ( IS_SET(recVarID) )   free(recVarID);
  if ( IS_SET(recLevelID) ) free(recLevelID);

  streamClose(ostreamID);
  streamClose(istreamID2);
  streamClose(istreamID1);
}


void eca3(const ECA_REQUEST_3 *request)
{
  const int operatorID = cdoOperatorID();
778

779
780
  int cmplen;
  char indate1[DATE_LEN+1], indate2[DATE_LEN+1];
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
  int gridsize;
  int ivdate1 = 0, ivtime1 = 0;
  int ivdate2 = 0, ivtime2 = 0;
  int ovdate = 0, ovtime = 0;
  int nrecs, nrecords;
  int gridID, zaxisID, varID, levelID, recID;
  int itsID;
  int otsID;
  long nsets;
  int i;
  int istreamID1, istreamID2, ostreamID;
  int ivlistID1, ivlistID2, ovlistID, itaxisID1, itaxisID2, otaxisID;
  int nlevels;
  int *recVarID, *recLevelID;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
797
  field_t *var1 = NULL, *var2 = NULL;
  field_t field1, field2;
798
  
799
  cmplen = DATE_LEN - cdoOperatorF2(operatorID);
800
801
802
803
804
805
806
807
808
809

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

  ivlistID1 = streamInqVlist(istreamID1);
  ivlistID2 = streamInqVlist(istreamID2);
  ovlistID  = vlistCreate();
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
810
811
  vlistCompare(ivlistID1, ivlistID2, CMP_HRD);
  vlistCompare(ivlistID1, ivlistID2, CMP_SFT);
812
813
814
815
816
817
818
819
820
821
822
823
  
  gridID  = vlistInqVarGrid(ivlistID1, FIRST_VAR_ID);
  zaxisID = vlistInqVarZaxis(ivlistID1, FIRST_VAR_ID);
  varID   = vlistDefVar(ovlistID, gridID, zaxisID, TIME_VARIABLE);
  
  if ( IS_SET(request->name) ) 
    vlistDefVarName(ovlistID, varID, request->name);
  if ( IS_SET(request->longname) ) 
    vlistDefVarLongname(ovlistID, varID, request->longname);
  if ( IS_SET(request->units) ) 
    vlistDefVarUnits(ovlistID, varID, request->units);

824
  if ( cdoOperatorF2(operatorID) == 16 ) vlistDefNtsteps(ovlistID, 1);
825
826
827

  itaxisID1 = vlistInqTaxis(ivlistID1);
  itaxisID2 = vlistInqTaxis(ivlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
  otaxisID = taxisCreate(TAXIS_RELATIVE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
829
  taxisDefTunit(otaxisID, TUNIT_MINUTE);
830
  taxisDefCalendar(otaxisID, CALENDAR_PROLEPTIC);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
831
832
  taxisDefRdate(otaxisID, 19550101);
  taxisDefRtime(otaxisID, 0);
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
  vlistDefTaxis(ovlistID, otaxisID);

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

  streamDefVlist(ostreamID, ovlistID);

  nrecords   = vlistNrecs(ivlistID1);
  recVarID   = (int *) malloc(nrecords*sizeof(int));
  recLevelID = (int *) malloc(nrecords*sizeof(int));

  gridsize = gridInqSize(gridID);

  field1.ptr = (double *) malloc(gridsize*sizeof(double));
  field2.ptr = (double *) malloc(gridsize*sizeof(double));

  nlevels = zaxisInqSize(zaxisID);
  missval = vlistInqVarMissval(ivlistID1, FIRST_VAR_ID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
852
853
  var1 = (field_t *) malloc(nlevels*sizeof(field_t));
  var2 = (field_t *) malloc(nlevels*sizeof(field_t));
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
        
  for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      var1[levelID].grid    = gridID;
      var1[levelID].nmiss   = 0;
      var1[levelID].missval = missval;
      var1[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
            
      var2[levelID].grid    = gridID;
      var2[levelID].nmiss   = 0;
      var2[levelID].missval = missval;
      var2[levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
    }

  itsID   = 0;
  otsID   = 0;
  while ( TRUE )
    {
      nsets = 0;
      while ( (nrecs = streamInqTimestep(istreamID1, itsID)) > 0 )
        {
	  if ( !streamInqTimestep(istreamID2, itsID) )
	    cdoAbort("Input streams have different number of time steps!");
            
          ivdate1 = taxisInqVdate(itaxisID1);
          ivdate2 = taxisInqVdate(itaxisID2);
          if ( ivdate1 != ivdate2 )
            cdoAbort("Input streams have different verification dates for time step %d!", itsID+1);
            
          ivtime1 = taxisInqVtime(itaxisID1);
          ivtime2 = taxisInqVtime(itaxisID2);
          if ( ivtime1 != ivtime2 )
            cdoAbort("Input streams have different verification times for time step %d!", itsID+1);
887
888
889
   
	  if ( nsets == 0 ) SET_DATE(indate2, ivdate1, ivtime1);
	  SET_DATE(indate1, ivdate1, ivtime1);
890

891
	  if ( DATE_IS_NEQ(indate1, indate2, cmplen) ) break;
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940

          for ( recID = 0; recID < nrecs; recID++ )
            {
              streamInqRecord(istreamID1, &varID, &levelID);
              streamInqRecord(istreamID2, &varID, &levelID);

              if ( itsID == 0 )
                {
                  recVarID[recID]   = varID;
                  recLevelID[recID] = levelID;
                }
              if ( varID != FIRST_VAR_ID ) continue;

              if ( nsets == 0 )
                {
                  for ( i = 0; i < gridsize; i++ )
                    {
                      var1[levelID].ptr[i] = missval;
                      var2[levelID].ptr[i] = missval;
                    }
                  var1[levelID].nmiss = gridsize;
                  var2[levelID].nmiss = gridsize;
                }

              streamReadRecord(istreamID1, field1.ptr, &field1.nmiss);
              field1.grid    = var1[levelID].grid;
              field1.missval = var1[levelID].missval;

              streamReadRecord(istreamID2, field2.ptr, &field2.nmiss);
              field2.grid    = var1[levelID].grid;
              field2.missval = var1[levelID].missval;

              request->f1(&var1[levelID], field1);
              request->f2(&var2[levelID], field2);
            }

          ovdate = ivdate1;
          ovtime = ivtime1;
          nsets++;
          itsID++;
        }

      if ( nrecs == 0 && nsets == 0 ) break;
      
      for ( levelID = 0; levelID < nlevels; levelID++ )
        request->f3(&var1[levelID], var2[levelID]);

      taxisDefVdate(otaxisID, ovdate);
      taxisDefVtime(otaxisID, ovtime);
941
      streamDefTimestep(ostreamID, otsID);
942

943
944
945
946
947
948
949
950
      if ( otsID && vlistInqVarTime(ivlistID1, FIRST_VAR_ID) == TIME_CONSTANT ) continue;

      varID = 0;
      for ( levelID = 0; levelID < nlevels; levelID++ )
	{
	  streamDefRecord(ostreamID, varID, levelID);
	  streamWriteRecord(ostreamID, var1[levelID].ptr, var1[levelID].nmiss);
	}
951
952

      if ( nrecs == 0 ) break;
953
      otsID++;
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
    }

  for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      free(var1[levelID].ptr);
      free(var2[levelID].ptr);
    }
  free(var1);
  free(var2);
    
  if ( IS_SET(field1.ptr) ) free(field1.ptr);
  if ( IS_SET(field2.ptr) ) free(field2.ptr);

  if ( IS_SET(recVarID) )   free(recVarID);
  if ( IS_SET(recLevelID) ) free(recLevelID);

  streamClose(ostreamID);
  streamClose(istreamID2);
  streamClose(istreamID1);
}


void eca4(const ECA_REQUEST_4 *request)
{
  const int operatorID = cdoOperatorID();
979

980
981
  int cmplen;
  char indate1[DATE_LEN+1], indate2[DATE_LEN+1];
982
983
  int gridsize, gridtype;
  double *yvals;
984
985
  int ivdate = 0, ivtime = 0;
  int ovdate = 0, ovtime = 0;
986
  int month, yearcnt = 0;
987
  int nrecs, nrecords;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988
  int gridID, zaxisID, varID, ovarID1, ovarID2, levelID, recID;
989
990
991
992
  int itsID;
  int otsID;
  long nsets;
  int i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
993
994
  int istreamID1, istreamID2, ostreamID;
  int ivlistID1, ivlistID2, ovlistID, itaxisID, otaxisID;
995
996
997
  int nlevels;
  int *recVarID, *recLevelID;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
998
999
1000
  field_t *startCount          , *endCount;
  field_t *startDateWithHist[2], *endDateWithHist[2];
  field_t *gslDuration, *gslFirstDay;
For faster browsing, not all history is shown. View entire blame