Loading Libraries and Data

Libraries

library(VulnToolkit)
library(maps)
library(ncdf4);
library(ncdf4.helpers);
library(maps);
library(RColorBrewer)
library(lattice)
library(latticeExtra)
graphics.off()
rm(list=ls())

Set Working Directory

setwd("/Volumes/Hard Drive/GY667_Workshop2")

Loading in Data

howth = read.csv(file.path(getwd(),'howth_harbour.csv'),skip = 1,header=TRUE)

Exploring Data

dim(howth)
## [1] 1013500       6
head(howth)
##   degrees_east degrees_north                  UTC             X    m X.1
## 1      -6.0683       53.3915 2006-10-24T12:00:00Z Howth Harbour 1.88   1
## 2      -6.0683       53.3915 2006-10-24T12:06:00Z Howth Harbour 1.84   1
## 3      -6.0683       53.3915 2006-10-24T12:12:00Z Howth Harbour 1.84   1
## 4      -6.0683       53.3915 2006-10-24T12:18:00Z Howth Harbour 1.83   1
## 5      -6.0683       53.3915 2006-10-24T12:24:00Z Howth Harbour 1.84   1
## 6      -6.0683       53.3915 2006-10-24T12:30:00Z Howth Harbour 1.81   1

Renaming Columns

names(howth)<-c("lon","lat","time","name","sl","flag")

Removing Unwanted Variables

Taking out longitude, latitude and name as they are not needed for reading the data.

howth.lat <- howth$lat[1]
howth.lon <- howth$lon[1]

Task 2

Mapping the Loaction

Showing the location of the tide gvauge on the map of Ireland.

map("world",c("ireland","uk"),fill=TRUE,xlim=c(-12,-4),ylim=c(51,56))
map.axes(cex.axis=1)
title(main="Location of Howth Harbour Tide Gauge",xlab="Longitude",ylab="Latitude")
points(howth.lon,howth.lat,pch=19,col="red",bg="red")
text(howth.lon-.5,howth.lat,"Howth",col="gray")

Task 3

Convert your data to POSIXlt date format

Changing the format of the dates in the data to allow it to be read properly in Rstudio.

howthtime <- as.POSIXlt(howth$time,format="%Y-%m-%dT%H:%M:%SZ",tz='UTC')
head(howthtime)
## [1] "2006-10-24 12:00:00 UTC" "2006-10-24 12:06:00 UTC"
## [3] "2006-10-24 12:12:00 UTC" "2006-10-24 12:18:00 UTC"
## [5] "2006-10-24 12:24:00 UTC" "2006-10-24 12:30:00 UTC"

Creating New Data Frame

With the new time data, creating a new data frame to work with, merging the new time format and the water level value from the original data set.

howth.data <- data.frame("time"=howthtime,
               "year"=howthtime$year+1900,
               "month"=howthtime$mon+1, 
               "day"=howthtime$mday,
               "hour"=howthtime$hour+1,
               "min"=howthtime$min,
               "sec"=howthtime$sec,
               "sl"=howth$sl,
               "flag"=howth$flag)

Making an Hourly Data Frame

Rounding Hours

Cutting and rounding hours spo they start at the beginning of an hour, and end at the end of the hour.

head(howth.data)
##                  time year month day hour min sec   sl flag
## 1 2006-10-24 12:00:00 2006    10  24   13   0   0 1.88    1
## 2 2006-10-24 12:06:00 2006    10  24   13   6   0 1.84    1
## 3 2006-10-24 12:12:00 2006    10  24   13  12   0 1.84    1
## 4 2006-10-24 12:18:00 2006    10  24   13  18   0 1.83    1
## 5 2006-10-24 12:24:00 2006    10  24   13  24   0 1.84    1
## 6 2006-10-24 12:30:00 2006    10  24   13  30   0 1.81    1
 tail(howth.data,n=11)
##                        time year month day hour min sec    sl flag
## 1013490 2019-02-20 14:55:00 2019     2  20   15  55   0 -0.18    0
## 1013491 2019-02-20 15:00:00 2019     2  20   16   0   0 -0.28    0
## 1013492 2019-02-20 15:05:00 2019     2  20   16   5   0 -0.38    0
## 1013493 2019-02-20 15:10:00 2019     2  20   16  10   0 -0.48    0
## 1013494 2019-02-20 15:15:00 2019     2  20   16  15   0 -0.58    0
## 1013495 2019-02-20 15:20:00 2019     2  20   16  20   0 -0.67    0
## 1013496 2019-02-20 15:25:00 2019     2  20   16  25   0 -0.80    0
## 1013497 2019-02-20 15:30:00 2019     2  20   16  30   0 -0.88    0
## 1013498 2019-02-20 15:35:00 2019     2  20   16  35   0 -0.96    0
## 1013499 2019-02-20 15:45:00 2019     2  20   16  45   0 -1.18    0
## 1013500 2019-02-20 15:50:00 2019     2  20   16  50   0 -1.25    0

Reducing the Hours

howth.sub <- howth.data[1:1013491,]
howth.hour <- aggregate(sl~hour+day+month+year,howth.sub,mean)
howth.hour <- cbind(howth.hour, NA)
names(howth.hour) <- c("hour","day","month", "year", "sl", "time")
howth.hour$time <- as.POSIXlt(sprintf("%s/%s/%s %s", 
                                    howth.hour$year, howth.hour$month, 
                                    howth.hour$day, howth.hour$hour),
                            format="%Y/%m/%d %H",tz='UTC')

Task 4

Creating Monthly Averages

As done with the hours, cutting the months so that the data begins at the start of the month and end at the end of the month.

howth.sub2 <- howth.hour[82:100548,]
howth.mon <- aggregate(sl~month+year,howth.sub2,mean)
howth.mon <- cbind(howth.mon, NA)
names(howth.mon) <- c("month", "year", "sl", "time")
howth.mon$time <- as.POSIXlt(sprintf("%s/%s/15", howth.mon$year, howth.mon$month),tz="UTC")