        subroutine find2mc(drarad,ddecrad,size,sizey,
     >datayes,cornerx,cornery,datadist,datafile,
     > dataplate,dataplatesr,datahk,datafield,dataedge,slop)
        implicit none
        real rarad,decrad,size,sizey,rvalra,rvaldec
        real*8 raf,decf,dist,distx,disty,dra,ddec,mindist,c,xpl,ypl,
     >sla_dsep,bepoch,r1950,d1950,dr1950,dd1950,radiff,xi,eta,
     >       pldist(10),datadist,datax,datay,lowdist,
     >plx(10),ply(10),platelim,pi,adddist,sortdist(10),slop,
     >ddecrad,drarad,platesize,cornerx,cornery
        integer proc,equinox,lstring,jf,nsrt,field(10),fieldstore,i
     >,lengthno,run,DAT__SZLOC,templength,length,sLength,flag,
     >possfield,nofields,irank(10),ifail,order,exist(10),ios,j,status,
     > pnum,gif,stemlength,hklength,fieldlength,scan(10),jj,
     > datayes,datafield,dataedge
       character*10 iplate,dataplate,dataplatesr,plate(10)
        character*75 inname,fitsname,errname
        character string*40,mmname*50,fieldfile(10)*75,numstr*20
        character nostring*30,wave*1,stem*75,hkfile*75,field_cen*75,
     > hkfilesr*75,answer*32
       character hkloc*16,tempfile*75,datafile*75,datahk*75 
       character*64 surDirN,surDirS,dataDir
      PARAMETER ( DAT__SZLOC = 16 ) 
       call getenv('SSS_DATA_DIR',dataDir)
        call getenv('SURVEY_DIR_MC',surDirS)
          datax=999.0d0
          datay=999.0d0
          datafile=' '
          datahk=' '
          datadist=999.0d0
          dataplate='-999'
          datafield=-999
c         read (50,*) drarad
c         read (50,*) ddecrad
c         read (50,*) size
c         read (50,'(a)') wave
c         read (50,*) equinox         

          sLength=length(surDirS)
  
          stem=surDirS(1:sLength)//'/Halpha/'   

        stemlength=length(stem)




        field_cen=dataDir(1:length(dataDir))//'/mc_fields.lis'

ccc        write (6,*) 'aaa ', field_cen, stem
ccc        write (6,*) 'aaa ', field_cen, surDirS
        c=3072.16

        platesize=177.8

        platelim=138.24



        pi=3.141592654d0
        tempfile=' '
        hkfile=' '
        rarad=drarad
        decrad=ddecrad


            platelim=platelim-slop        
         open (33,file=field_cen, status='old')
         nofields=0
         datayes=0
         dataedge=0
         lowdist=10000.

         do i=1,2000
         adddist=0.
           read (33,*,end=99)possfield,raf,decf
cccc           write (6,*)possfield,raf,decf, dRArad, dDECrad
           CALL sla_DS2TP (RAf, DECf, dRArad, dDECrad, XI, ETA, Jj)
           xpl=xi*c
           ypl=eta*c
           dist=sqrt(xpl*xpl+ypl*ypl)

ccc     add 30 mm in case nominal field centre is out

       if (abs(xpl).lt.(platelim+30.) .and. 
     >abs(ypl) .lt. (platelim+30.) .and. jj.eq.0) then


           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+5)='HALMC'
           write(tempfile(stemlength+6:stemlength+7),72)possfield
           write (6,*) tempfile
 72        format(i2.2)

           templength=length(tempfile)
c           write (6,*) templength
           hkfile(1:templength)=tempfile(1:templength)
           hkfile(templength+1:templength+11)='/hkfile.lis'
           hkfilesr=hkfile
           hkfilesr(stemlength+1:stemlength+3)='HAR'
