% pioneer_ar29_read_BTL.m
clear;

% --- Save Option.
%saveOption='yes';
saveOption='no';

% --- CTD list file columns#:
col_st=1;
col_year=2;
col_mon=3;
col_day=4;
col_h=5;
col_min=6;
col_sec=7;
col_lat=8;
col_lon=9;
%**************************
% Cruise ID, name and PATH.
%**************************
crNum=11;
leg=1;
path_name_part='~/OOI_pioneer/'; % ~/OOI_pioneer/p_6/leg_3
path_name_out=[path_name_part 'BTL_data/']
fname_part_1='ar29';
fname_part_2='';
path_name_ctd=[path_name_part 'p_' num2str(crNum) '/leg_' num2str(leg) '/'];
% --- Cruise id, name.
cruise_title=['Pioneer ' num2str(crNum) ' Cruise ' upper(fname_part_1)  upper(strrep(fname_part_2,'-',''))];
cr_id=[fname_part_1 fname_part_2];
%**************************
% CTD list file name.
%**************************
fname_list=['p_' num2str(crNum) '_' cr_id '_ctd_list'];
% ---- (4) CTD data file  - extention.
fname_ext='btl';
fname_out=[fname_list(1:end-8) 'BTL_data'];
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@ BTL Variables @@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Flag - 'bad data'.
bad_flag=-9.990e-29;
colSt_btl=1;
colBot_btl=2;
colPres_btl=18;
n_col_1=27;
variables_btl='Bottle        Date  Sigma-é00  Sigma-é11 OxsatMm/Kg Sbeox0Mm/Kg Potemp090C Potemp190C      Sal00      Sal11  Density00  Density11       SvCM      SvCM1       PrDM      T090C      T190C      C0S/m      C1S/m    Sbeox0V  FlECO-AFL TurbWETntu0       Spar        Par       Cpar';
variables_out=['% Station ',variables_btl];
%*******************************************
%********** Columns and Headers ************
%*******************************************
% ----- Columns and Headers for output BTL summary file.
% Do not use Date columns
not_use_BTL_cols=[];
% Original BTL file columns for use in the output file.
use_BTL_cols=1:1:n_col_1;
if ~isempty(not_use_BTL_cols), ...
        use_BTL_cols(not_use_BTL_cols)=[]; end
