-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1714 lines (1559 loc) · 70.1 KB
/
main.cpp
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SOD model
*
* Copyright (C) 2015-2017 by the authors.
*
* Authors: Zexi Chen (zchen22 ncsu edu)
* Vaclav Petras (wenzeslaus gmail com)
* Anna Petrasova (kratochanna gmail com)
*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*/
#include "graster.hpp"
#include "pops/model.hpp"
#include "pops/model_type.hpp"
#include "pops/date.hpp"
#include "pops/raster.hpp"
#include "pops/kernel.hpp"
#include "pops/treatments.hpp"
#include "pops/spread_rate.hpp"
#include "pops/statistics.hpp"
#include "pops/scheduling.hpp"
#include "pops/quarantine.hpp"
#include "pops/host_pool.hpp"
#include "pops/pest_pool.hpp"
#include "pops/multi_host_pool.hpp"
extern "C" {
#include <grass/gis.h>
#include <grass/glocale.h>
#include <grass/vector.h>
#include <grass/raster.h>
}
#include <map>
#include <tuple>
#include <vector>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <sys/stat.h>
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::round;
using std::isnan;
using namespace pops;
#define DIM 1
void fatal_option_required_for_value(struct Option* required, struct Option* given)
{
G_fatal_error(
_("The option %s is required for %s=%s"),
required->key,
given->key,
given->answer);
}
// check if a file exists
inline bool file_exists(const char* name)
{
struct stat buffer;
return (stat(name, &buffer) == 0);
}
inline void file_exists_or_fatal_error(struct Option* option)
{
if (option->answer && !file_exists(option->answer))
G_fatal_error(
_("Option %s: File %s does not exist"), option->key, option->answer);
}
string generate_name(const string& basename, const Date& date)
{
// counting on year being 4 digits
auto year = G_double_to_basename_format(date.year(), 4, 0);
auto month = G_double_to_basename_format(date.month(), 2, 0);
auto day = G_double_to_basename_format(date.day(), 2, 0);
auto sep = G_get_basename_separator();
string name = basename + sep + year + "_" + month + "_" + day;
return name;
}
void write_average_area(
const std::vector<Img>& infected,
const char* raster_name,
double ew_res,
double ns_res,
const std::vector<std::vector<int>>& suitable_cells)
{
struct History hist;
double avg = 0;
for (unsigned i = 0; i < infected.size(); i++) {
avg += area_of_infected(infected[i], ew_res, ns_res, suitable_cells);
}
avg /= infected.size();
string avg_string = "Average infected area: " + std::to_string(avg);
Rast_read_history(raster_name, "", &hist);
Rast_set_history(&hist, HIST_KEYWRD, avg_string.c_str());
Rast_write_history(raster_name, &hist);
}
inline Date treatment_date_from_string(const string& text)
{
try {
return Date(text);
}
catch (std::invalid_argument&) {
G_fatal_error(_("Date <%s> is invalid"), text.c_str());
}
}
inline void seasonality_from_option(Config& config, const Option* opt)
{
config.set_season_start_end_month(opt->answers[0], opt->answers[1]);
}
unsigned int get_num_answers(struct Option* opt)
{
unsigned int i = 0;
if (opt->answers)
for (i = 0; opt->answers[i]; i++)
;
return i;
}
/** Read list of names from a file
*
* Assumes one name per line. The current implementation basically splits the file by
* lines and returns the resulting list of strings.
*/
std::vector<string> read_names(const char* filename)
{
std::vector<string> names;
std::ifstream file(filename);
string line;
while (std::getline(file, line)) {
names.push_back(line);
}
return names;
}
/** From a file option representing a raster series, create series of rasters.
*
* Calls fatal error if the input is not correct. It will read all rasters if there is
* more than needed.
*/
std::vector<DImg> file_to_series(struct Option* option, int expected)
{
file_exists_or_fatal_error(option);
auto names{read_names(option->answer)};
if (names.size() < size_t(expected))
G_fatal_error(
_("Not enough names for %s in '%s' (%d expected)"),
option->key,
option->answer,
expected);
std::vector<DImg> rasters;
for (const auto& name : names) {
rasters.push_back(raster_from_grass_float(name));
}
return rasters;
}
/*!
* Warns about depreciated option value
*
* It uses the answer member. If the answer is not set,
* nothing is tested.
*
* \param opt Pointer to a valid option structure
* \param depreciated Value which is depreciated
* \param current Value which should be used instead
*/
void warn_about_depreciated_option_value(
const Option* opt, const string& depreciated, const string& current)
{
if (opt->answer && opt->answer == depreciated) {
G_warning(
_("The value <%s> for option %s is depreciated."
" Use value <%s> instead."),
opt->answer,
opt->key,
current.c_str());
}
}
std::vector<double> weather_file_to_list(const string& filename)
{
std::ifstream input(filename);
std::vector<double> output;
string line;
while (std::getline(input, line)) {
double m, c;
std::istringstream stream(line);
stream >> m >> c;
output.push_back(m * c);
}
return output;
}
/** Checks if there are any susceptible hosts left */
bool all_infected(Img& susceptible)
{
for (Img::IndexType j = 0; j < susceptible.rows(); j++)
for (Img::IndexType k = 0; k < susceptible.cols(); k++)
if (susceptible(j, k) > 0)
return false;
return true;
}
struct PoPSOptions
{
struct Option *host, *total_plants, *infected, *outside_spores;
struct Option* model_type;
struct Option* latency_period;
struct Option* dispersers_to_soils;
struct Option* soil_survival_steps;
struct Option *moisture_coefficient_file, *temperature_coefficient_file;
struct Option* weather_coefficient_file;
struct Option* weather_coefficient_stddev_file;
struct Option* survival_rate_month;
struct Option* survival_rate_day;
struct Option* survival_rate_file;
struct Option *lethal_temperature, *lethal_temperature_months;
struct Option* temperature_file;
struct Option *start_date, *end_date, *seasonality;
struct Option *step_unit, *step_num_units;
struct Option* treatments;
struct Option *treatment_date, *treatment_length;
struct Option* treatment_app;
struct Option* reproductive_rate;
struct Option *natural_kernel, *natural_scale;
struct Option *natural_direction, *natural_kappa;
struct Option *anthro_kernel, *anthro_scale;
struct Option *anthro_direction, *anthro_kappa;
struct Option* percent_natural_dispersal;
struct Option *infected_to_dead_rate, *first_year_to_die;
struct Option *mortality_frequency, *mortality_frequency_n;
struct Option* dead_series;
struct Option* seed;
struct Option* seeds;
struct Option* runs;
struct Option* threads;
struct Option* single_series;
struct Option *average, *average_series;
struct Option *stddev, *stddev_series;
struct Option *probability, *probability_series;
struct Option* spread_rate_output;
struct Option *quarantine, *quarantine_output, *quarantine_directions;
struct Option *dispersers_output, *established_dispersers_output;
struct Option *output_frequency, *output_frequency_n;
};
struct PoPSFlags
{
struct Flag* mortality;
struct Flag* generate_seed;
};
int main(int argc, char* argv[])
{
PoPSOptions opt;
PoPSFlags flg;
G_gisinit(argv[0]);
struct GModule* module = G_define_module();
G_add_keyword(_("raster"));
G_add_keyword(_("spread"));
G_add_keyword(_("model"));
G_add_keyword(_("simulation"));
G_add_keyword(_("disease"));
G_add_keyword(_("pest"));
module->description =
_("A dynamic species distribution model for pest or "
"pathogen spread in forest or agricultural ecosystems (PoPS)");
opt.host = G_define_standard_option(G_OPT_R_INPUT);
opt.host->key = "host";
opt.host->label = _("Input host raster map");
opt.host->description = _("Number of hosts per cell.");
opt.host->guisection = _("Input");
opt.total_plants = G_define_standard_option(G_OPT_R_INPUT);
opt.total_plants->key = "total_plants";
opt.total_plants->label = _("Input raster map of total plants");
opt.total_plants->description = _("Number of all plants per cell");
opt.total_plants->guisection = _("Input");
opt.infected = G_define_standard_option(G_OPT_R_INPUT);
opt.infected->key = "infected";
opt.infected->label = _("Input raster map of initial infection");
opt.infected->description = _("Number of infected hosts per cell");
opt.infected->guisection = _("Input");
opt.average = G_define_standard_option(G_OPT_R_OUTPUT);
opt.average->key = "average";
opt.average->description = _("Average infected across multiple runs");
opt.average->guisection = _("Output");
opt.average->required = NO;
opt.average_series = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
opt.average_series->key = "average_series";
opt.average_series->description =
_("Basename for output series of average infected across multiple runs");
opt.average_series->required = NO;
opt.average_series->guisection = _("Output");
opt.single_series = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
opt.single_series->key = "single_series";
opt.single_series->description =
_("Basename for output series of infected as single stochastic run");
opt.single_series->required = NO;
opt.single_series->guisection = _("Output");
opt.stddev = G_define_standard_option(G_OPT_R_OUTPUT);
opt.stddev->key = "stddev";
opt.stddev->description = _("Standard deviations");
opt.stddev->required = NO;
opt.stddev->guisection = _("Output");
opt.stddev_series = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
opt.stddev_series->key = "stddev_series";
opt.stddev_series->description =
_("Basename for output series of standard deviations");
opt.stddev_series->required = NO;
opt.stddev_series->guisection = _("Output");
opt.probability = G_define_standard_option(G_OPT_R_OUTPUT);
opt.probability->key = "probability";
opt.probability->description = _("Infection probability (in percent)");
opt.probability->required = NO;
opt.probability->guisection = _("Output");
opt.probability_series = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
opt.probability_series->key = "probability_series";
opt.probability_series->description =
_("Basename for output series of probabilities");
opt.probability_series->required = NO;
opt.probability_series->guisection = _("Output");
opt.outside_spores = G_define_standard_option(G_OPT_V_OUTPUT);
opt.outside_spores->key = "outside_spores";
opt.outside_spores->description =
_("Output vector map of spores or pest units outside of modeled area");
opt.outside_spores->required = NO;
opt.outside_spores->guisection = _("Output");
opt.spread_rate_output = G_define_standard_option(G_OPT_F_OUTPUT);
opt.spread_rate_output->key = "spread_rate_output";
opt.spread_rate_output->description =
_("Output CSV file containg yearly spread rate in N, S, E, W directions");
opt.spread_rate_output->required = NO;
opt.spread_rate_output->guisection = _("Output");
opt.quarantine = G_define_standard_option(G_OPT_R_INPUT);
opt.quarantine->key = "quarantine";
opt.quarantine->required = NO;
opt.quarantine->label = _("Input raster map of quarantine areas");
opt.quarantine->description = _("Values > 0 represent quarantine areas");
opt.quarantine->guisection = _("Input");
opt.quarantine_output = G_define_standard_option(G_OPT_F_OUTPUT);
opt.quarantine_output->key = "quarantine_output";
opt.quarantine_output->description =
_("Output CSV file containg yearly quarantine information");
opt.quarantine_output->required = NO;
opt.quarantine_output->guisection = _("Output");
opt.quarantine_directions = G_define_option();
opt.quarantine_directions->type = TYPE_STRING;
opt.quarantine_directions->key = "quarantine_directions";
opt.quarantine_directions->label = _("Quarantine directions to consider");
opt.quarantine_directions->description =
_("Comma separated directions to include"
"in the quarantine direction analysis, e.g., 'N,E' "
"(by default all directions (N, S, E, W) are considered)");
opt.quarantine_directions->required = NO;
opt.quarantine_directions->guisection = _("Output");
opt.model_type = G_define_option();
opt.model_type->type = TYPE_STRING;
opt.model_type->key = "model_type";
opt.model_type->label = _("Epidemiological model type");
opt.model_type->answer = const_cast<char*>("SI");
opt.model_type->options = "SI,SEI";
opt.model_type->descriptions =
_("SI;Susceptible-infected epidemiological model;"
"SEI;Susceptible-exposed-infected epidemiological model"
" (uses latency_period)");
opt.model_type->required = YES;
opt.model_type->guisection = _("Model");
opt.latency_period = G_define_option();
opt.latency_period->type = TYPE_INTEGER;
opt.latency_period->key = "latency_period";
opt.latency_period->label = _("Latency period in simulation steps");
opt.latency_period->description =
_("How long it takes for a hosts to become infected after being exposed"
" (unit is a simulation step)");
opt.latency_period->required = NO;
opt.latency_period->guisection = _("Model");
opt.dispersers_to_soils = G_define_option();
opt.dispersers_to_soils->type = TYPE_DOUBLE;
opt.dispersers_to_soils->key = "dispersers_to_soils";
opt.dispersers_to_soils->label = _("Ratio of dispersers going into soil");
opt.dispersers_to_soils->description =
_("Ratio (percentage) of generated dispersers going into soil instead "
"of being dispersed by the kernel");
opt.dispersers_to_soils->options = "0-1";
opt.dispersers_to_soils->required = NO;
opt.dispersers_to_soils->guisection = _("Model");
opt.soil_survival_steps = G_define_option();
opt.soil_survival_steps->type = TYPE_INTEGER;
opt.soil_survival_steps->key = "soil_survival_steps";
opt.soil_survival_steps->label = _("Steps dispersers stay in the soil");
opt.soil_survival_steps->description =
_("Number of simulation steps dispersers survive in the soil");
opt.soil_survival_steps->options = "1-";
opt.soil_survival_steps->required = NO;
opt.soil_survival_steps->guisection = _("Model");
opt.treatments = G_define_standard_option(G_OPT_R_INPUT);
opt.treatments->key = "treatments";
opt.treatments->multiple = YES;
opt.treatments->description =
_("Raster map(s) of treatments (treated 1, otherwise 0)");
opt.treatments->required = NO;
opt.treatments->guisection = _("Treatments");
opt.treatment_date = G_define_option();
opt.treatment_date->key = "treatment_date";
opt.treatment_date->type = TYPE_STRING;
opt.treatment_date->multiple = YES;
opt.treatment_date->description =
_("Dates when treatments are applied (e.g. 2020-01-15)");
opt.treatment_date->required = NO;
opt.treatment_date->guisection = _("Treatments");
opt.treatment_length = G_define_option();
opt.treatment_length->type = TYPE_INTEGER;
opt.treatment_length->key = "treatment_length";
opt.treatment_length->multiple = YES;
opt.treatment_length->label = _("Treatment length in days");
opt.treatment_length->description =
_("Treatment length 0 results in simple removal of host, length > 0 makes"
" host resistant for certain number of days");
opt.treatment_length->required = NO;
opt.treatment_length->guisection = _("Treatments");
opt.treatment_app = G_define_option();
opt.treatment_app->key = "treatment_application";
opt.treatment_app->type = TYPE_STRING;
opt.treatment_app->multiple = NO;
opt.treatment_app->description = _("Type of treatmet application");
opt.treatment_app->options = "ratio_to_all,all_infected_in_cell";
opt.treatment_app->required = NO;
opt.treatment_app->answer = const_cast<char*>("ratio_to_all");
opt.treatment_app->guisection = _("Treatments");
opt.moisture_coefficient_file = G_define_standard_option(G_OPT_F_INPUT);
opt.moisture_coefficient_file->key = "moisture_coefficient_file";
opt.moisture_coefficient_file->label =
_("Input file with one moisture coefficient map name per line");
opt.moisture_coefficient_file->description = _("Moisture coefficient");
opt.moisture_coefficient_file->required = NO;
opt.moisture_coefficient_file->guisection = _("Weather");
opt.temperature_coefficient_file = G_define_standard_option(G_OPT_F_INPUT);
opt.temperature_coefficient_file->key = "temperature_coefficient_file";
opt.temperature_coefficient_file->label =
_("Input file with one temperature coefficient map name per line");
opt.temperature_coefficient_file->description = _("Temperature coefficient");
opt.temperature_coefficient_file->required = NO;
opt.temperature_coefficient_file->guisection = _("Weather");
opt.weather_coefficient_file = G_define_standard_option(G_OPT_F_INPUT);
opt.weather_coefficient_file->key = "weather_coefficient_file";
opt.weather_coefficient_file->label =
_("Weather coefficients or weather coefficients mean values");
opt.weather_coefficient_file->description =
_("Input file with one weather coefficient map name per line");
opt.weather_coefficient_file->required = NO;
opt.weather_coefficient_file->guisection = _("Weather");
opt.weather_coefficient_stddev_file = G_define_standard_option(G_OPT_F_INPUT);
opt.weather_coefficient_stddev_file->key = "weather_coefficient_stddev_file";
opt.weather_coefficient_stddev_file->label =
_("Standard deviation of weather coefficient");
opt.weather_coefficient_stddev_file->description =
_("Input file with one standard deviation map name per line");
opt.weather_coefficient_stddev_file->required = NO;
opt.weather_coefficient_stddev_file->guisection = _("Weather");
// Survival rate
opt.survival_rate_file = G_define_standard_option(G_OPT_F_INPUT);
opt.survival_rate_file->key = "survival_rate";
opt.survival_rate_file->label =
_("Input file with one survival rate raster map name per line");
opt.survival_rate_file->description = _("Suvival rate is percentage (0-1)");
opt.survival_rate_file->required = NO;
opt.survival_rate_file->guisection = _("Weather");
opt.survival_rate_month = G_define_option();
opt.survival_rate_month->type = TYPE_INTEGER;
opt.survival_rate_month->key = "survival_month";
opt.survival_rate_month->label =
_("Month when the pest or pathogen dies with given rate");
opt.survival_rate_month->description =
_("The survival rate is applied at the selected month and day");
opt.survival_rate_month->required = NO;
opt.survival_rate_month->guisection = _("Weather");
opt.survival_rate_day = G_define_option();
opt.survival_rate_day->type = TYPE_INTEGER;
opt.survival_rate_day->key = "survival_day";
opt.survival_rate_day->label =
_("Day of selected month when the pest or pathogen dies with given rate");
opt.survival_rate_day->description =
_("The survival rate is applied at the selected month and day");
opt.survival_rate_day->required = NO;
opt.survival_rate_day->guisection = _("Weather");
// Temperature and lethal temperature
opt.lethal_temperature = G_define_option();
opt.lethal_temperature->type = TYPE_DOUBLE;
opt.lethal_temperature->key = "lethal_temperature";
opt.lethal_temperature->label = _("Temperature at which the pest or pathogen dies");
opt.lethal_temperature->description =
_("The temerature unit must be the same as for the"
"temerature raster map (typically degrees of Celsius)");
opt.lethal_temperature->required = NO;
opt.lethal_temperature->multiple = NO;
opt.lethal_temperature->guisection = _("Weather");
opt.lethal_temperature_months = G_define_option();
opt.lethal_temperature_months->type = TYPE_INTEGER;
opt.lethal_temperature_months->key = "lethal_month";
opt.lethal_temperature_months->label =
_("Month when the pest or pathogen dies due to low temperature");
opt.lethal_temperature_months->description =
_("The temperature unit must be the same as for the"
"temperature raster map (typically degrees of Celsius)");
// TODO: implement this as multiple
opt.lethal_temperature_months->required = NO;
opt.lethal_temperature_months->guisection = _("Weather");
// TODO: rename coefs in interface and improve their descs
opt.temperature_file = G_define_standard_option(G_OPT_F_INPUT);
opt.temperature_file->key = "temperature_file";
opt.temperature_file->label =
_("Input file with one temperature raster map name per line");
opt.temperature_file->description = _(
"The temperature should be in actual temperature units (typically degrees of Celsius)");
opt.temperature_file->required = NO;
opt.temperature_file->guisection = _("Weather");
opt.start_date = G_define_option();
opt.start_date->type = TYPE_STRING;
opt.start_date->key = "start_date";
opt.start_date->description =
_("Start date of the simulation in YYYY-MM-DD format");
opt.start_date->required = YES;
opt.start_date->guisection = _("Time");
opt.end_date = G_define_option();
opt.end_date->type = TYPE_STRING;
opt.end_date->key = "end_date";
opt.end_date->description = _("End date of the simulation in YYYY-MM-DD format");
opt.end_date->required = YES;
opt.end_date->guisection = _("Time");
opt.seasonality = G_define_option();
opt.seasonality->type = TYPE_STRING;
opt.seasonality->key = "seasonality";
opt.seasonality->label = _("Seasonal spread (from,to)");
opt.seasonality->description =
_("Spread limited to certain months (season), for example"
" 5,9 for spread starting at the beginning of May and"
" ending at the end of September");
opt.seasonality->key_desc = "from,to";
// opt.seasonality->options = "1-12";
opt.seasonality->answer = const_cast<char*>("1,12");
opt.seasonality->required = YES;
opt.seasonality->multiple = NO;
opt.seasonality->guisection = _("Time");
opt.step_unit = G_define_option();
opt.step_unit->type = TYPE_STRING;
opt.step_unit->key = "step_unit";
opt.step_unit->label = _("Unit of simulation steps");
opt.step_unit->options = "day,week,month";
opt.step_unit->answer = const_cast<char*>("month");
opt.step_unit->descriptions = _(
"day;Compute next simulation step every N days;week;Compute next simulation step every N weeks;month;Compute next simulation step every N months");
opt.step_unit->required = YES;
opt.step_unit->guisection = _("Time");
opt.step_num_units = G_define_option();
opt.step_num_units->type = TYPE_INTEGER;
opt.step_num_units->key = "step_num_units";
opt.step_num_units->answer = const_cast<char*>("1");
opt.step_num_units->label = _("Number of days/weeks/months in each step");
opt.step_num_units->description = _(
"Step is given by number and unit, e.g. step_num_units=5 and step_unit=day means step is 5 days");
opt.step_num_units->options = "1-";
opt.step_num_units->required = YES;
opt.step_num_units->guisection = _("Time");
opt.output_frequency = G_define_option();
opt.output_frequency->type = TYPE_STRING;
opt.output_frequency->key = "output_frequency";
opt.output_frequency->label = "Frequency of simulation output";
opt.output_frequency->options = "yearly,monthly,weekly,daily,every_n_steps";
opt.output_frequency->required = NO;
opt.output_frequency->answer = const_cast<char*>("yearly");
opt.output_frequency->guisection = _("Time");
opt.output_frequency_n = G_define_option();
opt.output_frequency_n->type = TYPE_INTEGER;
opt.output_frequency_n->key = "output_frequency_n";
opt.output_frequency_n->description = "Output frequency every N steps";
opt.output_frequency_n->options = "1-";
opt.output_frequency_n->answer = const_cast<char*>("1");
opt.output_frequency_n->required = NO;
opt.output_frequency_n->guisection = _("Time");
opt.reproductive_rate = G_define_option();
opt.reproductive_rate->type = TYPE_DOUBLE;
opt.reproductive_rate->key = "reproductive_rate";
opt.reproductive_rate->label =
_("Number of spores or pest units produced by a single host");
opt.reproductive_rate->description = _(
"Number of spores or pest units produced by a single host under optimal weather conditions");
opt.reproductive_rate->answer = const_cast<char*>("4.4");
opt.reproductive_rate->guisection = _("Dispersal");
opt.natural_kernel = G_define_option();
opt.natural_kernel->type = TYPE_STRING;
opt.natural_kernel->key = "natural_dispersal_kernel";
opt.natural_kernel->label = _("Natural dispersal kernel type");
opt.natural_kernel->answer = const_cast<char*>("cauchy");
opt.natural_kernel->options = "cauchy,exponential";
opt.natural_kernel->required = YES;
opt.natural_kernel->guisection = _("Dispersal");
opt.natural_scale = G_define_option();
opt.natural_scale->type = TYPE_DOUBLE;
opt.natural_scale->key = "natural_distance";
opt.natural_scale->label = _("Distance parameter for natural dispersal kernel");
opt.natural_scale->required = YES;
opt.natural_scale->guisection = _("Dispersal");
opt.natural_direction = G_define_option();
opt.natural_direction->type = TYPE_STRING;
opt.natural_direction->key = "natural_direction";
opt.natural_direction->label = _("Direction of natural dispersal kernel");
opt.natural_direction->description =
_("Typically prevailing wind direction;"
" none means that there is no directionality or no wind");
opt.natural_direction->options = "N,NE,E,SE,S,SW,W,NW,NONE,none";
opt.natural_direction->required = YES;
opt.natural_direction->answer = const_cast<char*>("none");
opt.natural_direction->guisection = _("Dispersal");
opt.natural_kappa = G_define_option();
opt.natural_kappa->type = TYPE_DOUBLE;
opt.natural_kappa->key = "natural_direction_strength";
opt.natural_kappa->label = _("Strength of direction of natural dispersal kernel");
opt.natural_kappa->description =
_("The kappa parameter of von Mises distribution"
" (concentration);"
" typically the strength of the wind direction");
opt.natural_kappa->required = NO;
opt.natural_kappa->guisection = _("Dispersal");
opt.anthro_kernel = G_define_option();
opt.anthro_kernel->type = TYPE_STRING;
opt.anthro_kernel->key = "anthropogenic_dispersal_kernel";
opt.anthro_kernel->label = _("Anthropogenic dispersal kernel type");
opt.anthro_kernel->options = "cauchy,exponential";
opt.anthro_kernel->guisection = _("Dispersal");
opt.anthro_scale = G_define_option();
opt.anthro_scale->type = TYPE_DOUBLE;
opt.anthro_scale->key = "anthropogenic_distance";
opt.anthro_scale->label =
_("Distance parameter for anthropogenic dispersal kernel");
opt.anthro_scale->guisection = _("Dispersal");
opt.anthro_direction = G_define_option();
opt.anthro_direction->type = TYPE_STRING;
opt.anthro_direction->key = "anthropogenic_direction";
opt.anthro_direction->label = _("Direction of anthropogenic dispersal kernel");
opt.anthro_direction->description =
_("Value none means that there is no directionality");
opt.anthro_direction->options = "N,NE,E,SE,S,SW,W,NW,NONE,none";
opt.anthro_direction->required = NO;
opt.anthro_direction->answer = const_cast<char*>("none");
opt.anthro_direction->guisection = _("Dispersal");
opt.anthro_kappa = G_define_option();
opt.anthro_kappa->type = TYPE_DOUBLE;
opt.anthro_kappa->key = "anthropogenic_direction_strength";
opt.anthro_kappa->label =
_("Strength of direction of anthropogenic dispersal kernel");
opt.anthro_kappa->description =
_("The kappa parameter of von Mises distribution"
" (concentration);"
" typically the strength of the wind direction");
opt.anthro_kappa->guisection = _("Dispersal");
opt.percent_natural_dispersal = G_define_option();
opt.percent_natural_dispersal->type = TYPE_DOUBLE;
opt.percent_natural_dispersal->key = "percent_natural_dispersal";
opt.percent_natural_dispersal->label = _("Percentage of natural dispersal");
opt.percent_natural_dispersal->description =
_("How often is the natural dispersal kernel used versus"
" the anthropogenic dispersal kernel");
opt.percent_natural_dispersal->options = "0-1";
opt.percent_natural_dispersal->guisection = _("Dispersal");
opt.dispersers_output = G_define_standard_option(G_OPT_R_OUTPUT);
opt.dispersers_output->key = "dispersers_output";
opt.dispersers_output->label = _("Output raster of disperses");
opt.dispersers_output->description =
_("Dispersers are accumulated over all steps and stochastic runs");
opt.dispersers_output->required = NO;
opt.dispersers_output->guisection = _("Output");
opt.established_dispersers_output = G_define_standard_option(G_OPT_R_OUTPUT);
opt.established_dispersers_output->key = "established_dispersers_output";
opt.established_dispersers_output->label =
_("Output raster of established disperses");
opt.established_dispersers_output->description =
_("Dispersers are accumulated over all steps and stochastic runs");
opt.established_dispersers_output->required = NO;
opt.established_dispersers_output->guisection = _("Output");
opt.infected_to_dead_rate = G_define_option();
opt.infected_to_dead_rate->type = TYPE_DOUBLE;
opt.infected_to_dead_rate->key = "mortality_rate";
opt.infected_to_dead_rate->label = _("Mortality rate of infected hosts");
opt.infected_to_dead_rate->description =
_("Percentage of infected hosts that die in a given year"
" (hosts are removed from the infected pool)");
opt.infected_to_dead_rate->options = "0-1";
opt.infected_to_dead_rate->guisection = _("Mortality");
opt.first_year_to_die = G_define_option();
opt.first_year_to_die->type = TYPE_INTEGER;
opt.first_year_to_die->key = "mortality_time_lag";
opt.first_year_to_die->label =
_("Time lag from infection until mortality can occur in years");
opt.first_year_to_die->description =
_("How many years it takes for an infected host to start dying"
" (value 0 for hosts dying at the end of the first year)");
opt.first_year_to_die->guisection = _("Mortality");
opt.dead_series = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
opt.dead_series->key = "mortality_series";
opt.dead_series->label = _("Basename for series of number of dead hosts");
opt.dead_series->description =
_("Basename for output series of number of dead hosts"
" (requires mortality to be activated)");
opt.dead_series->required = NO;
opt.dead_series->guisection = _("Mortality");
opt.mortality_frequency = G_define_option();
opt.mortality_frequency->type = TYPE_STRING;
opt.mortality_frequency->key = "mortality_frequency";
opt.mortality_frequency->description = _("Mortality frequency");
opt.mortality_frequency->options = "yearly,monthly,weekly,daily,every_n_steps";
opt.mortality_frequency->required = NO;
opt.mortality_frequency->guisection = _("Mortality");
opt.mortality_frequency_n = G_define_option();
opt.mortality_frequency_n->type = TYPE_INTEGER;
opt.mortality_frequency_n->key = "mortality_frequency_n";
opt.mortality_frequency_n->description = _("Mortality frequency every N steps");
opt.mortality_frequency_n->options = "1-";
opt.mortality_frequency_n->required = NO;
opt.mortality_frequency_n->guisection = _("Mortality");
flg.mortality = G_define_flag();
flg.mortality->key = 'm';
flg.mortality->label = _("Apply mortality");
flg.mortality->description =
_("After certain number of years, start removing dead hosts"
" from the infected pool with a given rate");
flg.mortality->guisection = _("Mortality");
opt.seed = G_define_option();
opt.seed->key = "random_seed";
opt.seed->type = TYPE_INTEGER;
opt.seed->required = NO;
opt.seed->label = _("Seed for random number generator");
opt.seed->description =
_("The same seed can be used to obtain same results"
" or random seed can be generated by other means.");
opt.seed->guisection = _("Randomness");
opt.seeds = G_define_option();
opt.seeds->key = "random_seeds";
opt.seeds->key_desc = "name=value";
opt.seeds->type = TYPE_STRING;
opt.seeds->required = NO;
opt.seeds->multiple = YES;
opt.seeds->label = _("Seeds for isolated random number generators");
opt.seeds->description = _(
"Multiple seeds for separate processes as a list of pairs key=value (comma-separated). "
"Seeds must be provided for: disperser_generation,natural_dispersal,"
"anthropogenic_dispersal,establishment,weather,movement,"
"overpopulation,survival_rate,soil");
opt.seeds->guisection = _("Randomness");
flg.generate_seed = G_define_flag();
flg.generate_seed->key = 's';
flg.generate_seed->label = _("Generate random seed (result is non-deterministic)");
flg.generate_seed->description =
_("Automatically generates random seed for random number"
" generator (use when you don't want to provide the seed option)");
flg.generate_seed->guisection = _("Randomness");
opt.runs = G_define_option();
opt.runs->key = "runs";
opt.runs->type = TYPE_INTEGER;
opt.runs->required = NO;
opt.runs->label = _("Number of simulation runs");
opt.runs->description =
_("The individual runs will obtain different seeds"
" and will be averaged for the output");
opt.runs->options = "1-";
opt.runs->guisection = _("Randomness");
opt.threads = G_define_option();
opt.threads->key = "nprocs";
opt.threads->type = TYPE_INTEGER;
opt.threads->required = NO;
opt.threads->description = _("Number of threads for parallel computing");
opt.threads->options = "1-";
opt.threads->guisection = _("Randomness");
G_option_required(
opt.average,
opt.average_series,
opt.single_series,
opt.probability,
opt.probability_series,
opt.outside_spores,
opt.stddev,
opt.stddev_series,
NULL);
G_option_requires_all(opt.average_series, opt.output_frequency, NULL);
G_option_requires_all(opt.single_series, opt.output_frequency, NULL);
G_option_requires_all(opt.probability_series, opt.output_frequency, NULL);
G_option_requires_all(opt.stddev_series, opt.output_frequency, NULL);
G_option_exclusive(opt.seed, opt.seeds, flg.generate_seed, NULL);
G_option_required(opt.seed, opt.seeds, flg.generate_seed, NULL);
// weather
G_option_collective(
opt.moisture_coefficient_file, opt.temperature_coefficient_file, NULL);
G_option_exclusive(
opt.moisture_coefficient_file, opt.weather_coefficient_file, NULL);
G_option_exclusive(
opt.temperature_coefficient_file, opt.weather_coefficient_file, NULL);
G_option_requires_all(
opt.weather_coefficient_stddev_file, opt.weather_coefficient_file, NULL);
// mortality
// With flag, the lag, rate, and frequency are always required.
// For simplicity of the code, mortality outputs are allowed only with the
// corresponding main outputs for single run.
G_option_collective(
flg.mortality,
opt.first_year_to_die,
opt.infected_to_dead_rate,
opt.mortality_frequency,
NULL);
G_option_requires_all(opt.dead_series, flg.mortality, opt.single_series, NULL);
G_option_requires_all(opt.mortality_frequency_n, opt.mortality_frequency, NULL);
// TODO: requires_all does not understand the default?
// treatment_app needs to be removed from here and check separately
G_option_requires_all(
opt.treatments,
opt.treatment_length,
opt.treatment_date,
opt.treatment_app,
NULL);
// lethal temperature options
G_option_collective(
opt.lethal_temperature,
opt.lethal_temperature_months,
opt.temperature_file,
NULL);
G_option_collective(opt.soil_survival_steps, opt.dispersers_to_soils, NULL);
G_option_collective(
opt.survival_rate_file, opt.survival_rate_month, opt.survival_rate_day, NULL);
G_option_collective(opt.quarantine, opt.quarantine_output, NULL);
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
unsigned num_runs = 1;
if (opt.runs->answer)
num_runs = std::stoul(opt.runs->answer);
unsigned threads = 1;
if (opt.threads->answer)
threads = std::stoul(opt.threads->answer);
// check for file existence
file_exists_or_fatal_error(opt.moisture_coefficient_file);
file_exists_or_fatal_error(opt.temperature_coefficient_file);
file_exists_or_fatal_error(opt.weather_coefficient_file);
file_exists_or_fatal_error(opt.weather_coefficient_stddev_file);
// Start creating the configuration.
Config config;
// model type
config.model_type = opt.model_type->answer;
ModelType model_type = model_type_from_string(config.model_type);
if (model_type == ModelType::SusceptibleExposedInfected
&& !opt.latency_period->answer) {
G_fatal_error(
_("The option %s is required for %s=%s"),
opt.latency_period->key,
opt.model_type->key,
opt.model_type->answer);
}
// get current computational region (for rows, cols and resolution)
struct Cell_head window;
G_get_window(&window);
config.rows = window.rows;
config.cols = window.cols;
config.ew_res = window.ew_res;
config.ns_res = window.ns_res;
// Seasonality: Do you want the spread to be limited to certain months?
if (!opt.seasonality->answer || opt.seasonality->answer[0] == '\0')
G_fatal_error(_("The option %s cannot be empty"), opt.seasonality->key);
seasonality_from_option(config, opt.seasonality);
// set the spore rate
config.reproductive_rate = std::stod(opt.reproductive_rate->answer);
// TODO: how to support DispersalKernelType::None for natural_kernel?
// perhaps the long-short should take the type instead of bool,
// then T is anything else than None
// TODO: should all kernels support None?
config.natural_kernel_type = opt.natural_kernel->answer;
config.natural_scale = std::stod(opt.natural_scale->answer);
config.natural_direction = opt.natural_direction->answer;
config.natural_kappa = 0;
if (direction_from_string(config.natural_direction) != Direction::None
&& !opt.natural_kappa->answer)
fatal_option_required_for_value(opt.natural_kappa, opt.natural_direction);
else if (opt.natural_kappa->answer)
config.natural_kappa = std::stod(opt.natural_kappa->answer);
config.use_anthropogenic_kernel = false;
if (opt.anthro_kernel->answer)
config.anthro_kernel_type = opt.anthro_kernel->answer;
if (kernel_type_from_string(config.anthro_kernel_type) != DispersalKernelType::None)
config.use_anthropogenic_kernel = true;
config.anthro_scale = 1;
if (config.use_anthropogenic_kernel && !opt.anthro_scale->answer)
fatal_option_required_for_value(opt.anthro_scale, opt.anthro_kernel);
else if (opt.anthro_scale->answer)
config.anthro_scale = std::stod(opt.anthro_scale->answer);
// we allow none and an empty string
config.anthro_direction = opt.anthro_direction->answer;
config.anthro_kappa = 0;