Open rburghol opened 2 years ago
Making river scenario vahydro_2022
copy_river_parameters.csh
copies some files, but omits others, such as the seemingly critical HYDR.csv
... need to understand this.
copy_river_parameters_all.csh
config/control/river/vadeq_2021.con
contains config PARAMETERS ... P620171001WQg ...END PARAMETERS
, which points to scenario P620171001WQg
, and the directory input/param/river/P620171001WQg
DOES have all the filesmkdir input/param/river/vahydro_2022
mkdir input/param/river/vahydro_2022/ftables
mkdir input/param/river/vahydro_2022/variable_ftables
# copy all segments for the James River
cbp copy_river_parameters.csh P620171001WQg vahydro_2022 J
This results in:
ls input/param/river/vahydro_2022/
NUTRX.csv OXRX.csv PLANK.csv RQUAL.csv SEDTRN.csv ftables variable_ftables
And FTABLES:
ls input/param/river/vahydro_2022/ftables/
JA0_7291_7290.ftable JL1_7080_7190.ftable JL6_6990_6960.ftable JU2_7140_7330.ftable
JA1_7600_7570.ftable JL1_7170_6800.ftable JL6_7150_6890.ftable JU2_7180_7380.ftable
JA1_7640_7280.ftable JL1_7190_7250.ftable JL6_7160_7440.ftable JU2_7360_7000.ftable
JA2_7290_0001.ftable JL1_7200_7250.ftable JL6_7320_7150.ftable JU2_7450_7360.ftable
JA2_7410_7470.ftable JL1_7530_7430.ftable JL6_7430_7320.ftable JU3_6380_6900.ftable
JA2_7550_7280.ftable JL2_6240_6520.ftable JL6_7440_7430.ftable JU3_6640_6790.ftable
JA2_7570_7480.ftable JL2_6440_6441.ftable JL7_6800_7070.ftable JU3_6650_7300.ftable
JA4_7280_7340.ftable JL2_6441_6520.ftable JL7_7030_6800.ftable JU3_6790_7260.ftable
JA4_7340_7470.ftable JL2_6850_6890.ftable JL7_7070_0001.ftable JU3_6950_7330.ftable
JA4_7470_7480.ftable JL2_7110_7120.ftable JL7_7100_7030.ftable JU3_7400_7510.ftable
JA5_7480_0001.ftable JL2_7120_6970.ftable JU1_6290_6590.ftable JU3_7490_7400.ftable
JB0_7051_0001.ftable JL2_7240_7350.ftable JU1_6300_6650.ftable JU4_7000_7300.ftable
JB0_7052_0001.ftable JL2_7250_7090.ftable JU1_6340_6650.ftable JU4_7260_7380.ftable
JB1_8090_0001.ftable JL2_7350_7090.ftable JU1_6590_6600.ftable JU4_7330_7000.ftable
JB2_7800_0001.ftable JL3_7020_7100.ftable JU1_6880_7260.ftable JU4_7380_7160.ftable
JB3_6820_7053.ftable JL3_7090_7150.ftable JU1_7560_7500.ftable JU5_7300_7510.ftable
JB3_7053_0001.ftable JL4_6520_6710.ftable JU1_7630_7490.ftable JU5_7420_7160.ftable
JL1_6560_6440.ftable JL4_6710_6740.ftable JU1_7690_7490.ftable JU5_7500_7420.ftable
JL1_6760_6910.ftable JL6_6740_7100.ftable JU1_7750_7560.ftable JU5_7510_7500.ftable
JL1_6770_6850.ftable JL6_6890_6990.ftable JU2_6410_6640.ftable
JL1_6910_6960.ftable JL6_6960_6970.ftable JU2_6600_6810.ftable
JL1_6940_7200.ftable JL6_6970_6740.ftable JU2_6810_6900.ftable
Add additional files left out by copy_river_parameters.csh
:
cbp copy_river_parameters_all.csh
cbp copy_river_parameters_all.csh src_scenario dest_scenario
cbp copy_river_parameters_all.csh P620171001WQg vahydro_2022
cp input/param/river/P620171001WQg/H* input/param/river/vahydro_2022/
cp input/param/river/P620171001WQg/H* input/param/river/vahydro_2022/
cp input/param/river/P620171001WQg/ADCALC.csv input/param/river/vahydro_2022/
cp input/param/river/P620171001WQg/SCRORG.csv input/param/river/vahydro_2022/
cp input/param/river/P620171001WQg/gen_info_rseg.csv input/param/river/vahydro_2022/
cp input/param/river/P620171001WQg/gen_info_rseg.csv input/param/river/vahydro_2022/
cp input/param/river/P620171001WQg/reservoir_rules input/param/river/vahydro_2022/ -Rfp
cp input/param/river/P620171001WQg/tau_percentiles.csv input/param/river/vahydro_2022/
Verify/Test FTABLE location.
cbp copy_river_parameters_all.csh P620171001WQg vahydro_2022
# edit config/control/river/vadeq_2021.con
# and set PARAMETERS = vahydro_2022
# now try to run_rug
cbp run_rug.csh vadeq_2021 PS2_5550_5560
# runs great! See result in http://deq1.bse.vt.edu:81/p6/vadeq/tmp/uci/river/vadeq_2021/PS2_5550_5560.uci
input/param/river/vahydro_2022/ftables/PS2_5550_5560.ftable
Create a Test Sub-watershed
cbp copy_scenario vadeq_2021 subsheds
cbp copy_river_parameters_all.csh vahydro_2022 subsheds
[scenario].con
file to point to the new PARAMETERS cbp add_trib_geo vadeq_2021 PS2_5550_5560 auto
(this function is TBD)cbp set_landuse_year
Code snippet to create a model/load an existing model:
riverseg<- RomFeature$new(
ds,
list(
hydrocode=paste('vahydrosw_wshed_',rseg_name, sep = ''),
ftype=rseg_ftype,
bundle='watershed'
),
TRUE
)
model <- RomProperty$new(
ds,
list(
varkey="om_model_element",
propname=riverseg$name,
featureid=riverseg$hydroid,
entity_type="dh_feature",
propcode=model_version
),
TRUE
)
model$save(TRUE)
PS2_5560_5100_linville_creek
Downstream: 5560 New ID: ? (1) choose random # , not pre-existing (2) +1 to downstream: 5561
Generate new ID = standalone script
Link to script: https://github.com/HARPgroup/HARParchive/blob/master/HARP-2022-Summer/AutomatedScripts/subsheds_naming.R
Use: .../subsheds_naming.R [subshed name] [path to save new name]
Ex: Rscript ~/HARParchive/HARP-2022-Summer/AutomatedScripts/subsheds_naming.R PS2_5560_5100_linville_creek ~/HARParchive/HARP-2022-Summer/AutomatedScripts/Subsheds
Result: PS2_5561_5560.txt with the content:
PS2_5561_5560
repeat {
if(ds %in% unique_ids) {
ds <- ds + 1
} else {
break
}
}
EU0_4120_0000
, where ds = 4120 resulted as ds = 4126 after the loop, since there are subsheds 4120 to 4125
EDITS:
The repeat loop mentioned before now looks like this:
repeat {
if(length(as.numeric(unique_ids)) >= 9998) {
print('No unique names available.')
break #script stops here and will not write a new name
}
if(sub_id %in% unique_ids) {
sub_id <- sub_id + 1
}
if(sub_id > 9999) {
sub_id <- 0002
}
else {
sub_id <- formatC(sub_id, width = 4, format = "d", flag = "0") #make sure new id stays 4 digits
new_name <- paste(splits[[1]][[1]],sub_id, downstr, sep = "_")
row <- data.frame(new_name, wordname)
colnames(row) <- colnames(full_list)
new_list <- rbind(head(full_list,-1), row)
new_list <- new_list[order(new_list$river), ]
end <- data.frame(tail(full_list,1))
new_list <- rbind(new_list, end)
write.table(new_list,
file=list,
append = TRUE,
quote = FALSE,
sep = ",",
row.names = FALSE,
col.names = TRUE)
break
}
}
Note @megpritch @juliabruneau : line 1 (the header) must end with "***" not "...", as it causes the routine to bomb :)
Note @megpritch @juliabruneau : line 1 (the header) must end with "***" not "...", as it causes the routine to bomb :)
@rburghol there was difficulties in matching what was interpreted as in R. It reads in R as ... . It seems that the header remained as after this morning's testing, so can we assume that ... is still saved as ?
@megpritch @juliabruneau -- sorry, I changec that manually so that I could test some things. Gotta figure out a way to make that work :). I suggest setting the value of that first line, 2nd column to "name***", right before saving and see what happens.
Got it - we will include that!
/opt/model/meta_model/run_model hsp2_cbp6 subsheds PS2_5568_5560 auto river 04_ps_sep_div
Use: bash /subsheds_naming.bat [path to subshed list] [master csv] [model version]
Ex: bash HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/subsheds_naming.bat HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/subshed_riversegs.txt rivernames.csv cbp-6.0
Ex: OD3_8720_8900_ut_leatherwood
from text file
Result: OD3_8723_8720
In VAHydro:
- [ ] set path name of routing file for R routine
I'm not exactly sure what this means/if it is included in the batch script?
When trying to run the script in terminal to update our list of subshed names, I get Error: C stack usage 9428040 is too close to the limit
. Whenever this script is run in R, Rstudio freezes and the REST retrieval always takes 5-10 minutes because it is such a large dataset before we subset it into just subwatersheds. I think this may be related to the terminal error, but I'm not sure what I can change to fix it. For now, I am generating the subshed list in Rstudio and pushing to Github.
For reference, this is the whole script minus the setup:
rsegs <- RomFeature$new(ds,list(ftype='vahydro',bundle='watershed'),TRUE)
subshed_hydrocodes <- subset(rsegs$hydrocode, nchar(rsegs$hydrocode)>29)
subshed_hydrocodes <- data.frame(subshed_hydrocodes) #collect hydrocodes into data frame
#make hydrocodes into riverseg's only (for ftable_creation purposes)
subshed_riversegs <- gsub("vahydrosw_wshed_","",as.character(subshed_hydrocodes$subshed_hydrocodes))
subshed_riversegs <- data.frame(subshed_riversegs)
#write to csv
file <- paste(path, 'subshed_riversegs.txt', sep='')
write.table(subshed_riversegs, file = file, quote = FALSE, row.names = FALSE, col.names = FALSE)
@durelles Clearer comments have been added to the naming script and here is the jamboard we made while organizing the script:
The main steps in this Rscript:
I had a question about using the sqldf; I'm receiving an error when trying to use our argument for the river segment, instead of 'PS2_5560_5100' typed out. Can you notice an error in this:
receiving_landuses <- sqldf("select * from landuse_full where riverseg = 'main_seg'")
(main_seg = PS2_5560_5100)
Find watershed areas for all models of a given type:
library("sqldf")
library("stringr") #for str_remove()
library("hydrotools")
library("openmi.om")
# Load Libraries
basepath='/var/www/R';
site <- "http://deq1.bse.vt.edu/d.dh" #Specify the site of interest, either d.bet OR d.dh
source("/var/www/R/config.local.private");
source(paste(basepath,'config.R',sep='/'))
# get the DA, need to grab a model output first in order to ensure segments with a channel subcomp
# are included
#
# GET DA
df_area <- data.frame(
'model_version' = c('vahydro-1.0', 'vahydro-1.0', 'vahydro-1.0'),
'runid' = c('runid_11', '0.%20River%20Channel', 'local_channel'),
'runlabel' = c('QBaseline_2020', 'comp_da', 'subcomp_da'),
'metric' = c('Qbaseline', 'drainage_area', 'drainage_area')
)
da_data <- om_vahydro_metric_grid(
metric = metric, runids = df_area,
base_url = paste(site,'entity-model-prop-level-export',sep="/")
)
da_data <- sqldf(
"select pid, comp_da, subcomp_da, riverseg,
CASE
WHEN comp_da is null then subcomp_da
ELSE comp_da
END as da
from da_data
")
@rburghol
When trying to run the code you gave us we receive the error Error in httr::add_headers(HTTP_X_CSRF_TOKEN = token) : object 'token' not found
:
FYI @juliabruneau - 2 follow-ups:
set path name of routing file for R routine
: this means get GEO
in your bash script, and create the path to the routing file to pass to the R script so that the R script doesn't need to guess what the file path might be, like routing_file= config/catalog/geo/$GEO/rivernames.csv
stringr
package (which we have installed, so library('stringr')
will load it). Info: https://stringr.tidyverse.org/reference/str_interp.html
receiving_landuses <- sqldf(str_interp("select * from landuse_full where riverseg = '${main_seg}'"))
@megpritch -- Question are you running this from the deq2 machine or your local machine?
Maybe it is as simple as using a slightly newer syntax to load the datasource stuff. Could you try the following and let me know?
library("sqldf")
library("stringr") #for str_remove()
site <- "http://deq1.bse.vt.edu/d.dh" #Specify the site of interest, either d.bet OR d.dh
#----------------------------------------------
# Load Libraries
basepath='/var/www/R';
source(paste(basepath,'config.R',sep='/'))
instead of:
library("sqldf")
library("stringr") #for str_remove()
library("hydrotools")
library("openmi.om")
# Load Libraries
basepath='/var/www/R';
site <- "http://deq1.bse.vt.edu/d.dh" #Specify the site of interest, either d.bet OR d.dh
source("/var/www/R/config.local.private");
source(paste(basepath,'config.R',sep='/'))
@rburghol - I am running from my local machine. The chunk you just sent did not work either. Neither did our usual method:
# Setup
suppressPackageStartupMessages(library("hydrotools")) #needed to pull values from VAHydro
# Link data source (ds) to VAHydro
basepath='/var/www/R';
source("/var/www/R/config.R") #will need file in same folder/directory
ds <- RomDataSource$new(site, rest_uname = rest_uname)
ds$get_token(rest_pw)
However, every time we run we still get this:
reading from http://deq1.bse.vt.edu:81/d.dh
[1] "REST AUTH INFO HAS BEEN SUPPLIED"
[1] "RETRIEVING REST TOKEN"
[1] "Login attempt successful"
[1] "token = _3vBjqU1cGUXZSQWjl1RKwShbxbNZcPhqktSlrImLMQ"
>
So I am not sure why it cannot find the token.
Julia ran what you sent in the terminal and it works, but uses a different token :
token = 'wrRjwZK6jtr975TbkEmwxBtEbW3ff_ICJ5PzMEFsrzM'
If we manually put this token in R then it works on the local machine as well.
Ahh @megpritch good debugging -- the function om_vahydro_metric_grid()
assumes that token
is a global variable, that is, token
is NOT passed in as an argument. That is poor coding practice, and honestly, I can't believe I haven't realized this by now.
The message that you showed:
[1] "Login attempt successful"
[1] "token = _3vBjqU1cGUXZSQWjl1RKwShbxbNZcPhqktSlrImLMQ"
Comes from the config.R
script, and sets the token
variable to allow secure conversations with the REST service. So it should be set. And also, whatever is the value of that token should be the value that would work for you when you type your token = 'wrRj...'
statement.
@jdkleiner -- I am drafting a new version of this function, instead of modifying the one that we have, We should send the ds
in as a param, and use the auth retrieval metric there. If the ds
is not sent in, it uses the old method (or tries) and tells the user that it is deprecated. It will require reinstalling hydrotools however... which isn't too painful. Steps:
Enter
when it asks if you want to update a bunch of ancillary packages.ds
add Script 2 into your code at the beginning after the line source("/var/www/R/config.R")
.ds = ds
to your argument that you pass into om_vahydro_metric_grid()
(Script 3).Script 1: Update Hydrotools.
library("devtools")
install_github("HARPgroup/hydro-tools")
Then add Script 2 into your code at the beginning.
Script 2: Load the datasource into variable ds
at beginning of scripts.
Finally, try this code
# add this after source("/var/www/R/config.R")
ds = RomDataSource$new(site, rest_uname)
ds$get_token(rest_pw)
Script 3: Add ds = ds
to the argument that you pass into om_vahydro_metric_grid()
.
da_data <- om_vahydro_metric_grid(
metric = metric, runids = df_area,
base_url = paste(site,'entity-model-prop-level-export',sep="/"),
ds = ds
)
Are you all able to move forward with the data, and you said it does work on deq2, so, we can at least tackle the next step?
@rburghol I did all the steps above, and received this error:
[1] "Found info"
model_version runid runlabel metric
1 vahydro-1.0 runid_11 QBaseline_2020 Qbaseline
[1] "retrieving http://deq1.bse.vt.edu:81/d.dh/entity-model-prop-level-export/all/dh_feature/watershed/vahydro/vahydro-1.0/runid_11/Qbaseline"
Error in vahydro_auth_read(url, token, ctype = "text/csv", delim = ",") :
object 'token' not found
Doing some testing here. First time thru works like a champ. Second time through has some trouble. One issue is that the old subwatershed does not get deleted, and the area calcs get squirrely. I am investigating.
proportion_landuse.R
(now area_propor.R
)Doing some testing here. First time thru works like a champ. Second time through has some trouble. One issue is that the old subwatershed does not get deleted, and the area calcs get squirrely. I am investigating.
Next:
copy_parent.R
area_propor.R
= works for proportioning land_use_2013VAHYDRO2018615.csv
and land_water_area.csv
SCRORG.csv
has a very different area value - not sure if it is drainage area? get_subsheds_riverseg.R
subsheds_naming.R
area_propor.R
copy_parent.R
Takes the main riverseg values and duplicates them for the subshed.
Use:
Rscript /copy_parent.R [file to edit] [subshed] [main_seg]
Example:
Rscript HARP-2022-Summer/AutomatedScripts/SubshedsCreation/copy_parent.R 'HARP-2022-Summer/AutomatedScripts/SubshedsCreation/wF180615RXAPXXXW_l2w.csv' 'PS2_5568_5560' 'PS2_5560_5100'
Files this works with:
Takes the main riverseg values and proportions them for the subshed.
Use:
Rscript /area_propor.R [file to edit] [subshed] [main_seg] [subshed_da] [columns_to_proportion]
Example:
Rscript HARP-2022-Summer/AutomatedScripts/SubshedsCreation/area_propor.R 'HARP-2022-Summer/AutomatedScripts/SubshedsCreation/land_water_area.csv' 'PS2_5568_5560' 'PS2_5560_5100' '46.04' '-1:-2'
Files this works with:
area_propor
, so this function may be applicable to SCRORG.csv , if we know which columns will be proportioned@megpritch @juliabruneau -- great work pushing this forward. Thanks also for putting up some example use/testing scripts. I will put mine below, as well. Let's get together on the batch script at your earliest convenience. To your questions: I think that SCRORG should also be done with the area weighting technique -- as noted, we don't actually use that (it is for organic matter scouring), it is just necessary to use some of the CBP routines that require it when analyzing hydrology. Also, septic should be handled specially wit ha copy statement. Both are described in the list at the top of the issue -- they are not perfect solutions, but they will work given that septic and sediment don't influence our hydrology, and these will be overwritten when the CBP simulates this in the future, so they are just a place holder to allow us to run hydrology.
My tests:
# copy the land_water_area file to the local directory for testing purposes
cp config/catalog/geo/vahydro/land_water_area.csv lwa.csv
# run the script for area 46.04
Rscript /opt/model/HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/proportion_landuse.R PS2_5560_5100 PS2_5568_5560 46.04 lwa.csv
# look for the receiving segment areas, to make sure no duplicates:
fgrep 5560_ lwa.csv
PS2_5560_5100,N51165,103656.746042785
PS2_5560_5100,N51171,57427.0449502788
PS2_5560_5100,N51660,497.759821436728
# look for the subwatershed segment areas, to make sure no duplicates:
fgrep 5568_ lwa.csv
PS2_5568_5560,N51165,18902.5801572155
PS2_5568_5560,N51171,10472.2496297213
PS2_5568_5560,N51660,90.7702130632719
# run the script for a new area:
Rscript /opt/model/HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/proportion_landuse.R PS2_5560_5100 PS2_5568_5560 25.1 lwa.csv
# look for the receiving segment areas, to make sure no duplicates:
fgrep 5560_ lwa.csv
PS2_5560_5100,N51165,112254.053351475
PS2_5560_5100,N51171,62190.0533613641
PS2_5560_5100,N51660,539.044101661422
# look for the subwatershed segment areas, to make sure no duplicates:
fgrep 5568_ lwa.csv
PS2_5568_5560,N51165,10305.2728485254
PS2_5568_5560,N51171,5709.24121863603
PS2_5568_5560,N51660,49.4859328385777
# go back to the original subwatershed area to insure that it is reversible:
Rscript /opt/model/HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/proportion_landuse.R PS2_5560_5100 PS2_5568_5560 46.04 lwa.csv
fgrep 5560_ lwa.csv
PS2_5560_5100,N51165,103656.746042785
PS2_5560_5100,N51171,57427.0449502789
PS2_5560_5100,N51660,497.759821436728
fgrep 5568_ lwa.csv
PS2_5568_5560,N51165,18902.5801572155
PS2_5568_5560,N51171,10472.2496297212
PS2_5568_5560,N51660,90.7702130632716
# now try with land use areas
subshed="PS2_5568_5560"
downstream="PS2_5560_5100"
darea=46.04
fgrep 5560_ $lu_file
PS2_5560_5100,N51165,1756.573852539,1543.499145508,3520.169189453,960.945739746,3001.164550781,2712.096435547,478.605255127,312.409790039,3272.076416016,93.118621826,232.714904785,953.464050293,13167.125976563,31907.837890625,103.151504517,212.606628418,1928.662066817,3122.345136404,170.526861668,960.980539322,8342.058493316,112.452880859,38.694198608,0.944061875,8.76594162,46.049324036,53.806312561,0,0,0,0,0,0,0,0,254.137008667,39939.290039063,11.329862595,43.119621277,2424.030373573,874.579755783,0,0,0
PS2_5560_5100,N51171,649.578063965,1515.272338867,2004.469360352,941.467163086,626.188842773,1383.432739258,244.13520813,574.1328125,335.051696777,73.389328003,169.168136597,973.683044434,6561.417480469,13924.020507813,19.79283905,46.106086731,1093.889055014,1431.119960785,146.281178117,505.34324193,2904.128371701,0,0,0,0,0,2.084458113,0,0,0,0,0,0,0,0,107.269371033,30199.81640625,17.853826523,19.131643295,864.975316048,566.09667635,0,0,0
PS2_5560_5100,N51660,1.07519424,3.665212154,1.136198282,5.175999641,1.657707453,6.216020584,1.096945405,1.806896925,0,0.131079838,0.327385128,1.336273193,8.666325569,16.483577728,0.084208697,0.174154788,29.797811508,3.61521244,0,0.829137743,18.563562393,41.381149292,92.106185913,0.349092931,12.327147484,176.085693359,4.279939651,0,0,0,0,0,0,0,0,0,129.676681519,0,0,21.864974976,8.620264053,0,0,0
Rscript /opt/model/p6/vadeq/run/resegment/area_propor.R $lu_file $subshed $downstream $darea '-1:-2'
# breaks.
@juliabruneau @megpritch Here is what I'm thinking for the batch script:
'-1:-2'
argument from our file. I love the idea that this can be flexible for landseg or riverseg only type things, but we don't want to be in the habit of passing in executable code fragments, and we should handle it more like "how many columns" should I match, 1 or 2 (or 3...)?add_sub_watershed.sh
into the folder where you all have created your other, scripts.Error in Proportion
subshed="PS2_5568_5560"
downstream="PS2_5560_5100"
darea=46.04
# using the scripts I copied
Rscript /opt/model/p6/vadeq/run/resegment/area_propor.R $lu_file $subshed $downstream $darea '-1:-2'
Error in `[.data.frame`(subsheds, cols) : undefined columns selected
Calls: area_propor -> [ -> [.data.frame
Execution halted
# using the original syntax
Rscript /opt/model/HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/area_propor.R '/opt/model/HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/land_water_area.csv' 'PS2_5568_5560' 'PS2_5560_5100' '46.04' ',-1:-2'
Error in `[.data.frame`(subsheds, cols) : undefined columns selected
Calls: area_propor -> [ -> [.data.frame
Execution halted
Proposed code for add_sub_watershed.sh
# we give our script:
# - hydrocode
# - receiving stream segment
# - drainage area of subshed
# - model version (cbp-6.0)
# - cbp scenario (subsheds)
# load name, rseg and area info
hydrocode=$1
downstream=$2
darea=$3
model_version=$4
scenario=$5
# get info
GEO=`cbp get_config subsheds river GEO`
# name it or retrieve the name if it already exists
read -r subshed downstream <<< "$(Rscript /opt/model/p6/vadeq/run/resegment/subsheds_naming.R PS2_5560_5100_linville_creek /opt/model/p6/vadeq/config/catalog/${GEO}/vahydro/rivernames.csv cbp-6.0)"
# set watershed area
Rscript /opt/model/p6/vadeq/run/resegment/area_propor.R /opt/model/p6/vadeq/config/catalog/${GEO}/vahydro/land_water_area.csv $subshed $downstream $darea
# set land use area
# iterate thru multiple lines if they exist (subsheds has one but others have more)
# see next comment for my bash work to iterate through multiple potential land use data sets
# copy transport stuff
Rscript /opt/model/p6/vadeq/input/param/transport/wF180615RXAPXXXW_l2w.csv $subshed $downstream
# ... repeat for other transport things
Iterate through multiple land use datasets
LANDUSE=`cbp get_config subsheds river 'LAND USE'`
# echo $LANDUSE
# 1984 07 01 2013VAHYDRO2018615 2055 07 01 2013VAHYDRO2018615
LANDUSE=`cbp get_config vadeq_2021 river 'LAND USE'`
echo $LANDUSE
# 1984 07 01 2013BASE20180615 2014 07 01 2013BASE20180615
- To iterate through, we know that each line has 4 values, year, month, day, and dataset name
- so we iterate through and know that every time we reach the 4th, 8th, etc element we have our dataset
cnt=0 for i in $LANDUSE; do ((cnt++)) if [[ $cnt -eq 1 ]]; then yr=$i fi if [[ $cnt -eq 4 ]]; then lu_file="input/scenario/river/land_use/landuse${i}.csv" echo "LU file for $yr = " $lu_file Rscript /opt/model/p6/vadeq/run/resegment/area_propor.R $lu_file $subshed $downstream $darea
# reset our counter
cnt=0
fi done
Thank you @rburghol for the work on the batch. Right off the bat, I noticed that we input drainage area manually as an argument; isn't there a way that we could extract it from get_subsheds_riverseg
, since we modified it to create the table of subsheds with da?
As below:
read -r subshed downstream <<< "$(Rscript ...
I'm now using the same strategy to extract drainage area from get_subsheds_riverseg.R
read -r darea <<< "$(Rscript /opt/model/p6/vadeq/run/resegment/get_subsheds_riverseg.R /opt/model/p6/vadeq/config/catalog/${GEO}/vahydro/)"
Please correct me if I understood that code wrong. I added cat(paste(darea))
into the Rscript in order to extract the correct value.
(@rburghol ) I created a copy of this file (area_propor_copy.R) in order to isolate where we are receiving the error, and based on my messages:
:~/HARParchive$ Rscript HARP-2022-Summer/AutomatedScripts/SubshedsCreation/area_propor_copy.R 'HARP-2022-Summer/AutomatedScripts/SubshedsCreation/land_water_area.csv' 'PS2_5568_5560' 'PS2_5560_5100' '46.04' '-1:-2'
Warning message:
no DISPLAY variable so Tk is not available
main segs and subsheds subset
row of zeros created
Error in `[.data.frame`(subsheds, , cols) : undefined columns selected
Calls: area_propor -> [ -> [.data.frame
Execution halted
We are getting an error only when run in the terminal, when calculating the sum of areas: sub_area <- sum(subsheds[cols])/640
.
I have tested on my local R and in R on the terminal, and errors do not occur at any point.
-1:-2
as an argument.@juliabruneau I am taking a look at get_subsheds_riverseg.R now, first time I have cracked it open and I will respond shortly!
@juliabruneau OK - we only want to run the command to get_subshed_riversegs.R
get the list of VAHydro-subshed models once saved as subshed_list.csv
, to get our list of sub-watersheds from VAHydro, but we will not be using that later on. So, that is why we want to input DA to our script, as we won't want to make that REST call every time we run the script, and also ,we want to have the ability to change drainage area on these models manually, as we move forward there won't be a vahydro-1.0 model for new segments that we create.
(@rburghol) After testing, the Rscript is working for both land_water_area.R and the land use files.
Ex:
juliasb@deq2:~/HARParchive$ Rscript HARP-2022-Summer/AutomatedScripts/SubshedsCreation/area_propor.R 'HARP-2022-Summer/AutomatedScripts/SubshedsCreation/land_water_area.csv' 'PS2_5568_5560' 'PS2_5560_5100' '46.04'
Warning message:
no DISPLAY variable so Tk is not available
proportioned the river segment and subshed
I will implement the changes in the batch now.
@juliabruneau good stuff. In order to verify that the thing does work, rather than just no errors, I would like to use the fgrep
statement from above in my tests with a clean file which can ensure no duplicates. I also want to replicate the tests where I proportion with one drainage area, then change that drainage area, then changed it back to ensure that it's reversible. They are back in this comment: https://github.com/HARPgroup/cbp_wsm/issues/85#issuecomment-1385672385
Per your question, there are at least two transport files that need to be dealt with, check the entries above. Every file that I listed above will ultimately have to be handled with one of our function routines. That means the transport files, the "hydro parameters" files, "land use" etc. Some of those WDM files will need to just be a copy from parent… I will try to put a checklist together when I have a minute, but they should be all up there in the bulleted list.
Testing the component scripts on all the files before adding to the bat:
darea=9.52
subshed="JL1_6562_6560"
downstream="JL1_6560_6440"
Rscript $CBP_ROOT/run/resegment/copy_parent.R $CBP_ROOT/input/param/river/${PARAMS}/HYDR.csv $subshed $downstream
created subshed containing parent values
fgrep JL1_6562_6560 input/param/river/${PARAMS}/HYDR.csv
# JL1_6562_6560,1,17.01,249.28,NA,0.5,NA,293.16,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
@rburghol I'm trying to perform testing on the batch script - script-by-script, and I ran into this hydrotools error:
Error: package or namespace load failed for ‘hydrotools’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
namespace ‘rlang’ 1.0.4 is already loaded, but >= 1.0.6 is required
How can I update hydrotools in my terminal? I tried in R in my terminal like we did before but it's not working.
Goal with fgrep
:
area_propor.R
- Proportioning done correctly (no duplicates), ex csv: land_water_area.csv
copy_parent.R
- Copying the parent done correctly (no duplicates), ex csv: gen_info_rseg.csv
Scenario 1. YM2_6120_6430_ni_reservoir
with da = 25.268455795755
/opt/model/p6/vadeq$ ./run/resegment/add_sub_watershed.bat YM2_6120_6430_ni_reservoir YM2_6120_6430 cbp-6.0 subsheds 25.268455795755
/opt/model/p6/vadeq$ fgrep YM2_6122_6120 config/catalog/geo/vahydro/rivernames.csv
YM2_6122_6120,Ni River Reservoir
/opt/model/p6/vadeq$ fgrep YM2_6122_6120 config/catalog/geo/vahydro/land_water_area.csv
YM2_6122_6120,N51033,3341.43704423947
YM2_6122_6120,N51137,318.983672820896
YM2_6122_6120,N51177,12511.3909922228
opt/model/p6/vadeq$ fgrep YM2_6120_6430 config/catalog/geo/vahydro/land_water_area.csv
YM2_6120_6430,N51033,30538.2575057605
YM2_6120_6430,N51137,2915.27430017911
YM2_6120_6430,N51177,114344.838707777
/opt/model/p6/vadeq$ fgrep YM2_6122_6120 input/param/river/vahydro_2022/gen_info_rseg.csv
YM2_6122_6120,3,0,S,60
/opt/model/p6/vadeq$ fgrep YM2_6120_6430 input/param/river/vahydro_2022/gen_info_rseg.csv
YM2_6120_6430,3,0,S,60
Scenario 2. YM2_6120_6430_ni_reservoir
with da = 20
/opt/model/p6/vadeq$ fgrep YM2_6122_6120 config/catalog/geo/vahydro/land_water_area.csv
YM2_6122_6120,N51033,2644.74969998034
YM2_6122_6120,N51137,252.475794642333
YM2_6122_6120,N51177,9902.77450537733
/opt/model/p6/vadeq$ fgrep YM2_6120_6430 config/catalog/geo/vahydro/land_water_area.csv
YM2_6120_6430,N51033,31234.9448500196
YM2_6120_6430,N51137,2981.78217835767
YM2_6120_6430,N51177,116953.455194622
/opt/model/p6/vadeq$ fgrep YM2_6122_6120 input/param/river/vahydro_2022/gen_info_rseg.csv
YM2_6122_6120,3,0,S,60
/opt/model/p6/vadeq$ fgrep YM2_6120_6430 input/param/river/vahydro_2022/gen_info_rseg.csv
YM2_6120_6430,3,0,S,60
Scenario 3. YM2_6120_6430_ni_reservoir
with da = 25.268455795755
same as in scenario 1.
/opt/model/p6/vadeq$ fgrep YM2_6122_6120 config/catalog/geo/vahydro/land_water_area.csv
YM2_6122_6120,N51033,3341.43704423948
YM2_6122_6120,N51137,318.983672820897
YM2_6122_6120,N51177,12511.3909922228
/opt/model/p6/vadeq$ fgrep YM2_6120_6430 config/catalog/geo/vahydro/land_water_area.csv
YM2_6120_6430,N51033,30538.2575057605
YM2_6120_6430,N51137,2915.27430017911
YM2_6120_6430,N51177,114344.838707777
/opt/model/p6/vadeq$ fgrep YM2_6122_6120 input/param/river/vahydro_2022/gen_info_rseg.csv
YM2_6122_6120,3,0,S,60
/opt/model/p6/vadeq$ fgrep YM2_6120_6430 input/param/river/vahydro_2022/gen_info_rseg.csv
YM2_6120_6430,3,0,S,60
Goal with fgrep:
rivernames.csv
area_propor.R
- Proportioning done correctly (no duplicates), ex csv: land_water_area.csv
copy_parent.R
- Copying the parent done correctly (no duplicates), ex csv: gen_info_rseg.csv
Norther River Segment: RU4_5640_6030_lake_pelham
with da = 26.15
/opt/model/p6/vadeq$ ./run/resegment/add_sub_watershed.bat RU4_5640_6030_lake_pelham RU4_5640_6030 cbp-6.0 subsheds 26.15
/opt/model/p6/vadeq$ fgrep RU4_5642_5640 config/catalog/geo/vahydro/rivernames.csv
RU4_5642_5640,Lake Pelham: Mountain Run
/opt/model/p6/vadeq$ fgrep RU4_5642_5640 config/catalog/geo/vahydro/land_water_area.csv
RU4_5642_5640,N51047,8209.21335628797
RU4_5642_5640,N51061,7183.42999536222
RU4_5642_5640,N51179,1343.35664834981
/opt/model/p6/vadeq$ fgrep RU4_5640_6030 config/catalog/geo/vahydro/land_water_area.csv
RU4_5640_6030,N51047,72415.502403712
RU4_5640_6030,N51061,63366.8135446378
RU4_5640_6030,N51179,11850.0814116502
/opt/model/p6/vadeq$ fgrep RU4_5642_5640 input/param/river/vahydro_2022/gen_info_rseg.csv
RU4_5642_5640,3,0,S,60
/opt/model/p6/vadeq$ fgrep RU4_5640_6030 input/param/river/vahydro_2022/gen_info_rseg.csv
RU4_5640_6030,3,0,S,60
./run/resegment/add_sub_watershed.bat [subshed hydrocode] [downstream rseg] cbp-6.0 subsheds [da]
Crooked Run: PS5_4380_4370_crooked_run
da: 47.04
Error: Initial REST results in -----This entity does not exist
instead of Total = 1
Black Creek Reservoir: JL1_7200_7250_black_creek_rsvr
da: 3.22
Error: another -----This entity does not exist
Beaver Creek: JL1_6560_6440_beaver_creek
da: 9.52
Error: same as Crooked Run/Black Creek
YL0_7010_0000_beaverdam
da: 17.57
Error: Ends with 000 - no parent ws to copy fromTinker: OR3_7740_8271_tinker
da: 12.9
Similar data.frame/tmp error to Crooked Run, but interesting because at least part of it was found on VAhydro:
Debugging: fgrep for the downstream river segment in the first file after generating the new subshed name: (Since that's where we wee the first error.)
/opt/model/p6/vadeq$ fgrep OR3_7740_8271 config/catalog/geo/vahydro/land_water_area.csv
OR3_7740_8271_catawba
da: 22.4
Error: same as TinkerLeatherwood: OD3_8720_8900_leatherwood
da: 68.6
Error: same as Tinker but with its respective rseg name "OD3_8722_8720"
Cranstons Mill: JB0_7050_0001_cranstons_mill
da: 6.99
Error: same as Tinker but with its respective rseg name
fgrep Routine: (re-stated just so it's easy to copy)
/opt/model/p6/vadeq$
:
./run/resegment/add_sub_watershed.bat [subshed hydrocode] [downstream rseg] cbp-6.0 subsheds [da]
Check for proportions/duplicates:
fgrep [new subshed rseg] config/catalog/geo/vahydro/rivernames.csv
fgrep [new subshed rseg] config/catalog/geo/vahydro/land_water_area.csv
fgrep [downstr rseg] config/catalog/geo/vahydro/land_water_area.csv
fgrep [new subshed rseg] input/param/river/vahydro_2022/gen_info_rseg.csv
fgrep [downstr rseg] input/param/river/vahydro_2022/gen_info_rseg.csv
RU4_5640_6030_lake_pelham
da: 26.15
YM2_6120_6430_ni_reservoir
da: 25.268455795755
YP3_6470_6690_spring_branch
da: 2
JL2_6440_6441_moormans_sugar_hollow
da: 17
JL6_6740_7100_cobbs_creek
da: 1.35
JU2_6600_6810_lbc
da: 27.23
PM7_4620_4580_broad_run
da: 39.577
JL2_6440_6441_buck_mtn_creek
da: 36
PS2_5560_5100_linville_creek
da: 46.04
YP3_6670_6720_mill_creek_meadows_pond
da: 6.99
Note: functional and documented here: #98
Overview
This will be used to create new sub-watersheds by sub-dividing old watersheds in the base CBP model domain.
This process is completed, with the final operational script detailed here: https://github.com/HARPgroup/cbp_wsm/issues/98
Tasks
subsheds
/opt/model/p6/vadeq/config/catalog/geo/vahydro
cbp get_config subsheds river GEO
vahydro-1.0
model to be our source datacbp-6.0
model or create it if it does not existriverseg
from your newcbp-6.0
modelcbp basingen.csh subsheds 5560
cat ./config/seglists/PS2_5560_5100.riv
cbp basingen.csh hsp2_2022 5560
and thencat ./config/seglists/PS2_5560_5100.riv
riverseg
on modelsubsheds
viaREST/ds
LEN
should be computed)LEN
cbp get_config subsheds river "LAND USE"
input/scenario/river/land_use/land_use_[LAND USE].csv
cbp basingen.csh subsheds 5560
(and check files in./config/seglists/PS2_5560_5100.land
)RU4_5640_6030_lake_pelham
Routing
Rscript /subsheds_naming.R [subshed name] [path to master list] [model version]
Rscript ~/HARParchive/HARP-2022-Summer/AutomatedScripts/subsheds_naming.R PS2_5560_5100_linville_creek /opt/model/p6/vadeq/config/catalog/geo/vahydro/rivernames.csv cbp-6.0
bash /subsheds_naming.bat [path to subshed list] [master csv] [model version]
bash HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/subsheds_naming.bat HARParchive/HARP-2022-Summer/AutomatedScripts/SubshedsCreation/subshed_riversegs.txt rivernames.csv cbp-6.0
basingen.csh
script accesses things to insure routingconfig/catalog/geo/[GEO]/rivernames.csv
config/catalog/geo/[GEO]/rivernames.csv
(see comment below on making test for river scenariovahydro_2022
)Hydro parameters:
PARAMETERS
block Point to files and datasets inconfig/control/river/[scenario].con
cbp copy_scenario src_scenario dest_scenario
cbp copy_river_parameters_all.csh src_scenario dest_scenario
ftables
input/param/river/vahydro_2022/ftables
input/param/river/[PARAMETERS]/ftables
ADCALC.csv
HYDR.csv
SCRORG.csv
wqinput/param/river/vahydro_2022/SCRORG.csv
gen_info_rseg.csv
input/param/river/vahydro_2022/gen_info_rseg.csv
transport
wqcbp get_config subsheds river TRANSPORT
input/param/transport/wF180615RXAPXXXW_l2w.csv
input/param/transport/wF180615RXAPXXXW_s2r.csv
input/param/transport/wF180615RXAPXXXW_res.csv
GEO
block Point to files and datasets inconfig/catalog/geo/[GEO]
river_met_wdm.csv
config/catalog/geo/vahydro/river_met_wdm.csv
river_prad_wdm.csv
config/catalog/geo/vahydro/river_prad_wdm.csv
land_water_area.csv
config/catalog/geo/vahydro/land_water_area.csv
LEN
,DELTH
,VOL
FTABLES
Land Use
LAND USE
(in[scenario].con
)WDM files
cbp get_config subsheds river DIV
DIV20160623_Y2020
cp config/blank_wdm/blank_ps_sep_div.wdm input/scenario/river/div/DIV20160623_Y2020/DIV_[riverseg].wdm
cp config/blank_wdm/blank_ps_sep_div.wdm input/scenario/river/div/DIV20160623_Y2020/DIV_PS2_5568_5560.wdm
cp input/scenario/river/septic/BASE20180615F_Y2020/sep_H51165_to_PS2_5550_5560.wdm input/scenario/river/septic/BASE20180615F_Y2020/sep_H51165_to_PS2_5568_5560.wdm
cp input/scenario/river/rib/BASE20180615F_Y2020/rib_H51165_to_PS2_5550_5560.wdm input/scenario/river/rib/BASE20180615F_Y2020/rib_H51165_to_PS2_5568_5560.wdm
cp input/scenario/river/rpaload/BASE20180615_Y2020/rpa_H51165_to_PS2_5550_5560.wdm input/scenario/river/rpaload/BASE20180615_Y2020/rpa_H51165_to_PS2_5568_5560.wdm
Testing