-   It is possible that all of the following steps can be controlled
by the GUI,   hires_redux.  I use it sometime, but it
is a bit buggy.
I'm hopeful that it will be the best
way to run the code someday.  
 -  To run, simply try: 
  IDL>     hires_redux, hires
-  Choose a setup
  
   -  Reduce the flats
   
 -  Reduce the arcs
   
 -  Check the QA plots
  
 
 -  Choose an object
  
   -  Reduce and extract
   
 -  Check the QA plots
   
 -  Check the spectra
  
 
  
 
Create Bias (Zero) frames [NOT recommended and NOT functional]
	
- The default method of bias subtraction is to use the overscan
	region and the bias row.  If the user prefers, one can also subtract
	off a combined bias frame.  This routine creates that combined frame
	from a series of Bias frames. This routine
	will create 1 bias frame per chip per binning mode.  
Alternatively, one may perform this step as a test of the bias
	subtraction algorithm.  The resulting images should have 0 counts
	with no gross structure.
             Example: IDL> hires_mkbias, hires 
             Time   : 3-5min 
 
-  You know the code is working right when you 
  check the Bias frame : IDL> xatv, 'Bias/name.fits'
and it shows a generally blank image with no counts.  Of course, there
is the possibility that you have bad bias frames and the code is still
working fine.
	
 
Set Gain
	
-  The stability of the gain with the new HIRES ccd's is not well known
at this time.  At present, we choose to measure the value by comparing
a series of flats.  
 -  Note that the gain was changed from approximately 1 to 
2 (Low mode) in October 2004.  You should definitely run this
routine if your data is from before then.
 - hires_findgain :: Loops on the Milky Flats and performs statistics (in hires_gain) to determine the gain and apply it to the gain TAG.
             Example: IDL> hires_findgain, hires, setup
             Time   : 3-5min 
 -  This routine is included in hires_allflat, see below.
 -  You know the code is working right when you see it looping
through all of the flat files and reporting gain values within
the ballpark of the published values.  You might also print out the gain
tags after completion:  
 IDL> print, hires.gain
 -  It is highly recommended that you use hires_wrstrct to write
out the structure now.
	
 
Process Flats 
 
 
These routines process the Milky and Trace flats to create a 
	response image (pixel by pixel variations) and to determine the 
	curvature of the orders on the CCD.  These files are essential to
	run the HIRES pipeline.  Note that the next four steps can be run
	together using the routine hires_allflat.
- hires_mktflat :: Combines the series of trace flats (standard flats) to create one high S/N image 
for order and slit tracing.  I highly recommend that one account
for pixel-to-pixel variations with a Pixel flat.  You can grab those
flats off the Web site.
             Example: IDL> hires_mktflat, hires, setup, [chip]
             Time   : 1min  
-  You know the code is working right when you   see it process and 
stack the various trace flat files.  You should then
check the Trace Flat: 
 IDL> xatv, 'Flats/Flat_B_01_T.fits'
 
 
- hires_edgeflat :: 
Routine to trace the fit the order curvature by using the 'trace flat'.  
-  The routine combines bits and pieces of the 2 (obsolete) routines
	      that follow.  The main difference is that the fit is performed
	      on the centroid of each order as determined by an average of
	      the left and right hand sides.
 -          Example: IDL> hires_edgeflat, hires, setup, [chip], /INTER, /CHK
             Time   : 2min 
 -  You know the code is working right when   the curves
look sensible. I have found that the reduced chi^2 might still be large
even when things are working just fine.
You can check the QA plots in (e.g. 'QA/Flats01/qa_trcflt_01B.ps').  
The X0 and PCA0 values should vary significantly but be well fit while 
the PCA1-4 values should have minor variations. 
 -  QA: HERE is an example of the QA. Note:
  -  The first page shows output from the PCA analysis.  The x0 and PCA0
  components should be well fit (there may be red, rejected outliers).
  
  -  PCA1 should be confined to a small range of values 
         (the magnitude is not particularly relevant) but may not.
  
 -  PCA2-4 should each be confined to a small range of values
         (the magnitude is not particularly relevant). 
  
 
   -  The second and third pages show the order edges (black points)
		and the fits to the edges.  
  
    -   Green lines indicate regions used in the PCA analysis
    
 -   Orange lines indicate where orders overlap.  These order edges
		    were extrapolated using the PCA analysis.
    
 -   Light blue lines show partial orders.  These were not included in the
		    PCA analysis.
    
 -   Dark blue lines show extrapolated orders at the edges of the CCD.
  
 
 
 -  You can also check the tracing with
hires_chktrcflat.  Along with 
	xatv, this routine is used to check the results from hires_trcflat.
	     IDL> xatv, 'Flats/Flat_B_01_T.fits' 
	     IDL> hires_chktrcflat, hires, setup, 1, /NOSTOP
 -  This routine has the most trouble with data taken with HIRESr
