function clean_lmg_sst
% sstn=clean_lmg_sst tries to edit 
% basic sst file produced on LMG03-02. 
% 2/27/03

load lmg_met1m
N=length(sst);

% edit before yd = 55.6
m=find(t<=55.6);
M=length(m);
x=sst(m);
k=[m(end)+1:N]';
K=length(k);
xend=sst(k);

L=5;
xn=x;
nn=[L+1:M-L-1];
Nn=length(nn);
for n=1:Nn;
   ind=nn(n);
   xwin=x([ind-L:1:ind+L]');
   xwin=xwin(:);
   xmax=max(xwin);
   m=find(xwin>=xmax-0.1);
   xwinc=xwin(m);
   xn(ind)=meannan(xwinc(:));
end
sstnf=xn;

% edit end
L=5;
xn=xend;
nn=[L+1:K-L-1];
Nn=length(nn);
for n=1:Nn;
   ind=nn(n);
   xwin=xend([ind-L:1:ind+L]');
   xwin=xwin(:);
   xmin=min(xwin);
   m=find(xwin<=xmin +0.1);
   xwinc=xwin(m);
   xn(ind)=meannan(xwinc(:));
end
sstne=xn;

% merge front and back
sstn=[sstnf;sstne];

plot(t,sst,t,sstn,'r')

% save new sst back in lmg_met1m
save sstnew sstn
clear
load lmg_met1m
load sstnew
save lmg_met1m
