pro stellar_parms1,tilenames,colblk=colblk,hrblk=hrblk,prmblk=prmblk,$
    xform=xform,zdb=zdb,bias=bias

; This routine estimates stellar parameters {teff, log(Z), log(g), A_V}
; for all of the stars found in the list of tiles tilenames.  For
; each tile, it reads the data in the 'stars' database, and uses
; the magnitudes found therein to estimate stellar properties and errors,
; using routine find_prop1.pro.  The properties and their estimated errors
; are written into the 'properties' database.
; Stellar colors (uncorrected for M67 color transformation) are obtained
; from file colorblock, written by routine color_block.pro.
; If keyword xform is set, these colors (also reddening values) are transformed
; using the coefficients in file xform before comparison with observations,
; with xforms data computed by m67_color_fit.pro.
; If keyword xform is not set, the color transformation file defaults to
; that in variable xformi.
; Also written are a number of missing-data flags, which are set to 1
; if the corresponding data are missing, else zero.
; These are:
;  uflag = 1 if no u photometry
;  m2flag = 1 if no 2MASS photometry
;  zflag = 1 if no Z photometry
;  oflag = 1 if any of {gri,gred,D51} missing
;
; A later version will do something useful with proper motion info.
; Note that the properties databases are recreated from scratch each
; time this routine is run -- they are not simply modified.
; If keywords colblk, hrblk, or prmblk are set, their values override
; the defaults for save files colorblock, hrblock, prmblock.
; If keyword bias is set, giants are preferred to dwarfs by an amoung proportional
; to the bias value.  values in the range 1-10 are plausible.

; path names, etc.
zdbase=getenv('ZDBASE')
if(keyword_set(zdb)) then zdbase=zdb
starpath=zdbase+'/survey/stars/'
proppath=zdbase+'/survey/properties/'

; the old one
;colorblock='/home/tbrown/d/basel/color_block1.sav'

;  the good one
colorblock='/home/tbrown/d/basel/colblock_cas4_smth.sav'

;hrblock='/home/tbrown/d/stardat/hrblock1.sav'
;hrblock='/home/tbrown/d/stardat/hrblock_gen2.sav'
;hrblock='/home/tbrown/d/stardat/hrblock_gen2.2.sav'
;hrblock='/home/tbrown/d/stardat/hrblock_gen2.3.sav'
;hrblock='/home/tbrown/d/stardat/hrblock_gen3.1.sav'
hrblock='/home/tbrown/d/stardat/hrblock_mod2.sav'
prmblock='/home/tbrown/d/stardat/propsblock.sav'
;xformi='/home/tbrown/d/basel/xform_color3.sav'

; the old one
;xformi='/home/tbrown/d/basel/xform_color5.sav'

; the good one
;xformi='/home/tbrown/d/basel/xform_color_cas3.sav'
xformi='/home/tbrown/d/basel/xform_color_cas4.sav'

guessdat='/home/tbrown/d/stardat/guessvals_in.sav'

deg2rad=180./!pi                ; degrees per radian
astrolib
!priv=2
;deltas=[250.,1.,1.,.5]         ; min step sizes in each parameter
tsolar=5777.			; solar Teff
if(not keyword_set(xform)) then xform=xformi
;stop

; fetch colors table, ancillary tables, 
; These restores create arrays cblock, teffu, loggu, logzu, hrb, logtn, logln
; Also logtp,loggp,bcp,bmvp,loglump,logmassp,rabsp,vabsp,vmrp
common tables,logln,logtn,hrb,$
              logtp,loggp,bcp,bmvp,loglump,logmassp,rabsp,vabsp,vmrp,$
              teffu,loggu,logzu,colblock
if(keyword_set(colblk)) then colorblock=colblk
if(keyword_set(hrblk)) then hrblock=hrblk
if(keyword_set(prmblk)) then prmblock=prmblk
restore,colorblock
restore,hrblock
restore,prmblock
restore,guessdat

; read list of tilenames
openr,iun,tilenames,/get_lun
ss=''
tnames=['']
while(not eof(iun)) do begin
  readf,iun,ss
  tnames=[tnames,strtrim(ss,2)]
endwhile
close,iun
free_lun,iun
tnames=tnames(1:*)
ntiles=n_elements(tnames)

; loop through tiles, do the work
for itile=0,ntiles-1 do begin
  fname=starpath+tnames(itile)
  dbopen,fname
  dbext,-1,'StarID,ra,dec,u,g,r,i,z,Gred,D51',sid,ra,dec,u,g,r,i,z,Gred,D51
  dbext,-1,'J,H,K,sig_u,sig_g,sig_r,sig_i,sig_z,sig_Gred,sig_D51',jj,hh,kk,$
            sig_u,sig_g,sig_r,sig_i,sig_z,sig_Gred,sig_D51
  
