# Calibrating narrow spectral line bands with the noise source

(01/10/2012, Demerese)

These notes are from a discussion with John C on how best to calibrate narrow band data.

An important thing to do at the observing script stage is to pick a bright passband and phase calibrator whenever possible! If this is not possible, you can use the noise source for passband calibration and determine the phases from the continuum data. You should always make sure to have enough continuum data to do a decent phase calibration.

## Bandpass Calibration

1. Start by splitting your continuum and spectral line data into separate "continuum" and "line" data sets.

If you plot the phase versus channel at this stage for the phase calibrator, then the data should be mostly a flat horizontal line. You might also notice breaks (or jumps) in the phase between windows, since each window can have a separate phase offset from 0 to begin with.
```  # smauvspec vis=\$data axis=chan,phase interval=10000 nxy=2,2 device=/xs select="source(\$passcal)"
```

2. The next step is to perform a channel-by-channel smamfcal or mfcal using the brightest astronomical source. Or, if all sources are too faint in the narrow spectral line windows, then use the noise source.

(sma)mfcal calculates the phase difference between each channel and a phase of 0. The advantage of using smamfcal is that is also allows you to do some boxcar smoothing of the narrow band data. The time averaging for (sma)mfcal interval should be kept small. This step will remove the small wiggles from the channel spectra and set the phase for all channels to 0.
```  # mfcal vis=\$continuum select="source(\$passcal)" line=chan,90,1,1,1 interval=1.,1000. refant=\$refant
# smamfcal vis=\$line select="source(\$passcal)" line=chan,254,1,1,1 interval=3.,1000. refant=\$refant smooth=5,1,0.9 options=msmooth,interpolate edge=5
```
(Note that if you must use the noise source, then you may notice that the noise source is only in the LSB. Therefore, you must calculate the conjugate of the LSB to extrapolate the noise source to the USB. Use uvcal with options=noisecal to do this.)
If you now re-plot the phases for your passband calibrator versus channel, then you should see that all phases lie around 0.
```  # smauvspec vis=\$data_pb axis=chan,phase interval=10000 nxy=2,2 device=/xs select="source(\$passcal)"
```
If the passband calibrator is an astronomical source, then your passband correction is complete, and you can apply the solution to your data. (Skip to step 4 if this is the case.) However, if you are using the noise source, then there likely remains a constant phase offset between the noise data and the astronomical data, which must be corrected. This offset between the noise source and the astronomical source may even be different from window to window.

3. Solve for the phase offset between the noise source and the astronomical data (if applicable).

After applying the passband solution derived from the noise source, run mfcal again, but now on the brightest astronomical source. This time you should also increase the solution time interval to be a very large number because the phase offset is a constant for each window. For weak astronomical sources, you may choose to use smamfcal with some smoothing.
```  # mfcal vis=\$line_noisepb select="source(\$astrosource)" interval=1000. refant=\$refant
# smamfcal vis=\$line_noisepb select="source(\$astrosource)" interval=1000. refant=\$refant smooth=5,1,0.9 options=msmooth,interpolate edge=5
```
Now if you plot the phase per channel for your astronomical source, then all windows and channels should have a phase of 0.
```  # smauvspec vis=\$line_astropb axis=chan,phase interval=10000 nxy=3,3 device=/xs select="source(\$astrosource)"
```

## Phase Calibration

4. Compute the gain solution. This step solves for the phases in time.

If you plot the phases in time for the phase calibrator, then the phases may have slopes or wraps.
```  # smauvplt vis=\$dataset select="source(\$phasecal)" axis=time,phase inc=1 nxy=2,2 device=1/xs
```
We start with the continuum data. Use mfcal (or selfcal) on the continuum data to flatten the phases in time.
```  #  mfcal vis=\$contdata select="source(\$phasecal)" interval=15 refant=\$refant flux=\$flux options=nopassol
```
Now if we re-plot the phases in time, each continuum window will be a flat horizontal line with a phase of 0. The continuum data is done.
Generally, for the narrow spectral line windows, you can apply the phase solution from the continuum data. So copy the gain solution to the spectral line windows using gpcopy. Of course, now we need to solve for the phase *and* amplitude offsets for each of the narrow bands. (When you set the flux during the continuum mfcal, you set the scaling parameter, but this parameter may be different in each of the spectral line bands, so you should use the flux keyword once again for mfcal of the spectral line windows).
```  # mfcal vis=\$linedata select="source(\$phasecal)" interval=10000. refant=\$refant flux=\$flux options=nopassol
```
Check that the spectral line phases are all flat with time. Apply the solution and then your spectral line data calibration is also complete.
```  # smauvplt vis=\$linedata select="source(\$phasecal)" axis=time,phase inc=1 options=nobase nxy=1,1 device=1/xs
```