From 3488c8bf4da63f59486bc9f6a1ad4bd114e45bc6 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Fri, 3 May 2024 11:44:32 +0200 Subject: [PATCH] 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. --- bin/preplots.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/bin/preplots.py b/bin/preplots.py index 1dae5e4..8f2de1a 100644 --- a/bin/preplots.py +++ b/bin/preplots.py @@ -1,9 +1,19 @@ 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: @@ -23,12 +33,20 @@ def from_file (file, realpath = None): """ filepath = realpath or file["path"] - records = fwr.from_file(filepath, file) + 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: