function trjcrv=readq(qname)

% trjcrv=readq(qname)
%
% Function for reading q-files generated by AUTO97.
% It reads the file q.qname that is in the current directory and returns
% the trajectory stored in it in the 3D matrix trjcrv.
%
% The matrix trjcrv is organized as follow:
% trjcrv(:,:,i) is the trajectory corresponding to the i-th nonzero
%               label in the variable bifcrv returned by READP.
% trjcrv(:,1,i) is the vector of the normalized time.
% trjcrv(:,j,i) is the time series of the j-1 state variable.
%
%
% SEE ALSO
%
% READP COMPOSEP COMPOSEQ
%
% For further informations refer to th AUTO97 user manual and to its source code.
%
% (C)Copyright Oscar De Feo 1998
%



% Open the file
  fidr=fopen(['q.' qname],'r');
  if fidr==-1,
     fclose('all');
     error(['Unable to open the file q.' qname '.']);
  end;

% Read the header on the backward file
  cl=fgetl(fidr);
  [head,nhead,err]=sscanf(cl,'%f');
  if ~isempty(err),
     fclose('all');
     error(['Corrupted data in the file q.' qname '.']);
  end;
  if nhead~=12,
    fclose('all');
    error(['The file q.' qname ' has not an AUTO97 q-file header.']);
  end;

% The structure of the data in the file
  lines=head(7);
  cols=head(8);
  skiplines=head(9)-head(7)+1;

% Read the data
  countright=lines*cols;
  i=1;
  while(~feof(fidr)),
       [tmp count]=fscanf(fidr,'%f',[cols lines]);
       if (count==countright),
          trjcrv(:,:,i)=tmp';
       else,
          fclose('all');
          error(['Corrupted data in the file q.' qname '.']);
       end;
       fgetl(fidr);
       i=i+1;
       for j=1:skiplines,
           cl=fgetl(fidr);
           if feof(fidr),
              break;
           end;
           if ~ischar(cl),
             fclose('all');
             error(['The file q.' qname ' is not compliant to the AUTO97 q-file standard.']);
           end;
       end;
  end;

% Close the backward file
  fclose(fidr);

% END
  return;

