def HRRR_error_wind_speed(DATE): """ return the error between the forecast and the analysis Use this for multiprocessing """ # HRRR Analysis Hu = get_hrrr_variable(DATE, variable='UGRD:'+level, verbose=False) Hv = get_hrrr_variable(DATE, variable='VGRD:'+level, verbose=False) # # HRRR Forecast Hfu = get_hrrr_variable(DATE-timedelta(hours=fxx), variable='UGRD:'+level, fxx=fxx, verbose=False) Hfv = get_hrrr_variable(DATE-timedelta(hours=fxx), variable='VGRD:'+level, fxx=fxx, verbose=False) # # proof that I grabbed the right data from the archive #print Hu['msg'], Hu['valid'] #print Hfu['msg'], Hfu['valid'] # # Calculate the difference between the HRRR analysis and HRRR forecast (fxx-anlys) # so that the red shows where the forecast is fast and blue shows where forecast # is slow. aSPD = wind_uv_to_spd(Hu['value'], Hv['value']) fSPD = wind_uv_to_spd(Hfu['value'], Hfv['value']) diff = fSPD-aSPD # return diff
def get_HRRR_value(validDATE): """ Get HRRR data. Retrun the value, not the latitude and longitude. """ runDATE = validDATE - timedelta(hours=fxx) if variable[:2] == 'UV': # Then we want to calculate the wind speed from U and V components. wind_lev = variable.split(':')[-1] # This is the level of interest Hu = get_hrrr_variable(runDATE, 'UGRD:'+wind_lev, fxx=fxx, model='hrrr', field='sfc', value_only=True, verbose=False) Hv = get_hrrr_variable(runDATE, 'VGRD:'+wind_lev, fxx=fxx, model='hrrr', field='sfc', value_only=True, verbose=False) if np.shape(Hu['value']) == () or np.shape(Hv['value']) == (): print("!! WARNING !! COULD NOT GET %s %s f%02d" % (variable, runDATE, fxx)) return None else: spd = wind_uv_to_spd(Hu['value'], Hv['value']) return spd else: H = get_hrrr_variable(runDATE, variable, fxx=fxx, model='hrrr', field='sfc', value_only=True, verbose=False) if np.shape(H['value']) == (): # If the data is a nan, then return None. This will be important for filtering. print("!! WARNING !! COULD NOT GET %s %s f%02d" % (variable, runDATE, fxx)) return None else: return H['value']
def get_HRRR_value(validDATE, variable, fxx): """ Get HRRR data. Return just the value, not the latitude and longitude. If data is np.nan, then return None, i.e. the shape of the data is (). This makes it possible to filter out the None values later. """ runDATE = validDATE - timedelta(hours=fxx) if variable.split(':')[0] == 'UVGRD': #print("getting U and V components") Hu = get_hrrr_variable(runDATE, 'UGRD', fxx=fxx, verbose=False)['value'] Hv = get_hrrr_variable(runDATE, 'VGRD', fxx=fxx, verbose=False)['value'] H = wind_uv_to_spd(Hu, Hv) else: H = get_hrrr_variable(runDATE, variable, fxx=fxx, verbose=False)['value'] if np.shape(H) == (): # Then the data is a nan. Return None so it can be filtered out. print("!! WARNING !! COULD NOT GET %s %s f%02d" % (variable, runDATE, fxx)) return None else: return H
TS_hcon = LocDic_hrrr_time_series( sDATE, eDATE, location, variable='HGT:level of adiabatic condensation from sfc', verbose=False, fxx=fxx) # Convert the units of each TimeSeries for loc in location.keys(): # Convert Units for the variables in the Pollywog TS_temp[loc] = KtoC(TS_temp[loc]) TS_dwpt[loc] = KtoC(TS_dwpt[loc]) # Derive some variables: TS_wind80[loc] = wind_uv_to_spd(TS_u80[loc], TS_v80[loc]) # Just need one vector of valid dates TS_dates = np.array(TS_temp['DATETIME']) # Make a dictionary of map object for each location. # (This speeds up plotting by creating each map once.) maps = {} for loc in location: l = location[loc] m = Basemap(resolution='i', projection='cyl',\ llcrnrlon=l['longitude']-.25, llcrnrlat=l['latitude']-.25,\ urcrnrlon=l['longitude']+.25, urcrnrlat=l['latitude']+.25,) maps[loc] = m # Create a figure for each location. Add permenant elements to each.
def plot_rose(l): """ l is a dictionary with MesoWest and HRRR data at the station for a period of time """ MW = l['MesoWest'] HR = l['HRRR'] HR['wind_speed'] = [ wind_uv_to_spd(HR['u'][i], HR['v'][i]) for i in range(len(HR['u'])) ] HR['wind_direction'] = [ wind_uv_to_dir(HR['u'][i], HR['v'][i]) for i in range(len(HR['u'])) ] # Make the wind rose - MesoWest axMW = new_axes() axMW.bar(MW['wind_direction'], MW['wind_speed'], nsector=16, normed=True, bins=range(0, 20, 2)) # Create a legend set_legend(axMW) plt.title("MesoWest %s (%s) \n %s - %s" % (MW['STID'], MW['NAME'], MW['DATETIME'][0].strftime('%d %b %Y'), MW['DATETIME'][-1].strftime('%d %b %Y'))) plt.grid(True) # Grid at 5% intervals plt.yticks(np.arange(5, 105, 5)) axMW.set_yticklabels( ['5%', '10%', '15%', '20%', '25%', '30%', '35%', '40%']) # Change the plot range axMW.set_rmax(np.max(np.sum(axMW._info['table'], axis=0))) plt.savefig(l['SAVE'] + '/' + MW['STID'] + '/rose_MW.png') print "Saved a windrose - MesoWest", MW['STID'] plt.clf() plt.cla() # Make the wind rose - HRRR axHR = new_axes() axHR.bar(HR['wind_direction'], HR['wind_speed'], nsector=16, normed=True, bins=range(0, 20, 2)) # Create a legend set_legend(axHR) plt.title("HRRR %s (%s) \n %s - %s" % (HR['STID'], HR['NAME'], HR['DATETIME'][0].strftime('%d %b %Y'), MW['DATETIME'][-1].strftime('%d %b %Y'))) plt.grid(True) # Grid at 5% intervals plt.yticks(np.arange(5, 105, 5)) axHR.set_yticklabels( ['5%', '10%', '15%', '20%', '25%', '30%', '35%', '40%']) # Change the plot range axHR.set_rmax(np.max(np.sum(axHR._info['table'], axis=0))) plt.savefig(l['SAVE'] + '/' + MW['STID'] + '/rose_HR.png') print "Saved a windrose - HRRR", HR['STID'], l['SAVE'] plt.clf() plt.cla()
def make_plots(inputs): VALIDDATE, fxx = inputs print 'working on %s f%02d' % (VALIDDATE, fxx) plt.clf() plt.cla() print fxx, VALIDDATE # === Some housekeeping variables ============================================= # Convert Valid Date to Run Date, adjusted by the forecast DATE = VALIDDATE - timedelta(hours=fxx) # Parse Location lat/lon if ',' in location: # User put inputted a lat/lon point request lat, lon = location.split(',') lat = float(lat) lon = float(lon) else: # User requested a MesoWest station stninfo = get_station_info([location]) lat = stninfo['LAT'] lon = stninfo['LON'] # Preload the latitude and longitude grid latlonpath = '/uufs/chpc.utah.edu/common/home/horel-group7/Pando/hrrr/HRRR_latlon.h5' latlonh5 = h5py.File(latlonpath, 'r') gridlat = latlonh5['latitude'][:] gridlon = latlonh5['longitude'][:] # === Create map of the domain ================================================ if dsize == 'conus' and model != 'hrrrAK': barb_thin = 70 alpha = 1 t1 = datetime.now() #m = draw_CONUS_HRRR_map(res=map_res) m = np.load( '/uufs/chpc.utah.edu/common/home/u0553130/public_html/Brian_Blaylock/cgi-bin/HRRR_CONUS_map_object_' + map_res + '.npy').item() m.drawcountries(zorder=500) m.drawstates(zorder=500) m.drawcoastlines(zorder=500) m.fillcontinents(color='tan', lake_color='lightblue', zorder=0) m.drawmapboundary(fill_color='lightblue') t2 = datetime.now() else: # configure some setting based on the requested domain size if dsize == 'small': plus_minus_latlon = .27 # +/- latlon box around center point barb_thin = 1 # Thin out excessive wind barbs arcgis_res = 1000 # ArcGIS image resolution bfr = 15 # trim domain buffer alpha = .75 # Alpha (pcolormesh transparency) elif dsize == 'medium': plus_minus_latlon = .75 barb_thin = 2 arcgis_res = 800 bfr = 35 alpha = .75 elif dsize == 'large': plus_minus_latlon = 2.5 barb_thin = 6 arcgis_res = 800 bfr = 110 alpha = .75 elif dsize == 'xlarge': plus_minus_latlon = 5 barb_thin = 12 arcgis_res = 700 bfr = 210 alpha = .75 elif dsize == 'xxlarge': # If domain runs into HRRR boundary, then it'll fail plus_minus_latlon = 10 barb_thin = 25 arcgis_res = 700 bfr = 430 alpha = .75 elif dsize == 'xxxlarge': plus_minus_latlon = 15 barb_thin = 35 arcgis_res = 1000 bfr = 700 alpha = .75 m = Basemap(resolution=map_res, projection='cyl',\ area_thresh=3000,\ llcrnrlon=lon-plus_minus_latlon, llcrnrlat=lat-plus_minus_latlon,\ urcrnrlon=lon+plus_minus_latlon, urcrnrlat=lat+plus_minus_latlon,) m.drawstates(zorder=500) m.drawcountries(zorder=500) m.drawcoastlines(zorder=500) #if dsize == 'small' or dsize == 'medium': # m.drawcounties() # === Add a Background image ================================================== # Start the map image plt.figure(1) if background == 'arcgis' and dsize != 'conus': m.arcgisimage(service='World_Shaded_Relief', xpixels=arcgis_res, verbose=False) elif background == 'arcgisSat' and dsize != 'conus': m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=arcgis_res, verbose=False) elif background == 'arcgisRoad' and dsize != 'conus': m.arcgisimage(service='NatGeo_World_Map', xpixels=arcgis_res, verbose=False) elif background == 'terrain': # Get data H_ter = get_hrrr_variable( DATE, 'HGT:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_land = get_hrrr_variable( DATE, 'LAND:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Plot the terrain m.contourf(gridlon, gridlat, H_ter['value'], levels=range(0, 4000, 200), cmap='Greys_r', zorder=1, latlon=True) # Plot Water area m.contour(gridlon, gridlat, H_land['value'], levels=[0, 1], colors='b', zorder=1, latlon=True) elif background == 'landuse': # Get data from BB_cmap.landuse_colormap import LU_MODIS21 if model == 'hrrr': VGTYP = 'VGTYP:surface' else: VGTYP = 'var discipline=2 center=59 local_table=1 parmcat=0 parm=198' H_LU = get_hrrr_variable( DATE, VGTYP, model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Plot the terrain cm, labels = LU_MODIS21() m.pcolormesh(gridlon, gridlat, H_LU['value'], cmap=cm, vmin=1, vmax=len(labels) + 1, zorder=1, latlon=True) # Add SHAPEFILE if is_FIRE: plt.gca().add_collection( PatchCollection(patches, facecolor='indianred', alpha=.65, edgecolor='k', linewidths=1, zorder=1)) # === Figure Title ============================================================ if dsize != 'conus': m.scatter(lon, lat, marker='+', c='r', s=100, zorder=1000, latlon=True) plt.title('Center: %s\n%s' % (location, model.upper()), fontweight='bold') else: plt.title('%s' % (model.upper()), fontweight='bold') plt.title('Run: %s F%02d' % (DATE.strftime('%Y-%m-%d %H:%M UTC'), fxx), loc='left') plt.title('Valid: %s' % (DATE + timedelta(hours=fxx)).strftime('%Y-%m-%d %H:%M UTC'), loc='right') # ============================================================================= if '10mWind_Fill' in plotcode or '10mWind_Shade' in plotcode or '10mWind_Barb' in plotcode or '10mWind_Quiver' in plotcode or '10mWind_p95_fill' in plotcode: # Get data H_u = get_hrrr_variable( DATE, 'UGRD:10 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_v = get_hrrr_variable( DATE, 'VGRD:10 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) spd = wind_uv_to_spd(H_u['value'], H_v['value']) if '10mWind_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap=cm_wind(), vmin=0, vmax=60, alpha=alpha) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'10 m Wind Speed (m s$\mathregular{^{-1}}$)') if '10mWind_Shade' in plotcode: m.contourf(gridlon, gridlat, spd, levels=[10, 15, 20, 25], colors=('yellow', 'orange', 'red'), alpha=alpha, extend='max', zorder=1, latlon=True) cb = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb.set_label(r'10 m Wind Speed (ms$\mathregular{^{-1}}$)') if '10mWind_Barb' in plotcode or '10mWind_Quiver' in plotcode: # For small domain plots, trimming the edges significantly reduces barb plotting time if barb_thin < 20: cut_v, cut_h = pluck_point_new(lat, lon, gridlat, gridlon) Cgridlat = gridlat[cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] Cgridlon = gridlon[cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] H_u['value'] = H_u['value'][cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] H_v['value'] = H_v['value'][cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] else: Cgridlat = gridlat Cgridlon = gridlon thin = barb_thin # Add to plot if '10mWind_Barb' in plotcode: m.barbs(Cgridlon[::thin, ::thin], Cgridlat[::thin, ::thin], H_u['value'][::thin, ::thin], H_v['value'][::thin, ::thin], zorder=200, length=5.5, barb_increments={ 'half': 2.5, 'full': 5, 'flag': 25 }, latlon=True) if '10mWind_Quiver' in plotcode: Q = m.quiver(Cgridlon[::thin, ::thin], Cgridlat[::thin, ::thin], H_u['value'][::thin, ::thin], H_v['value'][::thin, ::thin], zorder=350, latlon=True) qk = plt.quiverkey(Q, .92, 0.07, 10, r'10 m s$^{-1}$', labelpos='S', coordinates='axes', color='darkgreen') qk.text.set_backgroundcolor('w') if '10mWind_p95_fill' in plotcode: DIR = '/uufs/chpc.utah.edu/common/home/horel-group8/blaylock/HRRR_OSG/hourly30/UVGRD_10_m/' FILE = 'OSG_HRRR_%s_m%02d_d%02d_h%02d_f00.h5' % ( ('UVGRD_10_m', VALIDDATE.month, VALIDDATE.day, VALIDDATE.hour)) with h5py.File(DIR + FILE, 'r') as f: spd_p95 = f["p95"][:] masked = spd - spd_p95 masked = np.ma.array(masked) masked[masked < 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, masked, vmax=10, vmin=0, latlon=True, cmap='viridis', alpha=alpha) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label( r'10 m Wind Speed exceeding 95th Percentile (m s$\mathregular{^{-1}}$)' ) if '80mWind_Fill' in plotcode or '80mWind_Shade' in plotcode or '80mWind_Barb' in plotcode: # Get data H_u = get_hrrr_variable( DATE, 'UGRD:80 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_v = get_hrrr_variable( DATE, 'VGRD:80 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) spd = wind_uv_to_spd(H_u['value'], H_v['value']) if '80mWind_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap=cm_wind(), vmin=0, vmax=60, alpha=alpha) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'10 m Wind Speed (m s$\mathregular{^{-1}}$)') if '80mWind_Shade' in plotcode: m.contourf(gridlon, gridlat, spd, levels=[10, 15, 20, 25], colors=('yellow', 'orange', 'red'), alpha=alpha, extend='max', zorder=10, latlon=True) cb = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb.set_label(r'10 m Wind Speed (ms$\mathregular{^{-1}}$)') if '80mWind_Barb' in plotcode: # For small domain plots, trimming the edges significantly reduces barb plotting time if barb_thin < 20: cut_v, cut_h = pluck_point_new(lat, lon, gridlat, gridlon) Cgridlat = gridlat[cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] Cgridlon = gridlon[cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] H_u['value'] = H_u['value'][cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] H_v['value'] = H_v['value'][cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] else: Cgridlat = gridlat Cgridlon = gridlon # Add to plot thin = barb_thin m.barbs(Cgridlon[::thin, ::thin], Cgridlat[::thin, ::thin], H_u['value'][::thin, ::thin], H_v['value'][::thin, ::thin], zorder=200, length=5.5, color='darkred', barb_increments={ 'half': 2.5, 'full': 5, 'flag': 25 }, latlon=True) if 'Fill80mWind' in plotcode: m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap=cm_wind(), vmin=0, vmax=60, alpha=alpha) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'80 m Wind Speed (m s$\mathregular{^{-1}}$)') if 'Gust_Hatch' in plotcode: H_gust = get_hrrr_variable( DATE, 'GUST:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Add to plot m.contourf(gridlon, gridlat, H_gust['value'], levels=[0, 10, 15, 20, 25], hatches=[None, '.', '\\\\', '*'], colors='none', extend='max', zorder=10, latlon=True) cb = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb.set_label(r'Surface Wind Gust (ms$\mathregular{^{-1}}$)') m.contour(gridlon, gridlat, H_gust['value'], levels=[10, 15, 20, 25], colors='k', zorder=10, latlon=True) if 'dBZ_Fill' in plotcode or 'dBZ_Contour' in plotcode: from BB_cmap.reflectivity_colormap import reflect_ncdc # Get Data if model == 'hrrr': REFC = 'REFC:entire' elif model == 'hrrrX' or model == 'hrrrAK': REFC = 'var discipline=0 center=59 local_table=1 parmcat=16 parm=196' H_ref = get_hrrr_variable( DATE, REFC, model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values dBZ = H_ref['value'] dBZ = np.ma.array(dBZ) dBZ[dBZ == -10] = np.ma.masked # Add Contour to plot if 'dBZ_Contour' in plotcode: cREF = m.contour(gridlon, gridlat, dBZ, cmap=reflect_ncdc(), levels=range(10, 80, 10), latlon=True, zorder=50) plt.clabel(cREF, cREF.levels[::2], fmt='%2.0f', colors='k', fontsize=9) #cb2 = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) #cb2.set_label('Simulated Composite Reflectivity (dBZ)') # Add fill to plot if 'dBZ_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, dBZ, cmap=reflect_ncdc(), vmax=80, vmin=0, alpha=alpha, latlon=True) cb2 = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb2.set_label('Simulated Composite Reflectivity (dBZ)') if '2mDPT_p95p05_fill' in plotcode: H_dpt = get_hrrr_variable( DATE, 'DPT:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) DIR = '/uufs/chpc.utah.edu/common/home/horel-group8/blaylock/HRRR_OSG/hourly30/DPT_2_m/' FILE = 'OSG_HRRR_%s_m%02d_d%02d_h%02d_f00.h5' % ( ('DPT_2_m', VALIDDATE.month, VALIDDATE.day, VALIDDATE.hour)) ### Plot Dew Point Depression with h5py.File(DIR + FILE, 'r') as f: dpt_p05 = f["p05"][:] masked = H_dpt[ 'value'] - dpt_p05 # both these datasets are in Kelvin, but when we take the difference it is in Celsius masked = np.ma.array(masked) masked[masked > 0] = np.ma.masked mesh_depression = m.pcolormesh(gridlon, gridlat, masked, vmax=10, vmin=-10, latlon=True, cmap='BrBG') ### Plot Dew Point Exceedance with h5py.File(DIR + FILE, 'r') as f: dpt_p95 = f["p95"][:] masked = H_dpt[ 'value'] - dpt_p95 # both these datasets are in Kelvin, but when we take the difference it is in Celsius masked = np.ma.array(masked) masked[masked < 0] = np.ma.masked mesh_exceedance = m.pcolormesh(gridlon, gridlat, masked, vmax=10, vmin=-10, latlon=True, cmap='BrBG') cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label( r'5$\mathregular{^{th}}$/95$\mathregular{^{th}}$ percentile 2 m Dew Point Depression/Exceedance (C)' ) if '2mTemp_Fill' in plotcode or '2mTemp_Freeze' in plotcode or '2mTemp_p95p05_fill' in plotcode: # Get Data H_temp = get_hrrr_variable( DATE, 'TMP:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value'] - 273.15 # Add fill to plot if '2mTemp_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, TMP, cmap=cm_temp(), vmax=50, vmin=-50, alpha=alpha, zorder=3, latlon=True) cbT = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbT.set_label('2 m Temperature (C)') # Add freezing contour to plot if '2mTemp_Freeze' in plotcode: m.contour(gridlon, gridlat, TMP, colors='b', levels=[0], zorder=400, latlon=True) if '2mTemp_p95p05_fill' in plotcode: DIR = '/uufs/chpc.utah.edu/common/home/horel-group8/blaylock/HRRR_OSG/hourly30/TMP_2_m/' FILE = 'OSG_HRRR_%s_m%02d_d%02d_h%02d_f00.h5' % ( ('TMP_2_m', VALIDDATE.month, VALIDDATE.day, VALIDDATE.hour)) ### Plot Temperature Depression with h5py.File(DIR + FILE, 'r') as f: tmp_p05 = f["p05"][:] masked = H_temp[ 'value'] - tmp_p05 # both these datasets are in Kelvin, but when we take the difference it is in Celsius masked = np.ma.array(masked) masked[masked > 0] = np.ma.masked mesh_depression = m.pcolormesh(gridlon, gridlat, masked, vmax=10, vmin=-10, latlon=True, cmap='bwr') ### Plot Temperature Exceedance with h5py.File(DIR + FILE, 'r') as f: tmp_p95 = f["p95"][:] masked = H_temp[ 'value'] - tmp_p95 # both these datasets are in Kelvin, but when we take the difference it is in Celsius masked = np.ma.array(masked) masked[masked < 0] = np.ma.masked mesh_exceedance = m.pcolormesh(gridlon, gridlat, masked, vmax=10, vmin=-10, latlon=True, cmap='bwr') cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label( r'5$\mathregular{^{th}}$/95$\mathregular{^{th}}$ percentile 2 m Temperature Depression/Exceedance (C)' ) if '2mRH_Fill' in plotcode: # Get Data try: H_RH = get_hrrr_variable( DATE, 'RH:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Add fill to plot m.pcolormesh(gridlon, gridlat, H_RH['value'], cmap=cm_rh(), vmin=5, vmax=90, zorder=3, latlon=True) cbT = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cbT.set_label('2m Relative Humidity (%)') except: print "!! Some errors getting the RH value." print "!! If you requested an old date, from HRRR version 1, there isn't a RH variable," print "!! and this code doesn't get the dwpt and convert it to RH yet." if '700Temp_Fill' in plotcode or '700Temp_-12c' in plotcode: # Get Data H_temp = get_hrrr_variable( DATE, 'TMP:700 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value'] - 273.15 # Add fill to plot if '700Temp_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, TMP, cmap=cm_temp(), vmax=50, vmin=-50, alpha=alpha, zorder=3, latlon=True) cbT = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbT.set_label('700 mb Temperature (C)') # Add -12 C contour to plot if '700Temp_-12c' in plotcode: m.contour(gridlon, gridlat, TMP, colors='b', levels=[-12], latlon=True, zorder=400) if '500HGT_Contour' in plotcode: H_500 = get_hrrr_variable( DATE, 'HGT:500 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) CS = m.contour(gridlon, gridlat, H_500['value'], levels=range(5040, 6181, 60), linewidths=1.7, colors='k', latlon=True, zorder=400) plt.clabel(CS, inline=1, fmt='%2.f') if '500Wind_Fill' in plotcode or '500Wind_Barb' in plotcode or '500Vort_Fill' in plotcode or '500Conv_Fill' in plotcode: H_u = get_hrrr_variable( DATE, 'UGRD:500 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_v = get_hrrr_variable( DATE, 'VGRD:500 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) if '500Wind_Fill' in plotcode: spd = wind_uv_to_spd(H_u['value'], H_v['value']) m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap=cm_wind(), vmin=0, vmax=60) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'500 mb Wind Speed (m s$\mathregular{^{-1}}$)') if '500Conv_Fill' in plotcode or '500Vort_Fill' in plotcode: dudx, dudy = np.gradient(H_u['value'], 3, 3) dvdx, dvdy = np.gradient(H_v['value'], 3, 3) if '500Vort_Fill' in plotcode: vorticity = dvdx - dudy # Mask values vort = vorticity vort = np.ma.array(vort) vort[np.logical_and(vort < .05, vort > -.05)] = np.ma.masked m.pcolormesh(gridlon, gridlat, vort, latlon=True, cmap='bwr', vmax=np.max(vort), vmin=-np.max(vort)) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'500 mb Vorticity (s$\mathregular{^{-1}}$)') if '500Conv_Fill' in plotcode: convergence = dudx + dvdy # Mask values conv = convergence conv = np.ma.array(conv) conv[np.logical_and(conv < .05, conv > -.05)] = np.ma.masked m.pcolormesh(gridlon, gridlat, conv, latlon=True, cmap='bwr', vmax=np.max(conv), vmin=-np.max(conv)) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'500 mb Convergence (s$\mathregular{^{-1}}$)') if '500Wind_Barb' in plotcode: # For small domain plots, trimming the edges significantly reduces barb plotting time if barb_thin < 20: cut_v, cut_h = pluck_point_new(lat, lon, gridlat, gridlon) Cgridlat = gridlat[cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] Cgridlon = gridlon[cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] H_u['value'] = H_u['value'][cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] H_v['value'] = H_v['value'][cut_v - bfr:cut_v + bfr, cut_h - bfr:cut_h + bfr] else: Cgridlat = gridlat Cgridlon = gridlon thin = barb_thin m.barbs(Cgridlon[::thin, ::thin], Cgridlat[::thin, ::thin], H_u['value'][::thin, ::thin], H_v['value'][::thin, ::thin], zorder=200, length=6, color='navy', barb_increments={ 'half': 2.5, 'full': 5, 'flag': 25 }, latlon=True) #plt.ylabel(r'Barbs: half=2.5, full=5, flag=25 (ms$\mathregular{^{-1}}$)') if 'MSLP_Contour' in plotcode or 'MSLP_Fill' in plotcode: H = get_hrrr_variable( DATE, 'MSLMA:mean sea level', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) if 'MSLP_Contour' in plotcode: CS = m.contour(gridlon, gridlat, H['value'] / 100., latlon=True, levels=range(952, 1200, 4), colors='k', zorder=400) CS.clabel(inline=1, fmt='%2.f', zorder=400) if 'MSLP_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, H['value'] / 100., latlon=True, cmap='viridis') cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label('Mean Sea Level Pressure (hPa)') if '2mPOT_Fill' in plotcode: # Get Data H_temp = get_hrrr_variable( DATE, 'POT:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value'] - 273.15 m.pcolormesh(gridlon, gridlat, TMP, cmap="Oranges", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('2 m Potential Temperature (C)') if 'SkinTemp_Fill' in plotcode: # Get Data H_temp = get_hrrr_variable( DATE, 'TMP:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value'] - 273.15 m.pcolormesh(gridlon, gridlat, TMP, cmap="Spectral_r", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('Skin Temperature (C)') if 'AccumPrecip_Fill' in plotcode or '1hrPrecip_Fill' in plotcode: if 'AccumPrecip_Fill' in plotcode: # Get Data H = get_hrrr_variable( DATE, 'APCP:surface:0', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values prec = H['value'] prec = np.ma.array(prec) prec[prec == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, prec, cmap=cm_precip(), alpha=alpha, vmin=0, vmax=762, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('Accumulated Precipitation since F00 (mm)') if '1hrPrecip_Fill' in plotcode: # Get Data H = get_hrrr_variable( DATE, 'APCP:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values prec = H['value'] prec = np.ma.array(prec) prec[prec == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, prec, cmap=cm_precip(), alpha=alpha, vmin=0, vmax=762, zorder=3, latlon=True) cbS = plt.colorbar( orientation='horizontal', shrink=shrink, pad=pad, extend="max", ) cbS.set_label('1 hour Accumulated Precipitation (mm)') if 'SnowCover_Fill' in plotcode: # Get Data H = get_hrrr_variable( DATE, 'SNOWC', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values snow = H['value'] snow = np.ma.array(snow) snow[snow == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, snow, cmap="Blues", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('Snow Cover (%)') if 'PWAT_Fill' in plotcode: # Get Data H = get_hrrr_variable( DATE, 'PWAT:entire', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) m.pcolormesh(gridlon, gridlat, H['value'], cmap="RdYlGn", alpha=alpha, vmin=0, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label( r'Vertically Integrated Liquid Water (kg m$\mathregular{^{-2}}$)') if 'CAPE_Fill' in plotcode: # Get Data H = get_hrrr_variable( DATE, 'CAPE:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values cape = H['value'] cape = np.ma.array(cape) cape[cape == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, cape, cmap="Oranges", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label(r'Surface CAPE (J kg$\mathregular{^{-1}}$)') if 'CIN_Fill' in plotcode: # Get Data H = get_hrrr_variable( DATE, 'CIN:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values cin = H['value'] cin = np.ma.array(cin) cin[cin == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, cin, cmap="BuPu", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label(r'Surface CIN (J kg$\mathregular{^{-1}}$)') if 'RedFlag_Fill' in plotcode or 'RedFlag_Contour' in plotcode or 'RedFlagPot_Fill' in plotcode: # generalized criteria for red flag warning # Winds (gusts) greater than 6.7 m/s and RH < 25% rf_RH = 25 rf_WIND = 6.7 # Get Data H_gust = get_hrrr_variable( DATE, 'GUST:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_rh = get_hrrr_variable( DATE, 'RH:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) RedFlag = np.logical_and(H_gust['value'] > rf_WIND, H_rh['value'] < rf_RH) if 'RedFlag_Contour' in plotcode: try: CS = m.contour(gridlon, gridlat, RedFlag, latlon=True, colors='maroon', zorder=400) except: # maybe there isn't any contours in this domain pass if 'RedFlag_Fill' in plotcode: RedFlag = np.ma.array(RedFlag) RedFlag[RedFlag == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, RedFlag, cmap="YlOrRd_r", alpha=alpha, zorder=4, latlon=True) if 'RedFlagPot_Fill' in plotcode: cdict3 = { 'red': ((0.0, 1.0, 1.0), (0.5, 0.5, 0.5), (0.5, 1.0, 1.0), (1.0, 0.4, 0.4)), 'green': ((0.0, 1.0, 1.0), (0.5, 0.5, 0.5), (0.5, 0.4, 0.4), (1.0, 0.0, 0.0)), 'blue': ((0.0, 1.0, 1.0), (0.5, 0.5, 0.5), (0.5, 0.0, 0.0), (1.0, 0.0, 0.0)) } plt.register_cmap(name='FirePot', data=cdict3) # Definate Red Flag Area: RED_FLAG = np.logical_and(H_rh['value'] < rf_RH, H_gust['value'] > rf_WIND) # Linear Equation b = (rf_RH - rf_WIND) * (rf_RH / rf_WIND) z = -(rf_RH / rf_WIND) * (H_rh['value'] - H_gust['value']) + b m.pcolormesh(gridlon, gridlat, z, cmap="FirePot", alpha=alpha, vmax=200, vmin=-200, zorder=3, latlon=True) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'Red Flag Potential') m.contour(gridlon, gridlat, z, colors='k', levels=[0], zorder=3, latlon=True) m.contour(gridlon, gridlat, RED_FLAG, colors='darkred', levels=[0], zorder=3, latlon=True) plt.xlabel( r'Red Flag Criteria: Winds > 6.7 m s$\mathregular{^{-1}}$ and RH < 25%' ) # ============================================================================= # Hack! Plot an extra HRRR variable not listed on the webpage hrrr_custom.html # This extra argument will let you attempt to plot a different variable for # a quicklook. try: # Must be a variable from a line in the .idx file hrrrVAR = form['extraVAR'].value extraHRRR = get_hrrr_variable( DATE, hrrrVAR, model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) m.pcolormesh(gridlon, gridlat, extraHRRR['value'], cmap='viridis', alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label(hrrrVAR + ' (units)') except: pass # ============================================================================= #plt.ylabel('Section Timer:%s\nFull Timer:%s' % (t2-t1, datetime.now()-firsttimer)) SAVEDIR = '/uufs/chpc.utah.edu/common/home/u0553130/public_html/oper/HRRR_yesterday/%s/%s/' % ( MAP_DIR, DOMAIN) if not os.path.exists(SAVEDIR): os.makedirs(SAVEDIR) SAVEFIG = SAVEDIR + '%s_f%02d' % (VALIDDATE.strftime('h%H'), fxx) plt.savefig(SAVEFIG) # Plot standard output. print 'SAVED:', SAVEFIG
def get_hrrr_variable(DATE, variable, fxx=0, model='hrrr', field='sfc', removeFile=True, value_only=False, verbose=True, outDIR='./'): """ Uses cURL to grab the requested variable from a HRRR grib2 file in the HRRR archive. Uses the the requested variable string to search the .idx file and determine the byte range. When the byte range of a variable is known, cURL is capable of downloading a single variable from a larger GRIB2 file. This function packages the data in a dictionary. Input: DATE - The datetime(year, month, day, hour) for the HRRR file you want. This is the same as the model run time, in UTC. variable - A string describing the variable you are looking for in the GRIB2 file. Refer to the .idx files. For example: https://pando-rgw01.chpc.utah.edu/hrrr/sfc/20180101/hrrr.t00z.wrfsfcf00.grib2.idx You want to put the variable short name and the level information. For example, for 2m temperature: variable='TMP:2 m above ground' fxx - The forecast hour you desire. Default is the analysis hour, or f00. model - The model you want. Options include ['hrrr', 'hrrrX', 'hrrrak'] field - The file output type. Options include ['sfc', 'prs'] removeFile - True: remove the GRIB2 file after it is downloaded False: do not remove the GRIB2 file after it is downloaded value_only - True: only return the values, not the lat/lon. Returns output in 0.2 seconds False: returns value and lat/lon, grib message, analysis and valid datetime. Returns output in 0.75-1 seconds verbose - Prints some diagnostics outDIR - Specify where the downloaded data should be downloaded. Default is the current directory. Tips: 1. The DATE you request represents the model run time. If you want to retrieve the file based on the model's valid time, you need to offset the DATE with the forecast lead time. For example: VALID_DATE = datetime(year, month, day, hour) # We want the model data valid at this time fxx = 15 # Forecast lead time RUN_DATE = VALID_DATE-timedelta(hours=fxx) # The model run datetime that produced the data get_hrrr_variable(RUN_DATE, 'TMP:2 m', fxx=fxx) # The returned data will be a forecast for the requested valid time and lead time 2. You can request both U and V components at a level by using variable='UVGRD:10 m' This special request will return the U and V component winds converted from grid-relative to earth-relative, as well as the calculated wind speed. Note: You can still get the grid-relative winds by requesting both 'UGRD:10 m' and 'VGRD:10 m' individually. """ ## --- Catch Errors ------------------------------------------------------- # Check that you requested the right model name and field name if model not in ['hrrr', 'hrrrX', 'hrrrak']: raise ValueError( "Requested model must be 'hrrr', 'hrrrX', or 'hrrrak'") if field not in ['prs', 'sfc']: raise ValueError( "Requested field must be 'prs' or 'sfc'. We do not store other fields in the archive" ) # Check that you requested the right forecasts available for the model if model == 'hrrr' and fxx not in range(37): raise ValueError( "HRRR: fxx must be between 0 and 18\nYou requested f%02d" % fxx) elif model == 'hrrrX' and fxx != 0: raise ValueError( "HRRRx: fxx must be 0. We do not store other forecasts in the archive.\nYou requested f%02d" % fxx) elif model == 'hrrrak' and fxx not in range(37): raise ValueError( "HRRRak: fxx must be between 0 and 37\nYou requested f%02d" % fxx) # Check that the requested hour exists for the model if model == 'hrrrak' and DATE.hour not in range(0, 24, 3): raise ValueError( "HRRRak: DATE.hour must be 0, 3, 6, 9, 12, 15, 18, or 21\nYou requested %s" % DATE.hour) if verbose: # Check that the request datetime has happened if DATE > datetime.utcnow(): print "Warning: The datetime you requested hasn't happened yet\nDATE: %s F%02d\n UTC: %s" % ( DATE, fxx, datetime.utcnow()) ## ---(Catch Errors)------------------------------------------------------- ## --- Set Temporary File Name -------------------------------------------- # Temporary file name has to be unique, or else when we use multiprocessing # we might accidentally delete files before we are done with them. outfile = '%stemp_%s_%s_f%02d_%s.grib2' % (outDIR, model, DATE.strftime('%Y%m%d%H'), fxx, variable[:3].replace(":", '')) if verbose is True: print ' >> Dowloading tempfile: %s' % outfile ## --- Requested Variable ------------------------------------------------- # A special variable request is 'UVGRD:[level]' which will get both the U # and V wind components converted to earth-relative direction in a single # download. Since UGRD always proceeds VGRD, we will set the get_variable # as UGRD. Else, set get_variable as variable. if variable.split(':')[0] == 'UVGRD': # We need both U and V to convert winds from grid-relative to earth-relative get_variable = 'UGRD:' + variable.split(':')[1] else: get_variable = variable ## --- Set Data Source ---------------------------------------------------- """ Dear User, Only HRRR files are only downloaded and added to Pando every 3 hours. That means if you are requesting data for today that hasn't been copied to Pando yet, you will need to get it from the NOMADS website instead. But good news! It's an easy fix. All we need to do is redirect you to the NOMADS server. I'll check that the date you are requesting is not for today's date. If it is, then I'll send you to NOMADS. Deal? :) -Sincerely, Brian """ # If the datetime requested is less than six hours ago, then the file is # most likely on Pando. Else, download from NOMADS. #if DATE+timedelta(hours=fxx) < datetime.utcnow()-timedelta(hours=6): if DATE < datetime.utcnow() - timedelta(hours=12): # Get HRRR from Pando if verbose: print "Oh, good, you requested a date that should be on Pando." grib2file = 'https://pando-rgw01.chpc.utah.edu/%s/%s/%s/%s.t%02dz.wrf%sf%02d.grib2' \ % (model, field, DATE.strftime('%Y%m%d'), model, DATE.hour, field, fxx) fileidx = grib2file + '.idx' else: # Get operational HRRR from NOMADS if model == 'hrrr': if verbose: print "/n---------------------------------------------------------------------------" print "!! Hey! You are requesting a date that is not on the Pando archive yet. !!" print "!! That's ok, I'll redirect you to the NOMADS server. :) !!" print "---------------------------------------------------------------------------\n" #grib2file = 'https://nomads.ncep.noaa.gov/pub/data/nccf/com/hrrr/prod/hrrr.%s/%s.t%02dz.wrf%sf%02d.grib2' \ # % (DATE.strftime('%Y%m%d'), model, DATE.hour, field, fxx) grib2file = 'https://nomads.ncep.noaa.gov/pub/data/nccf/com/hrrr/prod/hrrr.%s/conus/hrrr.t%02dz.wrf%sf%02d.grib2' \ % (DATE.strftime('%Y%m%d'), DATE.hour, field, fxx) fileidx = grib2file + '.idx' elif model == 'hrrrX': print "\n-------------------------------------------------------------------------" print "!! Sorry, I haven't download that Experimental HRRR run from ESRL yet !!" print "!! Try again in a few hours. !!" print "-------------------------------------------------------------------------\n" return None elif model == 'hrrrak': if verbose: print "/n---------------------------------------------------------------------------" print "!! Hey! You are requesting a date that is not on the Pando archive yet. !!" print "!! That's ok, I'll redirect you to the PARALLEL NOMADS server. :) !!" print "---------------------------------------------------------------------------\n" grib2file = 'https://nomads.ncep.noaa.gov/pub/data/nccf/com/hrrr/prod/hrrr.%s/alaska/hrrr.t%02dz.wrf%sf%02d.ak.grib2' \ % (DATE.strftime('%Y%m%d'), DATE.hour, field, fxx) fileidx = grib2file + '.idx' if verbose: print 'GRIB2 File: %s' % grib2file print ' .idx File: %s' % fileidx print "" ## --- Download Requested Variable ---------------------------------------- try: ## 0) Read the grib2.idx file try: # ?? Ignore ssl certificate (else urllib2.openurl wont work). # Depends on your version of python. # See here: # http://stackoverflow.com/questions/19268548/python-ignore-certicate-validation-urllib2 ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE idxpage = urllib2.urlopen(fileidx, context=ctx) except: idxpage = urllib2.urlopen(fileidx) lines = idxpage.readlines() ## 1) Find the byte range for the requested variable. First find where # in the .idx file the variable is located. We need the byte number # The variable begins on. Keep a count (gcnt) of the line number so # we can also get the beginning byte of the next variable. This is # our byte range. gcnt = 0 for g in lines: expr = re.compile(get_variable) if expr.search(g): if verbose is True: print ' >> Matched a variable: ', g parts = g.split(':') rangestart = parts[1] if variable.split(':')[0] == 'UVGRD': parts = lines[gcnt + 2].split( ':') # Grab range between U and V variables else: parts = lines[gcnt + 1].split( ':') # Grab range for requested variable only rangeend = int(parts[1]) - 1 if verbose is True: print ' >> Byte Range:', rangestart, rangeend byte_range = str(rangestart) + '-' + str(rangeend) gcnt += 1 ## 2) When the byte range is discovered, use cURL to download the file. os.system('curl -s -o %s --range %s %s' % (outfile, byte_range, grib2file)) ## --- Convert winds to earth-relative -------------------------------- # If the requested variable is 'UVGRD:[level]', then we have to change # the wind direction from grid-relative to earth-relative. # You can still get the grid-relative winds by requesting 'UGRD:[level]' # and # 'VGRD:[level] independently. # !!! See more information on why/how to do this here: # https://github.com/blaylockbk/pyBKB_v2/blob/master/demos/HRRR_earthRelative_vs_gridRelative_winds.ipynb if variable.split(':')[0] == 'UVGRD': if verbose: print ' >> Converting winds to earth-relative' wgrib2 = '/uufs/chpc.utah.edu/sys/installdir/wgrib2/2.0.2/wgrib2/wgrib2' if model == 'hrrrak': regrid = 'nps:225.000000:60.000000 185.117126:1299:3000.000000 41.612949:919:3000.000000' if model == 'hrrr' or model == 'hrrrX': regrid = 'lambert:262.500000:38.500000:38.500000:38.500000 237.280472:1799:3000.000000 21.138123:1059:3000.000000' os.system('%s %s -new_grid_winds earth -new_grid %s %s.earth' % (wgrib2, outfile, regrid, outfile)) os.system('rm -f %s' % outfile) # remove the original file outfile = outfile + '.earth' # assign the `outfile`` as the regridded file ## 3) Get data from the file, using pygrib and return what we want to use grbs = pygrib.open(outfile) # Note: Returning only the variable value is a bit faster than returning # the variable value with the lat/lon and other details. You can # specify this when you call the function. if value_only: if variable.split(':')[0] == 'UVGRD': return_this = { 'UGRD': grbs[1].values, 'VGRD': grbs[2].values, 'SPEED': wind_uv_to_spd(grbs[1].values, grbs[2].values) } else: return_this = {'value': grbs[1].values} if removeFile: os.system('rm -f %s' % (outfile)) return return_this else: if variable.split(':')[0] == 'UVGRD': value1, lat, lon = grbs[1].data() if model == 'hrrrak': lon[lon > 0] -= 360 return_this = { 'UGRD': value1, 'VGRD': grbs[2].values, 'SPEED': wind_uv_to_spd(value1, grbs[2].values), 'lat': lat, 'lon': lon, 'valid': grbs[1].validDate, 'anlys': grbs[1].analDate, 'msg': [str(grbs[1]), str(grbs[2])], 'name': [grbs[1].name, grbs[2].name], 'units': [grbs[1].units, grbs[2].units], 'level': [grbs[1].level, grbs[2].level], 'URL': grib2file } else: value, lat, lon = grbs[1].data() if model == 'hrrrak': lon[lon > 0] -= 360 return_this = { 'value': value, 'lat': lat, 'lon': lon, 'valid': grbs[1].validDate, 'anlys': grbs[1].analDate, 'msg': str(grbs[1]), 'name': grbs[1].name, 'units': grbs[1].units, 'level': grbs[1].level, 'URL': grib2file } if removeFile: os.system('rm -f %s' % (outfile)) return return_this except: if verbose: print " _______________________________________________________________" print " !! Run Date Requested :", DATE, "F%02d" % fxx print " !! Valid Date Requested :", DATE + timedelta(hours=fxx) print " !! Current UTC time :", datetime.utcnow() print " !! ------------------------------------------------------------" print " !! ERROR downloading GRIB2:", grib2file print " !! Is the variable right?", variable print " !! Does the .idx file exist?", fileidx print " ---------------------------------------------------------------" return { 'value': np.nan, 'lat': np.nan, 'lon': np.nan, 'valid': np.nan, 'anlys': np.nan, 'msg': np.nan, 'URL': grib2file }
# Convert the units of each Pollywog for loc in location.keys(): # Convert Units for the variables in the Pollywog TS_temp[loc] = KtoF(TS_temp[loc]) TS_dwpt[loc] = KtoF(TS_dwpt[loc]) TS_wind[loc] = mps_to_MPH(TS_wind[loc]) TS_gust[loc] = mps_to_MPH(TS_gust[loc]) TS_u[loc] = mps_to_MPH(TS_u[loc]) TS_v[loc] = mps_to_MPH(TS_v[loc]) TS_u80[loc] = mps_to_MPH(TS_u80[loc]) TS_v80[loc] = mps_to_MPH(TS_v80[loc]) #TS_prec[loc] = mm_to_inches(TS_prec[loc]) # Derive some variables: TS_wind80[loc] = wind_uv_to_spd(TS_u80[loc], TS_v80[loc]) # Just need one vector of valid dates TS_dates = np.array(TS_temp['DATETIME']) # Make a dictionary of map object for each location. # (This speeds up plotting by creating each map once.) maps = {} for loc in location.keys(): l = location[loc] m = Basemap(resolution='i', projection='cyl',\ llcrnrlon=l['longitude']-.1, llcrnrlat=l['latitude']-.1,\ urcrnrlon=l['longitude']+.1, urcrnrlat=l['latitude']+.1,) maps[loc] = m for hh in range(len(TS_dates)):
def make_plots(inputs): VALID_DATE, fxx = inputs print 'working on %s f%02d' % (VALID_DATE, fxx) plt.clf(); plt.cla() print fxx, VALID_DATE # === Some housekeeping variables ============================================= # Convert Valid Date to Run Date, adjusted by the forecast DATE = VALID_DATE - timedelta(hours=fxx) # Parse Location lat/lon if ',' in location: # User put inputted a lat/lon point request lat, lon = location.split(',') lat = float(lat) lon = float(lon) else: # User requested a MesoWest station stninfo = get_station_info([location]) lat = stninfo['LAT'] lon = stninfo['LON'] # Preload the latitude and longitude grid latlonpath = '/uufs/chpc.utah.edu/common/home/horel-group/archive/HRRR/oper_HRRR_latlon.h5' latlonh5 = h5py.File(latlonpath, 'r') gridlat = latlonh5['latitude'][:] gridlon = latlonh5['longitude'][:] # === Create map of the domain ================================================ if dsize == 'conus' and model != 'hrrrAK': barb_thin = 70 alpha = 1 t1= datetime.now() #m = draw_CONUS_HRRR_map(res=map_res) m = np.load('/uufs/chpc.utah.edu/common/home/u0553130/public_html/Brian_Blaylock/cgi-bin/HRRR_CONUS_map_object_'+map_res+'.npy').item() m.drawcountries(zorder=500) m.drawstates(zorder=500) m.drawcoastlines(zorder=500) m.fillcontinents(color='tan',lake_color='lightblue', zorder=0) m.drawmapboundary(fill_color='lightblue') t2 = datetime.now() else: # configure some setting based on the requested domain size if dsize == 'small': plus_minus_latlon = .27 # +/- latlon box around center point barb_thin = 1 # Thin out excessive wind barbs arcgis_res = 1000 # ArcGIS image resolution bfr = 15 # trim domain buffer alpha = .75 # Alpha (pcolormesh transparency) elif dsize == 'medium': plus_minus_latlon = .75 barb_thin = 2 arcgis_res = 800 bfr = 35 alpha = .75 elif dsize == 'large': plus_minus_latlon = 2.5 barb_thin = 6 arcgis_res = 800 bfr = 110 alpha = .75 elif dsize == 'xlarge': plus_minus_latlon = 5 barb_thin = 12 arcgis_res = 700 bfr = 210 alpha = .75 elif dsize == 'xxlarge': # If domain runs into HRRR boundary, then it'll fail plus_minus_latlon = 10 barb_thin = 25 arcgis_res = 700 bfr = 430 alpha = .75 elif dsize == 'xxxlarge': plus_minus_latlon = 15 barb_thin = 35 arcgis_res = 1000 bfr = 700 alpha = .75 m = Basemap(resolution=map_res, projection='cyl',\ area_thresh=3000,\ llcrnrlon=lon-plus_minus_latlon, llcrnrlat=lat-plus_minus_latlon,\ urcrnrlon=lon+plus_minus_latlon, urcrnrlat=lat+plus_minus_latlon,) m.drawstates(zorder=500) m.drawcountries(zorder=500) m.drawcoastlines(zorder=500) #if dsize == 'small' or dsize == 'medium': # m.drawcounties() # === Add a Background image ================================================== # Start the map image plt.figure(1) if background == 'arcgis' and dsize != 'conus': m.arcgisimage(service='World_Shaded_Relief', xpixels=arcgis_res, verbose=False) elif background == 'arcgisSat' and dsize != 'conus': m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=arcgis_res, verbose=False) elif background == 'arcgisRoad' and dsize != 'conus': m.arcgisimage(service='NatGeo_World_Map', xpixels=arcgis_res, verbose=False) elif background == 'terrain': # Get data H_ter = get_hrrr_variable(DATE, 'HGT:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_land = get_hrrr_variable(DATE, 'LAND:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Plot the terrain m.contourf(gridlon, gridlat, H_ter['value'], levels=range(0, 4000, 200), cmap='Greys_r', zorder=1, latlon=True) # Plot Water area m.contour(gridlon, gridlat, H_land['value'], levels=[0, 1], colors='b', zorder=1, latlon=True) elif background == 'landuse': # Get data from BB_cmap.landuse_colormap import LU_MODIS21 if model=='hrrr': VGTYP = 'VGTYP:surface' else: VGTYP = 'var discipline=2 center=59 local_table=1 parmcat=0 parm=198' H_LU = get_hrrr_variable(DATE, VGTYP, model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Plot the terrain cm, labels = LU_MODIS21() m.pcolormesh(gridlon, gridlat, H_LU['value'], cmap=cm, vmin=1, vmax=len(labels) + 1, zorder=1, latlon=True) # Add SHAPEFILE if is_FIRE: plt.gca().add_collection(PatchCollection(patches, facecolor='indianred', alpha=.65, edgecolor='k', linewidths=1, zorder=1)) # === Figure Title ============================================================ if dsize != 'conus': m.scatter(lon, lat, marker='+', c='r', s=100, zorder=1000, latlon=True) plt.title('Center: %s\n%s' % (location, model.upper()), fontweight='bold') else: plt.title('%s' % (model.upper()), fontweight='bold') plt.title('Run: %s F%02d' % (DATE.strftime('%Y-%m-%d %H:%M UTC'), fxx), loc='left') plt.title('Valid: %s' % (DATE+timedelta(hours=fxx)).strftime('%Y-%m-%d %H:%M UTC') , loc='right') # ============================================================================= if '10mWind_Fill' in plotcode or '10mWind_Shade' in plotcode or '10mWind_Barb' in plotcode: # Get data H_u = get_hrrr_variable(DATE, 'UGRD:10 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_v = get_hrrr_variable(DATE, 'VGRD:10 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) spd = wind_uv_to_spd(H_u['value'], H_v['value']) if '10mWind_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap='magma_r', vmin=0, alpha=alpha) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'10 m Wind Speed (m s$\mathregular{^{-1}}$)') if '10mWind_Shade' in plotcode: m.contourf(gridlon, gridlat, spd, levels=[10, 15, 20, 25], colors=('yellow', 'orange', 'red'), alpha=alpha, extend='max', zorder=1, latlon=True) cb = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb.set_label(r'10 m Wind Speed (ms$\mathregular{^{-1}}$)') if '10mWind_Barb' in plotcode: # For small domain plots, trimming the edges significantly reduces barb plotting time if barb_thin < 20: cut_v, cut_h = pluck_point_new(lat, lon, gridlat, gridlon) Cgridlat = gridlat[cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] Cgridlon = gridlon[cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] H_u['value'] = H_u['value'][cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] H_v['value'] = H_v['value'][cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] else: Cgridlat = gridlat Cgridlon = gridlon # Add to plot thin = barb_thin m.barbs(Cgridlon[::thin,::thin], Cgridlat[::thin,::thin], H_u['value'][::thin,::thin], H_v['value'][::thin,::thin], zorder=200, length=5.5, barb_increments={'half':2.5, 'full':5,'flag':25}, latlon=True) if '80mWind_Fill' in plotcode or '80mWind_Shade' in plotcode or '80mWind_Barb' in plotcode: # Get data H_u = get_hrrr_variable(DATE, 'UGRD:80 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_v = get_hrrr_variable(DATE, 'VGRD:80 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) spd = wind_uv_to_spd(H_u['value'], H_v['value']) if '80mWind_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap='magma_r', vmin=0, alpha=alpha) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'10 m Wind Speed (m s$\mathregular{^{-1}}$)') if '80mWind_Shade' in plotcode: m.contourf(gridlon, gridlat, spd, levels=[10, 15, 20, 25], colors=('yellow', 'orange', 'red'), alpha=alpha, extend='max', zorder=10, latlon=True) cb = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb.set_label(r'10 m Wind Speed (ms$\mathregular{^{-1}}$)') if '80mWind_Barb' in plotcode: # For small domain plots, trimming the edges significantly reduces barb plotting time if barb_thin < 20: cut_v, cut_h = pluck_point_new(lat, lon, gridlat, gridlon) Cgridlat = gridlat[cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] Cgridlon = gridlon[cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] H_u['value'] = H_u['value'][cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] H_v['value'] = H_v['value'][cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] else: Cgridlat = gridlat Cgridlon = gridlon # Add to plot thin = barb_thin m.barbs(Cgridlon[::thin,::thin], Cgridlat[::thin,::thin], H_u['value'][::thin,::thin], H_v['value'][::thin,::thin], zorder=200, length=5.5, color='darkred', barb_increments={'half':2.5, 'full':5,'flag':25}, latlon=True) if 'Fill80mWind' in plotcode: m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap='plasma_r', vmin=0, alpha=alpha) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'80 m Wind Speed (m s$\mathregular{^{-1}}$)') if 'Gust_Hatch' in plotcode: H_gust = get_hrrr_variable(DATE, 'GUST:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Add to plot m.contourf(gridlon, gridlat, H_gust['value'], levels=[0, 10, 15, 20, 25], hatches=[None, '.', '\\\\', '*'], colors='none', extend='max', zorder=10, latlon=True) cb = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb.set_label(r'Surface Wind Gust (ms$\mathregular{^{-1}}$)') m.contour(gridlon, gridlat, H_gust['value'], levels=[10, 15, 20, 25], colors='k', zorder=10, latlon=True) if 'dBZ_Fill' in plotcode or 'dBZ_Contour' in plotcode: from BB_cmap.reflectivity_colormap import reflect_ncdc # Get Data if model == 'hrrr': REFC = 'REFC:entire' elif model == 'hrrrX' or model == 'hrrrAK': REFC = 'var discipline=0 center=59 local_table=1 parmcat=16 parm=196' H_ref = get_hrrr_variable(DATE, REFC, model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values dBZ = H_ref['value'] dBZ = np.ma.array(dBZ) dBZ[dBZ == -10] = np.ma.masked # Add Contour to plot if 'dBZ_Contour' in plotcode: cREF = m.contour(gridlon, gridlat, dBZ, cmap=reflect_ncdc(), levels=range(10, 80, 10), latlon=True, zorder=50) plt.clabel(cREF, cREF.levels[::2], fmt='%2.0f', colors='k', fontsize=9) #cb2 = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) #cb2.set_label('Simulated Composite Reflectivity (dBZ)') # Add fill to plot if 'dBZ_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, dBZ, cmap=reflect_ncdc(), vmax=80, vmin=0, alpha=alpha, latlon=True) cb2 = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cb2.set_label('Simulated Composite Reflectivity (dBZ)') if '2mTemp_Fill' in plotcode or '2mTemp_Freeze' in plotcode: # Get Data H_temp = get_hrrr_variable(DATE, 'TMP:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value']-273.15 # Add fill to plot if '2mTemp_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, TMP, cmap="Spectral_r", alpha=alpha, zorder=3, latlon=True) cbT = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbT.set_label('2m Temperature (C)') # Add freezing contour to plot if '2mTemp_Freeze' in plotcode: m.contour(gridlon, gridlat, TMP, colors='b', levels=[0], zorder=400, latlon=True) if '2mRH_Fill' in plotcode: # Get Data try: H_RH = get_hrrr_variable(DATE, 'RH:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Add fill to plot m.pcolormesh(gridlon, gridlat, H_RH['value'], cmap="BrBG", vmin=0, vmax=100, zorder=3, latlon=True) cbT = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cbT.set_label('2m Relative Humidity (%)') except: print "!! Some errors getting the RH value." print "!! If you requested an old date, from HRRR version 1, there isn't a RH variable," print "!! and this code doesn't get the dwpt and convert it to RH yet." if '700Temp_Fill' in plotcode or '700Temp_-12c' in plotcode: # Get Data H_temp = get_hrrr_variable(DATE, 'TMP:700 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value']-273.15 # Add fill to plot if '700Temp_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, TMP, cmap="Spectral_r", alpha=alpha, zorder=3, latlon=True) cbT = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbT.set_label('700 mb Temperature (C)') # Add -12 C contour to plot if '700Temp_-12c' in plotcode: m.contour(gridlon, gridlat, TMP, colors='b', levels=[-12], latlon=True, zorder=400) if '500HGT_Contour' in plotcode: H_500 = get_hrrr_variable(DATE, 'HGT:500 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) CS = m.contour(gridlon, gridlat, H_500['value'], levels=range(5040, 6181, 60), linewidths=1.7, colors='k', latlon=True, zorder=400) plt.clabel(CS, inline=1, fmt='%2.f') if '500Wind_Fill' in plotcode or '500Wind_Barb' in plotcode or '500Vort_Fill' in plotcode or '500Conv_Fill' in plotcode: H_u = get_hrrr_variable(DATE, 'UGRD:500 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) H_v = get_hrrr_variable(DATE, 'VGRD:500 mb', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) if '500Wind_Fill' in plotcode: spd = wind_uv_to_spd(H_u['value'], H_v['value']) m.pcolormesh(gridlon, gridlat, spd, latlon=True, cmap='BuPu', vmin=0) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'500 mb Wind Speed (m s$\mathregular{^{-1}}$)') if '500Conv_Fill' in plotcode or '500Vort_Fill' in plotcode: dudx, dudy = np.gradient(H_u['value'], 3, 3) dvdx, dvdy = np.gradient(H_v['value'], 3, 3) if '500Vort_Fill' in plotcode: vorticity = dvdx - dudy # Mask values vort = vorticity vort = np.ma.array(vort) vort[np.logical_and(vort < .05, vort > -.05) ] = np.ma.masked m.pcolormesh(gridlon, gridlat, vort, latlon=True, cmap='bwr', vmax=np.max(vort), vmin=-np.max(vort)) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'500 mb Vorticity (s$\mathregular{^{-1}}$)') if '500Conv_Fill' in plotcode: convergence = dudx + dvdy # Mask values conv = convergence conv = np.ma.array(conv) conv[np.logical_and(conv < .05, conv > -.05) ] = np.ma.masked m.pcolormesh(gridlon, gridlat, conv, latlon=True, cmap='bwr', vmax=np.max(conv), vmin=-np.max(conv)) cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label(r'500 mb Convergence (s$\mathregular{^{-1}}$)') if '500Wind_Barb' in plotcode: # For small domain plots, trimming the edges significantly reduces barb plotting time if barb_thin < 20: cut_v, cut_h = pluck_point_new(lat, lon, gridlat, gridlon) Cgridlat = gridlat[cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] Cgridlon = gridlon[cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] H_u['value'] = H_u['value'][cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] H_v['value'] = H_v['value'][cut_v-bfr:cut_v+bfr, cut_h-bfr:cut_h+bfr] else: Cgridlat = gridlat Cgridlon = gridlon thin = barb_thin m.barbs(Cgridlon[::thin, ::thin], Cgridlat[::thin, ::thin], H_u['value'][::thin, ::thin], H_v['value'][::thin, ::thin], zorder=200, length=6, color='navy', barb_increments={'half':2.5, 'full':5,'flag':25}, latlon=True) #plt.ylabel(r'Barbs: half=2.5, full=5, flag=25 (ms$\mathregular{^{-1}}$)') if 'MSLP_Contour' in plotcode or 'MSLP_Fill' in plotcode: H = get_hrrr_variable(DATE, 'MSLMA:mean sea level', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) if 'MSLP_Contour' in plotcode: CS = m.contour(gridlon, gridlat, H['value']/100., latlon=True, levels=range(952, 1200, 4), colors='k', zorder=400) CS.clabel(inline=1, fmt='%2.f', zorder=400) if 'MSLP_Fill' in plotcode: m.pcolormesh(gridlon, gridlat, H['value']/100., latlon=True, cmap='viridis') cb = plt.colorbar(orientation='horizontal', pad=pad, shrink=shrink) cb.set_label('Mean Sea Level Pressure (hPa)') if '2mPOT_Fill' in plotcode: # Get Data H_temp = get_hrrr_variable(DATE, 'POT:2 m', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value']-273.15 m.pcolormesh(gridlon, gridlat, TMP, cmap="Oranges", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('2 m Potential Temperature (C)') if 'SkinTemp_Fill' in plotcode: # Get Data H_temp = get_hrrr_variable(DATE, 'TMP:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) TMP = H_temp['value']-273.15 m.pcolormesh(gridlon, gridlat, TMP, cmap="Spectral_r", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('Skin Temperature (C)') if 'AccumPrecip_Fill' in plotcode or '1hrPrecip_Fill' in plotcode: #import matplotlib.colors #cmap = matplotlib.colors.LinearSegmentedColormap.from_list("Precip", ["#00db16", "blue", "#d10000", 'black']) cdict3 = {'red': ((0.0, 0.0, 0.0), (0.25, 1.0, 1.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)), 'green': ((0.0, 0.7, 0.7), (0.25, 1.0, 1.0), (0.5, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.18, 0.18), (0.25, 1.0, 1.0), (0.5, 1.0, 1.0), (1.0, 0.0, 0.0)) } plt.register_cmap(name='BlueRed3', data=cdict3) cmap = 'BlueRed3' if 'AccumPrecip_Fill' in plotcode: # Get Data H = get_hrrr_variable(DATE, 'APCP:surface:0', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values prec = H['value'] prec = np.ma.array(prec) prec[prec == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, prec, cmap='BlueRed3', alpha=alpha, vmin=.25, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('Accumulated Precipitation since F00 (mm)') if '1hrPrecip_Fill' in plotcode: # Get Data H = get_hrrr_variable(DATE, 'APCP:surface', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values prec = H['value'] prec = np.ma.array(prec) prec[prec == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, prec, cmap='BlueRed3', alpha=alpha, vmin=.25, vmax=20, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad, extend="max",) cbS.set_label('1 hour Accumulated Precipitation (mm)') if 'SnowCover_Fill' in plotcode: # Get Data H = get_hrrr_variable(DATE, 'SNOWC', model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) # Mask values snow = H['value'] snow = np.ma.array(snow) snow[snow == 0] = np.ma.masked m.pcolormesh(gridlon, gridlat, snow, cmap="Blues", alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label('Snow Cover (%)') # ============================================================================= # Hack! Plot an extra HRRR variable not listed on the webpage hrrr_custom.html # This extra argument will let you attempt to plot a different variable for # a quicklook. try: # Must be a variable from a line in the .idx file hrrrVAR = form['extraVAR'].value extraHRRR = get_hrrr_variable(DATE, hrrrVAR, model=model, fxx=fxx, outDIR='/uufs/chpc.utah.edu/common/home/u0553130/temp/', verbose=False, value_only=True) m.pcolormesh(gridlon, gridlat, extraHRRR['value'], cmap='viridis', alpha=alpha, zorder=3, latlon=True) cbS = plt.colorbar(orientation='horizontal', shrink=shrink, pad=pad) cbS.set_label(hrrrVAR+' (units)') except: pass # ============================================================================= #plt.ylabel('Section Timer:%s\nFull Timer:%s' % (t2-t1, datetime.now()-firsttimer)) SAVEDIR = '/uufs/chpc.utah.edu/common/home/u0553130/public_html/PhD/HRRR/Events_Day/%s/' % EVENT if not os.path.exists(SAVEDIR): os.makedirs(SAVEDIR) SAVEFIG = SAVEDIR + '%s_f%02d' % (VALID_DATE.strftime('%Y-%m-%d_h%H'), fxx) plt.savefig(SAVEFIG) # Plot standard output. print 'SAVED:', SAVEFIG
def plot_for_each_fxx(f): ## HRRR Model Run date RUN = D-timedelta(hours=f) ## Get HRRR data for this run if var.split(':')[0] == 'UVGRD': # Calculate wind speed from U and V components level = var.split(':')[1] HU = get_hrrr_variable(RUN, 'UGRD:'+level, fxx=f, value_only=True, verbose=False) HV = get_hrrr_variable(RUN, 'VGRD:'+level, fxx=f, value_only=True, verbose=False) speed = wind_uv_to_spd(HU['value'], HV['value']) H = HU H['value'] = speed else: H = get_hrrr_variable(RUN, var, fxx=f, value_only=True, verbose=False) ## Convert units and get values in bounding box area if var == 'TMP:2 m' or var == 'DPT:2 m': Hpoint = H['value'][x,y]-273.15 Harea = H['value'][x-box_radius:x+box_radius+1,y-box_radius:y+box_radius+1]-273.15 else: Hpoint = H['value'][x,y] Harea = H['value'][x-box_radius:x+box_radius+1,y-box_radius:y+box_radius+1] ## Calculate the HRRR percentiles in the boxed area for this run HP = np.percentile(Harea, [0,25,50,75,100]) ## --- Plot OSG percentiles --------------------------------------- plt.fill_between(percentiles, area_P[0][0], area_P[0][4], color='lightgrey', label='p100-p00', zorder=1) plt.fill_between(percentiles, area_P[0][1], area_P[0][3], color='grey', label='p25-p75', zorder=1) plt.plot(percentiles, area_P[0][2], color='lightgrey', label="p50", zorder=1) ## Plot all percentiles for each grid box #for i in range(box_radius*2+1): # for j in range(box_radius*2+1): # plt.plot(percentiles, PP[i,j,:]) ## Plot OSG percentiles for the single grid box nearest the station of interest plt.plot(percentiles, PP[box_radius,box_radius,:], linestyle='--', color='k', label='%s OSG Percentiles' % stn, zorder=1) ## --- Plot RUN percentiles ------------------------------------- # Linear interpolation to find the percentile for the value for i in range(5): p100Hpoint = HP[i] p100PP = area_P[0][i] if p100Hpoint >= np.max(p100PP): p100Y = p100Hpoint p100X = 100 print "Hpoint > PP, exceeds p100", D, f elif p100Hpoint <= np.min(p100PP): p100Y = p100Hpoint p100X = 0 print "Hpoint < PP exceeds p00", D, f, else: p100y1 = np.min(filter(lambda x: x>=p100Hpoint, p100PP)) p100y1_idx = np.where(p100PP==p100y1)[0][0] p100x1 = percentiles[p100y1_idx] p100y2 = p100PP[p100y1_idx-1] p100x2 = percentiles[p100y1_idx-1] p100m = (p100y2-p100y1)/(p100x2-p100x1) p100b = 0-p100m*p100x1+p100y1 p100Y = p100Hpoint p100X = (p100Y-p100b)/p100m plt.axhline(p100Hpoint, xmax=p100X/100, color='r', linewidth=1, zorder=50) plt.axvline(p100X, ymax=(p100Hpoint-ymin)/(ymax-ymin), color='r', linewidth=1, zorder=50) plt.scatter(p100X, p100Hpoint, s=25, color='r', zorder=50) ## Plot value from current HRRR run for every grid box #for i in range(box_radius*2+1): # for j in range(box_radius*2+1): # plt.axhline(Harea[i,j], linewidth=.1) ## Plot HRRR value for stn grid box for urrent HRRR run # Linear interpolation to find the percentile for the value stnPP = PP[box_radius,box_radius,:] if Hpoint >= np.max(stnPP): Y = Hpoint X = 100 print "station Hpoint > PP, exceeds p100", D, f elif Hpoint <= np.min(stnPP): Y = Hpoint X = 0 print "station Hpoint < PP, exceeds p00", D, f else: y1 = np.min(filter(lambda x: x>=Hpoint, stnPP)) y1_idx = np.where(stnPP==y1)[0][0] x1 = percentiles[y1_idx] y2 = stnPP[y1_idx-1] x2 = percentiles[y1_idx-1] m = (y2-y1)/(x2-x1) b = 0-m*x1+y1 Y = Hpoint X = (Y-b)/m plt.axhline(Hpoint, xmax=X/100, color='k', lw=6, zorder=100) plt.axvline(X, ymax=(Hpoint-ymin)/(ymax-ymin), color='k', lw=6, zorder=100) plt.scatter(X, Hpoint, s=150, color='k', zorder=100) ## --- Other Plot Elements -------------------------------------- plt.grid() plt.legend(loc='upper left') plt.xlabel('Percentiles\n%sx%s km box centered at %s' % (np.shape(PP)[0]*3, np.shape(PP)[0]*3, stn)) xtick_labels = [0,'','','','',5,10,25,33,50,66,75,90,95,'','','','',100] plt.xticks(percentiles, xtick_labels) plt.xlim([0,100]) plt.ylabel(var) plt.ylim([ymin, ymax]) plt.title('Valid: %s' % D.strftime('%Y-%b-%d %H:%M'), loc='Left') plt.title('%s\n%s' % (stn, var), loc='Center', fontweight='bold') plt.title('Run: %s F%02d' % (RUN.strftime("%Y-%b-%d %H:%M"), f), loc='right') plt.savefig(SAVEDIR+'%s_f%02d.png' % (D.strftime('%Y-%m-%d_%H'), f)) plt.close() return None
def plot_for_each_fxx_with_Map(f): fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[15,6]) plt.sca(ax1) ## HRRR Model Run date RUN = D-timedelta(hours=f) ## Get HRRR data for this run if var.split(':')[0] == 'UVGRD': # Calculate wind speed from U and V components level = var.split(':')[1] HU = get_hrrr_variable(RUN, 'UGRD:'+level, fxx=f, value_only=True, verbose=False) HV = get_hrrr_variable(RUN, 'VGRD:'+level, fxx=f, value_only=True, verbose=False) speed = wind_uv_to_spd(HU['value'], HV['value']) H = {'value': speed} Hpoint = speed[x,y] Harea = speed[x-box_radius:x+box_radius+1,y-box_radius:y+box_radius+1] HUarea = HU['value'][x-box_radius:x+box_radius+1,y-box_radius:y+box_radius+1] HVarea = HV['value'][x-box_radius:x+box_radius+1,y-box_radius:y+box_radius+1] else: H = get_hrrr_variable(RUN, var, fxx=f, value_only=True, verbose=False) ## Convert units and get values in bounding box area if var == 'TMP:2 m' or var == 'DPT:2 m': H['value'] = H['value']-273.15 Hpoint = H['value'][x,y] Harea = H['value'][x-box_radius:x+box_radius+1,y-box_radius:y+box_radius+1] else: Hpoint = H['value'][x,y] Harea = H['value'][x-box_radius:x+box_radius+1,y-box_radius:y+box_radius+1] ## Calculate the HRRR percentiles in the boxed area for this run HP = np.percentile(Harea, [0,25,50,75,100]) ## --- Plot OSG percentiles --------------------------------------- plt.fill_between(percentiles, area_P[0][0], area_P[0][4], color='lightgrey', label='p100-p00', zorder=1) plt.fill_between(percentiles, area_P[0][1], area_P[0][3], color='grey', label='p25-p75', zorder=1) plt.plot(percentiles, area_P[0][2], color='lightgrey', label="p50", zorder=1) ## Plot all percentiles for each grid box #for i in range(box_radius*2+1): # for j in range(box_radius*2+1): # plt.plot(percentiles, PP[i,j,:]) ## Plot OSG percentiles for the single grid box nearest the station of interest plt.plot(percentiles, PP[box_radius,box_radius,:], linestyle='--', color='k', label='%s OSG Percentiles' % stn, zorder=1) ## --- Plot RUN percentiles ------------------------------------- # Linear interpolation to find the percentile for the value for i in range(5): p100Hpoint = HP[i] p100PP = area_P[0][i] if p100Hpoint >= np.max(p100PP): p100Y = p100Hpoint p100X = 100 print "Hpoint > PP, exceeds p100", D, f elif p100Hpoint <= np.min(p100PP): p100Y = p100Hpoint p100X = 0 print "Hpoint < PP exceeds p00", D, f, else: p100y1 = np.min(filter(lambda x: x>=p100Hpoint, p100PP)) p100y1_idx = np.where(p100PP==p100y1)[0][0] p100x1 = percentiles[p100y1_idx] p100y2 = p100PP[p100y1_idx-1] p100x2 = percentiles[p100y1_idx-1] p100m = (p100y2-p100y1)/(p100x2-p100x1) p100b = 0-p100m*p100x1+p100y1 p100Y = p100Hpoint p100X = (p100Y-p100b)/p100m plt.axhline(p100Hpoint, xmax=p100X/100, color='r', linewidth=1, zorder=50) plt.axvline(p100X, ymax=(p100Hpoint-ymin)/(ymax-ymin), color='r', linewidth=1, zorder=50) plt.scatter(p100X, p100Hpoint, s=25, color='r', zorder=50) ## Plot value from current HRRR run for every grid box #for i in range(box_radius*2+1): # for j in range(box_radius*2+1): # plt.axhline(Harea[i,j], linewidth=.1) ## Plot HRRR value for stn grid box for urrent HRRR run # Linear interpolation to find the percentile for the value stnPP = PP[box_radius,box_radius,:] if Hpoint >= np.max(stnPP): Y = Hpoint X = 100 print "station Hpoint > PP, exceeds p100", D, f elif Hpoint <= np.min(stnPP): Y = Hpoint X = 0 print "station Hpoint < PP, exceeds p00", D, f else: y1 = np.min(filter(lambda x: x>=Hpoint, stnPP)) y1_idx = np.where(stnPP==y1)[0][0] x1 = percentiles[y1_idx] y2 = stnPP[y1_idx-1] x2 = percentiles[y1_idx-1] m = (y2-y1)/(x2-x1) b = 0-m*x1+y1 Y = Hpoint X = (Y-b)/m plt.axhline(Hpoint, xmax=X/100, color='k', lw=6, zorder=100) plt.axvline(X, ymax=(Hpoint-ymin)/(ymax-ymin), color='k', lw=6, zorder=100) plt.scatter(X, Hpoint, s=150, color='k', zorder=100) ## --- Other Plot Elements -------------------------------------- plt.grid() plt.legend(loc='upper left') plt.xlabel('Percentiles\n%sx%s km box centered at %s' % (np.shape(PP)[0]*3, np.shape(PP)[0]*3, stn)) xtick_labels = [0,'','','','',5,10,25,33,50,66,75,90,95,'','','','',100] plt.xticks(percentiles, xtick_labels) plt.xlim([0,100]) plt.ylabel(label_units) plt.ylim([ymin, ymax]) plt.title('Run: %s F%02d' % (RUN.strftime("%Y-%b-%d %H:%M"), f), loc='left') #plt.savefig(SAVEDIR+'%s_f%02d.png' % (D.strftime('%Y-%m-%d_%H'), f)) #plt.close() #####----- Side Map ------- plt.sca(ax2) am.drawcounties() am.arcgisimage(service='World_Shaded_Relief') if var == 'REFC:entire': dBZ = H['value'] dBZ = np.ma.array(dBZ) dBZ[dBZ == -10] = np.ma.masked vmax = 80 vmin = 0 H['value'] = dBZ elif var in ['UVGRD:10 m', 'UVGRD:80 m', 'GUST:surface']: vmax = np.max(Harea) vmin = 0 else: vmax = np.max(Harea) vmin = np.min(Harea) am.pcolormesh(lon, lat, H['value'], alpha=.3, cmap=cmap, vmax=vmax, vmin=vmin) cb = plt.colorbar(orientation='vertical', pad=0.01, shrink=0.85) cb.set_label(label_units) # Station Point am.scatter(MWlon, MWlat) # Bottom Boundary am.drawgreatcircle(LONS_BOX[0,-1], LATS_BOX[0,-1], LONS_BOX[0,0], LATS_BOX[0,0], color='k') # Left Boundary am.drawgreatcircle(LONS_BOX[0,0], LATS_BOX[0,0], LONS_BOX[-1,0], LATS_BOX[-1,0], color='k') # Top Boundary am.drawgreatcircle(LONS_BOX[-1,-1], LATS_BOX[-1,-1], LONS_BOX[-1,0], LATS_BOX[-1,0], color='k') # Right Boundary am.drawgreatcircle(LONS_BOX[-1,-1], LATS_BOX[-1,-1], LONS_BOX[0,-1], LATS_BOX[0,-1], color='k') # Barbs if var == 'UVGRD:10 m' or var == 'UVGRD:80 m': am.barbs(LONS_BOX, LATS_BOX, HUarea, HVarea, zorder=200, length=5, color='k', barb_increments={'half':2.5, 'full':5,'flag':25}, latlon=True) plt.title('Valid: %s' % D.strftime('%Y-%b-%d %H:%M'), loc='right') plt.suptitle('%s %s' % (stn, var)) plt.savefig(SAVEDIR+'h%02d_f%02d.png' % (D.hour, f)) plt.close() return None
ax3.scatter(P_wind['DATETIME'][fxx], P_wind[loc][fxx], c='darkorange', s=60) if l['is MesoWest'] is True: # we alreaded loaded mesowest data into a if a != 'ERROR': ax3.plot(a['DATETIME'], mps_to_MPH(a['wind_speed']), c='k', ls='--') # plt.barbs can not take a datetime object, so find the date indexes: idx = mpl.dates.date2num(P_u['DATETIME']) ax3.barbs(idx, wind_uv_to_spd(P_u[loc], P_v[loc]), P_u[loc], P_v[loc], length=6, barb_increments=dict(half=5, full=10, flag=50)) leg3 = ax3.legend() leg3.get_frame().set_linewidth(0) ax3.grid() #ax3.set_ylabel(r'Wind Speed (ms$\mathregular{^{-1}}$)') ax3.set_ylabel('Wind Speed (mph)') ax3.set_ylim([0, np.nanmax(P_gust[loc]) + 3]) ax3.set_yticks([0, np.nanmax(P_gust[loc]) + 3], 2.5) ax3.set_xlim([P_gust['DATETIME'][0], P_gust['DATETIME'][-1]]) ax3.xaxis.set_major_locator(mdates.HourLocator(range(0, 24, 3))) ax3.xaxis.set_minor_locator(mdates.HourLocator(range(0, 24, 1)))
uH = get_hrrr_variable(attime, variable='UGRD:10 m') vH = get_hrrr_variable(attime, variable='VGRD:10 m') uHX = get_hrrr_variable(attime, variable='UGRD:10 m', model='hrrrX', removeFile=True, value_only=True) vHX = get_hrrr_variable(attime, variable='VGRD:10 m', model='hrrrX', removeFile=True, value_only=True) H = { 'lat': uH['lat'], 'lon': uH['lon'], 'value': wind_uv_to_spd(uH['value'], vH['value']) } HX = {'value': wind_uv_to_spd(uHX['value'], vHX['value'])} else: H = get_hrrr_variable(attime, variable=variable) HX = get_hrrr_variable(attime, variable=variable, model='hrrrX', removeFile=True, value_only=True) Hx, Hy = m(H['lon'], H['lat']) HRRR_plot = ax1.pcolormesh(Hx, Hy, H['value'] - offset, vmin=vmin, vmax=vmax,
cpu_count = multiprocessing.cpu_count() # variable = 'UGRD:10 m' p = multiprocessing.Pool(cpu_count) U = p.map(get_HRRR_value, requestDATES) U = np.array([x for x in U if x is not None]) p.close() # variable = 'VGRD:10 m' p = multiprocessing.Pool(cpu_count) V = p.map(get_HRRR_value, requestDATES) V = np.array([x for x in V if x is not None]) p.close() # Convert U and V to Speed and Direction WSPD = wind_uv_to_spd(U, V) WDIR = wind_uv_to_dir(U, V) # get a single H dictionary (need the lat/lon arrays) H = get_hrrr_variable(requestDATES[0], 'UGRD:10 m') # Histogram bins num_dir_sectors = 8 # Number of directional sectors (sectors calculated in histogram function) speed_bins = np.arange(0, 10, 2) # Speed bins num_spd_bins = len(speed_bins) # Number of speed bins rose_dim = num_spd_bins * num_dir_sectors x_dim, y_dim = H['value'].shape # make rose table for each point in HRRR indexes = [[i, j] for i in range(x_dim) for j in range(y_dim)]