Example Usage

Here is a demonstartion of the potential usage of theGrassland Production module.

Imports

Firstly, we start of by importing the relevant classes from the toolkit.

import pandas as pd
from grassland_production.grassland_output import GrasslandOutput
import shutil
import os

import warnings

# Filter out the RuntimeWarning related to unsupported linux distribution
warnings.filterwarnings("ignore", message="unsupported linux distribution:", category=RuntimeWarning)

The GrasslandOutput class abstracts away the lower level details for the user and allows for easier access to final outputs from the model.

Grassland Model

In the example below, we will set the path to the input data and make the necessary directory.

#check for previous test data and remove if exists
if os.path.exists("./test_data"):
    shutil.rmtree("./test_data")

#create new test data directory
os.mkdir("./test_data")

#set up test data
path_to_data = "../tests/data/"

ef_country = "ireland"
calibration_year = 2020
target_year = 2050

scenario_dataframe = pd.read_csv(os.path.join(path_to_data,"scenario_input_dataframe2.csv"))
scenario_animal_dataframe = pd.read_csv(os.path.join(path_to_data,"scenario_animal_data.csv"))
baseline_animal_dataframe = pd.read_csv(os.path.join(path_to_data,"baseline_animal_data.csv"))

Define classes and Generate Data

#class instance
grassland = GrasslandOutput(
    ef_country,
    calibration_year,
    target_year,
    scenario_dataframe,
    scenario_animal_dataframe,
    baseline_animal_dataframe,
)
#total destocked area
print(grassland.total_spared_area())
                 0             1
2020  0.000000e+00  0.000000e+00
2050  2.850667e+06  2.863748e+06
#total remaining grassland 
print(grassland.total_grassland_area())
                 0             1
2020  3.910393e+06  3.910393e+06
2050  1.059726e+06  1.046645e+06
#farm inputs (nitrogen, phosphorus, potassium, lime)
print(grassland.farm_inputs_data())
  ef_country  farm_id    year  total_urea_kg  total_lime_kg     an_n_fert  \
0    ireland      0.0  2050.0   4.944569e+07    907916000.0  9.098007e+07   
1    ireland      1.0  2050.0   4.883536e+07    907916000.0  8.985706e+07   

    urea_n_fert  urea_abated_n_fert  total_p_fert  total_k_fert  diesel_kg  \
0  2.274502e+07                 0.0  1.871038e+07  4.262923e+07        0.0   
1  2.246426e+07                 0.0  1.847943e+07  4.210304e+07        0.0   

   elec_kwh  
0       0.0  
1       0.0  
#baseline (calibration) farm inputs (nitrogen, phosphorus, potassium, lime)
print(grassland.baseline_farm_inputs_data())
  ef_country  farm_id    year  total_urea_kg  total_lime_kg    an_n_fert  \
0    ireland   2020.0  2020.0    149185000.0    907916000.0  310893900.0   

   urea_n_fert  urea_abated_n_fert  total_p_fert  total_k_fert  diesel_kg  \
0   68625100.0                 0.0   62439555.64   142260647.1        0.0   

   elec_kwh  
0       0.0  
#total destocked area by soil group
print(grassland.total_spared_area_breakdown())
    Scenario  year cohort  soil_group        area_ha
0          0  2050  dairy           1  233028.671469
1          0  2050   beef           1  460342.311336
2          0  2050  sheep           1  446884.934949
3          0  2050  dairy           2  165307.342757
4          0  2050   beef           2  551314.720481
5          0  2050  sheep           2  664324.470462
6          0  2050  dairy           3   22293.356657
7          0  2050   beef           3   84396.090412
8          0  2050  sheep           3  222775.475034
9          1  2050  dairy           1  231901.287521
10         1  2050   beef           1  458115.192547
11         1  2050  sheep           1  453725.066920
12         1  2050  dairy           2  164507.592050
13         1  2050   beef           2  548647.480598
14         1  2050  sheep           2  674492.786048
15         1  2050  dairy           3   22185.502236
16         1  2050   beef           3   83987.785300
17         1  2050  sheep           3  226185.331867
#per hectare stocking rate
print(grassland.grassland_stocking_rate())
           dairy      beef     sheep
0 2020  1.371872  1.365295  1.309918
  2050  1.966824  2.188550  1.850708
1 2020  1.371872  1.365295  1.309918
  2050  1.980754  2.204051  0.000000
#save results to csv
test_data_path = "./test_data"

grassland.total_spared_area().to_csv(os.path.join(test_data_path,"spared_area.csv"))
grassland.total_grassland_area().to_csv(os.path.join(test_data_path,"total_grassland_area.csv"))
grassland.total_spared_area_breakdown().to_csv(os.path.join(test_data_path,"spared_area_breakdown.csv"))
grassland.total_concentrate_feed().to_csv(os.path.join(test_data_path,"concentrate_feed.csv"))
grassland.grassland_stocking_rate().to_csv(os.path.join(test_data_path,"stocking_rate.csv"))