Workshop 2: Tide Gauge Data

install.packages(“devtools”) # if devtools isn’t already installed

devtools::install_github(“troyhill/VulnToolkit”)

library(VulnToolkit)
library(maps)

Set your working directory

setwd("F:\\MSc\\GY667 Ocean\\Workshop 2")

Task 1: load your data.

Ringaskiddy tide gauge is a .txt file read.table for .txt file OPW data is tab delimited

ringaskiddy <- read.table(file.path(getwd(),"ringaskiddy.txt"),fill=TRUE,header=TRUE,sep="\t",skip=6)

Have to use fill = TRUE because R expects each row to have the same number of elements It doesn’t fill the blanks automatically so you have to specify fill

dim(ringaskiddy)
## [1] 244730      3
head(ringaskiddy)
##                  Date  Value Quality
## 1 2011/12/31 00:15:00 -0.535       *
## 2 2011/12/31 00:30:00 -0.715       *
## 3 2011/12/31 00:45:00 -0.905       *
## 4 2011/12/31 01:00:00 -1.025       *
## 5 2011/12/31 01:15:00 -1.155       *
## 6 2011/12/31 01:30:00 -1.265       *

The dataframe has 244730 rows and 3 columns (date, value, and quality)

Rename columns for clarity

names(ringaskiddy)<-c("time","h","flag")

Task 2: Plot the location of your tide gauge

The longitude and latitude of this tide are not included in the file The coordinates of this tide gauge are available from https://erddap.marine.ie/erddap/tabledap/IrishNationalTideGaugeNetwork Longitude = -8.305566 Latitude = 51.83496

ring.lat <- -8.305566
ring.lon <- 51.83496

Plot the map.

map("world",c("ireland","uk"),fill=TRUE,xlim=c(-12,-4),ylim=c(51,56))
map.axes(cex.axis=1)
title(main="Location of Ringaskiddy Tide Gauge",xlab="Longitude",ylab="Latitude")
points(ring.lat,ring.lon,pch=21,col="gray",bg="red",cex=1.1)
text(ring.lat,ring.lon-0.5,"Ring.",col="gray")

Task 3: Convert your data to POSIXlt date format.

ringaskiddy$time[1:4]
## [1] 2011/12/31 00:15:00 2011/12/31 00:30:00 2011/12/31 00:45:00
## [4] 2011/12/31 01:00:00
## 244730 Levels: 2011/12/31 00:15:00 2011/12/31 00:30:00 ... 2019/02/01 00:00:00
ring.time <- as.POSIXlt(ringaskiddy$time,format="%Y/%m/%d %H:%M:%S",tz='UTC')

Make a new data frame with variables year, month, day, hour, minute, second, sea level (h), and flag

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

Reduce the data

Make an hourly data frame.

Use head() and tail() to determine to check the start and the end of the data and ensure it begins and ends on a full hour.

head(ring) # need to cut out first 3 rows
##                  time year month day hour min sec      h flag
## 1 2011-12-31 00:15:00 2011    12  31    0  15   0 -0.535    *
## 2 2011-12-31 00:30:00 2011    12  31    0  30   0 -0.715    *
## 3 2011-12-31 00:45:00 2011    12  31    0  45   0 -0.905    *
## 4 2011-12-31 01:00:00 2011    12  31    1   0   0 -1.025    *
## 5 2011-12-31 01:15:00 2011    12  31    1  15   0 -1.155    *
## 6 2011-12-31 01:30:00 2011    12  31    1  30   0 -1.265    *
tail(ring) 
##                       time year month day hour min sec      h flag
## 244725 2019-01-31 22:45:00 2019     1  31   22  45   0 -0.617    *
## 244726 2019-01-31 23:00:00 2019     1  31   23   0   0 -0.484    *
## 244727 2019-01-31 23:15:00 2019     1  31   23  15   0 -0.307    *
## 244728 2019-01-31 23:30:00 2019     1  31   23  30   0 -0.124    *
## 244729 2019-01-31 23:45:00 2019     1  31   23  45   0 -0.124    *
## 244730 2019-02-01 00:00:00 2019     2   1    0   0   0  0.242    *
ring.sub <- ring[4:244729, ]

