Cloud APIs For a Rainy Day
By: Arnoud Buzing
Tags: Computational Thinking, Api Development, Cloud Services, Data Science, Wolfram

Cloud APIs For a Rainy Day

Writing, Deploying, and Sharing Computations Have Never Been Easier With the Wolfram Language

Photo by Nick Scheerbart on Unsplash

A (long) while back I had the good fortune to take a hiking trip to the western part of Ireland, near Galway and through the Connemara National Park. The scenery was spectacular, but it was the ever-changing weather patterns that really caught my attention. It was not unusual for a single day of hiking to have multiple rain-sun cycles and many days had afternoon rainbows.

Located near the Atlantic Ocean, a warm stream of water arrives from the Gulf of Mexico and the Caribbean Sea. This makes the climate of Ireland milder than can be expected based on its latitude, and also limits extreme temperature swings year-round. But it rains, a lot, in Ireland.

To get an idea of the amount of rainfall in Ireland, I visited the web site of the Irish Meteorological Service (Met Éireann). They offer both current weather information and historical data. From the historical data page, I downloaded a dataset with time-series data for 1850–2010. This dataset includes rainfall for 25 stations across Ireland.

In this story, I will discuss how you can use this data and create a callable API that acts on this data to visualize it.

Each station has its own CSV file. Here is a sample for the station at University College, Galway:

(image by author)

This raw data is best represented in the Wolfram Language with a TimeSeries object. We take the data fields from the cvs expression, generate the monthly dates with DateRange, and construct the TimeSeries (quick note: a notebook with complete code is provided at the end of this story):

rain = Flatten[csv[[4 ;;, 2 ;;]]]; dates = DateRange[

DateObject[{1850, 1}, "Month"],

DateObject[{2010, 12}, "Month"]]; ts = TimeSeries[Thread[{dates, rain}]]

Using this time-series object we can create a visualization function:

(image by author)

Which allows us to create a plot for any time range, for example, the 1950s:

(image by author)

At this point, it is very easy to deploy this function as an API. We define an APIFunction which defines the connection between URL parameters and functional arguments:

api = APIFunction[{

"y1" -> "Integer", "m1" -> "Integer",

"y2" -> "Integer", "m2" -> "Integer"},

ExportForm[GalwayRainPlot[{#y1, #m1}, {#y2, #m2}], "PNG"] &

];

Next, we deploy this APIFunction to a CloudObject with CloudDeploy:

CloudDeploy[api, CloudObject["GalwayRainPlot.api"], Permissions -> "Public"]

At this point, the API is publicly available. You can change the permission settings to keep your APIs more private. Here is an example to call the API for rainfall in Galway for the 1960s.

(image by author)

You can of course follow this workflow for any computation you want to share with others in the cloud. The concept of deploying any computation to the Wolfram Cloud in just a few lines of code is very powerful. For more information check out the following guide page:

As for hiking in Ireland, I would love to go again someday. But if you’re planning to go, make sure to bring a raincoat and an extra pair of dry socks!