#!/usr/bin/env python # coding: utf-8 # # Module 5 # # ## Video 25: Filtering by Vessel # **Python for the Energy Industry** # # [Vessels endpoint documentation.](https://vortechsa.github.io/python-sdk/endpoints/vessels/) # # By now, you should be well used to the process of filtering Cargo Movements, and searching the relevant endpoints to get IDs of interest for filtering. This works in exactly the same way for filtering Cargo Movements that are being carried on a known Vessel: # In[1]: # Some initial imports / settings from datetime import datetime from dateutil.relativedelta import relativedelta import vortexasdk as v now = datetime.utcnow() one_month_ago = now - relativedelta(months=1) required_columns = ["vessels.0.name","vessels.0.vessel_class","product.group.label","product.grade.label","quantity", "status","events.cargo_port_load_event.0.location.port.label","events.cargo_port_load_event.0.end_timestamp", "events.cargo_port_unload_event.0.location.port.label","events.cargo_port_unload_event.0.end_timestamp"] new_labels = ["vessel_name","vessel_class","product_group","product_grade","quantity","status","loading_port","loading_finish","unloading_port","unloading_finish"] relabel = dict(zip(required_columns,new_labels)) # In[13]: # get ID for all vessels with 'CRIMSON' in the name crimson_id = [ves['id'] for ves in v.Vessels().search(term='CRIMSON')] # filter for cargo movements on CRIMSON crimson_cm = v.CargoMovements().search( filter_activity="loading_state", filter_time_min=one_month_ago, filter_time_max=now, filter_vessels=crimson_id) crimson_cm.to_df(columns=required_columns).rename(relabel,axis=1) # Another way of using the Vessels endpoint is to limit searches based on vessel class. Say we are interested only in VLCC movements from Saudi Arabia: # In[4]: # get all vessels of vlcc class vlcc = v.Vessels().search(vessel_classes='vlcc') # get the ids of these vessels vlcc_ids = [ves['id'] for ves in vlcc] # In[5]: # saudi arabia id sa_search = v.Geographies().search("Saudi Arabia",exact_term_match=True) assert len(sa_search) == 1 sa_ID = sa_search[0]['id'] # filter for vlcc movements out of saudi arabia vlcc_query = v.CargoMovements().search( filter_activity="loading_state", filter_time_min=one_month_ago, filter_time_max=now, filter_vessels=vlcc_ids, filter_origins=sa_ID) # In[6]: # convert to dataframe vlcc_query.to_df(columns=required_columns).rename(relabel,axis=1) # ## Storing IDs in a Dictionary # # If there is a group of vessels of interest, it may be useful to store their IDs in a dictionary for easy access (of course, this applies to IDs for all other types of filter, as well). # In[7]: # search for all vlcc vessels with the term 'glory' vlcc_glory = v.Vessels().search(vessel_classes='vlcc',term='glory') vlcc_glory_df = vlcc_glory.to_df() # limit to those with 'glory' in the name vlcc_glory_df = vlcc_glory_df[vlcc_glory_df['name'].str.contains('GLORY')] vlcc_glory_df # In[8]: vlcc_glory_dict = dict(zip(vlcc_glory_df['name'],vlcc_glory_df['id'])) # In[9]: vlcc_glory_dict # In[12]: vlcc_EG_query = v.CargoMovements().search( filter_activity="loading_state", filter_time_min=one_month_ago, filter_time_max=now, filter_vessels=vlcc_glory_dict['DELTA GLORY']) # In[13]: vlcc_EG_query.to_df(columns=required_columns).rename(relabel,axis=1) # ### Exercise # # Make a DataFrame with all vessels. Aggregate this by vessel class, and plot a pie chart of the number of vessels in each class. # In[ ]: