Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Sep 24, 2024
1 parent 2f8258a commit 128110a
Show file tree
Hide file tree
Showing 18 changed files with 1,538 additions and 37 deletions.
File renamed without changes.
1,367 changes: 1,367 additions & 0 deletions docs/Manifest.toml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
PML = "6d0293b8-e749-48b5-896f-0ef93cd51027"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
86 changes: 86 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Documenter

if haskey(ENV, "GITHUB_ACTIONS")
ENV["JULIA_DEBUG"] = "Documenter"
end

deployconfig = Documenter.auto_detect_deploy_system()
Documenter.post_status(deployconfig; type="pending", repo="github.com/gee-hydro/PML.jl.git")
using Literate
using Plots # to not capture precompilation output

# generate examples
EXAMPLE = joinpath(@__DIR__, "..", "examples", "ex-calib_IGBP.jl")
OUTPUT = joinpath(@__DIR__, "src/generated")

function preprocess(str)
str = replace(str, "x = 123" => "y = 321"; count=1)
return str
end

Literate.markdown(EXAMPLE, OUTPUT, preprocess=preprocess)
Literate.notebook(EXAMPLE, OUTPUT, preprocess=preprocess)
Literate.script(EXAMPLE, OUTPUT, preprocess=preprocess)

# generate the example notebook for the documentation, keep in sync with outputformats.md
Literate.markdown(joinpath(@__DIR__, "src/outputformats.jl"), OUTPUT; credit=false, name="name")
Literate.notebook(joinpath(@__DIR__, "src/outputformats.jl"), OUTPUT; name="notebook")
Literate.script(joinpath(@__DIR__, "src/outputformats.jl"), OUTPUT; credit=false)

# Replace the link in outputformats.md
# since that page is not "literated"
if haskey(ENV, "GITHUB_ACTIONS")
folder = Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
Documenter.deploy_folder(
deployconfig;
repo="github.com/gee-hydro/PML.jl.git",
devbranch="master",
push_preview=true,
devurl="dev",
).subfolder
end
url = "https://nbviewer.jupyter.org/github/gee-hydro/PML.jl/blob/gh-pages/$(folder)/"
str = read(joinpath(@__DIR__, "src/outputformats.md"), String)
str = replace(str, "[notebook.ipynb](generated/notebook.ipynb)." => "[notebook.ipynb]($(url)generated/notebook.ipynb).")
write(joinpath(@__DIR__, "src/outputformats.md"), str)
end

# # Generate changelog
# using Changelog
# clog = joinpath(@__DIR__, "src", "changelog.md")
# Changelog.generate(
# Changelog.Documenter(),
# joinpath(@__DIR__, "..", "CHANGELOG.md"),
# clog;
# repo="gee-hydro/PML.jl",
# )
# write(clog, replace(read(clog, String), r"^# Literate.jl changelog"m => "# **9.** Changelog"))


makedocs(
format=Documenter.HTML(
assets=["assets/custom.css", "assets/favicon.ico"],
prettyurls=true, # haskey(ENV, "GITHUB_ACTIONS"),
canonical="https://fredrikekre.github.io/Literate.jl/v2",
),
modules=[Literate],
sitename="Literate.jl",
pages=Any[
# "index.md",
# "fileformat.md",
# "pipeline.md",
# "outputformats.md",
# "customprocessing.md",
# "documenter.md",
# "tips.md",
"generated/example.md",
# "changelog.md",
],
)

deploydocs(
repo="github.com/gee-hydro/PML.jl.git",
push_preview=true,
versions=["v2" => "v^", "v#.#", "dev" => "dev"],
deploy_config=deployconfig,
)
29 changes: 29 additions & 0 deletions docs/src/generated/ex-calib_IGBP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
```@meta
EditURL = "../../../examples/ex-calib_IGBP.jl"
```

````@example ex-calib_IGBP
using PML, Ipaper
dir_proj = "$(@__DIR__)/.."
df_out, df, par = deserialize("$dir_proj/data/CRO/FLUXNET_CRO")
df.GPP_obs = df.GPPobs
df.ET_obs = df.ETobs
````

r = PMLV2_sites(df; par)

````@example ex-calib_IGBP
theta, goal, flag = model_calib(df, par0)
df_out = PMLV2_sites(df; par=theta2par(theta))
gof = [
(; var="ET", GOF(df.ET_obs, df_out.ET)...),
(; var="GPP", GOF(df.GPP_obs, df_out.GPP)...)] |> DataFrame
DataFrame(gof)
````

---

*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*

