Example Usage

Here is a demonstartion of the potential usage of GOBLIN lite.

Imports

Firstly, we start of by importing the relevant classes.

import os
from IPython.display import Image

from goblin_lite.goblin import ScenarioRunner
from goblin_lite.scenario_analysis.data_grapher import DataGrapher
from goblin_lite.resource_manager.data_fetcher import DataFetcher
import shutil
import os


import warnings

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

The ScenarioRunner class is basically used to set to run the scenarios based on the configuration files for goblin and the CBM CFS3.

The DataFetcher class is used to retrieve the various results as dataframes from the scenario run. Each of the methods in the DataFetcher class relates to specific table.

The DataGrapher class is still being developed, but contains some basic plotting to assist the user in visualising the results tables.

The Scenario Runner

In the first example below, we will set the path to the configurations, define the emissions factor country and the base and target year. Finally, we will create a directory for our outputs

#configuration 

goblin_config = "../tests/data/config.json" #location of test goblin configuration 

cbm_config = "../tests/data/cbm_factory.yaml" #location of test cbm configuration

ef_country = "ireland" #emissions factor country

#years for the baseline, and the target year 
baseline_year = 2020
target_year = 2050

#Make directory to store exported file

os.mkdir("jupyter_example")

data_path = "./jupyter_example/"

Define classes and Run Scenarios

#class instances
runner_class = ScenarioRunner(ef_country, baseline_year,target_year, goblin_config, cbm_config)
graph_class = DataGrapher()
fetcher_class = DataFetcher()
#Run Scenarios 
runner_class.run_scenarios()
Existing tables have been deleted.
JSON file detected
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
... calibration year not present, 2015 default year used for Spread Manure Dictionary...
Scenario Generation Complete ... 

If the calibration year is not present for a specific dataset, the default year of 2015 is used.

The Data Fetcher

The Scenario Run is now complete, and we can use the DataFectcher class to retrieve our data.

Note: Once a scenario run is complete, when the user again runs scenarios, table output from the previous run is completely deleted.

Here we will retrieve data for:

- Livestock outputs 
- Aggregated emissions
- Animal emissions by category 
- Land use emissions by category 
- Crop emissions by category 
- Forest carbon flux 

These are not the only potential returns, as there are several additional methods returning results out. Please consult the documentation for further details.

#Livestock outputs 

animal_output_df = fetcher_class.get_livestock_output_summary()

print(animal_output_df)

# baseline is index -1 
           total_milk_kg  total_beef_kg
Scenarios                              
-1          8.251443e+09   6.528033e+08
 0          5.339220e+09   2.596987e+08
 1          5.339220e+09   2.596987e+08
#Aggregated emissions

aggregated_emmissions_df = fetcher_class.get_climate_change_emission_totals()

print(aggregated_emmissions_df)

# baseline is index -1 
              CH4        N2O           CO2          CO2e
index                                                   
-1     522.224424  23.967861   6747.482676  27721.249819
 0     232.911197  12.800875 -48934.172043 -39020.426644
 1     233.008983  12.783015 -44979.444631 -35067.694148
#Animal emissions by category

animal_emissions_df = fetcher_class.get_climate_change_animal_emissions_by_category()

print(animal_emissions_df)

# baseline is index -1 
       enteric_ch4  manure_management_N2O  manure_management_CH4  \
index                                                              
-1      465.584094               3.075505              42.622576   
 0      201.478299               1.314963              17.416696   
 1      201.562043               1.315096              17.430742   

       manure_applied_N  N_direct_PRP  N_indirect_PRP  N_direct_fertiliser  \
index                                                                        
-1             0.920240      4.628077        0.765050             7.109264   
 0             0.396043      2.014180        0.334145             2.119746   
 1             0.396043      2.015091        0.334278             2.103677   

       N_indirect_fertiliser   soils_CO2  soil_organic_N_direct  \
index                                                             
-1                  1.239013  508.885373               5.548316   
 0                  0.391338  436.243167               2.410223   
 1                  0.388371  435.964488               2.411134   

       soil_organic_N_indirect  soil_inorganic_N_direct  \
index                                                     
-1                    0.765050                 7.109264   
 0                    0.334145                 2.119746   
 1                    0.334278                 2.103677   

       soil_inorganic_N_indirect  soil_histosol_N_direct  crop_residue_direct  \
index                                                                           
-1                      1.239013                2.128494             3.529468   
 0                      0.391338                2.128494             3.529468   
 1                      0.388371                2.128494             3.529468   

       soil_N_direct  soil_N_indirect  soils_N2O  
index                                             
-1         18.315543         2.004063  20.319606  
 0         10.187932         0.725483  10.913414  
 1         10.172773         0.722649  10.895422  
#Land use emissions by category 

land_emissions_df = fetcher_class.get_landuse_emissions_totals()

print(land_emissions_df)

# baseline is index -1 
           land_use  year           CO2           CH4           N2O  \
