SUBROUTINE DRESSEDBASIS(D,ID,NM,MODES_NUM,FIELDS,U_FD,E_DRESSED,INFO)

!!$THIS SUBROUTINES CALCULATES THE TRANSFORMATION BETWEEN THE BARE BASIS TO THE DRESSED BASIS DEFINDED BY THE FULL SET OF DRIVING FIELDS.
!!$ D                            : DIMENSION OF THE MULTIMODE EXTENDED HILBERT SPACE
!!$ ID (IN)                      : TYPE OF QUATUM SYSTEM
!!$ NM (IN)                      : NUMBER OF MODES == NUMBER OF DRIVING FIELDS
!!$ MODES_NUM                    : VECTOR INDICATING THE NUMBER OF HARMONICS OF EACH DRESSING FIELD
!!$ FIELDS (IN)                  : AMPLITUDE, FREQUENCY AND PHASES OF ALL DRIVING FIELDS
!!$ U_FD (OUT)                   : THIS IS THE TRANSFORMATION WE ARE LOOKING FOR
!!$ E_DRESSED (OUT)              : DRESSED ENERGIES
!!$ INFO (INOUT)                 : INFO = 0 MEANS SUCESS
               

  USE ATOMIC_PROPERTIES
  USE TYPES
  USE SUBINTERFACE
  USE SUBINTERFACE_LAPACK
!  USE FLOQUETINIT_ 
  USE ARRAYS 

  IMPLICIT NONE
  TYPE(MODE), DIMENSION(NM),     INTENT(IN)    :: FIELDS
  TYPE(ATOM),                    INTENT(IN)    :: ID
  INTEGER,    DIMENSION(NM),     INTENT(IN)    :: MODES_NUM
  COMPLEX*16, DIMENSION(D,D),       INTENT(OUT)   :: U_FD
  DOUBLE PRECISION, DIMENSION(D), INTENT(OUT)   :: E_DRESSED
  INTEGER,                       INTENT(IN)    :: NM,D
  INTEGER,                       INTENT(INOUT) :: INFO


  INTEGER r,m,FIELD_INDEX
  !INTEGER ND_,NM_
  INTEGER TOTAL_FREQUENCIES
  !TYPE(MODE), DIMENSION(DRESSINGFIELDS) :: FIELDS_
  !TYPE(MODE), DIMENSION(:),ALLOCATABLE  :: FIELDS_
  !INTEGER,    DIMENSION(DRESSINGFIELDS) :: MODES_NUM_

  !NM_ = DRESSINGFIELDS
  !ND_ = DRESSINGFIELDS


  TOTAL_FREQUENCIES = SUM(MODES_NUM,1)

  CALL SETHAMILTONIANCOMPONENTS(ID,NM,TOTAL_FREQUENCIES,MODES_NUM,FIELDS,INFO)

  !--- FIND THE MULTIMODE FLOQUET SPECTRUM 
  CALL MULTIMODEFLOQUETMATRIX(ID,NM,TOTAL_FREQUENCIES,MODES_NUM,FIELDS,INFO)
  
  E_DRESSED = 0.0  
  CALL LAPACK_FULLEIGENVALUES(H_FLOQUET,SIZE(H_FLOQUET,1),E_DRESSED,INFO)

  U_FD = H_FLOQUET ! FOURIER DECOMPOSITION OF THE STATES DRESSED BY MODE NUMBER 
  DEALLOCATE(H_FLOQUET)

END SUBROUTINE DRESSEDBASIS


SUBROUTINE DRESSEDBASIS_SUBSET(ID,DRESSINGFLOQUETDIMENSION,DRESSINGFIELDS,NM,&
        & DRESSINGFIELDS_INDICES,MODES_NUM,FIELDS,U_FD,E_DRESSED,INFO)

