1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
Getting started
===============
Getting started with DataHub for Python is simple and you can write a
simple script which prints out data in just 6 lines of Python.
API Key
-------
To access DataPoint you need to `register <https://datahub.metoffice.gov.uk/>`__
with the Met Office and get yourself an API key. The process is simple and just
ensures that you don’t abuse the service. You will need access to the
Site-Specific forecast API.
Connecting to DataHub
-----------------------
Now that you have an API key you can import the module:
::
import datapoint
And create a connection to DataHub:
::
manager = datapoint.Manager(api_key="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")
This creates a `manager` object which manages the connection and interacts
with DataHub.
Getting data from DataHub
---------------------------
So now that you have a Manager object with a connection to DataHub you can
request some data. To do this, use the `manager` object:
::
forecast = manager.get_forecast(51, 0, "hourly", convert_weather_code=True)
This takes four parameters: the latitude and longitude of the location you want
a forecast for, a forecast type of “hourly” and an instruction to convert the
numeric weather code to a string description. We’ll discuss the forecast types
later on.
This Forecast Object which has been returned to us contains lots of information
which we will cover in a later section, right now we’re just going to get the
data for the current time:
::
current_weather = forecast.now()
This is a dict which contains many different details about the weather
but for now we’ll just print out one field.
::
print(current_weather["feelsLikeTemperature"])
And there you have it. If you followed all the steps you should have
printed out the current weather for your chosen location.
Further Examples
----------------
For more code examples please have a look in the `examples
folder <https://github.com/perseudonymous/datapoint-python/tree/master/examples>`__
in the GitHub project.
|