!
! License-Identifier: GPL
!
! Copyright (C) 2021 The Yambo Team
!
! Authors (see AUTHORS file for details): AM DS
!
subroutine OUTPUT_init(solver)
 !
 ! This subroutine defines X_obs elements
 ! - First, it init the list of possible obs via ELEMENTAL_init
 ! - Then, it defines as active some of them using global logicals
 ! - Last, for every active obs it defines the headers
 !
 use pars,          ONLY:schlen
 use drivers,       ONLY:l_chi,l_optics,l_bs_fxc
 use stderr,        ONLY:STRING_match,STRING_remove
 use D_lattice,     ONLY:l_0D,l_1D,l_2D
 use BS_solvers,    ONLY:l_abs_prop_chi_bse,l_eels_can_be_computed
 use BS,            ONLY:l_BS_kerr,l_BS_magnons,l_BS_dichroism,BS_K_coupling,&
&                        l_BS_photolum,l_BS_abs,l_BS_jdos,l_BS_esort_indx,l_BS_esort,&
&                        l_BS_mespin,l_BS_meorb
 use electrons,     ONLY:n_spinor
 use X_m,           ONLY:X_obs,N_X_obs,alpha_dim,X_do_obs
 use parser_m,      ONLY:parser
 !
 implicit none
 !
 character(*) :: solver
 !
 ! Workspace
 !
 logical            :: l_flag,l_print_res
 integer            :: it,itp,ic
 character(schlen)  :: BASIC_col(7)
 character(5)       :: obs_with_res(7)
   !
   call ELEMENTAL_init(1,"fxc","Exchange-Correlation Kernel","optics")
   call ELEMENTAL_init(2,"E_INDX_IP","Sorted IP Energies indexes","indexes")
   call ELEMENTAL_init(3,"E_IP","Sorted IP Energies","sort")
   call ELEMENTAL_init(4,"Esort","Sorted Interacting Energies","sort")
   call ELEMENTAL_init(5,"pl","Photoluminescence","PL")
   call ELEMENTAL_init(6,"eps","Absorption","optics")
   call ELEMENTAL_init(7,"refl","Reflectivity","optics")
   call ELEMENTAL_init(8,"eel","Electron Energy Loss","optics")
   call ELEMENTAL_init(9,"alpha","Polarizability  ( -<<X>> )","optics")
   call ELEMENTAL_init(10,"off","Off-diagonal Absorption","kerr")
   call ELEMENTAL_init(11,"beta","Off-diagonal Polarizability","kerr")
   call ELEMENTAL_init(12,"moke","MOKE parameters","kerr")
   call ELEMENTAL_init(13,"jdos","Joint Density of states","optics")
   call ELEMENTAL_init(14,"CD","Dichroism","dichroism")
   call ELEMENTAL_init(15,"Spm","Magnons (S+-)","magnons")
   call ELEMENTAL_init(16,"Smp","Magnons (S-p)","magnons")
   call ELEMENTAL_init(17,"MEspin","Magnetoelectric (spin)",  "mespin")
   call ELEMENTAL_init(18,"MEorb" ,"Magnetoelectric (orbital)","meorb")
   !
   ! Logicals activation...
   !------------------------
   if (l_bs_fxc) call X_obs_onoff("+ fxc")
   if (l_BS_photolum) call X_obs_onoff("+ pl")
   if (l_BS_abs.or.l_BS_kerr.or.(l_optics.and.l_chi)) then
     call X_obs_onoff("+ eps")
     call X_obs_onoff("+ eel")
     if (trim(alpha_dim)/='adim') call X_obs_onoff("+ alpha")
   endif
   !
   ! Switch off macroscopic quantities for lower dim materials
   if (l_0D.or.l_1D.or.l_2D) call X_obs_onoff("- eel eps off")
   ! Switch off eels if it cannot be computed
   if (.not.l_eels_can_be_computed) call X_obs_onoff("- eel")
   !
   if (l_BS_abs) then
     call parser('Reflectivity',l_flag)
     if (l_flag) call X_obs_onoff("+ refl") 
   endif
   if (l_BS_jdos) call X_obs_onoff("+ jdos")
   if (l_BS_esort)      call X_obs_onoff("+ Esort E_IP")
   if (l_BS_esort_indx) call X_obs_onoff("+ E_INDX_IP")
   if (l_BS_kerr) then
     if (X_do_obs("alpha")) call X_obs_onoff("+ beta")
     if (X_do_obs("eps"))   call X_obs_onoff("+ off moke")
   endif
   if (l_BS_magnons) call X_obs_onoff("+ Spm")
   if (l_BS_magnons.and.n_spinor==2) call X_obs_onoff("+ Smp")
   if (l_BS_dichroism) call X_obs_onoff("+ CD")
   if (l_BS_mespin) call X_obs_onoff("+ MEspin")
   if (l_BS_meorb)  call X_obs_onoff("+ MEorb")
   !
   !...solvers specific
   if (solver/="") then
     if (STRING_match(solver,"Slepc")) then
       if( l_abs_prop_chi_bse      .and. X_do_obs("eel") ) then
         call X_obs_onoff("- eel")
         call warning("eels evaluation switched off for slepc")
       endif
       if((.not.l_abs_prop_chi_bse).and.(X_do_obs("eps").or.X_do_obs("alpha"))) then
         call X_obs_onoff("- eps alpha")
         call warning("eps & alpha evaluation switched off for slepc")
       endif
     endif
     if (STRING_match(solver,"Inversion")) call X_obs_onoff("- Esort E_IP E_INDX_IP kerr magnons jdos")
     !if (STRING_match(solver,"Haydock")) call X_obs_onoff("- Esort E_IP E_INDX_IP")
     if (STRING_match(solver,"Haydock")) call X_obs_onoff("- Esort E_IP E_INDX_IP jdos")
     if (STRING_match(solver,"Diago").or.STRING_match(solver,"Slepc")) call X_obs_onoff("- E_IP E_INDX_IP")
   endif
   !
   ! Headers...
   !------------
   ! ... specific
   if (X_do_obs("fxc")) then
     X_obs(1)%N_columns=2
     X_obs(1)%COL_header(1)   = 'E'
     X_obs(1)%COL_header(2:3) = (/'Re(q^2Fxc)','Re(q^2Fxc)'/)
   endif
   if (X_do_obs("indexes")) then
     X_obs(2)%N_columns=6
     X_obs(2)%COL_header(1:6)  = (/' E  ','ikbz','ib1 ','ib2 ','isp1','isp2'/)
   endif
   if (X_do_obs("Esort")) then
     do ic=3,4
       X_obs(ic)%N_columns=2
       X_obs(ic)%COL_header(1:2)  = (/'E    ','N_deg'/)
     enddo
     if (BS_K_coupling) then
       X_obs(4)%N_columns=3
       X_obs(4)%COL_header(1:3)  = (/'Re(E)','Im(E)','N_deg'/)
     endif
     obs_with_res=[character(5) :: "eps","alpha","Spm","Smp","off","beta","CD" ]
     do it=1,N_X_obs
       if (.not.X_do_obs(trim(X_obs(it)%what))) cycle
       l_print_res=.false.
       do itp=1,7
         l_print_res=l_print_res.or.trim(X_obs(it)%what)==trim(obs_with_res(itp))
       enddo
       if (.not.l_print_res) cycle
       do ic=3,4
         X_obs(ic)%N_columns=X_obs(ic)%N_columns+1
         X_obs(ic)%COL_header(X_obs(ic)%N_columns) =trim(X_obs(it)%what) 
       enddo
     enddo
   endif
   if (X_do_obs("PL")) then
     X_obs(5)%N_columns=3
     X_obs(5)%COL_header(1)   = 'E [eV]'
     X_obs(5)%COL_header(2:3) = (/'PL  ','PL_o'/)
   endif
   !
   ! ... pre-formatted
   BASIC_col(1)   = 'E[1]'
   BASIC_col(2:3) = (/'Im(XXX)', 'Re(XXX)'/)
   BASIC_col(4:5) = (/'Im(XXX_o)', 'Re(XXX_o)'/)
   BASIC_col(6:7) = (/'Im(XXX`)','Re(XXX`)'/)
   do it=6,N_X_obs
     X_obs(it)%N_columns=7 
     do ic=1,X_obs(it)%N_columns
       X_obs(it)%COL_header(ic)=STRING_remove(BASIC_col(ic),"XXX",trim(X_obs(it)%what))
     enddo
   enddo
   !
 !
 contains
   !
   subroutine X_obs_onoff(WHAT)
     character(*)   :: WHAT
     integer        :: it
     do it=1,N_X_obs
       if (STRING_match(WHAT,trim(X_obs(it)%what)).and.STRING_match(WHAT,"+" )) X_obs(it)%active=.TRUE.
       if (STRING_match(WHAT,trim(X_obs(it)%what)).and.STRING_match(WHAT,"-")) X_obs(it)%active=.FALSE.
     enddo
   end subroutine
   !
   subroutine ELEMENTAL_init(it,short,long,scattering)
     integer      :: it
     character(*) :: short,long,scattering
     X_obs(it)%what=short
     X_obs(it)%title=long
     X_obs(it)%scattering=scattering
     X_obs(it)%N_messages=0
     X_obs(it)%N_columns=0
     X_obs(it)%active=.FALSE.
     X_obs(it)%prefix=X_obs(it)%what
   end subroutine
   !
