Subversion Repositories public iLand

Rev

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
 
961 werner 20
#include "barkbeetlescript.h"
21
 
22
#include "barkbeetlemodule.h"
1024 werner 23
#include "outputmanager.h"
1036 werner 24
#include "helper.h"
1081 werner 25
#include "spatialanalysis.h"
26
#include "scriptgrid.h"
1088 werner 27
#include "mapgrid.h"
28
#include "scriptglobal.h"
961 werner 29
 
1095 werner 30
/** @class BarkBeetleScript
31
    @ingroup beetlemodule
32
    BarkBeetleScript is the scripting shell for the bark beetle module.
33
  */
961 werner 34
 
35
BarkBeetleScript::BarkBeetleScript(QObject *)
36
{
37
    mBeetle = 0;
38
}
39
 
40
void BarkBeetleScript::test(QString value)
41
{
42
    qDebug() << value;
43
}
44
 
45
void BarkBeetleScript::init(QJSValue fun)
46
{
47
    QJSValueList args = QJSValueList() << 0 << 0;
48
 
49
    if (!fun.isCallable()) {
50
        qDebug() << "no valid function in init!!";
51
        return;
52
    }
53
    for (int y=0;y < mBeetle->mGrid.sizeY(); ++y)
54
        for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) {
55
            args[0] = x; args[1] = y;
56
            double result = fun.call(args).toNumber();
57
            mBeetle->mGrid.valueAtIndex(x,y).n = result;
58
        }
59
}
60
 
61
void BarkBeetleScript::run(QJSValue fun)
62
{
63
    QJSValueList args = QJSValueList() << 0 << 0;
64
 
65
    if (!fun.isCallable()) {
66
        qDebug() << "no valid function in run!!";
67
        return;
68
    }
69
    for (int y=0;y < mBeetle->mGrid.sizeY(); ++y)
70
        for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) {
71
            args[0] = x; args[1] = y;
72
            fun.call(args);
73
        }
74
}
75
 
76
double BarkBeetleScript::pixelValue(int ix, int iy)
77
{
78
    if (mBeetle->mGrid.isIndexValid(ix,iy)) {
79
        return mBeetle->mGrid.valueAtIndex(ix, iy).n;
80
    } else {
81
        return -9999;
82
    }
83
}
84
 
85
void BarkBeetleScript::setPixelValue(int ix, int iy, double val)
86
{
87
    if (mBeetle->mGrid.isIndexValid(ix,iy)) {
88
        mBeetle->mGrid.valueAtIndex(ix, iy).n = val;
89
    }
90
}
1008 werner 91
 
92
double BarkBeetleScript::generations(int ix, int iy)
93
{
94
    if (mBeetle->mGrid.isIndexValid(ix,iy)) {
95
        return mBeetle->mRUGrid.valueAt( mBeetle->mGrid.cellCenterPoint(QPoint(ix,iy)) ).generations;
96
    } else {
97
        return -9999;
98
    }
99
 
100
}
101
 
1010 werner 102
void BarkBeetleScript::reloadSettings()
103
{
104
    mBeetle->loadParameters();
105
}
106
 
1055 werner 107
void BarkBeetleScript::newYear()
108
{
109
    int y = mBeetle->manualYearBegin();
110
    qDebug() << "Barkbeetle-module: year=" << y;
111
}
112
 
1013 werner 113
void BarkBeetleScript::runBB(int iteration)
1008 werner 114
{
115
    qDebug() << "running bark beetle module....";
1013 werner 116
    mBeetle->run(iteration);
1024 werner 117
    GlobalSettings::instance()->outputManager()->save(); // just make sure database outputs are properly written
1008 werner 118
}
1013 werner 119
 
120
void BarkBeetleScript::clear()
121
{
122
    qDebug() << "clear bark beetle module....";
123
    mBeetle->clearGrids();
1039 werner 124
    mBeetle->loadParameters();
125
    mBeetle->loadAllVegetation();
1013 werner 126
}
1024 werner 127
 