Use aggregate to create an hourly average.

ring.hour <- aggregate(h~hour+day+month+year,ring.sub,mean)

Create a new empty column for a decimal year.

ring.hour <- cbind(ring.hour, NA)
names(ring.hour) <- c("hour","day","month", "year", "h", "time")

Add a decimal year variable.

ring.hour$time <- as.POSIXlt(sprintf("%s/%s/%s %s", 
                                    ring.hour$year, ring.hour$month, 
                                    ring.hour$day, ring.hour$hour),
                            format="%Y/%m/%d %H",tz='UTC')

summary(ring.hour)
##       hour           day           month             year     
##  Min.   : 0.0   Min.   : 1.0   Min.   : 1.000   Min.   :2011  
##  1st Qu.: 6.0   1st Qu.: 8.0   1st Qu.: 3.000   1st Qu.:2013  
##  Median :12.0   Median :16.0   Median : 6.000   Median :2015  
##  Mean   :11.5   Mean   :15.8   Mean   : 6.465   Mean   :2015  
##  3rd Qu.:18.0   3rd Qu.:23.0   3rd Qu.:10.000   3rd Qu.:2017  
##  Max.   :23.0   Max.   :31.0   Max.   :12.000   Max.   :2019  
##        h                time                    
##  Min.   :-3.0750   Min.   :2011-12-31 01:00:00  
##  1st Qu.:-1.3805   1st Qu.:2013-10-26 10:45:00  
##  Median :-0.4075   Median :2015-08-01 17:30:00  
##  Mean   :-0.3767   Mean   :2015-07-29 18:42:40  
##  3rd Qu.: 0.6275   3rd Qu.:2017-05-04 14:15:00  
##  Max.   : 2.3425   Max.   :2019-01-31 23:00:00
head(ring.hour)
##   hour day month year       h                time
## 1    1  31    12 2011 -1.2050 2011-12-31 01:00:00
## 2    2  31    12 2011 -1.6150 2011-12-31 02:00:00
## 3    3  31    12 2011 -1.8300 2011-12-31 03:00:00
## 4    4  31    12 2011 -1.7900 2011-12-31 04:00:00
## 5    5  31    12 2011 -1.3600 2011-12-31 05:00:00
## 6    6  31    12 2011 -0.6025 2011-12-31 06:00:00
tail(ring.hour)
##       hour day month year        h                time
## 61183   18  31     1 2019 -0.42450 2019-01-31 18:00:00
## 61184   19  31     1 2019 -0.86225 2019-01-31 19:00:00
## 61185   20  31     1 2019 -1.12375 2019-01-31 20:00:00
## 61186   21  31     1 2019 -1.12725 2019-01-31 21:00:00
## 61187   22  31     1 2019 -0.79450 2019-01-31 22:00:00
## 61188   23  31     1 2019 -0.25975 2019-01-31 23:00:00

summary() gives an overview of the data in each column. head() shows the beginning of the data on 31/12/2012. tail() shows the data end on 31/01/2019

Task 4: create monthly averages.

Find first full month index using which().

min(which(ring.hour$year == 2012 & ring.hour$month == 1)) # 24
## [1] 24

Find last full month index using which().

max(which(ring.hour$year == 2019 & ring.hour$month == 1)) # 61188
## [1] 61188

Make a monthly dataframe.

ring.sub_2 <- ring.hour[24:61188, ]

Use aggregate to create a monthly average.

ring.mon <- aggregate(h~month+year,ring.sub_2,mean)

Create a new empty column for time.

ring.mon <- cbind(ring.mon, NA)
names(ring.mon) <- c("month", "year", "h", "time")

Add a POSIXlt variable back in.

ring.mon$time <- as.POSIXlt(sprintf("%s/%s/15", ring.mon$year, ring.mon$month),tz="UTC")

Task 5: Plot the resultant data, add legends etc.

