Subversion Repositories public iLand

Rev

Rev 1221 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1221 Rev 1222
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/climate.h':
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/climate.h':
2
/********************************************************************************************
2
/********************************************************************************************
3
**    iLand - an individual based forest landscape and disturbance model
3
**    iLand - an individual based forest landscape and disturbance model
4
**    http://iland.boku.ac.at
4
**    http://iland.boku.ac.at
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
6
**
6
**
7
**    This program is free software: you can redistribute it and/or modify
7
**    This program is free software: you can redistribute it and/or modify
8
**    it under the terms of the GNU General Public License as published by
8
**    it under the terms of the GNU General Public License as published by
9
**    the Free Software Foundation, either version 3 of the License, or
9
**    the Free Software Foundation, either version 3 of the License, or
10
**    (at your option) any later version.
10
**    (at your option) any later version.
11
**
11
**
12
**    This program is distributed in the hope that it will be useful,
12
**    This program is distributed in the hope that it will be useful,
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
**    GNU General Public License for more details.
15
**    GNU General Public License for more details.
16
**
16
**
17
**    You should have received a copy of the GNU General Public License
17
**    You should have received a copy of the GNU General Public License
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************************************/
19
********************************************************************************************/
20
20
21
#ifndef CLIMATE_H
21
#ifndef CLIMATE_H
22
#define CLIMATE_H
22
#define CLIMATE_H
23
23
24
#include <QtSql>
24
#include <QtSql>
25
#include "phenology.h"
25
#include "phenology.h"
26
/// current climate variables of a day. @sa Climate.
26
/// current climate variables of a day. @sa Climate.
27
/// http://iland.boku.ac.at/ClimateData
27
/// http://iland.boku.ac.at/ClimateData
28
struct ClimateDay
28
struct ClimateDay
29
{
29
{
30
    int year; // year
30
    int year; // year
31
    int month; // month (1..12)
31
    int month; // month (1..12)
32
    int dayOfMonth; // day of the month (1..31)
32
    int dayOfMonth; // day of the month (1..31)
33
    double temperature; // average day  degree C (of the light hours)
33
    double temperature; // average day  degree C (of the light hours)
34
    double min_temperature; // minimum temperature of the day
34
    double min_temperature; // minimum temperature of the day
35
    double max_temperature; // maximum temperature of the day
35
    double max_temperature; // maximum temperature of the day
36
    double mean_temp() const { return (min_temperature + max_temperature) / 2.; } // mean temperature
36
    double mean_temp() const { return (min_temperature + max_temperature) / 2.; } // mean temperature
37
    double temp_delayed; // temperature delayed (after Maekela, 2008) for response calculations
37
    double temp_delayed; // temperature delayed (after Maekela, 2008) for response calculations
38
    double preciptitation; // sum of day [mm]
38
    double preciptitation; // sum of day [mm]
39
    double radiation; // sum of day (MJ/m2)
39
    double radiation; // sum of day (MJ/m2)
40
    double vpd; // average of day [kPa] = [0.1 mbar] (1 bar = 100kPa)
40
    double vpd; // average of day [kPa] = [0.1 mbar] (1 bar = 100kPa)
41
    static double co2; // ambient CO2 content in ppm
41
    static double co2; // ambient CO2 content in ppm
42
    QString toString() const { return QString("%1.%2.%3").arg(dayOfMonth).arg(month).arg(year); }
42
    QString toString() const { return QString("%1.%2.%3").arg(dayOfMonth).arg(month).arg(year); }
43
    bool isValid() const  { return (year>=0); }
43
    bool isValid() const  { return (year>=0); }
44
    int id() const { return year*10000 + month*100 + dayOfMonth; }
44
    int id() const { return year*10000 + month*100 + dayOfMonth; }
45
};
45
};
46
46
47
/// Sun handles calculations of day lengths, etc.
47
/// Sun handles calculations of day lengths, etc.
48
class Sun
48
class Sun
49
{
49
{
50
public:
50
public:
51
    void setup(const double latitude_rad);
51
    void setup(const double latitude_rad);
52
    QString dump();
52
    QString dump();
53
    const double &daylength(const int day) const { return mDaylength_h[day]; }
53
    const double &daylength(const int day) const { return mDaylength_h[day]; }
54
    int longestDay() const { return mDayWithMaxLength; }
54
    int longestDay() const { return mDayWithMaxLength; }
55
    bool northernHemishere() const { return mDayWithMaxLength<300; }
55
    bool northernHemishere() const { return mDayWithMaxLength<300; }
56
    int dayShorter10_5hrs() const { return mDayWith10_5hrs; }
56
    int dayShorter10_5hrs() const { return mDayWith10_5hrs; }
57
    int dayShorter14_5hrs() const { return mDayWith14_5hrs; }
57
    int dayShorter14_5hrs() const { return mDayWith14_5hrs; }
58
private:
58
private:
59
    double mLatitude; ///< latitude in radians
59
    double mLatitude; ///< latitude in radians
60
    int mDayWithMaxLength; ///< day of year with maximum day length
60
    int mDayWithMaxLength; ///< day of year with maximum day length
61
    double mDaylength_h[366]; ///< daylength per day in hours
61
    double mDaylength_h[366]; ///< daylength per day in hours
62
    int mDayWith10_5hrs; // last day of year with a day length > 10.5 hours (see Establishment)
62
    int mDayWith10_5hrs; // last day of year with a day length > 10.5 hours (see Establishment)
63
    int mDayWith14_5hrs; // last doy with at least 14.5 hours of day length
63
    int mDayWith14_5hrs; // last doy with at least 14.5 hours of day length
64
};
64
};
65
65
66
class Climate
66
class Climate
67
{
67
{
68
public:
68
public:
69
    Climate();
69
    Climate();
70
    void setup(); ///< setup routine that opens database connection
70
    void setup(); ///< setup routine that opens database connection
71
    bool isSetup() const { return mIsSetup; }
71
    bool isSetup() const { return mIsSetup; }
72
    const QString &name() const { return mName; } ///< table name of this climate
72
    const QString &name() const { return mName; } ///< table name of this climate
73
    // activity
73
    // activity
74
    void nextYear();
74
    void nextYear();
75
    // access to climate data
75
    // access to climate data
76
    const ClimateDay *dayOfYear(const int dayofyear) const { return mBegin + dayofyear;} ///< get pointer to climate structure by day of year (0-based-index)
76
    const ClimateDay *dayOfYear(const int dayofyear) const { return mBegin + dayofyear;} ///< get pointer to climate structure by day of year (0-based-index)
77
    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!)
77
    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!)
