      program adas_init
! use to initialize adas tables, write tableParameters into $ADASDIR/tables/<reaction type>/...
!Energy and Temperature -- [KeV/amu]
!density -- [1/cm**3]
        use ezcdf
        implicit none
        INTEGER, PARAMETER :: R8=SELECTED_REAL_KIND(12,100)
        character*40  :: adas_tables_file
        character*2, dimension(4)   :: react_type =(/ 'cx', 'ii', 'ei','sv' /)
        integer, dimension(2) :: izneut=(/1,2/) ! atomic charge of primary   particle {1, 2}
        integer, dimension(2,10) :: izion  ! atomic charge of secondary particle {1, ..., 10}
        character*9 :: cizneut, cizion    ! character if izneut and izion to creat filename
        character*240 :: filename         ! filename
        real*8, dimension(4,2,10,4) :: axis_param_coldTarget !axis parameter for table: min, max values,
                                                        ! number of points,specification 0 for LOG, 1 for LIN
        real*8, dimension(4,2,10,8) :: axis_param_warmTarget !axis parameter for table: min, max values, number of points,
                                                        !specification 0 for LOG, 1 for LIN
        real*8, dimension(4,2,10,8) :: axis_param_energyAvg_perAMU!axis parameter for table: min, max values,
                                                        ! number of points,specification 1 for LOG, 0 for LIN
        real*8, dimension(4,2,10,8) :: axis_param_average_vDotNorm!axis parameter for table: min, max values,
                                                        ! number of points,specification 0 for LOG, 1 for LIN
        real*8, dimension(10,12) :: axis_param_excite  !axis parameter for table: min, max values,
                                                        ! number of points,specification 0 for LOG, 1 for LIN
        integer :: i, ii, ij
        integer :: icdf,fErr

        izion(1,:)=(/1,2,3,4,5,6,7,8,9,10/) !available reaction for H CX, see adas_ion
        izion(2,:)=(/1,2,0,4,0,6,0,8,0,0/)  !available reaction for He CX, see adas_ion

        ! create table filename
        axis_param_coldTarget=0._R8
        axis_param_warmTarget=0._R8
        axis_param_energyAvg_perAMU=0._R8
        axis_param_average_vDotNorm=0._R8
        axis_param_excite=0._R8

