%			ADCP4Quoddy.m
% This routine retrieves ADCP velocity data from pingdata files 
% for a specified time period using FTCHADCP/RTLDITX/BNL_P2M and then
% plots and QA's using ADCP_QA.  During the QA process any gaps 
% within the velocity arrays are filled through vertical/linear 
% interpolation.  Finally the data are saved in a formated ascii
% file in the .m3d form.  The start and end times have to be 
% edited as necessary.

% CNF 12/14/98
% CNF 1/12/99 Modified so that important constants were brought to this
%	      main routine to simplify operations.

clear all
addpath /data/oc404_1/adcp/work/adcpdb/
addpath /data/oc404_1/adcp/work/analysis/
addpath /data/oc404_1/adcp/work/load_edit/
addpath /data/oc404_1/adcp/codas3/bin/lnx/
addpath /data/oc404_1/adcp/Flagg/matlab/

% ------------  Edit these global constants for sub routines as needed ---

v = version;
if str2num(v(1)) >= 5 
  global trans_depth Tx Sal PHI disk year 
end
% ----------- Edit the Start/End times of the data retrieval ------------

starttime = '2004/05/23-19:55:23';	% format: 'yyyy/mm/dd-hh:mm:ss'
endtime = '2004/07/03-08:46:47';
cruise = 'oc404_1';	        	% Name of the .m3d file
year = 2004;				% Year the data was collected
pgnum = 0;				% Beginning pingdata file number

% -----------------------------------------------------------------------

% Constants for FTCHADCP.M
trans_depth = 5;       % Transducer depth, m
Tx = 26;	       % Transducer temp,  A resonable estimate for T and S is
Sal = 36.0;	% Surface salinity, all that is necessary
PHI= -.4;
disk = ['./work/pingdata/']; % disk and sub-directory with the pingdata files

if str2num(v(1)) < 5 
  global trans_depth Tx Sal PHI disk year 
end

% -----------------------------------------------------------------------
% Retrieve the ADCP data from the pingdata files

[time,nav,zc,bt_depth,ABS_U,ABS_V,Sv] = ...
        ftchadcp_var_ang(pgnum,starttime,endtime); 
   
% Perform a preliminary quality assurance on the velocity data
% Be sure to check the velocity data for any short period fluctuations
% that might be sure to ship motion.  This usually indicates that
% the rotation angle is wrong.  Check to make sure you have the right
% angle for the ship that's being used.

adcp_qa

% At this point we've got all the data needed (nav, time, zc,
% bt_depth, ABS_U and ABS_V).

% Save the results in a matfile:

save adcp4quoddy nav time zc bt_depth ABS_U ABS_V

% The rest of the routine converts the adcp data into
% the .m3d form needed by Quoddy 4 and writes an ascii data file

rfmt4quoddy     % this routine performs the vertical velocity average
		% and copies the needed data into aobs
		% This is also the routine that eliminates those profiles
		% where bottom depth is unavailable due to bottom tracking
		% being turned off.

% Output .m3d file

aa = find((aobs(:,8) - aobs(:,7)) < 0);
%aobs(aa,:) = [];

fname=[cruise,'.m3d'];
fmt=' %f %f %f %f %f %f %f %f\n';
data=aobs;
gridname='stw';
ncol=8;
gmt='gmt';
write_obs(fname,fmt,data,gridname,year,ncol,gmt);

% remove the intermediate files generated by ftchadcp
op_sys = pwd;
if (op_sys(2) == ':')
   eval(['!del out*.mat'])
else
   eval(['!\rm out*.mat'])
end

% a couple quick figures to look at data.
figure(2);
subplot(2,1,1)
plot(aobs(:,1),aobs(:,4),'b-');
hold on
title('u velocity');
grid on

subplot(2,1,2)
plot(aobs(:,1),aobs(:,5),'r-');
hold on
title('v velocity')
grid on
