programextensions/sqlite/demos/notebook.xtl

The data notebook: a CSV in SQLite, SQL to select and group, X_eTaL arrays to compute, pictures to see it. Atmospheric CO2 at Mauna Loa, 1959-2025 (NOAA GML and Scripps; demos/data/PROVENANCE.txt). Run: just demo sqlite notebook (pictures go to work/draw/)

source · imports sq: extensions/sqlite/lib/Sqlite.xtl; s: std/Stats.xtl

db : Char

value · line 9
db ← "work/notebook.db"

x : Int

value · line 10
x ← db sq:e̲xec "drop table if exists co2"

n : Int

value · line 11
n ← db sq:i̲mport "co2=demos/data/co2-mlo-annual.csv"

3. arrays: the whole series, then what SQL is awkward at

m : Float

value · line 23
m ← db sq:n̲ums "select year, mean from co2 order by year"
Used in: y, c

y : Float

value · line 24
y ← 1 s̲elect₂ m
Used in: yb, s, e, w, p

c : Float

value · line 25
c ← 2 s̲elect₂ m

ᵘr̲ound : Float -> Float

function · line 26
ᵘr̲ound ← { v → (f̲loat f̲loor 0.5 + 100.0 × v) ÷ 100.0 }

d : Float

value · line 29

the rise each year: the series minus itself shifted by one

d ← (1 d̲rop c) − -1 d̲rop c

yb : Float

value · line 34

a least-squares line through all of it: slope and where it is in 2025

yb ← ˢm̲ean y

cb : Float

value · line 35
cb ← ˢm̲ean c

s : Float

value · line 36
s ← ('+ r̲/ (y − yb) × c − cb) ÷ '+ r̲/ (y − yb) × y − yb

e : Float

value · line 42

the line misses in a curve: high at both ends, low in the middle -- the rise is speeding up (residuals averaged by thirds)

e ← c − cb + s × y − yb

k : Int

value · line 43
k ← f̲loor (t̲ally e) ÷ 3

4. pictures

b : Int

value · line 51

the yearly rise as a histogram: half-ppm bins, one column per bin, drawn as an APL bar chart (a 0/1 matrix, 1s dark)

b ← 1 + f̲loor d ÷ 0.5
Used in: h

h : Int

value · line 52
h ← '+ r̲/₂ (r̲ange 8) '= t̲able b

p : Char

value · line 55
p ← ⎕S̲HOW ⎕G̲RID (r̲ev r̲ange 'm̲ax r̲/ h) '≤ t̲able h

w : Float

value · line 58

the series as a line, years across and ppm up (scaled to a wide box)

w ← 3.0 × (y − 1959.0) ÷ 66.0
Used in: p

v : Float

value · line 59
v ← (c − 315.0) ÷ 115.0
Used in: p

p : Char

value · line 60
p ← ⎕S̲HOW ⎕P̲ATH (2 c̲at t̲ally y) r̲eshape w c̲at v