end subroutine OUTPUT_init
!
!
subroutine OUTPUT_refine_filename(WHAT,keys,IQ,IG)
 !
 ! This subroutine refines the filename X_obs%prefix
 !
 use pars,          ONLY:schlen
 use X_m,           ONLY:X_obs,N_X_obs,IS_X_obs_ok
 use stderr,        ONLY:STRING_split,intc
 !
 implicit none
 !
 character(*),  intent(in) :: WHAT
 character(*),  intent(in) :: keys
 integer,       intent(in) :: IQ
 integer,       intent(in) :: IG
 !
 ! Workspace
 !
 character(schlen) :: key(10)
 integer           :: it,ik

   key=' '
   if (keys/="") call STRING_split(keys,key)
   do it=1,N_X_obs
     if (.not.IS_X_obs_ok(it,WHAT)) cycle
     if (IQ>0) then
       X_obs(it)%prefix=trim(X_obs(it)%prefix)//"_q"//trim(intc(IQ))
     endif
     if (IG>0) then
       if (IG>1) X_obs(it)%prefix=trim(X_obs(it)%prefix)//"_G"//trim(intc(IG))
     endif
     do ik=1,10 
       if (len_trim(key(ik))==0) cycle
       X_obs(it)%prefix=trim(X_obs(it)%prefix)//"_"//trim(key(ik))
     enddo
   enddo
 !
