Skip to content

Commit

Permalink
Merge pull request #3549 from anacmontoya/wpc-bulletin-coords
Browse files Browse the repository at this point in the history
fixes issue #3535
  • Loading branch information
dopplershift committed Jul 9, 2024
2 parents df51619 + 67062e5 commit 7997bfe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/metpy/io/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,22 @@ def _decode_coords(coordinates):
(-119.3, 47.3)
"""
# Define latitude orientation
flip = 1

if coordinates[0] == '-':
coordinates = coordinates[1:]
# Flip latitude to Southern Hemisphere
flip = -1

# Based on the number of digits, find the correct place to split between lat and lon
# Hires bulletins provide 7 digits for coordinates; regular bulletins provide 4 or 5 digits
split_pos = int(len(coordinates) / 2)
lat, lon = coordinates[:split_pos], coordinates[split_pos:]

# Insert decimal point at the correct place and convert to float
lat = float(f'{lat[:2]}.{lat[2:]}')
lat = float(f'{lat[:2]}.{lat[2:]}') * flip
lon = -float(f'{lon[:3]}.{lon[3:]}')

return lon, lat


Expand Down
37 changes: 37 additions & 0 deletions tests/io/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,40 @@ def test_parse_wpc_surface_bulletin():
[-102, 32], [-103, 31]])

assert all(df.valid == datetime(2021, 6, 28, 18, 0, 0))


@needs_module('shapely')
def test_negative_lat_highres():
"""Test decoding of high res coordinates with negative latitude."""
from io import BytesIO

import shapely.geometry as sgeom

sample = BytesIO(b"""
178
ASUS02 KWBC 281800
CODSUS
CODED SURFACE FRONTAL POSITIONS
NWS WEATHER PREDICTION CENTER COLLEGE PARK MD
342 PM EDT MON JUN 28 2021
VALID 062818Z
HIGHS 1022 -3961069 1020 -3851069 1026 3750773 1022 4430845 1019 5520728 1018
""")
df = parse_wpc_surface_bulletin(sample)
assert df.geometry[0] == sgeom.Point([-106.9, -39.6])


@needs_module('shapely')
def test_negative_lat():
"""Test decoding of coordinates with negative latitude."""
from io import BytesIO

import shapely.geometry as sgeom

sample = BytesIO(b"""12HR PROG VALID xxxxxxZ
HIGHS -351 -3985 -4046 -38117 -7510
""")
df = parse_wpc_surface_bulletin(sample)
assert df.geometry[0] == sgeom.Point([-51, -3])

0 comments on commit 7997bfe

Please sign in to comment.