C     program mash
      program mash
C                     combines a list of IDS-FITS-header (with 
C                     namechange.dat applied) names to the standard
C                     (Worthey et al. 1994) names, dragging data along

      character*10    hdnames(500)
      character*32    onames(500)
      character*16    fitsnames(500),temp(500)
      integer         data(500,200),ndata(500)
      character*42    blank
      character*44    link(200)
      character*13    field1
      integer         gotem(500)

      data blank / '   0.0000  0   0.0000  0   0.0000   0.0000' /


C     get modified fits header names & data from means output file
      open(unit=1,file='database.out',status='old')
      do i=1,500
         read(1,'(a16,200i7)',end=50) fitsnames(i),(data(i,k),k=1,200)
         do j=1,200
            if (data(i,j).eq.0) then
               ndata(i) = j-1
               goto 49
            end if
         end do
 49   end do
 50   ncount = i-1
      do i=1,ncount
         gotem(i) = -1
      end do
      close(1)

C     read name cross-references
      open(unit=2,file='nametemplate',status='old')
      read(2,'(x)')
      do i=1,500
         read(2,'(a10,a32,a16)',end=60) hdnames(i),onames(i),temp(i)
      end do
 60   close(2)
      ntemp = i - 1

C     having read all the necessary data, let us sort and output
      open(unit=3,file='mash.out',status='unknown')
      open(unit=33,file='html.out',status='unknown')

C     loop over template names
      do i=1,ntemp
C        for each template name, see if there is a match
         im = -1
         do j=1,ncount
            if ( fitsnames(j).eq.temp(i)) then
               if (im.eq.-1) then
                  im = j
               else
                  print*, 'Whoa ! Duplicate names! ',fitsnames(j)
               end if
            end if
         end do
         if (hdnames(i).eq.'          ') then
            field1 = '             '
         else
            write(field1,'(a3,a10)') 'HD ',hdnames(i)
         end if
         print*, field1
         if (im.eq.-1) then
            write(3,'(a13,a32,a42)') field1,onames(i),blank
            write(33,'(a5,a13,a32)') '<dt> ',field1,onames(i)
            write(33,'(a)') '<dd> Scan not available'
         else
            write(3,'(a13,a32,200i7)') 
     z               field1,onames(i),
     z               (data(im,k),k=1,ndata(im))
            write(33,'(a5,a13,a32)') '<dt> ',field1,onames(i)
C           write links
            do k=1,ndata(im)
               iid = data(im,k)
               write(link(k),'(a21,i6.6,a6,i6.6,a5)') 
     z              '<a href="sdrlinks/sdr',iid,'.bin">',iid,'</a> '
            end do
            write(33,'(a,200a44)') '<dd> ',(link(k),k=1,ndata(im))
            gotem(im) = 1
         end if
      end do

      close(3)

C     catch names in means output file which did not match up with any
C     names in the cross-reference file
      do i=1,ncount
         if (gotem(i).eq.-1) then
            print*, 'unmatched name in means95.out : ',fitsnames(i)
         end if
      end do

      stop
      end
