Skip to content

Instantly share code, notes, and snippets.

View barryrowlingson's full-sized avatar

Barry Rowlingson barryrowlingson

View GitHub Profile
@barryrowlingson
barryrowlingson / smarties.R
Created March 4, 2015 12:03
Smarties colour palette
### Colours of old Smarties with lovely artificial colouring, derived from:
###
###
### "Smarties old new" by John Penton and Paul Hughes - Own work. Licensed under CC BY-SA 2.5 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:Smarties_old_new.jpg#mediaviewer/File:Smarties_old_new.jpg
###
### image was loaded into gimp, representative parts of each Smartie were isolated, the image converted to indexed palette
### with 9 colours, the palette then saved.
##
### "Smarties" is a trademark of Nestle
##
@barryrowlingson
barryrowlingson / areaLL.R
Created February 19, 2015 22:21
compute surface area for lat-long grids.
areaLL <- function(r){
## cell areas in km^2
areas = area(r)
## area of non-NA in km^2
## this is the flat area
a = sum(values(area(r)*(!is.na(r))))
centre = cellFromRowCol(r, nrow(r)/2, ncol(r)/2)
## compute w/h of middle cell
a1 = areas[centre]
@barryrowlingson
barryrowlingson / mdiff
Created January 27, 2015 15:36
sequential file diff
#!/bin/bash
if [ "$#" == "0" ]; then
echo "diff [file1] [file2] ..."
echo " Does diff file1 file2 ; diff file2 file3 ; diff file3 file4 ; etc"
exit 1
fi
first="$1"
shift
@barryrowlingson
barryrowlingson / readit.R
Created December 5, 2014 23:21
read some data file..
readit = function(filename){
lines = readLines(filename)
whichdata = grep("^[0-9]{4}",lines)
headers = read.table(textConnection(lines[min(whichdata)-1]))
datalines = lines[whichdata]
dmat = read.table(textConnection(datalines),na.strings=c("***","****"))
names(dmat)=t(headers)[,1]
dmat
}
@barryrowlingson
barryrowlingson / convertPDF.sh
Created December 2, 2014 18:17
Convert PDF to images in a word doc
#!/bin/bash
mkdir -p pages
rm pages/image*png
convert -verbose -colorspace RGB -units PixelsPerInch -resize 1024 -geometry 1224 -interlace none -density 200 -quality 80 $1 pages/image.png
npages=`ls pages/image-*.png| wc -l`
for (( i=0 ; i<$npages ; i++ ))
do
echo '![](pages/image-'$i'.png)'
echo " "
@barryrowlingson
barryrowlingson / submitR.sh
Last active April 8, 2022 16:21
Run R scripts in SGE
#!/bin/bash
. /etc/profile
usage () {
printf "Usage: %s: [-h] -m -d -c -n name -j jobname -b Bfile -e Efile <Rfile> [Rargs]\n" $(basename $0) >&2
}
mflag=
nflag=
@barryrowlingson
barryrowlingson / GlobalLandCover.xml
Created October 1, 2014 10:51
Global Land Cover WMS XML Spec for QGIS
<!DOCTYPE connections>
<qgsWMSConnections version="1.0">
<wms ignoreGetMapURI="true" smoothPixmapTransform="false" dpiMode="7" password="" ignoreGetFeatureInfoURI="false" referer="" username="" url="http
://218.244.250.80:8080/erdas-apollo/coverage/CGLC" invertAxisOrientation="false" ignoreAxisOrientation="false" name="China Land Cover"/>
</qgsWMSConnections>
@barryrowlingson
barryrowlingson / dplyrclass.R
Created August 20, 2014 17:00
dplyr doesn't preserve data frame classes
## make a data frame with a new class
d=data.frame(x=1:10,y=rnorm(10))
class(d)=c("foo","data.frame")
class(d)
## [1] "foo" "data.frame"
## ordinary subsetting and subset preserve us:
##
@barryrowlingson
barryrowlingson / loadutils.R
Created August 20, 2014 15:45
Work with .RData files
# load an object 'name' from a .RData file 'file'.
# eg
# x1 = loadFrom("foo.RData","x")
# x2 = loadFrom("bar.RData","x")
#
# useful when you want to load from two or more files with objects with the same name and
# load(file) would stomp on the second one.
loadFrom=function(file, name){e=new.env();load(file,env=e);e[[name]]}
###
### superclassing is nice
###
require(dplyr)
require(testthat)
set.seed(310366)
classes = c("thingy","data.frame")