function varargout = meteo2(yb, in_file, out_file, dr, v_list, plt, info, inst)
%  1.  NAME
%
%  meteo2 - Plots data from MET files
%
%  2.  SYNOPSIS
%
%  [data, yd ] = meteo2(yb, in_file, out_file, dr, v_list, plt, info,
%  inst )
%
%  3.  DESCRIPTION
%
%  Read the given MET file and plots the specified variables for the
%  given date range. Input files have the following columns:
%
%  1. Date
%
%  2. Time
%
%  3. Reference reading
%
%  4. Relative wind speed
%
%  5. Relative wind direction
%
%  6. Ship speed
%
%  7. Heading
%
%  8. Air pressure
%
%  9. Air temperature
%
%  10.
%     Relative humidity
%
%  11.
%     Sea temperature
%
%  12.
%     Wind speed
%
%  13.
%     Wind direction
%
%  Use can also use this function from the Matlab script meteo(1m).
%
%  4.  PARAMETERS
%
%     yb Year base.
%
%     in_file
%        Name of MET input file.
%
%     out_file
%        Root name for output files. Suffixes are added automatically.
%
%     dr String with a date range in the usuall CODAS . See time_rng(5)
%        for more information.  Only MET data within this range will be
%        plotted.
%
%     v_list
%        Cell array of variable names. These must be the same as the
%        labels defined in the MET file. Possible values are
%        date,time,ref. reading,rel. wind speed,rel. wind dir. ,ship
%        speed ,heading,air pressure,air temp.,rel. humidity ,sea
%        temp.,wind speed,and wind dir..  A subplot for each of the
%        specified variables will be generated.
%
%     plt
%        Plot title.
%
%     info
%        Additionnal information string to be printed.
%
%     inst
%        An institute stamp.
%
%  5.  RETURNS
%
%     data optional
%        Array of meteorological data within the given day range.
%
%     yd optional
%        Vector of decimal days corresponding to the meteorlogical data.
%
%  6.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

units_table = { 'none', 'none', 'counts', 'm/s', 'deg', 'm/s', ...
    'deg', 'mbar', 'deg C', '% RH', 'deg C', 'm/s', 'deg' } ;
labels_table = { 'date', 'time', 'ref. reading', 'rel. wind speed', ...
    'rel. wind dir.', 'ship speed', 'heading', 'air pressure', ...
    'air temp.', 'rel. humidity', 'sea temp.', 'wind speed', ...
    'wind dir.' };

%--- Read data:
data = read_asc(in_file);

%--- Indexes:
d = 1; t = 2; c = 3 ; rws = 4; rwd = 5; ss = 6; hdg = 7; p = 8;
Ta = 9; rh = 10; Ts = 11; ws = 12; wd = 13;

%--- Process variables to plot:
nv = length(v_list);
nl = length(labels_table);
iv = [];
for i=1:nv,
  j = 1;
  while (j <= nl) & ~strcmp(v_list{i}, labels_table{j}),
    j = j + 1;
  end
  if j > nl,
    disp([ 'Could not find variable ' v_list{i} ]);
    disp([ 'Possible variables are:' ])
    for j=1:nl
      disp([ '   ' labels_table{j} ]);
    end
    error([ 'Exiting ...' ]);
  end
  iv(i) = j;
end

%--- Process date range:
tmp = sscanf(dr, '%2d%2d/%2d/%2d to %2d%2d/%2d/%2d'); 
d0 = tmp(2)*1.0E4 + tmp(3)*1.0E2 + tmp(4);
d1 = tmp(6)*1.0E4 + tmp(7)*1.0E2 + tmp(8);
century = tmp(1)*1.0E2;

%--- Directions are not always between 0 and 360:
data(:,rwd) = mod(data(:,rwd), 360);
data(:,wd)  = mod(data(:,wd), 360);

%--- Select good date lines:
igood = find( (data(:,d) >= d0) & (data(:,d) <= d1) );

%--- Build x-axis vector:
yd = [];
for i=1:length(igood),
  tmp = [ sprintf('%06d', data(igood(i),d)) ...
        sprintf('%04d', data(igood(i),t)) ];
  yd(i,:) = [ sscanf(tmp, '%2d%2d%2d%2d%2d')' 0 ];
end
yd(:,1) = yd(:,1) + century;
yd = to_day(yb, yd);

%--- Plot meteo:
figure;
orient tall;

for i=1:nv,

  subplot(nv, 1, i);
  plot(yd, data(igood,iv(i)));
  grid on
  ylabel([ labels_table{iv(i)} ' (' units_table{iv(i)} ')' ]);
  
end

xlabel([ 'Decimal days (' int2str(yb), ')' ]);

%--- Plot the rest:
ps_file = [ out_file '.ps' ];
subtitle = [ to_date(yb, yd(1)) '  to  ' ...
			to_date(yb, yd(length(yd))) ];
note = [ ' - PS File: ' ps_file ];
plotstuf('-title', plt, '-subtitle', subtitle, '-note', note, ...
		'-institute', inst, '-info', info);

%--- Print output:
eval([ 'print -dps ' ps_file ]);

%% 1999/03/02 Jaccard Pierre 
%%            Added possibility to return data to calling process.

if nargout > 0,
  varargout{1} = data(igood,:);
end
if nargout > 1,
  varargout{2} = yd;
end











