1. Introducción.

En este cuaderno se desarrollará la ilustración de valores mensuales de precipitación y temperatura “normales”. Se estudia, con ayuda de la biblioteca ClimateR, un periodo de 29 años (1981-2010). Se tiene que la precipitación normal no es igual al valor esperado, si no que es un promedio de los valores obtenidos durante esos años. Los valores reales de precipitación en un año dado puede ser superior o inferior al promedio estacional o ser “normal”.

2. Configuración.

rm(list=ls())
library(AOI)
library(climateR)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.3.2, PROJ 7.2.1; sf_use_s2() is TRUE
library(raster)
## Loading required package: sp
library(rasterVis)
## 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

3. Leer y estudiar el área.

(stder <- st_read("C:/Users/Gerardo/Desktop/GB2022/datos/15_BOYACA/Administrativo/MGN_MPIO_POLITICO.shp"))
## Reading layer `MGN_MPIO_POLITICO' from data source 
##   `C:\Users\GERARDO\Desktop\GB2022\Datos\15_BOYACA\ADMINISTRATIVO\MGN_MPIO_POLITICO.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 123 features and 9 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.66496 ymin: 4.655196 xmax: -71.94885 ymax: 7.055557
## Geodetic CRS:  WGS 84
## Simple feature collection with 123 features and 9 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.66496 ymin: 4.655196 xmax: -71.94885 ymax: 7.055557
## Geodetic CRS:  WGS 84
## First 10 features:
##    DPTO_CCDGO MPIO_CCDGO MPIO_CNMBR                           MPIO_CRSLC
## 1          15      15001      TUNJA                                 1541
## 2          15      15022    ALMEIDA                                 1908
## 3          15      15047  AQUITANIA                                 1789
## 4          15      15051   ARCABUCO                                 1856
## 5          15      15087      BELÉN                                 1756
## 6          15      15090     BERBEO      Ordenanza 28 de Abril 9 de 1913
## 7          15      15092  BETÉITIVA                                 1754
## 8          15      15097    BOAVITA                                 1613
## 9          15      15104     BOYACÁ                                 1537
## 10         15      15106    BRICEÑO Ordenanza 14 del 25 de Julio de 1890
##    MPIO_NAREA MPIO_NANO DPTO_CNMBR Shape_Leng  Shape_Area
## 1   119.68957      2017     BOYACÁ  0.5723744 0.009766301
## 2    57.67212      2017     BOYACÁ  0.3484692 0.004701759
## 3   942.14660      2017     BOYACÁ  1.8003115 0.076843504
## 4   137.89859      2017     BOYACÁ  0.7527090 0.011256738
## 5   163.08822      2017     BOYACÁ  0.6293489 0.013314920
## 6    58.01301      2017     BOYACÁ  0.4291743 0.004730850
## 7   101.89955      2017     BOYACÁ  0.4738184 0.008317810
## 8   145.30529      2017     BOYACÁ  0.6597822 0.011867743
## 9    48.02287      2017     BOYACÁ  0.3256140 0.003918022
## 10   64.59970      2017     BOYACÁ  0.4849753 0.005273255
##                          geometry
## 1  POLYGON ((-73.34014 5.58308...
## 2  POLYGON ((-73.36793 5.01349...
## 3  POLYGON ((-72.76242 5.63856...
## 4  POLYGON ((-73.50487 5.84347...
## 5  POLYGON ((-72.91692 6.08612...
## 6  POLYGON ((-73.0677 5.27048,...
## 7  POLYGON ((-72.81796 5.97422...
## 8  POLYGON ((-72.64907 6.43640...
## 9  POLYGON ((-73.34806 5.47411...
## 10 POLYGON ((-73.89118 5.73749...

4. Obtener las precipitaciones normales mensuales.

oneyear = seq(1,12)
precip = getTerraClimNormals(stder, param = "prcp", period = "19812010", month=oneyear)
## Spherical geometry (s2) switched off
## Spherical geometry (s2) switched on
precip
## $terraclim_19812010_prcp
## class      : RasterStack 
## dimensions : 59, 66, 3894, 12  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -74.66667, -71.91667, 4.625, 7.083333  (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,   5.0,  18.7, 111.5, 100.1,  78.4,  39.3,  44.8,  61.6,  73.2,  76.2,  11.8 
## max values : 128.2, 172.0, 286.7, 413.7, 618.8, 582.1, 638.9, 441.7, 392.0, 460.4, 366.8, 273.1
capas <-names(precip)
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)
}

year_precip <- sum(precip$terraclim_19812010_prcp)
year_precip
## class      : RasterLayer 
## dimensions : 59, 66, 3894  (nrow, ncol, ncell)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -74.66667, -71.91667, 4.625, 7.083333  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 872.2, 3780.9  (min, max)
ppal <- colorNumeric(c("#e66b00", "yellow", "#a1d99b", "#deebf7", "#9ecae1","#3182bd", "darkblue"), values(year_precip$layer), 
                    na.color = "transparent")
library(rgdal)
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
## Path to GDAL shared files: C:/Users/GERARDO/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/GERARDO/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
leaflet() %>% addTiles() %>%
  addRasterImage(year_precip, colors = ppal, opacity = 0.8) %>%
  addLegend(pal = ppal, values = values(year_precip$layer),
    title = "Precipitación anual")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition

5. Obtener la temperatura normal anual.

