Goose Management Model ODdox  1.02
Weather Class Reference

#include <weather.h>

Public Member Functions

double GetDDDegs (long a_date)
 
double GetGlobalRadiation (long a_date)
 
double GetGlobalRadiation (void)
 
double GetHumidity (void)
 Get the humidity score today. More...
 
double GetMeanTemp (long a_date, unsigned int a_period)
 
double GetRain (long a_date)
 
double GetRain (void)
 
double GetRainPeriod (long a_date, unsigned int a_period)
 
bool GetSnow (long a_date)
 
bool GetSnow (void)
 
double GetSnowDepth (void)
 Get the current snow depth. More...
 
double GetTemp (long a_date)
 Get the temperature on a particular date. More...
 
double GetTemp (void)
 Get the temperature today. More...
 
double GetTempPeriod (long a_date, unsigned int a_period)
 
double GetWind (long a_date)
 
double GetWind (void)
 
int GetWindDirection (void)
 
double GetWindPeriod (long a_date, unsigned int a_period)
 
bool Raining (void)
 
void Tick (void)
 
 Weather (const char *a_weatherdatafile)
 
 ~Weather (void)
 

Private Attributes

long m_datemodulus
 
double m_humiditytoday
 
double m_insolation
 
vector< double > m_rain
 
bool m_rainingtoday
 
double m_raintoday
 
double m_snowdepth
 The snow depth in cm. More...
 
bool m_snowtoday
 
vector< double > m_temp
 
double m_temptoday
 
vector< double > m_wind
 
bool m_wind_valid
 
vector< double > m_winddir
 
bool m_winddir_valid
 
int m_winddirtoday
 
double m_windtoday
 

Detailed Description

Definition at line 385 of file weather.h.

Constructor & Destructor Documentation

◆ Weather()

Weather::Weather ( const char *  a_weatherdatafile)

Definition at line 167 of file weather.cpp.

168 {
169  int NoDays, Day, Month, Year, FirstYear=0, LastYear=0;
170  double Temp, Rain, Wind;
171 
172  ifstream inFile(a_weatherdatafile);
173  if (!inFile) {
174  g_msg->Warn(WARN_FILE, "Weather::Weather(): Unable to open file",
175  a_weatherdatafile );
176  exit(1);
177  }
178 
179  string buf;
180  std::getline(inFile,buf);
181  NoDays=std::stoi(buf);
182 
183 
184  m_rain.resize( NoDays );
185  m_wind.resize( NoDays );
186  m_temp.resize( NoDays );
187 
188  bool storing = false; // Whether we are storing weather data.
189  unsigned int index = 0;
190 
191  g_date->SetLastYear( 0 );
192 
193 
194  for ( int i=0; i<NoDays; i++) {
195  inFile >> Year >> Month >> Day >> Temp >> Wind >> Rain;
196 
197  if ( Month == 2 && Day == 29 ) {
198  // Skip leap days.
199  continue;
200  }
201 
202  if ( Month == 1 && Day == 1 && !storing &&
203  (
206  )
207  ) {
208  // Commence storing of data from Jan. 1st of the first
209  // year requested.
210  storing = true;
211  g_date->SetFirstYear( Year );
212  FirstYear = Year;
213  g_date->Reset();
214  }
215 
216 
217  if ( storing ) {
218  //if (Temp > 0.0 )
219  // DDegs += Temp;
220  m_rain[ index ] = Rain;
221  m_wind[ index ] = Wind;
222  m_temp[ index ] = Temp;
223  index++;
224  }
225 
226  //cout << "i: " << i << " index: " << index << " Month: " << Month << " Year: " << Year <<"\n";
227 
228  if ( (Month == 12) && (Day == 31) && (storing) ) {
229  cout << "LastYear: " << Year << "\n";
230  // Found yet another full year worth of weather data.
231  g_date->SetLastYear( Year );
232  LastYear = Year;
233  }
234  }
235 
236  // Did we find at least one full year worth of data?
237  if ( g_date->GetLastYear() == 0 ) {
238  // Nope...
239  g_msg->Warn(WARN_FILE, "Weather::Weather(): Weather data file did",
240  "not contain at least one year of data!" );
241  exit(1);
242  }
243  //fclose( ifile );
244  m_datemodulus = (LastYear - FirstYear + 1)*365;
245  m_snowdepth = 0;
246  Tick();
247 }

