pro resid_plot2,x,colorse,time,resid,weight,igood,filter,blockid,psflag,$
    giffname,edit=edit,crn=crn,line=line

; This routine plots residual magnitudes after a fit done by, eg, fit_stds.pro.
; Points with nonzero weight are plotted with '+', others with '.'.
; Min and max resids with nonzero weight are printed.
; If keyword edit is set, then the user is allowed to delete or undelete
; points from the list of those with nonzero weights.
; In this case, the user is also queried as to whether to plot the full
; y range of the data.
; On input, igood is an array containing 0 or 1 according as the corresp
; data point has been deleted or not.
; On input, filter contains a string with the name of the current filter,
; to be placed in the plot title.
; On return, igood is updated to reflect the user's interactions.
; If keyword crn is set, it must contain a 2-element vector giving the
;  (x) range of colors to be plotted.
; If keyword line is set, its value must be a 2-element floating vector.
;  the routine will then overplot (on the airmass plots only) a dashed line
;  passing through (X=0, y=line(0)) with slope dy/dX = line(1).

; constants
dmag=0.40         ; half plot range, to begin with


s0=where(weight eq 0,ns0)
s1=where(weight ne 0,ns1)
npt=n_elements(resid)

yran=[-dmag,dmag]
sg=where(resid gt (-40) and resid lt 40,nsg)
if(nsg gt 0) then yran1=[min(resid(sg))-.1,max(resid(sg))+.1] else $
                  yran1=[min(resid)-.1,max(resid)+.1]
xran1=[.9,max(x)+.1]
xran2=[min(colorse)-.1,max(colorse)+.1]
xran3=[min(time)-.5,max(time)+.5]
if(keyword_set(crn)) then xran2=crn
ytit='Residuals (mag)'
xtit1='Airmass'
xtit2='Color'
xtit3='Time (local hours)'
tit='Filter = '+filter

if(keyword_set(line)) then begin
  xli=0.5+findgen(5)
  yli=line(0)+xli*line(1)
  xlit=xran3
  ylit=line(0)+xlit*line(1)
endif


if (psflag) then begin  ; produce a ps page of 3 plots for this filter & GIFs
  set_plot, 'ps'
  !p.multi = [0,1,3]
  yr=[-.6,.6]

  s0=where(igood eq 0,ns0)
  s1=where(igood eq 1,ns1)
  plot,x,resid,xran=xran1,yran=yr,xtit=xtit1,ytit=ytit,tit=tit,/nodata, $
       xstyle=1, ystyle=1, charsize=1.5
  if(ns0 gt 0) then begin
    oplot,x(s0),resid(s0),psym=3,symsiz=.7
  endif
  if(ns1 gt 0) then begin
    oplot,x(s1),resid(s1),psym=1,symsiz=.6
  endif
  if(keyword_set(line)) then begin
    oplot,xli,yli,line=2
  endif

  plot,colorse,resid,xran=xran2,yran=yr,xtit=xtit2,ytit=ytit,tit=tit, $
       xstyle=1, ystyle=1, /nodata, charsize=1.5
  if(ns0 gt 0) then begin
    oplot,colorse(s0),resid(s0),psym=3,symsiz=.7
  endif
  if(ns1 gt 0) then begin
    oplot,colorse(s1),resid(s1),psym=1,symsiz=.6
  endif

  plot,time,resid,xran=xran3,yran=yr,xtit=xtit3,ytit=ytit,tit=tit,/nodata, $
       xstyle=1, ystyle=1, charsize=1.5
  if(ns0 gt 0) then begin
    oplot,time(s0),resid(s0),psym=3,symsiz=.7
  endif
  if(ns1 gt 0) then begin
    oplot,time(s1),resid(s1),psym=1,symsiz=.6
  endif
  if(keyword_set(line)) then begin
    oplot,xlit,ylit,line=2
  endif

  xyouts, .5,1.05, 'Block '+strtrim(string(blockid),2), /normal, alignment=0.5

  set_plot, 'x'
   
  ;Make GIFs
  !p.multi = [0,1,3]
  WINDOW, /FREE, /PIXMAP, XSIZE=800, YSIZE=1000
  plot,x,resid,xran=xran1,yran=yr,xtit=xtit1,ytit=ytit,tit=tit,/nodata, $
       xstyle=1, ystyle=1, charsize=2
  if(ns0 gt 0) then begin
    oplot,x(s0),resid(s0),psym=3,symsiz=.7
  endif
  if(ns1 gt 0) then begin
    oplot,x(s1),resid(s1),psym=1,symsiz=.6
  endif
  if(keyword_set(line)) then begin
    oplot,xli,yli,line=2
  endif
  plot,colorse,resid,xran=xran2,yran=yr,xtit=xtit2,ytit=ytit,tit=tit, $
       xstyle=1, ystyle=1, /nodata, charsize=2
  if(ns0 gt 0) then begin
    oplot,colorse(s0),resid(s0),psym=3,symsiz=.7
  endif
  if(ns1 gt 0) then begin
    oplot,colorse(s1),resid(s1),psym=1,symsiz=.6
  endif
  plot,time,resid,xran=xran3,yran=yr,xtit=xtit3,ytit=ytit,tit=tit,/nodata, $
       xstyle=1, ystyle=1, charsize=2
  if(ns0 gt 0) then begin
    oplot,time(s0),resid(s0),psym=3,symsiz=.7
  endif
  if(ns1 gt 0) then begin
    oplot,time(s1),resid(s1),psym=1,symsiz=.6
  endif
  if(keyword_set(line)) then begin
    oplot,xlit,ylit,line=2
  endif

  tvlct, r, g, b, /get
  r[0]=255 & g[0]=255 & b[0]=255
  r[255]=0 & g[255]=0 & b[255]=0
  gif_image = tvrd()
  write_gif, giffname, gif_image, r, g, b

