sourceextensions/http/demos/Seismic.xtl
1⍝│ Seismic.xtl -- the earthquake report (quakes.xtl, quakes-live.xtl): a
2⍝│ USGS feed CSV into SQLite, SQL to select and group, X_eTaL arrays to
3⍝│ compute, pictures to see it. An ordinary X_eTaL library beside them
4⍝│ (not Quakes.xtl: on a case-insensitive file system that is quakes.xtl).
5⍝│ Import: "se:" u_se< "Seismic"
6
7"sq:" u̲se< "Sqlite"
8
9db ← "work/quakes.db"
10
11ʰr̲ound ← { v → (f̲loat f̲loor 0.5 + 100.0 × v) ÷ 100.0 }
12ʰm̲ean ← { v → ('+ r̲/ v) ÷ f̲loat t̲ally v }
13
14⍝│ se:r_eport "path.csv": prints the report and draws its pictures
15ˡr̲eport ← { csv →
16 x ← db sq:e̲xec "drop table if exists quakes"
17 n ← db sq:i̲mport "quakes=" c̲at csv
18
19 ⍝ -- 1. SQL: what is there
20 _ ← p̲rint! "quakes, the smallest and largest magnitude, the mean depth (km):"
21 _ ← p̲rint! ʰr̲ound r̲avel db sq:n̲ums "select count(*), min(mag), max(mag), avg(depth) from quakes"
22 _ ← p̲rint! "from, to (UTC):"
23 _ ← p̲rint! db sq:t̲exts "select substr(min(time), 1, 16), substr(max(time), 1, 16) from quakes"
24
25 ⍝ -- 2. SQL groups: the largest three, and quakes per day
26 _ ← p̲rint! "the largest three: magnitude, place"
27 _ ← p̲rint! db sq:t̲exts "select printf('%.1f', mag) || ' ' || place from quakes order by mag desc limit 3"
28 _ ← p̲rint! "quakes per day:"
29 _ ← p̲rint! r̲avel db sq:n̲ums "select count(*) from quakes group by substr(time, 1, 10) order by substr(time, 1, 10)"
30
31 ⍝ -- 3. arrays: magnitudes in half-unit bins (2.45 rounds into the
32 ⍝ first), and the Gutenberg-Richter law: the count at or above each
33 ⍝ magnitude falls about tenfold per unit -- its b-value is about 1.
34 ⍝ Outside the US this feed lists quakes from about 4.5 up, so b is
35 ⍝ estimated above 4.5 (Aki's maximum likelihood: log10 e over the
36 ⍝ mean magnitude's excess, magnitudes given to 0.1)
37 m ← r̲avel db sq:n̲ums "select mag from quakes"
38 h ← '+ r̲/₂ (r̲ange 10) '= t̲able 1 + 0 m̲ax f̲loor (m − 2.5) ÷ 0.5
39 _ ← p̲rint! "quakes in [2.5, 3), [3, 3.5), ... [7, 7.5):"
40 _ ← p̲rint! h
41 t ← 2.5 + 0.5 × f̲loat (r̲ange 8) − 1
42 c ← '+ r̲/₂ t '≤ t̲able m
43 k ← '+ r̲/ c > 0
44 kept ← k t̲ake t
45 g ← (l̲og f̲loat k t̲ake c) ÷ l̲og 10.0
46 _ ← p̲rint! "log10 of the count at or above 2.5, 3, 3.5, ...:"
47 _ ← p̲rint! ʰr̲ound g
48 big ← (m ≥ 4.5) r̲eplicate m
49 _ ← p̲rint! "quakes of 4.5 and up, and their b-value (Aki):"
50 _ ← p̲rint! (f̲loat t̲ally big) c̲at ʰr̲ound 0.4342944819 ÷ (ʰm̲ean big) − 4.45
51
52 ⍝ -- 4. pictures
53 ⍝ a world map: quakes counted in 10-degree cells, 18 rows (north at
54 ⍝ the top) by 36 columns (from 180 W), one array index per quake
55 ll ← db sq:n̲ums "select latitude, longitude from quakes"
56 row ← 17 m̲in f̲loor (90.0 − 1 s̲elect₂ ll) ÷ 10.0
57 col ← 35 m̲in f̲loor (180.0 + 2 s̲elect₂ ll) ÷ 10.0
58 w ← '+ r̲/₂ (r̲ange 648) '= t̲able 1 + col + 36 × row
59 _ ← ⎕S̲HOW ⎕G̲RID 18 36 r̲eshape (w > 0) + (w > 2) + w > 8
60 ⍝ the magnitude histogram as bars, a cell per 5 quakes, and the law
61 ⍝ as a line
62 h5 ← c̲eiling (f̲loat h) ÷ 5.0
63 _ ← ⎕S̲HOW ⎕G̲RID (r̲ev r̲ange 'm̲ax r̲/ h5) '≤ t̲able h5
64 _ ← ⎕S̲HOW ⎕P̲ATH (2 c̲at t̲ally kept) r̲eshape kept c̲at g
65 "drawn: the map, the histogram, the Gutenberg-Richter line"
66}