爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 39744|回复: 31

[作图] (求助)怎么用NCL画个带colorbar的散点图

[复制链接]

新浪微博达人勋

发表于 2014-10-8 21:50:53 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
   

        刚学NCL,一开始想用NCL画个contour图,所用的数据是flood.dat,三列,分别为经度,纬度,淹没高度。 数据点仅仅存在于雷州半岛周围很小一片区域(如matlab图上彩色点所示),画contour图发现雷州半岛中央也被差出了值,而实际上这里其实都没有数据点,也没有值。所以我想画散点图避免错误插值,但是我在NCL官网上没有找到比较合适的例子,附件中的图片是我用matlab的scatter函数画出的散点图,这就是我想用NCL画出的样子。当然NCL的contour如果能作出这个效果自然更好~

        求解惑,感激不尽!
        以下是我使用的脚本
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/csm/contributed.ncl"

begin
nrow = numAsciiRow("flood.dat")
ncol = numAsciiCol("flood.dat")
a = asciiread("flood.dat",(/nrow,ncol/),"float")   
lat=a(:,1)
lon=a(:,0)  
x=a(:,2)        
lat@units= "degrees-north"
lon@units= "degrees-east"  

xwks = gsn_open_wks("X11","FloodPlain")  
gsn_define_colormap(xwks,"rainbow") ; choose colormap

res                             = True        
res@mpFillOn     = True           ; Turn on map fill.
res@mpFillColors = (/0,0,-1,0/)   ;第二个填充海洋,第三个填充陆地
res@sfXArray                    = lon
res@sfYArray                    = lat
res@cnLineDrawOrder      = "PreDraw"
res@cnLinesOn                   = True                ; no contour lines
;res@cnLineLabelsDrawOrder      = "predraw"
res@cnLineLabelsOn =      True
res@cnFillDrawOrder      = "predraw"
res@cnFillOn                    = True                ; turn on color
res@mpProjection   ="Mercator"
res@mpLimitMode  ="LatLon"
res@mpMinLatF =20.15     
res@mpMaxLatF =  21.75
res@mpMinLonF =109.5
res@mpMaxLonF =110.75
res@mpCenterLonF = 180
res@mpDataBaseVersion   =  "HighRes"  ; Medium resolution database
res@mpDataResolution =        "Finest"
res@mpDataSetName    = "Earth..3"
res@gsnSpreadColors             = True              ; use full color map
res@lbLabelAutoStride           = True  ; every other label
res@lbBoxLinesOn         = False
res@lbOrientation = "Vertical"
res@cnLevelSelectionMode        = "ManualLevels"       ; manual levels
res@cnMinLevelValF              = 0
res@cnMaxLevelValF              =10
res@cnLevelSpacingF             =0.5
res@cnLevels             = 0
res@pmTickMarkDisplayMode       = "Always"             ; turn on tick marks
res@tiMainString                = "Floodplain area"
res@gsnAddCyclic                = False        ; regional data
res@gsnMaximize = True
restxt                                  = True
restxt@txFontHeightF                    = 0.01
restxt@txFontColor                        = "black"
polyres                              = True
polyres@gsMarkerIndex     = 16          ; polymarker style
polyres@gsMarkerSizeF     = 3.          ; polymarker size
ypts = (/ 35.383,         34.75,  31.4, 30.6167, 31.1167, 30.8467, 28.083, 32.133 /)
xpts = (/119.55, 119.42,121.5 ,121.6167,  121.9, 121.8393, 121.28  ,121.617 /)
labels = (/ "-5.71388004", "-10.72738152", "11.05874602", "3.474859275" ,"12.64624476","4.434917977",  "5.423288245",   "5.174664561" /);

plot=gsn_csm_contour_map(xwks,x,res)
points= gsn_add_polymarker(xwks, plot, xpts(:), ypts(:), polyres)
text        = gsn_add_text(xwks,plot,labels(:),xpts(:),ypts(:)+0.1,restxt)

