Fetch Meteorological Data From Your .Net Application With Climacell

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,
  1. Temperature
  2. Humidity
  3. Wind Speed
  4. Wind Direction
  5. Barometric Pressure
  6. Precipitation
  7. Visibility
Get the API Key from ClimaCell to make requests to the ClimaCell Weather API
 
Step 1
 
Go to the ClimaCell Weather API page, then click on start now.
 
Fetch Meteorological Data From Your .Net Application With Climacell
 
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.
 
Fetch Meteorological Data From Your .Net Application With Climacell
 
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.
 
Fetch Meteorological Data From Your .Net Application With Climacell
 
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.
 
Fetch Meteorological Data From Your .Net Application With Climacell
 
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.
 
Fetch Meteorological Data From Your .Net Application With Climacell
 
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.
 
Fetch Meteorological Data From Your .Net Application With Climacell
 
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.
  1. Install-Package Microsoft.AspNet.WebApi.Client  
Step 4
 
After this NuGet package is installed, add the below snippet in the APICall class.
  1. using System;  
  2. using System.Net;  
  3. using System.Net.Http;  
  4. using System.Net.Http.Headers;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ClimaCell  
  8. {  
  9.     public static class APICall  
  10.     {  
  11.         private static HttpClient GetHttpClient(string url)  
  12.         {  
  13.             var client = new HttpClient { BaseAddress = new Uri(url) };  
  14.             client.DefaultRequestHeaders.Accept.Clear();  
  15.             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  16.             return client;  
  17.         }  
  18.   
  19.         private static async Task<string> GetAsync<T>(string url, string urlParameters)  
  20.         {  
  21.             try  
  22.             {  
  23.                 using (var client = GetHttpClient(url))  
  24.                 {  
  25.                     HttpResponseMessage response = await client.GetAsync(urlParameters);  
  26.                     if (response.StatusCode == HttpStatusCode.OK)  
  27.                     {  
  28.                         var data = await response.Content.ReadAsStringAsync();  
  29.                         return data;  
  30.                     }  
  31.                     else  
  32.                         return "";  
  33.                 }  
  34.             }  
  35.             catch (Exception ex)  
  36.             {  
  37.                 Console.WriteLine(ex.Message);  
  38.                 return "";  
  39.             }  
  40.         }  
  41.   
  42.         public static async Task<string> RunAsync<T>(string url, string urlParameters)  
  43.         {  
  44.             return await GetAsync<string>(url, urlParameters);  
  45.         }  
  46.     }  
  47. }  
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.
  1. using System;  
  2. using System.Threading.Tasks;  
  3.   
  4. namespace ClimaCell  
  5. {  
  6.     public static class ClimaCellInfo  
  7.     {  
  8.         const string url = "https://api.climacell.co/v3/weather/realtime";  
  9.         const string apiKey = "<enter your api key here>";  
  10.           
  11.         public static string GetData()  
  12.         {  
  13.             string urlParameters = $"?apikey={apiKey}&lat=28&lon=77&unit_system=si&fields=temp,humidity,wind_speed,baro_pressure,precipitation,visibility";  
  14.             var response = APICall.RunAsync<string>(url, urlParameters).GetAwaiter().GetResult();  
  15.             return response;  
  16.         }  
  17.     }  
  18. }  
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.
  1. using System;  
  2.   
  3. namespace ClimaCell  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.   
  10.             FetchMeteorologicalData();  
  11.         }  
  12.   
  13.          private static void FetchMeteorologicalData()  
  14.             {  
  15.                 var response = ClimaCellInfo.GetData();  
  16.   
  17.             Console.WriteLine(response);  
  18.   
  19.                 Console.ReadLine();  
  20.             }  
  21.         }  
  22.     }  
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.
 
Fetch Meteorological Data From Your .Net Application With Climacell

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.