爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 7696|回复: 9

MeteoInfoLab脚本示例:gif动画图

[复制链接]

新浪微博达人勋

发表于 2015-11-27 16:33:51 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 MeteoInfo 于 2015-12-2 12:39 编辑

在MeteoInfoLab脚本中生成gif动画图要用gifanimation()方法创建一个gif动画:

    gifanimation(filename, repeat=0, delay=1000)

方法中第一个参数是gif动画图片的文件名,后面两个参数是可选的,repeat是动画的重复次数,0指的无限次重复;delay指的动画帧之间的时间间隔,单位是毫秒,delay=1000及时间间隔为1秒。


然后在脚本的循环中给gif动画添加帧,用gifaddframe()方法,方法中的参数为之前创建的gif动画变量。

最后用gif动画变量(此例中为animation)的finish()方法结束动画创建过程,同时会完成gif动画图片的写入。

这个示例脚本中的内容很丰富,演示了CUACE-Haze/Fog模式预报的能见度和站点观测的雾-霾天气符号的叠加,从而直观的判断模式的预报效果。

脚本程序:
  1. #Set data folders
  2. fcstdir = 'W:/CUACE_out/haze_54'
  3. obsdir = 'U:/data/micaps'
  4. mapdir = 'T:/verification/map'
  5. outdir = 'T:/verification/cases/cuace_54km/result'

  6. #Set time
  7. st = datetime.datetime(2015,11,1,0)
  8. sbjt = st + datetime.timedelta(hours=8)

  9. #Get forecasting data files
  10. fcstfn = os.path.join(fcstdir, str(st.year) + '/' + st.strftime('%Y%m') + '/' + \
  11.     st.strftime('%Y%m%d%H') + '/produce/haze_post_aero_' + st.strftime('%Y%m%d%H') + '.ctl')
  12. print 'Forecasting data file: ' + fcstfn
  13. fcstf = addfile(fcstfn)
  14. fcstf.bigendian(True)

  15. #Plot
  16. figure(figsize=[768,480], newfig=False)
  17. axesm(position=[0,0,1,1], xyscale=1.2, tickfontsize=12)
  18. lworld = shaperead(os.path.join(mapdir, 'country1.shp'))
  19. lchina = shaperead(os.path.join(mapdir, 'bou2_4p.shp'))
  20. geoshow(lchina)
  21. geoshow(lworld, edgecolor='k')
  22. levs = [0.1, 0.5, 1, 5, 10, 20, 30, 50]
  23. cols = [(192,0,0),(255,69,0),(255,105,180),(255,128,0),(255,192,128),(51,255,255), \
  24.     (153,255,153),(204,255,204),(255,255,255)]
  25. #Set weather list - haze, mist and fog
  26. weathers = [5,10,11,12,40,41,42,43,44,45,46,47,48,49]
  27. ls = weatherspec(weathers)
  28. #Add south China Sea
  29. lscs = shaperead(os.path.join(mapdir, 'bou1_4l.shp'))
  30. axesm(position=[0.76,0.09,0.16,0.22], axison=False, xyscale=1.2)
  31. geoshow(lscs, facecolor=(0,0,255))
  32. xlim(106, 123)
  33. ylim(2, 23)
  34. #Set current plot to 1
  35. currentplot(1)

  36. #Create gif animation
  37. giffn = os.path.join(outdir, 'V_vis_' + st.strftime('%Y%m%d') + '--loop-.gif')
  38. print giffn
  39. animation = gifanimation(giffn)

  40. #Loop
  41. tnum = fcstf.timenum()
  42. nn = 0
  43. for t in range(1, 25):
  44.     if nn > 0:
  45.         cll()
  46.         cll()
  47.     tt = fcstf.gettime(t)
  48.     bjt = tt + datetime.timedelta(hours=8)
  49.     obsfn = os.path.join(obsdir,  str(bjt.year) + '/plot/' + bjt.strftime('%y%m%d%H.000'))
  50.     if os.path.exists(obsfn):
  51.         print 'Observation data file: ' + obsfn   
  52.         obsf = addfile_micaps(obsfn)
  53.         fdata = fcstf['VIS'][t,:,:]
  54.         lfcst = contourfm(fdata, levs, colors=cols, proj=fcstf.proj)
  55.         odata = obsf.stationdata('WeatherNow')
  56.         lobs = scatterm(odata, symbolspec=ls)
  57.         title('CUACE/Haze-Fog visibility (km) ' + sbjt.strftime('%Y-%m-%d %H:00') + \
  58.                 ' +' + str(t*3) + ' (' + bjt.strftime('%Y-%m-%d %H:00') + ')', \
  59.                 bold=False)
  60.         xlim(68.6, 140.5)
  61.         ylim(17.3, 54)
  62.         colorbar(lfcst, extendrect=False, shrink=0.6)
  63.         #Add frame to gif animation
  64.         gifaddframe(animation)
  65.         nn += 1

  66. #Finish gif animation
  67. animation.finish()
  68. print 'Finished...'
V_vis_20151101--loop-.gif
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-11-27 16:48:35 | 显示全部楼层
王老师真厉害,赞一个
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-11-30 17:01:04 | 显示全部楼层
王老师了不起...
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-12-2 11:01:53 | 显示全部楼层
好东西 收藏了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-12-2 11:06:28 | 显示全部楼层
{:eb511:}{:eb511:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2015-12-2 12:32:42 | 显示全部楼层
很漂亮的动画图,有时间要认真学习!谢谢王老师!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2017-8-22 12:51:24 | 显示全部楼层
王老师,请问最后一张图那个第二个读取的.shp文件是什么呢?在哪里可以下到这个数据
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2017-11-22 23:32:28 | 显示全部楼层
厉害了我的王老师,然而,我看不懂
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2017-12-10 16:27:09 | 显示全部楼层
{:5_235:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2017-12-27 22:05:16 | 显示全部楼层
厉害了王老师
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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