ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OWinterBarley.cpp
Go to the documentation of this file.
1 //
2 // OWinterBarley.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/OWinterBarley.h"
30 
31 extern CfgFloat cfg_strigling_prop;
32 
33 bool OWinterBarley::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 owb_start:
44  {
45  // Set up the date management stuff
46  m_last_date=g_date->DayInYear(5,8);
47  // Start and stop dates for all events after harvest
48  int noDates= 2;
49  m_field->SetMDates(0,0,g_date->DayInYear(20,7));
50  // Determined by harvest date - used to see if at all possible
51  m_field->SetMDates(1,0,g_date->DayInYear(1,8));
52  m_field->SetMDates(0,1,g_date->DayInYear(25,7));
53  m_field->SetMDates(1,1,g_date->DayInYear(5,8));
54  // Check the next crop for early start, unless it is a spring crop
55  // in which case we ASSUME that no checking is necessary!!!!
56  // So DO NOT implement a crop that runs over the year boundary
57  if (m_ev->m_startday>g_date->DayInYear(1,7))
58  {
59  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
60  {
61  g_msg->Warn( WARN_BUG, "OWinterBarley::Do(): "
62  "Harvest too late for the next crop to start!!!", "" );
63  exit( 1 );
64  }
65  // Now fix any late finishing problems
66  for (int i=0; i<noDates; i++)
67  {
68  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
69  m_field->SetMDates(0,i,m_ev->m_startday-1);
70  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
71  m_field->SetMDates(1,i,m_ev->m_startday-1);
72  }
73  }
74  // Now no operations can be timed after the start of the next crop.
75 
76  int d1;
77  if ( ! m_ev->m_first_year )
78  {
79  int today=g_date->Date();
80  // Are we before July 1st?
81  d1 = g_date->OldDays() + g_date->DayInYear( 1,7 );
82  if (today < d1)
83  {
84  // Yes, too early. We assumme this is because the last crop was late
85  g_msg->Warn( WARN_BUG, "OWinterBarley::Do(): "
86  "Crop start attempt between 1st Jan & 1st July", "" );
87  exit( 1 );
88  }
89  else
90  {
91  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
92  if (today > d1)
93  {
94  // Yes too late - should not happen - raise an error
95  g_msg->Warn( WARN_BUG, "OWinterBarley::Do(): "
96  "Crop start attempt after last possible start date", "" );
97  exit( 1 );
98  }
99  }
100  }
101  else
102  {
103  SimpleEvent( g_date->OldDays() + g_date->DayInYear(20,7 ),
104  owb_harvest, false );
105  break;
106  }
107  // End single block date checking code. Please see next line
108  // comment as well.
109  // Reinit d1 to first possible starting date.
110  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
111  if ( g_date->Date() > d1 ) {
112  d1 = g_date->Date();
113  }
114  // OK, let's go.
115 
116  OWB_DID_SPRING_SOW = false;
117 
118  if ( m_farm->IsStockFarmer()) {
120  } else {
122  }
123  }
124  break;
125 
127  if ( m_ev->m_lock || m_farm->DoIt( 50 ))
128  {
129  if (!m_farm->FA_Manure( m_field, 0.0,
130  g_date->DayInYear( 30, 9 ) -
131  g_date->DayInYear())) {
132  SimpleEvent( g_date->Date() + 1, owb_fertmanure_stock_one, true );
133  break;
134  }
135  }
136  {
137  int d1 = g_date->Date();
138  if ( d1 < g_date->OldDays() + g_date->DayInYear( 25, 8 )) {
139  d1 = g_date->OldDays() + g_date->DayInYear( 25, 8 );
140  }
141  SimpleEvent( d1, owb_autumn_plough, false );
142  }
143  break;
144 
146  if ( m_ev->m_lock || m_farm->DoIt( 50 ))
147  {
148  if (!m_farm->FP_Manure( m_field, 0.0,
149  g_date->DayInYear( 30, 9 ) -
150  g_date->DayInYear())) {
151  SimpleEvent( g_date->Date() + 1, owb_fertmanure_plant_one, true );
152  break;
153  }
154  }
155  {
156  int d1 = g_date->Date();
157  if ( d1 < g_date->OldDays() + g_date->DayInYear( 25, 8 )) {
158  d1 = g_date->OldDays() + g_date->DayInYear( 25, 8 );
159  }
160  SimpleEvent( d1, owb_autumn_plough, false );
161  }
162  break;
163 
164  case owb_autumn_plough:
165  if (!m_farm->AutumnPlough( m_field, 0.0,
166  g_date->DayInYear( 25, 9 ) -
167  g_date->DayInYear())) {
168  SimpleEvent( g_date->Date() + 1, owb_autumn_plough, true );
169  break;
170  }
171  SimpleEvent( g_date->Date(), owb_autumn_harrow, false );
172  break;
173 
174  case owb_autumn_harrow:
175  if (!m_farm->AutumnHarrow( m_field, 0.0,
176  g_date->DayInYear( 25, 9 ) -
177  g_date->DayInYear())) {
178  SimpleEvent( g_date->Date() + 1, owb_autumn_harrow, true );
179  break;
180  }
181  {
182  int d1 = g_date->Date();
183  if ( d1 < g_date->OldDays() + g_date->DayInYear( 10, 9 )) {
184  d1 = g_date->OldDays() + g_date->DayInYear( 10, 9 );
185  }
186  SimpleEvent( d1, owb_autumn_sow, false );
187  }
188  break;
189 
190  case owb_autumn_sow:
191  if (!m_farm->AutumnSow( m_field, 0.0,
192  g_date->DayInYear( 25,9 ) -
193  g_date->DayInYear())) {
194  SimpleEvent( g_date->Date() + 1, owb_autumn_sow, true );
195  break;
196  }
197  {
198  int d1 = g_date->Date() + 10;
199  if ( d1 < g_date->OldDays() + g_date->DayInYear( 25, 9 )) {
200  d1 = g_date->OldDays() + g_date->DayInYear( 25, 9 );
201  }
202  SimpleEvent( d1, owb_strigling_one, false );
203  }
204  break;
205 
206  case owb_strigling_one:
207  if (!m_farm->Strigling( m_field, 0.0,
208  g_date->DayInYear( 10,10 ) -
209  g_date->DayInYear())) {
210  SimpleEvent( g_date->Date() + 1, owb_strigling_one, true );
211  break;
212  }
213  if ( m_farm->IsStockFarmer()) {
214  // Skip directly to second fertilization with manure.
215  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 4 ) + 365,
216  owb_fertslurry_stock, false );
217 
218  // Queue up second strigling too, plant farmers does this themselves
219  // after the second (spring) sow.
220  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 5,4 ) + 365,
221  owb_strigling_two, false );
222 
223  } else {
224  // Plant farmers (will perhaps) do the spring sow.
225  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 25, 3 ) + 365,
226  owb_spring_sow, false );
227  }
228  break;
229 
230  case owb_spring_sow:
231  if ( m_ev->m_lock || m_farm->DoIt( 60 ))
232  {
233  // We intend to sow, eventually, so stop second strigling
234  // ahead of time.
235  OWB_DID_SPRING_SOW = true;
236  if (!m_farm->SpringSow( m_field, 0.0,
237  g_date->DayInYear( 15,4 ) -
238  g_date->DayInYear())) {
239  SimpleEvent( g_date->Date() + 1, owb_spring_sow, true );
240  break;
241  }
242  }
243  {
244  int d1 = g_date->Date();
245  if ( d1 < g_date->OldDays() + g_date->DayInYear( 1, 4 )) {
246  d1 = g_date->OldDays() + g_date->DayInYear( 1, 4 );
247  }
248  SimpleEvent( d1, owb_fertslurry_plant, false );
249 
250  if ( d1 < g_date->OldDays() + g_date->DayInYear( 5, 4 )) {
251  d1 = g_date->OldDays() + g_date->DayInYear( 5, 4 );
252  }
253  SimpleEvent( d1, owb_strigling_two, false );
254  }
255  break;
256 
257  case owb_strigling_two:
258  if ( OWB_DID_SPRING_SOW ) {
259  break;
260  }
261  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 60 )))
262  {
263  if (!m_farm->Strigling( m_field, 0.0,
264  g_date->DayInYear( 25,4 ) -
265  g_date->DayInYear())) {
266  SimpleEvent( g_date->Date() + 1, owb_strigling_two, true );
267  break;
268  }
269  }
270  // End of thread.
271  break;
272 
274  if (!m_farm->FP_Slurry( m_field, 0.0,
275  g_date->DayInYear( 30,4 ) -
276  g_date->DayInYear())) {
277  SimpleEvent( g_date->Date() + 1, owb_fertslurry_plant, true );
278  break;
279  }
280  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20,7 ),
281  owb_harvest, false );
282  break;
283 
285  if (!m_farm->FA_Slurry( m_field, 0.0,
286  g_date->DayInYear( 30,4 ) -
287  g_date->DayInYear())) {
288  SimpleEvent( g_date->Date() + 1, owb_fertslurry_stock, true );
289  break;
290  }
291  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20,7 ),
292  owb_harvest, false );
293  break;
294 
295  case owb_harvest:
296  if (!m_farm->Harvest( m_field, 0.0,
297  m_field->GetMDates(1,0) -
298  g_date->DayInYear())) {
299  SimpleEvent( g_date->Date() + 1, owb_harvest, true );
300  break;
301  }
302  SimpleEvent( g_date->Date(), owb_straw_chopping, false );
303  break;
304 
305  case owb_straw_chopping:
306  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
307  {
308  if (!m_farm->StrawChopping( m_field, 0.0,
309  m_field->GetMDates(1,0) -
310  g_date->DayInYear())) {
311  SimpleEvent( g_date->Date() + 1, owb_straw_chopping, true );
312  break;
313  }
314  // Did chopping, so skip to stubble harrowing.
315  SimpleEvent( g_date->Date(), owb_stubble_harrowing, false );
316  break;
317  }
318  // No chop, thus try to bail hay.
319  {
320  int d1 = g_date->Date();
321  if ( d1 < g_date->OldDays() + m_field->GetMDates(0,1)) {
322  d1 = g_date->OldDays() + m_field->GetMDates(0,1);
323  }
324  SimpleEvent( d1, owb_hay_bailing, false );
325  }
326  break;
327 
328  case owb_hay_bailing:
329  if ( m_ev->m_lock || m_farm->DoIt( 80 )) {
330  if (!m_farm->HayBailing( m_field, 0.0,
331  m_field->GetMDates(1,1) -
332  g_date->DayInYear())) {
333  SimpleEvent( g_date->Date() + 1, owb_hay_bailing, true );
334  break;
335  }
336  }
337  SimpleEvent( g_date->Date(), owb_stubble_harrowing, false );
338  break;
339 
341  if ( m_ev->m_lock || m_farm->DoIt( 5 )) {
342  if (!m_farm->StubbleHarrowing( m_field, 0.0,
343  m_field->GetMDates(1,1) -
344  g_date->DayInYear())) {
345  SimpleEvent( g_date->Date() + 1, owb_stubble_harrowing, true );
346  break;
347  }
348  }
349  done = true;
350  break;
351 
352  default:
353  g_msg->Warn( WARN_BUG, "OWinterBarley::Do(): "
354  "Unknown event type! ", "" );
355  exit( 1 );
356  }
357 
358  return done;
359 }
360 
361 
owb_spring_sow
Definition: OWinterBarley.h:42
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
owb_fertslurry_plant
Definition: OWinterBarley.h:43
Farm::IsStockFarmer
bool IsStockFarmer(void)
Definition: farm.h:905
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
owb_fertmanure_plant_one
Definition: OWinterBarley.h:36
cfg_strigling_prop
CfgFloat cfg_strigling_prop
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
OWB_DID_SPRING_SOW
#define OWB_DID_SPRING_SOW
Definition: OWinterBarley.h:32
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
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
owb_autumn_plough
Definition: OWinterBarley.h:38
owb_stubble_harrowing
Definition: OWinterBarley.h:49
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
owb_fertmanure_stock_one
Definition: OWinterBarley.h:37
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
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
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
owb_straw_chopping
Definition: OWinterBarley.h:47
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
owb_start
Definition: OWinterBarley.h:35
owb_harvest
Definition: OWinterBarley.h:46
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
owb_autumn_harrow
Definition: OWinterBarley.h:39
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
owb_fertslurry_stock
Definition: OWinterBarley.h:44
owb_hay_bailing
Definition: OWinterBarley.h:48
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
owb_strigling_two
Definition: OWinterBarley.h:45
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
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
Farm
The base class for all farm types.
Definition: farm.h:767
OWinterBarley::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OWinterBarley.cpp:33
owb_strigling_one
Definition: OWinterBarley.h:41
owb_autumn_sow
Definition: OWinterBarley.h:40
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
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