Arithc.c 4.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.

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
18
  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.
*/

/*
19
   This module contains the following operators:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20

21
22
23
24
      Arithc     addc            Add by constant
      Arithc     subc            Subtract by constant
      Arithc     mulc            Multiply by constant
      Arithc     divc            Divide by constant
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
      Arithc     mod             Modulo operator
26
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28


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


Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
int *fill_vars(int vlistID)
{
  int varID;
  int nvars = vlistNvars(vlistID);
  int *vars = (int *) malloc(nvars*sizeof(int));

  if ( cdoNumVarnames )
    {
      int nfound = 0;
      char varname[CDI_MAX_NAME];

      for ( varID = 0; varID < nvars; ++varID )
	{
	  vars[varID] = 0;

	  vlistInqVarName(vlistID, varID, varname);

	  for ( int i = 0; i < cdoNumVarnames; ++i )
	    if ( strcmp(varname, cdoVarnames[i]) == 0 )
	      {
		vars[varID] = 1;
		nfound++;
		break;
	      }
	}

      if ( nfound == 0 )
	cdoAbort("Variable -n %s%s not found!", cdoVarnames[0], cdoNumVarnames > 1 ? ",..." : "");
    }
  else
    {
      for ( varID = 0; varID < nvars; ++varID ) vars[varID] = 1;
    }

  return (vars);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
75
76
77
void *Arithc(void *argument)
{
  int operatorID;
  int operfunc;
  int streamID1, streamID2;
78
  int gridsize, i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
81
82
83
  int nrecs, recID;
  int tsID;
  int varID, levelID;
  int vlistID1, vlistID2;
  double rconst;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
  field_t field;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
  int taxisID1, taxisID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
  int *vars = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
88
89

  cdoInitialize(argument);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
92
93
94
  cdoOperatorAdd("addc", func_add, 0, "constant value");
  cdoOperatorAdd("subc", func_sub, 0, "constant value");
  cdoOperatorAdd("mulc", func_mul, 0, "constant value");
  cdoOperatorAdd("divc", func_div, 0, "constant value");
  cdoOperatorAdd("mod",  func_mod, 0, "divisor");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96

  operatorID = cdoOperatorID();
97
  operfunc = cdoOperatorF1(operatorID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98

Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
  operatorInputArg(cdoOperatorEnter(operatorID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
101
102
103
104
105
106
  rconst = atof(operatorArgv()[0]);

  streamID1 = streamOpenRead(cdoStreamName(0));

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
  vars = fill_vars(vlistID1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
111
112
113
114
115
116
117
118
  taxisID1 = vlistInqTaxis(vlistID1);
  taxisID2 = taxisDuplicate(taxisID1);
  vlistDefTaxis(vlistID2, taxisID2);

  streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());

  streamDefVlist(streamID2, vlistID2);

  gridsize = vlistGridsizeMax(vlistID1);

119
  field_init(&field);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  field.ptr    = (double *) malloc(gridsize*sizeof(double));
  field.weight = NULL;

  tsID = 0;
  while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
    {
      taxisCopyTimestep(taxisID2, taxisID1);

      streamDefTimestep(streamID2, tsID);

      for ( recID = 0; recID < nrecs; recID++ )
	{
	  streamInqRecord(streamID1, &varID, &levelID);
	  streamReadRecord(streamID1, field.ptr, &field.nmiss);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
138
	  if ( vars[varID] )
	    {
	      field.grid    = vlistInqVarGrid(vlistID1, varID);
	      field.missval = vlistInqVarMissval(vlistID1, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139

Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
	      farcfun(&field, rconst, operfunc);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141

Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
144
145
146
147
	      /* recalculate number of missing values */
	      gridsize = gridInqSize(field.grid);
	      field.nmiss = 0;
	      for ( i = 0; i < gridsize; ++i )
		if ( DBL_IS_EQUAL(field.ptr[i], field.missval) ) field.nmiss++;
	    }
148

Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
151
152
153
154
155
156
157
158
	  streamDefRecord(streamID2, varID, levelID);
	  streamWriteRecord(streamID2, field.ptr, field.nmiss);
	}
      tsID++;
    }

  streamClose(streamID2);
  streamClose(streamID1);

  if ( field.ptr ) free(field.ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
  if ( vars ) free(vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160

161
162
  vlistDestroy(vlistID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
166
  cdoFinish();

  return (0);
}