Subversion Repositories public iLand

Rev

Rev 485 | Rev 552 | 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
414 werner 13
    double temperature; // average day °C (of the light hours)
14
    double min_temperature; // minimum temperature of the day
198 werner 15
    double temp_delayed; // temperature delayed (after Maekela, 2008) for response calculations
193 werner 16
    double preciptitation; // sum of day [mm]
17
    double radiation; // sum of day (MJ/m2)
234 werner 18
    double vpd; // average of day [kPa] = [0.1 mbar] (1 bar = 100kPa)
226 werner 19
    static double co2; // ambient CO2 content in ppm
214 werner 20
    QString toString() const { return QString("%1.%2.%3").arg(day).arg(month).arg(year); }
209 werner 21
    bool isValid() const  { return (year>=0); }
240 werner 22
    int id() const { return year*10000 + month*100 + day; }
193 werner 23
};
208 werner 24
 
210 werner 25
/// Sun handles calculations of day lengths, etc.
26
class Sun
27
{
28
public:
29
    void setup(const double latitude_rad);
30
    QString dump();
211 werner 31
    const double &daylength(const int day) const { return mDaylength_h[day]; }
32
    int longestDay() const { return mDayWithMaxLength; }
214 werner 33
    bool northernHemishere() const { return mDayWithMaxLength<300; }
440 werner 34
    int dayShorter10_5hrs() const { return mDayWith10_5hrs; }
210 werner 35
private:
211 werner 36
    double mLatitude; ///< latitude in radians
37
    int mDayWithMaxLength; ///< day of year with maximum day length
210 werner 38
    double mDaylength_h[366]; ///< daylength per day in hours
440 werner 39
    int mDayWith10_5hrs; // last day of year with a day length > 10.5 hours (see Establishment)
210 werner 40
};
41
 
193 werner 42
class Climate
43
{
44
public:
45
    Climate();
46
    void setup(); ///< setup routine that opens database connection
280 werner 47
    bool isSetup() const { return mIsSetup; }
318 werner 48
    const QString &name() const { return mName; } ///< table name of this climate
193 werner 49
    // activity
50
    void nextYear();
198 werner 51
    // access to climate data
255 werner 52
    const ClimateDay *dayOfYear(const int dayofyear) const { return mBegin + dayofyear;} ///< get pointer to climate structure by day of year (0-based-index)
53
    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 54
    /// returns two pointer (arguments!!!) to the begin and one after end of the given month (month: 0..11)
255 werner 55
    void monthRange(const int month, const ClimateDay **rBegin, const ClimateDay **rEnd) const;
327 werner 56
    double days(const int month) const; ///< returns number of days of given month (0..11)
490 werner 57
    int daysOfYear() const; ///< returns number of days of current year.
327 werner 58
    const ClimateDay *begin() const { return mBegin; } ///< STL-like (pointer)-iterator  to the first day of the current year
59
    const ClimateDay *end() const { return mEnd; } ///< STL-like pointer iterator to the day *after* last day of the current year
255 werner 60
    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
436 werner 61
    //
485 werner 62
    double totalRadiation() const { return mAnnualRadiation; } ///< return radiation sum (MJ) of the whole year
214 werner 63
    // access to other subsystems
238 werner 64
    const Phenology &phenology(const int phenologyGroup) const; ///< phenology class of given type
65
    const Sun &sun() const { return mSun; } ///< solar radiation class
66
    double daylength_h(const int doy) const { return sun().daylength(doy); } ///< length of the day in hours
193 werner 67
 
68
private:
280 werner 69
    bool mIsSetup;
348 werner 70
    bool mDoRandomSampling; ///< if true, the sequence of years is randomized
318 werner 71
    QString mName;
214 werner 72
    Sun mSun; ///< class doing solar radiation calculations
193 werner 73
    void load(); ///< load mLoadYears years from database
214 werner 74
    void setupPhenology(); ///< setup of phenology groups
201 werner 75
    void climateCalculations(ClimateDay &lastDay); ///< more calculations done after loading of climate data
214 werner 76
    ClimateDay mInvalidDay;
193 werner 77
    int mLoadYears; // count of years to load ahead
78
    int mCurrentYear; // current year (relative)
79
    int mMinYear; // lowest year in store (relative)
80
    int mMaxYear;  // highest year in store (relative)
313 werner 81
    double mTemperatureShift; // add this to daily temp
82
    double mPrecipitationShift; // multiply prec with that
214 werner 83
    ClimateDay *mBegin; // pointer to the first day of the current year
84
    ClimateDay *mEnd; // pointer to the last day of the current year (+1)
193 werner 85
    QVector<ClimateDay> mStore; ///< storage of climate data
86
    QVector<int> mDayIndices; ///< store indices for month / years within store
87
    QSqlQuery mClimateQuery; ///< sql query for db access
214 werner 88
    QList<Phenology> mPhenology; ///< phenology calculations
348 werner 89
    QVector<int> mRandomYearList; ///< for random sampling of years
90
    int mRandomListIndex; ///< current index of the randomYearList for random sampling
485 werner 91
    double mAnnualRadiation;  // this year's value for total radiation (MJ/m2)
193 werner 92
};
93
 
94
#endif // CLIMATE_H