;************************************************* ; lcmask_4.ncl ; ; Concepts illustrated: ; - Adding longitude/latitude labels to a masked Lambert Conformal map ; - Moving the main title up ; - Attaching text strings to the outside of a plot ; - Converting lat/lon values to NDC values ; - Changing the angle of text strings ; - Adding a carriage return to a text string using a function code ;************************************************* ; ; These files are loaded by default in NCL V6.2.0 and newer load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/calendar_decode2.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/crop.ncl" ;---------------------------------------------------------------------- ; This procedure attaches lat/lon labels to a masked lambert plot ; ; You will likely need to change lat_values and/or lon_values to ; contain the locations where you want lat/lon labels. ;---------------------------------------------------------------------- procedure add_lc_labels(wks,map,minlat,maxlat,minlon,maxlon) local lat_values, nlat, lat1_ndc, lat2_ndc, lon1_ndc, lon2_ndc,slope,txres, \ lon_values, PI, RAD_TO_DEG, dum_lft, dum_rgt, dum_bot begin PI = 3.14159 RAD_TO_DEG = 180./PI ;---Pick some "nice" values for the latitude labels. lat_values = ispan(toint(minlat),toint(maxlat),10) * 1. nlat = dimsizes(lat_values) ; ; We need to get the slope of the left and right min/max longitude lines. ; Use NDC coordinates to do this. ; lat1_ndc = new(1,float) lon1_ndc = new(1,float) lat2_ndc = new(1,float) lon2_ndc = new(1,float) datatondc(map,minlon,lat_values(0),lon1_ndc,lat1_ndc) datatondc(map,minlon,lat_values(nlat-1),lon2_ndc,lat2_ndc) slope_lft = (lat2_ndc-lat1_ndc)/(lon2_ndc-lon1_ndc) datatondc(map,maxlon,lat_values(0),lon1_ndc,lat1_ndc) datatondc(map,maxlon,lat_values(nlat-1),lon2_ndc,lat2_ndc) slope_rgt = (lat2_ndc-lat1_ndc)/(lon2_ndc-lon1_ndc) ;---Set some text resources txres = True txres@txFontHeightF = 0.02 txres@txPosXF = 0.1 ; ; Loop through lat values, and attach labels to the left and ; right edges of the masked LC plot. The labels will be ; rotated to fit the line better. ; dum_lft = new(nlat,graphic) ; Dummy array to hold attached strings. dum_rgt = new(nlat,graphic) ; Dummy array to hold attached strings. do n=0,nlat-1 ; Add extra white space to labels. lat_label_rgt = " " + lat_values(n) + "~S~o~N~" ;---Check if North, South, or Zero if(lat_values(n).lt.0) then lat_label_lft = lat_values(n) + "~S~o~N~S " lat_label_rgt = lat_label_rgt + "S" end if if(lat_values(n).gt.0) then lat_label_lft = lat_values(n) + "~S~o~N~N " lat_label_rgt = lat_label_rgt + "N" end if if(lat_values(n).eq.0) then lat_label_lft = lat_values(n) + "~S~o~N~ " end if ;---Left label txres@txAngleF = RAD_TO_DEG * atan(slope_lft) - 90 dum_lft(n) = gsn_add_text(wks,map,lat_label_lft,minlon,lat_values(n),txres) ;---Right label txres@txAngleF = RAD_TO_DEG * atan(slope_rgt) + 90 dum_rgt(n) = gsn_add_text(wks,map,lat_label_rgt,maxlon,lat_values(n),txres) end do ;---------------------------------------------------------------------- ; Now do longitude labels. These are harder because we're not ; adding them to a straight line. ; ; Loop through lon values, and attach labels to the bottom edge of the ; masked LC plot. ; delete(txres@txPosXF) txres@txPosYF = -5.0 ;---Pick some "nice" values for the longitude labels. lon_values = ispan(toint(minlon+10),toint(maxlon-10),10) * 1. nlon = dimsizes(lon_values) dum_bot = new(nlon,graphic) ; Dummy array to hold attached strings. do n=0,nlon-1 ; ; For each longitude label, we need to figure out how much to rotate ; it, so get the approximate slope at that point. ; datatondc(map,lon_values(n)-0.25,minlat,lon1_ndc,lat1_ndc) datatondc(map,lon_values(n)+0.25,minlat,lon2_ndc,lat2_ndc) slope_bot = (lat1_ndc-lat2_ndc)/(lon1_ndc-lon2_ndc) txres@txAngleF = atan(slope_bot) * RAD_TO_DEG ; ; Create longitude label. Add extra carriage returns to ; move label away from plot. ; ;---Check if East, West, or Zero lon_label_bot = " ~C~ ~C~" + abs(lon_values(n)) + "~S~o~N~" if(lon_values(n).lt.0) then lon_label_bot = lon_label_bot + "W" end if if(lon_values(n).gt.0) then lon_label_bot = lon_label_bot + "E" end if ;---Attach to map. dum_bot(n) = gsn_add_text(wks,map,lon_label_bot,lon_values(n),minlat,txres) end do end ;---------------------------------------------------------------------- ; Main code. ;---------------------------------------------------------------------- begin ;************************************************ ; read in netCDF file ;************************************************ a = addfile("E-OBS_tasmax.nc","r") b = addfile("E-OBS_tasmin.nc","r") c = addfile("E-OBS_1971-2000_ys.nc","r") tasmax = a->tasmax tasmin = b->tasmin Pr_annual_total = c->pr lon = a->longitude lat = a->latitude time = a->time minlat = 30 ; min lat to mask maxlat = 75 ; max lat to mask minlon = -20 ; min lon to mask maxlon = 40 ; max lon to mask nlat = dimsizes(lat) mlon = dimsizes(lon) lat_new=conform_dims( (/nlat,mlon/), lat, 0) time2 = calendar_decode2(time,-5) doy = day_of_year(time2(:,0),time2(:,1),time2(:,2)) print(doy) radext = radext_fao56(doy, lat_new, 0) radext = where(ismissing(radext), 0, radext) evtH_0 = refevt_hargreaves_fao56( tmin, tmax, radext, (/0,0,0/) ) print(evtH_0) opt = True opt@nval_crit = 12 PET_m = calculate_monthly_values (evtH_0, "sum", 0,opt) print(PET_m) PET_annual_total = month_to_annual(PET_m, 0) ; Pr_annual_total = month_to_annual(Pr_m, 0) AI = Pr_annual_total/PET_annual_total IM = new(dimsizes(AI),typeof(AI)) IM = where(AI .lt. 0.03,1.0,IM) IM = where(AI .ge. 0.03 .and. AI .lt. 0.2,2.0,IM) IM = where(AI .ge. 0.2 .and. AI .lt. 0.5,3.0,IM) IM = where(AI .ge. 0.5 .and. AI .lt. 0.65,4.0,IM) IM = where(AI .ge. 0.65,5.0,IM) ;************************************************ ; create plot ;************************************************ wks = gsn_open_wks("pdf","Aridity_hargreaves") ; send graphics to PNG file plot = new(1,graphic) cmap = (/ (/19,0,196/),(/102,255,255/), \ (/153,255,51/), (/255,255,101/),(/204,153,0/) /) / 255. res = True ; plot mods desired res@gsnMaximize = True ; enlarge plot res@gsnDraw = False ; Don't draw yet res@gsnFrame = False ; Don't advance frame yet res@mpProjection = "LambertConformal"; choose projection res@cnFillOn = True ; turn on color res@cnLinesOn = False ; turn off contour lines res@lbLabelBarOn = False res@cnLevelSelectionMode = "ManualLevels" res@cnMinLevelValF = toint(min(IM)) ;-- minimum contour level res@cnMaxLevelValF = toint(max(IM)) ;-- maximum contour level res@cnLevelSpacingF = 1 ;-- contour level spacing res@cnFillPalette = cmap(::-1,:) ; set color map res@mpMinLatF = minlat res@mpMaxLatF = maxlat res@mpMinLonF = minlon res@mpMaxLonF = maxlon res@gsnMaskLambertConformal = True ; turn on lc masking res@mpGridAndLimbOn = True res@mpGridLatSpacingF = 10 res@mpGridLonSpacingF = 5 res@gsnAddCyclic = False res@tiMainString = "" res@tiMainOffsetYF = 0.01 ; Move title up a little res@gsnRightString = "" res@gsnLeftString = "" ; pr&lon = pr&lon-180 ; make lon go -180 to 180 res@lbLabelAutoStride = False res@lbLabelStride = 1 res@lbLabelFontHeightF = 0.007 ; make labels smaller res@lbTitleOn = True res@lbTitleString = "Aridity" res@lbTitleFontHeightF = 0.007 res@lbFillColors = cmap(::-1,:) res@lbBoxCount = 5 res@lbLabelAlignment = "BoxCenters" res@lbLabelStrings = (/"Hyper-arid","Arid","Semi-arid","Sub-humid","Humid"/) plot(0) = gsn_csm_contour_map(wks,IM({minlat:maxlat},{minlon:maxlon}),res); create plot ; plot(1) = gsn_csm_contour_map(wks,IM_h({minlat:maxlat},{minlon:maxlon}),res); create plot ; plot(2) = gsn_csm_contour_map(wks,IM_n({minlat:maxlat},{minlon:maxlon}),res); create plot ; plot(3) = gsn_csm_contour_map(wks,IM_f({minlat:maxlat},{minlon:maxlon}),res); create plot ;---Attach latitude labels add_lc_labels(wks,plot(0),minlat,maxlat,minlon,maxlon) ; add_lc_labels(wks,plot(1),minlat,maxlat,minlon,maxlon) ; add_lc_labels(wks,plot(2),minlat,maxlat,minlon,maxlon) ; add_lc_labels(wks,plot(3),minlat,maxlat,minlon,maxlon) ;---Drawing the plot will also draw all the attached labels. draw(plot) frame(wks) ;---Panel plot------------------------------------------ ; resP = True ; modify the panel plot ; resP@gsnPanelLabelBar = True ; resP@lbLabelAutoStride = False ; resP@lbLabelStride = 1 ; resP@lbLabelFontHeightF = 0.008 ; make labels smaller ; resP@lbTitleOn = True ; resP@lbTitleString = "Aridity" ; resP@lbTitleFontHeightF = 0.005 ; resP@lbFillColors = cmap(::-1,:) ; resP@lbBoxCount = 5 ; resP@lbLabelAlignment = "BoxCenters" ; res@lbLabelStrings = (/"Hyper-arid","Arid","Semi-arid","Sub-humid","Humid"/) ; resP@gsnPanelFigureStringsPerimOn = False ; resP@amJust = "TopLeft" ; resP@gsnPanelFigureStrings = (/"A","B","C","D"/) ; resP@gsnPanelFigureStringsFontHeightF = 0.02 ; gsn_panel(wks,plot,(/2,2/),resP) end