-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
389 lines (349 loc) · 11.3 KB
/
Copy pathbuild.py
File metadata and controls
389 lines (349 loc) · 11.3 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import os
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# -----------------------------
# Config
# -----------------------------
DATA_FILE = os.path.join("data", "HadCRUT.4.6.0.0.monthly_ns_avg.txt")
OUT_HTML = "index.html"
# Spiral geometry (tweakable)
R_OFFSET = 1.2 # shifts anomalies to positive radii
R_SCALE = 1.0 # extra scaling if you want bigger/smaller spiral
Z_SCALE = 0.04 # vertical spacing between years (helix height)
# Visual settings
BG_COLOR_FIG = "#323331"
BG_COLOR_PLOT = "#000100"
GRID_COLOR = "rgba(255,255,255,0.15)"
FAINT_OPACITY = 0.18
FAINT_WIDTH = 1.2
HILITE_WIDTH = 6.0
MONTHS = ["Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"]
# -----------------------------
# 1) Load + prepare HadCRUT
# -----------------------------
if not os.path.exists(DATA_FILE):
raise FileNotFoundError(f"Missing data file: {DATA_FILE}")
# File format: first col "YYYY/MM", second col anomaly in °C
raw = pd.read_csv(DATA_FILE, delim_whitespace=True, usecols=[0, 1], header=None)
raw.columns = ["ym", "value"]
raw["year"] = raw["ym"].astype(str).str.split("/").str[0].astype(int)
raw["month"] = raw["ym"].astype(str).str.split("/").str[1].astype(int)
df = raw[["year", "month", "value"]].copy()
# keep full years (12 months)
counts = df.groupby("year")["month"].count()
full_years = counts[counts == 12].index
df = df[df["year"].isin(full_years)].reset_index(drop=True)
# baseline mean 1850–1900
baseline = df.loc[(df["year"] >= 1850) & (df["year"] <= 1900), "value"].mean()
df["anomaly"] = df["value"] - baseline
years = np.sort(df["year"].unique())
year_min, year_max = int(years.min()), int(years.max())
# Annual mean anomalies for bottom chart
annual = df.groupby("year")["anomaly"].mean().reset_index()
# Save cleaned data (for reproducibility / “store the data”)
os.makedirs("data", exist_ok=True)
df.to_csv(os.path.join("data", "hadcrut_monthly_clean.csv"), index=False)
annual.to_csv(os.path.join("data", "hadcrut_annual_mean.csv"), index=False)
# -----------------------------
# 2) Build 3D helix coordinates
# -----------------------------
# Angles: Jan at the top, months clockwise in x-y plane
angles = (np.pi / 2) - (2 * np.pi) * ((df["month"] - 1) / 12.0)
# radius = anomaly shifted positive
r = (df["anomaly"] + R_OFFSET) * R_SCALE
df["x"] = r * np.cos(angles)
df["y"] = r * np.sin(angles)
df["z"] = (df["year"] - year_min) * Z_SCALE # vertical axis = time
# Color mapping by annual mean (we pass the scalar, Plotly handles colorscale)
year_means = annual.set_index("year")["anomaly"]
def year_color(val):
return val # we give scalar; colorscale defined in layout/trace
# -----------------------------
# 3) Figure layout: 3D (top) + time series (bottom)
# -----------------------------
fig = make_subplots(
rows=2, cols=1,
specs=[[{"type": "scene"}], [{"type": "xy"}]],
row_heights=[0.72, 0.28],
vertical_spacing=0.06
)
# --- Static faint helix lines for ALL years ---
for yr in years:
d = df[df["year"] == yr].sort_values("month")
xs = np.r_[d["x"].values, d["x"].values[0]]
ys = np.r_[d["y"].values, d["y"].values[0]]
zs = np.r_[d["z"].values, d["z"].values[0]]
fig.add_trace(
go.Scatter3d(
x=xs, y=ys, z=zs,
mode="lines",
line=dict(
width=FAINT_WIDTH,
color=year_color(year_means.loc[yr]),
colorscale="RdBu_r",
cmin=year_means.min(),
cmax=year_means.max(),
),
opacity=FAINT_OPACITY,
showlegend=False,
hoverinfo="skip",
),
row=1, col=1
)
# --- Bottom: static faint full time series line (context) ---
fig.add_trace(
go.Scatter(
x=annual["year"],
y=annual["anomaly"],
mode="lines",
line=dict(width=2, color="rgba(255,255,255,0.25)"),
hoverinfo="skip",
showlegend=False
),
row=2, col=1
)
# --- Bottom: animated growing line (initially at first year only) ---
yr0 = int(years[0])
initial_sub = annual[annual["year"] <= yr0]
fig.add_trace(
go.Scatter(
x=initial_sub["year"],
y=initial_sub["anomaly"],
mode="lines",
line=dict(width=3, color="rgba(255,255,255,0.95)"),
hovertemplate="Year %{x}<br>Anomaly %{y:.2f}°C<extra></extra>",
showlegend=False
),
row=2, col=1
)
# --- 3D highlighted spiral (will be updated by frames) ---
d0 = df[df["year"] == yr0].sort_values("month")
xs0 = np.r_[d0["x"].values, d0["x"].values[0]]
ys0 = np.r_[d0["y"].values, d0["y"].values[0]]
zs0 = np.r_[d0["z"].values, d0["z"].values[0]]
highlight_trace_3d = go.Scatter3d(
x=xs0, y=ys0, z=zs0,
mode="lines",
line=dict(
width=HILITE_WIDTH,
color=year_color(year_means.loc[yr0]),
colorscale="RdBu_r",
cmin=year_means.min(),
cmax=year_means.max(),
),
opacity=1.0,
name="Selected year",
showlegend=False,
hovertemplate=(
"Year: %{customdata[0]}<br>"
"Month: %{customdata[1]}<br>"
"Anomaly: %{customdata[2]:.2f}°C<extra></extra>"
),
customdata=np.zeros((len(xs0), 3)),
)
fig.add_trace(highlight_trace_3d, row=1, col=1)
# --- Bottom: moving marker on the time series ---
marker_trace = go.Scatter(
x=[yr0],
y=[float(annual.loc[annual["year"] == yr0, "anomaly"].iloc[0])],
mode="markers+text",
marker=dict(size=12, color="rgba(255,255,255,0.95)"),
text=[str(yr0)],
textposition="top center",
textfont=dict(color="white", size=16),
hovertemplate="Year %{x}<br>Anomaly %{y:.2f}°C<extra></extra>",
showlegend=False
)
fig.add_trace(marker_trace, row=2, col=1)
# Trace indices (important for frames)
# 0 .. len(years)-1 -> faint 3D lines
# len(years) -> static faint time-series line
# len(years)+1 -> growing time-series line (animated)
# len(years)+2 -> highlighted 3D spiral (animated)
# len(years)+3 -> marker on time series (animated)
# -----------------------------
# 4) Build frames (one per year)
# -----------------------------
frames = []
for yr in years:
# --- 3D highlighted spiral for this year ---
d = df[df["year"] == yr].sort_values("month")
xs = np.r_[d["x"].values, d["x"].values[0]]
ys = np.r_[d["y"].values, d["y"].values[0]]
zs = np.r_[d["z"].values, d["z"].values[0]]
month_labels = [MONTHS[m-1] for m in d["month"].values]
month_labels = month_labels + [month_labels[0]]
anomalies = np.r_[d["anomaly"].values, d["anomaly"].values[0]]
customdata = np.column_stack([
np.full(len(xs), int(yr)),
np.array(month_labels, dtype=object),
anomalies
])
# --- Bottom chart data up to current year ---
sub = annual[annual["year"] <= yr]
y_anom = sub.iloc[-1]["anomaly"]
frames.append(
go.Frame(
name=str(int(yr)),
data=[
# 3D highlighted spiral
go.Scatter3d(
x=xs, y=ys, z=zs,
line=dict(
width=HILITE_WIDTH,
color=year_color(year_means.loc[yr]),
colorscale="RdBu_r",
cmin=year_means.min(),
cmax=year_means.max(),
),
customdata=customdata,
),
# Growing line on time series
go.Scatter(
x=sub["year"],
y=sub["anomaly"],
),
# Moving marker
go.Scatter(
x=[int(yr)],
y=[y_anom],
text=[str(int(yr))],
),
],
traces=[
len(years) + 2, # highlighted 3D spiral
len(years) + 1, # growing line
len(years) + 3, # marker
],
)
)
fig.frames = frames
# -----------------------------
# 5) Slider + play button
# -----------------------------
slider_steps = [
dict(
method="animate",
args=[[str(int(yr))],
dict(mode="immediate", frame=dict(duration=0, redraw=True),
transition=dict(duration=0))],
label=str(int(yr))
)
for yr in years
]
fig.update_layout(
updatemenus=[
dict(
type="buttons",
showactive=False,
x=0.02, y=1.02,
xanchor="left", yanchor="bottom",
buttons=[
dict(
label="Play",
method="animate",
args=[None,
dict(frame=dict(duration=60, redraw=True),
transition=dict(duration=0),
fromcurrent=True,
mode="immediate")]
),
dict(
label="Pause",
method="animate",
args=[[None],
dict(frame=dict(duration=0, redraw=False),
mode="immediate",
transition=dict(duration=0))]
),
],
)
],
sliders=[
dict(
active=0,
currentvalue=dict(prefix="Year: ", font=dict(color="white", size=16)),
pad=dict(t=30),
x=0.08, y=0.98,
len=0.84,
font=dict(color="white"),
steps=slider_steps
)
]
)
# -----------------------------
# 6) Styling + axes
# -----------------------------
fig.update_layout(
title=dict(
text=f"Global Temperature Change ({year_min}–{year_max})",
x=0.5,
xanchor="center",
font=dict(color="white", size=26)
),
paper_bgcolor=BG_COLOR_FIG,
plot_bgcolor=BG_COLOR_PLOT,
margin=dict(l=20, r=20, t=90, b=30),
)
fig.update_scenes(
row=1, col=1,
bgcolor=BG_COLOR_PLOT,
xaxis=dict(
showbackground=False,
showgrid=True, gridcolor=GRID_COLOR,
zeroline=False,
showticklabels=False,
title=""
),
yaxis=dict(
showbackground=False,
showgrid=True, gridcolor=GRID_COLOR,
zeroline=False,
showticklabels=False,
title=""
),
zaxis=dict(
showbackground=False,
showgrid=False,
zeroline=False,
showticklabels=False,
title=""
),
camera=dict(
eye=dict(x=1.8, y=1.2, z=0.8)
)
)
fig.update_xaxes(
row=2, col=1,
showgrid=False,
zeroline=False,
tickfont=dict(color="white"),
title="",
)
fig.update_yaxes(
row=2, col=1,
showgrid=True,
gridcolor="rgba(255,255,255,0.08)",
zeroline=False,
tickfont=dict(color="white"),
title="Anomaly (°C)",
)
# Annotation for baseline
fig.add_annotation(
text="Baseline: 1850–1900 mean (HadCRUT4)",
xref="paper", yref="paper",
x=0.01, y=0.02,
showarrow=False,
font=dict(color="rgba(255,255,255,0.6)", size=12)
)
# -----------------------------
# 7) Write standalone HTML
# -----------------------------
fig.write_html(OUT_HTML, include_plotlyjs=True, full_html=True)
print(f"Wrote {OUT_HTML} (open it in a browser).")
print("Saved cleaned data:")
print(" - data/hadcrut_monthly_clean.csv")
print(" - data/hadcrut_annual_mean.csv")