Create a gist now

Instantly share code, notes, and snippets.

Embed
Himawari 8 - Earth photos downloading
'''
Himawari 8 - Earth
Usage: python earth.py 2 10
First argument: quality of photo
Second argument: count of downloaded photos (default 1)
'''
from datetime import datetime, timedelta
from math import sqrt
from PIL import Image
import urllib2
from sys import argv
import hashlib
import io
try :
rank = int(argv[1]) # can be 2, 4, 8, 16, 20
except:
print 'Takes 0 arguments (1 expected)'
exit (1)
try :
count = int(argv[2]) # count of photos
except:
count = 1 # default: one photo
width = 550
size = int(rank**2)
base = 'http://himawari8-dl.nict.go.jp/himawari8/img/D531106/'
year, month, day, hour, minute = datetime.strftime(datetime.now(), '%Y %m %d %H %M').split(' ')
prefix = '{0}{1}d/{2}/{3}/{4}'.format(base, str(rank), str(width), year, month)
day = int(day)
hour = int(hour)
minute = int(minute[0])
while day >= 0 and count:
str_day = '0' + str(day) if day <= 9 else str(day)
str_hour = '0' + str(hour) if hour <= 9 else str(hour)
str_minute = str(minute) + '0'
url = '{0}/{1}/{2}{3}00'.format(prefix, str_day, str_hour, str_minute) + '_{0}_{1}.png'
print 'Search by date: {0}/{1}/{2} {3}:{4}:00'.format(year, month, str_day, str_hour, str_minute)
try:
h1 = hashlib.md5(urllib2.urlopen(url.format('0', '0')).read()).hexdigest()
h2 = hashlib.md5(urllib2.urlopen(url.format(str(rank/2), str(rank/2))).read()).hexdigest()
if h1 == h2:
raise urllib2.HTTPError("Error")
image = Image.new("RGB", (width * rank, width * rank))
print 'Found pictures'
current_percent = -1
for index in xrange(size):
percent = int((index + 1) * (100.0 / size))
if percent != current_percent:
current_percent = percent
print 'Concate pictures: {0}%'.format(str(current_percent))
row = index / rank
column = index % rank
path = io.BytesIO(urllib2.urlopen(url.format(str(row), str(column))).read())
image.paste(Image.open(path), (row * width, column * width))
photo_name = 'earth_{0}.{1}.{2}_{3}:{4}:{5}.png'.format(str_day, month, year, str_hour, str_minute, '00')
image.save(photo_name)
print 'Saved: {0}'.format(photo_name)
count = count - 1
except KeyboardInterrupt:
print '\nSearching was terminated'
exit(1)
except:
pass
if minute == 0:
minute = 5
hour = hour - 1
else:
minute = minute - 1
if minute == 0 and hour == 0:
minute = 5
hour = 23
day = day - 1
if minute == 0 and hour == 0 and day == 0:
print 'Url not found'
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment