Obtener valores normales climáticos mensuales del departamento del Casanare con ClimateR

Anyi Lorena Lopez Guevara

30/11/2022

Introduccion

En este cuaderno se mostrara de manera sencilla como obtener valores mensuales de precipitacion y temperatura “normales” en el periodo comprendido entre 1981 al 2010 colocando en practica la libreria climateR del departamento del Casanare. Cuando se habla de una precipitacion “normal” no hace referencia a un valor que se esperaria, se refiere a un promedio de los valores de precipitacion durante un periodo de 30 años y de esta manera la precipitacion real en un año determinado puede encontrarse por encima o por debajo del promedio estacional o tambien podria ser “normal”.

Setup

Primero se realiza una limpieza de la memoria de R mediante el siguiente codigo:

rm(list=ls())
# install.packages("remotes")

Ahora se cargaran las librerias

library(AOI)
library(climateR)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(raster)
## Loading required package: sp
library(rasterVis)
## Warning: package 'rasterVis' was built under R version 4.2.2
## Loading required package: lattice
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(leaflet)
library(leafem)
library(RColorBrewer)
library(stars)
## Loading required package: abind

Leer el área de estudio: Casanare

list.files()
## [1] "INPUTS"             "NotebookFinal.html" "NotebookFinal.Rmd"

Se lee un archivo previo con la division politica del departamento

(casanare <- st_read("./INPUTS/CasanareCitites.shp"))
## Reading layer `CasanareCitites' from data source 
##   `C:\Users\USER\OneDrive\Documentos\GEOMATICA\GB\PF\INPUTS\CasanareCitites.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 19 features and 13 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.0989 ymin: 4.246699 xmax: -69.84787 ymax: 6.250501
## Geodetic CRS:  WGS 84
## Simple feature collection with 19 features and 13 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.0989 ymin: 4.246699 xmax: -69.84787 ymax: 6.250501
## Geodetic CRS:  WGS 84
## First 10 features:
##    fid ID_0 ISO   NAME_0 ID_1   NAME_1 ID_2         NAME_2    TYPE_2
## 1    1   53 COL Colombia   10 Casanare  388        Aguazul Municipio
## 2    2   53 COL Colombia   10 Casanare  389        Chameza Municipio
## 3    3   53 COL Colombia   10 Casanare  390   Hato Corozal Municipio
## 4    4   53 COL Colombia   10 Casanare  391      La Salina Municipio
## 5    5   53 COL Colombia   10 Casanare  392           Mani Municipio
## 6    6   53 COL Colombia   10 Casanare  393      Monterrey Municipio
## 7    7   53 COL Colombia   10 Casanare  394        Nunchia Municipio
## 8    8   53 COL Colombia   10 Casanare  395         Orocue Municipio
## 9    9   53 COL Colombia   10 Casanare  396 Paz de Ariporo Municipio
## 10  10   53 COL Colombia   10 Casanare  397           Pore Municipio
##       ENGTYPE_2 NL_NAME_2 VARNAME_2       AREA                       geometry
## 1  Municipality      <NA>      <NA>  1433.3676 POLYGON ((-72.5075 4.834401...
## 2  Municipality      <NA>      <NA>   328.6694 POLYGON ((-72.7872 5.140501...
## 3  Municipality      <NA>      <NA>  5063.0322 POLYGON ((-72.0873 6.0335, ...
## 4  Municipality      <NA>      <NA>   152.3924 POLYGON ((-72.4195 6.2267, ...
## 5  Municipality      <NA>      <NA>  3862.1055 POLYGON ((-72.2622 4.3903, ...
## 6  Municipality      <NA>      <NA>   720.9689 POLYGON ((-72.93552 5.01439...
## 7  Municipality      <NA>      <NA>  1046.8378 POLYGON ((-72.0023 5.553099...
## 8  Municipality      <NA>      <NA>  4742.7239 POLYGON ((-71.0854 4.860201...
## 9  Municipality      <NA>      <NA> 12435.9210 POLYGON ((-69.84787 6.00488...
## 10 Municipality      <NA>      <NA>   784.1395 POLYGON ((-71.9291 5.521599...

Obtener valores normales de precipitación mensuales en Casanare

Un año abarca doce meses:

oneyear = seq(1,12)

Ahora se descarga la precipitación mensual “normal” de 1981 a 2010:

precip = getTerraClimNormals(casanare, param = "prcp", period = "19812010", month=oneyear)
## Spherical geometry (s2) switched off
## Spherical geometry (s2) switched on

Ahora se observa lo que se obtuvo:

