Skip to content

Commit

Permalink
environment and example scirpt changes
Browse files Browse the repository at this point in the history
  • Loading branch information
achiang17 committed Dec 6, 2024
1 parent 2c9a56f commit 4c986af
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 51 deletions.
25 changes: 10 additions & 15 deletions examples/routing/gamma_IRF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,30 @@ using ClimaRivers
using JSON, Dates

# build hillslope model
shape = 1.5
timescale = 1.0
t_max_hs = 60.0
hillslope = MizurouteHillslopeV1{Float64}(shape, timescale, t_max_hs)
hillslope = MizurouteHillslopeV1{Float64}()

# build channel model
wave_velocity = 1.5 * 86400
diffusivity = 8000 * 86400
t_max_ch = 120.0
channel = MizurouteChannelV1{Float64}(wave_velocity, diffusivity, t_max_ch)
channel = MizurouteChannelV1{Float64}()

# build model
river_model = HillslopeChannelRiverModel(hillslope, channel)

# build static environment
routing_lv_basins_dir = "/groups/esm/achiang/ClimaRivers.jl/data/routing/routing_lvs/routing_lvs_lv05"
attributes_dir = "/groups/esm/achiang/ClimaRivers.jl/data/routing/attributes/attributes_lv05"
static_env = StaticEnvironment(routing_lv_basins_dir, attributes_dir)
graph_dict = JSON.parsefile(
"/groups/esm/achiang/ClimaRivers.jl/data/routing/graphs/graph_lv05.json",
)
static_env = StaticEnvironment(routing_lv_basins_dir, attributes_dir, graph_dict)

# build dynamic environment
timeseries_dir = "/groups/esm/achiang/ClimaRivers.jl/data/routing/timeseries/timeseries_lv05"
forcing_timeseries_dir = "/groups/esm/achiang/ClimaRivers.jl/data/routing/timeseries/timeseries_lv05"
output_dir = "/groups/esm/achiang/ClimaRivers.jl/data/routing/simulations/simulations_lv05/gamma_IRF"
dynamic_env = DynamicEnvironment(timeseries_dir, output_dir)
dynamic_env = DynamicEnvironment(forcing_timeseries_dir, output_dir)

# build environment
graph_dict = JSON.parsefile(
"/groups/esm/achiang/ClimaRivers.jl/data/routing/graphs/graph_lv05.json",
)
env = Environment(static_env, dynamic_env, graph_dict)

env = Environment(static_env, dynamic_env)

## evolutionary model, evolving a state over time
model_types = ["instant"]
Expand Down
30 changes: 12 additions & 18 deletions examples/routing/mini_gamma_IRF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,43 @@ using ClimaRivers
using JSON, Dates

# build hillslope model
shape = 1.5
timescale = 1.0
t_max_hs = 60.0
hillslope = MizurouteHillslopeV1{Float64}(shape, timescale, t_max_hs)
hillslope = MizurouteHillslopeV1{Float64}()

# build channel model
wave_velocity = 1.5 * 86400
diffusivity = 8000 * 86400
t_max_ch = 120.0
channel = MizurouteChannelV1{Float64}(wave_velocity, diffusivity, t_max_ch)
channel = MizurouteChannelV1{Float64}()

# build model
river_model = HillslopeChannelRiverModel(hillslope, channel)

# build static environment
# build environment
data_file_path = joinpath(@__DIR__,"..","..","mini_data","routing")

# build static environment
@info "reading data files from $(data_file_path)"
graph_dict = JSON.parsefile(
joinpath(data_file_path, "graphs","graph_lv05.json"),
)
routing_lv_basins_dir = joinpath(data_file_path, "routing_lvs","routing_lvs_lv05")
attributes_dir = joinpath(data_file_path, "attributes","attributes_lv05")
static_env = StaticEnvironment(routing_lv_basins_dir, attributes_dir)
static_env = StaticEnvironment(routing_lv_basins_dir, attributes_dir, graph_dict)

# build dynamic environment
timeseries_dir = joinpath(data_file_path, "timeseries","timeseries_lv05")
forcing_timeseries_dir = joinpath(data_file_path, "timeseries","timeseries_lv05")
output_dir = joinpath(data_file_path, "simulations","simulations_lv05","gamma_IRF")
@info "creating output"
if !isdir(output_dir)
mkpath(output_dir)
end
dynamic_env = DynamicEnvironment(timeseries_dir, output_dir)
dynamic_env = DynamicEnvironment(forcing_timeseries_dir, output_dir)

