% detect_drogueoff
%
% 

if (~exist('id'))
    
    % load terms from terms_detect_drogueoff
    load terms_detect_drogueoff lon lat id t Wx Wy taux tauy drogue
    W=sqrt(Wx.^2+Wy.^2); clear Wx Wy;
    
    % load wind (Ekman) model calculated by calc_wind_model
    load windmodel_ralph alpha alpha2 

    % std dev. of alpha is very large for small wind speeds:
    %  For Rio-like model,
    %   7.82 for W<.05, 0.15 at W=1+-.05, 0.075 at W=2+-.05,
    %   0.05 at W=3+-.05, 0.015 at W=10+-.05, 0.011 at W=20+0.05
    %  For Ralph-like model,
    %	7.44 for W<.05, 0.15 at W=1+-.05, 0.076 at W=2+-.05,
    %   0.05 at W=3+-.05, 0.015 at W=10+-.05, 0.011 at W=20+0.05
    q=find(W<1.5);  % std(alpha) is 0.1 at this threshold
    alpha(q)=0;
    alpha2(q)=0;
    
    medt=median(diff(t)); 
    
    q=find(isnan(alpha));
    truncatedrift;
    
end;



% loop through, categorizing each drifter: compare what
%   alpha (residual) indicates, vs. what DAC has in metadata.
%
% Cat:
%   1=both agree: drogue off (unambiguous)  [DIRALL is OK]
%   2=both agree: drogue on until end or very near end   [DIRALL IS OK]
%   3=Doff from alpha, not derived from strain/subm
%       (at least not initially)  [DIRALL will have to be changed]
%   4=Doff doesn't match  [DIRALL may need to be changed]
%   5=No Doff from alpha, but Doff from strain/subm  [DIRALL OK]
%   6=record too short to tell
%   7=other / ambiguous
%   8=alpha ambiguous, Doff shown by strain/subm [DIRALL OK]
%   9=strain/subm ambiguous & alpha ambiguous 


if (~exist('ID'))
    makenew=input('Load "detect_results.mat" (1) or start over (2)? ');
    
    if (makenew==1)
        load detect_results
    else 
        disp('Creating ID, Cat, etc. with all NaNs')
        ID=unique(id);
        Cat=NaN*ones(size(ID));
        T=NaN*ones(length(ID),2);   % store start, end dates
        Doff=NaN*ones(length(ID),2);    % store drogue off dates 
            % Doff (:,1): Doff according to DAC.  Doff(:,2): from alpha
        Lat=Cat; Lon=Cat;   % store drogue off locations from alpha
        M=Cat;  % slope of int(alpha) (i.e. value of alpha)
    end;
end;

fprintf('Total number of drifters: %d\n',length(ID));
i1=input('Starting number for this run? ');
i2=input('Ending number for this run? ');
 
% note: for this routine, don't show the drogue status according
% to the DAC as this can affect interpetation: want this to be independent.
for i=i1:i2;
    
    q=find(id==ID(i));
    T(i,:)=[min(t(q)) max(t(q))];
    
    qq=find(drogue(q)==0);
    if (~isempty(qq)) 
        qq=qq(1); 
        Doff(i,1)=t(q(qq));
    end;
    
    figure(1);
    clf;
    subplot(311)
    plot_integrated_alpha(t(q),alpha(q),alpha2(q),drogue(q),W(q));
    xlim=get(gca,'xlim');
    title(num2str(id(q(1))),'fontsize',12);

    subplot(312)
    [str,ts]=load_strain(id(q(1)),t(q));
    plot(ts-ts(1),str,'.k','markersize',6);
    set(gca,'xlim',xlim);
    if (length(find(isfinite(str))>10))
        str2=sort(str(find(isfinite(str))));
        set(gca,'ylim',[0 1.25*str2(round(.95*length(str2)))+.1]);
    end;
    ylabel('subm/strain')

    subplot(313);
    plot(t(q)-t(q(1)),W(q)) 
    ylabel('Wind speed')

    figure(2);
    clf;
    q2=find(drogue(q)==1);
    q3=find(drogue(q)==0);
    q4=find(drogue(q)==.5);
    plot(lon(q),lat(q),'color',[.5 .5 .5]);
    hold on;
    plot(lon(q(q2)),lat(q(q2)),'.b',lon(q(q3)),lat(q(q3)),'.r');
    plot(lon(q(q4)),lat(q(q4)),'.','color',[.5 .5 .5]); 
    plot(lon(q(1)),lat(q(1)),'pk','markersize',10,'markerfacecolor','y');
    hold off;
    overlay;
    asprat;
    title(num2str(id(q(1))),'fontsize',12);

    txt=[num2str(i),' of ',num2str(length(ID)),': Cat? '];
    Cat(i)=input(txt);
        
    
    if (Cat(i)==3 || Cat(i)==4)  % determine Doff from alpha
        figure(1);
        subplot(311);
        hold on;
        [tdoff,m]=click_doff_time(t(q),alpha(q),alpha2(q), ...
            drogue(q),W(q));
        hold off;
        pause(2);
        
        Doff(i,2)=tdoff; 
        M(i)=m;
        qq=find(t(q)>tdoff);
        qq=qq(1);
        Lon(i)=lon(q(qq(1)));
        Lat(i)=lat(q(qq(1)));
    end;
        
end;

disp('Saving revised "detect_results.mat"');
save detect_results ID Cat T Doff Lat Lon M


