ALMaSS Rabbit ODdox  1.00
The rabbit model description following ODdox protocol
Public Member Functions | Private Attributes | List of all members
Weather Class Reference

#include <weather.h>

Public Member Functions

 Weather (const char *a_weatherdatafile)
 
 ~Weather (void)
 
double GetDDDegs (long a_date)
 
double GetGlobalRadiation (long a_date)
 
double GetGlobalRadiation (void)
 
double GetTemp (long a_date)
 Get the temperature on a particular date. More...
 
double GetTemp (void)
 Get the temperature today. More...
 
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 GetWind (long a_date)
 
double GetWind (void)
 
int GetWindDirection (void)
 
bool GetSnow (long a_date)
 
bool GetSnow (void)
 
double GetSnowDepth (void)
 Get the current snow depth. More...
 
bool Raining (void)
 
double GetRainPeriod (long a_date, unsigned int a_period)
 
double GetWindPeriod (long a_date, unsigned int a_period)
 
double GetTempPeriod (long a_date, unsigned int a_period)
 
void Tick (void)
 

Private Attributes

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

Detailed Description

Definition at line 385 of file weather.h.

Constructor & Destructor Documentation

Weather::Weather ( const char *  a_weatherdatafile)

Definition at line 167 of file weather.cpp.

References FloatToDouble(), 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.

168 {
169  FILE *ifile;
170  int Magic, FileFormat, NoDays, Day, Month, Year, FirstYear=0, LastYear=0;
171  float fTemp, fRain, fWind;
172  double Temp, Rain, Wind;
173  //char degree[20];
174  //double DDegs = 0.0;
175  ifile = fopen(a_weatherdatafile, "r" );
176  if (!ifile) {
177  g_msg->Warn(WARN_FILE, "Weather::Weather(): Unable to open file",
178  a_weatherdatafile );
179  exit(1);
180  }
181  fscanf( ifile, "%d", &Magic );
182 
183  if ( Magic != -1 ) {
184  // Assume old file format.
185  NoDays = Magic;
186  FileFormat = 1;
187  } else {
188  fscanf( ifile, "%d %d", &FileFormat, &NoDays );
189  }
190 
191  /*
192  switch( FileFormat ) {
193  case 1:
194  break;
195  default:
196  g_msg->Warn(WARN_FILE, "Weather::Weather(): "
197  "Unknown file format specified on line 1: ",
198  a_weatherdatafile);
199  g_msg->Warn(WARN_FILE, "First line must be: ",
200  "-1 <file format specifier> <days worth of weather data>" );
201  exit(1);
202  }
203  */
204 
205  m_rain.resize( NoDays );
206  m_wind.resize( NoDays );
207  m_temp.resize( NoDays );
208 
209  bool storing = false; // Whether we are storing weather data.
210  unsigned int index = 0;
211 
212  g_date->SetLastYear( 0 );
213 
214  for ( int i=0; i<NoDays; i++) {
215  // Read in the next data line.
216  fscanf( ifile, "%d %d %d %g %g %g",
217  &Year, &Month, &Day, &fTemp, &fWind, &fRain );
218  FloatToDouble(Temp, fTemp);
219  FloatToDouble(Rain, fRain);
220  FloatToDouble(Wind, fWind);
221  if ( Month == 2 && Day == 29 ) {
222  // Skip leap days.
223  continue;
224  }
225 
226  if ( Month == 1 && Day == 1 && !storing &&
227  (
230  )
231  ) {
232  // Commence storing of data from Jan. 1st of the first
233  // year requested.
234  storing = true;
235  g_date->SetFirstYear( Year );
236  FirstYear = Year;
237  g_date->Reset();
238  }
239 
240  if ( storing ) {
241  //if (Temp > 0.0 )
242  // DDegs += Temp;
243  m_rain[ index ] = Rain;
244  m_wind[ index ] = Wind;
245  m_temp[ index ] = Temp;
246  index++;
247  }
248 
249  if ( Month == 12 && Day == 31 && storing ) {
250  // Found yet another full year worth of weather data.
251  g_date->SetLastYear( Year );
252  LastYear = Year;
253  //sprintf( degree, "%f", DDegs );
254  //g_msg->Warn( WARN_FILE,
255  // "Sum of day degrees:",
256  // degree );
257  //DDegs = 0.0;
258  }
259  }
260 
261  // Did we find at least one full year worth of data?
262  if ( g_date->GetLastYear() == 0 ) {
263  // Nope...
264  g_msg->Warn(WARN_FILE, "Weather::Weather(): Weather data file did",
265  "not contain at least one year of data!" );
266  exit(1);
267  }
268  fclose( ifile );
269  m_datemodulus = (LastYear - FirstYear + 1)*365;
270  m_snowdepth = 0;
271  Tick();
272 }
vector< double > m_temp
Definition: weather.h:390
void SetLastYear(int a_year)
Definition: calendar.h:77
void Reset(void)
Definition: calendar.cpp:40
vector< double > m_rain
Definition: weather.h:387
long m_datemodulus
Definition: weather.h:404
double m_snowdepth
The snow depth in cm.
Definition: weather.h:407
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
void SetFirstYear(int a_year)
Definition: calendar.h:76
void Tick(void)
Definition: weather.cpp:58
class Calendar * g_date
Definition: calendar.cpp:38
static CfgInt l_weather_starting_year("WEATHER_STARTING_YEAR", CFG_CUSTOM, 0)
int value(void)
Definition: configurator.h:92
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56
void FloatToDouble(double &, float)
vector< double > m_wind
Definition: weather.h:388
int GetLastYear(void)
Definition: calendar.h:66
Weather::~Weather ( void  )

