HARPgroup / hydro-tools

Houses sets of commonly used hydrologic tools
0 stars 0 forks source link

VWP_CIA_Summary.Rmd error in om_auth_read() #381

Closed gmahadwar closed 5 months ago

gmahadwar commented 1 year ago

Summary

Details

Error message

Error in fromJSON(fac_model_info) : json_str must be a character string

VWP_CIA_Summary.Rmd stops running on GM's machine at line 165

if (exists("json_obj_url")) { fac_obj_url <- paste(json_obj_url, fac.model$pid, sep="/") fac_model_info <- ds$auth_read(fac_obj_url, "text/json", "") fac_model_info <- fromJSON(fac_model_info) .....

GM's om_auth_read()

om_auth_read <- function(uri, token, ctype = "text/csv", delim=',', enc="xml") { rawdat <- httr::GET( uri, httr::add_headers(HTTP_X_CSRF_TOKEN = token), encode = enc, httr::content_type(ctype) ) if (httr::http_error(rawdat)) { return(FALSE) } cdat <- httr::content(rawdat, type = ctype) return(cdat) }

JK's om_auth_read()

I don't have a copy, but om_auth_read() hasn't changed since summer 2022, and I have updated hydrotools more recently than that

@rburghol

gmahadwar commented 11 months ago

Identify problem

fac_model_info -> FALSE ds$auth_read(fac_obj_url, "text/json", "") -> FALSE token -> Error: object 'token' not found reran basepath … ds$get_token(rest_pw) -> Login attempt successful

Run this for solution

token <- rest_token(site, token, rest_uname, rest_pw); 
om_auth_read(fac_obj_url, token, "text/json", "", "xml")
fac_model_info <- ds$auth_read(fac_obj_url, "text/json", "")

Now render and it will continue to work until you reopen Rstudio

gmahadwar commented 7 months ago

If error is in ~\GitHub\vahydro\R\OWS_summaries\model_run_brief.Rmd

If Error in om_get_rundata() or finfo

Quitting from lines 39-150 [setup] (model_run_brief.Rmd) Error in fn_get_runfile(): ! object 'finfo' not found

Solution: finfo <- fn_get_runfile_info(elid, runid, scenid, site=omsite)

Various trials: ds <- RomDataSource$new("http://deq1.bse.vt.edu:81/d.dh", rest_uname) omsite should be "http://deq1.bse.vt.edu:81/" pr_data <- om_get_rundata(elid, runid, omsite, FALSE) dat <- fn_get_runfile(elid, runid, 37, site=omsite, cached = FALSE) fn_get_runfile_info(elid, runid, scenid, site=omsite) finfo <- fn_get_runfile_info(elid, runid, scenid, site=omsite) pr_data <- om_get_rundata(elid, runid, omsite, FALSE)

If Error appears as: (elementid == -1) { : argument is of length zero Then above won't fix it. Solution: sites are all :81 not mix of dh and :81 ex: ds <- RomDataSource$new(site, rest_uname) (these changes are already pushed) So, just: Get on VPN, rerun till it connects

gmahadwar commented 5 months ago

2/14/24 attempts to solve instead of using the workaround above config.R vahydro 1.0 is overwriting hydrotools with an older fn_get_runfile via fn_get_rundata, causing finfo to be empty

# source(paste(hydro_tools,"VAHydro-2.0/rest_functions.R", sep = "/")); 
# source(paste(hydro_tools,"VAHydro-1.0/fn_vahydro-1.0.R", sep = "/")); 

In utils.R, swap lines so finfo is defined before host_site

  finfo = fn_get_runfile_info(elementid, runid, scenid, site)
  # set this for comparisons
  host_site <- paste0('http://',finfo$host)

Also om_get_rundata.R may need to be rerun

lilbrary(zoo)

There were a few other updates to the VWP_CIA_Summary itself here: https://github.com/HARPgroup/vahydro/pull/975

NOTE: This fix is merged and works for for VWP_CIA_Summary.Rmd, for COBrogan and gmahadwar!!! Also works for model_run_brief.Rmd, if you add library(zoo)

verified using:

rmarkdown::render(
    'C:/users/rnv55934/Documents/GitHub/vahydro/R/examples/VWP_CIA_Summary.Rmd', 
    output_file = '/WorkSpace/tmp/te_appalachia_v01test.docx', 
    params = list( 
        doc_title = "Technical Evaluation – Town of Appalachia VWP 23-2243", 
        rseg.hydroid = 68033, impoundment_fid=616177, fac.hydroid = 71608, fac_model_pid = 4828042,
        cu_decimals = 1, cu_threshold = c(-10.0, -20.0, -30.0),  
        runid.list = c("runid_201", "runid_401"), cu_base_runid = 0, 
        cu_pre_var = "Qout", cu_post_var = "Qout",
        preferred_runid = "runid_600", intake_stats_runid = 600,
        upstream_rseg_ids=c(), downstream_rseg_ids=c()
    )
)