ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
Maize.cpp
Go to the documentation of this file.
1 //
2 // Maize.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, Christopher John Topping, University of Aarhus
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without modification, are permitted provided
10 that the following conditions are met:
11 
12 Redistributions of source code must retain the above copyright notice, this list of conditions and the
13 following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
15 the following disclaimer in the documentation and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
20 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ********************************************************************************************************
26 */
27 
28 #include "../../Landscape/ls.h"
29 #include "../../Landscape/cropprogs/Maize.h"
30 
31 extern CfgFloat cfg_ins_app_prop1;
32 extern CfgFloat cfg_herbi_app_prop;
33 extern CfgFloat cfg_fungi_app_prop1;
34 extern CfgFloat cfg_greg_app_prop;
35 
36 bool Maize::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
37 {
38  m_farm = a_farm;
39  m_field = a_field;
40  m_ev = a_ev;
41  int d1=0;
42 
43  bool done = false;
44  switch ( m_ev->m_todo ) {
45  case ma_start:
46  {
47  MAIZE_SOW_DATE = 0;
49  // Set up the date management stuff
50  m_last_date=g_date->DayInYear(30,11);
51  // Start and stop dates for all events after harvest
52  int noDates= 2;
53  m_field->SetMDates(0,0,g_date->DayInYear(25,9));
54  // 0,0 determined by harvest date - used to see if at all possible
55  m_field->SetMDates(1,0,g_date->DayInYear(10,11));
56  m_field->SetMDates(0,1,g_date->DayInYear(15,10));
57  m_field->SetMDates(1,1,g_date->DayInYear(30,11));
58  // Check the next crop for early start, unless it is a spring crop
59  // in which case we ASSUME that no checking is necessary!!!!
60  // So DO NOT implement a crop that runs over the year boundary
61 
62  //new if: do the check only for non-optimising farms and if year>0. (030713 - m_rotation used only in the hidden year, so I modified the condition from >7 to >0)
63  if(!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber()>0)){
64 
65  if (m_ev->m_startday>g_date->DayInYear(1,7))
66  {
67  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
68  {
69  g_msg->Warn( WARN_BUG, "Maize::Do(): "
70  "Harvest too late for the next crop to start!!!", "" );
71  exit( 1 );
72  }
73  // Now fix any late finishing problems
74  for (int i=0; i<noDates; i++)
75  {
76  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
77  m_field->SetMDates(0,i,m_ev->m_startday-1);
78  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
79  m_field->SetMDates(1,i,m_ev->m_startday-1);
80  }
81  }
82  // Now no operations can be timed after the start of the next crop.
83 
84  if ( ! m_ev->m_first_year )
85  {
86  int today=g_date->Date();
87  // Are we before July 1st?
88  d1 = g_date->OldDays() + g_date->DayInYear( 1,7 );
89  if (today < d1)
90  {
91  // Yes, too early. We assumme this is because the last crop was late
92  g_msg->Warn( WARN_BUG, "Maize::Do(): "
93  "Crop start attempt between 1st Jan & 1st July", "" );
94  exit( 1 );
95  }
96  else
97  {
98  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
99  if (today > d1)
100  {
101  // Yes too late - should not happen - raise an error
102  g_msg->Warn( WARN_BUG, "Maize::Do(): "
103  "Crop start attempt after last possible start date", "" );
104  exit( 1 );
105  }
106  }
107  }
108  else
109  {
110  SimpleEvent( g_date->OldDays() + g_date->DayInYear(25,4),
111  ma_spring_plough, false );
112  break;
113  }
114 
115  }//if
116 
117 
118  // End single block date checking code. Please see next line
119  // comment as well.
120  // Reinit d1 to first possible starting date.
121  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
122  if ( g_date->Date() > d1 ) {
123  d1 = g_date->Date();
124  }
125  // OK, let's go.
126  if ( m_farm->DoIt( 50 ))
127  {
128  SimpleEvent( d1, ma_fa_manure_a, false );
129  }
130  else
131  {
132  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20, 3 ) + 365,
133  ma_fa_manure_b, false );
134  }
135  break;
136  }
137 
138  case ma_fa_manure_a:
139  if (!m_farm->FA_Manure( m_field, 0.0,
140  g_date->DayInYear( 30, 11 ) - g_date->DayInYear())) {
141  SimpleEvent( g_date->Date() + 1, ma_fa_manure_a, false );
142  break;
143  }
144  SimpleEvent( g_date->Date(), ma_autumn_plough, false );
145  break;
146 
147  case ma_autumn_plough:
148  if (!m_farm->AutumnPlough( m_field, 0.0,
149  g_date->DayInYear( 30, 11 ) - g_date->DayInYear())) {
150  SimpleEvent( g_date->Date() + 1, ma_autumn_plough, false );
151  break;
152  }
153  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 4 ) + 365,
154  ma_spring_harrow, false );
155  break;
156 
157  case ma_fa_manure_b:
158  if (!m_farm->FA_Manure( m_field, 0.0,
159  g_date->DayInYear( 25, 4 ) - g_date->DayInYear())) {
160  SimpleEvent( g_date->Date() + 1, ma_fa_manure_b, false );
161  break;
162  }
163  {
164  d1 = g_date->OldDays() + g_date->DayInYear( 1, 4 );
165  if ( g_date->Date()+1 > d1 ) {
166  d1 = g_date->Date()+1;
167  }
168  SimpleEvent( d1, ma_fa_slurry_one, false );
169  }
170  break;
171 
172  case ma_fa_slurry_one:
173  if (!m_farm->FA_Slurry( m_field, 0.0,
174  g_date->DayInYear( 30, 4 ) - g_date->DayInYear())) {
175  SimpleEvent( g_date->Date() + 1, ma_fa_slurry_one, false );
176  break;
177  }
178  SimpleEvent( g_date->Date() + 1, ma_spring_plough, false );
179  break;
180 
181  case ma_spring_plough:
182  if (!m_farm->SpringPlough( m_field, 0.0,
183  g_date->DayInYear( 1, 5 ) - g_date->DayInYear())) {
184  SimpleEvent( g_date->Date() + 1, ma_spring_plough, false );
185  break;
186  }
187  SimpleEvent( g_date->Date(), ma_spring_harrow, false );
188  break;
189 
190  case ma_spring_harrow:
191  if (!m_farm->SpringHarrow( m_field, 0.0,
192  g_date->DayInYear( 10, 5 ) - g_date->DayInYear())) {
193  SimpleEvent( g_date->Date() + 1, ma_spring_harrow, false );
194  break;
195  }
196  {
197  d1 = g_date->OldDays() + g_date->DayInYear( 25, 4 );
198  if ( g_date->Date() > d1 ) {
199  d1 = g_date->Date();
200  }
201  SimpleEvent( d1, ma_spring_sow, false );
202  }
203  break;
204 
205  case ma_spring_sow:
206  if (!m_farm->SpringSow( m_field, 0.0,
207  g_date->DayInYear( 15, 5 ) - g_date->DayInYear())) {
208  SimpleEvent( g_date->Date() + 1, ma_spring_sow, false );
209  break;
210  }
211  MAIZE_SOW_DATE = g_date->Date();
212  SimpleEvent( g_date->Date(), ma_fa_npk, false );
213  break;
214 
215  case ma_fa_npk:
216  if (!m_farm->FA_NPK( m_field, 0.0,
217  g_date->DayInYear( 20, 5 ) - g_date->DayInYear())) {
218  SimpleEvent( g_date->Date() + 1, ma_fa_npk, false );
219  break;
220  }
221  if ( m_farm->DoIt( 70 )) {
222  {
223  d1 = g_date->OldDays() + g_date->DayInYear( 1, 5 );
224  if ( MAIZE_SOW_DATE + 5 > d1 ) {
225  d1 = MAIZE_SOW_DATE + 5;
226  }
227  SimpleEvent( d1, ma_herbi_one, false );
228  }
229  } else {
230  {
231  d1 = g_date->OldDays() + g_date->DayInYear( 2, 5 );
232  if ( MAIZE_SOW_DATE + 7 > d1 ) {
233  d1 = MAIZE_SOW_DATE + 7;
234  }
235  SimpleEvent( d1, ma_row_one, false );
236  }
237  }
238  break;
239 
240  case ma_herbi_one:
241  if ( m_ev->m_lock || m_farm->DoIt( (int) (100*cfg_herbi_app_prop.value()* m_farm->Prob_multiplier()) )) { //modified probability
242 
243  //new - for decision making
244  TTypesOfVegetation tov = m_field->GetVegType();
245  if(!m_ev->m_lock && !m_farm->Spraying_herbicides(tov)){
246  Field * pf = dynamic_cast<Field*>(m_field);
247  pf->Add_missed_herb_app();
248  if(m_farm->DoIt(59)) pf->Add_missed_herb_app(); //the 2nd missed application
249  } //end of the part for dec. making
250  else{
251  if (!m_farm->HerbicideTreat( m_field, 0.0, g_date->DayInYear( 25, 5 ) - g_date->DayInYear())) {
252  SimpleEvent( g_date->Date() + 1, ma_herbi_one, false );
253  break;
254  }
255  }
256  }
257  MAIZE_HERBI_ONE_DATE = g_date->Date();
258  {
259  d1 = g_date->OldDays() + g_date->DayInYear( 1, 5 );
260  if ( MAIZE_SOW_DATE + 5 > d1 ) {
261  d1 = MAIZE_SOW_DATE + 5;
262  }
263  SimpleEvent( d1, ma_fa_slurry_two, false );
264  }
265  break;
266 
267  case ma_row_one:
268  if (!m_farm->RowCultivation( m_field, 0.0,
269  g_date->DayInYear( 25, 5 ) - g_date->DayInYear())) {
270  SimpleEvent( g_date->Date() + 1, ma_row_one, false );
271  break;
272  }
273  {
274  d1 = g_date->OldDays() + g_date->DayInYear( 1, 5 );
275  if ( MAIZE_SOW_DATE + 5 > d1 ) {
276  d1 = MAIZE_SOW_DATE + 5;
277  }
278  SimpleEvent( d1, ma_fa_slurry_two, false );
279  }
280  break;
281 
282  case ma_fa_slurry_two:
283  if (!m_farm->FA_Slurry( m_field, 0.0,
284  g_date->DayInYear( 25, 5 ) - g_date->DayInYear())) {
285  SimpleEvent( g_date->Date() + 1, ma_fa_slurry_two, false );
286  break;
287  }
288  if ( m_farm->DoIt( 86 )) {
289  {
290  d1 = g_date->OldDays() + g_date->DayInYear( 10, 5 );
291  if ( MAIZE_HERBI_ONE_DATE + 10 > d1 ) {
292  d1 = MAIZE_HERBI_ONE_DATE + 10;
293  }
294  SimpleEvent( d1, ma_herbi_two, false );
295  }
296  } else {
297  {
298  d1 = g_date->OldDays() + g_date->DayInYear( 21, 5 );
299  if ( g_date->Date() + 1 > d1 ) {
300  d1 = g_date->Date() + 1;
301  }
302  SimpleEvent( d1, ma_row_two, false );
303  }
304  }
305  break;
306 
307  case ma_herbi_two:
308  if ( m_ev->m_lock || m_farm->DoIt( (int) (59*cfg_herbi_app_prop.value() * m_farm->Prob_multiplier() ))) { //modified probability, was 100
309  if (!m_farm->HerbicideTreat( m_field, 0.0,
310  g_date->DayInYear( 5, 6 ) - g_date->DayInYear())) {
311  SimpleEvent( g_date->Date() + 1, ma_herbi_two, false );
312  break;
313  }
314  }
315  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 7 ),
316  ma_water_one, false );
317  break;
318 
319  case ma_row_two:
320  if (!m_farm->RowCultivation( m_field, 0.0,
321  g_date->DayInYear( 20, 6 ) - g_date->DayInYear())) {
322  SimpleEvent( g_date->Date() + 1, ma_row_two, false );
323  break;
324  }
325  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 7 ),
326  ma_water_one, false );
327  break;
328 
329  case ma_water_one:
330  if ( m_ev->m_lock || m_farm->DoIt( 30 )) {
331  if (!m_farm->Water( m_field, 0.0,
332  g_date->DayInYear( 15, 7 ) - g_date->DayInYear())) {
333  SimpleEvent( g_date->Date() + 1, ma_water_one, false );
334  break;
335  }
336  }
337  {
338  d1 = g_date->OldDays() + g_date->DayInYear( 16, 7 );
339  if ( g_date->Date() + 7 > d1 ) {
340  d1 = g_date->Date() + 7;
341  }
342  SimpleEvent( d1, ma_water_two, false );
343  }
344  break;
345 
346  case ma_water_two:
347  if (!m_farm->Water( m_field, 0.0,
348  g_date->DayInYear( 30, 7 ) - g_date->DayInYear())) {
349  SimpleEvent( g_date->Date() + 1, ma_water_two, false );
350  break;
351  }
352  ChooseNextCrop (2);
353  //*that's it
354  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 25, 9 ), // was changed 30 June 2009 from 10, 10
355  ma_harvest, false );
356  break;
357 
358  case ma_harvest:
359  if (!m_farm->Harvest( m_field, 0.0,
360  m_field->GetMDates(1,0) - g_date->DayInYear())) {
361  SimpleEvent( g_date->Date() + 1, ma_harvest, false );
362  break;
363  }
364  {
365  d1 = g_date->OldDays() + m_field->GetMDates(0,1);
366  if ( g_date->Date() > d1 ) {
367  d1 = g_date->Date();
368  }
369  SimpleEvent( d1, ma_stubble, false );
370  }
371  break;
372 
373  case ma_stubble:
374  if ( m_ev->m_lock || m_farm->DoIt( 25 )) {
375  if (!m_farm->StubbleHarrowing( m_field, 0.0,
376  m_field->GetMDates(1,1) -
377  g_date->DayInYear())) {
378  SimpleEvent( g_date->Date() + 1, ma_stubble, false );
379  break;
380  }
381  }
382  d1=g_date->DayInYear();
383  done = true;
384  break;
385 
386  default:
387  g_msg->Warn( WARN_BUG, "Maize::Do(): "
388  "Unknown event type! ", "" );
389  exit( 1 );
390  }
391 
392  return done;
393 }
394 
395 
ma_start
Definition: Maize.h:36
MAIZE_HERBI_ONE_DATE
#define MAIZE_HERBI_ONE_DATE
Definition: Maize.h:33
Farm::FA_Slurry
virtual bool FA_Slurry(LE *a_field, double a_user, int a_days)
Spready slurry on a_field owned by an stock farmer.
Definition: farmfuncs.cpp:965
ma_fa_npk
Definition: Maize.h:44
Farm::SpringPlough
virtual bool SpringPlough(LE *a_field, double a_user, int a_days)
Carry out a ploughing event in the spring on a_field.
Definition: farmfuncs.cpp:444
Farm::HerbicideTreat
virtual bool HerbicideTreat(LE *a_field, double a_user, int a_days)
Apply herbicide to a_field.
Definition: farmfuncs.cpp:1156
Farm::Harvest
virtual bool Harvest(LE *a_field, double a_user, int a_days)
Carry out a harvest on a_field.
Definition: farmfuncs.cpp:1769
FarmEvent::m_lock
bool m_lock
Definition: farm.h:465
ma_stubble
Definition: Maize.h:53
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
ma_water_two
Definition: Maize.h:51
Maize::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: Maize.cpp:36
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
tof_OptimisingFarm
Definition: farm.h:273
ma_spring_harrow
Definition: Maize.h:42
ma_harvest
Definition: Maize.h:52
ma_fa_slurry_one
Definition: Maize.h:40
Farm::FA_Manure
virtual bool FA_Manure(LE *a_field, double a_user, int a_days)
Spread manure on a_field owned by an stock farmer.
Definition: farmfuncs.cpp:1036
ma_herbi_two
Definition: Maize.h:48
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
Farm::FA_NPK
virtual bool FA_NPK(LE *a_field, double a_user, int a_days)
Apply NPK fertilizer to a_field owned by an stock farmer.
Definition: farmfuncs.cpp:917
ma_water_one
Definition: Maize.h:50
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
ma_row_one
Definition: Maize.h:46
Farm::Prob_multiplier
virtual double Prob_multiplier()
Definition: farm.h:786
ma_autumn_plough
Definition: Maize.h:39
TTypesOfVegetation
TTypesOfVegetation
Definition: tov_declaration.h:30
Farm::Spraying_herbicides
virtual bool Spraying_herbicides(TTypesOfVegetation)
Definition: farm.h:784
MAIZE_SOW_DATE
#define MAIZE_SOW_DATE
Definition: Maize.h:32
cfg_ins_app_prop1
CfgFloat cfg_ins_app_prop1
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
cfg_herbi_app_prop
CfgFloat cfg_herbi_app_prop
Crop::m_field
LE * m_field
Definition: farm.h:538
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
cfg_fungi_app_prop1
CfgFloat cfg_fungi_app_prop1
ma_spring_plough
Definition: Maize.h:41
Farm::RowCultivation
virtual bool RowCultivation(LE *a_field, double a_user, int a_days)
Carry out a harrowing between crop rows on a_field.
Definition: farmfuncs.cpp:1510
Farm::Water
virtual bool Water(LE *a_field, double a_user, int a_days)
Carry out a watering on a_field.
Definition: farmfuncs.cpp:1717
Crop::m_last_date
int m_last_date
Definition: farm.h:542
Farm::AutumnPlough
virtual bool AutumnPlough(LE *a_field, double a_user, int a_days)
Carry out a ploughing event in the autumn on a_field.
Definition: farmfuncs.cpp:132
ma_fa_manure_a
Definition: Maize.h:37
ma_row_two
Definition: Maize.h:49
Farm::SpringSow
virtual bool SpringSow(LE *a_field, double a_user, int a_days)
Carry out a sowing event in the spring on a_field.
Definition: farmfuncs.cpp:546
ma_herbi_one
Definition: Maize.h:45
Farm
The base class for all farm types.
Definition: farm.h:767
ma_fa_manure_b
Definition: Maize.h:38
ma_spring_sow
Definition: Maize.h:43
ma_fa_slurry_two
Definition: Maize.h:47
Crop::ChooseNextCrop
void ChooseNextCrop(int a_no_dates)
Chooses the next crop to grow in a field.
Definition: farm.cpp:318
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
cfg_greg_app_prop
CfgFloat cfg_greg_app_prop
Farm::StubbleHarrowing
virtual bool StubbleHarrowing(LE *a_field, double a_user, int a_days)
Carry out stubble harrowing on a_field.
Definition: farmfuncs.cpp:2209
Farm::SpringHarrow
virtual bool SpringHarrow(LE *a_field, double a_user, int a_days)
Carry out a harrow event in the spring on a_field.
Definition: farmfuncs.cpp:471