      subroutine orbi
      use module
      use mod_licost
      use mod_script
      use mod_lirorb
      IMPLICIT REAL(8)(A-H,O-X,Z),logical(1)(y)
      real(8),allocatable,dimension(:,:) :: orbref,orb,orbfr
      dimension isymbid(8)
      character(80) title
      yw=.false.
!
!
!
      if(yw)print*,'noa,norb',noa,norb
      if(yw)print*,'isymoa',isymoa
      if(yw)print*,'isym',isym
      if(yw)print*,'frozen',frozen
      print*,' '
      print*,'===============ORBITALS==========================='
      call lirorb(orb1,norbbid,nsymbid,isymbid,32,22,title=title)
      close(22)
      print*,'read new orbitals on file ',trim(orb1)
      print*,                  'Header: ',trim(title)
      print*,'=================================================='
      if(yw)print*,trim(orb1),' lu'
 
 
      open(33,form='unformatted',status='unknown',file='PASS')
      open(34,form='unformatted',status='unknown',file='PASSfr')
      rewind 32
      do is=1,nsym
         allocate(orbfr(isymoa(is),frozen(is)))
         allocate(orb(isymoa(is),isym(is)))
         if(isymoa(is)>0)read(32) orbfr,orb
         write(33) orb
         write(34) orbfr
         if(nprint>0)print*,'frozen orbitals, sym',is
         if(nprint>0)call mscript(orbfr)
         if(nprint>0)print*,'non frozen orbitals, sym',is
         if(nprint>0)call mscript(orb)
         deallocate(orb,orbfr)
      enddo
!      
! insinv: re-sorting orbitals
!
! sorting according to Fermi Level (Occ, Virt.)
      allocate(insinv(norb))
      if(orb_sort=='OV') then
         nn=0
         ko=0
         noc=sum(fermi(1:nsym))
         if(yw) print*,'fermi',fermi
         if(yw) print*,'noc',noc
         kv=noc
         do is=1,nsym
            do k=1,fermi(is)
               nn=nn+1
               ko=ko+1
               insinv(nn)=ko
            enddo
            do k=1,isym(is)-fermi(is)
               nn=nn+1
               kv=kv+1
               insinv(nn)=kv
            enddo
         enddo
! sorting according to nature of orb (Inac, Act, Virt)
      else if(orb_sort=='OAV') then
         nn=0
         ko=0
         ninac=sum(inac(1:nsym))
         ka=ninac
         kv=ninac+noac
         do is=1,nsym
            do k=1,inac(is)
               nn=nn+1
               ko=ko+1
               insinv(nn)=ko
            enddo
            do k=1,act(is)
               nn=nn+1
               ka=ka+1
               insinv(nn)=ka
            enddo
            do k=1,isym(is)-inac(is)-act(is)
               nn=nn+1
               kv=kv+1
               insinv(nn)=kv
            enddo
         enddo
! sorting symmetry after symmetry
      else if(orb_sort=='SYM') then
         insinv=(/(k,k=1,norb)/)
      else
         print*,'bad value for orb_sort. must be OV, OAV or SYM'
         stop
      endif
      if(yw)print'(a)','insinv'
      if(yw)print'(20i4)',insinv(1:norb)
      return
      end
