Subversion Repositories public iLand

Rev

Rev 779 | Rev 889 | 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
 
269 werner 21
#include "global.h"
22
#include "environment.h"
808 werner 23
#include "debugtimer.h"
280 werner 24
#include "helper.h"
25
#include "csvfile.h"
567 werner 26
#include "gisgrid.h"
269 werner 27
 
28
#include "climate.h"
280 werner 29
#include "speciesset.h"
269 werner 30
 
697 werner 31
/** Represents the input of various variables with regard to climate, soil properties and more.
32
  @ingroup tools
269 werner 33
    Data is read from various sources and presented to the core model with a standardized interface.
697 werner 34
    see http://iland.boku.ac.at/simulation+extent
269 werner 35
*/
36
Environment::Environment()
37
{
567 werner 38
    mInfile = 0;
39
    mGrid = 0;
40
    mGridMode = false;
41
    mCurrentSpeciesSet = 0;
42
    mCurrentClimate = 0;
569 werner 43
    mCurrentID = 0;
269 werner 44
}
280 werner 45
Environment::~Environment()
46
{
314 werner 47
    if (mInfile) {
280 werner 48
        delete mInfile;
314 werner 49
    }
567 werner 50
    if (mGrid)
51
        delete mGrid;
280 werner 52
}
53
 
54
bool Environment::loadFromFile(const QString &fileName)
55
{
284 werner 56
    QString source = Helper::loadTextFile(GlobalSettings::instance()->path(fileName));
280 werner 57
    if (source.isEmpty())
58
        throw IException(QString("Environment: input file does not exist or is empty (%1)").arg(fileName));
59
    return loadFromString(source);
60
}
61
 
281 werner 62
// ******** specific keys *******
63
const QString speciesKey = "model.species.source";
64
const QString climateKey = "model.climate.tableName";
65
 
280 werner 66
bool Environment::loadFromString(const QString &source)
67
{
68
    try {
69
        if (mInfile)
70
            delete mInfile;
71
        mInfile = new CSVFile();
72
 
73
        mInfile->loadFromString(source);
74
        mKeys = mInfile->captions();
75
 
76
        XmlHelper xml(GlobalSettings::instance()->settings());
77
        mSpeciesSets.clear(); // note: the objects are not destroyed - potential memory leak.
78
        mClimate.clear();
79
        mRowCoordinates.clear();
281 werner 80
        mCreatedObjects.clear();
81
 
280 werner 82
        int index;
567 werner 83
        if (mGridMode) {
84
            int id = mInfile->columnIndex("id");
85
            if (id<0)
86
                throw IException("Environment:: (grid mode) input file has no 'id' column!");
87
            for (int row=0;row<mInfile->rowCount();row++) {
88
                mRowCoordinates[mInfile->value(row, id).toString()] = row;
89
            }
90
 
91
        } else {
92
            // ***  Matrix mode ******
93
            // each row must contain 'x' and 'y' coordinates
94
            // setup coordinates (x,y)
95
            int ix,iy;
96
            ix = mInfile->columnIndex("x");
97
            iy = mInfile->columnIndex("y");
98
            if (ix<0 || iy<0)
99
                throw IException("Environment:: (matrix mode) input file has no x/y coordinates!");
100
            for (int row=0;row<mInfile->rowCount();row++) {
101
                QString key=QString("%1_%2")
280 werner 102
                        .arg(mInfile->value(row, ix).toString())
103
                        .arg(mInfile->value(row, iy).toString());
567 werner 104
                mRowCoordinates[key] = row;
105
            }
280 werner 106
        }
107
 
108
 
281 werner 109
 
280 werner 110
        // ******** setup of Species Sets *******
111
        if ((index = mKeys.indexOf(speciesKey))>-1) {
112
            DebugTimer t("environment:load species");
113
            QStringList speciesNames = mInfile->column(index);
114
            speciesNames.removeDuplicates();
115
            qDebug() << "creating species sets:" << speciesNames;
116
            foreach (const QString &name, speciesNames) {
117
                xml.setNodeValue(speciesKey,name); // set xml value
118
                // create species sets
119
                SpeciesSet *set = new SpeciesSet();
120
                mSpeciesSets.push_back(set);
281 werner 121
                mCreatedObjects[name] = (void*)set;
318 werner 122
                set->setup();
280 werner 123
            }
124
            qDebug() << mSpeciesSets.count() << "species sets created.";
314 werner 125
        } else {
126
            // no species sets specified
127
            SpeciesSet *speciesSet = new SpeciesSet();
318 werner 128
            mSpeciesSets.push_back(speciesSet);
315 werner 129
            speciesSet->setup();
314 werner 130
            mCurrentSpeciesSet = speciesSet;
280 werner 131
        }
132
 
133
        // ******** setup of Climate *******
134
        if ((index = mKeys.indexOf(climateKey))>-1) {
135
            DebugTimer t("environment:load climate");
136
            QStringList climateNames = mInfile->column(index);
137
            climateNames.removeDuplicates();
138
            qDebug() << "creating climatae: " << climateNames;
139
            foreach (QString name, climateNames) {
140
                xml.setNodeValue(climateKey,name); // set xml value
141
                // create climate sets
142
                Climate *climate = new Climate();
143
                mClimate.push_back(climate);
316 werner 144
                mCreatedObjects[name]=(void*)climate;
318 werner 145
                climate->setup();
280 werner 146
            }
147
            qDebug() << mClimate.count() << "climates created";
314 werner 148
        } else {
149
            // no climate defined - setup default climate
150
            Climate *c = new Climate();
318 werner 151
            mClimate.push_back(c);
314 werner 152
            c->setup();
153
            mCurrentClimate = c;
280 werner 154
        }
567 werner 155
        if (!mCurrentClimate && mClimate.count()>0)
156
            mCurrentClimate = mClimate[0];
157
        if (!mCurrentSpeciesSet && mSpeciesSets.count()>0)
158
            mCurrentSpeciesSet = mSpeciesSets[0];
280 werner 159
        return true;
160
 
161
    } catch(const IException &e) {
318 werner 162
        QString addMsg;
163
        if (!mClimate.isEmpty())
164
            addMsg = QString("last Climate: %1 ").arg(mClimate.last()->name());
165
        if (!mSpeciesSets.isEmpty())
166
            addMsg += QString("last Speciesset table: %1").arg(mSpeciesSets.last()->name());
575 werner 167
        QString error_msg = QString("An error occured during the setup of the environment: \n%1\n%2").arg(e.message()).arg(addMsg);
280 werner 168
        qDebug() << error_msg;
169
        Helper::msg(error_msg);
170
        return false;
171
    }
172
}
173
 