plot(ring.mon$time, ring.mon$h,col='red',
     main = 'Ringaskiddy Monthly',xlab = 'Years',ylab = 'Sea Level (m)',type = 'b')

This plots the monthly time series for Rigaskiddy. In such a short period of time (12/2012-01/2019), it is difficult to determine if there are any trends in the data. On such a short timescale, the monthly variability is the dominant source of variability.

Going to look at adding average highs and lows for each month

ring.mon <- cbind(ring.mon, NA, NA, NA, NA)
names(ring.mon) <- c("month", "year", "h", "time",
                    "mhw","mlw","mtl","n")

Look at the data in more detail, month-by-month to determine high and low water levels.

for(j in 1:length(ring.mon$month)){
  ind <- which(ring.hour$year == ring.mon$year[j] &
                 ring.hour$month==ring.mon$month[j])
  
  t <- ring.hour$time[ind]
  h <- ring.hour$h[ind]
  # plot(t,h,type='l')
  
  hl<-HL(h,t)
  HL.plot(h,t)
  
  cat("you are on index ", j, " of ", length(ring.mon$month), "\n")
  cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
  cat("you have ", length(hl$level[hl$tide == "H"])," high tides\n")
  cat("you have ", length(hl$level[hl$tide == "L"])," low tides")
  
  # pull out high and low for later
  HH<-hl$level[hl$tide == "H"]
  LL<-hl$level[hl$tide == "L"]
  
  question1 <- readline("Do the numbers of high tides match low (Y/N)")
  if(regexpr(question1, 'y', ignore.case = TRUE) == 1){
    ring.mon$mhw[j] <- mean(HH)
    ring.mon$mlw[j] <- mean(LL)
    ring.mon$mtl[j] <- mean(c(HH,LL))
    ring.mon$n[j] <- length(LL)
    continue = TRUE
    next
  } else if (regexpr(question1, 'n', ignore.case = TRUE) == 1){
    question2 <- readline("Would you like to cut out a high or a low tide? (h/l/o)")
    if(regexpr(question2, 'h', ignore.case = TRUE) == 1){
      ring.mon$mhw[j] <- mean(HH[1:(length(HH)-1)])
      ring.mon$mlw[j] <- mean(LL)
      ring.mon$mtl[j] <- mean(c(HH[1:(length(HH)-1)],LL))
      ring.mon$n[j] <- length(LL)
      cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
      cat("you now have ", length(HH)-1," high tides\n")
      cat("you now have ", length(LL)," low tides\n")
      continue = TRUE
      
    } else if (regexpr(question2, 'l', ignore.case = TRUE) == 1){
      ring.mon$mhw[j] <- mean(HH)
      ring.mon$mlw[j] <- mean(LL[1:(length(LL)-1)])
      ring.mon$mtl[j] <- mean(c(LL[1:(length(LL)-1)],HH))
      ring.mon$n[j] <- length(LL)
      cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
      cat("you now have ", length(HH)," high tides\n")
      cat("you now have ", length(LL)-1," low tides\n")
      continue = TRUE
    }
      question3 <- readline("Do the numbers of high tides match low (Y/N)")
      if(regexpr(question3, 'y', ignore.case = TRUE) == 1){
        ring.mon$mhw[j] <- mean(HH)
        ring.mon$mlw[j] <- mean(LL)
        ring.mon$mtl[j] <- mean(c(HH,LL))
        ring.mon$n[j] <- length(LL)
        continue = TRUE
        next
      }
      else if (regexpr(question3, 'n', ignore.case = TRUE) == 1){
        question4 <- readline("Would you like to cut out a high or a low tide? (h/l/o)")
        if(regexpr(question4, 'h', ignore.case = TRUE) == 1){
          ring.mon$mhw[j] <- mean(HH[1:(length(HH)-1)])
          ring.mon$mlw[j] <- mean(LL)
          ring.mon$mtl[j] <- mean(c(HH[1:(length(HH)-1)],LL))
          ring.mon$n[j] <- length(LL)
          cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
          cat("you now have ", length(HH)-1," high tides\n")
          cat("you now have ", length(LL)," low tides\n")
          continue = TRUE
          
        } else if (regexpr(question4, 'l', ignore.case = TRUE) == 1){
          ring.mon$mhw[j] <- mean(HH)
          ring.mon$mlw[j] <- mean(LL[1:(length(LL)-1)])
          ring.mon$mtl[j] <- mean(c(LL[1:(length(LL)-1)],HH))
          ring.mon$n[j] <- length(LL)
          cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
          cat("you now have ", length(HH)," high tides\n")
          cat("you now have ", length(LL)-1," low tides\n")
          continue = TRUE
        }
      
    }
    else {ring.mon$mhw[j] <- NA
    ring.mon$mlw[j] <- NA
    ring.mon$mtl[j] <- NA
    ring.mon$n[j] <- length(LL)}
    
  }
}

