close all
clear all
clc
thisDir = fileparts( mfilename('fullpath') );

%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Basic info on the Total files.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
conf = MABSB_conf;

% time period
startdate=datenum(2013,04,27,18,0,0);
finaldate = datenum(2013,04,27,18,0,0);

dtime = startdate:6/24:finaldate;

% Generate all filenames at once using repmat appropriately
fnames=datenum_to_directory_filename(conf.Traj.BaseDir,dtime,...
    conf.Traj.FilePrefix,conf.Traj.FileSuffix,conf.Traj.MonthFlag);

for i = 1:length(fnames)
    
    Date = dtime(i);
    
    fprintf('******* Current time: %s  === Processing data time: %s \n', ...
        datestr(now,0), datestr(Date,0));
    
    % LCS stuff
    try
        
        % This calculates LCS
        
        % load the trajectory data
        filename=fnames{i};
        load(filename)
        
        % default variables
        startFrame = 1;
        finalFrame = size(TRAJ.TimeStamp,2);
        o = [1,0]; % direction [1,0]
        
        % convert from degree to kilometer
        % define an arbitrary reference origin
        lon_orig=TRAJ.InitialLonLat(1,1);
        lat_orig=TRAJ.InitialLonLat(1,2);
        
        [east, north] = lonlat2km(lon_orig,lat_orig,TRAJ.Lon,TRAJ.Lat);
        
        [...
            AD,...
            D,...
            MAD,...
            XMaxD,...
            XMinD,...
            XMAD,...
            XMean,...
            YMaxD,...
            YMinD,...
            YMAD,...
            YMean,...
            XYMaxD,...
            XYMinD,...
            XYMAD,...
            XYMean] = compute_met(...
            east,...
            north,...
            TRAJ.TimeStamp,...
            'startFrame',startFrame,...
            'finalFrame',finalFrame,...
            'direction',o);
        
        % set the last frame
        EAST = east(:,end);
        NORTH = north(:,end);
        
        % Relative Dispersion
        % generates a rectangular grid
        [ln,lt] = meshgrid_vector_data(TRAJ.InitialLonLat(:,1),TRAJ.InitialLonLat(:,2));
        [d1,d2] = size(ln);

        EAST = reshape(EAST,d1,d2);
        NORTH = reshape(NORTH,d1,d2);
        % convert from degree to kilometer
        RD = compute_rd(EAST,NORTH);
         
        InitialLonLat=TRAJ.InitialLonLat;
        TimeStamp=TRAJ.TimeStamp(end);
        AD=AD(:,end);
        MAD=MAD(:,end);
        XMAD=XMAD(:,end);
        YMAD=YMAD(:,end);
        RD=RD(:);
        

    catch
        fprintf(' LCS calculation failed because:\n')
        res = lasterror;
        fprintf('%s\n',res.message)
    end
    
    % Saving LCS
    try
        if ~exist( conf.LCS.BaseDir, 'dir' )
            fprintf('%s is creating the directory:\n%s\n',mfilename,conf.LCS.BaseDir);
            mkdir(conf.LCS.BaseDir);
        end
        
        filename = fullfile(...
            conf.LCS.BaseDir,...
            strcat(conf.LCS.FilePrefix,...
            datestr(Date,'yyyy_mm_dd_HHMM'),...
            conf.LCS.FileSuffix));
        save(filename,'AD','MAD','XMAD','YMAD','RD','InitialLonLat','TimeStamp');
    catch
        fprintf('Saving LCS failed because:\n')
        res = lasterror;
        fprintf('%s\n',res.message)
    end
    
% %     % Plotting
% %     try
% % figure
% % for iz=1:nelev
% % lcs = reshape(rd(:,iz),d1,d2);
% % surf(ln,lt,0.*lcs+elevation(iz),lcs)
% % hold on
% % end
% % zlim([1 elevation(nelev)])
% % grid on
% % shading interp
% % zlabel('depth')
% % view(-20,10)
% % set(gca,'ztick',0:500:elevation(nelev))
% % ylabel('latitude (^o)');
% % xlabel('longitude (^o)');
% % zlabel('depth m')
% %
% %     catch
% %         fprintf('plot LCS failed because:\n')
% %         res = lasterror;
% %         fprintf('%s\n',res.message)
% %     end

end    