Subversion Repositories public iLand

Rev

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