next up previous contents
Next: Colorbars Up: Other tips and tricks Previous: Other tips and tricks   Contents

Contours on images

We've covered contours and tv'ing images, but it can be handy to put contours on images (often with the image and the contours being of different 2D arrays). The code below is the best way I have found to do this (tvimage is in  jmarsham/IDL/Procedures/Display/ and again is from David Fanning.)

sz=200
array_1 = (intarr(sz)+1.)#indgen(sz);fltarr(200)#fltarr(200)=
                                    ;fltarr (200,200)
array_2 = indgen(sz)#(intarr(sz)+1)
pos=[0.13,0.17,0.95,0.85]

set_plot,'ps'
device,file='~jmarsham/eg_tv_image.eps',/encapsulated,/color
loadct,39
tvimage,array_1,position=pos,/keep_aspect_ratio,/minus_one
contour,array_2,indgen(sz),indgen(sz),position=pos,/noerase,$
levels=indgen(21)*10.,c_labels=intarr(21)+1,xtitle='x', ytitle='y'
device,/close

This is a bit dry but try,

pro eg_plot_lem

;+
;An example of contouring vertical velocities on top of ice mixing ratios
;for irregularly gridded LEM data
;JOHN MARSHAM 13/4/05
;-

xsz=502 ;xsize of data
ysz=91 ; ysize of data

zn=fltarr(ysz) ;heights of LEM levels
imr=fltarr(xsz,ysz) ;ice mixing ratio array
w=imr*0. ; vertical velocity array

;Open gzipped files (no need to unzip!)
openr,12,'~jmarsham/public_html/Teaching/IDL_course/LEM_data/run0764_dg33_zn.dat.gz',/compress
readu,12,zn
close,12
openr,12,'~jmarsham/public_html/Teaching/IDL_course/LEM_data/run0764_dg33_imr.dat.gz',/compress
readu,12,imr
close,12
openr,12,'~jmarsham/public_html/Teaching/IDL_course/LEM_data/run0764_dg33_w.dat.gz',/compress
readu,12,w
close,12

;Interpolate to a regular height grid to greyscale data
newz=indgen(201)*100.;0 to 20000 m in 100 m intervals
newimr=fltarr(xsz,201)
neww=fltarr(xsz,201)

for i = 0,xsz-1 do newimr(i,*)=interpol(imr(i,*),zn,newz)
for i = 0,xsz-1 do neww(i,*)=interpol(w(i,*),zn,newz)

;Defining plotting requirements
lev=(indgen(21)-10)*.25 ;-4,-3.5,.....4  contour levels for w
cls=[2,2,2,2,2,2,2,2,2,2,1,0,0,0,0,0,0,0,0,0,0,0];linestyle for contours
pos=[0.17,0.17,0.95,0.8];position in window
bs_min=0. ; min imr for greyscale
bs_max=0.0002 ;max imr for greyscale
bottom=50;min zpos to plot
top=100;max zpos to plot
lhs=0;left most position  to plot
rhs=200;right most position  to plot

;arrays for plotting
plot_imr=rebin(newimr(lhs:rhs,bottom:top),rhs-lhs+1,(top-bottom+1)*5)
plot_w=rebin(neww(lhs:rhs,bottom:top),rhs-lhs+1,(top-bottom+1)*5)
plot_z=rebin(newz(bottom:top),(top-bottom+1)*5)
Big=256l*256*256-1 ; white
!p.charsize=1.5
loadct,0
!p.color=0 ;black
!p.background=big;white

window,0,retain=2
tvimage,Big-bytscl(plot_imr,min=bs_min,max=bs_max),position=pos,$
/minus_one
contour,plot_w,indgen(rhs-lhs+1)*100.,plot_z,position=pos,/noerase,$
levels=lev,xtitle='x (m)', ytitle='y (m)',c_linestyle=cls,$
subtitle='Contours of vertical velocity at 0.25 m/s intervals'

colorbar,range=[bs_min,bs_max]*1000.,title='IMR (g/kg)',format='(f4.2)',$
divisions=4
write_png,'~jmarsham/imr_w_2d_greyscale.png',tvrd()

;OR 

window,1,retain=2
ncon=5
cols=[255,200,150,100,50,10]
contour,plot_imr,indgen(rhs-lhs+1)*100.,plot_z,position=pos,/noerase,$
levels=[0,1.e-5,5.0e-5,1.e-4,1.5e-4],xtitle='x (m)', ytitle='y (m)',/fill,$
c_color=cols
contour,plot_w,indgen(rhs-lhs+1)*100.,plot_z,position=pos,/noerase,$
levels=lev,xtitle='x (m)', ytitle='y (m)',c_linestyle=cls,$
subtitle='Contours of vertical velocity at 0.25 m/s intervals'

ncon=16 ; number of contours
cdif=1.5e-5; contour spacing
cmin=0 ; min contour value
conval=fltarr(ncon)
cols=244-12*findgen(ncon+1); Colours to use (0 to 244 in jumps of 12)
conval=(findgen(ncon)*cdif)+cmin ; values of contours: 0 to  0.000225000

window,1,retain=2
contour,plot_imr,indgen(rhs-lhs+1)*100.,plot_z,position=pos,/noerase,$
levels=conval,xtitle='x (m)', ytitle='y (m)',/fill,$
c_color=cols
contour,plot_w,indgen(rhs-lhs+1)*100.,plot_z,position=pos,/noerase,$
levels=lev,xtitle='x (m)', ytitle='y (m)',c_linestyle=cls,$
subtitle='Contours of vertical velocity at 0.25 m/s intervals'

conmin=min(conval)
conmax=max(conval)
nopos = [0.15,0.88,0.93,0.93]
!p.position=nopos
xtckvalues=findgen(ncon+1)*(conmax-conmin)/(ncon-1)+conmin
xv=xtckvalues([0,4,8,12,16])

contour,(findgen(ncon+2))#[1,1],$ 
1000.*(findgen(ncon+2)-1)*(conmax-conmin)/(ncon-1)+conmin,[0,1],/cell_fill,/closed,$
levels=(findgen(ncon)+1),$
c_colors=cols,/noerase,xrange=[conmin,conmax],xstyle=1,$
xticks=5,xticklen=1,ystyle=5,title='IMR (g/kg)', $
xtickv=xv*1000.,charsize=1.,charthick=1

write_png,'~jmarsham/imr_w_2d_contoured.png',tvrd()



John Marsham 2005-04-22