Definition at line 274 of file weather.cpp.

275 {
276 }

Member Function Documentation

double Weather::GetDDDegs ( long  a_date)

Definition at line 155 of file weather.cpp.

References m_datemodulus, and m_temp.

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

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 }
vector< double > m_temp
Definition: weather.h:390
long m_datemodulus
Definition: weather.h:404
double Weather::GetGlobalRadiation ( long  a_date)
inline

Definition at line 414 of file weather.h.

References c_insolation.

Referenced by Landscape::SupplyGlobalRadiation().

414 { return (double)c_insolation[a_date % m_datemodulus]; }
long m_datemodulus
Definition: weather.h:404
const double c_insolation[365]
Definition: weather.h:16
double Weather::GetGlobalRadiation ( void  )
inline

Definition at line 415 of file weather.h.

415 { return m_insolation; }
double m_insolation
Definition: weather.h:398
double Weather::GetHumidity ( void  )
inline

Get the humidity score today.

Definition at line 421 of file weather.h.

Referenced by Landscape::SupplyHumidity().

421 { return m_humiditytoday; }
double m_humiditytoday
Definition: weather.h:399
double Weather::GetMeanTemp ( long  a_date,
unsigned int  a_period 
)

Definition at line 120 of file weather.cpp.

References GetTemp().

Referenced by Landscape::SupplyMeanTemp().

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 }
double GetTemp(void)
Get the temperature today.
Definition: weather.h:419
double Weather::GetRain ( long  a_date)
inline

Definition at line 423 of file weather.h.

Referenced by Landscape::SupplyRain().

423 { return m_rain[a_date %m_datemodulus]; }
vector< double > m_rain
Definition: weather.h:387
long m_datemodulus
Definition: weather.h:404
double Weather::GetRain ( void  )
inline

Definition at line 424 of file weather.h.

Referenced by GetRainPeriod(), and Tick().

424 { return m_raintoday; }
double m_raintoday
Definition: weather.h:393
double Weather::GetRainPeriod ( long  a_date,
unsigned int  a_period 
)

Definition at line 278 of file weather.cpp.

References GetRain().

Referenced by 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().

279 {
280  double sum = 0.0;
281 
282  for ( unsigned int i=0; i<a_period; i++ )
283  sum += GetRain( a_date - i );
284  return sum;
285 }
double GetRain(void)
Definition: weather.h:424
bool Weather::GetSnow ( long  a_date)

Definition at line 132 of file weather.cpp.

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

Referenced by Landscape::SupplySnowcover().

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 }
vector< double > m_temp
Definition: weather.h:390
long Date(void)
Definition: calendar.h:57
bool m_snowtoday
Definition: weather.h:396
vector< double > m_rain
Definition: weather.h:387
long m_datemodulus
Definition: weather.h:404
class Calendar * g_date
Definition: calendar.cpp:38
bool Weather::GetSnow ( void  )
inline

Definition at line 429 of file weather.h.

429 { return m_snowtoday; }
bool m_snowtoday
Definition: weather.h:396
double Weather::GetSnowDepth ( void  )
inline

Get the current snow depth.

Definition at line 431 of file weather.h.

Referenced by Landscape::SupplySnowDepth().

431 { return m_snowdepth; }
double m_snowdepth
The snow depth in cm.
Definition: weather.h:407
double Weather::GetTemp ( long  a_date)
inline

