New METAR plugin

Creating and modifying plugins.
Post Reply
Maliuta
Regular
Posts: 6
Joined: Tue Jul 20, 2004 7:27 pm
Location: Brisbane, Australia
Contact:

New METAR plugin

Post by Maliuta »

Alright, I managed to write a functioning plugin from scratch. I works on my site at the moment with one slight hack ... I have a group of icons I want to bundle with the code, however I am not sure how to call them from within the plugins directory (at the moment I have put a copy of the icons in the blogs main dir).

In reality I would like to share these icons with another, related plugin I am going to write.

The plugin get METAR data from the net and then displays it in a table that has both textual and iconic represtations. Unlike the weather plugin by this one displays icons and it displays the time locally instead of in GMT. At the moment it is hardcoded to use metric units it can use imperial (otherwise called US standard) units. There are some other things I would like to do with the configuration directives, but that can wait.

I put up a page for it a http://lusan.id.au/~nikolai/plugin.html

here is the code for the plugin ... or you can view it at http://lusan.id.au/~nikolai/serendipity ... metar.phps

Code: Select all

<?
###############################################################################
#
# File: .......................... serendipity_plugin_metar.php
# Author: ........................ Nikolai Lusan
# Purpose: ....................... A serendipity plugin tp take METAR data and
#                                  provide a table of and textual iconic info.
#                                  
# Comments: ...................... Uses the PEAR_Services_Weather
#
###############################################################################

switch ($serendipity['lang']) {
    case 'en':
    default:
        @define('PLUGIN_SIDEBAR_METAR_NAME', 'Metar');
        @define('PLUGIN_SIDEBAR_METAR_DESC', 'Display the current weather in the sidebar.');
        @define('PLUGIN_SIDEBAR_METAR_TITLE', 'Headline');
        @define('PLUGIN_SIDEBAR_METAR_TITLE_BLAHBLAH', 'Sidebar item headline');
        @define('PLUGIN_SIDEBAR_METAR_SOURCE', 'METAR weather source');
        @define('PLUGIN_SIDEBAR_METAR_SOURCE_BLAHBLAH', 'Find your METAR weather source at http://weather.noaa.gov/');
        @define('PLUGIN_SIDEBAR_METAR_TIMEZONE','Your timezone');
        @define('PLUGIN_SIDEBAR_METAR_TIMEZONE_BLAHBLAH','Timezone should be relative to GMT (eg. +10 or -5)');
        break;
}


class serendipity_plugin_metar extends serendipity_plugin {
    /**
    * serendipity_plugin_metar::introspect()
    *
    * @param  $propbag
    * @return
    */
    function introspect(&$propbag) {
        global $serendipity;

        $propbag->add('name', PLUGIN_SIDEBAR_METAR_NAME);
        $propbag->add('description', PLUGIN_SIDEBAR_METAR_DESC);
        $propbag->add('configuration', array('title','metar','timezone'));
        return true;
    }

    /**
    * serendipity_plugin_metar::introspect_config_item()
    *
    * @param  $name
    * @param  $propbag
    * @return
    */
    function introspect_config_item($name, &$propbag) {

        switch ($name) {
            case 'title':
                $propbag->add('type', 'string');
                $propbag->add('name', PLUGIN_SIDEBAR_METAR_TITLE);
                $propbag->add('description', PLUGIN_SIDEBAR_METAR_TITLE_BLAHBLAH);
                break;
            case 'metar':
                $propbag->add('type', 'string');
                $propbag->add('name', PLUGIN_SIDEBAR_METAR_SOURCE);
                $propbag->add('description', PLUGIN_SIDEBAR_METAR_SOURCE_BLAHBLAH);
                break;
            case 'timezone':
                $propbag->add('type', 'string');
                $propbag->add('name', PLUGIN_SIDEBAR_METAR_TIMEZONE);
                $propbag->add('description', PLUGIN_SIDEBAR_METAR_TIMEZONE_BLAHBLAH);
                break;
            default:
                return false;
        }

        return true;
    }

