pro guessvals1,colvec,errvec,f1,f2,gmrs,rmis,flags,sbad
; this routine uses the valid data in colvec (where index array sbad
; defines the positions of the bad data) to estimate plausible values
; for the missing data.  Errors for data interpolated in this fashion are
; set to a very large value.
; The components of integer array flags are set to reflect which data
; are missing.
;
; On input, arrays f1, f2 and vectors grms, rmis are the result of running
; the fit_colors program, with results saved in ~/d/stardat/guessvals_in.sav.
;
; colvec contains the observed magnitudes in this order:
;  {u,g,r,i,z,J,H,K,Gred,D51}

; constants
bigerr=99.9                      ; error for missing data
di=[1,2,3,4,5,6,7,9]             ; do only these indices -- not u and Gred

ncol=n_elements(colvec)
good=lonarr(ncol)+1.
good(sbad)=0
sgood=where(good eq 1)

; deal with common cases, in which either (g,r) or (r,i) are known
; Prefer to base judgement on r-i, since this gives smaller errors on avg.
cv0=colvec                         ; for debugging
if(good(2) ne 0 and good(3) ne 0) then begin  ; r-i is defined
  cv=fltarr(10)
  for i=0,7 do begin
    cv(di(i))=colvec(2)+interpol(f2(*,i),rmis,(colvec(2)-colvec(3)))
  endfor
  colvec(sbad)=cv(sbad)
  goto,fini
endif
if(good(1) ne 0 and good(2) ne 0) then begin  ; g-r is defined
  cv=fltarr(10)
  for i=0,7 do begin
    cv(di(i))=colvec(2)+interpol(f1(*,i),gmrs,(colvec(1)-colvec(2)))
  endfor
  colvec(sbad)=cv(sbad)
  goto,fini
endif

; if previous things fail, set everything to same mag as 1st good value,
; barring u
if(max(sgood) gt 0) then begin
  sg1=where(good(1:*) eq 1)
  colvec(sbad)=colvec(min(sg1)+1)
endif else begin
  colvec(sbad)=colvec(0)-1.        ; If must use u, subtract 1 from all other mags
endelse

fini:

; set flags
flags=intarr(4)
if(good(0) eq 0) then flags(0)=1
if(good(5) eq 0 or good(6) eq 0 or good(7) eq 0) then flags(1)=1
if(good(4) eq 0) then flags(2)=1
if(good(1) eq 0 or good(2) eq 0 or good(3) eq 0 or good(8) eq 0 or good(9) $
    eq 0) then flags(3)=1

end