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