Created
December 21, 2019 16:40
-
-
Save jamesshannon/eae1bb995a906dcb1ba2ca8172a7c53e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks.