	PRO SIM_STUDY, UNIT, INPUT, OBS, HEADERS_ONLY=HEADERS_ONLY
;+
; Project     :	SOHO - CDS
;
; Name        :	SIM_STUDY
;
; Purpose     :	Simulates CDS telemetry for a study
;
; Category    :	Telemetry, CDS
;
; Explanation :	This routine simulates the telemetry that one would expect from
;		a CDS study, given the planning parameters.
;
; Syntax      :	SIM_STUDY, UNIT, INPUT
;
; Examples    :	
;
; Inputs      :	UNIT	= Logical unit number of file opened for write.  The
;			  telemetry packets are written into this file.
;		INPUT	= A structure variable containing the following
;			  parameters:
;
;				TIME     = Start time to use for first packet,
;					   in TAI format.  This value will be
;					   updated to reflect the times for the
;					   different packets.
;				COUNTER	 = Initial value of science data packet
;					   counter.  This number will be
;					   incremented for each packet written
;					   out.
;				SER_ID   = Series ID number
;				STUDYCNT = Study counter.  This number will be
;					   incremented by one each time this
;					   routine is called.
;
;		OBS	= The planning information for this study, in the
;			  format as given by GET_DETAIL.
;
; Opt. Inputs :	None.
;
; Outputs     :	Certain parameters in the INPUT structure are updated by this
;		routine.
;
; Opt. Outputs:	None.
;
; Keywords    :	HEADERS_ONLY = If set, then only the raster and exposure
;			       headers are written out.  No dummy data is
;			       generated.
;
; Calls       :	
;
; Common      :	None.
;
; Restrictions:	The routine TM_DEFINE_STRUC must be called first to define all
;		the structures.
;
;		The output file must already be open for write access.
;
;		VDS readout orientation bit not yet accommodated.
;
; Side effects:	None.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 21-Aug-1995, William Thompson, GSFC.
;
; Contact     :	WTHOMPSON
;-
;
	ON_ERROR, 2
;
;  Check the number of parameters.
;	
	IF N_PARAMS() NE 3 THEN MESSAGE, 'Syntax:  SIM_STUDY, UNIT, INPUT, OBS'
;
;  Get the study definition from the ID and variation numbers.
;
	GET_STUDY, OBS.STUDY_ID, OBS.STUDYVAR, STUDY
	IF STUDY.STUDY_ID LE -1 THEN MESSAGE, 'Study not found'
;
;  Format the input structure for each raster.
;
	RAS_INPUT = {TIME: INPUT.TIME,		$
		COUNTER: INPUT.COUNTER,		$
		SER_ID: INPUT.SER_ID,		$
		RAS_ID: 0,			$
		RAS_VAR: 0,			$
		SOLAR_X: 0.0,			$
		SOLAR_Y: 0.0,			$
		STUDYCNT: INPUT.STUDYCNT,	$
		RAS_CNT: 0}
;
;  Step through the number of repeats of the study.
;
	FOR I_REPEAT_S = 0, OBS.N_REPEAT_S-1 DO BEGIN
;
;  Step through the number of pointings in the study.  Set the raster counter
;  to zero.
;
	    N_POINTINGS = N_ELEMENTS(OBS.POINTINGS)
	    FOR I_POINTING = 0, N_POINTINGS-1 DO BEGIN
		POINTING = OBS.POINTINGS(I_POINTING)
		RAS_INPUT.RAS_CNT = 0
;
;  Step through each raster in the study.  Set the raster ID and variation
;  numbers.
;
		N_RASTERS = N_ELEMENTS(STUDY.RASTERS)
		FOR I_RASTER = 0, N_RASTERS-1 DO BEGIN
			RASTER = STUDY.RASTERS(I_RASTER)
			RAS_INPUT.RAS_ID  = RASTER.RAS_ID
			RAS_INPUT.RAS_VAR = RASTER.RAS_VAR
;
;  Determine the pointing of the raster.  It will either be
;
;	-1:  Relative to first raster
;	 0:  Embedded within study definition
;	 1:  In the OBS structure.
;
			CASE RASTER.POINTING OF
				-1:  BEGIN
					RAS_INPUT.SOLAR_X = SOLAR_X0
					RAS_INPUT.SOLAR_Y = SOLAR_Y0
					END
				 0:  BEGIN
					RAS_INPUT.SOLAR_X = RASTER.INS_X
					RAS_INPUT.SOLAR_Y = RASTER.INS_Y
					END
				 1:  BEGIN
					RAS_INPUT.SOLAR_X = POINTING.INS_X
					RAS_INPUT.SOLAR_Y = POINTING.INS_Y
					END
			ENDCASE
;
;  If this is the first raster, then save it for later use.
;
			IF I_RASTER EQ 0 THEN BEGIN
				SOLAR_X0 = RAS_INPUT.SOLAR_X
				SOLAR_Y0 = RAS_INPUT.SOLAR_Y
			ENDIF
;
;  Determine how many times to repeat this raster.  For anything other than the
;  last raster in the definition, it's embedded within the definition.
;  However, the number of repeats for the last raster is given within the OBS
;  structure.
;
			N_REPEAT_R = RASTER.N_REPEAT_R
			IF I_RASTER EQ (N_RASTERS-1) THEN	$
				N_REPEAT_R = OBS.N_RASTERS1
;
;  Generate the output telemetry.
;
			FOR I_REPEAT_R = 0, N_REPEAT_R-1 DO	$
				SIM_RASTER, UNIT, RAS_INPUT,	$
					HEADERS_ONLY=HEADERS_ONLY
;
		ENDFOR		;I_RASTER
	    ENDFOR		;I_POINTING
	ENDFOR			;I_REPEAT_S
;
;  Update the INPUT structure.
;
	INPUT.TIME = RAS_INPUT.TIME
	INPUT.COUNTER = RAS_INPUT.COUNTER
	INPUT.STUDYCNT = INPUT.STUDYCNT + 1
;
	RETURN
	END
