From 0f9422e15fa5c8cae4dac377d51a3dec65ab9dba Mon Sep 17 00:00:00 2001
From: Brian Groenke <brian.groenke@awi.de>
Date: Fri, 30 Jul 2021 11:50:23 +0200
Subject: [PATCH] Allow user to specify output directory when fetching forcing
 files

---
 src/IO/InputOutput.jl | 16 ++++++++++------
 src/Setup/Setup.jl    |  3 +++
 src/Setup/output.jl   |  2 --
 src/Setup/setup.jl    |  4 ----
 4 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/IO/InputOutput.jl b/src/IO/InputOutput.jl
index 84f12cdb..47163c69 100644
--- a/src/IO/InputOutput.jl
+++ b/src/IO/InputOutput.jl
@@ -7,11 +7,13 @@ using JSON3
 using Lazy: @>>
 using Unitful
 
+export loadforcings
+
 include("ioutils.jl")
 
 const INPUT_DIR = "input/"
-const FORCINGS_DIR = joinpath(INPUT_DIR, "forcings")
-const PARA_DIR = joinpath(INPUT_DIR, "para")
+const DEFAULT_FORCINGS_DIR = joinpath(INPUT_DIR, "forcings")
+const DEFAULT_PARA_DIR = joinpath(INPUT_DIR, "para")
 
 struct Resource
 	name::String
@@ -42,6 +44,10 @@ struct JsonSpec{Version} <: InputSpec end
 export InputSpec, JsonSpec
 
 """
+    loadforcings(filename::String, units...; spec::Type{T}=JsonSpec{1})
+    loadforcings(resource::Resource, units...; spec::Type{T}=JsonSpec{1}, outdir=DEFAULT_FORCINGS_DIR)
+    loadforcings(::Type{JsonSpec{1}}, filename::String, units::Pair{Symbol,<:Unitful.Units}...)
+
 Loads forcing data from the given file according to the format specified by `spec::InputSpec`. Default is JsonSpec{1}.
 Returns a NamedTuple of the form `(data=(...), timestamps=Array{DateTime,1})` where `data` is a NamedTuple matching
 the structure of the JSON file. Units can (and should) be supplied as additional pair arguments, e.g:
@@ -49,7 +55,7 @@ the structure of the JSON file. Units can (and should) be supplied as additional
 `loadforcings("example.json", :Tair=>u"°C", :Ptot=>u"mm")`
 """
 loadforcings(filename::String, units...; spec::Type{T}=JsonSpec{1}) where {T <: InputSpec} = loadforcings(T, filename, units...)
-loadforcings(resource::Resource, units...; spec::Type{T}=JsonSpec{1}) where {T <: InputSpec} = loadforcings(T, fetch(resource, FORCINGS_DIR), units...)
+loadforcings(resource::Resource, units...; spec::Type{T}=JsonSpec{1}, outdir=DEFAULT_FORCINGS_DIR) where {T <: InputSpec} = loadforcings(T, fetch(resource, outdir), units...)
 function loadforcings(::Type{JsonSpec{1}}, filename::String, units::Pair{Symbol,<:Unitful.Units}...)
         dict = open(filename, "r") do file; JSON3.read(file) end
         # convert JSON3 dict for data field to Julia dict
@@ -63,6 +69,4 @@ function loadforcings(::Type{JsonSpec{1}}, filename::String, units::Pair{Symbol,
         (data = NamedTuple{Tuple(keys(data))}(tuple(vals_with_units...)), timestamps = Array(ts))
 end
 
-export loadforcings
-
-end
\ No newline at end of file
+end
diff --git a/src/Setup/Setup.jl b/src/Setup/Setup.jl
index 55bb8afc..1cd7be95 100644
--- a/src/Setup/Setup.jl
+++ b/src/Setup/Setup.jl
@@ -6,6 +6,7 @@ import Zygote
 
 import CryoGrid.Interface: Top, Bottom
 
+using CryoGrid.InputOutput
 using CryoGrid.Interface
 using CryoGrid.Layers
 using CryoGrid.Numerics
@@ -28,6 +29,8 @@ using Reexport
 @reexport using OrdinaryDiffEq
 @reexport using SimulationLogs
 
+export CryoGridSetup, CryoGridOutput, withaxes, getstate, getvar
+
 include("stratigraphy.jl")
 include("setup.jl")
 include("problem.jl")
diff --git a/src/Setup/output.jl b/src/Setup/output.jl
index bc22c189..ed63d3bc 100644
--- a/src/Setup/output.jl
+++ b/src/Setup/output.jl
@@ -98,5 +98,3 @@ function Base.getproperty(out::CryoGridOutput, sym::Symbol)
         out.vars[sym]
     end
 end
-
-export CryoGridOutput, getvar
diff --git a/src/Setup/setup.jl b/src/Setup/setup.jl
index 9f18542f..c4a21905 100644
--- a/src/Setup/setup.jl
+++ b/src/Setup/setup.jl
@@ -16,8 +16,6 @@ struct CryoGridSetup{S,G,M,C,U,P}
         new{S,G,M,C,U,P}(strat,grid,meta,cache,uproto,pproto)
 end
 
-export CryoGridSetup
-
 """
 Constructs a `CryoGridSetup` from the given stratigraphy and grid. `arrayproto` keyword arg should be an array instance
 (of any arbitrary length, including zero, contents are ignored) that will determine the array type used for all state vectors.
@@ -168,8 +166,6 @@ Invokes `unconstrain` on all parameters in `p`. Assumes `p` to be of the same fo
 """
 Numerics.unconstrain(p::ComponentVector, setup::CryoGridSetup) = _apply_or_unapply_constraints(:unapply, p, setup)
 
-export withaxes, getstate, getvar
-
 """
 Generated step function (i.e. du/dt) for any arbitrary CryoGridSetup. Specialized code is generated and compiled
 on the fly via the @generated macro to ensure type stability. The generated code updates each layer in the stratigraphy
-- 
GitLab