From f5a776c397b2746489218adc45483bdecbb71ca4 Mon Sep 17 00:00:00 2001 From: Mengqi Zhao Date: Tue, 9 Jan 2024 07:54:32 -0800 Subject: [PATCH] Update read_ncdf.R --- R/read_ncdf.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/read_ncdf.R b/R/read_ncdf.R index 9732cf5..379354d 100644 --- a/R/read_ncdf.R +++ b/R/read_ncdf.R @@ -27,9 +27,6 @@ read_ncdf <- function(ncdf = NULL, ncdf_in <- ncdf4::nc_open(ncdf) var_names <- attributes(ncdf_in$var)$names - # get unit - var_unit <- ncdf4::ncatt_get(ncdf_in, var_names)$unit - dims <- c(ncdf_in$var[[var]]$dim[[1]]$name, ncdf_in$var[[var]]$dim[[2]]$name, ncdf_in$var[[var]]$dim[[3]]$name) @@ -39,6 +36,9 @@ read_ncdf <- function(ncdf = NULL, stop('Climate variable name is not valid. Please provide valid ncdf_var name.') } + # get unit + var_unit <- ncdf4::ncatt_get(ncdf_in, var)$unit + # get the data time series ncdf_brick <- raster::brick(ncdf, varname = var, ncdf = TRUE, dims = dims_order, stopIfNotEqualSpaced = F) @@ -120,9 +120,6 @@ read_ncdf <- function(ncdf = NULL, ncdf_in <- ncdf4::nc_open(ncdf) var_names <- attributes(ncdf_in$var)$names - # get unit - var_unit <- ncdf4::ncatt_get(ncdf_in, var_names)$unit - dims <- c(ncdf_in$var[[var]]$dim[[1]]$name, ncdf_in$var[[var]]$dim[[2]]$name, ncdf_in$var[[var]]$dim[[3]]$name) @@ -132,6 +129,9 @@ read_ncdf <- function(ncdf = NULL, stop('Climate variable name is not valid. Please provide valid ncdf_var name.') } + # get unit + var_unit <- ncdf4::ncatt_get(ncdf_in, var)$unit + ncdf_brick <- raster::brick(ncdf, varname = var, ncdf = TRUE, dims = dims_order, stopIfNotEqualSpaced = F)