Skip to content

utils

Miscellaneous utility functions that may be of use throughout PolarRoute

case_from_angle(start, end)

Determine the direction of travel between two points in the same cell and return the associated case

Parameters:

Name Type Description Default
start list

the coordinates of the start point within the cell

required
end list

the coordinates of the end point within the cell

required

Returns:

Name Type Description
case int

the case to use to select variable values from a list

convert_decimal_days(decimal_days, mins=False)

Convert decimal days to more readable Days, Hours and (optionally) Minutes Args: decimal_days (float): Number of days as a decimal mins (bool): Determines whether to return minutes or decimal hours Returns: new_time (str): The time in the new format

extract_geojson_routes(mesh)

Extract routes in a precomputed mesh in GEOJSON format

Parameters:

Name Type Description Default
mesh dict

Precomputed mesh JSON with routes embedded

required

Returns:

Name Type Description
list

List of all routes found in mesh. If no routes found, returns empty list

fftind(size)

Creates a numpy array of shifted Fourier coordinates.

Parameters:

Name Type Description Default
size int

The size of the coordinate array to create

required

Returns:

Type Description

np.array: Numpy array of shifted Fourier coordinates (k_x, k_y). Has shape (2, size, size), with:

array[0,:,:] = k_x components

array[1,:,:] = k_y components

gaussian_random_field(size, alpha)

Creates a gaussian random field with normal (circular) distribution Code from https://github.com/bsciolla/gaussian-random-fields/blob/master/gaussian_random_fields.py

Parameters:

Name Type Description Default
size int

Default = 512; The number of datapoints created per axis in the GRF

required
alpha float

Default = 3.0; The power of the power-law momentum distribution

required

Returns:

Type Description

np.array: 2D Array of datapoints, shape (size, size)

gpx_route_import(f_name)

Function to import a route in gpx format and convert it to geojson format

Parameters:

Name Type Description Default
f_name

Filename of gpx route file

required

Returns:

Name Type Description
geojson

Route in geojson format

json_str(input_json)

Normalise input for classes and functions that take in json, return dict whether a dict or path is given as input. Args: input_json (dict or str):

Returns:

Type Description

output_json (dict)

pandas_dataframe_str(input_df)

Normalise input for classes and functions that take in pandas dataframes, return a df whether a df or path is given as input. Args: input_df (pd.DataFrame or str):

Returns:

Type Description

output_df (pd.DataFrame)

round_to_sigfig(x, sigfig=5)

Rounds numbers to some number of significant figures

Parameters:

Name Type Description Default
x float or array

Value(s) to round to sig figs

required
sigfig int

Number of significant figures desired

5

Returns:

Type Description

np.array: Values rounded to the desired number of significant figures

setup_logging(func, log_format='[%(asctime)-17s :%(levelname)-8s] - %(message)s')

Wraps a CLI endpoint and sets up logging for it

This is probably not the smoothest implementation, but it's an educational one for people who aren't aware of decorators and how they're implemented. In addition, it supports a nice pattern for CLI endpoints

TODO: start handling level configuration from logging yaml config

:param func: :param log_format: :return:

to_chart_track_csv(route)

Output a route in Chart Track csv format

unit_speed(val, unit)

Converting speed from m/s to the given units

Parameters:

Name Type Description Default
val float

input speed in m/s

required
unit str

the unit to convert to

required

Returns: val (float) - output speed in unit type 'unit'

unit_time(val, unit)

Converting time from seconds to the given units

Parameters:

Name Type Description Default
val float

input time in s

required
unit str

the unit to convert to

required

Returns: val (float) - output time in the converted units