noaaweather Work with weather data from NOAA and the National Weather Service

  1. Requirements
  2. Installation
  3. Usage
  4. Known Issues
  5. Change Log
  6. Resources

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

4 0
6 followers
1 277 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Web Service
Developed by: markscarbrough
Created on: Oct 26, 2011
Last updated: 10 years ago

Related Extensions