end subroutine OUTPUT_refine_filename
!
!
subroutine OUTPUT_open_or_close(mode,WHAT)
 !
 use X_m,            ONLY:X_obs,N_X_obs,IS_X_obs_ok
 use stderr,         ONLY:STRING_match
 use OUTPUT,         ONLY:OUTPUT_driver
 !
 implicit none
 !
 character(*)   :: mode
 character(*)   :: WHAT
 !
 ! Work Space
 !
 integer            ::it
 !
 do it=1,N_X_obs
   if (.not.IS_X_obs_ok(it,WHAT)) cycle
   call OUTPUT_driver(trim(X_obs(it)%prefix),action=mode)
 enddo
 !
 !
end subroutine OUTPUT_open_or_close
!
!
subroutine OUTPUT_write_desc(WHAT,DESC)
 !
 use X_m,            ONLY:X_obs,N_X_obs,IS_X_obs_ok
 use interfaces,     ONLY:DESC_write
 use descriptors,    ONLY:IO_desc
 !
 implicit none
 !
 character(*)   :: WHAT
 type(IO_desc)  :: DESC
 !
 ! Work Space
 !
 integer            ::it
 do it=1,N_X_obs
   if (.not.IS_X_obs_ok(it,WHAT)) cycle
   call DESC_write("o "//trim(X_obs(it)%prefix),'#',DESC)
 enddo
 !
end subroutine OUTPUT_write_desc
!
!
subroutine OUTPUT_write_data(WHAT,data,indexes)
 !
 ! This subroutine write the output to file via OUTPUT_driver
 ! It can mix real numbers and integers.
 ! Moreover, it also writes the headers.
 ! However it is quite slow.
 !
 use pars,          ONLY:SP
 use X_m,           ONLY:X_obs,N_X_obs,IS_X_obs_ok
 use OUTPUT,        ONLY:OUTPUT_driver
 !
 implicit none
 !
 character(*),      intent(in)  :: WHAT
 real(SP),          intent(in)  :: data(:)
 integer, optional, intent(in)  :: indexes(:)
 !
 ! Workspace
 !
 integer            :: it,ic
 !
 do it=1,N_X_obs
   if (.not.IS_X_obs_ok(it,WHAT)) cycle
   call OUTPUT_driver(trim(X_obs(it)%prefix),R_VALUES=data(1:1),TITLES=X_obs(it)%COL_header(1:1),UNIT="eV")
   do ic=2,size(data)
     call OUTPUT_driver(trim(X_obs(it)%prefix),R_VALUES=data(ic:ic),TITLES=X_obs(it)%COL_header(ic:ic))
   enddo
   if (present(indexes)) then
     do ic=1,size(indexes)
       call OUTPUT_driver(trim(X_obs(it)%prefix),I_VALUES=indexes(ic:ic),TITLES=X_obs(it)%COL_header(ic+1:ic+1))
     enddo
   endif
   call OUTPUT_driver(trim(X_obs(it)%prefix),action="write")
 enddo
 !
end subroutine OUTPUT_write_data
!
!
subroutine OUTPUT_write_data_simple(WHAT,data,n)
 !
 ! This subroutine write the output to file via msg interface
 ! It cannot mix real numbers and integers.
 ! It does not write headers.
 ! However it is quite fast.
 !
 use pars,          ONLY:SP
 use units,         ONLY:HA2EV
 use com,           ONLY:msg
 use X_m,           ONLY:X_obs,N_X_obs,IS_X_obs_ok
 !
 implicit none
 !
 character(*),  intent(in)  :: WHAT
 real(SP),      intent(in)  :: data(n)
 integer,       intent(in)  :: n
 !
 ! Workspace
 !
 integer            :: it
 !
 do it=1,N_X_obs
   if (.not.IS_X_obs_ok(it,WHAT)) cycle
   call msg("o "//trim(X_obs(it)%prefix),' ',(/data(1)*HA2EV,data(2:n)/),INDENT=-2,USE_TABS=.TRUE.)
 enddo
 !
end subroutine OUTPUT_write_data_simple
!
!
subroutine OUTPUT_write_titles(WHAT,n)
 !
 ! It does write headers.
 ! To be used before OUTPUT_write_data_simple
 !
 use com,           ONLY:msg
 use X_m,           ONLY:X_obs,N_X_obs,IS_X_obs_ok
 !
 implicit none
 !
 character(*),  intent(in)  :: WHAT
 integer,       intent(in)  :: n
 !
 ! Workspace
 !
 integer            :: it
 !
 do it=1,N_X_obs
   if (.not.IS_X_obs_ok(it,WHAT)) cycle
   if (X_obs(it)%title=="E_INDX_IP")  cycle ! BSE exception ...
   call msg("o "//trim(X_obs(it)%prefix),'#')
   call msg("o "//trim(X_obs(it)%prefix),'#',X_obs(it)%COL_header(:n),INDENT=0,USE_TABS=.TRUE.)
   call msg("o "//trim(X_obs(it)%prefix),'#')
 enddo
 !
end subroutine OUTPUT_write_titles
