Viewing contents of file '../idllib/sdss/allpro/get_atlas.pro'
pro get_atlas, struct, index, clr=clr, dir=dir, $
               maxsize=maxsize, noprompt=noprompt, nodisplay=nodisplay,$
               imtot=imtot, imu=imu, img=img, imr=imr, imi=imi, imz=imz,$
               row0=row0, col0=col0, silent=silent, hideradec=hideradec,$
               _extra=extra

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;+
;
; NAME:
;       GET_ATLAS
; PURPOSE:
;	Grab the atlas images for a list of objects.
;
; CALLING SEQUENCE:
;   get_atlas, struct, index, clr=clr, dir=dir, 
;              maxsize=maxsize, noprompt=noprompt, nodisplay=nodisplay,
;              imtot=imtot, imu=imu, img=img, imr=imr, imi=imi, imz=imz,
;              row0=row0, col0=col0, silent=silent, hideradec=hideradec,
;              _extra=extra
;
; INPUTS: struct: photo structure.  Must contain run,field,camcol (rerun)
;         index:  array of indices (or just an integer for one object)
; 
; OPTIONAL INPUTS: 
;         clr: indices of colors to use.  Can be an array.
;
;         Follows photo convention: 0,1,2,3,4 -> u,g,r,i,z
;
;         dir: directory of atlas images. Only needed if the images 
;              are not in cwd
;
;         maxsize: maximum size for images.  [500,500] is default.
;         noprompt: If set there is no prompting.
;         nodisplay: If set there is no display.
;         silent: will run silently unless bad images or missing
;                 images are found
;	  hideradec: will not include ra dec in titles 
;         _extra=extra: extra keywords for plotting.
;
; OPTIONAL OUTPUTS: 
;         imtot:  An image containing Atlas images for index 
;                 in all 5 colors.  Only returns 
;                 the image of the last index that was retrieved.
;         imu, img, imr, imi, imz:  The images for individual
;                 colors.  Only returns 
;                 the image of the last index that was retrieved.
;         row0, col0:  objc_rowc, objc_colc position of
;                 bottom left corner of atlas image.  Only
;                 returns last one found (z for regular use)
; 
; PROCEDURE: call external c routine (originally written by R. Lupton) which
;      has been modified to link with IDL.  This c program gets an atlas image
;      for a given color, run, camcol, field, id.
;
; CALLED ROUTINES:  missing_atlas
;                   atlas_name
;                   display_atlas
;                   copy_atlas
;                   combine_atlas
;                   sharable excecutable "call_atlas.so"
;
; EXAMPLES:
;   Look at the atlas images in each bandpass for first 10 objects in frame 35
;   of run 109
;
; IDL> file='/sdss3/data4/run109/tsObj-000109-3-0-0011.fit'
; IDL> read_photo_col, file, struct, struct_type='all', start=35, nframes=1
; IDL> dir = '/sdss3/data4/run109/'
; IDL> index = indgen(10)
; IDL> get_atlas, struct, index, dir=dir
;
;   Save the r' band atlas image with no display or prompting.  If clr is 
;   specified, then only that image is retrieved and the program runs faster.
;
; IDL> get_atlas, struct, 33, dir=dir, clr=2, /noprompt, /nodisplay, $
; IDL> imr=red_image
; 
;
; REVISION HISTORY:
;       Author:
;       Erin Scott Sheldon    UM      2/5/99  Modified Tim Mckay's 
;                                       get_atlas_images which used spawn
;                                       to call Lupton's stand alone code.
;                                       Linked directly to IDL with 
;                                       call_external.  
;                                     2/8/99  Let index be an array, added
;                                       prompting.  Added subroutine
;                                       missing_atlas for when there is 
;                                       no atlas image.  Added prompt keyword
;                                       Added displaying.
;	David Johnston			added hideradec
;-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  if N_params() LT 2 then begin
      print,'Syntax -  get_atlas, struct, index, clr=clr, dir=dir, '
      print,'       maxsize=maxsize, noprompt=noprompt, nodisplay=nodisplay,'
      print,'       imtot=imtot, imu=imu, img=img, imr=imr, imi=imi, imz=imz,'
      print,'       row0=row0, col0=col0, silent=silent, hideradec=hideradec,'
      print,'       _extra=extra'

      print,''
      print,'-If you want to zoom, enter r when prompted.'
      print,'Use doc_library,"get_atlas"  for more help'
      return
  endif

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Parameters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  IF n_elements(dir) EQ 0 THEN dir=''

  color=['u','g','r','i','z']
  num = ['0 ','1 ','2 ','3 ','4 ']

;;;  Define the sharable object file name and define the entry   ;;;;;
;;;  point to sharable object file: sofile  Note this is slightly ;;;;
;;;  different on other OS's These variables must be set in      ;;;;;
;;;  sdssidl_setup.pro                                            ;;;;

  defsysv, '!SOFILE', exists=exists1
  defsysv, '!ENTRY', exists=exists2
  IF (NOT exists1) OR (NOT exists2) THEN sdssidl_setup

  sofile = !SOFILE
  entry  = !ENTRY

