Viewing contents of file '../idllib/uit/pro/bdrastrom.pro'
pro bdrastrom,h,nocompress = nocompress,old=old
;+
;  NAME
;	BDRDASTROM
;  PURPOSE:
;	Add astrometry from the HDRS database into a UIT FITS header.
;
;  CALLING SEQUENCE:
;	bdrastrom ,h [,/Nocompress,/OLD]
;
;  INPUT - OUTPUT:
;	h - UIT FITS header, string array.   It can be for either a compressed 
;		or full-resolution UIT image, but must be in the original UIT
;		orientation.   BDRASTROM will not work on subimages or rotated
;		images.
;
;  KEYWORD INPUT:
;	NOCOMPRESS - If present and non-zero, the restored astrometry will 
;		NOT be compressed.
;	OLD - If present and non-zero, HDRSOLD rather than HDRS will be used for
;		FLIGHT12 or QUICK13 images.
;
;  REVISION HISTORY:
;	Written,   W.Landsman         October 1991
;	Added NOCOMPRESS option   J. Offenberg  22 January 92
;	Added OLD option for pre-Flght13  J. Offenberg Jan 29 1992
;	Updated for compatiblity with ASTROMETRY structures and image 
;		distortion.       J. Offenberg Jan 1993
;-
 On_error,2

 if N_params() LT 1 then begin
     print,'Syntax - bdrastrom, h, [/NoCompress, /OLD ]
     return
 endif

 zparcheck,'BDRASTROM',h,1,7,1,'UIT FITS header'

 telescop = strtrim( sxpar(h,'TELESCOP'), 2)
 if ( telescop NE 'UIT' ) then message, $
       'ERROR - Header must be for a UIT image'

 image = sxpar(h,'IMAGE')

  bdrstrem = strupcase(strtrim(sxpar(h,'BDRSTREM')))
  if (( bdrstrem NE 'FLIGHT13') and (bdrstrem NE 'FLIGHT14') $
     and keyword_set(OLD)) then BEGIN
	dbopen,'hdrsold'
	message,'Now searching HDRSOLD database for image',/INF
	list = dbfind( 'image = '+image)
  endIF ELSE BEGIN
	 dbopen,'hdrs'
 	message,'Now searching HDRS database for image ' + image, /INF
 	list = dbfind( 'image = ' + image, /SILENT)

 	if !ERR EQ 0 then begin          ;If not in HDRS, try HDRSOLD
        	dbopen,'hdrsold'
        	list = dbfind('image = '+image)
 	endif
   endELSE

 if !ERR EQ 0 then message,'ERROR - Unable to find image ' + image + $
                           'in HDRS or HDRSOLD database'
 keywords = [ 'CTYPE1', 'CTYPE2', 'CRPIX1', 'CRPIX2', 'CRVAL1', 'CRVAL2', $
      'CD1_1','CD2_1','CD1_2','CD2_2','EQUINOX','FILTER']
 db_item,keywords,it,ivalnum,dtype,sbyte,numvals,nbytes

 nitems = N_elements(it)
 qnames = db_item_info('name',it)
 comment = db_item_info('description',it)
 dbrd,list(0),entry                         ; read an entry.

;
; Extract relevant keywords from the HDRS database, and insert into the
;         FITS header.
;
      for k = 0, nitems-1  do begin
         ;
            sxaddpar,h,qnames(k), $
		dbxval(entry,dtype(k),numvals(k),sbyte(k),nbytes(k)), $
                comment(k), 'HISTORY'
						;display name,value
       endfor   ; k




db_item,'ASTRPROG',it,ivalnum,dtype,sbyte,numvals,nbytes
as  = dbxval(entry,dtype(0),numvals(0),sbyte(0),nbytes(0)) 
if strtrim( as(0),2 ) NE '0' then begin
     comment = db_item_info('DESCRIPTION',it)
     sxaddpar,h,'ASTRPROG',as(0),comment(0),'HISTORY' 
endif
dbclose

; If image is compressed, then we will run HREBIN on the header

IF not(Keyword_set(NOCOMPRESS)) then begin	;Is NOCOMPRESS specified?
      naxis = sxpar(h,'NAXIS*')
      if naxis(0) NE 2048 then begin
          sxaddpar,h,'NAXIS1',2048
          sxaddpar,h,'NAXIS2',2048
          hrebin, h ,OUT = naxis
      endif
endIF
return
end