!23456789a123456789b123456789c123456789d123456789e123456789f123456789g12
! D.J. Jeffery, 2020jan06
!      include 'partition_function_read.f'
!23456789a123456789b123456789c123456789d123456789e123456789f123456789g12
!
      subroutine partition_function_read(nel,nion,jel_ion,idiagnostic,        &
     & aname,aweight,xion_en,partfunc)
!
      use precision_set
      use contants
      include 'module_implicit.f'
      character, intent(out) :: aname(nel)*2
      character :: record*100
      integer :: i,j,k,L,m,n ! Usually count variables for doloops.
      integer, intent(in) :: jel_ion(2,nion)
      integer, intent(in) :: nel ! Only to A=38 Sr is allowed at present. 
!                               !   Note nel cannot have an initializer if it is used to
!                               !   allocate array space.  Or so it seems.
      integer, intent(in) :: nion ! Ionization stages, -1,0,1,2,5 are calculated---except for
!                                 !   for those that do not have them, of course.
!                                 !   Stage 5 is treated as having infinite ionization energy
!                                 !   represented by 9999.999 which is just a placeholder.
!                                 !   The stage -1 is calculated for only H:  i.e., H-.
!                                 !   The ih_minus_not = 1 turns off the H-
!                                 !   which is useful for comparison to the
!                                 !   the analytic pure H xne_h which does not use stage H-.
!                                 !   Note nion cannot have an initializer if it is used to
!                                 !   allocate array space.  Or so it seems.
      integer, intent(in) :: idiagnostic   ! Input:  0 for no diagnostics of the saha solver.
!                                          !   1 for diagnostics for saha_solver.f
!                                          !   2 for diagnostics for saha_solver.f
!                                          !     and 'partition_function_read.f'
      real (kind=np), intent(out) :: aweight(nel) 
      real (kind=np), intent(out) :: partfunc(-1:nion,nel)
      real (kind=np), intent(out) :: xion_en(-1:nion,nel)
!
      xion_en(:,:)=0.0_np     ! Just to be definite especial for the -1 ionization stage.
      partfunc(:,:)=0.0_np    ! Just to be definite especial for the -1 ionization stage.
      open(unit=1,file='partition_function.dat',                            &
     & status='old',action='read')
      do410 : do
        read(1,'(a)') record
        if(idiagnostic .eq. 3) print*,record
        if(record(1:4) .eq. '!234') exit do410
      end do do410
      do 420 : do k=1,nel
        read(1,fmt=910) record
        if(idiagnostic .eq. 3) write(*,fmt=910) record
        read(record(2:13),'(a2,6x,f4.0)') aname(k),aweight(k)
        read(record(32:100),*) xion_en(jel_ion(1,k):jel_ion(2,k),k)
        if(idiagnostic .eq. 3) write(*,fmt=920) aname(k),k,aweight(k),
     &        xion_en(jel_ion(1,k):jel_ion(2,k),k)
        read(1,fmt=910) record
        if(idiagnostic .eq. 3) write(*,fmt=910) record
        read(record(32:100),*) partfunc(jel_ion(1,k):jel_ion(2,k),k)
        if(idiagnostic .eq. 3) write(*,fmt=930)                              &
     &              partfunc(jel_ion(1,k):jel_ion(2,k),k)
  910   format(85a)
  920   format(1x,a2,i4,f6.0,18x,7f9.3)
  930   format(31x,7f9.3)
        do j=1,3
          read(1,'(a)') record
        end do
      end do do420
      close(unit=1)
!
      end subroutine partition_function_read 
!
!23456789a123456789b123456789c123456789d123456789e123456789f123456789g12
