Last active
August 29, 2015 13:56
-
-
Save robinkraft/9221298 to your computer and use it in GitHub Desktop.
sample script to download UMD forest cover loss data (untested) http://www.earthenginepartners.appspot.com/science-2013-global-forest/download.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import requests | |
basefname = 'Hansen_GFC2013_lossyear_%d%s_%03d%s.tif' | |
baseurl = 'http://commondatastorage.googleapis.com/earthenginepartners-hansen/GFC2013/%s' | |
def download(url, output): | |
r = requests.get(url, stream = True) | |
if not r.status_code == 404: | |
print 'Downloading %s' % url | |
print 'Saving to %s' % output | |
with open(output, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: | |
f.write(chunk) | |
print "Download complete\n\n" | |
else: | |
print "Skipping %s - does not exist" % os.path.split(output)[1] | |
def northing(lat): | |
if lat >= 0: | |
return 'N' | |
else: | |
return 'S' | |
def easting(lon): | |
if lon >= 0: | |
return 'E' | |
else: | |
return 'W' | |
def gen_fnames(lat, lon, path): | |
# e.g. '/tmp/Hansen_GFC2013_lossyear_50N_010W.tif' | |
north = northing(lat) | |
east = easting(lon) | |
fname = basefname % (abs(lat), north, abs(lon), east) | |
url = baseurl % fname | |
local_path = os.path.join(path, fname) | |
return dict(url=url, path=local_path) | |
def get_file(lat, lon, path): | |
name_dict = gen_fnames(lat, lon, path) | |
download(name_dict['url'], name_dict['path']) | |
return | |
def main(path='/tmp'): | |
for lat in range(-90, 90 + 1, 10): | |
for lon in range(-180, 180 + 1, 10): | |
get_file(lat, lon, path) | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
main(sys.argv[1]) | |
else: | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment