FitsData

class sofia_redux.scan.source_models.maps.fits_data.FitsData(data=None, blanking_value=None, dtype=<class 'float'>, shape=None, unit=None)[source]

Bases: FlaggedArray

Creates a FitsData instance.

The FitsData class is an extension of FlaggedArray that allows additional functionality for FITS data. This includes specifying data units (astropy.units), history messages and FITS handling.

Notably, the dimensional ordering will now be in FITS (x, y) order rather than the FlaggedArray Numpy (y, x) ordering for the various methods.

Parameters:
datanumpy.ndarray, optional

Data to initialize the flagged array with. If supplied, sets the shape of the array. Note that the data type will be set to that defined by the dtype parameter.

blanking_valueint or float, optional

The blanking value defines invalid values in the data array. This is the equivalent of defining a NaN value.

dtypetype, optional

The data type of the data array.

shapetuple (int), optional

The shape of the data array. This will only be relevant if data is not defined.

unitstr or units.Unit or units.Quantity, optional

The data unit.

Attributes Summary

DEFAULT_UNIT

referenced_attributes

Return attribute names that should be referenced during a copy.

unit

Return the current data unit.

Methods Summary

add(value[, indices, factor])

Add a value or FlaggedData to the data array.

add_alternate_unit_names(unit_name, ...)

Add alternative names for a unit.

add_history(message)

Add a history message.

add_history_to_header(header)

Add history messages to a FITS header.

add_local_unit(unit[, alternate_names])

Add a unit to the dictionary of local units.

clear([indices])

Clear flags and set data to zero.

clear_history()

Clear the history messages.

copy()

Return a copy of the FITS data.

create_fits()

Create and return a FITS HDU list from data content.

create_hdu()

Create a FITS HDU from map data content.

crop(ranges)

Crop the array to the required dimensions.

delete_indices(indices_or_mask)

Completely deletes data elements.

despike(threshold[, noise_weight])

Discard spikes whose significance is above a given threshold.

destroy()

Destroy the image data.

edit_header(header)

Edit a FITS header using information in the current map.

fast_smooth(beam_map, steps[, ...])

Smooth using the fast method.

fill(value[, indices])

Fill the data array with a given value.

fits_to_numpy(coordinates)

Convert FITS based (x, y) coordinates/indices to numpy (y, x).

get_fast_smoothed(beam_map, steps[, ...])

Return smoothed values using the fast method.

get_fits_data()

Return the FITS data array.

get_hdus()

Create and return a list if FITS HDUs.

get_index_range()

Return the index ranges of valid points.

get_indices(indices)

Return selected data for given indices.

get_refined_peak_index(peak_index)

Get the peak index given a local peak.

get_size_string()

Return the shape of the data array as a string.

get_smoothed(beam_map[, reference_index, ...])

Return smoothed data and weights.

get_unit(unit)

Return the unit quantity for a given unit.

index_of_max([sign, data])

Return the maximum value and index of maximum value.

insert_blanks(insert_indices)

Inserts blank frame data.

merge(data)

Add additional data onto the end of this data.

numpy_to_fits(coordinates)

Convert numpy based (x, y) coordinates/indices to FITS coordinates.

parse_header(header)

Parse and apply information from a FITS header.

parse_history(header)

Set the history from a FITS header.

paste(source[, report])

Paste data from another MapData onto the data array.

record_new_data([detail])

Start recording new data.

resample_from(image, to_indices[, kernel, ...])

Resample an image onto given indices of this map.

scale(factor[, indices])

Scale the data by a given factor.

set_data(data[, change_type])

Set the data of the flagged array.

set_data_shape(shape)

Set the shape of the data image array.

set_default_unit()

Set the default unit for the map data.

set_executor(executor)

Set the executor?

set_history(messages)

Set the history to a given list of history messages.

set_parallel(threads)

Set the number of parallel threads.

set_unit(unit)

Set the map data unit.

smooth(beam_map[, reference_index, weights])

Smooth the map with a beam map kernel.

unit_to_quantity(unit)

Return an astropy Quantity from a given unit.

validate([validator])

Discard all invalid data.

value_at(index[, degree, reduce_degrees])

Return the data value at a given index.

Attributes Documentation

DEFAULT_UNIT = <Quantity 1.>
referenced_attributes

Return attribute names that should be referenced during a copy.

Returns:
set (str)
unit

Return the current data unit.

Returns:
astropy.units.Quantity

Methods Documentation

add(value, indices=None, factor=None)[source]

Add a value or FlaggedData to the data array.

Parameters:
valueint or float or FlaggedArray

The value to add.

indicestuple (numpy.ndarray (int)) or numpy.ndarray (bool), optional

The indices to discard. Either supplied as a boolean mask of shape (self.data.shape).

factorint or float, optional

An optional factor to scale the data by.

Returns:
None
add_alternate_unit_names(unit_name, alternate_names)[source]

Add alternative names for a unit.

Parameters:
unit_namestr

The name of the unit.

alternate_namesstr or list (str)

Alternate names for the unit.

Returns:
None
add_history(message)[source]

Add a history message.

Will also result in a log message if verbose is True.

Parameters:
messagestr or list(str)
Returns:
None
add_history_to_header(header)[source]

Add history messages to a FITS header.

Parameters:
headerastropy.io.fits.header.Header
Returns:
None
add_local_unit(unit, alternate_names=None)[source]

Add a unit to the dictionary of local units.

Parameters:
unitastropy.units.Quantity or units.Unit or str

The unit to add to the unit dictionary. Should be a quantity with both value and unit type. If a simple unit is supplied, the value is assumed to be 1.0.

alternate_nameslist, optional

A list of alternate names for the unit.

Returns:
None
clear(indices=None)[source]

Clear flags and set data to zero. Clear history.

Parameters:
indicestuple (numpy.ndarray (int)) or numpy.ndarray (bool), optional

The indices to discard. Either supplied as a boolean mask of shape (self.data.shape). Note that if an integer array or tuple of integer arrays are supplied, they should be in (x, y) FITS order. Boolean masks should be of the same shape as the data.

Returns:
None
clear_history()[source]

Clear the history messages.

Returns:
None
copy()[source]

Return a copy of the FITS data.

Returns:
FitsData
create_fits()[source]

Create and return a FITS HDU list from data content.

Returns:
astropy.io.fits.HDUList
create_hdu()[source]

Create a FITS HDU from map data content.

Returns:
astropy.io.fits.ImageHDU
crop(ranges)[source]

Crop the array to the required dimensions.

Parameters:
rangesnumpy.ndarray (int,)

The ranges to set crop the data to. Should be of shape (n_dimensions, 2) where ranges[0, 0] would give the minimum crop limit for the first dimension and ranges[0, 1] would give the maximum crop limit for the first dimension. In this case, the ‘first’ dimension is in FITS format. i.e., (x, y) for a 2-D array. Also note that the upper crop limit is inclusive so a range of (0, 3) includes indices [0, 1, 2, 3].

Returns:
None
delete_indices(indices_or_mask)[source]

Completely deletes data elements.

Actual indices should be passed in. To delete based on fixed index values, please convert first using find_fixed_indices.

Parameters:
indices_or_maskint or list or numpy.ndarray of (bool or int)

The indices to delete, or a boolean mask where True marks an element for deletion.

Raises:
NotImplementedError
despike(threshold, noise_weight=None)[source]

Discard spikes whose significance is above a given threshold.

Parameters:
thresholdfloat
noise_weightnumpy.ndarray (float), optional

Optional noise weights.

Returns:
None
destroy()[source]

Destroy the image data.

Returns:
None
edit_header(header)[source]

Edit a FITS header using information in the current map.

The information keywords added to the header are:

- DATAMIN: The lowest value in the data (float)
- DATAMAX: The highesst value in the data (float)
- BZERO: Zeroing level of the data (float)
- BSCALE: Scaling of the data (float)
- BUNIT: The data unit (str)

Note that BUNIT will default to ‘count’ if no unit has been set.

Parameters:
headerastropy.io.fits.header.Header
Returns:
None
fast_smooth(beam_map, steps, reference_index=None, weights=None)[source]

Smooth using the fast method.

Parameters:
beam_mapnumpy.ndarray (float)

The beam map image kernel with which to smooth the map of shape (ky, kx).

stepsIndex2D

The fast step skips in (x, y) FITS dimensional order.

reference_indexCoordinate2D, optional

The reference pixel index of the kernel in (x, y) FITS dimensional order. The default is the beam map center at (kx-1)/2, (ky-1)/2.

weightsnumpy.ndarray (float), optional

The map weights of shape (ny, nx). The default is no weighting.

Returns:
None
fill(value, indices=None)[source]

Fill the data array with a given value.

Parameters:
valueint or float

The value to fill.

indicestuple (numpy.ndarray (int)) or numpy.ndarray (bool), optional

The indices to discard. Either supplied as a boolean mask of shape (self.data.shape).

Returns:
None
classmethod fits_to_numpy(coordinates)[source]

Convert FITS based (x, y) coordinates/indices to numpy (y, x).

Reverses the dimensional ordering so that (x, y) coordinates are returned as (y, x) coordinates. Note that boolean arrays remain unaltered, since these usually indicate masking arrays.

Parameters:
coordinatesnumpy.ndarray or Coordinate or iterable
Returns:
numpy_coordinatesnumpy.ndarray (int or float)
get_fast_smoothed(beam_map, steps, reference_index=None, weights=None, get_weights=False)[source]

Return smoothed values using the fast method.

Parameters:
beam_mapnumpy.ndarray (float)

The kernel to convolve with.

stepsnumpy.ndarray (float)

The size of the steps in each dimension.

reference_indexnumpy.ndarray (float)

The reference index of the beam map center. The default is (beam_map.shape - 1) / 2.0.

weightsnumpy.ndarray (float)

Weights the same shape as beam map.

get_weightsbool, optional

If True, calculate the smoothed weights in addition to the smoothed data.

Returns:
smoothed_data, [smoothed_weights]numpy.ndarray, [numpy.ndarray]

The smoothed data and weights. Will only return smoothed data if get_weights is False.

get_fits_data()[source]

Return the FITS data array.

Returns:
array
get_hdus()[source]

Create and return a list if FITS HDUs.

Returns:
hdus: list (astropy.io.fits.hdu.base.ExtensionHDU)
get_index_range()[source]

Return the index ranges of valid points.

Returns:
rangesnumpy.ndarray (int) or Coordinate

A range for each dimension or shape (n_dimensions, 2) giving the minimum and maximum range in each dimension. Note that this is FITS dimension ordering (x-range = ranges[0], y-range = ranges[1]). Also note that the upper range is returned such that the real upper index is included in any slice operation. i.e., max = real max index + 1.

get_indices(indices)[source]

Return selected data for given indices.

Parameters:
indiceslist or int or numpy.ndarray (bool or int)

The indices to extract.

Raises:
NotImplementedError
get_refined_peak_index(peak_index)[source]

Get the peak index given a local peak.

Basically performs a quadratic fit on local neighborhood to determine the maximum.

Parameters:
peak_indextuple (int)

The peak index of the data array in (y, x) numpy format.

Returns:
refined_peak_indexnumpy.ndarray or Coordinate
get_size_string()[source]

Return the shape of the data array as a string.

Returns:
str
get_smoothed(beam_map, reference_index=None, weights=None)[source]

Return smoothed data and weights.

Parameters:
beam_mapnumpy.ndarray (float)

The kernel with which to smooth the map. Should be of shape (ny, nx).

reference_indexnumpy.ndarray (float or int) or Coordinate, optional

The reference index specifying the center pixel of the beam map. The default is the center pixel position, eg., (ny - 1) / 2.

weightsnumpy.ndarray (float), optional

The weights to apply during smoothing. Should be the same shape as the primary map image. The default is no weighting.

Returns:
smoothed_data, smoothed_weightsnumpy.ndarray, numpy.ndarray
get_unit(unit)[source]

Return the unit quantity for a given unit.

Parameters:
unitstr or units.Unit or units.Quantity
Returns:
units.Quantity
index_of_max(sign=1, data=None)[source]

Return the maximum value and index of maximum value.

Parameters:
signint or float, optional

If positive, find the maximum value in the array. If negative, find the minimum value in the array. If zero, find the maximum magnitude in the array.

datanumpy.ndarray (float), optional

The data array to examine. Default is self.data.

Returns:
maximum_value, maximum_indexfloat, numpy.ndarray or Coordinate
insert_blanks(insert_indices)[source]

Inserts blank frame data.

Actual indices should be passed in. To delete based on fixed index values, please convert first using find_fixed_indices.

Blank data are set to 0 in whatever unit is applicable.

Parameters:
insert_indicesint or list or numpy.ndarray of (bool or int)

The index locations to insert.

Raises:
NotImplementedError
merge(data)[source]

Add additional data onto the end of this data.

Parameters:
dataFlaggedData
Raises:
NotImplementedError
classmethod numpy_to_fits(coordinates)[source]

Convert numpy based (x, y) coordinates/indices to FITS coordinates.

Parameters:
coordinatesnumpy.ndarray or Coordinate or iterable
Returns:
numpy.ndarray
parse_header(header)[source]

Parse and apply information from a FITS header.

Parameters:
headerastropy.io.fits.header.Header
Returns:
None
parse_history(header)[source]

Set the history from a FITS header.

Parameters:
headerastropy.io.fits.header.Header
Returns:
None
paste(source, report=True)[source]

Paste data from another MapData onto the data array.

Parameters:
sourceFitsData
reportbool, optional

If True, add a history message.

Returns:
None
record_new_data(detail=None)[source]

Start recording new data.

Parameters:
detailstr, optional

An optional message to append to the start of the new data log.

Returns:
None
resample_from(image, to_indices, kernel=None, kernel_reference_index=None, weights=None)[source]

Resample an image onto given indices of this map.

Parameters:
imageFlaggedArray or numpy.ndarray (float)

The image to resample of shape (shape,) and have n_dimensions.

to_indicesnumpy.ndarray (float or int)

An array of shape (n_dimensions, self.shape or self.size) specifying which image pixels belong to the resampled map. Dimensions should be ordered using the FITS (x, y) convention. For example, if pixel i is at (x, y) = (2, 3) then to_indices[:, i] = [2, 3].

kernelnumpy.ndarray (float), optional

The kernel used to perform the resampling. If supplied, the result will be smoothed accordingly.

kernel_reference_indexnumpy.ndarray (int), optional

If a kernel is supplied, specifies the center pixel of the kernel to be used during kernel convolution. The default is (kernel.shape - 1) / 2. Should be an array of shape (n_dimensions,) using FITS dimensional ordering (x, y).

weightsnumpy.ndarray (int or float), optional

An optional weighting array used for kernel convolution. Should be an array of shape (shape,).

Returns:
None
scale(factor, indices=None)[source]

Scale the data by a given factor.

Parameters:
factorint or float

The factor to scale by.

indicestuple (np.ndarray) or np.ndarray (int or bool), optional

The indices to discard. Either supplied as a boolean mask of shape (self.data.shape).

Returns:
None
set_data(data, change_type=False)[source]

Set the data of the flagged array.

All flags are set to zero.

Parameters:
datanumpy.ndarray or FlaggedArray
change_typebool, optional

If True, change the data type to that of the data.

Returns:
None
set_data_shape(shape)[source]

Set the shape of the data image array.

Note that array shapes should still be provided in Numpy (y, x) format. In addition to settings the data shape, adds a new history message indicating that this has been done.

Parameters:
shapetuple (int)
Returns:
None
set_default_unit()[source]

Set the default unit for the map data.

Returns:
None
set_executor(executor)[source]

Set the executor?

Parameters:
executorExecutor
Returns:
None
set_history(messages)[source]

Set the history to a given list of history messages.

Parameters:
messagesstr or list (str)
Returns:
None
set_parallel(threads)[source]

Set the number of parallel threads.

Parameters:
threadsint
Returns:
None
set_unit(unit)[source]

Set the map data unit.

Parameters:
unitastropy.units.Quantity or str or astropy.units.Unit

The unit to set as the map data unit. Should be a quantity (value and unit type). If a string or Unit is supplied, the map data unit will be set to the value located in the local_units dictionary. If no such value exists, a KeyError will be raised.

Returns:
None
smooth(beam_map, reference_index=None, weights=None)[source]

Smooth the map with a beam map kernel.

Parameters:
beam_mapnumpy.ndarray (float)

The beam map with which to smooth the map of shape (ny, nx, …).

reference_indexnumpy.ndarray (int) or Coordinate, optional

The index specifying the center pixel of the kernel. Should be provided in (x, y) ordering. The default is the center of the kernel.

weightsnumpy.ndarray (float), optional

The map weights to apply. Should be the same shape as the primary map image. The default is to apply no weighting.

Returns:
None
static unit_to_quantity(unit)[source]

Return an astropy Quantity from a given unit.

If a string or astropy.units.Unit is supplied, the resultant output will be a quantity with value 1.

Parameters:
unitastropy.units.Quantity or astropy.units.Unit or str

The unit to convert to a quantity.

Returns:
astropy.units.Quantity
validate(validator=None)[source]

Discard all invalid data.

Parameters:
validatorValidator, optional

An optional object or function that can take FitsData as an argument and perform the validation.

Returns:
None
value_at(index, degree=3, reduce_degrees=False)[source]

Return the data value at a given index.

Parameters:
indexnumpy.ndarray (int or float)

An array of shape (n_dimensions,). Should be supplied in (x, y) order (FITS).

degreeint, optional

The degree of spline to fit.

reduce_degreesbool, optional

If True, allow the spline fit to reduce the number of degrees in cases where there are not enough points available to perform the spline fit of degree. If False, a ValueError will be raised if such a fit fails.

Returns:
float