% function micom_ovrtrn(datesta,dateend,ncfile)
% Synatic micom_ovrtrn_plot(datesta,dateend)

%Input:  --datesta : start of the averaged period
%	 --dateend : end of the averaged period
%    --ncfile  : name of input .nc file 
%Output: --

%   Written by He Yanchun, PKU,NZC,NERSC
%   $Revision: 1.0  $  $Date: 2009/01/02 19:13:41 $
%   $Revision: 1.0  $  $Date: 2009/04/25 19:13:41 $

%plot AMOC
clear all;
close all;
unix('ls ovrtrn.*.nc >filelist');
files=importdata('filelist');
lenfile=length(files);
amoc=NaN(1,lenfile);
for i=1:lenfile
	var=nc_varget(files{i},'amoc');
	amoc(i)=var;
        if ~mod(i,12)
        	display(['The Year Is :',int2str(fix(i/12)+1948)]);
        end
end

save amoc.mat amoc
display('plotting amoc ...');
plot(amoc,'-k','linewidth',1);
title ('Atlantic Meridonal Overturning Circulation','fontsize',18,'fontweight','bold')
xlabel('Year','fontsize',18,'fontweight','bold')
ylabel('Units:Sv','fontsize',18,'fontweight','bold');
%set(gca,'xtick',3:5:lenfile,'xticklabel',1950:5:1948+lenfile,...
%	'xgrid','on','ygrid','on','fontsize',18,'fontweight','bold');%ticklabel from 1950
set(gca,'xtick',[3,243,481,696],'xticklabel',{'1948','1968','1988','2006'},'fontsize',18,'fontweight','bold');
axis tight
grid on;
set(gcf,'Position',[0 400 1024 768],'Renderer','painters','PaperPositionMode','auto');
filename=['amoc','.png'];
print('-dpng','-r300',filename);
filename=['amoc','.eps'];
print('-deps','-r300',filename);

%% plot meridional overturning in north atlantic

clear all;
close all;

display('plotting overturning ...');
%% Define Vars
% datesta=num2str(datesta);
% dateend=num2str(dateend);
latn=75;                            %north limit is 75N.
lats=-30;                           %south limit is 30S.
%year='2006';
%file=[PATHIN,'H04S5_ovrtrn_ave_',year,'.nc'];    %data file;
file='ovrtrn_ave.nc';    %data file;
%% Load Data
ovrtd=nc_varget(file,'ovrtd');
lat=nc_varget(file,'lat');
depth=nc_varget(file,'depth');

%% Compute ***
[Y I]=min(abs(lat-latn));
ni=I;                               %north index
[Y I]=min(abs(lat-lats));
si=I;                               %south index
[lat depth]=meshgrid(lat(si:ni),depth);
% Atlantic,Indian,Pacific,Global
var=squeeze(ovrtd(1,:,si:ni));      %plot  Atlantic Ocean
var(find(var>200))=NaN;
var=smooth2d(var);                  %smooth data
var=smooth2d(var);
var=var+0.2;                          %the 'smooth' would reduce the value
temp=abs(max(max(var)));
mask=-2*temp*ones(size(var));
mask(find(isnan(var)))=2*temp;
clear temp;
%% Plot the Figures

pcolor(lat,depth,mask)
shading flat;
colormap([1 1 1;0.6 0.6 0.6]);
set(gca,'ydir','reverse');
hold on;
[C H]=contour(lat,depth,var,'k');
set(H,'levellist',2:2:18,'textlist',2:2:18);
set(gca,'ytick',0:1000:6000,'yticklabel',0:1000:6000,'ydir','reverse');
clabel(C,H,'fontsize',10,'fontweight','bold','rotation',0,'color','k',...
    'labelspacing',3900);
[C H]=contour(lat,depth,var);
set(H,'levellist',0,'textlist',0,'linewidth',2,'linecolor','k');
clabel(C,H,'fontsize',10,'fontweight','bold','rotation',0,'color','k',...
    'labelspacing',3900);
[C H]=contour(lat,depth,var,'--');
set(H,'levellist',-4:2:0,'textlist',-4:2:0,'linewidth',1,'linecolor','k');
clabel(C,H,'fontsize',10,'fontweight','bold','rotation',0,'color','k',...
    'labelspacing',3900);
xlabel('Latitude (\circ N)','fontsize',18,'fontweight','bold');
ylabel('Depth (m)','fontsize',18,'fontweight','bold');
%title(['Annual Mean Atlantic Meridional Overturning Stream Function @ year ',...
%    year],'fontsize',18,'fontweight','bold');
title('Longterm Mean Atlantic Meridional Overturning Stream Function ',...
     'fontsize',18,'fontweight','bold');
%% Save the Figures and Data
set(gca,'dataaspectratio',[1 100 1],'fontsize',18);
set(gcf,'Position',[0 400 1024 768],'Renderer','painters');
%filename=['H04S5_amo_',year,'.png'];
filename=['ovrtrn_ave','.png'];
print('-painter','-dpng','-r300',filename);
filename=['ovrtrn_ave','.eps'];
print('-painter','-deps','-r300',filename);
quit
%return
