fosanalysis
A framework to evaluate distributed fiber optic sensor data
Loading...
Searching...
No Matches
tsvutils.py
Go to the documentation of this file.
2"""
3Utility functions for Luna ODiSI 6100 TSV files.
4
5\author Bertram Richter
6\date 2025
7"""
8
9import datetime
10import glob
11import os
12
13import brplotviz
14
15from . import filereader
16
18 input_file: str,
19 output_file: str,
20 list_of_time_intervals: list,
21 ) -> None:
22 r"""
23 Extracts the parts of the measurement data into smaller `.tsv` files.
24 The parts are given by time intervals.
25 The file header is copied as is.
26 A measurement line is copied to the `output_file` if its timestamp
27 \f$t\f$ falls within at least one of the given time intervals:
28 \f$t_{\mathrm{s}} \leq t \leq t_{\mathrm{e}}\f$.
29 Overlapping intervals are supported.
30
31 \warning The `output_file` is overwritten without asking for confirmation!
32
33 \param input_file String with the path of the input file (.tsv)
34 \param output_file String with the path of the output file (.tsv)
35 \param list_of_time_intervals List of time intervals.
36 Each interval is given as tuple of its the limits
37 (start time \f$t_{\mathrm{s}}\f$, end time \f$t_{\mathrm{e}}\f$).
38 Both limits can be either `datetime.datetime` or `None`.
39 Setting one or both of the limits to `None` results in an (half) unlimited open interval.
40 It is not necessary that the exact interval boundaries appear in the data.
41 """
42 with open(input_file, "r") as infile, open(output_file, "w") as outfile:
43 for line in infile:
44 entries = line.split("\t")
45 try:
46 timestamp = datetime.datetime.fromisoformat(entries[0].strip())
47 for start_time, end_time in list_of_time_intervals:
48 if (start_time is None or timestamp >= start_time) and (end_time is None or timestamp <= end_time):
49 outfile.write(line)
50 break
51 except ValueError:
52 outfile.write(line)
53
55 root_dir: str,
56 pattern: str = "*",
57 keys: list = ["Channel", "Sensor Name", "Sensor Serial Number", "Length (m)", "Date"],
58 table_style: str = "Markdown",
59 ):
60 r"""
61 Read the metadata from all files matching the `pattern` in a given
62 directory `root_dir`, extract selected metadata entries (`keys`) and
63 display them in a nicely formatted table.
64 Use this function to quickly gain an overview over the content of a
65 directory with measurement files exported to TSV format by Luna ODiSI.
66 The file filtering is done with Python built-in module `glob`.
67 The table formatting is done with `brplotviz`.
68 \param root_dir Directory to scan for TSV files matching the `pattern`.
69 \param pattern File name pattern to select TSV files, according to
70 the specification of the Python `glob` module.
71 \param keys List of dictionary keys to extract.
72 These are the description texts in the file header.
73 \param table_style Table output style supported by `brplotviz`.
74 Please consider the `brplotviz` documentation for available options.
75 """
76 table = []
77 for file in glob.glob(pattern, root_dir=root_dir):
78 file_path = os.path.join(root_dir, file)
79 reader = filereader.TsvReader(file_path)
80 metadata = reader._read_metadata()
81 row = [file]
82 for key in keys:
83 row.append(metadata.get(key, ""))
84 table.append(row)
85 head_row = ["File"] + keys
86 brplotviz.table.print_table(table,
87 style=table_style,
88 head_row=head_row,
89 )
File reader class for the .tsv measurement files exported by the ODiSI 6100 series interrogators by L...
None tsv_extract_time_intervals(str input_file, str output_file, list list_of_time_intervals)
Extracts the parts of the measurement data into smaller .tsv files.
Definition tsvutils.py:21
tsv_print_metadata_table(str root_dir, str pattern="*", list keys=["Channel", "Sensor Name", "Sensor Serial Number", "Length (m)", "Date"], str table_style="Markdown")
Read the metadata from all files matching the pattern in a given directory root_dir,...
Definition tsvutils.py:59