Core¶
Parameters¶
Parameter handling.
-
openscm.core.parameters.
HIERARCHY_SEPARATOR
= '|'¶ String used to define different levels in our data hierarchies
By default we follow pyam and use “|”. In such a case, emissions of CO2 for energy from coal would be “Emissions|CO2|Energy|Coal”.
-
class
openscm.core.parameters.
ParameterInfo
(parameter)¶ Bases:
object
Information for a parameter.
-
__init__
(parameter)¶ Initialize.
- Parameters
parameter (
_Parameter
) – Parameter
-
_parameter
= None¶ Parameter
-
ensure
()¶ Ensure that parameter is not empty.
- Raises
ParameterEmptyError – If parameter is empty
- Return type
None
-
property
parameter_type
¶ Parameter type
- Return type
-
-
class
openscm.core.parameters.
ParameterType
¶ Bases:
enum.Enum
Parameter type.
-
AVERAGE_TIMESERIES
= 2¶
-
GENERIC
= 4¶
-
POINT_TIMESERIES
= 3¶
-
SCALAR
= 1¶
-
from_timeseries_type
(timeseries_type) = <bound method ParameterType.from_timeseries_type of <enum 'ParameterType'>>¶
-
timeseries_type_to_string
(timeseries_type) = <bound method ParameterType.timeseries_type_to_string of <enum 'ParameterType'>>¶
-
-
class
openscm.core.parameters.
_Parameter
(name, region)¶ Bases:
object
Represents a parameter in the parameter hierarchy.
-
attempt_read
(parameter_type, unit=None, time_points=None)¶ Tell parameter that it will be read from. If the parameter has child parameters it will be read in in an aggregated way, i.e., aggregating over child parameters.
- Parameters
parameter_type (
ParameterType
) – Parameter type to be readunit (
Optional
[str
]) – Unit to be read; only for scalar and timeseries parameterstime_points (
Optional
[ndarray
]) – Timeseries time points; only for timeseries parameters
- Raises
ParameterTypeError – If parameter has already been read from or written to in a different type
ParameterAggregationError – If parameter has child parameters which cannot be aggregated (for boolean and string parameters)
- Return type
None
-
attempt_write
(parameter_type, unit=None, time_points=None)¶ Tell parameter that its data will be written to.
- Parameters
parameter_type (
ParameterType
) – Parameter type to be writtenunit (
Optional
[str
]) – Unit to be written; only for scalar and timeseries parameterstime_points (
Optional
[ndarray
]) – Timeseries time points; only for timeseries parameters
- Raises
ParameterReadonlyError – If parameter is read-only because it has child parameters
- Return type
None
-
children
= None¶ Child parameters
-
data
= None¶ Data
-
property
full_name
¶ Full hierarchical name
- Return type
Tuple
[str
, …]
-
get_or_create_child_parameter
(name)¶ Get a (direct) child parameter of this parameter. Create and add it if not found.
- Parameters
name (
str
) – Name- Returns
Parameter found or newly created
- Return type
- Raises
ParameterReadError – If the child parameter would need to be added, but this parameter has already been read from. In this case a child parameter cannot be added.
ParameterWrittenError – If the child parameter would need to be added, but this parameter has already been written to. In this case a child parameter cannot be added.
-
get_subparameter
(name)¶ Get a sub parameter of this parameter or
None
if not found.- Parameters
name (
Union
[str
,Sequence
[str
]]) – Hierarchical name of the subparameter below this parameter or()
for this parameter- Returns
Parameter of
None
if not found- Return type
Optional[_Parameter]
-
has_been_read_from
= None¶ If True, parameter has already been read from
-
has_been_written_to
= None¶ If True, parameter data has already been changed
-
name
= None¶ Name
-
parameter_type
= None¶ Parameter type
-
parent
= None¶ Parent parameter
-
region
= None¶ Region this parameter belongs to
-
time_points
= None¶ Timeseries time points; only for timeseries parameters
-
unit
= None¶ Unit
-
version
= None¶ Internal version (incremented by each write operation)
-
Parameter views¶
Parameter views provide ways to read and write parameter data with a defined unit and time information.
-
class
openscm.core.views.
GenericView
(parameter)¶ Bases:
openscm.core.parameters.ParameterInfo
View of a generic parameter.
-
__init__
(parameter)¶ Initialize.
- Parameters
parameter (
_Parameter
) – Parameter to handle
-
_writable
= None¶ Is this view writable? Is set to
True
once this view is written to.
-
ensure
()¶ Ensure that parameter is not empty.
- Raises
ParameterEmptyError – If parameter is empty
- Return type
None
-
property
parameter_type
¶ Parameter type
- Return type
-
property
region
¶ Hierarchichal name of the region this parameter belongs to
- Return type
Tuple
[str
, …]
-
property
value
¶ Value of generic parameter
- Raises
ParameterEmptyError – Parameter is empty, i.e. has not yet been written to
ParameterReadonlyError – Parameter is read-only (e.g. because its parent has been written to)
- Return type
-
-
class
openscm.core.views.
ScalarView
(parameter, unit)¶ Bases:
openscm.core.parameters.ParameterInfo
View of a scalar parameter.
-
__init__
(parameter, unit)¶ Initialize.
- Parameters
parameter (
_Parameter
) – Parameter to handleunit (
str
) – Unit for the values in the view
-
_child_data_views
= None¶ List of views to the child parameters for aggregated reads
-
_unit_converter
= None¶ Unit converter
-
_writable
= None¶ Is this view writable? Is set to
True
once this view is written to.
-
ensure
()¶ Ensure that parameter is not empty.
- Raises
ParameterEmptyError – If parameter is empty
- Return type
None
-
property
parameter_type
¶ Parameter type
- Return type
-
property
region
¶ Hierarchichal name of the region this parameter belongs to
- Return type
Tuple
[str
, …]
-
property
value
¶ Value of scalar parameter
If the parameter has child parameters, the returned value will be the sum of the values of all of the child parameters.
- Raises
ParameterEmptyError – Parameter is empty, i.e. has not yet been written to
ParameterReadonlyError – Parameter is read-only (e.g. because its parent has been written to)
- Return type
-
-
class
openscm.core.views.
TimeseriesView
(parameter, unit, time_points, timeseries_type, interpolation_type, extrapolation_type)¶ Bases:
openscm.core.parameters.ParameterInfo
View of a timeseries.
-
__init__
(parameter, unit, time_points, timeseries_type, interpolation_type, extrapolation_type)¶ Initialize.
- Parameters
parameter (
_Parameter
) – Parameterunit (
str
) – Unit for the values in the viewtime_points (
ndarray
) – Timeseries time pointstimeseries_type (
ParameterType
) – Time series typeinterpolation_type (
InterpolationType
) – Interpolation typeextrapolation_type (
ExtrapolationType
) – Extrapolation type
-
_check_write
()¶
-
_child_data_views
= None¶ List of views to the child parameters for aggregated reads
-
_data
= None¶ Chache for underlying data
-
_locked
= None¶ Is this view locked (i.e., does it not update the underlying parameter on every write)?
-
_read
()¶
-
_timeseries
= None¶ Time series handler
-
_timeseries_converter
= None¶ Timeseries converter
-
_unit_converter
= None¶ Unit converter
-
_version
= None¶ Version of cache
-
_writable
= None¶ Is this view writable? Is set to
True
once this view is written to.
-
_write
()¶
-
ensure
()¶ Ensure that parameter is not empty.
- Raises
ParameterEmptyError – If parameter is empty
- Return type
None
-
lock
()¶ Lock this view (i.e., do not update the underlying parameter on every write).
- Return type
None
-
property
parameter_type
¶ Parameter type
- Return type
-
property
region
¶ Hierarchichal name of the region this parameter belongs to
- Return type
Tuple
[str
, …]
-
unlock
()¶ Unlock this view (i.e., update the underlying parameter on every write).
Updates the underlying parameter.
- Return type
None
-
property
values
¶ Values of the full timeseries
If the parameter has child parameters, the returned value will be the sum of the values of all of the child parameters.
- Raises
ParameterEmptyError – Parameter is empty, i.e. has not yet been written to
ParameterReadonlyError – Parameter is read-only (e.g. because its parent has been written to)
TimeseriesPointsValuesMismatchError – Lengths of set value and the time points number mismatch
- Return type
-
-
class
openscm.core.views.
_Timeseries
(input_array, parameter_view)¶ Bases:
pandas.core.arrays.base.ExtensionOpsMixin
,numpy.lib.mixins.NDArrayOperatorsMixin
Internal class which wraps numpy to make sure data is buffered and up-to-date
-
_HANDLED_TYPES
= (<class 'numpy.ndarray'>, <class 'numbers.Number'>)¶
-
__init__
(input_array, parameter_view)¶ Initialize.
- Parameters
input_array (
ndarray
) – Array to handleparameter_view (
TimeseriesView
) –TimeseriesView
this timeseries belongs to
-
classmethod
_add_arithmetic_ops
()¶
-
classmethod
_add_comparison_ops
()¶
-
classmethod
_create_arithmetic_method
(op)¶
-
classmethod
_create_comparison_method
(op)¶
-
property
nbytes
¶ Bytes block of the unterlying timeseries
-
property
ndim
¶ Dimension of the timeseries (
==1
)
-
Regions¶
Handling of region information.
-
class
openscm.core.regions.
_Region
(name)¶ Bases:
object
Represents a region in the region hierarchy.
-
_children
= None¶ Subregions
-
_has_been_aggregated
= None¶ If
True
, a parameter of this region has already been read in an aggregated way, i.e., aggregating over subregions
-
_name
= None¶ Name
-
_parameters
= None¶ Parameters
-
_parent
= None¶ Parent region (or
None
if root region)
-
attempt_aggregate
()¶ Tell region that one of its parameters will be read from in an aggregated way, i.e., aggregating over subregions.
- Return type
None
-
get_or_create_parameter
(name)¶ Get a root parameter for this region. Create and add it if not found.
- Parameters
name (
str
) – Name- Returns
Root parameter found or newly created
- Return type
-
get_or_create_subregion
(name)¶ Get a (direct) subregion of this region. Create and add it if not found.
- Parameters
name (
str
) – Name- Returns
Region found or newly created
- Return type
- Raises
RegionAggregatedError – If the subregion would need to be added and a parameter of this region has already been read in an aggregated way. In this case a subregion cannot be created.
-
get_parameter
(name)¶ Get a (root or sub-) parameter for this region or
None
if not found.- Parameters
name (
Union
[str
,Sequence
[str
]]) – Hierarchical name of the parameter- Returns
Parameter of
None
if not found- Return type
Optional[parameters._Parameter]
- Raises
ValueError – Name not given
-
get_subregion
(name)¶ Get a subregion of this region or
None
if not found.
-
Time¶
Different climate models often use different time frames for their input and output data. This includes different ‘meanings’ of time steps (e.g. beginning vs middle of year) and different lengths of the time steps (e.g. years vs months). Accordingly, OpenSCM supports the conversion of timeseries data between such timeseries, which is handled in this module. A thorough explaination of the procedure used is given in a dedicated Jupyter Notebook.
-
class
openscm.core.time.
ExtrapolationType
¶ Bases:
enum.Enum
Extrapolation type.
-
CONSTANT
= 0¶
-
LINEAR
= 1¶
-
NONE
= -1¶
-
from_extrapolation_type
(extrapolation_type) = <bound method ExtrapolationType.from_extrapolation_type of <enum 'ExtrapolationType'>>¶
-
-
class
openscm.core.time.
InterpolationType
¶ Bases:
enum.Enum
Interpolation type.
-
LINEAR
= 1¶
-
from_interpolation_type
(interpolation_type) = <bound method InterpolationType.from_interpolation_type of <enum 'InterpolationType'>>¶
-
-
class
openscm.core.time.
TimePoints
(values)¶ Bases:
object
Handles time points by wrapping
np.ndarray
ofnp.datetime64
..-
_values
= None¶ Actual time points array
-
months
()¶ Get month of each time point.
- Returns
Month of each time point
- Return type
np.array
ofint
-
to_index
()¶ Get time points as
pd.Index
.- Returns
pd.Index
ofnp.dtype
object
with name"time"
made from the time points represented asdatetime.datetime
.- Return type
pd.Index
-
-
class
openscm.core.time.
TimeseriesConverter
(source_time_points, target_time_points, timeseries_type, interpolation_type, extrapolation_type)¶ Bases:
object
Converts timeseries and their points between two timeseriess (each defined by a time of the first point and a period length).
-
__init__
(source_time_points, target_time_points, timeseries_type, interpolation_type, extrapolation_type)¶ Initialize.
- Parameters
source_time_points (
ndarray
) – Source timeseries time pointstarget_time_points (
ndarray
) – Target timeseries time pointstimeseries_type (
ParameterType
) – Time series typeinterpolation_type (
InterpolationType
) – Interpolation typeextrapolation_type (
ExtrapolationType
) – Extrapolation type
- Raises
InsufficientDataError – Timeseries too short to extrapolate
-
_calc_continuous_representation
(time_points, values)¶ Calculate a “continuous” representation of a timeseries (see
openscm.timeseries_converter._calc_integral_preserving_linear_interpolation()
) with the time pointstime_points
and valuesvalues
.- Parameters
- Returns
Function that represents the interpolated timeseries. It takes a single argument, time (“x-value”), and returns a single float, the value of the interpolated timeseries at that point in time (“y-value”).
- Return type
-
_convert
(values, source_time_points, target_time_points)¶ Wrap
_convert_unsafe()
to provide proper error handling._convert_unsafe()
converts time period average timeseries datavalues
for timeseries time pointssource_time_points
to the time pointstarget_time_points
.- Parameters
- Raises
InsufficientDataError – Length of the time series is too short to convert
InsufficientDataError – Target time points are outside the source time points and
_extrapolation_type
isExtrapolationType.NONE
- Returns
Converted time period average data for timeseries
values
- Return type
np.ndarray
-
_convert_unsafe
(values, source_time_points, target_time_points)¶ Convert time period average timeseries data
values
for timeseries time pointssource_time_points
to the time pointstarget_time_points
.- Parameters
- Raises
NotImplementedError – The timeseries type is not recognised
- Returns
Converted time period average data for timeseries
values
- Return type
np.ndarray
-
_extrapolation_type
= None¶ Extrapolation type
-
_interpolation_type
= None¶ Interpolation type
-
_source
= None¶ Source timeseries time points
-
_target
= None¶ Target timeseries time points
-
_timeseries_type
= None¶ Time series type
-
convert_from
(values)¶ Convert value from source timeseries time points to target timeseries time points.
- Parameters
values (
ndarray
) – Value- Returns
Converted array
- Return type
np.ndarray
-
-
openscm.core.time.
_calc_integral_preserving_linear_interpolation
(values)¶ Calculate the “linearization” values of the array
values
which is assumed to represent averages over time periods. Values at the edges of the periods are taken as the average of adjacent periods, values at the period middles are taken such that the integral over a period is the same as for the input data.- Parameters
values (
ndarray
) – Timeseries values of period averages- Returns
Values of linearization (of length
2 * len(values) + 1
)- Return type
np.ndarray
-
openscm.core.time.
_calc_interval_averages
(continuous_representation, target_intervals)¶ Calculate the interval averages of a continuous function.
Here interval average is calculated as the integral over the period divided by the period length.
- Parameters
continuous_representation (
Callable
[[float
],float
]) – Continuous function from which to calculate the interval averages. Should be calculated usingopenscm.timeseries_converter.TimeseriesConverter._calc_continuous_representation()
.target_intervals (
ndarray
) – Intervals to calculate the average of.
- Returns
Array of the interval/period averages
- Return type
np.ndarray
-
openscm.core.time.
_float_year_to_datetime
(inp)¶ - Return type
datetime64
-
openscm.core.time.
_format_datetime
(dts)¶ Convert an array to an array of
np.datetime64
.- Parameters
dts (
ndarray
) – Input to attempt to convert- Returns
Converted array
- Return type
np.ndarray
ofnp.datetime64
- Raises
ValueError – If one of the values in
dts
cannot be converted tonp.datetime64
-
openscm.core.time.
create_time_points
(start_time, period_length, points_num, timeseries_type)¶ Create time points for an equi-distant time series.
- Parameters
start_time (
datetime64
) – First time point of the timeseriesperiod_length (
timedelta64
) – Period lengthpoints_num (
int
) – Length of timeseriestimeseries_type (
Union
[ParameterType
,str
]) – Timeseries type
- Returns
Array of the timeseries time points
- Return type
np.ndarray
ofnp.datetime64