Cond.cc 6 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-2017 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.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
/*
   This module contains the following operators:

      Cond       ifthen          If then
      Cond       ifnotthen       If not then
*/

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


void *Cond(void *argument)
{
33
34
  enum {FILL_NONE, FILL_TS, FILL_REC};
  int filltype = FILL_NONE;
35
  int nrecs, nrecs2, nvars = 0, nlev;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
38
39
40
  int varID, levelID;
  int offset;
  int nmiss1, nmiss2, nmiss3;
  int i;
  double missval1 = -9.E33;
41
  double missval2 = -9.E33;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
43
44
45
46
  int **varnmiss1 = NULL;
  double **vardata1 = NULL;

  cdoInitialize(argument);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
  // clang-format off
48
49
  int IFTHEN    = cdoOperatorAdd("ifthen",    0, 0, NULL);
  int IFNOTTHEN = cdoOperatorAdd("ifnotthen", 0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
  // clang-format on
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51

52
  int operatorID = cdoOperatorID();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53

54
55
  int streamID1 = pstreamOpenRead(cdoStreamName(0));
  int streamID2 = pstreamOpenRead(cdoStreamName(1));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56

57
58
  int vlistID1 = pstreamInqVlist(streamID1);
  int vlistID2 = pstreamInqVlist(streamID2);
59
  int vlistID3 = vlistDuplicate(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60

61
62
  int taxisID2 = vlistInqTaxis(vlistID2);
  int taxisID3 = taxisDuplicate(taxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
64
  vlistDefTaxis(vlistID3, taxisID3);

65
66
  int ntsteps1 = vlistNtsteps(vlistID1);
  int ntsteps2 = vlistNtsteps(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
69
  if ( ntsteps1 == 0 ) ntsteps1 = 1;
  if ( ntsteps2 == 0 ) ntsteps2 = 1;

70
71
72
  if ( vlistNrecs(vlistID1) == 1 && vlistNrecs(vlistID2) != 1 )
    {
      filltype = FILL_REC;
73
      cdoPrint("Filling up stream1 >%s< by copying the first record.", cdoStreamName(0)->args);
74
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75

76
  if ( filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
    vlistCompare(vlistID1, vlistID2, CMP_DIM);
78
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
  nospec(vlistID1);
80
  nospec(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81

82
83
  int streamID3 = pstreamOpenWrite(cdoStreamName(2), cdoFiletype());
  pstreamDefVlist(streamID3, vlistID3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84

85
  int gridsize = vlistGridsizeMax(vlistID2);
86
87

  if ( filltype == FILL_REC && gridsize != gridInqSize(vlistGrid(vlistID1, 0)) )
88
    cdoAbort("Stream1 >%s< has wrong gridsize!", cdoStreamName(0)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89

90
91
92
  double *array1 = (double*) Malloc(gridsize*sizeof(double));
  double *array2 = (double*) Malloc(gridsize*sizeof(double));
  double *array3 = (double*) Malloc(gridsize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
94

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

97
  if ( filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
    {
99
      if ( ntsteps1 == 1 && ntsteps2 != 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
	{
101
	  filltype = FILL_TS;
102
	  cdoPrint("Filling up stream1 >%s< by copying the first timestep.", cdoStreamName(0)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103

104
	  nvars  = vlistNvars(vlistID1);
105
106
	  vardata1  = (double **) Malloc(nvars*sizeof(double *));
	  varnmiss1 = (int **) Malloc(nvars*sizeof(int *));
107
108
109
110
	  for ( varID = 0; varID < nvars; varID++ )
	    {
	      gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
	      nlev     = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
111
112
	      vardata1[varID]  = (double*) Malloc(nlev*gridsize*sizeof(double));
	      varnmiss1[varID] = (int*) Malloc(nlev*sizeof(int));
113
114
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
    }

117
  int tsID = 0;
118
  while ( (nrecs = pstreamInqTimestep(streamID2, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
    {
120
      if ( tsID == 0 || filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
	{
122
	  nrecs2 = pstreamInqTimestep(streamID1, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
126
127
128
	  if ( nrecs2 == 0 )
	    cdoAbort("Input streams have different number of timesteps!");
	}

      taxisCopyTimestep(taxisID3, taxisID2);

129
      pstreamDefTimestep(streamID3, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130

131
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
	{
133
134
	  pstreamInqRecord(streamID2, &varID, &levelID);
	  pstreamReadRecord(streamID2, array2, &nmiss2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135

136
	  if ( tsID == 0 || filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
	    {
138
	      if ( recID == 0 || filltype != FILL_REC )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
		{
140
141
		  pstreamInqRecord(streamID1, &varID, &levelID);
		  pstreamReadRecord(streamID1, array1, &nmiss1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
		}

144
	      if ( filltype == FILL_TS )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147
148
149
150
151
		{
		  gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
		  offset   = gridsize*levelID;
		  memcpy(vardata1[varID]+offset, array1, gridsize*sizeof(double));
		  varnmiss1[varID][levelID] = nmiss1;
		}
	    }
152
	  else if ( filltype == FILL_TS )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
156
157
158
159
160
	    {
	      gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
	      offset   = gridsize*levelID;
	      memcpy(array1, vardata1[varID]+offset, gridsize*sizeof(double));
	      nmiss1 = varnmiss1[varID][levelID];
	    }

	  gridsize = gridInqSize(vlistInqVarGrid(vlistID2, varID));
161
	  missval2 = vlistInqVarMissval(vlistID2, varID);
162
	  if ( recID == 0 || filltype != FILL_REC )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
166
167
168
169
	    {
	      missval1  = vlistInqVarMissval(vlistID1, varID);
	    }

	  if ( operatorID == IFTHEN )
	    {
	      for ( i = 0; i < gridsize; i++ )
170
		array3[i] = !DBL_IS_EQUAL(array1[i], missval1) && !DBL_IS_EQUAL(array1[i], 0.) ? array2[i] : missval2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
172
173
174
	    }
	  else if ( operatorID == IFNOTTHEN )
	    {
	      for ( i = 0; i < gridsize; i++ )
175
		array3[i] = !DBL_IS_EQUAL(array1[i], missval1) && DBL_IS_EQUAL(array1[i], 0.) ? array2[i] : missval2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178
179
180
181
182
183
	    }
	  else
	    {
	      cdoAbort("Operator not implemented!");
	    }

	  nmiss3 = 0;
	  for ( i = 0; i < gridsize; i++ )
184
	    if ( DBL_IS_EQUAL(array3[i], missval2) ) nmiss3++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185

186
187
	  pstreamDefRecord(streamID3, varID, levelID);
	  pstreamWriteRecord(streamID3, array3, nmiss3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189
190
191
192
	}

      tsID++;
    }

193
194
195
  pstreamClose(streamID3);
  pstreamClose(streamID2);
  pstreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
197
198
199
200

  if ( vardata1 )
    {
      for ( varID = 0; varID < nvars; varID++ )
	{
201
202
	  Free(vardata1[varID]);
	  Free(varnmiss1[varID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
204
	}

205
206
      Free(vardata1);
      Free(varnmiss1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
    }

209
210
211
  if ( array3 ) Free(array3);
  if ( array2 ) Free(array2);
  if ( array1 ) Free(array1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
213
214

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
}