n_col=use_BTL_cols;
% Headers for OUTPUT file - variables list.
%variables_out_v=variables_btl;
%if ~isempty(not_use_BTL_cols), ...
%        variables_out_v(not_use_BTL_cols,:)=[]; end
%variables_out='%CTD Cast #';
%for i=1:size(variables_out_v,1)
%    variables_out=[variables_out ', ' deblank(variables_out_v(i,:))];
%end
%*******************************************
%********* LOAD CTD LIST DATAFILE **********
%*******************************************
p=['load ' path_name_ctd fname_list ';'];
eval(p);
p=['c_list=' fname_list ';'];
eval(p);
ctdList=(unique(c_list(:,col_st)))';
% --- TEMPORARY: remove sts 85 and 98 from a list
% (different BTL format - test processing):
rr=find(ctdList(:)==85 | ctdList(:)==98);
ctdList(rr)=[];
%*******************************************
%*********** READ .BTL files data **********
%*******************************************
no_btl_list='--- CTD BTL files are not available:';
BTL_data_output=[];
fname_pre='';
str_disp_no_st='BTL File NOT FOUND:';
for stNum = ctdList
    % Write input file names to string.
    if stNum < 10
        stNum_str=['00' num2str(stNum)];
    else
        stNum_str=['0' num2str(stNum)];
    end
    % --- CTD file name.
    j = [fname_pre cr_id stNum_str '.' fname_ext];
    % --- Open file for reading.
    pfid = fopen([path_name_ctd j]);
    if pfid<0
        no_btl_list=strvcat(no_btl_list,[cr_id ' Station ' num2str(stNum)]);
    else % For if pfid<0
        % --- Remove headers.
        while 1
            headerLine=fgets(pfid);
            if length(headerLine)>=10
                if strcmp(headerLine(1:10),'  Position'), break; end
            end
        end
        % --- Read lines.
        count_line=0;
        data_btl=[];
        line_names='';
        while 1
            count_line=count_line+1;
            line = fgetl(pfid);
            if ~isstr(line), break, end
            %-----------------------------------------------------
            %-------- CHECK BTL data format - first 4 lines ------
            %-----------------------------------------------------
            if count_line>=1 & count_line<=4
                line_names=[line_names,line(end-3:end)];
            elseif count_line==5
                if ~strcmp(line_names,'avg)dev)min)max)')
                    disp(['!!! ' cruise_title ': BTL data format is not "4 data lines"']);
                    return;
                end
            end
            %-----------------------------------------------------
            line_cor_1 = line;
            % CORRECTION 1.
            % Replace Month name with Month number.
            if  rem(count_line,4)==1
                %if  rem(count_line,2)~=0
                line_cor_1 = strrep(line_cor_1,'Jan','1');
                line_cor_1 = strrep(line_cor_1,'Feb','2');
                line_cor_1 = strrep(line_cor_1,'Mar','3');
                line_cor_1 = strrep(line_cor_1,'Apr','4');
                line_cor_1 = strrep(line_cor_1,'May','5');
                line_cor_1 = strrep(line_cor_1,'Jun','6');
                line_cor_1 = strrep(line_cor_1,'Jul','7');
                line_cor_1 = strrep(line_cor_1,'Aug','8');
                line_cor_1 = strrep(line_cor_1,'Sep','9');
                line_cor_1 = strrep(line_cor_1,'Oct','10');
                line_cor_1 = strrep(line_cor_1,'Nov','11');
                line_cor_1 = strrep(line_cor_1,'Dec','12');
                % Remove (avg)  and (sdev).
                nn=findstr(line_cor_1,'(avg)');
                if isempty(nn)
                    disp(strvcat('Incorrect end (avg) in the line:',line_cor_1));
                    return;
                end
                line_cor_2 = strrep(line_cor_1,' (avg)','');
                % Data line - NUMBERS.
                line_data_1 = (sscanf(line_cor_2,'%f'))';
                % CHECK lines which were not read correctly.
                if length(line_data_1)~=length(n_col)
                    % DISPLAY THE PROBLEM.
                    disp(['Character (not number) is found in a line ' num2str(count_line) ':']);
                    disp(line_cor_2);
                    disp('This part of line is read sucessfully:');
                    disp(num2str(line_data_1));
                    return;
                end
                % Create data matrix: one station data.
                data_btl = [data_btl;line_data_1];
            end
            clear line_data_1;
        end  % For while 1
        % Close file.
        fclose(pfid);
        if isempty(data_btl)
            disp(['Station #' num2str(stNum) ': empty BTL']);
            %return;
        else
            % Create data matrix - all stations data.
            k_size=ones(size(data_btl,1),1);
            BTL_data_output = [BTL_data_output;stNum*k_size data_btl(:,use_BTL_cols)];
        end
        clear data_btl;
    end % For if pfid<0
