Viewing contents of file '../idllib/sdss/allpro/fchart_circ_radec.pro'
PRO fchart_circ_radec, photostr, ra, dec, fchart, $
                       clr=clr, radius=radius, tol=tol, $
                       maxsize=maxsize, $
                       nonoise=nonoise, $
                       circ_rad = circ_rad, $
                       circ_obj=circ_obj,                $
                       nodisplay=nodisplay, psfile=psfile, $
                       silent=silent, $
                       objx=objx, objy=objy, photoid=photoid, $
                       nocirc = nocirc, $
                       _extra=extra

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;+
;
; NAME: 
;    FCHART_CIRC_RADEC
;       
; PURPOSE: 
;    Build a finding chart and (unless requested otherwise ) circle positions 
;    in ra and dec arrays.
;	
;
; CALLING SEQUENCE: 
;    fchart_circ_radec, photostr, ra, dec, fchart, 
;                      clr=clr, radius=radius, tol=tol,
;                      maxsize=maxsize,  
;                      nonoise=nonoise, 
;                      nocirc=nocirc, 
;                      circ_rad = circ_rad, 
;                      circ_obj=circ_obj, 
;                      nodisplay=nodisplay, psfile=psfile, 
;                      silent=silent,
;                      objx=objx, objy=objy, photoid=photoid, _extra=extra
;
; INPUTS: photostr:  A photo structure.  Must contain RUN and CAMCOL tags to
;             find atlas directory (calls fetch_dir) Also must contain RA and
;             DEC so that CIRC_RADEC can make a good mapping from row,col to
;             ra and dec
;         ra, dec:   The arrays of ra and dec positions to be circled.  Finding
;             chart is made for object in photostr that most closely matches
;             the _first_ ra and dec in list.  Note, if you set /nocirc, none
;             of the objects in the list will be circled.
;
; OPTIONAL INPUTS:
;         maxsize: maximum size for atlas images; sent to atlas images.  If 
;                 your images are being clipped off, you should increase
;                 maxsize. default = [500,500]
;         clr:       Color to make finding chart.  [0,1,2,3,4]->[u,g,r,i,z]
;                 default is clr=2
;         radius:    Optional input.  Radius to make finding chart in pixels.
;         tol:       Optional input tolerance (in arcsec) used to find nearest 
;                    object to ra dec. This should be large to gaurantee it 
;                    will find and object.  Default is 500 arcsec. Will search 
;                    on first ra dec in list. 
;         cird_rad:  array of radii used to make circle around objects in 
;                    the list.  Default is size of finding chart/10.0 and 
;                    consecutively smaller for each in list.
;         circ_obj: If set, circle the object used to make finding chart.
;         psfile:    Name of psfile in which to plot.
;         photoid:   Can input photoid to make fchart from, else it will find
;                    nearest match in ra,dec. If not input, can
;                    be returned with this keyword. See below.
;         _extra:    Extra plotting options.
;
; KEYWORD PARAMETERS:
;         nonoise: keyword sent to fchart.  Set for no noise added to fchart.
;         nodisplay:  If set, won't display the image.
;         silent:   Shut off messages except errors.
;         nocirc: If set, no circling is done.
;
; OPTIONAL OUTPUTS:
;         fchart:  Return the image.
;         objx, objy:  The position of object used to make finding chart.
;         photoid:   If not input, returns id used to make finding chart
;
; CALLED ROUTINES:
;                  (CLOSE_MATCH_RADEC)
;                  FCHART
;                  (DISPLAY_FCHART)
;                  (CIRC_RADEC)
;                  FETCH_DIR
;                  (FETCH_RERUN)
; 
; PROCEDURE: 
;   if not input in photoid, match first ra and dec in structure to 
;   the photostr.  Then make a finding chart and circle all the ra and dec.
;	
;
; REVISION HISTORY:
;	Author: Erin Scott Sheldon Umich 5/25/99
;       
;                                      
;-                                       
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


  if N_params() LT 3 then begin
     print,'-Syntax: fchart_circ_radec, photostr, ra, dec [, 
     print,'           fchart, '
     print,'           clr=clr, radius=radius, tol=tol, '
     print,'           maxsize=maxsize, '
     print,'           nonoise=nonoise, '
     print,'           nocirc=nocirc, '
     print,'           circ_rad = circ_rad, '
     print,'           circ_obj=circ_obj,'
     print,'           nodisplay=nodisplay, psfile=psfile, '
     print,'           silent=silent, '
     print,'           objx=objx, objy=objy, photoid=photoid, '
     print,'           _extra=extra ]'
     print,''
     print,'Use doc_library,"fchart_circ_radec"  for more help.'  
     return
  endif

  tags = tag_names(photostr)
  wrun = where(tags EQ 'RUN',nrun)
  wcol = where(tags EQ 'CAMCOL',ncol)
  wrer = where(tags EQ 'RERUN',nrer)
  IF (nrun EQ 0) OR (ncol EQ 0) THEN BEGIN 
      print,'Structure must contain "RUN" and "CAMCOL"'
      return
  ENDIF 
  run = photostr[0].run
  IF nrer EQ 0 THEN BEGIN
      fetch_rerun, run, rerun
  ENDIF ELSE BEGIN
      rerun = photostr[0].rerun
  ENDELSE 
  camcol = photostr[0].camcol
  fetch_dir, run, camcol, rerun, dir, atldir

  IF NOT keyword_set(silent) THEN silent = 0
  IF n_elements(radius) EQ 0 THEN radius = 300.
  IF n_elements(maxsize) EQ 0 THEN maxsize=[500,500]

  nn = n_elements(ra)
  nn2 = n_elements(dec)
  IF (nn NE nn2) THEN BEGIN
    print,'ra and dec should be same size'
    return
  ENDIF 

