
  ! The module rcvm defines the type covama,
  ! which contains the necessary parameters to perform correlated chi^2 fits.

  module rcvm

  implicit none
    
  type covama
     integer :: n_row, chi2, n_data ! n_row: row count, n_row x n_row = dimension of covariance matrix
                                    ! n_data: number of data in fit in a given SE bin
     real(kind(0.d0)):: e_cm        ! central W of the energy bin 
     real(kind(0.d0)), dimension(44) :: iso, ll, jj ! parameters defining the partial wave: iso: isospin,
                                                    !  ll: orbital angular momentum, jj: total angular momentum
     integer, dimension(44) :: qq                   ! 
     real(kind(0.d0)), dimension(44) :: rho         ! phase shift
     real(kind(0.d0)), dimension(44,44) :: item     ! elements of the covariance matrix
    
  end type covama

  type(covama), dimension(0:100) :: cvm   ! cvm(1)%item(1...44,1...44) = elements of the covariance matrix of first set,
                                          !   e.g., cvm(1)%item(1,2) corresponds to the covariance matrix element calculated 
                                          !   from parameters with isospin cvm(1)%iso(1) and cvm(1)%iso(2)
                                          ! dimension of cvm depends on the number of sets. Here: 75

  contains

 !-----------------------------------------------

 ! Subroutine to read in the covariance matrix and the other parameters to describe the chi^2

  subroutine read_covama

  implicit none
    
  integer :: i, k
  character(len=4):: tmp
  real(kind(0.d0)):: e_max

  e_max=2476.d0
  i=0
  cvm(0)%e_cm=0.d0
  
  
  open(99,file='matrix.txt')

  
  do while (cvm(i)%e_cm.lt.e_max) ! stop at last e_cm in file 99 which is the first e_i to be larger than 2571

     i=i+1
     
     read(99,'(f6.1)') cvm(i)%e_cm
     read(99,'(i2)') cvm(i)%n_row
     read(99,'(i5)') cvm(i)%chi2
     read(99,'(i5)') cvm(i)%n_data

     do k=1,cvm(i)%n_row
        read(99,*) cvm(i)%iso(k), cvm(i)%ll(k), cvm(i)%jj(k), cvm(i)%qq(k), cvm(i)%rho(k)       
     end do !k
        
     do k=1,cvm(i)%n_row
        read(99,*) cvm(i)%item(k,1:cvm(i)%n_row)
     end do !k

     read(99,*)
     read(99,*)
     
  end do !while


  close(99)

  end subroutine read_covama

 !-----------------------------------------------
  
 ! test routine to check if the parameters were read in correctly

  subroutine test_covama

  implicit none
    
  integer :: i, k
  character(len=4):: tmp
  real(kind(0.d0)):: e_max
    
    
  open(98,file='test_covama.txt') ! this file should be compared to the file 'matrix.txt'

  do i=1,75
     write(98,'(f6.1)')cvm(i)%e_cm

     write(98,'(i2)') cvm(i)%n_row
     write(98,'(i5)') cvm(i)%chi2
     write(98,'(i5)') cvm(i)%n_data

     do k=1,cvm(i)%n_row
       write(98,'(f3.1,f7.1,f7.1,i5,f11.3)') cvm(i)%iso(k), cvm(i)%ll(k), cvm(i)%jj(k), cvm(i)%qq(k), cvm(i)%rho(k)       
     end do !k

     do k=1,cvm(i)%n_row
        write(98,'(100ES15.7)') cvm(i)%item(k,1:cvm(i)%n_row)    
     end do !k

     write(98,'(a1)')' '
     write(98,'(a1)')' '
     
     
  end do ! i
  
  close(98)

  end subroutine test_covama

 !-----------------------------------------------
 
  end module rcvm

  
!====================================================================
  
! Main program that calls the subroutines read_covama and test_covama
  
program covariance

  use rcvm

  call read_covama

  call test_covama

 
end program covariance


!====================================================================

  