; make galactic latitude for all stars
  glactc,ra,dec,2000.,gl,gb,1

; loop over stars
  nstars=n_elements(sid)

; make output arrays
  stteff=fltarr(nstars)
  stlogz=fltarr(nstars)
  stlogg=fltarr(nstars)
  stext=fltarr(nstars)
  errteff=fltarr(nstars)
  errlogz=fltarr(nstars)
  errlogg=fltarr(nstars)
  errext=fltarr(nstars)
  kepmag=fltarr(nstars)
  stflags=intarr(nstars,4)
  chisq=fltarr(nstars)
  ratios=fltarr(4,nstars)
  stradius=fltarr(nstars)

  for j=0L,nstars-1 do begin
    colvec=[u(j),g(j),r(j),i(j),z(j),jj(j),hh(j),kk(j),gred(j),d51(j)]
    err2m=err2mass(jj(j),hh(j),kk(j))
    errvec=[sig_u(j),sig_g(j),sig_r(j),sig_i(j),sig_z(j),err2m,$
           sig_Gred(j),sig_D51(j)]
    colvis=colvec([1,2,3,4,9])      ;g,r,i,z,d51
    sgv=where(colvis lt 40,nsgv)    ; nsgv = number of valid visible-light mags
    if(colvec(5) lt 40 and colvec(5) gt 0) then irgood=1 else irgood=0
    sbad=where(colvec lt 5 or colvec gt 25 or errvec gt 9.,nsbad)
    flags=intarr(4)
;   if(nsbad gt 0) then guessvals,colvec,errvec,flags,sbad
    if(nsbad gt 0) then guessvals1,colvec,errvec,f1,f2,gmrs,rmis,flags,sbad

    kmag=kepler_mag(colvec(1),colvec(2),colvec(3))
    rmag=colvec(2)

; test to see if photometry is complete enough to justify running find_prop1
; insists on at least 3 visible-light mags, or valid 2MASS data
    if(nsgv ge 3 or irgood ne 0) then begin
      find_prop1,colvec,errvec,rmag,gb(j),$
       teff,logz,logg,logl,ext,props,properr,chmin,rats,xform=xform,flg=flg,$
       bias=bias
      stteff(j)=props(0)
      stlogz(j)=props(1)
      stlogg(j)=props(2)
      stext(j)=props(3)
      errteff(j)=properr(0)
      errlogz(j)=properr(1) 
      errlogg(j)=properr(2)
      errext(j)=properr(3)
      kepmag(j)=kmag
      stflags(j,*)=reform(flags,1,4)
      chisq(j)=chmin
      ratios(*,j)=rats
;   stradius(j)=star_radius(stteff(j),stext(j),kepmag(j),ra(j),dec(j))
      logr=(logl - 4.*alog10((teff > 1.)/tsolar))/2.
      stradius(j)=10.^logr
    endif else begin
      stteff(j)=0.
      stlogz(j)=9.99
      stlogg(j)=9.99
      stext(j)=9.99
      errteff(j)=10.^9.99
      errlogz(j)=9.99
      errlogg(j)=9.99
      errext(j)=9.99
      kepmag(j)=kmag
      stflags(j,*)=reform([1,1,1,1],1,4)
      chisq(j)=999.9
      ratios(*,j)=reform([9.99,9.99,9.99,9.99],1,4)
      stradius(j)=10.^9.99
    endelse
    if(j mod 500 eq 0) then print,'j=',j
  endfor

; redo the radius estimate based on the reddening-based luminosity estimate
; lum_r,kepmag,stext,stteff,ra,dec,lumr
; stradius=sqrt(lumr > 0.)*(tsolar/stteff)^2 
; stradius=float(stradius)

;stop

; put results into properties database.  Create them from scratch every time.
  pname=proppath+tnames(itile)
  comm='cp '+zdbase+'/properties2.dbd '+pname+'.dbd'
; stop
  spawn,comm
  dbcreate,pname,1,1,/silent
  dbopen,pname,1
  dbbuild,sid,ra,dec,stteff,errteff,stlogz,errlogz,stlogg,errlogg,$
           stext,errext,stradius,kepmag,stflags(*,0),stflags(*,1),stflags(*,2),$
           stflags(*,3),chisq
  dbopen,pname,1
  dbindex
  dbclose
; stop
endfor

end