
% loaddirfile.m
%
% load an ASCII directory file "dirname" into Matlab.
% First, manually strip the header line.
% 
%  If the header file has dates
% in the format MONTH- DAY- YEAR, use "nedit" to
% convert "- " to "  ".  (don't just remove "-", as
% there are negative numbers such as lats)
%
% Death types:  (% of death>0 from S.Atl./N.Atl obs)
%	1: ran aground                  (12% / 18%)
%	2: picked up                    (7% / 9%)
%	3. quit transmitting            (77% / 69%)
%	4. unreliable transmissions     (0.8% / 1.3%)
%	5. bad battery voltage          (0.8% / 0.8%)
%	6. inactive status              (0.6% / 1.4%)
%	0. still active

clear all;
%filename='n_atlantic_prelowpass';
dirname='direc_13jan04';

%fprintf('Loading file ... ');
%eval(['load ',filename,' id lon lat t drogue']);
%clear filename

% load the direc file 
if (0)  % header: ID WMO EXP TYP Depl.TIME Depl.LAT Depl.LON 
        %       E.TIME DRO.OFF DEATH
    eval(['x=textread(''',dirname,'.dat'');']);
    dir_id=x(:,1);
    dir_type=x(:,4);
    dir_deploy_t=x(:,5);
    dir_deploy_lat=x(:,6);
    dir_deploy_lon=x(:,7);
    dir_death_t=x(:,8);
    dir_drglst_t=x(:,9);
    dir_death=x(:,10);

    q=find(dir_drglst_t==0);
    dir_drglst_t(q)=9e5;
    clear x;
elseif (0)    % header: ID WMO EXP DEP.DATE(M D Y) 
    % DEP.LAT DEP.LON END.DATE(M D Y) 
    % DROGOFFDATE(M D Y) DEATH
    eval(['x=textread(''',dirname,'.dat'');']);
    dir_id=x(:,1);
    dir_deploy_t=juliand([x(:,6) x(:,4) x(:,5)]);
    dir_deploy_lat=x(:,7);
    dir_deploy_lon=x(:,8);
    dir_death_t=juliand([x(:,11) x(:,9) x(:,10)]);
    dir_drglst_t=juliand([x(:,14) x(:,12) x(:,13)]);
    dir_death=x(:,15);

    q=find(dir_drglst_t==juliand([0 0 0]));
    dir_drglst_t(q)=9e5;
    clear x;
else
    % header: ID WMO EXPNO TYPE 
    % DEP.DATE(M/D/Y) DEP.TIME (HH:MM) DEP.LAT DEP.LON 
    % END.DATE(M/D/Y) END.TIME (HH:MM) END.LAT END.LON
    % DROGOFFDATE(M/D/Y) DROGOFFTIME (HH:MM) DEATH
    eval(['[dir_id,dir_wmo,dir_expno,dir_type,', ...
            'dir_deploy_t,dir_deploy_t2,dir_deploy_lat,', ...
            'dir_deploy_lon,dir_death_t,dir_death_t2,', ...
            'dir_death_lat,dir_death_lon,dir_drglst_t,', ...
            'dir_drglst_t2,dir_death]=textread(''',dirname, ...
            '.dat'',''%d%d%d%s%s%s%f%f%s%s%f%f%s%s%d'');']);

    % convert dates from format YY/MM/DD (which MUST
    % include placeholder 0, e.g. 1999/07/03)
    % to julian drifter days
    time=NaN*ones(length(dir_deploy_t),1);
    for i=1:length(time);
        t=char(dir_deploy_t(i));
        t=[str2num(t(1:4)) str2num(t(6:7)) str2num(t(9:10))];
        time(i)=juliand(t);
    end;
    dir_deploy_t=time;clear time dir_deploy_t2;
    time=NaN*ones(length(dir_death_t),1);
    for i=1:length(time);
        t=char(dir_death_t(i));
        t=[str2num(t(1:4)) str2num(t(6:7)) str2num(t(9:10))];
        time(i)=juliand(t);
    end;
    dir_death_t=time;clear time dir_death_t2;
    time=NaN*ones(length(dir_drglst_t),1);
    for i=1:length(time);
        t=char(dir_drglst_t(i));
        t=[str2num(t(1:4)) str2num(t(6:7)) str2num(t(9:10))];
        time(i)=juliand(t);
    end;
    dir_drglst_t=time;clear time dir_drglst_t2;
    
end

q=find(dir_deploy_lon>180);
dir_deploy_lon(q)=dir_deploy_lon(q)-360;

