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


!
!=================================================================
!    On place les gelees occupees en tete, 
!    les gelees virtuelles en queue
!    dans chaque symetrie
! Tri de ORB et Génération d'un fichier d'occupation
!=================================================================
!
allocate(orbtri(norb,norb))
allocate(occupat_tri(norb))

ifin=0
if(yw) then
   do i=1,norb
      print*,'label_OL(i),occupat(i)  ',i,label_OL(i),occupat(i)
   enddo
endif
iorbtri=0
do is=1,nsym
   ideb=ifin+1
   ifin=ifin+isym(is)

   ! occupees gelees
   if(yw) then
      do i=ideb,ifin
         print*,'label_OL(i),occupat(i)  ',label_OL(i),occupat(i)
      enddo
   endif
   do i=ideb,ifin
      if(label_OL(i)(1:1)=='c'.or.label_OL(i)(1:1)=='o'.and.occupat(i)) then
         iorbtri=iorbtri+1
         if(yw)print*,'1label_OL  ',label_OL(i),iorbtri,i
         orbtri(:,iorbtri)=orb(:,i)
         label_OL_temp(iorbtri)=label_OL(i)
         label_OL_temp(iorbtri)(1:1)='G'
         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))
      endif
   enddo
   ! coeurs et occupees
   do i=ideb,ifin
      if((label_OL(i)(1:1)=='C'.or.label_OL(i)(1:1)=='O').and.occupat(i)) then
         iorbtri=iorbtri+1
         if(yw)print*,'2label_OL  ',label_OL(i),iorbtri,i
         orbtri(:,iorbtri)=orb(:,i)
         label_OL_temp(iorbtri)=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))
      endif
   enddo
   ! actives
   do i=ideb,ifin
      if(label_OL(i)(1:1)=='A') then
         iorbtri=iorbtri+1
         if(yw)print*,'3label_OL  ',label_OL(i),iorbtri,i
         orbtri(:,iorbtri)=orb(:,i)
         label_OL_temp(iorbtri)=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))
      endif
   enddo
   ! virtuelles
   do i=ideb,ifin
      if((label_OL(i)(1:1)=='O'.or.label_OL(i)(1:1)=='T')&
         .and..not.occupat(i)) then
         iorbtri=iorbtri+1
         if(yw)print*,'4label_OL  ',label_OL(i),iorbtri,i,occupat(i)
         orbtri(:,iorbtri)=orb(:,i)
         label_OL_temp(iorbtri)=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))
      endif
   enddo
   ! virtuelles gelees
   do i=ideb,ifin
      if((label_OL(i)(1:1)=='o'.and..not.occupat(i)).or.label_OL(i)(1:1)=='t') then
         iorbtri=iorbtri+1
         if(yw)print*,'5label_OL  ',label_OL(i),iorbtri,i
         orbtri(:,iorbtri)=orb(:,i)
         label_OL_temp(iorbtri)=label_OL(i)
         label_OL_temp(iorbtri)(1:1)='G'
         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))
      endif
   enddo
   if(yw)print*,'norb,iorbtri',norb,isym(is),iorbtri
enddo
do i=1,norb
   orb(:,i)=orbtri(:,i)
   label_OL(i)=label_OL_temp(i)
   fnat(i)=fnat2(i)
   focvir(i)=focvir2(i)
   fsigpi(i)=fsigpi2(i)
   occupat(i)=occupat_tri(i)
   atoms_in_LO(i,1:fnat(i))=atoms_in_LO2(i,1:fnat(i))
enddo
deallocate(orbtri,occupat_tri)
if(yw) then
   do i=1,norb
      print*,'label_OL(i),occupat(i)  ',i,label_OL(i),occupat(i)
   enddo
endif
end
