Skip to content

Commit 88eda28

Browse files
committed
move wrapper function to PEPRMT
1 parent ad6fdab commit 88eda28

3 files changed

Lines changed: 51 additions & 50 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ For more information about this file see also [Keep a Changelog](http://keepacha
3434
- Added `PEcAn.data.land::to_co2e()` for converting SOC change, CH4, and N2O to CO2-equivalent emissions using IPCC Global Warming Potential values.
3535
- Added `PEcAn.data.land::event_parquet_to_json` for generating PEcAn `event.json` files from well-formatted event parquet files, with support for ensembles of events.
3636
- Added statewide synthetic fertilization and compost amendment event workflows for CA ag parcels. Outputs share an ensemble naming so a downstream cleaner unions them into one fertilization event type for SIPNET.
37+
- Added `PEcAn.remote::get_EVI_HLS()` for loading harmonized landsat-sentinel data and calculating EVI
3738

3839
### Fixed
3940
- The median run's manifest row went in with the literal strings `"NA"` for pft and trait, which `read.csv` turns into real `NA`, so `read.sa.output` never matched the median quantile and `splinefun` silently dropped that knot. Sensitivity analysis output changes as a result: partial variances shift slightly, though rankings are unaffected in the cases checked.

models/peprmt/R/hls2model.PEPRMT.R

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,52 @@ hls2model.PEPRMT <- function(in.path, in.prefix, outfolder, start_date, end_date
5555
utils::write.table(out, out.file.full, quote = FALSE, sep = " ", row.names = FALSE, col.names = TRUE)
5656

5757
} # hls2model.PEPRMT
58+
59+
60+
#' Wrapper function for double log calculation
61+
#'
62+
#' @param df data frame
63+
#'
64+
#' @returns modeled evi on each date
65+
#' @export
66+
calc_curve_beck <- function(df) {
67+
year_to_run <- unique(lubridate::year(df$Date))
68+
if(length(year_to_run) >1){
69+
stop("EVI has to be calculated one year at a time")
70+
}
71+
# Add explicit NAs
72+
x <- df |>
73+
dplyr::mutate(Date = as.Date(Date),
74+
img_doy = lubridate::yday(Date)) |>
75+
dplyr::group_by(img_doy) |>
76+
dplyr::summarise(
77+
Date = min(Date),
78+
evi = mean(evi, na.rm = TRUE),
79+
.groups = "drop"
80+
) |>
81+
tidyr::complete(img_doy = 1:365)
82+
83+
# Run double log function
84+
fit <- FitDoubleLogBeck(x$evi, t = x$img_doy, hessian = T, ninit = 100)
85+
86+
# Format output
87+
out <- data.frame(
88+
param_name = names(fit$params),
89+
param_value = fit$params,
90+
stdError = fit$stdError
91+
)
92+
rownames(out) <- NULL
93+
94+
pred_df_beck <- data.frame(doy = rep(1:365)) |>
95+
dplyr::cross_join(out |>
96+
tidyr::pivot_wider(names_from = param_name,
97+
values_from = c(param_value, stdError))) |>
98+
dplyr::mutate(pred = param_value_mn + (param_value_mx - param_value_mn) *
99+
(1/(1 + exp(-param_value_rsp * (doy - param_value_sos))) +
100+
1/(1 + exp(param_value_rau * (doy - param_value_eos))))) |>
101+
dplyr::left_join(df |> dplyr::rename(doy = img_doy)) |>
102+
dplyr::mutate(method = "Beck",
103+
Date = as.Date(paste0(year_to_run,"-01-01"))+ lubridate::days(doy-1))
104+
105+
return(pred_df_beck)
106+
}

modules/data.remote/R/get_EVI_HLS.R

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -223,53 +223,4 @@ build_mask <- function(fmask, selected_bit_nums){
223223
mask <- mask | mask_temp
224224
}
225225
return(mask)
226-
}
227-
228-
229-
#' Wrapper function for double log calculation
230-
#'
231-
#' @param df data frame
232-
#'
233-
#' @returns modeled evi on each date
234-
235-
calc_curve_beck <- function(df) {
236-
year_to_run <- unique(lubridate::year(df$Date))
237-
if(length(year_to_run) >1){
238-
stop("EVI has to be calculated one year at a time")
239-
}
240-
# Add explicit NAs
241-
x <- df |>
242-
dplyr::mutate(Date = as.Date(Date),
243-
img_doy = lubridate::yday(Date)) |>
244-
dplyr::group_by(img_doy) |>
245-
dplyr::summarise(
246-
Date = min(Date),
247-
evi = mean(evi, na.rm = TRUE),
248-
.groups = "drop"
249-
) |>
250-
tidyr::complete(img_doy = 1:365)
251-
252-
# Run double log function
253-
fit <- FitDoubleLogBeck(x$evi, t = x$img_doy, hessian = T, ninit = 100)
254-
255-
# Format output
256-
out <- data.frame(
257-
param_name = names(fit$params),
258-
param_value = fit$params,
259-
stdError = fit$stdError
260-
)
261-
rownames(out) <- NULL
262-
263-
pred_df_beck <- data.frame(doy = rep(1:365)) |>
264-
dplyr::cross_join(out |>
265-
tidyr::pivot_wider(names_from = param_name,
266-
values_from = c(param_value, stdError))) |>
267-
dplyr::mutate(pred = param_value_mn + (param_value_mx - param_value_mn) *
268-
(1/(1 + exp(-param_value_rsp * (doy - param_value_sos))) +
269-
1/(1 + exp(param_value_rau * (doy - param_value_eos))))) |>
270-
dplyr::left_join(df |> dplyr::rename(doy = img_doy)) |>
271-
dplyr::mutate(method = "Beck",
272-
Date = as.Date(paste0(year_to_run,"-01-01"))+ lubridate::days(doy-1))
273-
274-
return(pred_df_beck)
275-
}
226+
}

0 commit comments

Comments
 (0)