nickginsberg / noaa-gfs-js

A lightweight library for pulling GFS (Global Forecasting System) weather data from NOAA, without any major 3rd party depencies.
MIT License
8 stars 0 forks source link

noaa-gfs-js

A lightweight library for pulling GFS (Global Forecasting System) weather data from NOAA, without any major 3rd party depencies.

Installing

Installation is simple with npm:

npm i noaa-gfs-js --save

About

This library is loosely inspired by the Python library getgfs. As with the getgfs package, the goal was to build a lightweight library to pull GFS data without the need for 3rd party software (such as ECMWF's ecCodes).

All available fields from NOAA's grib files are available here, using OpenDAP instead of the grib2 binaries.

NOTE: This library is independently maintained and is not officially supported or endorsed by NOAA.

Usage

The goal of the library is to be simple to use. The get_gfs_data function takes straightforward parameters, and returns lat/long/time dimensions with the given data in both an array and object format.

import * as noaa_gfs from 'noaa-gfs-js';

noaa_gfs.get_gfs_data(
    '0p25', // Options are 0p25, 0p50, or 1p00
    new Date(Date.now() - 86400000).toISOString().split('T')[0].replaceAll('-',''), // YYYMMDD format date
    '00', // Every 6 hours. 00, 06, 12, or 18
    [40.5, 40.5], // Lat range
    [-74,-74], // Lon range
    5,  // Number of 8 hour  fwd increments to include in addition to the forecast time
    'rh2m', // The requested data item
    true // Whether or not to convert the times into dates or keep as NOAA formatted times (see below for more details)
).then((res) => console.log(res));

The resulting object will have the weather data in two formats: object and array.

Array format:

[                                                        
  {
    time: '2/22/2024, 6:00:00 AM',
    lat: 40.5, lon: -74,
    value: 93.3
  }, 
  {                                                                    
    time: '2/22/2024, 6:00:00 AM',                                     
    lat: 40.5,                                                         
    lon: -73.75,                                                       
    value: 91.6                                                        
  },                                                                   
  {                                                                    
    time: '2/22/2024, 6:00:00 AM',                                     
    lat: 40.5,                                                         
    lon: -73.5,                                                        
    value: 90.3                                                        
  },                                                                   
  {                                                                    
    time: '2/22/2024, 6:00:00 AM',                                     
    lat: 40.5,                                                         
    lon: -73.25,                                                       
    value: 89.6                                                        
  }
]                                                              

Object format is an object with keys time, lat, lng:

{
    '2/22/2024, 6:00:00 AM': {
      '41': {'-73.25': 89.6},
      '42': {'-73.5': 90.3},
      '40.5': {'-74': 91.6},
      '40.75': {'-73.75': 93.3}
    }
}

Explanation of Indexing Concepts

When making NOMADS API requests, we do not simply give the desired time/lat/lons. Rather, we have to give the INDEX of our desired points.

Per the docs (choose a folder, then choose the .info file for more):

Thus, we have to convert from our lat/lon to a scale of 0-360 for longitude with 1440 stops, -90 to 90 for latitude with 721 stops, etc.

Then get the min/max so we are consistent with where our square starts.

For example, if we're in resolution 0p25 (increments of 0.25), and our start coord is 5.9 degrees:

Our resulting URL, therefore, will look something like this: https://nomads.ncep.noaa.gov/dods/gfs_0p25/gfs20240223/gfs_0p25_06z.ascii?rh2m[0:5][522:530][1144:1148] , where the indexes requested are in the brackets. Note the date may need to be updated in the URL as data is only available for a few weeks.

Available Data Fields

Explanation of Date & Time Handling

The time field, as returned by the NOMADS, is represented by days since year 0. The time is represented by the decimal fraction of the day--i.e. .25 would be 1/4 through the day, or 6am.

So 738931.25 is Feb 14th, 2024, at 6:00 AM (738,931 full days and .25 days = 6 hours). This library returns times as they were by default, but the noaa_time_to_utc_datetime can be used to convert to a friendlier string.

Todo