Python Code -trials
Program
for determining the visible aurora in Finland
# Asks for the Location of the Weather station in LOCATION (FMI,
Finland)
# Downloads the fmi-page and using string search finds the
cloudiness
# 0/8-8/8. 8/8 is completely cloudy = 100%.
# It also gets the temperature and the sunset & sunrise
times
# Local time is retrived as well from the OS.
#
# The main executes and starts an eternal loop that calls
the function
# AnyAurora at time intervals. There are three locations,
that are altered
# for the weather data.
# -*- coding: cp1252 -*-
# We need modules outside the standard library, import those
import urllib2, time
def ReadNOAA():
# Opens the web file and returns the contents as a
string
try: response =
urllib2.urlopen('http://www.swpc.noaa.gov/ftpdir/latest/wwv.txt')
except:
print "Probem reading the
NOAA wwv file"
return False, ""
html = response.read()
return True, html
def ParseK(html):
# Parses the k index from the given string
html
alku= html.find("K-index")
if alku < 1:
return -1
# If we found it, find the next full stop, k-index
is right before it, make it into an integer
paikka = html.find(".",alku)
k = int(html[paikka-1])
return k
def ReadFMIPage(Location):
# reads the weather page, returns a string in
variable html
try: response =
urllib2.urlopen('http://ilmatieteenlaitos.fi/saa/' + Location + '/')
except:
print "Problem reading the
FMI data for " + Location
return False, ""
html = response.read()
return True, html
def Cloudiness(html):
# Finds the cloudiness index from the weather page
text string (html)
alku= html.find("/8")
if alku < 1:
print "Failed to read the
cloudiness in the weather data"
return -1
cloudiness = html[alku-1:alku+2]
print "Cloudiness " +
str(float(cloudiness[0])/8*100) + "%"
c = int(cloudiness[0])
return c
def Temperature(html):
# Looks for temperature i.e. "Lämpötila"
in the string (html)
alku = html.find('Lämpötila')
alku =
html.find(' °',alku) #
search for "deg", the temperature number ends there
apustring =
html[alku-6:alku]
# take a copy of a substring
apustring = apustring.replace(",",".",1) #
Replace the comma with dot, decimal
alku2 =
apustring.find(">")
# temperature is preceded by a ">"
Temp = float(apustring[alku2+1:alku-1])
# convert the substring with the number to a float
print "Temperature is " + str(Temp) + "C" # Let the
user know of the temp
return Temp
def Sun(html):
# looks for sunrise, sunset and local time
alku =
html.find("Auringonnousu")
# The page has the sunrise time "Auringonnousu"
apustring = html[alku:alku+50]
alku1 = apustring.find(":")
if apustring[alku1-2] == "1" :
print
apustring[alku1-2:alku1+3] + " Sunrise" #
else:
print
apustring[alku1-1:alku1+3] + " Sunrise" #
alku = html.find("Auringonlasku")
apustring = html[alku:alku+50]
alku1 = apustring.find(":")
print apustring[alku1-2:alku1+3] + " SunSet" #
We assume that sunset is always between 10:00 ... 23:59
print str(time.localtime(time.time()).tm_hour) + ":"
+ '{:02d}'.format((time.localtime(time.time()).tm_min)) + " Your Time
Now"
if int(time.localtime(time.time()).tm_hour) >
int(apustring[alku1-2:alku1]) :
print "Ok time"
status = True
if int(time.localtime(time.time()).tm_hour) <=
int(apustring[alku1-2:alku1]) :
print "Not Ok time"
status = False
return status
def AnyAurora(Location):
# Main function for location
Location.
print "...checking aurora for ", Location
# Open the NOAA 3-hour wwv file, get the text in
string variable html
Success, html = ReadNOAA()
if Success == False:
return "NOAA failed"
k = ParseK(html)
if k > 5:
print "K of " + str(k) + "
is High enough"
else:
print "K of " + str(k) + "
is too low"
Success, html = ReadFMIPage(Location)
if Success == False:
return "FMI failed"
c = Cloudiness(html)
temp = Temperature(html)
sunstatus = Sun(html)
if sunstatus == True and k > 5:
return True # Any Aurora
Function stops here
else:
return False
# MAIN PROGRAM
def main():
import time, winsound
from datetime import date
time_delta = 5 # interval in seconds
i = -1
Location = ["Porvoo", "Rovaniemi", "Vaasa"] # Places that
we are interested in
while True: # Eternal loop
i = i + 1
end_time = time.time() + time_delta
while time.time() < end_time:
time.sleep(1)
if date.today().weekday() in range(1,7):
print
str(AnyAurora(Location[i % 3])) + " for " + Location[i % 3]
print ""
winsound.PlaySound("C:/temp/Roland-GR-1-12-String-Guitar-C4.wav",winsound.SND_NODEFAULT)
main()