# Load the libraries
import requests
from requests.auth import HTTPBasicAuth
import pandas as pd
# Define the API parameters setting
="13.3123"
longi="42.4560"
lat="2021-01-01"
start_date="2021-12-31"
end_date="daily"
temporalapi
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/BasicWeatherData?long="+longi+"&lat="+lat+"&start_date="+start_date+"&end_date="+end_date+"&temporalapi=daily"
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the POST request
= requests.post(url,
response =headers,
headers=HTTPBasicAuth(USEREMAIL, APIKEY))
auth
# Convert from response to json
= response.json()
data
# Convert from json to pandas
= pd.DataFrame(data)
df
# Let's see the first 5 agricultural products
df.head()
Wheater data
In this part we will focus on two endpoint:
- “AFS/BasicWeatherData” endpoint, which allow the user to get historical climate data
- “AFS/ForecastWeatherData” endpoint, which allow the user to get 3h forecast data for the next 5 days
BasicWeatherData
Python
In this example we will test the BasicWeatherDataendpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
- start_date, Start date of the time period to acquire the meteorological data (example yyyy-mm-dd: 2021-01-01)
- end_date, End date of the time period to acquire weather data (example yyyy-mm-dd: 2021-12-31)
- temporalapi, Time period of the data call. Can be set as hourly, daily, monthly or climatology
With your information and let’s try out the API.
R
In this example we will test the BasicWeatherDataendpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
- start_date, Start date of the time period to acquire the meteorological data (example yyyy-mm-dd: 2021-01-01)
- end_date, End date of the time period to acquire weather data (example yyyy-mm-dd: 2021-12-31)
- temporalapi, Time period of the data call. Can be set as hourly, daily, monthly or climatology
With your information and let’s try out the API.
# import libraries
library(tictoc)
library(httr)
library(gt)
library(tidyverse)
# Define the API parameters setting
="13.3123"
longi="42.4560"
lat="2021-01-01"
start_date="2021-12-31"
end_date="daily"
temporalapi
# Define the url of the API
<- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/BasicWeatherData?long=", longi, "&lat=",lat, "&start_date=", start_date, "&end_date=", end_date, "&temporalapi=", temporalapi)
api_url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Get the start time to API Call
tic()
# Make the POST request
<- POST(
r
api_url,::authenticate(
httruser = Sys.getenv(USEREMAIL),
password = Sys.getenv(APIKEY)
)
)
# Print out the seconds needed to create the prescription map
print("The API needed:")
[1] "The API needed:"
toc()
5.27 sec elapsed
# Get the status of the request
::status_code(r) httr
[1] 200
# Print out the list of the Phytofarmaceutical products
<- httr::content(r, as = "text", type = "application/json", encoding="UTF-8")
cont <-jsonlite::fromJSON(cont) %>% as.data.frame
contgt(cont[1:5,])
LON | LAT | YEAR | MM | DD | DOY | YYYYMMDD | T2M | PRECTOTCORR | RH2M | WS2M | CLOUD_AMT | ALLSKY_SFC_PAR_TOT |
---|---|---|---|---|---|---|---|---|---|---|---|---|
13.3123 | 42.456 | 2021 | 1 | 1 | 1 | 2021-01-01 | 1.38 | 5.51 | 97.88 | 2.27 | 81.79 | 12.91 |
13.3123 | 42.456 | 2021 | 1 | 2 | 2 | 2021-01-02 | 4.18 | 16.15 | 96.75 | 3.58 | 90.93 | 20.18 |
13.3123 | 42.456 | 2021 | 1 | 3 | 3 | 2021-01-03 | 2.06 | 9.47 | 96.19 | 1.79 | 97.71 | 22.42 |
13.3123 | 42.456 | 2021 | 1 | 4 | 4 | 2021-01-04 | 0.98 | 6.48 | 95.81 | 1.35 | 74.50 | 31.73 |
13.3123 | 42.456 | 2021 | 1 | 5 | 5 | 2021-01-05 | 0.59 | 14.90 | 97.25 | 2.00 | 91.30 | 12.58 |
Node.js
In this example we will test the BasicWeatherDataendpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
- start_date, Start date of the time period to acquire the meteorological data (example yyyy-mm-dd: 2021-01-01)
- end_date, End date of the time period to acquire weather data (example yyyy-mm-dd: 2021-12-31)
- temporalapi, Time period of the data call. Can be set as hourly, daily, monthly or climatology
// load libraries
const axios = require('axios');
// Set username e apikey
const username = 'XXXXXXXXXXXXXX';
const password = 'XXXXXXXXXXXXXX';
// set the apiurl
const apiurl='https://www.api.automaticfarmsolutionwebapp.com/AFS/BasicWeatherData?long=';
// set the product_name parameter
const longi="13.3123";
const lat="42.4560";
const start_date="2021-01-01";
const end_date="2021-12-31";
const temporalapi="daily";
// set the apiendpoint
const url = apiurl.concat(longi, "&lat=", lat, "&start_date=", start_date, "&end_date=", end_date, "&temporalapi=", temporalapi)
const options = {
method: 'post',
url: url,
auth: {
username: username,
password: password,
,
};
}
axios(options)
.then(response => {
console.log('Risposta JSON:', response.data);
}).catch(error => {
console.error('Errore:', error.message);
; })
ForecastWeatherData
Python
In this example we will test the BasicWeatherDataendpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
With your information and let’s try out the API.
# Load the libraries
import requests
from requests.auth import HTTPBasicAuth
import pandas as pd
# Define the API parameters setting
="13.3123"
longi="42.4560"
lat
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/ForecastWeatherData?long="+longi+"&lat="+lat
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the POST request
= requests.post(url,
response =headers,
headers=HTTPBasicAuth(USEREMAIL, APIKEY))
auth
# Convert from response to json
= response.json()
data
# Convert from json to pandas
= pd.DataFrame(data)
df
# Let's see the first 5 agricultural products
df.head()
R
In this example we will test the BasicWeatherDataendpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
With your information and let’s try out the API.
# import libraries
library(tictoc)
library(httr)
library(gt)
library(tidyverse)
# Define the API parameters setting
="13.3123"
longi="42.4560"
lat
# Define the url of the API
<- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/ForecastWeatherData?long=", longi, "&lat=",lat)
api_url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Get the start time to API Call
tic()
# Make the POST request
<- POST(
r
api_url,::authenticate(
httruser = Sys.getenv(USEREMAIL),
password = Sys.getenv(APIKEY)
)
)
# Print out the seconds needed to create the prescription map
print("The API needed:")
[1] "The API needed:"
toc()
1.06 sec elapsed
# Get the status of the request
::status_code(r) httr
[1] 200
# Print out the list of the Phytofarmaceutical products
<- httr::content(r, as = "text", type = "application/json", encoding="UTF-8")
cont <-jsonlite::fromJSON(cont) %>% as.data.frame
contgt(cont[1:5,])
dt_txt | temp | pressure | humidity | temp_min | temp_max | weather_id | weather_main | weather_description | weather_icon | wind_speed | wind_deg | clouds_all | dt | visibility | pop | feels_like | sea_level | grnd_level | temp_kf | wind_gust | sys_pod | rain_3h |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2023-08-04 09:00:00 | 23.44 | 1005 | 58 | 23.44 | 27.65 | 800 | Clear | clear sky | 01d | 3.20 | 215 | 0 | 1691139600 | 10000 | 0.00 | 23.35 | 1005 | 923 | -4.21 | 6.92 | d | NA |
2023-08-04 12:00:00 | 25.43 | 1006 | 50 | 25.43 | 29.42 | 800 | Clear | clear sky | 01d | 6.82 | 223 | 1 | 1691150400 | 10000 | 0.00 | 25.33 | 1006 | 922 | -3.99 | 10.00 | d | NA |
2023-08-04 15:00:00 | 24.23 | 1007 | 60 | 24.23 | 24.63 | 500 | Rain | light rain | 10d | 5.60 | 232 | 39 | 1691161200 | 10000 | 0.58 | 24.28 | 1007 | 921 | -0.40 | 7.38 | d | 0.21 |
2023-08-04 18:00:00 | 18.57 | 1009 | 88 | 18.57 | 18.57 | 501 | Rain | moderate rain | 10d | 2.29 | 191 | 73 | 1691172000 | 10000 | 0.94 | 18.78 | 1009 | 919 | 0.00 | 2.56 | d | 3.62 |
2023-08-04 21:00:00 | 16.21 | 1009 | 92 | 16.21 | 16.21 | 501 | Rain | moderate rain | 10n | 1.54 | 231 | 100 | 1691182800 | 10000 | 0.92 | 16.29 | 1009 | 919 | 0.00 | 1.68 | n | 3.76 |
Node.js
In this example we will test the BasicWeatherDataendpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- long, Longitudinal coordinate (WGS 84) of the site whose weather data you want to receive (example: 13.3123)
- lat, Latitude coordinate (WGS 84) of the site whose weather data you want to receive (example: 42.4560)
With your information and let’s try out the API.
// load libraries
const axios = require('axios');
// Set username e apikey
const username = 'XXXXXXXXXXXXXX';
const password = 'XXXXXXXXXXXXXX';
// set the apiurl
const apiurl='https://www.api.automaticfarmsolutionwebapp.com/AFS/ForecastWeatherData?long=';
// set the product_name parameter
const longi="13.3123";
const lat="42.4560";
// set the apiendpoint
const url = apiurl.concat(longi, "&lat=", lat)
const options = {
method: 'post',
url: url,
auth: {
username: username,
password: password,
,
};
}
axios(options)
.then(response => {
console.log('Risposta JSON:', response.data);
}).catch(error => {
console.error('Errore:', error.message);
; })
Easy - Fast - Customizable