-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
396 lines (314 loc) · 11.3 KB
/
index.html
File metadata and controls
396 lines (314 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
390
391
392
393
394
395
396
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:site" content="@pmbarks"/>
<meta name="twitter:creator" content="@pmbarks"/>
<meta name="twitter:creator" content="@pmbarks"/>
<meta name="twitter:image" content="https://www.pmbarks.com/img/app.png"/>
<meta name="twitter:title" content="Trends in mean annual temperature at 729 Canadian climate stations"/>
<meta name="twitter:description" content="An app to visualize long-term temperature trends at 729 Canadian climate stations"/>
<meta property="og:type" content="article">
<meta property="article:published_time" content="2019-01-11">
<meta property="og:title" content="Trends in mean annual temperature at 729 Canadian climate stations">
<meta property="og:description" content="An app to visualize long-term temperature trends at 729 Canadian climate stations">
<meta property="og:url" content="https://patrickbarks.github.io/canada-clim/">
<meta property="og:site_name" content="Patrick Barks">
<meta property="og:image" content="https://www.pmbarks.com/img/app.png">
<meta property="og:tags" content="climate">
<meta property="og:tags" content="D3.js">
<meta property="og:tags" content="R">
<meta name="author" content="Patrick Barks">
<script src="https://d3js.org/d3.v4.min.js"></script>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<link rel="shortcut icon" href="favicon.ico">
<title>Trends in mean annual temperature at 729 Canadian climate stations · Patrick Barks</title>
</head>
<body>
<div class="div-head">
Trends in mean annual temperature at 729 Canadian climate stations
</div>
<div class="div-plot">
<div class="div-select-prov">
Province: <select id="province" onchange="cities_update()"></select>
</div>
<div class="div-select-station">
Station: <select id="city" onchange="plot_update()"></select>
</div>
<div class="div-trend">
Trend: <span id="trend-val"></span>°C per decade
</div>
</div>
<div class="div-main">
<div style="padding-bottom:10px;">
<svg width="45", height="25">
<line x1="5" y1="20" x2="40" y2="20" class="gap-line" />
</svg>
<span class="lab-dashed">dashed lines indicate one or more years with missing or incomplete temperature data</span>
</div>
<p>
Data are mean annual homogenized surface air temperatures from <a
href="https://doi.org/10.1080/07055900.2020.1765728">Vincent et al. (2020)</a>, available
from <a
href="https://www.canada.ca/en/environment-climate-change/services/climate-change/science-research-data/climate-trends-variability/adjusted-homogenized-canadian-data/surface-air-temperature-access.html">
Environment and Climate Change Canada</a> (see file <i>Monthly mean of daily
mean temperature</i>). Trend line based on least squares regression (note that
trend is <i>not</i> a projection of future change). Code available at <a
href="https://github.com/patrickbarks/canada-clim">
github.com/patrickbarks/canada-clim</a>.
</p>
<p>
<b>Data reference:</b> Vincent, L. A., Hartwell, M. M., and Wang, X. L. (2020) A
third generation of homogenized temperature for trend analysis and monitoring
changes in Canada’s climate. <i>Atmosphere-Ocean</i>, 58, 173-191.
<a href="https://doi.org/10.1080/07055900.2020.1765728">doi.org/10.1080/07055900.2020.1765728</a>
</p>
</div>
<br>
<footer class="foot">
<!-- Copyright -->
<div>Created by
<a href="https://www.pmbarks.com"> Patrick Barks</a> |
<i class="fab fa-twitter"></i>
<a href="https://twitter.com/pmbarks"> @pmbarks</a>
</div>
<!-- Copyright -->
</footer>
<script>
// set the dimensions and margins of the graph
var margin = {top: 70, right: 20, bottom: 35, left: 56};
var width = parseInt(d3.select('.div-plot').style('width'), 10) - margin.left - margin.right;
var height = width * 9 / 15 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([0, height]);
// define the line
var valueline = d3.line()
.defined(function (d) { return d.annual !== null; })
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.annual); });
var legendline = d3.line()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; });
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select(".div-plot")
.append("svg")
.attr("class", "svg-left")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
var stn_dat, clim_dat, fit_dat;
var province, prov, city;
d3.queue()
.defer(d3.csv, "dat/dat_station.csv", parse_csv_stn)
.defer(d3.csv, "dat/dat_clim.csv", parse_csv_clim)
.defer(d3.csv, "dat/dat_fit.csv", parse_csv_clim)
.await(analyze);
// update on window resize
var updateLayout = debounce(function(e) {
width = parseInt(d3.select('.div-plot').style('width'), 10) - margin.left - margin.right;
height = width * 9 / 15 - margin.top - margin.bottom;
d3.select(".svg-left").attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
x.range([0, width]);
y.range([0, height]);
valueline.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.annual); });
plot_update();
}, 50);
window.addEventListener("resize", updateLayout, false);
var prov_l = {
"British Columbia": "BC",
"Alberta": "AB",
"Saskatchewan": "SK",
"Manitoba": "MB",
"Ontario": "ON",
"Quebec": "QC",
"New Brunswick": "NB",
"Nova Scotia": "NS",
"Prince Edward Isl.": "PE",
"Newfoundland": "NL",
"Yukon": "YT",
"Northwest Terr.": "NT",
"Nunavut": "NU"
};
function analyze(error, stn_data, clim_data, fit_data) {
if(error) { console.log(error); }
stn_dat = stn_data;
clim_dat = clim_data;
fit_dat = fit_data;
d3.select("#province")
.selectAll("option")
.data(Object.keys(prov_l))
.enter().append("option")
.text(function(d) {return d;});
province = document.getElementById("province").value = "Ontario";
prov = prov_l[province];
d3.select("#city")
.selectAll("option")
.data(stn_data.filter(function(d) {return d.prov == prov;}))
.enter().append("option")
.text(function(d) {return d.station;});
// filter data to city of interest
city = document.getElementById("city").value = "Toronto";
clim_data = clim_data.filter(function(d) {return d.station == city;});
fit_data = fit_data.filter(function(d) {return d.station == city;});
// Scale the range of the data
x.domain([1877, 2023]);
update_y_axis(clim_data);
// add the x-axis
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x)
.tickValues([1880, 1900, 1920, 1940, 1960, 1980, 2000, 2020])
.tickSize(-height)
.tickPadding(8)
.tickFormat(d3.format("d")));
// add the y-axis
svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y)
.ticks(10)
.tickSize(-width)
.tickPadding(7)
.tickFormat(formatCust));
// add temperature line
svg.append("path")
.datum(clim_data)
.attr("class", "line")
.attr("d", valueline)
.style("opacity", 0)
.transition()
.duration(300)
.ease(d3.easeCubic)
.style("opacity", 1);
// add gap lines
svg.append("path")
.datum(clim_data.filter(function(d) { return d.annual !== null; }))
.attr("class", "gap-line")
.attr("d", valueline)
.style("opacity", 0)
.transition()
.duration(300)
.ease(d3.easeCubic)
.style("opacity", 1);
var trend_val = stn_dat.filter(function(d) {return d.station == city;})[0].trend_annual;
document.getElementById("trend-val").innerHTML = d3.format("+.2f")(trend_val);
// add fit line
svg.append("path")
.datum(fit_data)
.attr("class", "fit-line")
.attr("d", valueline);
};
// update y-axis
function update_y_axis(data) {
var y_min = d3.min(data, function(d) { return d.annual; });
var y_max = d3.max(data, function(d) { return d.annual; });
var diff = y_max - y_min;
var y_add = (7.5 - diff) / 2;
y_min -= y_add;
y_max += y_add;
y.domain([y_max, y_min]);
};
// update plot when city changes
function plot_update() {
city = document.getElementById("city").value;
province = document.getElementById("province").value;
prov = prov_l[province];
var clim_dat_sub = clim_dat.filter(function(d) {
return d.station == city && d.prov == prov;
});
var fit_dat_sub = fit_dat.filter(function(d) {
return d.station == city && d.prov == prov;
});
update_y_axis(clim_dat_sub);
// update y-axis
d3.select('.axis--y')
.transition()
.call(d3.axisLeft(y)
.ticks(10)
.tickSize(-width)
.tickPadding(7)
.tickFormat(formatCust));
// update x-axis
d3.select('.axis--x')
.attr("transform", "translate(0," + height + ")")
.transition()
.call(d3.axisBottom(x)
.tickValues([1880, 1900, 1920, 1940, 1960, 1980, 2000, 2020])
.tickSize(-height)
.tickPadding(8)
.tickFormat(d3.format("d")));
svg.select(".line")
.datum(clim_dat_sub)
.attr("d", valueline);
svg.select(".gap-line")
.datum(clim_dat_sub.filter(function(d) { return d.annual !== null; }))
.attr("d", valueline);
svg.select(".fit-line")
.datum(fit_dat_sub)
.attr("d", valueline);
var trend_val = stn_dat.filter(function(d) {return d.station == city;})[0].trend_annual;
document.getElementById("trend-val").innerHTML = d3.format("+.2f")(trend_val);
};
// update list of cities when prov changes
function cities_update() {
province = document.getElementById("province").value;
prov = prov_l[province];
var city_sel = d3.select("#city")
.selectAll("option")
.data(stn_dat.filter(function(d) {return d.prov == prov;}));
city_sel.exit().remove();
city_sel.enter().append("option")
.merge(city_sel)
.text(function(d) {return d.station;});
plot_update();
};
// fn to parse climate data
function parse_csv_clim(d) {
return {
station: d.station,
prov: d.prov,
year: +d.year,
annual: isNaN(d.annual) ? null : +d.annual
};
};
// fn to parse station data
function parse_csv_stn(d) {
return {
station: d.station,
prov: d.prov,
trend_annual: +d.trend_annual,
r2_annual: +d.r2_annual
};
};
// format y-axes
function formatCust(d, i) {
return i == 0 ? d3.format("d")(d) + " \u00B0C" : d3.format("d")(d);
};
// debounce (from underscore.js, https://underscorejs.org/)
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
</script>
</body>
</html>