def _calc_error(scale, weights, error_input, add_mu=False, add_mu_small=False): error = utils.l2norm_weighted(error_input, scale, weights) if add_mu: error = utils.l2norm(error, MU_ERROR) if add_mu_small: error = utils.l2norm(error, MU_ERROR_SMALL) return error
def _calc_error( scale: float, weights: tuple, error_input: tuple, add_mu: bool = False, add_mu_small: bool = False, ) -> ma.MaskedArray: error = utils.l2norm_weighted(error_input, scale, weights) if add_mu is True: error = utils.l2norm(error, MU_ERROR) if add_mu_small is True: error = utils.l2norm(error, MU_ERROR_SMALL) return error
def test_calc_lwc_relative_error(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) x = l2norm(*np.gradient(ERROR_OBJ.lwc)) compare = x / ERROR_OBJ.lwc / 2 compare[compare > 5] = 5 assert_array_almost_equal(ERROR_OBJ._calc_lwc_relative_error(), compare)
def _init_lwp_error(self) -> None: random_error, bias = 0.25, 20 lwp_error = utils.l2norm(self.data["lwp"][:] * random_error, bias) self.append_data(lwp_error, "lwp_error", units="g m-2") self.data["lwp_error"].comment = (( f"This variable is a rough estimate of the one-standard-deviation error in liquid\n" f"water path, calculated as a combination of a {bias} g m-2 linear error and a\n" f"{round(random_error*100)} % fractional error."), )
def test_calc_combined_error(): from cloudnetpy.utils import l2norm, transpose err_2d = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) err_1d = np.array([0.3, 0.2]) expected = l2norm(err_2d, transpose(err_1d)) assert_array_equal(ERROR_OBJ._calc_combined_error(err_2d, err_1d), expected)
def _calc_error(): z_precision = 4.343 * (1 / np.sqrt(_number_of_pulses()) + utils.db2lin(z_power_min - z_power) / 3) gas_error = attenuations['radar_gas_atten'] * 0.1 liq_error = attenuations['liquid_atten_err'].filled(0) z_error = utils.l2norm(gas_error, liq_error, z_precision) z_error[attenuations['liquid_uncorrected']] = ma.masked return z_error
def _calc_horizontal_wind(self): """Calculates magnitude of horizontal wind. Returns: ndarray: Horizontal wind (m s-1). """ u_wind, v_wind = p_tools.interpolate_model(self.cat_file, ['uwind', 'vwind']) return utils.l2norm(u_wind, v_wind)
def _calc_error() -> np.ndarray: if 'width' not in self.data: return 0.3 z_precision = 4.343 * (1 / np.sqrt(_number_of_independent_pulses()) + utils.db2lin(z_power_min - z_power) / 3) gas_error = attenuations['radar_gas_atten'] * 0.1 liq_error = attenuations['liquid_atten_err'].filled(0) z_error = utils.l2norm(gas_error, liq_error, z_precision) z_error[attenuations['liquid_uncorrected']] = ma.masked return z_error
def test_calc_v_sigma_factor(drizzle_cat_file): obj = drizzle.SpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables["height"][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables["uwind"][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables["vwind"][:] beam = height * np.deg2rad(0.5) wind = utils.l2norm(uwind, vwind) a_wind = (wind + beam)**(2 / 3) s_wind = (30 * wind + beam)**(2 / 3) expected = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), expected)
def _append_iwc_error(iwc_data, ice_class): """Estimates error of ice water content.""" def _calc_random_error(): scaled_temperature = iwc_data.coeffs.ZT * iwc_data.temperature scaled_temperature += iwc_data.coeffs.Z return iwc_data.getvar('Z_error') * scaled_temperature * 10 def _calc_error_in_uncorrected_ice(): lwp_prior = 250 # g / m-2 liq_atten_scaled = iwc_data.spec_liq_atten * iwc_data.coeffs.Z return lwp_prior * liq_atten_scaled * 2 * 1e-3 * 10 retrieval_uncertainty = 1.7 # dB random_error = _calc_random_error() error_uncorrected = _calc_error_in_uncorrected_ice() iwc_error = utils.l2norm(retrieval_uncertainty, random_error) iwc_error[ice_class.uncorrected_ice] = utils.l2norm( retrieval_uncertainty, error_uncorrected) iwc_error[(~ice_class.is_ice | ice_class.ice_above_rain)] = ma.masked iwc_data.append_data(iwc_error, 'iwc_error')
def _calc_error() -> Union[np.ndarray, float]: if "width" not in self.data: return 0.3 z_precision = 4.343 * ( 1 / np.sqrt(_number_of_independent_pulses()) + utils.db2lin(z_power_min - z_power) / 3 ) gas_error = attenuations["radar_gas_atten"] * 0.1 liq_error = attenuations["liquid_atten_err"].filled(0) z_error = utils.l2norm(gas_error, liq_error, z_precision) z_error[attenuations["liquid_uncorrected"]] = ma.masked return z_error
def _calc_horizontal_wind(self): """Calculates magnitude of horizontal wind. Returns: ndarray: Horizontal wind (m s-1). """ atmosphere = product_tools.interpolate_model(self.cat_file, ["uwind", "vwind"]) u_wind = atmosphere["uwind"] v_wind = atmosphere["vwind"] return utils.l2norm(u_wind, v_wind)
def append_error(self, ice_classification: IceClassification) -> None: """Estimates error of ice water content.""" def _calc_random_error() -> np.ndarray: scaled_temperature = self.coeffs.ZT * self.temperature scaled_temperature += self.coeffs.Z return self.getvar('Z_error') * scaled_temperature * 10 def _calc_error_in_uncorrected_ice() -> np.ndarray: spec_liq_atten = 1.0 if self.wl_band == 0 else 4.5 liq_atten_scaled = spec_liq_atten * self.coeffs.Z lwp_prior = 250 # g / m-2 return lwp_prior * liq_atten_scaled * 2 * 1e-3 * 10 retrieval_uncertainty = 1.7 # dB random_error = _calc_random_error() error_uncorrected = _calc_error_in_uncorrected_ice() iwc_error = utils.l2norm(retrieval_uncertainty, random_error) iwc_error[ice_classification.uncorrected_ice] = utils.l2norm( retrieval_uncertainty, error_uncorrected) iwc_error[(~ice_classification.is_ice | ice_classification.ice_above_rain)] = ma.masked self.append_data(iwc_error, 'iwc_error')
def test_l2_norm(a, b, result): assert_array_almost_equal(utils.l2norm(a, b), result)
def _calc_n_error(): z_error = error_input[0] dia_error = db2lin(results['Do_error']) n_error = utils.l2norm(z_error, 6 * dia_error) return _stack_errors(n_error, drizzle_indices)
def _calc_combined_error(error_2d, error_1d): error_1d_transposed = utils.transpose(error_1d) return utils.l2norm(error_2d, error_1d_transposed)
def _calc_combined_error(error_2d: np.ndarray, error_1d: np.ndarray) -> np.ndarray: error_1d_transposed = utils.transpose(error_1d) return utils.l2norm(error_2d, error_1d_transposed)
def _calc_lwc_gradient(self): gradient_elements = np.gradient(self.lwc.filled(0)) return utils.l2norm(*gradient_elements)
def _calc_lwc_gradient(self) -> np.ndarray: assert isinstance(self.lwc, ma.MaskedArray) gradient_elements = np.gradient(self.lwc.filled(0)) return utils.l2norm(*gradient_elements)
def _calc_n_error() -> ma.MaskedArray: z_error = error_input[0] dia_error = db2lin(results["Do_error"]) n_error = utils.l2norm(z_error, 6 * dia_error) return _stack_errors(n_error, drizzle_indices)
def test_calc_lwc_gradient(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) expected = l2norm(*np.gradient(ERROR_OBJ.lwc)) assert_array_almost_equal(ERROR_OBJ._calc_lwc_gradient(), expected)
def test_calc_horizontal_wind(drizzle_cat_file): obj = drizzle.SpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables["uwind"][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables["vwind"][:] expected = utils.l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), expected)
def _init_lwp_error(self): # TODO: Check these error values random_error, bias = 0.25, 50 lwp_error = utils.l2norm(self.data['lwp'][:]*random_error, bias) self.append_data(lwp_error, 'lwp_error', units='g m-2')