Detrend.c 5.84 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.

5
  Copyright (C) 2003-2011 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  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.
*/

18
19
20
21
22
23
/*
   This module contains the following operators:

      Detrend    detrend         Detrend
*/

24
25
26
#if defined (_OPENMP)
#  include <omp.h>
#endif
27

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


Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
#define  NALLOC_INC  1024
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36


Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
static
38
void detrend(long nts, double missval1, double *array1, double *array2)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
{
40
41
42
  long n;
  long j;
  double zj;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
46
47
48
  double sumj, sumjj;
  double sumx, sumjx;
  double work1, work2;
  double missval2 = missval1;

  sumx = sumjx = 0;
49
50
  sumj = sumjj = 0;
  n = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
53
  for ( j = 0; j < nts; j++ )
    if ( !DBL_IS_EQUAL(array1[j], missval1) )
      {
54
        zj = j;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
	sumx  += array1[j];
56
57
58
	sumjx += zj * array1[j];
	sumj  += zj;
	sumjj += zj * zj;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	n++;
      }

  work1 = DIV(SUB(sumjx, DIV(MUL(sumx, sumj), n) ),
	      SUB(sumjj, DIV(MUL(sumj, sumj), n)) );
  work2 = SUB(DIV(sumx, n), MUL(work1, DIV(sumj, n)));

  for ( j = 0; j < nts; j++ )
    array2[j] = SUB(array1[j], ADD(work2, MUL(j, work1)));
}


void *Detrend(void *argument)
{
73
  int ompthID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
76
77
78
79
80
81
82
83
84
85
86
  int gridsize;
  int nrecs;
  int gridID, varID, levelID, recID;
  int tsID;
  int i;
  int nts;
  int nalloc = 0;
  int streamID1, streamID2;
  int vlistID1, vlistID2, taxisID1, taxisID2;
  int nmiss;
  int nvars, nlevel;
  int *vdate = NULL, *vtime = NULL;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
  field_t ***vars = NULL;
88
89
90
91
92
93
  typedef struct
  {
    double *array1;
    double *array2;
  } memory_t;
  memory_t *mem = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
97
98
99
100
101
102

  cdoInitialize(argument);

  streamID1 = streamOpenRead(cdoStreamName(0));

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

  taxisID1 = vlistInqTaxis(vlistID1);
103
  taxisID2 = taxisDuplicate(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  vlistDefTaxis(vlistID2, taxisID2);

  streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());

  streamDefVlist(streamID2, vlistID2);

  nvars = vlistNvars(vlistID1);

  tsID = 0;
  while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
    {
      if ( tsID >= nalloc )
	{
	  nalloc += NALLOC_INC;
	  vdate = (int *) realloc(vdate, nalloc*sizeof(int));
	  vtime = (int *) realloc(vtime, nalloc*sizeof(int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
	  vars  = (field_t ***) realloc(vars, nalloc*sizeof(field_t **));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
123
124
125
	}

      vdate[tsID] = taxisInqVdate(taxisID1);
      vtime[tsID] = taxisInqVtime(taxisID1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
      vars[tsID] = (field_t **) malloc(nvars*sizeof(field_t *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
130
131
132
133

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
	  vars[tsID][varID] = (field_t *) malloc(nlevel*sizeof(field_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

	  for ( levelID = 0; levelID < nlevel; levelID++ )
	    {
	      vars[tsID][varID][levelID].grid    = gridID;
	      vars[tsID][varID][levelID].missval = missval;
	      vars[tsID][varID][levelID].ptr     = NULL;
	    }
	}

      for ( recID = 0; recID < nrecs; recID++ )
	{
	  streamInqRecord(streamID1, &varID, &levelID);
	  gridID   = vlistInqVarGrid(vlistID1, varID);
	  gridsize = gridInqSize(gridID);
	  vars[tsID][varID][levelID].ptr = (double *) malloc(gridsize*sizeof(double));
	  streamReadRecord(streamID1, vars[tsID][varID][levelID].ptr, &nmiss);
	  vars[tsID][varID][levelID].nmiss = nmiss;
	}

      tsID++;
    }

  nts = tsID;

159
160
161
162
163
164
  mem = (memory_t *) malloc(ompNumThreads*sizeof(memory_t));
  for ( i = 0; i < ompNumThreads; i++ )
    {
      mem[i].array1 = (double *) malloc(nts*sizeof(double));
      mem[i].array2 = (double *) malloc(nts*sizeof(double));
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167
168
169
170
171
172
173

  for ( varID = 0; varID < nvars; varID++ )
    {
      gridID   = vlistInqVarGrid(vlistID1, varID);
      missval  = vlistInqVarMissval(vlistID1, varID);
      gridsize = gridInqSize(gridID);
      nlevel   = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
      for ( levelID = 0; levelID < nlevel; levelID++ )
	{
174
175
176
#if defined (_OPENMP)
#pragma omp parallel for default(shared) private(i, ompthID, tsID)
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
178
	  for ( i = 0; i < gridsize; i++ )
	    {
179
180
181
182
183
#if defined (_OPENMP)
              ompthID = omp_get_thread_num();
#else
              ompthID = 0;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
	      for ( tsID = 0; tsID < nts; tsID++ )
185
		mem[ompthID].array1[tsID] = vars[tsID][varID][levelID].ptr[i];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186

187
	      detrend(nts, missval, mem[ompthID].array1, mem[ompthID].array2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189

	      for ( tsID = 0; tsID < nts; tsID++ )
190
		vars[tsID][varID][levelID].ptr[i] = mem[ompthID].array2[tsID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
193
194
	    }
	}
    }

195
196
197
198
199
200
  for ( i = 0; i < ompNumThreads; i++ )
    {
      free(mem[i].array1);
      free(mem[i].array2);
    }
  free(mem);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

  for ( tsID = 0; tsID < nts; tsID++ )
    {
      taxisDefVdate(taxisID2, vdate[tsID]);
      taxisDefVtime(taxisID2, vtime[tsID]);
      streamDefTimestep(streamID2, tsID);

      for ( varID = 0; varID < nvars; varID++ )
	{
	  nlevel = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
	  for ( levelID = 0; levelID < nlevel; levelID++ )
	    {
	      if ( vars[tsID][varID][levelID].ptr )
		{
		  nmiss = vars[tsID][varID][levelID].nmiss;
		  streamDefRecord(streamID2, varID, levelID);
		  streamWriteRecord(streamID2, vars[tsID][varID][levelID].ptr, nmiss);
		  free(vars[tsID][varID][levelID].ptr);
		}
	    }
	  free(vars[tsID][varID]);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
      free(vars[tsID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
224
225
226
227
228
229
230
231
232
233
234
235
236
    }

  if ( vars  ) free(vars);
  if ( vdate ) free(vdate);
  if ( vtime ) free(vtime);

  streamClose(streamID2);
  streamClose(streamID1);

  cdoFinish();

  return (0);
}