Ydrunstat.c 16.8 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
21
22
23
24
25
  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:

      Ydrunstat    ydrunmin          Multi-year daily running minimum
      Ydrunstat    ydrunmax          Multi-year daily running maximum
      Ydrunstat    ydrunsum          Multi-year daily running sum
      Ydrunstat    ydrunmean         Multi-year daily running mean
      Ydrunstat    ydrunavg          Multi-year daily running average
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
      Ydrunstat    ydrunvar          Multi-year daily running variance
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
      Ydrunstat    ydrunstd          Multi-year daily running standard deviation
*/

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41
42
43

#define NDAY 373


typedef struct {
  int     vdate[NDAY];
  int     vtime[NDAY];  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
  field_t **vars1[NDAY]; 
  field_t **vars2[NDAY];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
48
49
50
51
52
53
54
55
56
  int     nsets[NDAY];
  int     vlist;
}
YDAY_STATS;


static YDAY_STATS *ydstatCreate(int vlistID);
static void ydstatDestroy(YDAY_STATS *stats);
static void ydstatCreateVars1(YDAY_STATS *stats, int dayoy);
static void ydstatCreateVars2(YDAY_STATS *stats, int dayoy);
static void ydstatUpdate(YDAY_STATS *stats, int vdate, int vtime, 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
  field_t **vars1, field_t **vars2, int nsets, int operfunc);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59
60
61
62
static void ydstatFinalize(YDAY_STATS *stats, int operfunc);


void *Ydrunstat(void *argument)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
  static const char *func = "Ydrunstat";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  int operatorID;
  int operfunc;
  int gridsize;
  int varID;
  int recID;
  int gridID;
  int nrecs, nrecords;
  int levelID;
  int tsID;
  int otsID;
  int inp, its, ndates = 0;
  int streamID1, streamID2;
  int vlistID1, vlistID2;
  int nmiss;
  int nvars, nlevels;
  int *recVarID, *recLevelID;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
  field_t ***vars1 = NULL, ***vars2 = NULL;
82
  datetime_t *datetime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84
