bweather.bac

' ***********************************************************
' PROGRAM:      bweather.bac
' PURPOSE:      get metar weather conditions for five locations
' AUTHOR:               vovchik (Puppy Linux forum)
' COMMENTS:     based on clweather - a little c program        
' DEPENDS:      bash, bacon
' PLATFORM:     Puppy Linux (actually, any *nix)
' DATE:         07-04-2010
' VERSION:      0.1a
' ***********************************************************

' *****************
' DECLARATIONS
' *****************

OPTION BASE 1
GLOBAL Location$[50]
GLOBAL Report$[50]
GLOBAL stations

' *****************
' END DECLARATIONS
' *****************


' *****************
' SUBROUTINES
' *****************

'http://tgftp.nws.noaa.gov/data/observations/metar/decoded/UKLL.TXT

' --------
SUB INIT_VARS
' --------
    version$ = "v. 0.1a"
    q$ = CHR$(34)
    Website$ = "tgftp.nws.noaa.gov"
    port$ = "80"
    MyPath$ = "/data/observations/metar/decoded/"
    m1$ = "This program queries a NOAA weather server for current weather"
    m2$ = "information for a particular location. To use this program for"
    m3$ = "your locales of interest, check out the following link for ICAO"
    m4$ = "locator codes:"
    m5$ = CONCAT$("          http://www.weather.gov/tg/siteloc.shtml", NL$, NL$, "and pass the metar station codes on the command line.")
    m6$ = CONCAT$(NL$,"Example: bweather UKKK ULKK LOWW KJFK EHRD", NL$)
END SUB

' --------
SUB SHOWHLP()
' --------
    PRINT NL$,"bweather - shows metar data for chosen locations - ", version$, NL$
    PRINT m1$, NL$, m2$, NL$, m3$, NL$, m4$, NL$, NL$, m5$, NL$, m6$, NL$
END SUB

' --------
SUB GET_ARGS()
' --------
    LOCAL i
    SPLIT ARGUMENT$ BY " " TO ARGV$ SIZE TotalArgs
    IF TotalArgs > 1 THEN
    IF EQUAL(ARGV$[2], "-h") OR EQUAL(ARGV$[2], "--help") THEN
        SHOWHLP
        END
    ELSE
        FOR i = 2 TO TotalArgs
        Location$[i -1] = ARGV$[i]
        NEXT i
        stations = TotalArgs - 1
    END IF
    ELSE
    PRINT NL$, "Missing input arguments (metar codes).", NL$
    SHOWHLP
    END
    END IF      
END SUB

' --------
SUB dns_interrupt
' --------
    PRINT NL$, "That DNS lookup took too long! Check that you used a genuine metar code.", NL$
    END
END SUB

' --------
SUB GET_DATA()
' --------
    TRAP LOCAL
    CATCH GOTO error_msg

    FOR i = 1 TO stations
    ALARM dns_interrupt, 3000
    OPEN CONCAT$(Website$, ":", port$) FOR NETWORK AS mynet
    StationID$ = CONCAT$(MyPath$, Location$[i],".TXT")
    SEND CONCAT$("GET ", StationID$, " HTTP/1.1\r\nHost: ", Website$, "\r\n\r\n") TO mynet
    ALARM dns_interrupt, 0
    REPEAT
        RECEIVE Dat$ FROM mynet
        Total$ = CONCAT$(Total$, Dat$)
    UNTIL INSTR(Dat$,"cycle")
    Report$[i] = MID$(Total$, INSTR(Total$, "charset=UTF-8") + 14)
    Total$ = ""
    CLOSE NETWORK mynet
    NEXT i
    ' --------
    LABEL error_msg
    ' --------
    IF NOT(EQUAL(ERR$(ERROR),"Success")) THEN
        PRINT "Network error: ", ERR$(ERROR)        
        PRINT "Cannot communicate with ", Website$,"."
        PRINT "Please check your internet connection or try later."
        END
        END IF
END SUB

' --------
SUB PRINT_REPORT()
' --------
    PRINT 
    FOR i = 1 TO stations
        PRINT "-----------------"
    PRINT i,". Report for ", Location$[i]
    PRINT "-----------------"
    SPLIT Report$[i] BY NL$ TO MyArray$ SIZE dim
    FOR  j = 0 TO dim - 1
        PRINT MyArray$[j]
    NEXT j
    PRINT
    NEXT i
    PRINT
END SUB

' *****************
' END SUBROUTINES
' *****************


' *****************
' MAIN
' *****************

INIT_VARS
GET_ARGS
GET_DATA
PRINT_REPORT
END

' *****************
' END MAIN
' *****************


Generated by GNU Enscript 1.6.5.90.