Load the libraries

library(sp)
library(raster) 
## Warning: package 'raster' was built under R version 3.5.2
library(pracma)
library(maps)
library(RColorBrewer)
library(leaflet)

Empty the enviornment and set the working directory and path

graphics.off()
setwd("~/Dropbox/GY667_Assignment")
path <- file.path(getwd(),"Processed Monthly Tide Gauge Data-20190313")

Loading in each of the monthly data files into R

ark.ntgn <- read.csv(file.path(path,"arklow_monthly.csv"))
ballyc.ntgn <- read.csv(file.path(path,"ballycotton_monthly.csv"))
ballyg.ntgn <- read.csv(file.path(path,"ballyglass_monthly.csv"))
castletown.ntgn <- read.csv(file.path(path,"castletownbere_monthly.csv"))
dub.ntgn <- read.csv(file.path(path,"dublin_monthly.csv"))
dunmore.ntgn <- read.csv(file.path(path,"dunmore_east_monthly.csv"))
fenit.ntgn <- read.csv(file.path(path,"fenit_monthly.csv"))
gal.ntgn <- read.csv(file.path(path,"galway_monthly.csv"))
howth.ntgn <- read.csv(file.path(path,"howth_monthly.csv"))
malin.ntgn <- read.csv(file.path(path,"malin_monthly.csv"))
porto.ntgn <- read.csv(file.path(path,"port_oriel_monthly.csv"))
ringask.ntgn <- read.csv(file.path(path,"ringaskiddy_monthly.csv"))
rossav.ntgn <- read.csv(file.path(path,"rossaveel_monthly.csv"))

Changing the column names so that all of the stations have the same name

(howth had a different name for one column than the rest)

colnames(howth.ntgn)<-c("month", "year", "h", "time")

Inputting the coordinates for each station for the plotting of the map

ark.lon             =  -6.145231
ark.lat             =  52.792046
ballyc.lon          =  -8.0007
ballyc.lat          =  51.8278
ballyg.lon          =  -9.8928
ballyg.lat          =  54.2536
castletown.lon      =  -9.9034
castletown.lat      =  51.6496
dub.lon             =  -6.2217
dub.lat             =  53.3457
dunmore.lon         =  -6.99188
dunmore.lat         =  52.14767
fenit.lon           =  -9.8635645
fenit.lat           =  52.270702
gal.lon             =  -9.048
gal.lat             =  53.269
howth.lon           =  -6.0683
howth.lat           =  53.3915
malin.lon           =  -7.3344
malin.lat           =  55.3717
porto.lon           =  -6.221441
porto.lat           =  53.798042
ringask.lon         =  -8.305566
ringask.lat         =  51.83496
rossav.lon          =  -9.562056
rossav.lat          =  53.266926

Plotting each station on a map of Ireland

map("world",c("ireland","uk"),fill=FALSE,xlim=c(-12,-4),ylim=c(51,56))
map.axes(cex.axis=1)
title(main="Location of National Tide Gauge Network Gauges",xlab="Longitude",ylab="Latitude")
points(ark.lon,ark.lat,pch=21,col="red",bg="red")
points(ballyc.lon,ballyc.lat,pch=21,col="red",bg="red")
points(ballyg.lon,ballyg.lat,pch=21,col="red",bg="red")
points(castletown.lon,castletown.lat,pch=21,col="red",bg="red")
points(dub.lon,dub.lat,pch=21,col="red",bg="red")
points(dunmore.lon,dunmore.lat,pch=21,col="red",bg="red")
points(fenit.lon,fenit.lat,pch=21,col="red",bg="red")
points(gal.lon,gal.lat,pch=21,col="red",bg="red")
points(howth.lon,howth.lat,pch=21,col="red",bg="red")
points(malin.lon,malin.lat,pch=21,col="red",bg="red")
points(porto.lon,porto.lat,pch=21,col="red",bg="red")
points(ringask.lon,ringask.lat,pch=21,col="red",bg="red")
points(rossav.lon,rossav.lat,pch=21,col="red",bg="red")
text(ark.lon-.5,ark.lat,"Ark",col="black")
text(ballyc.lon-1.9,ballyc.lat,"Ballyc.",col="black")
text(ballyg.lon-.5,ballyg.lat,"Ballyg.",col="black")
text(castletown.lon-.5,castletown.lat,"Castletown.",col="black")
text(dub.lon-1.1,dub.lat,"Dub.",col="black")
text(dunmore.lon-.5,dunmore.lat,"Dunmore.",col="black")
text(fenit.lon-.5,fenit.lat,"Fenit.",col="black")
text(gal.lon,gal.lat,"Galway.",col="black")
text(howth.lon,howth.lat,"Howth.",col="black")
text(malin.lon-.5,malin.lat,"Malin.",col="black")
text(porto.lon-.5,porto.lat,"Port.O.",col="black")
text(ringask.lon,ringask.lat,"Ringask.",col="black")
text(rossav.lon-1.1,rossav.lat,"Rossav.",col="black")