## you are on index  1  of  85 
## in  2012 month =  1 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  2  of  85 
## in  2012 month =  2 
## you have  56  high tides
## you have  56  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  3  of  85 
## in  2012 month =  3 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  4  of  85 
## in  2012 month =  4 
## you have  57  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  5  of  85 
## in  2012 month =  5 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  6  of  85 
## in  2012 month =  6 
## you have  52  high tides
## you have  51  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  7  of  85 
## in  2012 month =  7 
## you have  12  high tides
## you have  13  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  8  of  85 
## in  2012 month =  8 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  9  of  85 
## in  2012 month =  9 
## you have  58  high tides
## you have  57  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  10  of  85 
## in  2012 month =  10 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  11  of  85 
## in  2012 month =  11 
## you have  58  high tides
## you have  57  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  12  of  85 
## in  2012 month =  12 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  13  of  85 
## in  2013 month =  1 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  14  of  85 
## in  2013 month =  2 
## you have  54  high tides
## you have  54  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  15  of  85 
## in  2013 month =  3 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  16  of  85 
## in  2013 month =  4 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  17  of  85 
## in  2013 month =  5 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  18  of  85 
## in  2013 month =  6 
## you have  57  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  19  of  85 
## in  2013 month =  7 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  20  of  85 
## in  2013 month =  8 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  21  of  85 
## in  2013 month =  9 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  22  of  85 
## in  2013 month =  10 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  23  of  85 
## in  2013 month =  11 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  24  of  85 
## in  2013 month =  12 
## you have  61  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  25  of  85 
## in  2014 month =  1 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  26  of  85 
## in  2014 month =  2 
## you have  55  high tides
## you have  53  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  27  of  85 
## in  2014 month =  3 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  28  of  85 
## in  2014 month =  4 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  29  of  85 
## in  2014 month =  5 
## you have  58  high tides
## you have  57  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  30  of  85 
## in  2014 month =  6 
## you have  587  high tides
## you have  586  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  31  of  85 
## in  2014 month =  7 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  32  of  85 
## in  2014 month =  8 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  33  of  85 
## in  2014 month =  9 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  34  of  85 
## in  2014 month =  10 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  35  of  85 
## in  2014 month =  11 
## you have  57  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  36  of  85 
## in  2014 month =  12 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  37  of  85 
## in  2015 month =  1 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  38  of  85 
## in  2015 month =  2 
## you have  54  high tides
## you have  54  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  39  of  85 
## in  2015 month =  3 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  40  of  85 
## in  2015 month =  4 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  41  of  85 
## in  2015 month =  5 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  42  of  85 
## in  2015 month =  6 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  43  of  85 
## in  2015 month =  7 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  44  of  85 
## in  2015 month =  8 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  45  of  85 
## in  2015 month =  9 
## you have  58  high tides
## you have  57  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  46  of  85 
## in  2015 month =  10 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  47  of  85 
## in  2015 month =  11 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  48  of  85 
## in  2015 month =  12 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  49  of  85 
## in  2016 month =  1 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  50  of  85 
## in  2016 month =  2 
## you have  56  high tides
## you have  56  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  51  of  85 
## in  2016 month =  3 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  52  of  85 
## in  2016 month =  4 
## you have  57  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  53  of  85 
## in  2016 month =  5 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  54  of  85 
## in  2016 month =  6 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  55  of  85 
## in  2016 month =  7 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  56  of  85 
## in  2016 month =  8 
## you have  61  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  57  of  85 
## in  2016 month =  9 
## you have  58  high tides
## you have  57  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  58  of  85 
## in  2016 month =  10 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  59  of  85 
## in  2016 month =  11 
## you have  58  high tides
## you have  57  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  60  of  85 
## in  2016 month =  12 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  61  of  85 
## in  2017 month =  1 
## you have  51  high tides
## you have  51  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  62  of  85 
## in  2017 month =  2 
## you have  54  high tides
## you have  54  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  63  of  85 
## in  2017 month =  3 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  64  of  85 
## in  2017 month =  4 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  65  of  85 
## in  2017 month =  5 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  66  of  85 
## in  2017 month =  6 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  67  of  85 
## in  2017 month =  7 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  68  of  85 
## in  2017 month =  8 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  69  of  85 
## in  2017 month =  9 
## you have  57  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  70  of  85 
## in  2017 month =  10 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  71  of  85 
## in  2017 month =  11 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  72  of  85 
## in  2017 month =  12 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  73  of  85 
## in  2018 month =  1 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  74  of  85 
## in  2018 month =  2 
## you have  54  high tides
## you have  54  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  75  of  85 
## in  2018 month =  3 
## you have  60  high tides
## you have  59  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  76  of  85 
## in  2018 month =  4 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  77  of  85 
## in  2018 month =  5 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  78  of  85 
## in  2018 month =  6 
## you have  58  high tides
## you have  57  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  79  of  85 
## in  2018 month =  7 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  80  of  85 
## in  2018 month =  8 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  81  of  85 
## in  2018 month =  9 
## you have  58  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  82  of  85 
## in  2018 month =  10 
## you have  60  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  83  of  85 
## in  2018 month =  11 
## you have  57  high tides
## you have  58  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  84  of  85 
## in  2018 month =  12 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

