This is a small demostration of the tool Climate Analogues developed by CIAT for the use on RStudio.
Their motto is “finding solutions today, for tomorrow’s climate adaptation”. It allows us:
This is a small demostration of the tool, using a beautiful coffee farm - Finca La Hilda - located on the foothills of the Poas Volcano, in Alajuela, Costa Rica.
For present data, we use the database “WorldClim”, with accumulated data from the 1970-2000 period.
For future projections, we use data from the CMIP5 projections for 2050. We also choose model RCP 8.5, the most pessimistic.
For now, we only we use monthly precipitation and mean temperature.
The underlying code can be seen by clicking the button on the right (same is true for all sections).
We check for locations with similar weather to ours.
params_pres <- createParameters(x=lon, y= lat, #Coordinates
vars=c("prec","tmean"), #Variabiles
weights=c(0.5,0.5), #"weigth per variable
ndivisions=c(12,12), # Number of months
growing.season=c(1,12), # Months to inclue
rotation="tmean",
threshold=0.5,
env.data.ref=list(prec_presente, temp_presente),
env.data.targ=list(prec_presente, temp_presente),
outfile="~/.",fname=NA,writefile=FALSE)
######### Calculare la similarita
simulation_pres <- calc_similarity(params_pres)
#Set the colors
pal <- colorNumeric(c( "#FFFFCC", "#41B6C4", "#0C2C84"), values(simulation_pres),
na.color = "transparent")
#Draw the map
leaflet() %>%
addTiles() %>%
addRasterImage(simulation_pres, colors= pal, opacity = 0.5) %>%
addRasterImage(simulation_pres, colors= pal, opacity = 0.6) %>%
setView(lng= lon, lat= lat, zoom= 8) %>%
addLegend( pal= pal, values = values(simulation_pres), title = "Similarity Index")%>%
addMarkers(lng= lon, lat= lat, label= "La Hilda Farm")
Unsurprisingly, we see that the weather from La Hilda shares the most similarities with that of locations at similar altitudes within the central mountain range of Costa Rica.
You can explore the similarities in other countries, but don’t go to far because the map has a finite size.
We first plot a climatogram, to visualize the changes in weather.
#Extract values for rain present
rasvalue = extract(prec_presente, coordinates[,c(2,3)])
rasvalue= cbind(coordinates[1],rasvalue)
rasvalue = melt(rasvalue, id = c("farm"))
rasvalue = cbind(rasvalue, month= rep(1:12, each = 2))
rain_pres = subset(rasvalue, farm== "La Hilda")
#Extract values for rain future
rasvalue = extract(c5_prec, coordinates[,c(2,3)])
rasvalue= cbind(coordinates[1],rasvalue)
rasvalue = melt(rasvalue, id = c("farm"))
rasvalue = cbind(rasvalue, month= rep(1:12, each = 2))
rain_fut = subset(rasvalue, farm== "La Hilda")
#Extract values for tmean present
rasvalue = extract(temp_presente, coordinates[,c(2,3)])
rasvalue= cbind(coordinates[1],rasvalue)
rasvalue = melt(rasvalue, id = c("farm"))
rasvalue = cbind(rasvalue, month= rep(1:12, each = 2))
temp_pres = subset(rasvalue, farm== "La Hilda")
#Extract values for tmean future
rasvalue = extract(c5_temp, coordinates[,c(2,3)])
rasvalue= cbind(coordinates[1],rasvalue)
rasvalue = melt(rasvalue, id = c("farm"))
rasvalue = cbind(rasvalue, month= rep(1:12, each = 2))
temp_fut = subset(rasvalue, farm== "La Hilda")
##Bind all
alldata= cbind(rain_pres, rain_fut= rain_fut$value, temp_pres = temp_pres$value/10, temp_fut = temp_fut$value/10)
names= c("name",'x','rain_pres', 'month','rain_fut', 'temp_pres', 'temp_fut')
colnames(alldata)= names
monthsinenglish= c("Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep","Oct", "Nov", "Dec")
months <- as.data.frame(cbind(seq(1:12), monthsinenglish))
colnames(months) = c('month', 'monthname')
alldata <- merge(alldata,months)
pres = alldata[,c(2,4,6,8,1)]
pres$Time <- "Present (1970-2000)"
fut=alldata[,c(2,5,7,8,1)]
fut$Time <- "Future (2050 w/ RCP 8.5)"
names= c("Farm", "prec", "temp", "month", "monthnumber","Time")
names(pres)= names
names(fut) = names
mydata = rbind(pres, fut)
mydata$Time = factor(mydata$Time , levels= c("Present (1970-2000)","Future (2050 w/ RCP 8.5)"))
#now plot
ggplot(data = mydata, mapping = aes(x = monthnumber, y = prec, fill = Time)) +
ggtitle(label= "Climatogram for La Hilda Farm")+
scale_y_continuous("Rainfall [mm]",
sec.axis = sec_axis(~ . /5 ,
name = "Mean Temperature [°C]"))+
geom_bar(stat= "identity", position=position_dodge())+
geom_line(mapping = aes(y = temp*5, x= monthnumber), col ="black", size=1.2)+
geom_line(mapping = aes(y = temp*5, x= monthnumber, col =Time), size=0.8)+
scale_color_manual(values =c("darkolivegreen3","firebrick3"))+
scale_fill_manual(values =c("darkolivegreen3","firebrick3"))+
theme(text= element_text( size = 14),
plot.title = element_text(hjust = 0.5),
axis.text = element_text( size = 14),
legend.position="bottom")+
scale_x_continuous(breaks=seq(1:12), labels= monthsinenglish, name= "Month")
We then make the map
params_pres_fut<- createParameters(x=lon, y= lat, #Coordinates
vars=c("prec","tmean"), #Variabiles
weights=c(0.5,0.5), #"WEIGHT per variabe
ndivisions=c(12,12), # Number of months
growing.season=c(1,12), # Months to include
rotation="tmean",
threshold=0.5, #minimum similarity to plot
env.data.targ=list(prec_presente, temp_presente),
env.data.ref=list(c5_prec, c5_temp),
outfile="~/.",fname=NA,writefile=FALSE)
simulation_back <- calc_similarity(params_pres_fut)
pal <- colorNumeric(c( "#FFFFCC", "#41B6C4", "#0C2C84"), values(simulation_back),
na.color = "transparent")
leaflet() %>%
addTiles() %>%
addRasterImage(simulation_back, colors= pal, opacity = 0.5) %>%
setView(lng= lon, lat= lat, zoom= 8) %>%
addLegend( pal= pal, values = values(simulation_back), title = "Similarity Index")%>%
addMarkers(lng= lon, lat= lat, label= "La Hilda Farm")
Surprisingly, for 2050 (and according to the worst of predictions), the weather from Finca La Hilda will be more similar to those that can be found, for example, in the areas of Naranjo and Palmares. In comparison to the last map, we see that all the most similar areas have “stepped down” in the altitude range.
That is all. Hope you enjoyed.
# Forward Analysis: Where will my weather be tomorrow?
#NOT DONE YET!
#This is how I did it, but I had to tag it away from the Markdown code because it was getting to heavy, i just decided to read the previously done raster
params_forw <- createParameters(x=lon, y= lat, #Coordinates
vars=c("prec","tmean"), #Variabiles
weights=c(0.5,0.5), #weight per variable
ndivisions=c(12,12), # Number of months
growing.season=c(1,12), # Months to include
rotation="tmean",
threshold=0.5, # THreshold
env.data.targ=list(c5_prec, c5_temp),
env.data.ref=list(prec_presente, temp_presente),
outfile="~/.",fname=NA,writefile=FALSE)
simulation_forw <- calc_similarity(params_forw)
writeRaster(simulation_forw, "simulation_forw_LaHilda", overwrite= T, format= "GTiff")
#Reading the raster from my disk
simulation_forw <- Raster('simulation_forw_LaHilda.tif')
#Draw the map
pal <- colorNumeric(c( "#FFFFCC", "#41B6C4", "#0C2C84"), values(simulation_forw),
na.color = "transparent")
leaflet() %>%
addTiles() %>%
addRasterImage(simulation_forw, colors= pal, opacity = 0.6) %>%
setView(lng= lon, lat= lat, zoom= 4) %>%
addLegend( pal= pal, values = values(simulation_pres), title = "Similarity Index")%>%
addMarkers(lng= lon, lat= lat, label= "La Hilda Farm")