%figure 1: plot amoc at 26.5N or 20-50N

clear all;
close all;

flag='max_region';
system('ls data/micom_hm_*.nc >filelist')

files=importdata('filelist');
len=length(files)
amoc=nan(1,len);
lat=ncread(files{1},'lat');
if strcmp(flag,'max_region') %max overturning between xx and xx
    for k=1:len
        files{k}
        tmp=ncread(files{k},'mmflxl');
        tmp=squeeze(tmp(:,:,1)); %region=1
        [c,latid1]=min(abs(lat-20));
        [c,latid2]=min(abs(lat-50));
        tmp1=squeeze(tmp(latid1:latid2,:));
        amoc(1,k)=max(max(tmp1)); %max along lat
    end
elseif strcmp(flag,'max_lat') % max at fixed latitude
    for k=1:len
        files{k}
        tmp=ncread(files{k},'mmflxl');
        tmp=squeeze(tmp(:,:,1)); %region=1
        [c,latid]=min(abs(lat-26.5));
        tmp1=squeeze(tmp(latid,:,1));
        amoc(1,k)=max(tmp1); %max along lat
    end
elseif strcmp(flag,'fixed_latdepth')
    for k=1:len
        files{k}
        tmp=ncread(files{k},'mmflxd');
        amoc(1,k)=squeeze(tmp(128,45,1)); %region=1,dep=45(1400m),lat=128(47N)
    end
else
    echo 'unkown definition to amoc, exiting...'
end
amoc=amoc/1e9; %convert from kg/s to Sv(m^3/s)

plot(amoc,'linewidth',2,'color','k')
set(gca,'xlim',[1-1 len+1],'ylim',[amoc_min amoc_max]);
set(gca,'tickdir','out');
set(gca,'xgrid','on','ygrid','on','gridlinestyle',':','linewidth',2)
set(gca,'fontsize',14,'fontweight','normal');
xlabel('year','fontsize',14)
ylabel('sv','fontsize',14)
if strcmp(flag,'max_region')
    title('Maximum (20N-50N) Atlantic Meridional Overturning','fontsize',14)
elseif strcmp(flag,'max_lat')
    title('Maximum (26.5N) Atlantic Meridional Overturning','fontsize',14)
else
    title('Region not set?','fontsize',14)
end
%%
print -dpng -r300 amoc_matlab.png

