From e7dc68d931814c29f2e52b9b857156cdcbb952fa Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Tue, 18 Feb 2025 18:07:09 -0500 Subject: [PATCH] Improve formatting, Shapefile timestamp parsing --- lib/nexrad/vtec.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/nexrad/vtec.py b/lib/nexrad/vtec.py index 93d8dfe..e53f689 100644 --- a/lib/nexrad/vtec.py +++ b/lib/nexrad/vtec.py @@ -7,8 +7,19 @@ import shapefile from nexrad.db import DatabaseTable from nexrad.coord import COORD_SYSTEM -def parse_timestamp(timestamp: str): - return datetime.datetime.strptime(timestamp, '%Y%m%d%H%M%S').astimezone(datetime.UTC) +def parse_timestamp(text: str): + size = len(text) + + if size == 8: + fmt = '%Y%m%d' + elif size == 10: + fmt = '%Y%m%d%H' + elif size == 12: + fmt = '%Y%m%d%H%M' + elif size == 14: + fmt = '%Y%m%d%H%M%S' + + return datetime.datetime.strptime(text, fmt).astimezone(datetime.UTC) def shape_to_geojson(shape: shapefile.Shape): return json.dumps(shape.__geo_interface__) @@ -55,6 +66,7 @@ class VTEC(DatabaseTable): def from_shapefile_record(record, shape): vtec = VTEC() vtec.id = None + vtec.timestamp_issued = parse_timestamp(record['ISSUED']) vtec.timestamp_expired = parse_timestamp(record['EXPIRED']) vtec.timestamp_init_iss = parse_timestamp(record['INIT_ISS']) @@ -62,9 +74,10 @@ class VTEC(DatabaseTable): vtec.timestamp_updated = parse_timestamp(record['UPDATED']) vtec.timestamp_poly_start = parse_timestamp(record['POLY_BEG']) vtec.timestamp_poly_end = parse_timestamp(record['POLY_END']) - vtec.event_id = int(record['ETN']) if (record['ETN'] is not None and record['ETN'] != '') else None - vtec.hail_size = float(record['HAILTAG']) if record['HAILTAG'] is not None else None - vtec.wind_speed = float(record['WINDTAG']) if record['WINDTAG'] is not None else None + + vtec.event_id = int(record['ETN']) if (record['ETN'] is not None and record['ETN'] != '') else None + vtec.hail_size = float(record['HAILTAG']) if record['HAILTAG'] is not None else None + vtec.wind_speed = float(record['WINDTAG']) if record['WINDTAG'] is not None else None vtec.status = record['STATUS'] vtec.wfo = record['WFO']