|
| 1 | +test_that("one dim", { |
| 2 | + local_edition(3) |
| 3 | + |
| 4 | + res <- c("a", "b") |> |
| 5 | + example_netcdf(file_path = withr::local_tempfile()) |> |
| 6 | + netcdf2df() |
| 7 | + |
| 8 | + expect_equal(dim(res), c(365, 3)) |
| 9 | + expect_equal(colnames(res), c("time", "a", "b")) |
| 10 | + expect_equal( |
| 11 | + attr(res, "units"), |
| 12 | + c(time = "days since 2001-01-01", a = "kg", b = "kg") |
| 13 | + ) |
| 14 | + expect_equal(res$time, (0L:364L)) |
| 15 | +}) |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +test_that("multiple dims, all but one scalar", { |
| 20 | + local_edition(3) |
| 21 | + |
| 22 | + # TODO does this work if pkg not installed (eg check time)? |
| 23 | + # I think testthat may shim system.file |
| 24 | + res <- netcdf2df( |
| 25 | + system.file("test-data/CRUNCEP.2000.nc", package = "PEcAn.utils") |
| 26 | + ) |
| 27 | + |
| 28 | + expect_equal(dim(res), c(366 * 4, 11)) |
| 29 | + expect_equal( |
| 30 | + colnames(res), |
| 31 | + c( |
| 32 | + "latitude", "longitude", "time", |
| 33 | + "air_temperature", "surface_downwelling_longwave_flux_in_air", |
| 34 | + "air_pressure", "surface_downwelling_shortwave_flux_in_air", |
| 35 | + "eastward_wind", "northward_wind", |
| 36 | + "specific_humidity", "precipitation_flux" |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + # Check units. NB some of these are nonstandard; |
| 41 | + # Key point is they should match what the file reports |
| 42 | + expect_equal( |
| 43 | + attr(res, "units"), |
| 44 | + c( |
| 45 | + latitude = "degree_north", |
| 46 | + longitude = "degree_east", |
| 47 | + time = "days since 2000-01-01T00:00:00Z", |
| 48 | + air_temperature = "Kelvin", |
| 49 | + surface_downwelling_longwave_flux_in_air = "W/m2", |
| 50 | + air_pressure = "Pascal", |
| 51 | + surface_downwelling_shortwave_flux_in_air = "W/m2", |
| 52 | + eastward_wind = "m/s", |
| 53 | + northward_wind = "m/s", |
| 54 | + specific_humidity = "g/g", |
| 55 | + precipitation_flux = "kg/m2/s" |
| 56 | + ) |
| 57 | + ) |
| 58 | + # dims |
| 59 | + expect_equal(res$time, seq(0, 365 + 3 / 4, 1 / 4) + 1 / 8, tolerance = 1e-12) |
| 60 | + expect_equal(unique(res$latitude), 45.25, tolerance = 1e-6) |
| 61 | + expect_equal(unique(res$longitude), -84.75, tolerance = 1e-6) |
| 62 | + expect_equal(mean(res$air_temperature), 278.798636, tolerance = 1e-6) |
| 63 | +}) |
| 64 | + |
| 65 | +# TODO test with more than one non-degenerate dimension |
0 commit comments