function [time,nav,zc,bt_depth,ABS_U,ABS_V,Sv] = ...
	ftchadcp(pgnum, starttime, endtime)
%function [time,nav,zc,bt_depth,ABS_U,ABS_V,Sv] = ...
%	ftchadcp(pgnum, starttime, endtime)
%
%ftchadcp calls rtldit.m to extract selected variables over a specified 
%time range from a pingdata file into Matlab .mat files

%CNF & YS, 12/11/98
% CNF, 1/11/99 fixed a bug in the calculation of Sv involving the 
% indexing of R in the bscat_1 routine

global NPRFS
% >>>>>>>>>>>>>>>>>>>>> edit the following lines >>>>>>>>>>>>>>>>>>>>>>

global trans_depth Tx Sal PHI

%trans_depth = 5;       % Transducer depth
%Tx = 15;	       % Transducer temp
%Sal = 32.0;	       % Surface salinity
%PHI = 6.63; %EN302    % Rotation angle to correct alignment of ADCP
%PHI = -0.92; %OC322    % It has the opposite sign as the total rotation
		      % determined during the bottom track calibration.set(

[time, u ,v ,pg ,nav, amp, pgnum, binsize, PL, blank, nbins, bt_depth] = ...
	rtlditx(pgnum, starttime, endtime);

fbin = 3;              	% first bin of the reference layer
lbin = 20;             	% last bin of the reference layer
pgc  = 30;             	% percent good cutoff for editing velocities
filter_width = 5;      	% size of fix window for smoothing--must be odd-valued!
iterations = 1;        	% no. of smoothing iterations
firstbin=13;          	%depth of first bin in meters, for plotting
crange = [-0.5 0.5];        % velocity range for color plot
arange = [-100 -50];     	% range corrected backscatter intensity range
nn = 300;               	% max. number of profiles to be plotted
			% 288=12*24=one day of 5-min profiles

pos1 = [ 0.0900    0.0060    0.9000    0.9000];

colors = 'jet'; %choose a colorbar

% <<<<<<<<<<<<<<<<<<<<<<<<<< end editing <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

[NBINS NPRFS] = size(u);

%--- rotate velocities for transducer missalignment
up = u*cos(PHI*pi/180) + v*sin(PHI*pi/180);
vp = -u*sin(PHI*pi/180) + v*cos(PHI*pi/180);
u = up;  v = vp;  clear up vp


%--- edit velocity data according to percent good ---
if exist('pg')
  ibad = find(pg < pgc);
  u(ibad) = NaN * ibad;
  v(ibad) = NaN * ibad;
end

%--- average relative reference layer velocities, ignoring NaNs ---
rel_ref_U = [mean_nan(u(fbin:lbin,:)); mean_nan(v(fbin:lbin,:))];

%--- check/interpolate fixes ---

nav = [nav(2,:); nav(1,:)];                   % make it lon-lat! 
ibad = find(nav(1,:) == 0 & nav(2,:) == 0 | ...
	    abs(nav(1,:)) > 180.0 | ...
	    abs(nav(2,:)) > 90.0 |...
	    isnan(rel_ref_U(1,:)) );            % find bad fixes 
if length(ibad) > 0
  nav(:,ibad) = NaN * ones(2, length(ibad));  % set bads to NaN
end

LONLAT = intrpnav(nav, rel_ref_U, time);      % interpolate over NaNs

%--- calculate and smooth absolute ref. layer velocities ---
abs_U = refabs(rel_ref_U, time, LONLAT);
sm_U = smoothr(abs_U, filter_width, iterations);

%--- reference the ADCP velocity (all bins) ---
ABS_U = ones(NBINS,1) * (sm_U(1,:) - rel_ref_U(1,:)) + u;
ABS_V = ones(NBINS,1) * (sm_U(2,:) - rel_ref_U(2,:)) + v;

i=find(pg < 30);
ABS_U(i)=NaN*ones(size(ABS_U(i)));
ABS_V(i)=NaN*ones(size(ABS_U(i)));

% Calibrate and range correct the backscatter intensity

xx = ABS_U;		% form array with 1's and nan's to edit amp
i = find(xx == 0.0);
if (length(i) ~= 0)
	xx(i) = i./i;	% turn zeros to ones
end
xx = xx ./ xx;
amp = amp .* xx;
R = s_range(blank,PL,binsize,NBINS,Tx,Sal);
zc = trans_depth + R*cos(30*pi/180);
Sv = nan*ones(NBINS,NPRFS);
for i = 1:NPRFS
	Sv(:,i) = bscat_1(Tx,Sal,amp(:,i), PL(i), R(:,i));
end
i = find(pg < 92);		% Eliminate poor Sv's
Sv(i) = nan*Sv(i);

% Interpolate over bad bottom track depth when the ADCP was
% running in the bottom track mode.  NANs are inserted for those
% ensembles where the bottom tracking was off

bt_depth = intrpbt_new(bt_depth,time);

