Subversion Repositories public iLand

Rev

Rev 240 | Rev 280 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
193 werner 2
#ifndef CLIMATE_H
3
#define CLIMATE_H
4
 
5
#include <QtSql>
214 werner 6
#include "phenology.h"
210 werner 7
/// current climate variables of a day. @sa Climate.
193 werner 8
struct ClimateDay
9
{
10
    int year; // year
11
    int month; // month
12
    int day; // day of year
13
    double temperature; // average day °C
198 werner 14
    double temp_delayed; // temperature delayed (after Maekela, 2008) for response calculations
193 werner 15
    double preciptitation; // sum of day [mm]
16
    double radiation; // sum of day (MJ/m2)
234 werner 17
    double vpd; // average of day [kPa] = [0.1 mbar] (1 bar = 100kPa)
226 werner 18
    static double co2; // ambient CO2 content in ppm
214 werner 19
    QString toString() const { return QString("%1.%2.%3").arg(day).arg(month).arg(year); }
209 werner 20
    bool isValid() const  { return (year>=0); }
240 werner 21
    int id() const { return year*10000 + month*100 + day; }
193 werner 22
};
208 werner 23
 
210 werner 24
/// Sun handles calculations of day lengths, etc.
25
class Sun
26
{
27
public:
28
    void setup(const double latitude_rad);
29
    QString dump();
211 werner 30
    const double &daylength(const int day) const { return mDaylength_h[day]; }
31
    int longestDay() const { return mDayWithMaxLength; }
214 werner 32
    bool northernHemishere() const { return mDayWithMaxLength<300; }
210 werner 33
private:
211 werner 34
    double mLatitude; ///< latitude in radians
35
    int mDayWithMaxLength; ///< day of year with maximum day length
210 werner 36
    double mDaylength_h[366]; ///< daylength per day in hours
37
};
38
 
193 werner 39
class Climate
40
{
41
public:
42
    Climate();
43
    void setup(); ///< setup routine that opens database connection
44
    // activity
45
    void nextYear();
198 werner 46
    // access to climate data
255 werner 47
    const ClimateDay *dayOfYear(const int dayofyear) const { return mBegin + dayofyear;} ///< get pointer to climate structure by day of year (0-based-index)
48
    const ClimateDay *day(const int month, const int day) const; ///< gets pointer to climate structure of given day (0-based indices, i.e. month=11=december!)
198 werner 49
    /// returns two pointer (arguments!!!) to the begin and one after end of the given month (month: 0..11)
255 werner 50
    void monthRange(const int month, const ClimateDay **rBegin, const ClimateDay **rEnd) const;
51
    double days(const int month) const; ///< returns number of days of given month
52
    int daysOfYear() const; ///< returns number of days of current year. points to the first day of the current year.
53
    const ClimateDay *begin() const { return mBegin; } ///< STL-like (pointer)-iterator to the day *after* last day of the current year
54
    const ClimateDay *end() const { return mEnd; } ///< STL-like pointer iterator
55
    void toDate(const int yearday, int *rDay=0, int *rMonth=0, int *rYear=0) const; ///< decode "yearday" to the actual year, month, day if provided
214 werner 56
    // access to other subsystems
238 werner 57
    const Phenology &phenology(const int phenologyGroup) const; ///< phenology class of given type
58
    const Sun &sun() const { return mSun; } ///< solar radiation class
59
    double daylength_h(const int doy) const { return sun().daylength(doy); } ///< length of the day in hours
193 werner 60
 
61
private:
214 werner 62
    Sun mSun; ///< class doing solar radiation calculations
193 werner 63
    void load(); ///< load mLoadYears years from database
214 werner 64
    void setupPhenology(); ///< setup of phenology groups
201 werner 65
    void climateCalculations(ClimateDay &lastDay); ///< more calculations done after loading of climate data
214 werner 66
    ClimateDay mInvalidDay;
193 werner 67
    int mLoadYears; // count of years to load ahead
68
    int mCurrentYear; // current year (relative)
69
    int mMinYear; // lowest year in store (relative)
70
    int mMaxYear;  // highest year in store (relative)
214 werner 71
    ClimateDay *mBegin; // pointer to the first day of the current year
72
    ClimateDay *mEnd; // pointer to the last day of the current year (+1)
193 werner 73
    QVector<ClimateDay> mStore; ///< storage of climate data
74
    QVector<int> mDayIndices; ///< store indices for month / years within store
75
    QSqlQuery mClimateQuery; ///< sql query for db access
214 werner 76
    QList<Phenology> mPhenology; ///< phenology calculations
193 werner 77
};
78
 
79
#endif // CLIMATE_H