## you are on index  85  of  85 
## in  2019 month =  1 
## you have  59  high tides
## you have  60  low tidesDo the numbers of high tides match low (Y/N)

Automate the above code

for(j in 1:length(ring.mon$month)){
  ind <- which(ring.hour$year == ring.mon$year[j] &
                 ring.hour$month==ring.mon$month[j])
  
  t <- ring.hour$time[ind]
  h <- ring.hour$h[ind]
  # plot(t,h,type='l')
  
  hl<-HL(h,t)
  HL.plot(h,t)
  
  cat("you are on index ", j, " of ", length(ring.mon$month), "\n")
  cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
  cat("you have ", length(hl$level[hl$tide == "H"])," high tides\n")
  cat("you have ", length(hl$level[hl$tide == "L"])," low tides")
  
  # pull out high and low for later
  HH<-hl$level[hl$tide == "H"]
  LL<-hl$level[hl$tide == "L"]
  
  if(length(HH) == length(LL)){
    ring.mon$mhw[j] <- mean(HH)
    ring.mon$mlw[j] <- mean(LL)
    ring.mon$mtl[j] <- mean(c(HH,LL))
    ring.mon$n[j] <- length(LL)
    continue = TRUE
    next
  } else if (length(HH) != length(LL)){
    if(length(HH) > length(LL)){
      x <- length(HH) - length(LL)
      ring.mon$mhw[j] <- mean(HH[1:(length(HH)-x)])
      ring.mon$mlw[j] <- mean(LL)
      ring.mon$mtl[j] <- mean(c(HH[1:(length(HH)-x)],LL))
      ring.mon$n[j] <- length(LL)
      cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
      cat("you now have ", length(HH)-1," high tides\n")
      cat("you now have ", length(LL)," low tides\n")
      continue = TRUE
      
    } else if (length(HH) < length(LL)){
      y <- length(LL) - length(HH)
      ring.mon$mhw[j] <- mean(HH)
      ring.mon$mlw[j] <- mean(LL[1:(length(LL)-1)])
      ring.mon$mtl[j] <- mean(c(LL[1:(length(LL)-1)],HH))
      ring.mon$n[j] <- length(LL)
      cat("in ", ring.mon$year[j], "month = ", ring.mon$month[j],"\n")
      cat("you now have ", length(HH)," high tides\n")
      cat("you now have ", length(LL)-1," low tides\n")
      continue = TRUE
    }

    else {ring.mon$mhw[j] <- NA
    ring.mon$mlw[j] <- NA
    ring.mon$mtl[j] <- NA
    ring.mon$n[j] <- length(LL)}
    
  }
}

