DOI-USGS / lake-temperature-model-prep

Pipeline #1
Other
6 stars 13 forks source link

Incorporate solution to resample lake hypsography #293

Closed hcorson-dosch-usgs closed 2 years ago

hcorson-dosch-usgs commented 2 years ago

In testing the new lake-temperature-process-model GLM modeling pipeline, we discovered that, for some lakes, GLM model runs that initially failed later succeeded if we decreased the resolution of the hypsography by resampling it to 1-meter intervals. For each lake, the hypsography is pulled into lake-temperature-process-models via 7_config_merge/ou/nml_list.rds from this lake-temperature-model-prep pipeline.

While troubleshooting, we resampled the hypsography within the lake-temperature-process-models pipeline using approx():

#' @Title Resample incoming hypsography to 1-m intervals
#' @decription Resample the H and A values to 1-meter intervals; calc bsn_vals
#' @param H the vector of H (elevation) values from the lake-specific 
#' nml parameters loaded in from the nml_list.rds from lake-temperature-model-prep
#' @param A the vector of A (area) values from the lake-specific nml 
#' parameters loaded in from the nml_list.rds from lake-temperature-model-prep
#' @return a list containing H and A vectors and a bsn_vals scaler
resample_H_A <- function(H, A) {
  ha_df <- tibble(H = H,A = A) %>%
    arrange(H)

  # Resample hypso to 1-meter intervals
  # Add an additional row for the deepest (final) raw H value so we don’t 
  # end up with with a lake shallower than the lake depth param. 
  # Then remove duplicate rows if any exist, which they will if the
  # final H value from the raw H vector is an integer
  ha_df_resampled <- bind_rows(tibble(H=seq.int(floor(min(ha_df$H)), floor(max(ha_df$H))),
                  A=approx(ha_df$H, ha_df$A, xout=seq.int(floor(min(ha_df$H)), floor(max(ha_df$H))), rule=2)$y),
                  tibble(A=approx(H, A, xout=max(ha_df$H), rule=2)$y, H=max(ha_df$H))) %>%
                  distinct()

  c(ha_df_resampled, bsn_vals = nrow(ha_df_resampled))
}

However, since the hypsography is compiled here, it makes sense to include a more robust solution within this pipeline.

Jordan notes:

the method we use in lake-temp-prep should probably be a depth-specific resampling. I.e., if a lake is only 2m deep, we probably don't want to resample down to ints.

hcorson-dosch-usgs commented 2 years ago

Note -- Lindsay implemented resampling hypso code in the lake-temperature-out pipeline but did it in a slightly different way. Would be good to review when addressing this issue.