Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions R/extractBlue.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extractBlue <- function(df,
.parallel = FALSE,
.ncores = NULL,
verbose = TRUE) {

# Initial checks of parameters
if(!X %in% colnames(df)){stop("Cannot find X coordinate in dataset, provide column name where variable can be found")}
if(!Y %in% colnames(df)){stop("Cannot find Y coordinate in dataset, provide column name where variable can be found")}
Expand All @@ -125,13 +125,21 @@ extractBlue <- function(df,
stop("Please provide the extract_depth argument as numeric")
}
if(full_timeperiod) {
if(is.null(station_name))
stop("Please provide column with station names in the 'station_name' argument.")
if(is.null(station_name))
stop("Please provide column with station names in the 'station_name' argument.")
}
if (fill_gaps & is.null(buffer)) {
stop("Please provide a 'buffer' size to fill gaps.")

}

# Tranform longitudes because BRAN is 0-360
long_neg <- which(df[,X] < 0)
long_pos <- which(df[,X] > 0)
if (length(long_neg) > 0)
df[long_neg,X] <- df[long_neg,X] + 180.001
if (length(long_pos) > 0)
df[long_pos,X] <- df[long_pos,X] + 179.001

# Define spatial and temporal extent of data fetch:
data_details <- ext_find(
Expand All @@ -141,13 +149,13 @@ extractBlue <- function(df,
.datetime = datetime,
.full_timeperiod = full_timeperiod,
verbose = verbose
)
)

# Create URLs to open data remotely
var_urls <- remote_urls(input = data_details,
var_name = env_var,
verbose = TRUE)
var_name = env_var,
verbose = TRUE)

# Open data remotely
if(verbose){
if (.parallel) {
Expand All @@ -157,26 +165,33 @@ extractBlue <- function(df,
}
}
df_env <- remote_open(input = var_urls,
depth = extract_depth,
data_details = data_details,
.fill_gaps = fill_gaps,
.buffer = buffer,
env_buffer = env_buffer,
.parallel = .parallel,
.ncores = .ncores,
var_name = env_var)
depth = extract_depth,
data_details = data_details,
.fill_gaps = fill_gaps,
.buffer = buffer,
env_buffer = env_buffer,
.parallel = .parallel,
.ncores = .ncores,
var_name = env_var)

# Match detections
df_output <- locs_match(aux_df = df,
X = X,
Y = Y,
datetime = datetime,
aux_env = df_env,
full_timeperiod = full_timeperiod,
station_name = station_name,
var_name = env_var)

X = X,
Y = Y,
datetime = datetime,
aux_env = df_env,
full_timeperiod = full_timeperiod,
station_name = station_name,
var_name = env_var)

# Revert longitudes back to original to export in lat/lon
long_neg <- which(df_output[,X] < 180)
long_pos <- which(df_output[,X] > 180)
if (length(long_neg) > 0)
df_output[long_neg,X] <- df[long_neg,X] - 180.001
if (length(long_pos) > 0)
df_output[long_pos,X] <- df_output[long_pos,X] - 179.001

# Export
return(df_output)

}