def test_bin_edge_summary(): esb = _equally_spaced_bins() center = bin_edge_summary(esb, "center") assert center.shape == (100, ) assert center[0] == 1.005 assert center[99] == 1.995 left = bin_edge_summary(esb, "left") assert left.shape == (100, ) assert left[0] == 1.0 assert left[99] == 1.99 right = bin_edge_summary(esb, "right") assert right.shape == (100, ) assert right[0] == 1.01 assert right[99] == 2.0 # Correct selection of summary type with pytest.raises(ValueError): bin_edge_summary(esb, "should raise the error") # The correct shape of bin edges are passed in with pytest.raises(ValueError): bin_edge_summary(np.arange(0, 10), "center") with pytest.raises(ValueError): bin_edge_summary(np.zeros((3, 4)), "center")
def intensity_enhance(smap, radial_bin_edges, scale=None, summarize_bin_edges="center", summary=np.mean, degree=1, normalization_radius=1 * u.R_sun, fit_range=[1, 1.5] * u.R_sun, **summary_kwargs): """ Returns a map with the off-limb emission enhanced. The enhancement is calculated as follows. A summary statistic of the radial dependence of the off-limb emission is calculated. Since the UV and EUV emission intensity drops of quickly off the solar limb, it makes sense to fit the log of the intensity statistic using some appropriate function. The function we use here is a polynomial. To calculate the enhancement, the fitted function is normalized to its value at the normalization radius from the center of the Sun (a sensible choice is the solar radius). The offlimb emission is then divided by this normalized function. Note that after enhancement plot settings such as the image normalization may have to be changed in order to obtain a good-looking plot. Parameters ---------- smap : `sunpy.map.Map` A SunPy map radial_bin_edges : `astropy.units.Quantity` A two-dimensional array of bin edges of size [2, nbins] where nbins is the number of bins. scale : None | `astropy.units.Quantity` The radius of the Sun expressed in map units. For example, in typical helioprojective Cartesian maps the solar radius is expressed in units of arcseconds. If None, then the map scale is used. summarize_bin_edges : `str` How to summarize the bin edges. summary : `function` A function that returns a summary statistic of the radial intensity, for example `~numpy.mean` and `~numpy.median`. degree : `int` Degree of the polynomial fit to the log of the intensity as a function of radius. normalization_radius : `astropy.units.Quantity` The radius at which the enhancement has value 1. For most cases the value of the enhancement will increase as a function of radius. fit_range : `astropy.units.Quantity` Array like with 2 elements defining the range of radii over which the polynomial function is fit. The preferred units are solar radii. summary_kwargs : `dict` Keywords applicable to the summary function. Returns ------- new_map : `sunpy.map.Map` A SunPy map that has the emission above the normalization radius enhanced. """ # Get the radii for every pixel map_r = find_pixel_radii(smap).to(u.R_sun) # Get the radial intensity distribution radial_intensity = get_radial_intensity_summary(smap, radial_bin_edges, scale=scale, summary=summary, **summary_kwargs) # Summarize the radial bins radial_bin_summary = bin_edge_summary(radial_bin_edges, summarize_bin_edges).to(u.R_sun) # Fit range if fit_range[0] >= fit_range[1]: raise ValueError("The fit range must be strictly increasing.") fit_here = np.logical_and( fit_range[0].to(u.R_sun).value <= radial_bin_summary.to(u.R_sun).value, radial_bin_summary.to(u.R_sun).value <= fit_range[1].to(u.R_sun).value, ) # Fits a polynomial function to the natural logarithm of an estimate of # the intensity as a function of radius. polynomial = fit_polynomial_to_log_radial_intensity( radial_bin_summary[fit_here], radial_intensity[fit_here], degree) # Calculate the enhancement enhancement = 1 / normalize_fit_radial_intensity(map_r, polynomial, normalization_radius) enhancement[map_r < normalization_radius] = 1 # Return a map with the intensity enhanced above the normalization radius # and the same meta data as the input map. return sunpy.map.Map(smap.data * enhancement, smap.meta)