program combine_files
implicit none

integer :: nfiles, startfile
integer :: i, idx, ios
real, dimension(:), allocatable :: val
integer, dimension(:), allocatable :: unit
character(len=20) :: fname
character(len=20) :: outfile

outfile = "combined.dat"

nfiles= no_of_h
startfile= start_file

allocate(val(nfiles), unit(nfiles))

! Open input files
do i = 1, nfiles
    write(fname,'("shift-",I0,".dat")') startfile+i-1
    unit(i) = 10 + i
    open(unit=unit(i), file=fname, status='old')
end do

! Open output file
open(unit=50, file=outfile, status='replace')

do
    read(unit(1),*,iostat=ios) idx, val(1)
    if (ios /= 0) then
        exit
    end if

    do i = 2, nfiles
        read(unit(i),*) idx, val(i)
    end do

    write(50,*) idx, (val(i), i=1,nfiles)
end do

close(50)

do i = 1, nfiles
    close(unit(i))
end do

end program combine_files
