Rev 1083 | Rev 1088 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1083 | Rev 1085 | ||
---|---|---|---|
1 | Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/plugins/barkbeetle/barkbeetlescript.cpp': |
1 | Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/plugins/barkbeetle/barkbeetlescript.cpp': |
2 | /********************************************************************************************
|
2 | /********************************************************************************************
|
3 | ** iLand - an individual based forest landscape and disturbance model
|
3 | ** iLand - an individual based forest landscape and disturbance model
|
4 | ** http://iland.boku.ac.at
|
4 | ** http://iland.boku.ac.at
|
5 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl
|
5 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl
|
6 | **
|
6 | **
|
7 | ** This program is free software: you can redistribute it and/or modify
|
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
|
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
|
9 | ** the Free Software Foundation, either version 3 of the License, or
|
10 | ** (at your option) any later version.
|
10 | ** (at your option) any later version.
|
11 | **
|
11 | **
|
12 | ** This program is distributed in the hope that it will be useful,
|
12 | ** This program is distributed in the hope that it will be useful,
|
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 | ** GNU General Public License for more details.
|
15 | ** GNU General Public License for more details.
|
16 | **
|
16 | **
|
17 | ** You should have received a copy of the GNU General Public License
|
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/>.
|
18 | ** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19 | ********************************************************************************************/
|
19 | ********************************************************************************************/
|
20 | 20 | ||
21 | #include "barkbeetlescript.h"
|
21 | #include "barkbeetlescript.h"
|
22 | 22 | ||
23 | #include "barkbeetlemodule.h"
|
23 | #include "barkbeetlemodule.h"
|
24 | #include "outputmanager.h"
|
24 | #include "outputmanager.h"
|
25 | #include "helper.h"
|
25 | #include "helper.h"
|
26 | #include "spatialanalysis.h"
|
26 | #include "spatialanalysis.h"
|
27 | #include "scriptgrid.h"
|
27 | #include "scriptgrid.h"
|
28 | 28 | ||
29 | 29 | ||
30 | BarkBeetleScript::BarkBeetleScript(QObject *) |
30 | BarkBeetleScript::BarkBeetleScript(QObject *) |
31 | {
|
31 | {
|
32 | mBeetle = 0; |
32 | mBeetle = 0; |
33 | }
|
33 | }
|
34 | 34 | ||
35 | void BarkBeetleScript::test(QString value) |
35 | void BarkBeetleScript::test(QString value) |
36 | {
|
36 | {
|
37 | qDebug() << value; |
37 | qDebug() << value; |
38 | }
|
38 | }
|
39 | 39 | ||
40 | void BarkBeetleScript::init(QJSValue fun) |
40 | void BarkBeetleScript::init(QJSValue fun) |
41 | {
|
41 | {
|
42 | QJSValueList args = QJSValueList() << 0 << 0; |
42 | QJSValueList args = QJSValueList() << 0 << 0; |
43 | 43 | ||
44 | if (!fun.isCallable()) { |
44 | if (!fun.isCallable()) { |
45 | qDebug() << "no valid function in init!!"; |
45 | qDebug() << "no valid function in init!!"; |
46 | return; |
46 | return; |
47 | }
|
47 | }
|
48 | for (int y=0;y < mBeetle->mGrid.sizeY(); ++y) |
48 | for (int y=0;y < mBeetle->mGrid.sizeY(); ++y) |
49 | for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) { |
49 | for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) { |
50 | args[0] = x; args[1] = y; |
50 | args[0] = x; args[1] = y; |
51 | double result = fun.call(args).toNumber(); |
51 | double result = fun.call(args).toNumber(); |
52 | mBeetle->mGrid.valueAtIndex(x,y).n = result; |
52 | mBeetle->mGrid.valueAtIndex(x,y).n = result; |
53 | }
|
53 | }
|
54 | }
|
54 | }
|
55 | 55 | ||
56 | void BarkBeetleScript::run(QJSValue fun) |
56 | void BarkBeetleScript::run(QJSValue fun) |
57 | {
|
57 | {
|
58 | QJSValueList args = QJSValueList() << 0 << 0; |
58 | QJSValueList args = QJSValueList() << 0 << 0; |
59 | 59 | ||
60 | if (!fun.isCallable()) { |
60 | if (!fun.isCallable()) { |
61 | qDebug() << "no valid function in run!!"; |
61 | qDebug() << "no valid function in run!!"; |
62 | return; |
62 | return; |
63 | }
|
63 | }
|
64 | for (int y=0;y < mBeetle->mGrid.sizeY(); ++y) |
64 | for (int y=0;y < mBeetle->mGrid.sizeY(); ++y) |
65 | for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) { |
65 | for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) { |
66 | args[0] = x; args[1] = y; |
66 | args[0] = x; args[1] = y; |
67 | fun.call(args); |
67 | fun.call(args); |
68 | }
|
68 | }
|
69 | }
|
69 | }
|
70 | 70 | ||
71 | double BarkBeetleScript::pixelValue(int ix, int iy) |
71 | double BarkBeetleScript::pixelValue(int ix, int iy) |
72 | {
|
72 | {
|
73 | if (mBeetle->mGrid.isIndexValid(ix,iy)) { |
73 | if (mBeetle->mGrid.isIndexValid(ix,iy)) { |
74 | return mBeetle->mGrid.valueAtIndex(ix, iy).n; |
74 | return mBeetle->mGrid.valueAtIndex(ix, iy).n; |
75 | } else { |
75 | } else { |
76 | return -9999; |
76 | return -9999; |
77 | }
|
77 | }
|
78 | }
|
78 | }
|
79 | 79 | ||
80 | void BarkBeetleScript::setPixelValue(int ix, int iy, double val) |
80 | void BarkBeetleScript::setPixelValue(int ix, int iy, double val) |
81 | {
|
81 | {
|
82 | if (mBeetle->mGrid.isIndexValid(ix,iy)) { |
82 | if (mBeetle->mGrid.isIndexValid(ix,iy)) { |
83 | mBeetle->mGrid.valueAtIndex(ix, iy).n = val; |
83 | mBeetle->mGrid.valueAtIndex(ix, iy).n = val; |
84 | }
|
84 | }
|
85 | }
|
85 | }
|
86 | 86 | ||
87 | double BarkBeetleScript::generations(int ix, int iy) |
87 | double BarkBeetleScript::generations(int ix, int iy) |
88 | {
|
88 | {
|
89 | if (mBeetle->mGrid.isIndexValid(ix,iy)) { |
89 | if (mBeetle->mGrid.isIndexValid(ix,iy)) { |
90 | return mBeetle->mRUGrid.valueAt( mBeetle->mGrid.cellCenterPoint(QPoint(ix,iy)) ).generations; |
90 | return mBeetle->mRUGrid.valueAt( mBeetle->mGrid.cellCenterPoint(QPoint(ix,iy)) ).generations; |
91 | } else { |
91 | } else { |
92 | return -9999; |
92 | return -9999; |
93 | }
|
93 | }
|
94 | 94 | ||
95 | }
|
95 | }
|
96 | 96 | ||
97 | void BarkBeetleScript::reloadSettings() |
97 | void BarkBeetleScript::reloadSettings() |
98 | {
|
98 | {
|
99 | mBeetle->loadParameters(); |
99 | mBeetle->loadParameters(); |
100 | }
|
100 | }
|
101 | 101 | ||
102 | void BarkBeetleScript::newYear() |
102 | void BarkBeetleScript::newYear() |
103 | {
|
103 | {
|
104 | int y = mBeetle->manualYearBegin(); |
104 | int y = mBeetle->manualYearBegin(); |
105 | qDebug() << "Barkbeetle-module: year=" << y; |
105 | qDebug() << "Barkbeetle-module: year=" << y; |
106 | }
|
106 | }
|
107 | 107 | ||
108 | void BarkBeetleScript::runBB(int iteration) |
108 | void BarkBeetleScript::runBB(int iteration) |
109 | {
|
109 | {
|
110 | qDebug() << "running bark beetle module...."; |
110 | qDebug() << "running bark beetle module...."; |
111 | mBeetle->run(iteration); |
111 | mBeetle->run(iteration); |
112 | GlobalSettings::instance()->outputManager()->save(); // just make sure database outputs are properly written |
112 | GlobalSettings::instance()->outputManager()->save(); // just make sure database outputs are properly written |
113 | }
|
113 | }
|
114 | 114 | ||
115 | void BarkBeetleScript::clear() |
115 | void BarkBeetleScript::clear() |
116 | {
|
116 | {
|
117 | qDebug() << "clear bark beetle module...."; |
117 | qDebug() << "clear bark beetle module...."; |
118 | mBeetle->clearGrids(); |
118 | mBeetle->clearGrids(); |
119 | mBeetle->loadParameters(); |
119 | mBeetle->loadParameters(); |
120 | mBeetle->loadAllVegetation(); |
120 | mBeetle->loadAllVegetation(); |
121 | }
|
121 | }
|
122 | 122 | ||
123 | bool BarkBeetleScript::gridToFile(QString type, QString filename) |
123 | bool BarkBeetleScript::gridToFile(QString type, QString filename) |
124 | {
|
124 | {
|
125 | if (!GlobalSettings::instance()->model()) |
125 | if (!GlobalSettings::instance()->model()) |
126 | return false; |
126 | return false; |
127 | QString result;
|
127 | QString result;
|
128 | result = gridToESRIRaster(mBeetle->mLayers, type); // use a specific value function (see above) |
128 | result = gridToESRIRaster(mBeetle->mLayers, type); // use a specific value function (see above) |
129 | 129 | ||
130 | if (result.isEmpty()) { |
130 | if (result.isEmpty()) { |
131 | result = gridToESRIRaster(mBeetle->mRULayers, type); // try RU-level indicators |
131 | result = gridToESRIRaster(mBeetle->mRULayers, type); // try RU-level indicators |
132 | }
|
132 | }
|
133 | 133 | ||
134 | if (!result.isEmpty()) { |
134 | if (!result.isEmpty()) { |
135 | filename = GlobalSettings::instance()->path(filename); |
135 | filename = GlobalSettings::instance()->path(filename); |
136 | Helper::saveToTextFile(filename, result); |
136 | Helper::saveToTextFile(filename, result); |
137 | qDebug() << "saved grid to " << filename; |
137 | qDebug() << "saved grid to " << filename; |
138 | return true; |
138 | return true; |
139 | }
|
139 | }
|
140 | qDebug() << "could not save gridToFile because" << type << "is not a valid grid."; |
140 | qDebug() << "could not save gridToFile because" << type << "is not a valid grid."; |
141 | return false; |
141 | return false; |
142 | 142 | ||
143 | }
|
143 | }
|
144 | 144 | ||
145 | QJSValue BarkBeetleScript::grid(QString type) |
145 | QJSValue BarkBeetleScript::grid(QString type) |
146 | {
|
146 | {
|
147 | int idx = mBeetle->mLayers.indexOf(type); |
147 | int idx = mBeetle->mLayers.indexOf(type); |
148 | if (idx<0) |
148 | if (idx<0) |
149 | qDebug() << "ERROR: BarkBeetleScript:grid(): invalid grid" << type; |
149 | qDebug() << "ERROR: BarkBeetleScript:grid(): invalid grid" << type; |
150 | // this is a copy
|
150 | // this is a copy
|
151 | Grid<double> *damage_grid = mBeetle->mLayers.grid(idx); |
151 | Grid<double> *damage_grid = mBeetle->mLayers.grid(idx); |
152 | 152 | ||
153 | QJSValue g = ScriptGrid::createGrid(damage_grid, type); |
153 | QJSValue g = ScriptGrid::createGrid(damage_grid, type); |
154 | return g; |
154 | return g; |
155 | }
|
155 | }
|
156 | 156 | ||
157 | int BarkBeetleScript::damagedArea(int threshold, QString fileName) |
157 | int BarkBeetleScript::damagedArea(int threshold, QString fileName) |
158 | {
|
158 | {
|
159 | // get damage grid:
|
159 | // get damage grid:
|
160 | Grid<double> *damage_grid = mBeetle->mLayers.grid(mBeetle->mLayers.indexOf("dead")); |
160 | Grid<double> *damage_grid = mBeetle->mLayers.grid(mBeetle->mLayers.indexOf("dead")); |
161 | SpatialAnalysis spat;
|
161 | SpatialAnalysis spat;
|
162 | QVector<int> patches = spat.extractPatches(*damage_grid, threshold+1, fileName); |
- | |
- | 162 | QList<int> patches = spat.extractPatches(*damage_grid, threshold+1, fileName); |
|
163 | int n=0, size=0; |
163 | int n=0, size=0; |
164 | for (int i=0;i<patches.count();++i) |
164 | for (int i=0;i<patches.count();++i) |
165 | if (patches[i]>threshold) { |
165 | if (patches[i]>threshold) { |
166 | size+=patches[i]; |
166 | size+=patches[i]; |
167 | n++; |
167 | n++; |
168 | }
|
168 | }
|
169 | qDebug() << "BarkBeetleScript:damagedArea:" << n << "patches (area=" << size << ") above threshold" << threshold; |
169 | qDebug() << "BarkBeetleScript:damagedArea:" << n << "patches (area=" << size << ") above threshold" << threshold; |
170 | delete damage_grid; |
170 | delete damage_grid; |
171 | return size; |
171 | return size; |
172 | 172 | ||
173 | }
|
173 | }
|
174 | 174 | ||
175 | bool BarkBeetleScript::simulate() |
175 | bool BarkBeetleScript::simulate() |
176 | {
|
176 | {
|
177 | return mBeetle->simulate(); |
177 | return mBeetle->simulate(); |
178 | }
|
178 | }
|
179 | 179 | ||
180 | void BarkBeetleScript::setSimulate(bool do_simulate) |
180 | void BarkBeetleScript::setSimulate(bool do_simulate) |
181 | {
|
181 | {
|
182 | mBeetle->setSimulate(do_simulate); |
182 | mBeetle->setSimulate(do_simulate); |
183 | }
|
183 | }
|
184 | 184 | ||
185 | bool BarkBeetleScript::enabled() |
185 | bool BarkBeetleScript::enabled() |
186 | {
|
186 | {
|
187 | return mBeetle->enabled(); |
187 | return mBeetle->enabled(); |
188 | }
|
188 | }
|
189 | 189 | ||
190 | void BarkBeetleScript::setEnabled(bool do_set_enable) |
190 | void BarkBeetleScript::setEnabled(bool do_set_enable) |
191 | {
|
191 | {
|
192 | mBeetle->setEnabled(do_set_enable); |
192 | mBeetle->setEnabled(do_set_enable); |
193 | }
|
193 | }
|
194 | 194 |