0

I am trying to get raster values from several layers from CHELSA climatic data. Here you can download one of them [https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V2/GLOBAL/climatologies/1981-2010/bio/CHELSA_bio12_1981-2010_V.2.1.tif] - the issue is persistent on all the layers I am working with (bio12, bio18 and bio17, so I guess it is layer-agnostic).

Essentially, while QGis is giving me values which always have one decimal such as 1365.2, 1165.3, the values read into the raster object in R are ignoring this decimal and interpreting them as 13652, 11653, and so on.

I am reading the raster layer like this: r <- raster("data/CHELSA_bio12_1981-2010_V.2.1.tif"). Both extracted values (with raster::extract) and values given in summary(r) are evidently wrong and are ignoring such decimal place. Summary for bio12 file (yearly precipitation, in kg*m^-2), for instance, is giving

r$> summary(r)
        CHELSA_bio12_1981.2010_V.2.1
Min.                               6
1st Qu.                         3534
Median                          8407
3rd Qu.                        14269
Max.                           65535
NA's                               0

Which is evidently lacking the final decimal, as such maximum precipitation (for instance) should be 6553.5.

For the moment I think I can confidently bypass the issue by dividing all such values by 10 (but should double-check well), but I can't help wondering what's the reason behind this. Any suggestions?

1 Answer 1

2

Computers store integers more efficiently than floating point (decimal) numbers. To store a floating point as an integer without losing too much precision, you can add a fixed value (offset) and divide by another value (scale) and round the resulting number. In other words:

stored value = (real value - offset) / scale

To retrieve the real value from the stored value, you just need to reverse the formula:

real value = stored value * scale + offset

This technique is used frequently with raster data to reduce storage and bandwidth. The technical specifications for the CHELSA climate data note on page 12 that the bio12 dataset is stored with a scale of 0.1 and an offset of 0. Using the formula above, you can calculate the correct value by swapping in those scale and offset parameters:

real value = stored value * 0.1 + 0

As you can see, this is equivalent to dividing the stored value by 10.

Some software will automatically apply scale and offset values when they are stored in the file metadata, which is why it appears correctly in QGIS. The raster package should display the correct values, but if you check the raster::gain function, it's incorrectly returning 1.0 as the scale, causing it to display the raw stored values. This seems to be a bug.

As you found, you can correct this by manually dividing by 10, or by using the newer terra package which identifies and applies the correct scale and offsets automatically for this file.

library(terra)

r <- rast("data/CHELSA_bio12_1981-2010_V.2.1.tif")
summary(r)

 CHELSA_bio12_1981.2010_V.2.1
 Min.   :   0.6              
 1st Qu.: 352.9              
 Median : 841.3              
 Mean   :1025.1              
 3rd Qu.:1426.7              
 Max.   :6553.5

scoff(r)
     scale offset
[1,]   0.1      0
1
  • On point! Thanks for the clarification.
    – jcasado94
    Dec 30, 2023 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.