Changing the format of the dates for R so that it can read it correctly fdor graphing the data

dub.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", dub.ntgn$year, dub.ntgn$month),tz="UTC")
ark.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", ark.ntgn$year, ark.ntgn$month),tz="UTC")
howth.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", howth.ntgn$year, howth.ntgn$month),tz="UTC")
gal.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", gal.ntgn$year, gal.ntgn$month),tz="UTC")
dunmore.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", dunmore.ntgn$year, dunmore.ntgn$month),tz="UTC")
fenit.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", fenit.ntgn$year, fenit.ntgn$month),tz="UTC")
castletown.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", castletown.ntgn$year, castletown.ntgn$month),tz="UTC")
malin.ntgn$time <- as.POSIXlt(sprintf("%s/%s/15", malin.ntgn$year, malin.ntgn$month),tz="UTC")

Inputting a select amount of stations from aroud the country to have an even distribution of gauges around the island of Ireland

Offsets have been included for visual aid in the graph, they are arbitrary in nature so that stations do not overlap and trends are easily noticed when looking at the graph.

plot(dub.ntgn$time, dub.ntgn$h + 0.3, type= 'l',main = "NTGN Tide Gauges",xlab= "Year",ylab= "Sea Level",ylim= c(0,4))
lines(ark.ntgn$time, ark.ntgn$h + 0.7,col='red')
lines(castletown.ntgn$time, castletown.ntgn$h + 1.3,col='yellow')
lines(dunmore.ntgn$time, dunmore.ntgn$h + 1.8,col='pink')
lines(fenit.ntgn$time, fenit.ntgn$h + 2.4,col='orange')
lines(gal.ntgn$time, gal.ntgn$h + 3,col='darkgreen')
lines(howth.ntgn$time, howth.ntgn$h + 3.5,col='darkorchid4')
lines(malin.ntgn$time, malin.ntgn$h + 3.8,col='darkgoldenrod')
legend("left","(x,y)", pch = 1,
       legend=c("Dublin", "Arklow", "Castletownbere", "Dunmore East","Fenit", "Galway", "Howth", "Malin Head"),
       col= c("black", "red", "yellow", "pink", "orange", "darkgreen","darkorchid4","darkgoldenrod"), lty=1, cex=0.5, lwd=1, bty="n")

Merging the data

Inputting the PSMSL data into R

Once in R, the data is changed from the RLR format that is used for the PSMSL gauges, back to its regular format by taking away 7007mm and multiplying it by 10e-4. The dates are then corrected so that it can be recognised by R and graphed for visualisation.

dub.psmsl <- read.csv(file.path(path,"dub.psmsl.csv"))
dub.psmsl <- cbind(dub.psmsl$year, dub.psmsl$month, (dub.psmsl$rlr -7007)*10e-4)
write.csv(dub.psmsl, "dub.psmsl.fix.csv")
dub.psmsl.fix <- read.csv(file.path(path,"dub.psmsl.fix.csv"))
dub.psmsl.fix <- cbind(dub.psmsl.fix, NA)
colnames(dub.psmsl.fix)<-c("year", "month", "h", "time")

The line of code: “dub.psmsl.fix\(time <- as.POSIXlt(sprintf("%s/%s/15", dub.psmsl.fix\)year, dub.psmsl.fix$month),tz=”UTC“)” will not allow me to knit to RMarkdown file together, so I have left it out of the file, unfortately this means the NTGN data will not plot on the merged Dublin graph. However, leaving it in would make the submission a lot more difficult, thus why I have left it out.

The data is then merged, put into the correct date format so R can read it, so that it can be plotted with each of the individual data sets.

dublin.merge <- read.csv(file.path(path,"dublin.merge.csv"))
dublin.merge$time <- as.POSIXlt(sprintf("%s/%s/15", dublin.merge$year, dublin.merge$month),tz="UTC")

