Laravel NOAA is a wrapper for the NOAA's climate data API at https://ncdc.noaa.giv/cdo-web/api/v2.
From your source directory, use the following command:
$ composer require epixian/laravel-noaa
Publish assets:
$ php artisan vendor:publish --tag=epixian-laravel-noaa
Add the following to your .env
file:
NOAA_TOKEN=YOUR_NOAA_TOKEN_HERE
Register for a token at https://www.ncdc.noaa.gov/cdo-web/token.
For a full listing of API request parameters, visit: https://www.ncdc.noaa.gov/cdo-web/webservices/v2.
where {requestType}
is one of:
datasets
dataCategories
dataTypes
locationCategories
locations
stations
data
Sets the type of request to be generated.
Executes the request and returns a JSON-decoded result (typically an array) depending on the API response. Note: requires a request type method to be previously called and must be the last call when chaining methods.
// fetch all datasets
$response = \Noaa::datasets()->get();
Alternatively, you may supply a single string
argument to get()
representing an id
belonging to that request type (valid for all request type methods except data()
:
// fetch dataset with id of 'GSOM'
$dataset = \Noaa::datasets()->get('GSOM');
startDate
is a string containing a valid ISO formatted date (e.g. '2010-10-02'
). Note: this field is required for the data()
request type.
endDate
is a string containing a valid ISO formatted date (e.g. '2010-10-07'
). Note: this field is required for the data()
request type.
field
is a string containing one of: id
, name
, mindate
, maxdate
, or datacoverage
.
direction
is an optional string containing either asc
(default if omitted) or desc
.
Defaults to 25 if omitted.
Defaults to 0 if omitted. Note: the API returns results with indexes starting at 1. offset(0)
and offset(1)
produce equivalent results.
A number of method aliases are provided for convenience:
latest()
- equivalent toorderBy('maxdate', 'desc')
oldest()
- equivalent toorderBy('mindate', 'asc')
skip(number)
- alias ofoffset()
take(number)
- alias oflimit()
where(field, value)
- sets a generic parameter (see https://www.ncdc.noaa.gov/cdo-web/webservices/v2)
Request constraints may be chained together to refine or limit the data returned:
$dataset = \Noaa::datasets()->from('1970-10-03')->to('2012-09-10')->take(50)->get();
Additional constraint methods specific to the request type are detailed below.
Returns the available dataset(s) applicable to the given constraints (if any). The following optional constraint methods are available:
id
is a string containing a single valid data type ID, or an array of strings representing multiple data type IDs.
$dataset = \Noaa::datasets()->withDataType('ACMH')->get();
// or
$dataset = \Noaa::datasets()->withDataType(['ACMH', 'MLY-TAVG-NORMAL'])->get();
id
is a string containing a single valid location ID, or an array of strings representing multiple location IDs.
$dataset = \Noaa::datasets()->withLocation('FIPS:37')->get();
// or
$dataset = \Noaa::datasets()->withLocation(['FIPS:09', 'FIPS:10'])->get();
id
is a string containing a single valid station ID, or an array of strings representing multiple station IDs.
$dataset = \Noaa::datasets()->withStation('COOP:310090')->get();
// or
$dataset = \Noaa::datasets()->withStation(['COOP:310090', 'COOP:310184'])->get();
Returns the available data category(s) applicable to the given constraints (if any). The following optional constraint methods are available:
id
is a string containing a single valid dataset ID, or an array of strings representing multiple dataset IDs.
$dataset = \Noaa::dataCategories()->withDataset('ACMH')->get();
// or
$dataset = \Noaa::dataCategories()->withDataset(['ACMH', 'GSOM'])->get();
id
is a string containing a single valid location ID, or an array of strings representing multiple location IDs.
$dataset = \Noaa::dataCategories()->withLocation('FIPS:37')->get();
// or
$dataset = \Noaa::dataCategories()->withLocation(['FIPS:09', 'FIPS:10'])->get();
id
is a string containing a single valid station ID, or an array of strings representing multiple station IDs.
$dataset = \Noaa::dataCategories()->withStation('COOP:310090')->get();
// or
$dataset = \Noaa::dataCategories()->withStation(['COOP:310090', 'COOP:310184'])->get();
Returns the available data type(s) applicable to the given constraints (if any). The following optional constraint methods are available:
id
is a string containing a single valid dataset ID, or an array of strings representing multiple dataset IDs.
$dataset = \Noaa::dataTypes()->withDataset('ACMH')->get();
// or
$dataset = \Noaa::dataTypes()->withDataset(['ACMH', 'GSOM'])->get();
id
is a string containing a single valid location ID, or an array of strings representing multiple location IDs.
$dataset = \Noaa::dataTypes()->withLocation('FIPS:37')->get();
// or
$dataset = \Noaa::dataTypes()->withLocation(['FIPS:09', 'FIPS:10'])->get();
id
is a string containing a single valid station ID, or an array of strings representing multiple station IDs.
$dataset = \Noaa::dataTypes()->withStation('COOP:310090')->get();
// or
$dataset = \Noaa::dataTypes()->withStation(['COOP:310090', 'COOP:310184'])->get();
id
is a string containing a single valid data category ID, or an array of strings representing multiple data category IDs.
$dataset = \Noaa::dataTypes()->withDataCategory('ANNAGR')->get();
// or
$dataset = \Noaa::dataTypes()->withDataCategory(['ANNAGR', 'ANNTEMP'])->get();