Subversion Repositories public iLand

Rev

Rev 280 | Rev 318 | 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
280 werner 44
    bool isSetup() const { return mIsSetup; }
193 werner 45
    // activity
46
    void nextYear();
198 werner 47
    // access to climate data
255 werner 48
    const ClimateDay *dayOfYear(const int dayofyear) const { return mBegin + dayofyear;} ///< get pointer to climate structure by day of year (0-based-index)
49
    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 50
    /// returns two pointer (arguments!!!) to the begin and one after end of the given month (month: 0..11)
255 werner 51
    void monthRange(const int month, const ClimateDay **rBegin, const ClimateDay **rEnd) const;
52
    double days(const int month) const; ///< returns number of days of given month
53
    int daysOfYear() const; ///< returns number of days of current year. points to the first day of the current year.
54
    const ClimateDay *begin() const { return mBegin; } ///< STL-like (pointer)-iterator to the day *after* last day of the current year
55
    const ClimateDay *end() const { return mEnd; } ///< STL-like pointer iterator
56
    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 57
    // access to other subsystems
238 werner 58
    const Phenology &phenology(const int phenologyGroup) const; ///< phenology class of given type
59
    const Sun &sun() const { return mSun; } ///< solar radiation class
60
    double daylength_h(const int doy) const { return sun().daylength(doy); } ///< length of the day in hours
193 werner 61
 
62
private:
280 werner 63
    bool mIsSetup;
214 werner 64
    Sun mSun; ///< class doing solar radiation calculations
193 werner 65
    void load(); ///< load mLoadYears years from database
214 werner 66
    void setupPhenology(); ///< setup of phenology groups
201 werner 67
    void climateCalculations(ClimateDay &lastDay); ///< more calculations done after loading of climate data
214 werner 68
    ClimateDay mInvalidDay;
193 werner 69
    int mLoadYears; // count of years to load ahead
70
    int mCurrentYear; // current year (relative)
71
    int mMinYear; // lowest year in store (relative)
72
    int mMaxYear;  // highest year in store (relative)
313 werner 73
    double mTemperatureShift; // add this to daily temp
74
    double mPrecipitationShift; // multiply prec with that
214 werner 75
    ClimateDay *mBegin; // pointer to the first day of the current year
76
    ClimateDay *mEnd; // pointer to the last day of the current year (+1)
193 werner 77
    QVector<ClimateDay> mStore; ///< storage of climate data
78
    QVector<int> mDayIndices; ///< store indices for month / years within store
79
    QSqlQuery mClimateQuery; ///< sql query for db access
214 werner 80
    QList<Phenology> mPhenology; ///< phenology calculations
193 werner 81
};
82
 
83
#endif // CLIMATE_H