Skip to content

Instantly share code, notes, and snippets.

@celoyd
Last active August 8, 2023 05:05
Show Gist options
  • Save celoyd/39c53f824daef7d363db to your computer and use it in GitHub Desktop.
Save celoyd/39c53f824daef7d363db to your computer and use it in GitHub Desktop.

Revisions

  1. celoyd revised this gist Jun 14, 2016. 1 changed file with 27 additions and 4 deletions.
    31 changes: 27 additions & 4 deletions hi8-fetch.py
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,32 @@ def pathfor(t, x, y):
    return "%s/%s/%02d/%02d/%02d%02d00_%s_%s.png" \
    % (base, t.year, t.month, t.day, t.hour, t.minute, x, y)

    def fetch(session, path, retries=1, verbose=False, failure='error'):
    # FIXME: this is kinda messy:
    if retries < 0:
    if failure == 'warning':
    raise Warning('Could not download %s (filling with black)')
    return np.zeros((width, height, 3))
    elif failure == 'silent':
    return np.zeros((width, height, 3))
    else: # presumed failure == 'error':
    raise IOError('Could not download %s')

    try:
    tiledata = session.get(path).content
    tile = Image.open(StringIO(tiledata))
    return tile
    except:
    if verbose:
    print('Retrying %s (%s retries left)' % (path, retries))
    return fetch(
    session,
    path,
    retries=(retries - 1),
    verbose=verbose,
    failure=failure
    )


    sess = req.Session() # so requests will reuse the connection
    png = Image.new('RGB', (width*scale, height*scale))
    @@ -43,10 +69,7 @@ def pathfor(t, x, y):
    for y in range(scale):

    path = pathfor(time, x, y)
    # print("fetching %s" % (path))
    tiledata = sess.get(path).content
    tile = Image.open(StringIO(tiledata))

    tile = fetch(sess, path, retries=4, verbose=True, failure='error')
    png.paste(tile, (width*x, height*y, width*(x+1), height*(y+1)))

    png.save(out, 'PNG')
  2. celoyd revised this gist Jan 15, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hi8-fetch.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    # hi8-fetch.py <date> <zoom level> <output>
    # E.g.: hi8-fetch.py 2016-01-13T22:10:00 8 2016-01-13T221000-z8.png
    # Fetch Himawari-8 full disks at a given zoom level.
    # Valid zoom levels seem to be powers of 2, 1..16.
    # Valid zoom levels seem to be powers of 2, 1..16, and 20.
    #
    # To do:
    # - Better errors (e.g., catch the "No Image" image).
  3. celoyd revised this gist Jan 14, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hi8-fetch.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import requests as req
    import sys
    from dateutil.parser import parse
    from PIL import Image, ImageDraw
    from PIL import Image
    from StringIO import StringIO

    # hi8-fetch.py <date> <zoom level> <output>
  4. celoyd created this gist Jan 14, 2016.
    52 changes: 52 additions & 0 deletions hi8-fetch.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import requests as req
    import sys
    from dateutil.parser import parse
    from PIL import Image, ImageDraw
    from StringIO import StringIO

    # hi8-fetch.py <date> <zoom level> <output>
    # E.g.: hi8-fetch.py 2016-01-13T22:10:00 8 2016-01-13T221000-z8.png
    # Fetch Himawari-8 full disks at a given zoom level.
    # Valid zoom levels seem to be powers of 2, 1..16.
    #
    # To do:
    # - Better errors (e.g., catch the "No Image" image).
    # - Don't ignore seconds, and/or:
    # - option to snap to nearest valid time.
    # - Librarify.


    # Tile size for this dataset:
    width = 550
    height = 550


    time = parse(sys.argv[1])
    scale = int(sys.argv[2])
    out = sys.argv[3]


    base = 'http://himawari8.nict.go.jp/img/D531106/%sd/550' % (scale)

    tiles = [[None] * scale] * scale


    def pathfor(t, x, y):
    return "%s/%s/%02d/%02d/%02d%02d00_%s_%s.png" \
    % (base, t.year, t.month, t.day, t.hour, t.minute, x, y)


    sess = req.Session() # so requests will reuse the connection
    png = Image.new('RGB', (width*scale, height*scale))

    for x in range(scale):
    for y in range(scale):

    path = pathfor(time, x, y)
    # print("fetching %s" % (path))
    tiledata = sess.get(path).content
    tile = Image.open(StringIO(tiledata))

    png.paste(tile, (width*x, height*y, width*(x+1), height*(y+1)))

    png.save(out, 'PNG')