      subroutine q52orb(if_orb)
      use mod_q5
      use mod_script
      use q5cost
      use mod_lirorb
      character(300) q5file
      integer(hid_t) file_id
      integer(4) error
      real(8),dimension(:,:),allocatable :: orbitals,orb2
      real(8),dimension(:),allocatable :: occupations,occ2
      integer(4),dimension(:),allocatable :: itsym
      integer,allocatable :: isym(:),isyms(:)
      scforb='Q5ORB'
!
! init library, open
!
      q5file=trim(workdir)//trim(prefix)//'q5'
      call q5cost_init(error)
      call q5cost_file_open(q5file,file_id,error)
!
      
!
! Allocate
!
      allocate(orbitals(norb,norb))
      allocate(occupations(norb))
!
! Read symmetry
!
      call Q5Cost_Sys_Symmetry_get_num_sym(file_id,nsym,error)
      allocate(isym(nsym))
      allocate(isyms(nsym))
      call q5cost_ao_get_num_orb_sym(file_id,isym,error)
      norb=sum(isym(1:nsym))
      do is=1,nsym
         isyms(is)=sum(isym(1:is-1))
      enddo
!!!!!!!!!!!!warning!!!!!!!! The symmetry AO's re supposed to be
!    ordered by symmetry!!!!!!!!!!!!!!
!
!     read MO symmetry
      allocate(itsym(norb))
      call  Q5Cost_MO_get_symmetry(file_id,itsym,error )
!
! Read
!
      call Q5Cost_MO_get_matrix(file_id, orbitals, error )
      call Q5Cost_MO_get_occ_num(file_id, occupations, error)
!
! Write
!
      open(12,form='unformatted',status='unknown',file='tmp_orb')
      open(13,form='unformatted',status='unknown',file='tmp_occ')
      print*,'Orbitals'
      call mscript(orbitals)
      do isy=1,nsym
         print*,'isym',isym
         allocate(orb2(isym(isy),isym(isy)))
         allocate(occ2(isym(isy)))
         kk=0
         do io=1,norb
            if(itsym(io)/=isy) cycle
            kk=kk+1
            orb2(:,kk)
     *      =orbitals(isyms(isy)+1:isyms(isy)+isym(isy),io)
            occ2(kk)=occupations(io)
         enddo
         write(12) orb2
         print*,'occupations',occ2
         call mscript(orb2)
         write(13) occ2
         deallocate(orb2,occ2)
      enddo
      rewind 12
      rewind 13
      print*,'avant ecriorb'
      !open(if_orb,file=trim(prefix)//'scforb',status='unknown')
      print*,'isym',isym(1:nsym)
      call ecriorb('Dolo-Q5 Orbitals',scforb1,nsym,isym,12,if_orb,
     *ifiloc=13,nolabels=.true.)
      close(12,status='delete')
      close(13,status='delete')
      deallocate(occupations,orbitals)
      close(if_orb)
      return
      end

    
