JP1.10 CROSSING PLATFORMS: PCS AND AWIPS USING THE PYTHON

C. Michael Callahan National Weather Service, Louisville KY

1. INTRODUCTION In order to show the power of the Python programming language, this paper will describe two applications, Dial The author has worked with a number of different and Convert. Dial is a Windows-based program which computer systems over the years, and has re-written many was designed to interrogate hydrologic gauges, and send programs. With the arrival of AWIPS, he decided to their data to the hydrologic AWIPS database. Convert is investigate cross-platform programming. He experimented a simple GUI-based meteorological converter whose code with a number of different languages but finally settled on runs on many platforms. the Python programming language. Python was created to be an interactive, dynamic, object-oriented, cross-platform 2. DIAL language (van Rossum 2000a). Because it is free and widely available, it has been used in many different The Local Data Acquisition and Dissemination (LDAD) applications, ranging from small system scripts to large, component of AWIPS originally was envisioned to complex Web-based systems. The author found the accomplish what Dial does. However, the LDAD language attractive because of its power and ease of use. hardware is almost overloaded with background processes Also, Python is not dogmatic. It does not force one into a and the gauge dialing has been difficult to set up certain programming style. For example, while Python has and maintain. The author felt that the gauge dialing task excellent object-oriented features, one can write in a would be better accomplished using a PC and not tying up procedural style just as easily. After learning a few new valuable LDAD resources. The PC interrogates the concepts and Python’s simple syntax, the programmer can gauges, encodes the collected data into the Standard quickly adopt old algorithms to Python, and then improve Hydrometeorologic Exchange Format or SHEF (NWS these programs as they become familiar to the more 1998), and then sends the data to LDAD, which allows powerful features of the language. The author now uses AWIPS to post the data into its hydrologic database. In a Python for most of his programming tasks. sense, AWIPS hands the interrogation task to the PC, rather than the LDAD hardware. However, with a few The standard library that comes with Python is a rich changes dealing with modem communications, Dial could collection of classes and functions that makes run on LD AD or other hardware as well. development easy (van Rossum 2000b). Included are interfaces to FTP, HTTP, SMTP, XML, CGI, and other The program is structured across five files whose Web technologies. combined size is about 114K. The main code is contained in DIAL.PY which handles the interfaces, error trapping, Python includes a module which links to the Tk library in interrogation, decoding, and SHEF encoding. Currently, order to create graphical user interfaces (GUIs). Using Dial supports 4 types of gauges: Handar 550A (known in this module, Tkinter, one can create Python programs that the NWS as a LARC), Handar 560A, Campbell Recorder adopt the native look-and-feel of any platform (Lundh 10, and Sutron 8200. The modular design makes it easy 2000). Tk is included on almost all -based to add new gauge types for future expansion. computers, but rarely is found on computers running Windows. In order to eliminate this potential problem, the The simple interface to the modem is in SMODEM.PY. standard Python installation package for Windows also This is the only file which is specific to Windows. In installs TCL and Tk under the Python directory. Thus, by order for Dial to run on another platform, it would be using Tkinter, one can write GUI programs that will run necessary to rewrite SMODEM.PY. However, this task anywhere there is a Python installation. would not be too difficult.

* Corresponding author address: . Michael The AWIPS LDAD interface is contained in two files, Callahan, National Weather Service, 6201 Theiler AWIPS.PY and AWIPARMS.PY. The first file is a Lane, Louisville, KY 40229; e-m ail: simple interface to send data to AWIPS via LDAD. The [email protected] transmission is through a mapped network drive and/or FTP. AWIPARMS.PY contains the parameters for a the hours of past data desired. mapped network drive and FTP servers of up to three different LDADs. If no options are found, the mode is set to interactive, the gauge list is cleared, and hours desired is defaulted to 6. Most of the information Dial needs is contained in DIAL.INI. The old INI style file was chosen rather than Now, the initialization file, DIAL.INI, is read, checked, to use the . First of all, INI files are easy and the data stored into memory. Next, the modem port is to maintain and even re-create in case of a failure. Also, initialized and the modem is reset and tested. The modem INI files are portable. must respond with “OK” within two tries or else the program logs the error and exits. Dial can be executed interactively or in an automatic mode by using command line parameters. The former Now, the program starts Tkinter, creates the GUI, and method generally is used during high water events to begins processing all user-generated events (Fig. 1). After obtain the most recent data. The latter method is used the user selects the desired gauges and clicks on the DIAL routinely when Dial is started by the Windows 98 task command button, or the program is in automatic mode, scheduler. Thus, one program handles routine and special the gauges are placed into a call list and the dialing data collection. process begins.

