%
%  nplot1 - produce a MATLAB plot of a NIMBUS trend group
%           uses colors for the screen or expensive printers
%
%  nplot1( group_name )
%     e.g. nplot1( 'level1' )
%
%  Bob Newell, September 1995
%
%function nplot1( group, axx )
group = 'ar';
axx = [ 0 6 0 8 ];
%
%  read data from group definition file
%
status = [];
minval = [];
maxval = [];
vnames = [];
m = 0;
file = [ group '.trf' ];
fid = fopen( file, 'r' );
if fid > 0
   data = fscanf( fid, '%d%g%g%s', [ 1, 4 ] );
   while ~isempty(data)
      status = [ status ; data(1) ];
      minval = [ minval ; data(2) ];
      maxval = [ maxval ; data(3) ];
      m = max( m, length(data)-3 );
      for i = length(data):50
         data = [ data 32 ];
      end;
      vnames = [ vnames ; sprintf( '%c', data(4:50) ) ];
      data = fscanf( fid, '%d%g%g%s', [ 1, 4 ] );
   end;
   fclose( fid );
   n = length( status );
else
   disp( [ file ' not found' ] );
   return;
end;
%
%  read data from group data file
%
trends = [];
file = [ group '.trd' ];
fid = fopen( file, 'r' );
if fid > 0
   data = fscanf( fid, '%g,', [ 1, n ] );
   while ~isempty(data)
      trends = [ trends ; data ];
      data = fscanf( fid, '%g,', [ 1, n ] );
   end;
   fclose( fid );
else
   disp( [ file ' not found' ] );
   return;
end;
%
% scale the trend data to 0 - 1 in Y direction
%
%for i = 2:n
%   trends(:,i) = ( trends(:,i) - minval(i) ) ./ ...
%                    ( maxval(i) - minval(i) );
%end;
%
% determine visible plots
%
vis = rem( status, 2 ) == 1;
vis(1) = 1;
%
% remove external data
%
extdata = []; extindex = [];
for i = 2:n
  pos = findstr( '=', vnames(i,1:m) );
  if pos, 
    extdata = [ extdata; vnames(i,1:m) ];
	extindex = [ extindex; i ];
	vis(i) = 0;
  end;
end;
trends = trends(:,vis);
vnames = vnames(vis,1:m);
n = length( status(vis) );
%
% now the plot itself
%
% first nimbus data
%
toobig = find( trends(:,1) > axx(2) );
trends( toobig, : ) = [];
plot( trends(:,1), trends(:,2), '-' );
%axis( [ minval(1) maxval(1) 0 1 ] );
set( gca, 'fontsize', 20 );
axis( axx );
xlabel( 'Time  in hours' );
sylabel( '\20Tracer concentration in \mu g/l' );
hold on;
%title( [ 'NIMBUS Trend Group <' group '>' ] );
%
% then external data if any
%
symbols = 'xo+*';
[ nr, nc ] = size( extdata );
if nr > 0,
%  hold on;
  sym = 0;
  for i = 1:nr,
    sym = sym + 1; if sym > 4, sym = 1; end;
    vnames = [ vnames; extdata(i,1:m) ];
	p1 = findstr( '=', extdata(i,1:m) );
	p2 = findstr( ',', extdata(i,1:m) );
	file = extdata(i,p1+1:p2(1)-1);
	tcol = str2num( extdata(i,p2(1)+1:p2(2)-1) );
	dcol = str2num( extdata(i,p2(2)+1:m) );
    fid = fopen( file, 'r' );
    if fid > 0,
	   time = [];
	   data = [];
	   line = 1;
	   while line > 0,
          line = fgetl( fid );
		  if isstr( line ),
            linedata = sscanf( line, '%g' );
			time = [ time; linedata(tcol) ];
			data = [ data; linedata(dcol) ];
          end;
       end;
       fclose( fid );
%       data = ( data - minval( extindex(i) ) ) ./ ...
%               ( maxval( extindex(i) ) - minval( extindex(i) ) );
%	   plot( time, data, symbols(sym) );
	   plot( time, data, '*' );
    else
       disp( [ 'external data file ' file ' not found' ] );
       return;
    end;
  end;
  hold off;
end;
%axis( [ minval(1) maxval(1) 0 1 ] );
%set( gca, 'fontsize', 16 );
%xlabel( 'Time' );
%legend( vnames(2:n+nr,:) );
%
% that's all
%
printsto -f1 -deps2 cppar.eps
%
