cdotest.cc 13.3 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

  Copyright (C) 2003-2006 Brockmann Consult
  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.
*/

#include <assert.h>
#include <stdlib.h>
#include <sys/stat.h>

Ralf Mueller's avatar
Ralf Mueller committed
22
#include <cdi.h>
23

24
#include "dmemory.h"
25
#include "process_int.h"
26
#include "cdo_cdi_wrapper.h"
27

Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
#define TO_KELVIN(x) ((x) + 273.15)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
#define MISSVAL -9.0E33
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30

31
32
typedef std::array<std::string, 2> cdoCommand;

33
34
static int
equals(double expected, double actual, double eps)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
{
36
  return (int) std::fabs(expected - actual) < eps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
/*
39
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
double humidityIndex(double t, double p, double r, double missval)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
42
{
  static const double tmin = 26.0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
  static const double rmin = 40.0;
44

Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
  if ( t < tmin || r < rmin )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
    return missval;
47

48
  return t + (5.0 / 9.0) * ((0.01 * r * p * 6.112 * std::pow(10.0, (7.5 * t) / (237.7
49
+ t))) - 10.0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
*/
52
53
54
/* reads a NetCDF file containing data for a single grid point */
static void
readNcFile(const char path[], double **vars, int nvars, int nts)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
{
  int taxisID;
57
58
  int vlistID, varID, tsID;
  CdiStreamID streamID;
59
60
  size_t nmiss;
  int nrecs;
61

Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
  streamID = streamOpenRead(path);
63
  if (streamID < 0)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
66
67
    {
      fprintf(stderr, "%s\n", cdiStringError(streamID));
      exit(EXIT_FAILURE);
    }
68

Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
  vlistID = streamInqVlist(streamID);
70

71
  assert(vlistNtsteps(vlistID) == nts);
72

Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
75
76
  assert(vlistGridsizeMax(vlistID) == 1);
  assert(vlistNvars(vlistID) == nvars);

  taxisID = vlistInqTaxis(vlistID);
77

Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
80
81
  for (tsID = 0; tsID < nts; ++tsID)
    {
      nrecs = streamInqTimestep(streamID, tsID);
      assert(nrecs == nvars);
82

Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84
      taxisInqVdate(taxisID);
      taxisInqVtime(taxisID);
85

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
      for (varID = 0; varID < nvars; ++varID) streamReadVar(streamID, varID, &vars[varID][tsID], &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
    }
88

Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
90
91
92
  streamClose(streamID);
}

/* writes a NetCDF file containing data for a single grid point */
93
94
static void
writeNcFile(const char path[], const double array[], int length)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96
97
{
  int gridID, zaxisID, taxisID;
  int vlistID, varID, streamID, tsID;
98
  size_t nmiss;
99
100
101

  double lons[] = { 0.0 };
  double lats[] = { 0.0 };
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102
  double value;
103

Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
107
108
  gridID = gridCreate(GRID_LONLAT, 1);
  gridDefXsize(gridID, 1);
  gridDefYsize(gridID, 1);
  gridDefXvals(gridID, lons);
  gridDefYvals(gridID, lats);
109

Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
111
  zaxisID = zaxisCreate(ZAXIS_SURFACE, 1);
  vlistID = vlistCreate();
112

113
  varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARYING);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
  vlistDefVarName(vlistID, varID, "test_values");
115
  vlistDefVarDatatype(vlistID, varID, CDI_DATATYPE_FLT64);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
  vlistDefVarMissval(vlistID, varID, MISSVAL);
117

118
  taxisID = cdoTaxisCreate(TAXIS_ABSOLUTE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
  vlistDefTaxis(vlistID, taxisID);
120

121
  streamID = streamOpenWrite(path, CDI_FILETYPE_NC);
122
  if (streamID < 0)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
126
    {
      fprintf(stderr, "%s\n", cdiStringError(streamID));
      exit(EXIT_FAILURE);
    }
127

Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
  streamDefVlist(streamID, vlistID);
129

Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
131
  for (tsID = 0; tsID < length; ++tsID)
    {
132
133
134
135
136
137
138
139
140
      /* TODO - find a better solution for setting the date */
      if (tsID < 6)
        {
          taxisDefVdate(taxisID, 20060625 + tsID);
        }
      else
        {
          taxisDefVdate(taxisID, 20060701 + tsID - 6);
        }
141
      taxisDefVtime(taxisID, 235900);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
      streamDefTimestep(streamID, tsID);
143

Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
      value = array[tsID];
      nmiss = DBL_IS_EQUAL(value, MISSVAL) ? 1 : 0;
146

Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
148
      streamWriteVar(streamID, varID, &value, nmiss);
    }
149

Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
  streamClose(streamID);
151

Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
154
  vlistDestroy(vlistID);
  taxisDestroy(taxisID);
  zaxisDestroy(zaxisID);
155
  gridDestroy(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
157
}

158
159
160
161
162
163
164
165
/* creates the necessary storage for nvars variables with nts time steps on a
 * single grid point */
static double **
createVars(int nvars, int nts)
{
  double *array = (double *) Malloc(nvars * nts * sizeof(double));
  double **vars = (double **) Malloc(nvars * sizeof(double *));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
  int i;
167

Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
  for (i = 0; i < nvars; ++i) vars[i] = &array[i * nts];
169
170

  return vars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
172
173
}

/* destroys storage */
174
175
176
static void
destroyVars(double **vars)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
  if (vars != nullptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
    {
179
180
      Free(vars[0]);
      Free(vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
183
184
    }
}

/* gets the path of the CDO binary executable */
185
186
static const char *
getCdoPath()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
188
{
  char *cdoPath = getenv("CDO_PATH");
189

Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
  if (cdoPath == nullptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
193
194
    {
      struct stat filestat;
      int status;
      status = stat("$HOME/bin/cdo", &filestat);
195
196
      if (status == 0)
        return "$HOME/bin/cdo";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
      else
198
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
          fprintf(stderr, "cdo binary not found! Use CDO_PATH to set the path to cdo.\n");
200
201
          exit(-1);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
    }
203

Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206
207
  return cdoPath;
}

/* submits a CDO command */
208
209
210
static int
submitCdoCommand(const char *argument)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
  const char *cdoPath = getCdoPath();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
  char *cdo_command = (char *) Malloc(strlen(cdoPath) + strlen(argument) + 8);
213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
  int status;
215

Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
218
219
  cdo_command[0] = '\0';
  strcat(cdo_command, cdoPath);
  strcat(cdo_command, " -b 64 ");
  strcat(cdo_command, argument);
220

Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
  status = system(cdo_command);
  Free(cdo_command);
223

Uwe Schulzweida's avatar
Uwe Schulzweida committed
224
225
226
  return status;
}

227
228
static void
testEcaFd()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
  const double array[] = { MISSVAL, MISSVAL, TO_KELVIN(1.0), TO_KELVIN(1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0) };
231

Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
  int nvars = 1;
233
234
  int nts = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
  double **vars = createVars(nvars, nts);
236

Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
  writeNcFile("in.nc", array, 2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
239
240

  submitCdoCommand("eca_fd in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242

Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
  writeNcFile("in.nc", array, 3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
245
246

  submitCdoCommand("eca_fd in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
247
  assert(DBL_IS_EQUAL(vars[0][0], 0.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
250
251
252

  writeNcFile("in.nc", array, 5);

  submitCdoCommand("eca_fd in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
253
  assert(DBL_IS_EQUAL(vars[0][0], 1.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
255
256
257
258

  writeNcFile("in.nc", array, 6);

  submitCdoCommand("eca_fd in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
259
  assert(DBL_IS_EQUAL(vars[0][0], 2.));
260

Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262
263
  destroyVars(vars);
}

264
265
static void
testEcaSu()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
266
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267
  const double array[] = { MISSVAL, MISSVAL, TO_KELVIN(26.0), TO_KELVIN(24.0), TO_KELVIN(26.0), TO_KELVIN(24.0) };
268

Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
  int nvars = 1;
270
271
  int nts = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
  double **vars = createVars(nvars, nts);
273

Uwe Schulzweida's avatar
Uwe Schulzweida committed
274
275
276
277
278
279
280
  writeNcFile("in.nc", array, 2);

  submitCdoCommand("eca_su in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));

  writeNcFile("in.nc", array, 6);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
281
282
283

  submitCdoCommand("eca_su in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
284
  assert(DBL_IS_EQUAL(vars[0][0], 2.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286
287

  submitCdoCommand("eca_su,20.0 in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
288
  assert(DBL_IS_EQUAL(vars[0][0], 4.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
291

  submitCdoCommand("eca_su,30.0 in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
292
  assert(DBL_IS_EQUAL(vars[0][0], 0.));
293

Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
  destroyVars(vars);
}

297
298
static void
testFdns()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
300
  const double array1[] = { MISSVAL, TO_KELVIN(1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0) };
301
302
  const double array2[] = { 0.0, 0.0, 1.0, 0.0, MISSVAL };

Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
  int nvars = 1;
304
305
  int nts = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
  double **vars = createVars(nvars, nts);
307

Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
309
310
  writeNcFile("in1.nc", array1, 1);
  writeNcFile("in2.nc", array2, 1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
  submitCdoCommand("fdns in1.nc in2.nc out.nc");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
313
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));
314

Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
316
317
  writeNcFile("in1.nc", array1, 2);
  writeNcFile("in2.nc", array2, 2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
  submitCdoCommand("fdns in1.nc in2.nc out.nc");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319
  readNcFile("out.nc", vars, nvars, nts);
320
  assert(DBL_IS_EQUAL(vars[0][0], 0.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321
322
323
324

  writeNcFile("in1.nc", array1, 3);
  writeNcFile("in2.nc", array2, 3);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
  submitCdoCommand("fdns in1.nc in2.nc out.nc");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326
  readNcFile("out.nc", vars, nvars, nts);
327
  assert(DBL_IS_EQUAL(vars[0][0], 0.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
329
330
331

  writeNcFile("in1.nc", array1, 4);
  writeNcFile("in2.nc", array2, 4);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
  submitCdoCommand("fdns in1.nc in2.nc out.nc");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
  readNcFile("out.nc", vars, nvars, nts);
334
  assert(DBL_IS_EQUAL(vars[0][0], 1.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
337
338

  writeNcFile("in1.nc", array1, 5);
  writeNcFile("in2.nc", array2, 5);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
339
  submitCdoCommand("fdns in1.nc in2.nc out.nc");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
  readNcFile("out.nc", vars, nvars, nts);
341
  assert(DBL_IS_EQUAL(vars[0][0], 1.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
343
344
345

  writeNcFile("in1.nc", array1 + 4, 1);
  writeNcFile("in2.nc", array2 + 4, 1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
  submitCdoCommand("fdns in1.nc in2.nc out.nc");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
349
350
351
352
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));

  destroyVars(vars);
}

353
354
static void
testEcaGsl()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
357
358
  const double array1[]
      = { TO_KELVIN(6.0),  TO_KELVIN(6.0),  TO_KELVIN(6.0),  TO_KELVIN(6.0),  TO_KELVIN(6.0),  TO_KELVIN(6.0),  TO_KELVIN(6.0),
          TO_KELVIN(-1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0), TO_KELVIN(-1.0) };
359
360
  const double array2[] = { 0.5 };

Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
  int nvars = 2;
362
363
  int nts = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
  double **vars = createVars(nvars, nts);
365

Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
368
369
370
371
372
373
  writeNcFile("in1.nc", array1, 14);
  writeNcFile("in2.nc", array2, 2);

  submitCdoCommand("eca_gsl in1.nc in2.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], 7.0));
  assert(DBL_IS_EQUAL(vars[1][0], 181.0));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
376
377
  //  submitCdoCommand("eca_gsl,6,5.0,0.6 in1.nc in2.nc out.nc");
  //  readNcFile("out.nc", vars, nvars, nts);
  //  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));
  //  assert(DBL_IS_EQUAL(vars[1][0], MISSVAL));
378

Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
381
382
383
384
385
386
387
388
389
390
391
  writeNcFile("in1.nc", array1, 7);

  submitCdoCommand("eca_gsl in1.nc in2.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], 1.0));
  assert(DBL_IS_EQUAL(vars[1][0], 181.0));

  writeNcFile("in1.nc", array1, 8);

  submitCdoCommand("eca_gsl in1.nc in2.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], 2.0));
  assert(DBL_IS_EQUAL(vars[1][0], 181.0));
392

Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
  //  writeNcFile("in1.nc", array1, 4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394

Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398
  //  submitCdoCommand("eca_gsl in1.nc in2.nc out.nc");
  //  readNcFile("out.nc", vars, nvars, nts);
  //  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));
  //  assert(DBL_IS_EQUAL(vars[1][0], MISSVAL));
399

Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
  destroyVars(vars);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
static
void testHi()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
406
407
408
{
  const double array1[] = {MISSVAL, 70.0, 36.0, 46.0, 56.0};
  const double array2[] = {1.0, 1.0, 1.0, 1.0, 1.0};
  const double array3[] = {0.4, 0.4, 0.3, 0.5, 0.6};
409

Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
411
  int nvars = 1;
  int nts   = 5;
412

Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
  double **vars = createVars(nvars, nts);
414

Uwe Schulzweida's avatar
Uwe Schulzweida committed
415
416
417
418
419
420
421
  writeNcFile("in1.nc", array1, 5);
  writeNcFile("in2.nc", array2, 5);
  writeNcFile("in3.nc", array3, 5);

  submitCdoCommand("hi in1.nc in2.nc in3.nc out.nc");

  readNcFile("out.nc", vars, nvars, nts);
422
423
424
425
426
427
428
  assert(equals(vars[0][0], humidityIndex(array1[0], array2[0], array3[0],
MISSVAL), 1.0e-5)); assert(equals(vars[0][1], humidityIndex(array1[1],
array2[1], array3[1], MISSVAL), 1.0e-5)); assert(equals(vars[0][2],
humidityIndex(array1[2], array2[2], array3[2], MISSVAL), 1.0e-5));
  assert(equals(vars[0][3], humidityIndex(array1[3], array2[3], array3[3],
MISSVAL), 1.0e-5)); assert(equals(vars[0][4], humidityIndex(array1[4],
array2[4], array3[4], MISSVAL), 1.0e-5));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431

  destroyVars(vars);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
*/
433
434
static void
testTimcount()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
435
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
436
  const double array[] = { MISSVAL, MISSVAL, TO_KELVIN(1.0), MISSVAL, TO_KELVIN(1.0), TO_KELVIN(1.0) };
437

Uwe Schulzweida's avatar
Uwe Schulzweida committed
438
439
  /* number of output variables and time steps */
  int nvars = 1;
440
441
  int nts = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
  double **vars = createVars(nvars, nts);
443

Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
445
446
447
448
449
450
451
452
453
  writeNcFile("in.nc", array, 2);

  submitCdoCommand("timcount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));

  writeNcFile("in.nc", array, 3);

  submitCdoCommand("timcount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
454
  assert(DBL_IS_EQUAL(vars[0][0], 1.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455
456
457
458
459

  writeNcFile("in.nc", array, 5);

  submitCdoCommand("timcount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
460
  assert(DBL_IS_EQUAL(vars[0][0], 2.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461
462
463
464
465

  writeNcFile("in.nc", array, 6);

  submitCdoCommand("timcount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
466
  assert(DBL_IS_EQUAL(vars[0][0], 3.));
467

Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
469
  destroyVars(vars);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
470
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
471
472
static
void testSeascount()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
{
474
  const double array[] = {MISSVAL, MISSVAL, TO_KELVIN(1.0), MISSVAL,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
475
    TO_KELVIN(1.0), TO_KELVIN(1.0)};
476

Uwe Schulzweida's avatar
Uwe Schulzweida committed
477
  // number of output variables and time steps
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478
479
  int nvars = 1;
  int nts   = 1;
480

Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
  double **vars = createVars(nvars, nts);
482

Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
484
485
486
487
488
489
490
491
492
  writeNcFile("in.nc", array, 2);

  submitCdoCommand("seascount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
  assert(DBL_IS_EQUAL(vars[0][0], MISSVAL));

  writeNcFile("in.nc", array, 3);

  submitCdoCommand("seascount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
493
  assert(DBL_IS_EQUAL(vars[0][0], 1.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494
495
496
497
498

  writeNcFile("in.nc", array, 5);

  submitCdoCommand("seascount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
499
  assert(DBL_IS_EQUAL(vars[0][0], 2.));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
501
502
503
504

  writeNcFile("in.nc", array, 6);

  submitCdoCommand("seascount in.nc out.nc");
  readNcFile("out.nc", vars, nvars, nts);
505
  assert(DBL_IS_EQUAL(vars[0][0], 3.));
506

Uwe Schulzweida's avatar
Uwe Schulzweida committed
507
508
  destroyVars(vars);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
*/
510
511
static void
testWct()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
{
513
514
515
  const double array1[] = { -3.1102 };
  const double array2[] = { 1.9787 };

Uwe Schulzweida's avatar
Uwe Schulzweida committed
516
  int nvars = 1;
517
518
  int nts = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
  double **vars = createVars(nvars, nts);
520

Uwe Schulzweida's avatar
Uwe Schulzweida committed
521
522
523
524
525
526
527
  writeNcFile("in1.nc", array1, 1);
  writeNcFile("in2.nc", array2, 1);

  submitCdoCommand("wct in1.nc in2.nc out.nc");

  readNcFile("out.nc", vars, nvars, nts);
  assert(equals(vars[0][0], -6.34597, 1.0e-5));
528

Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
530
531
  destroyVars(vars);
}

532
533
int
main(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
534
535
536
{
  testEcaFd();
  testEcaSu();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
538
539
  testEcaGsl();

  testFdns();
540
  /* testHi(); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
542
  testTimcount();
  testWct();
543

Uwe Schulzweida's avatar
Uwe Schulzweida committed
544
545
  return 0;
}