Open-EO / openeo-js-client

JavaScript and TypeScript client for the openEO API.
https://open-eo.github.io/openeo-js-client/latest/
Apache License 2.0
15 stars 6 forks source link

parameter naming issues #46

Closed jonathom closed 3 years ago

jonathom commented 3 years ago

I noticed some behaviour that I can't quite explain when executing a .js script. See this gist with the code I'm referring to.

  1. The script in total is failing because in line 40, I named the parameter ,process = scale_ as such. If I only put , scale_, not mentioning the name, the script runs without error.
  2. Although I'm naming parameters these names don't really seem to matter. E.g. when I switch reducer = ... and dimension = 't' in line 32, the script fails to execute.

Can't make any sense of it. EDIT: using authenticateBasic, deleted login for upload

m-mohr commented 3 years ago

JavaScript doesn't have named parameters, you just provide parameters by order.

So for example your code var cube_s2_b348_red_lin = builder.apply(data = cube_s2_b348_red, process = scale_); must be var cube_s2_b348_red_lin = builder.apply(cube_s2_b348_red, scale_); instead.

It seems you are doing too much R and Python. :-D

jonathom commented 3 years ago

alright :), I will change this in the cookbook as well.

jonathom commented 3 years ago

@m-mohr One question though: If I can't name parameters, how can I skip a parameter that I don't need to pass. E.g. in array_element, where I can choose to pass either an index or a label? Screenshot from 2021-05-06 14-40-28 (I am aware that there are easier ways to accomplish the overall task here)

m-mohr commented 3 years ago

You can pass undefined, i.e. b8 = this.array_element(data, undefined, "B08"). Not very nice though and therefore array_element has a nice simplification for that, you can basically do the following instead:

b4 = data[0]
b8 = data["B08"]

It detects the data type (integer = index, string = label) and converts it correctly into array_element calls.

See also: https://open-eo.github.io/openeo-js-client/1.3.1/Parameter.html

m-mohr commented 3 years ago

So your code could be shortened to:

var _ndvi = (data, _, child) => child.normalized_difference(data[0], data["B08"]);

or

var _ndvi = function(data) {
    return this.normalized_difference(data[0], data["B08"]);
}
jonathom commented 3 years ago

great, thanks! VITO backend throws an error (when doing the above) cause it expects a number for some reason, but that's their thing. Anyway, how's this for a bandmath definition? Or is it generally done differently? Screenshot from 2021-05-06 14-56-22

jonathom commented 3 years ago

Maybe this is the place for using Formula? Is there a way that should be preferred over the other?

m-mohr commented 3 years ago

Yes, that's a good place for using "Formula". For everything where you compute on a single number (i.e. not an array of numbers), Formula is probably the best choice.