% nbp1201_uw_plot_all_rvdas_loop.m
% INPUT: RVDAS UW/Navigation merged data (mat files)
% Plot locations and data values in colormap, all days, days 8:36 tested.

clear;
t0 = clock;

%saveOption='yes';
saveOption='no';
%
%disp('Protected - comment/uncomment the lines');
%return
% ----  Make data averaging.
average_option='yes'; % one minute average
%average_option='no';
if strcmp(average_option,'yes')
    average_note='(1 min average)';
else
    average_note='';
end
%
% ---- Plot time markrs.
plotTimeMarkes='yes';
%plotTimeMarkes='no';
%
% ---- Convert to a real units.
dataConvert='yes';
%dataConvert='no';
%
% ---- Data path, names.
path_part_1_uw='/data2/antarctic_data2/nbp1201/underway/rvdas/uw/';
fname_ext_uw='mat';
source='rvdas';
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@  UNDERWAY merded columns:  @@@@@@@@
%@@@@ Date/Time/Location - same for all @@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
colYear_M=1;
colDay_M=2;
colHour_M=3;
colMin_M=4;
colSec_M=5;
colLat_M=6;
colLon_M=7;
day_format='day_of_year';
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%******************
% C O N S T A N T S
%******************
% Font size.
fnt_axis=8;
fnt_lbl=8;
fnt_title=8;
fnt_text=8;
fnt_name='helvetica';
marker_size=1;
marker_size_time=8;
crName='NBP1201';
%#############################################
%############  COLORMAP data  ################
%#############################################
% Load Colormap.
load palet_13_ant;
cmap=palet_13_ant;
n_bands=size(cmap,1);
n_dec_chars=2;
cval=1:size(cmap,1)+1;
%#############################################
%############   Map limits  ##################
%#############################################
%axis([160 205 -80 -72]);
lonmin = 160;
lonmax = 195;
latmax = -72;
latmin =-79;
% --------  Calculate relation relyx = axisy/axisx (for PlotBoxAspectRatio)
weg = 111*cos(pi*(latmin+(latmax-latmin)/2)/180);% Weight of one degree longitude
xc = (lonmax-lonmin)*weg;                        % Longitude, km
yc = (latmax-latmin)*111;                        % Latitude, km
relyx = yc/xc;
%#############################################
%######## X,Y ticks, tickLabels ##############
%#############################################
% Ticks interval.
lon_int=5;
lat_int=1;
% ---- X-ticks.
x_tick=floor(lonmin):lon_int:lonmax;
x_tick_label='';
for ii=1:length(x_tick)
    if x_tick(ii)<180
        x_tick_str=[num2str(x_tick(ii)) 'E'];
    else
        x_tick_str=[num2str(360-x_tick(ii)) 'W'];
    end

    if ii==length(x_tick)
        delim='';
    else
        delim='|';
    end
    x_tick_label=[x_tick_label x_tick_str delim];
end
clear ii;
% ---- Y-ticks.
y_tick=floor(latmin):lat_int:latmax;
y_tick_label='';
for ii=1:length(y_tick)
    if ii==length(y_tick)
        delim='';
    else
        delim='|';
    end
    y_tick_label=[y_tick_label num2str(-y_tick(ii)) 'S' delim];
end
clear ii;
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@  DATA NAMES   @@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
channel_name_list=strvcat('eng1: Engineering',...
    'gen1: Micro-TSG #2',...
    'hdas: Hydro-DAS',...
    'knud: Knudsen',...
    'mtsg: Micro-TSG',...
    'mwx1: Meteorology',...
    'oxyg: Oxygen data',...
    'pco2: PCO2',...
    'pguv: GUV Data',...
    'rtmp: Remote Temperature',...
    'svp1: Sound Velosity Probe');
mwx1_channels=strvcat('MET','SUS','PUS');
pco2_channels=strvcat('01 Equil',...
    '02 Atmos',...
    '03 Nitrogen',...
    '04 CA06630',...
    '05 CA06632',...
    '06 CA06636',...
    '07 CA06661',...
    '08 CA07206',...
    'ALL Flow sources');
date_format='mm/dd/yyyy';
% Set figure properties
fig_pos=[118.00        475.00        490.00        354.00];
%fig_pos=[118.00        471.00        490.00        358.00];
figure;
set(gcf,'color',[1 1 1],'position',fig_pos,...
    'paperPositionMode','auto');
