%function tf_float = Merge_dura_msgs(MBARI_ID_str, dirs)
% ************************************************************************
% PURPOSE:
%    This function processes raw message files for a given APEX float
%    (.msg, .isus, .dura), calculates useful values from the raw signals
%    (P, T, S, O2, NO3, pH, CHl, Bbp, CDOM) and then merges these results
%    for a given profile.
%
%    Each profile is saved as a *.mat file with the following format:
%    WMO_ID.PROFILE_#.mat (Ex 5904683.006.mat) in a directory using the
%    WMO_ID for its name. Each file contains 3 structures with variables
%    inside named using ARGO definitions:
%       LR   - Low resolution data
%       HR   - High resolution data
%       INFO - Some information about the profile and float
%
% USAGE:
%	tf_float = Process_APEX_float(UW_ID_str, dirs, update_str)
%
% INPUTS:
%   UW_ID_str  = UW/MBARI Float ID # as a string
%
%   update_str = "all" or "update"
%                 all    - to process all available msg files
%                 update - only process new msg files
%
%   dirs       = Either an empty variable ora structure with directory
%                strings where files are located. It must contain these
%                fields at a minimum or be empty. If dirs is empty default
%                paths will be used.
%
% OUTPUTS:
%   tf_float =  1 if processing was a success, 0 otherwise
%
plot_flag = 1;
% ************************************************************************
% FOR TESTING
%UW_ID_str  = '12549SOOCN';
%UW_ID_str  = '12537SOOCN';
%UW_ID_str  = '12540SOOCN';
%UW_ID_str  = '12739SOOCN';
%UW_ID_str  = '12388SOOCN';
%UW_ID_str  = '12781SOOCN';
%MBARI_ID_str  = '9265SOOCN';
%UW_ID_str  = '9652SOOCN';
%UW_ID_str  = '12386SOOCN';
%UW_ID_str  = '12708SOOCN';
%UW_ID_str  = '12787SOOCN';
%UW_ID_str  = '12727SOOCN';
%UW_ID_str  = '12786SOOCN';
% UW_ID_str  = '12892SOOCN';
% 
% UW_ID_str  = '12688SOOCN';
% UW_ID_str  = '9757SOOCN';
% UW_ID_str  = '12711SOOCN';
% UW_ID_str  = '12701SOOCN';
% UW_ID_str  = '18864SOOCN';
%  UW_ID_str  = '18994SOOCN';
% MBARI_ID_str  = '8374HAWAII';
% MBARI_ID_str  = '9254SOOCN';
%MBARI_ID_str  = '8501CALCURRENT';
% MBARI_ID_str  = '12783EQPACW';
% MBARI_ID_str  = '17307HOT';
%MBARI_ID_str  = '18721SOOCN';
%MBARI_ID_str  = '18821SOOCN';
MBARI_ID_str  = '18340CalCurrent';
dirs =[];

% ************************************************************************
% SET FORMATS, DEFAULT DIRS, PREDIMENSION STRUCTURE, BOOKKEEPING
% ************************************************************************
fclose all; % CLOSE ANY OPEN FILES
tf_float = 0; % Default flag for float processing success 0 = no good
MVI_str = '-1e10';

% **** DEFAULT STRUCTURE FOR DIRECTORY PATHS ****
if isempty(dirs)
    dirs.msg       = 'C:\Users\gene\Documents\pH2020\Calcs\ChemWebData\floats\';
    dirs.mat       = 'C:\Users\gene\Documents\pH2020\Calcs\MATLAB\ARGO\DURA\';
    dirs.temp      = 'C:\temp\';
    dirs.FV        = 'C:\Users\gene\Documents\pH2020\Calcs\MATLAB\ARGO\FV\';
elseif ~isstruct(dirs)
    disp('Check "dirs" input. Must be an empty variable or a structure')
    return
