#!/usr/bin/python

#
# this tiny bit-o-python grabs ionograms
# from the CL424 ionosonde site
#

import time, shutil, sys, string, os, urllib, datetime
from time import gmtime, strftime, strptime, mktime

_DESTINATION="/data/incoming/NonStandard/CL424/"
_IONOGRAM_PREFIX="http://211.21.47.74/Ionosphere/Pics/"
_IONOGRAM_SUFFIX=".gif"

n=gmtime()
t=datetime.datetime(n[0],n[1],n[2],n[3],n[4],0)
t-=datetime.timedelta(minutes=10)
t-=datetime.timedelta(minutes=t.minute % 5,seconds=t.second,microseconds=t.microsecond)

# adjust from UTC to Taipai
t+=datetime.timedelta(hours=+8)

yyyymo=t.strftime("%Y")
mo=t.strftime("%m")
dd=t.strftime("%d")
hh=t.strftime("%H")
mm=t.strftime("%M")
ss=t.strftime("%S")
iofl="/tmp/"+"HI"+yyyymo+mo+dd+hh+mm+ss+_IONOGRAM_SUFFIX
iofr=_IONOGRAM_PREFIX+yyyymo+mo+"/"+dd+"/HI"+yyyymo+mo+dd+hh+mm+ss+_IONOGRAM_SUFFIX
print iofl
urllib.urlretrieve(iofr, iofl)
shutil.move(iofl, _DESTINATION)