pro get_sage_jd, file, ra, dec, timestamp, PRINT = print
;+
; NAME:
;       GET_SAGE_JD
;
; PURPOSE:
;       Get a timestamp from a SAGE timestamp fits image.
;
; EXPLANATION: 
;       Baseline Julian Date offset is 2453500.5 as indicated
;       in SAGE timestamp image fits files.  SAGE timestamp images
;       are in minutes past this date.  Timestamps are defined
;       by the observation time of the individual observed frames.
;
; CALLING SEQUENCE:
;       GET_SAGE_JD, FILE, A, D, TIMESTAMP [ /PRINT ]
;
; INPUTS:
;       FILE - FITS Image file for SAGE timestamp info
;       RA - RA coordinates in decimal degrees, scalar or vector
;       DEC - Dec coordinates in decimal degrees, scalar or vector
;       TIMESTAMP - scalar or vector that will receive the timestamp values
;    
;       Inputs are double precision.
;
; OPTIONAL KEYWORD INPUT:
;       /PRINT - If this keyword is set and non-zero, then results are displayed
;               at the terminal
;
; PROCEDURES CALLED
;       READFITS(), ADXY
;
; EXAMPLE:
;      Find the earliest observation time for SAGE EPOCH1 channel 1 data
;       at RA and Dec (83.6,-72.25).
;        IDL> get_sage_jd, 'sage1.b1.begintime.frms.fits',83.6D,-72.25D,begintime,/PRINT
;        Julian Date: 2453575.026 at position (  83.600000, -72.250000)
;        
;      Find the earliest observation time for SAGE EPOCH1 channel 1 data
;       at these 5 positions; (83.6,-72.25), (82.7,-71.35), (81.8,-70.45), (80.9,-69.55), (79.0,-68.65)
;       and print the results to the screen.
;        IDL> ra=[83.6D,82.7D,81.8D,80.9D,79.0D]
;        IDL> dec=[-72.25D,-71.35D,-70.45D,-69.55D,-68.65D]
;        IDL> get_sage_jd, 'sage1.b1.begintime.frms.fits',ra,dec,begintime,/PRINT
;             Julian Date: 2453575.026 at position (  83.600000, -72.250000)
;             Julian Date: 2453575.106 at position (  82.700000, -71.350000)
;             Julian Date: 2453573.981 at position (  81.800000, -70.450000)
;             Julian Date: 2453574.054 at position (  80.900000, -69.550000)
;             Julian Date: 2453572.836 at position (  79.000000, -68.650000)
;
;-

npar = N_params()
if ( npar LT 4 ) then begin
        print,'Syntax -  GET_SAGE_JD, file, ra, dec, timestamp, /PRINT'
        print,'file - FITS file containing SAGE timestamp image'
        print,'RA,Dec - Input RA and Dec in decimal degrees, (scalar or vector)'
        print,'timestamp - Timestamp values for the input RA and Dec, (scalar or vector)'
        print,'/PRINT - print output to terminal.
        return
endif                                                         

num_ra=n_elements(ra)
num_dec=n_elements(dec)
if num_ra ne num_dec then begin
 print,'RA and Dec must have the same number of elements.'
 return
endif
timestamp=dblarr(num_ra)

print,'reading in fits file ',file
im=readfits(file,hdr)
adxy,hdr,ra,dec,x,y
x=x+0.5001
y=y+0.5001
mjd_obs_mins=double(im[x,y])
for i=0L,num_ra-1 do begin
 if mjd_obs_mins[i] gt 0 then begin
   mjd_obs_mins[i]=mjd_obs_mins[i]/60.0D/24.0D
   timestamp[i]=2453500.5D
   timestamp[i]=timestamp[i]+mjd_obs_mins[i]
 endif
endfor
 
if keyword_set(PRINT) then begin
 for i=0L,num_ra-1 do begin
   print,'$("Julian Date: ",d11.3," at position (",d11.6,",",d11.6,")")',timestamp[i],ra[i],dec[i]
 endfor
endif

return
end