Plotting the Dublin data

plot(dub.ntgn$time, dub.ntgn$h +0.15, type= 'l',main = "NTGN Tide Gauges",xlab= "Year",ylab= "Sea Level")
lines(dub.psmsl.fix$time, dub.psmsl.fix$h,col='green')
lines(dublin.merge$time, dublin.merge$h + 0.05,col='red')
legend("topleft","(x,y)", pch = 1,
     legend=c("Dublin NTGN", "Dublin PSMSL", "Dublin Merge"),
       col= c("black", "green", "red"), lty=1, cex=0.5, lwd=1, bty="n")

Merging and Plotting the Malin Head data

The Malin Head data is imported as a file from PSMSL, as is the Portrush data. The Malin and Portrush data is merged to see the similarities between the two datasets, to check for a possibility of Portrush bridging the gap between the NTGN data and the PSMSL data. The PSMSL data is then changed to be able to merge with the NTGN data, to create one data set in total for Malin Head, being compiled of Malin HEad (PSMSL and NTGN) and Portrush (PSMSL).

malin.psmsl <- read.table(file.path(path,"malin.psmsl.txt"),sep=";")
colnames(malin.psmsl) <- c("year", "rlr", "flag1","flag2")
malin.psmsl$rlr[malin.psmsl$rlr == -99999] <- NA

portrush.psmsl <- read.table(file.path(path,"portrush.psmsl.txt"),sep=";")
colnames(portrush.psmsl) <- c("year", "rlr", "flag1","flag2")
portrush.psmsl$rlr[portrush.psmsl$rlr == -99999] <- NA

malin.merge <- merge(malin.psmsl,portrush.psmsl,by="year")

plot(malin.psmsl$year,malin.psmsl$rlr,type='l',col='black',xlim=c(1958,2020),
     xlab = 'Year',ylab='RLR [mm]')
lines(portrush.psmsl$year,portrush.psmsl$rlr,col='red')
lines(malin.merge$year,malin.merge$rlr.x,type='l',col='black',lwd=2)
lines(malin.merge$year,malin.merge$rlr.y,col='red',lwd=2,lty=2)


malin.ntgn$year <- malin.ntgn$year + round((malin.ntgn$month-.5)/12,digits = 3) 
malin.ntgn$h <- malin.ntgn$h * 1000 # convert to mm

lines(malin.ntgn$year,malin.ntgn$h + 7100,col='blue')

malin.recent.merge <- merge(malin.ntgn,portrush.psmsl,by="year")

plot(malin.merge$year,malin.merge$rlr.x-malin.merge$rlr.y,type='l',xlim=c(1990,2020))
lines(malin.recent.merge$year,malin.recent.merge$h - malin.recent.merge$rlr + 7100)

offset_9698 <- median(malin.merge$rlr.x[malin.merge$year<1998]-malin.merge$rlr.y[malin.merge$year<1998])
offset_1015 <- median(malin.recent.merge$rlr-malin.recent.merge$h,na.rm=TRUE)


malin.year <- c(malin.psmsl$year[malin.psmsl$year<1998],
                portrush.psmsl$year[portrush.psmsl$year>=1998& portrush.psmsl$year<2009],
                malin.ntgn$year[malin.ntgn$year>=2009])
malin.rlr <- c(malin.psmsl$rlr[malin.psmsl$year<1998],
               portrush.psmsl$rlr[portrush.psmsl$year>=1998 & portrush.psmsl$year<2009]+offset_9698,
               malin.ntgn$h[malin.ntgn$year>=2009] + offset_9698 + offset_1015)
malin.rlr <- malin.rlr - mean(malin.rlr,na.rm = TRUE) 

plot(malin.year,malin.rlr,type='l',
     ylab = 'Malin [mm]', xlab = 'Year')

mod.rise <- lm(malin.rlr~malin.year)
mod.rise
## 
## Call:
## lm(formula = malin.rlr ~ malin.year)
## 
## Coefficients:
## (Intercept)   malin.year  
##   -2392.590        1.203
abline(mod.rise) # add a slope
text(1970,-300,'SLR = 1.2 mm/year')

The outputs are put into an excel sheet with latitude and longitudes and then laoded back into R.

TrendPlot <- read.table(file.path(path,"TrendPlot.txt"),sep = ";", header = TRUE)