;+
; NAME:
;	DN_TRANS
;
; PURPOSE:
;	Finds the xray day/night transitions in BATSE housekeeping data
; 	Fills for bad data if needed.
; CATEGORY:
;	BATSE
;
; CALLING SEQUENCE:
;	dn_trans, ut, x_pos,y_pos,z_pos, daymarks, nightmarks
;
; INPUTS:
;	Ut - is in seconds from 79/1/1
;	X_pos, Y_pos, Z_pos - spacecraft position in 0.25 km units, celestial
;	J2000.
;	
;
; OPTIONAL INPUTS:
;	
; KEYWORD PARAMETERS:
;	o_angle - sun_earth_sc angle of day or night transitions, about 110 deg
;	s_angle - vector of angles formed by dotting earth-sun and earth-sc vectors, 
;	sc_new  - array of x(yz)_pos vectors, including estimated fill positions
;	bad_data - set to 1 if there are too few valid positions to calculate the fill
;	    positions
;	d_period - orbital period in seconds calculated from reported x,y,z_pos
;	ntrans - number of transitions through day/night or night/day bound. in ut
;
;	days & nights - times of day/night crossings for 31 orbits centered around
;                ut(0)
;	inradec - optional
;	   2 element vector, ra and dec in degrees of a point source instead of the sun
;
; OUTPUTS:
;	Daymarks:	times of transitions from night to day relative to 79/1/1
;	Nightmarks:	times of transitions from day to night relative to 79/1/1
;	daymarks set to 0 if there are no crossings into day, ras, 91/05/10
;	nightmarks set to 0  if there are no crossings into night
; CALLS:
;	OCCTIME, ORBITFILE
;
; PROCEDURE:
;	Looks for positions of the spacecraft with the Sun just at the horizon.
;	Missing position data is filled.
;
; EXAMPLE:
;	dn_trans, ut, x_pos,y_pos,z_pos, daymarks, nightmarks,$
;	sc_new=sc,s_angle=s_angle,o_angle=o_angle,bad_data=bad_data,$
;        ntrans=ntrans,d_period=d_period
;
; MODIFICATION HISTORY:
;	Modified by RAS, 92/9/15:
;-
pro dn_trans, ut, x_pos,y_pos,z_pos, daymarks, nightmarks,$
	sc_new=sc,s_angle=s_angle,o_angle=o_angle,bad_data=bad_data,$
        ntrans=ntrans,d_period=d_period, days=days, nights=nights, $
	inradec=inradec
;from s/c position array defined by inertial coordinates x_pos,y_pos,z_pos
;find the times of the day and night transitions over the range ut 
;
;		1. pass along XYZ from ORBIT_FILL to OCCTIME
;	        2. DAYS and NIGHTS keywords to be used by ORBIT

	nut=n_elements(ut)
	sc=reform([x_pos(*),y_pos(*),z_pos(*)],nut,3)

;FILL BAD POSITION DATA AND RETURN A MATRIX CONNECTING THE ORBITAL PLANE (XY)
;WITH CELESTIAL COORDINATES (XYZ)

	orbit_fill,ut,sc_new=sc,sc_old=sc_old,d_period=d_period, $
		bad_data=bad_data, xyz_orbit = xyz ;fill data gaps with interpolation on orbital path

	if bad_data eq 1 then return ;data is too bad to use

;
;GET THE TIMES OF THE DAY AND NIGHT TRANSITIONS
;
	occtime,spacecraft=sc,utime=ut,daymarks=days, nightmarks=nights,$
		s_angle=s_angle,o_angle=o_angle,ntrans=ntrans, inradec=inradec, $
		xyz_orbit = xyz, d_period=d_period, bad_data=bad_data

	if bad_data eq 1 then return ;data is too bad to use
        

;SELECT ONLY THOSE TRANSITIONS WITHIN TIME COVERED BY UT
;
	w_ut = where( (days ge ut(0)-1.024) and (days le ut(nut-1)+1.024),nd)
	if nd ge 1 then daymarks = days(w_ut) else daymarks = 0

	w_ut = where( (nights ge ut(0)-1.024) and $
		(nights le ut(nut-1)+1.024),nn)
	if nn ge 1 then nightmarks = nights(w_ut) else nightmarks = 0

	ntrans = nn+nd  ;TOTAL DAY/NIGHT TRANSITIONS IN ut

return
end
