Important information on LRIS-Red Mark 4:

In May 2021 the LRIS red CCD was replaced, following steady deterioration/malfunctioning 
of the previous CCD over the preceding year.  This new CCD (LRIS-R Mark 4) has different
geometry and detector properties than either of its predecessors: it has no chip gap, 
data are written as a single FITS extension, and the four amplifier regions are arranged
in quadrants rather than parallel stripes.

One year after the upgrade, the information on the new detector on the Keck webpage is
still largely missing or inaccurate.  A best-effort basis has been made to adapt the
pipeline to the new CCD, but it can only be tested on data the author (D. Perley) has
access to, so it may not work on all gratings and settings without additional
modifications.

Additionally, multiple users have reported and it has been confirmed that the header data
written by the LRIS software at the telescope to LRIS-red files is often inaccurate.  
This can include the CCD settings (binning, windowing, readout mode), the grating 
employed, the central wavelength, the filter, and so on.  Some other more general 
keywords are also occasionally in error (in particular, the status of the LRIS trapdoor).

LPipe cannot reduce observations without accurate information about these settings.  
Some of the inaccurate data can be inferred from looking at overlapping blue exposures
or from the image dimensions and the pipeline will now attempt to correct using this
wherever possible.  However, images with unpaired exposures and/or in unfamiliar
CCD modes may not be corrected.  These may require manual attention.

LRIS headers can be edited using LPipe's editheader command, which uses the following
syntax:
     editheader, [filename], [keyword], [newvalue]
(Filename can be a vector or wildcard, and keyword and newvalue can also be vectors.)

The most common keywords that require modification are those specifying the CCD windowing
and geometry - problems with these are usually easily evident as the pipeline will crash
or fail to process red images during the prepare step. Keck has not supplied any 
documentation about the LRIS-R4 header format and keywords, but by trial and error it has 
been inferred that the following keywords contain information about the geometry as 
follows:

  BSEC[N] - X and Y limits for the overscan region of the [N]th amplifier
  DSEC[N] - X and Y limits for the data region of the [N]th amplifier

These are stored in the format '[x0:x1,y0:y1]'.  The order of the limits can be reversed
(e.g., it can also be y1:y0).  The first row/column are 1 (as in DS9), not 0.
If the values stored in these keywords do not match your data, you can use editheader
to fix them.  The quadrants/amplifiers are arranged as follows (original data format
prior to the X/Y axis swap performed by the pipeline):
   4-amp mode amplifier regions:
     3 4
     1 2
   2-amp mode amplifier regions:
     1 2

First figure out whether your data are in 4-amp mode or 2-amp mode.  If in 4-amp mode,
you will need to edit all eight BSEC/DSEC values (4 each).  If in 2-amp mode, correct the 
values for amplifiers 1 and 2 only and remove BSEC3 (or set to an invalid value like -1).
The amp mode is usually evident from the pattern of the overscan region, which appears in
the middle of the image: if it is a vertical line, the data are in 2-amp mode; if it is a
cross, it is 4-amp mode.  Find the coordinates of the edges/corners of the data regions
using the mouse.  Add/subtract (2056/binning)-1 to infer the edges on the far left and far
right for the data regions, which are not always visible.  

Here is an example for 2-amp mode (binned 2x2):
  editheader, 'r210911_0010*.fits', 'DSEC1', '[5:1032,1:2064]'
  editheader, 'r210911_0010*.fits', 'BSEC1', '[1033:1103,1:2064]'
  editheader, 'r210911_0010*.fits', 'BSEC2', '[1106:1176,1:2064]'
  editheader, 'r210911_0010*.fits', 'DSEC2', '[1177:2204,1:2064]'
  editheader, 'r210911_0010*.fits', 'BSEC3', /remove

Here is an example for 4-amp mode (binned 1x1):
  editheader, 'r220701_0005*.fits', 'DSEC3', '[8:2064,2185:4248]'
  editheader, 'r220701_0005*.fits', 'BSEC3', '[2065:2117,2185:4248]'
  editheader, 'r220701_0005*.fits', 'BSEC4', '[2118:2168,2185:4248]'
  editheader, 'r220701_0005*.fits', 'DSEC4', '[2171:4227,2185:4248]'
  editheader, 'r220701_0005*.fits', 'DSEC1', '[8:2064,1:2064]'
  editheader, 'r220701_0005*.fits', 'BSEC1', '[2065:2117,1:2064]'
  editheader, 'r220701_0005*.fits', 'BSEC2', '[2118:2168,1:2064]'
  editheader, 'r220701_0005*.fits', 'DSEC2', '[2171:4227,1:2064]'
  
(These are just examples.  You will need to infer the actual positions of the amp 
boundaries by looking at the data.  Note that different files from your run may
use different CCD settings and require different values, especially if you used different 
instrument modes over the course of the run.)

Note that LPipe infers that it is 4-amp mode if BSEC3 exists and its coordinates are
valid.  (Other keywords exist that are intended to indicate the amplifier mode, but they 
are unreliable and thus not used.)  Note also that while keywords ASEC[1-4], CSEC[1-4], 
etc. are also present in the headers, they are not used by LPipe (their intended function 
is unclear).  The editheader tool accepts wildcard filename strings.

Other header values are also often inaccurate.  The lriscat command can be used to look 
for files with wrong entries, e.g. by comparing the output to your written night log:
  lriscat, 'r*.fits'

Values that may need correction are the trapdoor status (TRAPDOOR), grating (GRANAME), 
central wavelength (WAVELEN), filter (REDFILT), slit (SLITNAME), dichroic (DICHNAME),
and possibly other values.  As an example, you can correct the wavelengths of a set of 
files as follows:
  editheader, 'r210705_*.fits', 'WAVELEN', 7270

Since you will be editing raw files, it is recommended that you back up the data in case 
you make a mistake.  For keywords other than the CCD keywords, you can edit later pipeline
files instead of the raw files if you wish (as these can be regenerated from the raw data
by rerunning the pipeline).

