% This routine examines the files that are downloaded by the WGET process in "RawData_path", unzips them as needed into "Temp_path", % and then converts the data to matlab files which it places in "DecodedFiles_path".  The raw files are decoded anytime a decoded  % file doesn't already exist, or if the raw file is newer than an existing decoded file of the same name base (up to the ".csv"
% part of the raw filename.  If, a raw data file exists with a .gz extension, that file is used insted of any unzipped file of the
% same name since the zipped files will always be more complete than the unzipped versions due to how the data logger on the buoy
% controller works.

close all
clear all

base_path = getenv('BASE_PATH');
if(isempty(base_path))
    base_path = '../';
end

base_path

DecodedFiles_path = [base_path 'DecodedResults/'];
Temp_path = [base_path 'tmp/'];
RawData_path = [base_path 'buoy-sbc.shore.mbari.org/home/buoy/Logs/'];
QuickViewPlot_path = [base_path 'QuickViewPlots/'];
FTP_path = '/mbari/FTP/pub/Hamilton/'

if(~exist(Temp_path,'dir')) mkdir(Temp_path);  end
if(~exist(DecodedFiles_path,'dir')) mkdir(DecodedFiles_path);  end
if(~exist(QuickViewPlot_path,'dir')) mkdir(QuickViewPlot_path);  end

dataDirs = dir([RawData_path]);

for k = length(dataDirs):-1:1
    
    if((dataDirs(k).isdir == 1) && (dataDirs(k).name(1) == '2'))
        path = [RawData_path dataDirs(k).name '/'];
        disp(path);
        files = dir([path '2*']);
        for i = length(files):-1:1
            disp(files(i).name)
            
            %       idx = index(files(i).name,'.csv');
            %       jdx = index(files(i).name,'.gz');
            idx = strfind(files(i).name,'.csv');
            if(isempty(idx)) idx = 0; end
            if(length(idx) > 1) idx = idx(1); end

            jdx = strfind(files(i).name,'.gz');
            if(isempty(jdx)) jdx = 0; end
            if(length(jdx) > 1) jdx = jdx(1); end

            if(idx > 1)
                base_filename = files(i).name(1:idx-1);
            end
            
            
            %Decode only if raw file is newer that any of the resulting files, or if one of the resulting files doesn't exist.
            
            raw_filedatenum = files(i).datenum;
            
            mat_file = dir([DecodedFiles_path base_filename '.mat']);
            mat_filedatenum = 0; if(~isempty(mat_file)) mat_filedatenum = mat_file.datenum; end
            pdf_file = dir([QuickViewPlot_path base_filename '.pdf']);
            pdf_filedatenum = 0; if(~isempty(pdf_file)) pdf_filedatenum = pdf_file.datenum; end
            
            disp(['files(i).datenum = ' datestr(files(i).datenum)]);
            disp(['mat_filedatenum  = ' datestr(mat_filedatenum)]);
            if(min([mat_filedatenum pdf_filedatenum]) < files(i).datenum)
                % if(min([mat_filedatenum]) < files(i).datenum)
                
                
                %Process this file only if a valid .gz version doesn't exist in the raw data directory
                last_char = files(i).name(end);
                file_invalid = 1;
                if(last_char == 'v')
                    file_invalid = 0;
                else
                    cmd = ['gunzip -t ' path files(i).name]
                    file_invalid = unix(cmd);
                end
                disp(['file_invalid = ' num2str(file_invalid)]);
                
                if(~file_invalid)
                    
                    
                    
                    %Copy and unzip files if needed.
                    cmd = ['cp ' path files(i).name ' ' Temp_path files(i).name];
                    unix(cmd);
                    kdx = strfind(files(i).name,'.gz')
                    if(isempty(kdx)) kdx = 0; end
                    if(length(kdx) > 1) jdx = kdx(1); end
                    if(kdx)  %Unzip if it's a .gz file
                        cmd = ['gunzip -f ' Temp_path files(i).name];
                        unix(cmd);
                    end
                    
                    %Decode and save as matlab data.
                    MatlabFilenames = ConvertDataToMatlab(Temp_path,base_filename);
                    
                    %Make QuickView Plot for telemetry
                    MakeQuickViewPlots(Temp_path,base_filename);
		   %Send Lat/Lon to ODSS
		   SendDataToODSS(Temp_path,base_filename);
                    
                    cmd = ['mv ' Temp_path base_filename '*.mat ' DecodedFiles_path '.'];
                    unix(cmd);
                    
                    
                    cmd = ['mv ' Temp_path base_filename '*.pdf ' QuickViewPlot_path '.'];
                    unix(cmd);
                    
                    %Copy most recent QuickViewPlot to FTP  % Relies on files being listed alphanumerically with time...
                    plotDir = dir(QuickViewPlot_path);
                    if(length(plotDir) >= 3)
                        if(strcmp(plotDir(end).name(end-3:end),'.pdf')) %Copy if last file is a .pdf.
                            cmd = ['cp ' QuickViewPlot_path plotDir(end).name ' ' FTP_path 'LatestQuickViewPlot.pdf']
                            unix(cmd);
                        end
                    end
                    
                    cmd = ['rm ' Temp_path base_filename '.csv*'];
                    unix(cmd);
                    
                end
                
            end
        end
    end
    
end




exit  %End matlab after running (for use from command line)






