How are the values calculated within Sentinel Hub and how are they returned as an output?

Home > Frequently Asked Questions > How are the values calculated within Sentinel Hub and how are they returned as an output?

This is a bit complicated as it depends on several design decisions we’ve had to make in order to keep our service as generic as possible.

There are several basic rules used within Sentinel Hub:

  • whenever we work with sensor data (e.g. B02, B08, etc.), a reflectance value is used
  • when outputting the value from the system, it is clamped to the range 0-1 and then stretched to the range of values of the format of the output:
    • if format is 32-bit float, it is 0-1
    • if format is 16-bit, it is 0-65535; 0 -> 0 and 1 -> 65535
    • if format is 8-bit, it is 0-255; 0 -> 0 and 1 -> 255
  • IEEE 754 single-precision is used within calculations (e.g. 32-bit float)
  • internally during script evaluation, there is no clamping being done

As an example, if one wants to get Sentinel-2 reflectance value, one can use this simple custom script:
return [B02];

And depending on the output format will return:

  • reflectance values where reflectance = 1 is at a pixel value of 65535 (format=image/tiff;depth=16)
  • reflectance values where reflectance = 1 is at a pixel value of 255 (format=image/tiff;depth=8, format=image/jpg  or format=image/png)
  • reflectance values in the range 0-1 (format=image/tiff;depth=32f)

If one is calculating NDVI, e.g.:
var ndvi = (B08-B04)/(B08+B04);
return [ndvi];

This will return:

  • range 0-1 adjusted to an integer 0-65535 for 16 bit output (format=image/tiff;depth=16)
  • range 0-1 adjusted to an integer 0-255 if 8 bit output (format=image/tiff;depth=8, format=image/jpg  or format=image/png)
  • range 0-1 if 32 bit float output (format=image/tiff;depth=32f)

As NDVI values can sometimes be negative (e.g. -0.2) or if using a different index with values more than 1 (e.g. 1.5), one has to adapt the output if one wants to get this information. E.g. if one expects an index value from -1 to 2, the final script would have to be adapted to:
index = (index+1)/3;
The output between 0 and 1 can be there mapped like that:

  • output 0 -> value -1
  • output 0.333 -> value 0
  • output 0.667 -> value 1
  • output 1 -> value =2

Note that if one’s desired output is color visualisation, this can be as simple as this:

return colorBlend(ndvi, [-0.2, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 ], [[0, 0, 0], // < -.2 = #000000 (black) [165/255,0,38/255], // -> 0 = #a50026 [215/255,48/255,39/255], // -> .1 = #d73027 [244/255,109/255,67/255], // -> .2 = #f46d43 [253/255,174/255,97/255], // -> .3 = #fdae61 [254/255,224/255,139/255], // -> .4 = #fee08b [255/255,255/255,191/255], // -> .5 = #ffffbf [217/255,239/255,139/255], // -> .6 = #d9ef8b [166/255,217/255,106/255], // -> .7 = #a6d96a [102/255,189/255,99/255], // -> .8 = #66bd63 [26/255,152/255,80/255], // -> .9 = #1a9850 [0,104/255,55/255] // -> 1.0 = #006837 ]);