def parseGribData(self, filepath, lat, lon): ''' Executes wgrib2 and parses its output ''' args = ['-s', '-lon', '%f' % (lon), '%f' % (lat), os.sep.join([self.conf.cachepath, filepath]) ] if self.conf.spinfo: p = subprocess.Popen([self.conf.wgrib2bin] + args, stdout=subprocess.PIPE, startupinfo=self.conf.spinfo, shell=True) else: p = subprocess.Popen([self.conf.wgrib2bin] + args, stdout=subprocess.PIPE) it = iter(p.stdout) data = {} clouds = {} pressure = False for line in it: r = line[:-1].split(':') # Level, variable, value level, variable, value = [r[4].split(' '), r[3], r[7].split(',')[2].split('=')[1]] if len(level) > 1: if level[1] == 'cloud': #cloud layer clouds.setdefault(level[0], {}) if len(level) > 3 and variable == 'PRES': clouds[level[0]][level[2]] = value else: #level coverage/temperature clouds[level[0]][variable] = value elif level[1] == 'mb': # wind levels data.setdefault(level[0], {}) data[level[0]][variable] = value elif level[0] == 'mean': if variable == 'PRMSL': pressure = c.pa2inhg(float(value)) windlevels = [] cloudlevels = [] # Let data ready to push on datarefs. # Convert wind levels for level in data: wind = data[level] if 'UGRD' in wind and 'VGRD' in wind: hdg, vel = c.c2p(float(wind['UGRD']), float(wind['VGRD'])) #print wind['UGRD'], wind['VGRD'], float(wind['UGRD']), float(wind['VGRD']), hdg, vel alt = c.mb2alt(float(level)) # Optional varialbes temp, rh, dew = False, False, False # Temperature if 'TMP' in wind: temp = float(wind['TMP']) # Relative Humidity if 'RH' in wind: rh = float(wind['RH']) else: temp = False if temp and rh: dew = c.dewpoint(temp, rh) windlevels.append([alt, hdg, c.ms2knots(vel), {'temp': temp, 'rh': rh, 'dew': dew, 'gust': 0}]) #print 'alt: %i rh: %i vis: %i' % (alt, float(wind['RH']), vis) # Convert cloud level for level in clouds: level = clouds[level] if 'top' in level and 'bottom' in level and 'TCDC' in level: top, bottom, cover = float(level['top']), float(level['bottom']), float(level['TCDC']) #print "XPGFS: top: %.0fmbar %.0fm, bottom: %.0fmbar %.0fm %d%%" % (top * 0.01, c.mb2alt(top * 0.01), bottom * 0.01, c.mb2alt(bottom * 0.01), cover) #if bottom > 1 and alt > 1: cloudlevels.append([c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover]) #XP10 #cloudlevels.append((c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover/10)) windlevels.sort() cloudlevels.sort() data = { 'winds': windlevels, 'clouds': cloudlevels, 'pressure': pressure } return data
def parse_grib_data(self, filepath, lat, lon): """Executes wgrib2 and parses its output""" args = ['-s', '-lon', '%f' % (lon), '%f' % (lat), filepath ] kwargs = {'stdout': subprocess.PIPE} if self.conf.spinfo: kwargs.update({'startupinfo': self.conf.spinfo, 'shell': True}) print("Calling subprocess with {}, {}".format([self.conf.wgrib2bin] + args, kwargs)) p = subprocess.Popen([self.conf.wgrib2bin] + args, **kwargs) print("result of grib data subprocess is p={}".format(p)) it = iter(p.stdout) data = {} clouds = {} pressure = False for line in it: if sys.version_info.major == 2: r = line[:-1].split(':') else: r = line.decode('utf-8')[:-1].split(':') # Level, variable, value level, variable, value = [r[4].split(' '), r[3], r[7].split(',')[2].split('=')[1]] if len(level) > 1: if level[1] == 'cloud': # cloud layer clouds.setdefault(level[0], {}) if len(level) > 3 and variable == 'PRES': clouds[level[0]][level[2]] = value else: # level coverage/temperature clouds[level[0]][variable] = value elif level[1] == 'mb': # wind levels data.setdefault(level[0], {}) data[level[0]][variable] = value elif level[0] == 'mean': if variable == 'PRMSL': pressure = c.pa2inhg(float(value)) windlevels = [] cloudlevels = [] # Let data ready to push on datarefs. # Convert wind levels if sys.version_info.major == 2: wind_levels = data.iteritems() else: wind_levels = iter(data.items()) for level, wind in wind_levels: if 'UGRD' in wind and 'VGRD' in wind: hdg, vel = c.c2p(float(wind['UGRD']), float(wind['VGRD'])) # print wind['UGRD'], wind['VGRD'], float(wind['UGRD']), float(wind['VGRD']), hdg, vel alt = int(c.mb2alt(float(level))) # Optional varialbes temp, rh, dew = False, False, False # Temperature if 'TMP' in wind: temp = float(wind['TMP']) # Relative Humidity if 'RH' in wind: rh = float(wind['RH']) if temp and rh: dew = c.dewpoint(temp, rh) windlevels.append([alt, hdg, c.ms2knots(vel), {'temp': temp, 'rh': rh, 'dew': dew, 'gust': 0}]) # print 'alt: %i rh: %i vis: %i' % (alt, float(wind['RH']), vis) # Convert cloud level for level in clouds: level = clouds[level] if 'top' in level and 'bottom' in level and 'TCDC' in level: top, bottom, cover = float(level['top']), float(level['bottom']), float(level['TCDC']) # print "XPGFS: top: %.0fmbar %.0fm, bottom: %.0fmbar %.0fm %d%%" % (top * 0.01, c.mb2alt(top * 0.01), bottom * 0.01, c.mb2alt(bottom * 0.01), cover) # if bottom > 1 and alt > 1: cloudlevels.append([c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover]) # XP10 # cloudlevels.append((c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover/10)) windlevels.sort() cloudlevels.sort() data = { 'winds': windlevels, 'clouds': cloudlevels, 'pressure': pressure } return data
def parseGribData(self, filepath, lat, lon): ''' Executes wgrib2 and parses its output ''' args = [ '-s', '-lon', '%f' % (lon), '%f' % (lat), os.sep.join([self.conf.cachepath, filepath]) ] if self.conf.spinfo: p = subprocess.Popen([self.conf.wgrib2bin] + args, stdout=subprocess.PIPE, startupinfo=self.conf.spinfo, shell=True) else: p = subprocess.Popen([self.conf.wgrib2bin] + args, stdout=subprocess.PIPE) it = iter(p.stdout) data = {} clouds = {} pressure = False for line in it: r = line[:-1].split(':') # Level, variable, value level, variable, value = [ r[4].split(' '), r[3], r[7].split(',')[2].split('=')[1] ] if len(level) > 1: if level[1] == 'cloud': #cloud layer clouds.setdefault(level[0], {}) if len(level) > 3 and variable == 'PRES': clouds[level[0]][level[2]] = value else: #level coverage/temperature clouds[level[0]][variable] = value elif level[1] == 'mb': # wind levels data.setdefault(level[0], {}) data[level[0]][variable] = value elif level[0] == 'mean': if variable == 'PRMSL': pressure = c.pa2inhg(float(value)) windlevels = [] cloudlevels = [] # Let data ready to push on datarefs. # Convert wind levels for level in data: wind = data[level] if 'UGRD' in wind and 'VGRD' in wind: hdg, vel = c.c2p(float(wind['UGRD']), float(wind['VGRD'])) #print wind['UGRD'], wind['VGRD'], float(wind['UGRD']), float(wind['VGRD']), hdg, vel alt = c.mb2alt(float(level)) # Optional varialbes temp, rh, dew = False, False, False # Temperature if 'TMP' in wind: temp = float(wind['TMP']) # Relative Humidity if 'RH' in wind: rh = float(wind['RH']) else: temp = False if temp and rh: dew = c.dewpoint(temp, rh) windlevels.append([ alt, hdg, c.ms2knots(vel), { 'temp': temp, 'rh': rh, 'dew': dew, 'gust': 0 } ]) #print 'alt: %i rh: %i vis: %i' % (alt, float(wind['RH']), vis) # Convert cloud level for level in clouds: level = clouds[level] if 'top' in level and 'bottom' in level and 'TCDC' in level: top, bottom, cover = float(level['top']), float( level['bottom']), float(level['TCDC']) #print "XPGFS: top: %.0fmbar %.0fm, bottom: %.0fmbar %.0fm %d%%" % (top * 0.01, c.mb2alt(top * 0.01), bottom * 0.01, c.mb2alt(bottom * 0.01), cover) #if bottom > 1 and alt > 1: cloudlevels.append([ c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover ]) #XP10 #cloudlevels.append((c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover/10)) windlevels.sort() cloudlevels.sort() data = { 'winds': windlevels, 'clouds': cloudlevels, 'pressure': pressure } return data
def parseGribData(self, filepath, lat, lon): """ Executes wgrib2 and parses its output """ args = ["-s", "-lon", "%f" % (lon), "%f" % (lat), os.sep.join([self.conf.cachepath, filepath])] if self.conf.spinfo: p = subprocess.Popen( [self.conf.wgrib2bin] + args, stdout=subprocess.PIPE, startupinfo=self.conf.spinfo, shell=True ) else: p = subprocess.Popen([self.conf.wgrib2bin] + args, stdout=subprocess.PIPE) it = iter(p.stdout) data = {} clouds = {} pressure = False for line in it: r = line[:-1].split(":") # Level, variable, value level, variable, value = [r[4].split(" "), r[3], r[7].split(",")[2].split("=")[1]] if len(level) > 1: if level[1] == "cloud": # cloud layer clouds.setdefault(level[0], {}) if len(level) > 3 and variable == "PRES": clouds[level[0]][level[2]] = value else: # level coverage/temperature clouds[level[0]][variable] = value elif level[1] == "mb": # wind levels data.setdefault(level[0], {}) data[level[0]][variable] = value elif level[0] == "mean": if variable == "PRMSL": pressure = c.pa2inhg(float(value)) windlevels = [] cloudlevels = [] # Let data ready to push on datarefs. # Convert wind levels for level in data: wind = data[level] if "UGRD" in wind and "VGRD" in wind: hdg, vel = c.c2p(float(wind["UGRD"]), float(wind["VGRD"])) # print wind['UGRD'], wind['VGRD'], float(wind['UGRD']), float(wind['VGRD']), hdg, vel alt = c.mb2alt(float(level)) # Optional varialbes temp, rh, dew = False, False, False # Temperature if "TMP" in wind: temp = float(wind["TMP"]) # Relative Humidity if "RH" in wind: rh = float(wind["RH"]) else: temp = False if temp and rh: dew = c.dewpoint(temp, rh) windlevels.append([alt, hdg, c.ms2knots(vel), {"temp": temp, "rh": rh, "dew": dew, "gust": 0}]) # print 'alt: %i rh: %i vis: %i' % (alt, float(wind['RH']), vis) # Convert cloud level for level in clouds: level = clouds[level] if "top" in level and "bottom" in level and "TCDC" in level: top, bottom, cover = float(level["top"]), float(level["bottom"]), float(level["TCDC"]) # print "XPGFS: top: %.0fmbar %.0fm, bottom: %.0fmbar %.0fm %d%%" % (top * 0.01, c.mb2alt(top * 0.01), bottom * 0.01, c.mb2alt(bottom * 0.01), cover) # if bottom > 1 and alt > 1: cloudlevels.append([c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover]) # XP10 # cloudlevels.append((c.mb2alt(bottom * 0.01) * 0.3048, c.mb2alt(top * 0.01) * 0.3048, cover/10)) windlevels.sort() cloudlevels.sort() data = {"winds": windlevels, "clouds": cloudlevels, "pressure": pressure} return data