Skip to content

Commit

Permalink
Merge pull request #74 from lilyclements/main
Browse files Browse the repository at this point in the history
update test for nc
  • Loading branch information
lilyclements authored Mar 10, 2025
2 parents eb1777b + af0a0ec commit 76e2c9a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
53 changes: 26 additions & 27 deletions R/nc_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ nc_get_dim_min_max <- function(nc, dimension, time_as_date = TRUE) {
if (units$hasatt && units$value == "julian_day") {
time_vals <- as.character(as.Date(vals, origin = structure(-2440588, class = "Date")))
} else {
pcict_time <- ncdf4.helpers::nc.get.time.series(nc, time.dim.name = dimension)
posixct_time <- PCICt::as.POSIXct.PCICt(pcict_time)
pcict_time <- get_nc_time_series(nc, time.dim.name = dimension)
posixct_time <- convert_pcict_to_posixct(pcict_time)
time_vals <- as.character(as.Date(posixct_time))
}
})
Expand Down Expand Up @@ -103,8 +103,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
warning("Cannot subset data when some dimension axes cannot be identified.")
start <- NA
count <- NA
}
else {
} else {
start <- c()
count <- c()
for(dim in c("X", "Y", "Z", "T", "S")) {
Expand All @@ -120,28 +119,24 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
if(units$hasatt && units$value == "julian_day") {
# RDotNet interprets Date class as numeric so character needed to preserve date
time_vals <- as.Date(curr_dim_values, origin = structure(-2440588, class = "Date"))
}
else {
pcict_time <- ncdf4.helpers::nc.get.time.series(nc, time.dim.name = dim_var)
posixct_time <- PCICt::as.POSIXct.PCICt(pcict_time)
} else {
pcict_time <- get_nc_time_series(nc, time.dim.name = dim_var)
posixct_time <- convert_pcict_to_posixct(pcict_time)
time_vals <- as.Date(posixct_time)
}
ind <- which(time_vals >= boundary[[dim_var]][[1]] & time_vals <= boundary[[dim_var]][[2]])
})
}
else ind <- which(curr_dim_values >= boundary[[dim_var]][1] & curr_dim_values <= boundary[[dim_var]][2])
} else ind <- which(curr_dim_values >= boundary[[dim_var]][1] & curr_dim_values <= boundary[[dim_var]][2])
# TODO This is temporary solution for when there is only one value for a dimension and there are rounding difference
if(length(ind) == 0 && length(curr_dim_values) == 1 && round(curr_dim_values, 3) == round(boundary[[dim_var]][1], 3) && round(curr_dim_values, 3) == round(boundary[[dim_var]][2], 3)) ind <- 1
if(length(ind) == 0) {
stop("No values within the range specified for ", dim_var, ".")
}
else {
} else {
start <- c(start, min(ind))
count <- c(count, length(ind))
dim_values[[dim_var]] <- dim_values[[dim_var]][ind]
}
}
else {
} else {
start <- c(start, 1)
count <- c(count, length(curr_dim_values))
}
Expand All @@ -152,8 +147,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
count <- rep(-1, length(dim_axes))
}
}
}
else {
} else {
start <- rep(1, length(dim_axes))
count <- rep(-1, length(dim_axes))
}
Expand Down Expand Up @@ -192,8 +186,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
count_list[[i]] <- curr_count
dim_values_list[[i]] <- curr_dim_values
}
}
else {
} else {
start_list[[1]] <- start
count_list[[1]] <- count
dim_values_list[[1]] <- dim_values
Expand All @@ -214,8 +207,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
curr_dim_names <- get_nc_dim_names(nc, var)
if(!setequal(curr_dim_names, dim_names)) {
warning("The dimensions of", var, "do not match the other variables.", var, "will be dropped.")
}
else {
} else {
included_vars <- c(included_vars, var)
curr_var_data[[var]] <- as.vector(get_ncvar_values(nc, var, start = start_list[[i]], count = count_list[[i]]))
}
Expand All @@ -234,11 +226,10 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
units <- get_nc_attribute(nc, time_var, "units")
if(units$hasatt && units$value == "julian_day") {
time_df[["date"]] <- as.Date(raw_time, origin = structure(-2440588, class = "Date"))
}
else {
pcict_time <- ncdf4.helpers::nc.get.time.series(nc, time.dim.name = time_var)
} else {
pcict_time <- get_nc_time_series(nc, time.dim.name = time_var)
pcict_time <- pcict_time[time_ind]
posixct_time <- PCICt::as.POSIXct.PCICt(pcict_time)
posixct_time <- convert_pcict_to_posixct(pcict_time)
time_df[["date"]] <- as.Date(posixct_time)
time_df[["datetime"]] <- posixct_time
}
Expand All @@ -251,9 +242,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
}
var_data_list[[i]] <- curr_var_data
}
if(length(var_data_list) > 1) {
var_data <- dplyr::bind_rows(var_data_list)
}
if(length(var_data_list) > 1) var_data <- dplyr::bind_rows(var_data_list)
else if(length(var_data_list) == 1) var_data <- var_data_list[[1]]
else var_data_list <- data.frame()

Expand Down Expand Up @@ -380,3 +369,13 @@ close_nc_file <- function(nc) {
process_nc_file <- function(nc, vars, keep_raw_time, include_metadata, boundary, lon_points, lat_points, id_points, show_requested_points, great_circle_dist) {
nc_as_data_frame(nc, vars, keep_raw_time, include_metadata, boundary, lon_points, lat_points, id_points, show_requested_points, great_circle_dist)
}

# Wrapper for getting time series
get_nc_time_series <- function(nc, time.dim.name) {
ncdf4.helpers::nc.get.time.series(nc, time.dim.name)
}

# Wrapper for converting PCICt time to POSIXct
convert_pcict_to_posixct <- function(pcict_time) {
PCICt::as.POSIXct.PCICt(pcict_time)
}
38 changes: 38 additions & 0 deletions tests/testthat/test-n_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,42 @@ test_that("multiple_nc_as_data_frame handles empty directory", {
result <- multiple_nc_as_data_frame("mock_path", vars = c("var1"))

expect_equal(nrow(result), 0) # Should return an empty data frame
})

test_that("nc_get_dim_min_max correctly converts time when time_as_date is TRUE", {
local_mocked_bindings(
# Mock NetCDF axes to mark "time" as a time dimension
get_nc_dim_axes = function(nc) list(time = "T"),

# Mock NetCDF attribute function to return time units as "julian_day"
get_nc_attribute = function(nc, dimension, attr) {
if (dimension == "time" && attr == "units") {
return(list(hasatt = TRUE, value = "julian_day"))
} else {
return(list(hasatt = FALSE))
}
},

# Mock NetCDF time series retrieval
get_nc_time_series = function(nc, time.dim.name) {
c(0, 1, 2, 3, 4, 5) # Simulating 6 days from origin
},

# Mock PCICt time conversion
convert_pcict_to_posixct = function(pcict_time) {
as.POSIXct("2000-01-01") + (pcict_time * 86400) # Convert days to POSIXct time
}
)

# Fake NetCDF structure
mock_nc <- list(
dim = list(time = list(vals = c(0, 1, 2, 3, 4, 5))) # Simulated time values
)

result <- nc_get_dim_min_max(mock_nc, "time", time_as_date = TRUE)

# Expected output: min and max dates converted from Julian
expected_dates <- as.character(as.Date(c(0, 5), origin = structure(-2440588, class = "Date")))

expect_equal(result, expected_dates) # Ensures conversion works correctly
})

0 comments on commit 76e2c9a

Please sign in to comment.