1 /*
2 This file is part of CDO. CDO is a collection of Operators to manipulate and analyse Climate model Data.
3
4 Author: Uwe Schulzweida
5
6 */
7
8 /*
9 This module contains the following operators:
10
11 Arithc addc Add by constant
12 Arithc subc Subtract by constant
13 Arithc mulc Multiply by constant
14 Arithc divc Divide by constant
15 Arithc mod Modulo operator
16 */
17
18 #include <cdi.h>
19
20 #include "cdo_options.h"
21 #include "process_int.h"
22 #include "cdo_vlist.h"
23 #include "param_conversion.h"
24
25 static void
fill_vars(const VarList & varList,std::vector<bool> & vars)26 fill_vars(const VarList &varList, std::vector<bool> &vars)
27 {
28 const auto nvars = vars.size();
29
30 if (Options::cdo_num_varnames() > 0)
31 {
32 auto found = false;
33 for (size_t varID = 0; varID < nvars; ++varID)
34 {
35 vars[varID] = false;
36 for (size_t i = 0; i < Options::cdo_num_varnames(); ++i)
37 if (strcmp(varList[varID].name, Options::cdoVarnames[i].c_str()) == 0)
38 {
39 vars[varID] = true;
40 found = true;
41 break;
42 }
43 }
44
45 if (!found) cdo_abort("Variable %s%s not found!", Options::cdoVarnames[0], Options::cdo_num_varnames() > 1 ? ",..." : "");
46 }
47 else
48 {
49 for (size_t varID = 0; varID < nvars; ++varID) vars[varID] = true;
50 }
51 }
52
53 static void
addOperators(void)54 addOperators(void)
55 {
56 // clang-format off
57 cdo_operator_add("addc", FieldFunc_Add, 1, "constant value");
58 cdo_operator_add("subc", FieldFunc_Sub, 1, "constant value");
59 cdo_operator_add("mulc", FieldFunc_Mul, 1, "constant value");
60 cdo_operator_add("divc", FieldFunc_Div, 1, "constant value");
61 cdo_operator_add("minc", FieldFunc_Min, 0, "constant value");
62 cdo_operator_add("maxc", FieldFunc_Max, 0, "constant value");
63 cdo_operator_add("mod", FieldFunc_Mod, 0, "divisor");
64 // clang-format on
65 }
66
67 void *
Arithc(void * process)68 Arithc(void *process)
69 {
70 cdo_initialize(process);
71
72 addOperators();
73
74 const auto operatorID = cdo_operator_id();
75 const auto operfunc = cdo_operator_f1(operatorID);
76 const bool opercplx = cdo_operator_f2(operatorID);
77
78 operator_input_arg(cdo_operator_enter(operatorID));
79 if (cdo_operator_argc() < 1) cdo_abort("Too few arguments!");
80 if (cdo_operator_argc() > 2) cdo_abort("Too many arguments!");
81 const auto rconst = parameter_to_double(cdo_operator_argv(0));
82
83 double rconstcplx[2] = { rconst, 0.0 };
84 if (cdo_operator_argc() == 2) rconstcplx[1] = parameter_to_double(cdo_operator_argv(1));
85
86 const auto streamID1 = cdo_open_read(0);
87
88 const auto vlistID1 = cdo_stream_inq_vlist(streamID1);
89 const auto vlistID2 = vlistDuplicate(vlistID1);
90
91 VarList varList1;
92 varListInit(varList1, vlistID1);
93
94 const auto nvars = vlistNvars(vlistID1);
95 // for (int varID = 0; varID < nvars; ++varID) varList1[varID].memType = MemType::Double;
96
97 std::vector<bool> vars(nvars);
98 fill_vars(varList1, vars);
99
100 const auto taxisID1 = vlistInqTaxis(vlistID1);
101 const auto taxisID2 = taxisDuplicate(taxisID1);
102 vlistDefTaxis(vlistID2, taxisID2);
103
104 const auto streamID2 = cdo_open_write(1);
105 cdo_def_vlist(streamID2, vlistID2);
106
107 const auto nwpv = (vlistNumber(vlistID1) == CDI_COMP) ? 2 : 1;
108 if (nwpv == 2 && !opercplx) cdo_abort("Fields with complex numbers are not supported by this operator!");
109
110 Field field;
111
112 int tsID = 0;
113 while (true)
114 {
115 const auto nrecs = cdo_stream_inq_timestep(streamID1, tsID);
116 if (nrecs == 0) break;
117
118 cdo_taxis_copy_timestep(taxisID2, taxisID1);
119 cdo_def_timestep(streamID2, tsID);
120
121 for (int recID = 0; recID < nrecs; recID++)
122 {
123 int varID, levelID;
124 cdo_inq_record(streamID1, &varID, &levelID);
125 field.init(varList1[varID]);
126 cdo_read_record(streamID1, field);
127
128 if (vars[varID])
129 {
130 if (field.nwpv == 2)
131 fieldc_function_complex(field, rconstcplx, operfunc);
132 else
133 fieldc_function(field, rconst, operfunc);
134
135 // recalculate number of missing values
136 field_num_mv(field);
137 }
138
139 cdo_def_record(streamID2, varID, levelID);
140 cdo_write_record(streamID2, field);
141 }
142
143 tsID++;
144 }
145
146 cdo_stream_close(streamID2);
147 cdo_stream_close(streamID1);
148
149 vlistDestroy(vlistID2);
150
151 cdo_finish();
152
153 return nullptr;
154 }
155