This tutorial gives you a short overview on what is made possible in Scilab 6.1, using the new web tools.
We will take a simple example of an web service / API providing Weather Forecast.
This is how weather looks like for human, through a Graphical User Interface (GUI):
This is how weather looks like for a computer, through an Application Programming Interface (API):
The simplest and most common method used to request ressources over the web is called GET.
Simply use the Scilab function http_get to get this file hosted on our website:
https://www.scilab.org/sites/default/files/weather.txt
(it is formatted in the Javascript Object Notation JSON)
temp = http_get("https://www.scilab.org/sites/default/files/weather.txt") plot(temp.values)
--> temp = http_get("https://www.scilab.org/sites/default/files/weather.txt") temp = type = "temperature" unit = "degrees Celcius" values = [24,25,28,26,27,24,23,19,18,20,22]
Exposing your algorithms through a web service enable to collaborate regardless of the language used behind the server.
In this example, we connect to a Python server implemented with the Flask framework:
testAPI.py (change the extension from txt to py)
To run this code, you will need to install Python & Flask. Then start the server on your localhost (127.0.0.1) on the port 5000
You can view the data served as a JSON, at the following route: http://127.0.0.1:5000/api/weather/
You can perform a simple linear regression locally on the data you gathered:
If you have a Scilab Cloud account, you can perform your compute remotely through our web service offering:
You can simply test the Openweathermap API by testing the fake data provided by the test URL:
https://samples.openweathermap.org/data/2.5/forecast?lat=0&lon=0&appid=xxx
--> openweathermap = http_get("https://samples.openweathermap.org/data/2.5/forecast?lat=0&lon=0&appid=xxx") openweathermap = cod = "200" message = 0.0082 cnt = 40 list: [1x40 struct] with fields: ["dt", "main", "weather", "clouds", "wind", "rain", "sys", "dt_txt"] city: struct with fields: id = 1907296 name = "Tawarano" coord: struct with fields: lat = 35.0164 lon = 139.0077 country = "none"
This very simple web service provides a good demonstrator for the new web tools in Scilab 6.1:
We will use our eternal dataset from weather data in France
This is how the data structure looks like in the response to the get request:
res = 1x134 struct with fields: [ "numer_sta", "date", "pmer", "dirVent", "VitVent(m/s)", "temp(K)", "PointRosée(K)", "Hum(%)", "typeNuageInf", "typeNuageMid", "TypeNuageHaut", "pressionStation"]
For the POST request, we will simplify our dataset to a new spreadsheet with only one column representing the pressure. (it will be easier to upload a simpler structure with only one field).
We will fit a model on this data and predict the evolution on the next cycle. We will not use a linear regression as it only predict the trend, but we will fit a sine function with the help of a FFT to identify the parameters:
The model fitted here on the red part is:
y2 = a * cos(2 * %pi * indic * x2) + b
We will compute 23 additional values and upload them through a loop:
We can then verifying by getting the full dataset and plot it that we appended the new rows predicted
Sources:
Now you know how to get weather data as efficiently as batman gets bad guys!