爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 17751|回复: 12

[作图] 【已解决】关于南海的作图问题求助

[复制链接]

新浪微博达人勋

发表于 2019-12-6 19:23:32 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 不想去气象局 于 2019-12-8 22:58 编辑

用了兰溪大神的脚本,但在使用的时候因为我有三幅图的,不知道为啥出现了这样的情况然后我就把属性的复制稍微改了下,结果出现图一中海小图南岛的颜色和主图海南岛的颜色完全不一样,后面两幅图其实也有颜色深度上的区别,研究了好久捉摸不透, 请大神们赐教!!
我附上了全代码,但是我觉得的主要问题在代码里标了颜色,还是希望大家帮帮忙啦!!
当然也有可能问题不出在我以为的地方。。
eof.000001.png



; ==============================================================
; eof_1.ncl
;
; Concepts illustrated:
;   - Calculating EOFs
;   - Drawing a time series plot
;   - Using coordinate subscripting to read a specified geographical region
;   - Rearranging longitude data to span -180 to 180
;   - Calculating symmetric contour intervals
;   - Drawing filled bars above and below a given reference line
;   - Drawing subtitles at the top of a plot
;   - Reordering an array
; ==============================================================
; NCL V6.4.0 has new functions eofunc_n_Wrap and
; eofunc_ts_n_Wrap that allow you to calculate the EOFs without
; first having to first reorder the data. See eof_1_640.ncl.
; ==============================================================
; Calculate EOFs of the Sea Level Pressure over the North Atlantic.
; ==============================================================
; The slp.mon.mean file can be downloaded from:
; http://www.esrl.noaa.gov/psd/dat ... alysis.surface.html
; ==============================================================
; These files are loaded by default in NCL V6.2.0 and newer
; 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
; ==============================================================
; User defined parameters that specify region of globe and
; ==============================================================
  latS   =  15.
  latN   =  55.
  lonL   = 100.
  lonR   =  140.

  yStrt = 1961
  yLast = 2018

; season = "DJF"    ; choose Dec-Jan-Feb seasonal mean

  neof   = 3        ; number of EOFs
  optEOF = True      
  optEOF@jopt = 0   ; This is the default; most commonly used; no need to specify.
;optEOF@jopt = 1   ; **only** if the correlation EOF is desired

  optETS = False

; ==============================================================
; Open the file: Read only the user specified period
; ==============================================================
  f      = addfile ("3-mon.nc", "r")

  TIME   = f->time
;  YYYY   = cd_calendar(TIME,-1)/100                 ; entire file
;  iYYYY  = ind(YYYY.ge.yrStrt .and. YYYY.le.yrLast)

  spei = f->spei(2:686:12,:, :)
  printVarSummary(spei)                              ; variable overview

; compute desired global seasonal mean: month_to_season (contributed.ncl)
; ==============================================================
;  SLP    = month_to_season (slp, season)
;  nyrs   = dimsizes(SLP&time)
;  printVarSummary(SLP)

; =================================================================
; create weights:  sqrt(cos(lat))   [or sqrt(gw) ]
; =================================================================
  rad    = 4.*atan(1.)/180.
  clat   = f->lat           
  clat   = sqrt( cos(rad*clat) )                 ; gw for gaussian grid

; =================================================================
; weight all observations
; =================================================================
  wspei   = spei                                   ; copy meta data
  wspei   = spei*conform(spei, clat, 1)
; wspei@long_name = "Wgt: "+wspei@long_name

; =================================================================
; Reorder (lat,lon,time) the *weighted* input data
; Access the area of interest via coordinate subscripting
; =================================================================
  xw     = wspei({lat|latS:latN},{lon|lonL:lonR},time|:)
  x      = wspei(time|:,{lat|latS:latN},{lon|lonL:lonR})

  eof      = eofunc_Wrap(xw, neof, optEOF)      
  eof_ts   = eofunc_ts_Wrap (xw, eof, optETS)

  printVarSummary( eof )                         ; examine EOF variables
  printVarSummary( eof_ts )

; =================================================================
; Normalize time series: Sum spatial weights over the area of used
; =================================================================
  dimxw  = dimsizes( xw )
  mln    = dimxw(1)
  sumWgt = mln*sum( clat({lat|latS:latN}) )
  eof_ts = eof_ts/sumWgt


; =================================================================
; Extract the YYYYMM from the time coordinate
; associated with eof_ts [same as SLP&time]
; =================================================================
;yyyymm = cd_calendar(eof_ts&time,-2)/100  
;;yrfrac = yyyymm_to_yyyyfrac(yyyymm, 0.0); not used here

