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