def wks_setting(res): image_name = "Shimen_topography" wks_type = "png" print("\033[44m---Plot: {}.{} ---\033[0m".format(image_name,wks_type)) rlist = Ngl.Resources() rlist.wkWidth = 3000 #page resolution rlist.wkHeight = 3000 wks = Ngl.open_wks(wks_type,image_name,rlist) ''' cmap = Ngl.read_colormap_file("MPL_terrain") cmap1 = [] for cp in cmap: print(cp) print('---') #print(str(cp).replace('[','').replace(']','').replace(' ',' ').strip().split(' ')) dum = str(cp).replace('[','').replace(']','').strip() dum = dum.replace(' ',' ').replace(' ',' ').replace(' ',' ') dum = dum.replace(' ',',').split(',')[0:3] print(dum) cmap1.append(dum) cmap1 = [[1,1,1],[0,0,0]]+[cmap1[0]]+ cmap1[30:len(cmap1)] print(cmap1) cmap1 = np.float32(cmap1) print(cmap1) #cmap1 = np.concatenate(([cmap[0]], cmap[30:]), axis=0) #print(len(cmap1)) #print(cmap1) #Ngl.define_colormap(wks, cmap1 ) ''' Ngl.define_colormap(wks, colorbar_tables.TW_terrain()) #Ngl.define_colormap(wks, "MPL_terrain") res.nglMaximize = True res.nglDraw = False; res.nglFrame = False #res.vpWidthF = 0.74; res.vpHeightF = 0.54 return wks, res
def create_workstation(colorBarName, image_path): # 3.1打开工作台 wkres = Ngl.Resources() wkres.wkWidth = 930 wkres.wkHeight = 930 workstation = Ngl.open_wks("png", image_path, wkres) # 3.2获取底板颜色 cmap = colorTool.getColorMap(colorBarName) Ngl.define_colormap(workstation, cmap) return workstation
def wks_setting(res): image_name = "Falcon_track_20191031" wks_type = "png" print("\033[44m---Plot: {}.{} ---\033[0m".format(image_name,wks_type)) rlist = Ngl.Resources() rlist.wkWidth = 3000 #page resolution rlist.wkHeight = 3000 wks = Ngl.open_wks(wks_type,image_name,rlist) Ngl.define_colormap(wks, colorbar_tables.TW_terrain()) #Ngl.define_colormap(wks, "MPL_terrain") res.nglMaximize = True res.nglDraw = False; res.nglFrame = False #res.vpWidthF = 0.74; res.vpHeightF = 0.54 return wks, res
def wks_setting(res, vn): image_name = "{}_timeseries_Falcon_20190603".format(vn) wks_type = "png" print("\033[44m---Plot: {}.{} ---\033[0m".format(image_name, wks_type)) rlist = Ngl.Resources() rlist.wkWidth = 3000 #page resolution rlist.wkHeight = 3000 wks = Ngl.open_wks(wks_type, image_name, rlist) Ngl.define_colormap(wks, colorbar_tables.TW_terrain()) #Ngl.define_colormap(wks, "MPL_terrain") res.nglMaximize = True res.nglDraw = False res.nglFrame = False res.vpWidthF = 0.9 res.vpHeightF = 0.3 return wks, res
def rain_prf (ptype,cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs,casedir,varis,cscale,chscale,pname): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = 'Red' mkres.gsMarkerSizeF = 15. infiles = ['' for x in range(ncases)] ncdfs = ['' for x in range(ncases)] nregions = nsite varisobs = ['CC_ISBL', 'OMEGA','SHUM','CLWC_ISBL', 'THETA','RELHUM','U','CIWC_ISBL','T' ] nvaris = len(varis) cunits = ['%','mba/day','g/kg','g/kg','K', '%', 'm/s', 'g/kg', 'm/s', 'm/s','K','m' ] cscaleobs = [100, 1, 1, 1000 , 1., 1, 1, 1000, 1,1,1,1,1,1,1] obsdataset =['ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI','ERAI','ERAI'] plotrain=['' for x in range(nsite)] for ire in range (0, nsite): if not os.path.exists(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N'): os.mkdir(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N') plotname = casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N/'+pname+'_'+str(lons[ire])+'E_'+str(lats[ire])+'N_'+cseason plotrain[ire] = pname+'_'+str(lons[ire])+'E_'+str(lats[ire])+'N_'+cseason wks= Ngl.open_wks(ptype,plotname) Ngl.define_colormap(wks,'GMT_paired') plot = [] res = Ngl.Resources() res.nglDraw = False res.nglFrame = False res.lgLabelFontHeightF = .02 # change font height res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 #res.vpXF = 0.04 # res.vpYF = 0.30 res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tiMainFont = _Font res.tmXBLabelFontHeightF = 0.015 res.tmXBLabelFontThicknessF = 1.0 # res.tmXBLabelAngleF = 45 res.xyMarkLineMode = 'Lines' res.xyLineThicknesses = [3.0, 3.0, 3.0, 3.0, 3.0, 3.0,3.,3.,3.,3.,3,3,3,3,3,3,3] res.xyDashPatterns = np.arange(0,24,1) # res.xyMarkers = np.arange(16,40,1) # res.xyMarkerSizeF = 0.005 pres = Ngl.Resources() # pres.nglMaximize = True pres.nglFrame = False pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.88 pres.wkWidth = 2500 pres.wkHeight = 2500 for iv in range (0, nvaris): if(iv == nvaris-1): res.pmLegendDisplayMode = 'Never' res.xyExplicitLegendLabels = casenames[:] res.pmLegendSide = 'top' res.pmLegendParallelPosF = 0.6 res.pmLegendOrthogonalPosF = -0.5 res.pmLegendWidthF = 0.10 res.pmLegendHeightF = 0.10 res.lgLabelFontHeightF = .02 res.lgLabelFontThicknessF = 1.5 res.lgPerimOn = False else: res.pmLegendDisplayMode = 'NEVER' for im in range (0,ncases): ncdfs[im] = './data/'+cases[im]+'_site_location.nc' infiles[im]= filepath[im]+cases[im]+'/'+cases[im]+'_'+cseason+'_climo.nc' inptrs = Dataset(infiles[im],'r') # pointer to file1 lat=inptrs.variables['lat'][:] nlat=len(lat) lon=inptrs.variables['lon'][:] nlon=len(lon) lev=inptrs.variables['lev'][:] nlev=len(lev) ncdf= Dataset(ncdfs[im],'r') n =ncdf.variables['n'][:] idx_cols=ncdf.variables['idx_cols'][:,:] ncdf.close() if (im ==0): A_field = np.zeros((ncases,nlev),np.float32) for subc in range( 0, n[ire]): npoint=idx_cols[ire,n[subc]-1]-1 tmp=inptrs.variables[varis[iv]][0,:,npoint] theunits=str(chscale[iv])+'x'+inptrs.variables[varis[iv]].units A_field[im,:] = (A_field[im,:]+tmp[:]/n[ire]).astype(np.float32 ) A_field[im,:] = A_field[im,:] *cscale[iv] inptrs.close() res.tiMainString = varis[iv]+' '+theunits # res.trXMinF = min(np.min(A_field[0, :])) # res.trXMaxF = max(np.max(A_field[0, :])) res.trYReverse = True res.xyLineColors = np.arange(3,20,2) res.xyMarkerColors = np.arange(2,20,2) p = Ngl.xy(wks,A_field,lev,res) # res.trYReverse = False # res.xyLineColors = ['black'] # pt = Ngl.xy(wks,B,pre1,res) # Ngl.overlay(p,pt) plot.append(p) pres.txString = pname+' at'+ str(lons[ire])+'E,'+str(lats[ire])+'N' txres = Ngl.Resources() txres.txFontHeightF = 0.020 txres.txFont = _Font Ngl.text_ndc(wks,pname+' at'+ str(lons[ire])+'E,'+str(lats[ire])+'N',0.5,0.92+ncases*0.01,txres) Common_functions.create_legend(wks,casenames,np.arange(3,20,2),0.1,0.89+ncases*0.01) Ngl.panel(wks,plot[:],[nvaris/2,2],pres) Ngl.frame(wks) Ngl.destroy(wks) return plotrain
wkres = Ngl.Resources() #wkres.wkColorMap=colors rlist = Ngl.Resources() wks_type = "png" if (wks_type == "ps" or wks_type == "pdf"): rlist.wkOrientation = "Portrait" # For PS or PDF output only. wks = Ngl.open_wks(wks_type, "J8_teb_current_validation_urban_mean_teb_class", wkres) #wks2 = Ngl.open_wks(wks_type,"J8_teb_current_all_3h_validation3",wkres) colors = [ "White", "Black", "SkyBlue", "Turquoise", "Blue", "Green", "Yellow", "Orange", "tomato", "Magenta", "PaleVioletRed", "Maroon", "red4", "Red" ] #colors=["White","Black","SkyBlue","Turquoise","PaleVioletRed","Red"] Ngl.define_colormap(wks, colors) #Ngl.define_colormap(wks,"BlGrYeOrReVi200") #Ngl.define_colormap(wks,"hotres") #Ngl.define_colormap(wks2,colors) #Ngl.define_colormap(wks,"GHRSST_anomaly") resources = Ngl.Resources() Ngl.set_values(wks, resources) resources.pmLabelBarDisplayMode = "Never" # Turn on label bar. resources.pmLabelBarDisplayMode = "Never" # Turn on label bar. #resources.pmLabelBarWidthF = 0.50 resources.nglDraw = False # dont draw resources.nglFrame = False # dont advance frame #resources.nglAddCyclic = False
# # Generate the data. # # Note: we have not verified the accuracy of this data! # x = numpy.array(range(1,9)) y = numpy.array([154900,56600,40000,30200,29700,24400,21700,13900]) colors = ["white","black","FireBrick","Red","Orange","Green", "Navy", "Blue", "SkyBlue","SlateBlue"] labels = ["Lung","Colon","Breast","Prostate","Pancreas",\ "Lymphoma","Leukemias","Ovary"] wks_type = "ps" wks = Ngl.open_wks(wks_type,"bar2") Ngl.define_colormap(wks,colors) res = Ngl.Resources() res.nglMaximize = False # Need to set to False if using # vp resources. res.vpYF = 0.92 # Move plot up a little. res.vpHeightF = 0.77 # Make plot higher than res.vpWidthF = 0.70 # it is high. res.tmXBOn = False # Turn off bottom tickmarks & labes res.tmXTOn = False # Turn off top tickmarks & labes res.tmYROn = False # Turn off right tickmarks & labes res.tmYLMajorLengthF = 0.0 # Turn off Y major tickmarks
def draw_3D_plot(ptype, clevel, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) if not os.path.exists(casedir + "/2D"): os.mkdir(casedir + "/2D") _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] varis = ["T", "OMEGA", "Z3"] varisobs = ["T", "OMEGA", "Z3"] alpha = ["A", "B", "C", "D", "E", "F"] nvaris = len(varis) cunits = [""] cscale = [1, 864, 1, 1, 1, 1] cscaleobs = [1, 1, 1, 1, 0.04] cntrs = np.zeros((nvaris, 11), np.float32) obsdataset = ["ERAI", "ERAI", "ERAI", "ERAI", "ERAI"] level = [ 1000., 925., 850., 700., 600., 500., 400., 300., 250., 200., 150., 100. ] plot3d = ["" for x in range(nvaris)] for iv in range(0, nvaris): # make plot for each field # Observational data if (obsdataset[iv] == "CCCM"): if (cseason == "ANN"): fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-" + cseason + ".nc" else: fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-2010-" + cseason + ".nc" else: if (varisobs[iv] == "PRECT"): fileobs = filepathobs + '/GPCP_' + cseason + '_climo.nc' else: fileobs = filepathobs + obsdataset[ iv] + '_' + cseason + '_climo.nc' inptrobs = Dataset(fileobs, 'r') latobs = inptrobs.variables['lat'][:] lonobs = inptrobs.variables['lon'][:] levobs = inptrobs.variables['lev'][:] levobs_idx = np.abs(levobs - clevel).argmin() B = inptrobs.variables[varisobs[iv]][0, levobs_idx, :, :] B = B * cscaleobs[iv] # cntrs= np.arange(np.min(B),np.max(B),12) if (varis[iv] == "OMEGA"): cntrs[iv, :] = [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50] if (varis[iv] == "Z3"): if (clevel == 500): cntrs[iv, :] = [ 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300 ] else: cntrs[iv, :] = [ 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300 ] if (varis[iv] == "T"): if (clevel == 500): cntrs[iv, :] = [ 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310 ] else: cntrs[iv, :] = [ 292, 294, 296, 298, 300, 302, 304, 306, 308, 310, 312 ] if (varis[iv] == "Q"): if (clevel == 500): cntrs[iv, :] = [ 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310 ] else: cntrs[iv, :] = [ 292, 294, 296, 298, 300, 302, 304, 306, 308, 310, 312 ] #************************************************ # create plot #************************************************ plotname = casedir + '/2D/Horizontal_' + varis[ iv] + '_' + cseason + str(clevel) plot3d[iv] = 'Horizontal_' + varis[iv] + '_' + cseason + str(clevel) wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, "amwg256") plot = [] textres = Ngl.Resources() textres.txFontHeightF = 0.02 # Size of title. textres.txFont = _Font Ngl.text_ndc(wks, varis[iv], 0.1, .97, textres) pres = Ngl.Resources() # pres.nglMaximize = True pres.nglFrame = False pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelBottom = 0.2 pres.nglPanelTop = 0.9 pres.nglPanelLabelBar = True pres.pmLabelBarWidthF = 0.8 pres.nglFrame = False pres.nglPanelLabelBar = True # Turn on panel labelbar pres.nglPanelLabelBarLabelFontHeightF = 0.015 # Labelbar font height pres.nglPanelLabelBarHeightF = 0.0750 # Height of labelbar pres.nglPanelLabelBarWidthF = 0.700 # Width of labelbar pres.lbLabelFont = "helvetica-bold" # Labelbar font pres.nglPanelTop = 0.93 pres.nglPanelFigureStrings = alpha pres.nglPanelFigureStringsJust = "BottomRight" res = Ngl.Resources() res.nglDraw = False #-- don't draw plots res.nglFrame = False res.cnFillOn = True res.cnFillMode = "RasterFill" res.cnLinesOn = False res.nglMaximize = True res.mpFillOn = True res.mpCenterLonF = 180 res.tiMainFont = _Font res.tiMainFontHeightF = 0.025 res.tiXAxisString = "" res.tiXAxisFont = _Font res.tiXAxisFontHeightF = 0.025 res.tiYAxisString = "" res.tiYAxisFont = _Font res.tiYAxisOffsetXF = 0.0 res.tiYAxisFontHeightF = 0.025 res.tmXBLabelFont = _Font res.tmYLLabelFont = _Font res.tiYAxisFont = _Font # res.nglStringFont = _Font # res.nglStringFontHeightF = 0.04 # res.nglRightString = ""#"Cloud Fraction" # res.nglScalarContour = True res.cnInfoLabelOn = False res.cnFillOn = True res.cnLinesOn = False res.cnLineLabelsOn = False res.lbLabelBarOn = False # res.vcRefMagnitudeF = 5. # res.vcMinMagnitudeF = 1. # res.vcRefLengthF = 0.04 # res.vcRefAnnoOn = True#False # res.vcRefAnnoZone = 3 # res.vcRefAnnoFontHeightF = 0.02 # res.vcRefAnnoString2 ="" # res.vcRefAnnoOrthogonalPosF = -1.0 # res.vcRefAnnoArrowLineColor = "blue" # change ref vector color # res.vcRefAnnoArrowUseVecColor = False # res.vcMinDistanceF = .05 # res.vcMinFracLengthF = . # res.vcRefAnnoParallelPosF = 0.997 # res.vcFillArrowsOn = True # res.vcLineArrowThicknessF = 3.0 # res.vcLineArrowHeadMinSizeF = 0.01 # res.vcLineArrowHeadMaxSizeF = 0.03 # res.vcGlyphStyle = "CurlyVector" # turn on curley vectors # res@vcGlyphStyle ="Fillarrow" # res.vcMonoFillArrowFillColor = True # res.vcMonoLineArrowColor = True # res.vcLineArrowColor = "green" # change vector color # res.vcFillArrowEdgeColor ="white" # res.vcPositionMode ="ArrowTail" # res.vcFillArrowHeadInteriorXF =0.1 # res.vcFillArrowWidthF =0.05 #default # res.vcFillArrowMinFracWidthF =.5 # res.vcFillArrowHeadMinFracXF =.5 # res.vcFillArrowHeadMinFracYF =.5 # res.vcFillArrowEdgeThicknessF = 2.0 res.mpFillOn = False res.cnLevelSelectionMode = "ExplicitLevels" res.cnLevels = cntrs[iv, :] for im in range(0, ncases): ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) #area=inptrs.variables['area'][:] lev = inptrs.variables['lev'][:] lev_idx = np.abs(lev - clevel).argmin() area_wgt = np.zeros(nlat) # area_wgt[:] = gw[:] sits = np.linspace(0, nsite - 1, nsite) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:] A = inptrs.variables[varis[iv]][0, lev_idx, :] A_xy = A A_xy = A_xy * cscale[iv] ncdf.close() inptrs.close() if im == 0: dsizes = len(A_xy) field_xy = [[0 for col in range(dsizes)] for row in range(ncases)] field_xy[im][:] = A_xy res.lbLabelBarOn = False if (np.mod(im, 2) == 0): res.tiYAxisOn = True else: res.tiYAxisOn = False res.tiXAxisOn = False res.sfXArray = lon res.sfYArray = lat res.mpLimitMode = "LatLon" res.mpMaxLonF = max(lon) res.mpMinLonF = min(lon) res.mpMinLatF = min(lat) res.mpMaxLatF = max(lat) #res.tiMainString = "GLB="+str(np.sum(A_xy[:]*area[:]/np.sum(area))) res.tiMainString = "GLB=" textres.txFontHeightF = 0.015 Ngl.text_ndc(wks, alpha[im] + " " + casenames[im], 0.3, .135 - im * 0.03, textres) p = Ngl.contour_map(wks, A_xy, res) plot.append(p) # observation # res.nglLeftString = obsdataset[iv] # res@lbLabelBarOn = True # res@lbOrientation = "vertical" # vertical label bars res.lbLabelFont = _Font res.tiYAxisOn = True res.tiXAxisOn = True res.tiXAxisFont = _Font rad = 4.0 * np.arctan(1.0) / 180.0 re = 6371220.0 rr = re * rad dlon = abs(lonobs[2] - lonobs[1]) * rr dx = dlon * np.cos(latobs * rad) jlat = len(latobs) dy = np.zeros(jlat, dtype=float) # close enough dy[0] = abs(lat[2] - lat[1]) * rr dy[1:jlat - 2] = abs(lat[2:jlat - 1] - lat[0:jlat - 3]) * rr * 0.5 dy[jlat - 1] = abs(lat[jlat - 1] - lat[jlat - 2]) * rr area_wgt = dx * dy # is_SE = False sum1 = 0 sum2 = 0 for j in range(0, jlat - 1): for i in range(0, len(lonobs) - 1): if (np.isnan(B[j][i]) != "--"): sum1 = sum1 + area_wgt[j] * B[j][i] sum2 = sum2 + area_wgt[j] res.sfXArray = lonobs res.sfYArray = latobs res.mpLimitMode = "LatLon" res.mpMaxLonF = max(lonobs) res.mpMinLonF = min(lonobs) res.mpMinLatF = min(latobs) res.mpMaxLatF = max(latobs) res.tiMainString = "GLB=" + str( sum1 / sum2) #Common_functions.area_avg(B, area_wgt,is_SE)) p = Ngl.contour_map(wks, B, res) plot.append(p) if (np.mod(ncases + 1, 2) == 1): Ngl.panel(wks, plot[:], [(ncases + 1) / 2 + 1, 2], pres) else: Ngl.panel(wks, plot[:], [(ncases + 1) / 2, 2], pres) Ngl.frame(wks) Ngl.destroy(wks) return plot3d
# # Import Ngl support functions. # import Ngl # # Qualifiers for China provinces and India states. # boundaries = ["China:states", "India:States"] # # Change the color map. # wks_type = "png" wks = Ngl.open_wks(wks_type, "ctrydiv1") Ngl.define_colormap(wks, "StepSeq25") # # Create the plot. # # # Map resources. # res = Ngl.Resources() res.mpDataSetName = "Earth..4" # Change database res.mpDataBaseVersion = "MediumRes" # Medium resolution database res.mpOutlineOn = True # Turn on map outlines res.mpOutlineBoundarySets = "National"
def draw_clubb_bgt (ptype,cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs,casedir,varis,cscale,chscale,pname,dofv): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite varisobs = [ "CLOUD", "OMEGA","SHUM","CLWC_ISBL", "THATA","RELHUM"] nvaris = len(varis) cunits = ["%", "mba/day","g/kg","g/kg","K", "%", "mba/day", "K", "g/kg", "m/s", "m/s","K","m"] cscaleobs = [100., 100/86400., 1., 1000, 1., 1., 1, 1,1,1] obsdataset =["CCCM", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI","ERAI","ERAI"] plotbgt=["" for x in range(nsite*ncases)] for ire in range (0, nsite): for im in range (0,ncases): if not os.path.exists(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N'): os.mkdir(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N') plotname = casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N/'+pname+'_'+casenames[im]+"_"+str(lons[ire])+"E_"+str(lats[ire])+"N_"+cseason plotbgt[im+ncases*ire] = pname+'_'+casenames[im]+"_"+str(lons[ire])+"E_"+str(lats[ire])+"N_"+cseason wks= Ngl.open_wks(ptype,plotname) Ngl.define_colormap(wks,"radar") plot = [] res = Ngl.Resources() res.nglDraw = False res.nglFrame = False res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 # res.txFontHeightF = .01 # res.vpXF = 0.04 # res.vpYF = 0.30 res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tmXBLabelFontHeightF = 0.005 res.tmXBLabelFontThicknessF = 1.0 res.xyMarkLineMode = "MarkLines" res.xyLineThicknesses = [2.0, 2.0, 2.0, 2.0, 2.0, 2.0,2.,2.,2.,2.,2,2,2,2,2,2,2] res.xyLineColors = np.arange(2,16,1) res.xyDashPatterns = np.arange(0,24,1) res.xyMarkers = np.arange(16,40,1) res.xyMarkerSizeF = 0.005 res.xyMarkerColors = np.arange(2,16,1) res.pmLegendDisplayMode = "ALWAYS" res.pmLegendSide = "top" # Change location of res.pmLegendParallelPosF = 0.75 # move units right res.pmLegendOrthogonalPosF = -0.65 # more neg = down res.pmLegendWidthF = 0.10 # Change width and res.pmLegendHeightF = 0.15 # height of legend. res.lgLabelFontHeightF = .01 # change font height res.lgLabelFontThicknessF = 1. res.lgBoxMinorExtentF = 0.1 res.lgPerimOn = True res.tiYAxisString = "PRESSURE" res.tiYAxisString = "Pressure [hPa]" res.tmYLLabelFontHeightF = 0.02 res.trYReverse = True pres = Ngl.Resources() # pres.nglMaximize = True pres.nglFrame = False pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.93 for iv in range (0, nvaris): if (varis[iv] == "rtp2" or varis[iv] == "thlp2"): budget_ends = ["_bt", "_ma", "_ta", "_tp", "_dp1", "_dp2", "_cl", "_pd", "_sf", "_forcing"] nterms = len (budget_ends) if (varis[iv] == "wprtp") : budget_ends = ["_bt", "_ma", "_ta", "_tp", "_ac","_bp","_pr1","_pr2", "_pr3","_dp1","_mfl", "_cl", "_sicl","_pd", "_forcing"] nterms = len (budget_ends) if (varis[iv] == "wpthlp") : budget_ends = ["_bt", "_ma", "_ta", "_tp", "_ac","_bp","_pr1","_pr2", "_pr3","_dp1","_mfl", "_cl", "_sicl", "_forcing"] nterms = len (budget_ends) if (varis[iv] == "rtpthlp") : budget_ends = ["_bt", "_ma", "_ta", "_tp1","_tp2","_dp1","_dp2", "_cl", "_sf", "_forcing"] nterms = len (budget_ends) if (varis[iv] == "wp2") : budget_ends = ["_bt", "_ma", "_ta", "_ac","_bp","_pr1","_pr2", "_pr3","_dp1","_dp2", "_cl", "_pd", "_sf"] nterms = len (budget_ends) if (varis[iv] == "wp3") : budget_ends = ["_bt", "_ma", "_ta", "_tp", "_ac","_bp1","_bp2","_pr1","_pr2","_dp1", "_cl"] nterms = len (budget_ends) if (varis[iv] == "up2" or varis[iv] == "vp2") : budget_ends = ["_bt", "_ma", "_ta", "_tp", "_dp1", "_dp2","_pr1","_pr2" ,"_cl", "_pd", "_sf"] nterms = len (budget_ends) if (varis[iv] == "um" or varis[iv] == "vm") : budget_ends = ["_bt", "_ma","_ta","_gf", "_f"] nterms = len (budget_ends) if (varis[iv] == "thlm" or varis[iv] == "rtm") : budget_ends = ["_bt", "_ma","_ta","_cl", "_mc"] nterms = len (budget_ends) ncdfs[im] = './data/'+cases[im]+'_site_location.nc' infiles[im]= filepath[im]+'/'+cases[im]+'_'+cseason+'_climo.nc' inptrs = Dataset(infiles[im],'r') # pointer to file1 lat=inptrs.variables['lat'][:] nlat=len(lat) lon=inptrs.variables['lon'][:] nlon=len(lon) ilev=inptrs.variables['ilev'][:] nilev=len(ilev) ncdf= Dataset(ncdfs[im],'r') n =ncdf.variables['n'][:] idx_cols=ncdf.variables['idx_cols'][:,:] if (dofv): idx_lats=ncdf.variables['idx_coord_lat'][:,:] idx_lons=ncdf.variables['idx_coord_lon'][:,:] ncdf.close() A_field = np.zeros((nterms,nilev),np.float32) theunits=str(chscale[iv])+"x"+inptrs.variables[varis[iv]+'_bt'].units res.tiMainString = varis[iv]+" "+theunits for it in range(0, nterms): for subc in range( 0, n[ire]): varis_bgt= varis[iv]+budget_ends[it] npoint=idx_cols[ire,n[subc]-1]-1 if(dofv): npointlat=idx_lats[ire,0] npointlon=idx_lons[ire,0] if (dofv): tmp=inptrs.variables[varis_bgt][0,:,npointlat,npointlon] else: tmp=inptrs.variables[varis_bgt][0,:,npoint] #/n[ire] if (varis[iv] == "wprtp" ) : tmp [0:10] = 0.0 tmp=tmp*cscale[iv] A_field[it,:] = (A_field[it,:]+tmp[:]/n[ire]).astype(np.float32 ) inptrs.close() res.xyExplicitLegendLabels = budget_ends[:] p = Ngl.xy(wks,A_field,ilev,res) plot.append(p) xp=np.mod(iv,2) yp=int(iv/2) # pres.txFontHeightF = 0.02 # pres.txString = casenames[im]+" BUDGET at" +str(lons[ire])+"E,"+str(lats[ire])+"N" Ngl.panel(wks,plot[:],[nvaris/2,2],pres) txres = Ngl.Resources() txres.txFont = _Font txres.txFontHeightF = 0.020 Ngl.text_ndc(wks,casenames[im]+" BUDGET at" +str(lons[ire])+"E,"+str(lats[ire])+"N",0.5,0.95,txres) Ngl.frame(wks) Ngl.destroy(wks) return (plotbgt)
def large_scale_prf (ptype,cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = 'Red' mkres.gsMarkerSizeF = 15. infiles = ['' for x in range(ncases)] ncdfs = ['' for x in range(ncases)] nregions = nsite localnames=np.append('ERAI',casenames) varis = [ 'CLOUD' , 'CLDLIQ', 'THETA','RELHUM'] varisobs = ['CC_ISBL', 'CLWC_ISBL', 'THETA','RELHUM' ] alphas = ['a) ','b) ','c) ','d) ','e) ','f) ','g) ','h) ','i) ', 'j) ', 'k) ', 'l) ', 'm) ', 'n) ', 'o) ', 'p) ', 'q) ', 'r) ', 's) ', 't) ', 'u) ', 'v) ', 'w) ', 'x) ', 'y) ', 'z) '] nvaris = len(varis) cunits = ['%','g/kg','K', '%' ] cscale = [100, 1000 , 1., 1 ] cscaleobs = [100, 1000 , 1., 1, 1, 1000, 1,1,1,1,1,1,1] obsdataset =['ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI', 'ERAI','ERAI','ERAI'] plotlgs=['' for x in range(nsite)] for ire in range (0, nsite): if not os.path.exists(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N'): os.mkdir(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N') plotname = casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N/Largescale_'+str(lons[ire])+'E_'+str(lats[ire])+'N_'+cseason plotlgs[ire] = 'Largescale_'+str(lons[ire])+'E_'+str(lats[ire])+'N_'+cseason wks= Ngl.open_wks(ptype,plotname) Ngl.define_colormap(wks,'GMT_paired') plot = [] res = Ngl.Resources() res.nglMaximize = False res.nglDraw = False res.nglFrame = False res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 res.tiYAxisString = "Pressure [hPa]" if (lats[ire] > 0 and lons[ire]<230 ): res.trYMinF = 400. res.trYMaxF = 1000. else: res.trYMinF = 700. res.trYMaxF = 1000. res.tiMainFont = _Font res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tiYAxisFont = _Font res.tiXAxisFont = _Font res.tmXBLabelFontHeightF = 0.01 res.tmXBLabelFontThicknessF = 1.0 res.xyMarkLineMode = 'Lines' res.xyLineThicknesses = [3.0, 3.0, 3.0, 3.0, 3.0, 3.0,3.,3.,3.,3.,3,3,3,3,3,3,3] res.xyDashPatterns = np.arange(0,24,1) pres = Ngl.Resources() pres.nglFrame = False pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 # pres.nglPanelTop = 0.88 pres.wkPaperWidthF = 17 # in inches pres.wkPaperHeightF = 28 # in inches pres.nglMaximize = True pres.nglPanelTop = 0.935 pres.nglPanelFigureStrings = alphas pres.nglPanelFigureStringsJust = 'Topleft' pres.nglPanelFigureStringsFontHeightF = 0.015 for iv in range (0, nvaris): if(iv == nvaris-1): res.pmLegendDisplayMode = 'NEVER' res.xyExplicitLegendLabels = casenames[:] res.pmLegendSide = 'top' res.pmLegendParallelPosF = 0.6 res.pmLegendOrthogonalPosF = -0.5 res.pmLegendWidthF = 0.10 res.pmLegendHeightF = 0.10 res.lgLabelFontHeightF = .02 res.lgLabelFontThicknessF = 1.5 res.lgPerimOn = True else: res.pmLegendDisplayMode = 'NEVER' if(obsdataset[iv] =='CCCM'): if(cseason == 'ANN'): fileobs = '/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-'+cseason+'.nc' else: fileobs = '/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-2010-'+cseason+'.nc' inptrobs = Dataset(fileobs,'r') latobs=inptrobs.variables['lat'][:] latobs_idx = np.abs(latobs - lats[ire]).argmin() lonobs=inptrobs.variables['lon'][:] lonobs_idx = np.abs(lonobs - lons[ire]).argmin() B=inptrobs.variables[varisobs[iv]][:,latobs_idx,lonobs_idx] else: if (varisobs[iv] =='PRECT'): fileobs = filepathobs+'/GPCP_'+cseason+'_climo.nc' else: fileobs = filepathobs + obsdataset[iv]+'_'+cseason+'_climo.nc' inptrobs = Dataset(fileobs,'r') if (varisobs[iv] =='THETA'): latobs=inptrobs.variables['lat'][:] latobs_idx = np.abs(latobs - lats[ire]).argmin() lonobs=inptrobs.variables['lon'][:] lonobs_idx = np.abs(lonobs - lons[ire]).argmin() B = inptrobs.variables['T'][0,:,latobs_idx,lonobs_idx] pre1 = inptrobs.variables['lev'][:] for il1 in range (0, len(pre1)): B[il1] = B[il1]*(1000/pre1[il1])**0.286 else: pre1 = inptrobs.variables['lev'][:] latobs=inptrobs.variables['lat'][:] latobs_idx = np.abs(latobs - lats[ire]).argmin() lonobs=inptrobs.variables['lon'][:] lonobs_idx = np.abs(lonobs - lons[ire]).argmin() B = inptrobs.variables[varisobs[iv]][0,:,latobs_idx,lonobs_idx] B[:]=B[:] * cscaleobs[iv] for im in range (0,ncases): ncdfs[im] = './data/'+cases[im]+'_site_location.nc' infiles[im]= filepath[im]+cases[im]+'/'+cases[im]+'_'+cseason+'_climo.nc' inptrs = Dataset(infiles[im],'r') # pointer to file1 lat=inptrs.variables['lat'][:] nlat=len(lat) lon=inptrs.variables['lon'][:] nlon=len(lon) lev=inptrs.variables['lev'][:] nlev=len(lev) ncdf= Dataset(ncdfs[im],'r') n =ncdf.variables['n'][:] idx_cols=ncdf.variables['idx_cols'][:,:] ncdf.close() if (im ==0 ): A_field = np.zeros((ncases,nlev),np.float32) for subc in range( 0, n[ire]): npoint=idx_cols[ire,n[subc]-1]-1 if (varis[iv] == 'THETA'): tmp = inptrs.variables['T'][0,:,npoint] hyam =inptrs.variables['hyam'][:] hybm =inptrs.variables['hybm'][:] ps=inptrs.variables['PS'][0,npoint] ps=ps p0=inptrs.variables['P0'] pre = np.zeros((nlev),np.float32) for il in range (0, nlev): pre[il] = hyam[il]*p0 + hybm[il] * ps tmp[il] = tmp[il] * (100000/pre[il])**0.286 theunits=str(cscale[iv])+inptrs.variables['T'].units else: tmp=inptrs.variables[varis[iv]][0,:,npoint] theunits=str(cscale[iv])+inptrs.variables[varis[iv]].units A_field[im,:] = (A_field[im,:]+tmp[:]/n[ire]).astype(np.float32 ) A_field[im,:] = A_field[im,:] *cscale[iv] inptrs.close() textres = Ngl.Resources() textres.txFontHeightF = 0.02 textres.txFont = _Font textres.txFontHeightF = 0.015 res.tiXAxisString = varis[iv]+ " Unit= " + cunits[iv] res.trXMinF = min(np.min(A_field[0, :]),np.min(B)) res.trXMaxF = max(np.max(A_field[0, :]),np.max(B)) if(varis[iv] == 'CLOUD') : if (lats[ire] >= 0 and lons[ire]<230): res.trXMinF = 0. res.trXMaxF = 50. else: res.trXMinF = 0. res.trXMaxF = 100. if(varis[iv] =='CLDLIQ') : if (lats[ire] >= 0): res.trXMinF = 0. res.trXMaxF = 0.1 else: res.trXMinF = 0. res.trXMaxF = 0.1 if(varis[iv] =='RELHUM') : res.trXMinF = 0. res.trXMaxF = 100. if(varis[iv] == 'THETA') : if (lats[ire] >= 0 and lons[ire]<230 ): res.trXMinF = 290. res.trXMaxF = 340. else: res.trXMinF = 280 res.trXMaxF = 320 if(varis[iv] == 'T') : res.trXMinF = 180 res.trXMaxF = 300 if(varis[iv] == 'U') : res.trXMinF = -40 res.trXMaxF = 40 res.trYReverse = True res.xyLineColors = np.arange(3,20,2) res.xyMarkerColors = np.arange(2,20,2) p = Ngl.xy(wks,A_field,lev,res) res.trYReverse = False res.xyLineColors = ['black'] pt = Ngl.xy(wks,B,pre1,res) Ngl.overlay(p,pt) plot.append(p) Ngl.panel(wks,plot[:],[nvaris/2,2],pres) txres = Ngl.Resources() txres.txFontHeightF = 0.02 txres.txFont = _Font Ngl.text_ndc(wks,'Large-scale VAR at '+ str(lons[ire])+'E,'+str(lats[ire])+'N',0.5,0.93,txres) Common_functions.create_legend(wks,localnames,0.02,np.append('black',np.arange(3,20,2)),0.25,0.85) Ngl.frame(wks) Ngl.destroy(wks) return plotlgs
# # Draw two vertical strings on left side of plot. # txres.txFontHeightF = 0.009 txres.txAngleF = 90. Ngl.text_ndc(wks,"cooling",0.11,0.835,txres) Ngl.text_ndc(wks,"warming",0.11,0.95,txres) Ngl.draw(plot) # Draw the plot; all of the attached lines and # text strings will also get drawn. #========================================================================== print "Beginning robinson map projection section" Ngl.define_colormap(wks,"wh-bl-gr-ye-re") mpres = Ngl.Resources() # New resource list for map plots. mpres.nglMaximize = False mpres.nglFrame = False mpres.vpWidthF = 0.5 mpres.vpHeightF = 0.32 mpres.vpXF = .11 mpres.vpYF = .75 mpres.sfXArray = lon mpres.sfYArray = lat mpres.mpProjection = "Robinson" # choose projection mpres.mpFillOn = False # turn on map fill
# Import necessary modules import numpy, os, Nio, Ngl # Open data file data_dir = Ngl.pynglpath("data") cdffile = Nio.open_file(os.path.join(data_dir,"cdf","941110_P.cdf"),"r") # Read data psl = cdffile.variables["Psl"] psl_lon = cdffile.variables["lon"][:] psl_lat = cdffile.variables["lat"][:] # Open workstation wks_type = "ps" wks = Ngl.open_wks(wks_type,"tickmark2") Ngl.define_colormap(wks,"BlueDarkRed18") # Set resources res = Ngl.Resources() #---Define X/Y values of data. res.sfXArray = psl_lon res.sfYArray = psl_lat #---Contour resources res.cnFillOn = True res.cnLineLabelsOn = False res.cnLevelSelectionMode = "ManualLevels" res.cnMinLevelValF = 950 res.cnMaxLevelValF = 1040
# A single visualization is produced showing the filled U.S. states. # # # Import numpy and os # import numpy,os # # Import Ngl,Nio support functions. # import Ngl, Nio wks_type = "ps" wks = Ngl.open_wks (wks_type,"shapefile1") Ngl.define_colormap(wks,"rainbow+gray") # # Map resources. # res = Ngl.Resources() res.mpProjection = "LambertConformal" res.mpLambertParallel1F = 33 # two parallels res.mpLambertParallel2F = 45 res.mpLambertMeridianF = -98 # central meridian res.mpLimitMode = "Corners" # limit map via two opposite corners res.mpLeftCornerLatF = 22 # left corner res.mpLeftCornerLonF = -125 # left corner
# # Draw two vertical strings on left side of plot. # txres.txFontHeightF = 0.009 txres.txAngleF = 90. Ngl.text_ndc(wks, "cooling", 0.11, 0.835, txres) Ngl.text_ndc(wks, "warming", 0.11, 0.95, txres) Ngl.draw(plot) # Draw the plot; all of the attached lines and # text strings will also get drawn. #========================================================================== print("Beginning robinson map projection section") Ngl.define_colormap(wks, "wh-bl-gr-ye-re") mpres = Ngl.Resources() # New resource list for map plots. mpres.nglMaximize = False mpres.nglFrame = False mpres.vpWidthF = 0.5 mpres.vpHeightF = 0.32 mpres.vpXF = .11 mpres.vpYF = .75 mpres.sfXArray = lon mpres.sfYArray = lat mpres.mpProjection = "Robinson" # choose projection mpres.mpFillOn = False # turn on map fill
def silhs_prf (ptype,cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs,casedir): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite varis = [ "SILHS_CLUBB_PRECIP_FRAC","SILHS_CLUBB_ICE_SS_FRAC","HR","AWNC","AWNI","ADSNOW","ANSNOW","AQSNOW","RHW","QRS","QRL","AQRAIN" ] nvaris = len(varis) cunits = ["%","mba/day","g/kg","g/kg","K", "%", "m/s", "g/kg", "m/s", "m/s","K","m" ] cscale = [1, 1, 1, 1, 1., 1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ] plotsilhs=["" for x in range(nsite)] for ire in range (0, nsite): if not os.path.exists(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N'): os.mkdir(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N') plotname = casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N/silhs_'+str(lons[ire])+"E_"+str(lats[ire])+"N_"+cseason plotsilhs[ire] = 'CLUBB_standard_'+str(lons[ire])+"E_"+str(lats[ire])+"N_"+cseason wks= Ngl.open_wks(ptype,plotname) Ngl.define_colormap(wks,"GMT_paired") plot = [] res = Ngl.Resources() res.nglDraw = False res.nglFrame = False res.lgLabelFontHeightF = .012 # change font height res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 res.txFontHeightF = .01 #res.vpXF = 0.04 # res.vpYF = 0.30 res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tmXBLabelFontHeightF = 0.005 res.tmXBLabelFontThicknessF = 1.0 res.tmXBLabelAngleF = 45 res.xyMarkLineMode = "Lines" res.xyLineThicknesses = [2.0, 2.0, 2.0, 2.0, 2.0, 2.0,2.,2.,2.,2.,2,2,2,2,2,2,2] res.xyDashPatterns = np.arange(0,24,1) # res.xyMarkers = np.arange(16,40,1) # res.xyMarkerSizeF = 0.005 pres = Ngl.Resources() pres.nglMaximize = True pres.txString = str(lons[ire])+"E,"+str(lats[ire])+"N" pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.93 txres = Ngl.Resources() txres.txFontHeightF = 0.01 for iv in range (0, nvaris): if(iv == nvaris-1): res.pmLegendDisplayMode = "ALWAYS" res.xyExplicitLegendLabels = casenames[:] res.pmLegendSide = "top" res.pmLegendParallelPosF = 0.6 res.pmLegendOrthogonalPosF = -0.5 res.pmLegendWidthF = 0.10 res.pmLegendHeightF = 0.10 res.lgLabelFontHeightF = .02 res.lgLabelFontThicknessF = 1.5 res.lgPerimOn = False else: res.pmLegendDisplayMode = "NEVER" for im in range (0,ncases): ncdfs[im] = './data/'+cases[im]+'_site_location.nc' infiles[im]= filepath[im]+cases[im]+'/'+cases[im]+'_'+cseason+'_climo.nc' inptrs = Dataset(infiles[im],'r') # pointer to file1 lat=inptrs.variables['lat'][:] nlat=len(lat) lon=inptrs.variables['lon'][:] nlon=len(lon) lev=inptrs.variables['ilev'][:] nlev=len(lev) ncdf= Dataset(ncdfs[im],'r') n =ncdf.variables['n'][:] idx_cols=ncdf.variables['idx_cols'][:,:] ncdf.close() if (im ==0): A_field = np.zeros((ncases,nlev),np.float32) for subc in range( 0, n[ire]): npoint=idx_cols[ire,n[subc]-1]-1 if (varis[iv] == 'THETA'): tmp = inptrs.variables['T'][0,:,npoint] hyam =inptrs.variables['hyam'][:] hybm =inptrs.variables['hybm'][:] ps=inptrs.variables['PS'][0,npoint] ps=ps p0=inptrs.variables['P0'] pre = np.zeros((nlev),np.float32) for il in range (0, nlev): pre[il] = hyam[il]*p0 + hybm[il] * ps tmp[il] = tmp[il] * (100000/pre[il])**0.286 theunits=str(cscale[iv])+inptrs.variables['T'].units else: tmp=inptrs.variables[varis[iv]][0,:,npoint] theunits=str(cscale[iv])+inptrs.variables[varis[iv]].units A_field[im,:] = (A_field[im,:]+tmp[:]/n[ire]).astype(np.float32 ) A_field[im,:] = A_field[im,:] *cscale[iv] inptrs.close() res.tiMainString = varis[iv]+" x"+theunits res.trXMinF = min(np.min(A_field[0, :]))#,np.min(B)) res.trXMaxF = max(np.max(A_field[0, :]))#,np.max(B)) if(varis[iv] == "THETA"): res.trXMinF = 280. res.trXMaxF = 400. if(varis[iv] == "CLOUD" or varis[iv] =="RELHUM") : res.trXMinF = 0. res.trXMaxF = 100. if(varis[iv] == "T") : res.trXMinF = 180 res.trXMaxF = 300 if(varis[iv] == "U") : res.trXMinF = -40 res.trXMaxF = 40 res.trYReverse = True res.xyLineColors = np.arange(2,20,1) res.xyMarkerColors = np.arange(2,20,2) p = Ngl.xy(wks,A_field,lev,res) plot.append(p) Ngl.panel(wks,plot[:],[nvaris/3,3],pres) txres = Ngl.Resources() txres.txFontHeightF = 0.020 Ngl.text_ndc(wks,"SILHS VAR at"+ str(lons[ire])+"E,"+str(lats[ire])+"N",0.5,0.05,txres) Ngl.frame(wks) Ngl.destroy(wks) return plotsilhs
losing_teams_nm.append(team) # Name of losing team losing_colors[nl, :] = numpy.array(teams[team]["colors"]) / 100. nl += 1 # Store the winning and losing counts in numpy arrays. y_win = numpy.array(winning_teams_ct) x_win = numpy.array(range(1, y_win.shape[0] + 1)) y_lose = numpy.array(losing_teams_ct) x_lose = numpy.array(range(1, y_lose.shape[0] + 1)) # # Start the graphics portion of the script. # wks_type = "png" wks = Ngl.open_wks(wks_type, "bar1") Ngl.define_colormap(wks, winning_colors) res = Ngl.Resources() res.nglMaximize = False # Need to set to False if using # vp resources. res.vpXF = 0.12 # Move plot to left a little. res.vpYF = 0.98 # Move plot up a little. res.vpHeightF = 0.90 # Make plot higher than res.vpWidthF = 0.80 # it is wide. res.tmXBOn = False # Turn off bottom tickmarks & labes res.tmXTOn = False # Turn off top tickmarks & labes res.tmYROn = False # Turn off right tickmarks & labes res.tmYLMinorOn = False # Turn off left minor tickmarks
def plot_Robinson_pyngl(var,lon,lat,wks_name='plot', clim=None, cspace=None, cmap=None): # # Select a colormap and open a workstation. # rlist = Ngl.Resources() wks_type = "png" wks = Ngl.open_wks(wks_type,wks_name,rlist) if cmap is None: mycmap = 'BlAqGrYeOrReVi200' Ngl.define_colormap(wks,mycmap) else: try: mycmap = '/Users/frederic/python/cmap/' + cmap mycmap = np.loadtxt(mycmap) except: mycmap = cmap try: Ngl.define_colormap(wks,mycmap) except: raise Warning, 'Unknown colormap' # # The next set of resources will apply to the contour plot and the labelbar. # resources = Ngl.Resources() resources.sfXArray = lon resources.sfYArray = lat resources.gsnMaximize = True # use full page resources.cnFillOn = True resources.cnFillMode = "RasterFill" resources.cnMaxLevelCount = 255 resources.cnLinesOn = False resources.cnLineLabelsOn = False if clim is not None: resources.cnLevelSelectionMode = "ManualLevels" # set manual contour levels resources.cnMinLevelValF = clim[0] # set min contour level resources.cnMaxLevelValF = clim[1] # set max contour level if cspace is None: LevelSpacing = (clim[1] - clim[0]) / 100. resources.cnLevelSpacingF = LevelSpacing # set contour spacing else: resources.cnLevelSpacingF = cspace # set contour spacing resources.lbOrientation = "Horizontal" # Default is vertical. resources.lbBoxLinesOn = False resources.lbLabelFontHeightF = 0.01 # label font height resources.lbBoxMinorExtentF = 0.15 # # The contour plot is not very interesting, so don't draw it. # resources.nglDraw = False resources.nglFrame = False contour = Ngl.contour(wks,var,resources) # # Retrieve the actual lat/lon end points of the scalar array so # we know where to overlay on map. # xs = Ngl.get_float(contour.sffield,"sfXCActualStartF") xe = Ngl.get_float(contour.sffield,"sfXCActualEndF") ys = Ngl.get_float(contour.sffield,"sfYCActualStartF") ye = Ngl.get_float(contour.sffield,"sfYCActualEndF") resources.nglDraw = True # Turn these resources back on. resources.nglFrame = True resources.mpProjection = "Robinson" resources.mpCenterLonF = 270 resources.mpCenterLatF = 0 resources.mpGeophysicalLineThicknessF = 2 map = Ngl.contour_map(wks,var,resources) Ngl.end()
def draw_micro_bgt(ptype, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir, varis, vname, cscale, chscale, pname, dofv): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = 'Red' mkres.gsMarkerSizeF = 15. infiles = ['' for x in range(ncases)] ncdfs = ['' for x in range(ncases)] nregions = nsite nvaris = len(varis) plotmicrobgt = ['' for x in range(nsite * ncases)] for ire in range(0, nsite): for im in range(0, ncases): if not os.path.exists(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N'): os.mkdir(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N') plotname = casedir + '/' + str(lons[ire]) + 'E_' + str( lats[ire]) + 'N/' + pname + '_' + casenames[im] + '_' + str( lons[ire]) + 'E_' + str(lats[ire]) + 'N_' + cseason plotmicrobgt[ im + ncases * ire] = pname + '_' + casenames[im] + '_' + str( lons[ire]) + 'E_' + str(lats[ire]) + 'N_' + cseason wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, 'radar') plot = [] res = Ngl.Resources() res.nglDraw = False res.nglFrame = False res.lgLabelFontHeightF = .012 # change font height res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 # res.txFontHeightF = .01 # res.vpXF = 0.04 # res.vpYF = 0.30 res.tmYLLabelFont = 12 res.tmXBLabelFont = 12 res.tmXBLabelFontHeightF = 0.01 res.tmXBLabelFontThicknessF = 1.0 res.xyMarkLineMode = 'MarkLines' res.xyLineThicknesses = [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3., 3., 3., 3., 3, 3, 3, 3, 3, 3, 3 ] res.xyLineColors = np.arange(2, 16, 1) res.xyDashPatterns = np.arange(0, 24, 1) res.xyMarkers = np.arange(16, 40, 1) res.xyMarkerSizeF = 0.005 res.xyMarkerColors = np.arange(2, 16, 1) res.pmLegendDisplayMode = 'ALWAYS' res.pmLegendSide = 'top' # Change location of res.pmLegendParallelPosF = 0.6 # move units right res.pmLegendOrthogonalPosF = -0.55 # more neg = down res.pmLegendWidthF = 0.2 # Decrease width res.pmLegendHeightF = 0.1 # Decrease height res.lgBoxMinorExtentF = 0.1 # Shorten the legend lines res.lgLabelFontHeightF = 0.015 # Change the font size res.lgPerimOn = True res.tiYAxisString = 'PRESSURE' # res.nglLeftString = varis[iv] # res.nglRightString = cunits[iv] res.trYReverse = True pres = Ngl.Resources() # pres.nglMaximize = True pres.nglFrame = False pres.txFont = 12 pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.93 txres = Ngl.Resources() # txres.txFontHeightF = 0.01 for iv in range(0, nvaris): if (varis[iv] == 'MPDLIQ'): # LIQ budget_ends = [ 'PRCO', 'PRAO', 'MNUCCCO', 'MNUCCTO', 'MSACWIO', 'PSACWSO', 'BERGSO', 'BERGO' ] # in fortran prc*cld, pra*cld,mnuccc*cld,mnucct*cld,msacwi*cld,psacws*cld,bergs*cld, berg # (-pra-prc-mnuccc-mnucct-msacwi- psacws-bergs)*lcldm-berg nterms = len(budget_ends) if (varis[iv] == 'MPDICE'): # ICE budget_ends = [ 'PRCIO', 'PRAIO', 'MSACWIO', 'MNUCCCO', 'MNUCCTO', 'mnudepo', 'BERGO', 'CMEIOUT', 'mnuccrio' ] # in fortran prci*cld,prai*cld,msacwi*cld,mnuccc*cld, mnucct*cld, berg, vap_dep + ice_sublim + mnuccd # (mnuccc+mnucct+mnudep+msacwi)*lcldm+(-prci-prai)*icldm+(vap_dep+ice_sublim+mnuccd)+berg+mnuccri*precip_frac nterms = len(budget_ends) if (varis[iv] == 'QRSEDTEN'): # RAIN budget_ends = [ 'PRAO', 'PRCO', 'PRACSO', 'EVAPPREC', 'MNUCCRO', 'mnuccrio' ] # pra*cld,prc*cld, psacs*prf, -pre*prf(nevapr),mnuccr*prf # (pra+prc)*lcldm+(pre-pracs- mnuccr-mnuccri)*precip_frac nterms = len(budget_ends) if (varis[iv] == 'QSSEDTEN'): # SNOW budget_ends = [ 'PRAIO', 'PRCIO', 'PSACWSO', 'PRACSO', 'EVAPSNOW', 'MNUCCRO', 'BERGSO' ] # prai*cld,prci*cld,psacws*cld,psacs*prf, -prds*prc, mnuccr*prf,bergs*cld # (prai+prci)*icldm+(psacws+bergs)*lcldm+(prds+ pracs+mnuccr)*precip_frac nterms = len(budget_ends) if (varis[iv] == 'QISEVAP'): # Vapor budget_ends = [ 'EVAPPREC', 'EVAPSNOW', 'CMEIOUT', 'mnudepo' ] # -pre*prf(nevapr),-prds*prc ,vap_dep + ice_sublim + mnuccd # -(pre+prds)*precip_frac-vap_dep-ice_sublim-mnuccd-mnudep*lcldm nterms = len(budget_ends) if (varis[iv] == 'nnuccco'): # NUM of LIQ budget_ends = [ 'nnuccco', 'nnuccto', 'npsacwso', 'nsubco', 'nprao', 'nprc1o' ] # nnuccc*cld,nnucct*cld,npsacws*cld,nsubc*cld,npra*cld,nprc1*cld # (-nnuccc-nnucct-npsacws+nsubc-npra-nprc1)*lcldm nterms = len(budget_ends) if (varis[iv] == 'nnuccdo'): # NUM of ICE budget_ends = [ 'nnuccdo', 'nnuccto', 'tmpfrzo', 'nnudepo', 'nsacwio', 'nsubio', 'nprcio', 'npraio', 'nnuccrio', 'DETNICETND' ] # nnuccd ,nnucct*lcld,tmpfrz*lcld,nnudep*lcld,nsacwi*lcld,nsubi*icld,nprci*icld,nprai*icld,nnuccri*prf # nnuccd+ (nnucct+tmpfrz+nnudep+nsacwi)*lcldm+(nsubi-nprci- nprai)*icldm+nnuccri*precip_frac nterms = len(budget_ends) ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) ilev = inptrs.variables['lev'][:] nilev = len(ilev) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:, :] if (dofv): idx_lats = ncdf.variables['idx_coord_lat'][:, :] idx_lons = ncdf.variables['idx_coord_lon'][:, :] ncdf.close() A_field = np.zeros((nterms, nilev), np.float32) theunits = str( chscale[iv]) + 'x' + inptrs.variables[varis[iv]].units res.tiMainString = vname[iv] + ' ' + theunits for it in range(0, nterms): for subc in range(0, n[ire]): varis_bgt = budget_ends[it] npoint = idx_cols[ire, n[subc] - 1] - 1 if (dofv): npointlat = idx_lats[ire, 0] npointlon = idx_lons[ire, 0] if (dofv): tmp = inptrs.variables[varis_bgt][ 0, :, npointlat, npointlon] #/n[ire] else: tmp = inptrs.variables[varis_bgt][0, :, npoint] #/n[ire] tmp = tmp * cscale[iv] if (dofv): lcldm = inptrs.variables['CLOUD'][0, :, npointlat, npointlon] else: lcldm = inptrs.variables['CLOUD'][0, :, npoint] icldm = lcldm if (dofv): precip_frac = inptrs.variables['FREQR'][0, :, npointlat, npointlon] else: precip_frac = inptrs.variables['FREQR'][0, :, npoint] if (varis_bgt == 'MPDT' or varis_bgt == 'STEND_CLUBB'): tmp = tmp / 1004 if (varis[iv] == 'MPDLIQ'): # LIQ if (varis_bgt == 'PRCO' or varis_bgt == 'PRCIO' or varis_bgt == 'PRAO' or varis_bgt == 'PRAICSO' or varis_bgt == 'PRAIO' \ or varis_bgt == 'MNUCCCO' or varis_bgt == 'MNUCCTO' or varis_bgt == 'MSACWIO' or varis_bgt == 'PSACWSO' or varis_bgt == 'BERGSO' or varis_bgt == 'BERGO'): tmp = tmp * (-1) if (varis[iv] == 'MPDICE'): # ICE if (varis_bgt == 'PRCIO' or varis_bgt == 'PRAIO'): tmp = tmp * (-1) if (varis[iv] == 'QRSEDTEN'): # RAIN if (varis_bgt == 'MNUCCRO' or varis_bgt == 'PRACSO' or varis_bgt == 'EVAPPREC' or varis_bgt == 'mnuccrio'): tmp = tmp * (-1) if (varis[iv] == 'QSSEDTEN'): # SNOW if (varis_bgt == 'EVAPSNOW'): tmp = tmp * (-1) if (varis[iv] == 'QISEVAP'): # Vapor if (varis_bgt == 'CMEIOUT' or varis_bgt == 'mnudepo'): tmp = -1 * tmp if (varis[iv] == 'nnuccco'): # NUM of LIQ if (varis_bgt == 'nnuccco' or varis_bgt == 'nnuccto' or varis_bgt == 'npsacwso' or varis_bgt == 'nprao' or varis_bgt == 'nprc1o'): tmp = -1 * tmp if (varis[iv] == 'nnuccdo'): # NUM of ICE if (varis_bgt == 'nprcio' or varis_bgt == 'npraio'): tmp = -1 * tmp A_field[it, :] = (A_field[it, :] + tmp[:] / n[ire]).astype(np.float32) inptrs.close() res.xyExplicitLegendLabels = budget_ends[:] p = Ngl.xy(wks, A_field, ilev, res) plot.append(p) xp = np.mod(iv, 2) yp = int(iv / 2) pres.txFontHeightF = 0.02 pres.txFont = _Font pres.txString = casenames[im] + ' Microphy BUDGET at' + str( lons[ire]) + 'E,' + str(lats[ire]) + 'N' if (np.mod(nvaris, 2) == 1): Ngl.panel(wks, plot[:], [(nvaris) / 2 + 1, 2], pres) else: Ngl.panel(wks, plot[:], [(nvaris) / 2, 2], pres) txres = Ngl.Resources() txres.txFontHeightF = 0.020 txres.txFont = _Font Ngl.text_ndc( wks, casenames[im] + ' Microphy BUDGET at' + str(lons[ire]) + 'E,' + str(lats[ire]) + 'N', 0.5, 0.95, txres) Ngl.frame(wks) Ngl.destroy(wks) return (plotmicrobgt)
def plot_Robinson_pyngl(var, lon, lat, wks_name='plot', clim=None, cspace=None, cmap=None): # # Select a colormap and open a workstation. # rlist = Ngl.Resources() wks_type = "png" wks = Ngl.open_wks(wks_type, wks_name, rlist) if cmap is None: mycmap = 'BlAqGrYeOrReVi200' Ngl.define_colormap(wks, mycmap) else: try: mycmap = '/Users/frederic/python/cmap/' + cmap mycmap = np.loadtxt(mycmap) except: mycmap = cmap try: Ngl.define_colormap(wks, mycmap) except: raise Warning('Unknown colormap') # # The next set of resources will apply to the contour plot and the labelbar. # resources = Ngl.Resources() resources.sfXArray = lon resources.sfYArray = lat resources.gsnMaximize = True # use full page resources.cnFillOn = True resources.cnFillMode = "RasterFill" resources.cnMaxLevelCount = 255 resources.cnLinesOn = False resources.cnLineLabelsOn = False if clim is not None: resources.cnLevelSelectionMode = "ManualLevels" # set manual contour levels resources.cnMinLevelValF = clim[0] # set min contour level resources.cnMaxLevelValF = clim[1] # set max contour level if cspace is None: LevelSpacing = (clim[1] - clim[0]) / 100. resources.cnLevelSpacingF = LevelSpacing # set contour spacing else: resources.cnLevelSpacingF = cspace # set contour spacing resources.lbOrientation = "Horizontal" # Default is vertical. resources.lbBoxLinesOn = False resources.lbLabelFontHeightF = 0.01 # label font height resources.lbBoxMinorExtentF = 0.15 # # The contour plot is not very interesting, so don't draw it. # resources.nglDraw = False resources.nglFrame = False contour = Ngl.contour(wks, var, resources) # # Retrieve the actual lat/lon end points of the scalar array so # we know where to overlay on map. # xs = Ngl.get_float(contour.sffield, "sfXCActualStartF") xe = Ngl.get_float(contour.sffield, "sfXCActualEndF") ys = Ngl.get_float(contour.sffield, "sfYCActualStartF") ye = Ngl.get_float(contour.sffield, "sfYCActualEndF") resources.nglDraw = True # Turn these resources back on. resources.nglFrame = True resources.mpProjection = "Robinson" resources.mpCenterLonF = 270 resources.mpCenterLatF = 0 resources.mpGeophysicalLineThicknessF = 2 map = Ngl.contour_map(wks, var, resources) Ngl.end()
# f = Nio.open_file(os.path.join(Ngl.pynglpath("data"), "cdf", "hgt.nc"), "r") hgt = f.variables["HGT"][:, :, :] lat = Ngl.fspan(-90, 90, 73) lon = Ngl.fspan(0, 357.5, 144) # Add a cyclic point in the longitude dimension. hgt0, lon = Ngl.add_cyclic(hgt[0, :, :], lon) # # Start graphics. # wks_type = "png" wks = Ngl.open_wks(wks_type, "spaghetti") Ngl.define_colormap(wks, "default") # Change color map. mpres = Ngl.Resources() mpres.nglDraw = False # Do not draw until the end. mpres.nglFrame = False # Do not automatically advance frame. # # Set contour resources first, because we'll make a copy of them # to use later for when we generate contours only. # mpres.sfXArray = lon # Area of map to overlay mpres.sfYArray = lat # contours on. mpres.cnLevelSelectionMode = "ExplicitLevels" # explicit contour levels mpres.cnLevels = 5500 # which level(s) to plot
from __future__ import print_function import numpy, os, Nio, Ngl # Open data file data_dir = Ngl.pynglpath("data") cdffile = Nio.open_file(os.path.join(data_dir, "cdf", "941110_P.cdf"), "r") # Read data psl = cdffile.variables["Psl"][:] psl_lon = cdffile.variables["lon"][:] psl_lat = cdffile.variables["lat"][:] # Open workstation wks_type = "png" wks = Ngl.open_wks(wks_type, "tickmark2") Ngl.define_colormap(wks, "BlueDarkRed18") # Could also set # res.cnFillPalette = "BlueDarkRed18" # Set resources res = Ngl.Resources() #---Define X/Y values of data. res.sfXArray = psl_lon res.sfYArray = psl_lat #---Contour resources res.cnFillOn = True res.cnLineLabelsOn = False res.cnLevelSelectionMode = "ManualLevels" res.cnMinLevelValF = 950 res.cnMaxLevelValF = 1040
# # Perimeter. # pres.gsLineColor = 1 Ngl.polyline_ndc(wks, xx, yy, pres) # # Show how to put four plots, illustrating wind barb procedures, # on a single frame. # wks_type = "png" wks = Ngl.open_wks(wks_type, "wmstnm02") cmap = numpy.array([[1., 1., 1.], [0., 0., 0.], [1., 0., 0.]]) # color map Ngl.define_colormap(wks, cmap) ############################# # # # Lower left quatrant. # # # ############################# # # Use the procedure wmbarb to produce a table of the association # of wind barb symbols with wind speeds. This plot simply uses # NDC coordinates - the three other plots show how to plot # wind barbs over maps and position those plots on the frame. # width = 0.42 height = 0.42 xl = 0.06
def draw_clm_plot (ptype,cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs,casedir): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) if not os.path.exists(casedir+"/2D"): os.mkdir(casedir+"/2D") _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] varis = ["SOILWATER_10CM", "QBOT"] varisobs = ["PRECT" ,"PRECT"] alpha = ["A","B","C","D","E","F"] nvaris = len(varis) cunits = [""] cscale = [1,1,1,86400000, 1, 1,1,100,100,1,1,1000,1,1,1,1] cscaleobs = [1,1,1,1,1,1,1,1,1,1,1,1,1,1] # cntrs = [[0 for col in range(11)] for row in range(nvaris)] cntrs = np.zeros((nvaris,11),np.float32) obsdataset= ["GPCP","GPCP", "NCEP", "ERAI", "CALIPSOCOSP","CALIPSOCOSP","NCEP","NCEP","NVAP"] plotclm=["" for x in range(nvaris)] for iv in range(0, nvaris): # make plot for each field if(varis[iv] == "SOILWATER_10CM" or varis[iv] == "QBOT" ): cntrs[iv,:] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] # Observational data if(obsdataset[iv] =="CCCM"): if(cseason == "ANN"): fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-"+cseason+".nc" else: fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-2010-"+cseason+".nc" else: if (varisobs[iv] =="PRECT"): fileobs = filepathobs+'/GPCP_'+cseason+'_climo.nc' else: fileobs = filepathobs + obsdataset[iv]+'_'+cseason+'_climo.nc' # infiles[im]=filepath[im]+cases[im]+'/run/'+cases[im]+'_'+cseason+'_climo.nc' inptrobs = Dataset(fileobs,'r') latobs=inptrobs.variables['lat'][:] lonobs=inptrobs.variables['lon'][:] if (varisobs[iv] =="U10"): B0=inptrobs.variables[varisobs[iv]][0,:,:] B1=inptrobs.variables["V10"][0,:,:] B=(B0*B0+B1*B1) B=B * cscaleobs[iv] B=np.sqrt(B) else: B=inptrobs.variables[varisobs[iv]][0,:,:] B=B * cscaleobs[iv] #************************************************ # create plot #************************************************ plotname = casedir+'/2D/Land_'+varis[iv]+'_'+cseason plotclm[iv] = 'Land_'+varis[iv]+'_'+cseason wks= Ngl.open_wks(ptype,plotname) Ngl.define_colormap(wks,"cmocean_thermal") # ngl_define_colormap(wks,"prcp_1") plot = [] textres = Ngl.Resources() textres.txFontHeightF = 0.02 # Size of title. textres.txFont = _Font Ngl.text_ndc(wks,varis[iv],0.1,.97,textres) pres = Ngl.Resources() pres.nglMaximize = True pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelBottom = 0.20 pres.nglPanelTop = 0.9 pres.pmLabelBarWidthF = 0.8 pres.nglFrame = False pres.nglPanelLabelBar = True # Turn on panel labelbar pres.nglPanelLabelBarLabelFontHeightF = 0.015 # Labelbar font height pres.nglPanelLabelBarHeightF = 0.0750 # Height of labelbar pres.nglPanelLabelBarWidthF = 0.700 # Width of labelbar pres.lbLabelFont = "helvetica-bold" # Labelbar font pres.nglPanelTop = 0.935 pres.nglPanelFigureStrings = alpha pres.nglPanelFigureStringsJust = "BottomRight" res = Ngl.Resources() res.nglDraw = False #-- don't draw plots res.nglFrame = False res.cnFillOn = True res.cnFillMode = "RasterFill" res.cnLinesOn = False res.nglMaximize = True res.mpFillOn = True res.mpCenterLonF = 180 res.tiMainFont = _Font res.tiMainFontHeightF = 0.025 res.tiXAxisString = "" res.tiXAxisFont = _Font res.tiXAxisFontHeightF = 0.025 res.tiYAxisString = "" res.tiYAxisFont = _Font res.tiYAxisOffsetXF = 0.0 res.tiYAxisFontHeightF = 0.025 res.tmXBLabelFont = _Font res.tmYLLabelFont = _Font res.tiYAxisFont = _Font res.vpWidthF = 0.80 # set width and height res.vpHeightF = 0.40 res.vpXF = 0.04 res.vpYF = 0.30 res.cnInfoLabelOn = False res.cnFillOn = True res.cnLinesOn = False res.cnLineLabelsOn = False res.lbLabelBarOn = False res.mpFillOn = False res.cnLevelSelectionMode = "ExplicitLevels" res.cnLevels = cntrs[iv][:] for im in range(0, ncases): ncdfs[im] = './data/'+cases[im]+'_site_location.nc' infiles[im]= filepath[im]+cases[im]+'/'+cases[im]+'_'+cseason+'_clm.nc' inptrs = Dataset(infiles[im],'r') # pointer to file1 lat=inptrs.variables['lat'][:] nlat=len(lat) lon=inptrs.variables['lon'][:] nlon=len(lon) area=inptrs.variables['area'][:] area_wgt = np.zeros(nlat) sits=np.linspace(0,nsite-1,nsite) ncdf= Dataset(ncdfs[im],'r') n =ncdf.variables['n'][:] idx_cols=ncdf.variables['idx_cols'][:] if (varis[iv] == 'PRECT'): A = inptrs.variables['PRECC'][0,:]+inptrs.variables['PRECL'][0,:] else: A = inptrs.variables[varis[iv]][0,:] if (varis[iv] == 'FLUT'): A = inptrs.variables['FLUT'][0,:]-inptrs.variables['FLNS'][0,:] else: A = inptrs.variables[varis[iv]][0,:] if (varis[iv] == 'U10'): A = inptrs.variables['U10'][0,:]*inptrs.variables['U10'][0,:] A = np.sqrt(A) else: A = inptrs.variables[varis[iv]][0,:] A_xy=A A_xy = A_xy * cscale[iv] ncdf.close() inptrs.close() if im == 0 : dsizes = len(A_xy) field_xy = [[0 for col in range(dsizes)] for row in range(ncases)] field_xy[im][:] = A_xy res.lbLabelBarOn = False if(np.mod(im,2)==0): res.tiYAxisOn = True else: res.tiYAxisOn = False res.tiXAxisOn = False res.sfXArray = lon res.sfYArray = lat res.mpLimitMode = "LatLon" res.mpMaxLonF = max(lon) res.mpMinLonF = min(lon) res.mpMinLatF = min(lat) res.mpMaxLatF = max(lat) res.tiMainString = "GLB="+str(np.sum(A_xy[:]*area[:]/np.sum(area))) textres.txFontHeightF = 0.015 Ngl.text_ndc(wks,alpha[im]+" "+ casenames[im],0.3,.135-im*0.03,textres) # varM = A_xy # for ix in range(0, nlon-1): # if ( np.isnan(A_xy[ix]) or not np.isfinite(A_xy[ix])): # varM[ix]=0.0 p = Ngl.contour_map(wks,A_xy,res) plot.append(p) res.lbLabelFont = _Font res.tiYAxisOn = True res.tiXAxisOn = True res.tiXAxisFont = _Font rad = 4.0*np.arctan(1.0)/180.0 re = 6371220.0 rr = re*rad dlon = abs(lonobs[2]-lonobs[1])*rr dx = dlon* np.cos(latobs*rad) jlat = len(latobs ) dy = np.zeros(jlat,dtype=float) # close enough dy[0] = abs(lat[2]-lat[1])*rr dy[1:jlat-2] = abs(lat[2:jlat-1]-lat[0:jlat-3])*rr*0.5 dy[jlat-1] = abs(lat[jlat-1]-lat[jlat-2])*rr area_wgt = dx*dy # is_SE= False sum1 = 0 sum2 = 0 for j in range(0, jlat-1): for i in range(0, len(lonobs)-1): if (np.isnan(B[j][i]) != "--"): sum1= sum1+area_wgt[j]*B[j][i] sum2= sum2+area_wgt[j] glb=sum1/sum2 res.sfXArray = lonobs res.sfYArray = latobs res.mpLimitMode = "LatLon" res.mpMaxLonF = max(lonobs) res.mpMinLonF = min(lonobs) res.mpMinLatF = min(latobs) res.mpMaxLatF = max(latobs) res.tiMainString = "GLB="+str(glb) p =Ngl.contour_map(wks,B,res) plot.append(p) if(np.mod(ncases+1,2)==1): Ngl.panel(wks,plot[:],[(ncases+1)/2+1,2],pres) else: Ngl.panel(wks,plot[:],[(ncases+1)/2,2],pres) Ngl.frame(wks) Ngl.destroy(wks) # Ngl.end() return plotclm
def clubb_skw_prf(ptype, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir, dofv): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite varis = [ "C6rt_Skw_fnc", "C11_Skw_fnc", "C1_Skw_fnc", "C7_Skw_fnc", "Lscale", "Richardson_num", "Kh_zm", "tau_zm", "Skw_velocity" ] varisobs = [ "CC_ISBL", "OMEGA", "SHUM", "CLWC_ISBL", "THETA", "RELHUM", "U", "CIWC_ISBL", "T" ] nvaris = len(varis) cunits = [ "%", "mba/day", "g/kg", "g/kg", "K", "%", "m/s", "g/kg", "m/s", "m/s", "K", "m" ] cscale = [ 1, 1, 1, 1, 1., 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] cscaleobs = [100, 1, 1, 1000, 1., 1, 1, 1000, 1, 1, 1, 1, 1, 1, 1] obsdataset = [ "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI" ] plotskw = ["" for x in range(nsite)] for ire in range(0, nsite): if not os.path.exists(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N'): os.mkdir(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N') plotname = casedir + '/' + str(lons[ire]) + 'E_' + str( lats[ire]) + 'N/CLUBB_skewfunc_' + str(lons[ire]) + "E_" + str( lats[ire]) + "N_" + cseason plotskw[ire] = 'CLUBB_skewfunc_' + str(lons[ire]) + "E_" + str( lats[ire]) + "N_" + cseason wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, "GMT_paired") plot = [] res = Ngl.Resources() res.nglDraw = False res.nglFrame = False res.lgLabelFontHeightF = .012 # change font height res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 #res.vpXF = 0.04 # res.vpYF = 0.30 res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tmXBLabelFontHeightF = 0.01 res.tmXBLabelFontThicknessF = 1.0 # res.tmXBLabelAngleF = 45 res.xyMarkLineMode = "Lines" res.xyLineThicknesses = [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3., 3., 3., 3., 3, 3, 3, 3, 3, 3, 3 ] res.xyDashPatterns = np.arange(0, 24, 1) # res.xyMarkers = np.arange(16,40,1) # res.xyMarkerSizeF = 0.005 pres = Ngl.Resources() # pres.nglMaximize = True pres.nglFrame = False pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.88 pres.wkWidth = 2500 pres.wkHeight = 2500 for iv in range(0, nvaris): if (iv == nvaris - 1): res.pmLegendDisplayMode = "NEVER" res.xyExplicitLegendLabels = casenames[:] res.pmLegendSide = "top" res.pmLegendParallelPosF = 0.6 res.pmLegendOrthogonalPosF = -0.5 res.pmLegendWidthF = 0.10 res.pmLegendHeightF = 0.10 res.lgLabelFontHeightF = .02 res.lgLabelFontThicknessF = 1.5 res.lgPerimOn = False else: res.pmLegendDisplayMode = "NEVER" # if(obsdataset[iv] =="CCCM"): # if(cseason == "ANN"): # fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-"+cseason+".nc" # else: # fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-2010-"+cseason+".nc" # inptrobs = Dataset(fileobs,'r') # B=inptrobs.variables[varisobs[iv]][:,(lats[ire]),(lons[ire])] # else: # if (varisobs[iv] =="PRECT"): # fileobs = filepathobs+'/GPCP_'+cseason+'_climo.nc' # else: # fileobs = filepathobs + obsdataset[iv]+'_'+cseason+'_climo.nc' # inptrobs = Dataset(fileobs,'r') # if (varisobs[iv] =="THETA"): # B = inptrobs.variables['T'][0,:,(lats[ire]),(lons[ire])] # pre1 = inptrobs.variables['lev'][:] # for il1 in range (0, len(pre1)): # B[il1] = B[il1]*(1000/pre1[il1])**0.286 # else: # pre1 = inptrobs.variables['lev'][:] # B = inptrobs.variables[varisobs[iv]][0,:,(lats[ire]),(lons[ire])] # # B[:]=B[:] * cscaleobs[iv] for im in range(0, ncases): ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) lev = inptrs.variables['ilev'][:] nlev = len(lev) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:, :] if (dofv): idx_lats = ncdf.variables['idx_coord_lat'][:, :] idx_lons = ncdf.variables['idx_coord_lon'][:, :] ncdf.close() if (im == 0): A_field = np.zeros((ncases, nlev), np.float32) for subc in range(0, n[ire]): npoint = idx_cols[ire, n[subc] - 1] - 1 if (dofv): npointlat = idx_lats[ire, 0] npointlon = idx_lons[ire, 0] if (dofv): tmp = inptrs.variables[varis[iv]][0, :, npointlat, npointlon] else: tmp = inptrs.variables[varis[iv]][0, :, npoint] theunits = str( cscale[iv]) + inptrs.variables[varis[iv]].units A_field[im, :] = (A_field[im, :] + tmp[:] / n[ire]).astype( np.float32) A_field[im, :] = A_field[im, :] * cscale[iv] inptrs.close() res.tiMainString = varis[iv] + " " + theunits # res.trXMinF = min(np.min(A_field[0, :])) # res.trXMaxF = max(np.max(A_field[0, :])) res.trYReverse = True res.xyLineColors = np.arange(3, 20, 2) res.xyMarkerColors = np.arange(2, 20, 2) p = Ngl.xy(wks, A_field, lev, res) # res.trYReverse = False # res.xyLineColors = ["black"] # pt = Ngl.xy(wks,B,pre1,res) # Ngl.overlay(p,pt) plot.append(p) pres.txString = "Skewness Functions at" + str(lons[ire]) + "E," + str( lats[ire]) + "N" txres = Ngl.Resources() txres.txFontHeightF = 0.020 txres.txFont = _Font Ngl.text_ndc( wks, "Skewness Functions at" + str(lons[ire]) + "E," + str(lats[ire]) + "N", 0.5, 0.92 + ncases * 0.01, txres) Common_functions.create_legend(wks, casenames, 0.02, np.arange(3, 20, 2), 0.1, 0.89 + ncases * 0.01) Ngl.panel(wks, plot[:], [nvaris / 3, 3], pres) Ngl.frame(wks) Ngl.destroy(wks) return plotskw
#---------------------------------------------------------------------- # Main code #---------------------------------------------------------------------- # Read variables f = Nio.open_file("pres.mon.ltm.nc") pres = f.variables["pres"][0, :, :] lat = f.variables["lat"][:] lon = f.variables["lon"][:] #print(pres) #---------------------------------------------------------------------- wks = Ngl.open_wks("x11", "PyNGL_China_boundary") Ngl.define_colormap(wks, "gui_default") res = Ngl.Resources() res.nglMaximize = True res.nglDraw = False res.nglFrame = False #--------------------------------------------------------------- # set for the map res.sfXArray = lon res.sfYArray = lat res.mpLimitMode = "LatLon" res.mpMinLatF = 17. res.mpMaxLatF = 55. res.mpMinLonF = 72.
def draw_clubb_tau(ptype, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir, varis, vname, cscale, chscale, pname): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite nvaris = len(varis) plottau = ["" for x in range(nsite * ncases)] alphas = [ 'a) ', 'b) ', 'c) ', 'd) ', 'e) ', 'f) ', 'g) ', 'h) ', 'i) ', 'j) ', 'k) ', 'l) ', 'm) ', 'n) ', 'o) ', 'p) ', 'q) ', 'r) ', 's) ', 't) ', 'u) ', 'v) ', 'w) ', 'x) ', 'y) ', 'z) ' ] for ire in range(0, nsite): for im in range(0, ncases): if not os.path.exists(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N'): os.mkdir(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N') plotname = casedir + '/' + str(lons[ire]) + 'E_' + str( lats[ire]) + 'N/' + pname + '_' + casenames[im] + "_" + str( lons[ire]) + "E_" + str(lats[ire]) + "N_" + cseason plottau[im + ncases * ire] = pname + '_' + casenames[im] + "_" + str( lons[ire]) + "E_" + str(lats[ire]) + "N_" + cseason wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, "radar") plot = [] res = Ngl.Resources() res.nglMaximize = False res.nglDraw = False res.nglFrame = False res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 # if (lats[ire] > 0 and lons[ire]!=240 ): # res.trYMinF = 400. # res.trYMaxF = 1000. # else: res.trYMinF = 100. res.trYMaxF = 1000. res.tiMainFont = _Font res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tiYAxisFont = _Font res.tiXAxisFont = _Font res.tmXBLabelFontHeightF = 0.01 res.tmXBLabelFontThicknessF = 1.0 res.xyMarkLineMode = "MarkLines" res.xyLineThicknesses = [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3., 2., 2., 2., 2, 2, 2, 2, 2, 2, 2 ] res.xyDashPatterns = np.arange(0, 24, 1) res.xyMarkers = np.arange(16, 40, 1) res.xyMarkerSizeF = 0.005 res.pmLegendDisplayMode = "Never" res.pmLegendSide = "top" # Change location of res.pmLegendParallelPosF = 0.75 # move units right res.pmLegendOrthogonalPosF = -0.65 # more neg = down res.pmLegendWidthF = 0.1 # Change width and res.pmLegendHeightF = 0.15 # height of legend. res.lgLabelFontHeightF = .02 # change font height res.lgLabelFontThicknessF = 1. # res.lgBoxMinorExtentF = 0.2 res.lgPerimOn = True res.tiYAxisString = "Pressure [hPa]" res.trYReverse = True pres = Ngl.Resources() pres.nglFrame = False pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.93 pres.txFont = _Font pres.nglMaximize = False pres.txFont = _Font pres.nglPanelTop = 0.935 pres.nglPanelFigureStrings = alphas pres.nglPanelFigureStringsJust = 'Topright' pres.nglPanelFigureStringsFontHeightF = 0.015 for iv in range(0, nvaris): if (varis[iv] == 'tau_no_N2_zm'): budget_ends = ['tau_no_N2_zm', 'bkgnd', 'sfc', 'shear'] budget_name = [ '1/~F8~t~N~~F10~~B~noN2~N~', '1/~F8~t~N~~F10~~B~bkgnd~N~', '1/~F8~t~N~~F10~~B~surf~N~', '1/~F8~t~N~~F10~~B~shear~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_xp2_zm'): budget_ends = ['tau_xp2_zm', 'tau_no_N2_zm'] budget_name = [ "1/~F8~t~N~~F10~~B~x'~S~2~N~~N~", '1/~F8~t~N~~F10~~B~noN2~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_wp2_zm'): budget_ends = ['tau_wp2_zm', 'tau_no_N2_zm', 'bvpos'] budget_name = [ "1/~F8~t~N~~F10~~B~w'~S~2~N~~N~", '1/~F8~t~N~~F10~~B~noN2~N~', '1/~F8~t~N~~F10~~B~bv~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_wpxp_zm'): budget_ends = [ 'tau_wpxp_zm', 'tau_no_N2_zm', 'bvpos', 'clear' ] budget_name = [ "1/~F8~t~N~~F10~~B~w'x'~N~", "1/~F8~t~N~~F10~~B~noN2~N~", '1/~F8~t~N~~F10~~B~bv~N~', '1/~F8~t~N~~F10~~B~clr~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_wp3_zm'): budget_ends = [ 'tau_wpxp_zm', 'tau_no_N2_zm', 'bvpos', 'clear' ] budget_name = [ "1/~F8~t~N~~F10~~B~w'x'~N~", '1/~F8~t~N~~F10~~B~noN2~N~', '1/~F8~t~N~~F10~~B~bv~N~', '1/~F8~t~N~~F10~~B~clr~N~' ] nterms = len(budget_ends) ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + cases[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) ilev = inptrs.variables['ilev'][:] nilev = len(ilev) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:, :] ncdf.close() A_field = np.zeros((nterms, nilev), np.float32) if (varis[iv] == 'tau_no_N2_zm' or varis[iv] == 'tau_wp2_zm' or varis[iv] == 'tau_xp2_zm' or varis[iv] == 'tau_wp3_zm' or varis[iv] == 'tau_wpxp_zm'): theunits = str(chscale[iv]) + '~F10~' + inptrs.variables[ varis[iv]].units + '~S~-1~N~' else: theunits = str( chscale[iv]) + '~F10~' + inptrs.variables[varis[iv] + '_bt'].units res.tiXAxisString = '1/~F8~t~N~~F25~~B~' + vname[ iv] + "~N~ " + ' Unit= ' + theunits res1 = Ngl.Resources() res1 = res for it in range(0, nterms): for subc in range(0, n[ire]): npoint = idx_cols[ire, n[subc] - 1] - 1 if (varis[iv] == 'tau_wp2_zm' or varis[iv] == 'tau_xp2_zm' or varis[iv] == 'tau_wp3_zm' or varis[iv] == 'tau_wpxp_zm' or varis[iv] == 'tau_no_N2_zm'): varis_tau = budget_ends[it] if ( varis_tau == 'bkgnd' or varis_tau == 'shear' \ or varis_tau == 'sfc' ): tmp = inptrs.variables['invrs_tau_' + varis_tau][0, :, npoint] if (varis_tau == 'tau_no_N2_zm'): tmp = inptrs.variables[varis_tau][0, :, npoint] tmp = 1 / tmp if (varis[iv] == 'tau_wp2_zm'): if (varis_tau == 'tau_wp2_zm'): tmp = inptrs.variables[varis_tau][0, :, npoint] tmp = 1 / tmp if (varis_tau == 'bvpos'): tmp0 = inptrs.variables['tau_wp2_zm'][ 0, :, npoint] tmp1 = inptrs.variables['tau_no_N2_zm'][ 0, :, npoint] tmp = tmp0 tmp = 1 / tmp0 - 1 / tmp1 if (varis[iv] == 'tau_zm'): if (varis_tau == 'tau_zm'): tmp = inptrs.variables[varis_tau][0, :, npoint] tmp = 1 / tmp if (varis_tau == 'bvpos'): tmp0 = inptrs.variables['tau_zm'][0, :, npoint] tmp1 = inptrs.variables['tau_no_N2_zm'][ 0, :, npoint] tmp = tmp0 tmp = 1 / tmp0 - 1 / tmp1 if (varis[iv] == 'tau_xp2_zm'): if (varis_tau == 'tau_xp2_zm'): tmp = inptrs.variables[varis_tau][0, :, npoint] tmp = 1 / tmp if (varis_tau == 'Rich'): tmp0 = inptrs.variables['tau_xp2_zm'][ 0, :, npoint] tmp1 = inptrs.variables['tau_no_N2_zm'][ 0, :, npoint] tmp = tmp0 tmp = 1 / tmp0 / (1 / tmp1) / 1000 if (varis[iv] == 'tau_wp3_zm'): if (varis_tau == 'tau_wp3_zm' or varis_tau == 'tau_wpxp_zm'): tmp = inptrs.variables[varis_tau][0, :, npoint] tmp = 1 / tmp tmp[0:10] = 0 if (varis_tau == 'bvpos'): tmp0 = inptrs.variables['tau_wp2_zm'][ 0, :, npoint] tmp1 = inptrs.variables['tau_no_N2_zm'][ 0, :, npoint] tmp = tmp0 tmp = 1 / tmp0 - 1 / tmp1 if (varis_tau == 'clear'): tmp0 = inptrs.variables['tau_wp3_zm'][ 0, :, npoint] tmp1 = inptrs.variables['tau_wp2_zm'][ 0, :, npoint] tmp = tmp0 tmp = 1 / tmp0 - 1 / tmp1 tmp[0:10] = 0 if (varis[iv] == 'tau_wpxp_zm'): if (varis_tau == 'tau_wpxp_zm' or varis_tau == 'tau_zm'): tmp = inptrs.variables[varis_tau][0, :, npoint] tmp = 1 / tmp if (varis_tau == 'bvpos'): tmp0 = inptrs.variables['tau_zm'][0, :, npoint] tmp1 = inptrs.variables['tau_no_N2_zm'][ 0, :, npoint] tmp = tmp0 tmp = 2 / tmp0 - 1 / tmp1 if (varis_tau == 'clear'): tmp0 = inptrs.variables['tau_wpxp_zm'][ 0, :, npoint] tmp1 = inptrs.variables['tau_zm'][0, :, npoint] tmp = tmp0 tmp = 1 / tmp0 / 5 - 2 / tmp1 else: varis_tau = varis[iv] + budget_ends[it] tmp0 = inptrs.variables[varis[iv]][0, :, npoint] tmp = inptrs.variables[varis_tau][0, :, npoint] tmp = tmp * cscale[iv] A_field[it, :] = (A_field[it, :] + tmp[:] / n[ire]).astype(np.float32) inptrs.close() res.pmLegendDisplayMode = "Never" if (varis[iv] == 'tau_wp3_zm'): res.trXMinF = 0 res.trXMaxF = 450 res.xyMarkerColors = [ 'black', 'orange', 'purple', 'firebrick' ] res.xyLineColors = [ 'black', 'orange', 'purple', 'firebrick' ] if (varis[iv] == 'tau_no_N2_zm'): res.trXMinF = 0 res.trXMaxF = 20 res.xyMarkerColors = ['black', 'red', 'green', 'blue'] res.xyLineColors = ['black', 'red', 'green', 'blue'] if (varis[iv] == 'tau_wp2_zm'): res.trXMinF = 0 res.trXMaxF = 12 res.xyMarkerColors = ['black', 'orange', 'purple'] res.xyLineColors = ['black', 'orange', 'purple'] if (varis[iv] == 'tau_xp2_zm'): res.trXMinF = 0 res.trXMaxF = 35 res.xyMarkerColors = ['black', 'orange'] res.xyLineColors = ['black', 'orange'] p = Ngl.xy(wks, A_field, ilev, res) plot.append(p) xp = np.mod(iv, 2) yp = int(iv / 2) if (varis[iv] == 'tau_wp3_zm'): res.trXMinF = 0 res.trXMaxF = 450 res.xyMarkerColors = [ 'black', 'orange', 'purple', 'firebrick' ] res.xyLineColors = [ 'black', 'orange', 'purple', 'firebrick' ] Common_functions.create_legend( wks, budget_name[:], 0.02, ['black', 'orange', 'purple', 'firebrick'], 0.3 + xp * 0.5, 0.8 - yp * 0.5) if (varis[iv] == 'tau_no_N2_zm'): res.trXMinF = 0 res.trXMaxF = 20 res.xyMarkerColors = ['black', 'red', 'green', 'blue'] res.xyLineColors = ['black', 'red', 'green', 'blue'] Common_functions.create_legend( wks, budget_name[:], 0.02, ['black', 'red', 'green', 'blue'], 0.3 + xp * 0.5, 0.8 - yp * 0.5) if (varis[iv] == 'tau_wp2_zm'): res.trXMinF = 0 res.trXMaxF = 12 res.xyMarkerColors = ['black', 'orange', 'purple'] res.xyLineColors = ['black', 'orange', 'purple'] Common_functions.create_legend( wks, budget_name[:], 0.02, ['black', 'orange', 'purple'], 0.3 + xp * 0.5, 0.8 - yp * 0.5) if (varis[iv] == 'tau_xp2_zm'): res.trXMinF = 0 res.trXMaxF = 35 res.xyMarkerColors = ['black', 'orange'] res.xyLineColors = ['black', 'orange'] Common_functions.create_legend(wks, budget_name[:], 0.02, ['black', 'orange'], 0.3 + xp * 0.5, 0.8 - yp * 0.5) Ngl.panel(wks, plot[:], [nvaris / 2, 2], pres) txres = Ngl.Resources() txres.txFont = _Font txres.txFontHeightF = 0.020 Ngl.text_ndc( wks, casenames[im] + " BUDGET at " + str(lons[ire]) + "E," + str(lats[ire]) + "N", 0.5, 0.95, txres) Ngl.frame(wks) Ngl.destroy(wks) return (plottau)
def draw_2D_plot(ptype, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) if not os.path.exists(casedir + '/2D'): os.mkdir(casedir + '/2D') _Font = 25 interp = 2 extrap = False infiles = ['' for x in range(ncases)] ncdfs = ['' for x in range(ncases)] alpha = ['A', 'B', 'C', 'D', 'E', 'F'] cunits = [''] varis = [ 'SWCF', 'LWCF', 'PRECT', 'LHFLX', 'SHFLX', 'TMQ', 'PS', 'TS', 'U10', 'CLDTOT', 'CLDLOW', 'CLDHGH', 'TGCLDLWP' ] varisobs = [ 'SWCF', 'LWCF', 'PRECT', 'LHFLX', 'SHFLX', 'PREH2O', 'PS', 'TS', 'U10', 'CLDTOT_CAL', 'CLDTOT_CAL', 'CLDTOT_CAL', 'TGCLDLWP_OCEAN' ] nvaris = len(varis) cscale = [1, 1, 86400000, 1, 1, 1, 1, 1, 1, 100, 100, 100, 1000, 1, 1, 1] cscaleobs = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # cntrs = [[0 for col in range(11)] for row in range(nvaris)] cntrs = np.zeros((nvaris, 11), np.float32) obsdataset = [ 'CERES-EBAF', 'CERES-EBAF', 'GPCP', 'ERAI', 'NCEP', 'ERAI', 'NCEP', 'ERAI', 'ERAI', 'CALIPSOCOSP', 'CALIPSOCOSP', 'CALIPSOCOSP', 'NVAP' ] plot2d = ['' for x in range(nvaris)] for iv in range(0, nvaris): # make plot for each field if (varis[iv] == 'CLDTOT' or varis[iv] == 'CLDLOW' or varis[iv] == 'CLDHGH'): cntrs[iv, :] = [2, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90] if (varis[iv] == 'LWCF'): cntrs[iv, :] = [1, 2, 5, 10, 15, 20, 25, 30, 35, 40, 45] if (varis[iv] == 'SWCF' or varis[iv] == 'FLUT'): cntrs[iv, :] = [ -40, -50, -60, -70, -80, -90, -100, -110, -120, -130, -140 ] if (varis[iv] == 'PRECT' or varis[iv] == 'QFLX'): cntrs[iv, :] = [0.5, 1.5, 3, 4.5, 6, 7.5, 9, 10.5, 12, 13.5, 15] if (varis[iv] == 'LHFLX'): cntrs[iv, :] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110] if (varis[iv] == 'SHFLX'): cntrs[iv, :] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110] if (varis[iv] == 'U10'): cntrs[iv, :] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] if (varis[iv] == 'TMQ'): cntrs[iv, :] = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55] if (varis[iv] == 'TGCLDLWP'): cntrs[iv, :] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110] # Observational data if (obsdataset[iv] == 'CCCM'): if (cseason == 'ANN'): fileobs = '/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-' + cseason + '.nc' else: fileobs = '/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-2010-' + cseason + '.nc' else: if (varisobs[iv] == 'PRECT'): fileobs = filepathobs + '/GPCP_' + cseason + '_climo.nc' else: fileobs = filepathobs + obsdataset[ iv] + '_' + cseason + '_climo.nc' # infiles[im]=filepath[im]+cases[im]+'/run/'+cases[im]+'_'+cseason+'_climo.nc' inptrobs = Dataset(fileobs, 'r') latobs = inptrobs.variables['lat'][:] lonobs = inptrobs.variables['lon'][:] if (varisobs[iv] == 'U10'): B0 = inptrobs.variables[varisobs[iv]][0, :, :] B1 = inptrobs.variables['V10'][0, :, :] B = (B0 * B0 + B1 * B1) B = B * cscaleobs[iv] B = np.sqrt(B) else: B = inptrobs.variables[varisobs[iv]][0, :, :] B = B * cscaleobs[iv] #************************************************ # create plot #************************************************ plotname = casedir + '/2D/Horizontal_' + varis[iv] + '_' + cseason plot2d[iv] = 'Horizontal_' + varis[iv] + '_' + cseason wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, 'cmocean_thermal') # Ngl.define_colormap(wks,'MPL_coolwarm') # ngl_define_colormap(wks,'prcp_1') plot = [] textres = Ngl.Resources() textres.txFontHeightF = 0.02 # Size of title. textres.txFont = _Font Ngl.text_ndc(wks, varis[iv], 0.1, .97, textres) pres = Ngl.Resources() pres.nglMaximize = True pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelBottom = 0.20 pres.nglPanelTop = 0.9 pres.pmLabelBarWidthF = 0.8 pres.nglFrame = False pres.nglPanelLabelBar = True # Turn on panel labelbar pres.nglPanelLabelBarLabelFontHeightF = 0.015 # Labelbar font height pres.nglPanelLabelBarHeightF = 0.0750 # Height of labelbar pres.nglPanelLabelBarWidthF = 0.700 # Width of labelbar pres.lbLabelFont = 'helvetica-bold' # Labelbar font pres.nglPanelTop = 0.935 pres.nglPanelFigureStrings = alpha pres.nglPanelFigureStringsJust = 'BottomRight' res = Ngl.Resources() res.nglDraw = False #-- don't draw plots res.nglFrame = False res.cnFillOn = True res.cnFillMode = 'RasterFill' res.cnLinesOn = False res.nglMaximize = True res.mpFillOn = True res.mpCenterLonF = 180 res.tiMainFont = _Font res.tiMainFontHeightF = 0.025 res.tiXAxisString = '' res.tiXAxisFont = _Font res.tiXAxisFontHeightF = 0.025 res.tiYAxisString = '' res.tiYAxisFont = _Font res.tiYAxisOffsetXF = 0.0 res.tiYAxisFontHeightF = 0.025 res.tmXBLabelFont = _Font res.tmYLLabelFont = _Font res.tiYAxisFont = _Font res.vpWidthF = 0.80 # set width and height res.vpHeightF = 0.40 res.vpXF = 0.04 res.vpYF = 0.30 res.cnInfoLabelOn = False res.cnFillOn = True res.cnLinesOn = False res.cnLineLabelsOn = False res.lbLabelBarOn = False # res.vcRefMagnitudeF = 5. # res.vcMinMagnitudeF = 1. # res.vcRefLengthF = 0.04 # res.vcRefAnnoOn = True#False # res.vcRefAnnoZone = 3 # res.vcRefAnnoFontHeightF = 0.02 # res.vcRefAnnoString2 ='' # res.vcRefAnnoOrthogonalPosF = -1.0 # res.vcRefAnnoArrowLineColor = 'blue' # change ref vector color # res.vcRefAnnoArrowUseVecColor = False # res.vcMinDistanceF = .05 # res.vcMinFracLengthF = . # res.vcRefAnnoParallelPosF = 0.997 # res.vcFillArrowsOn = True # res.vcLineArrowThicknessF = 3.0 # res.vcLineArrowHeadMinSizeF = 0.01 # res.vcLineArrowHeadMaxSizeF = 0.03 # res.vcGlyphStyle = 'CurlyVector' # turn on curley vectors # res@vcGlyphStyle ='Fillarrow' # res.vcMonoFillArrowFillColor = True # res.vcMonoLineArrowColor = True # res.vcLineArrowColor = 'green' # change vector color # res.vcFillArrowEdgeColor ='white' # res.vcPositionMode ='ArrowTail' # res.vcFillArrowHeadInteriorXF =0.1 # res.vcFillArrowWidthF =0.05 #default # res.vcFillArrowMinFracWidthF =.5 # res.vcFillArrowHeadMinFracXF =.5 # res.vcFillArrowHeadMinFracYF =.5 # res.vcFillArrowEdgeThicknessF = 2.0 res.mpFillOn = False res.cnLevelSelectionMode = 'ExplicitLevels' res.cnLevels = cntrs[iv][:] for im in range(0, ncases): ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + cases[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) area = inptrs.variables['area'][:] area_wgt = np.zeros(nlat) sits = np.linspace(0, nsite - 1, nsite) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:] if (varis[iv] == 'PRECT'): A = inptrs.variables['PRECC'][ 0, :] + inptrs.variables['PRECL'][0, :] else: A = inptrs.variables[varis[iv]][0, :] if (varis[iv] == 'FLUT'): A = inptrs.variables['FLUT'][0, :] - inptrs.variables['FLNS'][ 0, :] else: A = inptrs.variables[varis[iv]][0, :] if (varis[iv] == 'U10'): A = inptrs.variables['U10'][0, :] * inptrs.variables['U10'][ 0, :] A = np.sqrt(A) else: A = inptrs.variables[varis[iv]][0, :] A_xy = A A_xy = A_xy * cscale[iv] ncdf.close() inptrs.close() if im == 0: dsizes = len(A_xy) field_xy = [[0 for col in range(dsizes)] for row in range(ncases)] field_xy[im][:] = A_xy res.lbLabelBarOn = False if (np.mod(im, 2) == 0): res.tiYAxisOn = True else: res.tiYAxisOn = False res.tiXAxisOn = False res.sfXArray = lon res.sfYArray = lat res.mpLimitMode = 'LatLon' res.mpMaxLonF = max(lon) res.mpMinLonF = min(lon) res.mpMinLatF = min(lat) res.mpMaxLatF = max(lat) res.tiMainString = 'GLB=' + str( np.sum(A_xy[:] * area[:] / np.sum(area))) textres.txFontHeightF = 0.015 Ngl.text_ndc(wks, alpha[im] + ' ' + casenames[im], 0.3, .135 - im * 0.03, textres) p = Ngl.contour_map(wks, A_xy, res) plot.append(p) # observation # res.nglLeftString = obsdataset[iv] # res@lbLabelBarOn = True # res@lbOrientation = 'vertical' # vertical label bars res.lbLabelFont = _Font res.tiYAxisOn = True res.tiXAxisOn = True res.tiXAxisFont = _Font rad = 4.0 * np.arctan(1.0) / 180.0 re = 6371220.0 rr = re * rad dlon = abs(lonobs[2] - lonobs[1]) * rr dx = dlon * np.cos(latobs * rad) jlat = len(latobs) dy = np.zeros(jlat, dtype=float) # close enough dy[0] = abs(lat[2] - lat[1]) * rr dy[1:jlat - 2] = abs(lat[2:jlat - 1] - lat[0:jlat - 3]) * rr * 0.5 dy[jlat - 1] = abs(lat[jlat - 1] - lat[jlat - 2]) * rr area_wgt = dx * dy # is_SE = False sum1 = 0 sum2 = 0 for j in range(0, jlat - 1): for i in range(0, len(lonobs) - 1): if (np.isnan(B[j][i]) != '--'): sum1 = sum1 + area_wgt[j] * B[j][i] sum2 = sum2 + area_wgt[j] glb = sum1 / sum2 res.sfXArray = lonobs res.sfYArray = latobs res.mpLimitMode = 'LatLon' res.mpMaxLonF = max(lonobs) res.mpMinLonF = min(lonobs) res.mpMinLatF = min(latobs) res.mpMaxLatF = max(latobs) res.tiMainString = 'GLB=' + str(glb) p = Ngl.contour_map(wks, B, res) if (iv == 0): poly_res = Ngl.Resources() poly_res.gsMarkerIndex = 16 poly_res.gsMarkerSizeF = 0.005 poly_res.gsMarkerColor = 'green' dum = Ngl.add_polymarker(wks, p, lons, lats, poly_res) plot.append(p) if (np.mod(ncases + 1, 2) == 1): Ngl.panel(wks, plot[:], [(ncases + 1) / 2 + 1, 2], pres) else: Ngl.panel(wks, plot[:], [(ncases + 1) / 2, 2], pres) Ngl.frame(wks) Ngl.destroy(wks) # Ngl.end() return plot2d
def clubb_std_prf(ptype, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir, varis, vname, cscale, chscale, pname): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. alphas = [ 'a) ', 'b) ', 'c) ', 'd) ', 'e) ', 'f) ', 'g) ', 'h) ', 'i) ', 'j) ', 'k) ', 'l) ', 'm) ', 'n) ', 'o) ', 'p) ', 'q) ', 'r) ', 's) ', 't) ', 'u) ', 'v) ', 'w) ', 'x) ', 'y) ', 'z) ' ] infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite nvaris = len(varis) plotstd = ["" for x in range(nsite)] for ire in range(0, nsite): if not os.path.exists(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N'): os.mkdir(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N') plotname = casedir + '/' + str(lons[ire]) + 'E_' + str( lats[ire]) + 'N/' + pname + '_' + str(lons[ire]) + "E_" + str( lats[ire]) + "N_" + cseason plotstd[ire] = pname + '_' + str(lons[ire]) + "E_" + str( lats[ire]) + "N_" + cseason wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, "GMT_paired") plot = [] res = Ngl.Resources() res.nglMaximize = False if (lats[ire] > 0 and lons[ire] < 230): res.trYMinF = 400. res.trYMaxF = 1000. else: res.trYMinF = 700. res.trYMaxF = 1000. res.nglDraw = False res.nglFrame = False res.lgLabelFontHeightF = .02 # change font height res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 res.vpXF = 0.04 res.vpYF = 0.30 res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tiYAxisFont = _Font res.tiXAxisFont = _Font res.tmXBLabelFontHeightF = 0.01 res.tmXBLabelFontThicknessF = 2.0 res.xyMarkLineMode = "Lines" res.xyLineThicknesses = [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3., 3., 3., 3., 3, 3, 3, 3, 3, 3, 3 ] res.xyDashPatterns = np.arange(0, 24, 1) # res.xyMarkers = np.arange(16,40,1) # res.xyMarkerSizeF = 0.005 res.tiYAxisString = "Pressure [hPa]" for iv in range(0, nvaris): if (iv == nvaris - 1): res.pmLegendDisplayMode = "NEVER" res.xyExplicitLegendLabels = casenames[:] res.pmLegendSide = "top" res.pmLegendParallelPosF = 0.6 res.pmLegendOrthogonalPosF = -0.5 res.pmLegendWidthF = 0.10 res.pmLegendHeightF = 0.10 res.lgLabelFontHeightF = .02 res.lgLabelFontThicknessF = 1.5 res.lgPerimOn = True else: res.pmLegendDisplayMode = "NEVER" # if(obsdataset[iv] =="CCCM"): # if(cseason == "ANN"): # fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-"+cseason+".nc" # else: # fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-2010-"+cseason+".nc" # inptrobs = Dataset(fileobs,'r') # B=inptrobs.variables[varisobs[iv]][:,(lats[ire]),(lons[ire])] # else: # if (varisobs[iv] =="PRECT"): # fileobs = filepathobs+'/GPCP_'+cseason+'_climo.nc' # else: # fileobs = filepathobs + obsdataset[iv]+'_'+cseason+'_climo.nc' # inptrobs = Dataset(fileobs,'r') # if (varisobs[iv] =="THETA"): # B = inptrobs.variables['T'][0,:,(lats[ire]),(lons[ire])] # pre1 = inptrobs.variables['lev'][:] # for il1 in range (0, len(pre1)): # B[il1] = B[il1]*(1000/pre1[il1])**0.286 # else: # pre1 = inptrobs.variables['lev'][:] # B = inptrobs.variables[varisobs[iv]][0,:,(lats[ire]),(lons[ire])] # # B[:]=B[:] * cscaleobs[iv] for im in range(0, ncases): ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + cases[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) lev = inptrs.variables['ilev'][:] nlev = len(lev) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:, :] ncdf.close() if (im == 0): A_field = np.zeros((ncases, nlev), np.float32) for subc in range(0, n[ire]): npoint = idx_cols[ire, n[subc] - 1] - 1 if (varis[iv] == 'THETA'): tmp = inptrs.variables['T'][0, :, npoint] hyam = inptrs.variables['hyam'][:] hybm = inptrs.variables['hybm'][:] ps = inptrs.variables['PS'][0, npoint] ps = ps p0 = inptrs.variables['P0'] pre = np.zeros((nlev), np.float32) for il in range(0, nlev): pre[il] = hyam[il] * p0 + hybm[il] * ps tmp[il] = tmp[il] * (100000 / pre[il])**0.286 theunits = str( chscale[iv]) + inptrs.variables['T'].units else: tmp = inptrs.variables[varis[iv]][0, :, npoint] # tmp2=inptrs.variables['C6rt_Skw_fnc'][0,:,npoint] # tmp3=inptrs.variables['tau_zm'][0,:,npoint] # tmp4=inptrs.variables['tau_wpxp_zm'][0,:,npoint] theunits = str( chscale[iv]) + inptrs.variables[varis[iv]].units if (varis[iv] == 'tau_zm' or varis[iv] == 'tau_wp2_zm' \ or varis[iv] == 'tau_wp3_zm' or varis[iv] == 'tau_xp2_zm' \ or varis[iv] == 'tau_no_N2_zm' or varis[iv] == 'tau_wpxp_zm'): tmp = 1 / tmp tmp[0:10] = 0.0 theunits = str(chscale[iv]) + inptrs.variables[ varis[iv]].units + '^-1' A_field[im, :] = (A_field[im, :] + tmp[:] / n[ire]).astype( np.float32) A_field[im, :] = A_field[im, :] * cscale[iv] inptrs.close() res.tiXAxisString = vname[iv] + " Unit= " + theunits res.trYReverse = True res.xyLineColors = np.arange(3, 20, 2) res.xyMarkerColors = np.arange(2, 20, 2) p = Ngl.xy(wks, A_field, lev, res) plot.append(p) pres = Ngl.Resources() pres.nglFrame = False pres.txFont = _Font pres.nglMaximize = False pres.txFont = _Font # pres.nglPanelYWhiteSpacePercent = 5 # pres.nglPanelXWhiteSpacePercent = 5 # pres.wkPaperWidthF = 17 # in inches # pres.wkPaperHeightF = 28 # in inches pres.nglPanelTop = 0.935 pres.nglPanelFigureStrings = alphas pres.nglPanelFigureStringsJust = 'Topleft' pres.nglPanelFigureStringsFontHeightF = 0.015 pres.gsnPaperOrientation = "Landscape" txres = Ngl.Resources() txres.txFontHeightF = 0.02 txres.txFont = _Font Ngl.text_ndc( wks, "CLUBB VAR at " + str(lons[ire]) + "E," + str(lats[ire]) + "N", 0.5, 0.75, txres) Common_functions.create_legend(wks, casenames, 0.015, np.arange(3, 20, 2), 0.3, 0.59 + ncases * 0.01) Ngl.panel(wks, plot[:], [nvaris / 2, 2], pres) Ngl.frame(wks) Ngl.destroy(wks) return plotstd
def large_scale_prf(ptype, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir, dofv): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite varis = [ "CLOUD", "OMEGA", "Q", "CLDLIQ", "THETA", "RELHUM", "U", "CLDICE", "T" ] varisobs = [ "CC_ISBL", "OMEGA", "SHUM", "CLWC_ISBL", "THETA", "RELHUM", "U", "CIWC_ISBL", "T" ] nvaris = len(varis) cunits = [ "%", "mba/day", "g/kg", "g/kg", "K", "%", "m/s", "g/kg", "m/s", "m/s", "K", "m" ] cscale = [100, 864, 1000, 1000, 1., 1, 1, 1000, 1, 1, 1, 1, 1, 1, 1] cscaleobs = [100, 1, 1, 1000, 1., 1, 1, 1000, 1, 1, 1, 1, 1, 1, 1] obsdataset = [ "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI", "ERAI" ] plotlgs = ["" for x in range(nsite)] for ire in range(0, nsite): if not os.path.exists(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N'): os.mkdir(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N') plotname = casedir + '/' + str(lons[ire]) + 'E_' + str( lats[ire]) + 'N/Largescale_' + str(lons[ire]) + "E_" + str( lats[ire]) + "N_" + cseason plotlgs[ire] = 'Largescale_' + str(lons[ire]) + "E_" + str( lats[ire]) + "N_" + cseason wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, "GMT_paired") plot = [] res = Ngl.Resources() res.nglMaximize = False res.nglDraw = False res.nglFrame = False res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 res.tiYAxisString = "Pressure [hPa]" res.tiMainFont = _Font res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tiYAxisFont = _Font res.tiXAxisFont = _Font res.tmXBLabelFontHeightF = 0.01 res.tmXBLabelFontThicknessF = 1.0 res.xyMarkLineMode = 'Lines' # res.tmXBLabelAngleF = 45 res.xyLineThicknesses = [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3., 3., 3., 3., 3, 3, 3, 3, 3, 3, 3 ] res.xyDashPatterns = np.arange(0, 24, 1) pres = Ngl.Resources() pres.nglFrame = False pres.txString = "Large-scale VAR at" + str(lons[ire]) + "E," + str( lats[ire]) + "N" pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.88 pres.wkPaperWidthF = 17 # in inches pres.wkPaperHeightF = 28 # in inches pres.nglMaximize = True pres.wkWidth = 10000 pres.wkHeight = 10000 for iv in range(0, nvaris): if (iv == nvaris - 1): res.pmLegendDisplayMode = "NEVER" res.xyExplicitLegendLabels = casenames[:] res.pmLegendSide = "top" res.pmLegendParallelPosF = 0.6 res.pmLegendOrthogonalPosF = -0.5 res.pmLegendWidthF = 0.10 res.pmLegendHeightF = 0.10 res.lgLabelFontHeightF = .02 res.lgLabelFontThicknessF = 1.5 res.lgPerimOn = True else: res.pmLegendDisplayMode = "NEVER" if (obsdataset[iv] == "CCCM"): if (cseason == "ANN"): fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-" + cseason + ".nc" else: fileobs = "/Users/guoz/databank/CLD/CCCm/cccm_cloudfraction_2007-2010-" + cseason + ".nc" inptrobs = Dataset(fileobs, 'r') latobs = inptrobs.variables['lat'][:] latobs_idx = np.abs(latobs - lats[ire]).argmin() lonobs = inptrobs.variables['lon'][:] lonobs_idx = np.abs(lonobs - lons[ire]).argmin() B = inptrobs.variables[varisobs[iv]][:, latobs_idx, lonobs_idx] else: if (varisobs[iv] == "PRECT"): fileobs = filepathobs + '/GPCP_' + cseason + '_climo.nc' else: fileobs = filepathobs + obsdataset[ iv] + '_' + cseason + '_climo.nc' inptrobs = Dataset(fileobs, 'r') if (varisobs[iv] == "THETA"): latobs = inptrobs.variables['lat'][:] latobs_idx = np.abs(latobs - lats[ire]).argmin() lonobs = inptrobs.variables['lon'][:] lonobs_idx = np.abs(lonobs - lons[ire]).argmin() B = inptrobs.variables['T'][0, :, latobs_idx, lonobs_idx] pre1 = inptrobs.variables['lev'][:] for il1 in range(0, len(pre1)): B[il1] = B[il1] * (1000 / pre1[il1])**0.286 else: pre1 = inptrobs.variables['lev'][:] latobs = inptrobs.variables['lat'][:] latobs_idx = np.abs(latobs - lats[ire]).argmin() lonobs = inptrobs.variables['lon'][:] lonobs_idx = np.abs(lonobs - lons[ire]).argmin() B = inptrobs.variables[varisobs[iv]][0, :, latobs_idx, lonobs_idx] B[:] = B[:] * cscaleobs[iv] for im in range(0, ncases): ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) lev = inptrs.variables['lev'][:] nlev = len(lev) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:, :] if (dofv): idx_lats = ncdf.variables['idx_coord_lat'][:, :] idx_lons = ncdf.variables['idx_coord_lon'][:, :] ncdf.close() theunits = "[units]" if (im == 0): A_field = np.zeros((ncases, nlev), np.float32) for subc in range(0, n[ire]): npoint = idx_cols[ire, n[subc] - 1] - 1 if (dofv): npointlat = idx_lats[ire, 0] npointlon = idx_lons[ire, 0] if (varis[iv] == 'THETA'): if (dofv): tmp = inptrs.variables['T'][0, :, npointlat, npointlon] else: tmp = inptrs.variables['T'][0, :, npoint] hyam = inptrs.variables['hyam'][:] hybm = inptrs.variables['hybm'][:] if (dofv): ps = inptrs.variables['PS'][0, npointlat, npointlon] else: ps = inptrs.variables['PS'][0, npoint] ps = ps p0 = 100000.0 #CAM uses a hard-coded p0 pre = np.zeros((nlev), np.float32) for il in range(0, nlev): pre[il] = hyam[il] * p0 + hybm[il] * ps tmp[il] = tmp[il] * (100000 / pre[il])**0.286 theunits = str( cscale[iv]) + "x" + inptrs.variables['T'].units else: if (dofv): tmp = inptrs.variables[varis[iv]][0, :, npointlat, npointlon] else: tmp = inptrs.variables[varis[iv]][0, :, npoint] theunits = str(cscale[iv]) + "x" + inptrs.variables[ varis[iv]].units ##import pdb; pdb.set_trace() A_field[im, :] = (A_field[im, :] + tmp[:] / n[ire]).astype( np.float32) A_field[im, :] = A_field[im, :] * cscale[iv] inptrs.close() res.tiMainString = varis[iv] + " " + theunits res.trXMinF = min(np.min(A_field[0, :]), np.min(B)) res.trXMaxF = max(np.max(A_field[0, :]), np.max(B)) if (varis[iv] == "THETA"): res.trXMinF = 270. res.trXMaxF = 400. if (varis[iv] == "CLOUD" or varis[iv] == "RELHUM"): res.trXMinF = 0. res.trXMaxF = 100. if (varis[iv] == "T"): res.trXMinF = 180 res.trXMaxF = 300 if (varis[iv] == "U"): res.trXMinF = -40 res.trXMaxF = 40 res.trYReverse = True res.xyLineColors = np.arange(3, 20, 2) res.xyMarkerColors = np.arange(2, 20, 2) p = Ngl.xy(wks, A_field, lev, res) res.trYReverse = False res.xyLineColors = ["black"] pt = Ngl.xy(wks, B, pre1, res) Ngl.overlay(p, pt) plot.append(p) Ngl.panel(wks, plot[:], [nvaris / 3, 3], pres) txres = Ngl.Resources() txres.txFontHeightF = 0.02 txres.txFont = _Font Ngl.text_ndc( wks, "Large-scale VAR at" + str(lons[ire]) + "E," + str(lats[ire]) + "N", 0.5, 0.92 + ncases * 0.01, txres) Common_functions.create_legend(wks, casenames, 0.02, np.arange(3, 20, 2), 0.1, 0.89 + ncases * 0.01) Ngl.frame(wks) Ngl.destroy(wks) return plotlgs
def draw_clubb_bgt(ptype, cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs, casedir, varis, vname, cscale, chscale, pname): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite nvaris = len(varis) plotbgt = ["" for x in range(nsite * ncases)] alphas = [ 'a) ', 'b) ', 'c) ', 'd) ', 'e) ', 'f) ', 'g) ', 'h) ', 'i) ', 'j) ', 'k) ', 'l) ', 'm) ', 'n) ', 'o) ', 'p) ', 'q) ', 'r) ', 's) ', 't) ', 'u) ', 'v) ', 'w) ', 'x) ', 'y) ', 'z) ' ] for ire in range(0, nsite): for im in range(0, ncases): if not os.path.exists(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N'): os.mkdir(casedir + '/' + str(lons[ire]) + 'E_' + str(lats[ire]) + 'N') plotname = casedir + '/' + str(lons[ire]) + 'E_' + str( lats[ire]) + 'N/' + pname + '_' + casenames[im] + "_" + str( lons[ire]) + "E_" + str(lats[ire]) + "N_" + cseason plotbgt[im + ncases * ire] = pname + '_' + casenames[im] + "_" + str( lons[ire]) + "E_" + str(lats[ire]) + "N_" + cseason wks = Ngl.open_wks(ptype, plotname) Ngl.define_colormap(wks, "radar") plot = [] res = Ngl.Resources() res.nglDraw = False res.nglFrame = False res.nglScale = False # res.vpWidthF = 0.40 # set width and height # res.vpHeightF = 0.40 # res.vpXF = 0.04 # res.vpYF = 0.30 res.nglMaximize = False if (lats[ire] > 0 and lons[ire] < 230): res.trYMinF = 400. res.trYMaxF = 1000. else: res.trYMinF = 700. res.trYMaxF = 1000. res.tiMainFont = _Font res.tmYLLabelFont = _Font res.tmXBLabelFont = _Font res.tiYAxisFont = _Font res.tiXAxisFont = _Font res.tmXBLabelFontHeightF = 0.02 res.tmXBLabelFontThicknessF = 1.0 res.xyMarkLineMode = "MarkLines" res.xyLineThicknesses = [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3., 2., 2., 2., 2, 2, 2, 2, 2, 2, 2 ] res.xyLineColors = np.arange(1, 16, 4) res.xyDashPatterns = np.arange(0, 24, 1) res.xyMarkers = np.arange(16, 40, 1) res.xyMarkerSizeF = 0.005 res.xyMarkerColors = np.arange(1, 16, 4) res.pmLegendDisplayMode = "NEVER" res.pmLegendSide = "top" # Change location of res.pmLegendParallelPosF = 0.75 # move units right res.pmLegendOrthogonalPosF = -0.65 # more neg = down res.pmLegendWidthF = 0.05 # Change width and res.pmLegendHeightF = 0.06 # height of legend. res.lgLabelFontHeightF = 0.03 # change font height res.lgLabelFontThicknessF = 1. # res.lgBoxMinorExtentF = 0.5 res.lgPerimOn = False res.tiYAxisString = "Pressure [hPa]" res.tmYLLabelFontHeightF = 0.02 res.trYReverse = True res1 = Ngl.Resources() res1 = res pres = Ngl.Resources() # pres.gsnPaperOrientation= "Landscape" # pres.wkPaperWidthF = 20 # in inches # pres.wkPaperHeightF = 28 # in inches pres.nglMaximize = False # pres.vpWidthF = 0.40 # set width and height # pres.vpHeightF = 0.40 pres.nglFrame = False pres.txFont = _Font pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 3 pres.nglPanelTop = .9 # pres.nglPanelBottom=0.3 # pres.nglPanelLeft= 0.2 # pres.nglPanelRight= 0.95 pres.nglPanelFigureStrings = alphas pres.nglPanelFigureStringsJust = 'Topleft' pres.nglPanelFigureStringsFontHeightF = 0.015 for iv in range(0, nvaris): if (varis[iv] == "rtp2" or varis[iv] == "thlp2"): budget_ends = ["_ta", "_tp", "_dp1"] budget_name = [ '~F10~turb adv', '~F10~turb prod', '~F10~dissipation' ] nterms = len(budget_ends) if (varis[iv] == "wprtp"): budget_ends = [ "_bt", "_ma", "_ta", "_tp", "_ac", "_bp", "_pr1", "_pr2", "_pr3", "_dp1", "_mfl", "_cl", "_sicl", "_pd", "_forcing" ] budget_name = budget_ends nterms = len(budget_ends) if (varis[iv] == "wpthlp"): budget_ends = ["_tp", "_bp", "_pr1"] budget_name = [ '~F10~turb prod', '~F10~buoy+pres', '~F10~ret to iso' ] nterms = len(budget_ends) if (varis[iv] == "rtpthlp"): budget_ends = [ "_bt", "_ma", "_ta", "_tp1", "_tp2", "_dp1", "_dp2", "_cl", "_sf", "_forcing" ] budget_name = budget_ends nterms = len(budget_ends) if (varis[iv] == "wp2"): budget_ends = ["_ta", "_bp", "_pr1", "_dp1", "_dp2"] budget_name = budget_ends nterms = len(budget_ends) if (varis[iv] == "wp3"): budget_ends = [ "_ta", "_tp", "_bp1", "_bp2", "_pr1", "_pr2", "_dp1" ] budget_name = budget_ends nterms = len(budget_ends) if (varis[iv] == "up2" or varis[iv] == "vp2"): budget_ends = [ "_bt", "_ma", "_ta", "_tp", "_dp1", "_dp2", "_pr1", "_pr2", "_cl", "_pd", "_sf" ] budget_name = budget_ends nterms = len(budget_ends) if (varis[iv] == "um" or varis[iv] == "vm"): budget_ends = ["_bt", "_ma", "_ta", "_gf", "_f"] budget_name = budget_ends nterms = len(budget_ends) if (varis[iv] == "thlm" or varis[iv] == "rtm"): budget_ends = ["_bt", "_ma", "_ta", "_cl", "_mc"] budget_name = budget_ends nterms = len(budget_ends) if (varis[iv] == 'tau_no_N2_zm'): budget_ends = ['tau_no_N2_zm', 'bkgnd', 'sfc', 'shear'] budget_name = [ '~F8~t~N~~F25~~B~noN2~N~', '~F8~t~N~~F25~~B~bkgnd~N~', '~F8~t~N~~F25~~B~shear~N~', '~F8~t~N~~F25~~B~shear~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_xp2_zm'): budget_ends = ['tau_xp2_zm', 'tau_no_N2_zm'] budget_name = [ '~F8~t~N~~F25~~B~x`~S~2~N~~N~', '~F8~t~N~~F25~~B~noN2~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_wp2_zm'): budget_ends = ['tau_wp2_zm', 'tau_no_N2_zm', 'bvpos'] budget_name = [ '~F8~t~N~~F25~~B~w`~S~2~N~~N~', '~F8~t~N~~F25~~B~noN2~N~', '~F8~t~N~~F25~~B~bv~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_wpxp_zm'): budget_ends = [ 'tau_wpxp_zm', 'tau_no_N2_zm', 'bvpos', 'clear' ] budget_name = [ '~F8~t~N~~F25~~B~w`x`~N~', '~F8~t~N~~F25~~B~noN2~N~', '~F8~t~N~~F25~~B~bv~N~', '~F8~t~N~~F25~~B~clr~N~' ] nterms = len(budget_ends) if (varis[iv] == 'tau_wp3_zm'): budget_ends = [ 'tau_wp3_zm', 'tau_no_N2_zm', 'bvpos', 'clear' ] budget_name = [ '~F8~t~N~~F25~~B~w`~S~3~N~~N~', '~F8~t~N~~F25~~B~noN2~N~', '~F8~t~N~~F25~~B~bv~N~', '~F8~t~N~~F25~~B~clr~N~' ] nterms = len(budget_ends) ncdfs[im] = './data/' + cases[im] + '_site_location.nc' infiles[im] = filepath[im] + cases[im] + '/' + cases[ im] + '_' + cseason + '_climo.nc' inptrs = Dataset(infiles[im], 'r') # pointer to file1 lat = inptrs.variables['lat'][:] nlat = len(lat) lon = inptrs.variables['lon'][:] nlon = len(lon) ilev = inptrs.variables['ilev'][:] nilev = len(ilev) ncdf = Dataset(ncdfs[im], 'r') n = ncdf.variables['n'][:] idx_cols = ncdf.variables['idx_cols'][:, :] ncdf.close() A_field = np.zeros((nterms, nilev), np.float32) for it in range(0, nterms): for subc in range(0, n[ire]): npoint = idx_cols[ire, n[subc] - 1] - 1 varis_bgt = varis[iv] + budget_ends[it] tmp0 = inptrs.variables[varis[iv]][0, :, npoint] theunits0 = "~F25~" + inptrs.variables[varis[iv]].units if (varis[iv] == 'wpthlp' and budget_ends[it] == '_bp'): tmp = inptrs.variables[varis_bgt][ 0, :, npoint] + inptrs.variables['wpthlp_pr3'][ 0, :, npoint] else: tmp = inptrs.variables[varis_bgt][0, :, npoint] theunits = str( chscale[iv]) + "~F25~" + inptrs.variables[ varis[iv] + '_bt'].units if (varis[iv] == "wprtp" or varis[iv] == "thlp2"): tmp[0:10] = 0.0 tmp0[0:10] = 0.0 txres = Ngl.Resources() txres.txFontHeightF = 0.02 txres.txFont = _Font tmp = tmp * cscale[iv] A_field[it, :] = (A_field[it, :] + tmp[:] / n[ire]).astype(np.float32) inptrs.close() res.tiXAxisString = vname[iv] + ' Unit= ' + theunits if (varis[iv] == "wpthlp" or varis[iv] == "thlp2" or varis[iv] == "wp3" or varis[iv] == "wp2"): res1.tiXAxisString = vname[iv] + ' Unit= ' + theunits0 p0 = Ngl.xy(wks, tmp0, ilev, res1) plot.append(p0) if (varis[iv] == 'wpthlp'): if (lons[ire] < 230): res.trXMinF = -1. res.trXMaxF = 1. Common_functions.create_legend(wks, budget_name, 0.013, np.arange(1, 16, 4), 0.77, 0.85 - iv * 0.45) else: res.trXMinF = -0.4 res.trXMaxF = 0.4 Common_functions.create_legend(wks, budget_name, 0.013, np.arange(1, 16, 4), 0.77, 0.85 - iv * 0.45) if (varis[iv] == 'thlp2'): if (lons[ire] < 230): res.trXMinF = -2.8 res.trXMaxF = 2.8 Common_functions.create_legend(wks, budget_name, 0.013, np.arange(1, 16, 4), 0.77, 0.85 - iv * 0.45) else: res.trXMinF = -1. res.trXMaxF = 1. Common_functions.create_legend(wks, budget_name, 0.013, np.arange(1, 16, 4), 0.77, 0.85 - iv * 0.45) res.tiXAxisString = vname[iv] + ' Unit= ' + theunits p = Ngl.xy(wks, A_field, ilev, res) plot.append(p) del (res.trXMaxF) del (res.trXMinF) xp = np.mod(iv, 2) yp = int(iv / 2) Ngl.panel(wks, plot[:], [nvaris, 2], pres) txres = Ngl.Resources() txres.txFont = _Font txres.txFontHeightF = 0.02 Ngl.text_ndc( wks, casenames[im] + " BUDGET at " + str(lons[ire]) + "E," + str(lats[ire]) + "N", 0.5, 0.91, txres) Ngl.frame(wks) Ngl.destroy(wks) return (plotbgt)
def draw_e3sm_bgt (ptype,cseason, ncases, cases, casenames, nsite, lats, lons, filepath, filepathobs,casedir,dpsc): # ncases, the number of models # cases, the name of models # casename, the name of cases # filepath, model output filepath # filepathobs, filepath for observational data # inptrs = [ncases] if not os.path.exists(casedir): os.mkdir(casedir) _Font = 25 interp = 2 extrap = False mkres = Ngl.Resources() mkres.gsMarkerIndex = 2 mkres.gsMarkerColor = "Red" mkres.gsMarkerSizeF = 15. infiles = ["" for x in range(ncases)] ncdfs = ["" for x in range(ncases)] nregions = nsite varis = [ "DCQ","DCCLDLIQ","DCCLDICE","PTEQ","PTTEND","DTCOND"] nvaris = len(varis) cscale = [1E8, 1E8, 1E8, 1E8, 1E4, 1E4] chscale = ['1E-8', '1E-8', '1E-8', '1E-8', '1E-4', '1E-4'] plote3smbgt=["" for x in range(nsite*ncases)] for ire in range (0, nsite): for im in range (0,ncases): if not os.path.exists(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N'): os.mkdir(casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N') plotname = casedir+'/'+str(lons[ire])+'E_'+str(lats[ire])+'N/E3SM_Budgets_'+casenames[im]+"_"+str(lons[ire])+"E_"+str(lats[ire])+"N_"+cseason plote3smbgt[im+ncases*ire] = 'E3SM_Budgets_'+casenames[im]+"_"+str(lons[ire])+"E_"+str(lats[ire])+"N_"+cseason wks= Ngl.open_wks(ptype,plotname) Ngl.define_colormap(wks,"radar") plot = [] res = Ngl.Resources() res.nglDraw = False res.nglFrame = False res.lgLabelFontHeightF = .012 # change font height res.lgPerimOn = False # no box around res.vpWidthF = 0.30 # set width and height res.vpHeightF = 0.30 # res.txFontHeightF = .01 # res.vpXF = 0.04 # res.vpYF = 0.30 res.tmYLLabelFont = 12 res.tmXBLabelFont = 12 res.tmXBLabelFontHeightF = 0.01 res.tmXBLabelFontThicknessF = 1.0 res.xyMarkLineMode = "MarkLines" res.xyLineThicknesses = [2.0, 2.0, 2.0, 2.0, 2.0, 2.0,2.,2.,2.,2.,2,2,2,2,2,2,2] res.xyLineColors = np.arange(2,16,2) res.xyDashPatterns = np.arange(0,24,1) res.xyMarkers = np.arange(16,40,1) res.xyMarkerSizeF = 0.005 res.xyMarkerColors = np.arange(2,16,2) res.pmLegendDisplayMode = "ALWAYS" res.pmLegendSide = "top" # Change location of res.pmLegendParallelPosF = 0.6 # move units right res.pmLegendOrthogonalPosF = -0.55 # more neg = down res.pmLegendWidthF = 0.2 # Decrease width res.pmLegendHeightF = 0.1 # Decrease height res.lgBoxMinorExtentF = 0.1 # Shorten the legend lines res.lgLabelFontHeightF = 0.015 # Change the font size res.lgPerimOn = True res.tiYAxisString = "PRESSURE" res.trYReverse = True pres = Ngl.Resources() pres.nglMaximize = True pres.wkWidth = 2000 pres.wkHeight = 2000 pres.nglFrame = False pres.txFont = 12 pres.nglPanelYWhiteSpacePercent = 5 pres.nglPanelXWhiteSpacePercent = 5 pres.nglPanelTop = 0.93 for iv in range (0, nvaris): if (varis[iv] == "DCQ" ): if (dpsc[im] == "zm" ): budget_ends = ["MPDQ", "RVMTEND_CLUBB","ZMDQ", "EVAPQZM"] else: budget_ends = ["MPDQ", "RVMTEND_CLUBB"] nterms = len (budget_ends) if (varis[iv] == "DTCOND" ): if (dpsc[im] == "zm" ): budget_ends = ["STEND_CLUBB", "MPDT", "DPDLFT","ZMDT", "EVAPTZM", "ZMMTT"] else: budget_ends = ["STEND_CLUBB", "MPDT", "DPDLFT"] nterms = len (budget_ends) if (varis[iv] == "PTTEND") : budget_ends = ["DTCOND", "QRS", "QRL", "TTGW"] nterms = len (budget_ends) if (varis[iv] == "PTEQ") : if (dpsc[im] == "zm" ): budget_ends = ["MPDQ", "RVMTEND_CLUBB","ZMDQ", "EVAPQZM"] else: budget_ends = ["MPDQ", "RVMTEND_CLUBB"] nterms = len (budget_ends) if (varis[iv] == "DCCLDLIQ") : if (dpsc[im] == "zm" ): budget_ends = ["MPDLIQ", "RCMTEND_CLUBB", "DPDLFLIQ","ZMDLIQ"] else: budget_ends = ["MPDLIQ", "RCMTEND_CLUBB", "DPDLFLIQ"] nterms = len (budget_ends) if (varis[iv] == "DCCLDICE") : if (dpsc[im] == "zm" ): budget_ends = ["MPDICE", "RIMTEND_CLUBB", "DPDLFICE","ZMDICE"] else: budget_ends = ["MPDICE", "RIMTEND_CLUBB", "DPDLFICE"] nterms = len (budget_ends) ncdfs[im] = './data/'+cases[im]+'_site_location.nc' infiles[im]= filepath[im]+cases[im]+'/'+cases[im]+'_'+cseason+'_climo.nc' inptrs = Dataset(infiles[im],'r') # pointer to file1 lat=inptrs.variables['lat'][:] nlat=len(lat) lon=inptrs.variables['lon'][:] nlon=len(lon) ilev=inptrs.variables['lev'][:] nilev=len(ilev) ncdf= Dataset(ncdfs[im],'r') n =ncdf.variables['n'][:] idx_cols=ncdf.variables['idx_cols'][:,:] ncdf.close() A_field = np.zeros((nterms,nilev),np.float32) theunits=str(chscale[iv])+"x"+inptrs.variables[varis[iv]].units res.tiMainString = varis[iv]+" "+theunits for it in range(0, nterms): for subc in range( 0, n[ire]): varis_bgt= budget_ends[it] npoint=idx_cols[ire,n[subc]-1]-1 tmp=inptrs.variables[varis_bgt][0,:,npoint] #/n[ire] tmp=tmp*cscale[iv] if (varis_bgt == "MPDT" or varis_bgt == "STEND_CLUBB" ): tmp=tmp/1004 A_field[it,:] = (A_field[it,:]+tmp[:]/n[ire]).astype(np.float32 ) inptrs.close() res.xyExplicitLegendLabels = budget_ends[:] p = Ngl.xy(wks,A_field,ilev,res) plot.append(p) xp=np.mod(iv,2) yp=int(iv/2) Ngl.panel(wks,plot[:],[nvaris/2,2],pres) txres = Ngl.Resources() txres.txFont = _Font txres.txFontHeightF = 0.020 Ngl.text_ndc(wks,casenames[im]+" BUDGET at" +str(lons[ire])+"E,"+str(lats[ire])+"N",0.5,0.95,txres) Ngl.frame(wks) Ngl.destroy(wks) return (plote3smbgt)
# A single visualization is produced showing the filled U.S. states. # # # Import numpy and os # import numpy,os # # Import Ngl,Nio support functions. # import Ngl, Nio wks_type = "png" wks = Ngl.open_wks (wks_type,"shapefile1") Ngl.define_colormap(wks,"rainbow+gray") # # Map resources. # res = Ngl.Resources() res.mpProjection = "LambertConformal" res.mpLambertParallel1F = 33 # two parallels res.mpLambertParallel2F = 45 res.mpLambertMeridianF = -98 # central meridian res.mpLimitMode = "Corners" # limit map via two opposite corners res.mpLeftCornerLatF = 22 # left corner res.mpLeftCornerLonF = -125 # left corner
# # Import Ngl support functions. # import Ngl # # Qualifiers for China provinces and India states. # boundaries = ["China:states", "India:States"] # # Change the color map. # wks_type = "ps" wks = Ngl.open_wks(wks_type, "ctrydiv1") Ngl.define_colormap(wks, "StepSeq25") # # Create the plot. # # # Map resources. # res = Ngl.Resources() res.mpDataSetName = "Earth..4" # Change database res.mpDataBaseVersion = "MediumRes" # Medium resolution database res.mpOutlineOn = True # Turn on map outlines res.mpOutlineBoundarySets = "National"