Hydra utility library

Utility functions

HydraLib.util.arr_to_vector(arr)

Reshape a multidimensional array to a vector.

HydraLib.util.array_dim(arr)

Return the size of a multidimansional array.

HydraLib.util.check_array_struct(array)

Check to ensure arrays are symmetrical, for example: [[1, 2, 3], [1, 2]] is invalid

HydraLib.util.get_restriction_as_dict(restriction_xml)

turn:

<restrictions>
    <restriction>
        <type>MAXLEN</type>
        <value>3</value>
    </restriction>
    <restriction>
        <type>VALUERANGE</type>
        <value><item>1</item><item>10</item></value>
    </restriction>
</restrictions>

into:

{
    'MAXLEN' : 3,
    'VALUERANGE' : [1, 10]
}
HydraLib.util.validate_BOOLYN(in_value, restriction)

Restriction is not used here. It is just present to be in line with all the other validation functions

HydraLib.util.validate_DATERANGE(value, restriction)

Test to ensure that the times in a timeseries fall between a lower and upper bound Parameters: A timeseries in the form [(datetime, val), (datetime, val)..] and a tuple containing the lower and upper bound as datetime objects.

HydraLib.util.validate_DECREASING(in_value, restriction)

Test to ensure the values in a list are decreasing. Parameters: a list of values and None. The none is there simply to conform with the rest of the validation routines.

HydraLib.util.validate_ENUM(in_value, restriction)

Test to ensure that the given value is contained in the provided list. the value parameter must be either a single value or a 1-dimensional list. All the values in this list must satisfy the ENUM

HydraLib.util.validate_EQUALTIMESTEPS(value, restriction)

Ensure that the timesteps in a timeseries are equal. If a restriction is provided, they must be equal to the specified restriction.

Value is a pandas dataframe.

HydraLib.util.validate_EQUALTO(in_value, restriction)

Test to ensure that a value is equal to a prescribed value. Parameter: Two values, which will be compared for equality.

HydraLib.util.validate_GREATERTHAN(in_value, restriction)

Test to ensure that a value is greater than a prescribed value. Parameter: Two values, which will be compared for the difference..

HydraLib.util.validate_GREATERTHANEQ(value, restriction)

Test to ensure that a value is greater than or equal to a prescribed value. Parameter: Two values, which will be compared for the difference..

HydraLib.util.validate_INCREASING(in_value, restriction)

Test to ensure the values in a list are increasing. Parameters: a list of values and None. The none is there simply to conform with the rest of the validation routines.

HydraLib.util.validate_ISNULL(value, restriction)

Restriction is not used here. It is just present to be in line with all the other validation functions

HydraLib.util.validate_LESSTHAN(in_value, restriction)

Test to ensure that a value is less than a prescribed value. Parameter: Two values, which will be compared for the difference..

HydraLib.util.validate_LESSTHANEQ(value, restriction)

Test to ensure that a value is less than or equal to a prescribed value. Parameter: Two values, which will be compared for the difference..

HydraLib.util.validate_MAXLEN(value, restriction)

Test to ensure that a list has the prescribed length. Parameters: A list and an integer, which defines the required length of the list.

HydraLib.util.validate_MULTIPLEOF(in_value, restriction)

Test to ensure that a value is a multiple of a specified restriction value. Parameters: Numeric value and an integer

HydraLib.util.validate_NOTEQUALTO(in_value, restriction)

Test to ensure that a value is NOT equal to a prescribed value. Parameter: Two values, which will be compared for non-equality.

HydraLib.util.validate_NOTNULL(value, restriction)

Restriction is not used here. It is just present to be in line with all the other validation functions

HydraLib.util.validate_NUMPLACES(in_value, restriction)

the value parameter must be either a single value or a 1-dimensional list. All the values in this list must satisfy the condition

HydraLib.util.validate_SUMTO(in_value, restriction)

Test to ensure the values of a list sum to a specified value: Parameters: a list of numeric values and a target to which the values in the list must sum

HydraLib.util.validate_VALUERANGE(in_value, restriction)

Test to ensure that a value sits between a lower and upper bound. Parameters: A Decimal value and a tuple, containing a lower and upper bound, both as Decimal values.

HydraLib.util.vector_to_arr(vec, dim)

Reshape a vector to a multidimensional array with dimensions ‘dim’.

Plug-in development

Units and Dimensions

This module provides facilities for unit conversion and consistency checking between units and dimensions.

class HydraLib.units.Units

This class provides functionality for unit conversion and checking of consistency between units and dimensions. Unit conversion factors are defined in a static built-in XML file and in a custom file defined by the user. The location of the unit conversion file provided by the user is specified in the config file in section [unit conversion]. This section and a file specifying custom unit conversion factors are optional.

add_dimension(dimension)

Add a dimension to the custom xml file as listed in the config file.

add_unit(dimension, unit)

Add a unit and conversion factor to a specific dimension. The new unit will be written to the custom XML-file.

check_consistency(unit, dimension)

Check whether a specified unit is consistent with the physical dimension asked for by the attribute or the dataset.

convert(values, unit1, unit2)

Convert a value from one unit to another one. The two units must represent the same physical dimension.

delete_dimension(dimension)

Delete a dimension from the custom XML file.

delete_unit(unit)

Delete a unit from the custom file.

get_dimension(unit)

Return the physical dimension a given unit refers to.

get_dimensions()

Get a list of all dimenstions listed in one of the xml files.

get_units(dimension)

Get a list of all units describing one specific dimension.

parse_unit(unit)

Helper function that extracts constant factors from unit specifications. This allows to specify units similar to this: 10^6 m^3.

save_user_file()

Save units or dimensions added to the server to the custom XML file.

update_unit(dimension, unit)

Update a unit in the custom file. Please note that units in the built-in file can not be updated.

HydraLib.units.validate_resource_attributes(resource, attributes, template, check_unit=True, exact_match=False)

Validate that the resource provided matches the template. Only passes if the resource contains ONLY the attributes specified in the template.

The template should take the form of a dictionary, as should the resources.

check_unit: Make sure that if a unit is specified in the template, it
is the same in the data
exact_match: Enure that all the attributes in the template are in
the data also. By default this is false, meaning a subset of the template attributes may be specified in the data. An attribute specified in the data must be defined in the template.

@returns a list of error messages. An empty list indicates no errors were found.

Logging

Error classes