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