function trjcrv=composeq(qname)

% trjcrv=composeq(qname)
%
% Function for reading and composing in the right sequence q-files
% generated by AUTO97.
% It reads the files q.qname.fw and q.qname.bw that are in the current
% directory and returns the trajectories stored in them 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 COMPOSEP.
% 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 READQ COMPOSEQ
%
% For further informations refer to th AUTO97 user manual and to its source code.
%
% (C)Copyright Oscar De Feo 1999
%



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

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

% The structure of the data in the file
  linesbw=headbw(7);
  colsbw=headbw(8);
  skiplinesbw=headbw(9)-headbw(7)+1;

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

% Close the backward file
  fclose(fidrbw);


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

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

% The structure of the data in the file
  linesfw=headfw(7);
  colsfw=headfw(8);
  skiplinesfw=headfw(9)-headfw(7)+1;

% Check that forward and backward files are coherent
  if ~all([linesbw colsbw]==[linesfw colsfw]),
     fclose('all');
     error(['The files q.' pname '.fw and q.' pname '.bw do not contain the same kind of data.']);
  end;

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

% Close the forward file
  fclose(fidrfw);


% Compose the trajectory matrix
  trjcrv=cat(3,flipdim(trjcrvbw,3),trjcrvfw);

% END
  return;
