npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@slatham/datapoint

v1.0.1

Published

Wrapper for the Met Office's datapoint API

Downloads

10

Readme

datapoint

Wrapper for the Met Office's DataPoint API - DataPoint

Usage

Install

Use your node package manager to install.

npm install @slatham/datapoint

Import

Include in your node project

// import the module
const dp = require('@slatham/datapoint');

Now, the variable dp will hold a datapoint object that can be cached by node. So when you import (as above) in other files, node will use this first cached version.

Initialise

Before using any of the datapoint functionality you must first run the init() function. This is an async operation with a callback function passed as the argument along with your metoffice api key. When called, the datapoint api will be queried for all forecast and observation sites. It will then store these sites in 2 separate quadtrees - one for forecast sites and one for observation sites. This is because these lists rarely change so it saves multiple async calls to the datapoint api for the same data. Also, as the locations are stored in a quadtree data structure they're organised based on their location. This makes queries for nearby sites more efficient. It is recommended to run any call to datapoint in the callback function of the init() function call. That way you'll be sure everything is set up and ready.

dp.init(apiKey, (ready) => {
  // YOUR CODE GOES HERE
});

You could even test if everything is working and no errors have occurred during initialisation

dp.init(apiKey, (ready) => {
  if(ready) {
    // YOUR CODE GOES HERE
  } else {
    // SOMETHING HAS GONE WRONG
  }
});

Get all forecast sites

Retrieve a full listing of all forecast sites (locations) in the UK that are covered. Note that will be a listing of over 5000 sites! The data returned contains a site id. A site id is needed so as you can request a weather forecast for that site id.

dp.init(apiKey, (ready) => {
  const allSites = dp.getAllForecastSites();
});

Get all observation sites

Retrieve a full listing of all observation sites (locations) in the UK that are covered. Note that will be a listing of over 140 sites - much less than the forecast sites. The data returned contains a site id. A site id is needed so as you can request observations for that site id.

dp.init(apiKey, (ready) => {
  const allSites = dp.getAllObservationSites();
});

Get nearby forecast sites

Retrieve a list of nearby forecast locations rather than a full listing. See Wikipedia precision table for a guide on setting the area size for the search.

// location to centre the search around
const location = {latitude: 53.430828, longitude: -2.960830};
// set a size for the area to search
const areaSize = 0.1; // decimal degrees
dp.init(apiKey, (ready) => {
  const nearbySites = dp.getNearbyForecastSites(location, areaSize);
});

The queries for nearby sites uses a quadtree data structure for fast retrival. Once init() has run these queries do not hit your datapoint api quota.

Get nearest forecast site

Given a coordinate, find the nearest forecast site. You will have a Set() containing a single item returned.

dp.init(apiKey, (ready) => {
  // for a given location
  const location = {latitude: 53.430828, longitude: -2.960830};
  // find the nearest site that datapoint has forecasts for
  const nearestSite = dp.getNearestForecastSite(location);
});

Get nearest observation site

Given a coordinate, find the nearest observation site. You will have a Set() containing a single item returned.

dp.init(apiKey, (ready) => {
  // for a given location
  const location = {latitude: 53.430828, longitude: -2.960830};
  // find the nearest site that datapoint has observations for
  const nearestSite = dp.getNearestObservationSite(location);
});

Get a forecast

Pull a weather forecast for a given site. Remember, you'll have to query for the site id before you can get a forecast or observation. You can pull a 3hourly or daily forecast from the api. You just set what time resolution you require as the second parameter. The first parameter is the ID of the site you're interested in.

const siteId = 322315;
dp.init(apiKey, (ready) => {
  // get the 3hourly forecast for the site
  dp.getForecast(siteId, '3hourly').then((forecast) => {
    console.log(forecast)
  });
});

Get observations

Pull weather observations for a given site. Remember, you'll have to query for the site id before you can get a forecast or observation. Also, the site ID for a forecast location isn't the same as an ID for an observations site. You'll have to query for them as decribed above. You can pull hourly observations for the last 24 hours from the api. The only parameter is the ID of the site you're interested in.

const siteId = 3503;
dp.init(apiKey, (ready) => {
  // get the observations for the site
  dp.getObservations(siteId).then((observation) => {
    console.log(observations)
  });
});

Note: The getForecast() and getObservation() are async and return a Promise. This mean you can use "then" and "catch" to handle the promise.

Examples

Some full examples to understand usage more fully

Get forecasts for nearby sites

Pull the daily forecasts for all nearby sites to a location for a given search area.

// import the module
const dp = require('@slatham/datapoint');
// set API key
const apiKey = xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
// location to centre the search around
const location = {latitude: 53.430828, longitude: -2.960830};
// set a size for the area to search
const areaSize = 0.1; // decimal degrees
dp.init(apiKey, (ready) => {
  const nearbySites = dp.getNearbyForecastSites(location, areaSize);
  nearbySites.forEach((site) => {
    dp.getForecast(site.data.id, 'daily').then((forecast) => {
      console.log(forecast)
    });
  })
});

Careful with this - don't try to query hundreds of sites at once for weather or you'll have your API key blocked for 24 hours!