File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions examples/ex-calib_IGBP.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using PML, Ipaper

df_out, df, par = deserialize(file_FLUXNET_CRO)
df.GPP_obs = df.GPPobs
df.ET_obs = df.ETobs

# r = PMLV2_sites(df; par)
theta, goal, flag = model_calib(df, par0)
df_out = PMLV2_sites(df; par=theta2par(theta))

gof = [
(; var="ET", GOF(df.ET_obs, df_out.ET)...),
(; var="GPP", GOF(df.GPP_obs, df_out.GPP)...)] |> DataFrame
DataFrame(gof)
File renamed without changes.
4 changes: 4 additions & 0 deletions examples/run.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using Literate


Literate.markdown("examples/README.jl", "."; flavor=Literate.CommonMarkFlavor())
File renamed without changes.
12 changes: 0 additions & 12 deletions scripts/calib_IGBP.jl

This file was deleted.

12 changes: 8 additions & 4 deletions src/PML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ module PML

export PMLV2_sites
export PMLV2, T_adjust_Vm25, f_VPD_Zhang2019
export GOF
export DataFrame, GOF
export file_FLUXNET_CRO

using DocStringExtensions
using Parameters, DataFrames
import HydroTools: cal_Uz, ET0_eq,
Cp, atm,
GOF, sceua
import HydroTools: cal_Uz, ET0_eq, Cp, atm, GOF, sceua

## global data
dir_proj = "$(@__DIR__)/.."
file_FLUXNET_CRO = "$dir_proj/data/CRO/FLUXNET_CRO" |> abspath


# lambda: [MJ kg-1]
W2mm(Ra; λ) = Ra * 86400 / 1e6 / λ
Expand Down
15 changes: 0 additions & 15 deletions src/Utilize/PMLV2_sites.jl

This file was deleted.

2 changes: 1 addition & 1 deletion src/Utilize/Utilize.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include("DataType.jl")
include("PMLV2_sites.jl")
# include("PMLV2_sites.jl")
include("model_calib.jl")
include("model_gof.jl")
6 changes: 4 additions & 2 deletions src/Utilize/model_calib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ end


## 最后一步,参数率定模块
"""
model_calib(df::AbstractDataFrame, par0::AbstractETParam;
IGBPcode=nothing, maxn=2500, of_gof=:NSE, kw...)
"""
function model_calib(df::AbstractDataFrame, par0::AbstractETParam; IGBPcode=nothing, maxn=2500, of_gof=:NSE, kw...)
parRanges = get_bounds(par0)
lower = parRanges[:, 1]
Expand All @@ -35,6 +39,4 @@ function model_calib(df::AbstractDataFrame, par0::AbstractETParam; IGBPcode=noth
theta0, lower, upper; maxn, kw...) # theta, goal, flag
end


# rounded_data = NamedTuple((field => round(value) for (field, value) in data))
export model_goal, model_calib
3 changes: 2 additions & 1 deletion src/main_Ipaper.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Statistics

# rounded_data = NamedTuple((field => round(value) for (field, value) in data))
round2(x::NamedTuple, digits=3; kw...) = map(val -> round(val; digits), x)


Expand All @@ -20,7 +21,7 @@ function movmean2(y::AbstractVector{T}, win_left::Integer, win_right::Integer=wi
end
z[i] = count > 0 ?/ count : T(NaN)
end
z
return z
end

function nanmean2(x::T1, y::T2) where {T1<:Real, T2<:Real}
Expand Down
20 changes: 18 additions & 2 deletions src/model_PMLV2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
- `U2` : m/s
- `LAI` : m2 m-2
- `Pa` : kPa
- `Ca` : ppm
- `Ω` : clamping index, default is 1.0
- `Ca` : ppm, default `380`
- `Ω` : clamping index, default is `1.0`
# Examples
```julia
Expand Down Expand Up @@ -120,3 +120,19 @@ function PMLV2(d::AbstractDataFrame; par::Param_PMLV2=Param_PMLV2(), kw...)
d.VPD, d.U2, d.LAI,
d.Pa, d.Ca; par, kw...) |> to_df
end

# 相同植被类型多个站点一起的计算
function PMLV2_sites(df::AbstractDataFrame; par::Param_PMLV2=Param_PMLV2(), kw...)
sites = df.site
grps = unique(sites)

# ! 这里有优化空间,但程序会写的很复杂
res = []
for grp in grps
inds = sites .== grp
d = df[inds, :]
r = PMLV2(d; par, kw...)
push!(res, r)
end
vcat(res...)
end

0 comments on commit 128110a

Please sign in to comment.