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