pro bad_hand, ps=ps, zdb=zdb

; This routine prompts the user for information needed to provide
; bad data flag information in the "images" database.
; The images database must exist for this routine to run successfully.
; The user specifies a date, and the routine plots diagnostics that
; relate to clouds, to astrometric errors, and to PSF fitting errors.
; The user specifies which images are to be flagged as bad via cursor
; activity, and appropriate flags are entered into the images database
; before the routine exits.

print, '*****current version as of 051206*****'

; Trunk directories where PS and GIF files are created.  Make sure they exist!
psdir='/mnt/server/tbrown/dbase/diagnostics/plots_ps/'
gifdir='/mnt/server/tbrown/dbase/diagnostics/plots_gif/'
;psdir='/knobos/e/kolinski/kepler_diagnostics/plots_ps/'
;gifdir='/knobos/e/kolinski/kepler_diagnostics/plots_gif/'
;psdir='/knobos/e/kolinski/kepler_diagnostics/test/'
;gifdir='/knobos/e/kolinski/kepler_diagnostics/test/'


; constants
timez=7             ; timezone -- local time = UT -timez
tstrt=15            ; earliest plausible start time, local hours
tend=34             ; latest plausible end time, local hours (>24 ok)
rad2deg=57.295      ; degrees per radian
filtnam=['u        ','g        ','r        ','i        ','z        ',$
   'Gred     ','D51      ']
zdbase=getenv('ZDBASE')
if(keyword_set(zdb)) then zdbase=zdb
astrolib
imdbase=zdbase+'/survey/images/images'
jdrnd=.00002              ; JD roundoff slop allowed
mmoff=[15.59,13.76,13.20,12.94,12.84,14.36,14.36]  ; typical minmag values
!priv=2
loadct, 39

; identify night in question
date:
print,'Enter Mt. Hopkins LOCAL date for beginning-of-night yyyy mm dd'
read,yy,mm,dd
if(yy lt 2002 or yy gt 2010 or mm lt 1 or mm gt 12 or dd lt 1 or dd gt 31) $
  then begin
  print,'Illegal date: Need 2002 <= yyyy <= 2010, etc.  Try again.'
  goto,date
endif
jd0=julday(mm,dd,yy,0,0,0)             ; corresp to noon UT = 5AM before obs
jdmn=jd0+(timez+tstrt)/24.             ; min plausible start date
jdmx=jd0+(timez+tend)/24.              ; max ditto
f1='(f13.5)'
sjdmn=string(jdmn-jdrnd,format=f1)
sjdmx=string(jdmx+jdrnd,format=f1)

; get data on images within this jd, sorted by time 
dbopen,imdbase
s=dbfind(sjdmn+' < jd < '+sjdmx)
nim=n_elements(s)
if(s(0) eq 0) then begin
  print,'No images for this date, jd = ',jd0
  print,'Quit? (y/n)'
  ss='' & read,ss
  if(ss eq 'y') then goto,fini else goto,date
endif
sox=dbsort(s,'jd')
dbext,sox,'entry,jd,ra,dec,exptime,filter,cldflg,astflg,fitflg',$
          iseq,jd,ra,dec,exptime,filter,cldf,astf,fitf
nobs=n_elements(iseq)

; read the ingestion log data, sort by time
rd_obsdiag,jdmn,jdmx,jdo,obsno,filto,expto,noino,no2mo,badso,goodo,refo,targo,$
  newo,diags,zdb=zdb
so=sort(jdo)
jdo=jdo(so)
filto=filto(so)
expto=expto(so)
badso(*,*)=badso(*,so)
diags(*,*)=diags(*,so)
nlo=n_elements(expto)

; make useful temporal coordinates
jdz=jd-min(long(jd))
jdoz=jdo-min(long(jd))     ; use jd for both zero points, so can see if
                           ; different days.
; make numerical filter index for each log data sample
findx=intarr(nlo)
for i=0,6 do begin
  s=where(strlowcase(strtrim(filto,2)) eq strlowcase(strtrim(filtnam(i))),ns)
  if(ns gt 0) then findx(s)=i
endfor

