#' Area-wide NMxQ Trend
#'
#' @description Function creates dataframe of stations, containing Trend (every year/within a specific season) of their NMxQ Value within the given timeframe. Therefore it filters all stations within the metadataset depending on the start- and endyear of their Measurement Series.
#'
#' @param x numeric; length of period (days). With decreasing Values for x , the influence of short-term anthropogenic influences increases. E.g. x=7, x=14, x=30
#' @param data list; Contains all stations that the discharge analysis should consider. List can be created by \link[dischanalyst]{grdc_list}. Each entry of the list contains the existing discharge measurements (as numeric) and the corresponding dates (as character) for the station.
#' @param metadata Data Frame. Overview of GRDC-Dataset. The metadata can be created by \link[dischanalyst]{metadata_grdc} function.
#' @param Startyear numeric; Startyear of timerange.
#' @param Endyear numeric; Endyear of timerange.
#'
#' @return dataframe. Including the stationname, the river, the spatial information of the station, the trend (linear model and zyp/"yuepilon"approach (with PreWhitening and Autocorrelation)) trend within every year/winter/summer/spring/autumn in timeframe.
#' \describe{
#' \item{intercept_zyp}{intercept created by \link[zyp]{zyp.trend.vector}}
#' \item{slope_zyp}{slope created by \link[zyp]{zyp.trend.vector}}
#' \item{sig_zyp}{significance (Kendall's P-Value) for the final detrended time-series}
#' \item{intercept_ls}{intercept created by \link[stats]{lm}}
#' \item{slope_ls}{slope created by \link[stats]{lm}}
#' }
#'@import stats
#'@import zyp
#' @export
#'
#' @examples
#' \dontrun{
#' NMxQmeta(7 , data, metadata, 1820, 2019)}
#'
NMxQmeta=function(x, data, metadata, Startyear, Endyear){
#Stations starting (at least) with November within hydrological year
dataset=which(metadata$startyear<=Startyear)
#stationnames that fit in startyear
ld=length(dataset)
starthydro=rep(0,ld )
for ( i in 1:ld){
starthydro[i]=as.numeric(substr(metadata$startday[dataset[i]], 6,7))
start=which(starthydro<12)
}
dataset=dataset[start]
datasetnames=metadata$station[dataset]
#Stations ending at least with October within Endyear
datasetend=which(metadata$endyear>=Endyear)
lde=length( datasetend)
endhydro=rep(0,lde )
for ( i in 1:lde){
endhydro[i]=as.numeric(substr(metadata$endday[datasetend[i]], 6,7))
end=which(endhydro>9)
}
datasetend=datasetend[end]
datasetendnames=metadata$station[datasetend]
#stationnames that fit in endyear
vec=rep(F,length(dataset))
for ( i in 1:length(dataset) ){
vec[i]=is.element(dataset[i],datasetend)
}
stations=dataset[which(vec==T)]
#stations that fit in start as well as endyear
######
l=length(stations)
# calculate for Year ------------------------------------------------------
Yslopezyp=rep(0, l)
Yinterceptzyp=rep(0, l)
Ysigzyp=rep(0, l)
Yslopelm=rep(0, l)
Yintlm=rep(0, l)
Wslopezyp=rep(0, l)
Winterceptzyp=rep(0, l)
Wsigzyp=rep(0, l)
Wslopelm=rep(0, l)
Wintlm=rep(0, l)
Spslopezyp=rep(0, l)
Spinterceptzyp=rep(0, l)
Spsigzyp=rep(0, l)
Spslopelm=rep(0, l)
Spintlm=rep(0, l)
Sslopezyp=rep(0, l)
Sinterceptzyp=rep(0, l)
Ssigzyp=rep(0, l)
Sslopelm=rep(0, l)
Sintlm=rep(0, l)
Aslopezyp=rep(0, l)
Ainterceptzyp=rep(0, l)
Asigzyp=rep(0, l)
Aslopelm=rep(0, l)
Aintlm=rep(0, l)
for ( i in 1:l){
datan=data[[stations[i]]]
min=paste(Startyear, "-11") #calc min of dataset
min=sub(" -", "-", min)
min=min(grep(min, datan[,1]))
max=paste(Endyear, "-10")
max=sub(" -", "-", max)
max=max(grep(max, datan[,1]))
datak=datan[min:max,]
years=(Startyear):(Endyear)
lm=length(years)-1
ls=length(years)
minxqy=rep(0,lm)
for (t in 1:lm){
yearmin=years[t]
yearmax=years[t+1]
min=paste(yearmin,"-11")
min=sub(" -", "-",min)
max= paste(yearmax,"-10")
max=sub(" -", "-",max)
start=min(grep(min, datak[,1]))
end=max(grep(max, datak[,1]))
datal=datak[start:end, ]
l=nrow(datal)
le=l-x
le
Nmxq=rep(0, le)
for ( k in 1:le){
Nmxq[k]=mean(datal[k:(k+x), 2])
}
minxqy[t]=(min(Nmxq))
}
hyears=years[-length(years)]
df=data.frame(hyears, minxqy)
zyp=zyp.trend.vector(df$minxqy, df$hyears, "yuepilon")
linmod=lm(df$minxqy~df$hyears)
Yslopezyp[i]=zyp[2]
Yinterceptzyp[i]=zyp[11]
Ysigzyp[i]=zyp[6]
Yslopelm[i]=as.numeric(linmod$coefficients[2])
Yintlm[i]=as.numeric(linmod$coefficients[1])
minxqw=rep(0,lm)
for (t in 1:lm){
yearmin=years[t]
yearmax=years[t+1]
min=paste(yearmin,"-11")
min=sub(" -", "-",min)
max= paste(yearmax,"-01")
max=sub(" -", "-",max)
start=min(grep(min, datak[,1]))
end=max(grep(max, datak[,1]))
datal=datak[start:end, ]
l=nrow(datal)
le=l-x
le
Nmxq=rep(0, le)
for ( k in 1:le){
Nmxq[k]=mean(datal[k:(k+x), 2])
}
minxqw[t]=(min(Nmxq))
}
hyears=years[-length(years)]
df=data.frame(hyears, minxqw)
zyp=zyp.trend.vector(df$minxqw, df$hyears, "yuepilon")
linmod=lm(df$minxqw~df$hyears)
Wslopezyp[i]=zyp[2]
Winterceptzyp[i]=zyp[11]
Wsigzyp[i]=zyp[6]
Wslopelm[i]=as.numeric(linmod$coefficients[2])
Wintlm[i]=as.numeric(linmod$coefficients[1])
minxqf=rep(0,ls)
for (t in 2:ls){
yearmin=years[t]
min=paste(yearmin,"-02")
min=sub(" -", "-",min)
max= paste(yearmin,"-04")
max=sub(" -", "-",max)
start=min(grep(min, datak[,1]))
end=max(grep(max, datak[,1]))
datal=datak[start:end, ]
p=nrow(datal)
le=p-x
Nmxq=rep(0, le)
for ( k in 1:le){
Nmxq[k]=mean(datal[k:(k+x), 2])
}
minxqf[t]=(min(Nmxq))
}
hyears=years[-1]
minxqf=minxqf[-1]
df=data.frame(hyears, minxqf)
zyp=zyp.trend.vector(df$minxqf, df$hyears, "yuepilon")
linmod=lm(df$minxqf~df$hyears)
Spslopezyp[i]=zyp[2]
Spinterceptzyp[i]=zyp[11]
Spsigzyp[i]=zyp[6]
Spslopelm[i]=as.numeric(linmod$coefficients[2])
Spintlm[i]=as.numeric(linmod$coefficients[1])
minxqs=rep(0,ls)
for (t in 2:ls){
yearmin=years[t]
min=paste(yearmin,"-05")
min=sub(" -", "-",min)
max= paste(yearmin,"-07")
max=sub(" -", "-",max)
start=min(grep(min, datak[,1]))
end=max(grep(max, datak[,1]))
datal=datak[start:end, ]
p=nrow(datal)
le=p-x
Nmxq=rep(0, le)
for ( k in 1:le){
Nmxq[k]=mean(datal[k:(k+x), 2])
}
minxqs[t]=(min(Nmxq))
}
hyears=years[-1]
minxqs=minxqs[-1]
df=data.frame(hyears, minxqs)
zyp=zyp.trend.vector(df$minxqs, df$hyears, "yuepilon")
linmod=lm(df$minxqs~df$hyears)
Sslopezyp[i]=zyp[2]
Sinterceptzyp[i]=zyp[11]
Ssigzyp[i]=zyp[6]
Sslopelm[i]=as.numeric(linmod$coefficients[2])
Sintlm[i]=as.numeric(linmod$coefficients[1])
# Autumn ------------------------------------------------------------------
minxqa=rep(0,ls)
for (t in 2:ls){
yearmin=years[t]
min=paste(yearmin,"-08")
min=sub(" -", "-",min)
max= paste(yearmin,"-10")
max=sub(" -", "-",max)
start=min(grep(min, datak[,1]))
end=max(grep(max, datak[,1]))
datal=datak[start:end, ]
p=nrow(datal)
le=p-x
Nmxq=rep(0, le)
for ( k in 1:le){
Nmxq[k]=mean(datal[k:(k+x), 2])
}
minxqa[t]=(min(Nmxq))
}
hyears=years[-1]
minxqa=minxqa[-1]
df=data.frame(hyears, minxqa)
zyp=zyp.trend.vector(df$minxqa, df$hyears, "yuepilon")
linmod=lm(df$minxqa~df$hyears)
Aslopezyp[i]=zyp[2]
Ainterceptzyp[i]=zyp[11]
Asigzyp[i]=zyp[6]
Aslopelm[i]=as.numeric(linmod$coefficients[2])
Aintlm[i]=as.numeric(linmod$coefficients[1])
}
# metadata ----------------------------------------------------------------
meta=data.frame( metadata$station[stations] , metadata$river[stations] , metadata$longitude[stations],
metadata$latitude[stations],
Yslopezyp, #Year
Yinterceptzyp,
Ysigzyp,
Yslopelm,
Yintlm,
Wslopezyp,#Winter
Winterceptzyp,
Wsigzyp,
Wslopelm,
Wintlm,
Spslopezyp, #Spring
Spinterceptzyp,
Spsigzyp,
Spslopelm,
Spintlm,
Sslopezyp, #Summer
Sinterceptzyp,
Ssigzyp,
Sslopelm,
Sintlm,
Aslopezyp, #Autumn
Ainterceptzyp,
Asigzyp,
Aslopelm,
Aintlm
)
colnames(meta)[c(1,2,3,4)]=c("station","river", "longitude","latitude" )
return(meta)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.