Spaces:
Runtime error
Runtime error
import requests | |
import io | |
import logging | |
import xarray as xr | |
from datetime import date, timedelta | |
import json | |
with open('credentials.txt') as f: | |
pwd = f.readlines() | |
response = requests.post( | |
"https://api.ombreapp.fr/api/v1.0/links/token/", | |
data={"email": "[email protected]", "password": pwd, "permission": True}, | |
) | |
token = response.json()["access"] | |
def get_air_sensors_data(plot_id, position,date_from = False, date_to= False, night=False): | |
if not date_from: | |
# Get Yesterday data by default | |
if night : | |
date_from = (date.today() - timedelta(2)).strftime("%Y-%m-%dT%H:%M:%S")[0:11] + "12:00:00" | |
date_to = (date.today() - timedelta(1)).strftime("%Y-%m-%dT%H:%M:%S")[0:11] + "11:50:00" | |
else : | |
date_from = (date.today() - timedelta(1)).strftime("%Y-%m-%dT%H:%M:%S") | |
date_to = date_from[0:11] + "23:50:00" | |
headers = {'Authorization': f"Bearer {token}", "Accept": "application/x-netcdf"} | |
# Get Climatic dataset as netcdf file | |
response = requests.get( | |
f"https://api.ombreapp.fr/api/v2.0/plots/{plot_id}/climatic_dataset/", | |
params={ | |
"start_time": date_from, | |
"end_time": date_to, | |
"position": position | |
}, | |
headers=headers | |
) | |
if response.status_code == 200: | |
# Open Dataset | |
ds_out = xr.open_dataset(io.BytesIO(response.content)) | |
else: | |
logging.error(f"Error {response.status_code}: {response.content}") | |
ds_out | |
df_air = ds_out[['air_temperature','relative_humidity','photon_flux_density','wind_speed']].to_dataframe() | |
return df_air | |
def get_lat_lon(plot_id): | |
headers = {'Authorization': f"Bearer {token}"} | |
response = requests.get( | |
"https://api.ombreapp.fr/api/v2.0/plots/", | |
headers=headers | |
) | |
plots = json.loads(response.content) | |
plot = [plots[i] for i in range(len(plots)) if plots[i]['id'] == plot_id][0] | |
return (plot['zone']['coordinates'][0][0][1],plot['zone']['coordinates'][0][0][0]) | |