[dev] Horde Metar Block

Rick Emery rick at emery.homelinux.net
Tue Nov 18 10:39:08 PST 2003


I just realized that I didn't attach the updated file. It's just as well, since I
made more changes. I'll try to make this my last submission until I get
feedback.

This adds another parameter to allow converting wind speed to knots. The two
if/else statements have some duplicate code, so I initially tried using the
conditional operator instead. The code got pretty cryptic, though, so I went
with the if/else statements. If anybody has an opinion the other way, let me
know.

By the way, converting the wind speed to knots when units are set to metric
causes a notice to be displayed (when debug level is E_ALL). I changed my pear
library and have sent a message to the package maintainer.

Thanks again.

--
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")
                )
            ),
            'knots' => array(
                'type' => 'checkbox',
                'name' => _("Wind speed in knots"),
                'default' => 0
            )
        );

        // 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 order by country');
        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') {
                    if (!empty($this->_params['knots'])) {
                        $html .= sprintf(_("%s at %s%s"),
                                    $weather['windDirection'],
                                    round($metar->convertSpeed($weather['wind'],
                                                                $units['wind'],
                                                                "kt")),
                                    "kt");
                    } else {
                        $html .= sprintf(_("%s at %s%s"),
                                    $weather['windDirection'],
                                    round($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']);
                    if (!empty($this->_params['knots'])) {
                        $html .= sprintf(_(" at %s%s"),
                                    round($metar->convertSpeed($weather['wind'],
                                        $units['wind'], "kt")),
                                    "kt");
                    } else {
                        $html .= sprintf(_(" at %s%s"),
                                    round($weather['wind']),
                                    $units['wind']);
                    }
                }
            }
            if (isset($weather['windGust'])) {
                if ($weather['windGust']) {
                    $html .= sprintf(_(", gusting %s%s"),
                        round($weather['windGust']), $units['wind']);
                }
            }
            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;
    }

}


More information about the dev mailing list