ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
NorwegianSpringBarley.cpp
Go to the documentation of this file.
1 //
2 // NorwegianSpringBarley.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, Christopher John Topping, Aarhus University
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/NorwegianSpringBarley.h"
30 #include "math.h"
31 
32 
33 bool NorwegianSpringBarley::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  int d1;
39 
40  bool done = false;
41 
42  switch (m_ev->m_todo)
43  {
44  case no_sb_start:
45  {
46 
47  // Set up the date management stuff
48  // Could save the start day in case it is needed later
49  // m_field->m_startday = m_ev->m_startday;
50  m_last_date = g_date->DayInYear(30, 8);
51  // Start and stop dates for all events after harvest
52  int noDates = 2;
53  m_field->SetMDates(0, 0, g_date->DayInYear(20, 8));
54  // Determined by harvest date - used to see if at all possible
55  m_field->SetMDates(1, 0, g_date->DayInYear(10, 8));
56  m_field->SetMDates(0, 1, g_date->DayInYear(10, 8));
57  m_field->SetMDates(1, 1, g_date->DayInYear(30, 8));
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  if (m_ev->m_startday > g_date->DayInYear(1, 7))
63  {
64  if (m_field->GetMDates(0, 0) >= m_ev->m_startday)
65  {
66  g_msg->Warn(WARN_BUG, "NorwegianSpringBarley::Do(): "
67  "Harvest too late for the next crop to start!!!", "");
68  exit(1);
69  }
70  // Now fix any late finishing problems
71  for (int i = 0; i < noDates; i++) {
72  if (m_field->GetMDates(0, i) >= m_ev->m_startday) {
73  m_field->SetMDates(0, i, m_ev->m_startday - 1); //move the starting date
74  }
75  if (m_field->GetMDates(1, i) >= m_ev->m_startday){
76  m_field->SetMConstants(i, 0);
77  m_field->SetMDates(1, i, m_ev->m_startday - 1); //move the finishing date
78  }
79  }
80  }
81  // Now no operations can be timed after the start of the next crop.
82 
83  if (!m_ev->m_first_year)
84  {
85  int today = g_date->Date();
86  // Are we before July 1st?
87  d1 = g_date->OldDays() + g_date->DayInYear(1, 7);
88  if (today < d1)
89  {
90  // Yes, too early. We assumme this is because the last crop was late
91  g_msg->Warn(WARN_BUG, "NorwegianSpringBarley::Do(): " "Crop start attempt between 1st Jan & 1st July", "");
92  exit(1);
93  }
94  else
95  {
96  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
97  if (today > d1)
98  {
99  // Yes too late - should not happen - raise an error
100  g_msg->Warn(WARN_BUG, "NorwegianSpringBarley::Do(): " "Crop start attempt after last possible start date", "");
101  exit(1);
102  }
103  }
104  }
105  else
106  {
107  SimpleEvent(g_date->OldDays() + g_date->DayInYear(1, 4), no_sb_spring_plough, false);
108  break;
109  }
110 
111 
112  // End single block date checking code. Please see next line
113  // comment as well.
114  // Reinit d1 to first possible starting date.
115  d1 = g_date->OldDays() + g_date->DayInYear(2, 11);
116  if (g_date->Date() > d1) {
117  d1 = g_date->Date();
118  }
119  // OK, let's go.
120  SimpleEvent(d1, no_sb_autumn_plough, false);
121  }
122  break;
123 
124  case no_sb_autumn_plough:
125  if (m_ev->m_lock || m_farm->DoIt(70)) // was 70
126  {
127  if (!m_farm->AutumnPlough(m_field, 0.0,
128  g_date->DayInYear(30, 11) - g_date->DayInYear())) {
129  SimpleEvent(g_date->Date() + 1, no_sb_autumn_plough, true);
130  break;
131  }
133  }
134  SimpleEvent(g_date->OldDays() + g_date->DayInYear(20, 3) + 365, no_sb_spring_plough, false);
135  break;
136 
137  case no_sb_spring_plough:
139  {
140  if (!m_farm->SpringPlough(m_field, 0.0,
141  g_date->DayInYear(10, 4) - // was 10,4
142  g_date->DayInYear())) {
143  SimpleEvent(g_date->Date() + 1, no_sb_spring_plough, true);
144  break;
145  }
146  }
147  {
148  d1 = g_date->Date();
149  if (d1 < g_date->OldDays() + g_date->DayInYear(20, 3)) {
150  d1 = g_date->OldDays() + g_date->DayInYear(20, 3);
151  }
152  SimpleEvent(d1, no_sb_spring_harrow, false);
153  }
154  break;
155 
156  case no_sb_spring_harrow:
157  if (!m_farm->SpringHarrow(m_field, 0.0,
158  g_date->DayInYear(10, 4) - g_date->DayInYear())) { // WAS 10,4
159  SimpleEvent(g_date->Date() + 1, no_sb_spring_harrow, true);
160  break;
161  }
162  d1 = g_date->Date();
163  if (d1 < g_date->OldDays() + g_date->DayInYear(25, 3)) {
164  d1 = g_date->OldDays() + g_date->DayInYear(25, 3);
165  }
166  SimpleEvent(d1, no_sb_spring_sow, false);
167  break;
168 
169  case no_sb_spring_sow:
170  if (!m_farm->SpringSow(m_field, 0.0,
171  g_date->DayInYear(10, 4) - g_date->DayInYear())) { // WAS 10,4
172  SimpleEvent(g_date->Date() + 1, no_sb_spring_sow, true);
173  break;
174  }
175  {
176  d1 = g_date->Date();
177  if (d1 < g_date->OldDays() + g_date->DayInYear(5, 4)) {
178  d1 = g_date->OldDays() + g_date->DayInYear(5, 4);
179  }
180  SimpleEvent(d1, no_sb_spring_roll, false);
181  }
182  break;
183 
184  case no_sb_spring_roll:
185  if (m_ev->m_lock || m_farm->DoIt(0)) // was 30
186  {
187  if (!m_farm->SpringRoll(m_field, 0.0,
188  g_date->DayInYear(20, 4) - g_date->DayInYear())) {
189  SimpleEvent(g_date->Date() + 1, no_sb_spring_roll, true);
190  break;
191  }
192  }
193  ChooseNextCrop(2);
194  SimpleEvent(g_date->OldDays() + g_date->DayInYear(1, 8), no_sb_harvest, false);
195  break;
196 
197 
198  case no_sb_harvest:
199  if (!m_farm->Harvest(m_field, 0.0, g_date->DayInYear(20, 8) - g_date->DayInYear())) {
200  SimpleEvent(g_date->Date() + 1, no_sb_harvest, true);
201  break;
202  }
203  SimpleEvent(g_date->Date(),no_sb_haybailing, false);
204  break;
205 
206  case no_sb_haybailing:
207  if (m_field->GetMConstants(0) == 0) {
208  if (!m_farm->HayBailing(m_field, 0.0, -1)) { //raise an error
209  g_msg->Warn(WARN_BUG, "NOSpringBarley::Do(): failure in 'HayBailing' execution", "");
210  exit(1);
211  }
212  }
213  else {
214  if (!m_farm->HayBailing(m_field, 0.0, m_field->GetMDates(1, 0) - g_date->DayInYear())) {
215  SimpleEvent(g_date->Date() + 1, no_sb_haybailing, true);
216  break;
217  }
218  }
219  // END MAIN THREAD
220  done = true;
221  break;
222 
223  default:
224  g_msg->Warn(WARN_BUG, "NorwegianSpringBarley::Do(): "
225  "Unknown event type! ", "");
226  exit(1);
227  }
228  return done;
229 }
230 
231 
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::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::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
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
no_sb_spring_plough
Definition: NorwegianSpringBarley.h:37
no_sb_harvest
Definition: NorwegianSpringBarley.h:41
no_sb_start
Definition: NorwegianSpringBarley.h:35
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
no_sb_spring_sow
Definition: NorwegianSpringBarley.h:39
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
no_sb_haybailing
Definition: NorwegianSpringBarley.h:42
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
NorwegianSpringBarley::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: NorwegianSpringBarley.cpp:33
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
no_sb_spring_harrow
Definition: NorwegianSpringBarley.h:38
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
no_sb_spring_roll
Definition: NorwegianSpringBarley.h:40
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::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
no_sb_autumn_plough
Definition: NorwegianSpringBarley.h:36
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
NO_SB_DID_AUTUMN_PLOUGH
#define NO_SB_DID_AUTUMN_PLOUGH
Definition: NorwegianSpringBarley.h:32
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