85
86
87
88
89
90
91
92
93
94
95
  int taxisID1, taxisID2;
  int calendar, dpy;
  int vdate, vtime;
  int dayoy;
  YDAY_STATS *stats;
    
  cdoInitialize(argument);

  cdoOperatorAdd("ydrunmin",  func_min,  0, NULL);
  cdoOperatorAdd("ydrunmax",  func_max,  0, NULL);
  cdoOperatorAdd("ydrunsum",  func_sum,  0, NULL);
  cdoOperatorAdd("ydrunmean", func_mean, 0, NULL);
  cdoOperatorAdd("ydrunavg",  func_avg,  0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
  cdoOperatorAdd("ydrunvar",  func_var,  0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  cdoOperatorAdd("ydrunstd",  func_std,  0, NULL);

  operatorID = cdoOperatorID();
  operfunc = cdoOperatorFunc(operatorID);

  operatorInputArg("number of timesteps");
  ndates = atoi(operatorArgv()[0]);
  
  streamID1 = streamOpenRead(cdoStreamName(0));
  if ( streamID1 < 0 ) cdiError(streamID1, "Open failed on %s", cdoStreamName(0));

  vlistID1 = streamInqVlist(streamID1);
  vlistID2 = vlistDuplicate(vlistID1);

  taxisID1 = vlistInqTaxis(vlistID1);
  taxisID2 = taxisDuplicate(taxisID1);
  vlistDefTaxis(vlistID2, taxisID2);

  calendar = taxisInqCalendar(taxisID1);
  dpy      = calendar_dpy(calendar);

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

  streamDefVlist(streamID2, vlistID2);

  nvars    = vlistNvars(vlistID1);
  nrecords = vlistNrecs(vlistID1);

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

129
  datetime = (datetime_t *) malloc((ndates+1)*sizeof(datetime_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
131
  
  stats = ydstatCreate(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
  vars1 = (field_t ***) malloc((ndates+1)*sizeof(field_t **));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
  if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
    vars2 = (field_t ***) malloc((ndates+1)*sizeof(field_t **));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
  
  for ( its = 0; its < ndates; its++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
      vars1[its] = (field_t **) malloc(nvars*sizeof(field_t *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
      if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
	vars2[its] = (field_t **) malloc(nvars*sizeof(field_t *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
143
144
145
146
147
148

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
	  vars1[its][varID] = (field_t *) malloc(nlevels*sizeof(field_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
	  if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
	    vars2[its][varID] = (field_t *) malloc(nlevels*sizeof(field_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
154
155
156
157
158

	  for ( levelID = 0; levelID < nlevels; levelID++ )
	    {
	      vars1[its][varID][levelID].grid    = gridID;
	      vars1[its][varID][levelID].nmiss   = 0;
	      vars1[its][varID][levelID].missval = missval;
	      vars1[its][varID][levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
	      if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
		{
		  vars2[its][varID][levelID].grid    = gridID;
		  vars2[its][varID][levelID].nmiss   = 0;
		  vars2[its][varID][levelID].missval = missval;
		  vars2[its][varID][levelID].ptr     = (double *) malloc(gridsize*sizeof(double));
		}
	    }
	}
    }
  
  for ( tsID = 0; tsID < ndates; tsID++ )
    {
      nrecs = streamInqTimestep(streamID1, tsID);
      if ( nrecs == 0 )
	cdoAbort("File has less then %d timesteps!", ndates);

      datetime[tsID].date = taxisInqVdate(taxisID1);
      datetime[tsID].time = taxisInqVtime(taxisID1);
	
      for ( recID = 0; recID < nrecs; recID++ )
	{
	  streamInqRecord(streamID1, &varID, &levelID);

	  if ( tsID == 0 )
	    {
	      recVarID[recID]   = varID;
	      recLevelID[recID] = levelID;
	    }
	  
	  streamReadRecord(streamID1, vars1[tsID][varID][levelID].ptr, &nmiss);
	  vars1[tsID][varID][levelID].nmiss = nmiss;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
	  if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	    {
	      farmoq(&vars2[tsID][varID][levelID], vars1[tsID][varID][levelID]);
	      for ( inp = 0; inp < tsID; inp++ )
		{
		  farsumq(&vars2[inp][varID][levelID], vars1[tsID][varID][levelID]);
		  farsum(&vars1[inp][varID][levelID], vars1[tsID][varID][levelID]);
		}
	    }
	  else
	    {
	      for ( inp = 0; inp < tsID; inp++ )
		{
		  farfun(&vars1[inp][varID][levelID], vars1[tsID][varID][levelID], operfunc);
		}
	    }
	}
    }
  
  while ( TRUE )
    {
      datetime_avg(dpy, ndates, datetime);
      
      vdate = datetime[ndates].date;
      vtime = datetime[ndates].time;
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
      if ( operfunc == func_std || operfunc == func_var )   
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
220
221
222
223
224
        ydstatUpdate(stats, vdate, vtime, vars1[0], vars2[0], ndates, operfunc);
      else
        ydstatUpdate(stats, vdate, vtime, vars1[0], NULL, ndates, operfunc);
        
      datetime[ndates] = datetime[0];
      vars1[ndates] = vars1[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
      if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226
227
228
229
230
231
        vars2[ndates] = vars2[0];

      for ( inp = 0; inp < ndates; inp++ )
	{
	  datetime[inp] = datetime[inp+1];
	  vars1[inp] = vars1[inp+1];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
	  if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
	    vars2[inp] = vars2[inp+1];
	}

      nrecs = streamInqTimestep(streamID1, tsID);
      if ( nrecs == 0 ) break;

      datetime[ndates-1].date = taxisInqVdate(taxisID1);
      datetime[ndates-1].time = taxisInqVtime(taxisID1);

      for ( recID = 0; recID < nrecs; recID++ )
	{
	  streamInqRecord(streamID1, &varID, &levelID);
	  
	  streamReadRecord(streamID1, vars1[ndates-1][varID][levelID].ptr, &nmiss);
	  vars1[ndates-1][varID][levelID].nmiss = nmiss;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
	  if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
251
252
253
254
255
256
257
258
259
260
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
	    {
	      for ( inp = 0; inp < ndates-1; inp++ )
		{
		  farsumq(&vars2[inp][varID][levelID], vars1[ndates-1][varID][levelID]);
		  farsum(&vars1[inp][varID][levelID], vars1[ndates-1][varID][levelID]);
		}
	      farmoq(&vars2[ndates-1][varID][levelID], vars1[ndates-1][varID][levelID]);
	    }
	  else
	    {
	      for ( inp = 0; inp < ndates-1; inp++ )
		{
		  farfun(&vars1[inp][varID][levelID], vars1[ndates-1][varID][levelID], operfunc);
		}
	    }
	}

      tsID++;
    }

  ydstatFinalize(stats, operfunc);
  otsID = 0;

  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    if ( stats->nsets[dayoy] )
      {
	taxisDefVdate(taxisID2, stats->vdate[dayoy]);
	taxisDefVtime(taxisID2, stats->vtime[dayoy]);
	streamDefTimestep(streamID2, otsID++);

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

	    if ( otsID == 1 || vlistInqVarTime(vlistID1, varID) == TIME_VARIABLE )
	      {
		streamDefRecord(streamID2, varID, levelID);
		streamWriteRecord(streamID2, stats->vars1[dayoy][varID][levelID].ptr,
		  stats->vars1[dayoy][varID][levelID].nmiss);
	      }
	  }
      }
  
  for ( its = 0; its < ndates; its++ )
    {
      for ( varID = 0; varID < nvars; varID++ )
	{
	  nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
	  for ( levelID = 0; levelID < nlevels; levelID++ )
	    {
	      free(vars1[its][varID][levelID].ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
	      if ( operfunc == func_std || operfunc == func_var ) free(vars2[its][varID][levelID].ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
304
305
	    }

	  free(vars1[its][varID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
	  if ( operfunc == func_std || operfunc == func_var ) free(vars2[its][varID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
307
308
	}
	free(vars1[its]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
	if ( operfunc == func_std || operfunc == func_var ) free(vars2[its]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
311
312
313
    }
  
  ydstatDestroy(stats);
  free(vars1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314
  if ( operfunc == func_std || operfunc == func_var ) free(vars2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315

316
317
  if ( datetime ) free(datetime);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320
321
322
323
324
325
326
327
328
329
330
  if ( recVarID   ) free(recVarID);
  if ( recLevelID ) free(recLevelID);

  streamClose(streamID2);
  streamClose(streamID1);

  cdoFinish();

  return (0);
}

static YDAY_STATS *ydstatCreate(int vlistID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
  static const char *func = "ydstatCreate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
  int dayoy;
  
  YDAY_STATS *stats = (YDAY_STATS *) malloc(sizeof(YDAY_STATS));
  
  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    {
      stats->vdate[dayoy] = 0;
      stats->vtime[dayoy] = 0;
      stats->vars1[dayoy] = NULL;
      stats->vars2[dayoy] = NULL;
      stats->nsets[dayoy] = 0;
    }
  stats->vlist = vlistID;
  
  return stats;
}

static void ydstatDestroy(YDAY_STATS *stats)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
  static const char *func = "ydstatDestroy";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  int dayoy, varID, levelID, nvars, nlevels;
  
  if ( stats != NULL )
    {
      nvars = vlistNvars(stats->vlist);
      
      for ( dayoy = 0; dayoy < NDAY; dayoy++ )
        {
          if ( stats->vars1[dayoy] != NULL )
            {
              for ( varID = 0; varID < nvars; varID++ )
                {
              	  nlevels = zaxisInqSize(vlistInqVarZaxis(stats->vlist, varID));
              	  for ( levelID = 0; levelID < nlevels; levelID++ )
              	    free(stats->vars1[dayoy][varID][levelID].ptr);
              	  free(stats->vars1[dayoy][varID]);
                }
              free(stats->vars1[dayoy]);
            }
          if ( stats->vars2[dayoy] != NULL )
            {
              for ( varID = 0; varID < nvars; varID++ )
                {
              	  nlevels = zaxisInqSize(vlistInqVarZaxis(stats->vlist, varID));
              	  for ( levelID = 0; levelID < nlevels; levelID++ )
              	    free(stats->vars2[dayoy][varID][levelID].ptr);
              	  free(stats->vars2[dayoy][varID]);
                }
              free(stats->vars2[dayoy]);
            }
        }
      free(stats);    
    }
}

static void ydstatCreateVars1(YDAY_STATS *stats, int dayoy)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
  static const char *func = "ydstatCreateVars1";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
  int varID, levelID, nvars, nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391
392
  int gridID, gridsize;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394
395

  nvars = vlistNvars(stats->vlist);
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
396
  stats->vars1[dayoy] = (field_t **) malloc(nvars * sizeof(field_t *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397
398
399
400
401
402
403
404

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
      stats->vars1[dayoy][varID] = (field_t *) malloc(nlevels * sizeof(field_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
408
409
410
411
412
413
414
415
416
417
418
	      
      for ( levelID = 0; levelID < nlevels; levelID++ )
        {
	  stats->vars1[dayoy][varID][levelID].grid    = gridID;
	  stats->vars1[dayoy][varID][levelID].nmiss   = 0;
	  stats->vars1[dayoy][varID][levelID].missval = missval;
	  stats->vars1[dayoy][varID][levelID].ptr     = (double *) malloc(gridsize * sizeof(double));
        }
    }
}

static void ydstatCreateVars2(YDAY_STATS *stats, int dayoy)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
  static const char *func = "ydstatCreateVars2";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
  int varID, levelID, nvars, nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
422
  int gridID, gridsize;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
423
424
425

  nvars = vlistNvars(stats->vlist);
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
426
  stats->vars2[dayoy] = (field_t **) malloc(nvars * sizeof(field_t *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429
430
431
432
433
434

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
435
      stats->vars2[dayoy][varID] = (field_t *) malloc(nlevels * sizeof(field_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
436
437
438
439
440
441
442
443
444
445
446
447
	      
      for ( levelID = 0; levelID < nlevels; levelID++ )
        {
	  stats->vars2[dayoy][varID][levelID].grid    = gridID;
	  stats->vars2[dayoy][varID][levelID].nmiss   = 0;
	  stats->vars2[dayoy][varID][levelID].missval = missval;
	  stats->vars2[dayoy][varID][levelID].ptr     = (double *) malloc(gridsize * sizeof(double));
        }
    }
}

static void ydstatUpdate(YDAY_STATS *stats, int vdate, int vtime, 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
448
  field_t **vars1, field_t **vars2, int nsets, int operfunc)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
449
450
{
  int varID, levelID, nvars, nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
451
  int gridsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
  int year, month, day, dayoy;

  nvars = vlistNvars(stats->vlist);
  
  year  =  vdate / 10000;
  month = (vdate - year * 10000) / 100;
  day   =  vdate - year * 10000 - month * 100;

  if ( month >= 1 && month <= 12 )
    dayoy = (month - 1) * 31 + day;
  else
    dayoy = 0;

  if ( dayoy < 0 || dayoy >= NDAY )
    cdoAbort("day %d out of range!", dayoy);

  stats->vdate[dayoy] = vdate;
  stats->vtime[dayoy] = vtime;

  if ( stats->vars1[dayoy] == NULL )
    {
      ydstatCreateVars1(stats, dayoy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
      if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
	ydstatCreateVars2(stats, dayoy);
    }

  for ( varID = 0; varID  < nvars; varID++ )
    {
      if ( vlistInqVarTime(stats->vlist, varID) == TIME_CONSTANT ) continue;
        
      gridsize = gridInqSize(vlistInqVarGrid(stats->vlist, varID));
      nlevels  = zaxisInqSize(vlistInqVarZaxis(stats->vlist, varID));
          
      for ( levelID = 0; levelID < nlevels; levelID++ )
        {
	  if ( stats->nsets[dayoy] == 0 )
	    {
	      memcpy(stats->vars1[dayoy][varID][levelID].ptr, vars1[varID][levelID].ptr, gridsize * sizeof(double));
	      stats->vars1[dayoy][varID][levelID].nmiss = vars1[varID][levelID].nmiss;
	       
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
	      if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
494
495
496
497
498
499
	        {
	          memcpy(stats->vars2[dayoy][varID][levelID].ptr, vars2[varID][levelID].ptr, gridsize * sizeof(double));
	          stats->vars2[dayoy][varID][levelID].nmiss = vars2[varID][levelID].nmiss;
	        }
	    }
	  else
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
	      if ( operfunc == func_std || operfunc == func_var )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
	        {
		  farsum(&stats->vars1[dayoy][varID][levelID], vars1[varID][levelID]);
		  farsum(&stats->vars2[dayoy][varID][levelID], vars2[varID][levelID]);
		}
	      else
		{
	          farfun(&stats->vars1[dayoy][varID][levelID], vars1[varID][levelID], operfunc);
		}
	    }
        }
    }

  stats->nsets[dayoy] += nsets;
}

static void ydstatFinalize(YDAY_STATS *stats, int operfunc)
{
  int varID, levelID, nvars, nlevels;
  int dayoy;
  
  nvars = vlistNvars(stats->vlist);
  
  for ( dayoy = 0; dayoy < NDAY; dayoy++ )
    if ( stats->nsets[dayoy] )
      {
      	switch ( operfunc )
      	  {
	    case func_avg: case func_mean:
	      for ( varID = 0; varID < nvars; varID++ )
	        {
	          if ( vlistInqVarTime(stats->vlist, varID) == TIME_CONSTANT ) continue;
	          nlevels = zaxisInqSize(vlistInqVarZaxis(stats->vlist, varID));
	          for ( levelID = 0; levelID < nlevels; levelID++ )
		    farcmul(&stats->vars1[dayoy][varID][levelID], 1.0 / stats->nsets[dayoy]);
	        }
	      break;
	      
	    case func_std:
	      for ( varID = 0; varID < nvars; varID++ )
	        {
	          if ( vlistInqVarTime(stats->vlist, varID) == TIME_CONSTANT ) continue;
	          nlevels = zaxisInqSize(vlistInqVarZaxis(stats->vlist, varID));
	          for ( levelID = 0; levelID < nlevels; levelID++ )
		    farcstd(&stats->vars1[dayoy][varID][levelID], stats->vars2[dayoy][varID][levelID],
		      1.0 / stats->nsets[dayoy]);
	        }
	      break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
548
549
550
551
552
553
554
555
556
557
558
	      
	    case func_var:
	      for ( varID = 0; varID < nvars; varID++ )
	        {
	          if ( vlistInqVarTime(stats->vlist, varID) == TIME_CONSTANT ) continue;
	          nlevels = zaxisInqSize(vlistInqVarZaxis(stats->vlist, varID));
	          for ( levelID = 0; levelID < nlevels; levelID++ )
		    farcvar(&stats->vars1[dayoy][varID][levelID], stats->vars2[dayoy][varID][levelID],
		      1.0 / stats->nsets[dayoy]);
	        }
	      break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
559
560
561
      	  }
      }
}