Climatología Temperatura Superficial del Mar
Trabajo3 Meteorología Tropical
Est. Victor Eduardo Diaz Romero
Se generó la climatología 1991-2020 a partir de datos mensuales de temperatura superficial del mar (TSM) del reanálisis ERA 5. Para todo el procesamiento se utilizó el lenguaje de programación R (versión 4.1.0) y las librerías raster, ncdf4, maps y fields.
Fuente de datos Era5: ERA5 mensual-Niveles unicos TSM(1991-2020)
Código disponible en la siguiente cuenta de Github: Viedro Github
Datos también disponibles en el siguiente Drive: Datos
#Estableciendo directorio de trabajo
setwd("C:/Users/USUARIO/Downloads")
#Librerías a usar
library(raster) #Manejo de data raster
library(fields) #Mapeo simple de matrices
library(ncdf4) #Manejo de data grillada
library(maps) #Mapa continentalPromedios mensuales climatologicos
Importación de data TSM mensual de 1991 a 2020
nc <- brick('sst(91-20).nc')
nc#Separación de capas por fechas## class : RasterBrick
## dimensions : 721, 1440, 1038240, 360 (nrow, ncol, ncell, nlayers)
## resolution : 0.25, 0.25 (x, y)
## extent : -0.125, 359.875, -90.125, 90.125 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : sst(91-20).nc
## names : X1991.01.01, X1991.02.01, X1991.03.01, X1991.04.01, X1991.05.01, X1991.06.01, X1991.07.01, X1991.08.01, X1991.09.01, X1991.10.01, X1991.11.01, X1991.12.01, X1992.01.01, X1992.02.01, X1992.03.01, ...
## Date/time : 1991-01-01, 2020-12-01 (min, max)
## varname : sst
Obtención de fechas
dates <- getZ(nc)
dates## [1] "1991-01-01" "1991-02-01" "1991-03-01" "1991-04-01" "1991-05-01"
## [6] "1991-06-01" "1991-07-01" "1991-08-01" "1991-09-01" "1991-10-01"
## [11] "1991-11-01" "1991-12-01" "1992-01-01" "1992-02-01" "1992-03-01"
## [16] "1992-04-01" "1992-05-01" "1992-06-01" "1992-07-01" "1992-08-01"
## [21] "1992-09-01" "1992-10-01" "1992-11-01" "1992-12-01" "1993-01-01"
## [26] "1993-02-01" "1993-03-01" "1993-04-01" "1993-05-01" "1993-06-01"
## [31] "1993-07-01" "1993-08-01" "1993-09-01" "1993-10-01" "1993-11-01"
## [36] "1993-12-01" "1994-01-01" "1994-02-01" "1994-03-01" "1994-04-01"
## [41] "1994-05-01" "1994-06-01" "1994-07-01" "1994-08-01" "1994-09-01"
## [46] "1994-10-01" "1994-11-01" "1994-12-01" "1995-01-01" "1995-02-01"
## [51] "1995-03-01" "1995-04-01" "1995-05-01" "1995-06-01" "1995-07-01"
## [56] "1995-08-01" "1995-09-01" "1995-10-01" "1995-11-01" "1995-12-01"
## [61] "1996-01-01" "1996-02-01" "1996-03-01" "1996-04-01" "1996-05-01"
## [66] "1996-06-01" "1996-07-01" "1996-08-01" "1996-09-01" "1996-10-01"
## [71] "1996-11-01" "1996-12-01" "1997-01-01" "1997-02-01" "1997-03-01"
## [76] "1997-04-01" "1997-05-01" "1997-06-01" "1997-07-01" "1997-08-01"
## [81] "1997-09-01" "1997-10-01" "1997-11-01" "1997-12-01" "1998-01-01"
## [86] "1998-02-01" "1998-03-01" "1998-04-01" "1998-05-01" "1998-06-01"
## [91] "1998-07-01" "1998-08-01" "1998-09-01" "1998-10-01" "1998-11-01"
## [96] "1998-12-01" "1999-01-01" "1999-02-01" "1999-03-01" "1999-04-01"
## [101] "1999-05-01" "1999-06-01" "1999-07-01" "1999-08-01" "1999-09-01"
## [106] "1999-10-01" "1999-11-01" "1999-12-01" "2000-01-01" "2000-02-01"
## [111] "2000-03-01" "2000-04-01" "2000-05-01" "2000-06-01" "2000-07-01"
## [116] "2000-08-01" "2000-09-01" "2000-10-01" "2000-11-01" "2000-12-01"
## [121] "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01" "2001-05-01"
## [126] "2001-06-01" "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01"
## [131] "2001-11-01" "2001-12-01" "2002-01-01" "2002-02-01" "2002-03-01"
## [136] "2002-04-01" "2002-05-01" "2002-06-01" "2002-07-01" "2002-08-01"
## [141] "2002-09-01" "2002-10-01" "2002-11-01" "2002-12-01" "2003-01-01"
## [146] "2003-02-01" "2003-03-01" "2003-04-01" "2003-05-01" "2003-06-01"
## [151] "2003-07-01" "2003-08-01" "2003-09-01" "2003-10-01" "2003-11-01"
## [156] "2003-12-01" "2004-01-01" "2004-02-01" "2004-03-01" "2004-04-01"
## [161] "2004-05-01" "2004-06-01" "2004-07-01" "2004-08-01" "2004-09-01"
## [166] "2004-10-01" "2004-11-01" "2004-12-01" "2005-01-01" "2005-02-01"
## [171] "2005-03-01" "2005-04-01" "2005-05-01" "2005-06-01" "2005-07-01"
## [176] "2005-08-01" "2005-09-01" "2005-10-01" "2005-11-01" "2005-12-01"
## [181] "2006-01-01" "2006-02-01" "2006-03-01" "2006-04-01" "2006-05-01"
## [186] "2006-06-01" "2006-07-01" "2006-08-01" "2006-09-01" "2006-10-01"
## [191] "2006-11-01" "2006-12-01" "2007-01-01" "2007-02-01" "2007-03-01"
## [196] "2007-04-01" "2007-05-01" "2007-06-01" "2007-07-01" "2007-08-01"
## [201] "2007-09-01" "2007-10-01" "2007-11-01" "2007-12-01" "2008-01-01"
## [206] "2008-02-01" "2008-03-01" "2008-04-01" "2008-05-01" "2008-06-01"
## [211] "2008-07-01" "2008-08-01" "2008-09-01" "2008-10-01" "2008-11-01"
## [216] "2008-12-01" "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01"
## [221] "2009-05-01" "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01"
## [226] "2009-10-01" "2009-11-01" "2009-12-01" "2010-01-01" "2010-02-01"
## [231] "2010-03-01" "2010-04-01" "2010-05-01" "2010-06-01" "2010-07-01"
## [236] "2010-08-01" "2010-09-01" "2010-10-01" "2010-11-01" "2010-12-01"
## [241] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" "2011-05-01"
## [246] "2011-06-01" "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01"
## [251] "2011-11-01" "2011-12-01" "2012-01-01" "2012-02-01" "2012-03-01"
## [256] "2012-04-01" "2012-05-01" "2012-06-01" "2012-07-01" "2012-08-01"
## [261] "2012-09-01" "2012-10-01" "2012-11-01" "2012-12-01" "2013-01-01"
## [266] "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01"
## [271] "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01"
## [276] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01"
## [281] "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01"
## [286] "2014-10-01" "2014-11-01" "2014-12-01" "2015-01-01" "2015-02-01"
## [291] "2015-03-01" "2015-04-01" "2015-05-01" "2015-06-01" "2015-07-01"
## [296] "2015-08-01" "2015-09-01" "2015-10-01" "2015-11-01" "2015-12-01"
## [301] "2016-01-01" "2016-02-01" "2016-03-01" "2016-04-01" "2016-05-01"
## [306] "2016-06-01" "2016-07-01" "2016-08-01" "2016-09-01" "2016-10-01"
## [311] "2016-11-01" "2016-12-01" "2017-01-01" "2017-02-01" "2017-03-01"
## [316] "2017-04-01" "2017-05-01" "2017-06-01" "2017-07-01" "2017-08-01"
## [321] "2017-09-01" "2017-10-01" "2017-11-01" "2017-12-01" "2018-01-01"
## [326] "2018-02-01" "2018-03-01" "2018-04-01" "2018-05-01" "2018-06-01"
## [331] "2018-07-01" "2018-08-01" "2018-09-01" "2018-10-01" "2018-11-01"
## [336] "2018-12-01" "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01"
## [341] "2019-05-01" "2019-06-01" "2019-07-01" "2019-08-01" "2019-09-01"
## [346] "2019-10-01" "2019-11-01" "2019-12-01" "2020-01-01" "2020-02-01"
## [351] "2020-03-01" "2020-04-01" "2020-05-01" "2020-06-01" "2020-07-01"
## [356] "2020-08-01" "2020-09-01" "2020-10-01" "2020-11-01" "2020-12-01"
Obtención de solo meses
months <- as.integer(format(as.Date(dates), "%m"))
months## [1] 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1
## [26] 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2
## [51] 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3
## [76] 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4
## [101] 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5
## [126] 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6
## [151] 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7
## [176] 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8
## [201] 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9
## [226] 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10
## [251] 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11
## [276] 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
## [301] 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1
## [326] 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2
## [351] 3 4 5 6 7 8 9 10 11 12
Promedio mensual climatológico
s <- stackApply(nc, months, fun=mean)
#Cambio de nombre de capas a meses
names(s) <- c("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre")Imagen simple del primer mes
image(s[[1]]-273,main="TSM Climatogía Enero (1991-2020)",xlab="Longitud",ylab="Latitud", maxpixels=100000)Esta es una forma simple de ploteo pero no permite agregar mas funcionalidades, por ello convertiremos el raster a netcdf
Conversión raster a netcdf
writeRaster(s-273, "tsm(91-20)_brick.nc", overwrite=TRUE, format="CDF", varname="tsm", varunit="degC",
longname="Temperature -- raster stack to netCDF", xname="Lon", yname="Lat", zname="Time (Month)")Abriendo el archivo netcdf recien creado
temp.nc <- nc_open("tsm(91-20)_brick.nc")
temp.nc## File tsm(91-20)_brick.nc (NC_FORMAT_CLASSIC):
##
## 2 variables (excluding dimension variables):
## int crs[]
## proj4: +proj=longlat +datum=WGS84 +no_defs
## float tsm[Lon,Lat,Time (Month)]
## units: degC
## _FillValue: -3.39999995214436e+38
## long_name: Temperature -- raster stack to netCDF
## grid_mapping: crs
## proj4: +proj=longlat +datum=WGS84 +no_defs
## min: -2.19052124023438
## min: -2.75900268554688
## min: -2.33212280273438
## min: -2.91500854492188
## min: -3.29061889648438
## min: -2.96270751953125
## min: -2.23785400390625
## min: -1.762939453125
## min: -1.72225952148438
## min: -1.935546875
## min: -2.56259155273438
## min: -2.35476684570312
## max: 31.6116333007812
## max: 31.2119750976562
## max: 31.6992797851562
## max: 32.7909851074219
## max: 32.8034057617188
## max: 32.6324157714844
## max: 33.9354858398438
## max: 34.5007629394531
## max: 33.9837036132812
## max: 32.7064514160156
## max: 31.680908203125
## max: 31.9154968261719
##
## 3 dimensions:
## Lon Size:1440
## units: degrees_east
## long_name: Lon
## Lat Size:721
## units: degrees_north
## long_name: Lat
## Time (Month) Size:12 *** is unlimited ***
## units: unknown
## long_name: Time (Month)
##
## 3 global attributes:
## Conventions: CF-1.4
## created_by: R, packages ncdf4 and raster (version 3.4-13)
## date: 2022-11-04 08:26:23
Obteniendo las variables del netcdf
temp <- ncvar_get(temp.nc,"tsm")
lat <- ncvar_get(temp.nc, "Lat")
lon <- ncvar_get(temp.nc, "Lon")
time <- ncvar_get(temp.nc, "Time (Month)")Manejando los datos
#Inversión de latitudes
lat <- rev(lat) # de 90 -90 a -90 90
#Inversión también de latitudes en el arreglo
temp <- temp[, ncol(temp):1,]
#Eligiendo la primera fecha - Enero
temp11 <- temp[ , , 1]Estableciendo escala de colores
col=designer.colors(100, c( "blue","lightblue","white","orange","red") )Grafica Climatología Enero
image.plot(lon,lat,temp11,xlab="Longitud (°)",ylab="Latitud (°)",nlevel=100,col=col,main= paste0("Climatología TSM (ºC) Enero (1991-2020)" ))
maps::map("world2", add = TRUE)
contour(lon,lat,temp11,add=T,nlevels =20 ,col="gray30",maxpixels=100000) De esta forma se podría seguir generando las graficas, pero para agilizar el proceso se hara uso de una funcion
Función para la creación de gráficas climatologicas por mes
tsm_gr <- function (i){
temp11 <- temp[ , , i]
mes <- c("Enero","Febrero","Marzo","Abril","Mayo","Junio" ,"Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre")
col=designer.colors(100, c( "blue","lightblue","white","orange","red") )
image.plot(lon,lat,temp11,xlab="Longitud (°)",ylab="Latitud (°)",nlevel=100,col=col,main= paste0("Climatología TSM (ºC) ",mes[i], " (1991-2020)" ))
maps::map("world2", add = TRUE)
contour(lon,lat,temp11,add=T,nlevels =20 ,col="gray30",maxpixels=100000)
}Ejemplo de uso Diciembre
tsm_gr(12)Vector de meses a iterar
mes <- c("Enero","Febrero","Marzo","Abril","Mayo","Junio" ,"Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre")Bucle para el guardado de graficas
for (i in 1:12){
png(paste("TSM_",mes[i],".png",sep=""), width = 10, height = 6, units = 'in', res = 700)
tsm_gr(i)
# Make plot
dev.off()
}Promedios estacionales climatologicos
Importación de data TSM mensual de 1991 a 2020
nc <- brick('sst(91-20).nc')
nc#Separación de capas por fechas## class : RasterBrick
## dimensions : 721, 1440, 1038240, 360 (nrow, ncol, ncell, nlayers)
## resolution : 0.25, 0.25 (x, y)
## extent : -0.125, 359.875, -90.125, 90.125 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : sst(91-20).nc
## names : X1991.01.01, X1991.02.01, X1991.03.01, X1991.04.01, X1991.05.01, X1991.06.01, X1991.07.01, X1991.08.01, X1991.09.01, X1991.10.01, X1991.11.01, X1991.12.01, X1992.01.01, X1992.02.01, X1992.03.01, ...
## Date/time : 1991-01-01, 2020-12-01 (min, max)
## varname : sst
Obtención de fechas
dates <- getZ(nc)
dates## [1] "1991-01-01" "1991-02-01" "1991-03-01" "1991-04-01" "1991-05-01"
## [6] "1991-06-01" "1991-07-01" "1991-08-01" "1991-09-01" "1991-10-01"
## [11] "1991-11-01" "1991-12-01" "1992-01-01" "1992-02-01" "1992-03-01"
## [16] "1992-04-01" "1992-05-01" "1992-06-01" "1992-07-01" "1992-08-01"
## [21] "1992-09-01" "1992-10-01" "1992-11-01" "1992-12-01" "1993-01-01"
## [26] "1993-02-01" "1993-03-01" "1993-04-01" "1993-05-01" "1993-06-01"
## [31] "1993-07-01" "1993-08-01" "1993-09-01" "1993-10-01" "1993-11-01"
## [36] "1993-12-01" "1994-01-01" "1994-02-01" "1994-03-01" "1994-04-01"
## [41] "1994-05-01" "1994-06-01" "1994-07-01" "1994-08-01" "1994-09-01"
## [46] "1994-10-01" "1994-11-01" "1994-12-01" "1995-01-01" "1995-02-01"
## [51] "1995-03-01" "1995-04-01" "1995-05-01" "1995-06-01" "1995-07-01"
## [56] "1995-08-01" "1995-09-01" "1995-10-01" "1995-11-01" "1995-12-01"
## [61] "1996-01-01" "1996-02-01" "1996-03-01" "1996-04-01" "1996-05-01"
## [66] "1996-06-01" "1996-07-01" "1996-08-01" "1996-09-01" "1996-10-01"
## [71] "1996-11-01" "1996-12-01" "1997-01-01" "1997-02-01" "1997-03-01"
## [76] "1997-04-01" "1997-05-01" "1997-06-01" "1997-07-01" "1997-08-01"
## [81] "1997-09-01" "1997-10-01" "1997-11-01" "1997-12-01" "1998-01-01"
## [86] "1998-02-01" "1998-03-01" "1998-04-01" "1998-05-01" "1998-06-01"
## [91] "1998-07-01" "1998-08-01" "1998-09-01" "1998-10-01" "1998-11-01"
## [96] "1998-12-01" "1999-01-01" "1999-02-01" "1999-03-01" "1999-04-01"
## [101] "1999-05-01" "1999-06-01" "1999-07-01" "1999-08-01" "1999-09-01"
## [106] "1999-10-01" "1999-11-01" "1999-12-01" "2000-01-01" "2000-02-01"
## [111] "2000-03-01" "2000-04-01" "2000-05-01" "2000-06-01" "2000-07-01"
## [116] "2000-08-01" "2000-09-01" "2000-10-01" "2000-11-01" "2000-12-01"
## [121] "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01" "2001-05-01"
## [126] "2001-06-01" "2001-07-01" "2001-08-01" "2001-09-01" "2001-10-01"
## [131] "2001-11-01" "2001-12-01" "2002-01-01" "2002-02-01" "2002-03-01"
## [136] "2002-04-01" "2002-05-01" "2002-06-01" "2002-07-01" "2002-08-01"
## [141] "2002-09-01" "2002-10-01" "2002-11-01" "2002-12-01" "2003-01-01"
## [146] "2003-02-01" "2003-03-01" "2003-04-01" "2003-05-01" "2003-06-01"
## [151] "2003-07-01" "2003-08-01" "2003-09-01" "2003-10-01" "2003-11-01"
## [156] "2003-12-01" "2004-01-01" "2004-02-01" "2004-03-01" "2004-04-01"
## [161] "2004-05-01" "2004-06-01" "2004-07-01" "2004-08-01" "2004-09-01"
## [166] "2004-10-01" "2004-11-01" "2004-12-01" "2005-01-01" "2005-02-01"
## [171] "2005-03-01" "2005-04-01" "2005-05-01" "2005-06-01" "2005-07-01"
## [176] "2005-08-01" "2005-09-01" "2005-10-01" "2005-11-01" "2005-12-01"
## [181] "2006-01-01" "2006-02-01" "2006-03-01" "2006-04-01" "2006-05-01"
## [186] "2006-06-01" "2006-07-01" "2006-08-01" "2006-09-01" "2006-10-01"
## [191] "2006-11-01" "2006-12-01" "2007-01-01" "2007-02-01" "2007-03-01"
## [196] "2007-04-01" "2007-05-01" "2007-06-01" "2007-07-01" "2007-08-01"
## [201] "2007-09-01" "2007-10-01" "2007-11-01" "2007-12-01" "2008-01-01"
## [206] "2008-02-01" "2008-03-01" "2008-04-01" "2008-05-01" "2008-06-01"
## [211] "2008-07-01" "2008-08-01" "2008-09-01" "2008-10-01" "2008-11-01"
## [216] "2008-12-01" "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01"
## [221] "2009-05-01" "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01"
## [226] "2009-10-01" "2009-11-01" "2009-12-01" "2010-01-01" "2010-02-01"
## [231] "2010-03-01" "2010-04-01" "2010-05-01" "2010-06-01" "2010-07-01"
## [236] "2010-08-01" "2010-09-01" "2010-10-01" "2010-11-01" "2010-12-01"
## [241] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" "2011-05-01"
## [246] "2011-06-01" "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01"
## [251] "2011-11-01" "2011-12-01" "2012-01-01" "2012-02-01" "2012-03-01"
## [256] "2012-04-01" "2012-05-01" "2012-06-01" "2012-07-01" "2012-08-01"
## [261] "2012-09-01" "2012-10-01" "2012-11-01" "2012-12-01" "2013-01-01"
## [266] "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01"
## [271] "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01"
## [276] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01"
## [281] "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01"
## [286] "2014-10-01" "2014-11-01" "2014-12-01" "2015-01-01" "2015-02-01"
## [291] "2015-03-01" "2015-04-01" "2015-05-01" "2015-06-01" "2015-07-01"
## [296] "2015-08-01" "2015-09-01" "2015-10-01" "2015-11-01" "2015-12-01"
## [301] "2016-01-01" "2016-02-01" "2016-03-01" "2016-04-01" "2016-05-01"
## [306] "2016-06-01" "2016-07-01" "2016-08-01" "2016-09-01" "2016-10-01"
## [311] "2016-11-01" "2016-12-01" "2017-01-01" "2017-02-01" "2017-03-01"
## [316] "2017-04-01" "2017-05-01" "2017-06-01" "2017-07-01" "2017-08-01"
## [321] "2017-09-01" "2017-10-01" "2017-11-01" "2017-12-01" "2018-01-01"
## [326] "2018-02-01" "2018-03-01" "2018-04-01" "2018-05-01" "2018-06-01"
## [331] "2018-07-01" "2018-08-01" "2018-09-01" "2018-10-01" "2018-11-01"
## [336] "2018-12-01" "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01"
## [341] "2019-05-01" "2019-06-01" "2019-07-01" "2019-08-01" "2019-09-01"
## [346] "2019-10-01" "2019-11-01" "2019-12-01" "2020-01-01" "2020-02-01"
## [351] "2020-03-01" "2020-04-01" "2020-05-01" "2020-06-01" "2020-07-01"
## [356] "2020-08-01" "2020-09-01" "2020-10-01" "2020-11-01" "2020-12-01"
Obtención de solo meses
months <- as.integer(format(as.Date(dates), "%m"))
months## [1] 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1
## [26] 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2
## [51] 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3
## [76] 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4
## [101] 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5
## [126] 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6
## [151] 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7
## [176] 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8
## [201] 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9
## [226] 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10
## [251] 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11
## [276] 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
## [301] 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1
## [326] 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2
## [351] 3 4 5 6 7 8 9 10 11 12
Bucle para obtener climatologias estacionales
for (i in 1:length(months)){
if(months[i] == 12|months[i]== 1 | months[i]==2){
months[i]= 1
}
if(months[i] == 3|months[i]== 4 | months[i]==5){
months[i]= 2
}
if(months[i] == 6|months[i]== 7 | months[i]==8){
months[i]= 3
}
if(months[i] == 9|months[i]== 10 | months[i]==11){
months[i]= 4
}
}
months## [1] 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1
## [38] 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1
## [75] 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2
## [112] 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2
## [149] 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2
## [186] 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3
## [223] 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3
## [260] 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3
## [297] 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4
## [334] 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1
Promedios estacionales
s <- stackApply(nc, months, fun=mean)
## Cambio de nombre de fechas
names(s) <- c("verano","Otoño","Invierno","Primavera")Imagen simple de la primea estacion
image(s[[1]]-273,main="TSM Climatogía Verano (1991-2020)",xlab="Longitud",ylab="Latitud", maxpixels=100000)Conversión raster a netcdf
writeRaster(s-273, "tsm(91-20)_est_brick.nc", overwrite=TRUE, format="CDF", varname="tsm", varunit="degC",
longname="Temperature -- raster stack to netCDF", xname="Lon", yname="Lat", zname="Time (Month)")Importación de netcdf estacional recien creado
temp.nc <- nc_open("tsm(91-20)_est_brick.nc")
temp.nc## File tsm(91-20)_est_brick.nc (NC_FORMAT_CLASSIC):
##
## 2 variables (excluding dimension variables):
## int crs[]
## proj4: +proj=longlat +datum=WGS84 +no_defs
## float tsm[Lon,Lat,Time (Month)]
## units: degC
## _FillValue: -3.39999995214436e+38
## long_name: Temperature -- raster stack to netCDF
## grid_mapping: crs
## proj4: +proj=longlat +datum=WGS84 +no_defs
## min: -2.1788330078125
## min: -2.84591674804688
## min: -2.30206298828125
## min: -1.88693237304688
## max: 31.5797119140625
## max: 32.4312133789062
## max: 33.258056640625
## max: 32.1280822753906
##
## 3 dimensions:
## Lon Size:1440
## units: degrees_east
## long_name: Lon
## Lat Size:721
## units: degrees_north
## long_name: Lat
## Time (Month) Size:4 *** is unlimited ***
## units: unknown
## long_name: Time (Month)
##
## 3 global attributes:
## Conventions: CF-1.4
## created_by: R, packages ncdf4 and raster (version 3.4-13)
## date: 2022-11-04 08:32:43
Obteniendo las variables del netcdf
temp <- ncvar_get(temp.nc,"tsm")
lat <- ncvar_get(temp.nc, "Lat")
lon <- ncvar_get(temp.nc, "Lon")
time <- ncvar_get(temp.nc, "Time (Month)")Manejo de datos
#Inversión de latitudes
lat <- rev(lat) # de 90 -90 a -90 90
#Inversión también de latitudes en el arreglo
temp <- temp[, ncol(temp):1,]
#Eligiendo la primera fecha - Enero
temp11 <- temp[ , , 1]Estableciendo escala de colores
col=designer.colors(100, c( "blue","lightblue","white","orange","red") )Grafica Climatología Verano
image.plot(lon,lat,temp11,xlab="Longitud (°)",ylab="Latitud (°)",nlevel=100,col=col,main= paste0("Climatología TSM (ºC) Verano (1991-2020)" ))
maps::map("world2", add = TRUE)
contour(lon,lat,temp11,add=T,nlevels =20 ,col="gray30",maxpixels=100000)Función para la creación de gráficas climatológicas por estación
tsm_g_est <- function (i){
temp11 <- temp[ , , i]
mes <- c("verano","Otoño","Invierno","Primavera")
col=designer.colors(100, c( "blue","lightblue","white","orange","red") )
image.plot(lon,lat,temp11,xlab="Longitud (°)",ylab="Latitud (°)",nlevel=100,col=col,main= paste0("Climatología TSM (ºC) ",mes[i], " (1991-2020)" ))
maps::map("world2", add = TRUE)
contour(lon,lat,temp11,add=T,nlevels =20 ,col="gray30",maxpixels=100000)
}Probando función Invierno Climatológico
tsm_g_est(3)Vector de estaciones a iterar
mes <- c("verano","Otoño","Invierno","Primavera")Bucle para el guardado de graficas
for (i in 1:4){
png(paste("TSM_",mes[i],".png",sep=""), width = 10, height = 6, units = 'in', res = 700)
tsm_g_est(i)
# Make plot
dev.off()
}