References g_date, g_msg, Calendar::GetLastYear(), l_weather_starting_year, m_datemodulus, m_rain, m_snowdepth, m_temp, m_wind, Calendar::Reset(), Calendar::SetFirstYear(), Calendar::SetLastYear(), Tick(), CfgInt::value(), MapErrorMsg::Warn(), and WARN_FILE.

◆ ~Weather()

Weather::~Weather ( void  )

Definition at line 249 of file weather.cpp.

250 {
251 }

Member Function Documentation

◆ GetDDDegs()

double Weather::GetDDDegs ( long  a_date)

Definition at line 155 of file weather.cpp.

156 {
157  double temp = m_temp[ a_date%m_datemodulus ];
158  if ( temp < 0.0 ) {
159  temp = 0.0;
160  }
161 
162  return temp;
163 }

References m_datemodulus, and m_temp.

Referenced by VegElement::DoDevelopment(), UnsprayedFieldMargin::DoDevelopment(), and VegElement::SetGrowthPhase().

◆ GetGlobalRadiation() [1/2]

double Weather::GetGlobalRadiation ( long  a_date)
inline

Definition at line 414 of file weather.h.

414 { return (double)c_insolation[a_date % m_datemodulus]; }

References c_insolation.

Referenced by Landscape::SupplyGlobalRadiation().

◆ GetGlobalRadiation() [2/2]

double Weather::GetGlobalRadiation ( void  )
inline

Definition at line 415 of file weather.h.

415 { return m_insolation; }

◆ GetHumidity()

double Weather::GetHumidity ( void  )
inline

Get the humidity score today.

Definition at line 421 of file weather.h.

421 { return m_humiditytoday; }

Referenced by Landscape::SupplyHumidity().

◆ GetMeanTemp()

double Weather::GetMeanTemp ( long  a_date,
unsigned int  a_period 
)

Definition at line 120 of file weather.cpp.

121 {
122  double sum = 0.0;
123 
124  for ( unsigned int i=0; i<a_period; i++ )
125  sum += GetTemp( a_date - i );
126 
127  return sum/(double)a_period;
128 }

References GetTemp().

Referenced by Landscape::SupplyMeanTemp().

◆ GetRain() [1/2]

double Weather::GetRain ( long  a_date)
inline

Definition at line 423 of file weather.h.

423 { return m_rain[a_date %m_datemodulus]; }

Referenced by Landscape::SupplyRain().

◆ GetRain() [2/2]

double Weather::GetRain ( void  )
inline

Definition at line 424 of file weather.h.

424 { return m_raintoday; }

Referenced by GetRainPeriod(), and Tick().

◆ GetRainPeriod()

double Weather::GetRainPeriod ( long  a_date,
unsigned int  a_period 
)

Definition at line 253 of file weather.cpp.

254 {
255  double sum = 0.0;
256 
257  for ( unsigned int i=0; i<a_period; i++ )
258  sum += GetRain( a_date - i );
259  return sum;
260 }

References GetRain().

Referenced by Farm::BulbHarvest(), Farm::BurnStrawStubble(), Farm::CutToHay(), Farm::Harvest(), OptimisingFarm::Harvest(), Farm::HarvestLong(), Farm::HayBailing(), Farm::HayTurning(), Farm::RowCultivation(), Farm::StrawChopping(), Farm::Strigling(), Farm::StriglingSow(), Farm::StubbleHarrowing(), and Landscape::SupplyRainPeriod().

◆ GetSnow() [1/2]

bool Weather::GetSnow ( long  a_date)

Definition at line 132 of file weather.cpp.

