How to debug Custom Script

Hi,

I tried to be creative with custom scripts but soon reached my limits.
What is the best way to debug my custom script. I simply paste my code into the custom script part of the EO-Browser and do not get any visual output nor information why it failed.

For example, I simply try to visualize more than one input layer from Sentinel-5 with

// VERSION=3
function setup() {
    return {
        input: [{ datasource: "s5pl2", bands: ["NO2", "CO", "O3"]}],
        output: { id: "default", bands: 3, sampleType: SampleType.AUTO }
    };
}

function evaluatePixel(sample) {
    var S5 = sample.s5pl2[0];

    return { default: [S5.NO2, S5.CO, S5.O3] };
}

If I take a look into the console, I do not get any useful information.

Best,
Johannes

@schmid Hi Johannes,

in order to get a feedback from our server I would suggest you use our Process API. If an exception is thrown from a script, the request execution is stopped and a response with the exception message will be returned, which can be used as a simple debugging tool.

In the Authentication section you find instructions about how to access and use the API with user-friendly software like Postman or in programming environments like Python and Javascript.

Cheers, Max

Hi,

thanks for that answer. It was very helpful. I was able to do so and got better tracebacks.
As you can see from the code above, I tried to combine the different information Sentinel-5 offers.

I was trying to use the custom script from here and oriented myself according to that use of several datasources. However, NO2 and CO for example are no datasources or bands but “product types” according to one error message I got. Is there a way I can use more than one product type of Sentinel-5 using version 3 of evalscript?

Best,
Johannes

As far as I know, using custom scripts with 3 different products in EO Browser is not possible yet.

If you really need the products as a layer stack, you could use the process API and specify three datasets in the request like shown in this example:

{
    "input": {
        "bounds": {
            "properties": {
                "crs": "http://www.opengis.net/def/crs/OGC/1.3/CRS84"
            },
            "bbox": [13, 45, 15, 47]
        },
        "data": [{"id": "s5_no2",
         "type": "S5PL2",
         "dataFilter": {"timeRange": {"from": "2020-05-26T00:00:00Z",
                                     "to": "2020-05-27T00:00:00Z"},
                        "mosaickingOrder": "mostRecent"}
        },
        {"id": "s5_co",
         "type": "S5PL2",
         "dataFilter": {"timeRange": {"from": "2020-05-26T00:00:00Z",
                                     "to": "2020-05-27T00:00:00Z"},
                        "mosaickingOrder": "mostRecent"}
        },
        {"id": "s5_o3",
         "type": "S5PL2",
         "dataFilter": {"timeRange": {"from": "2020-05-26T00:00:00Z",
                                     "to": "2020-05-27T00:00:00Z"},
                        "mosaickingOrder": "mostRecent"}
        }
       ]
    },
    "output": {
        "width": 512,
        "height": 512,
        "responses": [
            {
                "format": {
                    "type": "image/tiff"
                }
        }
        ]
    }
}

And then retrieve the different products (NO2, CO and O3) as datasources from the three datasets specified in your request (note the dataset IDs):

//VERSION=3
function setup (){
  return {
    input: [
      {datasource: "s5_no2", bands:["NO2"]},
      {datasource: "s5_co", bands:["CO"]},
      {datasource: "s5_o3", bands:["O3"]}],
    output: [
      {id: "default", bands: 3, sampleType: SampleType.UINT16}
    ]
  }
}
var f = 10000
function evaluatePixel(samples) {
  var S5_1 = samples.s5_no2[0]
  var S5_2 = samples.s5_co[0]
  var S5_3 = samples.s5_o3[0]
  //To save processing units, we return the data as integers multiplied by 10000. In order to retrieve the actual values, divide the resulting bands in the response tiff by 10000.
  return{default: [S5_1.SO2*f, S5_2.CO*f, S5_3.O3*f]}
}

I specified the response as a TIFF file with 16 bit depth, using "responses": [ { "format": { "type": "image/tiff" } } ] in the request and output: [ {id: "default", bands: 3, sampleType: SampleType.UINT16} ] in the evalscript (note the comment in the evalscript above the return statement). Of course you will need to adjust the request to your needs. I hope this helps.

Cheers, Max

2 Likes

Hi,

Max is mostly right. Data fusion is unfortunately not yet fully supported in EO Browser. EO Browser currently supports data fusion with Sentinel-1, Sentinel-2 L1C and Sentinel-2 L2A products (and their bands). To be able to use this, (at least) one of the three possible data sources (datasets) needs to be selected in the Search tab and Use additional datasets (advanced) needs to be checked and all the applicable datasets selected in the Custom rendering screen.

Additional “tool” for debugging is also manually throwing exceptions and as Max said, the response will be returned, in this case with a chosen message. (I wrote a simple example below).

const someVar = 1;
throw new Error("some text", someVar);

The only limit is that you can throw an exception only in one place, because the execution stops when it is thrown (if more exceptions would be thrown on multiple places, only the first to be executed would actually be executed).

Hope that helps in any way. Cheers

2 Likes