Rain / Temperature - Analysis

Switzerland 1864-2013

This example is a jupyter notebook adoption from the small desktop application I wrote some time ago, to analyze weather data (rain, temperature) from swiss weather stations from year 1864 - 2013.

It shows the capabilities of analyzing and visualizing data using the J array programming language.


In [1]:
NB. Required library scripts
require 'tables/dsv'
require 'graphics/plot'

NB. Utility verbs
chop =: 4 : '(y#i.#y)</.x'
desc =: 3 : '>(I. (<y) E. 0{keys) { 1{keys'
gets =: 4 : '(I. (<y) E. 0{|:x){x'
gety =: 4 : '(I. (<y) E. 1{|:x){x'
getm =: 4 : '(I. (<y) E. 2{|:x){x'
vfmx =: 3 : '}:(,|."1 [ 1,.-. *./\"1 |."1 y='' '')#,y,.LF'
lreg =: 4 : 'y %. 1 ,. x'

NB. Read and format raw data, provide filter scripts
data =: ('|';'') readdsv jpath '~user/Jupyter_Notebook_J_Example_Data/RainTemp_1864-2013.txt'
data =: (I. (<'PAY') i. 0{|:data){data NB. Remove PAY (no data)
stat =: ('|';'') readdsv jpath '~user/Jupyter_Notebook_J_Example_Data/Weather_Stations.txt'
keys =: (2,$(~.0{|:data))$(~.0{|:data),(I.((1}.0{|:stat)e.(~.0{|:data))){1}.1{|:stat
spym =: |: ((#data),2) $ (,/>1{|:data) chop ((2*#data) $ 4 2)
data =: (0{|:data),.(0{spym),.(1{spym),.(". each 2{|:data),.(". each 3{|:data)
mons =: '<All>';'Januar';'Februar';'März';'April';'Mai';'Juni';'Juli';'August';'September';'Oktober';'November';'Dezember'
yeal =: ": (".>1{0{data)+10*i.1+(>.(((".>1{0{_1{.data)-(".>1{0{data))%10))
yeax =: ": i. # ". yeal
monl =: 'Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez'
monx =: ": i. 12
xtin =: ":((i.16)*((#(data gets >0{0{keys))%15))
xlan =: yeal
xtit =: ":((i.16)*((#(data gets >0{0{keys))%15))
xlat =: yeal
nied =: ,/>3{|:(data gets >0{0{keys)
temp =: ,/>4{|:(data gets >0{0{keys)
nreg =: ((1{((i.#nied) lreg nied))*(i.#nied))+(0{((i.#nied) lreg nied))
treg =: ((1{((i.#temp) lreg temp))*(i.#temp))+(0{((i.#temp) lreg temp))
ni3d =: (150,12)$,/>3{|:(data gets >0{0{keys)
te3d =: (150,12)$,/>4{|:(data gets >0{0{keys)
vpox =: 2.8
vpoy =: _4
vpoz =: 2.4




In [2]:
NB. Show keys (available weather stations)
|:keys


┌───┬───────────────────────┐
│BAS│Basel/Binningen        │
├───┼───────────────────────┤
│BER│Bern/Zollikofen        │
├───┼───────────────────────┤
│CHM│Chaumont               │
├───┼───────────────────────┤
│CHD│Château-d'Oex          │
├───┼───────────────────────┤
│GSB│Col du Grand St.Bernard│
├───┼───────────────────────┤
│DAV│Davos                  │
├───┼───────────────────────┤
│ENG│Engelberg              │
├───┼───────────────────────┤
│GVE│Genève-Cointrin        │
├───┼───────────────────────┤
│LUG│Lugano                 │
├───┼───────────────────────┤
│SIA│Segl-Maria             │
├───┼───────────────────────┤
│SIO│Sion                   │
├───┼───────────────────────┤
│SAE│Säntis                 │
├───┼───────────────────────┤
│SMA│Zürich/Fluntern        │
└───┴───────────────────────┘

In [3]:
NB. Filter data on all years, weather station SMA, and month September (09)
]d =: (data gets 'SMA') getm '09'


...
│SMA│1902│09│97.2 │13  │
├───┼────┼──┼─────┼────┤
│SMA│1903│09│70.3 │13.5│
├───┼────┼──┼─────┼────┤
│SMA│1904│09│99.9 │11.6│
├───┼────┼──┼─────┼────┤
│SMA│1905│09│148.2│13.4│
├───┼────┼──┼─────┼────┤
│SMA│1906│09│23.7 │12.8│
├───┼────┼──┼─────┼────┤
│SMA│1907│09│39.5 │13.7│
├───┼────┼──┼─────┼────┤
│SMA│1908│09│146.2│12.3│
├───┼────┼──┼─────┼────┤
│SMA│1909│09│66.3 │12.7│
├───┼────┼──┼─────┼────┤
│SMA│1910│09│102.3│11  │
├───┼────┼──┼─────┼────┤
│SMA│1911│09│99.8 │15.5│
├───┼────┼──┼─────┼────┤
│SMA│1912│09│50.6 │8.4 │
├───┼────┼──┼─────┼────┤
│SMA│1913│09│104.8│12.4│
├───┼────┼──┼─────┼────┤
│SMA│1914│09│86.3 │12.6│
├───┼────┼──┼─────┼────┤
│SMA│1915│09│80.9 │11.2│
├───┼────┼──┼─────┼────┤
│SMA│1916│09│84.3 │11.2│
├───┼────┼──┼─────┼────┤
│SMA│1917│09│59.3 │15.4│
├───┼────┼──┼─────┼────┤
│SMA│1918│09│176.3│13.3│
├───┼────┼──┼─────┼────┤
│SMA│1919│09│39.1 │15.2│
├───┼────┼──┼─────┼────┤
│SMA│1920│09│87.3 │13.3│
├───┼────┼──┼─────┼────┤
│SMA│1921│09│90.4 │14.8│
├───┼────┼──┼─────┼────┤
│SMA│1922│09│94.4 │11  │
├───┼────┼──┼─────┼────┤
│SMA│1923│09│73.3 │13.2│
├───┼────┼──┼─────┼────┤
│SMA│1924│09│40.8 │13.3│
├───┼────┼──┼─────┼────┤
│SMA│1925│09│125.7│10.9│
├───┼────┼──┼─────┼────┤
│SMA│1926│09│43.9 │15.4│
├───┼────┼──┼─────┼────┤
│SMA│1927│09│155.4│13.4│
├───┼────┼──┼─────┼────┤
│SMA│1928│09│85.3 │13.2│
├───┼────┼──┼─────┼────┤
│SMA│1929│09│64.3 │16.4│
├───┼────┼──┼─────┼────┤
│SMA│1930│09│69.9 │13.9│
├───┼────┼──┼─────┼────┤
│SMA│1931│09│92   │9.2 │
├───┼────┼──┼─────┼────┤
│SMA│1932│09│47.6 │16.2│
├───┼────┼──┼─────┼────┤
│SMA│1933│09│83.6 │14  │
├───┼────┼──┼─────┼────┤
│SMA│1934│09│122.5│15.1│
├───┼────┼──┼─────┼────┤
│SMA│1935│09│101.1│14.1│
├───┼────┼──┼─────┼────┤
│SMA│1936│09│184.1│13.3│
├───┼────┼──┼─────┼────┤
│SMA│1937│09│164  │12.7│
├───┼────┼──┼─────┼────┤
│SMA│1938│09│100.8│13.9│
├───┼────┼──┼─────┼────┤
│SMA│1939│09│122.9│12.5│
├───┼────┼──┼─────┼────┤
│SMA│1940│09│235.1│13.3│
├───┼────┼──┼─────┼────┤
│SMA│1941│09│13.6 │12.6│
├───┼────┼──┼─────┼────┤
│SMA│1942│09│53.9 │15.7│
├───┼────┼──┼─────┼────┤
│SMA│1943│09│131  │14.4│
├───┼────┼──┼─────┼────┤
│SMA│1944│09│102.8│13  │
├───┼────┼──┼─────┼────┤
│SMA│1945│09│139  │14.3│
├───┼────┼──┼─────┼────┤
│SMA│1946│09│120.5│14  │
├───┼────┼──┼─────┼────┤
│SMA│1947│09│48.1 │16.4│
├───┼────┼──┼─────┼────┤
│SMA│1948│09│48   │13.5│
├───┼────┼──┼─────┼────┤
│SMA│1949│09│52.6 │16.7│
├───┼────┼──┼─────┼────┤
│SMA│1950│09│122.5│13.1│
├───┼────┼──┼─────┼────┤
│SMA│1951│09│89.5 │14.4│
├───┼────┼──┼─────┼────┤
│SMA│1952│09│110.7│10.7│
├───┼────┼──┼─────┼────┤
│SMA│1953│09│67.7 │14.2│
├───┼────┼──┼─────┼────┤
│SMA│1954│09│176.8│14  │
├───┼────┼──┼─────┼────┤
│SMA│1955│09│79   │13.1│
├───┼────┼──┼─────┼────┤
│SMA│1956│09│67.7 │14.3│
├───┼────┼──┼─────┼────┤
│SMA│1957│09│140.3│12.6│
├───┼────┼──┼─────┼────┤
│SMA│1958│09│87   │15.1│
├───┼────┼──┼─────┼────┤
│SMA│1959│09│11.8 │14.7│
├───┼────┼──┼─────┼────┤
│SMA│1960│09│104.1│11.9│
├───┼────┼──┼─────┼────┤
│SMA│1961│09│32.5 │17.3│
├───┼────┼──┼─────┼────┤
│SMA│1962│09│92.2 │13.3│
├───┼────┼──┼─────┼────┤
│SMA│1963│09│93.3 │13.9│
├───┼────┼──┼─────┼────┤
│SMA│1964│09│63.8 │14.1│
├───┼────┼──┼─────┼────┤
│SMA│1965│09│125.3│11.7│
├───┼────┼──┼─────┼────┤
│SMA│1966│09│77   │14.6│
├───┼────┼──┼─────┼────┤
│SMA│1967│09│170.4│13.1│
├───┼────┼──┼─────┼────┤
│SMA│1968│09│216.8│12.6│
├───┼────┼──┼─────┼────┤
│SMA│1969│09│21.9 │14.3│
├───┼────┼──┼─────┼────┤
│SMA│1970│09│53.7 │14  │
├───┼────┼──┼─────┼────┤
│SMA│1971│09│76.2 │12.7│
├───┼────┼──┼─────┼────┤
│SMA│1972│09│31.2 │10.5│
├───┼────┼──┼─────┼────┤
│SMA│1973│09│77.2 │14.6│
├───┼────┼──┼─────┼────┤
│SMA│1974│09│77.4 │13.3│
├───┼────┼──┼─────┼────┤
│SMA│1975│09│112.6│15.8│
├───┼────┼──┼─────┼────┤
│SMA│1976│09│98.5 │12.2│
├───┼────┼──┼─────┼────┤
│SMA│1977│09│54.5 │12.2│
├───┼────┼──┼─────┼────┤
│SMA│1978│09│51.6 │13.2│
├───┼────┼──┼─────┼────┤
│SMA│1979│09│95.6 │13.7│
├───┼────┼──┼─────┼────┤
│SMA│1980│09│33.4 │14.8│
├───┼────┼──┼─────┼────┤
│SMA│1981│09│196.7│13.9│
├───┼────┼──┼─────┼────┤
│SMA│1982│09│51.2 │15.8│
├───┼────┼──┼─────┼────┤
│SMA│1983│09│116.6│14.4│
├───┼────┼──┼─────┼────┤
│SMA│1984│09│229  │12.6│
├───┼────┼──┼─────┼────┤
│SMA│1985│09│44.9 │15.4│
├───┼────┼──┼─────┼────┤
│SMA│1986│09│29.3 │13.6│
├───┼────┼──┼─────┼────┤
│SMA│1987│09│168.2│16.7│
├───┼────┼──┼─────┼────┤
│SMA│1988│09│69.9 │13.9│
├───┼────┼──┼─────┼────┤
│SMA│1989│09│72.9 │13.9│
├───┼────┼──┼─────┼────┤
│SMA│1990│09│128.3│13.2│
├───┼────┼──┼─────┼────┤
│SMA│1991│09│83.5 │16.4│
├───┼────┼──┼─────┼────┤
│SMA│1992│09│60.7 │14.4│
├───┼────┼──┼─────┼────┤
│SMA│1993│09│72.4 │13  │
├───┼────┼──┼─────┼────┤
│SMA│1994│09│98.2 │13.7│
├───┼────┼──┼─────┼────┤
│SMA│1995│09│87.1 │11.9│
├───┼────┼──┼─────┼────┤
│SMA│1996│09│51.9 │10.8│
├───┼────┼──┼─────┼────┤
│SMA│1997│09│42.7 │14.9│
├───┼────┼──┼─────┼────┤
│SMA│1998│09│170.2│13.2│
├───┼────┼──┼─────┼────┤
│SMA│1999│09│129.3│16.6│
├───┼────┼──┼─────┼────┤
│SMA│2000│09│127.5│14.9│
├───┼────┼──┼─────┼────┤
│SMA│2001│09│189.3│11.5│
├───┼────┼──┼─────┼────┤
│SMA│2002│09│123.4│12.8│
├───┼────┼──┼─────┼────┤
│SMA│2003│09│45.6 │14.5│
├───┼────┼──┼─────┼────┤
│SMA│2004│09│72.5 │15.2│
├───┼────┼──┼─────┼────┤
│SMA│2005│09│53   │15.6│
├───┼────┼──┼─────┼────┤
│SMA│2006│09│110.4│16.9│
├───┼────┼──┼─────┼────┤
│SMA│2007│09│75.1 │13  │
├───┼────┼──┼─────┼────┤
│SMA│2008│09│117.1│12.3│
├───┼────┼──┼─────┼────┤
│SMA│2009│09│68.7 │15.4│
├───┼────┼──┼─────┼────┤
│SMA│2010│09│89.2 │13.2│
├───┼────┼──┼─────┼────┤
│SMA│2011│09│74.2 │16.4│
├───┼────┼──┼─────┼────┤
│SMA│2012│09│103.6│14.2│
└───┴────┴──┴─────┴────┘

In [4]:
NB. Extract data for visualization, and calculate linear regression
ra3d =: (150,12)$,/>3{|:(data gets 'SMA')
te3d =: (150,12)$,/>4{|:(data gets 'SMA')
rain =: ,/>3{|:d
temp =: ,/>4{|:d
rreg =: ((1{((i.#rain) lreg rain))*(i.#rain))+(0{((i.#rain) lreg rain))
treg =: ((1{((i.#temp) lreg temp))*(i.#temp))+(0{((i.#temp) lreg temp))

In [5]:
NB. Set some plot options
xtin =: ":((i.16)*((#d)%15))
xlan =: yeal
xtit =: ":((i.16)*((#d)%15))
xlat =: yeal

In [6]:
NB. Plot/Visualize data in 4 sub-plots

pd 'reset'
pd 'sub 4 1'

pd 'new'
pd 'title Rain SMA / September'
pd 'xticpos ',xtin
pd 'xlabel  ',xlan
pd rain
pd rreg

pd 'new'
pd 'title Temperature SMA / September'
pd 'xticpos ',xtit
pd 'xlabel  ',xlat
pd temp
pd treg

pd 'new'
pd 'titlecolor white'
pd 'title Rain'
pd 'yticpos ',monx
pd 'ylabel ',monl
pd 'xticpos ',":((i.16)*((#ra3d)%15))
pd 'xlabel ',yeal
pd 'type surface'
pd 'backcolor black'
pd 'axiscolor white'
pd 'labelcolor white'
pd 'bandcolor |.bgclr'
pd 'viewpoint ',":(vpox,vpoy,vpoz)
pd ni3d

pd 'new'
pd 'keyfont arial 4'
pd 'titlecolor white'
pd 'title Temperature'
pd 'yticpos ',monx
pd 'ylabel ',monl
pd 'xticpos ',":((i.16)*((#te3d)%15))
pd 'xlabel ',yeal
pd 'type surface'
pd 'backcolor black'
pd 'axiscolor white'
pd 'labelcolor white'
pd 'viewpoint ',":(vpox,vpoy,vpoz)
pd te3d

CANVAS_DEFSIZE_jwplot_ =: 600 1200
pd 'show'


J Plot

In [ ]: