improver.cli package

Submodules

Module contents

init for cli and clize

class DocutilizeClizeHelp(subject, owner, builder=<bound method HelpForAutodetectedDocstring.from_subject of <class 'improver.cli.HelpForNapoleonDocstring'>>)[source]

Bases: HelpCli

Subclass to build Napoleon docstring from subject.

class HelpForNapoleonDocstring(*args, **kwargs)[source]

Bases: HelpForAutodetectedDocstring

Subclass to add support for google style docstrings

add_docstring(docstring, *args, **kwargs)[source]

Adds the updated docstring.

class ObjectAsStr(obj, name=None)[source]

Bases: str

Hide object under a string to pass it through Clize parser.

static obj_to_name(obj, cls=None)[source]

Helper function to create the string.

original_object
class TimeIt(verbose=False)[source]

Bases: object

property elapsed

Return elapsed time in seconds.

_cli_items()[source]

Dynamically discover CLIs.

clizefy(obj=None, helper_class=<class 'improver.cli.DocutilizeClizeHelp'>, **kwargs)[source]

Decorator for creating CLI objects.

comma_separated_list(to_convert)[source]

Converts comma separated string to list or returns passed object.

Parameters:

to_convert (string or list) – comma separated string or list

Returns:

list

comma_separated_list_of_float(to_convert)[source]

Converts comma separated string to list of floats or returns passed object.

Parameters:

to_convert (string or list) – comma separated string or list

Returns:

list

command_executor(*argv, verbose=False, dry_run=False)[source]

Common entry point for straight command execution.

create_constrained_inputcubelist_converter(*constraints)[source]

Makes function that the input constraints are used in a loop.

The function is a @value_converter, this means it is used by clize to convert strings into objects. This is a way of not using the IMPROVER load_cube which will try to merge cubes. Iris load on the other hand won’t deal with meta data properly. So an example is if you wanted to load an X cube and a Y cube from a cubelist of 2. You call this function with a list of constraints. These cubes get loaded and returned as a CubeList.

Parameters:

*constraints (tuple of str or callable or iris.Constraint) – Constraints to be used in extracting the required cubes. Each constraint must match exactly one cube and extracted cubes will be sorted to match their order. A constraint can be an iris.Constraint object or a callable or cube name that can be used to construct one.

Returns:

A function with the constraints used for a list comprehension.

Return type:

callable

docutilize(obj)[source]

Convert Numpy or Google style docstring into reStructuredText format.

Parameters:

obj (str or object) – Takes an object and changes it’s docstrings to a reStructuredText format.

Returns:

A converted string or an object with replaced docstring depending on the type of the input.

Return type:

str or object

execute_command(dispatcher, prog_name, *args, verbose=False, dry_run=False)[source]

Common entry point for command execution.

improver_help(prog_name, command=None, *, usage=False)[source]

Show command help.

inputcube(to_convert)[source]

Loads cube from file or returns passed object.

Parameters:

to_convert (string or iris.cube.Cube) – File name or Cube object.

Returns:

Loaded cube or passed object.

inputcube_nolazy(to_convert)[source]

Loads cube from file or returns passed object. Where a load is performed, it will not have lazy data. :type to_convert: :param to_convert: File name or Cube object. :type to_convert: string or iris.cube.Cube

Returns:

Loaded cube or passed object.

inputcubelist(to_convert)[source]

Loads a cubelist from file or returns passed object. :type to_convert: :param to_convert: File name or CubeList object. :type to_convert: string or iris.cube.CubeList

Returns:

Loaded cubelist or passed object.

inputdatetime(to_convert)[source]

Converts string to datetime or returns passed object.

Parameters:

to_convert (string or datetime) – datetime represented as string of the format YYYYMMDDTHHMMZ

Returns:

datetime object

Return type:

(datetime)

inputjson(to_convert)[source]

Loads json from file or returns passed object.

Parameters:

to_convert (string or dict) – File name or json dictionary.

Returns:

Loaded json dictionary or passed object.

inputpath(to_convert)[source]

Converts string paths to pathlib Path objects

Parameters:

to_convert (string or pathlib.Path) – path represented as string

Returns:

Path object

Return type:

(pathlib.Path)

main(prog_name, command, *args, profile=None, memprofile=None, verbose=False, dry_run=False)[source]

IMPROVER NWP post-processing toolbox

Results from commands can be passed into file-like arguments of other commands by surrounding them by square brackets:

improver command [ command ... ] ...

Spaces around brackets are mandatory.

Parameters:
  • prog_name (pass_name) – The program name from argv[0].

  • command (str) – Command to execute

  • args (tuple) – Command arguments

  • profile (str) – If given, will write profiling to the file given. To write to stdout, use a hyphen (-)

  • memprofile (str) – Creates 2 files by adding a suffix to the provided arguemnt - a tracemalloc snapshot at the point of highest memory consumption of your program (suffixed with _SNAPSHOT) and a track of the maximum memory used by your program over time (suffixed with _MAX_TRACKER).

  • verbose (bool) – Print executed commands

  • dry_run (bool) – Print commands to be executed

See improver help [–usage] [command] for more information on available command(s).

maybe_coerce_with(converter, obj, **kwargs)[source]

Apply converter if str, pass through otherwise.

run_main(argv=None)[source]

Overrides argv[0] to be ‘improver’ then runs main.

Parameters:

argv (list of str) – Arguments that were from the command line.

unbracket(args)[source]

Convert input list with bracketed items into nested lists.

>>> unbracket('foo [ bar a b ] [ baz c ] -o z'.split())
['foo', ['bar', 'a', 'b'], ['baz', 'c'], '-o', 'z']
with_output(wrapped, *args, output=None, pass_through_output=False, compression_level=1, least_significant_digit=None, **kwargs)[source]

Add output keyword only argument. Add compression_level option. Add least_significant_digit option.

This is used to add extra output, compression_level and least_significant_digit CLI options. If output is provided, it saves the result of calling wrapped to file and returns None, otherwise it returns the result. If compression_level is provided, it compresses the data with the provided compression level (or not, if compression_level 0). If least_significant_digit provided, it will quantize the data to a certain number of significant figures.

Parameters:
  • wrapped (obj) – The function to be wrapped.

  • output (str, optional) – Output file name. If not supplied, the output object will be printed instead.

  • pass_through_output (bool) – Pass through the output object even if saved to file. Used in pipelines of commands if intermediate output needs to be saved.

  • compression_level (int) – Will set the compression level (1 to 9), or disable compression (0).

  • least_significant_digit (int) – If specified will truncate the data to a precision given by 10**(-least_significant_digit), e.g. if least_significant_digit=2, then the data will be quantized to a precision of 0.01 (10**(-2)). See http://www.esrl.noaa.gov/psd/data/gridded/conventions/cdc_netcdf_standard.shtml for details. When used with compression level, this will result in lossy compression.

Returns:

Result of calling wrapped or None if output is given.