lofarSun.BF.bftools

Module Contents

Functions

h5_fetch_meta(f[, SAP])

get info from the h5 file

downsample_h5_seg_by_time_ratio(data_array_uri, t_all, ...)

Downsample the h5 file by time ratio

cook_fits_spectr_hdu(data_fits, t_fits, f_fits, ...)

avg_1d(x, N)

very simple averaging for 1D array

averaging_stride(arr_query, n_point[, axis, ...])

Perform downsampling of a 2D array by averaging over strided subarrays.

averaging_walk(arr_query, n_point[, axis, start_idx, ...])

Perform downsampling of a 2D array by averaging over contiguous subarrays.

model_flux(calibrator, frequency)

Calculates the model flux for calibration using a known set of calibrators.

partition_avg(arr, ratio_range)

get_cal_bandpass(freq_idx, h5dir, h5name[, ratio_range])

avg_with_lightening_flag(array_dirty, idx_start, idx_end)

It's an averaging process but it can flag-out the time points with local discharges,

lin_interp(x, y, i, half)

FWHM(x, y)

Determine the FWHM position [x] of a distribution [y]

DecayExpTime(x, y)

fit_biGaussian(x, y)

Derive the best fit curve for the flux-time distribution

biGaussian(x, x0, sig1, sig2, A)

mask_extend_xy_npix(mask, n_pix_x, n_pix_y)

Extend a 2D boolean mask along both x and y axes by a specified number of pixels.

flag_frequency_slices(dynspec_cal, mask_cal[, ...])

Flag individual pixels in each frequency slice of the calibration dynamic spectrum.

perform_linear_interpolation(dynspec_cal_copy, ...)

Perform linear interpolation for flagged pixels in each frequency slice of the dynamic spectrum.

proc_calib_dynspec(dynspec_sun, dynspec_cal, time_sun, ...)

Calibrate a dynamic spectrum of the Sun using a given calibration dynamic spectrum.

proc_selfcalib_dynspec(dynspec_sun, time_sun, freq_sun)

Perform self-calibration on a dynamic spectrum of the Sun.

Attributes

device

lofarSun.BF.bftools.device
lofarSun.BF.bftools.h5_fetch_meta(f, SAP='000')

get info from the h5 file

Parameters:
  • f (h5 file) – target h5 file

  • SAP (str, optional) – Sub-Array-Pointing. Defaults to “000”.

Returns:

metadata of the h5 file

Return type:

list

lofarSun.BF.bftools.downsample_h5_seg_by_time_ratio(data_array_uri, t_all, t_ratio_start, t_ratio_end, t_idx_count, t_c_ratio, f_c_ratio, averaging=True, flagging=False, t_idx_cut=256, agg_factor=[1.66, 1.66, 0.45, 0.45], subband_edge=False, subband_ch=16, device=device)

Downsample the h5 file by time ratio

Parameters:
  • data_array_uri (array) – dynamic spectrum

  • t_all (1d array) – all time stamps

  • t_ratio_start (float) – start time ratio (0-1)

  • t_ratio_end (float) – end time ratio (0-1)

  • t_idx_count (int) – number of total time stamps

  • t_c_ratio (int) – time compression ratio

  • f_c_ratio (int) – frequency compression ratio

  • averaging (bool, optional) – averaging or direct sample. Defaults to True.

  • flagging (bool, optional) – flagging or not. Defaults to False.

  • t_idx_cut (int, optional) – factor to contour segment length for processing, to make use of the memory. Defaults to 256.

  • agg_factor (list, optional) – factor for flagging. Defaults to [1.66, 1.66, 0.45, 0.45].

  • subband_edge (bool, optional) – remove one channel every [subband num] channels. Defaults to False.

  • subband_ch (int, optional) – number of subbands per channel. Defaults to 16.

  • device (device, optional) – GPU or CPU, should be something like torch.device(“cuda:0”) . Defaults to device.

Returns:

[averaged dynamic spectrum, time stamps]

Return type:

list

lofarSun.BF.bftools.cook_fits_spectr_hdu(data_fits, t_fits, f_fits, t_start_fits, t_end_fits, stokes_key, antenna_set_name, telescop_name, target_name, pointing_ra, pointing_dec, pointing_x, pointing_y)
lofarSun.BF.bftools.avg_1d(x, N)

very simple averaging for 1D array

Parameters:
  • x (array) – input 1D array

  • N (int) – down-sample ratio

Returns:

averaged array

Return type:

array

lofarSun.BF.bftools.averaging_stride(arr_query, n_point, axis=0, start_idx=-1, end_idx=-1)

