Git Product home page Git Product logo

datapoint-cpp's Introduction

The Met Office has annouced it is moving to a new platform called "Datahub" that uses a different style API than Datapoint. This repo has therefore been archived.

https://www.metoffice.gov.uk/services/data/datapoint/notifications/weather-datahub

Datapoint for C++

A C++ Library for accessing weather data via the Met Office's open data API known as DataPoint. Depends on cURL and rapidJSON.

Designed to make few calls to the Datapoint API as possible, in order to keep within the fair use policy and improve the general speed of the library. Thus some functions like 'val/wxfcs/all/json/capabilities' are avoided.

You might also like to checkout "DataPoint for Python" - https://github.com/jacobtomlinson/datapoint-python

Disclaimer

This module is in no way part of the DataPoint project/service. No support for this module is provided by the Met Office and may break as the DataPoint service grows/evolves.

Prerequisites

Datapoint for C++ example program and library code requires libcurl and rapidJSON to compile.

Example Usage

Lib Files

#include "datapoint/datapoint.h"

//Create API object
datapoint::api weather;
 
//Set DataPoint API key as string.
std::string key = "01234567-89ab-cdef-0123-456789abcdef"; //Get from https://www.metoffice.gov.uk/datapoint/api
weather.Setkey(key);

//Set DataPoint latitude and longitude co-ords as floats.
float lat = 52.634001, lon = 1.293240;
weather.Setlocation(lat, lon);

// Create object of current observation
datapoint::observation observation = weather.GetObservation();
// Print current weather; convert code to text using array
cout << "Current Weather : " << weather.weather_codes[observation.weatherType] << endl;

// Create object (array) of current forecasts in 3 hour resolutions
datapoint::forecast *forecast = weather.GetForecast();
// Print out the site and next weather forecast
cout << "Future Weather : " << weather.weather_codes[forecast[0].weatherType] << endl;

Current class

  #define MAXHOURS 28 //Furthest away forecast - forecast[27].weatherType
		class forecast {
			public:
				std::string feelsLike; //Celsius
				std::string windGust; //Miles Per GHour
				std::string humidity; //Percent
				std::string temperature; //Celsius
				std::string visibility; //Descriptive: G/VG
				std::string windDirection; //Compass: N/E/S/W
				std::string windSpeed; //Miles Per Hour
				std::string uv; //
				unsigned short int weatherType; //Ref weather_codes
				std::string precipitationProb; //Percent
				std::string minsofday;
		} ;
		class observation {
			public:
				std::string windGust; //Miles Per Hour
				std::string temperature; //Celsius
				std::string visibility; //Meters
				std::string windDirection; //Compass: N/E/S/W
				std::string windSpeed; //Miles Per Hour
				unsigned short int weatherType; //Ref weather_codes
				std::string pressure; //Hectopascal (hpa)
				std::string tendency; //Pascals per Second  (Pa/s)
				std::string dewPoint; //Celsius
				std::string humidity; //Percent
		} ;

Authors

Alan Percy Childs - Initial work

Licence

This project is licensed under the MIT License - see the LICENSE.md file for details

datapoint-cpp's People

Contributors

percz avatar

Watchers

 avatar

datapoint-cpp's Issues

Handle JSON error returned gracefully

When no JSON is returned, or the JSON is in error, the wrapper code simply crashes with a cout. We should avoid cout at all costs and instead flow a error all the way back to the call function so that the error can be programmed per application.

    } else {
    	//TODO: HTTP Failed, deal with it safely. Maybe return an error in JSON format or something.
    	cout << "Error: " << errorCode << endl ;
    	cout << "DataPoint has not returned any data, this could be due to an incorrect API key" << endl ;
    	exit(1);
    }

Setlocation should be overloadable with other input types

Currently only accept location as...

bool Setlocation(double our_lat, double our_lon) ;

Which then requires a call to sitelist and some parsing. It should be possible, though overloading, to state the site ID if we already know it; although we must be careful that site ID's for Forecasting and Observations are different.

It might be possible to search for string based location names, but this would probably cause too much error handling responsibilities on the library side.

GetForecast to only parse required ahead number of forecasts

In the current code there is a slight C++ issue in that the forecast array, locally defined, can not be returned outside this function without defining the array size as static. This means the forecast object is potentially using more memory and processing loops then possibly needed.

The array should be set to only be as large as the call requires it. It ultimately should be possible to call something along the lines of forecast.GetForecast(startHoursAhead, endHoursAhead )

I suspect the answer is something with vectors around....

datapoint::forecast * datapoint::api::GetForecast() {

#define MAXHOURS 28 
static forecast frcst[MAXHOURS];

GetForecast stores 'forecasts' from past time

It seems that Datapoint sometimes returns requests that have already expired and these are accepted by

datapoint::forecast * datapoint::api::GetForecast()

These should be skipped with frcst.minsofday = (*itr)["$"].GetString(); being expanded to check against the system time and the forecast skipped as needed.

We would need to ensure the skip still returns the maximum, or user requested (when implemented,) number of returns provided this doesn't overrun the returned data.

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.