ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
PermanentSetAside.cpp
Go to the documentation of this file.
1 //
2 // PermanentSetAside.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/PermanentSetAside.h"
30 
31 
32 bool PermanentSetAside::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
33 {
34  m_farm = a_farm;
35  m_field = a_field;
36  m_ev = a_ev;
37  bool done = false;
38  int d1;
39  int today=g_date->Date();
40  int noDates= 1;
41  switch ( m_ev->m_todo ) {
42  case psa_start:
43 
44  // Set up the date management stuff
45  m_last_date=g_date->DayInYear(1,9);
46  // Start and stop dates for all events after harvest
47  m_field->SetMDates(0,0,g_date->DayInYear(1,9));
48  // Determined by harvest date - used to see if at all possible
49  m_field->SetMDates(1,0,g_date->DayInYear(1,9));
50  // Check the next crop for early start, unless it is a spring crop
51  // in which case we ASSUME that no checking is necessary!!!!
52  // So DO NOT implement a crop that runs over the year boundary
53 
54  //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)
55  if(!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber()>0)){
56 
57  if (m_ev->m_startday>g_date->DayInYear(1,7))
58  {
59  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
60  {
61  g_msg->Warn( WARN_BUG, "PermanentSetAside::Do(): "
62  "Harvest too late for the next crop to start!!!", "" );
63  exit( 1 );
64  }
65  // Now fix any late finishing problems
66  for (int i=0; i<noDates; i++) {
67  if(m_field->GetMDates(0,i)>=m_ev->m_startday) {
68  m_field->SetMDates(0,i,m_ev->m_startday-1); //move the starting date
69  }
70  if(m_field->GetMDates(1,i)>=m_ev->m_startday){
71  m_field->SetMConstants(i,0);
72  m_field->SetMDates(1,i,m_ev->m_startday-1); //move the finishing date
73  }
74  }
75  }
76  // Now no operations can be timed after the start of the next crop.
77 
78  d1 = g_date->OldDays() + m_first_date+365; // Add 365 for spring crop
79  if (today > d1)
80  {
81  // Yes too late - should not happen - raise an error
82  g_msg->Warn( WARN_BUG, " PermanentSetAside::Do(): "
83  "Crop start attempt after last possible start date", "" );
84  exit( 1 );
85  }
86  }//if
87 
88  // Reinit d1 to first possible starting date.
89  d1 = g_date->OldDays()+m_first_date;;
90  if ( ! m_ev->m_first_year ) d1+=365; // Add 365 for spring crop (not 1st yr)
91  if ( g_date->Date() > d1 )
92  {
93  d1 = g_date->Date();
94  }
95  // OK, let's go.
96  m_field->SetLastSownVeg( m_field->GetVegType() ); //Force last sown, needed for goose habitat classification
97 
98  // SimpleEvent( d1, psa_do_nothing_start, false );
99 
100  // ***CJT*** altered 16 August 2004 to be more similar to non-managed set-aside
101  // SimpleEvent( d1, sa_wait, false );
102  // break;
103 
104  SimpleEvent( d1, psa_cut_to_hay, false );
105  break;
106 
107  case psa_cut_to_hay:
108  if (!m_farm->CutToHay( m_field, 0.0,
109  g_date->DayInYear( 30, 7 ) - g_date->DayInYear())) {
110  SimpleEvent( g_date->Date() + 1, psa_cut_to_hay, false );
111  break;
112  }
113 
114  ChooseNextCrop (1);
115 
116  SimpleEvent( g_date->Date(), psa_do_nothing_start, false );
117  break;
118 
120  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 9 ),
121  psa_do_nothing_stop, false );
122  break;
123 
124  case psa_do_nothing_stop:
125  done = true;
126  break;
127 
128  default:
129  g_msg->Warn( WARN_BUG, "PermanentSetAside::Do(): "
130  "Unknown event type! ", "" );
131  exit( 1 );
132  }
133 
134  return done;
135 }
136 
137 
psa_do_nothing_stop
Definition: PermanentSetAside.h:36
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
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
Farm::CutToHay
virtual bool CutToHay(LE *a_field, double a_user, int a_days)
Carry out hay cutting on a_field.
Definition: farmfuncs.cpp:2269
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
psa_do_nothing_start
Definition: PermanentSetAside.h:35
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
Crop::m_last_date
int m_last_date
Definition: farm.h:542
psa_start
Definition: PermanentSetAside.h:34
Farm
The base class for all farm types.
Definition: farm.h:767
psa_cut_to_hay
Definition: PermanentSetAside.h:37
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
PermanentSetAside::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: PermanentSetAside.cpp:32