[dev] Horde Metar Block

Rick Emery rick at emery.homelinux.net
Mon Nov 17 06:31:53 PST 2003


OK, I finally did it (and am a little proud of it, if I may say so). I've written
a metar weather block that removes it from jonah (making it a horde applet) and
uses the new pear Services_Weather library. The current available version of
Services_Weather (1.0.0RC1) causes some notices to be displayed in certain
circumstances, but I've been working with the lead package maintainer, and he
says he's fixed the problems (in CVS, I assume); a new release candidate should
be forthcoming.

First, install Services_Weather. Then, edit and run the script buildMetarDB.php
under "[path_to_pear]/data/Services_Weather/". I had it create the new tables
in my horde database. Currently, the block only uses the "airports" table for
locations, but could be modified to use the other table (and maybe both). I was
interested in this information from a pilot's point of view, so airports made
more sense to me.

Please post comments/suggestions about the code in this script. I tried to follow
CODING_STANDARDS, but wasn't sure what to put for the "header comment block".
I'm also not sure about my use of "isset". Once this is deemed acceptable (if it
is), I'll work on weather blocks for the other two pear libraries (in addition
to metar, there are two other sources).

Attached should be the metar.php file (as mentioned, the "header comment block"
probably needs to be adjusted) which goes in horde/lib/Block/, as well as a
patch for registry.php.dist.

Thanks in advance for any comments/suggestions; I'm still learning :-)

--
Rick Emery

"When once you have tasted flight, you will forever walk the Earth
 with your eyes turned skyward, for there you have been, and there
 you will always long to return"
                                              -- Leonardo Da Vinci
-------------- next part --------------
<?php
/**
 * The Horde_Block_metar class provides an applet for the portal screen to
 * display METAR weather data for a specified location (currently airports).
 *
 * $Horde: horde/lib/Block/metar.php,v 1.4 2003/11/13 03:29:49 chuck Exp $
 *
 * @package Horde
 */
class Horde_Block_metar extends Horde_Block {

    var $_app = 'horde';
    var $_type = 'metar';

    /**
     * The title to go in this block.
     *
     * @return string   The title text.
     */
    function _title()
    {
        return _("Current Weather");
    }

    function getParams()
    {
        global $conf;
        
        $params = array(
            'location' => array(
                'type' => 'mlenum',
                'name' => _("Location"),
                'default' => 'KSFB'
            ),
            'units' => array(
                'type' => 'enum',
                'name' => _("Units"),
                'default' => 's',
                'values' => array(
                    's' => _("Standard"),
                    'm' => _("Metric")
                )
            )
        );

        // Get locations from the database
        require_once 'DB.php';
        $dsn = $conf['sql']['phptype'] . '://' . $conf['sql']['username'];
        $dsn = $dsn . ':' . $conf['sql']['password'] . '@';
        $dsn = $dsn . $conf['sql']['hostspec'] . '/' . $conf['sql']['database'];
        $db = DB::connect($dsn);
        if (DB::isError($db)) {
            return PEAR::raiseError(_("Error connecting to database %s for metar codes"),
                $conf['sql']['database']);
        }
        $result = $db->query('Select icao, name, country From metarAirports');
        if (DB::isError($result)) {
            return PEAR::raiseError(_("Error retrieving station IDs from metarAirports"));
        }
        while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
            $locations[$row['country']][$row['icao']] = $row['name'];
        }

        $params['location']['values'] = $locations;

        $db->disconnect();