# build environment
graph_dict = JSON.parsefile(
joinpath(data_file_path, "graphs","graph_lv05.json"),
)
env = Environment(static_env, dynamic_env, graph_dict)
env = Environment(static_env, dynamic_env)

## evolutionary model, evolving a state over time
model_types = ["instant"]
model_type = model_types[1]

start_date = Date("1996-01-01", "yyyy-mm-dd")
end_date = Date("2014-12-31", "yyyy-mm-dd")
dates = collect(start_date:Day(1):end_date)

# streamflow = zeros(dates,basins)
hillslope_data = zeros(10, 10) # Replace with actual data once implemented
Expand Down
21 changes: 12 additions & 9 deletions src/Environments.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
# Contains the structs that define the static and dynamic environment that configures/forces the river model.
export StaticEnvironment, DynamicEnvironment, Environment

# Static Data Objects
struct StaticEnvironment
# static data objects
"Directory containing list of all basins"
routing_lv_basins_dir::String
"Directory containing attribute csv"
attributes_dir::String
"Basin mapping dictionary"
graph_dict::Dict
end

# Dynamic Data Objects
struct DynamicEnvironment
# timeseries objects
# date/time - index map
timeseries_dir::String
"Directory containing forcing timeseries csv"
forcing_timeseries_dir::String
"Directory to store simulation results"
output_dir::String
end

struct Environment{SE <: StaticEnvironment, DE <: DynamicEnvironment}
"Static data objects"
static_env::SE
dyn_env::DE
graph_dict::Dict
"Dynamic data objects"
dynamic_env::DE
end

# get_static(env, :name)
# get_dynamic(env, :name, time=date/time)
6 changes: 3 additions & 3 deletions src/MizurouteV1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct MizurouteHillslopeV1{FT <: AbstractFloat} <: AbstractHillslopeModel
shape::FT
"Timescale factor, θ [day]"
timescale::FT
"Max time"
"Max time (days)"
t_max::FT
end
# MizurouteHillslopeV1(x,y)
Expand All @@ -26,13 +26,13 @@ struct MizurouteChannelV1{FT <: AbstractFloat} <: AbstractChannelModel
wave_velocity::FT
"Diffusivity, D [m²/day] (adjusted)"
diffusivity::FT
"Max time"
"Max time (days)"
t_max::FT
end

function MizurouteChannelV1{FT}() where {FT <: AbstractFloat}
wave_velocity = FT(1.5 * 86400)
diffusivity = FT(8000 * 86400)
diffusivity = FT(800 * 86400)
t_max = FT(120.0)
return MizurouteChannelV1(wave_velocity, diffusivity, t_max)
end
Expand Down
13 changes: 7 additions & 6 deletions src/Routing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function update_state_from_hillslope!(
day_to_s = 86400
km²_to_m² = 1000000

timeseries_dir = env.dyn_env.timeseries_dir
output_dir = env.dyn_env.output_dir
forcing_timeseries_dir = env.dynamic_env.forcing_timeseries_dir
output_dir = env.dynamic_env.output_dir
dates = collect(start_date:Day(1):end_date)

routing_lv_basins_dir = env.static_env.routing_lv_basins_dir
Expand All @@ -82,7 +82,7 @@ function update_state_from_hillslope!(
# make new txt file
for basin_id in all_basin_ids # routing_lv_basins
timeseries_df =
CSV.read(joinpath(timeseries_dir, "basin_$basin_id.csv"), DataFrame)
CSV.read(joinpath(forcing_timeseries_dir, "basin_$basin_id.csv"), DataFrame)
filtered_df =
filter(row -> start_date <= row[:date] <= end_date, timeseries_df)
basin_area =
Expand Down Expand Up @@ -139,9 +139,10 @@ function update_state_from_channel!(

km_to_m = 1e3

output_dir = env.dyn_env.output_dir
timeseries_dir = env.dyn_env.timeseries_dir
output_dir = env.dynamic_env.output_dir
forcing_timeseries_dir = env.dynamic_env.forcing_timeseries_dir

graph_dict = env.static_env.graph_dict
routing_lv_basins_dir = env.static_env.routing_lv_basins_dir
attributes_df = CSV.read(
joinpath(env.static_env.attributes_dir, "attributes.csv"),
Expand All @@ -168,7 +169,7 @@ function update_state_from_channel!(

# Iterate over upstreams
upstream_basin_list =
get_upstream_basins(string(basin_id), env.graph_dict)
get_upstream_basins(string(basin_id), graph_dict)
for up_basin in upstream_basin_list
# Read DataFrame with inputs from upstream
up_timeseries_df = CSV.read(
Expand Down

0 comments on commit 4c986af

Please sign in to comment.