#!/usr/local/bin/python2.2

# Copyright (C) 2002  Tobias Klausmann
# Modified by Jerome Alet
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import sys
import pymetar

def main(args):
    for arg in map(lambda x: x.strip(), args):
        try:
            weather = pymetar.MetarReport(arg)
        except IOError, msg:
            sys.stderr.write("Problem accessing the weather server: %s\n" % msg)
        else:
            if weather.valid:
                print "Source of report: %s" % weather.getReportURL()
                print "Station fullname: %s" % weather.getStationName()
                print "Station city: %s" % weather.getStationCity()
                print "Station country: %s" % weather.getStationCountry()
                print "Station position: Lat: %s, Long: %s, Alt: %s m" % weather.getStationPosition()
		if None not in weather.getStationPositionFloat():
			print "Station position as floats: %f / %f %fm"% weather.getStationPositionFloat()
                print "Station latitude: %s (%s)" % (weather.getStationLatitude(),weather.getStationLatitudeFloat())
                print "Station longitude: %s (%s)" % (weather.getStationLongitude(),weather.getStationLongitudeFloat())
		if weather.getStationAltitude() is not None:
	                print "Station altidude above sea: %f m" % weather.getStationAltitude()
                print "Metar's Raw answer: %s" % weather.getRawMetarCode()
                print "Time of last update: %s" % weather.getTime()
                print "Cycle: %s" % weather.getCycle()
                print "Wind direction: %s" % weather.getWindDirection()
                print "Wind compass: from %s" % weather.getWindCompass()
                print "Wind speed: %s ms" % weather.getWindSpeed()
                if weather.getTemperatureCelsius() is not None:
                    print "Temperature: %.1fC (%.1fF)" % (weather.getTemperatureCelsius(), weather.getTemperatureFahrenheit())
                if weather.getDewPointCelsius() is not None:
                    print "Dew point: %.1fC (%.1fF)" % (weather.getDewPointCelsius(), weather.getDewPointFahrenheit())
                if weather.getHumidity() is not None: 
                    print "Humidity: %.0f%%" % weather.getHumidity()
                if weather.getVisibilityKilometers() is not None:
                    print "Visibility: %.1f Km" % weather.getVisibilityKilometers()
                if weather.getPressure() is not None:
                    print "Pressure: %.0f hPa" % weather.getPressure()
                print "Weather: %s" % weather.getWeather()
                print "Sky conditions: %s" % weather.getSkyConditions()
                print "Suggested pixmap: %s" % weather.getPixmap()
                print "Time of report (METAR style): %s" % weather.getTime()
                print "Time of report (ISO 8601): %s" % weather.getISOTime()
            else:
                print "Either %s is not a valid station ID, the NOAA server is down or parsing is" % arg
                print "severely broken. If the problem persist and you are sure you have a valid"
                print "station ID, contact the author at %s, thank you!" % pymetar.__author__

if __name__ == "__main__":
    if len(sys.argv) > 1:
        if sys.argv[1]=="--help":
            sys.stderr.write("usage: %s MetarStationCode\n"% sys.argv[0])
        elif sys.argv[1]=="--version":
            sys.stderr.write("pymetar test script v0.4 using pymetar lib v%s from %s\n"%(pymetar.__version__,pymetar.__file__))
        else:
            main(sys.argv[1:])
    else:
        sys.stderr.write("usage: %s MetarStationCode\n"% sys.argv[0])
        sys.exit(-1)
