pro new_std_field,rac,decc,zdb=zdb
; This routine creates a new standard field centered at rac, dec,
; using data already existing in the database.
; On input, rac and decc are in decimal degrees.
; Stars found within 1/2 field width of the nominal center position,
; having g magnitudes 14.5 <= r <= 16.5 are moved from their 'object'
; star and observation databases, and placed in the standards databases.
; If keyword zdb is set, its value overrides the default value of zdbase.

; constants
zdbase=getenv('ZDBASE')
if(keyword_set(zdb)) then zdbase=zdb
blkdbase=zdbase+'/survey/blocks/blocks'
obspath=zdbase+'/survey/observations/'
starpath=zdbase+'/survey/stars/'
filtdir=['u','g','r','i','z','gr','d5']
nfilt=n_elements(filtdir)
filtnam=['u        ','g        ','r        ','i        ','z        ',$
   'Gred     ','D51      ']
filtstar=['u','g','r','i','z','Gred','D51']   ; filter names in stars dbase
fwidh=11.5/60.           ; field half-width, with a little slop
radian=57.295            ; degrees per radian
f1='(f8.3)'
f2='(f8.4)'
astrolib
!priv=3

; list tiles within 1/2 field width of specified center
decp=decc+fwidh
decm=decc-fwidh
dra=fwidh/cos(decc/radian)
racp=rac+dra
racm=rac-dra
raccorner=[racm,racm,racp,racp]
deccorner=[decm,decp,decm,decp]
tilenames,0,4,raccorner,deccorner,tnames
tilenames,0,4,raccorner,deccorner,tonames,/obj

; find all entries in 'stars' db in these tiles, within 1/2 wid of center
; and within brightness bounds in r magnitude
for i=0,3 do begin

; make string describing desired coords
  rastr=strtrim(string(rac/15,format=f2),2)+'('+strtrim(string(dra/15,$
        format=f2),2)+')'
  destr=strtrim(string(decc,format=f1),2)+'('+strtrim(string(fwidh,$
        format=f1),2)+')'
 
; check to see if star dbase exists
  stardbase=starpath+tnames(i)
  ierrf=findfile(stardbase+'.dbd')
  berrf=byte(ierrf(0))
  if(berrf(0) ne 0) then begin
    dbopen,stardbase,1
;   tstring='ra='+rastr+',dec='+destr+',14.5 < r < 16.5'
; special case for M35 and M3 standard fields, to avoid odd strings of stars
; along RA, Dec axes.  Not completely effective for M35.
    tstring='ra='+rastr+',dec='+destr+',14.5 < r < 15.7'
    sgood=dbfind(tstring, /silent)
    nst=n_elements(sgood)

; get entries for these stars, delete them from current tile dbase
    if(min(sgood) gt 0) then begin 
      dbext,sgood,'starid,ra,dec,u,sig_u,g,sig_g,r,sig_r,i,sig_i',$
        starid,ra,dec,u,sig_u,g,sig_g,r,sig_r,ii,sig_i
      dbext,sgood,'z,sig_z,d51,sig_d51,gred,sig_gred,j,h,k,id2mass',$
        z,sig_z,d51,sig_d51,gred,sig_gred,js,hs,ks,id2mass
      dbext,sgood,'var,crwd,jd,source,dra,ddec',var,crwd,jd,source,dras,ddecs
      dbdelete,sgood
      dbclose

; copy them into 'tstds' database
      stardbase=starpath+'tstds'
      dbopen,stardbase,1
      dbbuild,starid,ra,dec,dras,ddecs,u,sig_u,g,sig_g,r,sig_r,ii,sig_i,z,$
        sig_z,d51,sig_d51,gred,sig_gred,js,hs,ks,id2mass,var,crwd,jd,source,$
        /silent

; sort entries in modified db file
      dbopen,starpath+'tstds'
      so=dbsort(-1,'ra,dec')
      dbext,so,'starid,ra,dec,u,sig_u,g,sig_g,r,sig_r,i,sig_i',$
        sid,ra,dec,u,sig_u,g,sig_g,r,sig_r,ii,sig_i
      dbext,so,'z,sig_z,d51,sig_d51,gred,sig_gred,j,h,k,id2mass',$
        z,sig_z,d51,sig_d51,gred,sig_gred,js,hs,ks,id2mass
      dbext,so,'var,crwd,jd,source,dra,ddec',var,crwd,jd,source,dras,ddecs

      dbopen,starpath+'tstds',1
      dbupdate,-1,'starid,ra,dec,u,sig_u,g,sig_g,r,sig_r,i,sig_i',$
        sid,ra,dec,u,sig_u,g,sig_g,r,sig_r,ii,sig_i,/silent
      dbupdate,-1,'z,sig_z,d51,sig_d51,gred,sig_gred,j,h,k,id2mass',$
        z,sig_z,d51,sig_d51,gred,sig_gred,js,hs,ks,id2mass,/silent
      dbupdate,-1,'var,crwd,jd,source,dra,ddec',var,crwd,jd,source,dras,ddecs,$
        /silent
      dbindex
      dbclose

; find all observations relating to these stars
; loop over filters
      for j=0,nfilt-1 do begin
       print,'tile, filter =',i,j
;       print,'%%%%%%%%'
        obsdbase=obspath+filtdir(j)+'/'+tonames(i)
        ierr=findfile(obsdbase+'.dbh')
        berr=byte(ierr)
        if(berr(0) ne 0) then begin       ; do the rest only if db file exists
        dbopen,obsdbase,1
        sc=dbget('starid',starid, /silent)

; get these observations from appropriate tostds tile
        if(sc(0) eq 0) then print,'no dat filt=',filtdir(j)
        if(sc(0) gt 0) then begin
        dbext,sc,'starid,ra,dec,iseq,filter,mag,err,x,chip',tsid,tra,tdec,$
          tiseq,tfilter,tmag,terr,tx,tchip
        dbext,sc,'xcen,ycen,sky,sharp,chi,dra,ddec',txcen,tycen,tsky,tsharp,$
          tchi,tdra,tddec
; remove them from their original tile databases.
        dbdelete,sc
        dbclose
        
; copy them to tostds dbase in same directory.  Create file if it doesn't exist
        obsstddbase=obspath+filtdir(j)+'/tostds'
        ierr=findfile(obsstddbase+'.dbh')
        berr=byte(ierr)
        if(berr(0) eq 0) then begin
          comm='cp '+zdbase+'/observ.dbd '+obsstddbase+'.dbd'
          spawn,comm
          dbcreate,obsstddbase,1,1, silent
        endif
        dbopen,obsstddbase,1
        dbbuild,tsid,tra,tdec,tiseq,tfilter,tmag,terr,tx,tchip,txcen,tycen,$
          tsky,tsharp,tchi,tdra,tddec,/silent
        dbopen,obsstddbase,1
        so=dbsort(-1,'ra,dec')
        dbext,so,'starid,ra,dec,iseq,filter,mag,err,x,chip',v1,v2,v3,v4,v5,v6,$
              v7,v8,v9
        dbext,so,'xcen,ycen,sky,sharp,chi,dra,ddec',w1,w2,w3,w4,w5,w6,w7
        dbopen,obsstddbase,1
        dbupdate,-1,'starid,ra,dec,iseq,filter,mag,err,x,chip',v1,v2,v3,v4,v5,$
              v6,v7,v8,v9,/silent
        dbupdate,-1,'xcen,ycen,sky,sharp,chi,dra,ddec',w1,w2,w3,w4,w5,w6,w7,$
          /silent
        dbindex
        dbclose
        endif
        endif
      endfor
    endif
  endif
endfor

end