%start to compose the atmospheric energy budget year by year

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% clear all; close all;
% 
% 
% dir='F:\trace\SH\';
% fil={'trace.01.22000-20001BP.cam2.h0.SHFLX.0000101-0200012.nc';  
% 'trace.02.20000-19001BP.cam2.h0.SHFLX.0200101-0300012.nc';  
% 'trace.03.19000-18501BP.cam2.h0.SHFLX.0300101-0350012.nc';  
% 'trace.04.18500-18401BP.cam2.h0.SHFLX.0350101-0360012.nc';  
% 'trace.05.18400-17501BP.cam2.h0.SHFLX.0360101-0450012.nc'; 
% 'trace.06.17500-17001BP.cam2.h0.SHFLX.0450101-0500012.nc';  
% 'trace.07.17000-16001BP.cam2.h0.SHFLX.0500101-0600012.nc';  
% 'trace.08.16000-15001BP.cam2.h0.SHFLX.0600101-0700012.nc';  
% 'trace.09.15000-14901BP.cam2.h0.SHFLX.0700101-0710012.nc';  
% 'trace.10.14900-14351BP.cam2.h0.SHFLX.0710101-0765012.nc';  
% 'trace.11.14350-13871BP.cam2.h0.SHFLX.0765101-0813012.nc';  
% 'trace.12.13870-13101BP.cam2.h0.SHFLX.0813101-0890012.nc';  
% 'trace.13.13100-12901BP.cam2.h0.SHFLX.0890101-0910012.nc';  
% 'trace.14.12900-12501BP.cam2.h0.SHFLX.0910101-0950012.nc';  
% 'trace.15.12500-12001BP.cam2.h0.SHFLX.0950101-1000012.nc';  
% 'trace.16.12000-11701BP.cam2.h0.SHFLX.1000101-1030012.nc';  
% 'trace.17.11700-11301BP.cam2.h0.SHFLX.1030101-1070012.nc';  
% 'trace.18.11300-10801BP.cam2.h0.SHFLX.1070101-1120012.nc';  
% 'trace.19.10800-10201BP.cam2.h0.SHFLX.1120101-1180012.nc';  
% 'trace.20.10200-09701BP.cam2.h0.SHFLX.1180101-1230012.nc';  
% 'trace.21.09700-09201BP.cam2.h0.SHFLX.1230101-1280012.nc';  
% 'trace.22.09200-08701BP.cam2.h0.SHFLX.1280101-1330012.nc';  
% 'trace.23.08700-08501BP.cam2.h0.SHFLX.1330101-1350012.nc';  
% 'trace.24.08500-08001BP.cam2.h0.SHFLX.1350101-1400012.nc';  
% 'trace.25.08000-07601BP.cam2.h0.SHFLX.1400101-1440012.nc';  
% 'trace.26.07600-07201BP.cam2.h0.SHFLX.1440101-1480012.nc';  
% 'trace.27.07200-06701BP.cam2.h0.SHFLX.1480101-1530012.nc';  
% 'trace.28.06700-06201BP.cam2.h0.SHFLX.1530101-1580012.nc';  
% 'trace.29.06200-05701BP.cam2.h0.SHFLX.1580101-1630012.nc';  
% 'trace.30.05700-05001BP.cam2.h0.SHFLX.1630101-1700012.nc';  
% 'trace.31.05000-04001BP.cam2.h0.SHFLX.1700101-1800012.nc';  
% 'trace.32.04000-03201BP.cam2.h0.SHFLX.1800101-1880012.nc';  
% 'trace.33.03200-02401BP.cam2.h0.SHFLX.1880101-1960012.nc';  
% 'trace.34.02400-01401BP.cam2.h0.SHFLX.1960101-2060012.nc';  
% 'trace.35.01400-00401BP.cam2.h0.SHFLX.2060101-2160012.nc';  
% 'trace.36.400BP-1990CE.cam2.h0.SHFLX.2160101-2204012.nc'};   
% off=0;
% NF=length(fil);
% for n=1:NF;
%     tic
% FIL=[dir char(fil(n,:))];
% %ncdisp(FIL);
% 
% t=ncread(FIL, 'time', [1], [Inf]); NT=length(t);
% lon=ncread(FIL, 'lon', [1], [Inf]); N=length(lon);
% lat=ncread(FIL,'lat', [1], [Inf]); NN=length(lat);
% sh=permute(ncread(FIL, 'SHFLX', [1 1 1], [Inf Inf Inf]), [3 2 1]);
% year=rounddown(t*1000);
% month=round(12*(t*1000-(year)));
% if n == 36; %the times are just a little fubarred
%     year=g2c((-400:39)'*ones(1,12));
%     month=g2c( ((1:12)'*ones(1,length(-4000:39)))')
% end
% 
% t_step=10;
% yrs=min(year):t_step:max(year); NY=length(yrs);
% cut=20;
% don=find(lat>=0 & lat <= cut);
% dos=find(lat<=0 & lat >= -cut);
% 
% for ny=1:NY;
%     do = find( year >= yrs(ny) & year < yrs(ny)+t_step );
%     SH(off+ny,1:NN, 1:N)= (squeeze(mean(sh(do,:,:))));
%     
% end
% off=off+NY;
% [min(year) max(year) off]
% clear sh
% toc
% end
% 
% YEARS=-22000:t_step:39;
% save('F:\trace\SH_dec.mat', 'YEARS', 'lon','lat', 'SH');

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %start to compose the atmospheric energy budget year by year
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% clear all; close all;
% t_step=10;
% 
% dir='F:\trace\LH\';
% fil={'trace.01.22000-20001BP.cam2.h0.LHFLX.0000101-0200012.nc';  
% 'trace.02.20000-19001BP.cam2.h0.LHFLX.0200101-0300012.nc';  
% 'trace.03.19000-18501BP.cam2.h0.LHFLX.0300101-0350012.nc';  
% 'trace.04.18500-18401BP.cam2.h0.LHFLX.0350101-0360012.nc';  
% 'trace.05.18400-17501BP.cam2.h0.LHFLX.0360101-0450012.nc';  
% 'trace.06.17500-17001BP.cam2.h0.LHFLX.0450101-0500012.nc';  
% 'trace.07.17000-16001BP.cam2.h0.LHFLX.0500101-0600012.nc';  
% 'trace.08.16000-15001BP.cam2.h0.LHFLX.0600101-0700012.nc';  
% 'trace.09.15000-14901BP.cam2.h0.LHFLX.0700101-0710012.nc';  
% 'trace.10.14900-14351BP.cam2.h0.LHFLX.0710101-0765012.nc';  
% 'trace.11.14350-13871BP.cam2.h0.LHFLX.0765101-0813012.nc'; 
% 'trace.12.13870-13101BP.cam2.h0.LHFLX.0813101-0890012.nc';  
% 'trace.13.13100-12901BP.cam2.h0.LHFLX.0890101-0910012.nc';  
% 'trace.14.12900-12501BP.cam2.h0.LHFLX.0910101-0950012.nc';  
% 'trace.15.12500-12001BP.cam2.h0.LHFLX.0950101-1000012.nc';  
% 'trace.16.12000-11701BP.cam2.h0.LHFLX.1000101-1030012.nc';  
% 'trace.17.11700-11301BP.cam2.h0.LHFLX.1030101-1070012.nc';  
% 'trace.18.11300-10801BP.cam2.h0.LHFLX.1070101-1120012.nc';  
% 'trace.19.10800-10201BP.cam2.h0.LHFLX.1120101-1180012.nc';  
% 'trace.20.10200-09701BP.cam2.h0.LHFLX.1180101-1230012.nc';  
% 'trace.21.09700-09201BP.cam2.h0.LHFLX.1230101-1280012.nc';  
% 'trace.22.09200-08701BP.cam2.h0.LHFLX.1280101-1330012.nc';  
% 'trace.23.08700-08501BP.cam2.h0.LHFLX.1330101-1350012.nc';  
% 'trace.24.08500-08001BP.cam2.h0.LHFLX.1350101-1400012.nc';  
% 'trace.25.08000-07601BP.cam2.h0.LHFLX.1400101-1440012.nc';  
% 'trace.26.07600-07201BP.cam2.h0.LHFLX.1440101-1480012.nc';  
% 'trace.27.07200-06701BP.cam2.h0.LHFLX.1480101-1530012.nc';  
% 'trace.28.06700-06201BP.cam2.h0.LHFLX.1530101-1580012.nc';  
% 'trace.29.06200-05701BP.cam2.h0.LHFLX.1580101-1630012.nc';  
% 'trace.30.05700-05001BP.cam2.h0.LHFLX.1630101-1700012.nc';  
% 'trace.31.05000-04001BP.cam2.h0.LHFLX.1700101-1800012.nc';  
% 'trace.32.04000-03201BP.cam2.h0.LHFLX.1800101-1880012.nc';  
% 'trace.33.03200-02401BP.cam2.h0.LHFLX.1880101-1960012.nc';  
% 'trace.34.02400-01401BP.cam2.h0.LHFLX.1960101-2060012.nc';  
% 'trace.35.01400-00401BP.cam2.h0.LHFLX.2060101-2160012.nc';  
% 'trace.36.400BP-1990CE.cam2.h0.LHFLX.2160101-2204012.nc'};   
% off=0;
% NF=length(fil);
% for n=1:NF;
%     tic
% FIL=[dir char(fil(n,:))];
% %ncdisp(FIL);
% 
% t=ncread(FIL, 'time', [1], [Inf]); NT=length(t);
% lon=ncread(FIL, 'lon', [1], [Inf]); N=length(lon);
% lat=ncread(FIL,'lat', [1], [Inf]); NN=length(lat);
% lh=permute(ncread(FIL, 'LHFLX', [1 1 1], [Inf Inf Inf]), [3 2 1]);
% year=rounddown(t*1000);
% month=round(12*(t*1000-(year)));
% if n == 36; %the times are just a little fubarred
%     year=g2c((-400:39)'*ones(1,12));
%     month=g2c( ((1:12)'*ones(1,length(-4000:39)))')
% end
% 
% 
% yrs=min(year):t_step:max(year); NY=length(yrs);
% cut=20;
% don=find(lat>=0 & lat <= cut);
% dos=find(lat<=0 & lat >= -cut);
% 
% for ny=1:NY;
%     do = find( year >= yrs(ny) & year < yrs(ny)+t_step );
%     LH(off+ny,1:NN, 1:N)= (squeeze(mean(lh(do,:,:))));
%     
% end
% 
% off=off+NY;
% clear lh
% [min(year) max(year) off]
% toc
% end
% 
% YEARS=-22000:t_step:39;
% save('F:\trace\LH_dec.mat', 'YEARS', 'lat','lon', 'LH');
% 
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% clear all; close all;
% t_step=10;
% 
% dir='F:\trace\FSNT\';
% fil={'trace.01.22000-20001BP.cam2.h0.FSNTOA.0000101-0200012.nc';  
% 'trace.02.20000-19001BP.cam2.h0.FSNTOA.0200101-0300012.nc';  
% 'trace.03.19000-18501BP.cam2.h0.FSNTOA.0300101-0350012.nc';  
% 'trace.04.18500-18401BP.cam2.h0.FSNTOA.0350101-0360012.nc';  
% 'trace.05.18400-17501BP.cam2.h0.FSNTOA.0360101-0450012.nc'; 
% 'trace.06.17500-17001BP.cam2.h0.FSNTOA.0450101-0500012.nc';  
% 'trace.07.17000-16001BP.cam2.h0.FSNTOA.0500101-0600012.nc';  
% 'trace.08.16000-15001BP.cam2.h0.FSNTOA.0600101-0700012.nc';  
% 'trace.09.15000-14901BP.cam2.h0.FSNTOA.0700101-0710012.nc';  
% 'trace.10.14900-14351BP.cam2.h0.FSNTOA.0710101-0765012.nc';  
% 'trace.11.14350-13871BP.cam2.h0.FSNTOA.0765101-0813012.nc';  
% 'trace.12.13870-13101BP.cam2.h0.FSNTOA.0813101-0890012.nc';  
% 'trace.13.13100-12901BP.cam2.h0.FSNTOA.0890101-0910012.nc';  
% 'trace.14.12900-12501BP.cam2.h0.FSNTOA.0910101-0950012.nc';  
% 'trace.15.12500-12001BP.cam2.h0.FSNTOA.0950101-1000012.nc';  
% 'trace.16.12000-11701BP.cam2.h0.FSNTOA.1000101-1030012.nc';  
% 'trace.17.11700-11301BP.cam2.h0.FSNTOA.1030101-1070012.nc';  
% 'trace.18.11300-10801BP.cam2.h0.FSNTOA.1070101-1120012.nc';  
% 'trace.19.10800-10201BP.cam2.h0.FSNTOA.1120101-1180012.nc';  
% 'trace.20.10200-09701BP.cam2.h0.FSNTOA.1180101-1230012.nc';  
% 'trace.21.09700-09201BP.cam2.h0.FSNTOA.1230101-1280012.nc';  
% 'trace.22.09200-08701BP.cam2.h0.FSNTOA.1280101-1330012.nc';  
% 'trace.23.08700-08501BP.cam2.h0.FSNTOA.1330101-1350012.nc';  
% 'trace.24.08500-08001BP.cam2.h0.FSNTOA.1350101-1400012.nc';  
% 'trace.25.08000-07601BP.cam2.h0.FSNTOA.1400101-1440012.nc';  
% 'trace.26.07600-07201BP.cam2.h0.FSNTOA.1440101-1480012.nc';  
% 'trace.27.07200-06701BP.cam2.h0.FSNTOA.1480101-1530012.nc';  
% 'trace.28.06700-06201BP.cam2.h0.FSNTOA.1530101-1580012.nc';  
% 'trace.29.06200-05701BP.cam2.h0.FSNTOA.1580101-1630012.nc';  
% 'trace.30.05700-05001BP.cam2.h0.FSNTOA.1630101-1700012.nc';  
% 'trace.31.05000-04001BP.cam2.h0.FSNTOA.1700101-1800012.nc';  
% 'trace.32.04000-03201BP.cam2.h0.FSNTOA.1800101-1880012.nc';  
% 'trace.33.03200-02401BP.cam2.h0.FSNTOA.1880101-1960012.nc';  
% 'trace.34.02400-01401BP.cam2.h0.FSNTOA.1960101-2060012.nc';  
% 'trace.35.01400-00401BP.cam2.h0.FSNTOA.2060101-2160012.nc';  
% 'trace.36.400BP-1990CE.cam2.h0.FSNTOA.2160101-2204012.nc';};   
% off=0;
% NF=length(fil);
% for n=1:NF;
%     tic
% FIL=[dir char(fil(n,:))];
% %ncdisp(FIL);
% 
% t=ncread(FIL, 'time', [1], [Inf]); NT=length(t);
% lon=ncread(FIL, 'lon', [1], [Inf]); N=length(lon);
% lat=ncread(FIL,'lat', [1], [Inf]); NN=length(lat);
% fsnt=permute(ncread(FIL, 'FSNTOA', [1 1 1], [Inf Inf Inf]), [3 2 1]);
% year=rounddown(t*1000);
% month=round(12*(t*1000-(year)));
% if n == 36; %the times are just a little fubarred
%     year=g2c((-400:39)'*ones(1,12));
%     month=g2c( ((1:12)'*ones(1,length(-4000:39)))')
% end
% 
% 
% yrs=min(year):t_step:max(year); NY=length(yrs);
% cut=20;
% don=find(lat>=0 & lat <= cut);
% dos=find(lat<=0 & lat >= -cut);
% 
% for ny=1:NY;
%     do = find( year >= yrs(ny) & year < yrs(ny)+t_step );
%     FSNT(off+ny,1:NN, 1:N)= (squeeze(mean(fsnt(do,:,:))));
%     
% end
% 
% off=off+NY;
% clear fsnt
% [min(year) max(year) off]
% toc
% end
% 
% YEARS=-22000:t_step:39;
% save('F:\trace\FSNT_dec.mat', 'YEARS', 'lat','lon', 'FSNT');
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all; close all;

t_step=10;
dir='F:\trace\OLR\';
fil={'trace.01.22000-20001BP.cam2.h0.FLNT.0000101-0200012.nc';  
'trace.02.20000-19001BP.cam2.h0.FLNT.0200101-0300012.nc';  
'trace.03.19000-18501BP.cam2.h0.FLNT.0300101-0350012.nc';  
'trace.04.18500-18401BP.cam2.h0.FLNT.0350101-0360012.nc';  
'trace.05.18400-17501BP.cam2.h0.FLNT.0360101-0450012.nc';  
'trace.06.17500-17001BP.cam2.h0.FLNT.0450101-0500012.nc';  
'trace.07.17000-16001BP.cam2.h0.FLNT.0500101-0600012.nc';  
'trace.08.16000-15001BP.cam2.h0.FLNT.0600101-0700012.nc';  
'trace.09.15000-14901BP.cam2.h0.FLNT.0700101-0710012.nc'; 
'trace.10.14900-14351BP.cam2.h0.FLNT.0710101-0765012.nc';  
'trace.11.14350-13871BP.cam2.h0.FLNT.0765101-0813012.nc';  
'trace.12.13870-13101BP.cam2.h0.FLNT.0813101-0890012.nc';  
'trace.13.13100-12901BP.cam2.h0.FLNT.0890101-0910012.nc';  
'trace.14.12900-12501BP.cam2.h0.FLNT.0910101-0950012.nc'; 
'trace.15.12500-12001BP.cam2.h0.FLNT.0950101-1000012.nc';  
'trace.16.12000-11701BP.cam2.h0.FLNT.1000101-1030012.nc';  
'trace.17.11700-11301BP.cam2.h0.FLNT.1030101-1070012.nc';  
'trace.18.11300-10801BP.cam2.h0.FLNT.1070101-1120012.nc';  
'trace.19.10800-10201BP.cam2.h0.FLNT.1120101-1180012.nc';  
'trace.20.10200-09701BP.cam2.h0.FLNT.1180101-1230012.nc';  
'trace.21.09700-09201BP.cam2.h0.FLNT.1230101-1280012.nc';  
'trace.22.09200-08701BP.cam2.h0.FLNT.1280101-1330012.nc';  
'trace.23.08700-08501BP.cam2.h0.FLNT.1330101-1350012.nc';  
'trace.24.08500-08001BP.cam2.h0.FLNT.1350101-1400012.nc';  
'trace.25.08000-07601BP.cam2.h0.FLNT.1400101-1440012.nc';  
'trace.26.07600-07201BP.cam2.h0.FLNT.1440101-1480012.nc';  
'trace.27.07200-06701BP.cam2.h0.FLNT.1480101-1530012.nc';  
'trace.28.06700-06201BP.cam2.h0.FLNT.1530101-1580012.nc';  
'trace.29.06200-05701BP.cam2.h0.FLNT.1580101-1630012.nc';  
'trace.30.05700-05001BP.cam2.h0.FLNT.1630101-1700012.nc';  
'trace.31.05000-04001BP.cam2.h0.FLNT.1700101-1800012.nc';  
'trace.32.04000-03201BP.cam2.h0.FLNT.1800101-1880012.nc';  
'trace.33.03200-02401BP.cam2.h0.FLNT.1880101-1960012.nc';  
'trace.34.02400-01401BP.cam2.h0.FLNT.1960101-2060012.nc';  
'trace.35.01400-00401BP.cam2.h0.FLNT.2060101-2160012.nc';  
'trace.36.400BP-1990CE.cam2.h0.FLNT.2160101-2204012.nc';};   
off=0;
NF=length(fil);
for n=1:NF;
    tic
FIL=[dir char(fil(n,:))];
%ncdisp(FIL);

t=ncread(FIL, 'time', [1], [Inf]); NT=length(t);
lon=ncread(FIL, 'lon', [1], [Inf]); N=length(lon);
lat=ncread(FIL,'lat', [1], [Inf]); NN=length(lat);
olr=permute(ncread(FIL, 'FLNT', [1 1 1], [Inf Inf Inf]), [3 2 1]);
year=rounddown(t*1000);
month=round(12*(t*1000-(year)));
if n == 36; %the times are just a little fubarred
    year=g2c((-400:39)'*ones(1,12));
    month=g2c( ((1:12)'*ones(1,length(-4000:39)))')
end


yrs=min(year):t_step:max(year); NY=length(yrs);
cut=20;
don=find(lat>=0 & lat <= cut);
dos=find(lat<=0 & lat >= -cut);

for ny=1:NY;
    do = find( year >= yrs(ny) & year < yrs(ny)+t_step );
    OLR(off+ny,1:NN, 1:N)= (squeeze(mean(olr(do,:,:))));
    
end
off=off+NY;
clear olr
[min(year) max(year) off]
toc
end

YEARS=-22000:t_step:39;
save('F:\trace\OLR_dec.mat', 'YEARS', 'lat','lon', 'OLR');



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all; close all;

t_step=10;
dir='F:\trace\FSNS\';
fil={'trace.01.22000-20001BP.cam2.h0.FSNS.0000101-0200012.nc';  
'trace.02.20000-19001BP.cam2.h0.FSNS.0200101-0300012.nc';  
'trace.03.19000-18501BP.cam2.h0.FSNS.0300101-0350012.nc';  
'trace.04.18500-18401BP.cam2.h0.FSNS.0350101-0360012.nc';  
'trace.05.18400-17501BP.cam2.h0.FSNS.0360101-0450012.nc';  
'trace.06.17500-17001BP.cam2.h0.FSNS.0450101-0500012.nc';  
'trace.07.17000-16001BP.cam2.h0.FSNS.0500101-0600012.nc';  
'trace.08.16000-15001BP.cam2.h0.FSNS.0600101-0700012.nc'; 
'trace.09.15000-14901BP.cam2.h0.FSNS.0700101-0710012.nc';  
'trace.10.14900-14351BP.cam2.h0.FSNS.0710101-0765012.nc';  
'trace.11.14350-13871BP.cam2.h0.FSNS.0765101-0813012.nc';  
'trace.12.13870-13101BP.cam2.h0.FSNS.0813101-0890012.nc';  
'trace.13.13100-12901BP.cam2.h0.FSNS.0890101-0910012.nc';  
'trace.14.12900-12501BP.cam2.h0.FSNS.0910101-0950012.nc';  
'trace.15.12500-12001BP.cam2.h0.FSNS.0950101-1000012.nc';  
'trace.16.12000-11701BP.cam2.h0.FSNS.1000101-1030012.nc';  
'trace.17.11700-11301BP.cam2.h0.FSNS.1030101-1070012.nc';  
'trace.18.11300-10801BP.cam2.h0.FSNS.1070101-1120012.nc';  
'trace.19.10800-10201BP.cam2.h0.FSNS.1120101-1180012.nc';  
'trace.20.10200-09701BP.cam2.h0.FSNS.1180101-1230012.nc';  
'trace.21.09700-09201BP.cam2.h0.FSNS.1230101-1280012.nc';  
'trace.22.09200-08701BP.cam2.h0.FSNS.1280101-1330012.nc';  
'trace.23.08700-08501BP.cam2.h0.FSNS.1330101-1350012.nc';  
'trace.24.08500-08001BP.cam2.h0.FSNS.1350101-1400012.nc';  
'trace.25.08000-07601BP.cam2.h0.FSNS.1400101-1440012.nc';  
'trace.26.07600-07201BP.cam2.h0.FSNS.1440101-1480012.nc';  
'trace.27.07200-06701BP.cam2.h0.FSNS.1480101-1530012.nc';  
'trace.28.06700-06201BP.cam2.h0.FSNS.1530101-1580012.nc';  
'trace.29.06200-05701BP.cam2.h0.FSNS.1580101-1630012.nc';  
'trace.30.05700-05001BP.cam2.h0.FSNS.1630101-1700012.nc';  
'trace.31.05000-04001BP.cam2.h0.FSNS.1700101-1800012.nc';  
'trace.32.04000-03201BP.cam2.h0.FSNS.1800101-1880012.nc';  
'trace.33.03200-02401BP.cam2.h0.FSNS.1880101-1960012.nc';  
'trace.34.02400-01401BP.cam2.h0.FSNS.1960101-2060012.nc';  
'trace.35.01400-00401BP.cam2.h0.FSNS.2060101-2160012.nc';  
'trace.36.400BP-1990CE.cam2.h0.FSNS.2160101-2204012.nc';};   
off=0;
NF=length(fil);
for n=1:NF;
    tic
FIL=[dir char(fil(n,:))];
%ncdisp(FIL);

t=ncread(FIL, 'time', [1], [Inf]); NT=length(t);
lon=ncread(FIL, 'lon', [1], [Inf]); N=length(lon);
lat=ncread(FIL,'lat', [1], [Inf]); NN=length(lat);
fsns=permute(ncread(FIL, 'FSNS', [1 1 1], [Inf Inf Inf]), [3 2 1]);
year=rounddown(t*1000);
month=round(12*(t*1000-(year)));
if n == 36; %the times are just a little fubarred
    year=g2c((-400:39)'*ones(1,12));
    month=g2c( ((1:12)'*ones(1,length(-4000:39)))')
end


yrs=min(year):t_step:max(year); NY=length(yrs);
cut=20;
don=find(lat>=0 & lat <= cut);
dos=find(lat<=0 & lat >= -cut);

for ny=1:NY;
     do = find( year >= yrs(ny) & year < yrs(ny)+t_step );
     FSNS(off+ny,1:NN, 1:N)= (squeeze(mean(fsns(do,:,:))));
    
end
off=off+NY;
clear fsns
[min(year) max(year) off]
toc
end

YEARS=-22000:t_step:39;
save('F:\trace\FSNS_dec.mat', 'YEARS', 'lat','lon', 'FSNS');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all; close all;

t_step=10;
dir='F:\trace\FLNS\';
fil={'trace.01.22000-20001BP.cam2.h0.FLNS.0000101-0200012.nc';  
'trace.02.20000-19001BP.cam2.h0.FLNS.0200101-0300012.nc';  
'trace.03.19000-18501BP.cam2.h0.FLNS.0300101-0350012.nc';  
'trace.04.18500-18401BP.cam2.h0.FLNS.0350101-0360012.nc';  
'trace.05.18400-17501BP.cam2.h0.FLNS.0360101-0450012.nc';  
'trace.06.17500-17001BP.cam2.h0.FLNS.0450101-0500012.nc';  
'trace.07.17000-16001BP.cam2.h0.FLNS.0500101-0600012.nc';  
'trace.08.16000-15001BP.cam2.h0.FLNS.0600101-0700012.nc'; 
'trace.09.15000-14901BP.cam2.h0.FLNS.0700101-0710012.nc';  
'trace.10.14900-14351BP.cam2.h0.FLNS.0710101-0765012.nc';  
'trace.11.14350-13871BP.cam2.h0.FLNS.0765101-0813012.nc';  
'trace.12.13870-13101BP.cam2.h0.FLNS.0813101-0890012.nc';  
'trace.13.13100-12901BP.cam2.h0.FLNS.0890101-0910012.nc';  
'trace.14.12900-12501BP.cam2.h0.FLNS.0910101-0950012.nc';  
'trace.15.12500-12001BP.cam2.h0.FLNS.0950101-1000012.nc';  
'trace.16.12000-11701BP.cam2.h0.FLNS.1000101-1030012.nc';  
'trace.17.11700-11301BP.cam2.h0.FLNS.1030101-1070012.nc';  
'trace.18.11300-10801BP.cam2.h0.FLNS.1070101-1120012.nc';  
'trace.19.10800-10201BP.cam2.h0.FLNS.1120101-1180012.nc';  
'trace.20.10200-09701BP.cam2.h0.FLNS.1180101-1230012.nc';  
'trace.21.09700-09201BP.cam2.h0.FLNS.1230101-1280012.nc';  
'trace.22.09200-08701BP.cam2.h0.FLNS.1280101-1330012.nc';  
'trace.23.08700-08501BP.cam2.h0.FLNS.1330101-1350012.nc';  
'trace.24.08500-08001BP.cam2.h0.FLNS.1350101-1400012.nc';  
'trace.25.08000-07601BP.cam2.h0.FLNS.1400101-1440012.nc';  
'trace.26.07600-07201BP.cam2.h0.FLNS.1440101-1480012.nc';  
'trace.27.07200-06701BP.cam2.h0.FLNS.1480101-1530012.nc';  
'trace.28.06700-06201BP.cam2.h0.FLNS.1530101-1580012.nc';  
'trace.29.06200-05701BP.cam2.h0.FLNS.1580101-1630012.nc';  
'trace.30.05700-05001BP.cam2.h0.FLNS.1630101-1700012.nc';  
'trace.31.05000-04001BP.cam2.h0.FLNS.1700101-1800012.nc';  
'trace.32.04000-03201BP.cam2.h0.FLNS.1800101-1880012.nc';  
'trace.33.03200-02401BP.cam2.h0.FLNS.1880101-1960012.nc';  
'trace.34.02400-01401BP.cam2.h0.FLNS.1960101-2060012.nc';  
'trace.35.01400-00401BP.cam2.h0.FLNS.2060101-2160012.nc';  
'trace.36.400BP-1990CE.cam2.h0.FLNS.2160101-2204012.nc';};   
off=0;
NF=length(fil);
for n=1:NF;
    tic
FIL=[dir char(fil(n,:))];
%ncdisp(FIL);

t=ncread(FIL, 'time', [1], [Inf]); NT=length(t);
lon=ncread(FIL, 'lon', [1], [Inf]); N=length(lon);
lat=ncread(FIL,'lat', [1], [Inf]); NN=length(lat);
flns=permute(ncread(FIL, 'FLNS', [1 1 1], [Inf Inf Inf]), [3 2 1]);
year=rounddown(t*1000);
month=round(12*(t*1000-(year)));
if n == 36; %the times are just a little fubarred
    year=g2c((-400:39)'*ones(1,12));
    month=g2c( ((1:12)'*ones(1,length(-4000:39)))')
end


yrs=min(year):t_step:max(year); NY=length(yrs);
cut=20;
don=find(lat>=0 & lat <= cut);
dos=find(lat<=0 & lat >= -cut);

for ny=1:NY;
     do = find( year >= yrs(ny) & year < yrs(ny)+t_step );
     FLNS(off+ny,1:NN, 1:N)= (squeeze(mean(flns(do,:,:))));
    
end
off=off+NY;
clear flns
[min(year) max(year) off]
toc
end

YEARS=-22000:39;
save('F:\trace\FLNS_dec.mat', 'YEARS', 'lat','lon', 'FLNS');