爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6436|回复: 3

[作图] 请教关于NCL_example中例子09的作图问题

[复制链接]
发表于 2016-3-14 19:32:14 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 王磊 于 2016-3-14 19:39 编辑

在ncl学习过程中,有幸得到了气象家园QQ群中各位学兄学姐的帮助,在此表示感谢。近期在重新稳固NCL的基础知识,在练习NCL_example手册的例子9的过程中遇到如下一个问题:案例09给与的是南半球高纬度的极区等值线图形,但按照example09的代码应当得到的是经纬度的平面图(代码见example09.ncl).考虑到给与的是极地图,所以修改如下两处
第一,修改出图函数为 map = gsn_csm_contour_map_polar(wks,icemonnew,resources) ,即sy.example09.ncl的第96行,同时由于该地图默认的为画北半球图,故修改出图resources@gsnPolar   = "SH"(第95行)。
第二,按照function的功能,实际上是要实现周期的精度,但是使用addcycle函数后,在作图过程中出现了“gsn_add_cyclic: Warning: The range of your longitude coordinate array is at least 360”这一提示,故考虑resources@gsnAddCyclic =False这一设定,尽管可以做出图形,但是显然数据存在裂口,同时  ,下述语句也没有生效。
setvalues map@contour ; Change the title for the contour plot.
    "tiMainString" : "CSM Y00-99 Mean Ice Fraction Month =" + month
  end setvalues



因而求助各位学兄学姐,应该做出如何修改,才能使得画出的图形是不存在断裂问题的。

example09.png
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2016-3-14 19:34:26 | 显示全部楼层
原始代码如下example09.ncl

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"


function addcyclic(data[*][*]:float)
;
; Add a cyclic point in "x" to a 2D array
; for a lat/lon plot "x" corresponds to "lon"
; "ny" corresponds to "nlat"
; "mx" corresponds to "mlon"

local dims, newdata, ny, mx, mx1

begin
dims = dimsizes(data)
ny = dims(0)
mx = dims(1)
mx1 = mx+1

newdata = new((/ny ,mx1/),float)

newdata(:,0:mx-1) = data ; pass everything
newdata(:,mx) = (/ data(:,0) /) ; value only

if((.not.ismissing(newdata!1)) .and. iscoord(data,newdata!1)) then
newdata&$newdata!1$(mx) = newdata&$newdata!1$(0) + 360.0
end if

return(newdata)
end

begin

file1 = ncargpath("data") + "/cdf/fice.nc"

ice1 = addfile(file1,"r")

fice = ice1->fice ; Read fice -- ice concentration
hlat = ice1->hlat
hlon = ice1->hlon

dimf = dimsizes(fice) ; Define an array to hold long-term monthly means.
ntime = dimf(0)
nhlat = dimf(1)
nhlon = dimf(2)

icemon = new ( (/nhlat,nhlon/) , float, -999.0)

icemon!0 = "hlat" ; Name dimensions 0 and 1
icemon!1 = "hlon" ; of icemon and create
icemon&hlat = hlat ; coordinate variables for both.
icemon&hlon = hlon

; Calculate the January (nmo=0) average.
nmo = 0
month = nmo+1
icemon = dim_avg(fice(hlat | :, hlon | :, time | nmo:ntime-1:12))
icemon = mask(icemon, icemon.eq.0., False) ; Set 0.0 to _FillValue.

nsub = 16 ; Subscript location of northernmost hlat to be plotted.

wks = gsn_open_wks("NCGM",get_script_prefix_name()) ; Open an NCGM.

cmap = (/(/1.00,1.00,1.00/), (/0.00,0.00,0.00/), (/1.00,1.00,0.50/), \
(/0.00,0.00,0.50/), (/0.50,1.00,1.00/), (/0.50,0.00,0.00/), \
(/1.00,0.00,1.00/), (/0.00,1.00,1.00/), (/1.00,1.00,0.00/), \
(/0.00,0.00,1.00/), (/0.00,1.00,0.00/), (/1.00,0.00,0.00/), \
(/0.50,0.00,1.00/), (/1.00,0.50,0.00/), (/0.00,0.50,1.00/), \
(/0.50,1.00,0.00/), (/0.50,0.00,0.50/), (/0.50,1.00,0.50/), \
(/1.00,0.50,1.00/), (/0.00,0.50,0.00/), (/0.50,0.50,1.00/), \
(/1.00,0.00,0.50/), (/0.50,0.50,0.00/), (/0.00,0.50,0.50/), \
(/1.00,0.50,0.50/), (/0.00,1.00,0.50/), (/0.50,0.50,0.50/), \
(/0.625,0.625,0.625/)/)

gsn_define_colormap(wks,cmap) ; Define a color map.



resources = True
;print(icemon(:,1))
;exit()

icemonnew = addcyclic(icemon(0:nsub,:))

resources@sfXArray = icemonnew&hlon ; Necessary for overlay on a map.
resources@sfYArray = icemonnew&hlat

;print(icemonnew&hlat)
;exit()
resources@mpMinLatF=-80
resources@mpMaxLatF=-0


resources@tiMainString = "CSM Y00-99 Mean Ice Fraction Month =" + month
resources@cnLevelSpacingF=0.3

map = gsn_contour_map(wks,icemonnew,resources) ; Draw a contour
; over a map.
nmos = 12
do nmo = 1,nmos-1
    month = nmo+1
    icemon = dim_avg(fice(hlat | :, hlon | :, time | nmo:ntime-1:12))
    icemon = mask(icemon, icemon.eq.0., False) ; set 0.0 to _FillValue

    setvalues map@contour ; Change the title for the contour plot.
        "tiMainString" : "CSM Y00-99 Mean Ice Fraction Month =" + month
    end setvalues

    print(month)

    setvalues map@data ; Change the data for the contour plot.
        "sfDataArray" : addcyclic(icemon(0:nsub,:))
