Technology

Extract world’s real time weather data using Python

In simple terms, weather is the state of a certain place ‘s environment over a certain period of time ( usually one to a few days). Data that provide weather patterns and trends are considered weather data. Weather data includes all facts and figures on the state of the atmosphere.

In order to optimize risk management, social and technical institutions now examine and fuse weather data (as the main external data) with domestic data such as sales and operational data.

Because weather data is invaluable, scientists and geographers are continuously working on improving the art of accurate weather forecasting. They use data mining, processing and analysis methodologies so as to produce empirical evidence accurately.

Where Weather data is used and who use it?

The use of weather data has become invaluable for almost every aspect of our lives as weather and climate are seen as important data-driven activities.Apart from trivial uses of farming, aviation, construction and other open-field operations, common uses in logistics, utility and fast-food companies have demonstrated how weather data has been incorporated into their risk management analysis for maximizing customer safety and satisfaction.

Wind, precipitation , temperature, cloudiness, humidity and atmospheric pressure are components of weather data.

There are plenty of weather APIs. Some of them are free and some pay like AccuWeather,WeatherAlpha,weatherstack etc. As per your requirements you can use paid API or free API’s .

There are many ways to extract the data using API , here we have used Python code to extract the real time data of Weather.

import requests
import pandas as pd
import json 
url=("http://api.weatherstack.com/current?access_key=3aa0f109df510c8dae88d2ca62f&query=Pune")


r=requests.get(url)
r.json()

# Python program to write JSON 
# to a file 
# Writing to Weather.json 
with open("Weather.json", "w") as outfile: 
	#outfile.write(r.json()) 
	 json.dump( r.json(), outfile)

Above Python will fetch the weather details for “Pune” City, likewise you can give any other city name and fetch the details.

After running Python code you will get the output in json format :

{"request": {"type": "City", "query": "Pune, India", "language": "en", "unit": "m"}, 
"location": {"name": "Pune", "country": "India", "region": "Maharashtra", "lat": "18.533", "lon": "73.867", "timezone_id": "Asia/Kolkata", "localtime": "2020-06-09 17:52", "localtime_epoch": 1591725120, "utc_offset": "5.50"}, 
"current": {"observation_time": "12:22 PM", "temperature": 32, "weather_code": 116, "weather_icons": ["https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"], 
"weather_descriptions": ["Partly cloudy"], "wind_speed": 22, "wind_degree": 270, "wind_dir": "W", "pressure": 1007, "precip": 0, "humidity": 51, "cloudcover": 44, "feelslike": 34, "uv_index": 11, "visibility": 10, "is_day": "yes"}}

You can use any API’s in above Python code to extract the real time data.Also you can convert the data into readable format further in Python.