78
    int whichDayOfYear(const ClimateDay *climate) const {return climate-mBegin; } ///< get the 0-based index of the climate given by 'climate' within the current year
78
    int whichDayOfYear(const ClimateDay *climate) const {return climate-mBegin; } ///< get the 0-based index of the climate given by 'climate' within the current year
79
    /// returns two pointer (arguments!!!) to the begin and one after end of the given month (month: 0..11)
79
    /// returns two pointer (arguments!!!) to the begin and one after end of the given month (month: 0..11)
80
    void monthRange(const int month, const ClimateDay **rBegin, const ClimateDay **rEnd) const;
80
    void monthRange(const int month, const ClimateDay **rBegin, const ClimateDay **rEnd) const;
81
    double days(const int month) const; ///< returns number of days of given month (0..11)
81
    double days(const int month) const; ///< returns number of days of given month (0..11)
82
    int daysOfYear() const; ///< returns number of days of current year.
82
    int daysOfYear() const; ///< returns number of days of current year.
83
    const ClimateDay *begin() const { return mBegin; } ///< STL-like (pointer)-iterator  to the first day of the current year
83
    const ClimateDay *begin() const { return mBegin; } ///< STL-like (pointer)-iterator  to the first day of the current year
84
    const ClimateDay *end() const { return mEnd; } ///< STL-like pointer iterator to the day *after* last day of the current year
84
    const ClimateDay *end() const { return mEnd; } ///< STL-like pointer iterator to the day *after* last day of the current year
85
    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
85
    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
86
    //
86
    //
87
    double totalRadiation() const { return mAnnualRadiation; } ///< return radiation sum (MJ) of the whole year
87
    double totalRadiation() const { return mAnnualRadiation; } ///< return radiation sum (MJ) of the whole year
88
    const double* precipitationMonth() const { return mPrecipitationMonth; }
88
    const double* precipitationMonth() const { return mPrecipitationMonth; }
89
    /// the mean annual temperature of the current year (degree C)
89
    /// the mean annual temperature of the current year (degree C)
90
    double meanAnnualTemperature() const { return mMeanAnnualTemperature; }
90
    double meanAnnualTemperature() const { return mMeanAnnualTemperature; }
91
    /// annual precipitation sum (mm)
91
    /// annual precipitation sum (mm)
92
    double annualPrecipitation() const { double r=0.; for (int i=0;i<12;++i) r+=mPrecipitationMonth[i]; return r;}
92
    double annualPrecipitation() const { double r=0.; for (int i=0;i<12;++i) r+=mPrecipitationMonth[i]; return r;}