133 {
134  if ( a_date == g_date->Date() ) {
135  return m_snowtoday;
136  }
137 
138  int weatherday = a_date % m_datemodulus;
139  bool snow = false;
140 
141  if ( m_temp[ weatherday ] < 0 &&
142  m_rain[ weatherday ] > 1 &&
143  random(100) < 50 ) {
144  snow = true;
145  }
146 
147  // if ( ((dayinyear<90) || (dayinyear>330)) && (rand()%4==1) )
148  // snow = true;
149 
150  return snow;
151 }

References Calendar::Date(), g_date, m_datemodulus, m_rain, m_snowtoday, and m_temp.

Referenced by Landscape::SupplySnowcover().

◆ GetSnow() [2/2]

bool Weather::GetSnow ( void  )
inline

Definition at line 429 of file weather.h.

429 { return m_snowtoday; }

◆ GetSnowDepth()

double Weather::GetSnowDepth ( void  )
inline

Get the current snow depth.

Definition at line 431 of file weather.h.

431 { return m_snowdepth; }

Referenced by Landscape::SupplySnowDepth().

◆ GetTemp() [1/2]

double Weather::GetTemp ( long  a_date)
inline

Get the temperature on a particular date.

Definition at line 417 of file weather.h.

417 { return m_temp[a_date % m_datemodulus]; }

Referenced by Farm::FA_Manure(), Farm::FA_Sludge(), Farm::FA_Slurry(), Farm::FP_Manure(), Farm::FP_Sludge(), Farm::FP_Slurry(), Landscape::SupplyTemp(), and Pesticide::Tick().

◆ GetTemp() [2/2]

double Weather::GetTemp ( void  )
inline

Get the temperature today.

Definition at line 419 of file weather.h.

419 { return m_temptoday; }

Referenced by GetMeanTemp(), and GetTempPeriod().

◆ GetTempPeriod()

double Weather::GetTempPeriod ( long  a_date,
unsigned int  a_period 
)

Sums the temperature for the period from a_date back a_period days.

Parameters
[in]a_datethe day to start summing degrees
[in]a_periodthe number of days perĂ­od to sum
Returns
The sum of day degrees

Definition at line 272 of file weather.cpp.

273 {
280  double sum = 0.0;
281 
282  for ( unsigned int i=0; i<a_period; i++ )
283  sum += GetTemp( a_date - i );
284 
285  return sum;
286 }

References GetTemp().

Referenced by Landscape::SupplyTempPeriod(), and Tick().

◆ GetWind() [1/2]

◆ GetWind() [2/2]

double Weather::GetWind ( void  )
inline

Definition at line 426 of file weather.h.

426 { return m_windtoday; }

Referenced by GetWindPeriod().

◆ GetWindDirection()

int Weather::GetWindDirection ( void  )
inline

Definition at line 427 of file weather.h.

427 { return m_winddirtoday; }

Referenced by Landscape::SupplyWindDirection().

◆ GetWindPeriod()

double Weather::GetWindPeriod ( long  a_date,
unsigned int  a_period 
)

Definition at line 262 of file weather.cpp.

263 {
264  double sum = 0.0;
265 
266  for ( unsigned int i=0; i<a_period; i++ )
267  sum += GetWind( a_date - i );
268 
269  return sum;
270 }

References GetWind().

Referenced by Landscape::SupplyWindPeriod().

◆ Raining()

bool Weather::Raining ( void  )
inline

◆ Tick()

void Weather::Tick ( void  )

The complicated calculation of weatherday is due to the potential to run off either end of the rain data, especially since this is called on Day0

Humidity calculation. This is based on an assumption that humidity is relative to rainfall in the days before. The humidity score is the mean of rainfall over the last 5 days assuming 50% loss each day. Therefore rainfall of 10,0,0,2,0 mm in the previous 5 days would give a humidity of 0.625+0+0+1+0 = 1.625/5 = 0.325 But we also need to calculate temperature into this. Lets assume <10 degrees has no effect, 10-20 and this figure is halved, 20-40 and it is half again.

datemodulus is added to prevent negative overun on the rain or temperature data array, important since this is called on Day0

Wind directions is based on probabilities. Can also use the WindDirections array which allows montly probabilities

