Skip to content

Instantly share code, notes, and snippets.

@theecanmole
theecanmole / NZ emission unit allocation 2018
Created September 2, 2019 07:53
New Zealand ETS free allocation of emission units to the Top Ten NZ Emitters 2018
top10names<-c("NZ Steel", "NZ Aluminium Smelters", "Methanex NZ", "Fletcher Concrete", "Oji Fibre", "Ballance Agri-Nutrients", "Pan Pac Forest Products", "Norske Skog Tasman", "Winstone Pulp", "Graymont","Others")
top10allocation <-c(1782366, 1324556, 945210, 584032, 484322, 325594, 210652, 200556, 151546, 144405,590334 )
palettepair11<-brewer.pal(11, "Paired")
png("nzu-allocations-pie-2018-565.png", width=565, height=424, pointsize = 14)
pie(top10allocation,labels=c(top10names),radius = 0.99,clockwise = FALSE,init.angle=260, col = palettepair11, cex.lab=1.1,cex.main=1.5,main="NZETS free allocation of emission units\nto the Top Ten Emitters 2018")
mtext(side=1,cex=0.9,line=2.6,"Source: https://www.epa.govt.nz/industry-areas/\nemissions-trading-scheme/industrial-allocations/decisions/")
mtext(side=1,cex=1,line=0.4,"Of 6.7 million units allocated to industry, 6.2 million (91%) went to 10 companies")
dev.off()
@theecanmole
theecanmole / NZ-GHG-Net-By-Sectors-1990-2015.r
Created March 25, 2018 07:21
New Zealand Greenhouse Gas Emissions Net and By Sectors 1990-2015 data preparation and chart
# open xterminal, change directory, use wget command to download "2017%20CRF%20Summary%20data.xlsx" to /nzghg2015 folder
simon@I6:~$ cd /home/simon/R/nzghg2015/
simon@I6:~/R/nzghg2015 $ wget http://www.mfe.govt.nz/sites/default/files/media/2017%20CRF%20Summary%20data.xlsx
# use Gnumeric 'ssconvert' convert .xlsx file and all work sheets to separate csv files with sheet number appended in place of "%n" and the most recent year of emissions (not the year of publication)
simon@I6:~ $ ssconvert -S --export-file-per-sheet '2017 CRF Summary data.xlsx' crf-data-2015-%n.csv
# crf-data-2015-0.csv is emissions by sector
# to read the .csv data file to the R workspace as a dataframe
gsector2015 <- read.csv("/home/simon/R/nzghg2015/crf-data-2015-0.csv", skip=2, header=TRUE, sep=",", colClasses ="numeric", nrows=27, na.strings=NA, dec=".", strip.white=TRUE)
# Simplify the column names (vector names)
colnames(gsector2015) <- c("Year", "Energy", "Industrial", "Agriculture", "LULUCF", "Waste", "Netemissions", "Grossemissio
@theecanmole
theecanmole / WQdailymeansEcoli-csv.r
Created July 22, 2017 04:19
Load WQdailymeansEcoli.rdata, write as .csv format file
# load .rdata format file into work space
load (file="/home/R/WQdailymeansEcoli.rdata", envir=globalenv())
# write .rdata format file to .csv format
write.csv(WQdailymeansEcoli, file = "WQdailymeansEcoli.csv",row.names=FALSE)
@theecanmole
theecanmole / WQdailymeansEcoli.r
Created July 16, 2017 08:03
New Zealand Water Quality network E Coli daily means to CSV file
write.table(WQdailymeansEcoli, file = "WQdailymeansEcoli.csv", sep = ",", col.names = TRUE, qmethod = "double",row.names = FALSE)
@theecanmole
theecanmole / NZ-GHG-data-1990-2015chart
Created June 17, 2017 08:16
Make the chart of NZ greenhouse gases 1990 to 2015 by sector
png("NZ-ghgsector-2015-560.png", bg="white", width=560, height=420,pointsize = 14)
par(mar=c(2.7,2.7,1,1)+0.1)
plot(gsector2015[["Year"]],gsector2015[["Agriculture"]]/1000,ylim=c(-42,60), xlim=c(1989,2015),tck=0.01,axes=FALSE,ann=FALSE, type="n",las=1)
axis(side=1, tck=0.01, las=0, lwd = 1, at = c(1990:2015), labels = c(1990:2015), tick = TRUE)
axis(side=2, tck=0.01, las=2, line = NA,lwd = 1, at = c(-30,-20,-10,0,10,20,30,40), labels = c(-30,-20,-10,0,10,20,30,40),tick = TRUE)
axis(side=3, tck=0.01, at = c(1990:2015), labels = FALSE, tick = TRUE)
axis(side=4, tck=0.01, at = c(-30,-20,-10,0,10,20,30,40), labels = FALSE, tick = TRUE)
box(lwd=1)
lines(gsector2015[["Year"]],gsector2015[["Agriculture"]]/1000,col="#1b9e77",lwd=1)
points(gsector2015[["Year"]],gsector2015[["Agriculture"]]/1000,col="#1b9e77",cex=1,pch=7)
@theecanmole
theecanmole / NZ-GHG-data-2015-read-in-as-dataframe
Created June 17, 2017 08:12
Reading in the NZ Greenhouse Gas Inventory 2015 data as dataframe
# to read the .csv data file of GHG emissions by sector to the R workspace as a dataframe
gsector2015 <- read.csv("/home/user/R/nzghg2015/crf-data-2015-0.csv", skip=2, header=TRUE, sep=",", colClasses ="numeric", nrows=27, na.strings=NA, dec=".", strip.white=TRUE)
# examine the dataframe
str(gsector2015)
data.frame: 26 obs. of 8 variables:
$ Year : num 1990 1991 1992 1993 1994 ...
$ Energy. : num 23748 24140 26041 25475 25762 ...
$ Industrial.processes.and.product.use..IPPU. : num 3584 3733 3378 3218 3098 ...
$ Agriculture. : num 33123 33471 33149 33364 34340 ...
$ Land.use..land.use.change.and.forestry..LULUCF.: num -30122 -32130 -31620 -32263 -32317 ...
@theecanmole
theecanmole / wgetting-NZ-GHG-data-2015
Last active June 17, 2017 08:07
Wget the NZ GHG inventory 2015 summary Excel sheet and convert to CSV files
# open an XFCE terminal window, change directory, use the wget command to download "2017%20CRF%20Summary%20data.xlsx" to /nzghg2015 folder
$ cd /home/user/R/nzghg2015/
$ wget http://www.mfe.govt.nz/sites/default/files/media/2017%20CRF%20Summary%20data.xlsx
# use Gnumeric via the command line to convert the .xlsx file and it's work sheets to separate .csv files with sheet number appended in place of "%n" and the most recent year of emissions (not the year of publication)
$ ssconvert -S --export-file-per-sheet '2017 CRF Summary data.xlsx' crf-data-2015-%n.csv
# list contents of folder to see the two new csv data files
$ ls -l
total 180
-rw-r--r-- 1 user user 46759 May 23 10:49 2017 CRF Summary data.xlsx
-rw-r--r-- 1 user user 3749 Jun 14 14:26 crf-data-2015-0.csv
@theecanmole
theecanmole / chart-of-NZ-greenhouse-gases-2015
Created June 6, 2017 08:25
R script for chart of New Zealand Greenhouse Gas emissions 1990 to 2015
png("NZ-ghg-2015-560.png", bg="white", width=560, height=420,pointsize = 14)
par(mar=c(2.7,2.7,1,1)+0.1)
plot(year2015,gross2015/1000,ylim=c(0,99.5), xlim=c(1989,2015),tck=0.01,axes=FALSE,ann=FALSE, type="n",las=1)
axis(side=1, tck=0.01, las=0, lwd = 1, at = c(1990:2015), labels = c(1990:2015), tick = TRUE)
axis(side=2, tck=0.01, las=2, line = NA,lwd = 1, at = c(0,10,20,30,40,50,60,70,80), labels = c(0,10,20,30,40,50,60,70,80),tick = TRUE)
axis(side=3, tck=0.01, at = c(1990:2015), labels = FALSE, tick = TRUE)
axis(side=4, tck=0.01, at = c(0,10,20,30,40,50,60,70,80), labels = FALSE, tick = TRUE)
box(lwd=1)
lines(year2015,gross2015/1000,col="#000099",lwd=1)
points(year2015,gross2015/1000,col="#000099",cex=1.3,pch=17)
@theecanmole
theecanmole / reading in plain text lists into R
Last active June 6, 2017 08:23
Copying a column from Excel to Convert-Town tool to get a comma-separated plain text list
net2015 <-c(34451.43, 33428.11, 35249.79, 34181.32, 35205.70, 37595.94, 40173.47, 41887.90, 39179.52,39583.29,42774.45,46205.88,49178.70,50470.09,50070.49,53676.77,55381.04,55146.23,46551.24,47416.85,47462.99,51792.44,54446.07,55242.18,56471.91,56372.51)
gross2015 <-c(64573.82, 65558.53, 66869.50, 66444.14, 67522.89, 68447.75,70592.31,73322.21,71020.33,73049.33,75142.80,77955.23,78519.60,80929.90,80530.14,82493.69,82550.94,80168.82,80204.79,77710.07,78077.20,77951.12,79929.55,79397.17,80267.87,80155.14)
year2015 <-c(1990:2015)
@theecanmole
theecanmole / chart-co2-mm-mlo
Last active February 25, 2017 01:18
chart of concentrations of atmospheric carbon dioxide Mauna Loa Observatory
mlo <- read.csv("/home/simon/R/co2-ppm-master/data/co2-mm-mlo.csv", skip = 0,header=TRUE, sep=",", na.strings="-99.99", dec=".", strip.white=TRUE)
png("/home/simon/R/co2-ppm-master/TrendsinCarbonDioxideMaunaLoa560.png", width=560, height=420, pointsize = 14)
par(mar=c(2.3,2.8,1,1.5)+0.1)
plot(mlo[["Decimal.Date"]], mlo[["Interpolated"]],tck=0.01,ann=FALSE, type="n",las=1)
axis(side=1, tck=0.01, tick = TRUE)
axis(side=4, tck=0.01, at = NULL, labels = FALSE, tick = TRUE)
lines(mlo[["Decimal.Date"]], mlo[["Interpolated"]],col=2,lwd=1)
mtext(side=3,cex=1.5, line=-3.8,expression(paste("Trends in Atmospheric Carbon Dioxide, \nMauna Loa, Hawaii 1958 - 2017")) )
mtext(side=1,cex=0.75, line=-1.5,"Data: NOAA/Scripps Institution of Oceanography \nvia https://github.com/datasets/co2-ppm")
mtext(side=2,cex=1, line=-1.5,expression(paste("parts per million")))