# File saotdc/archive/Scripts/emrange.cl
# June 9, 1994
# By Doug Mink, Harvard-Smithsonian Center for Astrophysics

# EMRANGE -- Compute correlation and emission line velocities for
#            a range of reduced file numbers using RVSAO package

procedure emrange (rfn1,rfn2)

int	rfn1=1		{prompt="First RFN"}
int	rfn2=1		{prompt="Last RFN"}
bool	emis_vel=no	{prompt="Compute emission line velocities (y or n)"}
bool	corr_vel=no	{prompt="Compute cross correlation velocities (y or n)"}
int	offstart=1	{prompt="First RFN with dispersion offset of 1 pixel"}
bool	save_arch=no	{prompt="Save result in TDC archive (y or n)"}
bool	plot=yes	{prompt="Plot results on display (y or n)"}
bool	hard_copy=no	{prompt="Make printer hard copies (y or n)"}
bool	cursor=no	{prompt="Wait after plot with cursor (y or n)"}
bool	delfile=yes	{prompt="Delete IRAF copy of archive file (y or n)"}
bool	verbose=no	{prompt="Print what's happening (y or n)"}
bool	debug=no	{prompt="Debugging mode (y or n)"}

begin

bool vb, delf, savarc, arctest, corvel,emvel,plres,hcres
bool curwait
int rfn, nrfn, firstrfn, lastrfn, npix
file rfnlist, rfnfile, iraffile, irafhead, arcdir, arcrec, irafpix
file rootdir
string inst, arg, arg1, rfn_temp

	vb = verbose
	delf = delfile
	savarc = save_arch
	arctest = debug
	corvel = corr_vel
	emvel = emis_vel
	plres = plot
	hcres = hard_copy
	curwait = cursor
	firstrfn = rfn1
	lastrfn = rfn2
	
	inst = rarce.instrument
	arcdir = rarce.arc_dir
	rootdir = rarce.root_dir

#  Set up parameters in archive writing, graphic display, and hard copies

	if (corvel) {
	    rarce.dispcor = yes
	    rarce.log = yes
	    xcsao.archive=savarc
	    if (emvel)
		xcsao.save_vel=yes
	    xcsao.displot = plres
	    xcsao.hardcopy = hcres
	    xcsao.curmode = curwait
	    }

	if (emvel) {
	    rarce.dispcor = yes
	    rarce.log = no
	    emsao.archive = savarc
	    emsao.displot = plres
	    emsao.hardcopy = hcres
	    emsao.curmode = curwait
	    }

	set clobber = yes

	rfnobj.root_dir = rootdir
	rfnobj.instrument = inst
	rfnobj.arc_dir = arcdir
	rfnobj.verbose = no

#  Loop through range of reduced file numbers
	for (rfn=firstrfn; rfn <= lastrfn; rfn=rfn+1) {
	    rfn_temp = rfn
	    rfnobj (rfn_temp)
	    iraffile = rfnobj.arcfile

	# If reduced file is found, read it into IRAF file and process it
	    if (iraffile != "") {
		if (!rarce.keep_name)
		    iraffile = rfn_temp
		if (vb)
		    print (rfn,rfn_temp," ",iraffile)
		arg = "-inst " // inst // " " // rfn_temp
		irafhead = iraffile // ".imh"
		if (!access (irafhead)) {
		    if (rfn < offstart)
			rarce (rfn_temp, dispoff=0)
		    else
			rarce (rfn_temp, dispoff=1)
		    if (!access (irafhead)) {
			print ("EMRANGE: No IRAF header file ",irafhead)
			next
			}
		    demach (iraffile)
		    }
		irafpix = iraffile // ".pix"
		if (!access (irafpix)) {
		    print ("EMRANGE: No IRAF pixel file ",irafpix)
		    if (access (irafhead))
			imdel (irafhead)
		    next
		    }
		imgetp (irafhead,"i_naxis1")
		npix = int (imgetp.value)
		if (npix < 1) {
		    imdel (irafhead)
		    next
		    }

	# Find cross-correlation velocity
		if (corvel)
		    xcsao (iraffile)

	# Find emission line velocity
		if (emvel)
		    emsao (iraffile)
 
	# Save cross-correlation results to archive file
		if (savarc) {
		    if (corvel && emvel) {
			arg1 = " ndelrec=3 delrec002=CORRELATION delrec003=EMISSION_LINES -stop"
			cspec (arg1)
			arg1 = " naddrec=3 addrec002=corr addrec003=emlines -stop"
			cspec (arg1)
			}
		    else if (corvel) {
			arg1 = "ndelrec=2 delrec002=CORRELATION -stop"
			cspec (arg1)
			arg1 = "naddrec=2 addrec002=corr -stop"
			cspec (arg1)
			}
		    else if (emvel) {
			arg1 = "ndelrec=2 delrec002=EMISSION_LINES -stop"
			cspec (arg1)
			arg1 = "naddrec=2 addrec002=emlines -stop"
			cspec (arg1)
			}
		    cspec (arg)
		    }

#	Delete temporary archive record files
		if (delf && (xcsao.archive || emsao.archive)) {
		    arcrec = iraffile // ".ansum"
		    if (access (arcrec))
			delete (arcrec)
		    }
		if (delf && xcsao.archive) {
		    arcrec = iraffile // ".corr"
		    if (access (arcrec))
			delete (arcrec)
		    }
		if (delf && emsao.archive) {
		    arcrec = iraffile // ".emlines"
		    if (access (arcrec))
			delete (arcrec)
		    }

#	Delete IRAF file
		if (delf)
		    imdelete (iraffile)
		if (hcres)
		    gflush
		}

	# Print error message if file is not found
	    else if (vb)
		print ("IRAF file for RFN ",rfn," not found")
	    }
end