undef("gsnColorRange") function gsnColorRange(lower:numeric, upper:numeric, step:numeric, \ center:numeric, color_end:integer, center_color:integer) local nboxes_left, nboxes_right, ncolors_left, ncolors_right, output, \ color_start begin color_start = 2 ; All of the color maps begin with 2. ; ; Calculate the number of color bar boxes to the left and right of ; the "center" value. ; nboxes_left = (center - (lower - step))/step nboxes_right = ((upper + step) - center)/step ; ; Calculate the number of colors in the map on the left and right hand sides. ; ncolors_left = (center_color - color_start + 1) ncolors_right = (color_end - center_color) output = True ; ; ; Either the lower or upper extent of the color map will be adjusted. If ; the magnitude of the lower limit is less than the magnitude of the ; upper limit, then the lower limit has to be "moved in" towards the ; center color. Oppositely, the upper limit will be moved. If both the ; lower and upper numerical values are the same, then pass back 2 as the ; lower extent and color_end (number of colors) as the upper extent (use ; the whole map in other words). ; if(abs(lower) .lt. abs(upper))then output@ColorStart = round(center_color - (ncolors_right/nboxes_right)*nboxes_left, 3) output@ColorEnd = color_end else ;---no "else if" in NCL :( if(abs(lower) .gt. abs(upper))then output@ColorStart = 2 output@ColorEnd = round(center_color + (ncolors_left/nboxes_left)*nboxes_right, 3) else output@ColorStart = 2 output@ColorEnd = color_end end if end if return(output) end ;---------------------------------------------------------------------- ; Main code ;---------------------------------------------------------------------- begin ;---Set the plotting limits. grid_min = -20. ;cn min grid_max = 30. ;cn max grid_step = 1. ;cn int grid_center = 0.0 tmpf = addfile("air.mon.mean.nc","r") temp = tmpf->air(0,:,:) cmap_nam1 = read_colormap_file("MPL_gist_gray") ; 128 colormap cmap_nam2 = read_colormap_file("MPL_gist_rainbow") ; 128 cmap1 = cmap_nam1(3:,0:2) ; start from 3 to meet max allowed colors cmap2 = cmap_nam2(3:,0:2) nclr = dimsizes(cmap1(:,0)) cmap = new((/2*nclr+2,3/),typeof(cmap1)) cmap(0,:) = (/1.,1.,1./) ;foreground color cmap(1,:) = (/0.,0.,0./) ;background color cmap(2:2+nclr-1,:) = cmap1 cmap(2+nclr:2+2*nclr-1,:) = cmap2 delete([/cmap_nam1,cmap_nam2,cmap1,cmap2/]) wks = gsn_open_wks("png","test_file") gsn_define_colormap(wks,cmap) transis = 2+nclr+2 gsn_range = gsnColorRange(grid_min, grid_max, grid_step, grid_center, 2*nclr+2, transis) res = True res@cnLinesOn = False res@cnLineLabelsOn = False res@cnFillOn = True ; res@cnLevelSelectionMode = "ExplicitLevels" ; res@cnLevels = (/-25.,-20,-15,-10,-5,0,5,10,15,20,25,30,35/) res@cnLevelSelectionMode = "ManualLevels" res@cnMinLevelValF = grid_min res@cnMaxLevelValF = grid_max res@cnLevelSpacingF = grid_step res@gsnSpreadColors = True res@gsnSpreadColorStart = gsn_range@ColorStart res@gsnSpreadColorEnd = gsn_range@ColorEnd plot = gsn_csm_contour_map(wks,temp,res) end