smif.convert package

Submodules

smif.convert.area module

Handles conversion between the sets of regions used in the SosModel

class smif.convert.area.NamedShape(name, shape)

Bases: tuple

name

Alias for field number 0

shape

Alias for field number 1

class smif.convert.area.RegionRegister[source]

Bases: smif.convert.register.Register

Holds the sets of regions used by the SectorModels and provides conversion between data values relating to compatible sets of regions.

convert(data, from_set_name, to_set_name)[source]

Convert a list of data points for a given set of regions to another set of regions.

Parameters:
  • data (numpy.ndarray with dimension regions) –
  • from_set_name (str) –
  • to_set_name (str) –
get_entry(name)[source]

Returns the ResolutionSet of name

Parameters:name (str) – The unique identifier of a ResolutionSet in the register
Returns:
Return type:smif.convert.area.RegionSet
names

Names of registered region sets

Returns:sets
Return type:list of str
register(region_set)[source]

Register a set of regions as a source/target for conversion

class smif.convert.area.RegionSet(set_name, fiona_shape_iter)[source]

Bases: smif.convert.register.ResolutionSet

Hold a set of regions, spatially indexed for ease of lookup when constructing conversion matrices.

Parameters:
  • set_name (str) – Name to use as identifier for this set of regions
  • fiona_shape_iter (iterable) – Iterable (probably a list or a reader handle) of fiona feature records e.g. the ‘features’ entry of a GeoJSON collection
data
get_entry_names()[source]
intersection(bounds)[source]

Return the subset of regions intersecting with a bounding box

name
smif.convert.area.get_register()[source]

Return single copy of RegionRegister

smif.convert.area.proportion_of_a_intersecting_b(shape_a, shape_b)[source]

Calculate the proportion of shape a that intersects with shape b

smif.convert.interval module

Handles conversion between the set of time intervals used in the SosModel

There are three main classes, which are currently rather intertwined. Interval represents an individual definition of a period within a year. This is specified using the ISO8601 period syntax and exposes methods which use the isodate library to parse this into an internal hourly representation of the period.

TimeIntervalRegister holds the definitions of time-interval sets specified for the sector models at the SosModel level. This class exposes one public method, add_interval_set() which allows the SosModel to add an interval definition from a model configuration to the register.

Quantities

Quantities are associated with a duration, period or interval. For example 120 GWh of electricity generated during each week of February.:

Week 1: 120 GW
Week 2: 120 GW
Week 3: 120 GW
Week 4: 120 GW

Other examples of quantities:

  • greenhouse gas emissions
  • demands for infrastructure services
  • materials use
  • counts of cars past a junction
  • costs of investments, operation and maintenance

Upscale: Divide

To convert to a higher temporal resolution, the values need to be apportioned across the new time scale. In the above example, the 120 GWh of electricity would be divided over the days of February to produce a daily time series of generation. For example:

1st Feb: 17 GWh
2nd Feb: 17 GWh
3rd Feb: 17 GWh
...

Downscale: Sum

To resample weekly values to a lower temporal resolution, the values would need to be accumulated. A monthly total would be:

Feb: 480 GWh

Remapping

Remapping quantities, as is required in the conversion from energy demand (hourly values over a year) to energy supply (hourly values for one week for each of four seasons) requires additional averaging operations. The quantities are averaged over the many-to-one relationship of hours to time-slices, so that the seasonal-hourly timeslices in the model approximate the hourly profiles found across the particular seasons in the year. For example:

hour 1: 20 GWh
hour 2: 15 GWh
hour 3: 10 GWh
...
hour 8592: 16 GWh
hour 8593: 12 GWh
hour 8594: 21 GWh
...
hour 8760: 43 GWh

To:

season 1 hour 1: 20+16+.../4 GWh # Denominator number hours in sample
season 1 hour 2: 15+12+.../4 GWh
season 1 hour 3: 10+21+.../4 GWh
...

Prices

Unlike quantities, prices are associated with a point in time. For example a spot price of £870/GWh. An average price can be associated with a duration, but even then, we are just assigning a price to any point in time within a range of times.

Upscale: Fill

Given a timeseries of monthly spot prices, converting these to a daily price can be done by a fill operation. E.g. copying the monthly price to each day.

From:

Feb: £870/GWh

To:

1st Feb: £870/GWh
2nd Feb: £870/GWh
...

Downscale: Average

On the other hand, going down scale, such as from daily prices to a monthly price requires use of an averaging function. From:

1st Feb: £870/GWh
2nd Feb: £870/GWh
...

To:

Feb: £870/GWh

Development Notes

  • We could use numpy.convolve() to compare time intervals as hourly arrays before adding them to the set of intervals
class smif.convert.interval.Interval(name, list_of_intervals, base_year=2010)[source]

Bases: object

A time interval

Parameters:
  • id (str) – The unique name of the Interval
  • list_of_intervals (str) – A list of tuples of valid ISO8601 duration definition string denoting the time elapsed from the beginning of the year to the (beginning, end) of the interval
  • base_year (int, default=2010) – The reference year used for conversion to a datetime tuple

Example

>>> a = Interval('id', ('PT0H', 'PT1H'))
>>> a.interval = ('PT1H', 'PT2H')
>>> repr(a)
"Interval('id', [('PT0H', 'PT1H'), ('PT1H', 'PT2H')], base_year=2010)"
>>> str(a)
"Interval 'id' starts at hour 0 and ends at hour 1"
baseyear

The reference year

end

The end hour of the interval(s)

Returns:An integer or list of integers, representing the hour from the beginning of the year associated with the end of each of the intervals
Return type:int or list
interval

The list of intervals

Setter appends a tuple or list of intervals to the list of intervals

name

The name (or id) of the interval(s)

Returns:Name or ID
Return type:str
start

The start hour of the interval(s)

Returns:A list of integers, representing the hour from the beginning of the year associated with the start of each of the intervals
Return type:list
to_hourly_array()[source]

Converts a list of intervals to a boolean array of hours

to_hours()[source]

Return a list of tuples of the intervals in terms of hours

Returns:A list of tuples of the start and end hours of the year of the interval
Return type:list
class smif.convert.interval.IntervalSet(name, data, base_year=2010)[source]

Bases: smif.convert.register.ResolutionSet

A collection of intervals

Parameters:
  • name (str) – A unique identifier for the set of time intervals
  • data (list) – Time intervals required as a list of dicts, with required keys start, end and name
data
get_entry_names()[source]

Returns the names of the intervals

name
class smif.convert.interval.TimeIntervalRegister[source]

Bases: smif.convert.register.Register

Holds the set of time-intervals used by the SectorModels

convert(data, from_interval_set_name, to_interval_set_name)[source]

Convert some data to a time_interval type

Parameters:
  • data (numpy.ndarray) – The timeseries to convert from from_interval to to_interval
  • from_interval_set_name (str) – The unique identifier of a interval type which matches the timeseries
  • to_interval_set_name (str) – The unique identifier of a registered interval type
Returns:

An array of the resampled timeseries values.

Return type:

numpy.ndarray

get_entry(name)[source]

Returns the ResolutionSet of name

Parameters:name (str) – The unique identifier of a ResolutionSet in the register
Returns:
Return type:smif.convert.interval.IntervalSet
names

A list of the interval set names contained in the register

Returns:
Return type:list
register(interval_set)[source]

Add a time-interval definition to the set of intervals types

Detects duplicate references to the same annual-hours by performing a convolution of the two one-dimensional arrays of time-intervals.

Parameters:interval_set (smif.convert.interval.IntervalSet) – A collection of intervals
smif.convert.interval.get_register()[source]

Return single copy of TimeIntervalRegister

smif.convert.register module

Register and ResolutionSet abstract classes to contain area and interval metadata.

class smif.convert.register.Register[source]

Bases: object

get_entry(name)[source]

Implement to return the smif.convert.ResolutionSet associated with the name

Parameters:name (str) – The unique identifier of the ResolutionSet
register(resolution_set)[source]
class smif.convert.register.ResolutionSet[source]

Bases: object

data
get_entry_names()[source]

Get the names of the entries in the ResolutionSet

Returns:The set of names which identify each entry in the ResolutionSet
Return type:set
name

smif.convert.unit module

Handles conversion between units used in the SosModel

First implementation delegates to pint.

smif.convert.unit.parse_unit(unit_string)[source]

Parse a unit string (abbreviation or full) into a Unit object

Parameters:unit (str) –
Returns:quantity
Return type:pint.Unit

Module contents

In this module, we implement the conversion across space and time

The SpaceTimeConvertor is instantiated with data to convert, and the names of the four source and destination spatio-temporal resolutions.

The convert() method returns a new numpy.ndarray for passing to a sector model.

class smif.convert.SpaceTimeConvertor[source]

Bases: object

Handles the conversion of time and space for a list of values

Parameters:

Notes

Future development requires using a data object which allows multiple views upon the values across the three dimensions of time, space and units. This will then allow more efficient conversion across any one of these dimensions while holding the others constant. One option could be collections.ChainMap.

convert(data, from_spatial, to_spatial, from_temporal, to_temporal)[source]

Convert the data from set of regions and intervals to another

Parameters:
  • data (numpy.ndarray) – An array of values with dimensions regions x intervals
  • from_spatial (str) – The name of the spatial resolution of the data
  • to_spatial (str) – The name of the required spatial resolution
  • from_temporal (str) – The name of the temporal resolution of the data
  • to_temporal (str) – The name of the required temporal resolution
Returns:

An array of data with dimensions regions x intervals

Return type:

numpy.ndarray