ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
NLCatchPeaCrop.cpp
Go to the documentation of this file.
1 /*
2 *******************************************************************************************************
3 Copyright (c) 2017, Christopher John Topping, Aarhus University
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided
7 that the following conditions are met:
8 
9 Redistributions of source code must retain the above copyright notice, this list of conditions and the
10 following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12 the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
17 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 ********************************************************************************************************
23 */
38 //
39 // NLCatchPeaCrop.cpp
40 //
41 
42 
43 #include "../../Landscape/ls.h"
44 #include "../../Landscape/cropprogs/NLCatchPeaCrop.h"
45 
46 extern CfgBool g_farm_fixed_crop_enable;
47 
56 bool NLCatchPeaCrop::Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
57 {
58  bool done = false; // The boolean value done indicates when we are totally finished with this plan (i.e. it is set to true).
59  int d1 = 0;
60  // Depending what event has occured jump to the correct bit of code
61  switch (a_ev->m_todo)
62  {
63  case nl_cpc_start:
64  {
65 
66  // Set up the date management stuff
67 
68  // Check the next crop for early start, unless it is a spring crop
69  // in which case we ASSUME that no checking is necessary!!!!
70  // So DO NOT implement a crop that runs over the year boundary (i.e. from spring to spring!), at least not without fixing this.
71 
72  //new if: do the check only for non-optimising farms and if year>0. (030713 - m_rotation used only in the hidden year, so I modified the condition from >7 to >0)
73  //optimising farms not used for now so most of related code is removed (but not in 'start' case)
74  if (!(a_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber() > 0)) {
75 
76  if (a_ev->m_startday > g_date->DayInYear(1, 7)) {
77  if (a_field->GetMDates(0, 0) >= a_ev->m_startday)
78  {
79  g_msg->Warn(WARN_BUG, "NLCatchPeaCrop::Do(): ", "Harvest too late for the next crop to start!!!");
80  int almassnum = g_landscape_p->BackTranslateVegTypes(a_ev->m_next_tov);
81  g_msg->Warn("Next Crop ", (double)almassnum); // this causes exit
82  }
83  }
84  // Now no operations can be timed after the start of the next crop.
85 
86  if (!a_ev->m_first_year) {
87  // Are we before July 1st?
88  d1 = g_date->OldDays() + g_date->DayInYear(1, 7);
89  // We need to skip this test if its a catch crop that preceded
90 
91  if ((g_date->Date() < d1) && (!g_farm_fixed_crop_enable.value())) { // condition preventing from getting errors regarding looping catch crop
92  // Yes, too early. We assumme this is because the last crop was late
93  printf("Poly: %d\n", a_field->GetPoly());
94  g_msg->Warn(WARN_BUG, "NLCatchPeaCrop::Do(): ", "Crop start attempt between 1st Jan & 1st July");
95  int prev = g_landscape_p->BackTranslateVegTypes(a_field->GetOwner()->GetPreviousCrop(a_field->GetRotIndex()));
96  g_msg->Warn(WARN_BUG, "Previous Crop ", prev);
97  int almassnum = g_landscape_p->BackTranslateVegTypes(a_ev->m_next_tov);
98  g_msg->Warn("Next Crop ", (double)almassnum); // this causes exit
99  }
100  else {
101  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
102  if (g_date->Date() > d1) {
103  // Yes too late - should not happen - raise an error
104  g_msg->Warn(WARN_BUG, "NLCatchPeaCrop::Do(): ", "Crop start attempt after last possible start date");
105  g_msg->Warn(WARN_BUG, "Previous Crop ", "");
106  a_field->GetOwner()->GetPreviousCrop(a_field->GetRotIndex());
107  int almassnum = g_landscape_p->BackTranslateVegTypes(a_ev->m_next_tov);
108  g_msg->Warn("Next Crop ", (double)almassnum); // this causes exit
109  }
110  }
111  }
112  else {
113  // Is the first year
114  // Some special code to cope with that first start-up year in ALMaSS - ignore for all practical purposes
115  // Code for first spring treatment used
116  SimpleEvent_(g_date->OldDays() + g_date->DayInYear(1, 8), nl_cpc_stubble_cultivator, false, a_farm, a_field);
117  break;
118  }
119  }//if
120 
121  // End single block date checking code. Please see next line comment as well.
122  // Reinit d1 to first possible starting date.
123  d1 = g_date->OldDays() + g_date->DayInYear(1, 8);
124  // OK, let's go.
125  // Here we queue up the first event - this differs depending on whether we have a
126  // stock or arable farmer
127  SimpleEvent_(d1, nl_cpc_stubble_cultivator, false, a_farm, a_field);
128  }
129  break;
130 
131  // This is the first real farm operation
133  if (!a_farm->StubbleHarrowing(a_field, 0.0, g_date->DayInYear(10, 10) - g_date->DayInYear())) {
134  SimpleEvent_(g_date->Date() + 1, nl_cpc_stubble_cultivator, true, a_farm, a_field);
135  break;
136  }
137  if (a_farm->IsStockFarmer()) //Stock Farmer
138  {
139  SimpleEvent_(g_date->Date() + 5, nl_cpc_ferti_s1, false, a_farm, a_field);
140  }
141  else SimpleEvent_(g_date->Date() + 5, nl_cpc_ferti_p1, false, a_farm, a_field);
142  break;
143 
144  case nl_cpc_ferti_p1:
145  if (!a_farm->FP_Slurry(a_field, 0.0, g_date->DayInYear(15, 10) - g_date->DayInYear())) {
146  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_p1, true, a_farm, a_field);
147  break;
148  }
149  SimpleEvent_(g_date->Date() + 1, nl_cpc_preseeding_cultivator_with_sow, false, a_farm, a_field);
150  break;
151  case nl_cpc_ferti_s1:
152  if (!a_farm->FA_Slurry(a_field, 0.0, g_date->DayInYear(15, 10) - g_date->DayInYear())) {
153  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_s1, true, a_farm, a_field);
154  break;
155  }
156  SimpleEvent_(g_date->Date() + 1, nl_cpc_preseeding_cultivator_with_sow, false, a_farm, a_field);
157  break;
159  if (!a_farm->PreseedingCultivatorSow(a_field, 0.0, g_date->DayInYear(16, 10) - g_date->DayInYear())) {
160  SimpleEvent_(g_date->Date() + 1, nl_cpc_preseeding_cultivator_with_sow, true, a_farm, a_field);
161  break;
162  }
163  d1 = g_date->Date() + 14;
164  if (d1 < g_date->OldDays() + g_date->DayInYear(1, 9)) {
165  d1 = g_date->OldDays() + g_date->DayInYear(1, 9);
166  }
167  if (a_field->GetSoilType() == 2 || a_field->GetSoilType() == 6) { // on sandy soils (NL ZAND & LOSS)
168  SimpleEvent_(g_date->OldDays() + g_date->DayInYear(15, 2) + 365, nl_cpc_ferti_s2_sandy, false, a_farm, a_field);
169  }
170  else SimpleEvent_(d1, nl_cpc_ferti_s2_clay, false, a_farm, a_field);
171  break;
173  if (a_farm->IsStockFarmer())
174  {
175  if (!a_farm->FA_Manure(a_field, 0.0, g_date->DayInYear(25, 10) - g_date->DayInYear())) {
176  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_s2_clay, true, a_farm, a_field);
177  break;
178  }
179  d1 = g_date->Date() + 40;
180  if (d1 < g_date->OldDays() + g_date->DayInYear(15, 10)) {
181  d1 = g_date->OldDays() + g_date->DayInYear(15, 10);
182  }
183  SimpleEvent_(d1, nl_cpc_winter_plough_clay, false, a_farm, a_field);
184  break;
185  }
186  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_p2_clay, false, a_farm, a_field);
187  break;
189  if (!a_farm->FP_Manure(a_field, 0.0, g_date->DayInYear(25, 10) - g_date->DayInYear())) {
190  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_p2_clay, true, a_farm, a_field);
191  break;
192  }
193  d1 = g_date->Date() + 40;
194  if (d1 < g_date->OldDays() + g_date->DayInYear(15, 10)) {
195  d1 = g_date->OldDays() + g_date->DayInYear(15, 10);
196  }
197  SimpleEvent_(d1, nl_cpc_winter_plough_clay, false, a_farm, a_field);
198  break;
200  if (!a_farm->WinterPlough(a_field, 0.0, g_date->DayInYear(15, 12) - g_date->DayInYear())) {
201  SimpleEvent_(g_date->Date() + 1, nl_cpc_winter_plough_clay, true, a_farm, a_field);
202  break;
203  }
204  // The plan is finished on clay soils (catch crop in incorporated into soil on autumn)
205  // Calling sleep_all_day to move to the next year
206  SimpleEvent_(g_date->OldDays() + g_date->DayInYear(1, 1) + 365, nl_cpc_sleep_all_day, false, a_farm, a_field);
207  break;
209  if (a_farm->IsStockFarmer())
210  {
211  if (!a_farm->FA_Manure(a_field, 0.0, g_date->DayInYear(15, 3) - g_date->DayInYear())) {
212  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_s2_sandy, true, a_farm, a_field);
213  break;
214  }
215  // The plan is finished on sandy soils; spring plough (to incorporate catch crop into soil) will follow but it is called from the next crop
216  done = true;
217  break;
218  }
219  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_p2_sandy, false, a_farm, a_field);
220  break;
222  if (!a_farm->FP_Manure(a_field, 0.0, g_date->DayInYear(15, 3) - g_date->DayInYear())) {
223  SimpleEvent_(g_date->Date() + 1, nl_cpc_ferti_p2_sandy, true, a_farm, a_field);
224  break;
225  }
226  // The plan is finished on sandy soils; spring plough (to incorporate catch crop into soil) will follow but it is called from the next crop
227  done = true;
228  break;
230  if (!a_farm->SleepAllDay(a_field, 0.0, g_date->DayInYear(15, 3) - g_date->DayInYear())) {
231  SimpleEvent_(g_date->Date() + 1, nl_cpc_sleep_all_day, true, a_farm, a_field);
232  break;
233  }
234  done = true;
235  break;
236 
237 
238  default:
239  g_msg->Warn(WARN_BUG, "NLCatchPeaCrop::Do(): "
240  "Unknown event type! ", "");
241  exit(1);
242  }
243  return done;
244 }
Farm::IsStockFarmer
bool IsStockFarmer(void)
Definition: farm.h:905
nl_cpc_ferti_s2_sandy
Definition: NLCatchPeaCrop.h:42
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
nl_cpc_ferti_s2_clay
Definition: NLCatchPeaCrop.h:39
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
tof_OptimisingFarm
Definition: farm.h:273
Landscape::BackTranslateVegTypes
int BackTranslateVegTypes(TTypesOfVegetation VegReference)
Definition: Landscape.h:1669
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
nl_cpc_ferti_s1
Definition: NLCatchPeaCrop.h:36
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
nl_cpc_sleep_all_day
Definition: NLCatchPeaCrop.h:33
nl_cpc_start
Definition: NLCatchPeaCrop.h:32
nl_cpc_winter_plough_clay
Definition: NLCatchPeaCrop.h:40
FarmEvent::m_next_tov
TTypesOfVegetation m_next_tov
Definition: farm.h:471
nl_cpc_stubble_cultivator
Definition: NLCatchPeaCrop.h:34
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
nl_cpc_ferti_p2_clay
Definition: NLCatchPeaCrop.h:38
Farm
The base class for all farm types.
Definition: farm.h:767
nl_cpc_ferti_p1
Definition: NLCatchPeaCrop.h:35
nl_cpc_preseeding_cultivator_with_sow
Definition: NLCatchPeaCrop.h:37
NLCatchPeaCrop::Do
virtual bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
The one and only method for a crop management plan. All farm actions go through here.
Definition: NLCatchPeaCrop.cpp:56
nl_cpc_ferti_p2_sandy
Definition: NLCatchPeaCrop.h:41
g_farm_fixed_crop_enable
CfgBool g_farm_fixed_crop_enable
g_landscape_p
Landscape * g_landscape_p
Definition: Landscape.cpp:258
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
Crop::SimpleEvent_
void SimpleEvent_(long a_date, int a_todo, bool a_lock, Farm *a_farm, LE *a_field)
Adds an event to this crop management without relying on member variables.
Definition: farm.cpp:312