; Make PS and GIF files of all plots
psflag = keyword_set(ps)
if (psflag) then begin
  CALDAT, (min(jd)-1.0D), mon, day, yr
  yr = STRTRIM(STRING(yr[0]),2)
  mon = STRTRIM(STRING(mon[0]),2)
  IF (STRLEN(mon) LT 2) THEN mon='0'+mon
  day = STRTRIM(STRING(day[0]),2)
  IF (STRLEN(day) LT 2) THEN day='0'+day
  pspath = psdir+yr+mon+day+'/'
  file0 = FINDFILE(pspath, count=ct)
  IF (ct EQ 0) THEN BEGIN
    str0 = 'mkdir '+pspath
    SPAWN, 'umask a+rwx ; '+ str0 + ' ; umask 22'   ;make sure directory is world-writable
  ENDIF
  gifpath = gifdir+yr+mon+day+'/'
  file1 = FINDFILE(gifpath, count=ct)
  IF (ct EQ 0) THEN BEGIN
    str1 = 'mkdir '+gifpath
    SPAWN, 'umask a+rwx ; '+ str1 + ' ; umask 22'
  ENDIF

  psfile = pspath+yr+mon+day+'_bad_hand.ps'
  set_plot, 'ps'
  DEVICE, /helvetica, bits_per_pixel=8, filename=psfile, /landscape,  $
    /inches, xsize=10, ysize=8, xoffset=.5, yoffset=10.5
  set_plot, 'x'
endif

window, 0, retain=2, xsize=800, ysize=650
!p.multi=[0,1,2]

; plot the sky background and min magnitude
plot1:
sg=where(strtrim(filto,2) eq 'g',nsg)
si=where(strtrim(filto,2) eq 'i',nsi)

scf = where(cldf GT 0, nscf)  ;test to see if existing flags should be plotted
if (nscf GT 0) THEN BEGIN
  jdp=jdz(scf)
  yp=fltarr(nscf)+2.5
ENDIF

xtit='JD'
mnmag=reform(diags(1,*))    ;-mmoff(findx)+13.
IF (psflag) THEN BEGIN
  set_plot, 'ps'
  ytit='diff SDSS'
  plot,jdoz,mnmag,psym=-1,xtit=xtit,ytit=ytit,yran=[-1,2]  ;[-.5,.5]
  ytit='Sky Brightness'
  plot,jdoz(sg),diags(0,sg),xtit=xtit,ytit=ytit,psym=-1,yran=[.5,15.]
  oplot,jdoz(si),diags(0,si),psym=-2
  set_plot, 'x'
ENDIF
ytit='diff SDSS'
plot,jdoz,mnmag,psym=-1,xtit=xtit,ytit=ytit,charsize=1.0,yran=[-1,2]  ;[-.5,.5]
ytit='Sky Brightness'
plot,jdoz(sg),diags(0,sg),xtit=xtit,ytit=ytit,psym=-1,charsize=1.0,yran=[.5,15.]
oplot,jdoz(si),diags(0,si),psym=-2
IF (nscf GT 0) THEN oplot,jdp,yp,psym=2  ;plot existing flags
IF (psflag) THEN BEGIN
  giffname =  gifpath+yr+mon+day+'_bad_hand_a.gif'
  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
  psflag=0 ;turn off plotting so clearing flags won't produce multi-plots
ENDIF

choose1:
ss=''
repeat begin
  print,'Set/Reset/Quit cloud flags?'
  ss=get_kbrd(1)
  ss=strlowcase(ss)
endrep until (ss eq 's' or ss eq 'r' or ss eq 'q')
if(ss eq 'q') then goto,plot2
if(ss eq 'r') then begin
  cldf(0:nobs-1)=0
  goto,plot1
endif
if(ss eq 's') then begin
  print,'click on lower time to be flagged as cloudy'
  cursor,ix1,iy1,3
  print,'Upper time....'
  cursor,ix2,iy2,3
  ixmin=min([ix1,ix2])
  ixmax=max([ix1,ix2])
  s=where(jdz ge ixmin and jdz le ixmax,ns)
  if(ns gt 0) then begin
    jdp=jdz(s)
    yp=fltarr(ns)+2.5 
    oplot,jdp,yp,psym=-2
    cldf(s)=1
  endif
  goto,choose1
endif
  


; plot the unmatched stars and rms astrometric error
plot2:
psflag=keyword_set(ps)  ; set back to original value
xtit='JD'
scf = where(astf GT 0, nscf)  ;test to see if existing flags should be plotted
if (nscf GT 0) THEN BEGIN
  jdp=jdz(scf)
  yp=fltarr(nscf)+2.5
ENDIF

IF (psflag) THEN BEGIN
  set_plot, 'ps'
  ytit='Unmatched Stars'
  plot,jdoz,diags(7,*),psym=-1,xtit=xtit,ytit=ytit,yran=[0,100]
  ytit='RMS Err (arcsec)'
  plot,jdoz,diags(6,*),xtit=xtit,ytit=ytit,psym=-1,yran=[0.,4.]
  set_plot, 'x'
