pyrate package

Submodules

pyrate.cli module

Provides a command line interface to the pyrate library

The command line interface (CLI) expects that a configuration file named ‘aistool.conf’ is located in the current folder.

If the config file is not present, a runtime error is raised, and the commands set_default can be used to generate a default configuration file.

pyrate.cli.main()[source]

The command line interface

Type pyrate –help for help on how to use the command line interface

pyrate.config_setter module

Generates a default config file in current folder

pyrate.config_setter.gen_default_config(*args)[source]

Generates a default config file in current folder

This command generates a default configuration file and folder structure in the current folder.

The folders generated are:

repositories
To hold additional repository code for pyrate
algorithms
To hold additional algorithm code for pyrate
aiscsv
For AIS csv files (required by algorithms/aisparser.py)
baddata
For AIS import logfiles (required by algorithms/aisparser.py)

pyrate.loader module

This module provides the Loader class which loads a pyrate session from a configuation file. This session can then run tasks on data repositories and algorithms.

class pyrate.loader.Loader(config=None)[source]

Bases: object

The Loader joins together data repositories and algorithms, and executes operations on them.

execute_algorithm_command(algname, command, **args)[source]

Execute the specified command on the specified algorithm

execute_repository_command(reponame, command, **args)[source]

Execute the specified command on the specified repository.

get_algorithm(name)[source]

Returns the algorithm module specified.

get_algorithm_commands(algname)[source]

Returns a list of available commands for the specified algorithm

get_algorithms()[source]

Returns a set of the names of available algorithms

get_data_repositories()[source]

Returns a set of the names of available data repositories

get_data_repository(name, readonly=False)[source]

Returns a loaded instance of the specified data repository.

get_repository_commands(repo_name)[source]

Returns a list of available commands for the specified repository

pyrate.loader.load_all_modules(paths)[source]

Load all modules on the given paths.

pyrate.loader.load_module(name, paths)[source]

Load module name using the given search paths.

pyrate.utils module

pyrate.utils.detect_location_outliers(msg_stream, as_df=False)[source]

Detects outlier messages by submitting messages to a speed test

The algorithm proceeds as follows:

  1. Create a linked list of all messages with non-null locations (pointing to next message)

  2. Loop through linked list and check for location outliers:

    • A location outlier is who does not pass the speed test (<= 50kn; link is ‘discarded’ when not reached in time)

    • No speed test is performed when:

      • distance too small (< 0.054nm ~ 100m; catches most positioning inaccuracies) => no outlier
      • time gap too big (>= 215h ~ 9d; time it takes to get anywhere on the globe at 50kn not respecting land) => next message is new ‘start’

    If an alledged outlier is found its link is set to be the current message’s link

  3. The start of a linked list becomes special attention: if speed check fails, the subsequent link is tested

Line of thinking is: Can I get to the next message in time? If not ‘next’ must be an outlier, go to next but one.

Parameters:
  • msg_stream – A list of dictionaries representing AIS messages for a single MMSI number. Dictionary keys correspond to the column names from the ais_clean table. The list of messages should be ordered by timestamp in ascending order.
  • as_df (bool, optional) – Set to True if msg_stream are passed as a pandas DataFrame
Returns:

The rows in the message stream which are outliers

Return type:

outlier_rows

pyrate.utils.interpolate_passages(msg_stream)[source]

Interpolate far apart points in an ordered stream of messages.

Parameters:msg_stream – A list of dictionaries representing AIS messages for a single MMSI number. Dictionary keys correspond to the column names from the ais_clean table. The list of messages should be ordered by timestamp in ascending order.
Returns:artificial_messages – A list of artificial messages to fill in gaps/navigate around land.
Return type:list
pyrate.utils.is_valid_cog(cog)[source]

Validates course over ground

Parameters:cog (float) – Course over ground
Returns:
Return type:True if course over ground is greater than zero and less than 360 degrees
pyrate.utils.is_valid_heading(heading)[source]

Validates heading

Parameters:heading (float) – The heading of the ship in degrees
Returns:
Return type:True if heading is greater than zero and less than 360 degrees
pyrate.utils.is_valid_sog(sog)[source]

Validates speed over ground

Parameters:sog (float) – Speed over ground
Returns:
Return type:True if speed over ground is greater than zero and less than 102.2
pyrate.utils.speed_calc(msg_stream, index1, index2)[source]

Computes the speed between two messages in the message stream

Parameters:
  • msg_stream – A list of dictionaries representing AIS messages for a single MMSI number. Dictionary keys correspond to the column names from the ais_clean table. The list of messages should be ordered by timestamp in ascending order.
  • index1 (int) – The index of the first message
  • index2 (int) – The index of the second message
Returns:

  • timediff (datetime) – The difference in time between the two messages in datetime
  • dist (float) – The distance between messages in nautical miles
  • speed (float) – The speed in knots

pyrate.utils.valid_imo(imo=0)[source]

Check valid IMO using checksum.

Parameters:imo (integer) – An IMO ship identifier
Returns:
Return type:True if the IMO number is valid

Notes

Taken from Eoin O’Keeffe’s checksum_valid function in pyAIS

pyrate.utils.valid_latitude(lat)[source]

Check valid latitude.

Parameters:lon (integer) – A latitude
Returns:
Return type:True if the latitude is valid
pyrate.utils.valid_longitude(lon)[source]

Check valid longitude.

Parameters:lon (integer) – A longitude
Returns:
Return type:True if the longitude is valid
pyrate.utils.valid_message_id(message_id)[source]
pyrate.utils.valid_mmsi(mmsi)[source]

Checks if a given MMSI number is valid.

Parameters:mmsi (int) – An MMSI number
Returns:
Return type:Returns True if the MMSI number is 9 digits long.
pyrate.utils.valid_navigational_status(status)[source]

Module contents

pyrate.get_resource_filename(resource_name)[source]

Returns the absolute path associated with the file

Parameters:path (str) – The expected file path
Returns:path – The absolute file path
Return type:str