ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OSBarleySilage.cpp
Go to the documentation of this file.
1 //
2 // OSBarleySilage.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/OSBarleySilage.h"
30 
31 bool OSBarleySilage::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
32 {
33  m_farm = a_farm;
34  m_field = a_field;
35  m_ev = a_ev;
36  bool done = false;
37 
38  switch ( m_ev->m_todo )
39  {
40  case osbs_start:
41  {
42  // Set up the date management stuff
43  m_last_date=g_date->DayInYear(10,10);
44  // Start and stop dates for all events after harvest
45  int noDates= 3;
46  m_field->SetMDates(0,0,g_date->DayInYear(25,7));
47  // Determined by harvest date - used to see if at all possible
48  m_field->SetMDates(1,0,g_date->DayInYear(25,8));
49  m_field->SetMDates(0,1,g_date->DayInYear(10,10));
50  m_field->SetMDates(1,1,g_date->DayInYear(10,10));
51  m_field->SetMDates(0,2,g_date->DayInYear(25,7));
52  m_field->SetMDates(1,2,g_date->DayInYear(1,8));
53  // Check the next crop for early start, unless it is a spring crop
54  // in which case we ASSUME that no checking is necessary!!!!
55  // So DO NOT implement a crop that runs over the year boundary
56  if (m_ev->m_startday>g_date->DayInYear(1,7))
57  {
58  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
59  {
60  g_msg->Warn( WARN_BUG, "OSBarleySilage::Do(): "
61  "Harvest too late for the next crop to start!!!", "" );
62  exit( 1 );
63  }
64  // Now fix any late finishing problems
65  for (int i=0; i<noDates; i++)
66  {
67  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
68  m_field->SetMDates(0,i,m_ev->m_startday-1);
69  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
70  m_field->SetMDates(1,i,m_ev->m_startday-1);
71  }
72  }
73  // Now no operations can be timed after the start of the next crop.
74  int d1 = 0;
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, "OSBarleySilage::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 + 365; // 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, "OBarleySilage::Do(): "
94  "Crop start attempt after last possible start date", "" );
95  exit( 1 );
96  }
97  }
98  }
99  // All OK, let's go.
100  SimpleEvent( d1, osbs_ferti_zero, false );
101  }
102  break;
103 
104  case osbs_ferti_zero:
105  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
106  {
107  if (!m_farm->FA_Manure( m_field, 0.0,
108  g_date->DayInYear( 10, 4 ) - g_date->DayInYear()))
109  {
110  SimpleEvent( g_date->Date() + 1, osbs_ferti_zero, true );
111  break;
112  }
113  }
114  SimpleEvent( g_date->Date(),osbs_ferti_one, false );
115  break;
116 
117  case osbs_ferti_one:
118  if ( m_ev->m_lock || m_farm->DoIt( 100 ))
119  {
120  if (!m_farm->FA_Slurry( m_field, 0.0,
121  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
122  SimpleEvent( g_date->Date() + 1, osbs_ferti_one, true );
123  break;
124  }
125  }
126  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 15, 3 ),
127  osbs_spring_plough, false );
128  break;
129 
130  case osbs_spring_plough:
131  if (!m_farm->SpringPlough( m_field, 0.0,
132  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
133  SimpleEvent( g_date->Date() + 1, osbs_spring_plough, true );
134  break;
135  }
136  SimpleEvent( g_date->Date(), osbs_spring_harrow, false );
137  break;
138 
139  case osbs_spring_harrow:
140  if (!m_farm->SpringHarrow( m_field, 0.0,
141  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
142  SimpleEvent( g_date->Date() + 1, osbs_spring_harrow, true );
143  break;
144  }
145  SimpleEvent( g_date->Date(), osbs_spring_roll, false );
146  break;
147 
148  case osbs_spring_roll:
149  if (!m_farm->SpringRoll( m_field, 0.0,
150  g_date->DayInYear( 10, 4 ) - g_date->DayInYear())) {
151  SimpleEvent( g_date->Date() + 1, osbs_spring_roll, true );
152  break;
153  }
154  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20, 3 ),
155  osbs_spring_sow1, false );
156  break;
157 
158  case osbs_spring_sow1:
159  if (!m_farm->SpringSow( m_field, 0.0,
160  g_date->DayInYear( 12, 4 ) - g_date->DayInYear())) {
161  SimpleEvent( g_date->Date() + 1, osbs_spring_sow1, true );
162  break;
163  }
164  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20,3 ),
165  osbs_spring_sow2, false );
166  break;
167 
168  case osbs_spring_sow2:
169  if (!m_farm->SpringSow( m_field, 0.0,
170  g_date->DayInYear( 12, 4 ) - g_date->DayInYear())) {
171  SimpleEvent( g_date->Date() + 1, osbs_spring_sow2, true );
172  break;
173  }
174  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 5,6 ),
175  osbs_water1, false );
176  break;
177 
178  case osbs_water1:
179  if ( m_ev->m_lock || m_farm->DoIt( 10 ))
180  {
181  if (!m_farm->Water( m_field, 0.0,
182  g_date->DayInYear( 15,6 ) - g_date->DayInYear())) {
183  SimpleEvent( g_date->Date() + 1, osbs_water1, true );
184  break;
185  }
186  // Must have watered so do it again
187  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 16,6 ),
188  osbs_water2, false );
189  break;
190  }
191  // Must have failed to water so go straight to cut to silage
192  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 15,7 ),
193  osbs_cut_to_silage, false );
194  break;
195 
196  case osbs_water2:
197  if (!m_farm->Water( m_field, 0.0,
198  g_date->DayInYear( 30,6 ) - g_date->DayInYear())) {
199  SimpleEvent( g_date->Date() + 1, osbs_water2, true );
200  break;
201  }
202  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,2),
203  osbs_cut_to_silage, false );
204  break;
205 
206  case osbs_cut_to_silage:
207  if (!m_farm->CutToSilage( m_field, 0.0,
208  m_field->GetMDates(1,2) - g_date->DayInYear())) {
209  SimpleEvent( g_date->Date() + 1, osbs_cut_to_silage, true );
210  break;
211  }
212  m_field->SetGrowthPhase( harvest1 );
213  done=true;
214  // END OF MAIN THREAD
215  break;
216 
217 
218  default:
219  g_msg->Warn( WARN_BUG, "OSBarleySilage::Do(): "
220  "Unknown event type! ", "" );
221  exit( 1 );
222  }
223 
224  return done;
225 }
226 
227 
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
osbs_ferti_zero
Definition: OSBarleySilage.h:35
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
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::CutToSilage
virtual bool CutToSilage(LE *a_field, double a_user, int a_days)
Cut vegetation for silage on a_field.
Definition: farmfuncs.cpp:2329
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
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
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
osbs_water1
Definition: OSBarleySilage.h:36
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
osbs_spring_roll
Definition: OSBarleySilage.h:41
osbs_spring_plough
Definition: OSBarleySilage.h:39
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
osbs_ferti_one
Definition: OSBarleySilage.h:38
osbs_spring_sow1
Definition: OSBarleySilage.h:43
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
osbs_cut_to_silage
Definition: OSBarleySilage.h:45
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::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
The base class for all farm types.
Definition: farm.h:767
OSBarleySilage::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OSBarleySilage.cpp:31
osbs_water2
Definition: OSBarleySilage.h:37
osbs_spring_harrow
Definition: OSBarleySilage.h:40
osbs_spring_sow2
Definition: OSBarleySilage.h:44
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
osbs_start
Definition: OSBarleySilage.h:34
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