ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OrchardCrop.cpp
Go to the documentation of this file.
1 //
2 // OrchardCrop.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/OrchardCrop.h"
30 
31 //is this ok?
34 extern CfgInt cfg_productapplicstartyear;
35 extern CfgInt cfg_productapplicendyear;
36 extern CfgFloat cfg_ins_app_prop1;
37 extern CfgFloat cfg_pest_product_1_amount;
38 
39 //declared in elements.cpp
40 extern CfgInt cfg_OrchardNoCutsDay;
41 
42 
43 bool OrchardCrop::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
44 {
45  m_farm = a_farm;
46  m_field = a_field;
47  m_ev = a_ev;
48  bool done = false;
49  int d1;
50  int today = g_date->Date();
51  int noDates= 1;
52  int sprayday=cfg_pest_productapplic_startdate.value();
53 
54  switch ( m_ev->m_todo ) {
55  case orch_start:
56  // Set up the date management stuff
57  m_last_date=g_date->DayInYear(1,9);
58  // Start and stop dates for all events after harvest
59  m_field->SetMDates(0,0,g_date->DayInYear(1,9));
60  // Determined by harvest date - used to see if at all possible
61  m_field->SetMDates(1,0,g_date->DayInYear(1,9));
62  // Check the next crop for early start, unless it is a spring crop
63  // in which case we ASSUME that no checking is necessary!!!!
64  // So DO NOT implement a crop that runs over the year boundary
65 
66  //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)
67  if(!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber()>0)){
68 
69  if (m_ev->m_startday>g_date->DayInYear(1,7))
70  {
71  if (m_field->GetMDates(0,0) >=m_ev->m_startday){
72  g_msg->Warn( WARN_BUG, "OrchardCrop::Do(): Harvest too late for the next crop to start!!!", "" );
73  exit( 1 );
74  }
75  // Now fix any late finishing problems
76  for (int i=0; i<noDates; i++) {
77  if(m_field->GetMDates(0,i)>=m_ev->m_startday) {
78  m_field->SetMDates(0,i,m_ev->m_startday-1); //move the starting date
79  }
80  if(m_field->GetMDates(1,i)>=m_ev->m_startday){
81  m_field->SetMConstants(i,0);
82  m_field->SetMDates(1,i,m_ev->m_startday-1); //move the finishing date
83  }
84  }
85  }
86  // Now no operations can be timed after the start of the next crop.
87 
88  d1 = g_date->OldDays() + m_first_date+365; // Add 365 for spring crop
89  if (today > d1) {
90  // Yes too late - should not happen - raise an error
91  g_msg->Warn( WARN_BUG, " OrchardCrop::Do(): Crop start attempt after last possible start date", "" );
92  exit( 1 );
93  }
94  }//if
95  // Reinit d1 to first possible starting date.
96  d1 = g_date->OldDays()+m_first_date;;
97  if ( ! m_ev->m_first_year ) d1+=365; // Add 365 for spring crop (not 1st yr)
98  if ( g_date->Date() > d1 ){
99  d1 = g_date->Date();
100  }
101  // OK, let's go.
102 
103  SimpleEvent( d1, orch_cut, false ); //call cutting orchard
104  SimpleEvent( d1, orch_insecticide1, false ); //call insecticide - so there are two threds
105  break;
106 
107 
108  case orch_cut:
109 
110  switch ( cfg_OrchardNoCutsDay.value() ) {
111  case 99:
112  if ( today == ( sprayday - 7 ) ) m_farm->CutOrch( m_field, 0.0, -1); //so cut no matter what. ok?
113  break;
114  case 4:
115  if ( ( today == 259 ) || ( today == 122 ) || ( today == 92 ) || ( today == 196 ) )
116  m_farm->CutOrch( m_field, 0.0, -1);
117  break;
118  case 3:
119  if ( ( today == 259 ) || ( today == 122 ) || ( today == 92 ) ) m_farm->CutOrch( m_field, 0.0, -1);
120  break;
121  case 2:
122  if ( ( today == 259 ) || ( today == 122 ) ) m_farm->CutOrch( m_field, 0.0, -1);
123  break;
124  case 1:
125  if ( ( today == 259 ) ) m_farm->CutOrch( m_field, 0.0, -1);
126  break;
127  default: // No cut
128  break;
129  }
130 
131  /* if (!m_farm->CutOrch( m_field, 0.0, g_date->DayInYear( 30, 7 ) - g_date->DayInYear())) {
132  SimpleEvent( g_date->Date() + 1, psa_cut_to_hay, false );
133  break;
134  }*/
135  SimpleEvent( g_date->Date(), orch_do_nothing_start, false );
136  break;
137 
139  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 9 ), orch_do_nothing_stop, false );
140  break;
141 
143  done = true;
144  break;
145 
146  case orch_insecticide1:
147  // This will prevent product usage outside the test period
150  if ( m_ev->m_lock || m_farm->DoIt( (int) (100*cfg_ins_app_prop1.value() ))) {
151  if (!m_farm->ProductApplication( m_field, 0.0, cfg_pest_productapplic_startdate.value() - g_date->DayInYear(), cfg_pest_product_1_amount.value(), ppp_1)) {
152  SimpleEvent( g_date->Date() + 1, orch_insecticide1, true );
153  break;
154  }
155  else {
156  if ((cfg_pest_productapplic_startdate.value()<366) && ( cfg_pest_productapplic_startdate.value() <-1))
157  SimpleEvent( g_date->OldDays() + cfg_pest_productapplic_startdate2.value(), orch_insecticide2, false );
158  break;
159  }
160  }
161  break;
162 
163  case orch_insecticide2:
164  if ( m_ev->m_lock || m_farm->DoIt( (int)( 50*cfg_ins_app_prop1.value() ))) {
165  if (!m_farm->ProductApplication( m_field, 0.0, cfg_pest_productapplic_startdate2.value() - g_date->DayInYear(), cfg_pest_product_1_amount.value(), ppp_1)) {
166  SimpleEvent( g_date->Date() + 1, orch_insecticide2, true );
167  break;
168  }
169  }
170  // End of thread
171 
172  ChooseNextCrop (1);
173 
174 
175  break;
176 
177 
178 
179  default:
180  g_msg->Warn( WARN_BUG, "OrchardCrop::Do(): Unknown event type! ", "" );
181  exit( 1 );
182  }
183 
184  return done;
185 }
186 
187 
FarmEvent::m_lock
bool m_lock
Definition: farm.h:465
Farm::ProductApplication
virtual bool ProductApplication(LE *a_field, double a_user, int a_days, double a_applicationrate, PlantProtectionProducts a_ppp)
Apply test pesticide to a_field.
Definition: farmfuncs.cpp:1445
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
cfg_ins_app_prop1
CfgFloat cfg_ins_app_prop1
orch_insecticide2
Definition: OrchardCrop.h:41
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
tof_OptimisingFarm
Definition: farm.h:273
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
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
orch_do_nothing_stop
Definition: OrchardCrop.h:39
cfg_pest_product_1_amount
CfgFloat cfg_pest_product_1_amount
orch_cut
Definition: OrchardCrop.h:42
orch_insecticide1
Definition: OrchardCrop.h:40
Farm::CutOrch
virtual bool CutOrch(LE *a_field, double a_user, int a_days)
Cut vegetation on orchard crop. //based on cut to silage - values from cutting function of orchard.
Definition: farmfuncs.cpp:2362
Landscape::SupplyYearNumber
int SupplyYearNumber(void)
Definition: Landscape.h:1616
cfg_productapplicstartyear
CfgInt cfg_productapplicstartyear
orch_do_nothing_start
Definition: OrchardCrop.h:38
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
ppp_1
Definition: farm.h:422
Crop::m_last_date
int m_last_date
Definition: farm.h:542
Farm
The base class for all farm types.
Definition: farm.h:767
OrchardCrop::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OrchardCrop.cpp:43
cfg_productapplicendyear
CfgInt cfg_productapplicendyear
orch_start
Definition: OrchardCrop.h:37
cfg_OrchardNoCutsDay
CfgInt cfg_OrchardNoCutsDay
cfg_pest_productapplic_startdate2
CfgInt cfg_pest_productapplic_startdate2
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
g_landscape_p
Landscape * g_landscape_p
Definition: Landscape.cpp:258
cfg_pest_productapplic_startdate
CfgInt cfg_pest_productapplic_startdate