%fix_c_small
%routine to interpolate over short segments of bad conductivity data

stas=input('Which stations do you want to process?  ');
 [directory]=input('Enter the directory with the input grd mat file:  ','s');
 [outraw]=input('Now enter the directory where you want the new raw mat file to go:  ','s');

fignum=input('Enter min figure # to use for this process:   ');

for i=1:length(stas)

matfmtfile=[directory,'/grd',sprintf('%4.4i',stas(i)),'.mat'];
newfile=[outraw,'/grd',sprintf('%4.4i',stas(i)),'.mat'];

disp(['   working on file ',matfmtfile]);

if(exist(matfmtfile)==2)

eval(['load ',matfmtfile]);

disp([' ']);
disp([' Zoom in to find the scan numbers bracketing the spike ']);
disp([' ']);
figure(fignum)
clf
plot(cave)
grid
zoom on
hold on
title('Conductivity');

figure(fignum+1)
clf
plot(s_ave)
grid
zoom on
hold on
title('Salinity')


jstart=input(' Enter the scan number just prior to the C spike ');
jstop=input('   Now enter the first good C scan no. after the spike ');

x=[tave(jstart) tave(jstop)];
y=[cave(jstart) cave(jstop)];
coefs=polyfit(x,y,1);
z=polyval(coefs,tave);

cave(jstart+1:jstop-1)=z(jstart+1:jstop-1);
CR=cave/sw_c3515.*(1-(6.5e-6).*(tave-2.) + (1.5e-8).*(pave-2000.));
salt=sw_salt(CR,tave,pave);
s_ave(jstart+1:jstop-1)=salt(jstart+1:jstop-1);
thetave=sw_ptmp(s_ave,tave,pave,0);
sigthave=sw_pden(s_ave,tave,pave,0)-1000.;


figure(fignum)
plot([jstart:jstop],cave(jstart:jstop),'r');
figure(fignum+1)
plot([jstart:jstop],s_ave(jstart:jstop),'r');
pause
eval(['save ',newfile,' ascan1 file pstop thetave ascan2 pave s_ave ', ...
'uave cave pedate sigthave vave cscan1 pgrid startdaytime wave cscan2 ', ...
'psdate stopdaytime dpdtave pstart tave']);
         


end; %if file exists
end; %i loop 

