爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 8834|回复: 4

[经验总结] NCL计算小时平均浓度小结

[复制链接]

新浪微博达人勋

发表于 2016-7-25 22:46:59 | 显示全部楼层 |阅读模式

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

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

x
费了几天劲,终于灵光一现,写出了一个处理数据的简单脚本。
原始数据如下,可见是每分钟一个数,但经常有缺测,一缺缺几十分钟到几小时,很难用常规的方法来计算其小时平均浓度:
QQ图片20160725213017.png

Excel整理一下,删掉几列,再转为csv格式如下

QQ图片20160725213023.png
begin
  data =  asciiread("honohhmm.csv",-1,"string")
  data = str_sub_str(data,","," ")
  data = str_sub_str(data,":"," ")
  hono = stringtofloat(str_get_field(data,6," "))
  minute = stringtofloat(str_get_field(data,5," "))
  hh = stringtofloat(str_get_field(data,4," "))
  mm = stringtofloat(str_get_field(data,2," "))
  yy = stringtofloat(str_get_field(data,1," "))
  M = 1055
  N = 3  
   honoday=new((/M,N/),float)
   honoday(:,0) = hh(0:M-1)
   honoday(:,1) = minute(0:M-1)
   honoday(:,2) = hono(0:M-1)
   honohrmean=new(24,float)
   clock=new(24,float)
   honohr=new(M,float)
       在NCL中通过asciiread()函数读取文本文档为string后,将其中的“,”,“:” 都换成空格“ ”,并分别根据年月日时分及浓度属性,分割提取6列数据。定义一个新的数组,将小时及浓度等数据赋值过去,再定义一个一维数组用于接收随后的观测浓度数据,定义若干其他数组,用于存储最终结果。
       好戏来了,下面的这个循环实现了对落在某一小时区间的所有观测浓度求平均的功能。连用两次where,第一次where先找出落在某一小时内的所有数据,并将目标数据与其余数据取绝对值后强制取正负加以区分,再将已正负区分的数据赋值给honohr这个一维数组,第二次where通过判断这个一维数组中的数据是否>0,倘若符合,则保持不变,倘若不符合,则用无效值强制替换,再对这个一维数组进行求平均,无效值不参与计算,故轻松实现了每一小时观测浓度的平均。
   do it = 0,23,1
      a = it
      b = it+1
      honoday(:,2)=  where(honoday(:,0).ge.a .and. \     
                     honoday(:,0).lt.b ,abs(honoday(:,2)),-1*abs(honoday(:,2)))
      honohr = honoday(:,2)
      honohr = where(honohr.gt.0, honohr,honohr@_FillValue)
      honohrmean(it) = avg(honohr)
      clock(it) = it
    end do
  至此,计算工作已完成,再定义一个二维数组,将小时及对应的平均浓度写入,文件名为hr_mean,文件标题为cols:hh,HONO,共两列,每列占用十小格,浮点型,小数点后保留四位。
      print(honohrmean)
      y = new((/24,2/),float)
      y(:,0)=clock
      y(:,1)=honohrmean
      opt=True
      opt@row=True
      opt@title = "cols:hh,HONO"
      opt@fout ="hr_mean"
      fmtx="2f10.4"
      write_matrix(y,fmtx,opt)
end
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-7-26 08:41:43 | 显示全部楼层
感谢分享经验,
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-7-27 08:48:59 | 显示全部楼层
楼主辛苦,感谢分享。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-10-3 12:41:28 | 显示全部楼层
where是个好命令
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-10-16 15:29:46 | 显示全部楼层
您好,我用了您的程序改写了一下,有警告:
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average
warning:avg: Entire input array contains missing values, can't compute average

计算出的结果也是只有第一个时次是对的,其他都是错的
0.000 212.67741
1.000 9969209968386869046778552952102584320.00000
2.000 9969209968386869046778552952102584320.00000
3.000 9969209968386869046778552952102584320.00000
4.000 9969209968386869046778552952102584320.00000
5.000 9969209968386869046778552952102584320.00000
6.000 9969209968386869046778552952102584320.00000
7.000 9969209968386869046778552952102584320.00000
8.000 9969209968386869046778552952102584320.00000
9.000 9969209968386869046778552952102584320.00000
10.000 9969209968386869046778552952102584320.00000
11.000 9969209968386869046778552952102584320.00000
12.000 9969209968386869046778552952102584320.00000
13.000 9969209968386869046778552952102584320.00000
14.000 9969209968386869046778552952102584320.00000
15.000 9969209968386869046778552952102584320.00000
16.000 9969209968386869046778552952102584320.00000
17.000 9969209968386869046778552952102584320.00000

请问您遇到过这种情况吗?
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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