爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 4400|回复: 0

[讨论] MATLAB 如何绘制GIF动图,以全球SST展示厄尔尼诺现象为例

[复制链接]

新浪微博达人勋

发表于 2019-8-25 15:26:02 | 显示全部楼层 |阅读模式

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

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

x
matlab绘制GIF主要分为俩步:
1、批量导出图片
SST(longitude,latitude,time)就要批量导出绘制时间序列的月平均SST(本人在1979-2017年数据中提取07-16年的十年数据绘制)

2、GIF动图生成
由批量导出图制作GIF(本人提取了97-98年的月平均数据,就是合成厄尔尼诺年的SST动图)

动图不知道为啥上传不了,URL也不让放,SST数据太大了,你懂的。。。
所以自己下载吧,只为赚点积分和给大家提供一点帮助而已


代码
  1. %% load data
  2. clc; close all; clear ;
  3. input_path = 'D:\file_data\课件_秋季\气候统计方法和应用\气候统计方法与应用_homework\气候统计3\';
  4. out_path = 'D:\file_data\课件_秋季\气候统计方法和应用\气候统计方法与应用_homework\气候统计3\out_fig\';
  5. lon = ncread([input_path,'1979-2017.nc'],'longitude');
  6. lat = ncread([input_path,'1979-2017.nc'],'latitude');
  7. time = ncread([input_path,'1979-2017.nc'],'time');
  8. sst = ncread([input_path,'1979-2017.nc'],'sst');

  9. %% data process
  10. mon_s = 1;mon_e = 456;
  11. SST_07_16 = sst(:,:,mon_s:mon_e);
  12. SST_07_16(SST_07_16 > 35) = nan;
  13. SST_07_16(SST_07_16 < -4) = nan;
  14. nt = length(time(mon_s:mon_e));
  15. yr = nt/12;
  16. [sst_ano] = ano(SST_07_16,yr);
  17. [xc,yc] = meshgrid(lon,lat);

  18. for mon = 1:nt
  19.     %yr = (mon -1) / 12 + 1;
  20.     figure
  21.     contourf(xc,yc,sst_ano(:,:,mon)');colorbar;caxis([-1.5 1.5]);
  22.     colormap;
  23.     grid on;
  24.     xlabel('longitude(E)','FontSize',12);ylabel('latitude(S)','FontSize',12) ;
  25.     set(gca,'fontsize',12);
  26.     title(['SST ano TIME=',num2str(mon),' mon '],'fontsize',12);
  27.     %gtext({'(Pa)'},'fontsize',12);
  28.     set(gcf,'position',[200,300,800,600]);
  29.     saveas(gcf,[out_path,num2str(mon),'mon_ano.jpg']);
  30.     close
  31. end

  32. disp('okk');

  33. %% clc ; clear ;close all;
  34. filename = 'SST_eino_1.gif';
  35. key_s = 215;
  36. key_e = 240;
  37. for idx = key_s:key_e%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  38.    str = strcat(num2str(idx),'mon_ano.jpg');
  39.    K = imread(str);
  40.    [A,map] = rgb2ind(K,256);
  41. if idx == key_s%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42.     imwrite(A,map,filename,'gif','LoopCount',inf,'DelayTime',0.1);
  43. else
  44.     imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.1);
  45. end
  46. end

  47. disp('finish!!');


  48. function [A_anomaly] = ano(A,n)
  49. %% Description
  50. % ano is the code to get a muti-year monthly average data that removes monthly averages
  51. % separately.
  52. % A is the original data . It is designed specifically for 3D matricies of data such as  
  53. % sea surface temperatures where dimensions 1 and 2 are spatial dimensions (e.g., lat
  54. % and lon; lon and lat; x and y, etc.), and the third dimension represents different
  55. % slices or snapshots of data in time.  
  56. % n is the times of years about original data.
  57. % A_anomaly is a muti-year monthly average data that removes monthly averages separately.

  58. %% Author Info
  59. % huazhang
  60. % 27/11/2018

  61. for i1=1:12
  62.     a=A(:,:,i1);
  63.    for j1=1:n
  64.     i2=i1+12*(j1-1);
  65.     a=a+A(:,:,i2);
  66.    end
  67.    a_all(:,:,i1)=a./j1;
  68. end

  69. for i1=1:12
  70.    for j1=1:n
  71.     i2=i1+12*(j1-1);
  72.     a=A(:,:,i2)-a_all(:,:,i1);
  73.     A_anomaly(:,:,i2)=a;
  74.    end
  75. end
  76. end
复制代码


235mon_ano.jpg
233mon_ano.jpg
231mon_ano.jpg
229mon_ano.jpg
227mon_ano.jpg
225mon_ano.jpg
223mon_ano.jpg
221mon_ano.jpg
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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