2.1 Dial Program Structure

DIAL.PY was written as an object-oriented program. Below is a list of the classes created:

InitStream: Encapsulates the routines needed to read the ini file. Figure 1: Dial GUI Catcher: Catches any errors that arise while the GUI is running. Gui: Encapsulates not only the GUI but all the For each gauge, the modem port is set to the correct events that the GUI can generate. Most of parameters, and a message is written to the log stating that the code for the program is contained in this the dialing process has begun. The gauge is dialed and the class. program waits for the gauge to answer. If there is no dialtone, the line is busy, or there is no answer after two SMODEM.PY creates these classes: tries, the failure is written to the log and the next gauge is called. If the gauge does answer, the gauge is polled and Timer: Encapsulates a very simple stopwatch-type the raw data it send s back is stored and written to the log timer. file. Each gauge type has its own polling and processing Modem: Encapsulates a simple modem interface for routines. Windows PCs. Once the polling is complete, the processing begins. The AWIPS.PY creates this class: raw data is converted to a SHEF format report, then stored and written to the log (Fig. 2) and displayed on the screen. Ldad: Encapsulates an interface for sending The polling and processing continues until all gauges are products to LDAD. called, or the user clicks on STOP. Clicking STOP while in the automatic mode stops dialing and switches the 2.2 Dial Operation program to the interactive mode.

Dial first opens the log file, DIAL.LOG, routes all future At this point in the interactive mode, the user can call output to the file, and writes the start message. Then, it more gauges. When the dialing is completed, he or she checks the command line. clicks on EDIT. At this point, the screen is cleared and the SHEF messages of all the gauges are displayed. The user If it finds two options, it sets the mode to automatic and can edit the messages to add comments or correct bad assumes the first option is a gauge list. The program tries data. to open the gauge list and read the gauges into memory. If this fails, Dial assumes the first option is the ID of a gauge When the editing is completed, the user clicks on SEND. and puts this into memory. The second option is stored as The program will collect all the SHEF messages >>>Dial 3.10 started Thu Sep 20 12:58:48 2001 UTC. Dialing Gills Rock WI... No dialtone. Exiting... stopped Thu Sep 20 13:04:52 2001 UTC.

>>>Dial 3.10 started Thu Sep 20 14:41:04 2001 UTC. Dialing Deputy IN... answered at 14:42 UTC... asking for 3 scan(s). 6 12:15 07.08 07.08 13:15 06.99 06.99 14:15 06.88 06.88

.E DEPI3 0920 DH1415/HGIRP/DIN-60/6.88/6.99/7.08 Dialing Fredricksberg IN... answered at 14:42 UTC... asking for 6 scan(s). 12 12:00 02.39 02.39 12:30 02.41 02.41 13:00 02.42 02.42 13:30 02.43 02.43 14:00 02.44 02.44 14:30 02.44 02.44

.E FRDI3 0920 DH1430/HGIRP/DIN-30/2.44/2.44/2.43/2.42/2.41/2.39 Sending SHEF data to AWIPS... using FTP... First attempt was successful. Exiting... stopped Thu Sep 20 14:44:15 2001 UTC.