Definition at line 58 of file weather.cpp.

58  {
60  int weatherday = (g_date->Date() + m_datemodulus) % m_datemodulus;
61  double snowtempthreshold = -1.0;
62  if (m_snowdepth > 0.0 && m_temp[ weatherday ] < snowtempthreshold) {
63  m_snowdepth -= 0.1; // We decay the snow depth by 0.1 cm per day when temperatures are sub zero
64  }
65  else if (m_snowdepth > 0.0) // If temperatures are above 0.0, we decay snow 1 cm per degree C
66  {
67  m_snowdepth -= m_temp[ weatherday ];
68  if (m_snowdepth < 0.0) m_snowdepth = 0.0;
69  }
70 
71  if (m_temp[ weatherday ] < snowtempthreshold && m_rain[ weatherday ] > 0.0) {
72  m_snowdepth += m_rain[ weatherday ]; // rain is in mm and snow in cm, so we get the conversion for free.
73  }
74  if (m_temp[ weatherday ] > snowtempthreshold && m_rain[ weatherday ] > 0.0) {
75  m_snowdepth -= m_rain[ weatherday ]; // rain is in mm and snow in cm, so we get the conversion for free.
76  if (m_snowdepth < 0.0) m_snowdepth = 0.0;
77  }
78  if (m_snowdepth > 0.0) {
79  m_snowtoday = true;
80  }
81  else {
82  m_snowtoday = false;
83  }
84 
86  m_temptoday = m_temp[ weatherday ];
87  m_raintoday = m_rain[ weatherday ];
88  m_windtoday = m_wind[ weatherday ];
89 
90  if (m_raintoday > 0.001)
91  m_rainingtoday = true;
92  else
93  m_rainingtoday = false;
100  double rainscale = 0;
101  for (int i = 0; i < 5; i++) {
103  rainscale += GetRain( (weatherday + m_datemodulus) - (i + 1) ) * pow( 0.5, i );
104  }
105  double temp = GetTempPeriod( weatherday + m_datemodulus - 1, 5 ) / 5.0;
106  if (temp > 10.0) rainscale *= 0.5;
107  if (temp > 20.0) rainscale *= 0.5;
108  if (temp > 30.0) rainscale *= 0.5;
109  m_humiditytoday = rainscale;
111  double chance = g_rand_uni();
112  if (chance < 0.1) m_winddirtoday = 2; // South
113  else if (chance < 0.3) m_winddirtoday = 1; // East
114  else if (chance < 0.5) m_winddirtoday = 0; // North
115  else m_winddirtoday = 3; // West
116 }

References c_insolation, Calendar::Date(), Calendar::DayInYear(), g_date, g_rand_uni, GetRain(), GetTempPeriod(), m_datemodulus, m_humiditytoday, m_insolation, m_rain, m_rainingtoday, m_raintoday, m_snowdepth, m_snowtoday, m_temp, m_temptoday, m_wind, m_winddirtoday, and m_windtoday.

Referenced by Landscape::Tick(), and Weather().

Member Data Documentation

◆ m_datemodulus

long Weather::m_datemodulus
private

Definition at line 404 of file weather.h.

Referenced by GetDDDegs(), GetSnow(), Tick(), and Weather().

◆ m_humiditytoday

double Weather::m_humiditytoday
private

Definition at line 399 of file weather.h.

Referenced by Tick().

◆ m_insolation

double Weather::m_insolation
private

Definition at line 398 of file weather.h.

Referenced by Tick().

◆ m_rain

vector<double> Weather::m_rain
private

Definition at line 387 of file weather.h.

Referenced by GetSnow(), Tick(), and Weather().

◆ m_rainingtoday

bool Weather::m_rainingtoday
private

Definition at line 397 of file weather.h.

Referenced by Tick().

◆ m_raintoday

double Weather::m_raintoday
private

Definition at line 393 of file weather.h.

Referenced by Tick().

◆ m_snowdepth

double Weather::m_snowdepth
private

The snow depth in cm.

Definition at line 407 of file weather.h.

