Giter Club home page Giter Club logo

simple-nws's Introduction

SimpleNWS

PHP library for accessing NOAA's National Weather Service.

Using the free REST API provided by the National Weather Service should be straightforward enough - except it isn't. The results are overly convoluted - made to be read by machines, not humans. The purpose of this library is to make all that easier by doing all the hard work for you - it takes a latitude and longitude and gives back a nicely formatted object with all the weather data you need.

Requires PHP 5.3 (uses namespaces). Written by Cristian Radu, http://cristianradu.com/. MIT licensed, see the LICENSE file for details.

Example

// set your values for latitude and longitude
$lat  =  40.75;
$long = -73.92;

// instantiate the library
$simpleNWS = new SimpleNWS($lat, $long);

// get the forecast (for today in this example)
$forecast = $simpleNWS->getForecastForToday();

// extract the data that you need
$apparentTemperature = $forecast->getHourlyApparentTemperature();

Alternate use: if you prefer, you can pass the latitude and longitude parameters to the forecast function instead of the library constructor:

$simpleNWS = new SimpleNWS();

$forecast = $simpleNWS->getForecastForToday($lat, $long);

Check out example.php for a complete list of use cases for the library.

Library Methods

After instantiating the library with the latitude and longitude, you can use one of the following self-explanatory methods to retrieve the weather forecast:

  • getCurrentConditions()
  • getForecastForToday()
  • getForecastForWeek()

Forecast Model

All the methods return a forecast model object. You can use the following methods to extract the desired weather data:

Weather Data Method Returns Interval Units
Hourly Recorded Temperature getHourlyRecordedTemperature() array of integers hourly (every 3h) degrees F
Hourly Apparent Temperature getHourlyApparentTemperature() array of integers hourly (every 3h) degrees F
Daily Maximum Temperature getDailyMaximumTemperature() array of integers daily (every 24h) degrees F
Daily Minimum Temperature getDailyMinimumTemperature() array of integers daily (every 24h) degrees F
Hourly Precipitation getHourlyPrecipitation() array of floats hourly (every 6h) inches
Hourly Snow Amount getHourlySnowAmount() array of floats hourly (every 6h) inches
Hourly Cloud Coverage getHourlyCloudCover() array of integers hourly (every 3h) percent
Hourly Humidity getHourlyHumidity() array of integers hourly (every 3h) percent
Weather Conditions getWeatherConditions() array of arrays hourly (every 3h) description of weather type, intensity, coverage

The resulted arrays will have a time layout timestamp as the key. The format is YYYY-MM-DD-HH. The intervals are determined by the method you used to extract the weather data (see the above table).

Example:

Array (
    [2011-08-21-20] => 78
    [2011-08-21-23] => 74
    [2011-08-22-02] => 70
    [2011-08-22-05] => 67
    [2011-08-22-08] => 68
    [2011-08-22-11] => 75
    [2011-08-22-14] => 80
    [2011-08-22-17] => 79
... )

Extracting the weather conditions will return a timestamped list of arrays containing the description of the weather:

Array (
    [2011-08-21-23] => Array (
        [weather_type] => thunderstorms
        [intensity] => heavy
        [coverage] => likely
        )
... )

You can convert any of the results in degrees Celsius by passing them through the convert method:

$degreesCelsius = $forecast->convertToCelsius($forecast->getHourlyRecordedTemperature());

That's it! Have fun using the library. For questions and comments drop me a line at [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.