    program cluster_match

      ! Finds the nearest match between catalogues in cluster format.

      use define_star
      use cluster_match_subs
      use radec2rad_mod

      implicit none

      character(len=50) :: ifname

      integer :: nstars1, nstars2, ncol1, ncol2
      character(len=3), dimension(mcol) :: colstr1, colstr2
      type(a_star), dimension(:), allocatable :: star1, star2
      real, dimension(:), allocatable :: alpha, delta
      real :: alpha2, delta2

      integer :: all
      integer :: i, iostat, istar
      real :: fixrad, dec_off, ra_off, secs_in_rad
      integer, dimension(:), allocatable :: matches
      integer :: n_matches, imatch
      
      secs_in_rad=360.0*60.0*60.0/(8.0*atan(1.0))
      
      print*, 'You can either give the program a fixed matching radius,'
      print*, 'or (if you type -1) use the magnitude column as a'
      print*, 'variable error box.'
      print*, ' '
      print*, '> Give the radius to match in arcseconds.'
      read(*,*) fixrad
      print*, &
      '> Give the offsets to be applied to primary catalogue in RA and dec in arcsec.'
      read(*,*) ra_off, dec_off

      print*, 'Do you want the output file (matches.cat) to '
      print*, '   1) contain all the stars within the error box or'
      print*, '   2) just the brightest star unflagged in colour 1 in the error box, or'
      print*, '   3) just the brighest star (flagged or not)?'
      read(*,*) all
      if (all<1 .or. all>3) then
        print*, 'Wally.'
        stop
      end if

      print*, '> Give primary catalogue.'
      do
        read(*,'(a)') ifname
        open(1,file=ifname,status='old',iostat=iostat)
        if (iostat == 0) exit
        print*, '> File not found, please try again.'
      end do
      call read_cluster_file(1, nstars1, ncol1, colstr1, star1) 
      close(1)

      ! Make the ra and dec arrays.
      allocate(alpha(nstars1), delta(nstars1))
      do i=1, nstars1
        call radec2rad(star1(i)%ra_h, star1(i)%ra_m, star1(i)%ra_s, &
        star1(i)%dc_d, star1(i)%dc_m, star1(i)%dc_s, alpha(i), delta(i))
      end do
      ! Apply the offset.
      alpha=alpha+ ra_off/(cos(delta)*secs_in_rad)
      delta=delta+dec_off/(           secs_in_rad)

      print*, '> Give the file of error boxes to be searched.'
      do
        read(*,'(a)') ifname
        open(unit=1, file=ifname, status='old', iostat=iostat)
        if (iostat == 0) exit
        print*, '> File not found, please try again.'
      end do
      call read_cluster_file(1, nstars2, ncol2, colstr2, star2) 
      close(1)
      allocate(matches(nstars1))

      open(unit=23, file='pairings.cat', status='unknown')
      write(23,*) 'This file contains blocks which have the record from the'
      if (all==1) then 
        write(23,*) 'second file, followed by all the records from the first'
      else if (all == 2) then
        write(23,*) 'second file, followed by the brightest star unflagged in colour 1 from the first'
      else if (all == 3) then
        write(23,*) 'second file, followed by the brightest star in colour 1 from the first'
      end if
      write(23,*) 'file within the correlation radius.'
      open(unit=24, file='no_matches.cat', status='unknown')
      write(24,*) ncol2, ' colours were created.'
      write(24,24) (colstr2(i),i=1,ncol1)
      write(24,*)
      open(unit=25, file='matches1.cat', status='unknown')
      write(25,*) ncol1, ' colours were created.'
      write(25,24) (colstr1(i),i=1,ncol1)
      write(25,*)
      open(unit=26, file='matches2.cat', status='unknown')
      write(26,*) ncol2, ' colours were created.'
      write(26,24) (colstr2(i),i=1,ncol2)
      write(26,*)
 24   format(24(' ',a))
      open(unit=27, file='diffs.pos', status='unknown')
      write(27,'(/,/)')

      do istar=1, nstars2

        call match_them(nstars1, star1, alpha, delta, star2(istar), &
        fixrad, matches, n_matches)
        
        if (all == 3) then
          ! match_them puts the brightest unflagged star first, but we
          ! want the bightest irrespective of flags.
          do i=2, n_matches
            if (star1(matches(i))%col(1)%data < star1(matches(1))%col(1)%data) &
            matches(1)=matches(i)
          end do
        end if

        if (n_matches == 0) then
          ! Not found any match.
          call write_star(24, star2(istar), ncol2)
        else
          ! Write to pairings.cat
          write(23,*) ' '
          call write_star(23, star2(istar), ncol2)
          call radec2rad(star2(istar)%ra_h, star2(istar)%ra_m, &
          star2(istar)%ra_s, star2(istar)%dc_d, star2(istar)%dc_m, &
          star2(istar)%dc_s, alpha2, delta2)
          if (all /= 1) n_matches=1
          do imatch=1, n_matches
            call write_star(23, star1(matches(imatch)), ncol1)
            call write_star(25, star1(matches(imatch)), ncol1)
            call write_star(26, star2(istar), ncol2)
            write(27,*) &
            (alpha(matches(imatch))-alpha2)*cos(delta2)*secs_in_rad, &
            (delta(matches(imatch))-delta2)*secs_in_rad
          end do
        end if

      end do

      deallocate(star1, alpha, delta, matches)

      close(23)
      close(24)
      close(25)
      close(26)
      close(27)
            
    end program cluster_match


