function [bifcrv, pnames]=composep(pname)

% [bifcrv, pnames]=composep(pname)
%
% Function for reading and composing in the right sequence p-files
% generated by AUTO97.
% It reads the files p.pname.bw and p.pname.fw that are in the current
% directory and composes the data contained in them in such a way that the
% return variable bifcrv will contain the flipped up/down data of the
% backward continuation (bw) followed by the data of the forward
% continuation (fw), the labels of the special points are modified in such
% a way that the labels of the special points from the (bw) file are
% negative. The function returns the HEADER of the columnwise data
% contained in the p-files in the return variable pnames.
%
% Remeber that the structure of the p-files is as follow:
%
%   0    PT  TY LAB     PERIOD       L2-NORM       PAR(1)       PAR(3)      <- This is the header
%   1     1   4   1  1.446831E+01  3.443652E+01  2.086500E-01 8.908826E+00  <- those are the data
%   1     2   0   0  1.447551E+01  3.442895E+01  2.086509E-01 9.124533E+00 
%   1     3   0   0  1.448630E+01  3.441760E+01  2.086520E-01 9.435545E+00 
%   1     4   0   0  1.449817E+01  3.440511E+01  2.086532E-01 9.760761E+00 
%   1     5   0   0  1.451123E+01  3.439138E+01  2.086542E-01 1.009880E+01 
%   1     6   4   2  1.452558E+01  3.437631E+01  2.086552E-01 1.044784E+01 
%   1     7   0   0  1.454138E+01  3.435976E+01  2.086561E-01 1.080546E+01 
%   1     8   0   0  1.455876E+01  3.434163E+01  2.086569E-01 1.116857E+01 
%   1     9   0   0  1.457789E+01  3.432177E+01  2.086576E-01 1.153337E+01 
%   1    10   4   4  1.459896E+01  3.430004E+01  2.086581E-01 1.189518E+01 
%   1    11   0   0  1.462217E+01  3.427629E+01  2.086586E-01 1.224842E+01 
%   1    12   0   0  1.464776E+01  3.425033E+01  2.086589E-01 1.258652E+01 
%
%
% 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
%



% Service Variables
  SKIP=11;


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

% Skip the header on the backward file
  for i=1:SKIP,
      cl=fgetl(fidrbw);
      if((cl==-1)|~ischar(cl)),
        fclose('all');
        error(['The file p.' pname '.bw has not an AUTO97 p-file header.']);
      end;
  end;

% Save the names
  pnamesbw=fgetl(fidrbw);

% Read the data part
  cl=fgetl(fidrbw);
  [bifcrvbw1l,ncolbw,err]=sscanf(cl,'%f');
  if ~isempty(err),
     fclose('all');
     error(['Corrupted data in the file p.' pname '.bw.']);
  end;
  bifcrvbw=[bifcrvbw1l,fscanf(fidrbw,'%f',[ncolbw,inf])]';

% Close the backward file
  fclose(fidrbw);

% Change sign to the labels from the backward file
  bifcrvbw(:,4)=-bifcrvbw(:,4);


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

% Skip the header on the backward file
  for i=1:SKIP,
      cl=fgetl(fidrfw);
      if((cl==-1)|~ischar(cl)),
        fclose('all');
        error(['The file p.' pname '.fw has not an AUTO97 p-file header.']);
      end;
  end;

% Save the names
  pnamesfw=fgetl(fidrfw);

% Check that forward and backward files are coherent
  if ~strcmp(pnamesbw,pnamesbw),
     fclose('all');
     error(['The files p.' pname '.fw and p.' pname '.bw do not contain the same kind of data.']);
  end;

% Read the data part
  cl=fgetl(fidrfw);
  [bifcrvfw1l,ncolfw,err]=sscanf(cl,'%f');
  if ~isempty(err),
     fclose('all');
     error(['Corrupted data in the file ' pname '.fw.']);
  end;
  bifcrvfw=[bifcrvfw1l,fscanf(fidrfw,'%f',[ncolfw,inf])]';

% Close the forward file
  fclose(fidrfw);

% Compose the data
  bifcrv=[flipud(bifcrvbw); bifcrvfw];
  pnames=pnamesbw;

% END
  return;
