Fldstat2.cc 6.08 KB
Newer Older
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) 2003-2017 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  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:

      Fldstat2    fldcor         Correlation of two fields
*/


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


/* routine corr copied from PINGO */
33
/* correclation in space */
34
static
35
double correlation_s(const double * restrict in0, const double * restrict in1,
36
		     const double * restrict weight, double missval1, double missval2, long gridsize)
37
38
39
40
41
{
  double sum0, sum1, sum00, sum01, sum11, wsum0;
  sum0 = sum1 = sum00 = sum01 = sum11 = 0;
  wsum0 = 0;
	
42
  for ( long i = 0; i < gridsize; ++i )
43
    {
44
      if ( IS_NOT_EQUAL(weight[i], missval1) && IS_NOT_EQUAL(in0[i], missval1) && IS_NOT_EQUAL(in1[i], missval2) )
45
46
47
48
49
50
51
52
53
54
	    {
	      sum0  += weight[i] * in0[i];
	      sum1  += weight[i] * in1[i];
	      sum00 += weight[i] * in0[i] * in0[i];
	      sum01 += weight[i] * in0[i] * in1[i];
	      sum11 += weight[i] * in1[i] * in1[i];
	      wsum0 += weight[i];
	    }
    }

55
56
57
58
  double out = IS_NOT_EQUAL(wsum0, 0) ?
               DIVMN((sum01 * wsum0 - sum0 * sum1),
	            SQRTMN((sum00 * wsum0 - sum0 * sum0) *
	                 (sum11 * wsum0 - sum1 * sum1))) : missval1;
59

60
  return out;
61
62
}

63
64
65
/* covariance in space */
static
double covariance_s(const double * restrict in0, const double * restrict in1,
66
		    const double * restrict weight, double missval1, double missval2, long gridsize)
67
68
69
70
71
{
  double sum0, sum1, sum01, wsum0, wsum00;
  sum0 = sum1 = sum01 = 0;
  wsum0 = wsum00 = 0;

72
  for ( long i = 0; i < gridsize; ++i )
73
    {
74
      if ( IS_NOT_EQUAL(weight[i], missval1) && IS_NOT_EQUAL(in0[i], missval1) && IS_NOT_EQUAL(in1[i], missval2) )
75
76
77
78
79
80
81
82
83
	{
	  sum0   += weight[i] * in0[i];
	  sum1   += weight[i] * in1[i];
	  sum01  += weight[i] * in0[i] * in1[i];
	  wsum0  += weight[i];
	  wsum00 += weight[i] * weight[i];
	}
    }

84
85
  double out = IS_NOT_EQUAL(wsum0, 0) ?
               (sum01 * wsum0 - sum0 * sum1) / (wsum0 * wsum0) : missval1;
86

87
  return out;
88
89
}

90
91
92
93
94

