Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
237 lines (209 loc) · 9.2 KB
/
Copy pathapp.py
File metadata and controls
237 lines (209 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Canopy-SIP-SIF Model — Web Interface (Streamlit).
Interactive web UI for simulating canopy SIF anisotropy.
"""
import streamlit as st
import numpy as np
import plotly.graph_objects as go
st.set_page_config(page_title="Canopy-SIP-SIF Model", page_icon="🌿", layout="wide")
st.title("🌿 Canopy-SIP-SIF Model")
st.markdown(
"Simulates canopy **Solar-Induced chlorophyll Fluorescence (SIF)** anisotropy "
"using **Geometric-Optical (GO)** theory and **Spectral Invariants (p-theory)**."
)
# ── Sidebar: Parameters ─────────────────────────────────────────────────
with st.sidebar:
st.header("Model Parameters")
st.subheader("Sun Geometry")
SZA = st.slider("Sun Zenith Angle (SZA) [deg]", 0, 60, 20, 5)
SAA = st.slider("Sun Azimuth Angle (SAA) [deg]", 0, 360, 0, 10)
st.subheader("Canopy Structure")
LAI = st.slider("Leaf Area Index (LAI)", 1.0, 10.0, 5.0, 0.5)
Height = st.slider("Canopy Height [m]", 5.0, 40.0, 20.0, 1.0)
Crowndeepth = st.slider("Crown Depth [m]", 2.0, 25.0, 12.86, 0.5)
Height_c = st.slider("Crown Center Height [m]", 2.0, 20.0, 6.87, 0.5)
dthr = st.slider("Diameter to Height Ratio (dthr)", 0.1, 2.0, 0.86, 0.05,
help="Crown width / canopy height ratio")
HotSpotPar = st.slider("Hotspot Parameter", 0.001, 0.1, 0.02, 0.005,
help="Leaf-scale hotspot parameter")
D = st.slider("Diffuse Fraction (D)", 0.0, 1.0, 0.0, 0.05)
st.subheader("Leaf Angle Distribution")
TypeLidf = st.selectbox("LIDF Type", [2, 1],
format_func=lambda x: "Campbell (single-param)" if x == 2 else "Two-parameter")
if TypeLidf == 2:
LIDFa = st.slider("Average Leaf Angle [deg]", 10.0, 80.0, 57.3, 1.0,
help="57.3 = spherical, ~27 = planophile, ~63 = erectophile")
LIDFb = 0.0
else:
LIDFa = st.slider("LIDFa", -1.0, 1.0, -0.35, 0.05)
LIDFb = st.slider("LIDFb", -1.0, 1.0, -0.15, 0.05)
# ── Run simulation ──────────────────────────────────────────────────────
st.caption("Note: Each simulation may take a minute or more depending on the environment.")
if st.button("Run Simulation", type="primary", use_container_width=True):
with st.spinner("Running SIF simulation (this involves heavy computation)..."):
import time
t0 = time.perf_counter()
from canopy_sip_sif import run_simulation
result = run_simulation(
SZA=SZA, SAA=SAA, LAI=LAI,
Crowndeepth=Crowndeepth, Height=Height, Height_c=Height_c,
dthr=dthr, HotSpotPar=HotSpotPar,
D=D, TypeLidf=TypeLidf, LIDFa=LIDFa, LIDFb=LIDFb,
)
elapsed = time.perf_counter() - t0
st.session_state['result'] = result
st.session_state['params'] = {'SZA': SZA, 'LAI': LAI}
st.session_state['elapsed'] = elapsed
# ── Display results ─────────────────────────────────────────────────────
if 'result' in st.session_state:
result = st.session_state['result']
params = st.session_state['params']
wlf = result['wlf']
SRTE_all = result['SRTE_Fs_fdir_all']
SRTE_ps1 = result['SRTE_Fs_fdir1']
SRTE_ps2 = result['SRTE_Fs_fdir2']
nadir_idx = 12
col1, col2 = st.columns([2, 1])
with col1:
st.subheader("Nadir SIF Spectrum")
fig = go.Figure()
fig.add_trace(go.Scatter(
x=wlf, y=SRTE_all[:, nadir_idx],
mode='lines', name='Total SIF',
line=dict(color='red', width=2.5),
))
fig.add_trace(go.Scatter(
x=wlf, y=SRTE_ps1[:, nadir_idx],
mode='lines', name='PS I',
line=dict(color='blue', width=1.5, dash='dash'),
))
fig.add_trace(go.Scatter(
x=wlf, y=SRTE_ps2[:, nadir_idx],
mode='lines', name='PS II',
line=dict(color='green', width=1.5, dash='dash'),
))
fig.update_layout(
xaxis_title="Wavelength (nm)",
yaxis_title="SIF Radiance (W m⁻² sr⁻¹ μm⁻¹)",
title=f"Canopy SIF — SZA={params['SZA']}°, LAI={params['LAI']}",
xaxis=dict(range=[640, 850]),
template="plotly_white",
height=500,
legend=dict(x=0.7, y=0.95),
)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("SIF Values at Key Wavelengths")
idx_687 = np.argmin(np.abs(wlf - 687))
idx_740 = np.argmin(np.abs(wlf - 740))
import pandas as pd
df_key = pd.DataFrame({
'Wavelength': ['687 nm', '740 nm', 'Peak'],
'Total SIF': [
f"{SRTE_all[idx_687, nadir_idx]:.6f}",
f"{SRTE_all[idx_740, nadir_idx]:.6f}",
f"{SRTE_all[:, nadir_idx].max():.6f}",
],
'PS I': [
f"{SRTE_ps1[idx_687, nadir_idx]:.6f}",
f"{SRTE_ps1[idx_740, nadir_idx]:.6f}",
f"{SRTE_ps1[:, nadir_idx].max():.6f}",
],
'PS II': [
f"{SRTE_ps2[idx_687, nadir_idx]:.6f}",
f"{SRTE_ps2[idx_740, nadir_idx]:.6f}",
f"{SRTE_ps2[:, nadir_idx].max():.6f}",
],
})
st.dataframe(df_key, use_container_width=True, hide_index=True)
# Download CSV
df_full = pd.DataFrame({
'Wavelength (nm)': wlf,
'Total SIF': np.round(SRTE_all[:, nadir_idx], 8),
'PS I': np.round(SRTE_ps1[:, nadir_idx], 8),
'PS II': np.round(SRTE_ps2[:, nadir_idx], 8),
})
csv = df_full.to_csv(index=False)
st.download_button("Download Nadir SIF CSV", csv, "SIF_nadir_results.csv", "text/csv")
# Angular dependence plot
st.subheader("SIF Angular Dependence (at 740 nm)")
va = result['va']
signed_vza = np.where(va[:, 1] < 90, -va[:, 0], va[:, 0])
fig2 = go.Figure()
fig2.add_trace(go.Scatter(
x=signed_vza, y=SRTE_all[idx_740, :],
mode='lines+markers',
marker=dict(size=6, color='red'),
line=dict(color='red', width=2),
name='Total SIF at 740nm'
))
fig2.update_layout(
xaxis_title="View Zenith Angle (deg)",
yaxis_title="SIF at 740nm (W m⁻² sr⁻¹ μm⁻¹)",
title="SIF Angular Distribution in the Principal Plane",
xaxis=dict(range=[-65, 65], dtick=20),
template="plotly_white",
height=400,
)
st.plotly_chart(fig2, use_container_width=True)
# Polar axis plot (semicircular fan for principal plane)
st.subheader("SIF Polar Plot (Principal Plane)")
# Map signed VZA to polar angle: theta = 90 - signed_VZA
# Forward -60° → 150° (upper-left), Nadir 0° → 90° (top), Backward +60° → 30° (upper-right)
theta_polar = 90.0 - signed_vza
r_740_arr = SRTE_all[idx_740, :]
r_687_arr = SRTE_all[idx_687, :]
fig3 = go.Figure()
fig3.add_trace(go.Scatterpolar(
theta=theta_polar, r=r_740_arr,
mode='lines+markers',
marker=dict(size=5, color='red'),
line=dict(color='red', width=2),
name='SIF at 740nm',
))
fig3.add_trace(go.Scatterpolar(
theta=theta_polar, r=r_687_arr,
mode='lines+markers',
marker=dict(size=5, color='blue'),
line=dict(color='blue', width=2),
name='SIF at 687nm',
))
fig3.update_layout(
polar=dict(
sector=[20, 160],
angularaxis=dict(
tickvals=[30, 50, 70, 90, 110, 130, 150],
ticktext=['Back 60°', 'Back 40°', 'Back 20°', 'Nadir',
'Fwd 20°', 'Fwd 40°', 'Fwd 60°'],
),
radialaxis=dict(
title="SIF (W m⁻² sr⁻¹ μm⁻¹)",
angle=90,
tickangle=90,
),
),
title=f"SIF Polar Distribution — SZA={params['SZA']}°",
height=550,
legend=dict(x=0.85, y=0.95),
template="plotly_white",
)
st.plotly_chart(fig3, use_container_width=True)
# Summary metrics
st.subheader("Summary")
c1, c2, c3, c4 = st.columns(4)
c1.metric("SIF at 687nm", f"{SRTE_all[idx_687, nadir_idx]:.4f}")
c2.metric("SIF at 740nm", f"{SRTE_all[idx_740, nadir_idx]:.4f}")
c3.metric("Peak SIF", f"{SRTE_all[:, nadir_idx].max():.4f}")
c4.metric("Runtime", f"{st.session_state.get('elapsed', 0):.1f} s")
else:
st.info("Set parameters in the sidebar and click **Run Simulation** to generate results.")
# ── Footer ──────────────────────────────────────────────────────────────
st.divider()
st.markdown(
"**Citation**: He, Y., Zeng, Y., et al. (2025). *Remote Sensing of Environment*, 323, 114716. "
"[DOI](https://doi.org/10.1016/j.rse.2025.114716)"
)
st.markdown(
"For **JAX acceleration**, download the code from "
"[GitHub](https://github.com/UW-GCRL/Canopy-SIP-SIF-Model-python) and run locally."
)