#======================================================================= # A complex example of a CDAT contour plot. # # Copyright (c) 2003-2004 by Johnny Lin. For licensing, distribution # conditions, contact information, and additional documentation see # the URL http://www.johnny-lin.com/pylib.html#license. #======================================================================= #- Import modules: import MA import Numeric as N import cdms, vcs #- Data: "Distorted" bullseyes over each pole, with missing data in # the mid-eastern Pacific Ocean: lat = N.arange(13) * 15.0 - 90. lon = N.arange(25) * 15.0 - 180.0 data = N.outerproduct(N.sin(lat*N.pi/360.), N.cos(lon*N.pi/360.)) missing = 1e20 data[6:8,2:3] = missing data[4:5,3:4] = missing #- Open VCS canvas: v = vcs.init() #- Create graphics method object for isoline and fill plots and # template for isofill: my_fill_gm = v.createisofill('my_fill_gm', 'default') my_line_gm = v.createisoline('my_line_gm', 'default') my_fill_tpl = v.createtemplate('my_fill_tpl', 'default') #- Create a blue/red-scale colormap and make it the active color # map: my_cmap = v.createcolormap('my_cmap', 'default') for icell in range(16,128): rvalue = int( float(icell-16)/(127.-16.) * 75. ) gvalue = rvalue bvalue = 100 my_cmap.index[icell] = [rvalue, gvalue, bvalue] for icell in range(128,240): rvalue = 100 gvalue = 75 - int( float(icell-128)/(239.-128.) * 75. ) bvalue = gvalue my_cmap.index[icell] = [rvalue, gvalue, bvalue] v.setcolormap('my_cmap') #- Set coordinates (normalized units) for lower-left and upper- # right bounding box (bbll*, bbur*); set height of font for # the overall title (title_fontht), [xy]titles (xytitle_fontht), # and for the tick labels (ticklabel_fontht); set length of tick # marks (in normalized units): bbllx = 0.2 bblly = 0.3 bburx = 0.9 bbury = 0.9 title_fontht = 40 xytitle_fontht = 30 ticklabel_fontht = 30 ticklength = 0.01 #- Set font to use for the ticklabels (ticklabel_font) and the # titles (titles_font): ticklabel_font = 1 titles_font = 1 #- Set conversion factor to multiply font height coordinates by # to obtain the value in normalized coordinates. This is set # by trial-and-error: fontht2norm = 0.0007 #- Create text-table and text-orientation objects for x- and # y-axis tick labels and using them set font and font height. # Also set y-axis tick labels to be vertically aligned at the # mid-way point: ttab_xticklabel = \ v.createtexttable( 'xticklabel_ttab', 'default' ) ttab_yticklabel = \ v.createtexttable( 'yticklabel_ttab', 'default' ) tori_xticklabel = \ v.createtextorientation( 'xticklabel_tori', 'defcenter' ) tori_yticklabel = \ v.createtextorientation( 'yticklabel_tori', 'default' ) ttab_xticklabel.font = ticklabel_font ttab_yticklabel.font = ticklabel_font ttab_xticklabel.color = 241 ttab_yticklabel.color = 241 tori_xticklabel.height = ticklabel_fontht tori_yticklabel.height = ticklabel_fontht tori_yticklabel.valign = 'half' #- Create/set text-table and text-orientation objects for overall # title and axis title settings. Turn on these fields in the # template and set the new text-table/orientation objects to # those fields in the template: ttab_title = \ v.createtexttable( 'title_ttab', 'default' ) tori_title = \ v.createtextorientation( 'title_tori', 'defcenter' ) ttab_title.font = titles_font ttab_title.color = 241 tori_title.height = title_fontht ttab_xytitle = \ v.createtexttable( 'xytitle_ttab', 'default' ) ttab_xytitle.font = titles_font ttab_xytitle.color = 241 tori_xtitle = \ v.createtextorientation( 'xtitle_tori', 'defcenter' ) tori_ytitle = \ v.createtextorientation( 'ytitle_tori', 'defcentup' ) tori_xtitle.height = xytitle_fontht tori_ytitle.height = xytitle_fontht my_fill_tpl.dataname.texttable = ttab_title my_fill_tpl.dataname.textorientation = tori_title my_fill_tpl.xname.texttable = ttab_xytitle my_fill_tpl.xname.textorientation = tori_xtitle my_fill_tpl.yname.texttable = ttab_xytitle my_fill_tpl.yname.textorientation = tori_ytitle my_fill_tpl.dataname.priority = 1 my_fill_tpl.xname.priority = 1 my_fill_tpl.yname.priority = 1 #- Turn off some default titling fields: my_fill_tpl.mean.priority = 0 my_fill_tpl.max.priority = 0 my_fill_tpl.min.priority = 0 #- Set position of data field, plot box, and x-axis bottom ticks # and labels; set text-table and text-orientation of x-axis bottom # labels; turn off upper-right set of tick labels: my_fill_tpl.data.x1 = my_fill_tpl.box1.x1 = bbllx my_fill_tpl.data.y1 = my_fill_tpl.box1.y1 = bblly my_fill_tpl.data.x2 = my_fill_tpl.box1.x2 = bburx my_fill_tpl.data.y2 = my_fill_tpl.box1.y2 = bbury my_fill_tpl.xtic1.y1 = bblly - ticklength my_fill_tpl.xtic1.y2 = bblly my_fill_tpl.xlabel1.y = bblly - (3.0 * ticklength) my_fill_tpl.xlabel1.texttable = ttab_xticklabel my_fill_tpl.xlabel1.textorientation = tori_xticklabel my_fill_tpl.xtic2.priority = 0 my_fill_tpl.ytic2.priority = 0 #- Create "nice" x-axis labels and set them into the graphics # method: my_fill_gm.xticlabels1 = { 0.0:"0" , 45.0:"45E", -45.0:"45W" , 90.0:"90E", -90.0:"90W" , 135.0:"135E", -135.0:"135W" , 180.0:"180E", -180.0:"180W" } #- Create "nice" y-axis labels, set them in the graphics method, # calculate y-axis title location based on the longest label # length, set y-axis ticks and tick label locations, and set # text-table and text-orientation for y-axis tick labels: ylabels = { 0.0:"EQ" , 30.0:"30N", -30.0:"30S" , 60.0:"60N", -60.0:"60S" , 90.0:"90N", -90.0:"90S" } my_fill_gm.yticlabels1 = ylabels longest_ylabels = max([ len(ylabels.values()[i]) \ for i in range(len(ylabels)) ]) my_fill_tpl.ytic1.x1 = bbllx - ticklength my_fill_tpl.ytic1.x2 = bbllx my_fill_tpl.ylabel1.x = bbllx - ticklength \ - ( longest_ylabels \ * ticklabel_fontht * fontht2norm ) my_fill_tpl.ylabel1.texttable = ttab_yticklabel my_fill_tpl.ylabel1.textorientation = tori_yticklabel #- Position overall title, x-axis title, and y-axis title: my_fill_tpl.dataname.x = (bburx-bbllx)/2.0 + bbllx my_fill_tpl.dataname.y = bbury + ( 1.7 * title_fontht \ * fontht2norm ) my_fill_tpl.xname.x = (bburx-bbllx)/2.0 + bbllx my_fill_tpl.xname.y = my_fill_tpl.xlabel1.y \ - (1.7 * title_fontht * fontht2norm) my_fill_tpl.yname.x = my_fill_tpl.ylabel1.x \ - ( 1.6 * title_fontht * fontht2norm ) my_fill_tpl.yname.y = (bbury-bblly)/2.0 + bblly #- Calculate and set filled-contour levels based upon the max and # min of the non-missing data in the array: my_min = MA.minimum(MA.masked_values(data, missing)) my_max = MA.maximum(MA.masked_values(data, missing)) con_levels = vcs.mkscale(my_min, my_max, 16) my_fill_gm.levels = con_levels my_fill_gm.fillareacolors = vcs.getcolors(con_levels, split=1) #- Create text-table object that has color set to black to use in # making sure the legend (color bar) has black lettering and set # my isofill template legend text-table to that object: ttab_black = v.createtexttable('ttab_black', 'default') ttab_black.color = 241 my_fill_tpl.legend.texttable = ttab_black #- Now that the good template settings are all done, copy it and # turn off a bunch of things for the isoline overlay. Turn things # off on the isoline template to make sure nothing is over-written: my_line_tpl = v.createtemplate('my_line_tpl', 'my_fill_tpl') my_line_tpl.dataname.priority = 0 my_line_tpl.xname.priority = 0 my_line_tpl.yname.priority = 0 my_line_tpl.xlabel1.priority = 0 my_line_tpl.xlabel2.priority = 0 my_line_tpl.ylabel1.priority = 0 my_line_tpl.ylabel2.priority = 0 my_line_tpl.xtic1.priority = 0 my_line_tpl.xtic2.priority = 0 my_line_tpl.ytic1.priority = 0 my_line_tpl.ytic2.priority = 0 my_line_tpl.box1.priority = 0 #- Make contour lines to be at the same place as the filled contours # and turn the contour labels on. Make the linestyle for contour # lines to be dashed if contour are negative and solid if they are # positive or 0: my_line_gm.level = con_levels my_line_gm.label = 'y' line_pos_con = v.createline('pos_con_line', 'default') line_neg_con = v.createline('neg_con_line', 'default') line_pos_con.type = 'solid' line_neg_con.type = 'dash' my_line_gm.line = N.where( N.array(con_levels) >= 0.0 \ , line_pos_con, line_neg_con ).tolist() #- Create latitude and longitude axis: lonAxis = cdms.createAxis(lon) latAxis = cdms.createAxis(lat) #- Render plot, plot overall title, x-axis title, and y-axis title: v.plot( data, my_fill_gm, my_fill_tpl, xaxis=lonAxis, yaxis=latAxis \ , continents=1 \ , xname='Longitude [deg]', yname='Latitude [deg]' \ , name='A Complex Contour Plot' ) v.plot( data, my_line_gm, my_line_tpl, xaxis=lonAxis, yaxis=latAxis ) #- Postscript output of the plot: v.postscript('latlon_plot.ps') v.gif('latlon_plot.gif', 'r') #====== end of file ======