Files
dougal-software/bin/preplots.py
D. Berge 3488c8bf4d Support preplot imports in additional formats.
This adds support for SPS v1, SPS v2.1, custom fixed-width,
CSV and custom sailline info preplot imports.
2024-05-03 11:44:32 +02:00

60 lines
1.3 KiB
Python

import fwr
import delimited
"""
Preplot importing functions.
"""
def is_fixed_width (file):
fixed_width_types = [ "sps1", "sps21", "p190", "fixed-width" ]
return type(file) == dict and "type" in file and file["type"] in fixed_width_types
def is_delimited (file):
delimited_types = [ "csv", "p111", "x-sl+csv" ]
return type(file) == dict and "type" in file and file["type"] in delimited_types
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"]
if is_fixed_width(file):
records = fwr.from_file(filepath, file)
elif is_delimited(file):
records = delimited.from_file(filepath, file)
else:
return "Unrecognised file format"
if type(records) == str:
# This is an error message
return records
if file.get("type") == "x-sl+csv":
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