ENDIF
ytit='Unmatched Stars'
plot,jdoz,diags(7,*),psym=-1,xtit=xtit,ytit=ytit,yran=[0,100],charsize=1.0
ytit='RMS Err (arcsec)'
plot,jdoz,diags(6,*),xtit=xtit,ytit=ytit,psym=-1,yran=[0.,4.],charsize=1.0
IF (nscf GT 0) THEN oplot,jdp,yp,psym=2  ;plot existing flags
IF (psflag) THEN BEGIN
  giffname =  gifpath+yr+mon+day+'_bad_hand_b.gif'
  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
  psflag=0 ;turn off plotting so clearing flags won't produce multi-plots
ENDIF

choose2:
repeat begin
  print,'Set/Reset/Quit astrometry flags?'
  ss=get_kbrd(1)
  ss=strlowcase(ss)
endrep until (ss eq 's' or ss eq 'r' or ss eq 'q')
if(ss eq 'q') then goto,plot3
if(ss eq 'r') then begin
  astf(0:nobs-1)=0
  goto,plot2
endif
if(ss eq 's') then begin
  print,'click on lower time to be flagged as bad astrometry'
  cursor,ix1,iy1,3
  print,'Upper time....'
  cursor,ix2,iy2,3
  ixmin=min([ix1,ix2])
  ixmax=max([ix1,ix2])
  s=where(jdz ge ixmin and jdz le ixmax,ns)
  if(ns gt 0) then begin
    jdp=jdz(s)
    yp=fltarr(ns)+2.5 
    oplot,jdp,yp,psym=-2
    astf(s)=1
  endif
  goto,choose2
endif
  


; plot the sharp and chi fitting parameters
plot3:
psflag=keyword_set(ps)  ; set back to original value
xtit='JD'
scf = where(fitf GT 0, nscf)  ;test to see if existing flags should be plotted
if (nscf GT 0) THEN BEGIN
  jdp=jdz(scf)
  yp=fltarr(nscf)+2.5
ENDIF

IF (psflag) THEN BEGIN
  set_plot, 'ps'
  ytit='Median Sharpness'
  plot,jdoz,diags(2,*),psym=-1,xtit=xtit,ytit=ytit,yran=[-2.,2.]
  ytit='Median Chi'
  plot,jdoz,diags(3,*),xtit=xtit,ytit=ytit,psym=-1,yran=[-1.,5.], linestyle=0
  oplot,jdoz,diags(4,*),psym=-2, linestyle=1
  oplot,jdoz,diags(5,*),psym=-4, linestyle=2
  set_plot, 'x'
ENDIF
ytit='Median Sharpness'
plot,jdoz,diags(2,*),psym=-1,xtit=xtit,ytit=ytit,yran=[-2.,2.],charsize=1.0
ytit='Median Chi'
plot,jdoz,diags(3,*),xtit=xtit,ytit=ytit,psym=-1,yran=[-1.,5.],color=255, $
  charsize=1.0
oplot,jdoz,diags(4,*),psym=-2, color=150
oplot,jdoz,diags(5,*),psym=-4, color=240
IF (nscf GT 0) THEN oplot,jdp,yp,psym=2  ;plot existing flags
IF (psflag) THEN BEGIN
  giffname =  gifpath+yr+mon+day+'_bad_hand_c.gif'
  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
  psflag=0 ;turn off plotting so clearing flags won't produce multi-plots
ENDIF

choose3:
repeat begin
  print,'Set/Reset/Quit fitting flags?'
  ss=get_kbrd(1)
  ss=strlowcase(ss)
endrep until (ss eq 's' or ss eq 'r' or ss eq 'q')
if(ss eq 'q') then goto,doneplot
if(ss eq 'r') then begin
  fitf(0:nobs-1)=0
  goto,plot3
endif
if(ss eq 's') then begin
  print,'click on lower time to be flagged as bad fit'
  cursor,ix1,iy1,3
  print,'Upper time....'
  cursor,ix2,iy2,3
  ixmin=min([ix1,ix2])
  ixmax=max([ix1,ix2])
  s=where(jdz ge ixmin and jdz le ixmax,ns)
  if(ns gt 0) then begin
    jdp=jdz(s)
    yp=fltarr(ns)+2.5 
    oplot,jdp,yp,psym=2
    fitf(s)=1
  endif
  goto,choose3
endif
  
doneplot:
;test=where(cldf NE 0, nt)   ;*****************
;print, nt                   ;*****************

; put the flags into the database
dbopen,imdbase,1
dbupdate,sox,'cldflg,astflg,fitflg',cldf,astf,fitf
dbclose
wdelete, 0

fini:
!p.multi=[0,1,1]
if (keyword_set(ps)) then begin
  set_plot, 'ps'
  device, /close
  set_plot, 'x'
endif

end