Function that detects the unit of measurement of a pixel value in a SpatRaster (Celsius, Kelvin or Fahrenheit) and converts it to the international unit 'Celsius' if necessary.

BEE.convert.celsius(yourspatraster)

Arguments

yourspatraster

: It is the SpatRaster containing temperature data, for which you want to check the units and convert the values to Celsius.

Value

yourspatraster with values corrected to celsius and 'unit' metadata updated to 'Celsisus'.

Examples


### Load the example dataset in R environement :
file_name <- system.file(file.path("extdata", "copernicus_example_data.nc"),
                                  package = "BioExtremeEvent")
copernicus_data <- terra::rast(file_name)

### Values before applying functions :
head(na.omit(terra::values(x = copernicus_data[[1]],
                   dataframe=TRUE))) # first pixels are on land so their value is NA
#>    analysed_sst_1
#> 17         286.99
#> 18         286.91
#> 19         286.91
#> 20         286.95
#> 21         287.08
#> 22         287.21

### Apply function :
copernicus_data_celsius <- BioExtremeEvent::BEE.convert.celsius(copernicus_data)
#> Warning: Your data were in Kelvin and have been converted to Celsius using:
#>             former value - 273.15 = new value.

### Values after applying functions :
head(na.omit(terra::values(x = copernicus_data_celsius[[1]],
                  dataframe=TRUE))) # first pixels are on land so their value is NA
#>    analysed_sst_1
#> 17       13.83999
#> 18       13.76000
#> 19       13.76000
#> 20       13.79998
#> 21       13.92999
#> 22       14.05999

### Checking the unit of the spatraster :
# Before using BEE.convert.celsius :
terra::units(copernicus_data[[1]])
#> [1] "kelvin"
# After using BEE.convert.celsius :
terra::units(copernicus_data_celsius[[1]])
#> [1] "Celsius"