;;; use input id for nearest object if it is given, else find the nearest
;;; object to first ra-dec pair within tolerance

  IF NOT silent THEN print
  IF (n_elements(photoid) EQ 0) THEN BEGIN 
    IF NOT silent THEN print,'Getting photoid of closest object'
    IF NOT keyword_set(tol) THEN tol = 500/3600. ;100 arcseconds
    allow = 1
    close_match_radec, ra[0], dec[0], photostr.ra, photostr.dec, $
             match1, photoid, tol, allow, silent=silent
  ENDIF

  IF photoid[0] EQ -1 THEN BEGIN
    print,'No matches found'
    return
  ENDIF 

;;; make fchart for nearest object (photoid) and display it

  IF n_elements(psfile) NE 0 THEN BEGIN
      pcharold = !p.charsize
      !p.charsize = 1.
      begplot,name=psfile
  ENDIF 
  IF NOT silent THEN print,'Making the fchart'
  fchart, photostr, photoid, radius, clr, fchart, maxsize=maxsize, $
          dir=atldir, objx=objx, objy=objy, silent=silent, nonoise=nonoise

  IF keyword_set(nodisplay) THEN return 

  display_fchart, fchart, photostr[photoid],objx,objy,clr,/nocirc,$
                  silent=silent, nodisplay=nodisplay, _extra=extra

  IF keyword_set(nocirc) THEN return

  s=size(fchart)
  ss = min([s[1],s[2]])

  IF (n_elements(circ_obj) NE 0)  THEN BEGIN
    ra = [photostr[photoid].ra, ra]
    dec = [photostr[photoid].dec, dec]
    nn = nn+1
    IF (n_elements(circ_rad) NE 0) THEN circ_radius = [ss/10.0,circ_rad]
  ENDIF
  IF NOT silent THEN BEGIN 
    print,'' 
    print,strtrim(string(nn),2)+' positions to be circled'
  ENDIF 

;;; circle the ra's and dec's with different colors and sizes

  ;;; don't let radius get less than 1/30th image size if 
  ;;; radius's are not input

  color = .7*!d.n_colors
  ii = findgen(nn)
  IF (n_elements(circ_radius) EQ 0) THEN begin
      IF (nn NE 1) THEN fac_step = (30.0-10.0)/(nn-1) ELSE fac_step=1.0
      fac = 10.0 + ii*fac_step
      circ_radius = ss/fac
  ENDIF 
  FOR i=0, nn-1 DO BEGIN
      
      box = 0
      IF (n_elements(circ_obj) NE 0 AND i EQ 0) THEN box=1
      circ_radec, photostr, photoid, objx, objy, ra[i],dec[i],fchart,$
        /nodisplay, color=color, radius=circ_radius[i], $
        box=box,clr=clr,silent=silent
      
  ENDFOR
  IF n_elements(psfile) NE 0 THEN BEGIN
      endplot,/noprint
      !p.charsize = pcharold
  ENDIF 
  IF NOT silent THEN print

return
end