%  1.  NAME
%
%  detide - Detide ADCP data
%
%  2.  USAGE
%
%  detide
%
%  3.  DESCRIPTION
%
%  Detide reads ADCP currents from ascii files and corresponding
%  barotropic tidal components. It rewrites the ADCP files with detided
%  currents. Tidal data is interpolated to ADCP positions along the
%  specified synchro column, and substracted to ADCP U-V pairs. Currents
%  must be in m/s. If this is not the case, specify conversion factors
%  different from 1.0.
%
%  Detided data are written as is, in the same unit as on input in the
%  output file.
%
%  4.  PARAMETERS
%
%  The following parameters must be edited:
%
%     adcp_file
%        File containing ADCP data.
%
%     tide_file
%        File containing barotropic tidal components at ADCP times and
%        positions.
%
%     adcp_col
%        Cell array containg 2-elements vectors specifying the columns of
%        U-V pairs data in the ADCP data file.
%
%     tide_col
%        A two elements vector specifying the columns of U-V barotropic
%        tidal pairs in the tide file.
%
%     synchro
%        A two element vector specifying the columns to use for
%        synchronization of ADCP and tide data. The first element refers
%        to the column in the ADCP file,the second is for the tide file.
%
%     adcp_scale
%        Factor to get ADCP data in m/s.
%
%     tide_scale
%        Factor to get tide currents in m/s.
%
%     out_file
%        Name of output file for detided ADCP data.
%
%  5.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

% Edit following parameters:
%------------------------------------------------------------------

adcp_file = 'vec_all.vec';
tide_file = '../../../../dnmi/adcp/skag/001/adcp.tid';
out_file  = 'test.vec';
adcp_col  = { [3,4], [5,6], [7,8], [9,10] };
tide_col  = [3,4];
synchro   = [2,2];
adcp_scale= 1.0;
tide_scale= 1.0;

%------------------------------------------------------------------
% No more editing!!!

%--- Read ADCP data:
adcp = tonan(read_asc(adcp_file));
as = synchro(1);
nc = size(adcp,2);

%--- Read tide data:
tide = tonan(read_asc(tide_file));
ts = synchro(2);

%--- Get grid and remove NaN values from the synchro column:
[ adcp_grd, adcp ] = ascii_grd(adcp, as);
[ tide_grd, tide ] = ascii_grd(tide, ts);
adcp_grd = adcp_grd{1};
nt = size(tide,1);

%--- Interpolate tide values on ADCP grid:
fix_u = interp1(tide(:,ts), tide(:, tide_col(1)), adcp_grd);
fix_v = interp1(tide(:,ts), tide(:, tide_col(2)), adcp_grd);

% Now, fix_u and fix_v should be the tidal components at ADCP positions
% specified in adcp_grd. We can begin to apply the corrections. We build first
% an array of indexes from ADCP input data to the correction currents:

adcp = sortrows(adcp, as);
ng = length(adcp_grd);
ind = NaN * ones(ng, 2);
for i=1:ng,
	ii = find(adcp(:, as) == adcp_grd(i));
	if isempty(ii),
		error([ 'Cannot find ADCP positios' ]);
	end
	ind(i,:) = [ min(ii), max(ii) ];
end

%--- List of columns to be detided:
np = length(adcp_col);
iu = NaN * ones(1,np);
iv = NaN * ones(1,np);
vv = 1:nc;
for i=1:np,
	cols = adcp_col{i};
	iu(i) = cols(1);
	iv(i) = cols(2);
	vv = (vv ~= cols(1));
	vv = (vv ~= cols(2));
end
vv = find(vv);

%--- Loop through the ADCP U-V pairs:
nl = size(adcp,1);
detided = NaN * ones(nl, nc);
detided(:, vv) = adcp(:,vv);
for i=1:ng,
	ii = ind(i,1):ind(i,2);
	detided(ii,iu) = ((adcp(ii,iu)*adcp_scale) - ...
			(fix_u(i)*tide_scale))/adcp_scale;
	detided(ii,iv) = ((adcp(ii,iv)*adcp_scale) - ...
			(fix_v(i)*tide_scale))/adcp_scale;
end

%--- Print output:
fid = fopen(out_file, 'w');
for n=1:nl,
	line = sprintf(' %15.8f', detided(n,:));
	line = strrep(line, ' NaN', '1E38');
	fprintf(fid, '%s\n', line);
end
fclose(fid);



			
	



