function concatenate_files(dataDirectory, listFolder)
%   concatenate_files(dataDirectory, listFolder)
%
%   Function to take the excel spreadsheets of files to use for the
%   concatenation of MBLT and create a Matlab file containing the
%   concatenated data.  Data is in file is named SITE_VARIABLE_DEPTH_data, with
%   corresponding time vector SITE_VARIABLE_DEPTH_time.  Low-pass filtered
%   versions are also created (individual file data is low-passed prior to
%   concatenation), with _LP as an extension on the file name.  File
%   assumes that within the dataDirectory are two subfolders (MBLT_A and
%   MBLT_B) with the data from each site.
%
%   File saves the output into the dataDirectory.

%P. Soupy Dalyander, 08/15/2008

%Move into the data directory
cd(dataDirectory)

siteList = {'A'; 'B'};

%Lists of sites/depths/variables
depthsA = {'4-10 m'; '10 mab'; '1-2 mab'};
tagsA = {'S'; 'M'; 'D'};
depthsB = {'10 mab'; '1-2 mab'};
tagsB = {'M'; 'D'};
variableList = {'salinity'; 'temperature'; 'current'; 'pressure'; 'sdp'; 'attenuation'};

%Do the concatenation
for index = 1:2
    thisSite = siteList{index};
    eval(['depthList = depths' thisSite ';'])
    for index2 = 1:length(variableList)
        thisVar = variableList{index2};
        if (strcmp(thisVar, 'current') | strcmp(thisVar, 'pressure') | ...
                strcmp(thisVar, 'sdp')) & strcmp(thisSite, 'B')
            continue
        elseif strcmp(thisVar, 'current') & strcmp(thisSite, 'A')
            for index3 = 1:length(depthList)
                eval(['thisDepth = tags' thisSite '{index3};']);
                [nums, text] = xlsread(fullfile(listFolder, ...
                    [thisVar thisSite '_c.xls']), depthList{index3});
                text2 = text(2:end,:);
                text3 = text(1,:);
                varCol = strmatch('Target', text3);
                goods = find(nums(:,1)==1);
                fileList = text2(goods,1);
                varList = text2(goods,varCol);
                allTime = [];
                allU = [];
                allV = [];
                allU_lp = [];
                allV_lp = [];
                for index4 = 1:length(fileList)
                    thisFile = fileList{index4};
                    ncID = netcdf([dataDirectory '\MBLT_' thisSite '\' thisFile], 'nowrite');
                    fileTime = singlejd(ncID{'time'}(:), ncID{'time2'}(:));
                    fileDataU = ncID{'u_1205'}(:);
                    fileDataV = ncID{'v_1206'}(:);
                    if isempty(fileDataU) | isempty(fileDataV)
                        error('hit empty')
                    end
                    allTime = [allTime(:); fileTime(:)];
                    allU = [allU(:); fileDataU(:)];
                    allV = [allV(:); fileDataV(:)];
                    [fileDataU_lp, fileTime_lp] = plfilt(fileDataU, fileTime, 6);
                    fileDataU_lp(end) = NaN;
                    [fileDataV_lp, fileTime_lp] = plfilt(fileDataV, fileTime, 6);
                    fileDataV_lp(end) = NaN;
                    allTimeLP = [allTimeLP(:); fileTimeLP(:)];
                    allU_lp = [allU_lp(:); fileDataU_lp(:)];
                    allV_lp = [allV_lp(:); fileDataV_lp(:)];
                    close(ncID)
                    clear ncID

                end
                bads = find((allU+allV) > 1e3);
                allU(bads) = NaN;
                allV(bads) = NaN;
                timeName = ['A_current_' thisDepth '_time'];
                dataNameU = ['A_currentU_' thisDepth '_data'];
                dataNameV = ['A_currentV_' thisDepth '_data'];
                
                timeNameLP = ['A_current_' thisDepth '_time_LP'];
                dataNameULP = ['A_currentU_' thisDepth '_data_LP'];
                dataNameVLP = ['A_currentV_' thisDepth '_data_LP'];

                eval([timeName '= allTime;']);
                eval([dataNameU '= allU;']);
                eval([dataNameV '= allV;']);
                eval([dataNameULP '= allU_lp;']);
                eval([dataNameVLP '= allV_lp;']);

                continue
            end
        end
        for index3 = 1:length(depthList)
            eval(['thisDepth = tags' thisSite '{index3};']);
            if ((strcmp(thisVar, 'pressure') | strcmp(thisVar, 'sdp')) & ...
                    (strcmp(thisDepth, 'M') | strcmp(thisDepth, 'S'))) | ...
                    (strcmp(thisSite, 'B') & strcmp(thisVar, 'attenuation') & ...
                    strcmp(thisDepth, 'M'))
                continue
            end
            [nums, text] = xlsread(fullfile(listFolder, ...
                [thisVar thisSite '_c.xls']), depthList{index3});
            text2 = text(2:end,:);
            text3 = text(1,:);
            varCol = strmatch('Target', text3);
            goods = find(nums(:,1)==1);
            fileList = text2(goods,1);
            varList = text2(goods,varCol);
            allTime = [];
            allData = [];
            allTimeLP = [];
            allDataLP = [];
            for index4 = 1:length(fileList)
                thisFile = fileList{index4};
                fileVar = varList{index4};
                theCol = strfind(fileVar, ';');
                fileVar = fileVar(1:theCol-1);
                if strcmp(thisVar, 'sdp')
                    fileVar = 'SDP_850';
                elseif strcmp(thisVar, 'pressure')
                    fileVar = 'P_4023';
                elseif strcmp(thisVar, 'attenuation')
                    fileVar = 'ATTN_55';
                end
                ncID = netcdf([dataDirectory '\MBLT_' thisSite '\' thisFile], 'nowrite');
                fileTime = singlejd(ncID{'time'}(:), ncID{'time2'}(:));
                eval(['fileData = ncID{ ''' fileVar '''}(:);']);
                if isempty(fileData) & strcmp(thisVar, 'pressure')
                    fileData = ncID{'P_4022'}(:);
                    fileVar = 'P_4022';
                end
                if isempty(fileData) & strcmp(thisVar, 'pressure')
                    fileData = ncID{'P_1'}(:);
                    fileVar = 'P_1';
                end
                if isempty(fileData) & strcmp(thisVar, 'attenuation')
                    fileData = ncID{'ATTN1_55'}(:);
                    fileVar = 'ATTN1_55';
                end
                if isempty(fileData) & strcmp(thisVar, 'attenuation')
                    fileData = ncID{'ATTN2_55'}(:);
                    fileVar = 'ATTN2_55';
                end
                
                if strcmp(thisVar, 'pressure') & strcmp(thisFile, '7173advs-calq.cdf');
                    stopTime = value2index(fileTime, julian(2004, 02, 05, 00));
                    fileTime(stopTime:end) = [];
                    fileData(stopTime:end) = [];
                end
                
                if isempty(fileData)
                    error('hit empty')
                end
                bads = find(fileData > 1e9);
                fileData(bads) = NaN;
                fileData(end) = NaN;
                if strcmp(thisVar, 'pressure')
                    fileData = fileData - nanmean(fileData);
                    if strcmp(fileVar, 'P_1')
                        fileData = fileData*100;
                    end
                end
                [fileDataLP, fileTimeLP] = plfilt(fileData, fileTime, 6);
                fileDataLP(end) = NaN;
                allTime = [allTime(:); fileTime(:)];
                allData = [allData(:); fileData(:)];
                allTimeLP = [allTimeLP(:); fileTimeLP(:)];
                allDataLP = [allDataLP(:); fileDataLP(:)];
                close(ncID)
                clear ncID
            end
            timeName = [thisSite '_' thisVar '_' thisDepth '_time'];
            dataName = [thisSite '_' thisVar '_' thisDepth '_data'];
            timeNameLP = [thisSite '_' thisVar '_' thisDepth '_time_LP'];
            dataNameLP = [thisSite '_' thisVar '_' thisDepth '_data_LP'];

            eval([timeName '= allTime;']);
            eval([dataName '= allData;']);
            eval([timeNameLP '= allTimeLP;']);
            eval([dataNameLP '= allDataLP;']);

        end
    end
end

%Save the output
save(fullfile(dataDirectory, 'concat_data.mat'), '*_*')
            