;============================================================
; PLOTS
;============================================================
  wks = gsn_open_wks("png","eof")         ; send graphics to PNG file
  plot = new(neof,graphic)                ; create graphic array
                                          ; only needed if paneling
; EOF patterns

  res                      = True         
  res@gsnDraw              = False        ; don't draw yet
  res@gsnFrame             = False        ; don't advance frame yet

  res@gsnAddCyclic         = False        ; plotted dataa are not cyclic

  res@mpFillOn             = False        ; turn off map fill
  res@mpMinLatF            = latS         ; zoom in on map
  res@mpMaxLatF            = latN
  res@mpMinLonF            = lonL
  res@mpMaxLonF            = lonR

  res@cnFillOn             = True         ; turn on color fill
  res@mpDataSetName              = "./database/Earth..4"
  res@mpDataBaseVersion          = "MediumRes" ; or "Ncarg4_1"
  res@mpAreaMaskingOn            = True
  res@mpMaskAreaSpecifiers       = (/"China"/)
  res@mpOutlineSpecifiers        = (/"China","China:Provinces"/)

  res@mpLandFillColor            = "white"
  res@mpInlandWaterFillColor     = "white"
  res@mpOceanFillColor           = "white"
  res@mpFillBoundarySets         = "NoBoundaries"
  res@mpOutlineBoundarySets      = "NoBoundaries"
  res@mpNationalLineColor        = "black"
  res@mpProvincialLineColor      = "black"
  res@mpGeophysicalLineColor     = "black"
  res@mpNationalLineThicknessF   = 2
  res@mpProvincialLineThicknessF = 1

  res@cnInfoLabelOn              =False

  res@cnFillOn                   = True
  res@cnFillDrawOrder            = "PreDraw"
  res@cnLinesOn            = False        ; True is default
  res@cnLineLabelsOn       = False        ; True is default
  res@cnFillPalette        = "BlWhRe"     ; set color map
  res@lbLabelBarOn         = False        ; turn off individual lb's

; res@cnLevelSelectionMode       = "ExplicitLevels"
; res@cnLevels                   = fspan(-0.04,0.04,9)
  res@lbLabelAutoStride          = True
  res@pmTickMarkDisplayMode      = "Always"

                                          ; set symmetric plot min/max
  symMinMaxPlt(eof, 16, False, res)       ; contributed.ncl

; panel plot only resources
  resP                     = True         ; modify the panel plot
  resP@gsnMaximize         = True         ; large format
  resP@gsnPanelLabelBar    = True         ; add common colorbar

; yStrt                    = yyyymm(0)/100
; yLast                    = yyyymm(nyrs-1)/100
  resP@gsnPanelMainString  = "SPEI "+": "+yStrt+"-"+yLast

;*******************************************
; first plot
;*******************************************
  do n=0,neof-1
     res@gsnLeftString  = "EOF "+(n+1)
     res@gsnRightString = sprintf("%5.1f", eof@pcvar(n)) +"%"
     plot(n)=gsn_csm_contour_map(wks,eof(n,:,:),res)

;--- add South China Sea ---
  nhres                          = True
  nhres@gsnFrame                 = False
  nhres@gsnDraw                  = False

  nhres@vpHeightF                = 0.18   
  nhres@vpWidthF                 = 0.18

  nhres@cnInfoLabelOn              =False
  nhres@mpMinLatF                =   2.0   
  nhres@mpMaxLatF                =  23.0
  nhres@mpMinLonF                = 105.0
  nhres@mpMaxLonF                = 123.0

  nhres@lbLabelBarOn             = False
  nhres@tmXBOn                   = False
  nhres@tmYLOn                   = False
  nhres@tmYROn                   = False
  nhres@tmXTOn                   = False
  nhres@gsnLeftString            = ""
  nhres@gsnRightString           = ""

   getvalues map1@contour
    "cnFillOn"                   : nhres@cnFillOn
    "cnLevelSelectionMode"       : nhres@cnLevelSelectionMode
    "cnLevels"                   : nhres@cnLevels
    "cnFillColors"               : nhres@cnFillColors
    "cnFillDrawOrder"            : nhres@cnFillDrawOrder
    "cnLinesOn"                  : nhres@cnLinesOn
    "cnLineLabelsOn"             : nhres@cnLineLabelsOn
  end getvalues
这是兰溪大神的脚本里的,但是我在使用的时候报错了,我就改成了下面红色的脚本
报错.png