precip
## $terraclim_19812010_prcp
## class      : RasterStack 
## dimensions : 50, 79, 3950, 12  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -73.125, -69.83333, 4.208333, 6.291667  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## names      :   X01,   X02,   X03,   X04,   X05,   X06,   X07,   X08,   X09,   X10,   X11,   X12 
## min values :   1.0,  11.4,  39.4, 111.5, 105.9,  95.0,  51.4,  53.4,  61.6, 102.8,  82.5,  11.8 
## max values : 123.1, 162.0, 256.4, 406.7, 560.6, 624.4, 583.0, 490.2, 366.5, 381.6, 259.7, 210.1

Tenemos que tener en cuenta que el objeto “precip” tiene doce capas

capas <-names(precip)

Ahora obtendremos un histograma por cada una de las capas:

par(mfcol=c(12,12), mfrow=c(4,3), oma=c(1,1,0,0),
    mar=c(1,1,1,0), tcl=-0.1, mgp=c(0,0,0))
for (penta in capas) {
  hist(precip[[penta]], ylab=NA, cex.axis=0.5, font.main=1, cex.main=0.8)
}

Ahora se calcula la precicpitacion normal para todo el año:

year_precip <- sum(precip$terraclim_19812010_prcp)

Y obtenemos: una precipitacion minima de 960,4 y maxima de 3585,9

year_precip
## class      : RasterLayer 
## dimensions : 50, 79, 3950  (nrow, ncol, ncell)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -73.125, -69.83333, 4.208333, 6.291667  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 960.4, 3585.9  (min, max)

Ahora se creara una paleta de colores para la precipitacion:

ppal <- colorNumeric(c("orange", "yellow", "#a1d99b", "#deebf7", "#9ecae1","#3182bd", "darkblue"), values(year_precip$layer), 
                    na.color = "transparent")

Visualizar la precipitacion acumulada:

leaflet() %>% addTiles() %>%
  addRasterImage(year_precip, colors = ppal, opacity = 0.8) %>%
  addLegend(pal = ppal, values = values(year_precip$layer),
    title = "Annual Precipitation")
## Warning in colors(.): Some values were outside the color scale and will be
## treated as NA

Obtener valores normales mensuales de temperatura

Temperatura maxima

tmax = getTerraClimNormals(casanare, param = "tmax", period = "19812010", month=oneyear)
## Spherical geometry (s2) switched off
## Spherical geometry (s2) switched on

Visualizamos la temperatura maxima

tmax
## $terraclim_19812010_tmax
## class      : RasterStack 
## dimensions : 50, 79, 3950, 12  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -73.125, -69.83333, 4.208333, 6.291667  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## names      :  X01,  X02,  X03,  X04,  X05,  X06,  X07,  X08,  X09,  X10,  X11,  X12 
## min values :  9.9, 10.0,  9.9,  9.1,  8.7,  7.2,  6.5,  7.4,  8.1,  8.4,  9.0,  9.4 
## max values : 34.1, 35.3, 34.9, 32.9, 31.0, 29.7, 29.8, 30.7, 31.5, 31.7, 31.9, 32.4

Temperatura maxima anual

(year_tmax = max(tmax$terraclim_19812010_tmax))
## class      : RasterLayer 
## dimensions : 50, 79, 3950  (nrow, ncol, ncell)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -73.125, -69.83333, 4.208333, 6.291667  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 10, 35.3  (min, max)

Temperatura minima

tmin = getTerraClimNormals(casanare, param = "tmin", period = "19812010", month=oneyear)
## Spherical geometry (s2) switched off
## Spherical geometry (s2) switched on

Visualizamos la temperatura minima

tmin
## $terraclim_19812010_tmin
## class      : RasterStack 
## dimensions : 50, 79, 3950, 12  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -73.125, -69.83333, 4.208333, 6.291667  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## names      :  X01,  X02,  X03,  X04,  X05,  X06,  X07,  X08,  X09,  X10,  X11,  X12 
## min values :  0.0, -0.1,  1.1,  1.8,  1.4,  1.3,  0.7,  0.8,  0.7,  1.2,  1.3,  0.9 
## max values : 22.8, 23.4, 24.1, 23.9, 23.1, 22.8, 22.5, 22.7, 22.9, 23.2, 23.5, 23.2

Temperarura minima anual

(year_tmin = min(tmin$terraclim_19812010_tmin))
## class      : RasterLayer 
## dimensions : 50, 79, 3950  (nrow, ncol, ncell)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -73.125, -69.83333, 4.208333, 6.291667  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : -0.1, 22.5  (min, max)

Ahora crearemos una copia

