subroutine reor_OAV
use lewis_module
Use mod_script
implicit real*8 (a-h,o-x,z),logical*1(y)
logical,allocatable :: occupat_tri(:)



!
!=================================================================
!    On reordonne les OL (Occ - Act - Virt)
!    dans chaque symetrie
! Tri de ORB et Génération d'un fichier d'occupation
!=================================================================
!
      !noacs=0
      !nocs=fermi0
allocate(orbtri(norb,norb))
allocate(occupat_tri(norb))
occ=0.D0
occact=0.D0
ywork=.false.
orbtri=0.D0
iorbtri=0
do is=1,nsym
   call reor_OAV1(is,occupat_tri)
enddo
orb=orbtri
label_OL=label_OL_TEMP
fnat=fnat2
focvir=focvir2
fsigpi=fsigpi2
occupat=occupat_tri
      !print*,'OCCUPAT',occupat
atoms_in_LO=atoms_in_LO2
deallocate(occupat_tri)
deallocate(orbtri)
end
!***********************************************************************
 subroutine reor_OAV1(is,occupat_tri)
use lewis_module
Use mod_script
implicit real*8 (a-h,o-x,z),logical*1(y)
logical :: occupat_tri(norb),yw
yw=.false.

      !print*,'reor_OAV sym',is
      !i=sum(isym(1:is-1))
      !do ii=1,isym(is)
      !   i=i+1
      !   print'(3i4,l2,3x,a)',is,ii,i,occupat(i),label_OL(i)
      !enddo
      !do k=1,norb
      !print*,'k,occupat(k)',k,occupat(k)
      !enddo


!on prend les occupées gelees
i=sum(isym(1:is-1))
do ii=1,isym(is)
   i=i+1
   yG=label_OL(i)(1:1)=='c'.or.label_OL(i)(1:1)=='o'
   if(occupat(i).and.yG) then
      iorbtri=iorbtri+1
      orbtri(:,iorbtri)=orb(:,i)
      occ(iorbtri)=2.D0
      label_OL_temp(iorbtri)=trim(label_OL(i))
      fnat2(iorbtri)=fnat(i)
      focvir2(iorbtri)=focvir(i)
      fsigpi2(iorbtri)=fsigpi(i)
      occupat_tri(iorbtri)=occupat(i)
      atoms_in_LO2(iorbtri,1:fnat(i))=atoms_in_LO(i,1:fnat(i))
      ywork(i)=.true.
      if(yw)print'(a,3i4,f12.8,2a)','GO',is,i,iorbtri,occ(iorbtri),'  ',LABEL_OL(i)
   end if
end do
!on prend les orbitales de coeur
i=sum(isym(1:is-1))
do ii=1,isym(is)
   i=i+1
   if(ywork(i)) cycle
   if(label_OL(i)(1:1)=='C') then
      iorbtri=iorbtri+1
      orbtri(:,iorbtri)=orb(:,i)
      occ(iorbtri)=2.D0
      label_OL_temp(iorbtri)=trim(label_OL(i))
      fnat2(iorbtri)=fnat(i)
      focvir2(iorbtri)=focvir(i)
      fsigpi2(iorbtri)=fsigpi(i)
      occupat_tri(iorbtri)=occupat(i)
      atoms_in_LO2(iorbtri,1:fnat(i))=atoms_in_LO(i,1:fnat(i))
      ywork(i)=.true.
      if(yw)print'(a,3i4,f12.8,2a)','CO',is,i,iorbtri,occ(iorbtri),'  ',LABEL_OL(i)
   end if
end do
!on prend les valence occupées
i=sum(isym(1:is-1))
do ii=1,isym(is)
   i=i+1
   if(ywork(i)) cycle
   if(occupat(i).and.label_OL(i)(1:1)=='O') then
      iorbtri=iorbtri+1
      orbtri(:,iorbtri)=orb(:,i)
      occ(iorbtri)=2.D0
      label_OL_temp(iorbtri)=trim(label_OL(i))
      fnat2(iorbtri)=fnat(i)
      focvir2(iorbtri)=focvir(i)
      fsigpi2(iorbtri)=fsigpi(i)
      occupat_tri(iorbtri)=occupat(i)
      atoms_in_LO2(iorbtri,1:fnat(i))=atoms_in_LO(i,1:fnat(i))
      ywork(i)=.true.
      if(yw)print'(a,3i4,f12.8,2a)','OO',is,i,iorbtri,occ(iorbtri),'  ',LABEL_OL(i)
   end if
