Improve formatting, Shapefile timestamp parsing

This commit is contained in:
XANTRONIX Industrial 2025-02-18 18:07:09 -05:00
parent 0ad95bf0d3
commit e7dc68d931

View file

@ -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']