;nhres@cnLevelSelectionMode      = res@cnLevelSelectionMode
;nhres@cnLevels                  = res@cnLevels   
nhres@lbLabelAutoStride         = res@lbLabelAutoStride
nhres@pmTickMarkDisplayMode     = res@pmTickMarkDisplayMode
nhres@cnFillPalette             = res@cnFillPalette   ; set color map
nhres@lbLabelBarOn              =res@lbLabelBarOn     ; turn off individual lb's
nhres@cnFillOn                  =res@cnFillOn
nhres@cnLevelSelectionMode      =res@cnLevelSelectionMode
nhres@cnLinesOn                 =res@cnLinesOn
nhres@cnLineLabelsOn            =res@cnLineLabelsOn
nhres@gsnAddCyclic              = False        ; plotted dataa are not cyclic

  map_nanhai = gsn_csm_contour_map(wks,eof(n,:,:),nhres)

  adres                          = True
  adres@amParallelPosF           = 0.495 ; -0.5 is the left edge of the plot.
  adres@amOrthogonalPosF         = 0.49  ; -0.5 is the top edge of the plot.
  adres@amJust                   = "BottomRight"
  plotnh = gsn_add_annotation(plot(n),map_nanhai,adres)

  end do
  gsn_panel(wks,plot,(/1,neof/),resP)     ; now draw as one plot

;*******************************************
; second plot
;*******************************************
; EOF time series  [bar form]

  rts           = True
  rts@gsnDraw   = False       ; don't draw yet
  rts@gsnFrame  = False       ; don't advance frame yet
  rts@gsnScale  = True        ; force text scaling               

; these four resources allow the user to stretch the plot size, and
; decide exactly where on the page to draw it.

  rts@vpHeightF = 0.40        ; Changes the aspect ratio
  rts@vpWidthF  = 0.85
  rts@vpXF      = 0.10        ; change start locations
  rts@vpYF      = 0.75        ; the plot


  rts@tiYAxisString = "SPEI"                    ; y-axis label      

  rts@gsnYRefLine           = 0.              ; reference line   
  rts@gsnXYBarChart         = True            ; create bar chart
  rts@gsnAboveYRefLineColor = "red"           ; above ref line fill red
  rts@gsnBelowYRefLineColor = "blue"          ; below ref line fill blue

; panel plot only resources
  rtsP                      = True            ; modify the panel plot
  rtsP@gsnMaximize          = True            ; large format
  rtsP@gsnPanelMainString   = "SPEI "+": "+yStrt+"-"+yLast


;year = yyyymm/100

; create individual plots
  do n=0,neof-1
     rts@gsnLeftString  = "EOF "+(n+1)
     rts@gsnRightString = sprintf("%5.1f", eof@pcvar(n)) +"%"
     plot(n) = gsn_csm_xy (wks,ispan(1961,2018,1),dim_standardize(eof_ts(n,:),1),rts)
  end do
  gsn_panel(wks,plot,(/neof,1/),rtsP)     ; now draw as one plot
end

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

新浪微博达人勋

 楼主| 发表于 2019-12-8 22:50:03 | 显示全部楼层
江奈何 发表于 2019-12-8 22:03
do i=0,1
  getvalues plot(i)  
     "mpDataSetName"              : nhres@mpDataSetName

根据你的代码加了
    "cnMaxLevelValF"             : nhres@cnMaxLevelValF
     "cnMinLevelValF"             : nhres@cnMinLevelValF
     "cnLevelSpacingF"            : nhres@cnLevelSpacingF      
问题解决了!
再次感谢!
密码修改失败请联系微信:mofangbao
回复 支持 1 反对 0

使用道具 举报

新浪微博达人勋

发表于 2019-12-7 09:33:39 | 显示全部楼层

回帖奖励 +5 金钱

getvalues map1@contour这个是为了让南海地图复制上大图的颜色,所以你没有这一步当然颜色会有偏差,他是map1,你这里就要改为plot1,2。do i=0,1
  getvalues plot(i)  
     "mpDataSetName"              : nhres@mpDataSetName
     "mpDataBaseVersion"          : nhres@mpDataBaseVersion
类似于这样,你再试试
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2019-12-7 22:57:18 | 显示全部楼层
江奈何 发表于 2019-12-7 09:33
getvalues map1@contour这个是为了让南海地图复制上大图的颜色,所以你没有这一步当然颜色会有偏差,他是m ...

不好意思我没写清楚,我是把代码改成getvalues plot(n)@contour 之后变成了我发的图上的报错,你在报错的图上可以看见我已经改成了plot(n),所以我也不清楚为什么不能改成getvalues plot(n)@contour。请问还可能是什么原因,谢谢你的热心回复!!!
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2019-12-8 10:25:32 | 显示全部楼层
不想去气象局 发表于 2019-12-7 22:57
不好意思我没写清楚,我是把代码改成getvalues plot(n)@contour 之后变成了我发的图上的报错,你在报错的 ...

不不不,你还少了一步。应该是
do i=0,2
  getvalues plot(i)  
     "mpDataSetName"              : nhres@mpDataSetName
     "mpDataBaseVersion"          : nhres@mpDataBaseVersion
     ....
end getvalues
   end do
然后再 getvalues plot@contour
     "cnFillOn"                   : nhres@cnFillOn
     "cnLevelSelectionMode"       : nhres@cnLevelSelectionMode
     end getvalues
不能直接 getvalues plot(i)@contour

   
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2019-12-8 15:12:07 | 显示全部楼层
江奈何 发表于 2019-12-8 10:25
不不不,你还少了一步。应该是
do i=0,2
  getvalues plot(i)  

您好,我还是没能理解您的意思,您是说我漏了循环吗
do i=0,2
  getvalues plot(i)  
     "mpDataSetName"              : nhres@mpDataSetName
     "mpDataBaseVersion"          : nhres@mpDataBaseVersion
     ....
end getvalues
   end do
上述这些我有,我把底下代码改成了getvalues plot@contour 之后左下角南海区域变成了整个图带底色,还是不对,您看还有别的办法吗
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2019-12-8 22:03:32 | 显示全部楼层
do i=0,1
  getvalues plot(i)  
     "mpDataSetName"              : nhres@mpDataSetName
     "mpDataBaseVersion"          : nhres@mpDataBaseVersion
     "mpFillOn"                   : nhres@mpFillOn
     "mpFillDrawOrder"            : nhres@mpFillDrawOrder
     "mpAreaMaskingOn"            : nhres@mpAreaMaskingOn
     "mpMaskAreaSpecifiers"       : nhres@mpMaskAreaSpecifiers
     "mpOutlineSpecifiers"        : nhres@mpOutlineSpecifiers
     ;"mpOutlineBoundarySets"      : nhres@mpOutlineBoundarySets
     "mpLandFillColor"            : nhres@mpLandFillColor      
     "mpOceanFillColor"           : nhres@mpOceanFillColor      
     "mpInlandWaterFillColor"     : nhres@mpInlandWaterFillColor
     "mpNationalLineColor"        : nhres@mpNationalLineColor   
     "mpProvincialLineColor"      : nhres@mpProvincialLineColor      
   end getvalues
end do
   getvalues plot@contour
     "cnFillOn"                   : nhres@cnFillOn
     "cnLevelSelectionMode"       : nhres@cnLevelSelectionMode
     "cnLevels"                   : nhres@cnLevels
     "cnFillColors"               : nhres@cnFillColors
     "cnFillDrawOrder"            : nhres@cnFillDrawOrder
     "cnLinesOn"                  : nhres@cnLinesOn
     ;"cnLineLabelsOn"             : nhres@cnLineLabelsOn
     "cnMaxLevelValF"             : nhres@cnMaxLevelValF
     "cnMinLevelValF"             : nhres@cnMinLevelValF
     "cnLevelSpacingF"            : nhres@cnLevelSpacingF      
   end getvalues
  ;*********************************************************************
  map_nanhai(0) = gsn_csm_contour_map(wks,datat(:,:),nhres)
  map_nanhai(1) = gsn_csm_contour_map(wks,datap(:,:),nhres)
  cnres                  = True      
  cnres@gsLineThicknessF = 1.0      
  cnres@gsLineColor      = "black"

  cnres@minlon           = 105.0
  cnres@maxlon           = 123.0
  cnres@minlat           = 2.0
  cnres@maxlat           = 23.0
  do i=0,1
  plotsouth(i) = gsn_add_shapefile_polylines(wks,map_nanhai(i), "/mnt/d/data/cnmap/cnmap.shp",cnres)   
  end do   
  adres                  = True
;**********************************************************************
  adres@amParallelPosF   = 0.495    ; -0.5 is the left edge of the plot.
  adres@amOrthogonalPosF = 0.49   ; -0.5 is the top edge of the plot.
  adres@amJust           = "BottomRight"
  do i=0,1
plotnh(i) = gsn_add_annotation(plot(i),map_nanhai(i),adres)
delete(res@gsnLeftString)
end do
这是我的,供你参考
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2020-3-12 12:43:19 | 显示全部楼层

回帖奖励 +5 金钱

感谢,学习一下
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2020-3-29 19:04:15 | 显示全部楼层

回帖奖励 +5 金钱

{:5_275:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2020-4-15 22:24:11 | 显示全部楼层

回帖奖励 +5 金钱

你好,想问一下为什么我参考了你的代码但是只有中国大陆的边界,没有南海的九段线还有小岛呀
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

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