#Required R modules
library(XML)
library(ggplot2)
url <- "http://hilltopserver.horizons.govt.nz/data.hts?Service=Hilltop&Request=GetData&Site=Manawatu%20at%20Teachers%20College&Measurement=Stage&From=1/08/2017&To=5/08/2017"
#dataxml<-xmlParse(url)
#print(dataxml)
measxml<-xmlInternalTreeParse(url)
error<-as.character(sapply(getNodeSet(doc=measxml, path="//Error"), xmlValue))
#print(error)
if(length(error)==0){
m<-getNodeSet(measxml, "//Hilltop/Measurement")
for(ms in 1:length(m)){
d <- getNodeSet(measxml, paste("//Measurement[",ms,"]/Data",sep=""))
for(ds in 1:length(d)){
e <- getNodeSet(measxml, paste("//Measurement[",ms,"]/Data[",ds,"]/E",sep=""))
mes_df = NULL
for(es in 1:length(e)){
timexmldf <- xmlToDataFrame(getNodeSet(measxml, paste("//Measurement[",ms,"]/Data[",ds,"]/E[",es,"]/T",sep="")))
#t = rbind(t, data.frame(timexmldf))
#Time1<-as.POSIXct(strptime(timexmldf,format="%Y-%m-%dT%H:%M:%S"))
valuexmldf <- xmlToDataFrame(getNodeSet(measxml, paste("//Measurement[",ms,"]/Data[",ds,"]/E[",es,"]/I1",sep="")))
measurment <- "Stage"
#print(valuexmldf)
#v = rbind(v, data.frame(valuexmldf))
mes_df = rbind(mes_df,data.frame(measurment,timexmldf,valuexmldf))
}
#print(mes_df)
names(mes_df) <- c("Measurement", "Time", "Value") # renaming the fields
New_mes_df = NULL
NewMes<-(mes_df$Measurement)
NewTime<-as.POSIXct(strptime(mes_df$Time,format="%Y-%m-%dT%H:%M:%S")) #confirming time format
NewValue<-(as.numeric(as.character(mes_df$Value))) #make sure that the values are numbers
New_mes_df = rbind(New_mes_df,data.frame(NewMes,NewTime,NewValue)) #make a new dataframe
#print(New_mes_df)
print(ggplot(New_mes_df, aes(NewTime, NewValue)) +
geom_line(na.rm=TRUE, color="blue", size=1) +
ggtitle(paste("Stage (mm) at","\n ","Manawatu at Teachers College")) +
xlab("Date") + ylab("Stage (mm)"))
}
}
}

######################
url1 <- "http://hilltopserver.horizons.govt.nz/data.hts?Service=Hilltop&Request=GetData&Site=Manawatu%20at%20Teachers%20College&Measurement=Turbidity:%20Point%20Sample&From=1/08/2017&To=5/08/2017"
#dataxml<-xmlParse(url)
#print(dataxml)
measxml1<-xmlInternalTreeParse(url1)
error<-as.character(sapply(getNodeSet(doc=measxml1, path="//Error"), xmlValue))
#print(error)
if(length(error)==0){
m<-getNodeSet(measxml1, "//Hilltop/Measurement")
for(ms in 1:length(m)){
d <- getNodeSet(measxml1, paste("//Measurement[",ms,"]/Data",sep=""))
for(ds in 1:length(d)){
e <- getNodeSet(measxml1, paste("//Measurement[",ms,"]/Data[",ds,"]/E",sep=""))
mes_df1 = NULL
for(es in 1:length(e)){
timexmldf1 <- xmlToDataFrame(getNodeSet(measxml1, paste("//Measurement[",ms,"]/Data[",ds,"]/E[",es,"]/T",sep="")))
#t = rbind(t, data.frame(timexmldf1))
#Time1<-as.POSIXct(strptime(timexmldf1,format="%Y-%m-%dT%H:%M:%S"))
valuexmldf1 <- xmlToDataFrame(getNodeSet(measxml1, paste("//Measurement[",ms,"]/Data[",ds,"]/E[",es,"]/I1",sep="")))
measurment1 <- "Turbidity"
#print(valuexmldf1)
#v = rbind(v, data.frame(valuexmldf1))
mes_df1 = rbind(mes_df1,data.frame(measurment1,timexmldf1,valuexmldf1))
}
#print(mes_df1)
names(mes_df1) <- c("Measurement", "Time", "Value") # renaming the fields
New_mes_df1 = NULL
NewMes<-(mes_df1$Measurement)
NewTime<-as.POSIXct(strptime(mes_df1$Time,format="%Y-%m-%dT%H:%M:%S")) #confirming time format
NewValue<-(as.numeric(as.character(mes_df1$Value))) #make sure that the values are numbers
New_mes_df1 = rbind(New_mes_df1,data.frame(NewMes,NewTime,NewValue)) #make a new dataframe
#print(New_mes_df1)
print(ggplot(New_mes_df1, aes(NewTime, NewValue)) +
geom_line(na.rm=TRUE, color="brown", size=1) +
ggtitle(paste("Turbidity (NTU) (mm) at","\n ","Manawatu at Teachers College")) +
xlab("Date") + ylab("Turbidity (NTU)"))
}
}
}

summary(New_mes_df$NewValue)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 982 1164 1280 1380 1533 2409
summary(New_mes_df1$NewValue)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 8.70 14.10 32.40 95.96 83.30 672.00
p <- ggplot()
p <- p + geom_line(data = New_mes_df, aes(x = NewTime, y = NewValue, colour= "Stage"))
# adding the relative humidity data, transformed to match roughly the range of the temperature
p <- p + geom_line(data = New_mes_df1, aes(x = NewTime, y = NewValue/.28, colour= "Turbidity"), size=.75, linetype="F1")
# now adding the secondary axis, following the example in the help file ?scale_y_continuous
# and, very important, reverting the above transformation
p <- p + scale_y_continuous(sec.axis = sec_axis(~.*.28, name = "Turbidity [NTU]"))
#print(p)
# modifying colours and theme options
p <- p + scale_colour_manual(values = c("blue", "brown"))
p <- p + labs(y = "Stage[mm]",
x = "Date and time",
colour = "Parameter")
p <- p + theme(legend.position = c(0.8, 0.9))
p

#Line types
d=data.frame(lt=c("blank", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash", "1F", "F1", "4C88C488", "12345678"))
ggplot() +
scale_x_continuous(name="", limits=c(0,1), breaks=NULL) +
scale_y_discrete(name="linetype") +
scale_linetype_identity() +
geom_segment(data=d, mapping=aes(x=0, xend=1, y=lt, yend=lt, linetype=lt))