Get the temperature on a particular date.

Definition at line 417 of file weather.h.

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

417 { return m_temp[a_date % m_datemodulus]; }
vector< double > m_temp
Definition: weather.h:390
long m_datemodulus
Definition: weather.h:404
double Weather::GetTemp ( void  )
inline

Get the temperature today.

Definition at line 419 of file weather.h.

Referenced by GetMeanTemp(), and GetTempPeriod().

419 { return m_temptoday; }
double m_temptoday
Definition: weather.h:392
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 297 of file weather.cpp.

References GetTemp().

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

298 {
305  double sum = 0.0;
306 
307  for ( unsigned int i=0; i<a_period; i++ )
308  sum += GetTemp( a_date - i );
309 
310  return sum;
311 }
double GetTemp(void)
Get the temperature today.
Definition: weather.h:419
double Weather::GetWind ( long  a_date)
inline
double Weather::GetWind ( void  )
inline

Definition at line 426 of file weather.h.

Referenced by GetWindPeriod().

426 { return m_windtoday; }
double m_windtoday
Definition: weather.h:394
int Weather::GetWindDirection ( void  )
inline

Definition at line 427 of file weather.h.

Referenced by Landscape::SupplyWindDirection().

427 { return m_winddirtoday; }
int m_winddirtoday
Definition: weather.h:395
double Weather::GetWindPeriod ( long  a_date,
unsigned int  a_period 
)

Definition at line 287 of file weather.cpp.

References GetWind().

Referenced by Landscape::SupplyWindPeriod().

288 {
289  double sum = 0.0;
290 
291  for ( unsigned int i=0; i<a_period; i++ )
292  sum += GetWind( a_date - i );
293 
294  return sum;
295 }
double GetWind(void)
Definition: weather.h:426
bool Weather::Raining ( void  )
inline
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.

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().

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 }
vector< double > m_temp
Definition: weather.h:390
long Date(void)
Definition: calendar.h:57
int m_winddirtoday
Definition: weather.h:395
double m_humiditytoday
Definition: weather.h:399
boost::variate_generator< base_generator_type &, boost::uniform_real<> > g_rand_uni
double GetRain(void)
Definition: weather.h:424
bool m_snowtoday
Definition: weather.h:396
vector< double > m_rain
Definition: weather.h:387
long m_datemodulus
Definition: weather.h:404
double m_snowdepth
The snow depth in cm.
Definition: weather.h:407
double m_windtoday
Definition: weather.h:394
int DayInYear(void)
Definition: calendar.h:58
double m_raintoday
Definition: weather.h:393
bool m_rainingtoday
Definition: weather.h:397
double GetTempPeriod(long a_date, unsigned int a_period)
Definition: weather.cpp:297
class Calendar * g_date
Definition: calendar.cpp:38
const double c_insolation[365]
Definition: weather.h:16
double m_insolation
Definition: weather.h:398
double m_temptoday
Definition: weather.h:392
vector< double > m_wind
Definition: weather.h:388

Member Data Documentation

long Weather::m_datemodulus
private

Definition at line 404 of file weather.h.

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

double Weather::m_humiditytoday
private

Definition at line 399 of file weather.h.

Referenced by Tick().

double Weather::m_insolation
private

Definition at line 398 of file weather.h.

Referenced by Tick().

vector<double> Weather::m_rain
private

Definition at line 387 of file weather.h.

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

bool Weather::m_rainingtoday
private

Definition at line 397 of file weather.h.

Referenced by Tick().

double Weather::m_raintoday
private

Definition at line 393 of file weather.h.

Referenced by Tick().

double Weather::m_snowdepth
private

The snow depth in cm.

Definition at line 407 of file weather.h.

Referenced by Tick(), and Weather().

bool Weather::m_snowtoday
private

Definition at line 396 of file weather.h.

Referenced by GetSnow(), and Tick().

vector<double> Weather::m_temp
private

Definition at line 390 of file weather.h.

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

double Weather::m_temptoday
private

Definition at line 392 of file weather.h.

Referenced by Tick().

vector<double> Weather::m_wind
private

Definition at line 388 of file weather.h.

Referenced by Tick(), and Weather().

bool Weather::m_wind_valid
private

Definition at line 401 of file weather.h.

vector<double> Weather::m_winddir
private

Definition at line 389 of file weather.h.

bool Weather::m_winddir_valid
private

Definition at line 402 of file weather.h.

int Weather::m_winddirtoday
private

Definition at line 395 of file weather.h.

Referenced by Tick().

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: