ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OWinterWheat.cpp
Go to the documentation of this file.
1 //
2 // OWinterWheat.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2016, 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/OWinterWheat.h"
30 
31 extern CfgFloat cfg_strigling_prop;
32 
33 bool OWinterWheat::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
34 {
35  m_farm = a_farm;
36  m_field = a_field;
37  m_ev = a_ev;
38 
39  bool done = false;
40 
41  switch ( m_ev->m_todo )
42  {
43  case oww_start:
44  {
45  OWW_PLOUGH_RUNS = false;
46  OWW_HARROW_RUNS = false;
47  // Set up the date management stuff
48  m_last_date=g_date->DayInYear(15,9);
49  // Start and stop dates for all events after harvest
50  int noDates= 7;
51  m_field->SetMDates(0,0,g_date->DayInYear(1,8));
52  // Determined by harvest date - used to see if at all possible
53  m_field->SetMDates(1,0,g_date->DayInYear(20,8));
54  m_field->SetMDates(0,1,g_date->DayInYear(5,8));
55  m_field->SetMDates(1,1,g_date->DayInYear(5,9));
56  m_field->SetMDates(0,2,g_date->DayInYear(25,8));
57  m_field->SetMDates(1,2,g_date->DayInYear(25,8));
58  m_field->SetMDates(0,3,g_date->DayInYear(25,8));
59  m_field->SetMDates(1,3,g_date->DayInYear(30,8));
60  m_field->SetMDates(0,4,g_date->DayInYear(5,8));
61  m_field->SetMDates(1,4,g_date->DayInYear(5,8)); // not needed
62  m_field->SetMDates(0,5,g_date->DayInYear(15,9));
63  m_field->SetMDates(1,5,g_date->DayInYear(15,9));
64  m_field->SetMDates(0,6,g_date->DayInYear(1,8));
65  m_field->SetMDates(1,6,g_date->DayInYear(30,8));
66  // Check the next crop for early start, unless it is a spring crop
67  // in which case we ASSUME that no checking is necessary!!!!
68  // So DO NOT implement a crop that runs over the year boundary
69  if (m_ev->m_startday>g_date->DayInYear(1,7))
70  {
71  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
72  {
73  g_msg->Warn( WARN_BUG, "OWinterWheat::Do(): "
74  "Harvest too late for the next crop to start!!!", "" );
75  exit( 1 );
76  }
77  // Now fix any late finishing problems
78  for (int i=0; i<noDates; i++)
79  {
80  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
81  m_field->SetMDates(0,i,m_ev->m_startday-1);
82  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
83  m_field->SetMDates(1,i,m_ev->m_startday-1);
84  }
85  }
86  // Now no operations can be timed after the start of the next crop.
87 
88  int d1;
89  if ( ! m_ev->m_first_year )
90  {
91  int today=g_date->Date();
92  // Are we before July 1st?
93  d1 = g_date->OldDays() + g_date->DayInYear( 1,7 );
94  if (today < d1)
95  {
96  // Yes, too early. We assumme this is because the last crop was late
97  g_msg->Warn( WARN_BUG, "OWinterWheat::Do(): "
98  "Crop start attempt between 1st Jan & 1st July", "" );
99  exit( 1 );
100  }
101  else
102  {
103  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
104  if (today > d1)
105  {
106  // Yes too late - should not happen - raise an error
107  g_msg->Warn( WARN_BUG, "OWinterWheat::Do(): "
108  "Crop start attempt after last possible start date", "" );
109  exit( 1 );
110  }
111  }
112  }
113  else
114  {
115  SimpleEvent( g_date->OldDays() + g_date->DayInYear(28,8),
116  oww_harvest, false );
117  break;
118  }
119  // End single block date checking code. Please see next line
120  // comment as well.
121  // Reinit d1 to first possible starting date.
122  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
123  if ( g_date->Date() > d1 ) {
124  d1 = g_date->Date();
125  }
126 
127  // OK, let's go.
128  if (m_farm->IsStockFarmer()) // StockFarmer
129  {
130  SimpleEvent( d1, oww_ferti_s1, false );
131  }
132  else SimpleEvent( d1, oww_ferti_p1, false );
133  }
134  break;
135 
136  case oww_ferti_p1:
137  if ( m_ev->m_lock || m_farm->DoIt( 10 ))
138  {
139  if (!m_farm->FP_Manure( m_field, 0.0,
140  g_date->DayInYear( 30,9 ) - g_date->DayInYear())) {
141  SimpleEvent( g_date->Date() + 1, oww_ferti_p1, true );
142  break;
143  }
144  }
145  SimpleEvent( g_date->Date(),oww_autumn_plough, false );
146  break;
147 
148  case oww_ferti_s1:
149  if ( m_ev->m_lock || m_farm->DoIt( 40 ))
150  {
151  if (!m_farm->FA_Manure( m_field, 0.0,
152  g_date->DayInYear( 30,9 ) - g_date->DayInYear())) {
153  SimpleEvent( g_date->Date() + 1, oww_ferti_s1, true );
154  break;
155  }
156  }
157  SimpleEvent( g_date->Date(),oww_autumn_plough, false );
158  break;
159 
160  case oww_autumn_plough:
161  if (!m_farm->AutumnPlough( m_field, 0.0,
162  g_date->DayInYear( 30,9 ) - g_date->DayInYear())) {
163  SimpleEvent( g_date->Date() + 1, oww_autumn_plough, true );
164  break;
165  }
166  SimpleEvent( g_date->Date()+1,oww_autumn_harrow, false );
167  break;
168 
169  case oww_autumn_harrow:
170  if (!m_farm->AutumnHarrow( m_field, 0.0,
171  g_date->DayInYear( 10,10 ) - g_date->DayInYear())) {
172  SimpleEvent( g_date->Date() + 1, oww_autumn_harrow, true );
173  break;
174  }
175  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20,9 ),
176  oww_autumn_sow, false );
177  break;
178 
179  case oww_autumn_sow:
180  if (!m_farm->AutumnSow( m_field, 0.0,
181  g_date->DayInYear( 10,10 ) - g_date->DayInYear())) {
182  SimpleEvent( g_date->Date() + 1, oww_autumn_sow, true );
183  break;
184  }
185  {
186  long newdate1 = g_date->OldDays() + g_date->DayInYear( 10,9 );
187  long newdate2 = g_date->Date() + 10;
188  if ( newdate2 > newdate1 )
189  newdate1 = newdate2;
190  SimpleEvent( newdate1, oww_strigling1, false );
191  }
192  break;
193 
194  case oww_strigling1:
195  if (!m_farm->Strigling( m_field, 0.0,
196  g_date->DayInYear( 10,10 ) - g_date->DayInYear())) {
197  SimpleEvent( g_date->Date() + 1, oww_strigling1, true );
198  break;
199  }
200  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 24,9 ),
201  oww_strigling2, false );
202  break;
203 
204  case oww_strigling2:
205  // --FN--
206  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 70 )))
207  // **CJT** Soil Type 2-4 later implementation
208  {
209  if (!m_farm->Strigling( m_field, 0.0,
210  g_date->DayInYear( 24,10 ) - g_date->DayInYear())) {
211  SimpleEvent( g_date->Date() + 1, oww_strigling2, true );
212  break;
213  }
214  }
215  // Next year
216  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 25,3 )+365,
217  oww_strigling_sow, false );
218  break;
219 
220  case oww_strigling_sow:
221  if (!m_farm->StriglingSow( m_field, 0.0,
222  g_date->DayInYear( 15,4 ) - g_date->DayInYear())) {
223  SimpleEvent( g_date->Date() + 1, oww_strigling_sow, true );
224  break;
225  }
226  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,4 ),
227  oww_spring_roll1, false );
228  break;
229 
230  case oww_spring_roll1:
231  if ( m_ev->m_lock || m_farm->DoIt( 5 ))
232  {
233  if (!m_farm->SpringRoll( m_field, 0.0,
234  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
235  SimpleEvent( g_date->Date() + 1, oww_spring_roll1, true );
236  break;
237  }
238  }
239  if (m_farm->IsStockFarmer()) // StockFarmer
240  {
241  SimpleEvent( g_date->Date() + 1, oww_ferti_s2, false );
242  }
243  else
244  SimpleEvent( g_date->Date() + 1, oww_ferti_p2, false );
245  break;
246 
247  case oww_ferti_p2:
248  if (!m_farm->FP_Slurry( m_field, 0.0,
249  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
250  SimpleEvent( g_date->Date() + 1, oww_ferti_p2, true );
251  break;
252  }
253  SimpleEvent( g_date->Date()+1,oww_spring_roll2, false );
254  break;
255 
256  case oww_ferti_s2:
257  if (!m_farm->FA_Slurry( m_field, 0.0,
258  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
259  SimpleEvent( g_date->Date() + 1, oww_ferti_s2, true );
260  break;
261  }
262  SimpleEvent( g_date->Date()+1,oww_spring_roll2, false );
263  break;
264 
265  case oww_spring_roll2:
266  if ( m_ev->m_lock || m_farm->DoIt( 5 ))
267  {
268  if (!m_farm->SpringRoll( m_field, 0.0,
269  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
270  SimpleEvent( g_date->Date() + 1, oww_spring_roll2, true );
271  break;
272  }
273  }
274  if (m_farm->IsStockFarmer()) // StockFarmer
275  {
276  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,5 ),
277  oww_ferti_s3, false );
278  }
279  else
280  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,8 ),
281  oww_harvest, false );
282  break;
283 
284  case oww_ferti_s3:
285  if ( m_ev->m_lock || m_farm->DoIt( 50 ))
286  {
287  if (!m_farm->FA_Slurry( m_field, 0.0,
288  g_date->DayInYear( 30,5 ) - g_date->DayInYear())) {
289  SimpleEvent( g_date->Date() + 1, oww_ferti_s3, true );
290  break;
291  }
292  }
293  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,8 ),
294  oww_harvest, false );
295  break;
296 
297  case oww_harvest:
298  if (!m_farm->Harvest( m_field, 0.0,
299  m_field->GetMDates(1,0) - g_date->DayInYear())) {
300  SimpleEvent( g_date->Date() + 1, oww_harvest, true );
301  break;
302  }
303  SimpleEvent(g_date->Date(), oww_straw_chopping, false);
304  break;
305 
306  case oww_straw_chopping:
307  {
308  if ( m_ev->m_lock || m_farm->DoIt( 60 ))
309  {
310  if (!m_farm->StrawChopping( m_field, 0.0,
311  m_field->GetMDates(1,0) - g_date->DayInYear()))
312  {
313  SimpleEvent( g_date->Date() + 1, oww_straw_chopping, true );
314  break;
315  }
316  else
317  {
318  // Did Chopping so test for stubble harrow/deep_plough
319  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,8 ),
320  oww_deep_plough, false );
321  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,1),
322  oww_stubble_harrow1, false );
323  break;
324  }
325  }
326  int d1=g_date->Date() + 3;
327  if (d1>m_field->GetMDates(0,2)) d1=m_field->GetMDates(0,2);
328  SimpleEvent( d1, oww_hay_turning, false );
329  }
330  break;
331 
332  case oww_hay_turning:
333  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
334  {
335  if (!m_farm->HayTurning( m_field, 0.0,
336  m_field->GetMDates(1,2) - g_date->DayInYear())) {
337  SimpleEvent( g_date->Date() + 1, oww_hay_turning, true );
338  break;
339  }
340  }
341  {
342  long d1 = g_date->OldDays() + g_date->DayInYear( 7,8 );
343  long d2 = g_date->Date() + 3;
344  if ( d2 > d1 ) d1 = d2;
345  if (d1>m_field->GetMDates(0,3)) d1=m_field->GetMDates(0,3);
346  SimpleEvent( d1, oww_hay_baling, false );
347  }
348  break;
349 
350  case oww_hay_baling:
351  if (!m_farm->HayBailing( m_field, 0.0,
352  m_field->GetMDates(1,3) - g_date->DayInYear())) {
353  SimpleEvent( g_date->Date() + 1, oww_hay_baling, true );
354  break;
355  }
356  // These events will almost certainly predate
357  // 'today' due to the ending date used above.
358  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,6),
359  oww_deep_plough, false );
360  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,4),
361  oww_stubble_harrow1, false );
362  break;
363 
364  case oww_stubble_harrow1:
365  OWW_HARROW_RUNS = true;
366  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
367  {
368  if (!m_farm->StubbleHarrowing( m_field, 0.0,
369  m_field->GetMDates(1,1) - g_date->DayInYear())) {
370  SimpleEvent( g_date->Date() + 1, oww_stubble_harrow1, true );
371  break;
372  }
373  int d1=g_date->Date()+10;
374  if (d1>m_field->GetMDates(0,5)) d1=m_field->GetMDates(0,5);
375  SimpleEvent( d1,oww_stubble_harrow2, false );
376  break;
377  }
378  OWW_HARROW_RUNS = false;
379  SimpleEvent( g_date->Date(), oww_catch_all, false );
380  break;
381 
382  case oww_stubble_harrow2:
383  if (!m_farm->StubbleHarrowing( m_field, 0.0,
384  m_field->GetMDates(1,5) - g_date->DayInYear()))
385  {
386  SimpleEvent( g_date->Date() + 1, oww_stubble_harrow2, true );
387  break;
388  }
389  OWW_HARROW_RUNS = false;
390  SimpleEvent( g_date->Date(), oww_catch_all, false );
391  break;
392 
393  case oww_deep_plough:
394  OWW_PLOUGH_RUNS = true;
395  if ( m_ev->m_lock || m_farm->DoIt( 5 ))
396  {
397  if (!m_farm->DeepPlough( m_field, 0.0,
398  m_field->GetMDates(1,6) - g_date->DayInYear()))
399  {
400  SimpleEvent( g_date->Date() + 1, oww_deep_plough, true );
401  break;
402  }
403  // if we deep plough then finish plan here
404  done=true;
405  // END OF MAIN THREAD
406  break; //
407  }
408  OWW_PLOUGH_RUNS = false;
409  SimpleEvent( g_date->Date(), oww_catch_all, false );
410  break;
411 
412  case oww_catch_all:
413  if ( !OWW_PLOUGH_RUNS || !OWW_HARROW_RUNS )
414  done = true;
415  break;
416 
417  default:
418  g_msg->Warn( WARN_BUG, "OWinterWheat::Do(): "
419  "Unknown event type! ", "" );
420  exit( 1 );
421  }
422  return done;
423 }
424 
oww_hay_turning
Definition: OWinterWheat.h:53
oww_spring_roll1
Definition: OWinterWheat.h:50
oww_start
Definition: OWinterWheat.h:36
Farm::SpringRoll
virtual bool SpringRoll(LE *a_field, double a_user, int a_days)
Carry out a roll event in the spring on a_field.
Definition: farmfuncs.cpp:525
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
oww_ferti_s1
Definition: OWinterWheat.h:38
Farm::IsStockFarmer
bool IsStockFarmer(void)
Definition: farm.h:905
OWW_PLOUGH_RUNS
#define OWW_PLOUGH_RUNS
Definition: OWinterWheat.h:32
Farm::Strigling
virtual bool Strigling(LE *a_field, double a_user, int a_days)
Carry out a mechanical weeding on a_field.
Definition: farmfuncs.cpp:1545
oww_hay_baling
Definition: OWinterWheat.h:55
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
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
oww_harvest
Definition: OWinterWheat.h:52
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
oww_autumn_plough
Definition: OWinterWheat.h:43
OWW_HARROW_RUNS
#define OWW_HARROW_RUNS
Definition: OWinterWheat.h:33
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
oww_stubble_harrow1
Definition: OWinterWheat.h:56
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
oww_strigling1
Definition: OWinterWheat.h:46
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
Farm::FP_Manure
virtual bool FP_Manure(LE *a_field, double a_user, int a_days)
Spread manure on a_field owned by an arable farmer.
Definition: farmfuncs.cpp:773
oww_autumn_harrow
Definition: OWinterWheat.h:44
Farm::AutumnSow
virtual bool AutumnSow(LE *a_field, double a_user, int a_days)
Carry out a sowing event in the autumn on a_field.
Definition: farmfuncs.cpp:364
oww_stubble_harrow2
Definition: OWinterWheat.h:57
OWinterWheat::Do
virtual bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OWinterWheat.cpp:33
Farm::HayBailing
virtual bool HayBailing(LE *a_field, double a_user, int a_days)
Carry out hay bailing on a_field.
Definition: farmfuncs.cpp:2184
Farm::StrawChopping
virtual bool StrawChopping(LE *a_field, double a_user, int a_days)
Carry out straw chopping on a_field.
Definition: farmfuncs.cpp:2132
oww_autumn_sow
Definition: OWinterWheat.h:45
oww_straw_chopping
Definition: OWinterWheat.h:54
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
Farm::FP_Slurry
virtual bool FP_Slurry(LE *a_field, double a_user, int a_days)
Apply slurry to a_field owned by an arable farmer.
Definition: farmfuncs.cpp:701
oww_spring_roll2
Definition: OWinterWheat.h:51
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
Farm::AutumnHarrow
virtual bool AutumnHarrow(LE *a_field, double a_user, int a_days)
Carry out a harrow event in the autumn on a_field.
Definition: farmfuncs.cpp:261
oww_ferti_p1
Definition: OWinterWheat.h:41
Farm::StriglingSow
virtual bool StriglingSow(LE *a_field, double a_user, int a_days)
Carry out a mechanical weeding followed by sowing on a_field.
Definition: farmfuncs.cpp:1597
Farm
The base class for all farm types.
Definition: farm.h:767
oww_ferti_p2
Definition: OWinterWheat.h:42
Farm::DeepPlough
virtual bool DeepPlough(LE *a_field, double a_user, int a_days)
Carry out a deep ploughing event on a_field.
Definition: farmfuncs.cpp:417
oww_ferti_s2
Definition: OWinterWheat.h:39
oww_catch_all
Definition: OWinterWheat.h:59
oww_strigling_sow
Definition: OWinterWheat.h:48
cfg_strigling_prop
CfgFloat cfg_strigling_prop
oww_ferti_s3
Definition: OWinterWheat.h:40
Farm::HayTurning
virtual bool HayTurning(LE *a_field, double a_user, int a_days)
Carry out hay turning on a_field.
Definition: farmfuncs.cpp:2159
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
oww_strigling2
Definition: OWinterWheat.h:47
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
oww_deep_plough
Definition: OWinterWheat.h:58