*DECK LIBMIX
      SUBROUTINE LIBMIX(IPLIB,NBMIX,NGROUP,NBISO,ISONAM,MIX,DEN,MASK,
     1 MASKL,ITSTMP,TMPDAY)
*
*-----------------------------------------------------------------------
*
*Purpose:
* transformation of the isotope ordered microscopic cross sections to
* group ordered macroscopic cross sections (part 1).
*
*Copyright:
* Copyright (C) 2002 Ecole Polytechnique de Montreal
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version
*
*Author(s): A. Hebert
*
*Parameters: input
* IPLIB   pointer to the lattice microscopic cross section library
*         (L_LIBRARY signature).
* NBMIX   number of material mixtures.
* NGROUP  number of energy groups.
* NBISO   number of isotopes present in the calculation domain.
* ISONAM  alias name of isotopes.
* MIX     mixture number of each isotope (can be zero).
* DEN     density of each isotope.
* MASK    mixture mask (=.true. if a mixture is to be made).
* MASKL   group mask (=.true. if an energy group is to be treated).
*
*-----------------------------------------------------------------------
*
      USE GANLIB
*----
*  SUBROUTINE ARGUMENTS
*----
      TYPE(C_PTR) IPLIB
      INTEGER NBMIX,NGROUP,NBISO,ISONAM(3,NBISO),MIX(NBISO),ITSTMP
      REAL DEN(NBISO),TMPDAY(3)
      LOGICAL MASK(NBMIX),MASKL(NGROUP)
*----
*  LOCAL VARIABLES
*----
      PARAMETER (NBLK=50,NSTATE=40)
      LOGICAL LSAME
      INTEGER ISTATE(NSTATE),KCHAR(3)
      CHARACTER TEXT12*12
      REAL OLDTIM(3)
      INTEGER, ALLOCATABLE, DIMENSION(:) :: JNED
*----
*  RECOVER SOME LIBRARY PARAMETERS.
*----
      CALL LCMGET(IPLIB,'STATE-VECTOR',ISTATE)
      NL=ISTATE(4)
      ITRANC=ISTATE(5)
      IPROB=ISTATE(6)
      NED=ISTATE(13)
      NDEL=ISTATE(19)
      ALLOCATE(JNED(2*NED))
      IF(NED.GT.0) CALL LCMGET(IPLIB,'ADDXSNAME-P0',JNED)
*----
*  LOOK FOR OLD LIBRARY DATA
*----
      CALL LCMLEN(IPLIB,'MACROLIB',LENGTH,ITYLCM)
      IF(LENGTH.EQ.-1) THEN
        CALL LCMSIX(IPLIB,'MACROLIB',1)
        CALL LCMGET(IPLIB,'SIGNATURE',KCHAR)
        WRITE(TEXT12,'(3A4)') (KCHAR(I),I=1,3)
        IF(TEXT12.NE.'L_MACROLIB') THEN
          CALL XABORT('LIBMIX: INVALID SIGNATURE ON THE MACROLIB.')
        ENDIF
        CALL LCMLEN(IPLIB,'TIMESTAMP',LENGTH,ITYLCM)
        IF((LENGTH.GT.0).AND.(LENGTH.LE.3)) THEN
          CALL LCMGET(IPLIB,'TIMESTAMP',OLDTIM)
          IF(ITSTMP.EQ.0) THEN
            TMPDAY(1)=OLDTIM(1)
            TMPDAY(2)=OLDTIM(2)
            TMPDAY(3)=OLDTIM(3)
          ENDIF
        ENDIF
        CALL LCMSIX(IPLIB,' ',2)
      ENDIF
*----
*  TRANSPOSE THE MICROSCOPIC CROSS SECTIONS TO ADJOINT ORDERING.
*----
      IF(IPROB.EQ.1) THEN
         CALL LIBADJ (IPLIB,NGROUP,NBISO,NL,NDEL,ISONAM,NED,JNED)
      ENDIF
*----
*  COMPUTE THE MAXIMUM NUMBER OF FISSIONABLE ISOTOPES IN A MIXTURE.
*----
      DO 10 I=1,NBISO
      IF(MIX(I).GT.NBMIX) CALL XABORT('LIBMIX: NBMIX OVERFLOW.')
   10 CONTINUE
      MAXNFI=MIN(NBISO,200)
      CALL LIBNFI (IPLIB,NGROUP,NBISO,NBMIX,NDEL,ISONAM,MIX,MAXNFI,
     1 NFISSI,LSAME)
*----
*  BUILD THE MACROSCOPIC CROSS SECTIONS.
*----
      CALL LIBDEN (IPLIB,NGROUP,NBISO,NBMIX,NL,NDEL,ISONAM,MIX,DEN,
     1 MASK,MASKL,NED,JNED,ITRANC,NFISSI,LSAME,ITSTMP,TMPDAY)
*----
*  TRANSPOSE THE MICROSCOPIC CROSS SECTIONS BACK TO FORWARD ORDERING.
*----
      IF(IPROB.EQ.1) THEN
         CALL LIBADJ (IPLIB,NGROUP,NBISO,NL,NDEL,ISONAM,NED,JNED)
      ENDIF
      DEALLOCATE(JNED)
      RETURN
      END
