Timeseries .tim files¶
.tim files contain time series data of a D-Flow FM model. The support of .tim files for boundary time series will be discontinued. Instead, these will be replaced by the *.bc file.
They are represented by the classes below.
Model¶
TimModel (ParsableFileModel)
pydantic-model
¶
Class representing a tim (*.tim) file.
Parser¶
TimParser
¶
A parser for .tim files.
Full line comments at the start of the file are supported. Comment lines start with either a *
or a #
.
No other comments are supported.
parse(filepath: Path) -> Dict[str, List[Any]]
staticmethod
¶
Parse a .tim file into a dictionary with comments and time series data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Path |
Path to the .tim file to be parsed. |
required |
Returns:
Type | Description |
---|---|
Dict[str, List[Any]] |
A dictionary with keys "comments" and "timeseries". - "comments" represents comments found at the start of the file. - "timeseries" is a list of dictionaries with the key as "time" and values as "data". - "time" is a time as a string. - "data" is data as a list of strings. |
Exceptions:
Type | Description |
---|---|
ValueError |
If the file contains a comment that is not at the start of the file. |
ValueError |
If the data of the timeseries is empty. |
Source code in hydrolib/core/dflowfm/tim/parser.py
@staticmethod
def parse(filepath: Path) -> Dict[str, List[Any]]:
"""Parse a .tim file into a dictionary with comments and time series data.
Args:
filepath (Path): Path to the .tim file to be parsed.
Returns:
Dict[str, List[Any]]: A dictionary with keys "comments" and "timeseries".
- "comments" represents comments found at the start of the file.
- "timeseries" is a list of dictionaries with the key as "time" and values as "data".
- "time" is a time as a string.
- "data" is data as a list of strings.
Raises:
ValueError: If the file contains a comment that is not at the start of the file.
ValueError: If the data of the timeseries is empty.
"""
comments: List[str] = []
timeseries: List[TimData] = []
with filepath.open(encoding="utf8") as file:
lines = file.readlines()
comments, start_timeseries_index = TimParser._read_header_comments(lines)
timeseries = TimParser._read_time_series_data(lines, start_timeseries_index)
return {"comments": comments, "timeseries": timeseries}
Serializer¶
TimSerializer
¶
serialize(path: Path, data: Dict[str, List[Any]], config: TimSerializerConfig, save_settings: ModelSaveSettings) -> None
staticmethod
¶
Serialize timeseries data to a file in .tim format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path |
The path to the destination .tim file. |
required |
data |
Dict[str, List[Any]] |
A dictionary with keys "comments" and "timeseries". |
required |
config |
TimSerializerConfig |
The serialization configuration settings. |
required |
save_settings |
ModelSaveSettings |
The save settings to be used. |
required |
Source code in hydrolib/core/dflowfm/tim/serializer.py
@staticmethod
def serialize(
path: Path,
data: Dict[str, List[Any]],
config: TimSerializerConfig,
save_settings: ModelSaveSettings,
) -> None:
"""
Serialize timeseries data to a file in .tim format.
Args:
path (Path): The path to the destination .tim file.
data (Dict[str, List[Any]]): A dictionary with keys "comments" and "timeseries".
- "comments" represents comments found at the start of the file.
- "timeseries" is a list of dictionaries with the key as "time" and values as "data".
- "time" is a time as a string.
- "data" is data as a list of strings.
config (TimSerializerConfig): The serialization configuration settings.
save_settings (ModelSaveSettings): The save settings to be used.
"""
path.parent.mkdir(parents=True, exist_ok=True)
commentlines = TimSerializer._serialize_comment_lines(data)
timeserieslines = TimSerializer._serialize_timeseries_lines(data, config)
file_content = TimSerializer._serialize_file_content(
timeserieslines, commentlines
)
with path.open("w", encoding="utf8") as file:
file.write(file_content)
TimSerializerConfig (SerializerConfig)
pydantic-model
¶
Configuration settings for the TimSerializer.