cccc f77 pixel_mc.f -o pixel_mc.e find2mc.f -L/star/lib `sla_link` --no-second-underscore ~nch/recio/librecio.a /star/lib/libcnf.a /star/lib/libstarmem.a

        implicit none
        real rarad,decrad,size,sizey
        real*8 raf,decf,dist,distx,disty,dra,ddec,mindist,c,xpl,ypl,
     >sla_dsep,bepoch,r1950,d1950,dr1950,dd1950,radiff,xi,eta,pldist(4),
     >plx(4),ply(4),platelim,pi,adddist,sortdist(4),slop,reslt,dlat,dlon
        integer proc,equinox,lstring,jf,nsrt,field,fieldstore,i,
     >possfield(4),nofields,irank(4),ifail,order,exist(4),iosp(5),j,
     >status,ios,pnum,gif,jj,datafield,dataedge,outunits,flat
        character*75 srinname,hainname,fitsname,errname
        character string*40,mmname*50,fieldfile*60,numstr*20
        character nostring*30,stem*23,hkfile*70
        real*8 datadist,datax,datay,lowdist,drarad,ddecrad
        character datafile*75,datahk*75,wave*1,signd*1,signr*1
        integer datayes,tf,fail,rihmsf(4),dihmsf(4),wavecode
        character*10 iplate,dataplate,dataplatesr

        fail=0

        read (5,*)proc
        read (5,'(a)')hainname
        read (5,'(a)')srinname
        read (5,'(a)')string
        read (5,*,err=1,iostat=iosp(1)) equinox
  1     continue
        read (5,*,err=2,iostat=iosp(2)) size,sizey
  2     continue
     
        read (5,'(a)') errname 

        read (5,*,err=3,iostat=iosp(3)) gif
        read (5,*) outunits
        read (5,*) flat

  3     continue

        open (66,file=errname,status='new')
        
        if (equinox .ne. 0 .and. equinox.ne.1 .and. equinox.ne. 2) then
        fail=99
        write (66,*)'Invalid value entered for equinox.<br>'
        write (66,*)'Please go BACK and use drop down menu.<br>'
        end if

        if (string .eq.' ' .or. string.eq.'  ' .or.
     > string .eq. '                              ') then
        fail=99
        write (66,*)'Null string entered for coordinates.<br>'
        write (66,*)'Please go BACK and try again.<br>'
        end if   

        if (size*sizey .gt. 900.) then
        fail=99
        write (66,*) 'Area requested is greater than 900 sq. arcmin. '
        write (66,*) 'Currently this is not possible.<br> '
        write (66,*) 'If you need a larger single area,' 
        write (66,*) 'please email mar@roe.ac.uk<br>'
        end if        

        if (iosp(2).eq.0 .and. size.lt. 0.1) then
        fail=99
        write (66,*) 'Size requested is less than 0.1 arcmin.<br>'
        write (66,*) 'Please go BACK and enter a larger value.<br> '
        end if
        
        if (gif.ne.0 .and. gif.ne.1) then
        fail=99
        write (66,*) 'Invalid choice entered for GIF option.<br>'
        write (66,*) 'Please go BACK and use drop-down menu.<br>'
        end if

        if (iosp(1).ne.0.or.iosp(2).ne.0.or.iosp(3).ne.0) then
        fail=99
        write (66,*) 'Invalid value(s) entered for size, equinox'
        write (66,*) ' or GIF option(s).<br>'
        write (66,*) 'Please go BACK and check.<br>'
        end if

        nsrt=1
        pi=3.141592654d0

      if (equinox.eq.0) then
       call sla_dfltin(string,nsrt,reslt,j)
       if (j.gt.0) then
        fail=99
        write (66,*) 'SLA_DAFIN error decoding Galactic longitude:'
        write (66,'(a,a)') ' Offending line: '//string
        write (66,*) 
     :'Error decoding requested longitude: execution terminated.'
       endif
       dlon=reslt
       call sla_dfltin(string,nsrt,reslt,j)
       if (j.gt.0) then
        fail=99
        write (66,*) 'SLA_DAFIN error decoding Galactic latitude:'
        write (66,'(a,a)') ' Offending line: '//string
        write (66,*) 
     :'Error decoding requested latitude: execution terminated.'
       endif
       dlat=reslt
       call sla_ge50(dlon*pi/180.d0,dlat*pi/180.d0,dra,ddec)
        else

        call sla_dafin(string,nsrt,dra,jf)
        call sla_dafin(string,nsrt,ddec,jf)
          dra=dra*15.
        call sla_dr2tf(2,dra,signr,rihmsf)
        call sla_dr2af(1,ddec,signd,dihmsf)   
       endif

        if (equinox .eq. 2) then
        call sla_FK54z(dra,ddec,1980D0,r1950,d1950,dr1950,dd1950)
        dra=r1950
        ddec=d1950
        endif
       
        if (dra.gt.2.*pi .or. dra.lt.0. .or. 
     >ddec.lt. -pi/2. .or. ddec.gt. pi/2.) then
         fail=99
          write (66,*) 
     >'Please check inputted RA (0 to 24hrs) and/or Dec (-90 to +90)'
          write (66,*) '<br>'
          end if
         if (ddec.gt. 0.0555) then
         fail=99
       write (66,*) 'The survey/waveband requested only' 
       write (66,*) 'covers -90 < Dec < +3  B1950 <br>'
          end if        
 
         if (fail.eq.99) then
         close(66)
         stop
         end if         



       slop=4.  
       call find2mc(dra,ddec,size,sizey,
     >datayes,datax,datay,datadist,datafile,
     >dataplate,dataplatesr,datahk,datafield,dataedge,slop)

        if (datayes.eq.0) then
       slop=-10.
       call find2mc(dra,ddec,size,sizey,
     >datayes,datax,datay,datadist,datafile,
     >dataplate,dataplatesr,datahk,datafield,dataedge,slop)             
       endif


        if (datayes.eq.0) then 
      write (66,*)'The plate(s) covering the requested coordinates '
      write (66,*)'in the waveband/survey selected have not yet '
      write (66,*) 'been scanned. <br> <br>'
      write (66,*) 'Charts showing the status of survey scans available'
      write (66,*) ' on-line can be found under'
      write (66,*)'the survey scanning status page.<br><br>' 

        close (66)
        stop
         end if

        open (11,status='new',file=hainname)
        write (11,493) proc
 493    format(i5)
         write (11,494) datafile
 494     format(a75)
         if (equinox .ge. 1) then
        write (11,32) rihmsf(1),rihmsf(2),rihmsf(3),'.',rihmsf(4),
     >signd,dihmsf(1),dihmsf(2),dihmsf(3),'.',dihmsf(4)
 32   format(i2.2,1x,i2.2,1x,i2.2,a1,i2.2,1x,a1,i2.2,1x,i2.2,1x,
     >i2.2,a1,i1)
 31     format (a6)
        else
        write (11,33)dlon,dlat
 33     format (f10.5,1x,f10.5)
        end if
        write (11,*) equinox
        write (11,*) size,sizey
        write (11,*) 1
        write (11,798) datafield
        write (11,*) outunits
        write (11,*) flat
 798    format(i4.4)
        write (11,*) dataplate
        write (11,800) datax
        write (11,800) datay
 800    format(f7.2)
        write (11,*) dataedge
        write (11,493) proc
        close (11)
        open (11,status='new',file=srinname)
        write (11,493) proc
           datafile(24:24)='R'
         write (11,494) datafile
         if (equinox .ge. 1) then
        write (11,32) rihmsf(1),rihmsf(2),rihmsf(3),'.',rihmsf(4),
     >signd,dihmsf(1),dihmsf(2),dihmsf(3),'.',dihmsf(4)
        else
        write (11,33)dlon,dlat
        end if
        write (11,*) equinox
        write (11,*) size,sizey
        write (11,*) 2
        write (11,798) datafield
        write (11,*) outunits
        write (11,*) flat
        write (11,*) dataplatesr
        write (11,800) datax
        write (11,800) datay
        write (11,*) dataedge
        write (11,493) proc
        close (11)
        end














