---
title: "Sea level changes"
author: "Duncan Golicher"
date: "12/11/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE,warning = FALSE)
```

```{r}
library(tidyverse)
library(lubridate)
library(xts)
library(dygraphs)
library(mgcv)

pago_pago<-read.csv("http://uhslc.soest.hawaii.edu/data/csv/rqds/pacific/daily/d056a.csv", head=FALSE)
sf<-read.csv("http://uhslc.soest.hawaii.edu/data/csv/rqds/pacific/daily/d551a.csv", head=FALSE)
brest<-read.csv("http://uhslc.soest.hawaii.edu/data/csv/rqds/atlantic/daily/d822a.csv", head=FALSE)
guam<-read.csv("http://uhslc.soest.hawaii.edu/data/csv/rqds/pacific/daily/d053a.csv", head=FALSE)
honolulu<-read.csv("http://uhslc.soest.hawaii.edu/data/csv/rqds/pacific/daily/d057b.csv", head=FALSE)
gan<-read.csv("http://uhslc.soest.hawaii.edu/data/csv/rqds/indian/daily/d109a.csv", head=FALSE)



```

# Sea levels{.tabset}


Data from http://uhslc.soest.hawaii.edu/data and https://www.psmsl.org/data/obtaining/map.html#plotTab

## American Samoa

```{r}

d<-pago_pago

d$date<-dmy(paste(d$V3, d$V2,d$V1))
d$value<-d$V4
d<-filter(d,value>0)
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(6,9)],d$date), main="Pago Pago American Samoa", ylab="Sea level mm", xlab="Date") %>% dyRoller(rollPeriod = 365) %>% dyRangeSelector(dateWindow = c(dmy("1/1/1950"), dmy("31/12/2020")))
dy 

```

## Guam


```{r}

d<-guam

d$date<-dmy(paste(d$V3, d$V2,d$V1))
d$value<-d$V4
d<-filter(d,value>0)
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(6,9)],d$date), main="Guam", ylab="Sea level mm", xlab="Date") %>% dyRoller(rollPeriod = 365) %>% dyRangeSelector(dateWindow = c(dmy("1/1/1950"), dmy("31/12/2020")))
dy 

```


## Honolulu


```{r}

d<-honolulu

d$date<-dmy(paste(d$V3, d$V2,d$V1))
d$value<-d$V4
d<-filter(d,value>0)
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(6,9)],d$date), main="Honolulu", ylab="Sea level mm", xlab="Date") %>% dyRoller(rollPeriod = 365) %>% dyRangeSelector(dateWindow = c(dmy("1/1/1950"), dmy("31/12/2020")))
dy 

```


## Gan Maldives


```{r}

d<-gan

d$date<-dmy(paste(d$V3, d$V2,d$V1))
d$value<-d$V4
d<-filter(d,value>0)
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(6,9)],d$date), main="Honolulu", ylab="Sea level mm", xlab="Date") %>% dyRoller(rollPeriod = 365) %>% dyRangeSelector(dateWindow = c(dmy("1/1/1991"), dmy("31/12/2020")))
dy 

```






## San Francisco

```{r}

d<-sf

d$date<-dmy(paste(d$V3, d$V2,d$V1))
d$value<-d$V4
d<-filter(d,value>0)
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(6,9)],d$date), main="San Francisco", ylab="Sea level mm", xlab="Date") %>% dyRoller(rollPeriod = 365) %>% dyRangeSelector(dateWindow = c(dmy("1/1/1950"), dmy("31/12/2020")))
dy 

```

## Brest, France

```{r}

d<-brest

d$date<-dmy(paste(d$V3, d$V2,d$V1))
d$value<-d$V4
d<-filter(d,value>0)
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(6,9)],d$date), main="Brest", ylab="Sea level mm", xlab="Date") %>% dyRoller(rollPeriod = 365) %>% dyRangeSelector(dateWindow = c(dmy("1/1/1950"), dmy("31/12/2020")))
dy 

```
## Liverpool yearly


```{r}
d<-read.table("https://www.psmsl.org/data/longrecords/lpool.annual.mhw.fig1.grl")
names(d)<-c("Year","Value")
d$Value<-d$Value*10
d$trend<-predict(gam(data=d,Value~s(Year)))
dy<-dygraph(d, main="Liverpool", ylab="Sea level mm", xlab="Year") %>% dyRoller(rollPeriod = 1) 
dy
```


## Dan Helder Netherlands

```{r}
d<-read.csv("https://www.psmsl.org/data/obtaining/rlr.monthly.data/23.rlrdata", sep=";", head=FALSE)
d %>% separate(V1, into = c("Year", "Month")) ->d
d$Month<-round((as.numeric(paste("0.",d$Month, sep=""))*12 )+0.5)
d$date<-dmy(paste(1, d$Month,d$Year))
d<-data.frame(date=d$date, value=d$V2)
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(2,5)], d$date), main="Dan Helder, Netherlands", ylab="Sea level mm", xlab="Year") %>% dyRoller(rollPeriod = 12) 
dy %>% dyRangeSelector()
```


## Rejykavik


```{r}
d<-read.csv("https://www.psmsl.org/data/obtaining/rlr.monthly.data/638.rlrdata", sep=";", head=FALSE)
d %>% separate(V1, into = c("Year", "Month")) ->d
d$Month<-round((as.numeric(paste("0.",d$Month, sep=""))*12 )+0.5)
d$date<-dmy(paste(1, d$Month,d$Year))
d<-data.frame(date=d$date, value=d$V2)
d %>% filter(value>0)->d
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(2,5)], d$date), main="Rejkavik", ylab="Sea level mm", xlab="Year") %>% dyRoller(rollPeriod = 12) 
dy %>% dyRangeSelector()
```

## Kwajaleen, Marshall Islands

```{r}

d<-read.csv("https://www.psmsl.org/data/obtaining/rlr.monthly.data/513.rlrdata", sep=";", head=FALSE)
d %>% separate(V1, into = c("Year", "Month")) ->d
d$Month<-round((as.numeric(paste("0.",d$Month, sep=""))*12 )+0.5)
d$date<-dmy(paste(1, d$Month,d$Year))
d<-data.frame(date=d$date, value=d$V2)
d %>% filter(value>0)->d
d$year<-year(d$date)
d %>% group_by(year) %>% mutate(m=mean(value)) %>% ungroup() ->d
d$trend<-predict(gam(data=d,m~s(year)))
dy<-dygraph(xts(d[,c(2,5)], d$date), main="Marshall Islands", ylab="Sea level mm", xlab="Year") %>% dyRoller(rollPeriod = 12) 
dy %>% dyRangeSelector()

```