340 werner 174
/** sets the "pointer" to a "position" (metric coordinates).
175
    All specified values are set (also the climate/species-set pointers).
176
*/
280 werner 177
void Environment::setPosition(const QPointF position)
178
{
281 werner 179
    // no changes occur, when the "environment" is not loaded
180
    if (!isSetup())
181
        return;
567 werner 182
    QString key;
568 werner 183
    int ix=-1, iy=-1, id=-1;
567 werner 184
    if (mGridMode) {
185
        // grid mode
186
        id = mGrid->value(position);
569 werner 187
        mCurrentID = id;
567 werner 188
        key = QString::number(id);
189
        if (id==-1)
190
            return; // no data for the resource unit
191
    } else {
192
        // access data in the matrix by resource unit indices
193
        ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate
194
        iy = int(position.y() / 100.);
195
        key=QString("%1_%2").arg(ix).arg(iy);
196
    }
280 werner 197
 
198
    if (mRowCoordinates.contains(key)) {
199
        XmlHelper xml(GlobalSettings::instance()->settings());
200
        int row = mRowCoordinates[key];
201
        QString value;
550 werner 202
        if (logLevelInfo()) qDebug() << "settting up point" << position << "with row" << row;
280 werner 203
        for (int col=0;col<mInfile->colCount(); col++) {
567 werner 204
            if (mKeys[col]=="x" || mKeys[col]=="y" || mKeys[col]=="id") // ignore "x" and "y" keys
280 werner 205
                continue;
206
            value = mInfile->value(row,col).toString();
550 werner 207
            if (logLevelInfo()) qDebug() << "set" << mKeys[col] << "to" << value;
280 werner 208
            xml.setNodeValue(mKeys[col], value);
281 werner 209
            // special handling for constructed objects:
210
            if (mKeys[col]==speciesKey)
211
                mCurrentSpeciesSet = (SpeciesSet*)mCreatedObjects[value];
212
            if (mKeys[col]==climateKey)
213
                mCurrentClimate = (Climate*)mCreatedObjects[value];
214
 
280 werner 215
        }
216
 
567 werner 217
    } else {
218
        if (mGridMode)
219
            throw IException(QString("Environment:setposition: invalid grid id (or not present in input file): %1m/%2m (mapped to id %3).")
220
                             .arg(position.x()).arg(position.y()).arg(id));
221
        else
222
            throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1m/%2m (mapped to indices %3/%4).")
223
                             .arg(position.x()).arg(position.y()).arg(ix).arg(iy));
224
    }
280 werner 225
}
567 werner 226
 
227
bool Environment::setGridMode(const QString &grid_file_name)
228
{
229
    mGrid = new GisGrid();
230
    mGrid->loadFromFile(grid_file_name);
231
    mGridMode = true;
232
    return true;
233
}