PyTracerLab.model.io module

Reading tracer input time series and observations from text files.

The central function is read_series(). It reads simple delimited text files (e.g. CSV files exported from a spreadsheet) with one row per date and one column per tracer:

Date, 3H, 3H_sd
1963, 1450, 45
1985-06, 52.1, 1.6
1994.5, 18.3, 0.6

File format

  • The first column holds the date, every further column one tracer.

  • A header line with the column names is optional but recommended; it may start with #. Further lines starting with # are treated as comments.

  • Delimiter: comma, semicolon, tab or whitespace (detected automatically). With semicolon, tab or whitespace delimiters, a decimal comma is accepted as well.

  • Dates: YYYY, YYYY-MM, YYYY-MM-DD (also with /), DD.MM.YYYY, MM.YYYY or a decimal year (e.g. 1985.5). A time of day after the date (e.g. 1985-06-15 00:00:00) is ignored. A date given as a year only refers to 1 January of that year.

  • Missing values: empty cell, nan, NA, n/a or -.

  • A column named <tracer>_sd holds the measurement uncertainty (standard deviation, same unit as the tracer) of the column <tracer>.

Tracer input time series must have regular time steps without gaps (e.g. monthly or yearly). Observations can either be given for every time step of the input series (nan where nothing was observed) or only at the sampling dates; in the latter case they are assigned to the time step of the input series that contains the sampling date (use align_to).

exception PyTracerLab.model.io.DataFileError[source]

Bases: ValueError

A data file could not be read.

The message names the file, the line and the value that caused the problem, and what was expected instead.

class PyTracerLab.model.io.TracerSeries(times: numpy.ndarray, values: numpy.ndarray, tracers: List[str], uncertainty: numpy.ndarray | None = None, step: str | None = None)[source]

Bases: object

Time series of one or more tracers as returned by read_series().

Variables:
  • times (ndarray of datetime.datetime) – Dates of the time steps, shape (N,).

  • values (ndarray) – Tracer values, shape (N,) for one tracer or (N, K) for K tracers (the same convention as Model); nan where no value is available.

  • tracers (list of str) – Tracer names from the header line (or tracer_1, tracer_2, … if the file has no header).

  • uncertainty (ndarray or None) – Measurement uncertainties (standard deviations) from <tracer>_sd columns, same shape as values and nan where not given; None if the file has no uncertainty columns.

  • step (str or None) – Regular time step of the series, e.g. "1 month" or "1 year".

property n_tracers: int

Number of tracers (columns of values).

step: str | None = None
times: numpy.ndarray
tracers: List[str]
uncertainty: numpy.ndarray | None = None
values: numpy.ndarray
PyTracerLab.model.io.read_series(path: str | os.PathLike[str], align_to: PyTracerLab.model.io.TracerSeries | None = None) → PyTracerLab.model.io.TracerSeries[source]

Read a tracer input time series or observations from a text file.

See the module documentation (PyTracerLab.model.io) for the file format.

Parameters:
  • path (str or path-like) – File to read.

  • align_to (TracerSeries, optional) – Tracer input time series to which the observations in path are aligned. Each observation is assigned to the time step of align_to that contains its date, so the file may list only the sampling dates. The result has the same length as align_to and nan where nothing was observed, as required for the target_series of Model. Without align_to the file is read as it is and must have regular time steps (as required for tracer input time series).

Returns:

Dates, values, tracer names, optional uncertainties and time step.

Return type:

TracerSeries

Raises:

DataFileError – If the file cannot be read. The message names the line and the value that caused the problem.

Examples

>>> import PyTracerLab as lab
>>> inp = lab.read_series("tritium_input.csv")
>>> obs = lab.read_series("tritium_observations.csv", align_to=inp)
>>> m = lab.Model(dt=1.0, lambda_=lab.decay_constant("3H"),
...               input_series=inp.values, target_series=obs.values)