## you are on index  1  of  85 
## in  2012 month =  1 
## you have  60  high tides
## you have  60  low tides

## you are on index  2  of  85 
## in  2012 month =  2 
## you have  56  high tides
## you have  56  low tides

## you are on index  3  of  85 
## in  2012 month =  3 
## you have  59  high tides
## you have  60  low tidesin  2012 month =  3 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  4  of  85 
## in  2012 month =  4 
## you have  57  high tides
## you have  58  low tidesin  2012 month =  4 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  5  of  85 
## in  2012 month =  5 
## you have  60  high tides
## you have  60  low tides

## you are on index  6  of  85 
## in  2012 month =  6 
## you have  52  high tides
## you have  51  low tidesin  2012 month =  6 
## you now have  51  high tides
## you now have  51  low tides

## you are on index  7  of  85 
## in  2012 month =  7 
## you have  12  high tides
## you have  13  low tidesin  2012 month =  7 
## you now have  12  high tides
## you now have  12  low tides

## you are on index  8  of  85 
## in  2012 month =  8 
## you have  60  high tides
## you have  59  low tidesin  2012 month =  8 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  9  of  85 
## in  2012 month =  9 
## you have  58  high tides
## you have  57  low tidesin  2012 month =  9 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  10  of  85 
## in  2012 month =  10 
## you have  60  high tides
## you have  59  low tidesin  2012 month =  10 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  11  of  85 
## in  2012 month =  11 
## you have  58  high tides
## you have  57  low tidesin  2012 month =  11 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  12  of  85 
## in  2012 month =  12 
## you have  60  high tides
## you have  60  low tides

## you are on index  13  of  85 
## in  2013 month =  1 
## you have  60  high tides
## you have  60  low tides

## you are on index  14  of  85 
## in  2013 month =  2 
## you have  54  high tides
## you have  54  low tides

## you are on index  15  of  85 
## in  2013 month =  3 
## you have  60  high tides
## you have  60  low tides

## you are on index  16  of  85 
## in  2013 month =  4 
## you have  58  high tides
## you have  58  low tides

## you are on index  17  of  85 
## in  2013 month =  5 
## you have  60  high tides
## you have  60  low tides

## you are on index  18  of  85 
## in  2013 month =  6 
## you have  57  high tides
## you have  58  low tidesin  2013 month =  6 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  19  of  85 
## in  2013 month =  7 
## you have  59  high tides
## you have  60  low tidesin  2013 month =  7 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  20  of  85 
## in  2013 month =  8 
## you have  60  high tides
## you have  60  low tides

## you are on index  21  of  85 
## in  2013 month =  9 
## you have  58  high tides
## you have  58  low tides

## you are on index  22  of  85 
## in  2013 month =  10 
## you have  60  high tides
## you have  60  low tides

## you are on index  23  of  85 
## in  2013 month =  11 
## you have  58  high tides
## you have  58  low tides

## you are on index  24  of  85 
## in  2013 month =  12 
## you have  61  high tides
## you have  59  low tidesin  2013 month =  12 
## you now have  60  high tides
## you now have  59  low tides

