Commit d22d86a7 authored by Dan Baston's avatar Dan Baston

Improve wsim.lsm test organization

parent 04056051
Pipeline #107887573 passed with stages
in 20 minutes and 53 seconds
# Copyright (c) 2018 ISciences, LLC.
# All rights reserved.
#
# WSIM is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. You may
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require(testthat)
context('LSM IO functions')
test_that('we can read and write states from/to netCDF', {
fname <- tempfile()
state <- make_state(
extent=c(-180, 180, -90, 90),
Snowpack= matrix(runif(4), nrow=2),
Dr= matrix(runif(4), nrow=2),
Ds= matrix(runif(4), nrow=2),
snowmelt_month= matrix(rep.int(0, 4), nrow=2),
Ws= matrix(runif(4), nrow=2),
yearmon='201609'
)
write_lsm_values_to_cdf(state, fname, prec='double')
expect_true(file.exists(fname))
state2 <- read_state_from_cdf(fname)
expect_equal(state2, state, check.attributes=FALSE)
file.remove(fname)
})
test_that('we can read forcing from netCDF', {
fname <- tempfile()
forcing <- make_forcing(
extent=c(-180, 180, -90, 90),
pWetDays=matrix(rep.int(1, 4), nrow=2),
T=matrix(rep.int(NA, 4), nrow=2),
Pr=matrix(runif(4), nrow=2)
)
wsim.lsm::write_lsm_values_to_cdf(forcing, fname, prec='double')
forcing2 <- read_forcing_from_cdf(fname)
expect_equal(forcing2, forcing, check.attributes=FALSE)
file.remove(fname)
})
test_that('we can write model results to netCDF', {
static <- list(
elevation=matrix(seq(0, 800, 100), nrow=3),
flow_directions=matrix(rep.int(as.integer(NA), 9), nrow=3),
Wc=matrix(rep.int(150, 9), nrow=3)
)
forcing <- make_forcing(
extent=c(-180, 180, -90, 90),
pWetDays=matrix(rep.int(1, 9), nrow=3),
T=matrix(runif(9), nrow=3),
Pr=matrix(runif(9), nrow=3)
)
state <- make_state(
extent=c(-180, 180, -90, 90),
Snowpack= matrix(runif(9), nrow=3),
Dr= matrix(runif(9), nrow=3),
Ds= matrix(runif(9), nrow=3),
snowmelt_month= matrix(rep.int(0, 9), nrow=3),
Ws= static$Wc * runif(1),
yearmon='201609'
)
iter <- wsim.lsm::run(static, state, forcing)
fname <- tempfile(fileext='.nc')
wsim.lsm::write_lsm_values_to_cdf(iter$obs, fname, prec='double')
wsim.lsm::write_lsm_values_to_cdf(state, fname, prec='double')
expect_true(TRUE)
file.remove(fname)
})
......@@ -185,107 +185,3 @@ test_that('dWdt calculation tolerates NODATA inputs', {
expect_false(is.na(hydro$dWdt))
})
test_that('cell areas are computed correctly', {
dims <- c(360, 720)
extent <- c(-180, 180, -90, 90)
area_hlf_deg <- cell_areas_m2(extent, dims)
expect_equal(unname(area_hlf_deg[108, 17]), 2492775206)
})
test_that('date calculations are correct', {
expect_equal(next_yyyymm('201612'), '201701')
expect_equal(next_yyyymm('201701'), '201702')
expect_equal(days_in_yyyymm('201701'), 31)
expect_equal(days_in_yyyymm('201702'), 28)
expect_equal(days_in_yyyymm('201602'), 29)
})
test_that('we can compute daylength', {
daylength <- day_length_matrix(2017, 02, c(-180, 180, -90, 90), 181, 1)
expect_true(!any(is.na(daylength)))
expect_true(!any(daylength < 0 | daylength > 1))
expect_equal(daylength[1], 0) # dark at the north pole
expect_equal(daylength[181], 1) # light at the south pole
expect_equal(daylength[91], 0.5) # 12 hours of light at the equator
})
test_that('we can read and write states from/to netCDF', {
fname <- tempfile()
state <- make_state(
extent=c(-180, 180, -90, 90),
Snowpack= matrix(runif(4), nrow=2),
Dr= matrix(runif(4), nrow=2),
Ds= matrix(runif(4), nrow=2),
snowmelt_month= matrix(rep.int(0, 4), nrow=2),
Ws= matrix(runif(4), nrow=2),
yearmon='201609'
)
write_lsm_values_to_cdf(state, fname, prec='double')
expect_true(file.exists(fname))
state2 <- read_state_from_cdf(fname)
expect_equal(state2, state, check.attributes=FALSE)
file.remove(fname)
})
test_that('we can read forcing from netCDF', {
fname <- tempfile()
forcing <- make_forcing(
extent=c(-180, 180, -90, 90),
pWetDays=matrix(rep.int(1, 4), nrow=2),
T=matrix(rep.int(NA, 4), nrow=2),
Pr=matrix(runif(4), nrow=2)
)
wsim.lsm::write_lsm_values_to_cdf(forcing, fname, prec='double')
forcing2 <- read_forcing_from_cdf(fname)
expect_equal(forcing2, forcing, check.attributes=FALSE)
file.remove(fname)
})
test_that('we can write model results to netCDF', {
static <- list(
elevation=matrix(seq(0, 800, 100), nrow=3),
flow_directions=matrix(rep.int(as.integer(NA), 9), nrow=3),
Wc=matrix(rep.int(150, 9), nrow=3)
)
forcing <- make_forcing(
extent=c(-180, 180, -90, 90),
pWetDays=matrix(rep.int(1, 9), nrow=3),
T=matrix(runif(9), nrow=3),
Pr=matrix(runif(9), nrow=3)
)
state <- make_state(
extent=c(-180, 180, -90, 90),
Snowpack= matrix(runif(9), nrow=3),
Dr= matrix(runif(9), nrow=3),
Ds= matrix(runif(9), nrow=3),
snowmelt_month= matrix(rep.int(0, 9), nrow=3),
Ws= static$Wc * runif(1),
yearmon='201609'
)
iter <- wsim.lsm::run(static, state, forcing)
fname <- tempfile(fileext='.nc')
wsim.lsm::write_lsm_values_to_cdf(iter$obs, fname, prec='double')
wsim.lsm::write_lsm_values_to_cdf(state, fname, prec='double')
expect_true(TRUE)
file.remove(fname)
})
......@@ -68,3 +68,32 @@ test_that('we can convert forcing units to those required by the model', {
expect_equal(5, temp_celsius(5, 'degree_Celsius'))
expect_equal(5, temp_celsius(278.15, 'K'))
})
test_that('cell areas are computed correctly', {
dims <- c(360, 720)
extent <- c(-180, 180, -90, 90)
area_hlf_deg <- cell_areas_m2(extent, dims)
expect_equal(unname(area_hlf_deg[108, 17]), 2492775206)
})
test_that('date calculations are correct', {
expect_equal(next_yyyymm('201612'), '201701')
expect_equal(next_yyyymm('201701'), '201702')
expect_equal(days_in_yyyymm('201701'), 31)
expect_equal(days_in_yyyymm('201702'), 28)
expect_equal(days_in_yyyymm('201602'), 29)
})
test_that('we can compute daylength', {
daylength <- day_length_matrix(2017, 02, c(-180, 180, -90, 90), 181, 1)
expect_true(!any(is.na(daylength)))
expect_true(!any(daylength < 0 | daylength > 1))
expect_equal(daylength[1], 0) # dark at the north pole
expect_equal(daylength[181], 1) # light at the south pole
expect_equal(daylength[91], 0.5) # 12 hours of light at the equator
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment