astrolofar.plotfits

Module to plot fits files.

Note

This module is built based on WSClean fits files, i.e. it does not support image cubes (yet). This means you need a fits file for each frequency/polarization.

You can use the module as

>>> from astrolofar import plotfits
astrolofar.plotfits.calc_beam(fitsname)

Calculate the beam size in pixel units.

Parameters:

fitsname (str) – Name of the fits file. You may include the full path if the file is not in the directory where you ran Python.

Returns:

Omega – Area of the beam in pixel units.

Return type:

float

astrolofar.plotfits.gen_fits(data, outname, wcs=None, freq=None, dfreq=1, btype='None', comments=['Generated with astrolofar'])

Generate a simple fits file from input data.

Parameters:
  • data (array_like) – Input array of data.

  • outname (str) – Name of the output fits file without extension.

  • wcs (WCS object, default: None) –

    WCS object to save sky coordinates in the new fits file. You can use astrolofar.plotfits.read_fits_wcs() to extract WCS from an existing fits file that contains them. Default is None.

    Warning

    The fits file used to extract WCS must be of the same pixel size and scale of the new fits file.

  • freq (float, default: None) – Reference frequency. If default (None) is used, no frequency axis is added.

  • dfreq (float, default: 1) – Frequency increment. It is only activated if freq is not None. Default is 1.

  • btype (str, default: None) – Unit of data. Default is None.

  • comments (list [str], default: ['Generated with astrolofar']) – Any comment that you would like to add in the fits file under the COMMENT keyword. Default is ‘Generated with astrolofar’.

Returns:

outfits – Name of the output fits file (extension is included).

Return type:

str

astrolofar.plotfits.plot_fits(fitsname, wcs=None, c_rms=None, c_fitsname=None, c_levels=None, vrange=('min', 'max'), size=(6, 5), trim=(1, 1), cmap='viridis', scalelog10=False, cmaplabel=None, savefig=None, outname='fitsname')

Plot a fits file setting different parameters.

Parameters:
  • fitsname (str) – Name of the fits file. You may include the full path if the file is not in the directory where you ran Python.

  • wcs (WCS object, default: None) –

    WCS object to over-plot sky coordinates. You can use astrolofar.plotfits.read_fits_wcs() to extract WCS from an existing fits file that contains them. Default is None.

  • c_rms (float, default: None) –

    Standard deviation or RMS of the residuals/background to set the scale of contours plot.

    Note

    This parameters activate contour plotting.

  • c_fitsname (str, default: None) –

    Name of the fits file from which countours are extracted. It does not have to be the same of fitsname. If default (None) is used, c_fitsname = fitsname.

    Note

    This parameter does something only if c_rms is not None.

  • c_levels (list, default: None) –

    List of levels at which countours are plotted. The actual levels are plotted at levels * c_rms. If deafult (None) is used, c_levels is [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048].

    Note

    This parameter does something only if c_rms is not None.

  • vrange (str or tuple ({str, float}, {str, float}), default: ('min','max')) –

    Data range that the colormap covers. Supported values are:

    1. (‘min’, ‘max’) (tuple (str, str)): The default, the colormap covers the full value range of the data.

    2. ’Qperc’ or ‘Q%’ (str): The colormap range is computed with the Q-th percentile (see astrolofar.plotfits.vrange_percentile()).

    3. (A, B) (tuple (float, float)): The colormap ranges between A and B, with \(A>B\).

    4. Any combination of (1) with (3) (for example, you can set vrange=('min',100) or vrange=(-2.5,'max'), but you can not use vrange=('10perc',80)).

  • size (tuple (float, float), default: (6, 5)) – Width, height in inches of the full plot.

  • trim (tuple (float, float), default: (1, 1)) – Fractions along the x and y axis to crop the image about the image center. A value of 1 means no trim is performed. New ranges are assigned to the axis if different values are assigned: \(x_{min} = N_{data, x} (1-t_x)/2\) and \(x_{max} = N_{data, x} (1+t_x)/2\), where \(t_x\) is the input trim value (same for y).

  • cmap (str or Colormap, default: 'viridis') – The Colormap instance or Matplotlib colormap name used to color the data.

  • scalelog10 (bool, default: False) – Format the colormap in log10.

  • cmaplabel (str, default: None (default: fits BUNIT)) –

    Label of the colorbar. If None (default) is used, the BUNIT keyword of the input fits file is used.

    Warning

    The fits file must have the key BUNIT. Any WSClean fits file is formatted in the correct way, as well the spectral index map resulting from astrolofar.spectrum.extract_spix.

    savefig : {‘pdf’, ‘png’}, default: None Save the plot in PDF or PNG (with 200 dpi) formats (no plot is showed on screen). If None (default) is used, the plot is not saved on disk but showed on screen.

  • outname (str, default: fitsname) – Name of the output image file without extension. Activated only if savefig is not None. If fitsname (default) is used, the name of the output file will be the same of the input fits file.

Returns:

astrolofar.plotfits.read_fits_data(fitsname)

Open an existing fits file to extract data and their units using astropy.io.fits().

Parameters:

fitsname (str) – Name of the fits file. You may include the full path if the file is not in the directory where you ran Python.

Returns:

  • data (2D-array) – Data saved in the fits file, with shape (X,`Y`), where X and Y are the number of pixels along the x and y axis.

  • freq (float) – Frequency or wavelength of the measurement, stored in the fits at the key CRVAL3.

  • units (str) – Physical data units, stored in the fits at the key BUNIT.

Note

The fits file should have the keys BUNIT and CRVAL3. Any WSClean fits file is formatted in the correct way, as well the spectral index map resulting from astrolofar.spectrum.gen_spix_map.

astrolofar.plotfits.read_fits_wcs(fitsname)

Open an existing fits file to extract WCS (World Coordinate System).

Parameters:

fitsname (str) – Name of the fits file. You may include the full path if the file is not in the directory where you ran Python.

Return type:

WCS object

Example

>>> from astrolofar.plotfits import read_fits_wcs
>>> fitsname = "../tests/test.fits"    # fits file name
>>> wcs = read_fits_wcs(fitsname)
>>> wcs.pixel_to_world(10, 50)
<SkyCoord (FK5: equinox=2000.0): (ra, dec) in deg
    (299.91265067, 40.70179643)>

Refer to Astropy WCS to more information.

astrolofar.plotfits.regrid(fitsname, template, new_size=(512, 512))

Regrid a fits image to different sizes, using a template fits to transfer WCS information.

Parameters:
  • fitsname (str) – Name of the fits file. You may include the full path if the file is not in the directory where you ran Python.

  • template (str) – Name of the template fits file. This must have the size and the pixel scale of the wanted regridded image. It is used to set the correct WCS information in the output regridded fits image. Its size must be the same of new_size to avoid incorrect WCS projections.

  • new_size (tuple (int, int), default: (512, 512)) – Image size of the output regridded image in pixels. It must th same of the template fits file.

Returns:

regridded_data – Regridded data.

Return type:

array_like

astrolofar.plotfits.vrange_percentile(data, q)

Calculate minimum and maximum value of a q-th percentile of the data about the mean value using numpy.percentile().

Parameters:
  • data (array_like) – Input array of data.

  • q (float) – Percentile to compute, which must be in the range 0-100 (inclusive). To have a range similar to DS9, percentiles are calculated with the qi-th “image percentile” \(qi = q+(100-q)/2\).

Returns:

range – 2-item tuple (vmin, vmax), where vmin = numpy.percentile(data, 100-qi) and vmax = numpy.percentile(data, qi).

Return type:

tuple (float, float)

Example

>>> import numpy
>>> from astrolofar.plotfits import vrange_percentile
>>> a = numpy.arange(0,201)
>>> vrange_percentile(a, 95)
(5.0, 195.0)

Warning

This percentile is different from the “standard” definition of percentile: numpy.percentile(a, 95) outputs 190 instead of 195 because it is not centered on the mean value of the pixel distribution.

astrolofar.plotfits.vrange_percentile_fits(fitsname, q)

Use astrolofar.plotfits.vrange_percentile() to compute the q-th percentile directly from a fits file.

Parameters:
  • fitsname (str) – Name of the fits file. You may include the full path if the file is not in the directory where you ran Python.

  • q (float) –

    Percentile to compute which must be in the range 0-100 (inclusive). To have a range similar to DS9, percentiles are calculated with the qi-th “image percentile” \(qi = q+(100-q)/2\).

Returns:

range – 2-item tuple (vmin, vmax), where vmin = numpy.percentile(data, 100-qi) and vmax = numpy.percentile(data, qi).

Return type:

tuple (float, float)