        subroutine find2(drarad,ddecrad,size,wave,
     >equinox,datayes,cornerx,cornery,datadist,datafile,
     > dataplate,datahk,datafield,dataedge,slop)
        implicit none
        real rarad,decrad,size,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,diag,
     >       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,field_limit,flag,
     >possfield,nofields,irank(10),ifail,order,exist(10),ios,j,status,
     > pnum,gif,stemlength,hklength,fieldlength,scan(10),jj,
     > datayes,datafield,dataedge,sLength,nLength
       character*10 iplate,dataplate,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
        character hkloc*16,tempfile*75,datafile*75,datahk*75,answer*32 
        PARAMETER ( DAT__SZLOC = 16 ) 
        character*64 surDirN,surDirS,dataDir
        call getenv('SURVEY_DIR_N',surDirN)
        call getenv('SURVEY_DIR_S',surDirS)
        call getenv('SSS_DATA_DIR',dataDir)
      
          field_limit=999999999
          datax=999.
          datay=999.
          datafile=' '
          datahk=' '
          datadist=999.
          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)
          nLength=length(surDirN)
           if (wave.eq.'J') stem=surDirS(1:sLength)//'/UKJsurvey/'   
           if (wave.eq.'R') stem=surDirS(1:sLength)//'/UKRsurvey/'
           if (wave.eq.'I') stem=surDirS(1:sLength)//'/UKIsurvey/'
           if (wave.eq.'E') stem=surDirS(1:sLength)//'/ESORsurvey/'
           if (wave.eq.'P') stem=surDirS(1:sLength)//'/PAEsurvey/'
           if (wave.eq.'O') stem=surDirS(1:sLength)//'/PAOsurvey/'
           if (wave.eq.'B') stem=surDirN(1:nLength)//'/PAJsurvey/'
           if (wave.eq.'F') stem=surDirN(1:nLength)//'/PARsurvey/'
           if (wave.eq.'N') stem=surDirN(1:nLength)//'/PAIsurvey/'
           if (wave.eq.'A') stem=surDirN(1:nLength)//'/PAEsurvey/'

        stemlength=length(stem)


 
 

           if (wave.eq.'J'.or.wave.eq.'R'.or. wave.eq. 'I'
     > .or. wave .eq. 'E') 
     > field_cen=dataDir(1:length(dataDir))//'/fields.lis'


           if ( wave.eq. 'B' .or. wave.eq. 'F' .or. wave.eq. 'N') 
     > field_cen=dataDir(1:length(dataDir))//'/fields_poss2.lis'


       if (wave.eq.'P' .or.  wave.eq.'A')
     > field_cen=dataDir(1:length(dataDir))//'/possI_e.rad'
       if (wave.eq.'O')
     > field_cen=dataDir(1:length(dataDir))//'/possI_o.rad'
        c=3072.
c superc measurse 322.56mm on a UK plate and 276.48 on an ESO
        platesize=177.8

        platelim=161.28
        if (wave.eq.'E') then 
        platelim=138.24
        platesize=148.50
        c=3055.77
        end if
        if (wave.eq.'P' .or. wave.eq.'O'. or. wave.eq. 'A') 
     >platelim=167.04
        if (wave.eq.'B' .or. wave.eq.'F' .or. wave.eq. 'N') 
     >platelim=161.28

c       radians to mm

   


        lengthno=1+aint(log10(float(proc)))
        nsrt=1
        pi=3.141592654
        tempfile=' '
        hkfile=' '
        rarad=drarad
        decrad=ddecrad
        bepoch=1990.0
         if (equinox.eq.2) then
         call sla_fk54z(drarad,ddecrad,bepoch,r1950,d1950,
     > dr1950,dd1950)


         drarad=r1950
         ddecrad=d1950

         end if
         
            platelim=platelim-slop        
         open (33,file=field_cen, status='old')
         nofields=0
         datayes=0
         dataedge=0
         lowdist=10000.
         diag=sqrt(2.0d0)*platelim
         do i=1,2000
         adddist=0.
           read (33,*,end=99)possfield,raf,decf
         if (wave.eq.'B' .or. wave.eq.'F' 
     >.or. wave.eq. 'N' ) decf=-decf

           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

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