ccc           write (6,*) 'aaa ', hkfile
ccc           write (6,*) 'aaa ', hkfilesr
           open (55,file=hkfile
     >,status='old',iostat=ios,err=56)
 56      continue
        


        if (ios.eq.0) then
cc       call CIO_OPEN_HK (hkfile,'read',hkloc,status) 
cc       call CIO_RDIT0R (hkloc,'PLATE','RA_PNT',rvalra,status)
cc       call CIO_RDIT0R (hkloc,'PLATE','DEC_PNT',rvaldec,status)
cc       call CIO_RDIT0C (hkloc,'PLATE','PLATE',iplate,status)
cc       call CIO_CLOSE_HK (HKLOC,status) 
        call hkread(55,'PLATE.RA_PNT',answer,flag)
        read(answer,*)rvalra
        call hkread(55,'PLATE.DEC_PNT',answer,flag)
        read(answer,*)rvaldec
        call hkread(55,'PLATE.PLAT',answer,flag)
        read(answer,*)iplate
        close(55)   


       raf=rvalra*15.0d0*pi/180.0d0
       decf=rvaldec*pi/180.0d0

       CALL sla_DS2TP (RAf, DECf, dRArad, dDECrad, XI, ETA, Jj) 
           xpl=xi*c
           ypl=eta*c
           dist=sqrt(xpl*xpl+ypl*ypl)
cccc       write (666,*)possfield,xpl,ypl,platelim
       if (abs(xpl).lt. platelim .and. 
     >abs(ypl) .lt. platelim .and. jj.eq.0) then

      nofields=nofields+1
      scan(nofields)=1
      field(nofields)=possfield
      plx(nofields)=xpl
      ply(nofields)=ypl
      pldist(nofields)=dist
      fieldfile(nofields)=tempfile
      plate(nofields)=iplate
         if (dist.lt.lowdist) then

        ios=0
          open (55,file=hkfilesr
     >,status='old',iostat=ios,err=57)
 57       continue
        
        if (ios.eq.0) then
ccc       call CIO_OPEN_HK (hkfilesr,'read',hkloc,status) 
ccc      call CIO_RDIT0C (hkloc,'PLATE','PLATE',dataplatesr,status)
ccc       call CIO_CLOSE_HK (HKLOC,status) 

        call hkread(55,'PLATE.PLAT',answer,flag)
        read(answer,*)dataplatesr
        close(55)   

       end if

         datayes=1
         datafield=possfield
         datax=xpl
         datay=ypl
         datadist=dist
         lowdist=datadist
         datafile=tempfile
         datahk=hkfile
         dataplate=iplate
        cornerx=platesize+xpl
        cornery=platesize-ypl   
        if (sqrt((abs(xpl)+size/2.)**2.+(abs(ypl)+sizey/2.)**2.) 
     > .gt. 153. )  then 
        dataedge=1
        else
        dataedge=0
        end if
         end if
         end if         
      end if
        end if
   

         end do
         
         
         
         
         
         
         
         
         
         
 99      continue  


        
  71    continue

        close(33)

         

        return

        end

      subroutine hkread(unit,param,answer,flag)
      implicit none
      character param*(*),answer*(*),line*80
      integer plen,alen,unit,length,i,flag

      flag=-1
      plen=length(param)
      alen=len(answer)
      rewind(unit)

10    read (unit,'(a)',end=20) line
      if (line(8:8+plen-1).eq.param(1:plen)) then
        flag=0                                     
        answer(1:alen)=line(33:33+alen-1)
        i=length(answer)
        answer(i:i)=' '
        goto 20
      endif
      goto 10
20    continue
      return
      end

      function length(string)
      implicit none
      character string*(*)
      integer i,length,j
      length=0
      i=1
      j=len(string)                     
      do while (string(i:i).ne.' '.and.i.lt.j)
       i=i+1
      enddo
      if (i.eq.j.and.string(i:i).ne.' ') then
       length=i
      else
       length=i-1
      endif
      return
      end                         