## you are on index  25  of  85 
## in  2014 month =  1 
## you have  60  high tides
## you have  59  low tidesin  2014 month =  1 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  26  of  85 
## in  2014 month =  2 
## you have  55  high tides
## you have  53  low tidesin  2014 month =  2 
## you now have  54  high tides
## you now have  53  low tides

## you are on index  27  of  85 
## in  2014 month =  3 
## you have  60  high tides
## you have  59  low tidesin  2014 month =  3 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  28  of  85 
## in  2014 month =  4 
## you have  58  high tides
## you have  58  low tides

## you are on index  29  of  85 
## in  2014 month =  5 
## you have  58  high tides
## you have  57  low tidesin  2014 month =  5 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  30  of  85 
## in  2014 month =  6 
## you have  587  high tides
## you have  586  low tidesin  2014 month =  6 
## you now have  586  high tides
## you now have  586  low tides

## you are on index  31  of  85 
## in  2014 month =  7 
## you have  60  high tides
## you have  59  low tidesin  2014 month =  7 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  32  of  85 
## in  2014 month =  8 
## you have  60  high tides
## you have  60  low tides

## you are on index  33  of  85 
## in  2014 month =  9 
## you have  58  high tides
## you have  58  low tides

## you are on index  34  of  85 
## in  2014 month =  10 
## you have  59  high tides
## you have  60  low tidesin  2014 month =  10 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  35  of  85 
## in  2014 month =  11 
## you have  57  high tides
## you have  58  low tidesin  2014 month =  11 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  36  of  85 
## in  2014 month =  12 
## you have  59  high tides
## you have  60  low tidesin  2014 month =  12 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  37  of  85 
## in  2015 month =  1 
## you have  60  high tides
## you have  60  low tides

## you are on index  38  of  85 
## in  2015 month =  2 
## you have  54  high tides
## you have  54  low tides

## you are on index  39  of  85 
## in  2015 month =  3 
## you have  60  high tides
## you have  60  low tides

## you are on index  40  of  85 
## in  2015 month =  4 
## you have  58  high tides
## you have  58  low tides

## you are on index  41  of  85 
## in  2015 month =  5 
## you have  60  high tides
## you have  60  low tides

## you are on index  42  of  85 
## in  2015 month =  6 
## you have  58  high tides
## you have  58  low tides

## you are on index  43  of  85 
## in  2015 month =  7 
## you have  60  high tides
## you have  59  low tidesin  2015 month =  7 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  44  of  85 
## in  2015 month =  8 
## you have  60  high tides
## you have  59  low tidesin  2015 month =  8 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  45  of  85 
## in  2015 month =  9 
## you have  58  high tides
## you have  57  low tidesin  2015 month =  9 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  46  of  85 
## in  2015 month =  10 
## you have  60  high tides
## you have  59  low tidesin  2015 month =  10 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  47  of  85 
## in  2015 month =  11 
## you have  58  high tides
## you have  58  low tides

## you are on index  48  of  85 
## in  2015 month =  12 
## you have  60  high tides
## you have  60  low tides

## you are on index  49  of  85 
## in  2016 month =  1 
## you have  60  high tides
## you have  60  low tides

## you are on index  50  of  85 
## in  2016 month =  2 
## you have  56  high tides
## you have  56  low tides

## you are on index  51  of  85 
## in  2016 month =  3 
## you have  60  high tides
## you have  60  low tides

## you are on index  52  of  85 
## in  2016 month =  4 
## you have  57  high tides
## you have  58  low tidesin  2016 month =  4 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  53  of  85 
## in  2016 month =  5 
## you have  59  high tides
## you have  60  low tidesin  2016 month =  5 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  54  of  85 
## in  2016 month =  6 
## you have  58  high tides
## you have  58  low tides

## you are on index  55  of  85 
## in  2016 month =  7 
## you have  60  high tides
## you have  60  low tides

## you are on index  56  of  85 
## in  2016 month =  8 
## you have  61  high tides
## you have  59  low tidesin  2016 month =  8 
## you now have  60  high tides
## you now have  59  low tides

