pro basel_index1,teffran,loggran,logzran,fteff,flogg,flogz,$
colorfile,indexfile,indx,teffu,loggu,logzu,newt=newt
; This routine restores the contents of colorfile, containing model
; parameters teff, logg, logz and colors for each model.
; Input arrays teffran[2], loggran[2], logzran[2] contain min and max
; allowable values for these parameters.  All model colors within the
; designated ranges are retained,  except that all models with teff, logg, or
; logz values equal to forbidden ones listed in fteff, flogg, flogz are excised.
; An ordered list is made of all
; unique remaining values of teff, logg, and logz.  These parameter lists
; are placed in arrays teffu, loggu, logzu.  Then an index file
; is created:
;  long indx(nt,nlg,lnz), containing one entry for each possible
; combination of {teff, logg, logz}:
; [i,j,k]: index into colorfile corresponding to {teff=teffu(i),logg=loggu(j),
;   logz=logzu(k).  If no such model exists, contains -1.
;
; The indx array and teffu,loggu,logzu are written as an idl save file to
; indexfile, and all are returned by the routine for immediate use.
; If keyword newt is set, its value is an array of teff values that will be included
; in the possible teffs, assuming they fall within teffran.

; restore the data
restore,colorfile

; strip out data not within designated cube
ss=where(teff ge teffran(0) and teff le teffran(1) and $
         logg ge loggran(0) and logg le loggran(1) and $
         logz ge logzran(0) and logz le logzran(1),nss)
if(nss gt 0) then begin
; colors=colors(*,ss)
  teff1=teff(ss)
  logg1=logg(ss)
  logz1=logz(ss)
  ext1=ext(ss)
  rv1=rv(ss)
endif

; ########### new stuff #############
; strip out forbidden values of teff, logg, and logz
nft=n_elements(fteff)
nflg=n_elements(flogg)
nflz=n_elements(flogz)
igood=intarr(nss)+1
for i=0,nft-1 do begin
  s=where(teff1 eq fteff(i),ns)
  if(ns gt 0) then igood(s)=0
endfor
for i=0,nflg-1 do begin
  s=where(logg1 eq flogg(i),ns)
  if(ns gt 0) then igood(s)=0
endfor
for i=0,nflz-1 do begin
  s=where(logz1 eq flogz(i),ns)
  if(ns gt 0) then igood(s)=0
endfor
ss=where(igood eq 1,nss)
if(nss gt 0) then begin
  teff1=teff1(ss)
  logg1=logg1(ss)
  logz1=logz1(ss)
  ext1=ext1(ss)
  rv1=rv1(ss)
endif

;######### end new stuff ##########

; add teff values, if keyword is set
if(keyword_set(newt)) then teff1=[teff1,newt]

; make ordered lists of unique parameters, strip out those outside of ranges
so=sort(teff1)
teffs=teff1(so)
uu=uniq(teffs)
teffu=teffs(uu)
s=where(teffu ge teffran(0) and teffu le teffran(1),nt)
if(nt gt 0) then teffu=teffu(s)

so=sort(logg1)
loggs=logg1(so)
uu=uniq(loggs)
loggu=loggs(uu)
s=where(loggu ge loggran(0) and loggu le loggran(1),nlg)
if(nlg gt 0) then loggu=loggu(s)

so=sort(logz1)
logzs=logz1(so)
uu=uniq(logzs)
logzu=logzs(uu)
s=where(logzu ge logzran(0) and logzu le logzran(1),nlz)
if(nlz gt 0) then logzu=logzu(s)

; make index file
indx=lonarr(nt,nlg,nlz)-1L

; fill it up
for i=0,nt-1 do begin
  for j=0,nlg-1 do begin
     for k=0,nlz-1 do begin
       s=where(teff eq teffu(i) and logg eq loggu(j) and logz eq logzu(k),ns)
       if(ns eq 1) then begin
         s=s(0)
	 indx(i,j,k)=s
       endif
      endfor
    endfor
  endfor

save,teffu,loggu,logzu,indx,file=indexfile

end