function  [startc,endc,starta,enda,dpdt]=MMP_align_ctdacm(cpres,vup,samplerate)

%cpres = ctd pressure series, vup = acm derived vertical velocity
%samplerate is for the CTD in scans per day
% startc and endc are the ctd scan numbers for the start and end of the profile
% starta and enda are those for the acm
%here start and end are where the profile speed first rises above and last
%falls below half the mean profile speed.
%dpdt is the time rate of change of the pressure, scaled to cm/s


%first compute dP/dt as centered difference using the supplied sample rate
 
dpdt=gradient(cpres)/(samplerate.*24*3600)*100.; 

%filter the dpdt and vz data to better find the start and stop transients

[b,a]=butter(4,.05);

filtcpres=myfiltfilt(b,a,dpdt(:)); 
filtw=myfiltfilt(b,a,vup(:));
dpdt=filtcpres;

%now find the starting and stopping transient in both the dpdt and vz data

pthresh=.5*mean(filtcpres);
paddresses=find(abs(filtcpres)<abs(pthresh));
dividepaddresses=find(diff(paddresses)>1);
startc=(paddresses(dividepaddresses(1)));
endc=(paddresses(dividepaddresses(length(dividepaddresses))+1));
athresh=.5*mean(filtw);
aaddresses=find(abs(filtw)<abs(athresh));
divideaaddresses=find(diff(aaddresses)>1);
starta=aaddresses(divideaaddresses(1));
enda=aaddresses(divideaaddresses(length(divideaaddresses))+1);
