sourceextensions/sqlite/demos/notebook.xtl
1⍝# The data notebook: a CSV in SQLite, SQL to select and group, X_eTaL
2⍝# arrays to compute, pictures to see it. Atmospheric CO2 at Mauna Loa,
3⍝# 1959-2025 (NOAA GML and Scripps; demos/data/PROVENANCE.txt).
4⍝# Run: just demo sqlite notebook (pictures go to work/draw/)
5
6"sq:" u̲se< "Sqlite"
7ˢ⁼u̲se< "Stats"
8
9db ← "work/notebook.db"
10x ← db sq:e̲xec "drop table if exists co2"
11n ← db sq:i̲mport "co2=demos/data/co2-mlo-annual.csv"
12
13⍝## 1. SQL: what is there (rows, first and last year, lowest and highest ppm)
14
15db sq:n̲ums "select count(*), min(year), max(year), min(mean), max(mean) from co2"
16
17⍝## 2. SQL groups: the mean of each decade, and its years
18
19db sq:n̲ums "select (year / 10) * 10, round(avg(mean), 1), count(*) from co2 group by 1 order by 1"
20
21⍝## 3. arrays: the whole series, then what SQL is awkward at
22
23m ← db sq:n̲ums "select year, mean from co2 order by year"
24y ← 1 s̲elect₂ m
25c ← 2 s̲elect₂ m
26ᵘr̲ound ← { v → (f̲loat f̲loor 0.5 + 100.0 × v) ÷ 100.0 }
27
28⍝# the rise each year: the series minus itself shifted by one
29d ← (1 d̲rop c) − -1 d̲rop c
30"mean rise per year (ppm), first decade, last decade:"
31ᵘr̲ound (ˢm̲ean d) c̲at (ˢm̲ean 10 t̲ake d) c̲at ˢm̲ean -10 t̲ake d
32
33⍝# a least-squares line through all of it: slope and where it is in 2025
34yb ← ˢm̲ean y
35cb ← ˢm̲ean c
36s ← ('+ r̲/ (y − yb) × c − cb) ÷ '+ r̲/ (y − yb) × y − yb
37"least-squares slope (ppm per year), the line at 2025, the data at 2025:"
38ᵘr̲ound s c̲at (cb + s × 2025.0 − yb) c̲at -1 t̲ake c
39
40⍝# the line misses in a curve: high at both ends, low in the middle --
41⍝# the rise is speeding up (residuals averaged by thirds)
42e ← c − cb + s × y − yb
43k ← f̲loor (t̲ally e) ÷ 3
44"mean residual (ppm): first third, middle third, last third:"
45ᵘr̲ound (ˢm̲ean k t̲ake e) c̲at (ˢm̲ean k t̲ake k d̲rop e) c̲at ˢm̲ean (0 − k) t̲ake e
46
47⍝## 4. pictures
48
49⍝# the yearly rise as a histogram: half-ppm bins, one column per bin,
50⍝# drawn as an APL bar chart (a 0/1 matrix, 1s dark)
51b ← 1 + f̲loor d ÷ 0.5
52h ← '+ r̲/₂ (r̲ange 8) '= t̲able b
53"years with a rise in [0, 0.5), [0.5, 1), ... [3.5, 4) ppm:"
54h
55p ← ⎕S̲HOW ⎕G̲RID (r̲ev r̲ange 'm̲ax r̲/ h) '≤ t̲able h
56
57⍝# the series as a line, years across and ppm up (scaled to a wide box)
58w ← 3.0 × (y − 1959.0) ÷ 66.0
59v ← (c − 315.0) ÷ 115.0
60p ← ⎕S̲HOW ⎕P̲ATH (2 c̲at t̲ally y) r̲eshape w c̲at v
61"drawn: the histogram and the curve"