        return $params;
    }

    /**
     * The content to go in this block.
     *
     * @return string   The content
     */
    function _content()
    {
        global $conf;
        $html = '';

        if (empty($this->_params['location'])) {
            return _("No location is set.");
        }

        require_once "Services/Weather.php";
        $metar = &Services_Weather::service("METAR", array("debug" => 0));
        if (isset($conf['sql'])) {
            $dbString = $conf['sql']['phptype'] . '://';
            $dbString = $dbString . $conf['sql']['database'] . ':';
            $dbString = $dbString . $conf['sql']['username'] . '@';
            $dbString = $dbString . $conf['sql']['hostspec'] . '/weather';
            $metar->setMetarDB($dbString);
            $metar->setUnitsFormat($this->_params['units']);
            $units = $metar->getUnits(0, $this->_params['units']);

            $metar->setDateTimeFormat("m/d/Y", "H:i");
            $metar->setMetarSource("http");
            $weather = $metar->getWeather($this->_params['location']);
            $html .= sprintf(_("Current Weather for: %s"), $weather['station']);
            $html .= '<br /><br />';
            $html .= sprintf(_("Last Updated: %sZ"), $weather['update']);
            $html .= '<br /><br />';

            // Wind
            if (isset($weather['wind'])) {
                $html .= sprintf(_("Wind: "));
                if ($weather['windDirection'] == 'Variable') {
                    $html .= sprintf(_("%s at %s%s"), $weather['windDirection'],
                        $weather['wind'], $units['wind']);
                } elseif (($weather['windDegrees'] == '000') &&
                            ($weather['wind'] == '0')) {
                    $html .= sprintf(_("calm"));
                } else {
                    $html .= sprintf(_("from the %s (%s)"),
                        $weather['windDirection'], $weather['windDegrees']);
                    $html .= sprintf(_(" at %s%s"), $weather['wind'],
                        $units['wind']);
                }
            }
            if (isset($weather['windGust'])) {
                if ($weather['windGust']) {
                    $html .= sprintf(_(", gusting %sKT"),
                        round($weather['windGust']));
                }
            }
            if (isset($weather['windVariability'])) {
                if ($weather['windVariability']['from']) {
                    $html .= sprintf(_(", variable from %s to %s"),
                        $weather['windVariability']['from'],
                        $weather['windVariability']['to']);
                }
            }

            //Visibility
            if (isset($weather['visibility'])) {
                $html .= sprintf(_("<br />Visibility: %s%s"),
                    $weather['visibility'], $units['vis']);
            }

            //Temperature/DewPoint
            if (isset($weather['temperature'])) {
                $html .= sprintf(_("<br />Temperature: %s%s; "),
                    round($weather['temperature']), $units['temp']);
            }
            if (isset($weather['dewPoint'])) {
                $html .= sprintf(_("DewPoint: %s%s; "), round($weather['dewPoint']),
                    $units['temp']);
            }
            if (isset($weather['feltTemperature'])) {
                $html .= sprintf(_("Feels Like: %s%s"),
                    round($weather['feltTemperature']), $units['temp']);
            }

            //Pressure
            if (isset($weather['pressure'])) {
                $html .= sprintf(_("<br />Pressure: %s%s"), $weather['pressure'],
                    $units['pres']);
            }

            //Humidity
            if (isset($weather['humidity'])) {
                $html .= sprintf(_("<br />Humidity: %s%%"),
                    round($weather['humidity']));
            }

            //Clouds
            if (isset($weather['clouds'])) {
                foreach ($weather['clouds'] as $cloud) {
                    if (isset($cloud['height'])) {
                        $html .= sprintf(_("<br />Clouds: %s at %sFT"),
                            $cloud['amount'], $cloud['height']);
                    } else {
                        $html .= sprintf(_("<br />Clouds: %s"), $cloud['amount']);
                    }
                }
            }

            //Conditions
            if (isset($weather['condition'])) {
                $html .= sprintf(_("<br />Conditions: %s"),
                    $weather['condition']);
            }
        } else {
            $html .= 'A database backend is required for this block.';
        }

        return $html;
    }

}
-------------- next part --------------
Index: registry.php.dist
===================================================================
RCS file: /repository/horde/config/registry.php.dist,v
retrieving revision 1.190
diff -u -r1.190 registry.php.dist
--- registry.php.dist	11 Nov 2003 20:35:07 -0000	1.190
+++ registry.php.dist	17 Nov 2003 13:57:40 -0000
@@ -611,3 +611,7 @@
 $this->applets['moon'] = array(
     'name' => _("Moon Phases"),
 );
+
+$this->applets['metar'] = array(
+    'name' => _("Metar Weather"),
+);


More information about the dev mailing list