Yii 1.1: noaaweather

Work with weather data from NOAA and the National Weather Service
8 followers

The NOAA Weather Extension works with data provided by the U.S. National Oceanic and Atmospheric Administration (NOAA) and the National Weather Service (NWS). Features include:

  • Pre-made weather widgets you can quickly add to your site
  • Data providers that allow full access to raw weather data
  • Data lookup by METAR weather station or coordinates
  • Intelligent download retry and cache behaviors
  • Unlike proprietary services, all weather data is in the public domain

Requirements

  • Yii Framework 1.1+
  • database support (uses built-in sqlite db by default)
  • a cache directory that is writable by the web server

This extension makes every effort to follow the NOAA/NWS web service usage guidelines, please be respectful and do the same.

Installation

This extension is tested and known to work out of the box with MAMP, WAMP, and common Linux distributions.

1. Download and Extract

Download the extension from the Project GitHub page and extract the noaaWeather package into your application's protected/extensions directory. Make sure the extension directory is named 'noaaWeather'. The extension must be installed under the 'ext.noaaWeather' path alias to work properly.

2. Configure Cache

By default the extension will create a file cache repository under protected/runtime/cache. This path must be writeable by the webserver.

3. Configure Database

The extension is preconfigured to use an included sqlite database and should work with no configuration. If you need to use another database, import the sql files found in the extension's data subdirectory, then configure the database connection by merging the following key into the application-level parameters array:

// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
    // CDBConnection uses this database configuration by default
    // Set this parameter to null to use the application database connection 
    'noaaWeather.dbConfig' => array(
        'dsn' => 'sqlite:protected/extensions/noaaWeather/data/noaa_weather.db',
        'username' => '',
        'password' => '',
    ),
)

Usage

Widgets

Several widgets are included as examples. Start here if you just want to display weather information on your page.

Current Observations Widget

Displays the latest observed conditions for the nearest NWS METAR station using a Yii portlet widget. Current Observations Widget

$this->widget('ext.noaaWeather.NoaaCurrentObservationsWidget',array(
    // Required location can be coordinates or a weather station
    'location' => array('weatherStation' => 'KMWN'),
    // Optional params are passed to the view to customize the display
    'params' => array(
        'title' => 'Mount Washington Current Conditions',
        'errorMsg' => 'Error fetching current conditions.',
        ),
    )
);

Forecast Mapclick Widget

Displays summary forecast information using a Yii portlet widget. Map Click Widget

$this->widget('ext.noaaWeather.NoaaForecastMapClickWidget',array(
    // Required location can be coordinates or a weather station
    'location' => array('coordinates'=>array(
        'latitude' => 44.27,
        'longitude' => -71.3,
        )),
    // Optional params are passed to the view to customize the display
    'params' => array(
        'title' => 'Mount Washington Forecast',
        'errorMsg' => 'Error fetching forecast.',
        'dayOffset' => 0,
        'numDays' => 5,
        ),
    )
);

Forecast All Widget

Displays master/detail forecast information using a CJuiTabs widget. All Widget

$this->widget('ext.noaaWeather.NoaaForecastAllWidget',array(
    // Required location can be coordinates or a weather station
    'location' => array('coordinates'=>array(
        'latitude' => 44.27,
        'longitude' => -71.3,
        )),
    // Optional params are passed to the view to customize the display
    'params' => array(
        'errorMsg' => 'Error fetching forecast.',
        'dayOffset' => 0,
        'numDays' => 6,
        ),
    )
);

Customizing Widgets

The look and feel of noaaWeather widgets can be customized without changing the widget logic by providing your own css, view files, or icons and configuring the widget as follows:

$this->widget('ext.noaaWeather.NoaaForecastAllWidget',array(
    // Required location can be coordinates or a weather station
    'location' => array('coordinates'=>array(
        'latitude' => 44.27,
        'longitude' => -71.3,
        )),
    // Optional parameters that customize display
    // The view file to load
    'viewFile' => 'myViewFile',
    // The path to search for view files
    'viewPath' => 'views/site/',
    // A published asset url
    'assetUrl' => '/yii/published/url'
    // The name of the css file to register with the asset manager
    'cssFile' => 'my.css',
    )
);

Data Providers

The NOAA Weather extension implements access to National Weather Service data in terms of Yii data providers that can be used within your application or to create your own weather widgets. Note that NOAA data providers will throw an exception if they cannot connect to the weather services they depend on and should always be wrapped in a try/catch statement.

Current Observations Data Provider

This component gathers data from the NWS XML feeds of current weather conditions. It provides access to hourly observation data from NWS METAR stations across the US.

$currentDataProvider = Yii::createComponent(
    'ext.noaaWeather.NoaaCurrentObservationsDataProvider',
    array('coordinates'=>array(
        'latitude' => 44.27,
        'longitude' => -71.3,
        ))
);
$weather = $currentDataProvider->getData();

Forecast MapClick Data Provider

This component gathers data from the NWS map click forecast page. It provides access to the same familiar forecast information displayed when you search http://www.weather.gov/.

$mapClickDataProvider = Yii::createComponent(
    'ext.noaaWeather.NoaaForecastMapClickDataProvider',
    array('coordinates'=>array(
        'latitude' => 44.27,
        'longitude' => -71.3,
        ))  
    );
$weather = $mapClickDataProvider->getData();

Forecast Summary Data Provider

