






















































































































 


































































































































































 























































































































































































 



















































 










 








































































































































































































































 










                                
                                

                                
                                

                                
                                

                                

















































































module NullDecomp_IO
   implicit none

 contains

subroutine NullDecomp_WriteCoefFile(FieldName, TruncateFile, Lmin, Lmax, YlmCoef, time)
  character(*), intent(in) :: FieldName
  logical,      intent(in) :: TruncateFile 
  INTEGER*4,     intent(in) :: Lmin, Lmax
  REAL*8,    intent(in) :: time
  COMPLEX*16, dimension(Lmin:Lmax, -Lmax:Lmax) :: YlmCoef

  character(1000) DirName, FileName, lName, mName

  integer :: flen, nchar, l, m
  logical :: file_exists

  INTEGER*4 l_max
  integer, parameter :: cctki_use_l_max = kind(l_max)
  COMMON /NullDecomprest/l_max
  INTEGER*4 store_ylms
  integer, parameter :: cctki_use_store_ylms = kind(store_ylms)
  INTEGER*4 use_rsYlm
  integer, parameter :: cctki_use_use_rsYlm = kind(use_rsYlm)
  COMMON /NullDecomppriv/store_ylms, use_rsYlm
  REAL*8 CCTKH5
  integer, parameter :: cctki_use_CCTKH5 = kind(CCTKH5)
  REAL*8 CCTKH19
  integer, parameter :: cctki_use_CCTKH19 = kind(CCTKH19)
  REAL*8 CCTKH29
  integer, parameter :: cctki_use_CCTKH29 = kind(CCTKH29)
  REAL*8 CCTKH31
  integer, parameter :: cctki_use_CCTKH31 = kind(CCTKH31)
  REAL*8 CCTKH33
  integer, parameter :: cctki_use_CCTKH33 = kind(CCTKH33)
  REAL*8 CCTKH35
  integer, parameter :: cctki_use_CCTKH35 = kind(CCTKH35)
  REAL*8 CCTKH37
  integer, parameter :: cctki_use_CCTKH37 = kind(CCTKH37)
  REAL*8 CCTKH39
  integer, parameter :: cctki_use_CCTKH39 = kind(CCTKH39)
  REAL*8 CCTKH41
  integer, parameter :: cctki_use_CCTKH41 = kind(CCTKH41)
  REAL*8 CCTKH43
  integer, parameter :: cctki_use_CCTKH43 = kind(CCTKH43)
  REAL*8 CCTKH45
  integer, parameter :: cctki_use_CCTKH45 = kind(CCTKH45)
  integer*8 CCTKH2
  integer, parameter :: cctki_use_CCTKH2 = kind(CCTKH2)
  integer*8 CCTKH3
  integer, parameter :: cctki_use_CCTKH3 = kind(CCTKH3)
  integer*8 CCTKH6
  integer, parameter :: cctki_use_CCTKH6 = kind(CCTKH6)
  integer*8 CCTKH9
  integer, parameter :: cctki_use_CCTKH9 = kind(CCTKH9)
  integer*8 CCTKH10
  integer, parameter :: cctki_use_CCTKH10 = kind(CCTKH10)
  integer*8 CCTKH11
  integer, parameter :: cctki_use_CCTKH11 = kind(CCTKH11)
  integer*8 CCTKH15
  integer, parameter :: cctki_use_CCTKH15 = kind(CCTKH15)
  integer*8 out_dir
  integer, parameter :: cctki_use_out_dir = kind(out_dir)
  integer*8 CCTKH21
  integer, parameter :: cctki_use_CCTKH21 = kind(CCTKH21)
  integer*8 CCTKH22
  integer, parameter :: cctki_use_CCTKH22 = kind(CCTKH22)
  integer*8 CCTKH23
  integer, parameter :: cctki_use_CCTKH23 = kind(CCTKH23)
  integer*8 CCTKH25
  integer, parameter :: cctki_use_CCTKH25 = kind(CCTKH25)
  integer*8 CCTKH47
  integer, parameter :: cctki_use_CCTKH47 = kind(CCTKH47)
  integer*8 CCTKH49
  integer, parameter :: cctki_use_CCTKH49 = kind(CCTKH49)
  integer*8 CCTKH51
  integer, parameter :: cctki_use_CCTKH51 = kind(CCTKH51)
  integer*8 CCTKH53
  integer, parameter :: cctki_use_CCTKH53 = kind(CCTKH53)
  integer*8 CCTKH54
  integer, parameter :: cctki_use_CCTKH54 = kind(CCTKH54)
  integer*8 CCTKH57
  integer, parameter :: cctki_use_CCTKH57 = kind(CCTKH57)
  INTEGER*4 CCTKH0
  integer, parameter :: cctki_use_CCTKH0 = kind(CCTKH0)
  INTEGER*4 CCTKH1
  integer, parameter :: cctki_use_CCTKH1 = kind(CCTKH1)
  INTEGER*4 CCTKH4
  integer, parameter :: cctki_use_CCTKH4 = kind(CCTKH4)
  INTEGER*4 CCTKH7
  integer, parameter :: cctki_use_CCTKH7 = kind(CCTKH7)
  INTEGER*4 CCTKH8
  integer, parameter :: cctki_use_CCTKH8 = kind(CCTKH8)
  INTEGER*4 CCTKH12
  integer, parameter :: cctki_use_CCTKH12 = kind(CCTKH12)
  INTEGER*4 CCTKH13
  integer, parameter :: cctki_use_CCTKH13 = kind(CCTKH13)
  INTEGER*4 CCTKH14
  integer, parameter :: cctki_use_CCTKH14 = kind(CCTKH14)
  INTEGER*4 CCTKH16
  integer, parameter :: cctki_use_CCTKH16 = kind(CCTKH16)
  INTEGER*4 CCTKH17
  integer, parameter :: cctki_use_CCTKH17 = kind(CCTKH17)
  INTEGER*4 CCTKH18
  integer, parameter :: cctki_use_CCTKH18 = kind(CCTKH18)
  INTEGER*4 CCTKH20
  integer, parameter :: cctki_use_CCTKH20 = kind(CCTKH20)
  INTEGER*4 CCTKH24
  integer, parameter :: cctki_use_CCTKH24 = kind(CCTKH24)
  INTEGER*4 CCTKH26
  integer, parameter :: cctki_use_CCTKH26 = kind(CCTKH26)
  INTEGER*4 CCTKH27
  integer, parameter :: cctki_use_CCTKH27 = kind(CCTKH27)
  INTEGER*4 CCTKH28
  integer, parameter :: cctki_use_CCTKH28 = kind(CCTKH28)
  INTEGER*4 CCTKH30
  integer, parameter :: cctki_use_CCTKH30 = kind(CCTKH30)
  INTEGER*4 CCTKH32
  integer, parameter :: cctki_use_CCTKH32 = kind(CCTKH32)
  INTEGER*4 CCTKH34
  integer, parameter :: cctki_use_CCTKH34 = kind(CCTKH34)
  INTEGER*4 CCTKH36
  integer, parameter :: cctki_use_CCTKH36 = kind(CCTKH36)
  INTEGER*4 CCTKH38
  integer, parameter :: cctki_use_CCTKH38 = kind(CCTKH38)
  INTEGER*4 CCTKH40
  integer, parameter :: cctki_use_CCTKH40 = kind(CCTKH40)
  INTEGER*4 CCTKH42
  integer, parameter :: cctki_use_CCTKH42 = kind(CCTKH42)
  INTEGER*4 CCTKH44
  integer, parameter :: cctki_use_CCTKH44 = kind(CCTKH44)
  INTEGER*4 CCTKH46
  integer, parameter :: cctki_use_CCTKH46 = kind(CCTKH46)
  INTEGER*4 CCTKH48
  integer, parameter :: cctki_use_CCTKH48 = kind(CCTKH48)
  INTEGER*4 CCTKH50
  integer, parameter :: cctki_use_CCTKH50 = kind(CCTKH50)
  INTEGER*4 CCTKH52
  integer, parameter :: cctki_use_CCTKH52 = kind(CCTKH52)
  INTEGER*4 CCTKH55
  integer, parameter :: cctki_use_CCTKH55 = kind(CCTKH55)
  INTEGER*4 CCTKH56
  integer, parameter :: cctki_use_CCTKH56 = kind(CCTKH56)
  COMMON /IOrest/CCTKH5, CCTKH19, CCTKH29, CCTKH31, CCTKH33, CCTKH35, CCTKH37, CCTKH39, CCTKH41, CCTKH43, CCTKH45, CCTKH2, CCTKH3, &
  &CCTKH6, CCTKH9, CCTKH10, CCTKH11, CCTKH15, out_dir, CCTKH21, CCTKH22, CCTKH23, CCTKH25, CCTKH47, CCTKH49, CCTKH51, CCTKH53, CCTK&
  &H54, CCTKH57, CCTKH0, CCTKH1, CCTKH4, CCTKH7, CCTKH8, CCTKH12, CCTKH13, CCTKH14, CCTKH16, CCTKH17, CCTKH18, CCTKH20, CCTKH24, CC&
  &TKH26, CCTKH27, CCTKH28, CCTKH30, CCTKH32, CCTKH34, CCTKH36, CCTKH38, CCTKH40, CCTKH42, CCTKH44, CCTKH46, CCTKH48, CCTKH50, CCTK&
  &H52, CCTKH55, CCTKH56
  REAL*8 CCTKH59
  integer, parameter :: cctki_use_CCTKH59 = kind(CCTKH59)
  REAL*8 CCTKH60
  integer, parameter :: cctki_use_CCTKH60 = kind(CCTKH60)
  INTEGER*4 N_ang_ev_outside_eq
  integer, parameter :: cctki_use_N_ang_ev_outside_eq = kind(N_ang_ev_outside_eq)
  INTEGER*4 N_ang_ghost_pts
  integer, parameter :: cctki_use_N_ang_ghost_pts = kind(N_ang_ghost_pts)
  INTEGER*4 N_ang_pts_inside_eq
  integer, parameter :: cctki_use_N_ang_pts_inside_eq = kind(N_ang_pts_inside_eq)
  INTEGER*4 N_ang_stencil_size
  integer, parameter :: cctki_use_N_ang_stencil_size = kind(N_ang_stencil_size)
  INTEGER*4 CCTKH58
  integer, parameter :: cctki_use_CCTKH58 = kind(CCTKH58)
  INTEGER*4 CCTKH61
  integer, parameter :: cctki_use_CCTKH61 = kind(CCTKH61)
  COMMON /NULLGRIDrest/CCTKH59, CCTKH60, N_ang_ev_outside_eq, N_ang_ghost_pts, N_ang_pts_inside_eq, N_ang_stencil_size, CCTKH58, CC&
  &TKH61

  call CCTK_FortranString(nchar, out_dir, DirName)
  DirName = trim(DirName)

  do l = Lmin, Lmax
     do m = -l, l

       write(lName, '(i2.2)') l
       write(mName, '(i2.2)') abs(m)
       if(m.ge.0) then
         mName='p'//mName(1:2)
       else
         mName='m'//mName(1:2)
       end if

       flen = nchar+1+len(FieldName) +2+2+1+3+4

       FileName = DirName(1:nchar) //'/' //FieldName&
           //'.L'//lName(1:2)//'M'//mName(1:3) // '.asc'

       inquire(FILE=FileName(1:flen), EXIST=file_exists) 
       if (TruncateFile .or. .not. file_exists) then
         open(10, FILE=FileName(1:flen), STATUS='REPLACE')
       else 
         open(10, FILE=FileName(1:flen), STATUS='OLD', POSITION='APPEND')
       end if
       write(10, '(30g25.17)', advance='NO') time
       write(10, '(30g25.17, 30g25.17)', advance='NO') dble (YlmCoef(l, m)), dimag(YlmCoef(l, m))
       write(10,*)
       close(10)
            
     end do
  end do