93
    /// get a array with mean temperatures per month (deg C)
93
    /// get a array with mean temperatures per month (deg C)
94
    const double *temperatureMonth() const { return mTemperatureMonth; }
94
    const double *temperatureMonth() const { return mTemperatureMonth; }
95
    // access to other subsystems
95
    // access to other subsystems
96
    const Phenology &phenology(const int phenologyGroup) const; ///< phenology class of given type
96
    const Phenology &phenology(const int phenologyGroup) const; ///< phenology class of given type
97
    const Sun &sun() const { return mSun; } ///< solar radiation class
97
    const Sun &sun() const { return mSun; } ///< solar radiation class
98
    double daylength_h(const int doy) const { return sun().daylength(doy); } ///< length of the day in hours
98
    double daylength_h(const int doy) const { return sun().daylength(doy); } ///< length of the day in hours
99
99
100
private:
100
private:
101
    bool mIsSetup;
101
    bool mIsSetup;
102
    bool mDoRandomSampling; ///< if true, the sequence of years is randomized
102
    bool mDoRandomSampling; ///< if true, the sequence of years is randomized
103
    bool mTMaxAvailable; ///< tmax is part of the climate data
103
    bool mTMaxAvailable; ///< tmax is part of the climate data
104
    QString mName;
104
    QString mName;
105
    Sun mSun; ///< class doing solar radiation calculations
105
    Sun mSun; ///< class doing solar radiation calculations
106
    void load(); ///< load mLoadYears years from database
106
    void load(); ///< load mLoadYears years from database
107
    void setupPhenology(); ///< setup of phenology groups
107
    void setupPhenology(); ///< setup of phenology groups
108
    void climateCalculations(const ClimateDay &lastDay); ///< more calculations done after loading of climate data
108
    void climateCalculations(const ClimateDay &lastDay); ///< more calculations done after loading of climate data
109
    ClimateDay mInvalidDay;
109
    ClimateDay mInvalidDay;
110
    int mLoadYears; // count of years to load ahead
110
    int mLoadYears; // count of years to load ahead
111
    int mCurrentYear; // current year (relative)
111
    int mCurrentYear; // current year (relative)
112
    int mMinYear; // lowest year in store (relative)
112
    int mMinYear; // lowest year in store (relative)
113
    int mMaxYear;  // highest year in store (relative)
113
    int mMaxYear;  // highest year in store (relative)
114
    double mTemperatureShift; // add this to daily temp
114
    double mTemperatureShift; // add this to daily temp
115
    double mPrecipitationShift; // multiply prec with that
115
    double mPrecipitationShift; // multiply prec with that
116
    ClimateDay *mBegin; // pointer to the first day of the current year
116
    ClimateDay *mBegin; // pointer to the first day of the current year
117
    ClimateDay *mEnd; // pointer to the last day of the current year (+1)
117
    ClimateDay *mEnd; // pointer to the last day of the current year (+1)
118
    QVector<ClimateDay> mStore; ///< storage of climate data
118
    QVector<ClimateDay> mStore; ///< storage of climate data
119
    QVector<int> mDayIndices; ///< store indices for month / years within store
119
    QVector<int> mDayIndices; ///< store indices for month / years within store
120
    QSqlQuery mClimateQuery; ///< sql query for db access
120
    QSqlQuery mClimateQuery; ///< sql query for db access
121
    QList<Phenology> mPhenology; ///< phenology calculations
121
    QList<Phenology> mPhenology; ///< phenology calculations
122
    QVector<int> mRandomYearList; ///< for random sampling of years
122
    QVector<int> mRandomYearList; ///< for random sampling of years
123
    int mRandomListIndex; ///< current index of the randomYearList for random sampling
123
    int mRandomListIndex; ///< current index of the randomYearList for random sampling
124
    double mAnnualRadiation;  ///< this year's value for total radiation (MJ/m2)
124
    double mAnnualRadiation;  ///< this year's value for total radiation (MJ/m2)
125
    double mPrecipitationMonth[12]; ///< this years preciptitation sum (mm) per month
125
    double mPrecipitationMonth[12]; ///< this years preciptitation sum (mm) per month
126
    double mTemperatureMonth[12]; ///< this years average temperature per month
126
    double mTemperatureMonth[12]; ///< this years average temperature per month
127
    double mMeanAnnualTemperature; ///< mean temperature of the current year
127
    double mMeanAnnualTemperature; ///< mean temperature of the current year
128
};
128
};
129
129
130
#endif // CLIMATE_H
130
#endif // CLIMATE_H
131
 
131