# APOGEE Visit Spectra Combination

This page provides a brief description on how the individual visit spectra are combined into one spectrum for each star in a given field. While the basic methdology is presented in Nidever et al. (2015) , the visit combination software has been rewritten for DR17, in particular, to take advantage of a new method for determining radial velocities and to improve radial velocities for faint stars.

Most APOGEE fields are observed multiple times to build up the signal-to-noise ratio (S/N) for the faintest stars and to detect stellar binaries from their radial velocity variation. For analysis to be performed at maximum S/N, these multiple "visit" spectra (as output by ap1dvisit) are combined into one spectrum for each star. This process is called "visit combination" and involves several steps.

The output of this process are apStar/asStar (for APOGEE-N/APOGEE-S) files for each star, which are FITS files as described in the data model. These files contain the combined spectrum as well as all of the resampled visit spectra on the same wavelength scale.

Visit Combination Steps
1. Determination of Doppler shift (i.e., radial velocity) for each visit spectrum.
2. Resampling of each visit spectrum onto the same rest wavelength scale (i.e., the Doppler shift is removed).
3. Removal of continuum from each visit spectrum
4. Weighted combination of all the resampled visit spectra.
5. Re-application of mean continuum shape.

## Radial Velocity Determination

The radial velocity (RV) determination for APOGEE spectra has been rewritten for DR17. This was motivated by the availability of a new code, Doppler (Nidever 2021) for RV determination that uses a model to provide a continuous set of template spectra for a simultaneous template+RV fit, as well as an effort to improve radial velocities for faint stars.

Details of the RV code are given on the radial velocities page.

### Resampling

For spectral combination, the individual Doppler shifts for each visit spectrum are removed, and then the visit spectra are resampled onto the same wavelength grid. Using the radial velocity determined as described above, the wavelengths are corrected to the rest wavelength values (λrestobs/(1+RV/c), where c is the speed of light). Once corrected to the rest wavelength scale, the spectrum is resampled onto a final logarithmically-spaced wavelength scale using interpolation with a sinc function. As in previous data releases, the combined star wavelength scale, in vacuum wavelengths, is given by:
$$\log_{10} \lambda_{i+1} - \log_{10} \lambda_i = 6.e-6$$
with a starting wavelength of 15100.802 Angstroms ($\log \lambda_0 = 4.179$). This provides roughly 3 samples per resolution element, and corresponds to a velocity sampling of about 4.145 km/s per pixel.

### Weighted Combination

To create a final combined spectrum for each star, we do a weighted sum of the individual visit, rest-frame shifted, and resampled spectra. To avoid biasing the weighted sum in the case of variations in the flux calibration, we first remove the mean continuum (by dividing out a median-filtered spectrum) from each visit.

The combination is done in two different ways: (1) pixel-by-pixel weighting, where each pixel is weighted by its inverse variance, and (2) "global" weighting, where each visit spectrum is weighted by a median-filtered inverse variance. In both cases, bad pixels in individual visit spectra are discarded before the combination. Also, the uncertainties of pixels affected by persistence are inflated, resulting in reduced weight of these pixels to the combined spectrum. The two schemes give similar results in most cases, but both are saved in the apStar/asStar FITS file.

Finally, the combined spectra are multiplied by the average (over the multiple visit spectra) of the continuum shape to return the continuum to the combined spectrum.

## Output Star Spectra: apStar/asStar files

The combined spectra are provided in apStar files, which are FITS files as described in the data model, and can be accessed as described on the data access page.

The primary HDU (HDU0) contains a header that has information about the star and each visit. This includes STARFLAG and ANDFLAG bitmasks that provide information about potential global issues with the star, where the different values are described by the STARFLAG bitmask. STARFLAG is a bitwise-OR combined version of the STARFLAGs from the individual visits, while ANDFLAG is a bitwise-AND combined version.

Subsequent HDUs contain spectral data: the first HDU of each file contains an image which gives the two versions of the combined spectrum mentioned above for each object, plus the individual visit spectra that went into these combinations. Additional HDUs contain the estimated uncertainties in each pixel (HDU2), pixel bitmasks (HDU3), and sky and telluric spectra. HDU9 contains a FITS table that contains the cross-correlations of the final derived template against each visit.

The pixel bitmask in HDU3 images provides information about potential issues with individual pixels, where the different values are described by the APOGEE_PIXMASK bitmask. As the final spectrum is a combination of three or more individual exposures, it may be that some bits were flagged in some exposures but not in others.

As described above, all of the individual spectra have been resampled to a common logarithmically-spaced wavelength scale, with the radial velocity of each individual visit spectrum removed. Note that the APOGEE wavelength scale is based on vacuum wavelengths. The logarithmic wavelength grid spacing is the same for all objects
$$\log_{10} \lambda_{i+1} - \log_{10} \lambda_i = 6.e-6$$
with a starting wavelength of 15100.802 Angstroms ($\log \lambda_0 = 4.179$)