Rev 269 | Rev 281 | 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 | { |
||
20 | if (mInfile) |
||
21 | delete mInfile; |
||
22 | } |
||
23 | |||
24 | bool Environment::loadFromFile(const QString &fileName) |
||
25 | { |
||
26 | QString source = Helper::loadTextFile(fileName); |
||
27 | if (source.isEmpty()) |
||
28 | throw IException(QString("Environment: input file does not exist or is empty (%1)").arg(fileName)); |
||
29 | return loadFromString(source); |
||
30 | } |
||
31 | |||
32 | bool Environment::loadFromString(const QString &source) |
||
33 | { |
||
34 | try { |
||
35 | if (mInfile) |
||
36 | delete mInfile; |
||
37 | mInfile = new CSVFile(); |
||
38 | |||
39 | mInfile->loadFromString(source); |
||
40 | mKeys = mInfile->captions(); |
||
41 | |||
42 | XmlHelper xml(GlobalSettings::instance()->settings()); |
||
43 | mSpeciesSets.clear(); // note: the objects are not destroyed - potential memory leak. |
||
44 | mClimate.clear(); |
||
45 | mRowCoordinates.clear(); |
||
46 | int index; |
||
47 | // setup coordinates (x,y) |
||
48 | int ix,iy; |
||
49 | ix = mInfile->columnIndex("x"); |
||
50 | iy = mInfile->columnIndex("y"); |
||
51 | if (ix<0 || iy<0) |
||
52 | throw IException("Environment:: input file has no x/y coordinates!"); |
||
53 | for (int row=0;row<mInfile->rowCount();row++) { |
||
54 | QString key=QString("%1_%2") |
||
55 | .arg(mInfile->value(row, ix).toString()) |
||
56 | .arg(mInfile->value(row, iy).toString()); |
||
57 | mRowCoordinates[key] = row; |
||
58 | } |
||
59 | |||
60 | // ******** specific keys ******* |
||
61 | const QString speciesKey = "model.species.source"; |
||
62 | const QString climateKey = "model.climate.tableName"; |
||
63 | |||
64 | // ******** setup of Species Sets ******* |
||
65 | if ((index = mKeys.indexOf(speciesKey))>-1) { |
||
66 | DebugTimer t("environment:load species"); |
||
67 | QStringList speciesNames = mInfile->column(index); |
||
68 | speciesNames.removeDuplicates(); |
||
69 | qDebug() << "creating species sets:" << speciesNames; |
||
70 | foreach (const QString &name, speciesNames) { |
||
71 | xml.setNodeValue(speciesKey,name); // set xml value |
||
72 | // create species sets |
||
73 | SpeciesSet *set = new SpeciesSet(); |
||
74 | set->setup(); |
||
75 | mSpeciesSets.push_back(set); |
||
76 | } |
||
77 | qDebug() << mSpeciesSets.count() << "species sets created."; |
||
78 | } |
||
79 | |||
80 | // ******** setup of Climate ******* |
||
81 | if ((index = mKeys.indexOf(climateKey))>-1) { |
||
82 | DebugTimer t("environment:load climate"); |
||
83 | QStringList climateNames = mInfile->column(index); |
||
84 | climateNames.removeDuplicates(); |
||
85 | qDebug() << "creating climatae: " << climateNames; |
||
86 | foreach (QString name, climateNames) { |
||
87 | xml.setNodeValue(climateKey,name); // set xml value |
||
88 | // create climate sets |
||
89 | Climate *climate = new Climate(); |
||
90 | climate->setup(); |
||
91 | mClimate.push_back(climate); |
||
92 | } |
||
93 | qDebug() << mClimate.count() << "climates created"; |
||
94 | } |
||
95 | return true; |
||
96 | |||
97 | } catch(const IException &e) { |
||
98 | QString error_msg = QString("An error occured during the setup of the environment: \n%1").arg(e.toString()); |
||
99 | qDebug() << error_msg; |
||
100 | Helper::msg(error_msg); |
||
101 | return false; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | Climate *Environment::climate() |
||
106 | { |
||
107 | |||
108 | } |
||
109 | |||
110 | SpeciesSet *Environment::speciesSet() |
||
111 | { |
||
112 | |||
113 | } |
||
114 | |||
115 | void Environment::setPosition(const QPointF position) |
||
116 | { |
||
117 | if (!mInfile) |
||
118 | throw IException("Environment:setposition: input file not loaded."); |
||
119 | |||
120 | int ix, iy; |
||
121 | ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate |
||
122 | iy = int(position.y() / 100.); |
||
123 | QString key=QString("%1_%2").arg(ix).arg(iy); |
||
124 | if (mRowCoordinates.contains(key)) { |
||
125 | XmlHelper xml(GlobalSettings::instance()->settings()); |
||
126 | int row = mRowCoordinates[key]; |
||
127 | QString value; |
||
128 | qDebug() << "settting up point" << position << "with row" << row; |
||
129 | for (int col=0;col<mInfile->colCount(); col++) { |
||
130 | if (mKeys[col]=="x" || mKeys[col]=="y") |
||
131 | continue; |
||
132 | value = mInfile->value(row,col).toString(); |
||
133 | qDebug() << "set" << mKeys[col] << "to" << value; |
||
134 | xml.setNodeValue(mKeys[col], value); |
||
135 | } |
||
136 | |||
137 | } else |
||
138 | throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1/%2").arg(position.x()).arg(position.y())); |
||
139 | } |