function clean_lmg_sstA
% clean_lmg_sstA tries to edit 
% basic sst file produced on LMG03-02. 
% 2/27/03; 3/4/03

load lmg_met1m
N=length(sst);

% select pieces
% A: before yd = 55.6
m=find(t<=55.6);
sstA=sst(m);
tA=t(m);
% B 55.6 to 58
m=find(t>55.6 & t<58);
sstB=sst(m);
tB=t(m);
% C rest of record
m=find(t>=58);
sstC=sst(m);
tC=t(m);

% clean piece A
x=sstA;
M=length(x);
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
sstAn=xn;

% clean B
x=sstB;
M=length(x);
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(:);
   xmin=min(xwin);
   m=find(xwin<=xmin +0.1);
   xwinc=xwin(m);
   xn(ind)=meannan(xwinc(:));
end
sstBn=xn;

% clean piece C
x=sstC;
M=length(x);
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
sstCn=xn;
m=find(sstCn==0);
sstCn(m)=nan;
m=find(tC>=60.69 & tC<=60.75);
sstCn(m)=nan;
sstCn=gapfil(sstCn);


% merge pieces
sstn=[sstAn;sstBn;sstCn];
tm=[tA;tB;tC];

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

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