cccc       write (6,*) possfield,xpl,ypl,jj,platelim      
       if (
     > ((abs(xpl).lt.(platelim+30.0d0) 
     >  .and. abs(ypl) .lt. diag)
     >  .or.
     > (abs(ypl).lt.(platelim+30.0d0) 
     >  .and. abs(xpl) .lt. diag))
     > .and. jj.eq.0) then
cccc           write(6,*) stemlength,'tata'
           if (wave.eq.'J'. or. wave.eq. 'R' .or. 
     >wave .eq. 'I') then 

           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+3)='UK'//wave
           write(tempfile(stemlength+4:stemlength+6),72)possfield
           end if
 72        format(i3.3)
           if (wave.eq.'E') then
           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+4)='ESOR'
           write(tempfile(stemlength+5:stemlength+7),72)possfield
           end if
 73        format (i4.4)
           if (wave.eq.'A' .and. decf .lt. 0.02) then
ccc           write (6,*) 'here 1'
            stem=surDirS(1:sLength)//'/PAEsurvey/'
            end if
           if (wave.eq.'P' .or. wave.eq.'A') then

           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+3)='PAE'
           write(tempfile(stemlength+4:stemlength+7),73)possfield
ccc                write (6,*) stem,' here 2 ', tempfile
           end if

           if (wave.eq.'O') then
           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+3)='PAO'
           write(tempfile(stemlength+4:stemlength+7),73)possfield
           end if

           if (wave.eq.'B') then
           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+3)='PAJ'
           write(tempfile(stemlength+4:stemlength+6),72)possfield
           end if

           if (wave.eq.'F') then
           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+3)='PAR'
           write(tempfile(stemlength+4:stemlength+6),72)possfield
           end if
           if (wave.eq.'N') then
           tempfile(1:stemlength)=stem(1:stemlength)
           tempfile(stemlength+1:stemlength+3)='PAI'
           write(tempfile(stemlength+4:stemlength+6),72)possfield
           end if
           templength=length(tempfile)
c           write (6,*) templength
           hkfile(1:templength)=tempfile(1:templength)
           hkfile(templength+1:templength+11)='/hkfile.lis'
                   
cc           write (6,*)hkfile
           open (55,file=hkfile
     >,status='old',iostat=ios,err=56)
 56      continue

cc        write (6,*) ios,hkfile

        if (ios.eq.0) then
ccc       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)
ccc       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.*pi/180.
       decf=rvaldec*pi/180.

       CALL sla_DS2TP (RAf, DECf, dRArad, dDECrad, XI, ETA, Jj) 
           xpl=xi*c
           ypl=eta*c
           dist=sqrt(xpl*xpl+ypl*ypl)
cccc      write (6,*)possfield,xpl,ypl,platelim,RAf, DECf, dRArad, dDECrad
       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
         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 ((platelim-abs(datax)).lt.9. .or. 
     >(platelim-abs(datay)).lt.9. )  then 
        dataedge=1
        else
        dataedge=0
        end if

         end if         
      end if
        end if
   
      if (ios .ne. 0) then


           CALL sla_DS2TP (RAf, DECf, dRArad, dDECrad, XI, ETA, Jj) 
           xpl=xi*c
           ypl=eta*c
           dist=sqrt(xpl*xpl+ypl*ypl)

       if (abs(xpl).lt.platelim .and. 
     >abs(ypl) .lt. platelim .and. jj.eq.0) then
     
      nofields=nofields+1
      scan(nofields)=0
      field(nofields)=possfield
      plx(nofields)=xpl
      ply(nofields)=ypl
      pldist(nofields)=dist
      fieldfile(nofields)=tempfile    
      end if
      end if
           endif         

         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                         