% dir_drglst_t is set to 1979/01/01 if the
% drogue has been off since deployment
q=find(dir_drglst_t==juliand([1979 1 1]));
dir_drglst_t(q)=dir_deploy_t(q);
% dir_drglst_t is set to 0000/00/00 if the
% drogue is still on at time of update
q=find(dir_drglst_t==juliand([0 0 0]));
dir_drglst_t(q)=NaN;

% dir_death_t is actually the last time of that
% drifter in the record: where the drifter hasn't
% died (dir_death==0), set dir_death_t to 9e5
q=find(dir_death==0);
dir_death_t(q)=NaN;

% at this point, an individual drifter's trajectory
% including deployment, end_t and drogue lost time can
% be plotted with the script plotdirinfo.m

return;







% calculate the lon, lat of droguelost and of death
dir_drglst_lon=NaN*ones(size(dir_id));
dir_drglst_lat=dir_drglst_lon;
dir_death_lon=dir_drglst_lon;
dir_death_lat=dir_drglst_lat;

for dd=1:length(dir_id);
    q=find(id==dir_id(dd));
    
    if (dir_drglst_t(dd)==9e5);      % drogue still attached
                                    % at record end;
        dir_drglst_lon(dd)=NaN;
        dir_drglst_lat(dd)=NaN;
    elseif (dir_drglst_t(dd)==1 | ...
            (dir_drglst_t(dd)==dir_deploy_t(dd)));    
        % drogue status uncertain from deployment
        dir_drglst_lon(dd)=dir_deploy_lon(dd);
        dir_drglst_lat(dd)=dir_deploy_lat(dd);
    else
        ind=find(t(q)==dir_drglst_t(dd));
        if (length(ind))
            dir_drglst_lon(dd)=lon(q(ind));
            dir_drglst_lat(dd)=lat(q(ind));
        else % droge lost outside "filename"'s domain
            dir_drglst_lon(dd)=999;
            dir_drglst_lat(dd)=999;
        end;
    end;
    if (dir_drglst_t(dd)==9e5);      % drogue still alive
        dir_death_lon(dd)=NaN;
        dir_death_lat(dd)=NaN;
    else
        ind=find(t(q)>=dir_death_t(dd)-1);
        if (length(ind))
          dir_death_lon(dd)=lon(last(q(ind)));
          dir_death_lat(dd)=lat(last(q(ind)));
        else % death happened outside "filename"'s domain
            dir_death_lon(dd)=999;
            dir_death_lat(dd)=999;
        end;
    end;
end;
clear q dd ind;
    

% calculate the spatial distribution of
% where drogues were lost and where drifters died;
% for comparison, also calculate observation density
% (units: drifter years in bin)
del=10;delt=median(diff(t));
if strcmp(dirname,'dirfl_s_atlantic')
    Lon=-70+del/2:del:25-del/2;
    Lat=-70+del/2:del:0-del/2;
elseif strcmp(dirname,'dirfl_n_atlantic')
    Lon=-100+del/2:del:10-del/2;
    Lat=0+del/2:del:66-del/2;
else
    keyboard;
end;
Drglst=NaN*ones(length(Lat),length(Lon));
Death=Drglst; NumObs=Drglst;
for i=1:length(Lat);
    q=find(lat>Lat(i)-del/2 & lat<Lat(i)+del/2);
    qd=find(dir_drglst_lat>Lat(i)-del/2 & ...
        dir_drglst_lat<Lat(i)+del/2);
    qe=find(dir_death_lat>Lat(i)-del/2 & ...
        dir_death_lat<Lat(i)+del/2);
    for j=1:length(Lon);
        if (length(q))
            q2=find(lon(q)>Lon(j)-del/2 & ...
                lon(q)<Lon(j)+del/2);
            NumObs(i,j)=length(q2)*delt/365.24;
        end;
        if (length(qd))
            q2=find(dir_drglst_lon(qd)>Lon(j)-del/2 & ...
                dir_drglst_lon(qd)<Lon(j)+del/2);
            Drglst(i,j)=length(q2);
        end;
        if (length(qe))
            q2=find(dir_death_lon(qe)>Lon(j)-del/2 & ...
                dir_death_lon(qe)<Lon(j)+del/2);
            Death(i,j)=length(q2);
        end;
    end;    % loop over Lon
end;    % loop over Lat
q=find(isnan(Death));Death(q)=0;
q=find(isnan(Drglst));Drglst(q)=0;
q=find(NumObs<30/365.24);NumObs(q)=NaN;
        
clear del q qd qe q2 i j delt
clear lat lon t id drogue
eval(['save ',dirname]);