This component gathers data from the NWS National Digital Forecast Database REST Service Single Point Summarized Data interface. It provides access to summarized forecast information.

$summaryDataProvider = Yii::createComponent(
    'ext.noaaWeather.NoaaForecastSummaryDataProvider',
    array('coordinates'=>array(
        'latitude' => 44.27,
        'longitude' => -71.3,
        ))  
    );
$weather = $summaryDataProvider->getData();

Forecast Detail Data Provider

This component gathers data from the NWS National Digital Forecast Database REST Service Single Point Unsummarized Data interface. It provides access to detailed hourly forecast information.

$detailDataProvider = Yii::createComponent(
    'ext.noaaWeather.NoaaForecastDetailDataProvider',
    array('coordinates'=>array(
        'latitude' => 44.27,
        'longitude' => -71.3,
        ))  
    );
$weather = $detailDataProvider->getData();

Working with Formatted Data

The above data providers implement a getDataFormatted method that nomalizes weather data and formats it in an associative array rather than raw tabular format. Formatted arrays from multiple data providers can be merged using the php array_merge functions. See the noaaForecastAllWidget source code for an example.

SQL Data Provider

This component provides the extension with access to the Yii database system. This is required to lookup METAR stations in the noaa_weather_stations table. This data comes from http://www.weather.gov/data/current_obs/index.xml

$weatherStations = Yii::createComponent('ext.noaaWeather.NoaaSqlDataProvider',
    'SELECT * FROM noaa_weather_stations');

Behaviors

NOAA Weather behaviors implement shared functionality required to process weather data and can be attached to components when developing additional data providers. An overview of functionality is included here, see the class source files for more information.

Cache Behavior

Caches downloaded data to improve application performance and reduce the load on the NOAA servers.

Location Behavior

Translates location data between formats using locator methods. Currently supports Latitude/Longitude and NWS Weather Station ID locators.

Parse DWML Behavior

Parses a subset of the DWML xml schema.

Fetch Remote Behavior

Fetches data from the NOAA servers using the HTTP GET method. Failed downloads are retried using an exponential backoff method.

Known Issues

Title of Zii CJuiTabs Widget is not set properly. See issue 2885 for more information and workarounds.

Change Log

  • 10/26/2011: Initial 1.0 Release
  • 11/03/2011: Updated NoaaDetailDataProvider->getDataFormatted method to fix a bug where data is not added to the formatted array when the periodicity of the forecast changes.
  • 01/06/2013 Fixed bugs and converted icons to png format

Resources

Total 5 comments

#15989 report it
markscarbrough at 2014/01/07 10:22am
Minor extension updates

Thanks all for your kind words and interest in the noaaWeather extension. I apologize that other projects have occupied my attention and I have not been able to give the extension the time it deserves. I invite anyone with the interest to take over development to get in touch with me.

That said, I have made minor modifications to make it work again with newer versions of yii and with changes that NOAA/NWS has made. The new version must be downloaded from github as the new icon format makes it too large to attach to this page as a zip file.

I'll try to respond to your comments below:

@Trejder: Thanks for your comments.

  1. Good catch! The new version doesn't fit under Yii's 2mb size limit with or without the .git directory, so it must be checked out from git now.
  2. I've converted the icons to png's.
  3. The extension does not download icons and save them in the assets directory, those were deployed during development. The extension does use a CFileCache object to save cache files (of weather data) to a directory. In the new version the cache directory defaults to protected/runtime/cache. Note that this is the default directory for Yii, not chosen by me. You can easily customize this behavior by editing the file NoaaCacheBehavior.php

@rajesh chaurasia: There was a syntax error in my example code, I've updated it.

@ShobhitPokhriyal: The NOAA/NWS data only covers the continental US, Alaska, Hawaii, and a few outlying territories including Guam (as far as I know).

#15944 report it
ShobhitPokhriyal at 2014/01/02 12:48am
Is this extenssion for non-US countries?

I am using this extenssion for ashian lattitude and longittude. But it not displaying any results for any coordinate.

#15938 report it
rajesh chaurasia at 2013/12/31 06:37am
Error

$summaryDataProvider = Yii::createComponent( 'ext.noaaWeather.NoaaForecastSummaryDataProvider', 'coordinates'=>array( 'latitude' => 44.27, 'longitude' => -71.3, )
); $weather = $summaryDataProvider->getData();

this will give error for me.

#15601 report it
Johnny Gan at 2013/11/27 04:43pm
Very nice extension

I will try, and this is a very nice wiki for the extension introduction. Thanks for your perfect work!

#10542 report it
Trejder at 2012/11/03 12:29pm
Awesome Extension!

Just beginning my story with this extension, and even from detailed description, it looks like an awesome piece of code! GREAT!

But you should consider fixing some problems:

  1. Right now your ZIP file contains .git subdirectory (totally unnecessary), which doubles its size (has 745 kB itself).

  2. NOAA is sending PNGs file names right now and all icons in current version of extensions are JPGs, which ends up with missing file icon in widged!

  3. It seems that your extension is saving new files (received from NOAA?) in /protected/extensions/noaaWeather/assets/nwsIcons/. This is wrong! The /protected folder is protected (as it name says) and it is core body of application and neither application itself nor any extension should write anything into it! Only files added during development process can be stored in this folder. Any files added during runtime of application should be stored in assets dir in root directory or anywhere, but not in /protected folder.

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet