clear;

files = {'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2010\2010_10_12_BloomEX_24'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2010\2010_10_18_BloomEX_25'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2011\2011_04_15_CANON_31'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2011\2011_04_26_CANON_32'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2011\2011_04_26_CANON_33'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2011\2011_05_10_Turbulence_34'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2011\2011_09_06_CANON_41'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2011\2011_09_26_WF_Deployment_42'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_05_29_Daphne_CANON_4'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_06_05_Tethys_CANON_64'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_09_06_Tethys_CANON_67'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_09_10_Daphne_CANON_08'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_10_18_Tethys_Turbulence_69'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_10_25_Daphne_Turbulence_11'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_10_25_Daphne_Turbulence_11'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2012\2012_11_12_Daphne_PatchTracking_12'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2013\2013_03_12_Daphne_EcoHAB_15'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2013\2013_03_12_Tethys_ECOHAB_71'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2013\2013_04_02_Daphne_Compact_Models_16'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2013\2013_05_29_Tethys_2DFront_73'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2013\2013_09_09_Tethys_CANON_ESP_75'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2013\2013_09_13_Daphne_CANON_LST_24'
    ,'\\atlas.shore.mbari.org\ProjectLibrary\900739.LRAUV.Data\Testing\2013\2013_09_20_Tethys_CANON_ESP_76'}

missions_to_process = [6];

for mission_index = 1:length(missions_to_process)
    
    which_mission = missions_to_process(mission_index);
    
    % Point to the director with Logs...
    cd([files{which_mission} '\Logs'])
    
    % Make one big data mashup for the mission
    
    %------------------------------------------------------------------
    blew_up = 0;  % make marker in case something goes wrong...
    try LR_Concat_DataServer_Slate;
    catch ME
        % If thread is here, there was a problem reading the slate...go on
        % try next one, if there is a next one.
        disp('Error LRAUV_Batch_Processing.2: LR_Concat_DataServer_Slate');
        disp(ME.message);
        blew_up = 1;
    end;
    
    %------------------------------------------------------------------
    % If there was no error reading the slate...have some stuff to do
    if blew_up == 0
        % ------- Make name for concatenated file...
        tran = files{which_mission};
        n1 = length(tran);
        n2 = strfind(tran, '\');
        n2 = n2(length(n2));
        
        % ---------------------------------------------------------------
        % ------- Save results on server
        % Save backup copy of concatentated mission in Logs directory
        cd([files{which_mission} '\Logs'])
        save('backup'); % Just in case....
        concatenated_name = [tran(n2+1:n1) '_CommonTime.mat']
        
        % Write concatenated variables to file on server
        if ~exist(concatenated_name, 'file');  % If file already exists, do not 'refresh'...assume time already in there...
            ct = ct';
            save(concatenated_name, 'ct');
        end;
        
        for variable_index=1:length(from_to)
            try eval([from_to{variable_index,2} '=' from_to{variable_index,2} 'char(39);']); end;
            try save(concatenated_name, from_to{variable_index,2}, '-append'); catch; end;
        end;
        
        try cn = cn'; end;
        try save(concatenated_name, cn, '-append'); catch; end; % Save buoyancy freq if calculated...
        
        % ------- Save the mashup in JGBs folder...
        cd('C:\Users\jgb\Documents\MATLAB\FieldOps');
        
        if ~exist(concatenated_name, 'file');  % If file already exists, do not 'refresh'...assume time already in there...
            ct = ct';
            save(concatenated_name, 'ct');
        end;
        
        % Append concatenated variables
        for variable_index=1:length(from_to)
            try eval([from_to{variable_index,2} '=' from_to{variable_index,2} 'char(39);']); end;
            try save(concatenated_name, from_to{variable_index,2}, '-append'); catch; end;
        end;
        
        try cn = cn'; end;
        try save(concatenated_name, cn, '-append'); catch; end; % Save buoyancy freq if calculated...
    end;
end;