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     634.471263  23.086912    914.344304  24797.571275
 0     345.156833  11.919926 -52312.890656 -39489.718905
 1     345.254615  11.902066 -48716.729109 -35895.552370
#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                1.203983             3.529468   
 0                      0.391338                1.203983             3.529468   
 1                      0.388371                1.203983             3.529468   

       soil_N_direct  soil_N_indirect  soils_N2O  
index                                             
-1         17.391033         2.004063  19.395095  
 0          9.263421         0.725483   9.988904  
 1          9.248262         0.722649   9.970911  
#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          CO2e
scenario                                                                   
-1         cropland  2020      0.000000    0.000154  0.000004      0.005380
-1        grassland  2020   1061.271545   42.841175  0.001507   2261.223915
-1           forest  2020  -1509.617775    1.926434  0.512809  -1319.783364
-1          wetland  2020    853.805160   81.496830  0.101991   3162.744029
-1            total  2020    405.458930  126.264594  0.616311   4104.189960
 0         cropland  2050      0.000000    0.000154  0.000004      0.005380
 0        grassland  2050   2114.310751   42.838420  0.001256   3314.119304
 0           forest  2050 -55717.249734    1.926434  0.512809 -55527.415323
 0          wetland  2050    853.805160   81.496830  0.101991   3162.744029
 0            total  2050 -52749.133823  126.261838  0.616059 -49050.546611
 1         cropland  2050      0.000000    0.000154  0.000004      0.005380
 1        grassland  2050   2117.334858   42.838412  0.001255   3317.142998
 1           forest  2050 -52123.833616    1.926434  0.512809 -51933.999205
 1          wetland  2050    853.805160   81.496830  0.101991   3162.744029
 1            total  2050 -49152.693598  126.261830  0.616059 -45454.106798
# 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      2016 -1.266127e+06 -2.618384e+05    5102.027979 -1.184498e+05   
1      2017 -7.805690e+05 -1.542363e+05  -61197.065313 -2.938623e+05   
2      2018 -7.505445e+05 -1.593684e+05  -56187.842107 -2.723639e+05   
3      2019 -5.924877e+05 -1.144305e+05  -86351.247069 -2.558944e+05   
4      2020 -6.084902e+05 -1.147854e+05  -97224.693899 -1.998624e+05   
...     ...           ...           ...            ...           ...   
100    2046 -1.002326e+07 -2.256911e+06 -491980.251238 -1.000019e+06   
101    2047 -1.024281e+07 -2.293113e+06 -590479.773195 -1.160341e+06   
102    2048 -9.884681e+06 -2.207148e+06 -751864.227561 -1.435174e+06   
103    2049 -9.831562e+06 -2.188388e+06 -845030.698928 -1.520163e+06   
104    2050 -9.708535e+06 -2.157711e+06 -941986.878994 -1.607536e+06   

                Soil       Harvest  Total Ecosystem  Scenario  
index                                                          
0      -20067.805281 -6.382280e+05    -1.661381e+06        -1  
1      -26011.569723 -9.102110e+05    -1.315876e+06        -1  
2      -26367.823978 -9.165912e+05    -1.264832e+06        -1  
3      -27775.638553 -1.027711e+06    -1.076939e+06        -1  
4      -27576.477034 -1.009912e+06    -1.047939e+06        -1  
...              ...           ...              ...       ...  
100   -203185.136292 -9.064669e+05    -1.397536e+07         1  
101   -212966.329239 -1.025050e+06    -1.449971e+07         1  
102   -224841.380814 -1.443944e+06    -1.450371e+07         1  
103   -232678.633331 -1.636748e+06    -1.461782e+07         1  
104   -241785.071075 -1.866628e+06    -1.465755e+07         1  

[105 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/deeb25e7e9df32e0d2eaff72e5d5656f7f9cdd1cb952ddbdde48c9cfb38d7f7b.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/41b994942fc677dde58af16b0e0fc8759916239e4e5c3b2f8b2f6e20a7e118ad.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