HARPgroup / HARParchive

This repo houses HARP code development items, resources, and intermediate work products.
1 stars 0 forks source link

Error when rendering NHD missing attribute `StreamOrde` #994

Closed rburghol closed 1 year ago

rburghol commented 1 year ago

FIXED - I updated from nhdplusTools 0.6.1 to 0.6.2 and this went away.

Run with the following (from #988):

rmarkdown::render(
    'C:/usr/local/home/git/HARParchive/HARP-2023-Summer/mapping_codeReview.Rmd',
    output_file ='C:/Workspace/tmp/james_rva_wsp.Rmd',
    params = list(
        rivseg = "JL7_7070_0001", locality = "NA", region = "Shenandoah_2",
        type = "facility", model_version = "vahydro-1.0", runid_list = c("runid_11", "runid_13"), 
        metric_type = "measured", metric= "wd_mgd",
        metric_type = "modeled", map_type = "basin", bbox_type = "auto"
    )
)

Error:

Quitting from lines 506-512 (mapping_codeReview.Rmd) 
Error in -flow$StreamOrde : invalid argument to unary operator
In addition: There were 50 or more warnings (use warnings() to see the first 50)

Printing the names of the nhd list, I get:

names(nhd)
[1] "plot_bbox"        "outlets"          "flowline"         "basin"           
[5] "catchment"        "network_wtbd"     "off_network_wtbd"

Fix:

glenncampagna commented 1 year ago

Interesting .. The StreamOrde column is within nhd$flowline which can be checked with

names(nhd$flowline)
  [1] "id"           "COMID"        "fdate"        "resolution"   "gnis_id"      "gnis_name"    "LENGTHKM"     "REACHCODE"    "flowdir"      "WBAREACOMI"  
 [11] "FTYPE"        "FCODE"        "shape_length" "streamleve"   "StreamOrde"   "StreamCalc"   "FromNode"     "ToNode"       "Hydroseq" etc.

The error being returned looks like it's coming from the labeling process outlined in fn_labelprep.R. Did the error come from the use of this function? These are the specific lines of that function that seem to be the cause:

flow <- nhd$flowline[nhd$flowline$gnis_name!=' ' & #name!=blank & order 4, 5, or 6
                                (nhd$flowline$StreamOrde==6 | nhd$flowline$StreamOrde==5 | nhd$flowline$StreamOrde==4),]
      ## no duplicate names; prioritize higher order names and then the longest segment of each duplicate
      flow <- flow[order(-flow$StreamOrde, flow$gnis_name, -flow$LENGTHKM) & !duplicated(flow$gnis_name),]

which have worked for us and is currently working for me both from the rmd and in the function itself, as long as the nhd object is created before the labeling function is called. Try regenerating the nhdobject and then executing fn_labelprep right after

rburghol commented 1 year ago

Thanks for the look @glenncampagna this is occurring when running the script via the rmarkdown command shown above.

rburghol commented 1 year ago

FYI - I updated from nhdplusTools 0.6.1 to 0.6.2 and this went away.