Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
890 | werner | 1 | /** |
2 | * iLand Javascript API |
||
1188 | werner | 3 | |
890 | werner | 4 | Overview |
5 | ======== |
||
1188 | werner | 6 | |
7 | iLand - the invidiual based landscape and disturbance model - is capable of simulating the development of forests on landscapes |
||
8 | of thousands of hectares. The model is built in C++ (using the [Qt-framework](http://qt.io) and utilizes the the built-in |
||
9 | V8 Javascript engine. The model exposes a number of (C++)-objects to the Javascript context, that allow accessing various iLand functions from Javascript. |
||
10 | |||
11 | See [iLand scripting](http://iland.boku.ac.at/iLand+scripting) for an overview. |
||
12 | |||
13 | |||
890 | werner | 14 | * |
15 | * @module iLand |
||
16 | */ |
||
17 | |||
18 | |||
19 | |||
20 | /** |
||
21 | The Globals object exposes a series of auxiliary functions to the scripting context. An instance of "Globals" is always available in the global context, so no explicit creation is necessary. |
||
22 | The provided function cover inter alia: |
||
23 | |||
24 | - handling of directories and simple text files |
||
25 | - access to XML settings |
||
26 | - access and control over Outputs |
||
27 | - miscellaneous functions (e.g., for making screeshots) |
||
28 | |||
29 | @class Globals |
||
30 | */ |
||
1194 | werner | 31 | |
890 | werner | 32 | /** |
1189 | werner | 33 | The current year of the simulation. |
34 | @property year |
||
35 | @type {integer} |
||
36 | @readOnly |
||
890 | werner | 37 | */ |
1189 | werner | 38 | |
890 | werner | 39 | /** |
1189 | werner | 40 | The number of resource units in the current simulation. |
890 | werner | 41 | @property resourceUnitCount |
42 | @type integer |
||
43 | @default 1 |
||
44 | @readOnly */ |
||
45 | |||
1189 | werner | 46 | |
47 | |||
890 | werner | 48 | |
1061 | werner | 49 | /** |
50 | Completely reloads the ABE (agent based management engine) sub module. This includes loading of the (static) stand description file, |
||
51 | and the javascript source code of ABE. |
||
52 | @method reloadABE |
||
1189 | werner | 53 | */ |
54 | |||
55 | /** |
||
56 | Read a value from the [project file](http://iland.boku.ac.at/project+file). The `key` is the full path to the |
||
57 | requested node using a '.'-notation. |
||
58 | |||
59 | See also: {{#crossLink "Globals/set:method"}}{{/crossLink}} |
||
60 | |||
61 | @Example |
||
62 | // global 'onInit' function is called during startup |
||
63 | var width = Globals.setting('model.world.width'); // the horizontal extent of the project area |
||
64 | var filename = Globals.setting('user.targetdir') + "my_file.txt"; // a user-defined key in the project file |
||
65 | |||
66 | @method setting |
||
67 | @param {string} key fully qualified key within the project file |
||
68 | @return The value of the setting, or _undefined_ if the setting is not present |
||
69 | */ |
||
70 | |||
71 | |||
72 | /** |
||
73 | Set a setting in the [project file](http://iland.boku.ac.at/project+file) with the key `key` to a value of `value`. |
||
74 | The `key` is the full path to the requested node using a '.'-notation. |
||
75 | |||
76 | See also: {{#crossLink "Globals/setting:method"}}{{/crossLink}} |
||
77 | |||
78 | @method set |
||
79 | @param {string} key fully qualified key within the project file |
||
80 | @param value new value of the setting `key`. |
||
81 | @return {boolean} _true_ on success. |
||
82 | */ |
||
83 | |||
84 | |||
85 | /** |
||
86 | Get directory with of the given category `category`. See [filenames and paths](http://iland.boku.ac.at/filenames+and+paths) for available categories. |
||
87 | Using this defaultDirectory() avoids absolute file paths in scripts. |
||
88 | |||
89 | See also: {{#crossLink "Globals/currentDir:property"}}{{/crossLink}} |
||
90 | |||
91 | @method defaultDirectory |
||
92 | @param category {string} folder category |
||
93 | @return {string} The requested file path (without a trailing slash) |
||
94 | */ |
||
95 | |||
96 | |||
97 | /** |
||
98 | Use `path()` to construct a path relative to the project directory. Useful for avoiding absolute file paths in Javascript. |
||
99 | |||
100 | Example (Project-file: "c:\iland\test\test.xml"): Globals.path('temp/filexy.txt') -> "c:\iland\test\temp.filexy.txt" |
||
101 | |||
102 | See also: `defaultDirectory`, `currentDirectory` |
||
103 | |||
104 | @method path |
||
105 | @param filename {string} filename that should be extended |
||
106 | @return {string} fully qualified file path |
||
107 | */ |
||
108 | |||
109 | |||
110 | /** |
||
111 | gets or sets the current directory of iLand. Relative paths are resolved relative to the `currentDir`. |
||
112 | |||
113 | Example (Project-file: "c:\iland\test\test.xml"): Globals.path('temp/filexy.txt') -> "c:\iland\test\temp.filexy.txt" |
||
114 | |||
115 | See also: {{#crossLink "Globals/path:method"}}{{/crossLink}} |
||
116 | @property currentDir |
||
117 | @type {string} |
||
118 | @Example |
||
119 | Globals.currentDir="c:/temp"; |
||
120 | Globals.loadTextFile("i_am_in_temp.txt"); // accesses c:/temp |
||
121 | */ |
||
122 | |||
123 | /** |
||
124 | extent of the world (without buffer) in meters (x-direction). |
||
125 | |||
126 | See also: {{#crossLink "Globals/worldY:property"}}{{/crossLink}} |
||
127 | @property worldX |
||
128 | @type {integer} |
||
129 | */ |
||
130 | |||
131 | /** |
||
132 | extent of the world (without buffer) in meters (y-direction). |
||
133 | |||
134 | See also: {{#crossLink "Globals/worldX:property"}}{{/crossLink}} |
||
135 | @property worldY |
||
136 | @type {integer} |
||
137 | */ |
||
1194 | werner | 138 | /** |
139 | The milliseconds since the start of the day. |
||
1189 | werner | 140 | |
1194 | werner | 141 | @property msec |
142 | @type {integer} |
||
143 | @Example |
||
144 | // simple timer functions: |
||
145 | var _elapsed=-1; |
||
146 | function start_timer() { _elapsed = Globals.msec; } |
||
147 | function elapsed(thingi) |
||
148 | { |
||
149 | var elapsed=Globals.msec - _elapsed; |
||
150 | _elapsed = Globals.msec; |
||
151 | console.log("Time: " + thingi + ": " + elapsed + "ms"); |
||
152 | } |
||
1191 | werner | 153 | |
1194 | werner | 154 | // use timers: |
155 | start_timer(); |
||
156 | // do some lengthy operation x |
||
157 | elapsed("x"); // prints time elapsed since start_timer() |
||
158 | // some other lenghty operation y |
||
159 | elapsed("y"); // print duration of 'y' in ms |
||
160 | */ |
||
1191 | werner | 161 | |
162 | |||
1189 | werner | 163 | /** |
164 | starts the output `table_name`. The table name for each output can be found on the [output](http://iland.boku.ac.at/output) wiki page. |
||
165 | Starting [debug outputs](http://iland.boku.ac.at/debug+outputs) is also possible - the `table_name` has to have the format _debug_XXX_, with _XXX_ one of the following: |
||
166 | |||
167 | + treeNPP |
||
168 | + treePartition |
||
169 | + treeGrowth |
||
170 | + waterCycle |
||
171 | + dailyResponse |
||
172 | + establishment |
||
173 | + carbonCycle |
||
174 | + performance |
||
175 | |||
176 | See also: {{#crossLink "Globals/stopOutput:method"}}{{/crossLink}} |
||
177 | |||
178 | @method startOutput |
||
179 | @param table_name {string} Output that should be started |
||
180 | @return {boolean} true on succes, an error message is printed in case of failure. |
||
181 | @Example |
||
182 | Globals.startOutput("debug_performance"); |
||
183 | */ |
||
184 | /** |
||
185 | stops the output `table_name`. The table name for each output can be found on the [output](http://iland.boku.ac.at/output) wiki page. |
||
186 | Starting (debug outputs) is also possible - see {{#crossLink "Globals/startOutput:method"}}{{/crossLink}}. |
||
187 | |||
188 | See also: {{#crossLink "Globals/startOutput:method"}}{{/crossLink}} |
||
189 | |||
190 | @method stopOutput |
||
191 | @param table_name {string} Output that should be stopped |
||
192 | @return {boolean} true on succes, an error message is printed in case of failure. |
||
193 | */ |
||
194 | |||
195 | |||
196 | /** |
||
197 | creates a snapshot from the current state of the model. `file_name` is the path to the target database, which is created if the database file does not exist |
||
198 | (paths relative to the _home_ directory). The [wiki](http://iland.boku.ac.at/initialize+trees) provides details about snapshots. |
||
199 | |||
200 | See also: {{#crossLink "Globals/loadModelSnapshot:method"}}{{/crossLink}} |
||
201 | |||
202 | @method saveModelSnapshot |
||
203 | @param file_name {string} filename of the output database. |
||
204 | @return {boolean} true on succes. |
||
205 | */ |
||
206 | |||
1194 | werner | 207 | /** |
208 | loads a snapshot database (created with a previous call to {{#crossLink "Globals/saveModelSnapshot:method"}}{{/crossLink}}). |
||
209 | The model must be already created (i.e. resource units, ...); |
||
210 | exisiting trees are removed and replaced by the trees from the database. |
||
1189 | werner | 211 | |
1194 | werner | 212 | See also: {{#crossLink "Globals/saveModelSnapshot:method"}}{{/crossLink}} |
1189 | werner | 213 | |
1194 | werner | 214 | @method loadModelSnapshot |
215 | @param file_name {string} filename of the input database. |
||
216 | @return {boolean} true on succes. |
||
217 | */ |
||
1189 | werner | 218 | |
1190 | werner | 219 | /** |
220 | `viewOptions` allow some control over the visualization of the landscape in the iLand GUI. The `viewOptions` is an object with the following elements: |
||
1189 | werner | 221 | |
1190 | werner | 222 | + `minValue` and `maxValue`: the value range of the visualization; if not present, the value range of the ruler is automatically scaled |
223 | + `type`: defines the type of visualization, and is one of the following: |
||
224 | + `lif`: the 'Light Influence Field' (2m) |
||
225 | + `dom`: the dominant height (10m) |
||
226 | + `regeneratation`: saplings/regeneration (2m) |
||
227 | + `seed`: seed availability (20m) |
||
228 | + `trees`: individual trees |
||
229 | + `ru`: resource units (1ha) |
||
230 | + `clip`: boolean ; if _true_, grids are clipped to the height grid (i.e. white for out-of-project-areas) (not supported for all visualizations) |
||
231 | + `transparent`: boolean; if true, trees are drawn semi-transparent |
||
232 | + `species` |
||
233 | + boolean value in single tree mode: if true, draw trees using species colors (true) |
||
234 | + a species short name (e.g. '_piab_'): select this species, e.g. for seed availability per species, or species shares |
||
235 | + `grid`: draw one of the dynamic grids layers from active sub modules (check the possible names either in iLand or in the wiki) |
||
236 | + `expression`: provides a expression that can be evaluated for trees or for resource unit (depending on the `type`) |
||
237 | + `filter`: expression that is used to filter trees (useful for showing only a subset of trees) |
||
238 | |||
239 | This option is not available in the iLand console version. |
||
240 | |||
241 | @property viewOptions |
||
242 | @type {object} |
||
243 | @Example |
||
244 | Globals.viewOptions = { type: 'trees', species: true }; |
||
245 | var vo = Globals.viewOptions; |
||
246 | vo.filter = 'dbh<10'; |
||
247 | Globals.viewOptions = vo; |
||
248 | Globals.repaint(); |
||
249 | */ |
||
250 | |||
251 | |||
1194 | werner | 252 | /** |
253 | Load the content of a text file into a string. |
||
1190 | werner | 254 | |
1194 | werner | 255 | @method loadTextFile |
256 | @param file_name {string} filename to load |
||
257 | @return {string} the content of the file or an empty text if file does not exist/is empty. |
||
1190 | werner | 258 | |
259 | */ |
||
1194 | werner | 260 | /** |
261 | Save the content of a string `content` to a file. |
||
1190 | werner | 262 | |
1194 | werner | 263 | @method saveTextFile |
264 | @param file_name {string} filename to save to |
||
265 | @param content {string} content that should be written |
||
266 | @return {boolean} true on success. |
||
1190 | werner | 267 | */ |
1194 | werner | 268 | /** |
269 | Check if the file `file_name` already exists. |
||
1190 | werner | 270 | |
1194 | werner | 271 | @method fileExists |
272 | @param file_name {string} file to check |
||
273 | @return {boolean} true if the file already exists |
||
1190 | werner | 274 | */ |
275 | /** |
||
1194 | werner | 276 | Execute a system command (e.g., for copying files). Commands are operating system dependent; For windows, |
277 | `cmd.exe /C` executes a command without a separate command window (see example below). Output of the executed |
||
278 | command is redirected to the iLand log (stdout, stderr). |
||
1190 | werner | 279 | |
1194 | werner | 280 | @method systemCmd |
281 | @param command {string} command to execute |
||
282 | @Example |
||
283 | // helper function for windows: fix slashes and add 'cmd.exe /C' |
||
284 | function winnify(s) { |
||
285 | // replace forward with backward slashes |
||
286 | s = s.replace(/\//g, '\\'); |
||
287 | s = "cmd.exe /C " + s; |
||
288 | return s; |
||
289 | } |
||
1190 | werner | 290 | |
1194 | werner | 291 | // onYearEnd: is called automtically from iLand at the end of a year |
292 | function onYearEnd() |
||
293 | { |
||
294 | v = Globals.setting('user.v'); // 'version' is a user defined variable in the project file |
||
295 | // create a folder for the simulation using 'v' at the end of the first simulation year |
||
296 | if (Globals.year==1) |
||
297 | Globals.systemCmd(winnify('mkdir ' + Globals.path('output/v' + v))); |
||
298 | } |
||
1190 | werner | 299 | */ |
300 | |||
301 | |||
302 | |||
303 | |||
304 | /** |
||
1194 | werner | 305 | Add single trees on a specific resource unit with the 0-based index `resourceIndex`. |
306 | The tree list is in the string `content` and follows the |
||
307 | single-tree syntax described in [the wiki](http://iland.boku.ac.at/initialize+trees). |
||
1190 | werner | 308 | |
309 | |||
1194 | werner | 310 | @method addSingleTrees |
311 | @param resourceIndex {integer} 0-based resource unit index |
||
312 | @param content {string} line (or lines) of an init-file to initialize |
||
313 | @return {integer} the number of added trees. |
||
1190 | werner | 314 | */ |
315 | /** |
||
1194 | werner | 316 | Add trees distribution on a specific resource unit with the 0-based index `resourceIndex`. |
317 | The tree list is in the string `content` and follows the distribution-tree syntax described in [the wiki](http://iland.boku.ac.at/initialize+trees). |
||
1190 | werner | 318 | |
1194 | werner | 319 | @method addTrees |
320 | @param resourceIndex {integer} 0-based resource unit index |
||
321 | @param content {string} line (or lines) of an init-file to initialize |
||
322 | @return {integer} the number of added trees. |
||
1190 | werner | 323 | */ |
324 | /** |
||
1194 | werner | 325 | Add trees distribution on a specific stand described by the `standID`. |
326 | The stand is defined in the global stand grid. The tree list is in the string `content` and follows the distribution-tree |
||
327 | syntax described in [the wiki](http://iland.boku.ac.at/initialize+trees). |
||
1190 | werner | 328 | |
1194 | werner | 329 | @method addTreesOnMap |
330 | @param standID {integer} ID of the stand in the stand grid |
||
331 | @param content {string} line (or lines) of an init-file to initialize |
||
332 | @return {integer} the number of added trees. |
||
1190 | werner | 333 | */ |
334 | |||
335 | |||
336 | /** |
||
337 | make a screenshot from the central viewing widget (as if pressing Ctrl+P in the iLand viewer) and stores the image to the provided file `file_name`. |
||
338 | The image type depends on the extension provided with `file_name`. Default path is the home directory. |
||
339 | |||
340 | @method screenshot |
||
341 | @param {string} file_name file name that |
||
342 | @return {boolean} true on success. |
||
343 | @Example |
||
344 | // make a screenshot every 5 years |
||
345 | function screenshot() { |
||
346 | if (Globals.year % 5 == 0) |
||
347 | Globals.screenshot( Globals.defaultDirectory('temp') + 'image_' + Globals.year + '.png' ); |
||
348 | } |
||
349 | */ |
||
350 | |||
351 | /** |
||
352 | force a repaint of the GUI main visualization area. |
||
353 | |||
354 | @method repaint |
||
355 | */ |
||
356 | |||
357 | /** |
||
358 | Creates a [ESRI style](http://en.wikipedia.org/wiki/Esri_grid) grid file from an iLand grid. |
||
359 | Specify the target file name with `file_name` and the type of the source grid with the string `grid_type`. |
||
360 | Allowed grids are: 'height' and 'lif', being the 10m dominant heights and the 2m LIF pixels. |
||
361 | |||
362 | See also: {{#crossLink "Globals/grid:method"}}{{/crossLink}} |
||
363 | |||
364 | @method gridToFile |
||
365 | @param {string} grid_type select the type of grid to export |
||
366 | @param {string} file_name target file path (relative to the home directory) |
||
367 | @return {boolean} true on success. |
||
368 | @Example |
||
369 | Globals.gridToFile('height', 'temp/heightgrid.txt'); // store in project_folder/temp |
||
370 | */ |
||
371 | |||
372 | /** |
||
373 | Creates a [ESRI style](http://en.wikipedia.org/wiki/Esri_grid) grid file from an iLand grid. |
||
374 | Specify the target file name with `file_name` and the type of the source grid with the string `grid_type`. |
||
375 | Allowed grids are: 'height' and 'lif', being the 10m dominant heights and the 2m LIF pixels. |
||
376 | create a seed map for 'species' the next time seed dispersal is calculated. `species` is the species-id (e.g., 'piab', 'fasy'), |
||
377 | and `file_name` the destination location of the grid file [ESRI ASCII raster](http://en.wikipedia.org/wiki/Esri_grid). |
||
378 | The saved seed map contains the seed distribution on 20m resolution. |
||
379 | Seed maps are only saved once, i.e. if a time series of seed maps is needed, `seedMapToFile()` need to be called periodically. |
||
380 | |||
381 | @method seedMapToFile |
||
382 | @param {string} species species code to export (e.g., 'piab') |
||
383 | @param {string} file_name target file path (relative to the home directory) |
||
384 | @return {boolean} true on success. |
||
385 | @Example |
||
386 | // save maps for scots pine and beech |
||
387 | Globals.seedMapToFile('pisy', 'temp/map_pisy.asc'); |
||
388 | Globals.seedMapToFile('fasy', 'temp/map_fasy.asc'); |
||
389 | // now run the model, at least for one year |
||
390 | // files are created during model execution |
||
391 | */ |
||
392 | |||
393 | /** |
||
394 | extract a grid of type `type` from iLand. The extracted grid is a floating point grid (double precision) and |
||
395 | a copy of the current state in iLand (memory is freed automatically during Javascript garbage collection). |
||
396 | |||
397 | The grid is has a 10m cell size and covers the full extent of the model. |
||
398 | |||
399 | The available grid types are: |
||
400 | + `height`: dominant tree height (m) |
||
401 | + `count`: number of living trees (>4m height) on each pixel |
||
402 | + `valid`: pixels inside the project area get a value of 1, pixels non within the project area 0 (see [wiki page](http://iland.boku.ac.at/landscape+setup)) |
||
403 | + `forestoutside`: 1 if a pixel is out of project area and is considered to be forested (see [wiki page](http://iland.boku.ac.at/landscape+setup)) |
||
404 | |||
405 | |||
406 | See also: {{#crossLink "Globals/grid:method"}}{{/crossLink}} |
||
407 | |||
408 | @method grid |
||
409 | @param {string} type select the type of grid to return |
||
410 | @param {string} file_name target file path (relative to the home directory) |
||
411 | @return {Grid} a Javascript object encapsulating the {{#crossLink "Grid"}}{{/crossLink}} |
||
412 | |||
413 | */ |
||
1191 | werner | 414 | |
415 | |||
1194 | werner | 416 | /** |
417 | Return a grid with the basal area of the given `species` (resource unit resolution, i.e. 100m). |
||
1191 | werner | 418 | |
1194 | werner | 419 | See also: {{#crossLink "Globals/grid:method"}}{{/crossLink}} |
1191 | werner | 420 | |
1194 | werner | 421 | @method speciesShareGrid |
422 | @param {string} species species code (e.g., 'piab') of the species |
||
423 | @return {Grid} a Javascript {{#crossLink "Grid"}}{{/crossLink}} |
||
424 | */ |
||
1191 | werner | 425 | |
1194 | werner | 426 | /** |
427 | Return a grid (resolution of resource units) with the result of an `expression` |
||
428 | ([Expression](http://iland.boku.ac.at/Expression)) evaluated in the context of the resource unit |
||
429 | (see the wiki for a list of [available variables](http://iland.boku.ac.at/resource+unit+variables)). |
||
1191 | werner | 430 | |
431 | |||
1194 | werner | 432 | @method resourceUnitGrid |
433 | @param {string} expression Expression to evaluate for each resource unit |
||
434 | @return {Grid} a Javascript {{#crossLink "Grid"}}{{/crossLink}} |
||
435 | */ |
||
1191 | werner | 436 | |
1194 | werner | 437 | /** |
438 | Pause model execution for `milliseconds` ms. This can be useful to slow down animations. |
||
1191 | werner | 439 | |
440 | |||
1194 | werner | 441 | See also: {{#crossLink "Globals/repaint:method"}}{{/crossLink}} |
1191 | werner | 442 | |
1194 | werner | 443 | @method wait |
444 | @param {integer} milliseconds time to wait in milliseconds |
||
445 | */ |
||
1191 | werner | 446 | |
1194 | werner | 447 | /** |
448 | This is a helper function that allows to add shortcut links to the 'Scripting' panel in the iLand Viewer user interface. |
||
449 | `shortcuts` is a object with name/value pairs, where the _value_ is the string displayed in iLand, and _name_ the |
||
450 | Javascript function call (as a string). |
||
1191 | werner | 451 | |
452 | |||
1194 | werner | 453 | @method setUIshortcuts |
454 | @param {object} shortcuts Javascript object that defines named Javascript calls |
||
455 | @Example |
||
456 | Globals.setUIshortcuts({ 'kyrill()': 'run the kyrill storm', |
||
457 | 'emma_paula()': 'run the emma/paula storms' }) ; |
||
1191 | werner | 458 | |
1194 | werner | 459 | */ |
460 | Globals = { |
||
890 | werner | 461 | } |