*DECK D2PINP
      SUBROUTINE D2PINP(  IPSAP, IPDAT ,  IPHEL, IPINP,  FILNAM, IPRINT,
     >                   STAVEC, CRDINF,   NCRD,   PKEY,  SFAC,    BFAC,
     >                     ISOT,   MESH, USRPAR, USRVAL, USRSTA,USRVAPK,
     >                      SAP,    MIC, EXC   ,   SCAT, ADF   ,    DEB,
     >                      COM,   FA_K,   IUPS,   LADD,   LNEW,    MIX,
     >                      FC1,    FC2,    FC3,    FC4,    XSC,    NGP,
     >                   JOBTIT, JOBOPT,    DER,  VERS                 )
*
*-----------------------------------------------------------------------
*
*Purpose:
* 1) Recover data from saphyb or multicompo object.
* 2) Build headers of GenPMAXS and Helios like file
*
*Copyright:
* Copyright (C) 2015 IRSN
*
*Author(s): J. Taforeau
*
*parameters: input/output
* IPSAP   address of saphyb or multicompo object
* IPDAT   address of data structure INFO
* IPHEL   file unit of Helios like file
* IPINP   file unit of GenPMAXS input file
* NGP     number of energy groups recovered from D2P input user
* NCRD    number of control rod composition recovered from D2P
*         input user
* MIX     index of mixture on which XS are to be extracted (only
*         for reflector cases)
* FA_K    assembly type
*         =0 reflector
*         =1 assembly
* IUPS    treatment for upscattering
*         =0 keep up scatter XS
*         =1 remove up scatter XS, modify  down scatter with DRAGON
*         spectrum (not available in this version)
*         =2 remove up scatter XS, modify  down scatter with infinite
*         medium spectrum
* USRSTA  state variable names recovered from GLOBAL record in D2P:
* USRVAL  number of value for state variables  recovered from GLOBAL
*         record in D2P:
* IPRINT  control the printing on screen
* STAVEC  various parameters associated with the IPDAT structure
* CRDINF  meaning of control rods in the IPSAP object
* VERS    version of PARCS to be used
* SFAC    the scattering cross section factor
* BFAC    the multiplier for betas
* FC1     FILE_CONT_1 recovered from D2P: input
* FC2     FILE_CONT_2 recovered from D2P: input
* FC3     FILE_CONT_3 recovered from D2P: input
* FC4     FILE_CONT_4 recovered from D2P: input
* XSC     XS_CONT     recovered from D2P: input
* DEB     FLAG to indicate the first call to the D2PGEN subroutine
* USRVAPK value of state prameter set by the user and recoverd from
*         USER ADD option in D2P:
* ADF     type of ADF to be selected
* DER     partials derivative (T) or row cross section (F) to be stored
*         in PMAXS
* JOBOPT  flag for JOB_OPT record in IPINP object
* USRPAR  name of state variables (sapnam) in IPSAP associated to
*         DMOD TCOM etc. recovered from PKEY card in D2P:
* MESH    type of meshing to be applied for the branching calculation
* PKEY    name of state variable (refnam) recovered from PKEY card in
*         D2P:
* FILNAM  name of IPINP
* ISOT    name of isotopes in IPSAP for xenon samarium and spomethium
* JOBTIT  title of in header of PMAXS file
* COM     comment to be printed in PMAXS file
* SAP     flag to indicate that absorption cross section must be
*         directly recovered from IPSAP
* MIC     flag to indicate that absorption cross section must be
*         directly recovered from IPMIC
* EXC     flag to indicate that excess cross section is to be extracted
*         from absoption xs (only if SAP)
* SCAT    flag to indicate that scattering cross section must be
*         directly reconstructed from IPSAP
* LADD    flag to indicate that new points must be added to the IPSAP
*         original meshing
* LNEW    flag to indicate that only new points must be used during the
*         branching calculation
*
*-----------------------------------------------------------------------
*
      USE GANLIB
*----
*  SUBROUTINE ARGUMENTS
*----
      TYPE(C_PTR) IPSAP,IPDAT
      INTEGER NGP,NCRD,MIX,FA_K,IUPS,USRSTA
      INTEGER IPHEL,IPINP,IPRINT,DEB
      INTEGER STAVEC(40),CRDINF(20),USRVAL(12)
      REAL VERS,SFAC,BFAC
      REAL FC1(5),FC2(8),FC3(7),FC4(3),XSC(3)
      REAL USRVAPK(12,10)
      CHARACTER*3 ADF
      CHARACTER*4 DER,JOBOPT(14),USRPAR(12)*4
      CHARACTER*5 MESH
      CHARACTER*8 PKEY(6)
      CHARACTER*12 FILNAM,ISOT(3)
      CHARACTER*16 JOBTIT
      CHARACTER*40 COM
      LOGICAL SAP, MIC, EXC,SCAT,LADD,LNEW
*----
*  LOCAL VARIABLES
*----
      LOGICAL LADF

      IF (JOBOPT(1)=='T') THEN
        LADF = .TRUE.
      ELSE
        LADF = .FALSE.
      ENDIF

      WRITE(6,*) "*******    EXTRACTION OF DATA FROM SPAHYB       ****"
      IF(FA_K.EQ.1) THEN
*       CASE FOR FUEL PMAXS
        CALL    D2PSAP (  IPSAP,  IPDAT, STAVEC, CRDINF,   NCRD,   PKEY,
     >                    ISOT ,   MESH, USRPAR, USRVAL, USRSTA,USRVAPK,
     >                    SAP  ,    MIC,    EXC,   SCAT,    ADF,   LADD,
     >                    LNEW ,   LADF, IPRINT                        )

      ELSE IF(FA_K.EQ.0) THEN
*       CASE FOR REFLECTOR PMAXS
        CALL    D2PMUL (  IPSAP,  IPDAT, STAVEC,    MIX, IPRINT        )
      ELSE
        CALL XABORT('@D2PINP: PHASE 1: FUEL OR REFLECTOR CARD EXPECTED')
      ENDIF

      WRITE(6,*) "*******    CREATION OF HELIOS LIKE FILE         ****"

      DEB = 0

      CALL      D2PHEL ( IPHEL,  IPDAT,  STAVEC,    FC1,    FC2,    NGP,
     >                     FC3,    FC4,     XSC, IPRINT                )

      IF(IPRINT > 0)  THEN
       WRITE(6,*) "*******    CREATION OF GENPMAXS INPUT FILE      ****"
      ENDIF

      CALL      D2PGEN ( IPINP, IPDAT,   STAVEC, JOBTIT, FILNAM,    DER,
     >                    VERS,   COM,   JOBOPT,   IUPS,   FA_K,   SFAC,
     >                    BFAC,   DEB,   IPRINT                        )

      CALL LCMSIX(IPDAT,' ',0)
      CALL LCMPUT(IPDAT,'STATE-VECTOR',40,1,STAVEC)
      END
