% plot_os75.m
% =====================================================
% plot velocity vectors using OS75 adcp data
% =====================================================

function [f]=plot_os75(st,et)

clf
set(gcf,'color','w');

% load CODAS output data: contour
load /home/howitt/adcp/oc415/contour/contour_uv
load /home/howitt/adcp/oc415/contour/contour_xy

% averaging through the depth 250 m (22 x 10m + 30 m); 
% codas output is 10 m bins (8 m is setup of OS75)
nd=22;
u=uv(1:nd,1:2:end);
v=uv(1:nd,2:2:end);
% get averages         
um=nanmean(u);vm=nanmean(v);
lon=xyt(1,:)-360;
lat=xyt(2,:);
time=xyt(3,:);
% set up time limit: it's not necessary to display the whole
% record from the very beginning; so, let's start from some point n1 
% until n2
%n1=1;n2=length(um);
%% CHANGE PLOT START TIME BY CHANGING n1 - CJS
%n0 = 1;
%n1=900;
%n2=length(um);

timet = datenum(time+datenum(2005,1,1));

st_dnum = datenum(st);
findst = find(min(abs(timet-st_dnum)) == abs(timet-st_dnum));
n1 = findst;

if strcmpi(et,'now')
  et_dnum = now;
  n2 = length(um);
else
  et_dnum = datenum(et);
  findet = find(min(abs(timet-et_dnum)) == abs(timet-et_dnum));
  n2 = findet;
end

% avoid program crash if last point has bad GPS fix
while isnan(lat(n2))
  n2 = n2 - 1;
end

startt_str=datestr(time(n1)+datenum(2005,1,1),0);
endt_str=datestr(time(n2)+datenum(2005,1,1),0);

% this line is to stretch a figure whether ship track is 
% meridional
plot([-67. -66.2],[30.8 30.8],'w-');
set(gca,'fontsize',16,'fontweight','bold')
hold on
h(1) = plot(lon(n1:n2),lat(n1:n2),'m-','linewidth',2);
ss=find(~isnan(lon)==1);
n2=ss(end)

%add adcp vectors
vecplot2(lon(n1:n2),lat(n1:n2),um(n1:n2),vm(n1:n2),1.0,'cm/sec', -69.7,30.0)

% add position of last ADCP fix
h(2)=plot(lon(n2),lat(n2),'d','markersize',10,'markerfacecolor','c',...
     'markeredgecolor','r');
% add current ship position
mm = datestr(now,5);
dd  = datestr(now,7);
if 1
  % stop doing this while GPS not available 7/26/05 CJS
  [lon,lat,year,month,day,hour,minit,sec]=get_gps(mm,dd);
  h(3)=plot(lon(end),lat(end),'d','color','k','markersize',10,'markerfacecolor','m');
  % this adds an 'icon' of the oceanus but had trouble with it. and
  % legend needs fixing
  %add_OC(lon(end),lat(end),8);

  % add ship track between current position and last adcp fix
  track_index = find(datenum(year,month,day,hour,minit,sec)> ...
		     datenum(time(n2)+datenum(2005,1,1)));
  plot(lon(track_index),lat(track_index),'m','linewidth',2)
end

grid

title(['OS75: time (GMT): ',startt_str,' -- ',endt_str]);

% add the legend
legend(h,'Ship Track','Last ADCP','Current Ship')

xlabel('Longitude')
ylabel('Latitude')

% change final axis limits here:
ax=axis;
axrat = 1/cos(mean(ax(3:4))*pi/180);
set(gca,'DataAspectRatio',[1 1/axrat 1]);
axis([-70.1 -69.1 29.4 30.1])

set(gcf,'position',[1 55 1280 897])

% add the uimenu
adcp_uimenu
