From 66f0bb691d7959629537536356b179741afaffaf Mon Sep 17 00:00:00 2001 From: David Law Date: Thu, 17 Oct 2024 15:48:41 -0400 Subject: [PATCH] Code style cleanup --- src/stcal/jump/jump.py | 175 ++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 98 deletions(-) diff --git a/src/stcal/jump/jump.py b/src/stcal/jump/jump.py index 5a0fe9d6..3a6e2b4e 100644 --- a/src/stcal/jump/jump.py +++ b/src/stcal/jump/jump.py @@ -304,51 +304,6 @@ def detect_jumps( dqflags['DO_NOT_USE'] gdq[gdq == np.bitwise_or(dqflags['SATURATED'], dqflags['JUMP_DET'])] = \ dqflags['SATURATED'] - # This is the flag that controls the flagging of snowballs. - if expand_large_events: - gdq, total_snowballs = flag_large_events( - gdq, - jump_flag, - sat_flag, - min_sat_area=min_sat_area, - min_jump_area=min_jump_area, - expand_factor=expand_factor, - sat_required_snowball=sat_required_snowball, - min_sat_radius_extend=min_sat_radius_extend, - edge_size=edge_size, - sat_expand=sat_expand, - max_extended_radius=max_extended_radius, - mask_persist_grps_next_int=mask_persist_grps_next_int, - persist_grps_flagged=persist_grps_flagged, - ) - log.info("Total snowballs = %i", total_snowballs) - number_extended_events = total_snowballs - if find_showers: - gdq, num_showers = find_faint_extended( - data, - gdq, - pdq, - readnoise_2d, - frames_per_group, - minimum_sigclip_groups, - dqflags, - snr_threshold=extend_snr_threshold, - min_shower_area=extend_min_area, - inner=extend_inner_radius, - outer=extend_outer_radius, - sat_flag=sat_flag, - jump_flag=jump_flag, - ellipse_expand=extend_ellipse_expand_ratio, - num_grps_masked=grps_masked_after_shower, - max_extended_radius=max_extended_radius, - ) - log.info("Total showers= %i", num_showers) - number_extended_events = num_showers - - # This is where we look for micrometeorite flashes if the triggering - # threshold is less than the entire array - if (mmflashfrac < 1): - gdq = find_micrometeorite_flashes(gdq, jump_flag, mmflashfrac) else: yinc = int(n_rows // n_slices) @@ -475,51 +430,53 @@ def detect_jumps( gdq[gdq == np.bitwise_or(dqflags['SATURATED'], dqflags['JUMP_DET'])] = \ dqflags['SATURATED'] - # This is the flag that controls the flagging of snowballs. - if expand_large_events: - gdq, total_snowballs = flag_large_events( - gdq, - jump_flag, - sat_flag, - min_sat_area=min_sat_area, - min_jump_area=min_jump_area, - expand_factor=expand_factor, - sat_required_snowball=sat_required_snowball, - min_sat_radius_extend=min_sat_radius_extend, - edge_size=edge_size, - sat_expand=sat_expand, - max_extended_radius=max_extended_radius, - mask_persist_grps_next_int=mask_persist_grps_next_int, - persist_grps_flagged=persist_grps_flagged, - ) - log.info("Total snowballs = %i", total_snowballs) - number_extended_events = total_snowballs - if find_showers: - gdq, num_showers = find_faint_extended( - data, - gdq, - pdq, - readnoise_2d, - frames_per_group, - minimum_sigclip_groups, - dqflags, - snr_threshold=extend_snr_threshold, - min_shower_area=extend_min_area, - inner=extend_inner_radius, - outer=extend_outer_radius, - sat_flag=sat_flag, - jump_flag=jump_flag, - ellipse_expand=extend_ellipse_expand_ratio, - num_grps_masked=grps_masked_after_shower, - max_extended_radius=max_extended_radius, - ) - log.info("Total showers= %i", num_showers) - number_extended_events = num_showers + # Look for snowballs in near-IR data + if expand_large_events: + gdq, total_snowballs = flag_large_events( + gdq, + jump_flag, + sat_flag, + min_sat_area=min_sat_area, + min_jump_area=min_jump_area, + expand_factor=expand_factor, + sat_required_snowball=sat_required_snowball, + min_sat_radius_extend=min_sat_radius_extend, + edge_size=edge_size, + sat_expand=sat_expand, + max_extended_radius=max_extended_radius, + mask_persist_grps_next_int=mask_persist_grps_next_int, + persist_grps_flagged=persist_grps_flagged, + ) + log.info("Total snowballs = %i", total_snowballs) + number_extended_events = total_snowballs + + # Look for showers in mid-IR data + if find_showers: + gdq, num_showers = find_faint_extended( + data, + gdq, + pdq, + readnoise_2d, + frames_per_group, + minimum_sigclip_groups, + dqflags, + snr_threshold=extend_snr_threshold, + min_shower_area=extend_min_area, + inner=extend_inner_radius, + outer=extend_outer_radius, + sat_flag=sat_flag, + jump_flag=jump_flag, + ellipse_expand=extend_ellipse_expand_ratio, + num_grps_masked=grps_masked_after_shower, + max_extended_radius=max_extended_radius, + ) + log.info("Total showers= %i", num_showers) + number_extended_events = num_showers - # This is where we look for micrometeorite flashes if the triggering - # threshold is less than the entire array - if (mmflashfrac < 1): - gdq = find_micrometeorite_flashes(gdq, jump_flag, mmflashfrac) + # Look for micrometeorite flashes if the triggering + # threshold is less than the entire array + if (mmflashfrac < 1): + gdq = find_micrometeorite_flashes(gdq, jump_flag, mmflashfrac) elapsed = time.time() - start log.info("Total elapsed time = %g sec", elapsed) @@ -532,20 +489,42 @@ def detect_jumps( # Return the updated data quality arrays return gdq, pdq, total_primary_crs, number_extended_events, stddev -# Find micrometeorite flashes that light up the entire array in a small number -# of groups. Do this by looking for cases where greater than mmflash_pct percent -# of the array is flagged as 'JUMP_DET' and flag the entire array. +# Find micrometeorite flashes def find_micrometeorite_flashes(gdq, jump_flag, mmflashfrac): - npix = gdq.shape[2]*gdq.shape[3] # Number of pixels in array + """ + This routine looks for micrometeorite flashes that light up the entire array in a small number + of groups. It does this by looking for cases where greater than mmflash_pct percent + of the array is flagged as 'JUMP_DET', and flags the entire array as JUMP_DET in such cases. + + Parameters + ---------- + gdq : int, 4D array + Group dq array + jump_flag : int + DQ flag for jump detection. + mmflashfrac : float + Fraction of the array that can be flagged as a jump before the entire array + will be flagged. + Returns + ------- + gdq : int, 4D array + Updated group dq array + + """ + log.info("Looking for micrometeorite flashes") + + nints, ngroups, nrows, ncols = gdq.shape + + npix = nrows * ncols # Number of pixels in array # Loop over integrations - for ii in range(0,gdq.shape[0]): + for ii in range(0, nints): # Loop over groups - for jj in range(0,gdq.shape[1]): - indx = np.where(gdq[ii,jj,:,:] & jump_flag != 0) + for jj in range(0, ngroups): + indx = np.where(gdq[ii, jj, :, :] & jump_flag != 0) fraction = float(len(indx[0])) / npix if (fraction > mmflashfrac): - gdq[ii,jj,:,:] = gdq[ii,jj,:,:] | jump_flag - log.info("Detected a micrometeorite flash in integ = %i, group= %i", ii, jj) + gdq[ii, jj, :, :] = gdq[ii, jj, :, :] | jump_flag + log.info(f"Detected a micrometeorite flash in integ = {ii}, group = {jj}") return gdq def flag_large_events(