Subversion Repositories public iLand

Rev

Rev 1039 | Rev 1081 | 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"
961 werner 26
 
27
 
28
BarkBeetleScript::BarkBeetleScript(QObject *)
29
{
30
    mBeetle = 0;
31
}
32
 
33
void BarkBeetleScript::test(QString value)
34
{
35
    qDebug() << value;
36
}
37
 
38
void BarkBeetleScript::init(QJSValue fun)
39
{
40
    QJSValueList args = QJSValueList() << 0 << 0;
41
 
42
    if (!fun.isCallable()) {
43
        qDebug() << "no valid function in init!!";
44
        return;
45
    }
46
    for (int y=0;y < mBeetle->mGrid.sizeY(); ++y)
47
        for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) {
48
            args[0] = x; args[1] = y;
49
            double result = fun.call(args).toNumber();
50
            mBeetle->mGrid.valueAtIndex(x,y).n = result;
51
        }
52
}
53
 
54
void BarkBeetleScript::run(QJSValue fun)
55
{
56
    QJSValueList args = QJSValueList() << 0 << 0;
57
 
58
    if (!fun.isCallable()) {
59
        qDebug() << "no valid function in run!!";
60
        return;
61
    }
62
    for (int y=0;y < mBeetle->mGrid.sizeY(); ++y)
63
        for (int x=0;x < mBeetle->mGrid.sizeX(); ++x) {
64
            args[0] = x; args[1] = y;
65
            fun.call(args);
66
        }
67
}
68
 
69
double BarkBeetleScript::pixelValue(int ix, int iy)
70
{
71
    if (mBeetle->mGrid.isIndexValid(ix,iy)) {
72
        return mBeetle->mGrid.valueAtIndex(ix, iy).n;
73
    } else {
74
        return -9999;
75
    }
76
}
77
 
78
void BarkBeetleScript::setPixelValue(int ix, int iy, double val)
79
{
80
    if (mBeetle->mGrid.isIndexValid(ix,iy)) {
81
        mBeetle->mGrid.valueAtIndex(ix, iy).n = val;
82
    }
83
}
1008 werner 84
 
85
double BarkBeetleScript::generations(int ix, int iy)
86
{
87
    if (mBeetle->mGrid.isIndexValid(ix,iy)) {
88
        return mBeetle->mRUGrid.valueAt( mBeetle->mGrid.cellCenterPoint(QPoint(ix,iy)) ).generations;
89
    } else {
90
        return -9999;
91
    }
92
 
93
}
94
 
1010 werner 95
void BarkBeetleScript::reloadSettings()
96
{
97
    mBeetle->loadParameters();
98
}
99
 
1055 werner 100
void BarkBeetleScript::newYear()
101
{
102
    int y = mBeetle->manualYearBegin();
103
    qDebug() << "Barkbeetle-module: year=" << y;
104
}
105
 
1013 werner 106
void BarkBeetleScript::runBB(int iteration)
1008 werner 107
{
108
    qDebug() << "running bark beetle module....";
1013 werner 109
    mBeetle->run(iteration);
1024 werner 110
    GlobalSettings::instance()->outputManager()->save(); // just make sure database outputs are properly written
1008 werner 111
}
1013 werner 112
 
113
void BarkBeetleScript::clear()
114
{
115
    qDebug() << "clear bark beetle module....";
116
    mBeetle->clearGrids();
1039 werner 117
    mBeetle->loadParameters();
118
    mBeetle->loadAllVegetation();
1013 werner 119
}
1024 werner 120
 
1036 werner 121
bool BarkBeetleScript::gridToFile(QString type, QString filename)
122
{
123
    if (!GlobalSettings::instance()->model())
124
        return false;
125
    QString result;
126
    result = gridToESRIRaster(mBeetle->mLayers, type); // use a specific value function (see above)
127
 
128
    if (result.isEmpty()) {
129
        result = gridToESRIRaster(mBeetle->mRULayers, type); // try RU-level indicators
130
    }
131
 
132
    if (!result.isEmpty()) {
133
        filename = GlobalSettings::instance()->path(filename);
134
        Helper::saveToTextFile(filename, result);
135
        qDebug() << "saved grid to " << filename;
136
        return true;
137
    }
138
    qDebug() << "could not save gridToFile because" << type << "is not a valid grid.";
139
    return false;
140
 
141
}
142
 
1024 werner 143
bool BarkBeetleScript::simulate()
144
{
145
    return mBeetle->simulate();
146
}
147
 
148
void BarkBeetleScript::setSimulate(bool do_simulate)
149
{
150
    mBeetle->setSimulate(do_simulate);
151
}
1037 werner 152
 
153
bool BarkBeetleScript::enabled()
154
{
155
    return mBeetle->enabled();
156
}
157
 
158
void BarkBeetleScript::setEnabled(bool do_set_enable)
159
{
160
    mBeetle->setEnabled(do_set_enable);
161
}