Rev 290 | Rev 299 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | |||
106 | Werner | 2 | #include "global.h" |
3 | #include "standloader.h" |
||
4 | |||
290 | werner | 5 | |
106 | Werner | 6 | #include "grid.h" |
7 | #include "model.h" |
||
189 | iland | 8 | #include "resourceunit.h" |
106 | Werner | 9 | #include "speciesset.h" |
10 | |||
11 | #include "helper.h" |
||
290 | werner | 12 | #include "random.h" |
137 | Werner | 13 | #include "expression.h" |
140 | Werner | 14 | #include "expressionwrapper.h" |
284 | werner | 15 | #include "environment.h" |
287 | werner | 16 | #include "csvfile.h" |
106 | Werner | 17 | |
18 | |||
155 | werner | 19 | QStringList picusSpeciesIds = QStringList() << "0" << "1" << "17"; |
20 | QStringList iLandSpeciesIds = QStringList() << "piab" << "piab" << "fasy"; |
||
290 | werner | 21 | StandLoader::~StandLoader() |
22 | { |
||
23 | if (mRandom) |
||
24 | delete mRandom; |
||
25 | } |
||
106 | Werner | 26 | |
281 | werner | 27 | void StandLoader::loadForUnit() |
28 | { |
||
106 | Werner | 29 | |
281 | werner | 30 | } |
106 | Werner | 31 | |
281 | werner | 32 | void StandLoader::copyTrees() |
33 | { |
||
34 | // we assume that all stands are equal, so wie simply COPY the trees and modify them afterwards |
||
35 | const Grid<ResourceUnit*> &ruGrid=mModel->RUgrid(); |
||
36 | ResourceUnit **p = ruGrid.begin(); |
||
37 | if (!p) |
||
38 | throw IException("Standloader: invalid resource unit pointer!"); |
||
39 | ++p; // skip the first... |
||
40 | const QVector<Tree> &tocopy = mModel->ru()->trees(); |
||
41 | for (; p!=ruGrid.end(); ++p) { |
||
42 | QRectF rect = (*p)->boundingBox(); |
||
43 | foreach(const Tree& tree, tocopy) { |
||
44 | Tree &newtree = (*p)->newTree(); |
||
45 | newtree = tree; // copy tree data... |
||
46 | newtree.setPosition(tree.position()+(*p)->boundingBox().topLeft()); |
||
47 | newtree.setRU(*p); |
||
48 | newtree.setNewId(); |
||
49 | } |
||
50 | } |
||
51 | qDebug() << Tree::statCreated() << "trees loaded / copied."; |
||
52 | } |
||
53 | |||
284 | werner | 54 | /** main routine of the stand setup. |
55 | */ |
||
106 | Werner | 56 | void StandLoader::processInit() |
57 | { |
||
58 | GlobalSettings *g = GlobalSettings::instance(); |
||
192 | werner | 59 | XmlHelper xml(g->settings().node("model.initialization")); |
191 | werner | 60 | |
281 | werner | 61 | QString copy_mode = xml.value("mode", "copy"); |
62 | QString type = xml.value("type", ""); |
||
192 | werner | 63 | QString fileName = xml.value("file", ""); |
106 | Werner | 64 | |
65 | Tree::resetStatistics(); |
||
281 | werner | 66 | |
67 | // one global init-file for the whole area: |
||
284 | werner | 68 | if (copy_mode=="single") { |
281 | werner | 69 | loadInitFile(fileName, type); |
70 | return; |
||
284 | werner | 71 | } |
281 | werner | 72 | |
73 | // copy trees from first unit to all other units: |
||
74 | if (copy_mode=="copy") { |
||
75 | loadInitFile(fileName, type); |
||
76 | copyTrees(); |
||
77 | return; |
||
106 | Werner | 78 | } |
137 | Werner | 79 | |
281 | werner | 80 | // call a single tree init for each resource unit |
81 | if (copy_mode=="unit") { |
||
284 | werner | 82 | foreach( const ResourceUnit *const_ru, g->model()->ruList()) { |
83 | ResourceUnit *ru = const_cast<ResourceUnit*>(const_ru); |
||
84 | // set environment |
||
85 | g->model()->environment()->setPosition(ru->boundingBox().center()); |
||
86 | type = xml.value("type", ""); |
||
87 | fileName = xml.value("file", ""); |
||
88 | loadInitFile(fileName, type, ru); |
||
89 | qDebug() << "loaded" << fileName << "on" << ru->boundingBox() << "," << ru->trees().count() << "trees."; |
||
90 | } |
||
91 | return; |
||
281 | werner | 92 | } |
93 | throw IException("StandLoader::processInit: invalid initalization.mode!"); |
||
94 | } |
||
95 | |||
96 | void StandLoader::evaluateDebugTrees() |
||
97 | { |
||
137 | Werner | 98 | // evaluate debugging |
99 | QString dbg_str = GlobalSettings::instance()->settings().paramValueString("debug_tree"); |
||
100 | if (!dbg_str.isEmpty()) { |
||
140 | Werner | 101 | TreeWrapper tw; |
102 | Expression dexp(dbg_str, &tw); // load expression dbg_str and enable external model variables |
||
137 | Werner | 103 | AllTreeIterator at(GlobalSettings::instance()->model()); |
104 | double result; |
||
105 | while (Tree *t = at.next()) { |
||
140 | Werner | 106 | tw.setTree(t); |
137 | Werner | 107 | result = dexp.execute(); |
108 | if (result) |
||
109 | t->enableDebugging(); |
||
110 | } |
||
111 | } |
||
106 | Werner | 112 | } |
113 | |||
284 | werner | 114 | void StandLoader::loadInitFile(const QString &fileName, const QString &type, ResourceUnit *ru) |
281 | werner | 115 | { |
284 | werner | 116 | QString pathFileName = GlobalSettings::instance()->path(fileName, "init"); |
117 | if (!QFile::exists(pathFileName)) |
||
118 | throw IException(QString("StandLoader::loadInitFile: File %1 does not exist!").arg(pathFileName)); |
||
281 | werner | 119 | |
120 | if (type=="picus") |
||
294 | werner | 121 | return loadPicusFile(pathFileName, ru); |
281 | werner | 122 | if (type=="iland") |
287 | werner | 123 | return loadiLandFile(pathFileName, ru); |
281 | werner | 124 | |
125 | throw IException("StandLoader::loadInitFile: unknown initalization.type!"); |
||
126 | } |
||
127 | |||
294 | werner | 128 | void StandLoader::loadPicusFile(const QString &fileName, ResourceUnit *ru) |
106 | Werner | 129 | { |
294 | werner | 130 | QString content = Helper::loadTextFile(fileName); |
131 | if (content.isEmpty()) { |
||
132 | qDebug() << "file not found: " + fileName; |
||
133 | return; |
||
134 | } |
||
135 | loadSingleTreeList(content, ru, fileName); |
||
136 | } |
||
137 | |||
138 | void StandLoader::loadSingleTreeList(const QString &content, ResourceUnit*ru, const QString &fileName) |
||
139 | { |
||
106 | Werner | 140 | if (!ru) |
141 | ru = mModel->ru(); |
||
284 | werner | 142 | Q_ASSERT(ru!=0); |
106 | Werner | 143 | |
284 | werner | 144 | QPointF offset = ru->boundingBox().topLeft(); |
145 | SpeciesSet *speciesSet = ru->speciesSet(); // of default RU |
||
146 | |||
106 | Werner | 147 | // cut out the <trees> </trees> part.... |
148 | QRegExp rx(".*<trees>(.*)</trees>.*"); |
||
294 | werner | 149 | rx.indexIn(content, 0); |
106 | Werner | 150 | if (rx.capturedTexts().count()<1) |
151 | return; |
||
294 | werner | 152 | QString my_content = rx.cap(1).trimmed(); |
153 | QStringList lines=my_content.split('\n'); |
||
106 | Werner | 154 | if (lines.count()<2) |
155 | return; |
||
156 | char sep='\t'; |
||
157 | if (!lines[0].contains(sep)) |
||
158 | sep=';'; |
||
155 | werner | 159 | QStringList headers = lines[0].trimmed().split(sep); |
138 | Werner | 160 | |
247 | werner | 161 | int iID = headers.indexOf("id"); |
106 | Werner | 162 | int iX = headers.indexOf("x"); |
163 | int iY = headers.indexOf("y"); |
||
164 | int iBhd = headers.indexOf("bhdfrom"); |
||
165 | int iHeight = headers.indexOf("treeheight"); |
||
166 | int iSpecies = headers.indexOf("species"); |
||
170 | werner | 167 | int iAge = headers.indexOf("age"); |
204 | werner | 168 | if (iX==-1 || iY==-1 || iBhd==-1 || iSpecies==-1 || iHeight==-1) |
138 | Werner | 169 | throw IException(QString("Initfile %1 is not valid!").arg(fileName)); |
106 | Werner | 170 | |
138 | Werner | 171 | double dbh; |
106 | Werner | 172 | for (int i=1;i<lines.count();i++) { |
173 | QString &line = lines[i]; |
||
138 | Werner | 174 | dbh = line.section(sep, iBhd, iBhd).toDouble(); |
175 | if (dbh<5.) |
||
176 | continue; |
||
285 | werner | 177 | |
106 | Werner | 178 | QPointF f; |
179 | if (iX>=0 && iY>=0) { |
||
180 | f.setX( line.section(sep, iX, iX).toDouble() ); |
||
181 | f.setY( line.section(sep, iY, iY).toDouble() ); |
||
182 | f+=offset; |
||
285 | werner | 183 | |
106 | Werner | 184 | } |
285 | werner | 185 | // position valid? |
186 | if (!mModel->heightGrid()->valueAt(f).isValid()) |
||
187 | continue; |
||
188 | Tree &tree = ru->newTree(); |
||
189 | tree.setPosition(f); |
||
247 | werner | 190 | if (iID>=0) |
191 | tree.setId(line.section(sep, iID, iID).toInt() ); |
||
106 | Werner | 192 | |
138 | Werner | 193 | tree.setDbh(dbh); |
194 | tree.setHeight(line.section(sep, iHeight, iHeight).toDouble()/100.); // convert from Picus-cm to m. |
||
204 | werner | 195 | if (iAge>=0) |
196 | tree.setAge(line.section(sep, iAge, iAge).toInt()); |
||
197 | else |
||
198 | tree.setAge(10); |
||
270 | werner | 199 | QString speciesid = line.section(sep, iSpecies, iSpecies); |
200 | bool ok; |
||
201 | int picusid = speciesid.toInt(&ok); |
||
202 | if (ok) { |
||
203 | int idx = picusSpeciesIds.indexOf(line.section(sep, iSpecies, iSpecies)); |
||
204 | if (idx==-1) |
||
205 | throw IException(QString("Loading init-file: invalid Picus-species-id. Species: %1").arg(picusid)); |
||
138 | Werner | 206 | speciesid = iLandSpeciesIds[idx]; |
270 | werner | 207 | } |
138 | Werner | 208 | Species *s = speciesSet->species(speciesid); |
209 | if (!ru || !s) |
||
270 | werner | 210 | throw IException(QString("Loading init-file: either ressource unit or species invalid. Species: %1").arg(speciesid)); |
107 | Werner | 211 | |
138 | Werner | 212 | tree.setRU(ru); |
213 | tree.setSpecies(s); |
||
106 | Werner | 214 | tree.setup(); |
215 | } |
||
216 | //qDebug() << "loaded init-file contained" << lines.count() <<"lines."; |
||
217 | //qDebug() << "lines: " << lines; |
||
218 | } |
||
287 | werner | 219 | |
294 | werner | 220 | void StandLoader::loadDistributionList(const QString &content, ResourceUnit *ru, const QString &fileName) |
287 | werner | 221 | { |
222 | if (!ru) |
||
223 | ru = mModel->ru(); |
||
224 | Q_ASSERT(ru!=0); |
||
225 | SpeciesSet *speciesSet = ru->speciesSet(); // of default RU |
||
226 | Q_ASSERT(speciesSet!=0); |
||
227 | |||
228 | DebugTimer t("StandLoader::loadiLandFile"); |
||
294 | werner | 229 | CSVFile infile; |
230 | infile.loadFromString(content); |
||
231 | |||
287 | werner | 232 | int icount = infile.columnIndex("count"); |
233 | int ispecies = infile.columnIndex("species"); |
||
234 | int idbh_from = infile.columnIndex("dbh_from"); |
||
235 | int idbh_to = infile.columnIndex("dbh_to"); |
||
236 | int ihd = infile.columnIndex("hd"); |
||
237 | int iage = infile.columnIndex("age"); |
||
238 | if (icount<0 || ispecies<0 || idbh_from<0 || idbh_to<0 || ihd<0 || iage<0) |
||
239 | throw IException(QString("load-ini-file: file '%1' containts not all required fields (count, species, dbh_from, dbh_to, hd, age).").arg(fileName)); |
||
240 | |||
294 | werner | 241 | mInitItems.clear(); |
287 | werner | 242 | InitFileItem item; |
243 | for (int row=0;row<infile.rowCount();row++) { |
||
244 | item.count = infile.value(row, icount).toInt(); |
||
245 | item.dbh_from = infile.value(row, idbh_from).toDouble(); |
||
246 | item.dbh_to = infile.value(row, idbh_to).toDouble(); |
||
247 | item.hd = infile.value(row, ihd).toDouble(); |
||
248 | item.age = infile.value(row, iage).toInt(); |
||
249 | item.species = speciesSet->species(infile.value(row, ispecies).toString()); |
||
250 | if (!item.species) { |
||
251 | throw IException(QString("load-ini-file: unknown speices '%1' in file '%2', line %3.") |
||
252 | .arg(infile.value(row, ispecies).toString()) |
||
253 | .arg(fileName) |
||
254 | .arg(row)); |
||
255 | } |
||
290 | werner | 256 | mInitItems.push_back(item); |
287 | werner | 257 | } |
290 | werner | 258 | // setup the random distribution |
259 | QString density_func = GlobalSettings::instance()->settings().value("model.initialization.randomFunction", "1-x^2"); |
||
260 | qDebug() << "density function:" << density_func; |
||
261 | if (!mRandom || (mRandom->densityFunction()!= density_func)) { |
||
262 | if (mRandom) |
||
263 | delete mRandom; |
||
264 | mRandom=new RandomCustomPDF(density_func); |
||
265 | qDebug() << "new probabilty density function:" << density_func; |
||
266 | } |
||
267 | |||
287 | werner | 268 | // exeucte the |
269 | executeiLandInit(ru); |
||
290 | werner | 270 | ru->cleanTreeList(); |
287 | werner | 271 | |
272 | } |
||
273 | |||
294 | werner | 274 | void StandLoader::loadiLandFile(const QString &fileName, ResourceUnit *ru) |
275 | { |
||
276 | if (!QFile::exists(fileName)) |
||
277 | throw IException(QString("load-ini-file: file '%1' does not exist.").arg(fileName)); |
||
278 | QString content = Helper::loadTextFile(fileName); |
||
279 | loadDistributionList(content, ru, fileName); |
||
280 | } |
||
281 | |||
287 | werner | 282 | // evenlist: tentative order of pixel-indices (within a 5x5 grid) used as tree positions. |
283 | // e.g. 12 = centerpixel, 0: upper left corner, ... |
||
284 | int evenlist[25] = { 12, 6, 18, 16, 8, 22, 2, 10, 14, 0, 24, 20, 4, |
||
285 | 1, 13, 15, 19, 21, 3, 7, 11, 17, 23, 5, 9}; |
||
290 | werner | 286 | int unevenlist[25] = { 11,13,7,17, 1,19,5,21, 9,23,3,15, |
287 | 6,18,2,10,4,24,12,0,8,14,20,22}; |
||
288 | |||
289 | |||
287 | werner | 290 | // sort function |
291 | bool sortPairLessThan(const QPair<int, double> &s1, const QPair<int, double> &s2) |
||
292 | { |
||
293 | return s1.second < s2.second; |
||
294 | } |
||
290 | werner | 295 | void StandLoader::executeiLandInit(ResourceUnit *ru) |
287 | werner | 296 | { |
290 | werner | 297 | |
287 | werner | 298 | QPointF offset = ru->boundingBox().topLeft(); |
299 | QPoint offsetIdx = GlobalSettings::instance()->model()->grid()->indexAt(offset); |
||
300 | |||
301 | // a multimap holds a list for all trees. |
||
302 | // key is the index of a 10x10m pixel within the resource unit |
||
303 | QMultiMap<int, int> tree_map; |
||
304 | QVector<QPair<int, double> > tcount; // counts |
||
305 | for (int i=0;i<100;i++) |
||
306 | tcount.push_back(QPair<int,double>(i,0.)); |
||
307 | |||
308 | int key; |
||
288 | werner | 309 | |
290 | werner | 310 | foreach(const InitFileItem &item, mInitItems) { |
287 | werner | 311 | for (int i=0;i<item.count;i++) { |
312 | // create trees |
||
313 | int tree_idx = ru->newTreeIndex(); |
||
314 | Tree &tree = ru->trees()[tree_idx]; // get reference to modify tree |
||
315 | tree.setDbh(nrandom(item.dbh_from, item.dbh_to)); |
||
316 | tree.setHeight(tree.dbh()/100. * item.hd); // dbh from cm->m, *hd-ratio -> meter height |
||
317 | tree.setSpecies(item.species); |
||
318 | tree.setAge(item.age); |
||
319 | tree.setRU(ru); |
||
320 | tree.setup(); |
||
321 | // key: rank of target pixel |
||
322 | // first: index of target pixel |
||
323 | // second: sum of target pixel |
||
290 | werner | 324 | key = limit(int(100*mRandom->get()), 0, 99); // get from random number generator |
287 | werner | 325 | tree_map.insert(tcount[key].first, tree_idx); // store tree in map |
326 | tcount[key].second+=tree.basalArea(); // aggregate the basal area for each 10m pixel |
||
290 | werner | 327 | if (i%30==0) |
287 | werner | 328 | qSort(tcount.begin(), tcount.end(), sortPairLessThan); |
329 | } |
||
330 | qSort(tcount.begin(), tcount.end(), sortPairLessThan); |
||
331 | } |
||
332 | |||
333 | int bits, index, pos; |
||
334 | int c; |
||
335 | QList<int> trees; |
||
336 | QPoint tree_pos; |
||
289 | werner | 337 | |
287 | werner | 338 | for (int i=0;i<100;i++) { |
339 | trees = tree_map.values(i); |
||
340 | c = trees.count(); |
||
290 | werner | 341 | QPointF pixel_center = ru->boundingBox().topLeft() + QPointF((i/10)*10. + 5., (i%10)*10. + 5.); |
342 | if (!mModel->heightGrid()->valueAt(pixel_center).isValid()) { |
||
343 | // no trees on that pixel: let trees die |
||
344 | foreach(int tree_idx, trees) { |
||
345 | ru->trees()[tree_idx].die(); |
||
346 | } |
||
347 | continue; |
||
348 | } |
||
349 | |||
287 | werner | 350 | bits = 0; |
351 | index = -1; |
||
290 | werner | 352 | double r; |
287 | werner | 353 | foreach(int tree_idx, trees) { |
354 | if (c>18) { |
||
355 | index = (index + 1)%25; |
||
356 | } else { |
||
357 | int stop=1000; |
||
290 | werner | 358 | index = 0; |
287 | werner | 359 | do { |
290 | werner | 360 | //r = drandom(); |
361 | //if (r<0.5) // skip position with a prob. of 50% -> adds a little "noise" |
||
362 | // index++; |
||
363 | //index = (index + 1)%25; // increase and roll over |
||
364 | |||
365 | // search a random position |
||
366 | r = drandom(); |
||
367 | index = limit(int(25 * r*r), 0, 24); // use rnd()^2 to search for locations -> higher number of low indices (i.e. 50% of lookups in first 25% of locations) |
||
287 | werner | 368 | } while (isBitSet(bits, index)==true && stop--); |
369 | if (!stop) |
||
370 | qDebug() << "executeiLandInit: found no free bit."; |
||
371 | setBit(bits, index, true); // mark position as used |
||
372 | } |
||
290 | werner | 373 | // get position from fixed lists (one for even, one for uneven resource units) |
374 | pos = ru->index()%2?evenlist[index]:unevenlist[index]; |
||
287 | werner | 375 | tree_pos = offsetIdx // position of resource unit |
376 | + QPoint(5*(i/10), 5*(i%10)) // relative position of 10x10m pixel |
||
377 | + QPoint(pos/5, pos%5); // relative position within 10x10m pixel |
||
378 | //qDebug() << tree_no++ << "to" << index; |
||
379 | ru->trees()[tree_idx].setPosition(tree_pos); |
||
380 | } |
||
381 | } |
||
382 | } |