Perform downsampling of a 2D array by averaging over strided subarrays.

This function is designed for scenarios where the 2D array is large but the number of points to average (n_point) is small. The resulting array is not significantly smaller than the original.

Parameters:
  • arr_query (numpy.ndarray) – The 2D array to be downsampled.

  • n_point (int) – Number of points in each strided subarray over which the averaging is performed.

  • axis (int, optional) – The axis along which to average, either 0 or 1. Default is 0.

  • start_idx (int, optional) – The starting index for averaging. Default is the first index of the array.

  • end_idx (int, optional) – The ending index for averaging. Default is the last index of the array.

Returns:

The resulting downsampled array.

Return type:

numpy.ndarray

Examples

>>> arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
>>> averaging_stride(arr, 2, axis=0)
Output will be the downsampled array along axis 0.

Notes

This function is optimized for small downsampling ratios where n_point is small.

lofarSun.BF.bftools.averaging_walk(arr_query, n_point, axis=0, start_idx=-1, end_idx=-1)

Perform downsampling of a 2D array by averaging over contiguous subarrays.

This function is designed for scenarios where the 2D array is large and the number of points to average (n_point) is also large. The result is a much smaller array.

Parameters:
  • arr_query (numpy.ndarray) – The 2D array to be downsampled.

  • n_point (int) – Number of points in each subarray over which the averaging is performed.

  • axis (int, optional) – The axis along which to average, either 0 or 1. Default is 0.

  • start_idx (int, optional) – The starting index for averaging. Default is the first index of the array.

  • end_idx (int, optional) – The ending index for averaging. Default is the last index of the array.

Returns:

The resulting downsampled array.

Return type:

numpy.ndarray

Examples

>>> arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
>>> averaging_walk(arr, 2, axis=0)
Output will be the downsampled array along axis 0.

Notes

This function is optimized for large downsampling ratios where n_point is large.

lofarSun.BF.bftools.model_flux(calibrator, frequency)

Calculates the model flux for calibration using a known set of calibrators.

Parameters:
  • calibrator (str) – Name of the calibrator source.

  • frequency (float) – Frequency in MHz for which to calculate the flux.

Returns:

Model flux in sfu (solar flux units).

Return type:

float

Notes

The parameters for each calibrator source are sourced from https://arxiv.org/pdf/1609.05940.pdf.

lofarSun.BF.bftools.partition_avg(arr, ratio_range)
lofarSun.BF.bftools.get_cal_bandpass(freq_idx, h5dir, h5name, ratio_range=[0.2, 0.8])
lofarSun.BF.bftools.avg_with_lightening_flag(array_dirty, idx_start, idx_end, f_avg_range=[1600, 3500], peak_ratio=1.08, stride=96, rm_bandpass=True)

It’s an averaging process but it can flag-out the time points with local discharges, (the very bright and vertical lines)

lofarSun.BF.bftools.lin_interp(x, y, i, half)
lofarSun.BF.bftools.FWHM(x, y)

Determine the FWHM position [x] of a distribution [y]

lofarSun.BF.bftools.DecayExpTime(x, y)
lofarSun.BF.bftools.fit_biGaussian(x, y)

Derive the best fit curve for the flux-time distribution

lofarSun.BF.bftools.biGaussian(x, x0, sig1, sig2, A)
lofarSun.BF.bftools.mask_extend_xy_npix(mask, n_pix_x, n_pix_y)

Extend a 2D boolean mask along both x and y axes by a specified number of pixels.

This function takes a 2D mask and extends the ‘False’ values in both x and y directions based on the number of pixels specified. The purpose is to enlarge masked areas in a 2D array by including adjacent pixels.

Parameters:
  • mask (ndarray (2D)) – A 2D boolean mask where ‘True’ represents areas to keep and ‘False’ represents areas to mask.

  • n_pix_x (int) – Number of pixels to extend the mask in the x-direction.

  • n_pix_y (int) – Number of pixels to extend the mask in the y-direction.

Returns:

mask_extend – The extended 2D mask.

Return type:

ndarray (2D)

lofarSun.BF.bftools.flag_frequency_slices(dynspec_cal, mask_cal, ratio_flag=1.5, lower_perc=15, upper_perc=40)

Flag individual pixels in each frequency slice of the calibration dynamic spectrum.

This function flags pixels based on a condition that compares the pixel value to the mean of a specific range of values in the dynamic spectrum.

Parameters:
  • dynspec_cal (ndarray) – The calibration dynamic spectrum.

  • mask_cal (ndarray) – The existing mask for the calibration dynamic spectrum.

Returns:

mask_cal – Updated mask for the calibration dynamic spectrum.

