-
Notifications
You must be signed in to change notification settings - Fork 0
/
getCHL.R
106 lines (85 loc) · 3.19 KB
/
getCHL.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
### Get CHL timeseries from satellite products using erddap
### the source of data is `nesdisVHNSQchlaMonthly`. See https://coastwatch.pfeg.noaa.gov/erddap/griddap/nesdisVHNSQchlaMonthly.html
### data is extracted with `rerddap::griddap` for a particular coordinate and stored as csv file.
### E Klein. [email protected] modified by E. Montes ([email protected])
### 2019-04-10
library(readr)
library(rerddap)
library(lubridate)
library(dplyr)
library(flexdashboard)
library(reshape2)
library(leaflet)
library(ggplot2)
library(vegan)
library(xts)
library(dygraphs)
library(plotly)
library(RColorBrewer)
palette(brewer.pal(8, "Set2"))
## functions
## remove all spaces from string
NoSpaces = function(x){
return(gsub(" ", "", x))
}
## set site coordinates and time for CHL extraction
CHLSiteName = "GPatagonia" ## for the resulting file name
CHLcoords.lon = -64
CHLcoords.lat = -41.7
CHLstartDate = "2012-01-01"
## set climatological date start-end
CHLclimStartDate = "2012-01-01"
CHLclimEndDate = "2016-12-31"
## set dataset source
CHLsource = info("erdMH1chla8day")
##
## Get CHL
CHL <- griddap(CHLsource,
time=c(CHLstartDate, "last"),
longitude = c(CHLcoords.lon,CHLcoords.lon),
latitude = c(CHLcoords.lat,CHLcoords.lat),
fields = "chlorophyll", fmt = "csv")
CHL = CHL[,c(1,4)]
names(CHL) = c("time", "CHL")
CHL = na.omit(CHL)
## convert time to a Data object
CHL$time = as.Date(ymd_hms(CHL$time))
##
## Calculate climatology
CHL.clim = CHL %>% filter(time>=ymd(CHLclimStartDate), time<=CHLclimEndDate) %>%
group_by(yDay = yday(time)) %>%
summarise(CHL.mean = mean(CHL),
CHL.median = median(CHL),
CHL.sd = sd(CHL),
CHL.q5 = quantile(CHL, 0.05),
CHL.q10 = quantile(CHL, 0.10),
CHL.q25 = quantile(CHL, 0.25),
CHL.q75 = quantile(CHL, 0.75),
CHL.q90 = quantile(CHL, 0.90),
CHL.q95 = quantile(CHL, 0.95),
CHL.min = min(CHL),
CHL.max = max(CHL))
## Plot CHL
CHL.xts = as.xts(CHL$CHL, CHL$time)
dygraph(CHL.xts,
ylab = "Chlorophyll a (mg m-3) @ lon: -64 and lat: 41.7") %>%
dySeries("V1", label ="CHL", color = "steelblue") %>%
dyHighlight(highlightCircleSize = 5,
highlightSeriesBackgroundAlpha = 0.2,
hideOnMouseOut = FALSE) %>%
dyOptions(fillGraph = FALSE, fillAlpha = 0.4) %>%
dyRangeSelector(dateWindow = c(max(CHL$time) - years(5), max(CHL$time)))
### CHL Last year with smoothed Climatology {data-width=250}
## subset CHL for last year
CHL.lastyear = CHL %>% filter(year(time)==max(year(time)))
## make the plot
pp = ggplot(CHL.clim, aes(yDay, CHL.mean))
pp = pp + geom_line() + geom_smooth(span=0.25, se=FALSE, colour="steelblue") +
geom_ribbon(aes(ymin=CHL.q25, ymax=CHL.q75), fill="steelblue", alpha=0.5) +
geom_line(data=CHL.lastyear, aes(yday(time), CHL), colour="red") +
ylab("Chlorophyll a (mg m-3) @ lon = -64 and lat = 41.7") + xlab("Day of the Year") +
theme_bw(base_size = 9)
ggplotly(pp) %>% plotly::config(displayModeBar = F)
## save CHL
write_csv(CHL, path = paste0(NoSpaces(CHLSiteName), "_CHL.csv"))
write_csv(CHL.clim, path = paste0(NoSpaces(CHLSiteName), "_Climatology.csv"))