in a setup with an improper 2nd order blocking filter.  The 'extra'
orders causes troubles.
	 
 
 
- [OPTIONAL and NOT recommended] 
hires_nrmflat 
        :: This routines attempts to normalize the traditional 'trace flats'
	  that one takes with HIRES.  Ideally you will instead use the 'pixel'
	  flats which I have archived at the redux website and skip this step.  
             Example: IDL> hires_nrmflat, hires, setup, [chip]
             Time   : <2min per chip
-  You know the code is working right when   the routine
finishes.  You should inspect the resulting images and consider if the
variations from pixel-to-pixel are sufficiently small. 
 
 
- hires_allflat 
        :: The previous 4 routines have been bundled into
	one simple script.  We recommend using this routine once you are 
	comfortable that the code is working well with your setup, etc.  We
	recommend you 
	run hires_chktrcflat after the procedure is through to examine
	the solutions and also look at the QA.  
             Example: IDL> hires_allflat, hires, setup, [chip, /INTER]
             Time   : <20min per chip
-  You know the code is working right when you go through all of the
checking steps listed above.
 
 
Arc Images 
  
- General :: 
	The arcs are processed through a series of steps which:
	
	  -  Process the raw frame (hires_procarc) 
	  
 -  Derive a 1D solution down the center of each order (hires_fitarc)
	  
 -  Create a 2D solution (hires_fit2darc)
	  
 -  Traces the individual arc lines (hires_tracearc)
	  
 -  Fits the changing slope of the arc lines (hires_fittrcarc)
	  
 -  Create a 2D wavelength image (hires_mkaimg)
	
 
	
 - Recommended: One Shot 
hires_allarc: There are two modes of processing the arcs.  
We strongly recommend against running the arc steps individually unless you 
are debugging.  In fact, it is possible that some procedures will not run
on their own at the present time.  
	-  Process individually.  This is generally only recommended 
	    for redoing specific arcs.  It is also useful for testing the code
            on non-standard setups before running on all of the arcs.  
            The user inputs the indice(s) of the arc(s) to process.
             Example: IDL> hires_allarc, hires, [indx], /INDX
             Time   : 5min per arc
	 -  Process all together.  This is recommended. 
	If you are reducing a full night
	of data, I suggest the latter. 
	    I do highly recommend, however, that you check the 1D
	    solutions and consider tweaking a few orders as need be.
             Example: IDL> hires_allarc, hires, setup, [chip]
             Time   : 5min per arc
 
 
	  
