-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_combine_dataset.Rmd
224 lines (183 loc) · 7.49 KB
/
03_combine_dataset.Rmd
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
title: "03_combine_dataset"
output: github_document
---
Combine results from multiple ERDDAPs
In this notebook we use data from one ERDDAP (OSMC animal-borne sensors) to make subsequent queries to other ERDDAPs supplying Argo and satellite data
```{r}
#| echo: false
library(dplyr)
library(ggplot2)
library(lubridate)
library(mapdata)
library(mapproj)
#library(plotdap)
library(rerddap)
```
1. MEOPS animals
We can use ERDDAP's built in search function to find datasets that match a keyword. Let's find some animal borne sensor data
```{r}
# use ERDDAP search through 'rerddap::ed_search()'
osmc_erddap <- "https://osmc.noaa.gov/erddap/"
animals <- ed_search("animal", which = "tabledap", url = osmc_erddap)
head(animals$info)
```
```{r}
# use ERDDAP search through 'rerddap::tabledap()'
# to obtain all "Southern ellie records
df_MEOP_info <- info("MEOP_profiles", url = osmc_erddap )
df_MEOP <- tabledap(df_MEOP_info,
fields = c('latitude', 'longitude', 'time', 'species'),
'species="Southern ellie"',
'longitude>=-90',
'longitude<=-60',
'latitude<=-60',
url = osmc_erddap
)
# convert latitude and longitude to numeric
df_MEOP$latitude <- as.numeric(df_MEOP$latitude)
df_MEOP$longitude <- as.numeric(df_MEOP$longitude)
# convert time to datetime using 'lubridate::as_datetime()'
df_MEOP$time <- as_datetime(df_MEOP$time)
# and as a factor a year column using 'lubridate::year()'
df_MEOP$year <- as.factor(year(df_MEOP$time))
df_MEOP$source <- rep('MEOP', length(df_MEOP$time))
head(df_MEOP)
# plot using ggplot2
# set plot limits
xlim <- c(-90, -60)
ylim <- c(-80, -60)
# get hi-res map outline
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
# plot both layers
p <- ggplot() +
geom_point(data = df_MEOP, aes(x = longitude, y = latitude, colour = year), size = 0.1) +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "grey80") +
coord_quickmap(xlim = xlim, ylim = ylim) +
theme_bw() +
ylab("latitude") + xlab("longitude")
p
```
2. Add argo data
We use a bounding box from the lon, lat and time of the seal data to look for co-located Argo floats on the ifremer ERDDAP
```{r}
argo_erddap <- 'https://erddap.ifremer.fr/erddap/'
# get info for the argo data
argo_erddap_info <- info("ArgoFloats", url = argo_erddap)
# get the field names from the info result
fields <- argo_erddap_info$variables$variable_name
# set the contraints based on the MEOP download
time_min_constraint <- paste0('time>=', min(df_MEOP$time))
time_max_constraint <- paste0('time<=', max(df_MEOP$time))
lat_min_constraint <- paste0('latitude>=', min(df_MEOP$latitude))
lat_max_constraint <- paste0('latitude<=', max(df_MEOP$latitude))
lon_min_constraint <- paste0('longitude>=', min(df_MEOP$longitude))
lon_max_constraint <- paste0('longitude<=', max(df_MEOP$longitude))
# obtain argo data using tabledap
df_argo <- tabledap(argo_erddap_info,
fields = fields,
time_min_constraint,
time_max_constraint,
lat_min_constraint,
lat_max_constraint,
lon_min_constraint,
lon_max_constraint,
url = argo_erddap
)
# convert latitdue and longitude to numerics
df_argo$latitude <- as.numeric(df_argo$latitude)
df_argo$longitude <- as.numeric(df_argo$longitude)
# convert time to R datetime
df_argo$time <- as_datetime(df_argo$time)
# create new column for year
df_argo$year <- as.factor(year(df_argo$time))
df_argo$source <- rep('argo', length(df_argo$time))
# map the data
xlim <- c(-90, -60)
ylim <- c(-80, -60)
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
ggplot() +
geom_point(data = df_argo, aes(x = longitude, y = latitude, colour = year), size = 0.1) +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "grey80") +
coord_quickmap(xlim = xlim, ylim = ylim) +
theme_bw() +
ylab("latitude") + xlab("longitude")
```
now compare the two datasets across time:
```{r}
xlim <- c(-90, -60)
ylim <- c(-80, -60)
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
ggplot() +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "grey80") +
geom_point(data = df_argo, aes(x = longitude, y = latitude, color = source,), size = 0.05, show.legend = TRUE) +
#scale_fill_manual(values = ('Argo' = 'red') )+
geom_point(data = df_MEOP, aes(x = longitude, y = latitude, color = source), size = 0.05, show.legend = TRUE) +
#scale_fill_manual(values= ('MEOP' ='black')) +
labs(x = 'longitude',
y = 'latitude',
color = 'Source') +
scale_color_manual(values=c('argo' = 'red', 'MEOP' ='black')) +
coord_quickmap(xlim = xlim, ylim = ylim) +
theme_bw()
```
3. Gridded SST
Finally, we get some matching JPL reanalysis SST data from the Coastwatch West Coast Node (wcn) ERDDAP
```{r}
wcn_erddap <- 'https://coastwatch.pfeg.noaa.gov/erddap'
# get info for the MUR data
mur_info <- info('jplMURSST41', url = wcn_erddap)
# set constraints based on MEOP data
time <- as.character(c(min(df_MEOP$time), max(df_MEOP$time)))
latitude <- c(min(df_MEOP$latitude), max(df_MEOP$latitude))
longitude <- c( min(df_MEOP$longitude), max(df_MEOP$longitude))
# because of possible size of extract, "thin" the request
stride <- c(365, 10, 10)
# extract data using gridddap
sst <- griddap(mur_info,
fields = 'analysed_sst',
stride = stride,
time = time,
latitude = latitude,
longitude = longitude,
url = wcn_erddap
)
```
# plot against first time
```{r, warning = FALSE}
xlim <- c(-90, -60)
ylim <- c(-80, -60)
first_time <- filter(sst$data, time == "2005-02-13T09:00:00Z")
mycolor <- colors$temperature
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
ggplot(data = first_time, aes(x = longitude, y = latitude, fill = analysed_sst)) +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "grey80") +
geom_raster(interpolate = FALSE) +
scale_fill_gradientn(colours = mycolor, na.value = NA) +
theme_bw() + ylab("latitude") + xlab("longitude") +
coord_quickmap(xlim = xlim, ylim = ylim)
```
```{r, warning = FALSE}
xlim <- c(-90, -60)
ylim <- c(-80, -60)
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
ggplot() +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "grey80") +
geom_raster(data = first_time, aes(x = longitude, y = latitude, fill = analysed_sst), interpolate = FALSE) +
scale_fill_gradientn(colours = mycolor, na.value = NA) +
geom_point(data = df_argo, aes(x = longitude, y = latitude, color = source,), size = 0.05, show.legend = TRUE) +
geom_point(data = df_MEOP, aes(x = longitude, y = latitude, color = source), size = 0.05, show.legend = TRUE) +
labs(x = 'longitude',
y = 'latitude',
color = 'Source') +
scale_color_manual(values=c('argo' = 'red', 'MEOP' ='black')) +
coord_quickmap(xlim = xlim, ylim = ylim) +
theme_bw()
```
We will cover more on the use of the griddap protocol in the next session
Bonus: change the species to 'Crabeater seal' and re-run the notebook
References
MEOP data from https://meop.net
Argo data from ifremer https://erddap.ifremer.fr/erddap/tabledap/ArgoFloats.html
SST reanalysis from coastwatch https://coastwatch.pfeg.noaa.gov/erddap
More info on using ERDDAP's inbuilt search https://ioos.github.io/erddapy/01b-tabledap-output.html