Created
July 22, 2016 16:58
-
-
Save mikeboers/ee771f9dae9f71a68d9eb528660f2140 to your computer and use it in GitHub Desktop.
Downloader for NASA's DSCOVR EPIC images
This file contains 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 argparse | |
import datetime | |
import json | |
import os | |
import re | |
import requests | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-r', '--root', default=os.path.abspath(os.path.join(__file__, '..', 'epic'))) | |
parser.add_argument('-d', '--dir-fmt', default='%Y/%m/%d') | |
parser.add_argument('date', nargs='+') | |
args = parser.parse_args() | |
for raw_date in args.date: | |
date_parts = re.split(r'\D+', raw_date) | |
date_parts = tuple(map(int, date_parts)) | |
assert len(date_parts) == 3 | |
res = requests.get('http://epic.gsfc.nasa.gov/api/images.php?date=%s-%s-%s' % date_parts) | |
images = res.json() | |
for meta in images: | |
date = datetime.datetime.strptime(meta['date'], '%Y-%m-%d %H:%M:%S') | |
dir_ = os.path.join(args.root, date.strftime(args.dir_fmt)) | |
if not os.path.exists(dir_): | |
os.makedirs(dir_) | |
meta_path = os.path.join(dir_, meta['image'] + '.json') | |
if not os.path.exists(meta_path): | |
with open(meta_path, 'w') as fh: | |
fh.write(json.dumps(meta, sort_keys=True, indent=4)) | |
png_path = os.path.join(dir_, meta['image'] + '.png') | |
if not os.path.exists(png_path) or not os.path.getsize(png_path): | |
png_url = 'http://epic.gsfc.nasa.gov/epic-archive/png/%s.png' % meta['image'] | |
print png_url | |
png_res = requests.get(png_url) | |
png_data = png_res.content # Force it to read it all. | |
with open(png_path, 'w') as fh: | |
fh.write(png_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment