## 12.4. doGridding in Detail

### 12.4.1. Particulars of Convolution

The gridding and convolution technique used is very similar to the one used in CLASS. The convolution is performed with a Gaussian filter function (recommended for OTF maps), where the Gaussian function is defined as

and where the FWHM of the antenna beam B is

The final beam, Bf, is equal to

Using a kernel of 0.3∗B then Bf = CB, where C = 1.0440306508 i.e. the final beam is 4.4% larger than the normal beam. We loose a bit of the resolution in order to have a better reconstruction of the map.

If the smooth factor (Sm) is > C then the kernel becomes equal to

and the filter is Gaussian where

Figure 12.2 shows an example where we have three beam positions, 1, 2 and 3 (red circles) and where we find, in the influence area (blue square), beams 1 and 3. We define: 1] the influence area (also called the length) of the filter as (3∗Kernel / PixelSize). It is the area surrounding a grid point where the algorithm must pick up all the available data point, 2] Dx and Dy as the distances from the centre of the beam to the centre of the influence area, measured along their respective x and y axis, and 3] the filter parameter for each of the x and y:

The filter final value, V, is Vx∗Vy.

The doGridding task offers two options to determine the weights: all same weight and read weights column from dataset. The final weight value W at the centre of the influence area is (W1V1+W3V3). For option all same weight, W1=W3=1 and the Cube Weight array will contain only the weights computed when building the map. For option read weights column from dataset, W1 and W3 are the values of the weight of the related spectra at that specific frequency. Thus, the Cube Weight array will contain a mix of the weights found in the htp and the weights computed when building a map. Finally, the final flux F at the centre of the influence area is (F1V1W1+F3V3W3) / (V1W1+V3W3).

### 12.4.2. Using the Gridding task with the Spectrum Toolbox

Another task, called Gridding, is available to make cubes of images. Like doGridding, it creates a cube by performing a spatial regridding of the input spectra onto a regular grid. However, unlike doGridding, it works with any dataset or product that implement the SpectrumContainer or interface, this means that the Gridding task is not limited to working with HTP and can be used instead for more general (to all three Herschel instruments) Spectrum1d and Spectrum2d. Using the Spectrum Toolbox, you may also create your own collection of datasets to pass to the Gridding task.

The Gridding task and the Spectrum Toolbox.  You can make use of the spectrum selection tools of the spectrum toolbox to perform any selection of spectra followed by the usage of the Gridding task to create a cube for each segment of the spectra in these selections. The following example shows how to combine SelectSpectrum with the Gridding task:

```# first, create an instance of the SelectSpectrum task
selector = herschel.hifi.pipeline.util.tools.SelectSpectrum()
# use SelectSpectrum to get a single HifiSpectrumDataset with the spectra that fulfill certain criteria
# e.g. here one selects those spectra where its containing dataset has bbtype equal to 6022.
selected = selector(htp=htp, selection_lookup={'bbtype':[6022]}, return_single_ds=Boolean.TRUE )
# one might have a glance at the spectra in the "selection" dataset e.g. in the TablePlotter
cube = gridding(selected)
cubes = gridding.cubes
```

Making a SpectralSimpleCube with the Gridding task .

```#-------------------------------------------------------------------------------
#  make a dataset with all the spectra from all the science datasets
#  (isLine == true => bbtype == 6022)
#-------------------------------------------------------------------------------
selector = herschel.hifi.pipeline.util.tools.SelectSpectrum()
selected = selector(htp=htp, selection_lookup={'bbtype':[6022]}, return_single_ds=Boolean.TRUE )

scienceOnIndices = htp.summary['isLine'].data.where(\
htp.summary['isLine'].data == Boolean.TRUE)
bbid = htp.summary['Bbid'].data[scienceOnIndices]

#another way of selecting...

selected = selector(htp=htp, \
selection_lookup={'bbtype':bbid[0]}, \
return_single_ds=Boolean.TRUE )

#-------------------------------------------------------------------------------
# the Gridding task that can work with any SpectrumContainer or collection of
# SpectrumContainers, like the selected above
#-------------------------------------------------------------------------------

cube = gridding(container=selected)

#get a point spectrum from this selection,
ds_spectrum = selected.getPointSpectrum(1)
ds_segment = ds_spectrum.getSegment(3) # read its third subband : get SpectralSegment.
plotSegment = PlotXY(ds_segment.wave,ds_segment.flux,xtitle='Frequency (MHz)',ytitle='Intensity')

#-------------------------------------------------------------------------------
# now let's play with the result cubes
#
#
# each cube is a SpectralSimpleCube which in its turn is an SpectrumContainer
# hence we profit from all the spectrum toolboxes: arithmetics, statistics, etc.
# and we can e.g. directly obtain a point spectrum as for any other SpectrumContainer
#-------------------------------------------------------------------------------

row = 0; column = 10;
spectrum = cube.getPointSpectrum(row,column)

print spectrum.getLongitude()
print spectrum.getLatitude()

print spectrum.segmentIndices
# you can check the cube, hence its spectra has a single "segment" or subband

segment = spectrum.getSegment(0)
# or...
segment = spectrum.getSegment(spectrum.segmentIndices[0])

plotSpectrum = PlotXY(segment.getWave(),segment.getFlux(),xtitle='Frequency (MHz)',ytitle='Intensity')

# There are several ways to visualize a cube such as
# the CubeSpectrumAnalysisToolbox:

cat = CubeSpectrumAnalysisToolbox(cube)

# you can also visualize it with the SpectrumExplorer,
# since the cube is an SpectrumContainer

# or simply display it as a cube of images:
display = Display(cube)

```

Optional inputs for the Gridding task.

Most of the optional inputs of the DoGridding task are also applicable to the Gridding task namely: weightMode, filterType, mapSize, refPixel, refPixelCoordinates, pixelSize, smoothFactor, filterType, filterParams, detail, and the input Wcs.

In addition, there are other optional inputs which are specific to this task, namely container and containerBox.