Subversion Repositories public iLand

Rev

Rev 319 | Rev 550 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
269 werner 2
#include "global.h"
3
#include "environment.h"
280 werner 4
#include "helper.h"
5
#include "csvfile.h"
269 werner 6
 
7
 
8
#include "climate.h"
280 werner 9
#include "speciesset.h"
269 werner 10
 
11
/** Represents the physical simulation site with regard to climate, soil properties and such.
12
    Data is read from various sources and presented to the core model with a standardized interface.
13
*/
14
Environment::Environment()
15
{
280 werner 16
    mInfile=0;
269 werner 17
}
280 werner 18
Environment::~Environment()
19
{
314 werner 20
    if (mInfile) {
280 werner 21
        delete mInfile;
314 werner 22
    }
280 werner 23
}
24
 
25
bool Environment::loadFromFile(const QString &fileName)
26
{
284 werner 27
    QString source = Helper::loadTextFile(GlobalSettings::instance()->path(fileName));
280 werner 28
    if (source.isEmpty())
29
        throw IException(QString("Environment: input file does not exist or is empty (%1)").arg(fileName));
30
    return loadFromString(source);
31
}
32
 
281 werner 33
// ******** specific keys *******
34
const QString speciesKey = "model.species.source";
35
const QString climateKey = "model.climate.tableName";
36
 
280 werner 37
bool Environment::loadFromString(const QString &source)
38
{
39
    try {
40
        if (mInfile)
41
            delete mInfile;
42
        mInfile = new CSVFile();
43
 
44
        mInfile->loadFromString(source);
45
        mKeys = mInfile->captions();
46
 
47
        XmlHelper xml(GlobalSettings::instance()->settings());
48
        mSpeciesSets.clear(); // note: the objects are not destroyed - potential memory leak.
49
        mClimate.clear();
50
        mRowCoordinates.clear();
281 werner 51
        mCreatedObjects.clear();
52
 
280 werner 53
        int index;
54
        // setup coordinates (x,y)
55
        int ix,iy;
56
        ix = mInfile->columnIndex("x");
57
        iy = mInfile->columnIndex("y");
58
        if (ix<0 || iy<0)
59
            throw IException("Environment:: input file has no x/y coordinates!");
60
        for (int row=0;row<mInfile->rowCount();row++) {
61
            QString key=QString("%1_%2")
62
                        .arg(mInfile->value(row, ix).toString())
63
                        .arg(mInfile->value(row, iy).toString());
64
            mRowCoordinates[key] = row;
65
        }
66
 
67
 
281 werner 68
 
280 werner 69
        // ******** setup of Species Sets *******
70
        if ((index = mKeys.indexOf(speciesKey))>-1) {
71
            DebugTimer t("environment:load species");
72
            QStringList speciesNames = mInfile->column(index);
73
            speciesNames.removeDuplicates();
74
            qDebug() << "creating species sets:" << speciesNames;
75
            foreach (const QString &name, speciesNames) {
76
                xml.setNodeValue(speciesKey,name); // set xml value
77
                // create species sets
78
                SpeciesSet *set = new SpeciesSet();
79
                mSpeciesSets.push_back(set);
281 werner 80
                mCreatedObjects[name] = (void*)set;
318 werner 81
                set->setup();
280 werner 82
            }
83
            qDebug() << mSpeciesSets.count() << "species sets created.";
314 werner 84
        } else {
85
            // no species sets specified
86
            SpeciesSet *speciesSet = new SpeciesSet();
318 werner 87
            mSpeciesSets.push_back(speciesSet);
315 werner 88
            speciesSet->setup();
314 werner 89
            mCurrentSpeciesSet = speciesSet;
280 werner 90
        }
91
 
92
        // ******** setup of Climate *******
93
        if ((index = mKeys.indexOf(climateKey))>-1) {
94
            DebugTimer t("environment:load climate");
95
            QStringList climateNames = mInfile->column(index);
96
            climateNames.removeDuplicates();
97
            qDebug() << "creating climatae: " << climateNames;
98
            foreach (QString name, climateNames) {
99
                xml.setNodeValue(climateKey,name); // set xml value
100
                // create climate sets
101
                Climate *climate = new Climate();
102
                mClimate.push_back(climate);
316 werner 103
                mCreatedObjects[name]=(void*)climate;
318 werner 104
                climate->setup();
280 werner 105
            }
106
            qDebug() << mClimate.count() << "climates created";
314 werner 107
        } else {
108
            // no climate defined - setup default climate
109
            Climate *c = new Climate();
318 werner 110
            mClimate.push_back(c);
314 werner 111
            c->setup();
112
            mCurrentClimate = c;
280 werner 113
        }
114
        return true;
115
 
116
    } catch(const IException &e) {
318 werner 117
        QString addMsg;
118
        if (!mClimate.isEmpty())
119
            addMsg = QString("last Climate: %1 ").arg(mClimate.last()->name());
120
        if (!mSpeciesSets.isEmpty())
121
            addMsg += QString("last Speciesset table: %1").arg(mSpeciesSets.last()->name());
122
        QString error_msg = QString("An error occured during the setup of the environment: \n%1\n%2").arg(e.toString()).arg(addMsg);
280 werner 123
        qDebug() << error_msg;
124
        Helper::msg(error_msg);
125
        return false;
126
    }
127
}
128
 
340 werner 129
/** sets the "pointer" to a "position" (metric coordinates).
130
    All specified values are set (also the climate/species-set pointers).
131
*/
280 werner 132
void Environment::setPosition(const QPointF position)
133
{
281 werner 134
    // no changes occur, when the "environment" is not loaded
135
    if (!isSetup())
136
        return;
280 werner 137
 
138
    int ix, iy;
139
    ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate
140
    iy = int(position.y() / 100.);
141
    QString key=QString("%1_%2").arg(ix).arg(iy);
142
    if (mRowCoordinates.contains(key)) {
143
        XmlHelper xml(GlobalSettings::instance()->settings());
144
        int row = mRowCoordinates[key];
145
        QString value;
146
        qDebug() << "settting up point" << position << "with row" << row;
147
        for (int col=0;col<mInfile->colCount(); col++) {
281 werner 148
            if (mKeys[col]=="x" || mKeys[col]=="y") // ignore "x" and "y" keys
280 werner 149
                continue;
150
            value = mInfile->value(row,col).toString();
151
            qDebug() << "set" << mKeys[col] << "to" << value;
152
            xml.setNodeValue(mKeys[col], value);
281 werner 153
            // special handling for constructed objects:
154
            if (mKeys[col]==speciesKey)
155
                mCurrentSpeciesSet = (SpeciesSet*)mCreatedObjects[value];
156
            if (mKeys[col]==climateKey)
157
                mCurrentClimate = (Climate*)mCreatedObjects[value];
158
 
280 werner 159
        }
160
 
319 werner 161
    } else
162
        throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1m/%2m (mapped to indices %3/%4).")
163
                         .arg(position.x()).arg(position.y()).arg(ix).arg(iy));
280 werner 164
}