Using openweathermap api – part 1

Lets start from something simple – current weather data for your city.

One of module in my project is used to gather weather information. I was looking for something free and good and found It has API, so all we need to know is how to work with this API and how we want to use informations.

In few parts I will try to describe API and show simple code to read weather.

Lets start with few basic things.

Current weather endpoint

First, lets look at current weather endpoint.

Opening this url in browser will show you output in XML format. XML is good for human reading but in code I prefer JSON as its smaller and all languages have supporting library. To see output in JSON use mode=json instead of mode=xml.

Next important thing is the city that we want to see. In example city id is used (id=3103402). But first we need to grab it. Of course there is an endpoint that uses city names.

This will return weather data for city Ostrava and what is more important city id.  Now find your city id and save it for later usage.

There is also possibility for using city name instead of id. Such endpoint will look like this:

Now time to look at output:

 <city id="3103402" name="Bielsko-Biala">
  <coord lon="19.05" lat="49.82"/>
  <sun rise="2014-08-27T03:52:23" set="2014-08-27T17:38:01"/>
 <temperature value="13.35" min="12.7" max="14" unit="celsius"/>
 <humidity value="92" unit="%"/>
 <pressure value="1008.5" unit="hPa"/>
  <speed value="4.47" name="Gentle Breeze"/>
  <direction value="337.5" code="NNW" name="North-northwest"/>
 <clouds value="92" name="overcast clouds"/>
 <precipitation value="1" mode="rain" unit="3h"/>
 <weather number="500" value="light rain" icon="10d"/>
 <lastupdate value="2014-08-27T10:57:33"/>

What we have here, first is opening tag <current>. Next we have a city section with information about city id, name, country, geo position and sun raise/set times.

Next we see a temperature data. We have three attributes, value that displays current temperature, max – shows maximum current temperatures and min – minimal current temperature. Max and min may differ from value in big cities (large area with not stable temperature)

Next sections are speaking for them selfs. If you need full details read description on openweatermap.

One important thing is weather number. As you can see we have number (500) and value (light rain). I needed to compensate as much information in 20 chars as possible so all weather descriptions are shortened. For example light rain is R-.

Forecast endpoint

Now lets look at forecast endpoint.

This will return forecast for 3 days including today. Once again we have city id, mode and units. Something new is cnt, its a number of days for weather forecast.

And quick look at the output

  <location altitude="0" latitude="49.822449" longitude="19.04686" geobase="geonames" geobaseid="3103402"/>
 <sun rise="2014-08-27T03:52:24" set="2014-08-27T17:38:02"/>
  <time day="2014-08-27">
   <symbol number="500" name="light rain" var="10d"/>
   <precipitation value="0.5" type="rain"/>
   <windDirection deg="304" code="NW" name="Northwest"/>
   <windSpeed mps="2.27" name="Light breeze"/>
   <temperature day="13.24" min="10.37" max="13.24" night="10.37" eve="13.24" morn="13.24"/>
   <pressure unit="hPa" value="971.84"/>
   <humidity value="88" unit="%"/>
   <clouds value="overcast clouds" all="92" unit="%"/>
  <time day="2014-08-28">
   <symbol number="500" name="light rain" var="10d"/>
   <precipitation value="0.5" type="rain"/>
   <windDirection deg="276" code="W" name="West"/>
   <windSpeed mps="2.71" name="Light breeze"/>
   <temperature day="14.41" min="7.71" max="15.7" night="7.71" eve="15.7" morn="11.38"/>
   <pressure unit="hPa" value="976.45"/>
   <humidity value="84" unit="%"/>
   <clouds value="broken clouds" all="68" unit="%"/>
  <time day="2014-08-29">
   <symbol number="800" name="sky is clear" var="01d"/>
   <windDirection deg="182" code="S" name="South"/>
   <windSpeed mps="1.67" name="Light breeze"/>
   <temperature day="17.09" min="4.44" max="18.73" night="13.43" eve="18.33" morn="4.44"/>
   <pressure unit="hPa" value="977.07"/>
   <humidity value="84" unit="%"/>
   <clouds value="clear sky" all="0" unit="%"/>

I thing output is easy to read. Most important thing is section forecast. It contain forecasts for each day.

Unfortunately there is a difference in weather data block between forecast and current weather. Its clearly visible when we look at wind. In current we have nice section but here we have two fields. Next is clouds, we have value for name and all for percentage but  current returns value as percentage and name as name.
And we have symbol instead of weather.

Later we will do something about it.

So we have some basic info, time to put python in action. But this in part 2.




One comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s