draw(plot)
frame(xwks)

end






matlab作的散点图

matlab作的散点图

NCL作的contour图

NCL作的contour图

flood.dat

178.22 KB, 下载次数: 98, 下载积分: 金钱 -5

使用的数据

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-10-8 21:55:30 | 显示全部楼层
不好意思,flood.dat的形式是这样的
110.2420 20.9566  0.4995
110.2505 20.9602  0.5020
110.2607 20.9608  0.5166
110.2652 20.9537  0.5111
110.2261 20.9539  0.5048
110.2335 20.9601  0.5047
110.2410 20.9650  0.5039
110.2474 20.9710  0.5044
110.2557 20.9669  0.5040
110.2645 20.9701  0.5028
110.2697 20.9626  0.5021
110.2743 20.9552  0.4999
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-10-9 09:07:27 | 显示全部楼层
我觉得NCL画这种渐变色散点图是比较麻烦的。
以前曾经尝试过用gsn_add_polymarker,根据数值大小一个个设置颜色,然后循环点在地图上。
坏处是效率太低了。

评分

参与人数 1金钱 +5 收起 理由
Ahsars + 5 很给力!

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-10-9 11:11:42 | 显示全部楼层

评分

参与人数 1金钱 +5 收起 理由
Ahsars + 5 赞一个!

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-10-9 15:23:06 | 显示全部楼层
longlivehj 发表于 2014-10-9 11:11
楼上正解。楼主参考以下链接中的例子:
http://www.ncl.ucar.edu/Applications/scatter.shtml
http://www ...

我准备试一下station中的例子
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-10-10 18:03:38 | 显示全部楼层
hzwjy 发表于 2014-10-9 09:07
我觉得NCL画这种渐变色散点图是比较麻烦的。
以前曾经尝试过用gsn_add_polymarker,根据数值大小一个个设 ...

搞定了,谢谢
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-10-10 18:03:54 | 显示全部楼层
longlivehj 发表于 2014-10-9 11:11
楼上正解。楼主参考以下链接中的例子:
http://www.ncl.ucar.edu/Applications/scatter.shtml
http://www ...

多谢了,已搞定
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-1-7 20:49:48 | 显示全部楼层
Ahsars 发表于 2014-10-10 18:03
多谢了,已搞定

