Files
dougal-software/bin/preplots.py
D. Berge be0d7b269f Support import of various fixed-width formats.
This supports reading: SPSv1, SPSv2.1, P190 and custom
fixed-width formats. Supports skipping lines by startswith()
matching or by complete match (e.g., "EOF").

Closes #300 (SPS v1)
Closes #301 (SPS v2.1)
Closes #302 (P1/90)
2024-05-01 10:47:54 +02:00

42 lines
831 B
Python

import fwr
"""
Preplot importing functions.
"""
def from_file (file, realpath = None):
"""
Return a list of dicts, where each dict has the structure:
{
"line_name": <int>,
"points": [
{
"line_name": <int>,
"point_number": <int>,
"easting": <float>,
"northing": <float>
},
]
}
On error, return a string describing the error condition.
"""
filepath = realpath or file["path"]
records = fwr.from_file(filepath, file)
if type(records) == str:
# This is an error message
return records
lines = []
line_names = set([r["line_name"] for r in records])
for line_name in line_names:
line = dict(line_name=line_name)
line_points = [r for r in records if r["line_name"] == line_name]
if line_points:
line["points"] = line_points
lines.append(line)
return lines