end setvalues



draw(map) ; Draw the contour plot.
frame(wks) ; Advance the frame.
end do
delete(icemon) ; Clean up.
delete(icemonnew)
delete(map)
end
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2016-3-14 19:35:32 | 显示全部楼层
修改代码如下,sy.example09.ncl(修改部分为94-96行)
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"
   
    function addcyclic(data[*][*]:float)
   
     ; Add a cyclic point in "x" to a 2D array
    ; for a lat/lon plot "x" corresponds to "lon"
    ; "ny" corresponds to "nlat"
     ; "mx" corresponds to "mlon"
    local dims, newdata, ny, mx, mx1

    begin
    dims = dimsizes(data)
    ny = dims(0)
    mx = dims(1)
    mx1 = mx+1
   
    newdata = new((/ny ,mx1/),float)
   
    newdata(:,0:mx-1) = data ; pass everything
    newdata(:,mx) = (/ data(:,0) /) ; value only
   
    if((.not.ismissing(newdata!1)) .and. iscoord(data,newdata!1)) then
        newdata&$newdata!1$(mx) = newdata&$newdata!1$(0) + 360.0
    end if
   
    return(newdata)
    end
   
    begin
   
    file1 = ncargpath("data") + "/cdf/fice.nc"
   
    ice1 = addfile(file1,"r")
   
    fice = ice1->fice ; Read fice -- ice concentration
    hlat = ice1->hlat
    hlon = ice1->hlon
   
    dimf = dimsizes(fice) ; Define an array to hold long-term monthly means.
    ntime = dimf(0)
    nhlat = dimf(1)
    nhlon = dimf(2)
   
    icemon = new ( (/nhlat,nhlon/) , float, -999.0)
   
    icemon!0 = "hlat" ; Name dimensions 0 and 1
    icemon!1 = "hlon" ; of icemon and create
    icemon&hlat = hlat ; coordinate variables for both.
    icemon&hlon = hlon
   
    ; Calculate the January (nmo=0) average.
    nmo = 0
    month = nmo+1
    icemon = dim_avg(fice(hlat | :, hlon | :, time | nmo:ntime-1:12))
    icemon = mask(icemon, icemon.eq.0., False) ; Set 0.0 to _FillValue.
   
    nsub = 16 ; Subscript location of northernmost hlat to be plotted.
   
    wks = gsn_open_wks("NCGM",get_script_prefix_name()) ; Open an NCGM.
   
    cmap = (/(/1.00,1.00,1.00/), (/0.00,0.00,0.00/), (/1.00,1.00,0.50/), \
    (/0.00,0.00,0.50/), (/0.50,1.00,1.00/), (/0.50,0.00,0.00/), \
    (/1.00,0.00,1.00/), (/0.00,1.00,1.00/), (/1.00,1.00,0.00/), \
    (/0.00,0.00,1.00/), (/0.00,1.00,0.00/), (/1.00,0.00,0.00/), \
    (/0.50,0.00,1.00/), (/1.00,0.50,0.00/), (/0.00,0.50,1.00/), \
    (/0.50,1.00,0.00/), (/0.50,0.00,0.50/), (/0.50,1.00,0.50/), \
    (/1.00,0.50,1.00/), (/0.00,0.50,0.00/), (/0.50,0.50,1.00/), \
    (/1.00,0.00,0.50/), (/0.50,0.50,0.00/), (/0.00,0.50,0.50/), \
    (/1.00,0.50,0.50/), (/0.00,1.00,0.50/), (/0.50,0.50,0.50/), \
    (/0.625,0.625,0.625/)/)
   
    gsn_define_colormap(wks,cmap) ; Define a color map.
   
    resources = True
   
    icemonnew = addcyclic(icemon(0:nsub,:))
    ;printVarSummary(icemon(0:nsub,:))
    ;printVarSummary(icemonnew)
    ;print(icemon(0:nsub,:))
    ;exit
   




    resources@sfXArray = icemonnew&hlon ; Necessary for overlay on a map.
    resources@sfYArray = icemonnew&hlat
   
    resources@tiMainString = "CSM Y00-99 Mean Ice Fraction Month =" + month
   

    resources@gsnAddCyclic =False
    resources@gsnPolar   = "SH"
     map = gsn_csm_contour_map_polar(wks,icemonnew,resources) ; Draw a contour
    ; over a map.
   


    nmos = 12
    do nmo = 1,nmos-1
        month=nmo+1
    print(month)
    icemon = dim_avg(fice(hlat | :, hlon | :, time | nmo:ntime-1:12))
    icemon = mask(icemon, icemon.eq.0., False) ; set 0.0 to _FillValue
   
    setvalues map@contour ; Change the title for the contour plot.
    "tiMainString" : "CSM Y00-99 Mean Ice Fraction Month =" + month
    end setvalues
   
    setvalues map@data ; Change the data for the contour plot.
    "sfDataArray" : addcyclic(icemon(0:nsub,:))
    end setvalues

    draw(map) ; Draw the contour plot.
    frame(wks) ; Advance the frame.
    end do
    delete(icemon) ; Clean up.
    delete(icemonnew)
    delete(map)
end

密码修改失败请联系微信:mofangbao
发表于 2017-5-12 22:05:51 | 显示全部楼层

回帖奖励 +1 金钱

今天也碰到这个问题,把@gsnAddCyclic 改成True就好了
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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