Viewing contents of file '../idllib/ghrs/pro/calhrs_ord.pro'
pro calhrs_ord,tname,gmode,aperture,cpos,ydefs,orders
;
;+
;			calhrs_ord
;
; Routine to compute spectral orders from the carrousel position and
; y-deflection using the carrousel calibration coefficients.
;
; CALLING SEQENCE:
;	calhrs_ord,tname,gmode,aperture,cpos,ydefs,orders
;
; INPUTS:
;	tname - name of the carrousel calibration table
;	gmode - grating mode 'G-1',...'E-A'
;	aperture - 'LSA' 'SSA' 'SC1' or 'SC2'
;	cpos - vector of carrousel positions (same length as ydefs)
;	ydefs - vector of y-deflections
;
; OUTPUTS:
;	orders - spectral orders
;
; METHOD:
;	If it is a first order grating the carrousel positions are
;	set to all 1. For echelle modes the central wavelength is
;	computed by:
;
;				    b*A*sin((C-cpos)/B)
;		order = FIX ( -------------------------------- + 0.5)
;				y - a - d*A*sin((C-cpos)/B)
;
;	where cpos is the carrousel position
;	      y is the y-deflection adjusted for the given aperture
;	      a,b,d,A,B,C are table coefficients which vary with
;			grating
;-
;------------------------------------------------------------------------
;
; if first order grating then set orders to all 1
;
	if strmid(gmode,0,1) eq 'G' then begin
		orders=replicate(1,n_elements(ydefs))
		return
	endif
;
; read table parameters for the correct grating mode
;
	tab_read,strtrim(tname,2),tcb,table
	gmodes=tab_val(tcb,table,'GRATING')
	for i=0,n_elements(gmodes)-1 do $	find last row for grating
		if strtrim(gmodes(i),2) eq gmode then irow=i
	if n_elements(irow) lt 1 then begin
		print,'CALHRS_ORD--no valid row for grating '+gmode+ $
			'found in table '+strtrim(tname,2)
		retall
	endif
;
; read coefficients
;
	lita = tab_val(tcb,table,'LIT_A',irow)
	litb = tab_val(tcb,table,'LIT_B',irow)
	litd = tab_val(tcb,table,'LIT_D',irow)
	capa = tab_val(tcb,table,'CAP_A',irow)
	capb = tab_val(tcb,table,'CAP_B',irow)
	capc = tab_val(tcb,table,'CAP_C',irow)
;
; adjust y-deflections for specified aperture
;
	case aperture of
		'LSA': yd=ydefs+128
		'SSA': yd=ydefs
		'SC1': yd=ydefs-128
		'SC2': yd=ydefs
	endcase
;
; compute orders
;
	sinx = sin((capc-cpos)/capb)
	orders=fix((litb*capA*sinx)/(yd-lita-litd*capA*sinx)+0.5)
	return
	end