{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 6\n", "\n", "## Video 27: Multiple Vessels in a Movement\n", "**Python for the Energy Industry**\n", "\n", "Sometimes, a Cargo Movement will contain information on multiple vessels. There are two reasons this might happen:\n", "- The cargo is loaded to, or unloaded from, a floating storage vessel\n", "- A ship to ship transfer (STS) occurs\n", "\n", "Let's find some example Cargo Movements where this is the case.\n", "\n", "[Cargo Movements documentation](https://vortechsa.github.io/python-sdk/endpoints/cargo_movements/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from dateutil.relativedelta import relativedelta\n", "import pandas as pd\n", "import vortexasdk as v\n", "now = datetime.utcnow()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start as before with a simple query:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm_query = v.CargoMovements().search(\n", " filter_activity=\"any_activity\",\n", " filter_time_min=now,\n", " filter_time_max=now)\n", "\n", "print(len(cm_query))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can find Cargo Movements with STS events by getting those there multiple vessels are detailed:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "multi_vessel = [q for q in cm_query if len(q['vessels']) > 1]\n", "print(len(multi_vessel))" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "Let's take a look at the types of event happening in each of these:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for cm in multi_vessel:\n", " event_types = [e['event_type'] for e in cm['events']]\n", " print(event_types)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## STS Events\n", "\n", "We'll look in more detail at those Cargo Movements with STS events." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sts_movements = [cm for cm in multi_vessel if 'cargo_sts_event' in [e['event_type'] for e in cm['events']]]\n", "print(len(sts_movements))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a look at the vessels in one of them:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sts_movements[0]['vessels'][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sts_movements[0]['vessels'][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " And also the STS event:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "sts_movements[0]['events'][2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "required_columns = [\"vessels.0.name\",\"vessels.0.vessel_class\",\"vessels.1.name\",\"vessels.1.vessel_class\",\"product.group.label\",\"quantity\",\n", " \"events.cargo_port_load_event.0.location.port.label\",\"events.cargo_sts_event.0.location.sts_zone.label\",\n", " \"events.cargo_port_unload_event.0.location.port.label\"]\n", "\n", "new_labels = [\"vessel0\",\"vessel0_class\",\"vessel1\",\"vessel1_class\",\"product_group\",\"quantity\",\"loading_port\",\"sts_location\",\"unloading_port\"]\n", "\n", "relabel = dict(zip(required_columns,new_labels))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm_df = cm_query.to_df(columns=required_columns).rename(relabel,axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm_df_multi = cm_df.dropna(subset=['loading_port','sts_location','unloading_port'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm_df_multi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }