ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OWinterRape.cpp
Go to the documentation of this file.
1 //
2 // OWinterRape.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/OWinterRape.h"
30 
31 
32 bool OWinterRape::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
33 {
34  m_farm = a_farm;
35  m_field = a_field;
36  m_ev = a_ev;
37 
38  bool done = false;
39 
40  switch ( m_ev->m_todo ) {
41  case owr_start:
42  {
43  // Set up the date management stuff
44  // Could save the start day in case it is needed later
45  // m_field->m_startday = m_ev->m_startday;
46  m_last_date=g_date->DayInYear(15,8);
47  // Start and stop dates for all events after harvest
48  int noDates= 1;
49  m_field->SetMDates(0,0,g_date->DayInYear(1,8));
50  // Determined by harvest date - used to see if at all possible
51  m_field->SetMDates(1,0,g_date->DayInYear(15,8));
52  // Check the next crop for early start, unless it is a spring crop
53  // in which case we ASSUME that no checking is necessary!!!!
54  // So DO NOT implement a crop that runs over the year boundary
55  if (m_ev->m_startday>g_date->DayInYear(1,7))
56  {
57  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
58  {
59  g_msg->Warn( WARN_BUG, "OWinterRape::Do(): "
60  "Harvest too late for the next crop to start!!!", "" );
61  exit( 1 );
62  }
63  // Now fix any late finishing problems
64  for (int i=0; i<noDates; i++)
65  {
66  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
67  m_field->SetMDates(0,i,m_ev->m_startday-1);
68  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
69  m_field->SetMDates(1,i,m_ev->m_startday-1);
70  }
71  }
72  // Now no operations can be timed after the start of the next crop.
73 
74  int d1;
75  if ( ! m_ev->m_first_year )
76  {
77  int today=g_date->Date();
78  // Are we before July 1st?
79  d1 = g_date->OldDays() + g_date->DayInYear( 1,7 );
80  if (today < d1)
81  {
82  // Yes, too early. We assumme this is because the last crop was late
83  g_msg->Warn( WARN_BUG, " OWinterRape::Do(): "
84  "Crop start attempt between 1st Jan & 1st July", "" );
85  exit( 1 );
86  }
87  else
88  {
89  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
90  if (today > d1)
91  {
92  // Yes too late - should not happen - raise an error
93  g_msg->Warn( WARN_BUG, " OWinterRape::Do(): "
94  "Crop start attempt after last possible start date", "" );
95  exit( 1 );
96  }
97  }
98  }
99  else
100  {
101  SimpleEvent( g_date->OldDays() + g_date->DayInYear(5,7 ),
102  owr_swarth, false );
103  break;
104  }
105  // End single block date checking code. Please see next line
106  // comment as well.
107  // Reinit d1 to first possible starting date.
108  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
109  if ( g_date->Date() > d1 ) {
110  d1 = g_date->Date();
111  }
112  // OK, let's go.
113  if ( m_farm->IsStockFarmer()) {
114  SimpleEvent( d1, owr_fa_manure, false );
115  } else {
116  SimpleEvent( d1, owr_fp_manure, false );
117  }
118  }
119  break;
120 
121  case owr_fa_manure:
122  if ( m_ev->m_lock || m_farm->DoIt( 75 ))
123  {
124  if (!m_farm->FA_Manure( m_field, 0.0,
125  g_date->DayInYear( 25, 8 ) - g_date->DayInYear())) {
126  SimpleEvent( g_date->Date() + 1, owr_fa_manure, true );
127  break;
128  }
129  }
130  SimpleEvent( g_date->Date() + 1, owr_autumn_plough, false );
131  break;
132 
133  case owr_fp_manure:
134  if ( m_ev->m_lock || m_farm->DoIt( 50 ))
135  {
136  if (!m_farm->FP_Manure( m_field, 0.0,
137  g_date->DayInYear( 25, 8 ) - g_date->DayInYear())) {
138  SimpleEvent( g_date->Date() + 1, owr_fp_manure, true );
139  break;
140  }
141  }
142  SimpleEvent( g_date->Date() + 1, owr_autumn_plough, false );
143  break;
144 
145  case owr_autumn_plough:
146  if (!m_farm->AutumnPlough( m_field, 0.0,
147  g_date->DayInYear( 25, 8 ) -
148  g_date->DayInYear())) {
149  SimpleEvent( g_date->Date() + 1, owr_autumn_plough, true );
150  break;
151  }
152  SimpleEvent( g_date->Date() + 1, owr_autumn_harrow, false );
153  break;
154 
155  case owr_autumn_harrow:
156  if (!m_farm->AutumnHarrow( m_field, 0.0,
157  g_date->DayInYear( 27, 8 ) -
158  g_date->DayInYear())) {
159  SimpleEvent( g_date->Date() + 1, owr_autumn_harrow, false );
160  break;
161  }
162  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10, 8 ),
163  owr_autumn_sow, false );
164  break;
165 
166  case owr_autumn_sow:
167  if (!m_farm->AutumnSow( m_field, 0.0,
168  g_date->DayInYear( 27, 8 ) - g_date->DayInYear())) {
169  SimpleEvent( g_date->Date() + 1, owr_autumn_sow, false );
170  break;
171  }
172  {
173  int d1 = g_date->Date() + 10;
174  if ( d1 < g_date->OldDays() + g_date->DayInYear( 20, 8 ))
175  d1 = g_date->OldDays() + g_date->DayInYear( 20, 8 );
176  SimpleEvent( d1, owr_row_cultivation_one, false );
177  }
178  break;
179 
181  if (!m_farm->RowCultivation( m_field, 0.0,
182  g_date->DayInYear( 20, 9 ) -
183  g_date->DayInYear())) {
184  SimpleEvent( g_date->Date() + 1, owr_row_cultivation_one, true );
185  break;
186  }
187  {
188  int d1 = g_date->Date() + 7;
189  if ( d1 < g_date->OldDays() + g_date->DayInYear( 28, 8 ))
190  d1 = g_date->OldDays() + g_date->DayInYear( 28, 8 );
191  SimpleEvent( d1, owr_row_cultivation_two, false );
192  }
193  break;
194 
196  if (!m_farm->RowCultivation( m_field, 0.0,
197  g_date->DayInYear( 30, 9 ) -
198  g_date->DayInYear())) {
199  SimpleEvent( g_date->Date() + 1, owr_row_cultivation_two, true );
200  break;
201  }
202  {
203  int d1 = g_date->OldDays() + g_date->DayInYear( 15, 3 ) + 365;
204  if ( m_farm->IsStockFarmer()) {
205  SimpleEvent( d1, owr_fa_slurry, false );
206  } else {
207  SimpleEvent( d1, owr_fp_slurry, false );
208  }
209  }
210  break;
211 
212  case owr_fa_slurry:
213  if (!m_farm->FA_Slurry( m_field, 0.0,
214  g_date->DayInYear( 15, 4 ) -
215  g_date->DayInYear())) {
216  SimpleEvent( g_date->Date() + 1, owr_fa_slurry, true );
217  break;
218  }
219  {
220  int d1 = g_date->Date() + 1;
221  if ( d1 < g_date->OldDays() + g_date->DayInYear( 1, 4 ))
222  d1 = g_date->OldDays() + g_date->DayInYear( 1, 4 );
224  }
225  break;
226 
227  case owr_fp_slurry:
228  if (!m_farm->FP_Slurry( m_field, 0.0,
229  g_date->DayInYear( 15, 4 ) -
230  g_date->DayInYear())) {
231  SimpleEvent( g_date->Date() + 1, owr_fp_slurry, true );
232  break;
233  }
234  {
235  int d1 = g_date->Date() + 1;
236  if ( d1 < g_date->OldDays() + g_date->DayInYear( 1, 4 ))
237  d1 = g_date->OldDays() + g_date->DayInYear( 1, 4 );
239  }
240  break;
241 
243  if (!m_farm->RowCultivation( m_field, 0.0,
244  g_date->DayInYear( 30, 4 ) -
245  g_date->DayInYear())) {
246  SimpleEvent( g_date->Date() + 1, owr_row_cultivation_three, true );
247  break;
248  }
249  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 5, 7 ),
250  owr_swarth, false );
251  break;
252 
253  case owr_swarth:
254  if (m_ev->m_lock || m_farm->DoIt( 50 ))
255  {
256  if ( !m_farm->Swathing( m_field, 0.0,
257  g_date->DayInYear( 25, 7 ) - g_date->DayInYear())) {
258  SimpleEvent( g_date->Date() + 1, owr_swarth, true );
259  break;
260  }
261  }
262  {
263  int d1 = g_date->Date() + 5;
264  if ( d1 < g_date->OldDays() + g_date->DayInYear( 10, 7 ))
265  d1 = g_date->OldDays() + g_date->DayInYear( 10, 7 );
266  SimpleEvent( d1, owr_harvest, false );
267  }
268  break;
269 
270  case owr_harvest:
271  if ( !m_farm->Harvest( m_field, 0.0,
272  g_date->DayInYear( 1, 8 ) -
273  g_date->DayInYear())) {
274  SimpleEvent( g_date->Date() + 1, owr_harvest, false );
275  break;
276  }
277  SimpleEvent( g_date->Date(), owr_straw_chop, false );
278  break;
279 
280  case owr_straw_chop:
281  if ( !m_farm->StrawChopping( m_field, 0.0,
282  g_date->DayInYear( 1, 8 ) -
283  g_date->DayInYear())) {
284  SimpleEvent( g_date->Date() + 1, owr_straw_chop, true );
285  break;
286  }
287  {
288  int d1 = g_date->Date() + 1;
289  if ( d1 < g_date->OldDays() + g_date->DayInYear( 15, 7 ))
290  d1 = g_date->OldDays() + g_date->DayInYear( 15, 7 );
291  SimpleEvent( d1, owr_stub_harrow, false );
292  }
293  break;
294 
295  case owr_stub_harrow:
296  if ( m_ev->m_lock || m_farm->DoIt( 75 )) {
297  if ( !m_farm->StubbleHarrowing( m_field, 0.0,
298  g_date->DayInYear( 15, 8 ) - g_date->DayInYear())) {
299  SimpleEvent( g_date->Date() + 1, owr_stub_harrow, true );
300  break;
301  }
302  }
303  done = true;
304  break;
305 
306  default:
307  g_msg->Warn( WARN_BUG, "OWinterRape::Do(): "
308  "Unknown event type! ", "" );
309  exit( 1 );
310  }
311  return done;
312 }
313 
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
owr_row_cultivation_two
Definition: OWinterRape.h:50
Farm::IsStockFarmer
bool IsStockFarmer(void)
Definition: farm.h:905
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
OWinterRape::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OWinterRape.cpp:32
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
owr_fp_slurry
Definition: OWinterRape.h:52
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
owr_row_cultivation_three
Definition: OWinterRape.h:53
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
owr_autumn_plough
Definition: OWinterRape.h:46
owr_row_cultivation_one
Definition: OWinterRape.h:49
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
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
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
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
owr_autumn_harrow
Definition: OWinterRape.h:47
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
owr_fa_slurry
Definition: OWinterRape.h:51
Farm
The base class for all farm types.
Definition: farm.h:767
owr_stub_harrow
Definition: OWinterRape.h:57
owr_start
Definition: OWinterRape.h:43
owr_fa_manure
Definition: OWinterRape.h:44
owr_straw_chop
Definition: OWinterRape.h:56
Farm::Swathing
virtual bool Swathing(LE *a_field, double a_user, int a_days)
Cut the crop on a_field and leave it lying (probably rape)
Definition: farmfuncs.cpp:1744
owr_fp_manure
Definition: OWinterRape.h:45
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
owr_harvest
Definition: OWinterRape.h:55
owr_swarth
Definition: OWinterRape.h:54
owr_autumn_sow
Definition: OWinterRape.h:48
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