Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Dec 22, 2024
1 parent 847103e commit 0f29b13
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Literate
using Plots # to not capture precompilation output

# generate examples
EXAMPLE = joinpath(@__DIR__, "..", "examples", "CalibOneSite.jl")
EXAMPLE = joinpath(@__DIR__, "..", "examples", "Calib_site.jl")
OUTPUT = joinpath(@__DIR__, "src/generated")

preprocess(str) = replace(str, "x = 123" => "y = 321"; count=1)
Expand Down Expand Up @@ -72,7 +72,7 @@ makedocs(
# "customprocessing.md",
# "documenter.md",
# "tips.md",
"generated/CalibOneSite.md",
"generated/Calib_site.md",
# "changelog.md",
],
warnonly=true,
Expand Down
4 changes: 3 additions & 1 deletion examples/CalibOneIGBP.jl → examples/Calib_IGBP.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using PML, Ipaper, Test, RTableTools
df_out, df, par = deserialize(file_FLUXNET_CRO)

df_out, df, _par = deserialize(file_FLUXNET_CRO)
par = Param_PMLV2(; _par..., hc=0.5)
df.GPP_obs = df.GPPobs
df.ET_obs = df.ETobs
r = PMLV2(df; par)
Expand Down
10 changes: 7 additions & 3 deletions examples/CalibOneSite.jl → examples/Calib_site.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
using PML, Ipaper

#nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}}
df_out, df, par = deserialize(file_FLUXNET_CRO_USTwt)
df_out, df, _par = deserialize(file_FLUXNET_CRO_USTwt)
par = Param_PMLV2(; _par..., hc=0.5)
df.GPP_obs = df.GPPobs
df.ET_obs = df.ETobs

# ## 模型参数率定
parNames = [
, , :g1, :Am_25, :VPDmin, :VPDmax, :D0, :kQ, :kA, :S_sls, :fER0 # :hc
]

#nb # %% A slide [markdown] {"slideshow": {"slide_type": "fragment"}}
theta, goal, flag = ModelCalib(df, par0)
df_out = PMLV2_sites(df; par=theta2par(theta))
theta, goal, flag = ModelCalib(df, par0, parNames)
df_out = PMLV2_sites(df; par=theta2par(theta, parNames))
df_out[1:10, :]

# ## 拟合优度
Expand Down
20 changes: 4 additions & 16 deletions src/DataType.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interm_PML, output_PML
export to_mat;


@with_kw mutable struct interm_PML{T}
ET::T = 0.0
GPP::T = 0.0
Expand Down Expand Up @@ -74,19 +78,3 @@ function to_df(res::output_PML{T}) where {T<:Real}
data, names = to_mat(res)
DataFrame(data, names)
end

function map_df_tuple(fun::Function, lst::GroupedDataFrame{DataFrame}, args...; kw...)
n = length(lst)
_keys = keys(lst)
map(i -> begin
d = lst[i]
key = NamedTuple(_keys[i])
r = fun(d, args...; kw...)
(; key..., r...)
end, 1:n)
end


export interm_PML, output_PML
export to_mat;
export map_df_tuple
25 changes: 10 additions & 15 deletions src/ModelCalib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ export model_goal
export model_gof

include("DataType.jl")
# include("model_gof.jl")

function ModelCalib_IGBPs(data::AbstractDataFrame;
parNames=ParNames,
of_gof=:KGE, maxn=2500)
parNames=ParNames, of_gof=:KGE, maxn=2500)

vars = ["IGBPname", "IGBPcode", "site", "date", "GPP_obs", "ET_obs",
"Prcp", "Tavg", "U2", "Rn", "Rs", "VPD", "LAI", "Pa", "Ca"]
Expand All @@ -16,8 +14,7 @@ function ModelCalib_IGBPs(data::AbstractDataFrame;
@time params = par_map(IGBP -> begin
df = data[data.IGBP.==IGBP, vars]
IGBPcode = df.IGBPcode[1]
theta, _, _ = ModelCalib(df, par0, parNames; IGBPcode, of_gof, maxn, verbose=false)
theta
ModelCalib(df, par0, parNames; IGBPcode, of_gof, maxn, verbose=false)[1]
end, IGBPs; progress=false)

# printstyled("[i=$i, IGBP = $IGBP] \n", bold=true, color=:green, underline=false)
Expand Down Expand Up @@ -79,17 +76,15 @@ end

## gof of ET and GPP for ALL IGBP
function model_gof(df_out::DataFrame; all=true)
fun_et(d) = GOF(d.ET_obs, d.ET)
fun_gpp(d) = GOF(d.GPP_obs, d.GPP)

lst = groupby(df_out, :IGBP)

res = map_df_tuple(fun_et, lst)
all && push!(res, (; IGBP="ALL", fun_et(df_out)...))
gof_et = DataFrame(res)
fun_et(d) = GOF(d.ET_obs, d.ET)
fun_gpp(d) = GOF(d.GPP_obs, d.GPP)
func = (; ET=fun_et, GPP=fun_gpp)

res = map_df_tuple(fun_gpp, lst)
all && push!(res, (; IGBP="ALL", fun_gpp(df_out)...))
gof_gpp = DataFrame(res)
(; ET=gof_et, GPP=gof_gpp)
map(f -> begin
res = map_df_tuple(fun_et, lst)
all && push!(res, (; IGBP="ALL", fun_et(df_out)...))
DataFrame(res)
end, func)
end
4 changes: 2 additions & 2 deletions src/PML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ file_FLUXNET_CRO = "$dir_proj/data/CRO/FLUXNET_CRO" |> abspath
file_FLUXNET_CRO_USTwt = "$dir_proj/data/CRO/FLUXNET_CRO_US-Twt" |> abspath


include("main_Ipaper.jl")
include("Params.jl")
include("utilize.jl")
include("Parameter.jl")
include("ModelCalib.jl")
include("ET_helper.jl")
include("water_constrain.jl")
Expand Down
File renamed without changes.
15 changes: 14 additions & 1 deletion src/main_Ipaper.jl → src/utilize.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# export struct2vec, struct2tuple
export round2;
export movmean2, nanmean2, getDataType, replace_miss
export map_df_tuple

# rounded_data = NamedTuple((field => round(value) for (field, value) in data))
round2(x::NamedTuple, digits=3; kw...) = map(val -> round(val; digits), x)
Expand Down Expand Up @@ -47,7 +48,6 @@ function getDataType(x)
typeof(type) == Union ? type.b : type
end


# function struct2vec(x)
# keys = fieldnames(typeof(x))
# vals = [getfield(x, key) for key in keys]
Expand All @@ -59,4 +59,17 @@ end
# vals = [getfield(x, key) for key in keys]
# (; zip(keys, vals)...)
# end

weighted_mean(x::AbstractVector, w::AbstractVector) = sum(x .* w) / sum(w)


function map_df_tuple(fun::Function, lst::GroupedDataFrame{DataFrame}, args...; kw...)
n = length(lst)
_keys = keys(lst)
map(i -> begin
d = lst[i]
key = NamedTuple(_keys[i])
r = fun(d, args...; kw...)
(; key..., r...)
end, 1:n)
end

0 comments on commit 0f29b13

Please sign in to comment.