tmax = getTerraClimNormals(stder, param = "tmax", period = "19812010", month=oneyear)
## Spherical geometry (s2) switched off
## Spherical geometry (s2) switched on
tmax
## $terraclim_19812010_tmax
## class      : RasterStack 
## dimensions : 59, 66, 3894, 12  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -74.66667, -71.91667, 4.625, 7.083333  (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 :  6.4,  6.4,  6.4,  5.7,  5.3,  3.9,  3.3,  4.1,  4.9,  5.1,  5.5,  6.1 
## max values : 33.7, 34.9, 34.3, 32.9, 33.0, 33.0, 34.3, 34.1, 33.1, 32.1, 31.9, 32.6
(year_tmax = max(tmax$terraclim_19812010_tmax))
## class      : RasterLayer 
## dimensions : 59, 66, 3894  (nrow, ncol, ncell)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -74.66667, -71.91667, 4.625, 7.083333  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 6.4, 34.9  (min, max)
tmin = getTerraClimNormals(stder, param = "tmin", period = "19812010", month=oneyear)
## Spherical geometry (s2) switched off
## Spherical geometry (s2) switched on
tmin
## $terraclim_19812010_tmin
## class      : RasterStack 
## dimensions : 59, 66, 3894, 12  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -74.66667, -71.91667, 4.625, 7.083333  (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 : -3.6, -3.7, -2.4, -1.8, -2.1, -1.9, -2.4, -2.3, -2.3, -2.2, -2.1, -2.6 
## max values : 23.9, 24.0, 24.3, 24.1, 23.8, 23.9, 23.4, 23.6, 23.4, 23.5, 23.8, 24.2
(year_tmin = min(tmin$terraclim_19812010_tmin))
## class      : RasterLayer 
## dimensions : 59, 66, 3894  (nrow, ncol, ncell)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -74.66667, -71.91667, 4.625, 7.083333  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : -3.7, 23.4  (min, max)
(tempera <- stack(year_tmin, year_tmax))
## class      : RasterStack 
## dimensions : 59, 66, 3894, 2  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -74.66667, -71.91667, 4.625, 7.083333  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## names      : layer.1, layer.2 
## min values :    -3.7,     6.4 
## max values :    23.4,    34.9
names(tempera)  <- c("tmin", "tmax")
tvals <- sort(c(values(tempera$tmin),values(tempera$tmax)))
pal <- colorNumeric(rev(c("red", "orange", "#fff9ae",
                        "#77ab59",  "#dcf3ff", "#a2d2df", "#257ca3")), 
                         tvals, na.color = "transparent")
capas <- names(tempera)
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]) 
#
# 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 = "Temperatura anual")
m
sessionInfo()
## R version 4.2.0 (2022-04-22 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19043)
## 
## 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] rgdal_1.5-32       stars_0.5-5        abind_1.4-5        RColorBrewer_1.1-3
##  [5] leafem_0.2.0       leaflet_2.1.1      dplyr_1.0.9        rasterVis_0.51.2  
##  [9] lattice_0.20-45    raster_3.5-15      sp_1.5-0           sf_1.0-7          
## [13] climateR_0.1.0     AOI_0.2.1         
## 
## loaded via a namespace (and not attached):
##  [1] httr_1.4.3          rnaturalearth_0.1.0 sass_0.4.1         
##  [4] jsonlite_1.8.0      viridisLite_0.4.0   foreach_1.5.2      
##  [7] bslib_0.3.1         assertthat_0.2.1    highr_0.9          
## [10] latticeExtra_0.6-29 yaml_2.3.5          pillar_1.7.0       
## [13] glue_1.6.2          digest_0.6.29       rvest_1.0.2        
## [16] colorspace_2.0-3    htmltools_0.5.2     pkgconfig_2.0.3    
## [19] s2_1.0.7            purrr_0.3.4         scales_1.2.0       
## [22] terra_1.5-21        jpeg_0.1-9          tibble_3.1.7       
## [25] proxy_0.4-27        farver_2.1.0        generics_0.1.2     
## [28] ellipsis_0.3.2      hexbin_1.28.2       cli_3.3.0          
## [31] fipio_1.1.1         magrittr_2.0.3      crayon_1.5.1       
## [34] evaluate_0.15       fansi_1.0.3         doParallel_1.0.17  
## [37] xml2_1.3.3          lwgeom_0.2-8        class_7.3-20       
## [40] tools_4.2.0         lifecycle_1.0.1     stringr_1.4.0      
## [43] munsell_0.5.0       compiler_4.2.0      jquerylib_0.1.4    
## [46] e1071_1.7-11        RNetCDF_2.5-2       rlang_1.0.2        
## [49] classInt_0.4-7      units_0.8-0         grid_4.2.0         
## [52] iterators_1.0.14    rstudioapi_0.13     htmlwidgets_1.5.4  
## [55] crosstalk_1.2.0     base64enc_0.1-3     rmarkdown_2.14     
## [58] wk_0.6.0            codetools_0.2-18    DBI_1.1.3          
## [61] R6_2.5.1            zoo_1.8-10          knitr_1.39         
## [64] fastmap_1.1.0       utf8_1.2.2          KernSmooth_2.23-20 
## [67] stringi_1.7.6       parallel_4.2.0      Rcpp_1.0.8.3       
## [70] vctrs_0.4.1         png_0.1-7           tidyselect_1.1.2   
## [73] xfun_0.31