end do
!on prend les actives
i=sum(isym(1:is-1))
do ii=1,isym(is)
   i=i+1
   if(ywork(i)) cycle
   if(label_OL(i)(1:1)=='A') then
            !print*,'occu',i,occupat(i),label_OL_TEMP(i)
            !noacs(is)=noacs(is)+1
            !if(occupat(i)) nocs(is)=nocs(is)-1
      iorbtri=iorbtri+1
      orbtri(:,iorbtri)=orb(:,i)
      occ(iorbtri)=0.D0
      occact(iorbtri)=0.D0
      if(occupat(i)) occact(iorbtri)=2.d0
      label_OL_temp(iorbtri)=trim(label_OL(i))
      fnat2(iorbtri)=fnat(i)
      focvir2(iorbtri)=focvir(i)
      fsigpi2(iorbtri)=fsigpi(i)
      occupat_tri(iorbtri)=occupat(i)
      atoms_in_LO2(iorbtri,1:fnat(i))=atoms_in_LO(i,1:fnat(i))
      ywork(i)=.true.
      if(yw)print'(a,3i4,f12.8,2a)','A ',is,i,iorbtri,occ(iorbtri),'  ',LABEL_OL(i)
   end if
end do
!on prend les non-occupées de valence
i=sum(isym(1:is-1))
do ii=1,isym(is)
   i=i+1
   !print*,'ywork(i),occupat(i),active(i)',i,ywork(i),occupat(i),active(i)
   if(ywork(i)) cycle
   if(.not. occupat(i).and.label_OL(i)(1:1)=='O') then
      iorbtri=iorbtri+1
      occ(iorbtri)=0.d0
      orbtri(:,iorbtri)=orb(:,i)
      label_OL_temp(iorbtri)=trim(label_OL(i))
      fnat2(iorbtri)=fnat(i)
      focvir2(iorbtri)=focvir(i)
      fsigpi2(iorbtri)=fsigpi(i)
      occupat_tri(iorbtri)=occupat(i)
      atoms_in_LO2(iorbtri,1:fnat(i))=atoms_in_LO(i,1:fnat(i))
      ywork(i)=.true.
      if(yw) print'(a,3i4,f12.8,2a)','VO',is,i,iorbtri,occ(iorbtri),'  ',LABEL_OL(i)
   end if
   ilongueur=len_trim(label_OL(i))
end do
!on prend les diffuses
i=sum(isym(1:is-1))
do ii=1,isym(is)
   i=i+1
   if(ywork(i)) cycle
   if(.not.occupat(i).and.label_OL(i)(1:1)=='T') then
      iorbtri=iorbtri+1
      orbtri(:,iorbtri)=orb(:,i)
      occ(iorbtri)=0.D0
      label_OL_temp(iorbtri)=trim(label_OL(i))
      fnat2(iorbtri)=fnat(i)
      focvir2(iorbtri)=focvir(i)
      fsigpi2(iorbtri)=fsigpi(i)
      occupat_tri(iorbtri)=occupat(i)
      atoms_in_LO2(iorbtri,1:fnat(i))=atoms_in_LO(i,1:fnat(i))
      ywork(i)=.true.
      if(yw) print'(a,3i4,f12.8,2a)','VD',is,i,iorbtri,occ(iorbtri),'  ',LABEL_OL(i)
   end if
end do
!on prend les virtuelles et diffuses gelees
i=sum(isym(1:is-1))
do ii=1,isym(is)
   i=i+1
   if(ywork(i)) cycle
   yG=label_OL(i)(1:1)=='o'.or.label_OL(i)(1:1)=='t'
   if(.not.occupat(i).and.yG) then
      iorbtri=iorbtri+1
      orbtri(:,iorbtri)=orb(:,i)
      occ(iorbtri)=0.D0
      label_OL_temp(iorbtri)=trim(label_OL(i))
      fnat2(iorbtri)=fnat(i)
      focvir2(iorbtri)=focvir(i)
      fsigpi2(iorbtri)=fsigpi(i)
      occupat_tri(iorbtri)=occupat(i)
      atoms_in_LO2(iorbtri,1:fnat(i))=atoms_in_LO(i,1:fnat(i))
      if(yw) print'(a,3i4,f12.8,2a)','VG',is,i,iorbtri,occ(iorbtri),'  ',LABEL_OL(i)
   end if
end do
!do i=1,norb ; print*,'LABEL_OL_TEMP',i,'   ',LABEL_OL_TEMP(i) ; enddo
end
