Weather forecasting with darksky api in python

Today we learn how to work with dark sky API for weather forecasting in python. Nowadays many websites and apps using weather forecasting. They are actually using various type of API service. In this article, we have focused on how to build a python function that returns our required weather data.

Weather forecast in Python using Darksky API

Requirements:

  • requests library
  • datetime library

 

In this python program, we create 3 different functions which will help to determine our required weather.



we create : current(), hourly(), weekly() function.

  1. current() function returns the Current weather.
  2. hourly() function returns the Hourly forecasted weather.
  3. weekly() function returns the Weekly forecasted weather.

Get the source code: https://github.com/CodeSpeedy/weather-forecasting-using-darksky-api-python

In this program, we used two parameters as every functions argument.

Learn: How to parse JSON in python

Let’s see what the parameter stands for.

If we use (0,0) as the input arguments, then another function will fetch your device’s public IP and fetch your location as Latitude and Longitude. And forecast the function weather. Here the first and second argument in int data type.

If we use (‘IP’,’Any-IP-Address’) as the input arguments, then another function will fetch the IP’s location as Latitude and Longitude. And forecast the function weather. Here the first and second argument in string data type.

If we use (‘LATLON’,[25.00,29.00]) as the input arguments, then another function will fetch the location from given Latitude and Longitude. And forecast the function weather. Here first argument in int and second argument in the list with two float data type. Inside the list first one is Lattitude and the Second one is Longitude value.

 

 

Current() Function :

Input :

current(0,0)

Output:

{'Overall Weather ': 'Mostly Cloudy',
 'Current Temperature in C': '32.34',
 'Current Temperature in F': '90.21',
 'Current Humidity': 47,
 'Current Wind Speed': 5.13,
 'Current Wind Pressure': 1006.14}

Input:

current('latlon',[52.3824,4.8995])

Output:

{'Overall Weather ': 'Clear',
 'Current Temperature in C': '20.25',
 'Current Temperature in F': '68.45',
 'Current Humidity': 47,
 'Current Wind Speed': 10.95,
 'Current Wind Pressure': 1029.75}

Input:

current('IP','Provided ip')

Output:

{'Overall Weather ': 'Clear',
 'Current Temperature in C': '20.25',
 'Current Temperature in F': '68.45',
 'Current Humidity': 47,
 'Current Wind Speed': 10.95,
 'Current Wind Pressure': 1029.75}

Input:

current(0,0)['Current Humidity']

Output:

47

Fetch current weather data in Python from using Darksky API

We can also fetch from Current() function :

  • time
  • summary
  • icon
  • precipIntensity
  • precipProbability
  • temperature
  • apparentTemperature
  • dewPoint
  • humidity
  • pressure
  • windSpeed
  • windGust
  • windBearing
  • cloudCover
  • uvIndex
  • visibility
  • ozone

 

Hourly() Function :

Input:

hourly(0,0)

Output:

{'Today': {'0.00': {},
'1.00': {'Overall Weather': 'Mostly Cloudy',
'Temperature in C': '31.65',
'Temperature in F': '88.97',
'Humidity': 47,
'Wind Speed': 1.4,
'Wind Pressure': 1006.4},
'2.00': {'Overall Weather': 'Partly Cloudy',
'Temperature in C': '30.25',
'Temperature in F': '86.45',
'Humidity': 49,
'Wind Speed': 3.43,
'Wind Pressure': 1007.29},
'3.00': {'Overall Weather': 'Clear',
'Temperature in C': '28.73',
'Temperature in F': '83.72',
'Humidity': 51,
'Wind Speed': 3.14,
'Wind Pressure': 1008.25},
.............. Upto 24 hr
},
'Tomorrow': {'0.00': {},
'1.00': {'Overall Weather': 'Partly Cloudy',
'Temperature in C': '33.17',
'Temperature in F': '91.70',
'Humidity': 38,
'Wind Speed': 2.91,
'Wind Pressure': 1004.72},
................Upto 24 hr
}}}

Hourly Data fetch from darksky API