colormap(cmap);
kk_x=1.045;
kk_y=1.045;
%***********************************************************
%******************  LOOP 1 - UW channel  ******************
%***********************************************************
fname_out_all='';
for channel_num=2%1:size(channel_name_list,1)
    channel_id_name=channel_name_list(channel_num,:);
    channel_id=channel_id_name(1:4);
    channel_name=deblank(channel_id_name(7:end));
    % Path name.
    path_name_uw=[path_part_1_uw channel_id '/process/'];
    if strcmp(channel_id,'mwx1')
        sub_channel_name_list=mwx1_channels;
        sub_channel_list=1:size(sub_channel_name_list,1);
    elseif strcmp(channel_id,'pco2')
        sub_channel_name_list=pco2_channels;
        sub_channel_list=1%1:size(sub_channel_name_list,1);
        % NOTE: take "EQUIL" for now (pco2 measurements)
    else
        sub_channel_name_list='';
        sub_channel_list=1;
    end
    %***********************************************************
    %******************  LOOP 2 - UW subchannel  ***************
    %***********************************************************
    for sub_channel_num=sub_channel_list
        sub_channel_id='';
        sub_channel_id_str='';
        sub_channel_id_text='';
        sub_channel_id_title='';
        flowName_str='';
        flowName_title='';
        flowCode=[];
        if ~isempty(sub_channel_name_list)
            if  strcmp(channel_id,'pco2')
                colFlowSource=18;
                flowCode_flowName=sub_channel_name_list(sub_channel_num,:);
                if strcmp(flowCode_flowName,'ALL Flow sources')
                    flowCode=[];
                    flowName=deblank(flowCode_flowName);
                    flowName_str='_all';
                    flowName_title='/All';
                else
                    flowCode=str2num(flowCode_flowName(1:2));
                    flowName=deblank(flowCode_flowName(4:end));
                    flowName_str=['_' flowName];
                    flowName_title=['/' flowName];
                end
            else
                sub_channel_id=sub_channel_name_list(sub_channel_num,:);
                sub_channel_id_str=['_' sub_channel_id];
                sub_channel_id_text=['_' sub_channel_id];
                sub_channel_id_title=['/' sub_channel_id];
            end
        end
        %*********************************************
        % C O N S T A N T S depending on CHANNEL name.
        %*********************************************
        par_not_use=[];
        par_note='';
        if strcmp(channel_id,'eng1')
            par_list=strvcat('08 Power Supply Voltage V',...
                '09 Internal Case Temperature degC',...
                '10 Pump #1 flow rate L/min',...
                '11 Pump #2 flow rate L/min',...
                '12 Pump #3 flow rate L/min',...
                '13 Seismic air pressure Lbs/sq-in',...
                '14 PIR case resistance (not currently hooked up, data is irrelevant Kohm)',...
                '15 PIR case ratiometric output (not currently hooked up, data is irrelevant) mV',...
                '16 Freezer #1 temperature degC',...
                '17 Freezer #2 temperature degC',...
                '18 Freezer #3 temperature degC',...
                '19 Not indicated');
            %par_not_use=[7 8 12];
            par_not_use=[7 8];
        elseif strcmp(channel_id,'gen1')
            par_list=strvcat('08 Internal Temperature degC',...
                '09 Conductivity s/m',...
                '10 Salinity PSU',...
                '11 Sound velocity m/s');
        elseif strcmp(channel_id,'hdas')
            par_list=strvcat('08 Supply voltage V',...
                '09 Panel temperature degC',...
                '10 Fluorometer mV',...
                '11 Transmissometer mV',...
                '12 Sea Water Valve (-1 = stern thruster valve, 0 = moon pool valve)',...
                '13 Flow meter 1 frequency Hz',...
                '14 Flow meter 2 frequency Hz',...
                '15 Flow meter 3 frequency Hz',...
                '16 Flow meter 4 frequency Hz');
        elseif strcmp(channel_id,'knud')
            par_list=strvcat('08 High frequency depth meters',...
                '09 Low frequency depth meters');
        elseif strcmp(channel_id,'mtsg')
            par_list=strvcat('08 Internal Temperature degC',...
                '09 Conductivity s/m',...
                '10 Salinity PSU',...
                '11 Sound velocity m/s');
        elseif strcmp(channel_id,'mwx1')
            if strcmp(sub_channel_id,'MET')
                par_list=strvcat('08 Power Supply Voltage V',...
                    '09 Enclosure Relative Humidity %',...
                    '10 Air temperature degC ',...
                    '11 Air Relative Humidity %',...
                    '12 PAR (photosynthetically available radiation)* mV',...
                    '13 PSP (short wave radiation)* mV',...
                    '14 PIR Thermopile (long wave radiation)* mV',...
                    '15 PIR Case Temperature degKelvin',...
                    '16 PIR Dome Temperature degKelvin',...
                    '17 Barometer mBar');
            elseif strcmp(sub_channel_id,'PUS')
                par_list=strvcat('08 A (unit identification, A=1)',...
                    '09 Port Wind direction relative deg',...
                    '10 Port Wind speed relative m/s',...
                    '11 Units (M=1)',...
                    '12 Sound Speed m/s',...
                    '13 Sonic Temperature degC',...
                    '14 Unit Status (00 or 60 are good, any other value indicates fault)');
                par_not_use=[1 4 7];
            elseif strcmp(sub_channel_id,'SUS')
                par_list=strvcat('08 A (unit identification, A=1)',...
                    '09 Starboard Wind direction relative deg',...
                    '10 Starboard Wind speed relative m/s',...
                    '11 Units (M=1)',...
                    '12 Sound Speed m/s',...
                    '13 Sonic Temperature degC',...
                    '14 Unit Status (00 or 60 are good, any other value indicates fault)');
                par_not_use=[1 4 7];
            else
                return;
            end
            %sub_channel_id=deblank(sub_channel_id);
            %sub_channel_id_str=['_' sub_channel_id];
            %sub_channel_id_text=['\_' sub_channel_id];
            %sub_channel_id_title=['/' sub_channel_id];
        elseif strcmp(channel_id,'oxyg')
            par_list=strvcat('08 Measurement ID: Model Number',...
                '09 Measurement ID: Serial Number',...
                '10 Oxygen Reading MkM',...
                '11 Saturation Reading %',...
                '12 Water Temperature degC',...
                '13 Dphase Raw numeric',...
                '14 Bphase Raw numeric',...
                '15 Rphase Raw numeric',...
                '16 Bamp Raw numeric',...
                '17 Bpot Raw numeric',...
                '18 Ramp Raw numeric',...
                '19 RawTemp');
            par_not_use=[1 2];
        elseif strcmp(channel_id,'pco2')
            par_list=strvcat('09 Raw voltage (IR) mV',...
                '10 Cell temperature degC',...
                '11 Barometer MBar',...
                '12 Concentration ppm',...
                '13 Equilibrated temperature degC',...
                '14 pCO2 pressure microAtm',...
                '15 Flow rate ml/min',...
                '16 Source ID #  1 or 2 digits',...
                '17 Valve position 1 or 2 digits',...
                '18 Flow source (Equil = pCO2 measurement) 1 or 2 digits');
            par_not_use=[8 9 10];
        elseif strcmp(channel_id,'pguv')
            %'08 Date mmddyy',...
            %'09 Time hhmmss',...
            par_list=strvcat('10 Ed0Gnd V',...
                '11 Ed0320 uW (cm^2 nm)',...
                '12 Ed0340 uW (cm^2 nm)',...
                '13 Ed0313 uW (cm^2 nm)',...
                '14 Ed0305 uW (cm^2 nm)',...
                '15 Ed0380 uW (cm^2 nm)',...
                '16 Ed0PAR uW (cm^2 nm)',...
                '17 Ed0395 uW (cm^2 nm)',...
                '18 Ed0Temp degC',...
                '19 Ed0Vin V');
        elseif strcmp(channel_id,'rtmp')
            par_list=strvcat('08 Temperature at seawater intake degC');
        elseif strcmp(channel_id,'svp1')
            par_list=strvcat('08 Sound velocity in ADCP sonar well m/s');
        else
            return;
        end
        if ~isempty(par_not_use), ...
                par_list(par_not_use,:)=[];  end
        %***********************************************************
        %******************     LOOP 3 - QUANTITY    ***************
        %***********************************************************
        for par_num=[1]%1:size(par_list,1)
            colNum_name =  par_list(par_num,:);
            colNum=str2num(colNum_name(1:2));
            q_name=deblank(colNum_name(4:end));
            q_name_print=q_name;
            %***********************************************************
            % Quantity name characters to use in filename.
            %***********************************************************
            if strcmp(channel_id,'eng1') & strcmp(q_name(1:7),'Freezer')
                chars_use=9;
            elseif strcmp(channel_id,'eng1') & strcmp(q_name(1:4),'Pump')
                chars_use=6;
            elseif (strcmp(channel_id,'mwx1') & ( strcmp(q_name(1:4),'Star') | strcmp(q_name(1:4),'Port') )) | ...
                 strcmp(channel_id,'hdas') & ( strcmp(q_name(1:4),'Flow') )  
                chars_use=15;
            else
                chars_use=7;
            end
            if strcmp(dataConvert,'yes')
                if strcmp(channel_id,'hdas') & strcmp(q_name,'Fluorometer mV')
                    Dark=0.0701; % from instrument.coeff
                    SF=11.0782;  % from instrument.coeff
                    %Fluor=(Vmeasured-Dark)*SF;
                    q_name_print='Fluorescence \mug/L'; % !!! name changed
                elseif strcmp(channel_id,'hdas') & strcmp(q_name,'Transmissometer mV')
                    Vdark=0.059; % from instrument.coeff
                    Vref=4.804;  % from instrument.coeff
                    %Tr=(Vsig-Vdark)/(Vref-Vdark);
                    q_name_print='Transmition %'; % !!! name changed to real units
                elseif strcmp(channel_id,'mwx1') & strcmp(sub_channel_id,'MET') & strcmp(q_name(1:3),'PAR')
                    %parc1=0.17;
                    %parcv=-0.0001; % V
                    parc1=0.1568; % from instrument.coeff
                    parcv=0.0007; % V % from instrument.coeff
                    %parCalc=(par-parcv) * parc1 * 10000; % muEinstiens/m^2sec
                    %d_uw(:,colNum)=(d_uw(:,colNum)/1000-parcv) * parc1 * 10000; % muEinstiens/m^2sec
                    q_name_print=[q_name(1:end-2) '\muEinstiens/m^2sec']; % !!! name changed to real units
                elseif strcmp(channel_id,'mwx1') & strcmp(sub_channel_id,'MET') & strcmp(q_name(1:3),'PSP')
                    pspCoeff=7.87;
                    %calibration_scale=pspCoeff*10^-6;      % V/(W/m^2)
                    %pspCalc=psp/(pspCoeff*10^-6*1000);     % W/m^2
                    q_name_print=[q_name(1:end-2) 'W/m^2']; % !!! name changed to real units
                elseif strcmp(channel_id,'mwx1') & strcmp(sub_channel_id,'MET') & strcmp(q_name(1:3),'PIR')
                    % Constants.
                    Sigma_pir=5.6704e-8;
                    % PIR coefficient.
                    pirCoeff=4.11;
                    %calibration_scale=pirCoeff*10^-6;      % V/(W/m^2)
                    %pirCalc=pir/(pirCoeff*10^-6*1000);     % W/m^2
                    q_name_print=[q_name(1:end-2) 'W/m^2']; % !!! name changed to real units
                end
            end % For if strcmp(dataConvert,'yes')
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%  Collect  ALL DAYS DATA    %%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            mi_ma_before=[];
            mi_ma_after=[];
            DATA_all=[];
            days=[];
            q_note='';
            %day_list=[361:365 1:36];
            day_list=8:36;
            if strcmp(channel_id,'gen1')
                % Day #22 is not available - exclude from the list.
                r_22=find(day_list(:)==22);
                if ~isempty(r_22), day_list(r_22)=[]; end
            end
            %***********************************************************
            %******************        LOOP 4 - DAY      ***************
            %***********************************************************
            for day=day_list
                days=[days day];
                if day>=361
                    year=2011;
                else
                    year=2012;
                end
                if day<10
                    day_str=['d00' num2str(day)];
                elseif day>=10 & day<100
                    day_str=['d0' num2str(day)];
                else
                    day_str=['d' num2str(day)];
                end
                % Date.
                date_num=datenum([year-1 12 31 24 0 0])+(day-1);
                date_vec=datevec(date_num);
                month=date_vec(1,2);
                date_day=date_vec(1,3);
                clear date_num date_vec;
                %$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
                %$$$$$$$$$$$$$$$$  LOAD UNDERWAY merged data  $$$$$$$$$$$$
                %$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
                fname_uw=['NBP1201_uw_' channel_id sub_channel_id_str '_' day_str];
                fname_uw_text=['NBP1201\_uw\_' channel_id sub_channel_id_text '\_' day_str];
                p=['load ' path_name_uw fname_uw '.' fname_ext_uw ';'];
                eval(p);
                p=['d_uw=' fname_uw ';'];
                eval(p);
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%%  Make West Lon positive %%%%%%%%%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % Make W Lon positive - all E lon
                r_w=find(d_uw(:,colLon_M)<0);
                d_uw(r_w,colLon_M)=d_uw(r_w,colLon_M)+360;
                clear r_w;
                %%%subname_str='';
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%%   pco2 vs FLOW SOURCE   %%%%%%%%%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                if strcmp(channel_id,'pco2') & ~isempty(flowCode)
                    r_use=find(d_uw(:,colFlowSource)==flowCode);
                    d_uw=d_uw(r_use,:);
                end
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%  E X C L U D E  NaN data  %%%%%%%%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                r_nan=find(isnan(d_uw(:,colNum)));
                d_uw(r_nan,:)=[];
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%%   C O R R E C T I O N   %%%%%%%%%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % ------ C O R R E C T I O N  - svp1 (Sound velocity), pco2 NEGATIVES ---------
                if strcmp(channel_id,'svp1') | ...
                        ( strcmp(channel_id,'pco2') & strcmp(q_name,'pCO2 pressure microAtm') ) | ...
                        ( strcmp(channel_id,'pco2') & strcmp(q_name,'Concentration ppm') ) | ...
                        ( strcmp(channel_id,'mwx1') & strcmp(q_name,'Air Relative Humidity %') )
                    %%%%| ...  ( strcmp(channel_id,'mwx1') & strcmp(q_name,'Enclosure Relative Humidity %') )
                    % Enclosure Relative Humidity NOTE. Range: (-44):25. Minus - not clear???
                    r_minus=find(d_uw(:,colNum)<0);
                    if ~isempty(r_minus)
                        d_uw(r_minus,:)=[];
                        q_note=strvcat('NOTE:',q_name,[num2str(length(r_minus)) ' NEGATIVES have been removed']);
                    end
                end
                % ------ C O R R E C T I O N  -  svp1 (Sound velocity), pco2 NEGATIVES END ---
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%%%  DATA %%%%%%%%%%%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%
                if isempty(d_uw)
                    %disp(['Day #' num2str(day) ': ' channel_id  ' ' q_name ' ' subname_str ': no data'])
                    disp(['Day #' num2str(day) ': ' channel_id  ' ' q_name ': no data'])
                    return;
                else % For if isempty(d_uw)
                    %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                    %@@@@@@@@@@@@@       DATA Conversion       @@@@@@@@@@@@@
                    %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                    if strcmp(dataConvert,'yes')
                        % ---- convert to physical units.
                        if strcmp(channel_id,'hdas')
                            if strcmp(q_name,'Fluorometer mV')
                                mi_ma_before= [mi_ma_before;min(d_uw(:,colNum)) max(d_uw(:,colNum))];
                                d_uw(:,colNum)=(d_uw(:,colNum)/1000-Dark)*SF;
                                mi_ma_after= [mi_ma_after;min(d_uw(:,colNum)) max(d_uw(:,colNum))];
                                r_minus=find(d_uw(:,colNum)<0);
                                if ~isempty(r_minus)
                                    d_uw(r_minus,colNum)=0;
                                    q_note=strvcat('NOTE:',q_name,[num2str(length(r_minus)) ' NEGATIVES have been replaced with zero']);
                                end
                            elseif strcmp(q_name,'Transmissometer mV')
                                mi_ma_before= [mi_ma_before;min(d_uw(:,colNum)) max(d_uw(:,colNum))];
                                d_uw(:,colNum)=( (d_uw(:,colNum)/1000-Vdark)/(Vref-Vdark) ) *100;
                                mi_ma_after= [mi_ma_after;min(d_uw(:,colNum)) max(d_uw(:,colNum))];
                                r_minus=find(d_uw(:,colNum)<0);
                                if ~isempty(r_minus)
                                    d_uw(r_minus,:)=[];
                                    q_note=strvcat('NOTE:',q_name,[num2str(length(r_minus)) ' NEGATIVES removed']);
                                end
                            end
                        elseif strcmp(channel_id,'mwx1') & strcmp(sub_channel_id,'MET')
                            if strcmp(q_name(1:3),'PAR')
                                % Convert mV to muEinstiens/m^2sec.
                                %parCalc=(par-parcv) * parc1 * 10000; % muEinstiens/m^2sec
                                d_uw(:,colNum)=(d_uw(:,colNum)/1000-parcv) * parc1 * 10000; % muEinstiens/m^2sec
                            elseif strcmp(q_name(1:3),'PSP')
                                % Convert mV to W/m^2.
                                %pspCalc=psp/(pspCoeff*10^-6*1000);     % W/m^2
                                d_uw(:,colNum)=(d_uw(:,colNum)*1000) / pspCoeff;
                            elseif strcmp(q_name(1:3),'PIR')
                                % (1) ---- Convert mV to W/m^2.
                                %pirCalc=pir/(pirCoeff*10^-6*1000);     % W/m^2
                                pirCalc=(d_uw(:,colNum)*1000) / pirCoeff;
                                % (2) ---- Correct for case temperature.
                                pir_case=d_uw(:,15);
                                pirCalc_plus=Sigma_pir * pir_case;
                                % (3) ---- Correct for dome temperature.
                                % dome constant =3.5;
                                pir_dome=d_uw(:,16);
                                pirCalc_minus=3.5 * Sigma_pir * (pir_dome-pir_case);
                                pirCalc=pirCalc + pirCalc_plus - pirCalc_minus;
                                d_uw(:,colNum)=pirCalc;
                            end
                        end % For if strcmp(channel_id,'hdas')
                    end % For if strcmp(dataConvert,'yes')
                    %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                    DATA_all=[DATA_all;d_uw];
                end % For if isempty(d_uw)
                clear d_uw;
            end % for day=day_list
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%    DATA   AVERAGING        %%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            if strcmp(average_option,'yes')
                DATA=[];
                d_list=unique(DATA_all(:,colDay_M));
                for t_d=d_list'
                    r_d=find( DATA_all(:,colDay_M)==t_d );
                    data_d=DATA_all(r_d,:);
                    h_list=unique(data_d(:,colHour_M));
                    for t_h=h_list'
                        r_h=find( data_d(:,colHour_M)==t_h );
                        data_h=data_d(r_h,:);
                        min_list=unique(data_h(:,colMin_M));
                        for t_m=min_list'
                            % One minute data.
                            r_m=find( data_h(:,colMin_M)==t_m );
                            data_m=data_h(r_m,:);
                            % Take the last measurement for time and location with
                            % averaged quantity.
                            data_m_av=data_h(r_m(end),:);
                            data_m_av(1,colNum)=mean(data_m(:,colNum));
                            DATA=[DATA;data_m_av];
                            clear data_m data_m_av;
                        end
                        clear data_h;
                    end
                    clear data_d;
                end
                clear t_h r_h t_m t_h t_d r_d;
            else
                DATA=DATA_all;
            end
            % Quantity.
            q_uw=DATA(:,colNum);
            % Min, max.
            data_min_max=[min(q_uw) max(q_uw)];
            % Quantity min, max.
            q_min_d = data_min_max(1,1);
            q_max_d = data_min_max(1,2);
            % Location.
            lat_uw=DATA(:,colLat_M);
            lon_uw=DATA(:,colLon_M);
            if ~isempty(q_note), ...
                    disp(q_note); end
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%          PLOT              %%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %*********************
            % Set axes position.
            %*********************
            gca;
            aPos=get(gca,'position');
            clf;
            hhh=axes('position',[aPos(1)-aPos(3)*0.06 aPos(2)-aPos(4)*0.022 aPos(3)*kk_x aPos(4)*kk_y]);
            if q_min_d==q_max_d
                %***********************************************************
                %*******************       MIN=MAX      ********************
                %***************   PLOT locations only  ********************
                %***********************************************************
                hold on;
                plot(lon_uw,lat_uw,'o','color',[0 0 1],'markersize',marker_size);
                hold off;
            elseif isempty(f_colormap_vector(q_min_d,q_max_d,n_bands,n_dec_chars))
                %***********************************************************
                %******************  Range is too small  *******************
                %***********  PLOT locations only,Place Note ***************
                %***********************************************************
                hold on;
                plot(lon_uw,lat_uw,'o','color',[0 0 1],'markersize',marker_size);
                hold off;
                text(lonmin+range([lonmin lonmax])*0.1,latmin+range([latmin latmax])*0.93,'Data range is too small to plot colormap',...
                    'fontsize',fnt_text,'fontname',fnt_name,'horizontalAlignment','left');
            else % For if q_min_d==q_max_d
                %***********************************************************
                %** PLOT locations colored in accordance with the colormap *
                %***********************************************************
                % Calculate colormap vector.
                if strcmp(channel_id,'hdas') & strcmp(q_name,'Transmissometer mV') & strcmp(dataConvert,'yes')
                    vv=f_colormap_vector(q_min_d+40,q_max_d,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'pco2') & strcmp(q_name,'Flow rate ml/min')
                    vv=f_colormap_vector(q_min_d+50,q_max_d,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'pco2') & strcmp(q_name,'Barometer MBar')
                    vv=f_colormap_vector(q_min_d+366,q_max_d,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'pco2') & strcmp(q_name,'Raw voltage (IR) mV')
                    vv=f_colormap_vector(q_min_d+1100,q_max_d,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'pco2') & strcmp(q_name,'Cell temperature degC')
                    vv=f_colormap_vector(q_min_d+38,q_max_d,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'svp1') & strcmp(q_name,'Sound velocity in ADCP sonar well m/s')
                    vv=f_colormap_vector(q_min_d,q_max_d-13,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'eng1') & strcmp(q_name,'Pump #1 flow rate L/min')
                    vv=f_colormap_vector(200,q_max_d,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'eng1') & strcmp(q_name,'Pump #2 flow rate L/min')
                    vv=f_colormap_vector(295,q_max_d,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'eng1') & strcmp(q_name,'Pump #3 flow rate L/min')
                    vv=f_colormap_vector(216,222,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'knud') & strcmp(q_name,'Low frequency depth meters')
                    vv=f_colormap_vector(0,1000,n_bands,n_dec_chars);
                elseif strcmp(channel_id,'hdas') & strcmp(q_name,'Fluorometer mV') & strcmp(dataConvert,'yes')
                    vv=f_colormap_vector(0,12,n_bands,n_dec_chars);
                else
                    vv=f_colormap_vector(q_min_d,q_max_d,n_bands,n_dec_chars);
                end
                if isempty(vv)
                    disp(strvcat('Data range is too small to calculate the colormap vector',...
                        'Try to increase the number of decimal points (function f_colormap_vector input variable n_dec_chars'));
                    return;
                end
                colormap(cmap);
                %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                %@@@@@@  PLOT markers in COLORMAP color @@@@@@@@@@@@
                %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                hold on;
                D=[];
                for jj=1:size(q_uw,1)
                    q_j=q_uw(jj,1);
                    lat=lat_uw(jj,1);
                    lon=lon_uw(jj,1);
                    countSearch=0;
                    for ii=1:length(vv)-1
                        countSearch=countSearch+1;
                        if q_j<=vv(1)
                            color_j=cmap(1,:);
                        elseif q_j>=vv(end)
                            color_j=cmap(end,:);
                        elseif q_j>=vv(ii) & q_j<vv(ii+1)
                            color_j=cmap(ii,:);
                        else
                            color_j=[];
                        end
                        if ~isempty(color_j), break; end
                    end
                    if ~isempty(color_j)
                        % Check.
                        D=[D;q_j ii cmap(ii,:)];
                        % Plot marker in colormap color.
                        plot(lon,lat,'o','markersize',marker_size,...
                            'markeredgecolor',color_j,'markerfacecolor',color_j,...
                            'linewidth',0.3);
                    else % For if ~isempty(color_j)
                        disp(['Color not found for the point: #' num2str(jj) ', value=' num2str(q_j)]);
                        return;
                    end % For  if ~isempty(color_j)
                    clear q_j lat lon color_j;
                end
                hold off;
                %***********************************************************
                %***********************   COLORBAR   **********************
                %***********************************************************
                cb_ticklabel=vv;
                caxis([min(cval) max(cval)]);
                h1 = colorbar;
                aPos = get(h1,'Position');
                set(h1,'Position',[aPos(1,1)+aPos(1,3)*1.7 aPos(1,2)+aPos(1,4)*0.1 aPos(1,3)*0.45 aPos(1,4)*0.8]);
                set(h1,'ytick',cval,...
                    'yLim',[min(cval) max(cval)],...
                    'ytick',cval,'yticklabel',cb_ticklabel,...
                    'Fontsize',fnt_axis,'ticklength',get(h1,'ticklength')*3.5,...
                    'xcolor',[0 0 0],'ycolor',[0 0 0],'fontname',fnt_name);
            end % For if q_min_d==q_max_d
            %***********************************************************
            %********************   PLOT Min/Max   *********************
            %***********************************************************
            str_mi_ma(1)={['Min = ' num2str(data_min_max(1,1),'%6.4f')]};
            str_mi_ma(2)={['Max = ' num2str(data_min_max(1,2),'%6.4f')]};
            text(lonmin+range([lonmin lonmax])*0.1,latmin+range([latmin latmax])*0.85,str_mi_ma,...
                'fontsize',fnt_text,'fontname',fnt_name,'horizontalAlignment','left');
            %***********************************************************
            %*****************  Set Axes Properties  *******************
            %***********************************************************
            axis([lonmin lonmax latmin latmax]);
            set(gca,'PlotBoxAspectRatio',[1 relyx*1 1],...
                'tickdir','out',...
                'ticklength',(get(gca,'ticklength')*0.6),...
                'fontname',fnt_name,'Fontsize',fnt_axis,...
                'box','on');
            ylabel('Latitude','fontname',fnt_name,...
                'Fontsize',fnt_lbl);
            set(gca,'ytick',y_tick,'yticklabel',y_tick_label);
            xlabel('Longitude','fontname',fnt_name,...
                'Fontsize',fnt_lbl);
            set(gca,'xtick',x_tick,'xticklabel',x_tick_label);
            source_title=[source '/' channel_id sub_channel_id_title];
            title([crName ' UW ' source_title flowName_title ': ' q_name_print ' ' average_note],...
                'fontname',fnt_name,...
                'Fontsize',fnt_title,'color',[0 0 0]);
            if strcmp(plotTimeMarkes,'yes')
                %***********************************************************
                %*****************  PLOT TIME Markers    *******************
                %***********************************************************
                % Decimal hour.
                h_dec_N=DATA_all(:,colHour_M) + ( DATA_all(:,colMin_M) + DATA_all(:,colSec_M)/60 )/60;
                %d_hour=[0 4 8 12 16 20 23];
                hour_n=0;
                %date_format='yyyy-mm-dd HH:MM:SS';
                date_format='mm/dd/yy HH:MM';
                %date_format='mm/dd/yyyy HH:MM:SS';
                %date_format='mm/dd';
                %date_format='yyyy/mm/dd HH:MM';
                %date_format='HH:MM';
                dd_year=2012;%%%%%%
                hold on;
                for dday=[8]%days
                    % Date.
                    date_num=datenum([dd_year-1 12 31 24 0 0])+(dday-1);%%%%%%%%%%%
                    date_vec=datevec(date_num);
                    year_n=date_vec(1,1);
                    month_n=date_vec(1,2);
                    date_day_n=date_vec(1,3);
                    %%%%%%%%%%%%%%%%%%%%%%%%%
                    % Find selected date/time from all the data.
                    if strcmp(day_format,'day_of_date')
                        r_h=find(DATA_all(:,colYear_M)==year_n-2000 & DATA_all(:,colMonth_M)==month_n & DATA_all(:,colDay_M)==date_day_n & ...
                            DATA_all(:,colHour_M)==hour_n &  DATA_all(:,colMin_M)==0 & DATA_all(:,colMin_M)<=6);
                    elseif strcmp(day_format,'day_of_year')
                        r_h=find(DATA_all(:,colYear_M)==year_n-2000 & DATA_all(:,colDay_M)==dday & ...
                            DATA_all(:,colHour_M)==hour_n & DATA_all(:,colMin_M)>=0  & DATA_all(:,colMin_M)<=6);
                    else
                        disp('Not considered date format');
                        return;
                    end
                    %%%%%%%%%%%%%%%%%%%%%%%%%
                    if ~isempty(r_h)
                        % use the point with min HOUR time.
                        [mi,rmi]=min(h_dec_N(r_h,1));
                        lat=DATA_all(r_h(rmi),colLat_M);
                        lon=DATA_all(r_h(rmi),colLon_M);
                        dnum_n=datenum([year_n month_n date_day_n hour_n DATA_all(r_h(rmi),colMin_M) 0]);
                        text(lon,lat+range([latmin latmax])*0.015,['  ' datestr(dnum_n,date_format)],...
                            'fontsize',fnt_text,'fontname',fnt_name,'horizontalAlignment','right','color',[0 0 0]);
                        plot(lon,lat,'k+','markersize',marker_size_time,'markerEdgeColor',[0 0 0]);
                    end
                end % For  for dday=days
                hold off;
            end % For if strcmp(plotTimeMarkes,'yes')
            %if ~isempty(q_note), ...
            %        disp(q_note); end
            if strcmp(saveOption,'yes')
                fig=get(0,'currentFigure');
                pause(0.01);
                q_name_print=q_name;
                space_nums=strfind(q_name_print,' ');
                q_name_print(space_nums)='';
                bracket_nums=strfind(q_name_print,'(');
                q_name_print(bracket_nums)='';
                q_name_print=strrep(q_name_print,'#','');
                fname_out=['nbp1201_uw_rvdas_' channel_id sub_channel_id_str '_' q_name_print(1:chars_use) flowName_str '.png'];
                %%%p=['print -r120 ' fname_out ' -dpng -f' num2str(fig)];
                p=['print ' fname_out ' -dpng -f' num2str(fig)];
                eval(p);
                fname_out_all=strvcat(fname_out_all,fname_out);
                fname_out
                estimated_time_minutes=etime(clock,t0)/60
                clear fname_out q_name_print q_name;
                clf;
                figure(fig);
            end
            clear DATA DATA_all q_uw lat_uw lon_uw data_min_max;
        end % For for par_num=1:size(par_list,1)
        clear sub_channel_id  sub_channel_id_str  flowName_str;
    end % For for sub_channel_num=sub_channel_list
    clear channel_id;
end % For for channel_num=1:size(channel_name_list,1)
estimated_time_minutes=etime(clock,t0)/60