pro grid_likeli,cobs,sig2,rmag,gb,$
  ltrang,dlt,lgrang,dlg,lzrang,dlz,chi2,lprior,loglum,lto,lgo,lzo,$
  lto1,lgo1,lzo1,xform=xform,bias=bias
; this routine takes observed properties of one star and produces two output
; arrays containing the chi^2 values and the log of the prior probabilities,
; both on a specified 3-dimensional grid in log(teff), log(g), and log(Z).
; On input:
;  cobs(9),sig2(9) = observed colors and corresponding squared uncertainties
;  rmag = r magnitude of star
;  gb = galactic latitude of star (degrees)
;  cblock = 3D array of model colors
;  teffu, loggu, logzu = coordinate vectors on which cblock is defined
;  hrb = HR diagram log prior probability array
;  logtn, logln = coordinate vectors on which hrb is defined
;  ltran(2) = min, max values of log(teff(K)) on which chi^2 is to be evaluated
;  dlt = sampling interval in log(teff)
;  lgrang(2), dlg = ditto for log(g(cm/s^2))
;  lzrang(2), dlz = ditto for log(Z/Z_sun)
; On output:
;  chi2(nt,nlg,nlt) = chi^2 evaluated on the desired grid
;  lprior(nt,nlg,nlz) = log of prior probability on the same grid
;  loglum(nt,nlg,nlz) = log of bolometric luminosity on the same grid
;  lto = log(teff) grid coordinates
;  lgo = log(g) grid coordinates
;  lzo = log(Z) grid coordinates
;  
; If keyword xform is set, then the model colors are transformed according to
; the linear transformation in idl save file xform before they are compared
; to the observations, and reddenings are transformed appropriately also.
; If keyword bias is set, giants are favored over dwarfs a priori by an amount
; proportional to the bias value.

; constants
common tables,logln,logtn,hrb,$
              logtp,loggp,bcp,bmvp,loglump,logmassp,rabsp,vabsp,vmrp,$
              teffu,loggu,logzu,colblock
wt1=0.     ; added weight factor for g-d51
wt2=0.     ; added weight factor for J-K

; make grid coordinate arrays
nt=long((max(ltrang)-min(ltrang))/dlt) + 1
lto=min(ltrang)+dlt*findgen(nt)
tto=10.^lto
nlg=long((max(lgrang)-min(lgrang))/dlg) + 1
lgo=min(lgrang)+dlg*findgen(nlg)
nlz=long((max(lzrang)-min(lzrang))/dlz) + 1
lzo=min(lzrang)+dlz*findgen(nlz)

; make 3D and 1D versions, so each desired model has a unique teff, logg, logz
lto3=rebin(reform(lto,nt,1,1),nt,nlg,nlz)
tto3=rebin(reform(tto,nt,1,1),nt,nlg,nlz)
lgo3=rebin(reform(lgo,1,nlg,1),nt,nlg,nlz)
lzo3=rebin(reform(lzo,1,1,nlz),nt,nlg,nlz)
nmod=long(nt)*nlg*nlz
lto1=reform(lto3,nmod)
tto1=reform(tto3,nmod)
lgo1=reform(lgo3,nmod)
lzo1=reform(lzo3,nmod)

; make model luminosities, distances, extinctions, given rmag and gb
;linked_props,lto1,lgo1,rmag,gb,loglm,logdm,a_vm
linked_props2,lto1,lgo1,rmag,gb,loglm,logdm,a_vm
loglum=reform(loglm,nt,nlg,nlz)

; make model colors, given the other properties
colors_blk_intrp,colblock,teffu,loggu,logzu,tto1,lgo1,lzo1,colsout,$
  xform=xform

; correct these for expected reddening
red_vector,a_vm,dcolor,xform=xform
colsout=colsout+dcolor

; make chi2 array, fill it
; **testing** increase weight of g-d51 and J-K in relevant ranges of g-r,
; g-i
chi2=fltarr(nmod)
wf1=1.+wt1*exp(-((cobs(1)-1.11)^2/0.073) < 30)      ; gaussian width 0.27
gmi=cobs(1)-cobs(2)                           ; g-i color
wf2=1.+wt2*errorf((gmi-1.7)/.15)
cwts=fltarr(9)+1.
cwts(1)=wf1
cwts(5)=wf2
cwts=9.*cwts/total(cwts)
tsig2=sig2
tsig56=sig2(5)+sig2(6)               ; errors for J-K, J+K-2H
tsig2(5:6)=[tsig56,tsig56]
; transform obsd J-H, H-K to J-K, J+K-2H
tcobs=cobs
tcobs(5:6)=[cobs(5)+cobs(6),cobs(5)-cobs(6)]

for i=0L,nmod-1 do begin
  cmod=colsout(*,i)
; transform color model to J-K, J+K-2H
  tcmod=cmod
  tcmod(5:6)=[cmod(5)+cmod(6),cmod(5)-cmod(6)]
  chi2(i)=9.*total(cwts*(tcobs-tcmod)^2/tsig2)
; chi2(i)=total((cobs-cmod)^2/sig2)
endfor

; make prior probability array, fill it
hr_blk_intrp,hrb,logtn,logln,10.^lto1,loglm,lnprob,bias=bias  ; probs in T,L space
prob_dist,logdm,gb,lprobd              ; prob vs distance, also fn of T,L
prob_z,lzo1,lprobz                     ; prob of log(Z)
lprior=lnprob+lprobd+lprobz       ; log of total prior probability
lprior=lprior-max(lprior)         ; normalized for convenience

end