1036 werner 128
bool BarkBeetleScript::gridToFile(QString type, QString filename)
129
{
130
    if (!GlobalSettings::instance()->model())
131
        return false;
132
    QString result;
133
    result = gridToESRIRaster(mBeetle->mLayers, type); // use a specific value function (see above)
134
 
135
    if (result.isEmpty()) {
136
        result = gridToESRIRaster(mBeetle->mRULayers, type); // try RU-level indicators
137
    }
138
 
139
    if (!result.isEmpty()) {
140
        filename = GlobalSettings::instance()->path(filename);
141
        Helper::saveToTextFile(filename, result);
142
        qDebug() << "saved grid to " << filename;
143
        return true;
144
    }
145
    qDebug() << "could not save gridToFile because" << type << "is not a valid grid.";
146
    return false;
147
 
148
}
149
 
1081 werner 150
QJSValue BarkBeetleScript::grid(QString type)
151
{
152
    int idx = mBeetle->mLayers.indexOf(type);
153
    if (idx<0)
154
        qDebug() << "ERROR: BarkBeetleScript:grid(): invalid grid" << type;
155
    // this is a copy
1088 werner 156
    Grid<double> *damage_grid = mBeetle->mLayers.copyGrid(idx);
1081 werner 157
 
158
    QJSValue g = ScriptGrid::createGrid(damage_grid, type);
159
    return g;
160
}
161
 
162
int BarkBeetleScript::damagedArea(int threshold, QString fileName)
163
{
164
    // get damage grid:
1088 werner 165
    Grid<double> *damage_grid = mBeetle->mLayers.copyGrid(mBeetle->mLayers.indexOf("dead"));
1081 werner 166
    SpatialAnalysis spat;
1085 werner 167
    QList<int> patches = spat.extractPatches(*damage_grid, threshold+1, fileName);
1081 werner 168
    int n=0, size=0;
169
    for (int i=0;i<patches.count();++i)
170
        if (patches[i]>threshold) {
171
            size+=patches[i];
172
            n++;
173
        }
174
    qDebug() << "BarkBeetleScript:damagedArea:" << n << "patches (area=" << size << ") above threshold" << threshold;
175
    delete damage_grid;
176
    return size;
177
 
178
}
179
 
1088 werner 180
int BarkBeetleScript::clearInfestedPixels(QJSValue standmap, int stand_id, double fraction)
181
{
182
    MapGridWrapper *gr = qobject_cast<MapGridWrapper*> ( standmap.toQObject() );
183
    if (!gr || !gr->map()) {
184
        qDebug() << "BarkBeetleScript::clearInfestedPixels: no valid stand-map!";
185
        return 0;
186
    }
187
    QRectF box = gr->map()->boundingBox(stand_id);
188
    //GridRunner<BarkBeetleCell> runner(mBeetle->mGrid, box);
189
    GridRunner<int> runner(gr->map()->grid(), box);
190
    int n_cleared = 0;
191
    while (runner.next()) {
192
        if (*runner.current() == stand_id) {
193
            BarkBeetleCell &bbc = mBeetle->mGrid.valueAt( runner.currentCoord() );
194
            if (bbc.infested) {
195
                if (fraction==1. || drandom()<fraction) {
196
                    bbc.infested = false;
197
                    n_cleared++;
198
                }
199
            }
200
        }
201
    }
202
    return n_cleared;
203
}
204
 
1157 werner 205
bool BarkBeetleScript::setInfested(int x, int y)
206
{
207
    if (!mBeetle->mGrid.isIndexValid(QPoint(x,y))) {
208
        qDebug() << "invalid index in BarkBeetleScript::setInfested(): x:" << x << "y:" << y;
209
        return false;
210
    }
211
    BarkBeetleCell &c = mBeetle->mGrid.valueAtIndex(QPoint(x,y));
212
    if (!c.isHost() || c.killed)
213
        return false;
214
    c.setInfested(true);
215
    c.outbreakYear = mBeetle->internalYear();
216
    return true;
217
}
218
 
1024 werner 219
bool BarkBeetleScript::simulate()
220
{
221
    return mBeetle->simulate();
222
}
223
 
224
void BarkBeetleScript::setSimulate(bool do_simulate)
225
{
226
    mBeetle->setSimulate(do_simulate);
227
}
1037 werner 228
 
229
bool BarkBeetleScript::enabled()
230
{
231
    return mBeetle->enabled();
232
}
233
 
234
void BarkBeetleScript::setEnabled(bool do_set_enable)
235
{
236
    mBeetle->setEnabled(do_set_enable);
237
}