forked from perabrahamsen/daisy-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_table.C
445 lines (409 loc) · 15.4 KB
/
action_table.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// action_table.C
//
// Copyright 2005 Per Abrahamsen and KVL.
//
// This file is part of Daisy.
//
// Daisy is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Daisy 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 Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with Daisy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define BUILD_DLL
#include "action.h"
#include "metalib.h"
#include "library.h"
#include "daisy.h"
#include "field.h"
#include "am.h"
#include "im.h"
#include "units.h"
#include "lexer_table.h"
#include "mathlib.h"
#include "librarian.h"
#include "treelog.h"
#include "frame_model.h"
#include "volume.h"
#include "check.h"
#include "time.h"
#include <set>
#include <map>
#include <memory>
#include <sstream>
struct ActionTable : public Action
{
const Metalib& metalib;
const std::unique_ptr<Action> sow;
const std::unique_ptr<Action> emerge;
const std::unique_ptr<Action> harvest;
const std::unique_ptr<FrameModel> am;
std::set<Time> sow_dates;
std::set<Time> emerge_dates;
std::set<Time> harvest_dates;
std::map<Time, double> fertilize_events;
std::map<Time, double> irrigate_events;
std::map<Time, const FrameModel*> fertilizers;
const double flux; // [mm/h]
const boost::shared_ptr<Volume> volume;
static void read_model (const LexerTable&,
const std::vector<std::string>& entries,
int tag_c, const Library& library,
std::map<Time, const FrameModel*>& alists);
static void read_event (const LexerTable&,
const std::vector<std::string>& entries,
int tag_c, std::map<Time, double>& events);
static void read_date (const LexerTable&,
const std::vector<std::string>& entries,
int rag_c, std::set<Time>& dates);
void doIt (Daisy& daisy, const Scope&, Treelog& msg);
void tick (const Daisy&, const Scope&, Treelog&);
void initialize (const Daisy&, const Scope&, Treelog&);
bool check (const Daisy&, const Scope&, Treelog& err) const;
ActionTable (const BlockModel& al);
};
void
ActionTable::read_event (const LexerTable& lex,
const std::vector<std::string>& entries,
int tag_c, std::map<Time, double>& events)
{
if (tag_c < 0)
return;
const std::string val = entries[tag_c];
if (lex.is_missing (val))
return;
Time time (9999, 1, 1, 0);
if (!lex.get_time_dh (entries, time, 8))
return;
const double value = lex.convert_to_double (val);
events[time] = value;
}
void
ActionTable::read_model (const LexerTable& lex,
const std::vector<std::string>& entries,
int tag_c, const Library& library,
std::map<Time, const FrameModel*>& alists)
{
if (tag_c < 0)
return;
const std::string val = entries[tag_c];
if (lex.is_missing (val))
return;
if (!library.check (symbol (val)))
{
lex.error ("'" + val + "' undefined");
return;
}
Time time (9999, 1, 1, 0);
if (!lex.get_time_dh (entries, time, 8))
return;
alists[time] = &library.model (symbol (val));
}
void
ActionTable::read_date (const LexerTable& lex,
const std::vector<std::string>& entries,
int tag_c, std::set<Time>& dates)
{
if (tag_c < 0)
return;
const std::string val = entries[tag_c];
if (lex.is_missing (val))
return;
Time time (9999, 1, 1, 0);
if (!LexerTable::get_time_dh (val, time, 8))
return;
dates.insert (time);
}
void
ActionTable::doIt (Daisy& daisy, const Scope& scope, Treelog& msg)
{
// Units.
const Units& units = daisy.units ();
static const symbol mg_per_square_m ("mg/m^2");
const Unit& u_mg_per_square_m = units.get_unit (mg_per_square_m);
const Unit& u_ppm = units.get_unit (Units::ppm ());
const Unit& u_per_mm = units.get_unit (Units::per_mm ());
const Unit& u_solute = units.get_unit (IM::solute_unit ());
if (sow.get () && sow_dates.find (daisy.time ()) != sow_dates.end ())
sow->doIt (daisy, scope, msg);
if (emerge.get () && emerge_dates.find (daisy.time ()) != emerge_dates.end ())
emerge->doIt (daisy, scope, msg);
if (harvest.get ()
&& harvest_dates.find (daisy.time ()) != harvest_dates.end ())
harvest->doIt (daisy, scope, msg);
if ((am.get ()
|| fertilizers.find (daisy.time ()) != fertilizers.end ())
&& fertilize_events.find (daisy.time ()) != fertilize_events.end ())
{
FrameModel fert (((fertilizers.find (daisy.time ()) != fertilizers.end ())
? *fertilizers[daisy.time ()] : *am), Frame::parent_link);
AM::set_utilized_weight (metalib, fert, fertilize_events[daisy.time ()]);
if (irrigate_events.find (daisy.time ()) != irrigate_events.end ())
{
double value = irrigate_events[daisy.time ()];
std::ostringstream tmp;
if (iszero (value))
{
tmp << "Applying minimum of 0.1 mm\n";
value = 0.1;
}
IM im = AM::get_IM (metalib, u_mg_per_square_m, fert);
daisy_assert (std::isnormal (value));
im.multiply_assign (Scalar (1.0 / value, u_per_mm), u_ppm);
daisy.field ().irrigate (value/flux, flux,
Irrigation::at_air_temperature,
Irrigation::subsoil,
im, volume, false, msg);
tmp << "Fertigating " << value << " mm, with";
for (IM::const_iterator i = im.begin (); i != im.end (); i++)
{
const symbol chem = *i;
const double value = im.get_value (chem, u_ppm);
if (std::isnormal (value))
tmp << " " << value << " ppm " << chem;
}
msg.message (tmp.str ());
}
else
{
double water = 0.0;
std::ostringstream tmp;
if (AM::is_mineral (metalib, fert))
tmp << "Fertilizing " << fert.number ("weight")
<< " kg "<< fert.type_name () << "-N/ha";
else if (AM::is_organic (metalib, fert))
{
tmp << "Fertilizing " << fert.number ("weight")
<< " ton "<< fert.type_name () << " ww/ha";
const double utilized_weight
= AM::utilized_weight (metalib, fert);
if (utilized_weight > 0.0)
tmp << "; utilized " << utilized_weight << " kg N/ha";
water = AM::get_water (metalib, fert);
if (water > 0.0)
tmp << "; water " << water << " mm";
}
else
tmp << "Fertilizing " << fert.type_name ();
msg.message (tmp.str ());
daisy.field ().fertilize (metalib, fert,
daisy.time (), msg);
if (water > 0.0)
daisy.field ().irrigate (water/flux, flux,
Irrigation::at_air_temperature,
Irrigation::surface, IM (u_solute),
boost::shared_ptr<Volume> (), false, msg);
}
}
else if (irrigate_events.find (daisy.time ()) != irrigate_events.end ())
{
const double value = irrigate_events[daisy.time ()];
std::ostringstream tmp;
IM im;
daisy.field ().irrigate (value/flux, flux,
Irrigation::at_air_temperature,
Irrigation::overhead, im,
boost::shared_ptr<Volume> (), false, msg);
tmp << "Irrigating " << value << " mm";
msg.message (tmp.str ());
}
}
void
ActionTable::tick (const Daisy& daisy, const Scope& scope, Treelog& msg)
{
if (sow.get ())
sow->tick (daisy, scope, msg);
if (emerge.get ())
emerge->tick (daisy, scope, msg);
if (harvest.get ())
harvest->tick (daisy, scope, msg);
}
void
ActionTable::initialize (const Daisy& daisy, const Scope& scope, Treelog& msg)
{
if (sow.get ())
sow->initialize (daisy, scope, msg);
if (emerge.get ())
emerge->initialize (daisy, scope, msg);
if (harvest.get ())
harvest->initialize (daisy, scope, msg);
}
bool
ActionTable::check (const Daisy& daisy, const Scope& scope, Treelog& msg) const
{
bool ok = true;
if (sow.get () && ! sow->check (daisy, scope, msg))
ok = false;
if (emerge.get () && ! emerge->check (daisy, scope, msg))
ok = false;
if (harvest.get () && ! harvest->check (daisy, scope, msg))
ok = false;
return ok;
}
ActionTable::ActionTable (const BlockModel& al)
: Action (al),
metalib (al.metalib ()),
sow (al.check ("sow")
? Librarian::build_item<Action> (al, "sow")
: NULL),
emerge (al.check ("emerge")
? Librarian::build_item<Action> (al, "emerge")
: NULL),
harvest (al.check ("harvest")
? Librarian::build_item<Action> (al, "harvest")
: NULL),
am (al.check ("fertilizer")
? &al.model ("fertilizer").clone ()
: NULL),
flux (al.number ("flux")),
volume (Volume::build_obsolete (al).release ())
{
LexerTable lex (al);
if (!lex.read_header (al.msg ()))
{
al.error ("Read failed");
return;
}
const int harvest_c = harvest.get () ? lex.find_tag ("Harvest") : -1;
const int sow_c = sow.get () ? lex.find_tag ("Planting") : -1;
const int emerge_c = emerge.get () ? lex.find_tag ("Emerging") : -1;
const int irrigate_c = al.flag ("enable_irrigation")
? lex.find_tag ("Irrigate") : -1;
const int fertilizer_c = lex.find_tag ("Fertilizer");
const int fertilize_c = (al.flag ("enable_fertilization")
&& (am.get () || fertilizer_c >= 0))
? lex.find_tag ("Fertilize") : -1;
if (sow_c < 0 && emerge_c < 0 && harvest_c < 0
&& irrigate_c < 0 && fertilize_c < 0)
al.msg ().warning ("No applicable column found");
if (sow_c < 0 && sow.get ())
al.msg ().warning ("Specified planting operation not used");
if (emerge_c < 0 && emerge.get ())
al.msg ().warning ("Specified emerge operation not used");
if (harvest_c < 0 && harvest.get ())
al.msg ().warning ("Specified harvest operation not used");
if (fertilize_c < 0 && am.get ())
al.msg ().warning ("Specified fertilizer not used");
if (fertilizer_c >= 0 && am.get ())
al.msg ().warning ("Fertilizer specified twice");
while (lex.good ())
{
// Read entries.
std::vector<std::string> entries;
if (!lex.get_entries (entries))
continue;
read_event (lex, entries, irrigate_c, irrigate_events);
read_event (lex, entries, fertilize_c, fertilize_events);
read_model (lex, entries, fertilizer_c,
al.metalib ().library (AM::component),
fertilizers);
read_date (lex, entries, sow_c, sow_dates);
read_date (lex, entries, emerge_c, emerge_dates);
read_date (lex, entries, harvest_c, harvest_dates);
}
if (sow_dates.size () == 0
&& emerge_dates.size () == 0
&& harvest_dates.size () == 0
&& fertilize_events.size () == 0
&& irrigate_events.size () == 0)
al.msg ().warning ("Nothing to do");
}
// Add the ActionTable syntax to the syntax table.
static struct ActionTableSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new ActionTable (al); }
ActionTableSyntax ()
: DeclareModel (Action::component, "table", "\
Read management actions from a Daisy data file.\n\
\n\
After the ddf header, the following column tags are recognized (with\n\
the dimension for the dimension linein square brackets).\n\
\n\
Date [date]: The date for fertilization or irrigation.\n\
\n\
Planting [date]: The content should be a date in yyyy-mm-dd format,\n\
where the operation specified by the 'sow' attribute will be perfomed.\n\
\n\
Emerging [date]: The content should be a date in yyyy-mm-dd format,\n\
where the operation specified by the 'emerge' attribute will be perfomed.\n\
\n\
Harvest [date]: The content should be a date in yyyy-mm-dd format,\n\
where the operation specified by the 'harvest' attribute will be\n\
perfomed.\n\
\n\
Irrigate [mm]: The content should be an irrigation amount, that will\n\
be applied as overhead irrigation for the date specified in the 'Date'\n\
field. You can disable it with the 'enable_irrigation' attribute.\n\
\n\
Fertilize [kg N/ha]: The content should be an amount of nitrogen\n\
fertilizer to be applied on the date specified in the 'Date' field.\n\
The fertilizer type will be either the one specified in the\n\
'Fertilizer' column, or the 'fertilizer' attribute. You can disable\n\
it with the 'enable_fertilization' attribute.\n\
\n\
Fertilizer [name]: The type of fertilizer to be applied.")
{ }
static bool check_alist (const Metalib&, const Frame& al, Treelog& err)
{
bool ok = true;
if (al.check ("from") && al.check ("to"))
{
const double from = al.number ("from");
const double to = al.number ("to");
if (from <= to)
{
err.entry ("'from' must be higher than 'to' in"
" the subsoil irrigation zone");
ok = false;
}
}
return ok;
}
void load_frame (Frame& frame) const
{
frame.add_check (check_alist);
LexerTable::load_syntax (frame);
frame.declare_object ("sow", Action::component,
Attribute::OptionalConst, Attribute::Singleton,
"Sow action.");
frame.declare_object ("emerge", Action::component,
Attribute::OptionalConst, Attribute::Singleton,
"Emerge action.");
frame.declare_object ("harvest", Action::component,
Attribute::OptionalConst, Attribute::Singleton,
"Harvest action.");
frame.declare_object ("fertilizer", AM::component,
Attribute::OptionalConst, Attribute::Singleton, "\
The fertilizer you want to apply.");
frame.declare_boolean ("enable_irrigation", Attribute::Const, "\
Set this to false to ignore any irrigation information in the file.");
frame.set ("enable_irrigation", true);
frame.declare_boolean ("enable_fertilization", Attribute::Const, "\
Set this to false to ignore any fertilization information in the file.");
frame.set ("enable_fertilization", true);
frame.declare ("flux", "mm/h", Check::positive (), Attribute::Const,
"Water application speed.");
frame.set ("flux", 2.0);
frame.declare_object ("volume", Volume::component,
Attribute::Const, Attribute::Singleton,
"Soil volume to add irritaion.");
frame.set ("volume", "box");
frame.declare ("from", "cm", Check::non_positive (), Attribute::Const, "\
Height where you want to start the incorporation (a negative number).\n\
OBSOLETE: Use (volume box (top FROM)) instead.");
frame.set ("from", -5.0);
frame.declare ("to", "cm", Check::non_positive (), Attribute::Const, "\
Height where you want to end the incorporation (a negative number).\n\
OBSOLETE: Use (volume box (bottom TO)) instead.");
frame.set ("to", -25.0);
}
} ActionTable_syntax;
// action_table.C ends here.