-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhimawari_lowres.py
171 lines (136 loc) · 4.94 KB
/
himawari_lowres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
from os.path import abspath, dirname, join
import datetime
import requests
import subprocess
import shlex
from PIL import Image
from common import get_api, set_up_logging
BASE_DIR = dirname(abspath(__file__))
LOGFILE = join(BASE_DIR, 'lowres.log')
LOWRES_FOLDER = join(BASE_DIR, 'lowres/')
JMA_URL = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/1d/550/"
NOAA_URL = "ftp://ftp.nnvl.noaa.gov/GOES/HIMAWARI/simplecontrast/"
def round_time_10(dt):
# Round date down to nearest 0.1 hour, then remove seconds & microseconds
logger.debug("Rounding {0}".format(dt))
rounded_date = dt - datetime.timedelta(minutes=dt.minute % 10)
rounded_date = rounded_date.replace(second=0, microsecond=0)
# Give them time to upload new image
rounded_date -= datetime.timedelta(minutes=10)
logger.debug("Rounded to {0}".format(rounded_date))
return rounded_date
def date_to_jma_filename(date):
"""
Just for the JMA images. Converts a datetime object into a filename in the
form of:
"http://himawari8-dl.nict.go.jp/himawari8/img/D531106/1d/550/2016/10/23/115000_0_0.png"
Args:
datetime: datetime object
Returns:
filename (str): I.e., 201510311230-00.png
"""
return date.strftime("%Y/%m/%d/%H%M00_0_0.png")
def get_jma_images(start_time=None, num=48):
"""
Get the most recent [num] images from the JMA.
Returns:
images (list): List of [num] filenames for images to download.
"""
logger.debug('Getting images')
if not start_time:
start_time = round_time_10(datetime.datetime.utcnow())
logger.debug("Start time: {0}".format(start_time))
images = []
for i in range(0, num):
fn = date_to_jma_filename(round_time_10(start_time))
images.append(fn)
start_time -= datetime.timedelta(minutes=10)
logger.debug('using image list: {0}'.format(images))
return images
def delete_old_images(num=48):
"""
Only keep the most recent `num` images (my server is not *that* large)
"""
logger.debug("Starting delete of old images")
images = sorted(os.listdir(LOWRES_FOLDER))
num_images_to_del = len(images) - num
if num_images_to_del <= 0:
return True
images_to_del = images[:abs(num_images_to_del)]
for img in images_to_del:
if os.path.splitext(img)[1] != '.png':
continue
logger.debug('deleting {0}'.format(img))
os.remove(join(LOWRES_FOLDER, img))
return True
def process_image(image):
logger.debug('processing image: {0}'.format(image))
try:
im = Image.open(image)
old_size = im.size
new_size = (650, 650)
new_im = Image.new("RGB", new_size)
new_im.paste(im, ((new_size[0] - old_size[0]) // 2,
(new_size[1] - old_size[1]) // 2))
new_im = new_im.resize((500, 500))
new_im.save(image)
except Exception as e:
logger.error(str(e))
def download_jma_images():
"""
Downloads the most recent(ish) 20 images from the JMA.
Returns:
ISO date of last image.
"""
logger.debug('downloading images')
rounded_now = round_time_10(datetime.datetime.utcnow())
logger.debug('rounded time is {0}'.format(rounded_now))
images = get_jma_images(start_time=rounded_now)
logger.debug('images: {0}'.format(str(images)))
old_images = os.listdir(LOWRES_FOLDER)
logger.debug('old_images: {0}'.format(old_images))
for image in images:
image_name = image.replace('/', '')
if image_name in old_images:
logger.debug('skipping {0}'.format(image_name))
continue
logger.debug('downloading {0}'.format(image_name))
image_url = JMA_URL + image
data = requests.get(image_url)
if len(data._content) < 2**13:
continue
with open(LOWRES_FOLDER + image_name, 'wb') as f:
f.write(data._content)
process_image(LOWRES_FOLDER + image_name)
return rounded_now.isoformat()
def images_to_gif():
logger.debug('creating GIF')
cmd = ("{0}/mp4_to_gif.sh {0} {0}/gif.gif".format(BASE_DIR))
subprocess.call(shlex.split(cmd))
return os.path.realpath("{0}/gif.gif".format(BASE_DIR))
def tweet_gif(gif, status):
logger.debug("Starting to tweet")
api = get_api()
uploaded_gif = api.UploadMediaChunked(media=gif, media_category="tweet_gif")
logger.debug('media ID: {0}'.format(uploaded_gif))
status = api.PostUpdate(status=status, media=uploaded_gif)
logger.debug('Finished tweet: {0}'.format(status))
return status
def main():
status = download_jma_images()
delete_old_images()
gif = images_to_gif()
try:
tweet_gif(gif, status)
except Exception as e:
logger.critical(str(e))
def make_local_gif():
status = download_jma_images()
gif = images_to_gif()
print(status)
print(gif)
if __name__ == '__main__':
logger = set_up_logging(log_file=LOGFILE)
logger.info('Started program')
main()