function timep(t, unit, vars, varargin)
% ------------------------------------------------------------------
% Print valid time ranges.
%
% SYNOPSIS:
%
%   timep(t, unit, vars, options)
%
%      Print valid time ranges according to the tests done defined in
%      global variable LIM. The time variable number is given by 't',
%      and 'unit' is a string specifying the unit of this variable
%      as it is in the log file. Possible unit strings are:
%
%         'yd' : decimal year day
%
%      Argument 'vars' is a vector with variable numbers that should be
%      used for test limits.
%
% A valid time range is defined as a time range for which all
% intervals specified in global variable LIM are satisfactory.
%
% The purpose of this function is to generate time ranges that can
% be easily copied from the ascii output file into further CODAS
% control files.
%
% OPTIONS:
%
%   '-min_gap', min_gap
%      Specify a minimum gap (seconds) between two consecutive time ranges
%      for considering these as separate (default is 300).
%
%   '-min_rng', min_rng
%      Specify a minimum length (seconds) for time ranges that should be
%      considered as satisfactory.
%
% ------------------------------------------------------------------
%Time:<Last updated on 98/10/10 at 21:39:33 by veins@TORSK>
%File:<c:/codas3/gfi/matlab/matlab50/scan_bin/timep.m>

scanglob;

%--- Defaults:
opt.min_gap = 300;
opt.min_rng = 300;

%--- Process unit argument:
switch unit
  case 'yd',
  otherwise,
	 error([ 'Unknown unit ' unit ]);
end
opt.unit = unit;

%--- Get options:
i = 1;
while i<=length(varargin),
  if isstr(varargin{i}),
	 switch(varargin{i})
		case '-min_gap',
		  opt.min_gap = varargin{i+1};
		case '-min_rng',
		  opt.min_rng = varargin{i+1};
		otherwise,
		  error([ 'Unknown options ' varargin{i} ]);
	 end
	 i=i+2;
  else
	 error([ 'Options must be preceded by a string' ]);
  end
end
	 
% Initializations:
startp = 1;
prev = 1; this = 2; start = 1; stop = 2; 
time = ones(2,1) * NaN;
good = zeros(2,1);
range = ones(2,2) * NaN;

% Initialize ascii output file:
str = sprintf('\n TIME RANGES: \n');
str = sprintf('%s\n TIME VARIABLE: %d', str, t);
str = sprintf('%s\n UNIT FOR TIME: %s', str, unit);
str = sprintf('%s\n LIMIT VARIABLES:', str);
for i=1:length(vars),
  str = sprintf('%s %-d', str, vars(i));
end
str = sprintf('%s\n%s\n', str, limp);
str = sprintf('%s\n VALID TIME RANGES:\n\n', str);
logp(10, str);
fprintf('%s', str);

% Loop through the data:
err = 0;
while err == 0,
  
  % Get NPINGS of data:
  err = getp(startp);
  
  % Check if data satisfy limits:
  [DATA, flags] = flagdata(DATA, LIM, '-hdg', HDG);
  
  % Loop through the columns:
  ii = 1:size(DATA,2);
  for i=ii,
	 
	 % Check for unsatisfactory limit tests:
	 bad = find(flags(vars,i) ~= 1);
	 if ~isempty(bad),
		good(this) = -1;
	 else
		good(this) = 1;
	 end
	 time(this) = DATA(t,i);
	 ping(this) = startp + i - 1;

	 % Check for validity changes:
	 if good(this) ~= good(prev),
		if good(this) > 0,
		  % Start of a time range:
		  range(this,start) = time(this);
		  % Initialize the previous start:
		  if isnan(range(prev,start)),
			 range(prev,start) = time(this);
		  end
		  
		elseif (good(this) < 0) & (good(prev) > 0),
		  % End of time range:
		  range(this,stop) = time(prev);
		  % Initialize the previous stop:
		  if isnan(range(prev,stop)),
			 range(prev,stop) = time(this);
		  end
	
		  % Process and log time range data:
		  range = process_time_range(range, opt);
		end
	 end
	 good(prev) = good(this);
	 time(prev) = time(this);
	 ping(prev) = ping(this);

  end
  startp = startp + size(DATA,2);
    
end

% Closing last time range:
if good(this) > 0,
  range(this, stop) = time(this);
  range = process_time_range(range, opt);
end

% Flush time range data:
if ~isnan(range(prev,stop)),
  range(this, start) = range(prev,stop) + ...
		seconds_to_time(opt.min_gap + 1, opt.unit);
  range = process_time_range(range, opt);
end

% Possibility to add comments:
logp(1);
fprintf('\n\n Time ranges have been written into \n\t%s\n\n', ASCFILE);

%------------------------------------------------------------------
function range = process_time_range(range, opt);

% Indexes to range data:
prev = 1; this = 2; start = 1; stop = 2; 

% Calculate the time gap between this and previous time range:
gap = time_to_seconds(range(this,start) - range(prev,stop), opt.unit);
		  
% Check if previous and current time ranges should be considered as
% separate.
log = (gap > opt.min_gap);

% Log and update time ranges:
if log,
  % Check if previous time range is long enough:
  if time_to_seconds(range(prev,stop)-range(prev,start), opt.unit) ...
		  > opt.min_rng,
	 str = sprintf('%s  to  %s\n', ...
		  time_to_string(range(prev,start), opt.unit), ...
		  time_to_string(range(prev,stop), opt.unit));
	 logp(0, str);
	 fprintf('%s', str);
  end
  % Update time range data:
  range(prev,:) = range(this,:);
else
  % Update time range data:
  range(prev,stop) = range(this,stop);
end
range(this, :) = NaN;

return;

% ------------------------------------------------------------------
function s = time_to_string(d, unit)

scanglob;

switch unit
  case 'yd',
	 s = to_date(YEAR_BASE, d, 's');
end

%------------------------------------------------------------------
function dt = time_to_seconds(dt, unit)

switch unit
  case 'yd',
	 dt = dt * 86400.0;
end

return;

%------------------------------------------------------------------
function dt = seconds_to_time(dt, unit)

switch unit
  case 'yd',
	 dt = dt/86400.0;
  otherwise,
	 error([ 'Unknown time unit ' unit ]);
end

return;

