ALMaSS Vole ODDox  1.1
The vole model description following ODdox protocol
NorwegianOats.cpp
Go to the documentation of this file.
1 //
2 // NorwegianOats.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2016, 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/NorwegianOats.h"
30 #include "math.h"
31 
32 
33 bool NorwegianOats::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_oats_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  char veg_type[20];
67  sprintf(veg_type, "%d", m_ev->m_next_tov);
68  g_msg->Warn(WARN_BUG, "NorwegianOats::Do(): : Harvest too late for the next crop to start!!! The next crop is: ", veg_type);
69  exit(1);
70  }
71  // Now fix any late finishing problems
72  for (int i = 0; i < noDates; i++) {
73  if (m_field->GetMDates(0, i) >= m_ev->m_startday) {
74  m_field->SetMDates(0, i, m_ev->m_startday - 1); //move the starting date
75  }
76  if (m_field->GetMDates(1, i) >= m_ev->m_startday){
77  m_field->SetMConstants(i, 0);
78  m_field->SetMDates(1, i, m_ev->m_startday - 1); //move the finishing date
79  }
80  }
81  }
82  // Now no operations can be timed after the start of the next crop.
83 
84  if (!m_ev->m_first_year)
85  {
86  int today = g_date->Date();
87  // Are we before July 1st?
88  d1 = g_date->OldDays() + g_date->DayInYear(1, 7);
89  if (today < d1)
90  {
91  // Yes, too early. We assumme this is because the last crop was late
92  g_msg->Warn(WARN_BUG, "NorwegianOats::Do(): " "Crop start attempt between 1st Jan & 1st July", "");
93  exit(1);
94  }
95  else
96  {
97  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
98  if (today > d1)
99  {
100  // Yes too late - should not happen - raise an error
101  char veg_type[20];
102  sprintf(veg_type, "%d", m_ev->m_field->GetPreviousCrop(m_ev->m_field->GetRotIndex()));
103  g_msg->Warn(WARN_BUG, "NorwegianOats::Do(): " "Crop start attempt after last possible start date", veg_type);
104  exit(1);
105  }
106  }
107  }
108  else
109  {
110  SimpleEvent(g_date->OldDays() + g_date->DayInYear(1, 4), no_oats_spring_plough, false);
111  break;
112  }
113 
114 
115  // End single block date checking code. Please see next line
116  // comment as well.
117  // Reinit d1 to first possible starting date.
118  d1 = g_date->OldDays() + g_date->DayInYear(7, 9);
119  if (g_date->Date() > d1) {
120  d1 = g_date->Date();
121  }
122  // OK, let's go.
124  }
125  break;
126 
128  if (m_ev->m_lock || m_farm->DoIt(70)) // was 70
129  {
130  if (!m_farm->AutumnPlough(m_field, 0.0, g_date->DayInYear(07, 11) - g_date->DayInYear())) {
131  SimpleEvent(g_date->Date() + 1, no_oats_autumn_plough, true);
132  break;
133  }
135  }
136  SimpleEvent(g_date->OldDays() + g_date->DayInYear(21, 4) + 365, no_oats_spring_plough, false);
137  break;
138 
141  {
142  if (!m_farm->SpringPlough(m_field, 0.0, g_date->DayInYear(1, 5) - g_date->DayInYear())) {
143  SimpleEvent(g_date->Date() + 1, no_oats_spring_plough, true);
144  break;
145  }
146  }
147  {
148  d1 = g_date->Date();
149  if (d1 < g_date->OldDays() + g_date->DayInYear(1, 5)) {
150  d1 = g_date->OldDays() + g_date->DayInYear(1, 5);
151  }
153  }
154  break;
155 
157  if (!m_farm->SpringHarrow(m_field, 0.0,
158  g_date->DayInYear(10, 5) - g_date->DayInYear())) {
159  SimpleEvent(g_date->Date() + 1, no_oats_spring_harrow, true);
160  break;
161  }
162  d1 = g_date->Date();
163  if (d1 < g_date->OldDays() + g_date->DayInYear(1, 5)) {
164  d1 = g_date->OldDays() + g_date->DayInYear(1, 5);
165  }
166  SimpleEvent(d1, no_oats_spring_sow, false);
167  break;
168 
169  case no_oats_spring_sow:
170  if (!m_farm->SpringSow(m_field, 0.0,
171  g_date->DayInYear(17, 5) - g_date->DayInYear())) {
172  SimpleEvent(g_date->Date() + 1, no_oats_spring_sow, true);
173  break;
174  }
175  {
176  int d1 = g_date->Date();
177  SimpleEvent(d1, no_oats_spring_roll, false);
178  }
179  break;
180 
181  case no_oats_spring_roll:
182  if (m_ev->m_lock || m_farm->DoIt(50))
183  {
184  if (!m_farm->SpringRoll(m_field, 0.0,
185  g_date->DayInYear(20, 5) - g_date->DayInYear())) {
186  SimpleEvent(g_date->Date() + 1, no_oats_spring_roll, true);
187  break;
188  }
189  }
190  ChooseNextCrop(2);
191  SimpleEvent(g_date->OldDays() + g_date->DayInYear(1, 8), no_oats_harvest, false);
192  break;
193 
194 
195  case no_oats_harvest:
196  if (m_field->GetVegDDegs() >= m_ddegstoharvest) {
197  if (!m_farm->Harvest(m_field, 0.0, -1)) {
198  SimpleEvent(g_date->Date() + 1, no_oats_harvest, true);
199  break;
200  }
201  }
202  SimpleEvent(g_date->Date() + 14, no_oats_haybailing, false);
203  break;
204 
205  case no_oats_haybailing:
206  if (!m_farm->HayBailing(m_field, 0.0, m_field->GetMDates(1, 0) - g_date->DayInYear())) {
207  SimpleEvent(g_date->Date() + 1, no_oats_haybailing, true);
208  break;
209  }
210  // END MAIN THREAD
211  done = true;
212  break;
213 
214  default:
215  g_msg->Warn(WARN_BUG, "NorwegianOats::Do(): "
216  "Unknown event type! ", "");
217  exit(1);
218  }
219  return done;
220 }
221 
222 
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
no_oats_haybailing
Definition: NorwegianOats.h:42
Crop::m_ddegstoharvest
int m_ddegstoharvest
Definition: farm.h:543
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
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
no_oats_spring_sow
Definition: NorwegianOats.h:39
no_oats_autumn_plough
Definition: NorwegianOats.h:36
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
no_oats_start
Definition: NorwegianOats.h:35
NO_OATS_DID_AUTUMN_PLOUGH
#define NO_OATS_DID_AUTUMN_PLOUGH
Definition: NorwegianOats.h:32
FarmEvent::m_field
LE * m_field
Definition: farm.h:472
FarmEvent::m_next_tov
TTypesOfVegetation m_next_tov
Definition: farm.h:471
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
no_oats_spring_plough
Definition: NorwegianOats.h:37
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
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_oats_spring_roll
Definition: NorwegianOats.h:40
no_oats_harvest
Definition: NorwegianOats.h:41
NorwegianOats::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: NorwegianOats.cpp:33
no_oats_spring_harrow
Definition: NorwegianOats.h:38
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
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