endif

if(keyword_set(edit)) then begin

  ;Plot to window
  window, 0, retain=2, xsize=1000, ysize=800 ;'retain' keyword sets window mem
  !p.multi=[0,1,2]

  plot,x,resid,xran=xran1,yran=yran,xtit=xtit1,ytit=ytit,tit=tit,/nodata
  if(ns0 gt 0) then begin
    oplot,x(s0),resid(s0),psym=2,symsiz=1
  endif
  if(ns1 gt 0) then begin
    oplot,x(s1),resid(s1),psym=1,symsiz=1
  endif
  if(keyword_set(line)) then begin
    oplot,xli,yli,line=2
  endif

  plot,colorse,resid,xran=xran2,yran=yran,xtit=xtit2,ytit=ytit,tit=tit,/nodata
  if(ns0 gt 0) then begin
    oplot,colorse(s0),resid(s0),psym=2,symsiz=1
  endif
  if(ns1 gt 0) then begin
    oplot,colorse(s1),resid(s1),psym=1,symsiz=1
  endif
  !p.multi=[0,1,1]

loop0:
  qf = 0            ; flag so that mouse query text isn't displayed each time
  print,'Edit (y/n)?'
  ss=''
  ss = GET_KBRD(1)
  if(strmid(ss,0,1) ne 'y') then goto,fini
  print,'Plot X, Color, or Time (x/c/t)?'
  ss = GET_KBRD(1)
  ss0=strmid(ss,0,1)  
  print,'Full y plot range (y/n/o)?'
  ss = GET_KBRD(1)
  ss1=strmid(ss,0,1)
  if(ss1 eq 'y') then yr=yran1 
  if (ss1 eq 'o') then begin
     rq = fltarr(2)
     read, rq, prompt="enter new yrange (format y1, y2): "
     yr = rq
  endif
  if (ss1 ne 'y' and ss1 ne 'o') then yr=yran
loop1:
  s0=where(igood eq 0,ns0)
  s1=where(igood eq 1,ns1)
  if(ss0 eq 'x') then begin
    plot,x,resid,xran=xran1,yran=yr,xtit=xtit1,ytit=ytit,tit=tit,/nodata, $
         xstyle=1, ystyle=1
    if(ns0 gt 0) then begin
      oplot,x(s0),resid(s0),psym=3,symsiz=.7
    endif
    if(ns1 gt 0) then begin
      oplot,x(s1),resid(s1),psym=1,symsiz=.6
    endif
    if(keyword_set(line)) then begin
      oplot,xli,yli,line=2
    endif
  endif
  if(ss0 eq 'c') then begin
    plot,colorse,resid,xran=xran2,yran=yr,xtit=xtit2,ytit=ytit,tit=tit, $
         xstyle=1, ystyle=1, /nodata
    if(ns0 gt 0) then begin
      oplot,colorse(s0),resid(s0),psym=3,symsiz=.7
    endif
    if(ns1 gt 0) then begin
      oplot,colorse(s1),resid(s1),psym=1,symsiz=.6
    endif
  endif
  if(ss0 eq 't') then begin
    plot,time,resid,xran=xran3,yran=yr,xtit=xtit3,ytit=ytit,tit=tit,/nodata, $
         xstyle=1, ystyle=1
    if(ns0 gt 0) then begin
      oplot,time(s0),resid(s0),psym=3,symsiz=.7
    endif
    if(ns1 gt 0) then begin
      oplot,time(s1),resid(s1),psym=1,symsiz=.6
    endif
    if(keyword_set(line)) then begin
      oplot,xlit,ylit,line=2
    endif
  endif

  IF (NOT qf) THEN BEGIN
    print, '- Left mouse button drags box around stars to delete/undelete.'
    print, '- Right mouse button exits editing mode.'
    qf = 1
  ENDIF

  editbox, x0,y0, x1,y1, flag

  IF (flag EQ 4) THEN GOTO, loop0
  xc0 = x0 < x1 & xc1 = x0 > x1
  yc0 = y0 < y1 & yc1 = y0 > y1

  IF (ss0 eq 'x') THEN $
    bx = WHERE(x GE xc0 AND x LE xc1 AND resid GE yc0 AND resid LE yc1, nbx)
  IF (ss0 eq 'c') THEN $
    bx = WHERE(colorse GE xc0 AND colorse LE xc1 AND resid GE yc0 $
               AND resid LE yc1, nbx)
  IF (ss0 eq 't') THEN $
    bx = WHERE(time GE xc0 AND time LE xc1 AND resid GE yc0 AND resid LE yc1, $
               nbx)

  IF (nbx GT 0) THEN BEGIN  ;if there are stars within edit box
    gd = WHERE(igood[bx], ngd)
    IF (ngd GT 0) THEN BEGIN
      igood[bx] = 1
      igood[bx[gd]] = 0
    ENDIF ELSE igood[bx]=1
  ENDIF
 
  goto,loop1

endif

fini:
IF (!d.window EQ 0) THEN WDELETE, 0

end