/
day13_raster.R
167 lines (126 loc) · 5.08 KB
/
day13_raster.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
library(sf)
library(raster)
#library(tabularaster)
library(dplyr)
library(ggplot2)
options(scipen=10)
#from: https://psl.noaa.gov/data/gridded/data.cpc.globalprecip.html
#Load Rasters
prec <- brick("C:/Richard/R and Python/Environmental Data Science/GIS/ChileClimate/Data_raw/precip.2019.nc")
prec <- rotate(prec)
### The whole world
library(gganimate)
library(tmap)
data("World")
germany <- World %>% filter(name=="Germany") %>%
st_transform(crs(prec)@projargs)
crs(prec) <- crs(germany)
rt_germany <- prec %>%
mask(germany) %>%
rasterToPoints() %>%
data.frame() %>%
tidyr::pivot_longer(cols=-c(x,y),names_to="date",values_to="value") %>%
mutate(date=gsub("X","",date),
date=gsub("[.]","-",date) %>% as.Date("%Y-%m-%d"))
cities <- data.frame(
city=c("Berlin","Munich","Hamburg","Cologne"),
lon=c(52.516667,48.15,53.575323,50.933333),
lat=c(13.4,11.583333,10.01534,6.95)
)
rt_germany %>%
group_by(x,y) %>%
summarise(total_precipitation=sum(value)) %>%
ggplot()+
geom_tile(aes(x=x,y=y,fill=total_precipitation))+
geom_point(data=cities,aes(x=lat,y=lon))+
geom_label(data=cities,aes(x=lat,y=lon,label=city),fill = alpha("white",0.5),nudge_y = -0.4)+
borders(regions="Germany")+
coord_quickmap()+
scale_fill_gradient2(low = "lightblue", mid = "blue", high = "darkblue",midpoint = 900)+
labs(title = 'Total precipitation 2019 in Germany',
fill = "Precip (mm)",
caption = "CPC Global Unified Precipitation data provided by the NOAA/OAR/ESRL from their Web site at https://psl.noaa.gov/") +
theme_void()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),plot.caption = element_text(size=7))
anim <- rt_germany %>%
mutate(value=ifelse(value==0,NA,value)) %>%
ggplot()+
geom_tile(aes(x=x,y=y,fill=value))+
geom_point(data=cities,aes(x=lat,y=lon))+
geom_label(data=cities,aes(x=lat,y=lon,label=city),fill = alpha("white",0.5),nudge_y = -0.3)+
borders(regions="Germany")+
coord_quickmap()+
transition_states(date,state_length = 0.3)+
enter_fade()+
exit_shrink()+
scale_fill_gradient2(low = "white", mid = "deepskyblue2",
high = "darkblue",midpoint = 25,na.value = "white")+
labs(title = 'Date: {closest_state}',
fill = "Precipitation in mm",
caption = "CPC Global Unified Precipitation data provided by the NOAA/OAR/ESRL from https://psl.noaa.gov/") +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),plot.caption = element_text(size=7))
# Video output
an <- animate(anim,nframes=1000)
anim_save("precip.gif",an)
#### Other things and ideas
#Get the scale of measurement
ncin <- ncdf4::nc_open("C:/Richard/R and Python/Environmental Data Science/GIS/ChileClimate/Data_raw/precip.2019.nc")
ncdf4::ncatt_get(ncin,"precip","units")
ncdf4::nc_close(ncin)
#Load Shapefiles from Chilean regions
regiones <- read_sf("C:/Richard/R and Python/Environmental Data Science/Regiones/Regional.shp")
### Optional: preprocessing - removing Easter Island
## I am sure there are more elegant ways to solve this
#For Valparaiso remove Easter Island and Isla Juan Fernandez, then buffer to make it larger
valpo <- read_sf("C:/Richard/R and Python/Environmental Data Science/Comunas/comunas.shp") %>%
filter(!Comuna %in% c("Isla de Pascua","Juan Fernández"),Region=="Región de Valparaíso") %>%
st_simplify(dTolerance=500) %>%
st_buffer(5000) %>%
st_union()
#Plot Valparaiso Region without Easter Island
valpo %>%
ggplot()+
geom_sf()+
theme(legend.position = "none")
#Intersect Valparaiso with the new Valpo without Easter Island
test <- regiones %>% filter(Region=="Región de Valparaíso") %>%
st_buffer(dist=0) %>%
st_intersection(valpo)
#Remove old Valparaiso region from regions and add new Valpo
regiones <- regiones %>%
filter(!Region%in%c("Región de Valparaíso","Zona sin demarcar")) %>%
rbind(test)
#Simplified version for faster visualization
reg_simp <- regiones %>%
st_simplify(dTolerance=8000)
reg_simp %>%
ggplot()+
geom_sf()+
theme(legend.position = "none")
regiones <- regiones %>% st_transform(crs(prec)@projargs)
reg_simp <- reg_simp %>% st_transform(crs(prec)@projargs)
crs(prec) <- crs(regiones)
#Convert to Dataframe and visualize
rt <- prec %>%
mask(reg_simp) %>%
rasterToPoints() %>%
data.frame() %>%
tidyr::pivot_longer(cols=-c(x,y),names_to="date",values_to="value") %>%
mutate(date=gsub("X","",date),
date=gsub("[.]","-",date) %>% as.Date("%Y-%m-%d"))
rt %>% mutate(month=format(date,"%b")) %>%
mutate(month=forcats::fct_reorder(month,as.numeric(format(date,"%m")))) %>%
group_by(x,y,month) %>%
summarise(value=sum(value)) %>%
ggplot()+
geom_tile(aes(x=x,y=y,fill=log(value)))+
coord_quickmap()+
facet_grid(~month)+
labs(caption = "CPC Global Unified Precipitation data provided by the NOAA/OAR/ESRL PSL, Boulder, Colorado, USA, from their Web site at https://psl.noaa.gov/")+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),plot.caption = element_text(size=7))