Rev 315 | Rev 318 | 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 | set->setup(); |
||
80 | mSpeciesSets.push_back(set); |
||
281 | werner | 81 | mCreatedObjects[name] = (void*)set; |
280 | werner | 82 | } |
83 | qDebug() << mSpeciesSets.count() << "species sets created."; |
||
314 | werner | 84 | } else { |
85 | // no species sets specified |
||
86 | SpeciesSet *speciesSet = new SpeciesSet(); |
||
315 | werner | 87 | speciesSet->setup(); |
314 | werner | 88 | mSpeciesSets.push_back(speciesSet); |
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 | climate->setup(); |
||
103 | mClimate.push_back(climate); |
||
316 | werner | 104 | mCreatedObjects[name]=(void*)climate; |
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(); |
||
110 | c->setup(); |
||
111 | mClimate.push_back(c); |
||
112 | mCurrentClimate = c; |
||
280 | werner | 113 | } |
114 | return true; |
||
115 | |||
116 | } catch(const IException &e) { |
||
117 | QString error_msg = QString("An error occured during the setup of the environment: \n%1").arg(e.toString()); |
||
118 | qDebug() << error_msg; |
||
119 | Helper::msg(error_msg); |
||
120 | return false; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | |||
125 | void Environment::setPosition(const QPointF position) |
||
126 | { |
||
281 | werner | 127 | // no changes occur, when the "environment" is not loaded |
128 | if (!isSetup()) |
||
129 | return; |
||
280 | werner | 130 | |
131 | int ix, iy; |
||
132 | ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate |
||
133 | iy = int(position.y() / 100.); |
||
134 | QString key=QString("%1_%2").arg(ix).arg(iy); |
||
135 | if (mRowCoordinates.contains(key)) { |
||
136 | XmlHelper xml(GlobalSettings::instance()->settings()); |
||
137 | int row = mRowCoordinates[key]; |
||
138 | QString value; |
||
139 | qDebug() << "settting up point" << position << "with row" << row; |
||
140 | for (int col=0;col<mInfile->colCount(); col++) { |
||
281 | werner | 141 | if (mKeys[col]=="x" || mKeys[col]=="y") // ignore "x" and "y" keys |
280 | werner | 142 | continue; |
143 | value = mInfile->value(row,col).toString(); |
||
144 | qDebug() << "set" << mKeys[col] << "to" << value; |
||
145 | xml.setNodeValue(mKeys[col], value); |
||
281 | werner | 146 | // special handling for constructed objects: |
147 | if (mKeys[col]==speciesKey) |
||
148 | mCurrentSpeciesSet = (SpeciesSet*)mCreatedObjects[value]; |
||
149 | if (mKeys[col]==climateKey) |
||
150 | mCurrentClimate = (Climate*)mCreatedObjects[value]; |
||
151 | |||
280 | werner | 152 | } |
153 | |||
316 | werner | 154 | } //else |
155 | // throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1/%2").arg(position.x()).arg(position.y())); |
||
280 | werner | 156 | } |