Skip to content

Instantly share code, notes, and snippets.

@robinkraft
robinkraft / forma2csv.do
Created February 10, 2012 22:52
Export FORMA csv
outsheet modh modv sample line hansen prob$start_datestr-prob$end_datestr using "/Users/robin/code/forma-analysis/data/$iso$underscore$bboxname.csv" if prob$end_datestr !=. & lat <= $top & lat >= $bottom & lon >= $left & lon <= $right, comma replace
@robinkraft
robinkraft / forma_reshape.do
Created February 10, 2012 22:53
Reshaping FORMA data in Stata
* Snippit is a useful reminder of how Stata does reshapes
reshape long prob, string i(yearmo)
reshape long prob, string
reshape long i, string i(iso) j(yo)
reshape long prob, string i(id2) j(yearmo)
replace yearmo = dotm(yearmo)
@robinkraft
robinkraft / move_modis.sh
Created February 10, 2012 23:04
Move MODIS files between s3 buckets
s3cmd cp --recursive s3://formastaging/MOD13A3/2011-10-01/ s3://modisfiles/MOD13A3/2011-10-01/
s3cmd cp --recursive s3://formastaging/MOD13A3/2011-11-01/ s3://modisfiles/MOD13A3/2011-11-01/
s3cmd cp --recursive s3://formastaging/MOD13A3/2011-12-01/ s3://modisfiles/MOD13A3/2011-12-01/
@robinkraft
robinkraft / replace_hansen_nodata.py
Created February 13, 2012 00:52
Replace Hansen missing values -9999 with 0
# gdal_translate -ot Byte -of GTiff -a_nodata 0 IDN_new.asc IDN.tif
fp = open("IDN.asc", "r")
fout = open("IDN_new.asc", "w")
n = 0
for line in fp:
# replace -9999 with zeros - much smaller file
newline = "0".join(line.split("-9999"))
@robinkraft
robinkraft / first_cascalog_queries.clj
Created February 13, 2012 03:10
Explorations with Cascalog
;; Sam, I got up to 21, some of which now reside forma-clj
(def nums
[1
2
3
4
5
6])
@robinkraft
robinkraft / indexed_image.py
Created February 13, 2012 03:05
Convert an image file readable by GDAL into a row-indexed text file
import numpy as np
from osgeo import gdal
def progress(n, interval=100):
"Print progress at regular intervals"
# don't want to divide by zero on first try
if n > 0:
if n % interval == 0:
print "Parsing item %i" % n
return
@robinkraft
robinkraft / drops.do
Created February 13, 2012 19:23
what to drop from FORMA data
drop if prob201108 == . & hansen == 0
drop country ecoid gadm coastdist modh modv sample line prob200512
drop if prob201108 < 50 & hansen == 0
outsheet using "/Users/robin/Dropbox/code/forma-app/data/IDN.csv", comma noquote
@robinkraft
robinkraft / gist:1829381
Created February 14, 2012 19:16
csv-ify some ints and a vector
(let [lat 90
lon -180
series [1 2 3]
j (partial join ",")]
(j [lat (j [lon (j series)])]))
@robinkraft
robinkraft / gist:1952544
Created March 1, 2012 19:41
Want to define a function at master node repl, have it accessible in query
(defn get-max
[v]
(apply max (flatten v)))
(let [src (hfs-seqfile "s3n://formaresults/finaloutput/*")]
(??<- [?count]
(src ?s-res ?mod-h ?mod-v ?s ?l ?prob-series)
(get-max ?prob-series :> ?max)
(>= ?max 0.5)
(c/count ?count)))
(defmain through-reli
[tmp-root pail-path ts-pail-path out-path run-key country-seq]
(let [{:keys [s-res t-res est-end] :as est-map} (forma-run-parameters run-key)
mk-filter (fn [vcf-path ts-src]
(forma/filter-query (hfs-seqfile vcf-path)
(:vcf-limit est-map)
ts-src))]
(assert est-map (str run-key " is not a valid run key!"))
(workflow [tmp-root]
vcf-step