We can also fetch from Hourly() function :

  • time
  • summary
  • icon
  • precipIntensity
  • precipProbability
  • temperature
  • happarentTemperature
  • dewPoint
  • humidity
  • pressur
  • windSpeed
  • windGust
  • windBearing
  • cloudCover
  • uvIndex
  • visibility
  • ozone

Weekly function to fetch weekly data from darksky API

Weekly() Function :

Input:

weekly(0,0)

Output:

{'19-April-2019': {'Overall Weather': 'Humid throughout the day and foggy in the morning.', 'Max Temperature in C': '26.16', 'Max Temperature in F': '79.09', 'Min Temperature in C': '32.64', 'Min Temperature in F': '90.75', 'Humidity': 57, 'Wind Speed': 4.79, 'Wind Pressure': 1008.21}, '20-April-2019': {'Overall Weather': 'Mostly cloudy in the morning and humid throughout the day.', 'Max Temperature in C': '24.41', 'Max Temperature in F': '75.93', 'Min Temperature in C': '36.37', 'Min Temperature in F': '97.46', 'Humidity': 51, 'Wind Speed': 3.36, 'Wind Pressure': 1006.67}, '21-April-2019': {'Overall Weather': 'Mostly cloudy starting in the afternoon.', 'Max Temperature in C': '24.99', 'Max Temperature in F': '76.99', 'Min Temperature in C': '36.91', 'Min Temperature in F': '98.43', 'Humidity': 55, 'Wind Speed': 6.64, 'Wind Pressure': 1005.36}, '22-April-2019': {'Overall Weather': 'Mostly cloudy throughout the day.', 'Max Temperature in C': '24.93', 'Max Temperature in F': '76.88', 'Min Temperature in C': '38.79', 'Min Temperature in F': '101.82', 'Humidity': 48, 'Wind Speed': 6.06, 'Wind Pressure': 1005.81}, '23-April-2019': {'Overall Weather': 'Humid throughout the day and mostly cloudy starting in the evening.', 'Max Temperature in C': '24.24', 'Max Temperature in F': '75.64', 'Min Temperature in C': '38.49', 'Min Temperature in F': '101.29', 'Humidity': 54, 'Wind Speed': 6.64, 'Wind Pressure': 1007.19}, '24-April-2019': {'Overall Weather': 'Partly cloudy in the morning and humid throughout the day.', 'Max Temperature in C': '24.43', 'Max Temperature in F': '75.98', 'Min Temperature in C': '38.48', 'Min Temperature in F': '101.26', 'Humidity': 56, 'Wind Speed': 8.09, 'Wind Pressure': 1008.26}, '25-April-2019': {'Overall Weather': 'Humid throughout the day.', 'Max Temperature in C': '24.93', 'Max Temperature in F': '76.87', 'Min Temperature in C': '39.46', 'Min Temperature in F': '103.02', 'Humidity': 56, 'Wind Speed': 9.59, 'Wind Pressure': 1007.35}}

 

We can also fetch from Weekly() function :

  • time
  • summary
  • icon
  • sunriseTime
  • sunsetTime
  • moonPhase
  • precipIntensity
  • precipIntensityMax
  • precipIntensityMaxTime
  • precipProbability
  • precipType
  • temperatureHigh
  • temperatureHighTime
  • temperatureLow
  • temperatureLowTime
  • apparentTemperatureHigh
  • apparentTemperatureHighTime
  • apparentTemperatureLow
  • apparentTemperatureLowTime
  • dewPoint
  • humidity
  • pressure
  • windSpeed
  • windGust
  • windGustTime
  • windBearing
  • cloudCover
  • uvIndex
  • uvIndexTime
  • visibility
  • ozone
  • temperatureMin
  • temperatureMinTime
  • temperatureMax
  • temperatureMaxTime
  • apparentTemperatureMin
  • apparentTemperatureMinTime
  • apparentTemperatureMax
  • apparentTemperatureMaxTime

Every function returns dictionary value. You can perform any dictionary operation with those values.

 

The whole program is stored in our Github repository. If you want the program please visit: https://github.com/CodeSpeedy/weather-forecasting-using-darksky-api-python

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *