爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 7890|回复: 1

[经验总结] NCL舍入小数的一个bug

[复制链接]

新浪微博达人勋

发表于 2014-9-28 15:52:58 | 显示全部楼层 |阅读模式

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

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

x
  1. ;******************************************************
  2. ; check and modify if a 1-d array is succession
  3. ;******************************************************
  4. undef("check_succession")
  5. function check_succession(x[*]:integer, n[1]:integer)
  6. local number_x, index, number_0, i
  7. begin

  8.     number_x = dimsizes(x)
  9.     index    = ind(x .eq. 0)
  10.     number_0 = dimsizes(index)

  11.     do i = 0,number_0-2
  12.         if((index(i+1) - index(i)) .le. n) then
  13.             x(index(i):index(i+1)) = 0
  14.         end if
  15.     end do

  16. ; check 1 and end value
  17.    
  18.     if((x(0) .eq. 1) .and. (index(0) .lt. n)) then
  19.         x(0:index(0)) = 0
  20.     end if

  21.     if((x(number_x-1) .eq. 1) .and. ((number_x-1) - index(number_0-1)) .lt. n ) then
  22.         x(index(number_0-1):(number_x-1)) = 0
  23.     end if

  24.     return(x)
  25. end
  26. ;**********************************************************
  27. ; longitude index B1D for a single day
  28. ;**********************************************************
  29. undef("TM_OneDay")
  30. function TM_OneDay(x[*][*][*]:numeric, mid[1]:numeric, \
  31.                    ext[1]:numeric, dn[1]:numeric, dm[1]:numeric, \
  32.                    ds[1]:numeric, Emin[1]:numeric, Wmin[1]:numeric)
  33. local Zn, Zm, Zs, dims, ntime, nlon, xblock, GHGS, GHGN, i, j
  34. begin

  35.     Zn = (mid + 1.5*ext) + dn
  36.     Zm = (mid + 0.5*ext) + dm
  37.     Zs = (mid - 0.5*ext) + ds
  38.    
  39.     dims  = dimsizes(x)
  40.     ntime = dims(0)
  41.     nlon  = dims(2)
  42.    
  43.     xblock = new((/ntime, nlon/), integer)
  44.    
  45.     do i = 0, ntime-1
  46.         do j = 0, nlon-1
  47.            GHGN = (x(i,{Zn},j) - x(i,{Zm},j)) / (Zn - Zm)
  48.            GHGS = (x(i,{Zm},j) - x(i,{Zs},j)) / (Zm - Zs)
  49.             if (GHGS .gt. Emin .and. GHGN .lt. Wmin) then
  50.                 xblock(i,j) = 1
  51.             else
  52.                 xblock(i,j) = 0
  53.             end if
  54.         end do
  55.     end do

  56.     return(xblock)
  57. end
  58. ;*********************************************************
  59. ;*************************************************************
  60. load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
  61. load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
  62. load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
  63. begin

  64. ; parameters----

  65.     Minlon = 285
  66.     Maxlon = 345

  67.     B_days = 4
  68.     B_lons = 10

  69.     mid  = 60
  70.     ext  = 15
  71.     dn   = 0
  72.     dm   = 0
  73.     ds   = 0
  74.     Emin = 0
  75.     Wmin = -5

  76. ; read data------

  77.     f      = addfile("./Z500_daily_deg1_1979_2013_JJA.nc", "r")
  78.     geo    = short2flt(f->z(:,:,{Minlon:Maxlon}))
  79.     geo    = geo / 9.8
  80.     dims   = dimsizes(geo)
  81.     Zg     = onedtond(ndtooned(geo), (/35,92,dims(1),dims(2)/))

  82.     Zg!0    = "year"
  83.     Zg&year = ispan(1979, 2013, 1)
  84.     Zg!2    = "lat"
  85.     Zg&lat  = geo&latitude
  86.     Zg!3    = "lon"
  87.     Zg&lon  = geo&longitude
  88.    
  89. ; calculate oneday block

  90.     block = TM_OneDay(Zg({2012},:,:,:), mid, ext, dn, dm, ds, Emin, Wmin)
  91.     write_matrix(block, "61i1", False)

  92. ; check succession
  93.    
  94.     ndim  = dimsizes(block)
  95.     Ntime = ndim(0)
  96.     Nlons = ndim(1)

  97.     do i = 0, Nlons-1
  98.         block(:,i) = check_succession(block(:,i), B_days)
  99.     end do
  100.     write_matrix(block, "61i1", False)

  101.     do i = 0, Ntime-1
  102.         block(i,:) = check_succession(block(i,:), B_lons)
  103.     end do
  104.     write_matrix(block, "61i1", False)

  105. ;----------------------------
  106. end
复制代码

这里读入数据时候如果纬度反向geo    = short2flt(f->z(:,::-1,{Minlon:Maxlon}))运行结果会有很大差别,因为ext变量如果取奇数,存在舍入问题。

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

新浪微博达人勋

发表于 2014-9-28 16:40:10 来自手机 | 显示全部楼层
纬度反向和舍入有什么联系?bug 的话,用尽量简单的代码说明。在弄清你说的bug 前,可能已经被你的代码搞晕了。
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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