%  1.  NAME
%
%  crfp - Pot raw and smoothed velcities
%
%  2.  USAGE
%
%  crfp
%
%  3.  DESCRIPTION
%
%  This Matlab script is an interface to function crfp2(3m). It generates
%  the input mat files by calling ref2mat and sm2mat on the input files.
%  It also generates several pages, one for each time intervall.
%  Parameters min_speed, max_speed and max_gap_ratio should have the same
%  value as those used in smoothr.
%
%  4.  PARAMETERS
%
%  The following parameters must be edited:
%
%     refabsfile
%        Output from refabs.
%
%     smoothrfile
%        Output from smoothr.
%
%     year_base
%        Base year used in input files.
%
%     start_day
%        Day to start plotting,in decimal days.
%
%     stop_day
%        Day to stop plotting,in decimal days.
%
%     delta_day
%        Time intervall to plot on each page,in decimal days.
%
%     overlap
%        This parameter allows to overlap (if negative) or skip time
%        ranges (if positive) between each pages. It is a time in decimal
%        days.
%
%     outfile
%        Root name of output postscript file. The suffix will be added
%        automatically. If the file exists on entry,it is deleted.
%
%     y0 Minimal velocity on plot axes.
%
%     dy Velocity range on plot axes.
%
%     min_speed
%        Minimal allowed ship speed for processing good fixes.
%
%     max_speed
%        Maximal allowed ship speed for processing good fixes.
%
%     max_gap_ratio
%        The maximal gap ratio allowed for processing good fixes.
%
%     cruise
%        String to insert in the title.
%
%  5.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

% This is a modified version of callrefp, modified as a script, to 
% make things automatically.
%
% Usage:  crfp
% where:  t0 = time origin (for x-axis) in decimal days
%
% callrefp generates the input files and sets up the call for plotting
% the absolute reference layer velocities (smoothed and/or unsmoothed).
%
% It calls the ref2mat and sm2mat programs for converting
% the user-specified input files into Mat-files, and then
% calls the refplot.m function to perform the plotting.
% These programs/function must therefore be in the user's search path.
%
% 98/08/25 PJ
%          Modified to convert the whole files first, and then plot the 
%          data. That is, data must be converted to `mat' files before
%          calling callrefp. This can be done with:
%
%          ref2mat <input> <output>
%          sm2mat  <input> <output>
%
% >>>>>>>>>>>>>>>>>>>> user must edit the following lines >>>>>>>>>>>>>>>>>>>>

refabsfile    = 'nuka.ref';
smoothrfile   = 'nuka.bin';

year_base     = 1999;
start_day     = 189.75;
stop_day      = 197.0;
delta_day     = 2;
overlap       = -0.25;

outfile       = 'crfp';

y0            = -1; 
dy            =  2;  

max_gap_ratio = 0.05;      
min_speed     = 1.0;      
max_speed     = 5.0;

cruise  = 'NUKA ARCTICA: Aalborg to Nuuk';

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

% Number of pages to generate:
t0 = start_day;
t = t0;
n = 0;
while t < stop_day,
	n = n + 1;
	t = t + delta_day;
	if t < stop_day,
		t = t + overlap;
	end
end

% File names:
psfile = [ outfile '.ps' ];

% Eeventually delete output file:
if exist(psfile, 'file'),
	delete(psfile);
end

% Loop through pages:
dt = delta_day;
for i=1:n,

	% Create mat file for ref data:	
	time_range = sprintf(' %f %f ', t0 - 0.1 * dt, t0 + 1.1 * dt);
	if ~isempty(refabsfile)
		ref_ncol = 9;                  % # of columns to be retrieved
		ref_matname = [ outfile 'ref.mat' ];
		eval([ '!ref2mat ' refabsfile ' ' ref_matname time_range ...
					sprintf('%f', ref_ncol) ]);
	else
		ref_matname = '';
	end

	% Create mat file for sm data:
	if ~isempty(smoothrfile)
		sm_matname  = [ outfile 'sm.mat' ];
		sm_ncol = 8;                   % # of columns to be retrieved
		step = 1;                      % default 1
		eval([ '!sm2mat ' smoothrfile ' ' sm_matname time_range ...
					sprintf('%f %f', step, sm_ncol) ]);
	else
		sm_matname = '';
	end;

	ax = [t0 t0+dt y0 y0+dy];
	crfp2(year_base, psfile, ax, ref_matname, sm_matname, ...
			min_speed, max_speed, max_gap_ratio, cruise);

	% Update start time:
	t0 = t0 + dt + overlap;
end



