爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 17087|回复: 18

[作图] NCL绘制斜垂直剖面和grads出图效果相差很大

[复制链接]

新浪微博达人勋

发表于 2018-10-26 16:43:44 | 显示全部楼层 |阅读模式

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

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

x
分别利用ncl和grads对同一个斜线作垂直剖面

出来的图效果感觉ncl的画的不对,请各位大神指点。


----------------------------------
NCL出图效果

ncl1

ncl1

ncl2

ncl2


----------------------------------
grads出图效果

08.Grads.AB.png 08.Grads.png


---------------------------------------------------------------------------------------------------------------------------------------
ncl绘图脚

  1. ;   Example script to produce plots for a WRF real-data run,
  2. ;   with the ARW coordinate dynamics option.
  3. ;   Plot data on a cross section
  4. ;   This script will plot data at a set angle through a specified point
  5. ;   This script adds lon/lat info along X-axis

  6. load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
  7. load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"

  8. begin
  9. ;
  10. ; The WRF ARW input file.  
  11. ; This needs to have a ".nc" appended, so just do it.
  12.   a = addfile("./wrfout_d03_2016-05-05_08_00_00.nc","r")


  13. ; We generate plots, but what kind do we prefer?
  14. ; type = "x11"
  15. ; type = "pdf"
  16. ; type = "ps"
  17. ; type = "ncgm"
  18. type = "png"
  19. wks = gsn_open_wks(type,"plt_UW_mdBz_08.ncl")
  20. ;gsn_define_colormap(wks,"WhViBlGrYeOrReWh")       ; Overwrite the standard color map
  21. ;gsn_define_colormap(wks, "Radar2")  ;加载自己定义的色标
  22. gsn_define_colormap(wks, "Radar.mdbz")  ;加载自己定义的色标

  23. ; Set some basic resources
  24.   res = True
  25.   res@MainTitle = "REAL-TIME WRF"
  26.   res@Footer = False


  27.   pltres = True
  28.   mpres = True
  29.   mpres@mpGeophysicalLineColor = "Black"
  30.   mpres@mpNationalLineColor    = "Black"
  31.   mpres@mpUSStateLineColor     = "Black"
  32.   mpres@mpGridLineColor        = "Black"
  33.   mpres@mpLimbLineColor        = "Black"
  34.   mpres@mpPerimLineColor       = "Black"


  35.   mpres@mpDataSetName               = "Earth..4"
  36.   mpres@mpDataBaseVersion           = "MediumRes"
  37.   mpres@mpOutlineOn                 = True
  38.   mpres@mpOutlineSpecifiers         = (/"China:states","Taiwan","Zhejiang"/)
  39.   mpres@mpGeophysicalLineThicknessF = 2
  40.   mpres@mpNationalLineThicknessF    = 2
  41.   mpres@mpNationalLineColor         = "black"
  42.   mpres@mpGeophysicalLineColor      = "black"
  43.   mpres@mpProvincialLineColor       = "black"
  44.   mpres@mpProvincialLineThicknessF  = 2

  45.   mpres@mpFillOn                    = True
  46.   mpres@mpFillAreaSpecifiers        = (/"water",       "land" /)
  47.   mpres@mpSpecifiedFillColors       = (/"skyblue1","white"/)
  48.   mpres@mpMaskAreaSpecifiers        = (/"China:states","Taiwan","Zhejiang"/)


  49. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  50.   FirstTime = True
  51.   FirstTimeMap = True
  52.   times  = wrf_user_getvar(a,"times",-1) ; get times in the file
  53.   ntimes = dimsizes(times)         ; number of times in the file

  54.   mdims = getfilevardimsizes(a,"P") ; get some dimension sizes for the file
  55.   dNames = getfilevardims(a,"P")    ; NCL 6.4.0 and earlier
  56.   nd = dimsizes(mdims)              ; 查看mdims为几围数组
  57.   print(dNames + " : " + mdims)     ; 查看各个变量维数

  58.   xlat = wrf_user_getvar(a, "XLAT",0)
  59.   xlon = wrf_user_getvar(a, "XLONG",0)
  60.   ter = wrf_user_getvar(a, "HGT",0)

  61.   printVarSummary(xlat)



  62. ;---------------------------------------------------------------

  63.   do it = 0,ntimes-1             ; TIME LOOP

  64.     print("Working on time: " + times(it) )
  65.     res@TimeLabel = times(it)   ; Set Valid time to use on plots

  66.    
  67.     z    = wrf_user_getvar(a, "z",it)      ; grid point height
  68.     dbz = wrf_user_getvar(a,"dbz",it)    ;dBz
  69.     mdbz = wrf_user_getvar(a,"mdbz",it)    ;dBz
  70.     printVarSummary(dbz)

  71.     u = wrf_user_getvar(a,"ua",it)
  72.     v = wrf_user_getvar(a,"va",it)
  73.     w = wrf_user_getvar(a,"wa",it)

  74.     if ( FirstTime ) then                ; get height info for labels
  75.       zmin = 0.
  76.       zmax = 10.                          ; We are only interested in the first 6km
  77.       nz   = floattoint(zmax + 1)
  78.     end if


  79. ;---------------------------------------------------------------

  80.     do ip = 1, 1,1        ; we are doing 3 plots;我们这里只绘制2张图
  81.             ; all with the pivot point (plane) in the center of the domain
  82.             ; at angles 0, 45 and 90
  83. ;  
  84. ;                   |
  85. ;       angle=0 is  |          ;垂直剖面直线AB的形状
  86. ;                   |
  87. ;

  88.         ;线段AB的起点和终点经纬度设置

  89.       

  90.         ;08 UTC
  91.         startlon = 119.2
  92.         endlon   = 120.0
  93.         startlat = 28.8
  94.         endlat   = 29.6

  95.         PI=3.1415926
  96.         ;--------------------------------------------------------------------------
  97.         ;经纬度转为网格点后对应的最小最大值
  98.         ;--------------------------------------------------------------------------
  99.         ; Xstart = xy(0,0)         ;经度最小
  100.         ; Xend   = xy(0,1)         ;经度最大
  101.         ; Ystart = xy(1,0)         ;纬度最小
  102.         ; Yend   = xy(1,1)         ;纬度最大

  103.    
  104.         xy = wrf_user_ll_to_ij(a,(/startlon,endlon/),(/startlat,endlat/),res)  ;AB经纬度转换为坐标点
  105.         xy = xy-1  ;转换为NCL下标
  106.         ;plot AB垂直剖面的位置
  107.         ; xlat2d=xlat(xy(1,0):xy(1,1),xy(0,0):xy(0,1))
  108.         ; xlon2d=xlon(xy(1,0):xy(1,1),xy(0,0):xy(0,1))
  109.         ; dimsX=dimsizes(xlat2d)
  110.         ; dimsY=dimsizes(xlon2d)
  111.         ; print(dimsX)

  112.         ; pointX = xy(0,0)+dimsX(1)/2.
  113.         ; pointY = xy(1,0)+dimsX(0)/2.
  114.         
  115.         plane = new(4,float)                 ; a new array
  116.         plane  = (/ xy(0,0),xy(1,1),xy(0,1),xy(1,0)/)       ;start x;y & end x;y point
  117.         alfa   = atan2((startlat-endlat),(endlon-startlon))         ;计算线段AB与x轴方向的夹角
  118.         angle  = 90-alfa*180/PI
  119.         uv     = u*cos(alfa)+v*sin(alfa)                                 ;由于atan2函数正北为0度,因此线段AB与x轴方向的夹角angle和atan2函数出来的alfa之间要换算过
  120.         print("angle of Line AB with X in plot is " +angle+" Degree")

  121.    
  122.       
  123.         opts = False                                   ; start and end point not supplied
  124.         ;--------------------------------------------------------------------------
  125.         ;由于atan2函数正北为0度,因此线段AB的夹角angle和atan2函数出来的alfa之间要换算过
  126.         ;--------------------------------------------------------------------------
  127.         if(ip .eq. 1) then
  128.           ;angle = 90.
  129.           X_plane = wrf_user_intrp2d(xlon,plane,angle,opts) ;This function interpolates 2D model data onto a specified line.
  130.           X_desc = "longitude"
  131.         end if
  132.         if(ip .eq. 2) then
  133.           ;angle = 0.
  134.           X_plane = wrf_user_intrp2d(xlat,plane,angle,opts)
  135.           X_desc = "latitude"
  136.         end if
  137.         if(ip .eq. 3) then
  138.           ;angle = 45.
  139.           X_plane = wrf_user_intrp2d(xlon,plane,angle,opts)
  140.           X_desc = "longitude"
  141.         end if

  142.         ;把数据插值到各个高度上
  143.         dbz_plane = wrf_user_intrp3d(dbz,z,"v",plane,angle,opts)
  144.         u_plane = wrf_user_intrp3d(uv,z,"v",plane,angle,opts)
  145.         w_plane = wrf_user_intrp3d(w,z,"v",plane,angle,opts)
  146.         ter_plane = wrf_user_intrp2d(ter,plane,angle,opts)
  147.         mdbz_plane = wrf_user_intrp2d(mdbz,plane,angle,opts)

  148.         print("Max terrain height in plot is " + max(ter_plane)+"m")

  149.    

  150.       ; Find the index where 6km is - only need to do this once
  151.         if ( FirstTime ) then
  152.           zz = wrf_user_intrp3d(z,z,"v",plane,angle,opts)
  153.           b = ind(zz(:,0) .gt. zmax*1000. )
  154.           zmax_pos = b(0) - 1
  155.           if ( abs(zz(zmax_pos,0)-zmax*1000.) .lt. abs(zz(zmax_pos+1,0)-zmax*1000.) ) then
  156.             zspan = b(0) - 1
  157.           else
  158.             zspan = b(0)
  159.           end if
  160.           delete(zz)
  161.           delete(b)
  162.           FirstTime = False
  163.         end if

  164.         ; X-axis lables
  165.         dimsX = dimsizes(X_plane)
  166.         xmin  = X_plane(0)
  167.         xmax  = X_plane(dimsX(0)-1)
  168.         xspan = dimsX(0)-1
  169.         nx    = floattoint( (xmax-xmin)/2 + 1)
  170.         ;nx    = 10
  171.         ;print("xspan and nx in plot is " + xmin+" and "+xmax)
  172.         print("xspan and nx in plot is " + xspan+" and "+nx)
  173.         ;---------------------------------------------------------------
  174.         
  175.         ; Options for XY Plots
  176.         opts_xy                         = res
  177.         opts_xy@tiXAxisString           = X_desc
  178.         opts_xy@tiYAxisString           = "Height (km)"
  179.         opts_xy@cnMissingValPerimOn     = True
  180.         opts_xy@cnMissingValFillColor   = 0
  181.         opts_xy@cnMissingValFillPattern = 11
  182.         opts_xy@tmXTOn                  = False
  183.         opts_xy@tmYROn                  = False
  184.         opts_xy@tmXBMode                = "Explicit"

  185.       
  186.         ;;-------------------想要绘制的数据的经纬度范围-------------------------------------------------
  187.         minlat =  startlat
  188.         maxlat =  endlat
  189.         minlon = startlon
  190.         maxlon = endlon
  191.         loc = wrf_user_ll_to_ij(a,(/minlon,maxlon/),(/minlat,maxlat/),res)  ;查找目标经纬度对应的格点
  192.         loc = loc-1   ;转换为NCL下标
  193.         ; Xstart = loc(0,0)         ;经度最小
  194.         ; Xend   = loc(0,1)         ;经度最大
  195.         ; Ystart = loc(1,0)         ;纬度最小
  196.         ; Yend   = loc(1,1)         ;纬度最大
  197.         xspan = abs(loc(0,1)-loc(0,0))
  198.         xmin  = minlon
  199.         xmax  = maxlon
  200.         nx    = 10
  201.         ;;-----------------------------------------------------------------------------------------------



  202.         opts_xy@tmXBMode                = "Explicit"
  203.         opts_xy@tmXBValues              = fspan(0,xspan,10)                    ; Create tick marks
  204.         opts_xy@tmXBLabels              = sprintf("%.2f",fspan(xmin,xmax,nx))  ; Create labels
  205.         ;opts_xy@tmXBLabels              = (/"29.60N,119.20E","29.40N,119.40E","29.20N,119.60E","29.00N,119.80E","28.80N,120.00E"/)  ; Create labels
  206.         opts_xy@tmXBLabelFontHeightF    = 0.010
  207.         opts_xy@tmYLMode                = "Explicit"
  208.         opts_xy@tmYLValues              = fspan(0,zspan,nz)                    ; Create tick marks
  209.         opts_xy@tmYLLabels              = sprintf("%.1f",fspan(zmin,zmax,nz))  ; Create labels
  210.         opts_xy@tiXAxisFontHeightF      = 0.020
  211.         opts_xy@tiYAxisFontHeightF      = 0.020
  212.         opts_xy@tmXBMajorLengthF        = 0.02
  213.         opts_xy@tmYLMajorLengthF        = 0.02
  214.         opts_xy@tmYLLabelFontHeightF    = 0.015


  215.         ; Plotting options for dBz
  216.         opts_dbz = opts_xy                        
  217.         opts_dbz@cnFillOn = True  
  218.         ;opts_dbz@ContourParameters = (/ 5., 65., 5./)
  219.         opts_dbz@cnLevelSelectionMode= "ManualLevels"
  220.         opts_dbz@cnMinLevelValF      = 15.
  221.         opts_dbz@cnMaxLevelValF      = 65.
  222.         opts_dbz@cnLevelSpacingF     = 1.0   ;色标间隔  


  223.         ; Plotting options for mdBz
  224.         opts_mdbz = opts_xy                        
  225.         opts_mdbz@cnFillOn = True  
  226.         ;opts_mdbz@ContourParameters = (/ 5., 65., 5./)
  227.         opts_mdbz@cnLevelSelectionMode= "ManualLevels"
  228.         opts_mdbz@cnMinLevelValF      = 15.
  229.         opts_mdbz@cnMaxLevelValF      = 65.
  230.         opts_mdbz@cnLevelSpacingF     = 1.0   ;色标间隔  


  231.         ; Plotting options for terrain
  232.         opts_ter = opts_xy
  233.         opts_ter@gsnYRefLine = 0.0
  234.         opts_ter@gsnAboveYRefLineColor = "black"
  235.         opts_ter@gsnDraw = False
  236.         opts_ter@gsnFrame = False
  237.         opts_ter@trYMaxF = zmax*1000 ;地形数据为m,因此这里要放大倍数为km



  238.         vcres = opts_xy
  239.         ;vcres@vcGlyphStyle       = "WindBarb"
  240.         vcres@vcGlyphStyle       = "LineArrow";"CurlyVector"
  241.         vcres@vcPositionMode     = "ArrowTail"
  242.         vcres@vcRefAnnoOn        = True
  243.         vcres@vcMinDistanceF     = 0.03
  244.         vcres@vcRefMagnitudeF    = 8.   ;单位长度
  245.         vcres@vcLineArrowThicknessF = 3.0
  246.         vcres@vcMapDirection        = False
  247.         vcres@vcFillArrowsOn     = True
  248.         vcres@vcRefLengthF       = 0.02
  249.         vcres@vcFillArrowHeadXF  = 0.2
  250.         vcres@vcFillArrowHeadYF  = 0.2
  251.         vcres@vcFillArrowHeadInteriorXF = 0.15

  252.         
  253.         ;;u ,v for streamline
  254.         uvres                    = opts_xy                     ; plot mods desired
  255.         uvres@tiMainString       = "Streamlines"            ; title
  256.         uvres@stArrowLengthF     = 0.004                    ; size of the arrows.
  257.         uvres@stMinArrowSpacingF = 0.004                    ; arrow spacing.
  258.         uvres@stArrowStride      = 1                        ; arrows start every third
  259.         uvres@stArrowLengthF     = 0.008                ; default is dynamic
  260.         uvres@stLengthCheckCount = 15                   ; default is 35
  261.         uvres@stLineStartStride  = 3.5                    ; default is 2 流线的密度
  262.         uvres@stMinArrowSpacingF = 0.035                ; default is 0.0            
  263.         uvres@stStepSizeF        = 0.001                ; default is dynamic
  264.         uvres@stLineThicknessF   = 2.0           ; changes the line thickness



  265.         
  266.         
  267.         ; Get the contour info for the rh and temp
  268.         contour_dbz = wrf_contour(a,wks,dbz_plane(0:zmax_pos,xy(0,0):xy(0,1)),opts_dbz)  ;;loc(0,0):loc(0,1)为想要绘制的经度范围
  269.         contour_ter = gsn_csm_xy(wks,X_plane,ter_plane,opts_ter)
  270.         vector = gsn_streamline(wks,u_plane(0:zmax_pos,xy(0,0):xy(0,1)),w_plane(0:zmax_pos,xy(0,0):xy(0,1))*10.,uvres)
  271.         
  272.   ; MAKE PLOTS         

  273.         if (FirstTimeMap) then

  274.           lat_plane = wrf_user_intrp2d(xlat,plane,angle,opts)
  275.           lon_plane = wrf_user_intrp2d(xlon,plane,angle,opts)
  276.          
  277.           pltres = True
  278.           pltres@FramePlot = False
  279.           ;mpres = True

  280.           optsM = res
  281.           optsM@NoHeaderFooter = True
  282.           optsM@cnFillOn = True
  283.           optsM@lbTitleOn = False
  284.           optsM@cnLevelSelectionMode= "ManualLevels"
  285.           optsM@cnMinLevelValF      = 15.
  286.           optsM@cnMaxLevelValF      = 65.
  287.           optsM@cnLevelSpacingF     = 1.0   ;色标间隔  


  288.           ;设置mdbz绘图区域
  289.           minlat =  27.
  290.           maxlat =  31.5
  291.           minlon = 118.0
  292.           maxlon = 122.5
  293.           loc = wrf_user_ll_to_ij(a,(/minlon,maxlon/),(/minlat,maxlat/),res)  ;查找目标经纬度对应的格点
  294.           contour  = wrf_contour(a,wks,mdbz(loc(1,0):loc(1,1),loc(0,0):loc(0,1)),optsM)

  295.           ;设置绘图区域
  296.           mpres@mpLeftCornerLatF  = minlat
  297.           mpres@mpRightCornerLatF = maxlat
  298.           mpres@mpLeftCornerLonF  = minlon
  299.           mpres@mpRightCornerLonF = maxlon
  300.           plot = wrf_map_overlays(a,wks,(/contour/),pltres,mpres)

  301.           ;绘制垂直剖面的直线AB
  302.           lnres = True
  303.           lnres@gsLineThicknessF = 3.0
  304.           lnres@gsLineColor = "Black"
  305.           do ii = 0,dimsX(0)-2
  306.             gsn_polyline(wks,plot,(/lon_plane(ii),lon_plane(ii+1)/),(/lat_plane(ii),lat_plane(ii+1)/),lnres)
  307.           end do

  308.           frame(wks)
  309.           delete(lon_plane)
  310.           delete(lat_plane)
  311.           pltres@FramePlot = True
  312.        end if
  313.         
  314.         plot = wrf_overlays(a,wks,(/contour_dbz,vector,contour_ter/),mpres)    ; plot x-section

  315.   ; Delete options and fields, so we don't have carry over
  316.         delete(opts_xy)
  317.         delete(opts_dbz)
  318.         delete(vcres)
  319.         delete(X_plane)
  320.         delete(ter_plane)
  321.         delete(mdbz_plane)
  322.       

  323.     end do  ; make next cross section

  324. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  325.     FirstTimeMap = False
  326.   end do        ; END OF TIME LOOP

  327. end