- Individual Procedures 
- hires_procarc :: Process the Arcs.  
	This step bias subtracts and flat fields the arc images.  It chooses
	the arc image closest in UT time to the science exposure.  This routine
	will also calculate the x,y shifts between the arcs being processed
	and a 'template' arc.  The `template' arc should be the arc taken
	most closely in time to the trace flats.  By default, the first Arc
	of the night is taken as the template.  Output
	are fits files in the 'Arcs/' directory.  
             Example: IDL> hires_procarc, hires, setup, obj_id, [chip] 
             Example: IDL> 
		rslt = hires_procarc_sngl('raw.fits', setup, chip)
             Time   : 1min per image
-  You know the code is working right when   it finishes.  It would
be quite unusual for this procedure to fail.
 
 - hires_fitarc :: This routine extracts a 1D spectrum down the
	center of each order and determines the wavelength solution.  
 
  - 
	There are two levels of interaction with this routine.  The most
	interaction (not recommended) is to use /INTER which prompts the
	user to identify and fit the Arc lines.  One can also use the option
	/PINTER which has the program attempt to identify a set of
	lines in each order.  The user than interactively fits the lines
	using the routine x_identify which calls x1dfit.
	As long as your arc lines are within several 
	pixels of my solution, things ought to run smoothly in the non-interactive
	mode.  
 -  At this point, we recommend using the full AUTO mode.  If it fails,
	we would hope to fix it.
	The output is an IDL file containing the arc lines identified and 
	their pixel centroids: 'Arcs/Fits/Arc_name_fit.idl'.  
	The program can also create a ps file to examine the quality of fits.  
             Example: IDL> hires_fitarc, hires, setup, obj_id, 
		[chip, /INTER, /PINTER, /PSOUT]
             Example: IDL> result = hires_fitarc_work('arc_file', 
	setup,  chip) 
             Time   : 5min per chip per arc image
 -  You know the code is working right when   the RMS of the fits to
each order are generally less than 0.1 pixel.  There will be several exceptions
per full exposure.  Also, it is common for the code to fail for the very bluest
orders of the blue chip (at least with the original blue CCD).
The code produces a QA file (e.g. 'QA/Arcs01/qa_arcfit_mb0038.ps.gz')
which shows the residuals to the order by order fits including the RMS
(in pixels).
 -  QA: HERE is an example of the QA. Each
page shows the residuals of the fits to a set of echelle orders.  Red points
indicate arc lines rejected in the fit.  The RMS values are in pixels and 
the Dlambda values are Ang.  If any of the RMS values exceed 
0.1 (and especially 0.2) you may wish to 'tweak' the arcs as described
here.  In the example shown here, I have
a little concern about Order #88, but none about #108.
 
 
- hires_fit2darc :: This routine fits a 2D solution to the
	wavelengths as a function of their y pixel value and their order number.  
	It is rather straightforward. 
             Example: IDL> hires_fit2darc, hires, setup, obj_id, [chip]  
             Example: IDL> result = hires_fit2darc_work('arc_file', 
	  setup, chip) 
             Time   : fast
-  You know the code is working right when   the RMS of the 2D fit
is less than 0.1 pix or so.
The code produces a QA file (e.g. 'QA/Arcs01/qa_arc2dfit_mb0038.ps.gz')
which shows the 2D solution and lists the RMS.
 -  QA: HERE is an example of the QA. The
first page shows the 2D solution and the lines fit.  There isn't too much
to see visually.  I mainly check the RMS and hope to see values of 
0.1 to 0.15 Ang*Order# (strange units).  The remaining pages show the
residuals off the 2D solution.  Again, I hope to see RMS of 0.1pix or less.
If you don't, consider a tweak.  
 
 
- hires_tracearc :: This routine traces arc lines in each
	order.  It traces the lines in the curved order frame of the original
	image and then fits a straight line to each arc line.  The slope
	and centroid of the arc line is recorded to a file for later use. 
             Example: IDL> hires_tracearc, hires, setup, obj_id, [chip]
             Example: IDL> result = hires_tracearc_work('arc_file', setup, 
 	chip) 
             Time   : 2min per chip per arc image
-  You know the code is working right    by reviewing the QA file
it produces (e.g. 'QA/Arcs01/qa_tracearc_mb0034.ps.gz').  So long as the
arc lines and the fits look reasonably straight, everything should be fine.
 -  You can check the output by using  
hires_chkarctrc
             Example: IDL> xatv, arc_img
             Example: IDL> hires_chkarctrc, hires, indx  
 
 
- hires_fittrcarc :: Using the slopes derived in the previous step,
	this routine fits for the slope throughout the 2D arc image.  That is
	the slope as a function of order and vertical height in the image.  Uses
	the usual least-squares algorithm.
             Example: IDL> hires_fittrcarc, hires, setup, 
		obj_id, [chip], /CLOBBER, /CHK 
             Example: IDL> result = hires_fittrcarc_work('arc_file', 
		setup, chip) 
             Time   : Fast
-  You know the code is working right when   RMS3 is less than 0.01.
The code produces a QA file (e.g. 'QA/Arcs01/qa_fittrcarc_mb0038.ps.gz'). 
 
 
- hires_mkaimg :: Using the 2D slope solution and the 1D arc
	solution from step 2, this routine calculates a wavelength value for
	every science pixel in the image.
	The final wavelength image has air wavelengths and are saved as alog10 
	double values.
	Output is : 'Arcs/Arc_nameI.fits'
             Example: IDL> hires_mkaimg, hires, setup, obj_id, [chip],
		/CHK 
             Example: IDL> result = hires_mkaimg_work('arc_file', 
		setup, chip, XOFF=) 
             Time   : 1min  
-  You know the code is working right provided  it does not
complain about an order not being single-valued in wavelength.  Also
one can examine the BAD_ANLY parameter (it should be 0 upon completion).
 
 
 
Slit Profile
  
- hires_slitflat :: 
This routine determines the slit profile
for each order.  This is crucial for optimal sky subtraction, particularly
given the short slit length used in HIRES.   The profile is used to correct
the illumination pattern of the science frames.  The procedure is also important
for optimal extraction.  It must be run after the arc calibrations as the
slope of the arc lines is a necessary input.
             Example: IDL> hires_slitflat, hires, setup, [chip], [/chk], [/nodetilt]
             Time   : 10min per setup per chip 
-  You know the code is working right when  you check the QA
file (e.g. 'QA/Flats01/qa_slitflat_01B.ps.gz') and the slit profiles
look reasonably well behaved and the chi^2 values are around 0.5.
 
-  If you want to see the fits in real-time, use /chk 
 
-  For internal flats (standard), there is likely to be an illumination
pattern imposed on the slit.  Therefore, the default is to have the code
'detilt' the slit profile.  If you prefer NOT to detilt, turn on /NODETILT.
 
 
 Last modified 2005-10-10