MATLAB 如何绘制GIF动图,以全球SST为例

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

matlab绘制GIF主要分为俩步:

1、批量导出图片

SST(longitude,latitude,time)就要批量导出绘制时间序列的月平均SST(本人在1979-2017年数据中提取07-16年的十年数据绘制)

2、GIF动图生成

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

SST动图展示

代码

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

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

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

disp('okk');

%% clc ; clear ;close all;
filename = 'SST_eino_1.gif';
key_s = 215;
key_e = 240;
for idx = key_s:key_e%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   str = strcat(num2str(idx),'mon_ano.jpg');
   K = imread(str);
   [A,map] = rgb2ind(K,256); 
 if idx == key_s%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    imwrite(A,map,filename,'gif','LoopCount',inf,'DelayTime',0.1);
 else
    imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.1);
 end
end

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

%% Author Info 
% huazhang 
% 27/11/2018

for i1=1:12
    a=A(:,:,i1);
   for j1=1:n
    i2=i1+12*(j1-1);
    a=a+A(:,:,i2);
   end
   a_all(:,:,i1)=a./j1;
end

for i1=1:12
   for j1=1:n
    i2=i1+12*(j1-1);
    a=A(:,:,i2)-a_all(:,:,i1);
    A_anomaly(:,:,i2)=a;
   end
end
end

SST数据

sst数据过大,需要自己下载哦

SST数据下载地址

你可能感兴趣的:(matlab,海洋大气)