tests/testthat/test_season.R

######################################################
## testing that extraction of the annual maximum work
#######################################################

## Verify that the function return the right output
x <- ExtractAmax(flow~date, flowStJohn)
expect_equal(dim(x), c(89,4))
expect_equal(names(x), c("flow","date","n","yy" ))

## Verify the filtering work. there is one year of 92 observations
x <- ExtractAmax(flow~date, flowStJohn, tol = 92)
expect_equal(dim(x), c(89,4))

x <- ExtractAmax(flow~date, flowStJohn, tol = 93)
expect_equal(dim(x), c(88,4))

## Verify that work with data.frame
y <- ExtractAmax(flowStJohn[,c('flow','date')], tol = 365)
expect_equal(y,x)

## multiple site
x2 <- rbind(cbind(station = 1, x),cbind(station = 2, x))

ex1 <- ExtractAmax(flow~date, x)
ex2 <- ExtractAmax(flow~station + date, x2)

ex1.2 <- rbind(cbind(ex1, station = 1),
               cbind(ex1, station = 2))

expect_true( all(ex2 == ex1.2))


######################################################
## testing that seasonal statistics
#######################################################

## Verify that a known exemple work
fit <- SeasonStat(ex1$date)
expect_true(class(fit) == 'numeric')
ref <- c(-0.4259258,  0.8665048,  2.0276568,  0.9655275)
expect_true(all(abs(ref-fit) < 1e-5))

## verify that multiple stations works
fit2 <- SeasonStat(date~station, ex2)
expect_true(class(fit2) == 'matrix')
expect_true(all(rbind(fit,fit) == fit2))

## Verify that using a date in characters works
expect_equal(fit,SeasonStat(as.character(ex1$date)))

## return an error if it is not a date
expect_error(SeasonStat(1:5))
expect_error(SeasonStat(month.name))

######################################################
## testing that seasonal distance
#######################################################

nsite <- 1000
scoord <- data.frame(angle = runif(nsite,0,2*pi),
                     radius = runif(nsite))

d1 <- DistSeason(radius ~ angle , scoord)

radMat <- as.matrix(dist(scoord[,2], method = 'man'))

angMat <- mx <-  matrix(0, nsite,nsite)

for(ii in seq(nsite-1)){
  for(jj in seq(ii+1,nsite)){

	  mx[jj,ii] <- max(scoord[c(ii,jj),1]) - min(scoord[c(ii,jj),1])
	  angMat[jj,ii] <- min(2*pi - mx[jj,ii], mx[jj,ii])/pi
	  angMat[ii,jj] <- angMat[jj,ii]
	  mx[ii,jj] <- mx[jj,ii]
  }
}

d2 <-  sqrt(angMat^2+radMat^2)


expect_equal(d1,d2)
martindurocher/floodStat documentation built on May 31, 2019, 12:42 a.m.