Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

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