% define desired region
region_name='global_4320';
nx=4320;

g = 9.806;
rho = 1027.5;
SEAICE_rhoIce = 910;
SEAICE_rhoSnow = 330;

%hamming12 =  [0.0097    0.0187    0.0438    0.0805    0.1192    0.1485    0.1594    0.1485    0.1192    0.0805    0.0438    0.0187 0.0097];
%hamming = hamming./sum(hamming);  %  not required as this filter sums to 1
 hamming =     [0.0021    0.0033    0.0063    0.0116    0.0195    0.0299    0.0423    0.0556    0.0686    0.0799    0.0883    0.0927 ...
    0.0927    0.0883    0.0799    0.0686    0.0556    0.0423    0.0299    0.0195    0.0116    0.0063    0.0033    0.0021];

% get and save daily-averaged regional fields
pin='~dmenemen/llc_4320/MITgcm/run/';
pout=['/nobackupp8/awinetee/' region_name '/'];
for fnm={'Eta'}
    eval(['mkdir ' pout fnm{1} '_seaIceRemoved_30hLPF_hamming_daily'])

    eval(['cd ' pout fnm{1} '_seaIceRemoved_30hLPF_hamming_daily'])
   
   
    for ts=10368:(144*24):485568 
        
            
        dy=ts2dte(ts,25,2011,9,10,29)      
                  
        fout=[fnm{1} '_seaIceRemoved_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];

       
        
        fld=0; n=0; fld2 = 0; pwr = 0; fld_mn = 0; fld_mn2 = 0; n2 = 0;
        
        for h=0:23 %, mydisp(h)
            fin=[pin fnm{1} '.' myint2str(ts+h*144,10) '.data'];
            
            if exist(fin)
                tmp = readbin(fin,[nx nx*13]);
                SIheff = readbin([pin 'SIheff.' myint2str(ts,10) '.data'],[nx,nx*13]);
                SIhsnow = readbin([pin 'SIhsnow.' myint2str(ts,10) '.data'],[nx,nx*13]);
            
                iceLoad = SIheff*SEAICE_rhoIce + SIhsnow*SEAICE_rhoSnow;
                
                ssh = tmp + iceLoad/rho;
                if sum(sum(tmp)) == 0
                    warning(['ZEROs FILE ' fin])
                    break
                else
                    fld=fld+ hamming(h+1)*ssh;
                    
                    n=n+1;
                end
            else
                warning(['MISSING FILE ' fin])
                break
            end

        end
        if n==24
            try
            writebin([pout fnm{1} '_seaIceRemoved_30hLPF_hamming_daily/' fout],fld);
            catch err
                warning(['file not written ' fout]);
                disp(err.message);
                
            end
            
        end
    end
end
