What Is Meteorological Data ?
Meteorological data is the data used by meteorologists to predict changes in the weather patterns by using various tools. These are facts pertaining to the atmosphere, such as wind, temperature, air density and other factors that affect the weather.
Why Is Meteorological Data Important?
Meteorological data is important as it used by meteorologists for forecasting and predicting any future changes in the atmosphere across many geographical locations. This type of data is essential for multiple reasons. First of all, it plays a vital role in urban administration. Cities can prepare for extreme weather conditions such as tornadoes and snowstorms in order to prevent disasters.
Secondly, in the long-term, this data is crucial for agriculture. Droughts can cause a lot of issues for farmers by impacting the crop yields which can later result in shortage of food production resulting in deaths due to starvation.
There are many such applications which are dependent on this kind of data for the purpose of weather forecasting resulting in prevention of any fatal accidents.
How Do We Fetch Meteorological Data ?
Now, we know that meteorological data is so important for the world. You may be wondering how we can get this data on a real-time basis to analyze it or use it to build a regression model to predict future changes in the weather or for any other use case that interests or fascinates you.
The answer for this is the ClimaCell Weather API.
What Is ClimaCell Weather API?
ClimaCell Weather API gives you real-time and historical weather data with hyperlocal weather forecasts around the world.
With more than 60 different data fields, including weather, air quality, pollen, road-risk and fire index, you can build almost anything related to weather forecasting using it.
So how do we fetch Meteorological data from ClimaCell's Weather API in Our .NET Application ?
In this article, we are going to consume ClimaCell Weather API’s core data layer to fetch some key meteorological data points.
Here is some of the data which we'll be fetching using the weather API,
- Temperature
- Humidity
- Wind Speed
- Wind Direction
- Barometric Pressure
- Precipitation
- Visibility
Get the API Key from ClimaCell to make requests to the ClimaCell Weather API
Step 1
Step 2
Select the Plan which is good for you. For this article, I'm going with the developer plan. After selecting the Plan click on Start now.
Step 3
If you have already created an account here, then you will be asked to login and then you'll be directed to the dashboard. If you are a new user, then you'll be redirected to a registration page. Here you will need to fill in the details to register and later they will send you a verification e-mail. After the verification, you can login directly.
Step 4
Now you have registered your account with ClimaCell. When you login in, you need to click on the Dashboard label in the top right corner. After that, your screen should look similar to the below attached image. Click on copy to copy the API key.
Keep the API key somewhere as we will need it in our application to send requests to ClimaCell's Weather API.
Let’s get started With building our .Net application.
Step 1
Open Visual Studio and create a console application. I have created a console application called ClimaCell. You can name it anything you want.
Step 2
Visual Studio will have created some Program Class having a statement to print 'Hello world'. Now, right click on the project and add a class named APICall.
Step 3
Now open the Package Manger Console and type in the following command to install the NuGet package. This package will help us make requests to remote APIs.
- Install-Package Microsoft.AspNet.WebApi.Client
Step 4
After this NuGet package is installed, add the below snippet in the APICall class.
- using System;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
-
- namespace ClimaCell
- {
- public static class APICall
- {
- private static HttpClient GetHttpClient(string url)
- {
- var client = new HttpClient { BaseAddress = new Uri(url) };
- client.DefaultRequestHeaders.Accept.Clear();
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- return client;
- }
-
- private static async Task<string> GetAsync<T>(string url, string urlParameters)
- {
- try
- {
- using (var client = GetHttpClient(url))
- {
- HttpResponseMessage response = await client.GetAsync(urlParameters);
- if (response.StatusCode == HttpStatusCode.OK)
- {
- var data = await response.Content.ReadAsStringAsync();
- return data;
- }
- else
- return "";
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return "";
- }
- }
-
- public static async Task<string> RunAsync<T>(string url, string urlParameters)
- {
- return await GetAsync<string>(url, urlParameters);
- }
- }
- }
Here the main function is public static async Task<string> RunAsync<T>(string url, string urlParameters)
using(var client=GetHttpClient(url)) - this function creates the HTTP client with the URL Parameters
HttpResponseMessageresponse=awaitclient.GetAsync(urlParameters); - This sends a GET asynchronous request to the specified URL
Var data =awaitresponse.Content.ReadAsStringAsync() - And finally, this serializes the HTTP response object to a string using the ReadAsStringSync method.
Step 5
Now, as our API Caller class is ready, we'll add the ClimaCellInfo Class which will call this RunAsync method of APICall Class to send a request to ClimaCell’s weather API and return the response it got back from the API. Add the below snippet in the ClimaCellInfo Class.
- using System;
- using System.Threading.Tasks;
-
- namespace ClimaCell
- {
- public static class ClimaCellInfo
- {
- const string url = "https://api.climacell.co/v3/weather/realtime";
- const string apiKey = "<enter your api key here>";
-
- public static string GetData()
- {
- string urlParameters = $"?apikey={apiKey}&lat=28&lon=77&unit_system=si&fields=temp,humidity,wind_speed,baro_pressure,precipitation,visibility";
- var response = APICall.RunAsync<string>(url, urlParameters).GetAwaiter().GetResult();
- return response;
- }
- }
- }
Here, we are passing the URL, along with the URL parameters, to the RunAsync method of APICall Class. You need to replace the value of apiKey with your own API key. After that, if you wanted the data in a different Unit system, you can also change that. For this article, I'm using SI Sytem.
I have defined the longitude and latitude here in the Url Parameter as 77 and 28 respectively. You can change it as per your requirement. Finally, if you want to only fetch only specific meteorological data, then you can modify that by adding or removing data points. If you only wanted temperature and humidity then, assign fields= temp,humidity.
Please note that we are requesting real-time data from the geographical location with latitude value of 28 E and longitude value of 77 N. You can also fetch historical data but that is beyond the scope of this article.
In case you wanted information about more meteorological data like Air Quality and many more as such, you can refer to
here.
Step 6
Finally, we'll call this function from our Program.cs class in the application by adding the below code as follows.
- using System;
-
- namespace ClimaCell
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- FetchMeteorologicalData();
- }
-
- private static void FetchMeteorologicalData()
- {
- var response = ClimaCellInfo.GetData();
-
- Console.WriteLine(response);
-
- Console.ReadLine();
- }
- }
- }
Step 7
Now, we are done with the coding part of our console application. This class contains FetchMeterologicalData Method, which calls the ClimaCellInfo class's GetData method and stores the result in response and later displays it. Let's click on start to see the result in the console window.
This is the data that we had requested from the weather API. You can see the values for all the meteorological data in the value and units part. You can deserialize this json string into an object too, by defining the properties properly.
Conclusion
Meteorological data is an essential part of weather forecasting. This helps in tracking the changes in the atmosphere well, ensuring early alarms for drastic weather changes which can cause difficulties for everyday life and or even cause loss of human lives.
This helps in early detection of tropical cyclones, heatwaves or heavy rainfall which can result in flooding. Using ClimaCell's Weather API, we can create applications that gather this crucial information and share it with the end user.
The end user can just have a look at the real time values for weather data points like temperature or humidity in their respective locations or fetch the data for a specific time period to do some analysis on the weather pattern to gather insights. There are a plethora of things to explore.