Subversion Repositories public iLand

Rev

Rev 653 | Rev 720 | 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)
414 werner 33
    double temperature; // average day °C (of the light hours)
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; }
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)
210 werner 62
};
63
 
193 werner 64
class Climate
65
{
66
public:
67
    Climate();
68
    void setup(); ///< setup routine that opens database connection
280 werner 69
    bool isSetup() const { return mIsSetup; }
318 werner 70
    const QString &name() const { return mName; } ///< table name of this climate
193 werner 71
    // activity
72
    void nextYear();
198 werner 73
    // access to climate data
255 werner 74
    const ClimateDay *dayOfYear(const int dayofyear) const { return mBegin + dayofyear;} ///< get pointer to climate structure by day of year (0-based-index)
75
    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 76
    /// returns two pointer (arguments!!!) to the begin and one after end of the given month (month: 0..11)
255 werner 77
    void monthRange(const int month, const ClimateDay **rBegin, const ClimateDay **rEnd) const;
327 werner 78
    double days(const int month) const; ///< returns number of days of given month (0..11)
490 werner 79
    int daysOfYear() const; ///< returns number of days of current year.
327 werner 80
    const ClimateDay *begin() const { return mBegin; } ///< STL-like (pointer)-iterator  to the first day of the current year
81
    const ClimateDay *end() const { return mEnd; } ///< STL-like pointer iterator to the day *after* last day of the current year
255 werner 82
    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 83
    //
485 werner 84
    double totalRadiation() const { return mAnnualRadiation; } ///< return radiation sum (MJ) of the whole year
553 werner 85
    const double* precipitationMonth() const { return mPrecipitationMonth; }
214 werner 86
    // access to other subsystems
238 werner 87
    const Phenology &phenology(const int phenologyGroup) const; ///< phenology class of given type
88
    const Sun &sun() const { return mSun; } ///< solar radiation class
89
    double daylength_h(const int doy) const { return sun().daylength(doy); } ///< length of the day in hours
193 werner 90
 
91
private:
280 werner 92
    bool mIsSetup;
348 werner 93
    bool mDoRandomSampling; ///< if true, the sequence of years is randomized
653 werner 94
    bool mTMaxAvailable; ///< tmax is part of the climate data
318 werner 95
    QString mName;
214 werner 96
    Sun mSun; ///< class doing solar radiation calculations
193 werner 97
    void load(); ///< load mLoadYears years from database
214 werner 98
    void setupPhenology(); ///< setup of phenology groups
201 werner 99
    void climateCalculations(ClimateDay &lastDay); ///< more calculations done after loading of climate data
214 werner 100
    ClimateDay mInvalidDay;
193 werner 101
    int mLoadYears; // count of years to load ahead
102
    int mCurrentYear; // current year (relative)
103
    int mMinYear; // lowest year in store (relative)
104
    int mMaxYear;  // highest year in store (relative)
313 werner 105
    double mTemperatureShift; // add this to daily temp
106
    double mPrecipitationShift; // multiply prec with that
214 werner 107
    ClimateDay *mBegin; // pointer to the first day of the current year
108
    ClimateDay *mEnd; // pointer to the last day of the current year (+1)
193 werner 109
    QVector<ClimateDay> mStore; ///< storage of climate data
110
    QVector<int> mDayIndices; ///< store indices for month / years within store
111
    QSqlQuery mClimateQuery; ///< sql query for db access
214 werner 112
    QList<Phenology> mPhenology; ///< phenology calculations
348 werner 113
    QVector<int> mRandomYearList; ///< for random sampling of years
114
    int mRandomListIndex; ///< current index of the randomYearList for random sampling
553 werner 115
    double mAnnualRadiation;  ///< this year's value for total radiation (MJ/m2)
116
    double mPrecipitationMonth[12]; ///< this years preciptitation sum (mm) per month
193 werner 117
};
118
 
119
#endif // CLIMATE_H