
format compact;clear all

en_number = 10;

cases = {'b.e21.BSSP245smbb.f09_g17.001';...
         'b.e21.BSSP245smbb.f09_g17.002';...
         'b.e21.BSSP245smbb.f09_g17.003';...
         'b.e21.BSSP245smbb.f09_g17.004';...
         'b.e21.BSSP245smbb.f09_g17.005';...
         'b.e21.BSSP245smbb.f09_g17.006';...
         'b.e21.BSSP245smbb.f09_g17.007';...
         'b.e21.BSSP245smbb.f09_g17.009';...
         'b.e21.BSSP245smbb.f09_g17.010';...
         'b.e21.BSSP245smbb.f09_g17.011';... 
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.001';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.002';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.003';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.004';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.005';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.006';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.007';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.009';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.010';...
         'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.011'};

vars = {'LANDFRAC';'TREFHT';'PRECC';'PRECL';'PRECT';'FLNT';'FSNT';'FLNTC';'FSNTC';'LWCF';'SWCF';'CLDHGH';'CLDMED';'CLDLOW';'TS';'CLDTOT';'ICEFRAC';'FSNS';'FSDS';'FSNSC';'FSDSC';'FSNTOA';'FSNTOAC';'FLNS';'FLDS';'PSL';'TMQ'};

dates = {'201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '201501-210012';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912';...
         '203501-206912'};

data = struct('casename',{},'varname',{},'lon',{},'lat',{},'date',{},'year',{},'month',{},'gw',{},'value',{},'global_avg',{},'annual_avg',{},'annual_avg_year',{});


for i=1:length(cases);
for j=1:length(vars);

    
    if( i<=10 )
      f = ['../../' char(cases(i)) '/atm/proc/tseries/month_1/' char(cases(i)) '.cam.h0.' char(vars(j)) '.201501-206412.nc']
      f2 = ['../../' char(cases(i)) '/atm/proc/tseries/month_1/' char(cases(i)) '.cam.h0.' char(vars(j)) '.206501-210012.nc'];
      date1 = ncread(f,'date');
      date2 = ncread(f2,'date');
      data(i,j).date = [ncread(f,'date');ncread(f2,'date')];
    %if(j>=length(vars)-1)
    %  xxx = ncread(f,char(vars(j)));
    %  data(i,j).value = squeeze(xxx(:,:,32,:));
    %  clear xxx
    %else
      data(i,j).value = ncread(f,char(vars(j)));
    %end
      data(i,j).value(:,:,length(date1)+1:length(date1)+length(date2)) = ncread(f2,char(vars(j)));
      clear date1 date2
    else 
      f = ['../' char(cases(i)) '/atm/proc/tseries/month_1/' char(cases(i)) '.cam.h0.' char(vars(j)) '.' char(dates(i)) '.nc']
      data(i,j).date = ncread(f,'date');
    %if(j>=length(vars)-1)
    %  xxx = ncread(f,char(vars(j)));
    %  data(i,j).value = squeeze(xxx(:,:,32,:));
    %  clear xxx
    %else
      data(i,j).value = ncread(f,char(vars(j)));
    %end
    end

    data(i,j).casename = char(cases(i));
    data(i,j).varname = char(vars(j));
    data(i,j).lon = ncread(f,'lon');
    data(i,j).lat = ncread(f,'lat');
    data(i,j).gw = ncread(f,'gw');
    [data(i,j).year data(i,j).month] = set_yr_mon(data(i,j).date);

    if( (j>=5)&&(j<=7) )
      data(i,j).value = data(i,j).value*1000*86400;
    end

    for n=1:length(data(i,j).date)
        data(i,j).global_avg(:,n) =  global_avg(squeeze(data(i,j).value(:,:,n)),data(i,j).gw,squeeze(data(i,1).value(:,:,n)));
    end 
    
    for m=1:3
       [data(i,j).annual_avg(m,:) data(i,j).annual_avg_year] = annual_avg_1d(squeeze(data(i,j).global_avg(m,:)),data(i,j).year,data(i,j).month);
    end
end
end




save('S5.mat','-v7.3')
