% b_read_ctd_matfile_bv18_bv_19.m
% BVAL cruises 18 and 19 CTD data.
%
% I N P U T:
% 
% (1) CTD data file, filename:  1997_bval18.mat;
% contains structure array 'cruise' with the fields:
%      stanum: [1x35 double]
%        time: [35x5 double]
%         lat: [1x35 double]
%         lon: [1x35 double]
%        temp: [358x35 double]
%         sal: [358x35 double]
%           z: [358x1 double]
%    descript: [12x66 char]
% 
% (2) CTD data file, filename:  1997_bval19.mat;
% contains structure array 'cruise' with the fields:
%     stanum: [1x42 double]
%        time: [42x5 double]
%         lat: [1x42 double]
%         lon: [1x42 double]
%        temp: [357x42 double]
%         sal: [357x42 double]
%           z: [357x1 double]
%    descript: [12x67 char]
% 
% Structure array 'cruise' fields and contents:                           
%  stanum   - Cast#                                             
%  time     - Date and time                                     
%  lat      - Latitude (degN)                                   
%  lon      - Longitude (degW)                                  
%  temp     - Temperature (degC)                                
%  sal      - Salinity (psu)                                    
%  z        - Pressure (db)               
% descript  - Description

clear;
flops(0);
%*******************
% C O N S T A N T S
%*******************
% Font size.
fnt_ax=8;
fnt_lbl=9;
fnt_title=9;
fnt_text=7;
line_width=1;
%*********************************************************************
%************************    INPUT DIALOG BOX         ****************
%*********************************************************************
       c1='18';                % Cruise# 
       c2='10';                % Cast# 
       c3 = '0    750';        % Pressure (db): upper and lower
       def = {c1,c2,c3};
       prompt = {'Cruise# (18 or 19)',...
               'Cast# (BV18 1:35, BV19 1:42)',...
               'Pressure limits: Upper Lower'};
       title1 = 'DATA QUERRY';
       lineNo = 1;
       answer = inputdlg(prompt,title1,lineNo,def);
       if isempty(answer)
          return;
       else
% -----Read answers          
          crNum = str2num([answer{1}]);
          stNum = str2num([answer{2}]);
          dep_lim = str2num([answer{3}]);
          upper_dep = dep_lim(1,1);
          lower_dep = dep_lim(1,2);
          %y_axis_range=[-lower_dep -upper_dep];
          y_axis_range=[upper_dep lower_dep];
       end
       if length(stNum)>1 | length(crNum)>1 | upper_dep>=lower_dep
          disp('Incorrect input');
          return;
       end
%*********************************************************************
%***********************       CRUISE INFO         *******************
%*********************************************************************
if crNum==18 
   cruise_name='BVAL Cruise 18';
   cruise_date='July (7 - 13), 1997';
   fname='1997_bval18_ctd';
   st_list=1:35;
elseif crNum==19
   cruise_name='BVAL Cruise 19';
   cruise_date='July (22 - 27), 1997';
   fname='1997_bval19_ctd';
   st_list=1:42;
else
    disp('Wrong cruise number')
    return;
end
if isempty(find(st_list(:)==stNum))
    disp('Wrong station number');
    return;
end
% Cruise title.
cruise_title=[cruise_name ', ' cruise_date];
% Figure.
figure;
set(gcf,'position',[360 327 328 607],'color','w','paperPositionMode','auto');
% Axes for profiles plot.
hs=subplot(1,1,1);
ah=get(hs,'position');
delete(hs);
h(1)=axes('position',[ah(1) ah(2)-ah(4)*0.07 ah(3) ah(4)*0.6]);
%*********************************************************************
%************************     READ DATA FILE    **********************
%*********************************************************************
p=['load ' fname ';'];
eval(p);
pres=cruise.z';
presSt=pres;
lat=cruise.lat';
lon=cruise.lon';
st_list=cruise.stanum;
station_time=cruise.time(stNum,:);
station_time_str=datestr( datenum( station_time(1),station_time(2),...
   station_time(3),station_time(4),station_time(5),0 ),0 );
countAxes=0;
for parNum=1:2
   countAxes=countAxes+1;
   % CONSTANTS depending on Quantity name.
   switch parNum
   case 1
      jN='temp'; % field name
      j_label='Temperature (deg C)';
      plot_id='-';
      plot_color=[0 0 0];
   case 2
      jN='sal'; % field name
      j_label='Salinity (psu)';
      plot_id='-';
      plot_color=[0 0 1];
   end
   % Quantity data.
   p=['parSt = cruise.' jN '(:,stNum);'];
   eval(p);
