nexrad-archive/lib/nexrad/radar.py

90 lines
2.4 KiB
Python
Raw Normal View History

2025-02-10 20:05:00 -05:00
import csv
import re
2025-02-11 11:29:16 -05:00
from nexrad.coord import Coord, COORD_SYSTEM
"""
2025-02-11 12:10:02 -05:00
Implements a parser and wrapper class for the WSR-88D radar list
2025-02-11 11:29:16 -05:00
available at the following location (accessed 10 Feb 2025):
https://apollo.nvu.vsc.edu/classes/remote/lecture_notes/radar/88d/88D_locations.html
The input TSV file is created by copying and pasting the tabular data
from a web browser into a text file.
"""
2025-02-10 20:05:00 -05:00
RE_PARSE = re.compile(r'^\s*(\d+)([NS]*)\s+/\s+(\d+)([EW]*)\s*$')
def parse_int(text: str):
size = len(text)
degree = int(text[0:size-4])
minute = int(text[size-4:size-2])
second = int(text[size-2:])
return degree + (minute / 60) + (second / 3600)
def parse(text: str):
match = RE_PARSE.match(text)
if match is None:
2025-02-11 11:29:58 -05:00
raise Exception("Invalid coordinates '%s'" % text)
2025-02-10 20:05:00 -05:00
sign_lon = 1 if match[4] == 'E' else -1
sign_lat = -1 if match[2] == 'S' else 1
2025-02-10 20:05:00 -05:00
lon = parse_int(match[3])
lat = parse_int(match[1])
2025-02-10 20:05:00 -05:00
return Coord(sign_lon * lon, sign_lat * lat)
2025-02-10 20:05:00 -05:00
2025-02-11 16:06:21 -05:00
RADAR_RANGE = 230000
2025-02-11 12:10:02 -05:00
class Radar():
2025-02-10 20:05:00 -05:00
__slots__ = 'wban', 'call', 'name', 'coord', 'site_elevation', 'tower_height',
wban: int
call: str
name: str
coord: Coord
site_elevation: float
tower_height: float
@staticmethod
def from_tsv_row(row: list):
2025-02-11 12:10:02 -05:00
radar = Radar()
radar.wban = int(row[0]) if row[0] != 'PENDING' else None
radar.call = row[1]
radar.name = row[2]
radar.coord = parse(row[3])
radar.site_elevation = 0.3048 * float(row[4])
radar.tower_height = float(row[5])
2025-02-10 20:05:00 -05:00
2025-02-11 12:10:02 -05:00
return radar
2025-02-10 20:05:00 -05:00
@staticmethod
def each_from_tsv(file: str):
with open(file) as fh:
reader = csv.reader(fh, delimiter='\t')
for row in reader:
for i in range(0, len(row)):
row[i] = row[i].rstrip()
2025-02-11 12:10:02 -05:00
yield Radar.from_tsv_row(row)
2025-02-10 20:05:00 -05:00
def add_to_db(self, db):
sql = """
2025-02-11 12:10:02 -05:00
insert into nexrad_radar (
2025-02-10 20:05:00 -05:00
wban, call, name, site_elevation, tower_height, coord
) values (
2025-02-11 11:29:16 -05:00
?, ?, ?, ?, ?, MakePoint(?, ?, %d)
2025-02-10 20:05:00 -05:00
)
2025-02-11 11:29:16 -05:00
""" % (COORD_SYSTEM)
2025-02-10 20:05:00 -05:00
db.execute(sql, (
self.wban, self.call, self.name,
self.site_elevation, self.tower_height,
self.coord.lon, self.coord.lat
2025-02-10 20:05:00 -05:00
))