楼主如何搞定的贴出来分享下吧~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2015-1-12 22:29:33 | 显示全部楼层
海蓝海魂一 发表于 2015-1-7 20:49
楼主如何搞定的贴出来分享下吧~
  1. ;----------------------------------------------------------------------
  2. ; station_2.ncl
  3. ;----------------------------------------------------------------------
  4. ;
  5. ; Concepts illustrated:
  6. ;   - Drawing markers on a map indicating the locations of station data
  7. ;   - Generating dummy data using "random_uniform"
  8. ;   - Drawing markers of different sizes and colors on a map
  9. ;   - Drawing a custom legend outside of a map plot
  10. ;   - Attaching a custom labelbar to a plot
  11. ;
  12. ; This example creates some dummy station data, and then plots each
  13. ; value by coloring and sizing it depending on which range of values
  14. ; it falls in.
  15. ;
  16. ; It creates two plots: one with a legend with markers and text,
  17. ; and one with a labelbar.
  18. ;----------------------------------------------------------------------
  19. load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
  20. load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
  21. load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
  22. ;----------------------------------------------------------------------
  23. ; This procedure draws a legend with markers and text at the bottom
  24. ; of the screen
  25. ;----------------------------------------------------------------------
  26. procedure draw_legend(wks,lat[*][*]:numeric,lon[*][*]:numeric,\
  27.                       arr[*]:numeric,colors)
  28. local gsres, txres, xleg, xtxt, yleg, ytxt, i, labels, nitems
  29. begin
  30.   narr   = dimsizes(arr)
  31.   nmarkers = narr+1
  32.   labels = new(nmarkers,string)

  33. ;---Generate the labels for each marker.
  34.   do i = 0, nmarkers-1
  35.     if (i.eq.0) then
  36.       labels(i) = "x < " + arr(0)
  37.     end if
  38.     if (i.eq.nmarkers-1) then
  39.       labels(i) = "x >= " + max(arr)
  40.     end if
  41.     if (i.gt.0.and.i.lt.nmarkers-1) then      
  42.       labels(i) = arr(i-1) + " <= x < " + arr(i)
  43.     end if
  44.   end do
  45. ;
  46. ;  Create logical variables to hold the marker and text resources.
  47. ;  These markers are different than the XY markers, because they are not
  48. ;  associated with an XY plot. You can put these markers on any plot.
  49. ;
  50.   gsres               = True
  51.   gsres@gsMarkerIndex = 16          ; Use filled dots for markers.

  52.   txres               = True
  53.   txres@txFontHeightF = 0.015

  54. ;
  55. ; Loop through each grouping of markers, and draw them one set at
  56. ; a time, assigning the proper color and size with gsn_marker.
  57. ;
  58. ; At the same time, draw a legend showing the meaning of the markers.
  59. ;

  60.   xleg = (/0.07,0.07,0.32,0.32,0.57,0.57,0.82,0.82/)   ; Location of
  61.   xtxt = (/0.16,0.16,0.41,0.41,0.66,0.66,0.91,0.91/)   ; legend markers
  62.   yleg = (/0.22,0.17,0.22,0.17,0.22,0.17,0.22,0.17/)   ; and text
  63.   ytxt = (/0.22,0.17,0.22,0.17,0.22,0.17,0.22,0.17/)   ; strings.

  64.   do i = 0, dimsizes(lat(:,0))-1
  65.     if (.not.ismissing(lat(i,0)))
  66.       gsres@gsMarkerColor      = colors(i)
  67.       gsres@gsMarkerThicknessF = 0.7*(i+1)
  68. ;---Add marker and text for the legend.
  69.       gsn_polymarker_ndc(wks,          xleg(i),yleg(i),gsres)
  70.       gsn_text_ndc      (wks,labels(i),xtxt(i),ytxt(i),txres)
  71.     end if
  72.   end do

  73. end

  74. ;----------------------------------------------------------------------
  75. ; This function attaches a labelbar to the given plot.
  76. ;----------------------------------------------------------------------
  77. function attach_labelbar(wks,map,arr[*]:numeric,colors[*])
  78. local lbres, vph, vpw, nboxes
  79. begin
  80.   getvalues map
  81.     "vpHeightF" : vph
  82.     "vpWidthF"  : vpw
  83.   end getvalues

  84.   nboxes = dimsizes(colors)

  85.    
  86.   lbres                    = True          ; labelbar only resources
  87.   lbres@lbAutoManage       = False          ; Necessary to control sizes
  88.   lbres@lbFillColors       = colors
  89.   lbres@vpWidthF           = 0.7 * vpw     ; labelbar width
  90.   lbres@vpHeightF          = 0.1 * vph     ; labelbar height
  91.   lbres@lbMonoFillPattern  = True          ; Solid fill pattern
  92.   lbres@lbLabelFontHeightF = 0.01          ; font height. default is small
  93.   lbres@lbOrientation      = "horizontal"
  94.   ;lbres@lbOrientation      = "vertical"
  95.   ;lbres@lbLabelPosition                        ="Right"
  96.   lbres@lbPerimOn          = False
  97.   lbres@lbLabelAlignment   = "InteriorEdges"
  98.   lbid = gsn_create_labelbar(wks,nboxes,""+toint(arr),lbres)

  99.   amres                  = True
  100.   amres@amJust           = "TopCenter"
  101.   amres@amParallelPosF   =  0.0    ; Center
  102.   amres@amOrthogonalPosF =  0.6    ; Bottom
  103.   annoid = gsn_add_annotation(map,lbid,amres)
  104.   return(annoid)
  105. end

  106. ;----------------------------------------------------------------------
  107. ; Main code
  108. ;----------------------------------------------------------------------
  109. filename = new(3,string)
  110. filename(0) = "flood_T"
  111. filename(1) = "flood_S"
  112. filename(2) = "flood_TS"

  113. begin
  114. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Tide;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  115. ;---Set some needed arrays
  116.   arr = ispan(0,450,15)                                       
  117.   narr = dimsizes(arr)
  118.   labels = new(narr+1,string)   ; Labels for legend.
  119. nrow_T = numAsciiRow("flood_T.dat")
  120. ncol_T = numAsciiCol("flood_T.dat")
  121. print("flood_T.dat")
  122. data_T = asciiread("flood_T.dat",(/nrow_T,ncol_T/),"float")   
  123. lat_T = data_T(:,1)
  124. lon_T = data_T(:,0)
  125. lat_T@units= "degrees-north"
  126. lon_T@units= "degrees-east"  
  127. R_T = data_T(:,2)*100
  128. npts_T = nrow_T                                    ; Number of points.
  129. ;
  130. ; Create X and Y arrays to hold the points for each range and initialize
  131. ; them to missing values.  We want to use num_distinct_markers
  132. ; different colors, so we need num_distinct_markers sets of X and
  133. ; Y points.
  134. ;
  135.   num_distinct_markers = dimsizes(arr)+1        ; number of distinct markers
  136.   lat_new_T = new((/num_distinct_markers,dimsizes(R_T)/),float,-999)
  137.   lon_new_T = new((/num_distinct_markers,dimsizes(R_T)/),float,-999)

  138. ;---Group the points according to which range they fall in.
  139.   do i = 0, num_distinct_markers-1
  140.     if (i.eq.0) then
  141.       indexes = ind(R_T.lt.arr(0))
  142.     end if
  143.     if (i.eq.num_distinct_markers-1) then
  144.       indexes = ind(R_T.ge.max(arr))
  145.     end if
  146.     if (i.gt.0.and.i.lt.num_distinct_markers-1) then      
  147.       indexes = ind(R_T.ge.arr(i-1).and.R_T.lt.arr(i))
  148.     end if
  149. ;
  150. ; Now that we have the set of indexes whose values fall within
  151. ; the given range, take the corresponding lat/lon values and store
  152. ; them, so later we can color this set of markers with the appropriate
  153. ; color.
  154. ;
  155.     if (.not.any(ismissing(indexes))) then
  156.       npts_range = dimsizes(indexes)   ; # of points in this range.
  157.       lat_new_T(i,0:npts_range-1) = lat_T(indexes)
  158.       lon_new_T(i,0:npts_range-1) = lon_T(indexes)
  159.     end if
  160.     delete(indexes)            ; Necessary b/c "indexes" may be a different
  161.                                ; size next time.
  162.   end do

  163. ;----------------------------------------------------------------------
  164. ; Begin plotting section.
  165. ;----------------------------------------------------------------------
  166.   wks = gsn_open_wks("ps","flood_T")     ; Open a workstation and

  167. ;---Set up some map resources.
  168.   mpres              = True
  169.   
  170.   
  171.   
  172.   mpres@gsnMaximize  = True             ; Maximize plot in frame.
  173.   mpres@gsnFrame     = False            ; Don't advance the frame
  174.   mpres@gsnDraw      = False            ; Don't advance the frame


  175.   mpres@tmXTBorderOn           = True     ; Don't draw top axis.
  176.   mpres@tmXTOn                 = True
  177.   mpres@tmXBMode             = "Explicit"  ; Set tick mark mode.
  178.   mpres@tmXBValues           = ispan(10940,11090,30)/100.0                                       
  179.   mpres@tmXBLabels           = ispan(10940,11090,30)/100.0                                       
  180.   mpres@tmYLMode             = "Explicit"  ; Set tick mark mode.
  181.   mpres@tmYLValues           = ispan(202,215,2)/10.0                                       
  182.   mpres@tmYLLabels           = ispan(202,215,2)/10.0
  183. ;---Zoom in on United States.
  184.   mpres@mpMinLatF            = 20.2
  185.   mpres@mpMaxLatF            = 21.5
  186.   mpres@mpMinLonF            = 109.40
  187.   mpres@mpMaxLonF            = 110.9
  188.   mpres@mpCenterLonF         = 180
  189.   mpres@mpDataBaseVersion   =  "HighRes"  ; High resolution database
  190.   mpres@mpDataResolution =        "Finest"
  191.   mpres@mpDataSetName    = "Earth..3" ;
  192.   mpres@mpFillColors = (/"transparent","transparent","gray","transparent"/)

  193.   mpres@tiMainString = "FloodPlain Scatters "
  194.   map = gsn_csm_map(wks,mpres)
  195.   
  196.   gsres               = True
  197.   gsres@gsMarkerIndex = 1          ; Use filled dots for markers.

  198. ;---Get nice spacing through color map for marker colors
  199.         gsn_define_colormap(wks,"Rainbow")
  200.   getvalues wks
  201.     "wkColorMapLen" : clen     ; number of colors in color map
  202.   end getvalues

  203.   nstep = (clen-2)/narr
  204.   colors = ispan(2,clen-1,nstep)

  205. ;---Loop through each "bin" and attach the markers to the map.
  206.   do i = 0, num_distinct_markers-1
  207.     if (.not.ismissing(lat_new_T(i,0)))
  208.       gsres@gsMarkerColor      = colors(i)
  209.       dumstr = unique_string("marker")
  210.       map@$dumstr$ = gsn_add_polymarker(wks,map,lon_new_T(i,:),lat_new_T(i,:),gsres)
  211.     end if
  212.   end do

  213. ;----------------------------------------------------------------------
  214. ; First version of this plot has a legend at the bottom
  215. ;----------------------------------------------------------------------
  216.   ;draw(map)                  ; Drawing the map will draw the markers
  217.   ;draw_legend(wks,lat_new,lon_new,arr,colors) ; Draw a legend at the bottom

  218.   ;frame(wks)    ; Advance the frame

  219. ;----------------------------------------------------------------------
  220. ; Second version of this plot has a labelbar added.
  221. ;----------------------------------------------------------------------
  222.   lbid = attach_labelbar(wks,map,arr,colors)   ; Attach a labelbar  

  223.   draw(map)     ; Drawing the map will draw everything   
  224.       
  225.   frame(wks)    ; Advance the frame.

  226. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Surge;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  227. ;---Set some needed arrays
  228.   arr = ispan(0,450,15)                                       
  229.   narr = dimsizes(arr)
  230.   labels = new(narr+1,string)   ; Labels for legend.
  231. nrow_S = numAsciiRow("flood_S.dat")
  232. ncol_S = numAsciiCol("flood_S.dat")
  233. print("flood_S.dat")
  234. data_S = asciiread("flood_S.dat",(/nrow_S,ncol_S/),"float")   
  235. lat_S = data_S(:,1)
  236. lon_S = data_S(:,0)
  237. lat_S@units= "degrees-north"
  238. lon_S@units= "degrees-east"  
  239. R_S = data_S(:,2)*100
  240. npts_S = nrow_S                                    ; Number of points.
  241. ;
  242. ; Create X and Y arrays to hold the points for each range and initialize
  243. ; them to missing values.  We want to use num_distinct_markers
  244. ; different colors, so we need num_distinct_markers sets of X and
  245. ; Y points.
  246. ;
  247.   num_distinct_markers = dimsizes(arr)+1        ; number of distinct markers
  248.   lat_new_S = new((/num_distinct_markers,dimsizes(R_S)/),float,-999)
  249.   lon_new_S = new((/num_distinct_markers,dimsizes(R_S)/),float,-999)

  250. ;---Group the points according to which range they fall in.
  251.   do i = 0, num_distinct_markers-1
  252.     if (i.eq.0) then
  253.       indexes = ind(R_S.lt.arr(0))
  254.     end if
  255.     if (i.eq.num_distinct_markers-1) then
  256.       indexes = ind(R_S.ge.max(arr))
  257.     end if
  258.     if (i.gt.0.and.i.lt.num_distinct_markers-1) then      
  259.       indexes = ind(R_S.ge.arr(i-1).and.R_S.lt.arr(i))
  260.     end if
  261. ;
  262. ; Now that we have the set of indexes whose values fall within
  263. ; the given range, take the corresponding lat/lon values and store
  264. ; them, so later we can color this set of markers with the appropriate
  265. ; color.
  266. ;
  267.     if (.not.any(ismissing(indexes))) then
  268.       npts_range = dimsizes(indexes)   ; # of points in this range.
  269.       lat_new_S(i,0:npts_range-1) = lat_S(indexes)
  270.       lon_new_S(i,0:npts_range-1) = lon_S(indexes)
  271.     end if
  272.     delete(indexes)            ; Necessary b/c "indexes" may be a different
  273.                                ; size next time.
  274.   end do

  275. ;----------------------------------------------------------------------
  276. ; Begin plotting section.
  277. ;----------------------------------------------------------------------
  278.   wks = gsn_open_wks("ps","flood_S")     ; Open a workstation and

  279. ;---Set up some map resources.
  280.   mpres              = True
  281.   
  282.   
  283.   
  284.   mpres@gsnMaximize  = True             ; Maximize plot in frame.
  285.   mpres@gsnFrame     = False            ; Don't advance the frame
  286.   mpres@gsnDraw      = False            ; Don't advance the frame


  287.   mpres@tmXTBorderOn           = True     ; Don't draw top axis.
  288.   mpres@tmXTOn                 = True
  289.   mpres@tmXBMode             = "Explicit"  ; Set tick mark mode.
  290.   mpres@tmXBValues           = ispan(10940,11090,30)/100.0                                       
  291.   mpres@tmXBLabels           = ispan(10940,11090,30)/100.0                                       
  292.   mpres@tmYLMode             = "Explicit"  ; Set tick mark mode.
  293.   mpres@tmYLValues           = ispan(202,215,2)/10.0                                       
  294.   mpres@tmYLLabels           = ispan(202,215,2)/10.0
  295. ;---Zoom in on United States.
  296.   mpres@mpMinLatF            = 20.2
  297.   mpres@mpMaxLatF            = 21.5
  298.   mpres@mpMinLonF            = 109.40
  299.   mpres@mpMaxLonF            = 110.9
  300.   mpres@mpCenterLonF         = 180
  301.   mpres@mpDataBaseVersion   =  "HighRes"  ; High resolution database
  302.   mpres@mpDataResolution =        "Finest"
  303.   mpres@mpDataSetName    = "Earth..3" ;
  304.   mpres@mpFillColors = (/"transparent","transparent","gray","transparent"/)

  305.   mpres@tiMainString = "FloodPlain Scatters "
  306.   map = gsn_csm_map(wks,mpres)
  307.   
  308.   gsres               = True
  309.   gsres@gsMarkerIndex = 1          ; Use filled dots for markers.

  310. ;---Get nice spacing through color map for marker colors
  311.         gsn_define_colormap(wks,"Rainbow")
  312.   getvalues wks
  313.     "wkColorMapLen" : clen     ; number of colors in color map
  314.   end getvalues

  315.   nstep = (clen-2)/narr
  316.   colors = ispan(2,clen-1,nstep)

  317. ;---Loop through each "bin" and attach the markers to the map.
  318.   do i = 0, num_distinct_markers-1
  319.     if (.not.ismissing(lat_new_S(i,0)))
  320.       gsres@gsMarkerColor      = colors(i)
  321.       dumstr = unique_string("marker")
  322.       map@$dumstr$ = gsn_add_polymarker(wks,map,lon_new_S(i,:),lat_new_S(i,:),gsres)
  323.     end if
  324.   end do

  325. ;----------------------------------------------------------------------
  326. ; First version of this plot has a legend at the bottom
  327. ;----------------------------------------------------------------------
  328.   ;draw(map)                  ; Drawing the map will draw the markers
  329.   ;draw_legend(wks,lat_new,lon_new,arr,colors) ; Draw a legend at the bottom

  330.   ;frame(wks)    ; Advance the frame

  331. ;----------------------------------------------------------------------
  332. ; Second version of this plot has a labelbar added.
  333. ;----------------------------------------------------------------------
  334.   lbid = attach_labelbar(wks,map,arr,colors)   ; Attach a labelbar  

  335.   draw(map)     ; Drawing the map will draw everything   
  336.       
  337.   frame(wks)    ; Advance the frame.
  338. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Tide_Surge;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  339. ;---Set some needed arrays
  340.   arr = ispan(0,450,15)                                       
  341.   narr = dimsizes(arr)
  342.   labels = new(narr+1,string)   ; Labels for legend.
  343. nrow_TS = numAsciiRow("flood_TS.dat")
  344. ncol_TS = numAsciiCol("flood_TS.dat")
  345. print("flood_TS.dat")
  346. data_TS = asciiread("flood_TS.dat",(/nrow_TS,ncol_TS/),"float")   
  347. lat_TS = data_TS(:,1)
  348. lon_TS = data_TS(:,0)
  349. lat_TS@units= "degrees-north"
  350. lon_TS@units= "degrees-east"  
  351. R_TS = data_TS(:,2)*100
  352. npts_TS = nrow_TS                                    ; Number of points.
  353. ;
  354. ; Create X and Y arrays to hold the points for each range and initialize
  355. ; them to missing values.  We want to use num_distinct_markers
  356. ; different colors, so we need num_distinct_markers sets of X and
  357. ; Y points.
  358. ;
  359.   num_distinct_markers = dimsizes(arr)+1        ; number of distinct markers
  360.   lat_new_TS = new((/num_distinct_markers,dimsizes(R_TS)/),float,-999)
  361.   lon_new_TS = new((/num_distinct_markers,dimsizes(R_TS)/),float,-999)

  362. ;---Group the points according to which range they fall in.
  363.   do i = 0, num_distinct_markers-1
  364.     if (i.eq.0) then
  365.       indexes = ind(R_TS.lt.arr(0))
  366.     end if
  367.     if (i.eq.num_distinct_markers-1) then
  368.       indexes = ind(R_TS.ge.max(arr))
  369.     end if
  370.     if (i.gt.0.and.i.lt.num_distinct_markers-1) then      
  371.       indexes = ind(R_TS.ge.arr(i-1).and.R_TS.lt.arr(i))
  372.     end if
  373. ;
  374. ; Now that we have the set of indexes whose values fall within
  375. ; the given range, take the corresponding lat/lon values and store
  376. ; them, so later we can color this set of markers with the appropriate
  377. ; color.
  378. ;
  379.     if (.not.any(ismissing(indexes))) then
  380.       npts_range = dimsizes(indexes)   ; # of points in this range.
  381.       lat_new_TS(i,0:npts_range-1) = lat_TS(indexes)
  382.       lon_new_TS(i,0:npts_range-1) = lon_TS(indexes)
  383.     end if
  384.     delete(indexes)            ; Necessary b/c "indexes" may be a different
  385.                                ; size next time.
  386.   end do

  387. ;----------------------------------------------------------------------
  388. ; Begin plotting section.
  389. ;----------------------------------------------------------------------
  390.   wks = gsn_open_wks("ps","flood_TS")     ; Open a workstation and

  391. ;---Set up some map resources.
  392.   mpres              = True
  393.   
  394.   
  395.   
  396.   mpres@gsnMaximize  = True             ; Maximize plot in frame.
  397.   mpres@gsnFrame     = False            ; Don't advance the frame
  398.   mpres@gsnDraw      = False            ; Don't advance the frame


  399.   mpres@tmXTBorderOn           = True     ; Don't draw top axis.
  400.   mpres@tmXTOn                 = True
  401.   mpres@tmXBMode             = "Explicit"  ; Set tick mark mode.
  402.   mpres@tmXBValues           = ispan(10940,11090,30)/100.0                                       
  403.   mpres@tmXBLabels           = ispan(10940,11090,30)/100.0                                       
  404.   mpres@tmYLMode             = "Explicit"  ; Set tick mark mode.
  405.   mpres@tmYLValues           = ispan(202,215,2)/10.0                                       
  406.   mpres@tmYLLabels           = ispan(202,215,2)/10.0
  407. ;---Zoom in on United States.
  408.   mpres@mpMinLatF            = 20.2
  409.   mpres@mpMaxLatF            = 21.5
  410.   mpres@mpMinLonF            = 109.40
  411.   mpres@mpMaxLonF            = 110.9
  412.   mpres@mpCenterLonF         = 180
  413.   mpres@mpDataBaseVersion   =  "HighRes"  ; High resolution database
  414.   mpres@mpDataResolution =        "Finest"
  415.   mpres@mpDataSetName    = "Earth..3" ;
  416.   mpres@mpFillColors = (/"transparent","transparent","gray","transparent"/)

  417.   mpres@tiMainString = "FloodPlain Scatters "
  418.   map = gsn_csm_map(wks,mpres)
  419.   
  420.   gsres               = True
  421.   gsres@gsMarkerIndex = 1          ; Use filled dots for markers.

  422. ;---Get nice spacing through color map for marker colors
  423.         gsn_define_colormap(wks,"Rainbow")
  424.   getvalues wks
  425.     "wkColorMapLen" : clen     ; number of colors in color map
  426.   end getvalues

  427.   nstep = (clen-2)/narr
  428.   colors = ispan(2,clen-1,nstep)

  429. ;---Loop through each "bin" and attach the markers to the map.
  430.   do i = 0, num_distinct_markers-1
  431.     if (.not.ismissing(lat_new_TS(i,0)))
  432.       gsres@gsMarkerColor      = colors(i)
  433.       dumstr = unique_string("marker")
  434.       map@$dumstr$ = gsn_add_polymarker(wks,map,lon_new_TS(i,:),lat_new_TS(i,:),gsres)
  435.     end if
  436.   end do

  437. ;----------------------------------------------------------------------
  438. ; First version of this plot has a legend at the bottom
  439. ;----------------------------------------------------------------------
  440.   ;draw(map)                  ; Drawing the map will draw the markers
  441.   ;draw_legend(wks,lat_new,lon_new,arr,colors) ; Draw a legend at the bottom

  442.   ;frame(wks)    ; Advance the frame

  443. ;----------------------------------------------------------------------
  444. ; Second version of this plot has a labelbar added.
  445. ;----------------------------------------------------------------------
  446.   lbid = attach_labelbar(wks,map,arr,colors)   ; Attach a labelbar  

  447.   draw(map)     ; Drawing the map will draw everything   
  448.       
  449.   frame(wks)    ; Advance the frame.

  450. end     
复制代码
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-1-15 19:28:22 | 显示全部楼层

{:eb502:}{:eb502:}好高大上的感觉
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表