Skip to content

Commit

Permalink
Merge pull request #27 from PALEOtoolkit/logging_tidyup
Browse files Browse the repository at this point in the history
Tidy up logging
  • Loading branch information
sjdaines authored Aug 31, 2023
2 parents 23b5c64 + a3d8e43 commit d6d3c37
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
24 changes: 14 additions & 10 deletions src/Forcings/COPSEForcings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,32 +321,36 @@ function setup_force_spreadsheet(m::PB.ReactionMethod, pars, (), cellrange::PB.A
attribute_name == :setup || return nothing

forcingfile = joinpath(pars.datafolder[], pars.datafile[])
@info "setup_force_spreadsheet ReactionForce_spreadsheet $(PB.fullname(rj)): "*
"loading $(pars.forcename[]) forcing from 'datafolder/datafile'='$(forcingfile)'"
rj.forcing_data = _read_xlsx(forcingfile, sheetname=pars.sheetname[])

io = IOBuffer()
println(io, "setup_force_spreadsheet ReactionForce_spreadsheet $(PB.fullname(rj)): ")
println(io, " loading $(pars.forcename[]) forcing from 'datafolder/datafile'='$(forcingfile)'")
rj.forcing_data = _read_xlsx(io, forcingfile, sheetname=pars.sheetname[])

sp_times = pars.timemultiplier[]*rj.forcing_data[:, pars.timecolumn[]]
@info " 'tforce' from $(pars.timemultiplier[]) * column $(pars.timecolumn[]) ($(names(rj.forcing_data)[pars.timecolumn[]]))"
println(io, " 'tforce' from $(pars.timemultiplier[]) * column $(pars.timecolumn[]) ($(names(rj.forcing_data)[pars.timecolumn[]]))")

sp_values = Float64.(rj.forcing_data[:, pars.datacolumn[]])
@info " '$(pars.forcename[])' from column $(pars.datacolumn[]) ($(names(rj.forcing_data)[pars.datacolumn[]]))"
println(io, " '$(pars.forcename[])' from column $(pars.datacolumn[]) ($(names(rj.forcing_data)[pars.datacolumn[]]))")

# sort in ascending time order
sp_perm = sortperm(sp_times)
rj.force_times = sp_times[sp_perm]
rj.force_values = sp_values[sp_perm]

extrap_past = isnan(pars.extrap_value_past[]) ? "earlist value in spreadsheet = $(first(rj.force_values))" : "'extrap_value_past' = $(pars.extrap_value_past[])"
@info " extrapolating out-of-range tforce < $(first(rj.force_times)) (yr) to $extrap_past"
println(io, " extrapolating out-of-range tforce < $(first(rj.force_times)) (yr) to $extrap_past")
extrap_future = isnan(pars.extrap_value_future[]) ? "latest value in spreadsheet = $(last(rj.force_values))" : "'extrap_value_future' = $(pars.extrap_value_future[])"
@info " extrapolating out-of-range tforce > $(last(rj.force_times)) (yr) to $extrap_future"
println(io, " extrapolating out-of-range tforce > $(last(rj.force_times)) (yr) to $extrap_future")

# create interpolation object
rj.interp_FORCE = Interpolations.LinearInterpolation(
rj.force_times, rj.force_values,
extrapolation_bc = Interpolations.Flat() # only used for extrap_value_past, future == NaN
)

@info String(take!(io))

return nothing
end

Expand All @@ -367,16 +371,16 @@ function do_force_spreadsheet(m::PB.ReactionMethod, pars, (var_tforce, var_FORCE
return nothing
end

function _read_xlsx(forcingfile; sheetname="Sheet1")
function _read_xlsx(io, forcingfile; sheetname="Sheet1")

@info " read_xlsx: spreadsheet $(forcingfile) sheet $(sheetname)"
println(io, " read_xlsx: spreadsheet $(forcingfile) sheet $(sheetname)")

xf = XLSX.readxlsx(forcingfile)

# Read using built-in iterator - assumes single header row, automatic type conversion
df = XLSX.eachtablerow(xf[sheetname]) |> DataFrames.DataFrame

@info " read_xlsx read $(DataFrames.describe(df))"
println(io, " read_xlsx read $(DataFrames.describe(df))")

return df
end
Expand Down
24 changes: 14 additions & 10 deletions src/Forcings/LipForcing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ end
#####################################################

"""
read_lips_xlsx(lipfile) -> LIPdata::NamedTuple
read_lips_xlsx(io, lipfile) -> LIPdata::NamedTuple
read LIP data from spreadsheet
"""
function read_lips_xlsx(lipfile)
function read_lips_xlsx(io, lipfile)

@info " read_lips_xlsx: spreadsheet $(lipfile)"
println(io, " read_lips_xlsx: spreadsheet $(lipfile)")

xf = XLSX.readxlsx(lipfile)
sh = xf["Sheet1"]

column_names = sh["A1:I1"]
@info " read_lips_xlsx: column_names: $(column_names)"
println(io, " read_lips_xlsx: column_names: $(column_names)")
expected_column_names = ["Age" "Name" "Type" "Continental areas" "All volumes" "CO2 release (min)" "CO2 release (max)" "Degassing duration" "Present day area"]
column_names == expected_column_names || error("spreadsheet column names don't match expected_column_names=", expected_column_names)

Expand Down Expand Up @@ -296,24 +296,28 @@ function setup_force_LIPs(m::PB.ReactionMethod, pars, (), cellrange::PB.Abstract

attribute_name == :setup || return nothing

io = IOBuffer()

lipfile = joinpath(pars.datafolder[], pars.datafile[])
@info "setup_force_LIPs! ReactionForce_LIPs: $(PB.fullname(rj)) loading LIP forcing from 'datafolder/datafile'='$(lipfile)'"
println(io, "setup_force_LIPs! ReactionForce_LIPs: $(PB.fullname(rj)) loading LIP forcing from 'datafolder/datafile'='$(lipfile)'")

rj.LIP_data = read_lips_xlsx(lipfile)
rj.LIP_data = read_lips_xlsx(io, lipfile)

lipkwargs = (smoothempl=pars.smoothempl[], )

rj.default_lambda = find_default_lambda(pars.present_day_CFB_area[], rj.LIP_data, lipkwargs )

@info " found default_lambda=$(rj.default_lambda) "*
"to match present_day_CFB_area = $(pars.present_day_CFB_area[]) km^2"
println(io, " found default_lambda=$(rj.default_lambda) "*
"to match present_day_CFB_area = $(pars.present_day_CFB_area[]) km^2")

co2releasefield = Symbol(pars.co2releasefield[])
@info " add LIP CO2 release from $(pars.co2releasefield[]) field"
println(io, " add LIP CO2 release from $(pars.co2releasefield[]) field")

empty!(rj.LIPs)
rj.LIPs = create_LIPs(rj.LIP_data, co2releasefield, rj.default_lambda; lipkwargs...)


@info String(take!(io))

return nothing
end

Expand Down

0 comments on commit d6d3c37

Please sign in to comment.