Python for the Energy Industry
Sometimes, a Cargo Movement will contain information on multiple vessels. There are two reasons this might happen:
Let's find some example Cargo Movements where this is the case.
from datetime import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd
import vortexasdk as v
now = datetime.utcnow()
We start as before with a simple query:
cm_query = v.CargoMovements().search(
filter_activity="any_activity",
filter_time_min=now,
filter_time_max=now)
print(len(cm_query))
We can find Cargo Movements with STS events by getting those there multiple vessels are detailed:
multi_vessel = [q for q in cm_query if len(q['vessels']) > 1]
print(len(multi_vessel))
Let's take a look at the types of event happening in each of these:
for cm in multi_vessel:
event_types = [e['event_type'] for e in cm['events']]
print(event_types)
We'll look in more detail at those Cargo Movements with STS events.
sts_movements = [cm for cm in multi_vessel if 'cargo_sts_event' in [e['event_type'] for e in cm['events']]]
print(len(sts_movements))
Let's take a look at the vessels in one of them:
sts_movements[0]['vessels'][0]
sts_movements[0]['vessels'][1]
And also the STS event:
sts_movements[0]['events'][0]
Let's view the data on movements with STS transfers in a DataFrame. The easiest way to do this is to convert our initial search into a DataFrame using the to_df()
function, and then drop all movements with no STS event.
required_columns = ["vessels.0.name","vessels.0.vessel_class","vessels.1.name","vessels.1.vessel_class","product.group.label","quantity",
"events.cargo_port_load_event.0.location.port.label","events.cargo_sts_event.0.location.sts_zone.label",
"events.cargo_port_unload_event.0.location.port.label"]
new_labels = ["vessel0","vessel0_class","vessel1","vessel1_class","product_group","quantity","loading_port","sts_location","unloading_port"]
relabel = dict(zip(required_columns,new_labels))
cm_df = cm_query.to_df(columns=required_columns).rename(relabel,axis=1)
cm_df_multi = cm_df.dropna(subset=['loading_port','sts_location','unloading_port'])
cm_df_multi
Expand the range of the Cargo Movements - say to a ~2 week period. Can you find any movements involving 3 or more vessels? If so, isolate and have a look at these.