end subroutine NullDecomp_WriteCoefFile


subroutine NullDecomp_WriteCoefRadiusFile(FieldName, TruncateFile, Lmin, Lmax, YlmCoef, time, radius)
  character(*), intent(in) :: FieldName
  logical,      intent(in) :: TruncateFile 
  INTEGER*4,     intent(in) :: Lmin, Lmax
  REAL*8,    intent(in) :: time, radius
  COMPLEX*16, dimension(Lmin:Lmax, -Lmax:Lmax) :: YlmCoef

  character(1000) DirName, FileName, lName, mName

  integer :: flen, nchar, l, m
  logical :: file_exists

  INTEGER*4 l_max
  integer, parameter :: cctki_use_l_max = kind(l_max)
  COMMON /NullDecomprest/l_max
  INTEGER*4 store_ylms
  integer, parameter :: cctki_use_store_ylms = kind(store_ylms)
  INTEGER*4 use_rsYlm
  integer, parameter :: cctki_use_use_rsYlm = kind(use_rsYlm)
  COMMON /NullDecomppriv/store_ylms, use_rsYlm
  REAL*8 CCTKH5
  integer, parameter :: cctki_use_CCTKH5 = kind(CCTKH5)
  REAL*8 CCTKH19
  integer, parameter :: cctki_use_CCTKH19 = kind(CCTKH19)
  REAL*8 CCTKH29
  integer, parameter :: cctki_use_CCTKH29 = kind(CCTKH29)
  REAL*8 CCTKH31
  integer, parameter :: cctki_use_CCTKH31 = kind(CCTKH31)
  REAL*8 CCTKH33
  integer, parameter :: cctki_use_CCTKH33 = kind(CCTKH33)
  REAL*8 CCTKH35
  integer, parameter :: cctki_use_CCTKH35 = kind(CCTKH35)
  REAL*8 CCTKH37
  integer, parameter :: cctki_use_CCTKH37 = kind(CCTKH37)
  REAL*8 CCTKH39
  integer, parameter :: cctki_use_CCTKH39 = kind(CCTKH39)
  REAL*8 CCTKH41
  integer, parameter :: cctki_use_CCTKH41 = kind(CCTKH41)
  REAL*8 CCTKH43
  integer, parameter :: cctki_use_CCTKH43 = kind(CCTKH43)
  REAL*8 CCTKH45
  integer, parameter :: cctki_use_CCTKH45 = kind(CCTKH45)
  integer*8 CCTKH2
  integer, parameter :: cctki_use_CCTKH2 = kind(CCTKH2)
  integer*8 CCTKH3
  integer, parameter :: cctki_use_CCTKH3 = kind(CCTKH3)
  integer*8 CCTKH6
  integer, parameter :: cctki_use_CCTKH6 = kind(CCTKH6)
  integer*8 CCTKH9
  integer, parameter :: cctki_use_CCTKH9 = kind(CCTKH9)
  integer*8 CCTKH10
  integer, parameter :: cctki_use_CCTKH10 = kind(CCTKH10)
  integer*8 CCTKH11
  integer, parameter :: cctki_use_CCTKH11 = kind(CCTKH11)
  integer*8 CCTKH15
  integer, parameter :: cctki_use_CCTKH15 = kind(CCTKH15)
  integer*8 out_dir
  integer, parameter :: cctki_use_out_dir = kind(out_dir)
  integer*8 CCTKH21
  integer, parameter :: cctki_use_CCTKH21 = kind(CCTKH21)
  integer*8 CCTKH22
  integer, parameter :: cctki_use_CCTKH22 = kind(CCTKH22)
  integer*8 CCTKH23
  integer, parameter :: cctki_use_CCTKH23 = kind(CCTKH23)
  integer*8 CCTKH25
  integer, parameter :: cctki_use_CCTKH25 = kind(CCTKH25)
  integer*8 CCTKH47
  integer, parameter :: cctki_use_CCTKH47 = kind(CCTKH47)
  integer*8 CCTKH49
  integer, parameter :: cctki_use_CCTKH49 = kind(CCTKH49)
  integer*8 CCTKH51
  integer, parameter :: cctki_use_CCTKH51 = kind(CCTKH51)
  integer*8 CCTKH53
  integer, parameter :: cctki_use_CCTKH53 = kind(CCTKH53)
  integer*8 CCTKH54
  integer, parameter :: cctki_use_CCTKH54 = kind(CCTKH54)
  integer*8 CCTKH57
  integer, parameter :: cctki_use_CCTKH57 = kind(CCTKH57)
  INTEGER*4 CCTKH0
  integer, parameter :: cctki_use_CCTKH0 = kind(CCTKH0)
  INTEGER*4 CCTKH1
  integer, parameter :: cctki_use_CCTKH1 = kind(CCTKH1)
  INTEGER*4 CCTKH4
  integer, parameter :: cctki_use_CCTKH4 = kind(CCTKH4)
  INTEGER*4 CCTKH7
  integer, parameter :: cctki_use_CCTKH7 = kind(CCTKH7)
  INTEGER*4 CCTKH8
  integer, parameter :: cctki_use_CCTKH8 = kind(CCTKH8)
  INTEGER*4 CCTKH12
  integer, parameter :: cctki_use_CCTKH12 = kind(CCTKH12)
  INTEGER*4 CCTKH13
  integer, parameter :: cctki_use_CCTKH13 = kind(CCTKH13)
  INTEGER*4 CCTKH14
  integer, parameter :: cctki_use_CCTKH14 = kind(CCTKH14)
  INTEGER*4 CCTKH16
  integer, parameter :: cctki_use_CCTKH16 = kind(CCTKH16)
  INTEGER*4 CCTKH17
  integer, parameter :: cctki_use_CCTKH17 = kind(CCTKH17)
  INTEGER*4 CCTKH18
  integer, parameter :: cctki_use_CCTKH18 = kind(CCTKH18)
  INTEGER*4 CCTKH20
  integer, parameter :: cctki_use_CCTKH20 = kind(CCTKH20)
  INTEGER*4 CCTKH24
  integer, parameter :: cctki_use_CCTKH24 = kind(CCTKH24)
  INTEGER*4 CCTKH26
  integer, parameter :: cctki_use_CCTKH26 = kind(CCTKH26)
  INTEGER*4 CCTKH27
  integer, parameter :: cctki_use_CCTKH27 = kind(CCTKH27)
  INTEGER*4 CCTKH28
  integer, parameter :: cctki_use_CCTKH28 = kind(CCTKH28)
  INTEGER*4 CCTKH30
  integer, parameter :: cctki_use_CCTKH30 = kind(CCTKH30)
  INTEGER*4 CCTKH32
  integer, parameter :: cctki_use_CCTKH32 = kind(CCTKH32)
  INTEGER*4 CCTKH34
  integer, parameter :: cctki_use_CCTKH34 = kind(CCTKH34)
  INTEGER*4 CCTKH36
  integer, parameter :: cctki_use_CCTKH36 = kind(CCTKH36)
  INTEGER*4 CCTKH38
  integer, parameter :: cctki_use_CCTKH38 = kind(CCTKH38)
  INTEGER*4 CCTKH40
  integer, parameter :: cctki_use_CCTKH40 = kind(CCTKH40)
  INTEGER*4 CCTKH42
  integer, parameter :: cctki_use_CCTKH42 = kind(CCTKH42)
  INTEGER*4 CCTKH44
  integer, parameter :: cctki_use_CCTKH44 = kind(CCTKH44)
  INTEGER*4 CCTKH46
  integer, parameter :: cctki_use_CCTKH46 = kind(CCTKH46)
  INTEGER*4 CCTKH48
  integer, parameter :: cctki_use_CCTKH48 = kind(CCTKH48)
  INTEGER*4 CCTKH50
  integer, parameter :: cctki_use_CCTKH50 = kind(CCTKH50)
  INTEGER*4 CCTKH52
  integer, parameter :: cctki_use_CCTKH52 = kind(CCTKH52)
  INTEGER*4 CCTKH55
  integer, parameter :: cctki_use_CCTKH55 = kind(CCTKH55)
  INTEGER*4 CCTKH56
  integer, parameter :: cctki_use_CCTKH56 = kind(CCTKH56)
  COMMON /IOrest/CCTKH5, CCTKH19, CCTKH29, CCTKH31, CCTKH33, CCTKH35, CCTKH37, CCTKH39, CCTKH41, CCTKH43, CCTKH45, CCTKH2, CCTKH3, &
  &CCTKH6, CCTKH9, CCTKH10, CCTKH11, CCTKH15, out_dir, CCTKH21, CCTKH22, CCTKH23, CCTKH25, CCTKH47, CCTKH49, CCTKH51, CCTKH53, CCTK&
  &H54, CCTKH57, CCTKH0, CCTKH1, CCTKH4, CCTKH7, CCTKH8, CCTKH12, CCTKH13, CCTKH14, CCTKH16, CCTKH17, CCTKH18, CCTKH20, CCTKH24, CC&
  &TKH26, CCTKH27, CCTKH28, CCTKH30, CCTKH32, CCTKH34, CCTKH36, CCTKH38, CCTKH40, CCTKH42, CCTKH44, CCTKH46, CCTKH48, CCTKH50, CCTK&
  &H52, CCTKH55, CCTKH56
  REAL*8 CCTKH59
  integer, parameter :: cctki_use_CCTKH59 = kind(CCTKH59)
  REAL*8 CCTKH60
  integer, parameter :: cctki_use_CCTKH60 = kind(CCTKH60)
  INTEGER*4 N_ang_ev_outside_eq
  integer, parameter :: cctki_use_N_ang_ev_outside_eq = kind(N_ang_ev_outside_eq)
  INTEGER*4 N_ang_ghost_pts
  integer, parameter :: cctki_use_N_ang_ghost_pts = kind(N_ang_ghost_pts)
  INTEGER*4 N_ang_pts_inside_eq
  integer, parameter :: cctki_use_N_ang_pts_inside_eq = kind(N_ang_pts_inside_eq)
  INTEGER*4 N_ang_stencil_size
  integer, parameter :: cctki_use_N_ang_stencil_size = kind(N_ang_stencil_size)
  INTEGER*4 CCTKH58
  integer, parameter :: cctki_use_CCTKH58 = kind(CCTKH58)
  INTEGER*4 CCTKH61
  integer, parameter :: cctki_use_CCTKH61 = kind(CCTKH61)
  COMMON /NULLGRIDrest/CCTKH59, CCTKH60, N_ang_ev_outside_eq, N_ang_ghost_pts, N_ang_pts_inside_eq, N_ang_stencil_size, CCTKH58, CC&
  &TKH61

  call CCTK_FortranString(nchar, out_dir, DirName)
  DirName = trim(DirName)

  do l = Lmin, Lmax
     do m = -l, l

       write(lName, '(i2.2)') l
       write(mName, '(i2.2)') abs(m)
       if(m.ge.0) then
         mName='p'//mName(1:2)
       else
         mName='m'//mName(1:2)
       end if

       flen = nchar+1+len(FieldName) +2+2+1+3+4

       FileName = DirName(1:nchar) //'/' //FieldName&
           //'.L'//lName(1:2)//'M'//mName(1:3) // '.asc'
 
       inquire(FILE=FileName(1:flen), EXIST=file_exists) 
       if (TruncateFile .or. .not. file_exists) then
         open(10, FILE=FileName(1:flen), STATUS='REPLACE')
       else 
         open(10, FILE=FileName(1:flen), STATUS='OLD', POSITION='APPEND')
       end if
       write(10, '(30g25.17)', advance='NO') time
       write(10, '(30g25.17)', advance='NO') radius
       write(10, '(30g25.17, 30g25.17)', advance='NO') dble (YlmCoef(l, m)), dimag(YlmCoef(l, m))
       write(10,*)
       close(10)
            
     end do
  end do

end subroutine NullDecomp_WriteCoefRadiusFile


end module
