Comp.c 8.47 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) 2003-2014 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
24
25
26
27
28
/*
   This module contains the following operators:

      Comp       eq              Equal
      Comp       ne              Not equal
      Comp       le              Less equal
      Comp       lt              Less than
      Comp       ge              Greater equal
      Comp       gt              Greater than
*/

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


void *Comp(void *argument)
{
  int EQ, NE, LE, LT, GE, GT;
  int operatorID;
39
40
41
42
  enum {FILL_NONE, FILL_TS, FILL_REC};
  int filltype = FILL_NONE;
  int streamIDx1, streamIDx2, streamID1, streamID2, streamID3;
  int gridsize, gridsize1, gridsize2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
46
  int nrecs, nrecs2, nvars = 0, nlev, recID;
  int tsID;
  int varID, levelID;
  int offset;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
  int ntsteps1, ntsteps2;
48
49
50
  int vlistIDx1, vlistIDx2, vlistID1, vlistID2, vlistID3;
  int taxisIDx1, taxisID1, taxisID2, taxisID3;
  int nmiss1, nmiss2, nmiss3;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
  int i;
  double missval1, missval2 = 0;
53
54
55
  double *missvalx1, *missvalx2;
  double *arrayx1, *arrayx2, *array1, *array2, *array3;
  double **vardata = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

  cdoInitialize(argument);

  EQ = cdoOperatorAdd("eq", 0, 0, NULL);
  NE = cdoOperatorAdd("ne", 0, 0, NULL);
  LE = cdoOperatorAdd("le", 0, 0, NULL);
  LT = cdoOperatorAdd("lt", 0, 0, NULL);
  GE = cdoOperatorAdd("ge", 0, 0, NULL);
  GT = cdoOperatorAdd("gt", 0, 0, NULL);

  operatorID = cdoOperatorID();

  streamID1 = streamOpenRead(cdoStreamName(0));
  streamID2 = streamOpenRead(cdoStreamName(1));

71
72
73
74
75
76
  streamIDx1 = streamID1;
  streamIDx2 = streamID2;

  missvalx1 = &missval1;
  missvalx2 = &missval2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
78
  vlistID1 = streamInqVlist(streamID1);
  vlistID2 = streamInqVlist(streamID2);
79
80
  vlistIDx1 = vlistID1;
  vlistIDx2 = vlistID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82

  taxisID1 = vlistInqTaxis(vlistID1);
83
84
85
  taxisID2 = vlistInqTaxis(vlistID2);
  taxisIDx1 = taxisID1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
88
89
90
  ntsteps1 = vlistNtsteps(vlistID1);
  ntsteps2 = vlistNtsteps(vlistID2);
  if ( ntsteps1 == 0 ) ntsteps1 = 1;
  if ( ntsteps2 == 0 ) ntsteps2 = 1;

91
92
93
  if ( vlistNrecs(vlistID1) != 1 && vlistNrecs(vlistID2) == 1 )
    {
      filltype = FILL_REC;
94
      cdoPrint("Filling up stream2 >%s< by copying the first record.", cdoStreamName(1)->args);
95
96
97
98
    }
  else if ( vlistNrecs(vlistID1) == 1 && vlistNrecs(vlistID2) != 1 )
    {
      filltype = FILL_REC;
99
      cdoPrint("Filling up stream1 >%s< by copying the first record.", cdoStreamName(0)->args);
100
101
102
103
104
105
      streamIDx1 = streamID2;
      streamIDx2 = streamID1;
      vlistIDx1 = vlistID2;
      vlistIDx2 = vlistID1;
      taxisIDx1 = taxisID2;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106

107
  if ( filltype == FILL_NONE )
108
    vlistCompare(vlistID1, vlistID2, CMP_ALL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110

  nospec(vlistID1);
111
  nospec(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112

113
  gridsize = vlistGridsizeMax(vlistIDx1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
117
118

  array1 = (double *) malloc(gridsize*sizeof(double));
  array2 = (double *) malloc(gridsize*sizeof(double));
  array3 = (double *) malloc(gridsize*sizeof(double));

119
120
121
  arrayx1 = array1;
  arrayx2 = array2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
  if ( cdoVerbose )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
    cdoPrint("Number of timesteps: file1 %d, file2 %d", ntsteps1, ntsteps2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124

125
  if ( filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
      if ( ntsteps1 != 1 && ntsteps2 == 1 )
128
129
	{
	  filltype = FILL_TS;
130
	  cdoPrint("Filling up stream2 >%s< by copying the first timestep.", cdoStreamName(1)->args);
131
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
      else if ( ntsteps1 == 1 && ntsteps2 != 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
	{
134
	  filltype = FILL_TS;
135
	  cdoPrint("Filling up stream1 >%s< by copying the first timestep.", cdoStreamName(0)->args);
136
137
138
139
140
	  streamIDx1 = streamID2;
          streamIDx2 = streamID1;
	  vlistIDx1 = vlistID2;
	  vlistIDx2 = vlistID1;
	  taxisIDx1 = taxisID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
	}

143
144
145
146
147
148
149
150
151
152
153
      if ( filltype == FILL_TS )
	{
	  nvars  = vlistNvars(vlistIDx2);
	  vardata  = (double **) malloc(nvars*sizeof(double *));
	  for ( varID = 0; varID < nvars; varID++ )
	    {
	      gridsize = gridInqSize(vlistInqVarGrid(vlistIDx2, varID));
	      nlev     = zaxisInqSize(vlistInqVarZaxis(vlistIDx2, varID));
	      vardata[varID]  = (double *) malloc(nlev*gridsize*sizeof(double));
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
155
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
  if ( filltype != FILL_NONE && ntsteps1 == 1 )
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    {
      arrayx1 = array2;
      arrayx2 = array1;
      missvalx1 = &missval2;
      missvalx2 = &missval1;
    }

  vlistID3 = vlistDuplicate(vlistIDx1);

  taxisID3 = taxisDuplicate(taxisIDx1);
  vlistDefTaxis(vlistID3, taxisID3);

  streamID3 = streamOpenWrite(cdoStreamName(2), cdoFiletype());

  streamDefVlist(streamID3, vlistID3);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
  tsID = 0;
174
  while ( (nrecs = streamInqTimestep(streamIDx1, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
    {
176
      if ( tsID == 0 || filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
	{
178
	  nrecs2 = streamInqTimestep(streamIDx2, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
181
182
	  if ( nrecs2 == 0 )
	    cdoAbort("Input streams have different number of timesteps!");
	}
	  
183
      taxisCopyTimestep(taxisID3, taxisIDx1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
185
186
187
188

      streamDefTimestep(streamID3, tsID);

      for ( recID = 0; recID < nrecs; recID++ )
	{
189
190
	  streamInqRecord(streamIDx1, &varID, &levelID);
	  streamReadRecord(streamIDx1, arrayx1, &nmiss1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191

192
	  if ( tsID == 0 || filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
	    {
194
	      if ( recID == 0 || filltype != FILL_REC )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
		{
196
197
		  streamInqRecord(streamIDx2, &varID, &levelID);
		  streamReadRecord(streamIDx2, arrayx2, &nmiss2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
		}

200
	      if ( filltype == FILL_TS )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
		{
202
		  gridsize = gridInqSize(vlistInqVarGrid(vlistIDx2, varID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
		  offset   = gridsize*levelID;
204
		  memcpy(vardata[varID]+offset, arrayx2, gridsize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
		}
	    }
207
	  else if ( filltype == FILL_TS )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
	    {
209
	      gridsize = gridInqSize(vlistInqVarGrid(vlistIDx2, varID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
	      offset   = gridsize*levelID;
211
	      memcpy(arrayx2, vardata[varID]+offset, gridsize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
213
	    }

214
215
	  gridsize1 = gridInqSize(vlistInqVarGrid(vlistIDx1, varID));
	  *missvalx1 = vlistInqVarMissval(vlistIDx1, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216

217
	  if ( filltype == FILL_REC )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
	    {
219
220
	      gridsize2 = gridInqSize(vlistInqVarGrid(vlistIDx2, 0));
	      *missvalx2 = vlistInqVarMissval(vlistIDx2, 0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
	    }
222
223
224
225
226
227
228
229
230
231
	  else
	    {
	      gridsize2 = gridInqSize(vlistInqVarGrid(vlistIDx2, varID));
	      *missvalx2 = vlistInqVarMissval(vlistIDx2, varID);
	    }

	  if ( gridsize1 != gridsize2 ) cdoAbort("Streams have different gridsize (gridsize1 = %d; gridsize2 = %d!",
						 gridsize1, gridsize2);

	  gridsize = gridsize1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
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

	  if ( operatorID == EQ )
	    {
	      for ( i = 0; i < gridsize; i++ )
		array3[i] = (DBL_IS_EQUAL(array1[i], missval1) || DBL_IS_EQUAL(array2[i], missval2) ?
			     missval1 : DBL_IS_EQUAL(array1[i], array2[i]));
	    }
	  else if ( operatorID == NE )
	    {
	      for ( i = 0; i < gridsize; i++ )
		array3[i] = (DBL_IS_EQUAL(array1[i], missval1) || DBL_IS_EQUAL(array2[i], missval2) ?
			     missval1 : !DBL_IS_EQUAL(array1[i], array2[i]));
	    }
	  else if ( operatorID == LE )
	    {
	      for ( i = 0; i < gridsize; i++ )
		array3[i] = (DBL_IS_EQUAL(array1[i], missval1) || DBL_IS_EQUAL(array2[i], missval2) ?
			     missval1 : array1[i] <= array2[i]);
	    }
	  else if ( operatorID == LT )
	    {
	      for ( i = 0; i < gridsize; i++ )
		array3[i] = (DBL_IS_EQUAL(array1[i], missval1) || DBL_IS_EQUAL(array2[i], missval2) ?
			     missval1 : array1[i] < array2[i]);
	    }
	  else if ( operatorID == GE )
	    {
	      for ( i = 0; i < gridsize; i++ )
		array3[i] = (DBL_IS_EQUAL(array1[i], missval1) || DBL_IS_EQUAL(array2[i], missval2) ?
			     missval1 : array1[i] >= array2[i]);
	    }
	  else if ( operatorID == GT )
	    {
	      for ( i = 0; i < gridsize; i++ )
		array3[i] = (DBL_IS_EQUAL(array1[i], missval1) || DBL_IS_EQUAL(array2[i], missval2) ?
			     missval1 : array1[i] > array2[i]);
	    }
	  else
	    {
	      cdoAbort("Operator not implemented!");
	    }

	  nmiss3 = 0;
	  for ( i = 0; i < gridsize; i++ )
	    if ( DBL_IS_EQUAL(array3[i], missval1) ) nmiss3++;

	  streamDefRecord(streamID3, varID, levelID);
	  streamWriteRecord(streamID3, array3, nmiss3);
	}

      tsID++;
    }

  streamClose(streamID3);
  streamClose(streamID2);
  streamClose(streamID1);

289
  if ( vardata )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
    {
291
292
      for ( varID = 0; varID < nvars; varID++ ) free(vardata[varID]);
      free(vardata);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
296
297
298
299
300
301
302
    }

  if ( array3 ) free(array3);
  if ( array2 ) free(array2);
  if ( array1 ) free(array1);

  cdoFinish();

  return (0);
}