vignettes/b_convert_celsius.Rmd
b_convert_celsius.RmdIdentifies the unit of a temperature spatraster and convert the pixels values into celsius degrees. If the pixel values are modified, the units metadata of the spatraster is updated to celsius.
A Spatraster imported containning the sea surface temperature in the Gulf of Lion from the 1st of January 2023 to the 31th of Decembre 2025. There are only one variable in the spatraster (sea surface temperature).
### 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)The original dataset is in Kelvin :
terra::units(copernicus_data[[1]]) #first layer only## [1] "kelvin"
You will receive a warning if :
If your dataset was in Kelvin or Fahrenheit, you will receive a message saying which conversion has been done.
The output is a spatraster with the same properties as yourspatraster but yhe pixel values are in degrees Celsius. Pixels that were NA remain NA.
library(BioExtremeEvent)
### Apply function :
copernicus_data_celsius <- BioExtremeEvent::BEE.convert.celsius(copernicus_data)## Warning in BioExtremeEvent::BEE.convert.celsius(copernicus_data): 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"
### Apply function :
copernicus_data_celsius <- BioExtremeEvent::BEE.convert.celsius(copernicus_data_celsius)## [1] "Your dataset is already in celsius, there is no need to use this function."
The output is a spatraster, you can save it using:
terra::writeRaster(copernicus_data_celsius, "your_path/data/copernicus_data_celsius.tiff")