|
| 1 | +--- |
| 2 | +title: "Tutorial: Join sensor data with weather forecast data by using Azure Notebooks(Python) | Microsoft Docs" |
| 3 | +description: "Tutorial: This tutorial shows you how to join sensor data with weather forecast data from Azure Maps weather service by using Azure Notebooks(Python)." |
| 4 | +author: walsehgal |
| 5 | +ms.author: v-musehg |
| 6 | +ms.date: 12/09/2019 |
| 7 | +ms.topic: tutorial |
| 8 | +ms.service: azure-maps |
| 9 | +services: azure-maps |
| 10 | +manager: philmea |
| 11 | +ms.custom: mvc |
| 12 | +--- |
| 13 | + |
| 14 | +# Join sensor data with weather forecast data by using Azure Notebooks (Python) |
| 15 | + |
| 16 | +Wind power is one alternative energy source for fossil fuels to combat against climate change. Because wind itself is not consistent by nature, wind power operators need to build ML (machine learning) models to predict the wind power capacity to meet electricity demand and ensure the grid stability. In this tutorial, we walk through how Azure Maps weather forecast data can be combined with demo data set of sensor locations with weather readings. Weather forecast data is requested by calling Azure Maps Weather service. |
| 17 | + |
| 18 | +In this tutorial, you will: |
| 19 | + |
| 20 | +> [!div class="checklist"] |
| 21 | +> * Work with data files in [Azure Notebooks](https://docs.microsoft.com/azure/notebooks) in the cloud. |
| 22 | +> * Load demo data from file. |
| 23 | +> * Call Azure Maps REST APIs in Python. |
| 24 | +> * Render location data on the map. |
| 25 | +> * Enrich the demo data with Azure Maps [Daily Forecast](https://aka.ms/AzureMapsWeatherDailyForecast) weather data. |
| 26 | +> * Plot forecast data in graphs. |
| 27 | +
|
| 28 | + |
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +To complete this tutorial, you first need to: |
| 32 | + |
| 33 | +1. Create an Azure Maps account subscription in the S0 pricing tier by following instructions in [Manage your Azure Maps account](https://docs.microsoft.com/azure/azure-maps/how-to-manage-account-keys#create-a-new-account). |
| 34 | +2. Get the primary subscription key for your account, follow the instructions in [Get the primary key for your account](./tutorial-search-location.md#getkey). |
| 35 | + |
| 36 | +To get familiar with Azure notebooks and to know how to get started, follow the instructions [Create an Azure Notebook](https://docs.microsoft.com/azure/azure-maps/tutorial-ev-routing#create-an-azure-notebook). |
| 37 | + |
| 38 | +> [!Note] |
| 39 | +> The Jupyter notebook file for this project can be downloaded from the [Weather Maps Jupyter notebook repository](https://github.com/Azure-Samples/Azure-Maps-Jupyter-Notebook/tree/master/AzureMapsJupyterSamples/Tutorials/Analyze%20Weather%20Data). |
| 40 | +
|
| 41 | +## Load the required modules and frameworks |
| 42 | + |
| 43 | +To load all the required modules and frameworks, run the following script: |
| 44 | + |
| 45 | +```python |
| 46 | +import aiohttp |
| 47 | +import pandas as pd |
| 48 | +import datetime |
| 49 | +from IPython.display import Image, display |
| 50 | +``` |
| 51 | + |
| 52 | +## Import weather data |
| 53 | + |
| 54 | +For the sake of this tutorial, we will utilize weather data readings from sensors installed at four different wind turbines. The sample data consists of 30 days of weather readings gathered from weather data centers near each turbine location. The demo data contains data readings for temperature, wind speed and, direction. You can download the demo data from [here](https://github.com/Azure-Samples/Azure-Maps-Jupyter-Notebook/tree/master/AzureMapsJupyterSamples/Tutorials/Analyze%20Weather%20Data/data). The script below imports demo data to the Azure Notebook. |
| 55 | + |
| 56 | +```python |
| 57 | +df = pd.read_csv("./data/weather_dataset_demo.csv") |
| 58 | +``` |
| 59 | + |
| 60 | +## Request daily forecast data |
| 61 | + |
| 62 | +In our example scenario, we would like to request daily forecast for each sensor location. The following script calls the [Daily Forecast API](https://aka.ms/AzureMapsWeatherDailyForecast) of the Azure Maps weather service to get daily weather forecast for each wind turbine, for the next 15 days from the current date. |
| 63 | + |
| 64 | + |
| 65 | +```python |
| 66 | +subscription_key = "Your Azure Maps primary subscription key" |
| 67 | + |
| 68 | +# Get a lists of unique station IDs and their coordinates |
| 69 | +station_ids = pd.unique(df[['StationID']].values.ravel()) |
| 70 | +coords = pd.unique(df[['latitude','longitude']].values.ravel()) |
| 71 | + |
| 72 | +years,months,days = [],[],[] |
| 73 | +dates_check=set() |
| 74 | +wind_speeds, wind_direction = [], [] |
| 75 | + |
| 76 | +# Call azure maps weather service to get daily forecast data for 15 days from current date |
| 77 | +session = aiohttp.ClientSession() |
| 78 | +j=-1 |
| 79 | +for i in range(0, len(coords), 2): |
| 80 | + wind_speeds.append([]) |
| 81 | + wind_direction.append([]) |
| 82 | + |
| 83 | + query = str(coords[i])+', '+str(coords[i+1]) |
| 84 | + forecast_response = await(await session.get("https://atlas.microsoft.com/weather/forecast/daily/json?query={}&api-version=1.0&subscription-key={}&duration=15".format(query, subscription_key))).json() |
| 85 | + j+=1 |
| 86 | + for day in range(len(forecast_response['forecasts'])): |
| 87 | + date = forecast_response['forecasts'][day]['date'][:10] |
| 88 | + wind_speeds[j].append(forecast_response['forecasts'][day]['day']['wind']['speed']['value']) |
| 89 | + wind_direction[j].append(forecast_response['forecasts'][day]['day']['windGust']['direction']['degrees']) |
| 90 | + |
| 91 | + if date not in dates_check: |
| 92 | + year,month,day= date.split('-') |
| 93 | + years.append(year) |
| 94 | + months.append(month) |
| 95 | + days.append(day) |
| 96 | + dates_check.add(date) |
| 97 | + |
| 98 | +await session.close() |
| 99 | +``` |
| 100 | + |
| 101 | +The script below renders the turbine locations on the map by calling the Azure Maps [Get Map Image service](https://docs.microsoft.com/rest/api/maps/render/getmapimage). |
| 102 | + |
| 103 | +```python |
| 104 | +# Render the turbine locations on the map by calling the Azure Maps Get Map Image service |
| 105 | +session = aiohttp.ClientSession() |
| 106 | + |
| 107 | +pins="default|la-25+60|ls12|lc003C62|co9B2F15||'Location A'{} {}|'Location B'{} {}|'Location C'{} {}|'Location D'{} {}".format(coords[1],coords[0],coords[3],coords[2],coords[5],coords[4], coords[7],coords[6]) |
| 108 | + |
| 109 | +image_response = "https://atlas.microsoft.com/map/static/png?subscription-key={}&api-version=1.0&layer=basic&style=main&zoom=6¢er={},{}&pins={}".format(subscription_key,coords[7],coords[6],pins) |
| 110 | + |
| 111 | +static_map_response = await session.get(image_response) |
| 112 | + |
| 113 | +poi_range_map = await static_map_response.content.read() |
| 114 | + |
| 115 | +await session.close() |
| 116 | + |
| 117 | +display(Image(poi_range_map)) |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +In order to augment the demo data with the forecast data, we will group the forecast data with the demo data based on the station ID for the weather data center. |
| 124 | + |
| 125 | +```python |
| 126 | +# Group forecasted data for all locations |
| 127 | +df = df.reset_index(drop=True) |
| 128 | +forecast_data = pd.DataFrame(columns=['StationID','latitude','longitude','Year','Month','Day','DryBulbCelsius','WetBulbFarenheit','WetBulbCelsius','DewPointFarenheit','DewPointCelsius','RelativeHumidity','WindSpeed','WindDirection']) |
| 129 | + |
| 130 | +for i in range(len(station_ids)): |
| 131 | + loc_forecast = pd.DataFrame({'StationID':station_ids[i], 'latitude':coords[0], 'longitude':coords[1], 'Year':years, 'Month':months, 'Day':days, 'WindSpeed':wind_speeds[i], 'WindDirection':wind_direction[i]}) |
| 132 | + forecast_data = pd.concat([forecast_data,loc_forecast], axis=0, sort=False) |
| 133 | + |
| 134 | +combined_weather_data = pd.concat([df,forecast_data]) |
| 135 | +grouped_weather_data = combined_weather_data.groupby(['StationID']) |
| 136 | +``` |
| 137 | + |
| 138 | +The following table displays the combined historical and forecast data for one of the turbine locations. |
| 139 | + |
| 140 | +```python |
| 141 | +# Display data for first location |
| 142 | +grouped_weather_data.get_group(station_ids[0]).reset_index() |
| 143 | +``` |
| 144 | + |
| 145 | +<center> |
| 146 | + |
| 147 | +</center> |
| 148 | + |
| 149 | +## Plot forecast data |
| 150 | + |
| 151 | +In order to see how the wind speed and direction change over the course of next 15 days, we will plot the forecasted values against the days for which they are forecasted. |
| 152 | + |
| 153 | +```python |
| 154 | +# Plot wind speed |
| 155 | +curr_date = datetime.datetime.now().date() |
| 156 | +windsPlot_df = pd.DataFrame({ 'Location A': wind_speeds[0], 'Location B': wind_speeds[1], 'Location C': wind_speeds[2], 'Location D': wind_speeds[3]}, index=pd.date_range(curr_date,periods=15)) |
| 157 | +windsPlot = windsPlot_df.plot.line() |
| 158 | +windsPlot.set_xlabel("Date") |
| 159 | +windsPlot.set_ylabel("Wind speed") |
| 160 | +``` |
| 161 | + |
| 162 | +```python |
| 163 | +#Plot wind direction |
| 164 | +windsPlot_df = pd.DataFrame({ 'Location A': wind_direction[0], 'Location B': wind_direction[1], 'Location C': wind_direction[2], 'Location D': wind_direction[3]}, index=pd.date_range(curr_date,periods=15)) |
| 165 | +windsPlot = windsPlot_df.plot.line() |
| 166 | +windsPlot.set_xlabel("Date") |
| 167 | +windsPlot.set_ylabel("Wind direction") |
| 168 | +``` |
| 169 | + |
| 170 | +The graphs below visualize the forecast data for the change of wind speed (left graph) and direction (right graph) in the next 15 days from the current day. |
| 171 | + |
| 172 | +<center> |
| 173 | + |
| 174 | + </center> |
| 175 | + |
| 176 | + |
| 177 | +## Next steps |
| 178 | + |
| 179 | +In this tutorial you learned, how to call Azure Maps REST APIs to get weather forecast data and visualize the data on graphs. |
| 180 | + |
| 181 | +To learn more about how to call Azure Maps REST APIs inside Azure Notebooks, see [EV routing using Azure Notebooks](https://docs.microsoft.com/azure/azure-maps/tutorial-ev-routing). |
| 182 | + |
| 183 | +To explore the Azure Maps APIs that are used in this tutorial, see: |
| 184 | + |
| 185 | +* [Daily Forecast](https://aka.ms/AzureMapsWeatherDailyForecast) |
| 186 | +* [Render - Get Map Image](https://docs.microsoft.com/rest/api/maps/render/getmapimage) |
| 187 | + |
| 188 | +For a complete list of Azure Maps REST APIs, see [Azure Maps REST APIs](https://docs.microsoft.com/azure/azure-maps/#reference). |
| 189 | + |
| 190 | +To learn more about Azure Notebooks, see [Azure Notebooks](https://docs.microsoft.com/azure/notebooks). |
0 commit comments