## you are on index  57  of  85 
## in  2016 month =  9 
## you have  58  high tides
## you have  57  low tidesin  2016 month =  9 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  58  of  85 
## in  2016 month =  10 
## you have  60  high tides
## you have  59  low tidesin  2016 month =  10 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  59  of  85 
## in  2016 month =  11 
## you have  58  high tides
## you have  57  low tidesin  2016 month =  11 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  60  of  85 
## in  2016 month =  12 
## you have  60  high tides
## you have  60  low tides

## you are on index  61  of  85 
## in  2017 month =  1 
## you have  51  high tides
## you have  51  low tides

## you are on index  62  of  85 
## in  2017 month =  2 
## you have  54  high tides
## you have  54  low tides

## you are on index  63  of  85 
## in  2017 month =  3 
## you have  60  high tides
## you have  60  low tides

## you are on index  64  of  85 
## in  2017 month =  4 
## you have  58  high tides
## you have  58  low tides

## you are on index  65  of  85 
## in  2017 month =  5 
## you have  60  high tides
## you have  60  low tides

## you are on index  66  of  85 
## in  2017 month =  6 
## you have  58  high tides
## you have  58  low tides

## you are on index  67  of  85 
## in  2017 month =  7 
## you have  59  high tides
## you have  60  low tidesin  2017 month =  7 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  68  of  85 
## in  2017 month =  8 
## you have  59  high tides
## you have  60  low tidesin  2017 month =  8 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  69  of  85 
## in  2017 month =  9 
## you have  57  high tides
## you have  58  low tidesin  2017 month =  9 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  70  of  85 
## in  2017 month =  10 
## you have  59  high tides
## you have  60  low tidesin  2017 month =  10 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  71  of  85 
## in  2017 month =  11 
## you have  58  high tides
## you have  58  low tides

## you are on index  72  of  85 
## in  2017 month =  12 
## you have  60  high tides
## you have  60  low tides

## you are on index  73  of  85 
## in  2018 month =  1 
## you have  60  high tides
## you have  59  low tidesin  2018 month =  1 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  74  of  85 
## in  2018 month =  2 
## you have  54  high tides
## you have  54  low tides

## you are on index  75  of  85 
## in  2018 month =  3 
## you have  60  high tides
## you have  59  low tidesin  2018 month =  3 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  76  of  85 
## in  2018 month =  4 
## you have  58  high tides
## you have  58  low tides

## you are on index  77  of  85 
## in  2018 month =  5 
## you have  60  high tides
## you have  60  low tides

## you are on index  78  of  85 
## in  2018 month =  6 
## you have  58  high tides
## you have  57  low tidesin  2018 month =  6 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  79  of  85 
## in  2018 month =  7 
## you have  60  high tides
## you have  60  low tides

## you are on index  80  of  85 
## in  2018 month =  8 
## you have  60  high tides
## you have  60  low tides

## you are on index  81  of  85 
## in  2018 month =  9 
## you have  58  high tides
## you have  58  low tides

## you are on index  82  of  85 
## in  2018 month =  10 
## you have  60  high tides
## you have  60  low tides

## you are on index  83  of  85 
## in  2018 month =  11 
## you have  57  high tides
## you have  58  low tidesin  2018 month =  11 
## you now have  57  high tides
## you now have  57  low tides

## you are on index  84  of  85 
## in  2018 month =  12 
## you have  59  high tides
## you have  60  low tidesin  2018 month =  12 
## you now have  59  high tides
## you now have  59  low tides

## you are on index  85  of  85 
## in  2019 month =  1 
## you have  59  high tides
## you have  60  low tidesin  2019 month =  1 
## you now have  59  high tides
## you now have  59  low tides

Based on your viewing of the data, what are the problems that may arise with the data? How could you flag it to a user?

There are problems with the data evident from the calculation of high and low water levels. For example, there are only 13 high and low tides calculated for July 2012 compared to the values for the other months in the dataset which are all above 50. This suggests the possibility of missing data for this month. Furthermore, there are 586 calculated high and low tides which is a significantly greater value than for all other months. Thus, the data could possibly be faulty.