(tempera <- stack(year_tmin, year_tmax))
## class      : RasterStack 
## dimensions : 50, 79, 3950, 2  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -73.125, -69.83333, 4.208333, 6.291667  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## names      : layer.1, layer.2 
## min values :    -0.1,    10.0 
## max values :    22.5,    35.3

Ahora cambiamos los nombres de las capas

names(tempera)  <- c("tmin", "tmax")

Ahora definiremos los valores para la visualizacion

tvals <- sort(c(values(tempera$tmin),values(tempera$tmax)))

Crearemos la paleta de colores

pal <- colorNumeric(rev(c("red", "orange", "#fff9ae",
                        "#77ab59",  "#dcf3ff", "#a2d2df", "#257ca3")), 
                         tvals, na.color = "transparent")

Ahora podremos visualizar:

capas <- names(tempera)

Crearemos la visualizacion del mapa:

# Create leaflet widget --------------------------------------------------------
m <- leaflet() %>%
  addTiles(group = "OSM (default)") %>%
  addRasterImage(year_tmin, colors = pal, opacity = 0.8, group= capas[1]) %>%
  addRasterImage(year_tmax, colors = pal, opacity = 0.8, group= capas[2]) 
## Warning in colors(.): Some values were outside the color scale and will be
## treated as NA

## Warning in colors(.): Some values were outside the color scale and will be
## treated as NA
#
# Additional leaflet options 
m <- m %>%
  # Add layers controls
  addLayersControl(
    overlayGroups = rev(capas),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  # Add common legend
  addLegend(pal = pal, values = tvals,
   title = "Annual Temperature")

Y lo visualizamos

m

Reproduccion

Citar este trabajo como: López, A., 2022. Obtener valores normales climáticos mensuales del departamento del Casanare con ClimateR.

sessionInfo()
## R version 4.2.1 (2022-06-23 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Spanish_Colombia.utf8  LC_CTYPE=Spanish_Colombia.utf8   
## [3] LC_MONETARY=Spanish_Colombia.utf8 LC_NUMERIC=C                     
## [5] LC_TIME=Spanish_Colombia.utf8    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] stars_0.5-6        abind_1.4-5        RColorBrewer_1.1-3 leafem_0.2.0      
##  [5] leaflet_2.1.1      dplyr_1.0.10       rasterVis_0.51.4   lattice_0.20-45   
##  [9] raster_3.6-3       sp_1.5-0           sf_1.0-8           climateR_0.1.0    
## [13] AOI_0.2.1         
## 
## loaded via a namespace (and not attached):
##  [1] httr_1.4.4          rnaturalearth_0.1.0 sass_0.4.2         
##  [4] jsonlite_1.8.0      viridisLite_0.4.1   foreach_1.5.2      
##  [7] bslib_0.4.0         assertthat_0.2.1    highr_0.9          
## [10] latticeExtra_0.6-30 yaml_2.3.5          pillar_1.8.1       
## [13] glue_1.6.2          digest_0.6.29       rvest_1.0.3        
## [16] colorspace_2.0-3    htmltools_0.5.3     pkgconfig_2.0.3    
## [19] s2_1.1.1            purrr_0.3.4         scales_1.2.1       
## [22] terra_1.6-17        jpeg_0.1-9          tibble_3.1.8       
## [25] proxy_0.4-27        farver_2.1.1        generics_0.1.3     
## [28] cachem_1.0.6        hexbin_1.28.2       cli_3.4.0          
## [31] fipio_1.1.1         magrittr_2.0.3      deldir_1.0-6       
## [34] evaluate_0.16       fansi_1.0.3         doParallel_1.0.17  
## [37] xml2_1.3.3          lwgeom_0.2-9        class_7.3-20       
## [40] tools_4.2.1         lifecycle_1.0.2     stringr_1.4.1      
## [43] interp_1.1-3        munsell_0.5.0       compiler_4.2.1     
## [46] jquerylib_0.1.4     e1071_1.7-11        RNetCDF_2.6-1      
## [49] rlang_1.0.5         classInt_0.4-8      units_0.8-0        
## [52] grid_4.2.1          iterators_1.0.14    rstudioapi_0.14    
## [55] htmlwidgets_1.5.4   crosstalk_1.2.0     base64enc_0.1-3    
## [58] rmarkdown_2.16      wk_0.7.0            codetools_0.2-18   
## [61] DBI_1.1.3           R6_2.5.1            zoo_1.8-11         
## [64] knitr_1.40          fastmap_1.1.0       utf8_1.2.2         
## [67] KernSmooth_2.23-20  stringi_1.7.8       parallel_4.2.1     
## [70] Rcpp_1.0.9          vctrs_0.4.1         png_0.1-7          
## [73] tidyselect_1.1.2    xfun_0.33