Basic Time-Series Hydro-logical Data Analysis : Solutions (Part1)

Below are the solutions to these exercises on Basic Hydrological Data Analysis (Part 1).

if (!require(hydroTSM)){install.packages(hydroTSM, dep=T)}
## Warning: package 'hydroTSM' was built under R version 3.3.3
## Warning: package 'zoo' was built under R version 3.3.3
## Warning: package 'xts' was built under R version 3.3.3
library(hydroTSM)

###############
#             #
# Exercise 1  #
#             #
###############

saugeenPrecipt<-read.csv(file.choose(),header=TRUE)

###############
#             #
# Exercise 2  #
#             #
###############
saugeenPrecipt<-saugeenPrecipt[-c(1400:1403),]
colnames(saugeenPrecipt)<-c("date","P")

###############
#             #
# Exercise 3  #
#             #
###############

sapply(saugeenPrecipt,class)
##     date        P 
## "factor" "factor"
saugeenPrecipt[,2]<-as.numeric(saugeenPrecipt[,2])
Date.y<-as.Date(saugeenPrecipt[,1],format = "%Y-%m-%d")
zoo_saugeenP<- zoo( saugeenPrecipt[,2:ncol(saugeenPrecipt)], Date.y)
sapply(zoo_saugeenP,class)
## [1] "zoo"
zoo_saugeenP<-window(zoo_saugeenP,start=as.Date("1988-01-01"),end=as.Date("1990-12-31"))

###############
#             #
# Exercise 4  #
#             #
###############

smry(zoo_saugeenP)
##               Index zoo_saugeenP
## Min.     1988-01-01       4.0000
## 1st Qu.  1988-09-30       4.0000
## Median   1989-07-02       6.0000
## Mean     1989-07-01      30.0300
## 3rd Qu.  1990-04-01      55.0000
## Max.     1990-12-31     123.0000
## IQR            <NA>      51.0000
## sd             <NA>      37.6339
## cv             <NA>       1.2532
## Skewness       <NA>       1.1144
## Kurtosis       <NA>      -0.3643
## NA's           <NA>       0.0000
## n              <NA>    1095.0000
###############
#             #
# Exercise 5  #
#             #
###############

nyears <- yip(from=start(zoo_saugeenP), to=end(zoo_saugeenP), out.type="nmbr")
nyears
## [1] 3
###############
#             #
# Exercise 6  #
#             #
###############

annualfunction(zoo_saugeenP, FUN=sum, na.rm=TRUE) / nyears
##    value 
## 10961.33
###############
#             #
# Exercise 7  #
#             #
###############
saugeen_monthly_P<-daily2monthly(zoo_saugeenP,FUN=sum,na.rm = TRUE)

###############
#             #
# Exercise 8  #
#             #
###############
cmonth <- format(time(saugeen_monthly_P), "%b")
# Creating ordered monthly factors
months <- factor(cmonth, levels=unique(cmonth), ordered=TRUE)
# Boxplot of the monthly values
boxplot(coredata(saugeen_monthly_P) ~ months, col="lightgreen", main="Total Monthly Precipitation",
        ylab="Precipitation(mm)", xlab="Month")
###############
#             #
# Exercise 9  #
#             #
###############
P<- matrix(saugeen_monthly_P, ncol=12, byrow=TRUE)
colnames(P) <- month.abb
rownames(P) <- unique(format(time(saugeen_monthly_P), "%Y"))
# Plotting the monthly precipitation values
require(lattice)
print(matrixplot(P, ColorRamp="Precipitation",
                    main="Total Monthly Precipitation"))
###############
#             #
# Exercise 10  #
#             #
###############
hydroplot(zoo_saugeenP[,1], var.type = "Precipitation", ptype="ts+boxplot",main="at Saugeen",pfreq = "dm", from="1988-01-01")
#ptype : ts/ts+boxplot/ts+hist/ts+boxplot+hist (default)
#pfreq : dm/dma/ma/o/seasonal
#FUN   : sum,mean or can be changed with var.type 
#var.type : Flow (mean), Temperature(mean), Precipitation (sum)

#play with FUN,pfreq argument
hydroplot(zoo_saugeenP, FUN=sum, main="at Saugeen",pfreq = "dm", from="1988-01-01",to="1990-12-31")