Weather.py: How It Works

Whenever the KOB weather program receives a request, it performs the following actions:

  1. Parse the request.

  2. Get the weather station data.

  3. Get the current conditions.

  4. Get the forecast.

Parse the request

The program waits until the end of the user's transmission and then deals with the request string as a whole. It ignores all spaces and everything at the beginning of the string and just looks for WXstn at the end of the string, where stn is the four or five character code of a National Weather Service weather station.

If the request string doesn't end in WX followed by four or five characters, the program echoes the request string, surrounded by question marks, back to the sender.

In the following examples, we'll assume the requested weather station is KROC.

Get the weather station data

Next the program gets static information about weather station KROC by querying this site:

https://api.weather.gov/stations/KROC

From the text that's returned, the program obtains the weather station's name and coordinates (latitude and longitude).

If the query fails, then the program sends an error message back to the user stating that the requested station is UNKNOWN.

If the query is successful, but for some reason the program is unable to determine the name or the coordinates, then the program replies that the requested station is UNAVAILABLE.

Get the current conditions

The program gets the current weather conditions for KROC by going to

https://api.weather.gov/stations/KROC/observations/current

If the query fails, the program returns the error message CURRENT WX UNAVAILABLE.

If the query returns data but the program can't find the information, then the error message is CURRENT WX MISSING.

If the temperature is missing or invalid, the program returns the current conditions without the temperature.

Get the forecast

The program gets the forecast for the next two time periods by specifying KROC's coordinates as follows:

https://api.weather.gov/points/43.11667,-77.67667/forecast

The coordinates are obtained as part of the station data (see above).

If the query fails, the program returns the error message FORECAST UNAVAILABLE.

If the query returns data but the program can't find the information, then the error message is FORECAST MISSING.