Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[dev] Horde Metar Block

0 views
Skip to first unread message

Rick Emery

unread,
Nov 17, 2003, 9:32:29 AM11/17/03
to
This message is in MIME format.

--=_5b1wbj2ydm9s
Content-Type: text/plain; charset="ISO-8859-1"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

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
--=_5b1wbj2ydm9s
Content-Type: text/plain; charset="ISO-8859-1"; name="metar.php"
Content-Disposition: attachment; filename="metar.php"
Content-Transfer-Encoding: 7bit

<?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;
}

}

--=_5b1wbj2ydm9s
Content-Type: text/plain; charset="ISO-8859-1"; name="registry.php.dist.diff"
Content-Disposition: attachment; filename="registry.php.dist.diff"
Content-Transfer-Encoding: 7bit

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"),
+);

--=_5b1wbj2ydm9s
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--
Horde developers mailing list
Frequently Asked Questions: http://horde.org/faq/
To unsubscribe, mail: dev-uns...@lists.horde.org

--=_5b1wbj2ydm9s--

Rick Emery

unread,
Nov 18, 2003, 12:04:41 PM11/18/03
to
Sorry to send updates to something that hasn't even been accepted or rejected
yet, but this version of metar.php (to go in horde/lib/Block/) fixes a bug with
the wind gust output and makes the wind speed display more presentable.

Thanks,
Rick

Rick Emery

unread,
Nov 18, 2003, 1:40:16 PM11/18/03
to
This message is in MIME format.

--=_1pt0qrkilmio


Content-Type: text/plain; charset="ISO-8859-1"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

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

--=_1pt0qrkilmio

),
'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');

$db->disconnect();

return $params;
}

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']);

return $html;
}

}

--=_1pt0qrkilmio


Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

--
Horde developers mailing list
Frequently Asked Questions: http://horde.org/faq/
To unsubscribe, mail: dev-uns...@lists.horde.org

--=_1pt0qrkilmio--

0 new messages