%*********************************************************************
%*******************  Plot profiles, use multiple X-axis  ************      
%*********************************************************************
   % Axes limits.      
   par_min=min(parSt);
   par_max=max(parSt);
   add_lim=0.05*range([par_min par_max]);
   x_axis_range=[par_min-add_lim par_max+add_lim];
   if countAxes==1
      % plot 1st parameter vs depth
      p=['hl' num2str(countAxes) '=plot(parSt,presSt,plot_id,''color'',plot_color);'];
      eval(p);
      p=['set(hl' num2str(countAxes) ',''linewidth'',line_width);'];
      eval(p);
      % assign current axis handle to variable for later reference if needed
      p=['ax' num2str(countAxes) '=gca;'];
      eval(p);
      % set properties of the axes
      p=['set(ax' num2str(countAxes) ',''fontsize'',fnt_ax,''box'',''on'',''ydir'',''reverse'',''xcolor'',get(hl' num2str(countAxes) ',''color''),''XMinorTick'',''on'');'];
      eval(p);
      % Set axes limits.
      p=['axis([' num2str(x_axis_range) ' ' num2str(y_axis_range) ']);']; 
      eval(p);
      % Title, labels.
      xlabel(j_label,'fontsize',fnt_lbl,'verticalAlignment','cap');
      ylabel('Pressure (db)','fontsize',fnt_lbl,'verticalAlignment','middle');
      title('Temperature and Salinity','fontsize',fnt_lbl,'color',[0.8 0 0]);
   else
      % add 1st floating axis for the second parameter plotted
      p=['[hl' num2str(countAxes) ',ax' num2str(countAxes) ',ax' num2str(countAxes+1) '] = b_floatAxisX(parSt,presSt,plot_id,plot_color,j_label,[' num2str(x_axis_range) ' ' num2str(y_axis_range) ']);'];
      eval(p);
      p=['set(hl' num2str(countAxes) ',''linewidth'',line_width);'];
      eval(p);
      p=['set(ax' num2str(countAxes+1) ',''fontsize'',fnt_ax);'];
      eval(p);
   end
end % For for parNum=1:2
%*********************************************************************
%*******************     Plot Stations Locations  ********************      
%*********************************************************************
% Map limits.
latmin = 32;
latmax = 34.5;
lonmin = 59.4;
lonmax = 62.7;
% Calculate relation relyx = axisy/axisx (for PlotBoxAspectRatio) 
weg = 111*cos(pi*(latmin+(latmax-latmin)/2)/180);
xc = (lonmax-lonmin)*weg;                        % Longitude, km
yc = (latmax-latmin)*111;                        % Latitude, km
relyx = yc/xc;
% Plot stations locations.
hs=subplot(2,1,1);
pos_1=get(hs,'position');
delete(hs);
h(2)=axes('position',[pos_1(1)+pos_1(3)*0.15 pos_1(2)+pos_1(4)*0.1 pos_1(3)*0.7 pos_1(4)*0.9]);
plot(lon,lat,'k+',lon(stNum),lat(stNum),'ro','markersize',3);
axis([lonmin lonmax latmin latmax]);
set(gca,'xdir','reverse','PlotBoxAspectRatio',[1 relyx*1 1],'fontsize',fnt_ax);
% Plot text - station number.
if crNum==19
   ht_1=text(lon(26:42),lat(26:42)+range([latmin latmax])*0.025,num2str(st_list(26:42)'),...
       'horizontalAlignment','center','fontsize',fnt_text,'color','b');
   ht_2=text(lon(1:25)-range([lonmin lonmax])*0.01,lat(1:25),num2str(st_list(1:25)'),...
       'verticalAlignment','middle','fontsize',fnt_text,'color','b');
   if stNum>25
      rt=find([26:42]==stNum);
      delete(ht_1(rt));
      text(lon(stNum),lat(stNum)+range([latmin latmax])*0.025,num2str(stNum),...
         'horizontalAlignment','center','fontsize',fnt_text,'color','r');
   else
      rt=find([1:25]==stNum);
      delete(ht_2(rt));
      text(lon(stNum)-range([lonmin lonmax])*0.01,lat(stNum),num2str(stNum),...
        'verticalAlignment','middle','fontsize',fnt_text,'color','r');
  end
else
   ht=text(lon-range([lonmin lonmax])*0.01,lat,num2str(st_list'),...
       'verticalAlignment','middle','fontsize',fnt_text,'color','b');
   delete(ht(stNum));
   text(lon(stNum)-range([lonmin lonmax])*0.01,lat(stNum),num2str(stNum),...
       'verticalAlignment','middle','fontsize',fnt_text,'color','r');
end
% Title, labels.
xlabel('Longitude (degW)','fontsize',fnt_lbl), ylabel('Latitude (degN)','fontsize',fnt_lbl);
str_t(1)={[cruise_title]};
str_t(2)={[' CTD Station# ' num2str(stNum) ', ' station_time_str]};
text(lonmax-range([lonmin lonmax])*0.5,latmax+range([latmin latmax])*0.2,str_t,...
  'fontsize',fnt_title,'horizontalAlignment','center','color',[0 0 0],'fontname','lucida');
title('Stations Locations','fontsize',fnt_lbl,'color',[0.8 0 0]);