Return type:

ndarray

lofarSun.BF.bftools.perform_linear_interpolation(dynspec_cal_copy, mask_cal, t_cal)

Perform linear interpolation for flagged pixels in each frequency slice of the dynamic spectrum.

Parameters:
  • dynspec_cal_copy (ndarray) – Copy of the calibration dynamic spectrum that will be modified.

  • mask_cal (ndarray) – The existing mask for the calibration dynamic spectrum.

  • t_cal (ndarray) – Time array corresponding to the calibration dynamic spectrum.

Returns:

dynspec_cal_copy – The modified calibration dynamic spectrum after performing interpolation.

Return type:

ndarray

Examples

>>> dynspec_cal_copy = perform_linear_interpolation(dynspec_cal_copy, mask_cal, t_cal)
lofarSun.BF.bftools.proc_calib_dynspec(dynspec_sun, dynspec_cal, time_sun, freq_sun, t_cal, f_cal, abs_thresh=100000000000000.0)

Calibrate a dynamic spectrum of the Sun using a given calibration dynamic spectrum.

Parameters:
  • dynspec_sun (ndarray) – 2D array representing the dynamic spectrum of the Sun.

  • dynspec_cal (ndarray) – 2D array representing the calibration dynamic spectrum.

  • time_sun (ndarray) – 1D array of time values corresponding to dynspec_sun.

  • freq_sun (ndarray) – 1D array of frequency values corresponding to dynspec_sun.

  • t_cal (ndarray) – 1D array of time values corresponding to dynspec_cal.

  • f_cal (ndarray) – 1D array of frequency values corresponding to dynspec_cal.

  • abs_thresh (float, optional) – Absolute threshold for masking, default is 1e14.

Returns:

  • calibrated_dynspec (ndarray) – Calibrated dynamic spectrum.

  • dynspec_cal (ndarray) – Input calibration dynamic spectrum.

  • dynspec_cal_copy (ndarray) – Dynamic spectrum after interpolation.

  • mask_cal (ndarray) – Mask after initial flagging.

  • mask_cal_2nd (ndarray) – Mask after second-round flagging.

Examples

>>> (calibrated_dynspec, dynspec_cal, dynspec_cal_copy, mask_cal, mask_cal_2nd) = proc_calib_dynspec(dynspec_sun, dynspec_cal, time_sun, freq_sun, t_cal, f_cal)

Notes

  1. Initial masking based on absolute threshold.

  2. Flagging pixels in each frequency slice.

  3. Extending the mask spatially.

  4. Linear interpolation for flagged pixels.

  5. Second round of flagging based on snapshot median.

  6. Interpolation after second-round flagging.

  7. Averaging the dynamic spectrum.

  8. Final calibration using the average dynamic spectrum.

lofarSun.BF.bftools.proc_selfcalib_dynspec(dynspec_sun, time_sun, freq_sun, abs_thresh=200000000000000.0)

Perform self-calibration on a dynamic spectrum of the Sun.

This function takes a dynamic spectrum of the Sun and performs various steps to calibrate it, including masking based on thresholds, flagging, interpolation, and averaging. The calibration is done in a ‘self-calibration’ mode, meaning it uses the Sun’s own dynamic spectrum to create a bandpass for calibration.

Parameters:
  • dynspec_sun (ndarray) – Dynamic spectrum of the Sun.

  • time_sun (ndarray) – Time array corresponding to dynspec_sun.

  • freq_sun (ndarray) – Frequency array corresponding to dynspec_sun.

Returns:

  • calibrated_dynspec (ndarray) – Calibrated dynamic spectrum.

  • mask_cal (ndarray) – Mask after initial flagging and extending.

  • dynspec_cal_bp (ndarray) – Averaged dynamic spectrum used for bandpass calculation.

Examples

>>> calibrated_dynspec, mask_cal, dynspec_cal_bp = proc_selfcalib_dynspec(dynspec_sun, time_sun, freq_sun)
# size of dynspec_sun(M*N), time_sun(M), freq_sun(N) should match

Notes

  1. Handle NaN values in the Sun’s dynamic spectrum.

  2. Create a copy of the Sun’s dynamic spectrum for bandpass calculation.

  3. Average the copied dynamic spectrum.

  4. Mask the averaged spectrum based on an absolute threshold.

  5. Perform further flagging of frequency slices.

  6. Extend the mask in time and frequency.

  7. Interpolate the masked values.

  8. Perform a second round of flagging based on snapshot median.

  9. Interpolate again after the second round of flagging.

  10. Average the dynamic spectrum for bandpass calculation.

  11. Calibrate each frequency slice.