Figure 2: Dial log and write them to a temporary file, DIAL.SHF. Next, it because he wanted to develop a completely cross-platform will add the correct headers and trailers to the file. Now, GUI program that was useful for all people in the National it attempts to copy DIAL.SHF to a network drive which Weather Service. He also wanted to demonstrate how easy points to the local LDAD /data/Incoming directory. The it was to accomplish this using Python. program now looks at the LDAD directory every 5 seconds for a user-determined amount of time to see if Convert handles these conversions between these units: AWIPS ingests it and deletes the file. Temperature: °F, °C, °K If this fails, the program will try to logon the FTP server Length: in, ft, mm, m of the local LDAD and send the file to /data/Incoming. Speed: mph, kt, km/h, m/s (note: these can Again, it waits to see if AW IPS ingests and deletes the also be used to convert between mi, file. If this fails, two other FTP servers of remote LDADs nm, and km) are attempted. Pressure: mb, mm of Hg, in of Hg Relative Humidity: temperature and dewp oint in °F, °C, If any of these FTP transfers fails, the program creates an °K email message and sends it to a list of recipients. This Heat Index: temperature in °F, °C, °K; dewpoint list, the frequency of transmitted messages, and the error in °F, °C, °K; or relative humidity messages desired is defined by the user. For example, the in percent service hydrologist may want to know about failures every Wind Chill: (both the old and new equations) hour, but the system analysis may want to be informed temperature in °F, °C, °K; wind about failures every 6 hours. Having three sites ensures speed in mph, kt, km/h, m/s that the decoded SHEF data will make it onto the AWIPS network, unless the PC’s Internet connection fails. Notice, Convert was designed to be easy to install. All that is Dial will collect and disseminate data even if the local required is to copy the CONVERT.PY file to a directory AWIPS is down. available to all users and create links to it. No registry editing or start up scripts are required. The file is small, After the program exits, the modem is reset, the modem only 11K. port is released, the GUI is cleared, a closing message is written to the log, and the log is trimmed to delete the 3.1 Convert Operation earliest session. After the user starts the program, it looks for a command 3. CONVERT line argument. If found, it sets the operating system DISPLAY environment variable to the argument. Convert is a cross-platform meteorological units converter Windows and most systems ignore this variable, but that can handle 50 different conversions. While a units AWIPS requires it. This allows the display code to be converter is nothing new, the author selected this project cross-platform. The AW IPS display environment variable for a text workstation is this: xt#-xxx:0.0, where # is software produces hydrographs which are placed on the the workstation number and xxx is the site. Graphics Web. This is the only source of real-time data for some workstations are this: ws#-xxx:0.0 for the left screen gauges. Convert is used routinely, running on Windows and ws#-xxx:0.1 for the right screen. PCs and AWIPS. Both Dial and Convert are being used in a number of different offices as well. Next, the program starts Tkinter and builds the GUI (Fig. 3a and 3b). Now it waits for the user to generate events. As hardware in NWS offices continue to evolve, it will be The program comes up ready to convert °C to °F. The necessary to port operational programs to different user clicks on the desired input and output units to set up platforms. Taking advantage of cross-platform techniques the converter. Note, calculating relative humidity, wind demonstrated in Dial and Convert using the Python chill, or heat index requires two input boxes. programming language can make this process almost painless.

Python is not a programming panacea. It should not be used for system critical tasks where processing time is the most important factor. However, even in these applications one can write time-critical routines in C, and call them from inside Python applications.

In the past, the author has written programs and scripts in C, C++, C shell, Delphi, and Perl for many different computers and operating systems. Now, he finds that he can stay with one language, Python, not worrying about Figure 3a: Convert GUI (Windows 98) the platform, and get the job done.

5. REFERENCES

Lundh, F., 1999: An Introduction to Tkinter, www.pythonware.com/library/tkinter/introduction/ index.htm

National Weather Service, 1998: Standard Hydro- meteorological Exchange Format, Version 1.3, Weather Service Hydrology H andboo k No. 1 Figure 3b: Convert GUI (AWIPS) van Rossum, G., 2000a: Python Tutorial, www.python.org Users can either use the mouse and click on the slider bar, /doc/2.0/tut/tut.html or the keyboard to work the converter. For making many different conversions, the mouse and slider bar work best. ___, 2000b: Python Library Reference, www.python.org When the user selects an input unit, pre-d efined limits /doc/2.0/lib/lib.html automatically are imposed on the slider. Slider values are always whole numbers. 6. ACKNOWLEDGMENTS

To exceed these limits, or to make conversions without The author would like to thank the service hydrologists in the the mouse, the keyboard can be used. Once the converter National Weather Service for testing Dial and Convert and for making excellent suggestions to improve the programs. He is set up, the user presses the up and down keys to clear would also like to thank Ted Funk for reviewing this the top and bottom entry windows. The user then types in manuscript. the values to co nvert and presses Enter.

4. SUMMARY

In the Louisville NWS office, Dial is used as an integral part of the Advanced Hydrologic Predication System (AHPS). Dial calls gauges every 6 hours, and the AHPS