def _find_melting_layer_from_v(v_prof: np.ndarray, width_prof: Union[np.ndarray, None], height: np.ndarray) -> Union[np.ndarray, None]: v = np.copy(v_prof[:-1]) v_diff = np.diff(v_prof) v[v_diff < 0] = 0 v[v_diff > 0] = 1 n_increasing = utils.cumsumr(v) try: top = int(np.argmax(n_increasing)) base = np.where(n_increasing[:top] == 0)[0][-1] except IndexError: return None if width_prof is not None: conditions = [ width_prof[base] - width_prof[top] > 0.2, v_prof[top] - v_prof[base] > 0.5, 50 < (height[top] - height[base]) < 1000, v_prof[base] < -2, ] else: conditions = [ v_prof[top] - v_prof[base] > 2, 50 < (height[top] - height[base]) < 1000, v_prof[base] < -2, ] if all(conditions): base = int(round(top - (top - base) / 2)) return np.arange(base, top) return None
def calc_adiabatic_lwc(lwc_change_rate: np.ndarray, dheight: float) -> np.ndarray: """Calculates adiabatic liquid water content (g/m3). Args: lwc_change_rate: Liquid water content change rate (g/m3/m) calculated at the base of each cloud and filled to that cloud. dheight: Median difference of the height vector (m). Returns: Liquid water content (g/m3). """ is_liquid = lwc_change_rate != 0 ind_from_base = utils.cumsumr(is_liquid, axis=1) return ind_from_base * dheight * lwc_change_rate
def _mask_low_values_above_consequent_negatives( data: np.ndarray, n_negatives: int = 5, threshold: float = 8e-6, n_gates: int = 95, n_skip_lowest: int = 5, ) -> np.ndarray: negative_data = data[:, n_skip_lowest:n_gates + n_skip_lowest] < 0 n_consequent_negatives = utils.cumsumr(negative_data, axis=1) time_indices, alt_indices = np.where( n_consequent_negatives > n_negatives) alt_indices += n_skip_lowest for time_ind, alt_ind in zip(time_indices, alt_indices): profile = data[time_ind, alt_ind:] profile[profile < threshold] = ma.masked cleaned_time_indices = np.unique(time_indices) logging.info( f"Cleaned {len(cleaned_time_indices)} profiles with negative filter" ) return cleaned_time_indices
def test_cumsumr_4(): x = np.array([[0, 1, 1, 0], [0, 5, 0, 0]]) res = np.array([[0, 1, 1, 0], [0, 6, 0, 0]]) assert_array_almost_equal(utils.cumsumr(x), res)
def test_cumsumr_2(): x = np.array([[0, 1, 1, 0], [0, 5, 0, 0]]) res = np.array([[0, 1, 2, 0], [0, 5, 0, 0]]) assert_array_almost_equal(utils.cumsumr(x, axis=1), res)
def test_cumsumr_1(): x = np.array([0, 1, 2, 0, 1, 1]) res = np.array([0, 1, 3, 0, 1, 2]) assert_array_almost_equal(utils.cumsumr(x), res)