Skip to content

Instantly share code, notes, and snippets.

@jamesshannon
Created December 21, 2019 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesshannon/eae1bb995a906dcb1ba2ca8172a7c53e to your computer and use it in GitHub Desktop.
Save jamesshannon/eae1bb995a906dcb1ba2ca8172a7c53e to your computer and use it in GitHub Desktop.
import datetime
import re
class RUCAnalysis(object):
def __init__(self, gsd):
lines = gsd.split('\n')
self.soundings = []
self._parse_header(lines[:2])
self._parse_other(lines[3:])
def _parse_header(self, header_lines):
pattern = r'(.*) analysis.*grid point (\d+\.\d+) nm / (\d+) deg from (.*):'
match = re.match(pattern, header_lines[0])
self.grid = {'source': match.group(4),
'offset_nm': float(match.group(2)),
'offset_deg': float(match.group(3))}
time_bits = ' '.join(header_lines[1].split()[1:])
self.valid = datetime.datetime.strptime(time_bits, '%H %d %b %Y')
def _parse_other(self, other_lines):
for line in other_lines:
cols = line.split()
if not (len(cols) == 7 and cols[0].isnumeric()):
continue
# Capture ValueError exceptions when trying to parse the strings to ints
try:
temp = int(cols[3])
if temp == 99999:
continue
# Temp and dewpoint are in celsius * 10
temp_c = temp / 10
dewpoint_c = int(cols[4]) / 10
# Altitude is in meters
alt_ft = int(round(int(cols[2]) * 3.28084, 0))
# Pressure is in decapascals
pressure_mb = int(round(int(cols[1]) / 10, 0))
self.soundings.append((alt_ft, pressure_mb, temp_c, dewpoint_c))
except ValueError:
continue
@cyberorg
Copy link

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment