Skip to content

Commit

Permalink
Merge pull request #9 from CIAT-DAPA/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Minotriz02 authored May 28, 2024
2 parents b03af73 + c2c8738 commit 2fe294a
Show file tree
Hide file tree
Showing 6 changed files with 879 additions and 306 deletions.
72 changes: 49 additions & 23 deletions src/aclimate_api/agronomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,64 @@
import json
import pandas as pd


class Agronomy:

def __init__(self, url_root):
self.url_root = url_root

def get_agronomy(self):
url = f"{self.url_root}Agronomic/true/json"
response = requests.get(url)
data = json.loads(response.text)
"""
Retrieves agronomy data from the API.
cultivars_data = []
for crop in data:
for cultivar in crop['cultivars']:
cultivars_data.append({
'crop_id': crop['cp_id'],
'crop_name': crop['cp_name'],
'cultivar_id': cultivar['id'],
'cultivar_name': cultivar['name'],
'cultivar_rainfed': cultivar['rainfed'],
'cultivar_national': cultivar['national']
})
Returns:
dict: A dictionary containing two DataFrames: 'cultivars' and 'soils'.
The 'cultivars' DataFrame contains information about different cultivars,
including crop ID, crop name, cultivar ID, cultivar name, rainfed status,
and national status.
The 'soils' DataFrame contains information about different soils,
including crop ID, crop name, soil ID, and soil name.
"""
try:
url = f"{self.url_root}Agronomic/true/json"
response = requests.get(url)
response.raise_for_status() # Raise an exception if the GET request failed
except requests.exceptions.RequestException as e:
print(f"Error: Failed to retrieve data from {url}")
print(e)
return None

try:
data = json.loads(response.text)
except json.JSONDecodeError as e:
print("Error: Failed to decode the response as JSON")
print(e)
return None

cultivars_data = []
soils_data = []
for crop in data:
for soil in crop['soils']:
soils_data.append({
'crop_id': crop['cp_id'],
'crop_name': crop['cp_name'],
'soil_id': soil['id'],
'soil_name': soil['name']
})
try:
for crop in data:
for cultivar in crop['cultivars']:
cultivars_data.append({
'crop_id': crop['cp_id'],
'crop_name': crop['cp_name'],
'cultivar_id': cultivar['id'],
'cultivar_name': cultivar['name'],
'cultivar_rainfed': cultivar['rainfed'],
'cultivar_national': cultivar['national']
})

for soil in crop['soils']:
soils_data.append({
'crop_id': crop['cp_id'],
'crop_name': crop['cp_name'],
'soil_id': soil['id'],
'soil_name': soil['name']
})
except KeyError as e:
print("Error: Missing key in data")
print(e)
return None

agronomy = {'cultivars': pd.DataFrame(cultivars_data), 'soils': pd.DataFrame(soils_data)}
return agronomy
Expand Down
Loading

0 comments on commit 2fe294a

Please sign in to comment.