Referenced by Tick(), and Weather().

◆ m_snowtoday

bool Weather::m_snowtoday
private

Definition at line 396 of file weather.h.

Referenced by GetSnow(), and Tick().

◆ m_temp

vector<double> Weather::m_temp
private

Definition at line 390 of file weather.h.

Referenced by GetDDDegs(), GetSnow(), Tick(), and Weather().

◆ m_temptoday

double Weather::m_temptoday
private

Definition at line 392 of file weather.h.

Referenced by Tick().

◆ m_wind

vector<double> Weather::m_wind
private

Definition at line 388 of file weather.h.

Referenced by Tick(), and Weather().

◆ m_wind_valid

bool Weather::m_wind_valid
private

Definition at line 401 of file weather.h.

◆ m_winddir

vector<double> Weather::m_winddir
private

Definition at line 389 of file weather.h.

◆ m_winddir_valid

bool Weather::m_winddir_valid
private

Definition at line 402 of file weather.h.

◆ m_winddirtoday

int Weather::m_winddirtoday
private

Definition at line 395 of file weather.h.

Referenced by Tick().

◆ m_windtoday

double Weather::m_windtoday
private

Definition at line 394 of file weather.h.

Referenced by Tick().


The documentation for this class was generated from the following files:
Weather::m_snowdepth
double m_snowdepth
The snow depth in cm.
Definition: weather.h:407
Weather::GetWind
double GetWind(void)
Definition: weather.h:426
Weather::m_temp
vector< double > m_temp
Definition: weather.h:390
l_weather_starting_year
static CfgInt l_weather_starting_year("WEATHER_STARTING_YEAR", CFG_CUSTOM, 0)
Weather::m_temptoday
double m_temptoday
Definition: weather.h:392
Calendar::SetFirstYear
void SetFirstYear(int a_year)
Definition: calendar.h:76
Weather::m_humiditytoday
double m_humiditytoday
Definition: weather.h:399
Weather::GetRain
double GetRain(void)
Definition: weather.h:424
Weather::m_datemodulus
long m_datemodulus
Definition: weather.h:404
Calendar::SetLastYear
void SetLastYear(int a_year)
Definition: calendar.h:77
Weather::Tick
void Tick(void)
Definition: weather.cpp:58
Weather::m_rain
vector< double > m_rain
Definition: weather.h:387
Weather::m_snowtoday
bool m_snowtoday
Definition: weather.h:396
Weather::GetTemp
double GetTemp(void)
Get the temperature today.
Definition: weather.h:419
Calendar::Date
long Date(void)
Definition: calendar.h:57
Calendar::Reset
void Reset(void)
Definition: calendar.cpp:40
c_insolation
const double c_insolation[365]
Definition: weather.h:16
Calendar::GetLastYear
int GetLastYear(void)
Definition: calendar.h:66
MapErrorMsg::Warn
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:59
Weather::m_rainingtoday
bool m_rainingtoday
Definition: weather.h:397
Weather::m_windtoday
double m_windtoday
Definition: weather.h:394
Weather::GetTempPeriod
double GetTempPeriod(long a_date, unsigned int a_period)
Definition: weather.cpp:272
WARN_FILE
Definition: maperrormsg.h:37
g_msg
MapErrorMsg * g_msg
This pointer provides access the to the internal ALMaSS error message system.
Definition: maperrormsg.cpp:41
Weather::m_wind
vector< double > m_wind
Definition: weather.h:388
Weather::m_insolation
double m_insolation
Definition: weather.h:398
Weather::m_raintoday
double m_raintoday
Definition: weather.h:393
CfgInt::value
int value(void)
Definition: configurator.h:98
g_rand_uni
boost::variate_generator< base_generator_type &, boost::uniform_real<> > g_rand_uni
A random number generator (0-1)
Calendar::DayInYear
int DayInYear(void)
Definition: calendar.h:58
g_date
class Calendar * g_date
Definition: calendar.cpp:38
Weather::m_winddirtoday
int m_winddirtoday
Definition: weather.h:395