Skip to content

Commit

Permalink
Update extractBlue
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriNiella committed Jan 9, 2025
1 parent d6dc111 commit fceac3f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
26 changes: 13 additions & 13 deletions R/dataDownload.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,50 @@ dataDownload <- function(type, year, month = NULL, dir, varname, quiet = TRUE) {
month <- 1:12
for (i in 1:length(month)) {
if (month[i] < 10) {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_0", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_0", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_0", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
} else {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
}
}
} else {
for (i in 1:length(month)) {
if (month[i] < 10) {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_0", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_0", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_0", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
} else {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
}
}
}
} else {
if (is.null(month)) {
month <- 1:6 # 2023 data goes to June!
month <- 1:12 # 2023 data now goes to december!
for (i in 1:length(month)) {
if (month[i] < 10) {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_0", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_0", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_0", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
} else {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
}
}
} else {
for (i in 1:length(month)) {
if (month[i] < 10) {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_0", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_0", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_0", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
} else {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/", varname, "_", year, "_", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", varname, "_", year, "_", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
}
Expand All @@ -90,23 +90,23 @@ dataDownload <- function(type, year, month = NULL, dir, varname, quiet = TRUE) {
month <- 1:12
for (i in 1:length(month)) {
if (month[i] < 10) {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_0", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_0", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_0", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
} else {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
}
}
} else {
for (i in 1:length(month)) {
if (month[i] < 10) {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_0", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_0", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_0", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
} else {
download.file(paste0("https://dapds00.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_", month[i], ".nc"),
download.file(paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/month/", varname, "_mth_", year, "_", month[i], ".nc"),
destfile = paste0(dir, "/", varname, "_", year, "_", month[i], ".nc"), mode = 'wb', quiet = quiet)
gc()
}
Expand Down
8 changes: 6 additions & 2 deletions R/extractBlue.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ extractBlue <- function(df, X, Y, datetime,
}
}
parallel::stopCluster(cl)
nc.bran$x <- as.numeric(nc.bran$x)
nc.bran$y <- as.numeric(nc.bran$y)
} else { # Not in parallel
if (verbose) {
message(paste("Downloading", env_var, "data:", min(dates), "|", max(dates)))
Expand Down Expand Up @@ -324,6 +326,8 @@ extractBlue <- function(df, X, Y, datetime,
if (verbose)
setTxtProgressBar(pb, i)
}
nc.bran$x <- as.numeric(nc.bran$x)
nc.bran$y <- as.numeric(nc.bran$y)
if (verbose)
close(pb)
}
Expand Down Expand Up @@ -369,11 +373,11 @@ extractBlue <- function(df, X, Y, datetime,
.combine = 'c',
.packages = c('foreach', 'geosphere')) %dopar% {
aux.nc <- subset(nc.bran, as.Date(Time) == index.day[i])
index.locs <- which(as.Date(df.run[,"date"]) == index.day[i])
index.locs <- which(as.Date(df.run[,datetime]) == index.day[i])
var.run <- foreach::foreach(ii = 1:length(index.locs),
.combine = 'c',
.packages = 'geosphere') %dopar% {
aux.nc$Distance <- as.numeric(geosphere::distm(y = c(df.run[index.locs[ii], "lon"], df.run[index.locs[ii], "lat"]),
aux.nc$Distance <- as.numeric(geosphere::distm(y = c(df.run[index.locs[ii], X], df.run[index.locs[ii], Y]),
x = aux.nc[,c("x","y")]
))
if (env_var %in% c("BRAN_cur", "BRAN_wind")) {
Expand Down
29 changes: 15 additions & 14 deletions R/remoteNCDF.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
remoteNCDF <- function(year, month, var, depth, lon.min, lon.max, lat.min, lat.max) {
options(dplyr.summarise.inform = FALSE)
# Create filename
if (month < 10) {
if (month < 10) {
filename = paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily/", var, "_", year, "_0", month, ".nc")
} else {
filename = paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily/", var, "_", year, "_", month, ".nc")
filename = paste0("https://thredds.nci.org.au/thredds/dodsC/gb6/BRAN/BRAN2020/daily", var, "_", year, "_", month, ".nc")
}
# Open file remotely
tryCatch(
Expand All @@ -38,7 +38,7 @@ remoteNCDF <- function(year, month, var, depth, lon.min, lon.max, lat.min, lat.m
bran_depth <- nc %>%
tidync::activate("st_ocean") %>%
tidync::hyper_tibble()
bran_depth$st_ocean <- bran_depth$st_ocean * -1
bran_depth$st_ocean <- as.numeric(bran_depth$st_ocean) * -1
if (depth > 0)
depth <- depth * -1
depth_layer <- which.min(abs(bran_depth$st_ocean - depth))
Expand Down Expand Up @@ -66,22 +66,23 @@ remoteNCDF <- function(year, month, var, depth, lon.min, lon.max, lat.min, lat.m
yt_ocean = yt_ocean > lat.min & yt_ocean < lat.max) %>%
tidync::hyper_tibble()
}
# Convert time variable
tunit <- ncmeta::nc_atts(filename, "Time") %>% dplyr::filter(name == "units")
tunit <- as.character(tunit$value)
time_parts <- RNetCDF::utcal.nc(tunit, df.nc$Time)
df.nc$Time <- ISOdatetime(time_parts[,"year"],
time_parts[,"month"],
time_parts[,"day"],
time_parts[,"hour"],
time_parts[,"minute"],
time_parts[,"second"])
# Convert time and depth variables
# tunit <- ncmeta::nc_atts(filename, "Time") %>% dplyr::filter(name == "units")
# tunit <- as.character(tunit$value)
# time_parts <- RNetCDF::utcal.nc(tunit, df.nc$Time)
# df.nc$Time <- ISOdatetime(time_parts[,"year"],
# time_parts[,"month"],
# time_parts[,"day"],
# time_parts[,"hour"],
# time_parts[,"minute"],
# time_parts[,"second"])
df.nc$Time <- as.POSIXct(df.nc$Time, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
if (var == "atm_flux_diag")
names(df.nc) <- c("u", "v", "x", "y", "Time")
if (var %in% c("ocean_eta_t", "ocean_mld"))
names(df.nc) <- c(var, "x", "y", "Time")
if (var %in% c("ocean_temp", "ocean_salt", "ocean_u", "ocean_v"))
names(df.nc) <- c(var, "x", "y", "depth", "Time")
names(df.nc) <- c(var, "x", "y", "Time")
if (var == "ocean_w") {
df.nc <- df.nc[-which(df.nc$sw_ocean > 200),] # Use only layers < 200 m
names(df.nc) <- c(var, "x", "y", "depth", "Time")
Expand Down

0 comments on commit fceac3f

Please sign in to comment.