;;;;;;;; which colors are we getting?           ;;;;;;;;;;;;;;;;;;;;

  if ( n_elements(clr) ne 0 ) then begin
      if (max(clr) gt 5 or min(clr) lt 0) then begin
          print,'clr must be in [0,4]'
          return
      endif
      ncolor = n_elements(clr)
      if (ncolor ne 1) then begin
          s = sort(clr)
          cnum=clr(s)
      endif else begin
          cnum = clr
      endelse
  endif else begin
      ncolor = 5
      cnum = indgen(ncolor)
  endelse
  
;;;;;;;;;;;  See if display is requested    ;;;;;;;;;;;;;;;;;;;;;;;;;
  
  if keyword_set(nodisplay) then begin
      if (nodisplay ne 1) then begin
          nodisplay = 1
      endif
  endif else nodisplay = 0

;;;;;;;;;; Display ra and dec?       ;;;;;;;;;

  if (NOT keyword_set(hideradec) ) then hideradec = 0

;;;;;;;;;;  See if prompting about missing images unwanted ;;;;;;;;;;

  if keyword_set(noprompt) then begin
      if (noprompt ne 1) then begin
          noprompt = 1
      endif
  endif else noprompt = 0


;;;;;;;;;;;  Define the maximum size for each image ;;;;;;;;;;;;;;;;;
;;;;;;;;;;;  Default is [500,500]                   ;;;;;;;;;;;;;;;;;

  size=[500,500]
  if keyword_set(maxsize) then size=maxsize

;;; If zoom not set, don't zoom unless told to at the prompt  ;;;; 

  if ( not keyword_set(zoom) ) then zoom=0


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;  If index is an array, loop over them all ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  tags = tag_names(struct)
  wrer = where(tags EQ 'RERUN', nrer)

  max=n_elements(index)
  FOR i=0L, max-1 DO BEGIN

      JUMP:                ;;;;;;;; Used a goto, don't hate me ;;;;;;;;;;
 
      ;;;;;;;;;; Build up the filename for the field...;;;;;

      obj_id = fix(struct(index[i]).id)
   
      field = struct(index[i]).field
      camcol=struct(index[i]).camcol
      run=struct(index[i]).run
      IF nrer NE 0 THEN rerun = struct(index[i]).rerun

      ;;;;;;;  call subroutine atlas_name  ;;;;;;;

      atlas_name, run, camcol, field, fname
      fname=dir+fname

      if (not keyword_set(silent) ) then begin
          print,'------------------------------------------------'
          print,'Index:     ',ntostr(i),'  Of: ',ntostr(max-1)
          print,'Object id: ',ntostr(obj_id)
          print,'Field:     ',ntostr(field)
          print,'File:',fname
          print,'------------------------------------------------'
      endif
      
      ;;;;;;;;;;;;;; Get the images ;;;;;;;;;;;

      ids = ntostr(obj_id)
      xs = intarr(5)
      ys = intarr(5)

      for kk=0,ncolor-1 do begin

          n=cnum[kk]

          ;; in case no image is found ;;;
          im = intarr(size[0],size[1])

          size_im = size	    

          orig=intarr(2)
          s=call_external(value=[0B,0B,0B,0B,0B,0B],sofile,entry,im,$
                          size_im,num[n],fname,ids,orig)
          if (s eq 1) then begin
              missing_atlas, request, i, max, noprompt,silent=silent
              CASE request OF
                  'q': return
                  ELSE: goto,JUMP
              ENDCASE
          endif
          row0=orig[0]
          col0=orig[1]

          col = color[n]
          ;; Call Subroutine copy_atlas ;;;;
          IF size_im[0] GT 0 AND size_im[1] GT 0 THEN BEGIN 
              copy_atlas, col,im, xs, ys, size_im, $
                imu=imu, img=img, imr=imr, imi=imi, imz=imz
          ENDIF ELSE BEGIN 
              print,'Corrupt atlas image'
          ENDELSE 

      endfor

      ;;     Call subroutine to combine all Atlas images
 
      if (ncolor ne 1) then combine_atlas, xs, ys, cnum, color, imtot,imu=imu,$
        img=img,imr=imr,imi=imi,imz=imz else imtot=im

      ;; Call subroutine display_atlas if display is requested ;;;;;;;;

      if (nodisplay eq 0) then begin
	
          ;; see if we need to comput separation
          wt = where(tags EQ 'NCHILD', nwt)
          IF nwt NE 0 THEN BEGIN
              IF struct[index[i]].nchild EQ 2 THEN BEGIN
                  compute_sep, struct, index[i], sep, pa
              ENDIF ELSE BEGIN 
                  sep=-1 & pa = -1 
              ENDELSE 
          ENDIF ELSE BEGIN 
              sep=-1 & pa = -1 
          ENDELSE 
             

          display_atlas, struct[index[i]], imtot, i, ret, zoom, run, camcol, $ 
            field,obj_id,cnum, rerun=rerun, noprompt=noprompt, struct=struct,$
            silent=silent, sep=sep,pa=pa,_extra=extra, hideradec=hideradec

          if (ret eq 'q') then return
      endif

  endfor
  return
end