%  printSigonSection.m    - print potential density from ctd on a vertical section.
%
%   need variables set: RefPress xrange deprange ctdcastlist
%                       DataDir CruiseName DepthResolution Line
%                       InfoX InfoZ InfoString SectionNx
%   sets variables: none
%   calls: readctd swstate
%
MaxNz=33;
%      standard depths (33 total)
%stddep=[0 10 20 30 50 75 100 125 150 200 250 300 400 500 600 700 800 900 ...
%    1000 1100 1200 1300 1400 1500 1750 2000 2500 3000 3500 4000 4500 5000 5500]';

figtitle=['Potential Density for ' Line ' Section'];

%        open an new graphics window.
figure;

Darray=ones(MaxNz,SectionNx);
Zarray=ones(MaxNz,SectionNx);
Xarray=ones(MaxNz,SectionNx);
bottom=ones(1,SectionNx);
X=ones(1,SectionNx);
Y=ones(1,SectionNx);

[n,stations]=size(ctdcastlist);
Casts=0;
for station=1:stations
%           count number of casts
   Casts=Casts+1;
   cast=ctdcastlist(station);
   file= [DataDir '/' CruiseName '/Ctd/' DepthResolution '/ctd' castnum(cast) '.dat'];
   [nlines,ndepths,desc,p,t,s,pt,r0,r1,bv,t0,t1,s0,s1,o2,par,tr,fl] = readctd(file);
   x=ctdxgrid(cast);
   y=ctdygrid(cast);
   bdepth=ctdtotdepth(cast);
%          convert pressure to depth
   Z=-p;
   [SVAN,Pden]=swstate(s,pt,RefPress);
%              fill array with observations
   bottom(1,station)=-bdepth;
   X(1,station)=x;
   Y(1,station)=y;
   [nz,n]=size(Z);
   Darray(1:nz,station)=Pden;
   Zarray(1:nz,station)=Z;
%   ri=interp1(Z,Pden,zi,'linear');
%   Darray(1:MaxNz,station)=ri';
%   Zarray(1:MaxNz,station)=zi';
end
dx=X(1,1:station)-X(1,1);
dy=Y(1,1:station)-Y(1,1);
dx2=dx.*dx;
dy2=dy.*dy;
if Line(1) == 'x'
%             cross shelf arrays
   Xarray(1,1:station)=Y(1:station);
elseif Line(1) == 'y'
%             along shelf arrays
   Xarray(1,1:station)=X(1:station);
else
   Xarray(1,1:station)=sqrt( (X(1,1:station)-X(1,1)).^2 ...
                            +(Y(1,1:station)-Y(1,1)).^2);
end
for i=2:MaxNz
   Xarray(i,1:station)=Xarray(1,1:station);
end

i=find(~isnan(Darray));
text(Xarray(i),Zarray(i),num2str(Darray(i),3),'Color',[1 0 0], ...
     'FontSize',8,'HorizontalAlignment','center');

title(figtitle);
xlabel('distance (km)');
ylabel('depth(m)');
text(InfoX,InfoZ,InfoString);
text(InfoX,1.1*InfoZ,CruiseName);
axis([xrange deprange]);
hold on;
plot(Xarray(1,1:station),bottom(1:station));
%for station=1:stations
%  text(Xarray(1,station),-20,'T');
%end
hold off;

%disp('printSigonSection is done')
%    end of printSigonSection.m
