File size: 2,061 Bytes
98a2104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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])