<jupyter_start><jupyter_text>Decision tree for allocating oil type to tanker trafficsee google drawing (Tanker_Oil_Attribution)[https://docs.google.com/drawings/d/1-4gl2yNNWxqXK-IOr4KNZxO-awBC-bNrjRNrt86fykU/edit ] for a visual representation<jupyter_code># These will become function inputs
origin = 'Westridge Marine Terminal'
destination = 'Pacific'
ship_type = 'tanker'
random_seed=None
import numpy 
import yaml
import pathlib
# load the list of CAD, US and generic origins and destinations
master_dir  = '/Users/rmueller/Projects/MIDOSS/analysis-rachael/notebooks/monte_carlo/'
master_file = 'master.yaml'

with open(f'{master_dir}{master_file}') as file:
    master = yaml.safe_load(file)

# Assign US and CAD origin/destinations from master file
CAD_origin_destination = master['categories']['CAD_origin_destination']
US_origin_destination = master['categories']['US_origin_destination']

# Get file paths to fuel-type yaml files
home = pathlib.Path(master['directories'])
CAD_yaml     = home/master['files']['cargo_CAD']
WA_in_yaml   = home/master['files']['cargo_WA_in']
WA_out_yaml  = home/master['files']['cargo_WA_out']
US_yaml      = home/master['files']['cargo_US']
Pacific_yaml = home/master['files']['cargo_Pacific']<jupyter_output><empty_output><jupyter_text>Create decision-making tree for tanker trafficAll attributions will be tank-cargo at this point.  I don't think we have decision-making around spill type (fuel or cargo) yet (need to check!).  Will likely add here<jupyter_code>def get_fuel_type(yaml_file, facility):
     with yaml_file.open("rt") as file:
            cargo = yaml.safe_load(file)
            tanker = cargo[facility][ship_type]
            probability = [tanker[fuel]['fraction_of_total'] for fuel in tanker]    
            fuel_type = random_generator.choice(list(tanker.keys()), p = probability)
            return fuel_type
# these pairs need to be used together for "get_fuel_type" (but don't yet have error-checks in place):
# - "WA_in_yaml" and "destination"
# - "WA_out_yaml" and "origin"

# Need to add a catch for erroneous cases where origin-destination in AIS analysis
# pairs vessel-type and facility to null values in the DOE transfer data. 
# For these cases (which shouldn't happen but might), we will use the generic US fuel allocations

# Initialize PCG-64 random number generator
random_generator = numpy.random.default_rng(random_seed)

if origin in CAD_origin_destination:
    if origin == 'Westridge Marine Terminal':
        fuel_type = get_fuel_type(CAD_yaml, origin)
    else:
        if destination in US_origin_destination:
            # we have better information on WA fuel transfers, so I'm prioritizing this information source
            fuel_type = get_fuel_type(WA_in_yaml, destination)
        else:
            fuel_type = get_fuel_type(CAD_yaml, origin)
elif origin in US_origin_destination:  
    fuel_type = get_fuel_type(WA_out_yaml, origin)
elif destination in US_origin_destination:
    fuel_type = get_fuel_type(WA_in_yaml, destination)
elif destination in CAD_origin_destination:
    fuel_type = get_fuel_type(CAD_yaml, destination)
elif origin == 'US':
    fuel_type = get_fuel_type(US_yaml, origin)
elif origin == 'Canada':
    fuel_type = get_fuel_type(US_yaml, origin)
else:
    # this is the error-check allocation for the (hopefully no) cases in which a ship track 
    # wasn't allocated either origin or destination
    fuel_type = random_generator.choice(['diesel','akns'], p = [.5, .5])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09274537695590328, 'fraction_numerical': 0.002844950213371266, 'mean_word_length': 4.754500818330605, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '234945', 'license': 'Apache-2.0', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1074, 'n_words': 310, 'path': 'notebooks/monte_carlo_dev/get_oil_type_tanker.ipynb', 'repo_name': 'MIDOSS/analysis-rachael', 'size': '3515'}