program tess
  implicit none

  integer, parameter :: dp=selected_real_kind(15,300)
  real(kind=dp), allocatable, dimension(:,:) :: conf,tconf,lconf
  real(kind=dp), dimension(1:3,1:3) :: cell
  real(kind=dp) :: a,b,c,sf
  integer :: N,i,nl
  character(2) :: Ne,H
  character(20) :: infile1,infile2,file1,file2,scale_factor
  character(255) :: gumph
  external getarg

  Ne = 'Ne'
  H = 'H '

  sf = 0.0_dp

  call getarg(1,infile1)
  call getarg(2,infile2)
  call getarg(3,scale_factor)

  read(scale_factor,*) sf

  file1 = trim(infile1)
  file2 = trim(infile2)

  open(unit=99,file=file1)

  read(99,*) N
  read(99,*) gumph

  allocate(conf(1:3,1:N),tconf(1:3,1:27*N),lconf(1:3,1:27*N))

  conf = 0.0_dp
  tconf = 0.0_dp
  lconf = 0.0_dp

  do i=1,N
     read(99,*) gumph,conf(:,i)
  end do

  close(99)

  open(unit=98,file=file2)

  do i=1,3
     read(98,*) cell(:,i)
  end do

  close(98)

  a = sqrt(cell(1,1)**2 + cell(1,2)**2 + cell(1,3)**2)
  b = sqrt(cell(2,1)**2 + cell(2,2)**2 + cell(2,3)**2)
  c = sqrt(cell(3,1)**2 + cell(3,2)**2 + cell(3,3)**2)


  do i=1,N
     tconf(:,i) = conf(:,i)
  end do

  do i=N+1,2*N
     tconf(:,i) = conf(:,i-N) + cell(:,1)
  end do
  do i=2*N+1,3*N
     tconf(:,i) = conf(:,i-2*N) + cell(:,2)
  end do
  do i=3*N+1,4*N
     tconf(:,i) = conf(:,i-3*N) + cell(:,3)
  end do

  do i=4*N+1,5*N
     tconf(:,i) = conf(:,i-4*N) - cell(:,1)
  end do
  do i=5*N+1,6*N
     tconf(:,i) = conf(:,i-5*N) - cell(:,2)
  end do
  do i=6*N+1,7*N
     tconf(:,i) = conf(:,i-6*N) - cell(:,3)
  end do

  do i=7*N+1,8*N
     tconf(:,i) = conf(:,i-7*N) + cell(:,1) + cell(:,2)
  end do
  do i=8*N+1,9*N
     tconf(:,i) = conf(:,i-8*N) + cell(:,1) - cell(:,2)
  end do
  do i=9*N+1,10*N
     tconf(:,i) = conf(:,i-9*N) - cell(:,1) + cell(:,2)
  end do
  do i=10*N+1,11*N
     tconf(:,i) = conf(:,i-10*N) - cell(:,1) - cell(:,2)
  end do

  do i=11*N+1,12*N
     tconf(:,i) = conf(:,i-11*N) + cell(:,3) + cell(:,1)
  end do
  do i=12*N+1,13*N
     tconf(:,i) = conf(:,i-12*N) + cell(:,3) - cell(:,1)
  end do
  do i=13*N+1,14*N
     tconf(:,i) = conf(:,i-13*N) + cell(:,3) + cell(:,2)
  end do
  do i=14*N+1,15*N
     tconf(:,i) = conf(:,i-14*N) + cell(:,3) - cell(:,2)
  end do

  do i=15*N+1,16*N
     tconf(:,i) = conf(:,i-15*N) - cell(:,3) + cell(:,1)
  end do
  do i=16*N+1,17*N
     tconf(:,i) = conf(:,i-16*N) - cell(:,3) - cell(:,1)
  end do
  do i=17*N+1,18*N
     tconf(:,i) = conf(:,i-17*N) - cell(:,3) + cell(:,2)
  end do
  do i=18*N+1,19*N
     tconf(:,i) = conf(:,i-18*N) - cell(:,3) - cell(:,2)
  end do

  do i=19*N+1,20*N
     tconf(:,i) = conf(:,i-19*N) + cell(:,3) + cell(:,1) + cell(:,2)
  end do
  do i=20*N+1,21*N
     tconf(:,i) = conf(:,i-20*N) + cell(:,3) + cell(:,1) - cell(:,2)
  end do
  do i=21*N+1,22*N
     tconf(:,i) = conf(:,i-21*N) + cell(:,3) - cell(:,1) + cell(:,2)
  end do
  do i=22*N+1,23*N
     tconf(:,i) = conf(:,i-22*N) + cell(:,3) - cell(:,1) - cell(:,2)
  end do

  do i=23*N+1,24*N
     tconf(:,i) = conf(:,i-23*N) - cell(:,3) + cell(:,1) + cell(:,2)
  end do
  do i=24*N+1,25*N
     tconf(:,i) = conf(:,i-24*N) - cell(:,3) + cell(:,1) - cell(:,2)
  end do
  do i=25*N+1,26*N
     tconf(:,i) = conf(:,i-25*N) - cell(:,3) - cell(:,1) + cell(:,2)
  end do
  do i=26*N+1,27*N
     tconf(:,i) = conf(:,i-26*N) - cell(:,3) - cell(:,1) - cell(:,2)
  end do

  open(unit=97,file="tess.xyz")

101 format(I6)
102 format(A)
103 format(A2,3F18.6)

  write(97,101) 27*N
  write(97,102) "* TESSILATED ATOMS"

  do i=1,N
     write(97,103) Ne,tconf(:,i)
  end do

  do i=N+1,27*N
     write(97,103) H,tconf(:,i)
  end do

  close(97)

  nl = 0

  do i=N+1,27*N
     if (&
          &(((tconf(1,i) > -sf*a) .and. (tconf(1,i) <= (1.0_dp+sf)*a))  .and. &
          & ((tconf(2,i) > -sf*b) .and. (tconf(2,i) <= (1.0_dp+sf)*b))) .and. &
          & ((tconf(3,i) > -sf*c) .and. (tconf(3,i) <= (1.0_dp+sf)*c))        &
          & ) then
        nl = nl+1
        lconf(:,nl) = tconf(:,i)
     end if
  end do

  open(unit=96,file="layer.xyz")

  write(96,101) nl + N
  write(96,102) "* LAYER ATOMS"

  do i=1,N
     write(96,103) Ne,conf(:,i)
  end do

  do i=1,nl
     write(96,103) H,lconf(:,i)
  end do

  close(96)

  deallocate(conf,tconf,lconf)

end program tess