Created
April 7, 2015 00:10
-
-
Save robinkraft/7fd05925fa25632b9c3f to your computer and use it in GitHub Desktop.
downloading Hansen 2013 data from http://earthenginepartners.appspot.com/science-2013-global-forest/download_v1.1.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): | |
print 'Downloading %s' % url | |
print 'Saving to %s' % output | |
r = requests.get(url, stream = True) | |
with open(output, 'wb') as f: | |
if not r.status_code == 404: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: | |
f.write(chunk) | |
print "Download complete\n\n" | |
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