!Charge-Exchange
! H->H(cx)
        axis_param_coldTarget(1,1,1,:)= (/1e-3_R8, 1e3_R8, 400._R8, 0._R8/)
        axis_param_warmTarget(1,1,1,:)= (/1e-3_R8, 1e3_R8, 192._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
        axis_param_energyAvg_perAMU(1,1,1,:)= (/1e-3_R8, 1e3_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
        axis_param_average_vDotNorm(1,1,1,:)= (/1e-3_R8, 1e3_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
! H+->He0x
        axis_param_coldTarget(1,2,1,:)= (/1e-3_R8, 2.5e4_R8, 400._R8, 0._R8/)
        axis_param_warmTarget(1,2,1,:)= (/1e-2_R8, 1e4_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
        axis_param_energyAvg_perAMU(1,2,1,:)= (/1e-2_R8, 1e4_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
        axis_param_average_vDotNorm(1,2,1,:)= (/1e-2_R8, 1e4_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
! He++>Hex
        axis_param_coldTarget(1,2,2,:)= (/1e-3_R8, 1e3_R8, 400._R8, 0._R8/)
        axis_param_warmTarget(1,2,2,:)= (/1e-3_R8, 1e3_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
        axis_param_energyAvg_perAMU(1,2,2,:)= (/1e-3_R8, 1e3_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
        axis_param_average_vDotNorm(1,2,2,:)= (/1e-3_R8, 1e3_R8, 64._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
! Ionization
        do ii=1,2
           do ij=1,10
              if (izion(ii,ij).eq.0) cycle
              axis_param_coldTarget(2,ii,ij,:)= (/1e-3_R8, 2.5e4_R8, 400._R8, 0._R8/)
              axis_param_warmTarget(2,ii,ij,:)= (/1e-3_R8, 1e4_R8, 96._R8, 0._R8, 1e-3_R8, 2e2_R8, 64._R8, 0._R8/)
           enddo
        enddo
! H+H II
        axis_param_warmTarget(2,1,1,:)= (/1e-3_R8, 1e4_R8, 192._R8, 0._R8, 1e-3_R8, 2e2_R8, 32._R8, 0._R8/)
!H,e- -> H+,e-  impact ionization
        axis_param_coldTarget(3,1,1,:)= (/1e-3_R8, 2.5e4_R8, 400._R8, 0._R8/)
!H stopping on impurities {1,..,10}
        do ij=1,10
           axis_param_coldTarget(4,1,ij,:)= (/1e-3_R8, 2.5e4_R8, 400._R8, 0._R8/)
        enddo

!H stopping on impurities {1,..,10} excited state
        goto 332
        do ij=1,10
           axis_param_excite(ij,:)= (/1e-3_R8, 2.5e4_R8, 48._R8, 0._R8,1.000E+12_R8,1.000E+15_R8,50._R8, 1._R8,&
                &1e-3_R8, 2e2_R8, 192._R8, 1._R8/)
        enddo
332     continue

        do i=1,4  !"cx", "ii", "ei", "sv"
           ! write tableParameters file with dummy data
           adas_tables_file=trim('tables/')//trim(react_type(i))//'/'//'tableParameters.cdf'
           call UFILNAM('ADASDIR',adas_tables_file,filename)
           call cdf_open(icdf,trim(filename),'w',fErr)
           call cdf_define(icdf,'axis_param_coldTarget',axis_param_coldTarget(i,1,1,1:4)) 
           call cdf_define(icdf,'axis_param_warmTarget',axis_param_warmTarget(i,1,1,1:8)) 
           call cdf_define(icdf,'axis_param_energyAvg_perAMU',&
                &axis_param_energyAvg_perAMU(i,1,1,1:8))   
           call cdf_define(icdf,'axis_param_average_vDotNorm',&
                &axis_param_average_vDotNorm(i,1,1,1:8)) 
           call cdf_write(icdf,'axis_param_coldTarget',axis_param_coldTarget(i,1,1,1:4)) 
           call cdf_write(icdf,'axis_param_warmTarget',axis_param_warmTarget(i,1,1,1:8)) 
           call cdf_write(icdf,'axis_param_energyAvg_perAMU',&
                &axis_param_energyAvg_perAMU(i,1,1,1:8))   
           call cdf_write(icdf,'axis_param_average_vDotNorm',&
                &axis_param_average_vDotNorm(i,1,1,1:8))
           call cdf_close(icdf)
           do  ii=1,2
              write(cizneut,'(i5)') izneut(ii)
              do ij=1,10
                 if(axis_param_coldTarget(i,ii,ij,1).eq.0._R8) cycle  ! reaction not available
                 write(cizion,'(i5)') izion(ii,ij)
                 adas_tables_file=trim('tables/')//trim(react_type(i))//'/'//trim(react_type(i))//'_'//&
                      & trim(adjustl(cizneut))//'_'//trim(adjustl(cizion))//'_'//'tableParameters.cdf'
                 if(i.eq.3) then  !for electron impact ionization
                    adas_tables_file=trim('tables/')//trim(react_type(i))//'/'//trim(react_type(i))//'_'//&
                         & trim(adjustl(cizneut))//'_'//'tableParameters.cdf'
                 endif
                    
                 call UFILNAM('ADASDIR',adas_tables_file,filename)
                 call cdf_open(icdf,trim(filename),'w',fErr)
                 if(fErr.ne.0) then
                    write(6,*) ' ?adas_init: open failure: ',trim(filename)
                    stop
                 endif
                 call cdf_define(icdf,'axis_param_coldTarget',axis_param_coldTarget(i,ii,ij,1:4)) 
                 if(i.lt.3) then
                    call cdf_define(icdf,'axis_param_warmTarget',axis_param_warmTarget(i,ii,ij,1:8)) 
                    if(i.lt.2) then
                       call cdf_define(icdf,'axis_param_energyAvg_perAMU',&
                            &axis_param_energyAvg_perAMU(i,ii,ij,1:8))   
                       call cdf_define(icdf,'axis_param_average_vDotNorm',&
                            &axis_param_average_vDotNorm(i,ii,ij,1:8)) 
                    endif
                 endif
                 call cdf_write(icdf,'axis_param_coldTarget',axis_param_coldTarget(i,ii,ij,1:4)) 
                 if(i.lt.3) then
                    call cdf_write(icdf,'axis_param_warmTarget',axis_param_warmTarget(i,ii,ij,1:8)) 
                    if(i.lt.2) then
                       call cdf_write(icdf,'axis_param_energyAvg_perAMU',&
                            &axis_param_energyAvg_perAMU(i,ii,ij,1:8))
                       call cdf_write(icdf,'axis_param_average_vDotNorm',&
                            &axis_param_average_vDotNorm(i,ii,ij,1:8)) 
                    endif
                 endif
                 call cdf_close(icdf)
             enddo !end loop for izion
           enddo !end loop for izneut
        enddo !end loop for reaction type
!sv from excited states
        goto 333
        adas_tables_file=trim('tables/sv/')//'ex_tableParameters.cdf'
        call UFILNAM('ADASDIR',adas_tables_file,filename)
        call cdf_open(icdf,trim(filename),'w',fErr)
        call cdf_define(icdf,'axis_param_excite',axis_param_excite(1,1:12)) 
        call cdf_write(icdf,'axis_param_excite',axis_param_excite(1,1:12)) 
        call cdf_close(icdf)
        do  ii=1,1
           write(cizneut,'(i5)') izneut(ii)
           do ij=1,10
              write(cizion,'(i5)') izion(ii,ij)
              adas_tables_file=trim('tables/sv/ex')//'_'//&
                   & trim(adjustl(cizneut))//'_'//trim(adjustl(cizion))//'_'//'tableParameters.cdf'
              call UFILNAM('ADASDIR',adas_tables_file,filename)
              call cdf_open(icdf,trim(filename),'w',fErr)
              call cdf_define(icdf,'axis_param_excite',axis_param_excite(ij,1:12)) 
              call cdf_write(icdf,'axis_param_excite',axis_param_excite(ij,1:12)) 
              call cdf_close(icdf)
           enddo
        enddo

333     continue
        
        stop
      end program adas_init
