-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Potentially helpful functions? #1
Open
abbylewis
wants to merge
1
commit into
LTREB-reservoirs:main
Choose a base branch
from
abbylewis:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#' Convert units and names to match VERA targets and calculate daily output | ||
#' | ||
#' Output units: | ||
#' * air_temperature: C | ||
#' * relative_humidity: % | ||
#' * precipitation_flux: mm d-1 | ||
#' * wind_speed: m s-1 | ||
#' | ||
#' Output is daily mean for all variables except precipitation, which is daily sum | ||
#' | ||
#' @param df EFI standard df | ||
#' | ||
#' @return data frame | ||
#' @export | ||
convert_to_vera_met_P1D <- function(df){ | ||
weather_dat <- df |> | ||
# rename variables to match met station | ||
mutate(variable = ifelse(variable == "air_temperature", | ||
"AirTemp_C_mean", variable), | ||
prediction = ifelse(variable == "AirTemp_C_mean", | ||
prediction - 273.15, prediction), #Update units from K to C | ||
variable = ifelse(variable == "precipitation_flux", | ||
"Rain_mm_sum", variable), | ||
prediction = ifelse(variable == "Rain_mm_sum", | ||
prediction * 60 * 60, prediction), #Update units from kg/m2/s to mm/d | ||
variable = ifelse(variable == "relative_humidity", | ||
"RH_percent_mean", variable), | ||
prediction = ifelse(variable == "RH_percent_mean", | ||
prediction * 100, prediction), #Update units from proportion to % | ||
variable = ifelse(variable == "wind_speed", | ||
"WindSpeed_ms_mean", variable)) %>% | ||
mutate(datetime = as.Date(datetime)) %>% | ||
group_by_at(colnames(df)[colnames(df) != "prediction"]) %>% | ||
summarise(sum_pred = sum(prediction), | ||
prediction = mean(prediction, na.rm = T), | ||
.groups = "drop") %>% | ||
mutate(prediction = ifelse(variable == "Rain_mm_sum", sum_pred, prediction)) %>% | ||
select(-sum_pred) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
source("./R/convert_to_vera_met_P1D.R") | ||
|
||
#' load_met | ||
#' | ||
#' Loads historical and forecasted met data for a given site. | ||
#' | ||
#' @param forecast_date reference date for forecast generation | ||
#' @param forecast_days days into the future to forecast | ||
#' @param site site | ||
#' | ||
#' @return no return. Exports historical and future met | ||
#' | ||
load_met <- function(site, | ||
forecast_date, | ||
forecast_days = 35) { | ||
|
||
message("Loading met data for site ", site) | ||
|
||
#Stop if too many sites | ||
if(length(site) > 1) { | ||
stop("length(site) > 1. Only one site can be loaded at a time.") | ||
} | ||
|
||
#Specify variables | ||
variables <- c("relativehumidity_2m", | ||
"precipitation", | ||
"windspeed_10m", | ||
"temperature_2m") | ||
|
||
variables_renamed <- c("RH_percent_mean", | ||
"Rain_mm_sum", | ||
"WindSpeed_ms_mean", | ||
"AirTemp_C_mean") | ||
|
||
#Load sites | ||
site_list <- read_csv("https://raw.githubusercontent.com/LTREB-reservoirs/vera4cast/main/vera4cast_field_site_metadata.csv", | ||
show_col_types = FALSE) | ||
lat <- site_list$latitude[site_list$site_id == site] | ||
long <- site_list$longitude[site_list$site_id == site] | ||
if(!site %in% site_list$site_id){ | ||
stop("Site not found in site list") | ||
} | ||
|
||
#Weather predictions | ||
message("Loading weather predictions") | ||
weather_pred <- RopenMeteo::get_ensemble_forecast( | ||
latitude = lat, | ||
longitude = long, | ||
forecast_days = forecast_days, # days into the future | ||
past_days = 92, # past days that can be used for model fitting | ||
model = "gfs_seamless", # this is the NOAA gefs ensemble model | ||
variables = variables) |> | ||
# function to convert to EFI standard | ||
RopenMeteo::convert_to_efi_standard() |> | ||
# rename variables to match met station | ||
convert_to_vera_met_P1D() %>% | ||
mutate(site_id = site) | ||
|
||
message("Loading historical weather") | ||
weather_hist <- RopenMeteo::get_historical_weather( | ||
latitude = lat, | ||
longitude = long, | ||
start_date = as.Date("2010-01-01"), | ||
end_date = as.Date(Sys.Date()), | ||
variables = variables) |> | ||
# function to convert to EFI standard | ||
RopenMeteo::convert_to_efi_standard() |> | ||
# rename variables to match met station | ||
convert_to_vera_met_P1D() %>% | ||
mutate(site_id = site) | ||
|
||
message("Adjusting forecasts to match historical data") | ||
comparison_mod <- weather_hist %>% | ||
rename(hist_pred = prediction) %>% | ||
filter(!is.na(hist_pred)) %>% | ||
left_join(weather_pred, by = c("datetime", "variable")) %>% | ||
filter(!is.na(prediction)) %>% | ||
mutate(datetime = as.Date(datetime)) %>% | ||
group_by(datetime, variable) %>% | ||
summarize(future_sd = sd(prediction), | ||
future = mean(prediction), | ||
hist = unique(hist_pred), | ||
.groups = "drop") | ||
|
||
weather_pred_adjust <- weather_pred | ||
for(var in variables_renamed){ | ||
lm <- lm(future ~ hist, data = comparison_mod %>% filter(variable == var)) | ||
weather_pred_adjust <- weather_pred_adjust %>% | ||
mutate(prediction = ifelse(variable == var, | ||
prediction - lm$coefficients[1] + (1-lm$coefficients[2]) * prediction, | ||
prediction)) | ||
} | ||
|
||
#Filter to the future | ||
weather_pred_export <- weather_pred_adjust %>% | ||
filter(datetime >= forecast_date) %>% | ||
pivot_wider(names_from = variable, values_from = prediction) | ||
|
||
write.csv(weather_pred_export, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to return the data frame and let the user write the file format of their choice? or is there a specific reason for serialized to csv in the function? |
||
paste0("./met_downloads/future_daily_",site,"_", | ||
forecast_date,".csv"), | ||
row.names = F) | ||
|
||
write.csv(weather_hist %>% | ||
pivot_wider(names_from = variable, values_from = prediction), | ||
paste0("./met_downloads/past_daily_",site,"_", | ||
forecast_date,".csv"), | ||
row.names = F) | ||
return() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add
model
to the arguement so they can download a different open_meteo models