void *Fldstat2(void *argument)
{
  int gridID, lastgridID = -1;
  int gridID3;
95
  int nrecs, nrecs2;
96
  int varID, levelID;
97
  int nmiss1, nmiss2;
98
99
  bool wstatus = false;
  bool needWeights = true;
100
  double sglval = 0;
101
  char varname[CDI_MAX_NAME];
102
103
104

  cdoInitialize(argument);

105
106
  cdoOperatorAdd("fldcor",   func_cor,   0, NULL);
  cdoOperatorAdd("fldcovar", func_covar, 0, NULL);
107

108
109
  int operatorID = cdoOperatorID();
  int operfunc = cdoOperatorF1(operatorID);
110

111
112
  int streamID1 = pstreamOpenRead(cdoStreamName(0));
  int streamID2 = pstreamOpenRead(cdoStreamName(1));
113

114
115
  int vlistID1 = pstreamInqVlist(streamID1);
  int vlistID2 = pstreamInqVlist(streamID2);
116
  int vlistID3 = vlistDuplicate(vlistID1);
117

Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
  vlistCompare(vlistID1, vlistID2, CMP_ALL);
119

120
121
  int taxisID1 = vlistInqTaxis(vlistID1);
  int taxisID3 = taxisDuplicate(taxisID1);
122
123
  vlistDefTaxis(vlistID3, taxisID3);

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  if ( CDO_Reduce_Dim )
    {
      gridID3 = gridCreate(GRID_GENERIC, 1);
      gridDefXsize(gridID3, 0);
      gridDefYsize(gridID3, 0);
    }
  else
    {
      double slon = 0;
      double slat = 0;
      gridID3 = gridCreate(GRID_LONLAT, 1);
      gridDefXsize(gridID3, 1);
      gridDefYsize(gridID3, 1);
      gridDefXvals(gridID3, &slon);
      gridDefYvals(gridID3, &slat);
    }
140

141
  int ngrids = vlistNgrids(vlistID1);
142

143
  for ( int index = 0; index < ngrids; index++ )
144
145
    vlistChangeGridIndex(vlistID3, index, gridID3);

146
  int streamID3 = pstreamOpenWrite(cdoStreamName(2), cdoFiletype());
147

148
  pstreamDefVlist(streamID3, vlistID3);
149

150
  int gridsize = vlistGridsizeMax(vlistID1);
151

152
153
  double *array1 = (double*) Malloc(gridsize*sizeof(double));
  double *array2 = (double*) Malloc(gridsize*sizeof(double));
154
  double *weight = needWeights ? (double*) Malloc(gridsize*sizeof(double)) : NULL;
155

156
  int tsID = 0;
157
  while ( (nrecs = pstreamInqTimestep(streamID1, tsID)) )
158
    {
159
      nrecs2 = pstreamInqTimestep(streamID2, tsID);
160

161
162
163
164
165
166
      if ( nrecs2 == 0 )
	{
	  cdoWarning("Input streams have different number of time steps!");
	  break;
	}

167
168
      taxisCopyTimestep(taxisID3, taxisID1);

169
      pstreamDefTimestep(streamID3, tsID);
170

171
      for ( int recID = 0; recID < nrecs; recID++ )
172
	{
173
174
175
176
	  pstreamInqRecord(streamID1, &varID, &levelID);
	  pstreamInqRecord(streamID2, &varID, &levelID);
	  pstreamReadRecord(streamID1, array1, &nmiss1);
	  pstreamReadRecord(streamID2, array2, &nmiss2);
177
178
179
180
181
182

	  gridID = vlistInqVarGrid(vlistID1, varID);
	  gridsize = gridInqSize(gridID);
	  if ( needWeights && gridID != lastgridID )
	    {
	      lastgridID = gridID;
183
	      wstatus = gridWeights(gridID, weight) != 0;
184
	    }
185
	  if ( wstatus && tsID == 0 && levelID == 0 )
186
187
188
189
	    {
	      vlistInqVarName(vlistID1, varID, varname);
	      cdoWarning("Using constant grid cell area weights for variable %s!", varname);
	    }
190

191
192
	  double missval1 = vlistInqVarMissval(vlistID1, varID);
	  double missval2 = vlistInqVarMissval(vlistID2, varID);
193

194
195
	  if ( operfunc == func_cor )
	    {
196
	      sglval = correlation_s(array1, array2, weight, missval1, missval2, gridsize);
197
198
199
	    }
	  else if ( operfunc == func_covar )
	    {
200
	      sglval = covariance_s(array1, array2, weight, missval1, missval2, gridsize);
201
	    }
202

203
          int nmiss3 = DBL_IS_EQUAL(sglval, missval1) ? 1 : 0;
204

205
206
	  pstreamDefRecord(streamID3, varID,  levelID);
	  pstreamWriteRecord(streamID3, &sglval, nmiss3);
207
	}
208

209
210
211
      tsID++;
    }

212
213
214
  pstreamClose(streamID3);
  pstreamClose(streamID2);
  pstreamClose(streamID1);
215

216
217
218
  if ( array1 ) Free(array1);
  if ( array2 ) Free(array2);
  if ( weight ) Free(weight);
219
220
221

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
  return 0;
223
}