end % For for stNum = ctdList
if ~isempty(BTL_data_output)
    total_st=length(unique(BTL_data_output(:,1)));
    %*********************
    % SAVE data file
    %*********************
    if strcmp(saveOption,'yes')
        p=[fname_out '=BTL_data_output;'];
        eval(p);
        % (1) Save as MAT file:
        p=['save ' fname_out ' ' fname_out ';'];
        eval(p);
        % (2) Save data matrix as ASCII tab delimited file with the first
        % commented line - headers.
        % -------------- Method 1 - using "fprintf" and Unix "cat" --------------
        % Write formated data to file- matrix only.
        dlmwrite('TEMP.txt',BTL_data_output,'\t');
        %------------------------------
        % The following (with "precision") tried but seems did not work for PAR,
        % SPAR. Look on the result carefully when use:
        %dlmwrite(['TEMP.txt'],BTL_data_output,'delimiter','\t','precision','%.4f');
        %------------------------------
        % Make text file - headers.
        pfid=fopen('headers.txt','w');
        fprintf(pfid,'%s\n',variables_out);
        fclose(pfid);
        % Concatenate 'Headers' file and 'Matrix' file.
        p=['! cat headers.txt TEMP.txt > ' fname_out '.txt'];
        eval(p);
        % Remove text files - headers and TEMP.
        ! rm headers.txt  TEMP.txt
        %%%p=['! mv ' fname_out '.* ' path_name_out ';'];
        %%%eval(p);
        disp(strvcat(blanks(1),['File has been saved, filename: ' fname_out '.mat (and *.txt)'],...
            ['Location: ' path_name_out],...
            ['Size: ' num2str(size(BTL_data_output))],...
            ['First cast listed: ' num2str(min(BTL_data_output(:,1))) ', Last cast listed: ' num2str(max(BTL_data_output(:,1)))],...
            ['Total casts: ' num2str(total_st)],...
            ['CTD data source: ' path_name_ctd]));
    else
        disp(strvcat(blanks(1),cruise_title,' OUTPUT Matrix name: BTL_data_output',...
            ['Size: ' num2str(size(BTL_data_output))],...
            ['First cast listed: ' num2str(min(BTL_data_output(:,1))) ', Last cast listed: ' num2str(max(BTL_data_output(:,1)))],...
            ['Total casts: ' num2str(total_st)],...
            ['CTD data source: ' path_name_ctd]));
    end
    if size(str_disp_no_st,1)>1, ...
            disp(str_disp_no_st); end
else % For if ~isempty(BTL_data_output)
    disp(['!!! ' cruise_title ': there are no BTL files'])
end  % For if ~isempty(BTL_data_output)
%--------------- CHECK --------------
if ~isempty(BTL_data_output)
    st_check=3;
    rr=find(BTL_data_output(:,colSt_btl)==st_check);
    if ~isempty(colPres_btl)
        st_botle_pressure=BTL_data_output(rr,[colSt_btl colBot_btl colPres_btl]);
    end
end % For if ~isempty(BTL_data_output)
%--------------- CHECK  END --------------
%********************
% Display info - stations not having BTL files.
%********************
if size(no_btl_list,1)>1, disp(no_btl_list); end
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@ BTL Summary COLUMNS   @@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% For future use, not used in this program.
% variables_btl='Bottle        Date  Sigma-é00  Sigma-é11 OxsatMm/Kg Sbeox0Mm/Kg Potemp090C Potemp190C
% Sal00      Sal11  Density00  Density11       SvCM      SvCM1       PrDM      T090C      T190C
% C0S/m      C1S/m    Sbeox0V  FlECO-AFL TurbWETntu0       Spar        Par       Cpar';
% Summary BTL columns:
colSt_btl=1;         % CTD cast
colBot_btl=2;        % Bottle
colMon_btl=3;        % Month
colDay_btl=4;        % Day
colYear_btl=5;       % Year
colSig_btl=6;        % Sigma-é00
colSig_s_btl=7;      % Sigma-é00
colOxySat_btl=8;     % OxsatMm/Kg
colOxy_btl=9;        % Sbeox0Mm/Kg
colPotTemp_btl=10;   % Potemp090C
colPotTemp_s_btl=11; % Potemp190C
colSal_btl=12;       % Sal00
colSal_s_btl=13;     % Sal11
colDens_btl=14;      % Density00
colDens_s_btl=15;    % Density11
colSoundVel_btl=16;  % SvCM
colSoundVel_s_btl=17;% SvCM1
colPres_btl=18;      % PrDM
colTemp_btl=19;      % T090C
colTemp_s_btl=20;    % T190C
colCnd_btl=21;       % C0S/m
colCnd_s_btl=22;     % C1S/m
colOxyV_btl=23;      % Sbeox0V
colFluor_btl=24;     % FlECO-AFL
colTurb_btl=25;      % TurbWETntu0
colSpar_btl=26;      % Spar
colPar_btl=27;       % Par
colCpar_btl=28;      % Cpar


