;---------------------------------------------------------------------- ; opacity_1.ncl ; ; Concepts illustrated: ; - Drawing partially transparent markers ; - Using ImageMagick's "convert" to convert PS files to JPEG or PNG ; - Using ImageMagick's "composite" to composite two images with opacity. ; - Generating dummy data ; - Creating your own markers for a contour plot ; - Changing the color and thickness of polymarkers ; - Generating dummy data using "random_uniform" ;---------------------------------------------------------------------- ; Note: this example shows the old way of doing transparency with ; NCL. In versions 6.1.0 and later, this method is no longer ; necessary. See the page: ; ; http://www.ncl.ucar.edu/Applications/rgbacolor.shtml ; ; for examples of how to use the new transparency capabilities in NCL. ;---------------------------------------------------------------------- load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" begin ;---Generate some dummy data. nx = 100 ny = 100 data = generate_2d_array(10, 10, -23.,15.,11, (/ny,nx/)) ;---Open a PS file and define a color map wks = gsn_open_wks("ps","opacity") gsn_define_colormap(wks,"rainbow") ;---Resources for contours. res = True res@gsnMaximize = True ; Do NOT set this True! res@cnFillOn = True ; Turn on contour fill res@cnLinesOn = False ; Turn off contour lines res@gsnSpreadColors = True ; Span full color map res@lbOrientation = "Vertical" ; Default is horizontal res@tiMainString = "Opaque markers" ;---Draw the contour plot. plot = gsn_csm_contour(wks,data,res) ; ; Start code for markers. ; ; First get a clover marker. ; clover = NhlNewMarker(wks, "q", 35, 0.0, 0.0, 1.3125, 1., 0.) ;---Marker resources. mkres = True mkres@gsMarkerSizeF = 0.015 ;---Create some dummy marker positions. xpos = ispan(0,100,1) ypos1 = random_uniform(0.,100.,101) random_setallseed(36484749, 9494848) ypos2 = random_uniform(0.,100.,101) ; ; Draw the two sets of markers twice: the first time in black, ; and the second time in two different shades of gray. The ; intensity of the gray will determine how opaque the marker ; ; Draw the markers on their own frames; the contours and two sets ; of markers will be overlaid onto one image later. ; do iwks = 0,1 ; ; The first time in the loop, define a color map with just ; whites and blacks for two sets of markers. ; ; The second time in the loop define a grayscale color map ; of the same size. The second two gray colors will indicate ; how opaque the black markers should be. ; if(iwks.eq.0) then ; ; First time in loop: white, black (dots), black (clover) ; cmap = (/(/1.,1.,1./),(/0.,0.,0./),(/0.,0.,0./)/) gsn_define_colormap(wks,cmap) else ; ; Second time in loop: white, medium gray (dots), darker gray (clover) ; cmap = (/(/0.,0.,0./),(/0.5,0.5,0.5/),(/0.6,0.6,0.6/)/) gsn_define_colormap(wks,cmap) end if ;---Draw the markers mkres@gsMarkerIndex = 16 ; Filled dots mkres@gsMarkerColor = 1 ; first color in color map mkres@gsMarkerThicknessF = 1.0 ; Default gsn_polymarker(wks,plot,xpos,ypos1,mkres) mkres@gsMarkerColor = 2 ; second color in color map mkres@gsMarkerIndex = clover ; Circle with "x" mkres@gsMarkerThicknessF = 2.0 ; 2x as thick gsn_polymarker(wks,plot,xpos,ypos2,mkres) frame(wks) end do ; ; This section converts the PS files to jpegs and composites them and ; overlays them into one big PNG file. ; ; Thanks to Tim Scheitlin for showing me this trick! ; ; Split the PS file into three PS files. ; pict0001.ps - color contours ; pict0002.ps - black markers ; pict0002.ps - gray markers ; delete(wks) ; Make sure PS file is closed properly system("psplit opacity.ps") ; pict0001.ps, pict0002.ps, pict0003.ps ;---Convert to jpeg. do i=1,3 system("convert -density 300 -geometry 1500x1500 pict000" + i + ".ps pict000" + i + ".jpg") end do ;---Composite the black and gray markers and convert to png. system("composite -compose CopyOpacity pict0003.jpg pict0002.jpg markers.png") ;---Overlay on color contours. system("convert pict0001.jpg markers.png -composite final.jpg") system("convert -trim final.jpg opacity_1_lg.jpg") ;---Clean up system("/bin/rm pict0001.ps pict0002.ps pict0003.ps") system("/bin/rm pict0001.jpg pict0002.jpg pict0003.jpg") system("/bin/rm final.jpg") end