scenario                                                              
-1         cropland  2020      0.000000  1.542857e-07  4.000000e-09   
-1        grassland  2020   6132.388421  8.939332e+00  2.618187e-03   
-1           forest  2020  -2131.638526  1.615270e+00  5.113616e-01   
-1          wetland  2020   2237.847408  3.463152e+00  5.877036e-02   
-1            total  2020   6238.597303  1.401775e+01  5.727501e-01   
 0         cropland  2050      0.000000  1.542857e-07  4.000000e-09   
 0        grassland  2050   6131.300543  8.937781e+00  2.365710e-03   
 0           forest  2050 -57553.461838  1.615270e+00  5.113616e-01   
 0          wetland  2050   2051.746085  3.463152e+00  5.877036e-02   
 0            total  2050 -49370.415210  1.401620e+01  5.724976e-01   
 1         cropland  2050      0.000000  1.542857e-07  4.000000e-09   
 1        grassland  2050   6131.297419  8.937776e+00  2.364985e-03   
 1           forest  2050 -53598.452623  1.615270e+00  5.113616e-01   
 1          wetland  2050   2051.746085  3.463152e+00  5.877036e-02   
 1            total  2050 -45415.409120  1.401620e+01  5.724969e-01   

                  CO2e  
scenario                
-1            0.000005  
-1         6383.383542  
-1        -1950.900155  
-1         2350.389800  
-1         6782.873192  
 0            0.000005  
 0         6382.185323  
 0       -57372.723466  
 0         2164.288477  
 0       -48826.249661  
 1            0.000005  
 1         6382.181882  
 1       -53417.714252  
 1         2164.288477  
 1       -44871.243888  
# Crop emissions by category 

crop_emissions_df= fetcher_class.get_climate_change_crop_emissions_by_category()

print(crop_emissions_df)

# baseline is index -1 
       crop_residue_direct  N_direct_fertiliser  N_indirect_fertiliser  \
index                                                                    
-1                3.529468             0.491902               0.090813   
 0                3.529468             0.491902               0.090813   
 1                3.529468             0.491902               0.090813   

       soils_CO2  soils_N2O  
index                        
-1      6.166289   4.112183  
 0      6.166289   4.112183  
 1      6.166289   4.112183  
#Forest carbon flux 

forest_flux_df = fetcher_class.get_forest_flux()

print(forest_flux_df)

# baseline is index -1 
       Year           AGB           BGB       Deadwood        Litter  \
index                                                                  
0      2017 -1.270048e+06 -2.626540e+05   12332.273072 -2.960829e+04   
1      2018 -7.392698e+05 -1.450470e+05  -95682.240227 -2.227229e+05   
2      2019 -7.529513e+05 -1.597873e+05  -51169.806783 -2.161922e+05   
3      2020 -5.977532e+05 -1.155789e+05  -82323.881084 -2.118970e+05   
4      2021 -6.290696e+05 -1.193461e+05  -83654.421934 -1.636219e+05   
...     ...           ...           ...            ...           ...   
97     2046 -9.941789e+06 -2.247434e+06 -436671.321894 -9.043702e+05   
98     2047 -1.031635e+07 -2.324601e+06 -513703.780947 -1.030944e+06   
99     2048 -1.052731e+07 -2.358651e+06 -624057.623622 -1.198516e+06   
100    2049 -1.016328e+07 -2.270876e+06 -782085.789839 -1.486257e+06   
101    2050 -1.011758e+07 -2.253391e+06 -882709.572478 -1.567570e+06   

                Soil       Harvest  Total Ecosystem  Scenario  
index                                                          
0      -10725.238142 -6.382280e+05    -1.560704e+06        -1  
1      -16941.873768 -9.084560e+05    -1.219664e+06        -1  
2      -16889.374509 -9.165473e+05    -1.196990e+06        -1  
3      -18205.419547 -1.027711e+06    -1.025758e+06        -1  
4      -17845.259170 -1.009415e+06    -1.013537e+06        -1  
...              ...           ...              ...       ...  
97    -188337.035950 -8.702853e+05    -1.371860e+07         1  
98    -197508.820602 -9.219153e+05    -1.438311e+07         1  
99    -207733.703278 -1.049438e+06    -1.491627e+07         1  
100   -219980.042404 -1.487515e+06    -1.492247e+07         1  
101   -228160.887283 -1.680597e+06    -1.504941e+07         1  

[102 rows x 9 columns]

These are simple examples, additional information on inputs and outputs can be retrieved. Furhter impact categories related to eutrophication and air quality are also available.

DataGrapher

The data grapher allows the user to plot some of the data from the generated tables

#save image
graph_class.plot_animal_lca_emissions_by_category(data_path)

#retrieve an display
image_path = os.path.join(data_path,"Livestock_LCA.png")

# display the image
Image(filename=image_path)
_images/a4240e8e78066d7dd70f4df80dc1078d23d21d3a0ee1e466b03f4300476d9c83.png
#Land use emissions

graph_class.plot_land_use_emissions(data_path)

#retrieve an display
image_path = os.path.join(data_path,"climate_change_land_use.png")

# display the image
Image(filename=image_path)
_images/b2084e6cb3b34de3421c2bca978f63c348abc542a5dd75f09942e77f62d29d42.png
# Remove Generated files 

for filename in os.listdir(data_path):
    file_path = os.path.join(data_path, filename)
    try:
        if os.path.isfile(file_path):
            os.remove(file_path)
            print(f"Deleted file: {file_path}")
    except Exception as e:
        print(f"Error deleting file: {file_path}. Reason: {e}")
        
#Remove direcotry 
os.rmdir(data_path)
Deleted file: ./jupyter_example/climate_change_land_use.png
Deleted file: ./jupyter_example/Livestock_LCA.png