Skip to content

Commit

Permalink
Merge pull request #153 from metno/parse_vona_latlon_format
Browse files Browse the repository at this point in the history
Parse N6451/W01947 format
  • Loading branch information
magnusuMET authored Feb 13, 2025
2 parents 5419cee + e586df0 commit 80c1715
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions utils/SnapPy/Snappy/EEMEP/resources/startScreenVolc.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ <h2>EEMEP Log</h2>
<br>
<fieldset>
If <b>Use Lat-Lon</b>:<br>
<span title="e.g. 60.15, 60:2:3N, 60°2'3 N"><label>Latitude: &nbsp;&nbsp;<input name="latitude" type="text" size="8" maxlength="10"></label></span><br>
<span title="e.g. 10.15, 10:2:3E, 10°2'03 E"><label>Longitude: <input name="longitude" type="text" size="8" maxlength="10"></label></span><br>
<span title="e.g. 60.15, 60:2:3N, 60°2'3 N, N6451"><label>Latitude: &nbsp;&nbsp;<input name="latitude" type="text" size="8" maxlength="10"></label></span><br>
<span title="e.g. 10.15, 10:2:3E, 10°2'03 E, W01947"><label>Longitude: <input name="longitude" type="text" size="8" maxlength="10"></label></span><br>
<label>Volcano altitude (meters above sea level): <input name="altitude" type="number" step="1" max="9000" min="0"></label>
</fieldset>
</fieldset>
Expand Down
17 changes: 14 additions & 3 deletions utils/SnapPy/Snappy/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
def _parseLLNumber(llstr):
''' parse a latitude or longitude string to a decimal returning (decimal, character)
where character can should be NSEW or empty.
Possible formats: -3.54, 3:5:3 S, 3° 5' 34"S
Possible formats: -3.54, 3:5:3 S, 3° 5', 34"S, N6451
Raises a ValueError if the format doesn't match
'''
Expand All @@ -51,6 +51,15 @@ def _parseLLNumber(llstr):
llstr = llstr.strip()
decimal = 0

if len(llstr) > 0 and llstr[0].upper() in "NSEW":
character = llstr[0]
num = llstr[1:].strip()
degrees = num[:2]
minutes = num[2:]

number = float(degrees) + float(minutes) / 60.0
return number, character.upper()

# fetch and remove last character (NSEW)
character = ''
if re.search(r'[A-Za-z]$', llstr):
Expand All @@ -77,7 +86,7 @@ def _parseLLNumber(llstr):

def parseLat(latStr):
''' parse a latitude string to decimal degrees, raise an exception on error
Possible formats: -3.54, 3:5:3 S, 3° 5' 34"S
Possible formats: -3.54, 3:5:3 S, 3° 5', 34"S, N6451
'''
try:
(decimal, northSouth) = _parseLLNumber(latStr)
Expand All @@ -96,7 +105,7 @@ def parseLat(latStr):

def parseLon(lonStr):
''' parse a longitude string to decimal degrees, raise an exception on error
Possible formats: -3.54, 3:5:3 W, 3° 5' 34"W
Possible formats: -3.54, 3:5:3 W, 3° 5', 34"W, W01947
'''
try:
(decimal, eastWest) = _parseLLNumber(lonStr)
Expand All @@ -122,6 +131,7 @@ def testParseLat(self):
self.assertAlmostEqual(parseLat("3 °5' 3\" S"), -3.0841, msg="parseLat(\"3 °5' 3\" S\")", delta=1e-3)
self.assertAlmostEqual(parseLat("60°5'5\"N"), 60.084722, msg="parseLat(\"60°5'5\"N\")", delta=1e-3)
self.assertAlmostEqual(parseLat("8°20′2+″S+"), -8.333, msg="parseLat(\"8°20′27″S \")", delta=1e-3)
self.assertAlmostEqual(parseLat("N6451"), 64.85, msg="parseLat(\"N6451 \")", delta=1e-3)
self.assertRaises(ValueError, parseLat, "195")

def testParseLon(self):
Expand All @@ -133,6 +143,7 @@ def testParseLon(self):
self.assertAlmostEqual(parseLon("10:5W"), -10.08333, msg="parseLon(\"10:5W\")", delta=1e-3)
self.assertAlmostEqual(parseLon("3 °5' 3\" W"), -3.0841, msg="parseLon(\"3 °5' 3\" W\")", delta=1e-3)
self.assertAlmostEqual(parseLon("10°4'W"), -10.06666, msg="parseLon(\"10°4'W\")", delta=1e-3)
self.assertAlmostEqual(parseLon("W01947"), -19.7833333, msg="parseLon(\"W01947\")", delta=1e-3)
self.assertRaises(ValueError, parseLon, "370")


Expand Down
4 changes: 2 additions & 2 deletions utils/SnapPy/Snappy/resources/startScreen.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ <h2>SNAP Log</h2>
</select>
<br>
If <b>Use Lat-Lon</b>:<br>
<span title="e.g. 60.15, 60:2:3N, 60°2'3 N">Latitude: &nbsp;&nbsp;<input name="latitude" type="text" size="10" maxlength="10"></span><br>
<span title="e.g. 10.15, 10:2:3E, 10°2'03 E">Longitude: <input name="longitude" type="text" size="10" maxlength="10"></span><p>
<span title="e.g. 60.15, 60:2:3N, 60°2'3 N, N6451">Latitude: &nbsp;&nbsp;<input name="latitude" type="text" size="10" maxlength="10"></span><br>
<span title="e.g. 10.15, 10:2:3E, 10°2'03 E, W01947">Longitude: <input name="longitude" type="text" size="10" maxlength="10"></span><p>
</fieldset>
</div>
<div style="float: left;">
Expand Down

0 comments on commit 80c1715

Please sign in to comment.