end
%INFO = dirs.INFO;

     
% ************************************************************************
% GET PH INFo FROM EXCEL FILE (MSC# VERSION #,



% ************************************************************************
% BUILD *.DURA LIST
% ************************************************************************
dlist = get_msg_list(MBARI_ID_str, dirs,'dura'); % isus file list

% ************************************************************************
% COPY FLOAT MESSAGE FILES TO LOCAL TEMPORARY DIRECTORY
% Three file groups *.msg, *.isus, *.dura
% ************************************************************************
if isempty(dlist)
    disp(['No float dura files found to process for float ',MBARI_ID_str]);
    return
elseif isempty(dlist.reg_list) && isempty(dlist.alt_list)
    disp(['No float dura files found to process for float ',MBARI_ID_str]);
    return
else
    disp(['Copying dura message files to ', dirs.temp, '  .......'])
    % CHECK FLOAT TYPE
    if regexp(dlist.reg_dir,'floats\\f','once') % APEX
        float_type = 'APEX';
        disp([MBARI_ID_str,' is an APEX float'])
    end
    
    % COPY *.dura files
    if ~isempty(ls([dirs.temp,'*.dura']))
        delete([dirs.temp,'*.dura']) % Clear any message files in temp dir
    end
    if ~isempty(dlist)
        if ~isempty(dlist.reg_list)
            for i = 1:length(dlist.reg_list)
                copyfile([dlist.reg_dir, dlist.reg_list{1,i}],dirs.temp);
            end
        end
        if ~isempty(dlist.alt_list)
            for i = 1:length(dlist.alt_list)
                copyfile([dlist.alt_dir, dlist.alt_list{1,i}],dirs.temp);
            end
        end
        clear dlist i
    end
end

% ************************************************************************
% PROCESS APEX MESSAGE FILES FOR GIVEN FLOAT
% ************************************************************************
msg_list   = ls([dirs.temp,'*.dura']); % get list of file names to process

disp(['Processing ARGO float ',MBARI_ID_str, '.........'])

merge_pH =[];
for msg_ct = 1:size(msg_list,1)
    pH_file = strtrim(msg_list(msg_ct,:));
    % find block of numbers then look ahead to see if '.dura' follows
    cast_num = regexp(pH_file,'\d+(?=\.dura)','once','match');
    cast_num = str2double(cast_num);
    
    % ****************************************************************
    % CALCULATE pH (µmol / kg scale)
    % ****************************************************************
    % GET  DATA FROM *.dura FOR DIAGNOSTICS
    dura = parse_pHmsg([dirs.temp,pH_file]);
    if isempty(dura.data)
        disp(['No pH data returned for ',pH_file])
        delete([dirs.temp,pH_file]);
        continue
    else
        if msg_ct == 1
            pH_hdr = dura.hdr;
            pH_hdr = ['Cast #', pH_hdr];
        end
        pH_data = dura.data;
        pH_data = [ones(size(pH_data(:,1)))*cast_num,pH_data];
        delete([dirs.temp,pH_file]);
    end
    merge_pH = [merge_pH;pH_data];
    clear dura pH_data
    
end
iSDN = find(strcmp(pH_hdr,'Date/Time') == 1);
iC   = find(strcmp(pH_hdr,'Cast #') == 1);
iP   = find(strcmp(pH_hdr,'CTD Pres') == 1);
iH   = find(strcmp(pH_hdr,'Internal Humidity') == 1);
iBB  = find(strcmp(pH_hdr,'Backup Batt, V') == 1);
iVRS = find(strcmp(pH_hdr,'Vrs') == 1);
iSVRS = find(strcmp(pH_hdr,'std Vrs') == 1);
iVK  = find(strcmp(pH_hdr,'Vk') == 1);
iSVK = find(strcmp(pH_hdr,'std Vk') == 1);
iIK  = find(strcmp(pH_hdr,'Ik') == 1);
iIB  = find(strcmp(pH_hdr,'Ib') == 1);

% ****************************************************************
if plot_flag == 1
    % MAKE SOME CONTOUR PLOT
    % {'Cast #','Date/Time','CTD Pres','CTD Temp','CTD Sal','Internal Temp',
    %     'Internal Humidity','System Power, V','System Power, I','Backup Batt, V',
    %     'Vrs','std Vrs','Vk','std Vk','Ik','Ib'}
    

    
    P          = [0:5:100,110:10:400,450:50:1000,1100:100:2000]';
    rP         = size(P,1);
    cycles     = unique(merge_pH(:,iC));
    r_cycles   = size(cycles,1);
    interp_ph  = ones(rP*r_cycles, size(pH_hdr,2))*NaN;
    line_ct    = 1;
    for cyc_ct = 1:r_cycles
        t1       = merge_pH(:,iC) == cycles(cyc_ct);
        tmp      = merge_pH(t1,:);
        [~,ia,~] = unique(tmp(:,iP));
        tmp      = tmp(ia,:); % sorted and unique
        rtmp     = size(tmp,1);
        tg       = ~isnan(tmp(:,iP));
        %     itmp     = ones(rP, r_cycles)*NaN;
        %     itmp(tg,:) = interp1(tmp(tg,iP),tmp(tg,:),P);
        %     interp_ph(line_ct:line_ct+rP-1,:) = itmp;
        interp_ph(line_ct:line_ct+rP-1,:) = interp1(tmp(tg,iP),tmp(tg,:),P);
        line_ct    = line_ct+rP;
    end
    
    
    % NOW MAKE SOME DAIGNOSTIC DATA PLOTS
    F10 = figure(10);
    F10.Units = 'Normalized';
    %F10.Position = [0.0917 0.0713 0.8000 0.8000];
    F10.Position =[0.0917 0.3278 0.4515 0.5435];
    ax(1) = subplot(3,2,1);
    ax(2) = subplot(3,2,2);
    ax(3) = subplot(3,2,3);
    ax(4) = subplot(3,2,4);
    ax(5) = subplot(3,2,5);
    ax(6) = subplot(3,2,6);
    
    ax(1).Position = ax(1).Position + [0 -0.05 0 0.05];
    ax(2).Position = ax(2).Position + [0 -0.05 0 0.05];
    ax(3).Position = ax(3).Position + [0 -0.1 0 0.05];
    ax(4).Position = ax(4).Position + [0 -0.1 0 .05];
    ax(5).Position = ax(5).Position + [0 -0.05 0 -0.025];
    ax(6).Position = ax(6).Position + [0 -0.05 0 -0.035];
    
    y_lim = [0 max(interp_ph(:,iP))];
        
    
    % ax(1) = subplot(3,2,1);
    % ax(1).Position = ax(1).Position + [0 -0.05 0 0.05];
    IX    = iVRS;
    Z     = reshape(interp_ph(:,IX), rP, r_cycles);
    t_str = [MBARI_ID_str,'    ',pH_hdr{IX}];
    if IX == iIB || IX == iIK
        Z = Z * 1e9;
        Z(abs(Z) > 1000) = NaN;
        t_str =[t_str,' * 1e9'];
    elseif IX == iVRS || IX == iVK
        Z(Z > -0.4 | Z <-1.6) = NaN;
    end
    contourf(ax(1), cycles', P, Z, 30,'LineColor','none')
    set(ax(1),'YDir','Reverse')
    ylim(ax(1),y_lim);
    title(ax(1), t_str);
    cb = colorbar(ax(1));
    
    % ax(2) = subplot(3,2,2);
    % ax(2).Position = ax(2).Position + [0 -0.05 0 0.05];
    IX    = iVK;
    Z     = reshape(interp_ph(:,IX), rP, r_cycles);
    t_str = [MBARI_ID_str,'    ',pH_hdr{IX}];
    if IX == iIB || IX == iIK
        Z = Z * 1e9;
        Z(abs(Z) > 1000) = NaN;
        t_str =[t_str,' * 1e9'];
    elseif IX == iVRS || IX == iVK
        Z(Z > -0.4 | Z <-1.6) = NaN;
    end
    contourf(ax(2), cycles', P, Z, 30,'LineColor','none')
    set(ax(2),'YDir','Reverse')
    ylim(ax(2),y_lim);
    title(ax(2), t_str);
    cb = colorbar(ax(2));
    
    % ax(3) = subplot(3,2,3);
    % ax(3).Position = ax(3).Position + [0 -0.1 0 0.05];
    IX    = iIK;
    IX    = iSVRS;
    Z     = reshape(interp_ph(:,IX), rP, r_cycles);
    t_str = [MBARI_ID_str,'    ',pH_hdr{IX}];
    if IX == iIB || IX == iIK
        Z = Z * 1e9;
        Z(abs(Z) > 1000) = NaN;
        t_str =[t_str,' * 1e9'];
    elseif IX == iVRS || IX == iVK
        Z(Z > -0.4 | Z <-1.6) = NaN;
    end
    contourf(ax(3), cycles', P, Z, 30,'LineColor','none')
    set(ax(3),'YDir','Reverse')
    ylim(ax(3),y_lim);
    title(ax(3), t_str);
    cb = colorbar(ax(3));
    
    % ax(4) = subplot(3,2,4);
    % ax(4).Position = ax(4).Position + [0 -0.1 0 0.05];
    IX    = iIB;
    IX    = iSVK;
    Z     = reshape(interp_ph(:,IX), rP, r_cycles);
    t_str = [MBARI_ID_str,'    ',pH_hdr{IX}];
    if IX == iIB || IX == iIK
        Z = Z * 1e9;
        Z(abs(Z) > 1000) = NaN;
        t_str =[t_str,' * 1e9'];
    elseif IX == iVRS || IX == iVK
        Z(Z > -0.4 | Z <-1.6) = NaN;
    end
    contourf(ax(4), cycles', P, Z, 30,'LineColor','none')
    set(ax(4),'YDir','Reverse')
    ylim(ax(4),y_lim);
    title(ax(4),t_str);
    cb = colorbar(ax(4));
    
    
    
    IX    = iIK;
    plot(ax(5),interp_ph(:,iC),interp_ph(:,IX),'ko-','MarkerSize', 4, ...
        'MarkerFaceColor','r')
    ylabel(ax(5),pH_hdr{IX})
    ax(5).Position(3) = ax(3).Position(3);
    
    
    IX    = iIB;
    plot(ax(6),interp_ph(:,iC),interp_ph(:,IX),'ko-','MarkerSize',4, ...
        'MarkerFaceColor','r')
    ylabel(ax(6), pH_hdr{IX})
    ax(6).Position(3) = ax(4).Position(3);
    
    % ax(5) = subplot(3,2,5);
    % ax(5).Position = ax(5).Position + [0 -0.1 0 -0.05];
    %
    % ax(6) = subplot(3,2,6);
    % ax(6).Position = ax(6).Position + [0 -0.1 0 -0.05];
    
    %linkaxes(ax,'x');
    % disp('I put a "return" in at line 304')
    % return
    
end

% *********************************************************************
% CLEAN UP
if ~isempty(ls([dirs.temp,'*.dura']))
    delete([dirs.temp,'*.dura']);
end


% ************************************************************************
% PRINT MERGED DURA FILES TO TEXT
% ************************************************************************
iSDN = find(strcmp(pH_hdr,'Date/Time') == 1);
iC   = find(strcmp(pH_hdr,'Cast #') == 1);
iP   = find(strcmp(pH_hdr,'CTD Pres') == 1);
iH   = find(strcmp(pH_hdr,'Internal Humidity') == 1);
iBB  = find(strcmp(pH_hdr,'Backup Batt, V') == 1);
iVRS = find(strcmp(pH_hdr,'Vrs') == 1);
iSVRS = find(strcmp(pH_hdr,'std Vrs') == 1);
iVK  = find(strcmp(pH_hdr,'Vk') == 1);
iSVK = find(strcmp(pH_hdr,'std Vk') == 1);
iIK  = find(strcmp(pH_hdr,'Ik') == 1);
iIB  = find(strcmp(pH_hdr,'Ib') == 1);

dstr  = datestr(merge_pH(:,iSDN),'yyyy-mm-dd HH:MM');
% merge_pH(1:3,1:5)
% disp(dstr(1:10))
% return

% GET LAT AND LON FROM FLOATVIZ FILES
d = get_FloatViz_data([dirs.FV, MBARI_ID_str,'.TXT']);
uC = unique(merge_pH(:,iC)); % merge file cycle #'s
LL = ones(size(merge_pH,1),2)*NaN; % predim
for i = 1: size(uC,1)
    t1 = d.data(:,2) == uC(i);
    tmp = d.data(t1,:);
    if isempty(tmp)
        disp(['Cycle ',num2str(uC(i)), ' exists for dura msg file ', ...
            'but not for FloatViz file - settin lon & lat to NaN'])
        disp('Are your Float Viz files up to date on you local computer?')
        LL(t2,1) = NaN;
        LL(t2,2) = NaN;
    else
        t2 = merge_pH(:,iC) == uC(i);
        LL(t2,1) = tmp(1,3);
        LL(t2,2) = tmp(1,4);
    end
end

tNaN = isnan(merge_pH);
merge_pH(tNaN) = -1e10;


% PRINT HEADER FIRST
out_name = [MBARI_ID_str,'_DURA.TXT'];

print_hdr ={'Cruise' 'Station' 'Type' 'yyyy-mm-dd hh:mm' ...
    'Longitude [degrees_east]' 'Latitude [degrees_north]' ...
    'Pressure[dbar]' 'Temperature[°C]' 'Salinity[pss]'};

hdr_tmp = pH_hdr(6:end);
hdr_tmp = regexprep(hdr_tmp,',',''); % remove commas
hdr_tmp = regexprep(hdr_tmp,' ','_'); % replace spaces with underscore

%print_hdr = [print_hdr, pH_hdr(6:end)];
print_hdr = [print_hdr, hdr_tmp];
print_hdr = [print_hdr, 'DF_ID' 'DF_ver' 'K2' 'K0'];




disp(['Printing ph diagnostic data data to: ',dirs.mat,out_name]);
fid  = fopen([dirs.mat, out_name],'W');
fprintf(fid,'//0\r\n');
fprintf(fid,['//File updated on ',datestr(now,'mm/dd/yyyy HH:MM'), ...
       '\r\n']);
fprintf(fid,['//MBARI ID: ',MBARI_ID_str,'\r\n']);
fprintf(fid,['//Missing data value = ',MVI_str,'\r\n']);

hdr_size  = size(print_hdr,2);
data_rows = size(merge_pH,1);

% PRINT HEAFER & BUILD FORMAT STRING
fstr = '';
for i = 1:size(print_hdr,2)-1 
    if regexp(print_hdr{i},'Station','once')
        fstr =[fstr,'%d\t'];
    elseif regexp(print_hdr{i},'Cruise|Type|yyyy|DF\_ID','once')
        fstr =[fstr,'%s\t'];
    elseif regexp(print_hdr{i},'Longitude|Latitude|','once')
        fstr =[fstr,'%0.4f\t'];
    else
        fstr =[fstr,'%0.4g\t'];
    end
    fprintf(fid,'%s\t',print_hdr{i});
end
fstr =[fstr,'%0.4f\r\n'];
fprintf(fid,'%s\r\n',print_hdr{i+1});

% SET DF_num to string because of SEB DF_num with letters
if ~ischar(INFO.DF_num)
    INFO.DF_str = num2str(INFO.DF_num,'%0.0f');
else
    INFO.DF_str = INFO.DF_num;
end
DF_info = [INFO.DF_ver, INFO.K2, INFO.KO];
DF_info(isnan(DF_info)) = -1e10; % nan's to mvi

for ct = 1 : data_rows

    fprintf(fid, fstr, MBARI_ID_str, merge_pH(ct,iC), 'B', dstr(ct,:), ...
        LL(ct,:), merge_pH(ct,iP:end), INFO.DF_str, DF_info);
end

fclose(fid);
















