Cond.c 6.02 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
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
33
34
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"


void *Cond(void *argument)
{
  int IFTHEN, IFNOTTHEN;
  int operatorID;
35
36
  enum {FILL_NONE, FILL_TS, FILL_REC};
  int filltype = FILL_NONE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41
42
43
44
45
46
  int streamID1, streamID2, streamID3;
  int gridsize;
  int nrecs, nrecs2, nvars = 0, nlev, recID;
  int tsID;
  int varID, levelID;
  int offset;
  int vlistID1, vlistID2, vlistID3;
  int nmiss1, nmiss2, nmiss3;
  int i;
  double missval1 = -9.E33;
47
  double missval2 = -9.E33;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  double *array1, *array2, *array3;
  int **varnmiss1 = NULL;
  double **vardata1 = NULL;
  int taxisID2, taxisID3;

  cdoInitialize(argument);

  IFTHEN    = cdoOperatorAdd("ifthen",    0, 0, NULL);
  IFNOTTHEN = cdoOperatorAdd("ifnotthen", 0, 0, NULL);

  operatorID = cdoOperatorID();

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

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

  taxisID2 = vlistInqTaxis(vlistID2);
  taxisID3 = taxisDuplicate(taxisID2);
  vlistDefTaxis(vlistID3, taxisID3);

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

77
  if ( filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
    vlistCompare(vlistID1, vlistID2, CMP_SFT);
79
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
  nospec(vlistID1);
81
  nospec(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
83
84
85
86

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

  streamDefVlist(streamID3, vlistID3);

87
88
89
90
  gridsize = vlistGridsizeMax(vlistID2);

  if ( filltype == FILL_REC && gridsize != gridInqSize(vlistGrid(vlistID1, 0)) )
    cdoAbort("Stream1 >%s< has wrong gridsize!", cdoStreamName(0));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
92
93
94
95
96
97
98

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

  if ( cdoVerbose )
    cdoPrint("Number of timesteps: file1 %d, file2 %d", vlistNtsteps(vlistID1), vlistNtsteps(vlistID2));

99
  if ( filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
    {
101
102
      if ( (vlistNtsteps(vlistID1) == 1 || vlistNtsteps(vlistID1) == 0) &&
	    vlistNtsteps(vlistID2) != 1 && vlistNtsteps(vlistID2) != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
	{
104
105
	  filltype = FILL_TS;
	  cdoPrint("Filling up stream1 >%s< by copying the first timestep.", cdoStreamName(0));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106

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

  tsID = 0;
  while ( (nrecs = streamInqTimestep(streamID2, tsID)) )
    {
123
      if ( tsID == 0 || filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
	{
	  nrecs2 = streamInqTimestep(streamID1, tsID);
	  if ( nrecs2 == 0 )
	    cdoAbort("Input streams have different number of timesteps!");
	}

      taxisCopyTimestep(taxisID3, taxisID2);

      streamDefTimestep(streamID3, tsID);

      for ( recID = 0; recID < nrecs; recID++ )
	{
	  streamInqRecord(streamID2, &varID, &levelID);
	  streamReadRecord(streamID2, array2, &nmiss2);

139
	  if ( tsID == 0 || filltype == FILL_NONE )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
	    {
141
	      if ( recID == 0 || filltype != FILL_REC )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
144
145
146
		{
		  streamInqRecord(streamID1, &varID, &levelID);
		  streamReadRecord(streamID1, array1, &nmiss1);
		}

147
	      if ( filltype == FILL_TS )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
150
151
152
153
154
		{
		  gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
		  offset   = gridsize*levelID;
		  memcpy(vardata1[varID]+offset, array1, gridsize*sizeof(double));
		  varnmiss1[varID][levelID] = nmiss1;
		}
	    }
155
	  else if ( filltype == FILL_TS )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
157
158
159
160
161
162
163
	    {
	      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));
164
	  missval2 = vlistInqVarMissval(vlistID2, varID);
165
	  if ( recID == 0 || filltype != FILL_REC )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
167
168
169
170
171
172
	    {
	      missval1  = vlistInqVarMissval(vlistID1, varID);
	    }

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

	  nmiss3 = 0;
	  for ( i = 0; i < gridsize; i++ )
187
	    if ( DBL_IS_EQUAL(array3[i], missval2) ) nmiss3++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189
190
191
192
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
218
219

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

      tsID++;
    }

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

  if ( vardata1 )
    {
      for ( varID = 0; varID < nvars; varID++ )
	{
	  free(vardata1[varID]);
	  free(varnmiss1[varID]);
	}

      free(vardata1);
      free(varnmiss1);
    }

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

  cdoFinish();

  return (0);
}