    /**
    * serendipity_plugin_weather::generate_content()
    *
    * @param  $title
    * @return
    */
    function generate_content(&$title) {

        global $serendipity;

        $title = $this->get_config('title');
        $metarSource = $this->get_config('metar');
        $tz = $this->get_config('timezone');

        require_once "Services/Weather.php";

        # Initialise objects and check for error.
        $metar_data = &Services_Weather::service("Metar");
        if (Services_Weather::isError($metar_data)) {
            die("Error: ".$metar_data->getMessage()."");
        }

        # Set the unit format for  the data.
        $metar_data->setUnitsFormat('metric');

        # Make an array that makes the unit types more readable
        $units = $metar_data->getUnitsFormat();
        $units["temp"]   = "°".strtoupper($units["temp"]);
        $units["wind"]   = " ".str_replace("kmh", "km/h", $units["wind"]);
        $units["vis"]    = " ".$units["vis"];
        $units["height"] = " ".$units["height"];
        $units["pres"]   = " ".$units["pres"];
        $units["rain"]   = " ".$units["rain"];
        
        # Set METAR access method
        $metar_data->setMetarSource('http','','http','');

        # Set the time/date format
        $metar_data->setDateTimeFormat('j M Y', 'H:i');

        # get location, weather and forecast information.
        $location_data = $metar_data->getLocation($metarSource);
        $weather_data = $metar_data->getWeather($metarSource);
        $forecast_data = $metar_data->getForecast($metarSource);

        # Set icon for wind dir
        $windDir = $weather_data["windDirection"];
        switch ($windDir) {
            case "S":
                $windDirIcon = "icons/sss.png";
                break;
            case "SSW":
                $windDirIcon = "icons/ssw.png";
                break;
            case "SSE":
                $windDirIcon = "icons/sse.png";
                break;
            case "SW":
                $windDirIcon = "icons/sw.png";
                break;
            case "WSW":
                $windDirIcon = "icons/sww.png";
                break;
            case "E":
                $windDirIcon = "icons/eee.png";
                break;
            case "ESE":
                $windDirIcon = "icons/see.png";
                break;
            case "ENE":
                $windDirIcon = "icons/nee.png";
                break;
            case "N":
                $windDirIcon = "icons/nnn.png";
                break;
            case "NNW":
                $windDirIcon = "icons/nnw.png";
                break;
            case "NNE":
                $windDirIcon = "icons/nne.png";
                break;
            case "NW":
                $windDirIcon = "icons/nw.png";
                break;
            case "NE":
                $windDirIcon = "icons/ne.png";
                break;
            case "SE":
                $windDirIcon = "icons/se.png";
                break;
            case "W":
                $windDirIcon = "icons/www.png";
                break;
            case "WNW":
                $windDirIcon = "icons/nww.png";
                break;
            case "Variable":
                $windDirIcon = "icons/vrb.png";
                break;
            default :
                $windDirIcon = "wind_nodata.png";
        }

        # Turn the GMT time from the update into a local time
        $localTime = date("j M Y H:i", strtotime($weather_data["updateRaw"]) + (3600*$tz));


        # Now we need to get cloud and weather info to determine the weather icon
        
        # Get local hour to determing if it is night
        $hour = date("H" , strtotime($weather_data["updateRaw"]) + (3600*$tz));
        if ($hour > 18 || $hour < 6) {
            $night = "n_";
        } else {
            $night = "";
        }

        # Ascertain cloud cover
        # Note - We could be dealing with cloud at several levels, so find the heaviest
        #        cover and go with that.
        $cloudData = $weather_data["clouds"];
        
        # See if we are dealing with an array of arrays or some information
        $cloudKeys = array_keys($cloudData);
        $testKey = $cloudKeys[0];

        if (!is_array($cloudData["$testKey"])) {
        # we have information
            $amount = $cloudData["amount"];
        } else {
        # we have information on several levels - get highest
            $key = count($cloudKeys)-1;
            $useArray = $cloudData[$key];
            $amount = $useArray["amount"];
        }

        switch ($amount) {
            case "Clear Below":
            case "clear sky":
            case "no significant cloud":
            case "clear below 12,000 ft":
            case "vertical visibility":
                $cloudLevel = "0cloud";
                break;
            case "few":
            case "scattered":
                $cloudLevel = "1cloud";
                break;
            case "Cumulonimbus":
                $cloudLevel = "2cloud";
                break;
            case "Towering Cumulus":
            case "broken":
                $cloudLevel = "3cloud";
                  break;
            case "overcast":
                $cloudLevel = "4cloud";
                $night = "";
                break;
            default:
                $cloudLevel = "0cloud";
        }

        # Determine weather conditions (rain, snow etc);
        $conditions = $weather_data["condition"];
        
        switch ($cloudLevel) {
            case "0cloud":
                    if (strstr($conditions, "fog") != FALSE) {
                        $condUse = "_fog";
                    } else {
                        $condUse = "";
                    }
                break;
            case "1cloud":
                if (strstr($conditions, "fog") != FALSE) {
                    $condUse = "_fog";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "light") != FALSE) {
                    $condUse = "_lightrain";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "heavy") != FALSE) {
                    $condUse = "_heavyrain";
                } elseif (strstr($conditions, "rain") != FALSE) {
                    $condUse = "_modrain";
                } else {
                    $condUse = "_norain";
                }
                break;
            case "2cloud":
                if (strstr($conditions, "fog") != FALSE) {
                    $condUse = "_fog";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "light") != FALSE) {
                    $condUse = "_lightrain";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "heavy") != FALSE) {
                    $condUse = "_heavyrain";
                } elseif (strstr($conditions, "rain") != FALSE) {
                    $condUse = "_modrain";
                } elseif (strstr($conditions, "snow") != FALSE) {
                    $condUse = "_snow";
                } elseif (strstr($conditions, "thunderstorm") != FALSE) {
                    $condUse = "_thunders";
                } else {
                    $condUse = "_norain";
                }
                break;
            case "3cloud":
                if (strstr($conditions, "fog") != FALSE) {
                    $condUse = "_fog";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "light") != FALSE) {
                    $condUse = "_lightrain";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "heavy") != FALSE) {
                    $condUse = "_heavyrain";
                } elseif (strstr($conditions, "rain") != FALSE) {
                    $condUse = "_modrain";
                } elseif (strstr($conditions, "snow") != FALSE) {
                    $condUse = "_snow";
                } elseif (strstr($conditions, "thunderstorm") != FALSE) {
                    $condUse = "_thunders";
                } elseif (strstr($conditions, "hail") != FALSE) {
                    $condUse = "_hail";
                } else {
                    $condUse = "_norain";
                }
                break;
            case "4cloud":
                if (strstr($conditions, "fog") != FALSE) {
                    $condUse = "_fog";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "light") != FALSE) {
                    $condUse = "_lightrain";
                } elseif (strstr($conditions, "rain") != FALSE && strstr($conditions, "heavy") != FALSE) {
                    $condUse = "_heavyrain";
                } elseif (strstr($conditions, "rain") != FALSE) {
                    $condUse = "_modrain";
                } elseif (strstr($conditions, "snow") != FALSE && strstr($conditions, "light") != FALSE) {
                    $condUse = "_lightsnow";
                } elseif (strstr($conditions, "snow") != FALSE && strstr($conditions, "heavy") != FALSE) {
                    $condUse = "_heavysnow";
                } elseif (strstr($conditions, "snow") != FALSE) {
                    $condUse = "_snow";
                } elseif (strstr($conditions, "thunderstorm") != FALSE) {
                    $condUse = "_thunders";
                } elseif (strstr($conditions, "hail") != FALSE && strstr($conditions, "light") != FALSE) {
                    $condUse = "_lighthail";
                } elseif (strstr($conditions, "hail") != FALSE && strstr($conditions, "heavy") != FALSE) {
                    $condUse = "_heavyhail";
                } elseif (strstr($conditions, "hail") != FALSE) {
                    $condUse = "_hail";
                } else {
                    $condUse = "_norain";
                }
                break;
            default:
                $condUse = "_norain";
        }

        # Construct icon name
        $conditionIcon = "icons/$night$cloudLevel$condUse.png";
        $wind = $weather_data["wind"];
        $windUnits = $units["wind"];

        # Construct content
        $content = '<TABLE BORDER=1>';
        $content .= '<TR><TH valign="top" align="right">Sky</TH><TD ALIGN="CENTER"><IMG SRC="' . $conditionIcon . '" ALT=""><BR>'. $conditions . '</TD></TR>';
       $content .= '<TR><TH valign="top" align="right">Wind</TH><TD ALIGN="CENTER"><IMG SRC="' . $windDirIcon . '" ALT=""><BR>' . $windDir . ' ' . $wind . $windUnits . '</TD></TR>';
        $content .= '<TR><TH valign="top" align="right">Temp</TH><TD>';
        $content .= '<TABLE BORDER="0">';
        $content .= '<TR><TD align="right">Actual:</TD><TD>' . $weather_data["temperature"] . $units["temp"] . '</TD></TR>';
       $content .= '<TR><TD align="right">Feels Like:</TD><TD>' . $weather_data["feltTemperature"] . $units["temp"] . '</TD></TR>';
        $content .= '</TABLE>';
        $content .= '</TD></TR>';
        $content .= '<TR><TH valign="top" align="right">Humidity</TH><TD>' . $weather_data["humidity"] . '%</TD></TR>';
        $content .= '<TR><TH valign="top" align="right">Visibility</TH></TD><TD>' . $weather_data["visibility"] . $units["vis"] . '</TD></TR>';
        $content .= '<TR><TH valign="top" align="right">Updated</TH><TD>' . $localTime . '</TD></TR>';
        $content .= '</TABLE>';
    
        # Output content
        echo($content);
    }
}
?>
[/code]
Post Reply