test-evap.R 3.95 KB
Newer Older
1
2
context("Test evaporation")

3
4
5
6

rm(list = ls())
data(L0123001); BasinObs_L0123001 <- BasinObs
data(L0123002); BasinObs_L0123002 <- BasinObs
7
data(L0123003); BasinObs_L0123003 <- BasinObs
8

9
10
11
12
comp_evap <- function(BasinObs,
                      Lat, LatUnit,
                      TimeStepIn = "daily",
                      TimeStepOut = "daily") {
13
14
  PotEvap <- PE_Oudin(JD = as.POSIXlt(BasinObs$DatesR)$yday + 1,
                      Temp = BasinObs$T,
15
                      Lat = Lat, LatUnit = LatUnit,
16
                      TimeStepIn = TimeStepIn, TimeStepOut = TimeStepOut)
17
18
  PotEvapFor <- PE_Oudin(JD = as.POSIXlt(BasinObs$DatesR)$yday + 1,
                         Temp = BasinObs$T,
19
20
21
                         Lat = Lat, LatUnit = LatUnit,
                         TimeStepIn = TimeStepIn, TimeStepOut = TimeStepOut,
                         RunFortran = TRUE)
22
23
24
  all(range(PotEvap - PotEvapFor) < 0.000001)
}

25

26
test_that("PE_Oudin works", {
27
  skip_on_cran()
28

29
30
31
32
33
34
35
  expect_true(comp_evap(BasinObs = BasinObs_L0123001,
                        Lat = 0.8, LatUnit = "rad",
                        TimeStepIn = "daily", TimeStepOut = "daily"))
  expect_true(comp_evap(BasinObs = BasinObs_L0123001,
                        Lat = 0.8, LatUnit = "rad",
                        TimeStepIn = "daily", TimeStepOut = "hourly"))
  expect_true(comp_evap(BasinObs = BasinObs_L0123002,
36
                        Lat = 0.9, LatUnit = "rad",
37
38
39
40
                        TimeStepIn = "daily", TimeStepOut = "daily"))
  expect_true(comp_evap(BasinObs = BasinObs_L0123002,
                        Lat = 0.9, LatUnit = "rad",
                        TimeStepIn = "daily", TimeStepOut = "hourly"))
41

42
  ## check with several catchments using different values for Lat
43

44
45
46
47
  ## one by one
  PotEvapFor1 <- PE_Oudin(JD = as.POSIXlt(BasinObs_L0123001$DatesR)$yday + 1,
                          Temp = BasinObs_L0123001$T,
                          Lat = 0.8, LatUnit = "rad",
48
                          RunFortran = TRUE)
49
50
51
  PotEvapFor2 <- PE_Oudin(JD = as.POSIXlt(BasinObs_L0123002$DatesR)$yday + 1,
                          Temp = BasinObs_L0123002$T,
                          Lat = 0.9, LatUnit = "rad",
52
                          RunFortran = TRUE)
53

54
55
56
57
58
59
60
  ## all in one
  BasinObs_L0123001$Lat <- 0.8
  BasinObs_L0123002$Lat <- 0.9
  BasinObs <- rbind(BasinObs_L0123001, BasinObs_L0123002)
  PotEvapFor <- PE_Oudin(JD = as.POSIXlt(BasinObs$DatesR)$yday + 1,
                         Temp = BasinObs$T,
                         Lat = BasinObs$Lat, LatUnit = "rad",
61
                         RunFortran = TRUE)
62

63
  expect_equal(PotEvapFor, c(PotEvapFor1, PotEvapFor2))
64
})
65
66
67
68

test_that("Inconsitent time series", {
  skip_on_cran()

69
70
  msgDaily <- "each day should have only one identical value of Julian days. The time series is not sorted, or contains duplicate or missing dates"
  msgHoury <- "each day must have 24 identical values of Julian days (one for each hour). The time series is not sorted, or contains duplicate or missing dates"
71
72
73
74
75
76
77

  # duplicated dates
  DatesFor1Dupl <- BasinObs_L0123001$DatesR
  DatesFor1Dupl[5L] <- DatesFor1Dupl[4L]
  expect_warning(object = PE_Oudin(JD = as.POSIXlt(DatesFor1Dupl)$yday + 1,
                                   Temp = BasinObs_L0123001$T,
                                   Lat = 0.8, LatUnit = "rad"),
78
                 regexp = msgDaily)
79

80
  # not ordered daily dates
81
82
83
84
  DatesFor1Messy <- sample(BasinObs_L0123001$DatesR)
  expect_warning(object = PE_Oudin(JD = as.POSIXlt(DatesFor1Messy)$yday + 1,
                                   Temp = BasinObs_L0123001$T,
                                   Lat = 0.8, LatUnit = "rad"),
85
86
87
88
89
                 regexp = msgDaily)

  # not ordered hourly dates
  DatesFor3Messy <- sample(BasinObs_L0123003$DatesR)
  expect_error(object = PE_Oudin(JD = as.POSIXlt(DatesFor3Messy)$yday + 1,
90
                                   Temp = seq_along(BasinObs_L0123003$T),
91
92
                                   Lat = 0.8, LatUnit = "rad", TimeStepIn = "hourly"),
                 regexp = msgHoury, fixed = TRUE)
93
94
95
96
97


})