def info(ff, pyngl_map=True, print_timeid=False): ''' domain information of WRF output ''' ts = getvar(ff, "times", timeidx=ALL_TIMES) #-1 isn't work nt = np.int32(len(ts)) dtimes = [] #for t in ts: # dtimes.append(str(t.values)[0:19]) dtimes = [str(t.values)[0:19] for t in ts] #print("\033[44m{}\033[0m".format(dtimes[0])) var = getvar(ff, "QCLOUD", timeidx=0) #Xarray (nz, ny, nx) = var.shape #nz = np.int32(len(var[:,0,0])) (lat, lon) = latlon_coords(var) # Get the latitude and longitude points lat = to_np(lat) lon = to_np(lon) print(u'nt:{}'.format(nt)+'|nz:{}'.format(nz)+\ '|ny:{}'.format(ny)+'|nx:{} '.format(nx)+'.'*6+'Get Dimensions '+ \ 'and coordinates(lat. & long.)') if pyngl_map: #Set some map options based on information in WRF output file res = get_pyngl(var) del var res.tfDoNDCOverlay = True # required for native projection print( 'pyngl_map = {}, Get WRF map-related resources'.format(pyngl_map)) else: res = Ngl.Resources() if print_timeid: for i, dtime in enumerate(dtimes): print('\033[95m({:02d}){}\033[0m'.format(i, dtime)) return nt, nz, ny, nx, dtimes, lat, lon, res
def print_cape_for_timestamp(wrf_data, timestamp, filepath): slp = pressure_lib.get_sea_level_pressure(wrf_data) cinfo = getvar(wrf_data, "cape_2d", missing=0.0) cape = cinfo[0, :, :].fillna(0) lat, lon = latlon_coords(slp) lat_normal = to_np(lat) lon_normal = to_np(lon) # rain sum cape_res = get_pyngl(cinfo) cape_res.nglDraw = False # don't draw plot cape_res.nglFrame = False # don't advance frame cape_res.cnFillOn = True # turn on contour fill cape_res.cnLinesOn = False # turn off contour lines cape_res.cnLineLabelsOn = False # turn off line labels cape_res.cnFillMode = "RasterFill" # These two resources cape_res.cnLevelSelectionMode = "ExplicitLevels" cape_res.cnFillColors = numpy.array([ [255,255,255], [ 0,255, 0], [ 0,128, 0], \ [240,230,140], [255,255, 0], [255,140, 0], \ [255, 0, 0], [139, 0, 0], [186, 85,211],\ [153, 50,204], [139, 0,139], ],'f') / 255. cape_res.cnLevels = numpy.array( [.1, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]) cape_res = geography_lib.initialize_geography(cape_res, "gray50") cape_res.lbTitleString = "Convective available potential energy [CAPE] in (J/kg)" cape_res.lbOrientation = "horizontal" cape_res.lbTitleFontHeightF = 0.015 cape_res.lbLabelFontHeightF = 0.015 cape_res.tiMainString = "Thunderstorm probability (%s)" % timestamp.strftime( "%b %d %Y %HUTC") cape_res.trGridType = "TriangularMesh" # can speed up plotting. cape_res.tfDoNDCOverlay = True # required for native projection # pressure p_res = pressure_lib.get_pressure_resource(lat_normal, lon_normal) wk_res = Ngl.Resources() wk_res.wkWidth = 2500 wk_res.wkHeight = 2500 output_path = "%scape_%s" % (filepath, timestamp.strftime("%Y_%m_%d_%H")) wks_comp = Ngl.open_wks("png", output_path, wk_res) # creating plots for the measurands capeplot = Ngl.contour_map(wks_comp, cape, cape_res) pplot = Ngl.contour(wks_comp, slp, p_res) Ngl.overlay(capeplot, pplot) Ngl.maximize_plot(wks_comp, capeplot) Ngl.draw(capeplot) Ngl.frame(wks_comp) Ngl.delete_wks(wks_comp) # delete currently used workstation
def initialize_rain_resource(rain_con): rr_res = get_pyngl(rain_con) rr_res.nglDraw = False # don't draw plot rr_res.nglFrame = False # don't advance frame rr_res.cnFillOn = True # turn on contour fill rr_res.cnLinesOn = False # turn off contour lines rr_res.cnLineLabelsOn = False # turn off line labels rr_res.cnFillMode = "RasterFill" # These two resources rr_res.cnLevelSelectionMode = "ExplicitLevels" rr_res.cnFillColors = numpy.array([ [255,255,255], [255,255,255], [152,251,152], \ [127,255, 0], [ 50,205, 50], [ 0,255, 0], \ [ 0,128, 0], [ 0,100, 0], [255,255, 0], \ [255,165, 0], [255, 69, 0], [139, 0,139], \ [ 0, 0, 255] ],'f') / 255. rr_res.cnLevels = numpy.array( [ .1, .2, .4, .8, 1.6, 3.2, 6.4, \ 12.8, 25.6, 51.2, 102.4, 204.8 ]) rr_res = geography_lib.initialize_geography(rr_res, "gray50") return rr_res
"You do not have the necessary '{}' file to run this example.".format( filename)) print("You need to supply your own WRF output file") print("WRF output files usually have names like '{}'".format(filename)) sys.exit() #---Read data a = Nio.open_file(filename + ".nc") # Must add ".nc" suffix for Nio.open_file tc = getvar(a, "tc") #---Open file for graphics wks_type = "png" wks = Ngl.open_wks(wks_type, "wrf3") # Set some map options based on information in WRF output file res = get_pyngl(tc) res.tfDoNDCOverlay = True # required for native projection #---Contour options res.cnFillOn = True # turn on contour fill res.cnLinesOn = False # turn off contour lines res.cnLineLabelsOn = False # turn off line labels res.cnFillMode = "RasterFill" # These two resources res.trGridType = "TriangularMesh" # can speed up plotting. res.cnFillPalette = "ncl_default" res.lbOrientation = "horizontal" # default is vertical res.pmLabelBarHeightF = 0.08 res.pmLabelBarWidthF = 0.65 res.lbTitleString = "%s (%s)" % (tc.description, tc.units) res.lbTitleFontHeightF = 0.015
print("No shapefile outlines will be added.") ADD_SHAPE_OUTLINES = False else: ADD_SHAPE_OUTLINES = True #---Read data a = Nio.open_file("{}.nc".format(wrf_filename)) var_name = "slp" # "slp", "ter" var = getvar(a, var_name) #---Send graphics to PNG file wks_type = "png" wks = Ngl.open_wks(wks_type, "wrf2") # Set some map options based on information in WRF output file res = get_pyngl(var) res.tfDoNDCOverlay = True # required for native projection # Contour options res.cnFillOn = True # turn on contour fill res.cnLinesOn = False # turn off contour lines res.cnLineLabelsOn = False # turn off line labels if var_name == "ter": # # You may need to change these levels depending on your WRF output file. # Try first running this script with these two lines commented out. # This is for terrain # res.cnFillPalette = "OceanLakeLandSnow" res.cnLevelSelectionMode = "ExplicitLevels"
def print_comp_for_timestamp(wrf_data, timestamp, filepath): slp = pressure_lib.get_sea_level_pressure(wrf_data) temperature = getvar(wrf_data,"tc") u = get_latitude_wind(wrf_data) v = get_longitude_wind(wrf_data) lat, lon = latlon_coords(temperature) lat_normal = to_np(lat) lon_normal = to_np(lon) # temperature t_res = get_pyngl(temperature) t_res.nglDraw = False # don't draw plot t_res.nglFrame = False # don't advance frame t_res.cnFillOn = True # turn on contour fill t_res.cnLinesOn = False # turn off contour lines t_res.cnLineLabelsOn = False # turn off line labels t_res.cnFillMode = "RasterFill" # These two resources t_res.cnFillPalette = "BlAqGrYeOrReVi200" t_res.cnLevelSelectionMode = "ManualLevels" t_res.cnMinLevelValF = -25.0 # min. temperature for composite t_res.cnMaxLevelValF = 45.0 # max. temperature for composite t_res.cnLevelSpacingF = 1 # increment t_res = geography_lib.initialize_geography(t_res, "gray25") t_res.lbTitleString = "%s in (%s)" % (temperature.description,temperature.units) t_res.lbOrientation = "horizontal" t_res.lbTitleFontHeightF = 0.015 t_res.lbLabelFontHeightF = 0.015 t_res.tiMainString = "Composite (%s)" % timestamp.strftime("%b %d %Y %HUTC") t_res.trGridType = "TriangularMesh" # can speed up plotting. t_res.tfDoNDCOverlay = True # required for native projection # pressure p_res = pressure_lib.get_pressure_resource(lat_normal, lon_normal) # wind uv_res = Ngl.Resources() uv_res.nglDraw = False # don't draw plot uv_res.nglFrame = False # don't advance frame uv_res.vcFillArrowsOn = True uv_res.vcRefMagnitudeF = 15.0 uv_res.vcRefLengthF = 0.03 uv_res.vcMinDistanceF = 0.02 uv_res.vcRefAnnoFontHeightF = 0.01 uv_res.vcRefAnnoString1 = "$VMG$ m/s" uv_res.vcRefAnnoString2 = "ground windspeed (m/s) reference" uv_res.vfXArray = lon_normal uv_res.vfYArray = lat_normal wk_res = Ngl.Resources() wk_res.wkWidth = 2500 wk_res.wkHeight = 2500 output_path = "%scomp_%s" % (filepath, timestamp.strftime("%Y_%m_%d_%H")) wks_comp = Ngl.open_wks("png", output_path, wk_res) # creating plots for the measurands tplot = Ngl.contour_map(wks_comp,temperature[0,:,:],t_res) pplot = Ngl.contour(wks_comp,slp,p_res) vector = Ngl.vector(wks_comp,u,v,uv_res) Ngl.overlay(tplot, vector) Ngl.overlay(tplot, pplot) Ngl.maximize_plot(wks_comp, tplot) Ngl.draw(tplot) Ngl.frame(wks_comp) Ngl.delete_wks(wks_comp) # delete currently used workstation