!!$THIS SUBROUTINES CALCULATES THE TRANSFORMATION BETWEEN THE BARE BASIS TO THE DRESSED BASIS DEFINDED BY A SUBSET OF THE DRIVING FIELDS.
!!$
!!$ ID (IN)                      : TYPE OF QUATUM SYSTEM
!!$ DRESSINGFLOQUETDIMENSION(IN) : DIMENSION OF THE DRESSED FLOQUET HILBERT SPACE
!!$ DRESSINGFIELDS (IN)          : NUMBER OF DRESSING FIELDS
!!$ NM (IN)                      : NUMBER OF MODES == NUMBER OF DRIVING FIELDS
!!$ DRESSINGFIELDS_INDICES       : OUT OF ALL THE DRIVING FIELDS, THIS VECTOR INDICATES THE INDEX OF THE DRESSING FIELD
!!$ MODES_NUM                    : VECTOR INDICATING THE NUMBER OF HARMONICS OF EACH DRESSING FIELD
!!$ FIELDS (IN)                  : AMPLITUDE, FREQUENCY AND PHASES OF ALL DRIVING FIELDS
!!$ U_FD (OUT)                   : THIS IS THE TRANSFORMATION WE ARE LOOKING FOR
!!$ E_DRESSED (OUT)              : DRESSED ENERGIES
!!$ INFO (INOUT)                 : INFO = 0 MEANS SUCESS
               

  USE ATOMIC_PROPERTIES
  USE TYPES
  USE SUBINTERFACE
  USE SUBINTERFACE_LAPACK
!  USE FLOQUETINIT_ 
  USE ARRAYS 

  IMPLICIT NONE
  INTEGER,                                               INTENT(IN)    :: NM
  INTEGER,                                               INTENT(IN)    :: DRESSINGFLOQUETDIMENSION,DRESSINGFIELDS
  TYPE(MODE), DIMENSION(NM),                             INTENT(IN)    :: FIELDS
  TYPE(ATOM),                                            INTENT(IN)    :: ID
  INTEGER,    DIMENSION(DRESSINGFIELDS),                 INTENT(IN)    :: DRESSINGFIELDS_INDICES
  INTEGER,    DIMENSION(NM),                             INTENT(IN)    :: MODES_NUM
  COMPLEX*16, DIMENSION(DRESSINGFLOQUETDIMENSION,DRESSINGFLOQUETDIMENSION),       INTENT(OUT)   :: U_FD
  DOUBLE PRECISION, DIMENSION(DRESSINGFLOQUETDIMENSION), INTENT(OUT)   :: E_DRESSED
  INTEGER,                                               INTENT(INOUT) :: INFO


  INTEGER r,m,FIELD_INDEX
  INTEGER ND_,NM_
  INTEGER TOTAL_FREQUENCIES_
  TYPE(MODE), DIMENSION(:),ALLOCATABLE  :: FIELDS_
  INTEGER,    DIMENSION(DRESSINGFIELDS) :: MODES_NUM_


!  write(*,*)"non-SP A:", DRESSINGFIELDS_INDICES

  DO r=1,DRESSINGFIELDS
     MODES_NUM_(r)=modes_num(DRESSINGFIELDS_INDICES(r))
  END DO

  TOTAL_FREQUENCIES_ = SUM(MODES_NUM_,1)
  ALLOCATE(FIELDS_(TOTAL_FREQUENCIES_))
  DO m=1,TOTAL_FREQUENCIES_
     ALLOCATE(FIELDS_(m)%V(ID%D_BARE,ID%D_BARE))
  END DO
 ! write(*,*) "non-SP:",dressingfields,modes_num_,total_frequencies_


  FIELD_INDEX = 1
  DO r=1,DRESSINGFIELDS
     DO m=1,MODES_NUM_(r)
        FIELDS_(FIELD_INDEX) = FIELDS(DRESSINGFIELDS_INDICES(r)+m-1)    
        !FIELDS_(FIELD_INDEX)%N_Floquet = FIELDS(DRESSINGFIELDS_INDICES(r)+m-1)%N_FLOQUET    
        !write(*,*) r,m,field_index,DRESSINGFIELDS_INDICES
        FIELD_INDEX = FIELD_INDEX+1
     END DO
  END DO

  
  CALL SETHAMILTONIANCOMPONENTS(ID,size(modes_num_,1),total_frequencies_,MODES_NUM_,FIELDS_,INFO)

!!$  !--- FIND THE MULTIMODE FLOQUET SPECTRUM 
!  write(*,*) fields_(2)%N_Floquet, fields(2)%N_floquet
  !fields_(2)%N_Floquet = fields(2)%N_floquet
  CALL MULTIMODEFLOQUETMATRIX(ID,size(modes_num_,1),total_frequencies_,MODES_NUM_,FIELDS_,INFO)
  E_DRESSED = 0.0  
  CALL LAPACK_FULLEIGENVALUES(H_FLOQUET,SIZE(H_FLOQUET,1),E_DRESSED,INFO)
  U_FD = H_FLOQUET ! FOURIER DECOMPOSITION OF THE STATES DRESSED BY MODE NUMBER 
  DEALLOCATE(H_FLOQUET) 
  DEALLOCATE(FIELDS_)
  
END SUBROUTINE DRESSEDBASIS_SUBSET