复制代码



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

新浪微博达人勋

发表于 2018-10-26 19:14:50 | 显示全部楼层
二者地形都不一样,最好检查下输入数据~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-10-26 19:32:38 | 显示全部楼层
我估计是136/149行的问题。136行你写的点和你预剖的点不一样,改这样应该可以xy = wrf_user_ll_to_ij(a,(/startlon,endlon/),(/endlat,startlat/),res) ,。你可以事先先找找(119.2,29.6)和(120,28.8)这两个点所对应的的x,y格点是几。或者你可以直接沿大值中心那个点做斜剖
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-11-1 10:43:26 | 显示全部楼层
Wetter 发表于 2018-10-26 19:14
二者地形都不一样,最好检查下输入数据~

ncl是用wrfout的,grads用的是wrfout转成的数据,数据源应该是一样的
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-11-1 11:57:26 | 显示全部楼层
1649518749 发表于 2018-10-26 19:32
我估计是136/149行的问题。136行你写的点和你预剖的点不一样,改这样应该可以xy = wrf_user_ll_to_ij(a,(/s ...

应该不影响的,因为我下面设置palne的时候按照实际排列了坐标点
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-11-3 21:19:14 | 显示全部楼层
1649518749 发表于 2018-10-26 19:32
我估计是136/149行的问题。136行你写的点和你预剖的点不一样,改这样应该可以xy = wrf_user_ll_to_ij(a,(/s ...

找到原因了,在插值的时候,如果给定了线段AB,则opts要设置为True
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-11-3 21:26:18 | 显示全部楼层
Wetter 发表于 2018-10-26 19:14
二者地形都不一样,最好检查下输入数据~

找到原因了,在插值的时候,如果给定了线段AB,则opts要设置为True
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2019-4-19 17:34:25 | 显示全部楼层
多谢楼主分享
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2019-4-25 16:33:30 | 显示全部楼层
小其其格 发表于 2018-11-3 21:26
找到原因了,在插值的时候,如果给定了线段AB,则opts要设置为True

多谢楼主!刚遇到这个问题
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2019-5-14 16:40:31 | 显示全部楼层
楼主,方便发一下grads脚本吗
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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