Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1033 | werner | 1 | /******************************************************************************************** |
2 | ** iLand - an individual based forest landscape and disturbance model |
||
3 | ** http://iland.boku.ac.at |
||
4 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl |
||
5 | ** |
||
6 | ** This program is free software: you can redistribute it and/or modify |
||
7 | ** it under the terms of the GNU General Public License as published by |
||
8 | ** the Free Software Foundation, either version 3 of the License, or |
||
9 | ** (at your option) any later version. |
||
10 | ** |
||
11 | ** This program is distributed in the hope that it will be useful, |
||
12 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | ** GNU General Public License for more details. |
||
15 | ** |
||
16 | ** You should have received a copy of the GNU General Public License |
||
17 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | ********************************************************************************************/ |
||
19 | |||
716 | werner | 20 | #include "windscript.h" |
21 | #include "windmodule.h" |
||
717 | werner | 22 | #include "helper.h" |
1080 | werner | 23 | #include "spatialanalysis.h" |
1085 | werner | 24 | #include "scriptgrid.h" |
716 | werner | 25 | |
26 | WindScript::WindScript(QObject *parent) : |
||
27 | QObject(parent) |
||
28 | { |
||
29 | mModule = 0; |
||
30 | } |
||
31 | |||
32 | int WindScript::windEvent(double windspeed, double winddirection, int max_iteration, bool simulate, int iteration) |
||
33 | { |
||
34 | mModule->setWindProperties(winddirection*M_PI/180., windspeed); |
||
35 | mModule->setSimulationMode(simulate); |
||
36 | mModule->setMaximumIterations(max_iteration); |
||
740 | werner | 37 | try { |
719 | werner | 38 | mModule->run(iteration, true); |
740 | werner | 39 | } catch (const IException &e) { |
40 | qDebug() << "ERROR in windEvent():" << e.message(); |
||
41 | } |
||
42 | |||
716 | werner | 43 | qDebug() << "run wind module from script..."; |
44 | return 0; |
||
45 | } |
||
717 | werner | 46 | |
47 | bool WindScript::gridToFile(QString grid_type, QString file_name) |
||
48 | { |
||
49 | if (!GlobalSettings::instance()->model()) |
||
50 | return false; |
||
51 | QString result; |
||
52 | |||
53 | result = gridToESRIRaster(mModule->mWindLayers, grid_type); // use a specific value function (see above) |
||
54 | |||
55 | if (!result.isEmpty()) { |
||
56 | file_name = GlobalSettings::instance()->path(file_name); |
||
57 | Helper::saveToTextFile(file_name, result); |
||
58 | qDebug() << "saved grid to " << file_name; |
||
59 | return true; |
||
60 | } |
||
61 | qDebug() << "could not save gridToFile because" << grid_type << "is not a valid grid."; |
||
62 | return false; |
||
63 | |||
64 | } |
||
748 | werner | 65 | |
1085 | werner | 66 | QJSValue WindScript::grid(QString type) |
67 | { |
||
68 | int idx = mModule->mWindLayers.indexOf(type); |
||
69 | if (idx<0) |
||
70 | qDebug() << "ERROR: WindScript:grid(): invalid grid" << type << "valid:" << mModule->mWindLayers.layerNames(); |
||
71 | // this is a copy |
||
1088 | werner | 72 | Grid<double> *damage_grid = mModule->mWindLayers.copyGrid(idx); |
1085 | werner | 73 | |
74 | QJSValue g = ScriptGrid::createGrid(damage_grid, type); |
||
75 | return g; |
||
76 | |||
77 | } |
||
78 | |||
748 | werner | 79 | void WindScript::initialize() |
80 | { |
||
81 | mModule->setup(); |
||
82 | qDebug() << "initialized the wind module."; |
||
83 | } |
||
1080 | werner | 84 | |
1157 | werner | 85 | void WindScript::initializeEdgeAge(int years) |
86 | { |
||
87 | if (mModule) { |
||
88 | bool mode = mModule->simulationMode(); |
||
89 | mModule->setSimulationMode(true); |
||
90 | mModule->initWindGrid(); |
||
91 | mModule->initializeEdgeAge(years); |
||
92 | mModule->incrementEdgeAge(); |
||
93 | mModule->setSimulationMode(mode); |
||
94 | } |
||
95 | } |
||
96 | |||
1081 | werner | 97 | int WindScript::damagedArea(int threshold, QString fileName) |
1080 | werner | 98 | { |
99 | // get damage grid: |
||
1088 | werner | 100 | Grid<double> *damage_grid = mModule->layers().copyGrid(mModule->layers().indexOf("basalAreaKilled")); |
1080 | werner | 101 | SpatialAnalysis spat; |
1085 | werner | 102 | QList<int> patches = spat.extractPatches(*damage_grid, threshold+1, fileName); |
1080 | werner | 103 | int n=0, size=0; |
104 | for (int i=0;i<patches.count();++i) |
||
105 | if (patches[i]>threshold) { |
||
106 | size+=patches[i]; |
||
107 | n++; |
||
108 | } |
||
109 | qDebug() << "WindScript:damagedArea:" << n << "patches (area=" << size << ") above threshold" << threshold; |
||
110 | delete damage_grid; |
||
111 | return size; |
||
112 | } |