def fwhms(self):
"""
This function ...
:return:
"""
# Initialize a list to contain the fwhm of the fitted stars
fwhms = []
# Loop over all stars
for star in self.stars:
# If the star contains a model, add the fwhm of that model to the list
if star.has_model:
fwhm_pix = star.fwhm * Unit("pix")
fwhm_arcsec = fwhm_pix * self.frame.average_pixelscale.to("arcsec/pix")
fwhms.append(fwhm_arcsec)
# Return the list
return fwhms
# -----------------------------------------------------------------
python类Unit()的实例源码
def pixelscale(self):
"""
This function ...
:return:
"""
result = utils.proj_plane_pixel_scales(self)
# returns: A vector (ndarray) of projection plane increments corresponding to each pixel side (axis).
# The units of the returned results are the same as the units of cdelt, crval, and cd for the celestial WCS
# and can be obtained by inquiring the value of cunit property of the input WCS WCS object.
x_pixelscale = result[0] * Unit("deg/pix")
y_pixelscale = result[1] * Unit("deg/pix")
# Return the pixel scale as an extent
return Pixelscale(x_pixelscale, y_pixelscale)
# -----------------------------------------------------------------
def from_pixel(cls, circle, wcs):
"""
This function ...
:param circle:
:param wcs:
:return:
"""
center = SkyCoordinate.from_pixel(circle.center, wcs)
# Get the pixelscale
radius = circle.radius * Unit("pix") * wcs.average_pixelscale
# Create a new SkyCircle
return cls(center, radius, meta=circle.meta)
# -----------------------------------------------------------------
def from_pixel(cls, rectangle, wcs):
"""
This function ...
:param rectangle:
:param wcs:
:return:
"""
center = SkyCoordinate.from_pixel(rectangle.center, wcs)
# Get the pixelscale
radius = rectangle.radius * Unit("pix") * wcs.average_pixelscale
# Create a new SkyRectangle
return cls(center, radius, rectangle.angle, meta=rectangle.meta)
# -----------------------------------------------------------------
def __init__(self, wavelengths=None, attenuations=None):
"""
This function ...
:param wavelengths:
:param attenuations:
:return:
"""
# Column names
names = ["Wavelength", "Attenuation"]
# Create the table
if wavelengths is None or attenuations is None: self.table = Table(names=names, dtype=('f8', 'f8'))
else: self.table = tables.new([wavelengths, attenuations], names)
# Set column units
self.table["Wavelength"].unit = Unit("micron")
# -----------------------------------------------------------------
def luminosity_for_filter(self, filter, unit="W/micron"):
"""
This function ...
:param filter:
:param unit:
:return:
"""
# Convole the Sun SED over the filter transmission curve
luminosity = filter.convolve(self.sed.wavelengths(unit="micron", asarray=True), self.sed.luminosities(unit="W/micron", asarray=True)) # also in W/micron
luminosity = luminosity * Unit("W/micron")
# Return the luminosity
return luminosity.to(unit)
# -----------------------------------------------------------------
def from_file(cls, path, skiprows=0):
"""
This function ...
:param path:
:param skiprows:
:return:
"""
# Create a new SED
sed = cls()
wavelength_column, luminosity_column = np.loadtxt(path, dtype=float, unpack=True, skiprows=skiprows)
sed.table = tables.new([wavelength_column, luminosity_column], ["Wavelength", "Luminosity"])
sed.table["Wavelength"].unit = Unit("micron")
sed.table["Luminosity"].unit = Unit("W/micron")
# Return the SED
return sed
# -----------------------------------------------------------------
def luminosity_for_filter(self, filter, unit="W/micron"):
"""
This function ...
:param filter:
:param unit:
:return:
"""
#luminosity = filter.integrate(self.sed["Wavelength"], self.sed["Luminosity"])
luminosity = filter.convolve(self.sed.wavelengths(unit="micron", asarray=True), self.sed.luminosities(unit="W/micron", asarray=True)) # also in W/micron
luminosity = luminosity * Unit("W/micron")
# Return the luminosity in the desired unit
return luminosity.to(unit)
# -----------------------------------------------------------------
def get_quantity(entry, default_unit=None):
"""
This function ...
:param entry:
:param default_unit:
:return:
"""
splitted = entry.split()
value = float(splitted[0])
try: unit = splitted[1]
except IndexError: unit = default_unit
# Create a quantity object and return it
if unit is not None: value = value * Unit(unit)
return value
# -----------------------------------------------------------------
def get_quantity(entry, default_unit=None):
"""
This function ...
:param entry:
:param default_unit:
:return:
"""
splitted = entry.split()
value = float(splitted[0])
try: unit = splitted[1]
except IndexError: unit = default_unit
# Create a quantity object and return it
if unit is not None: value = value * Unit(unit)
return value
# -----------------------------------------------------------------
def spectral_factor(wavelength, wavelength_unit, frequency_unit):
"""
This function ...
:param wavelength:
:param wavelength_unit:
:param frequency_unit:
:return:
"""
# Convert string units to Unit objects
if isinstance(wavelength_unit, basestring): wavelength_unit = u.Unit(wavelength_unit)
if isinstance(frequency_unit, basestring): frequency_unit = u.Unit(frequency_unit)
conversion_factor_unit = wavelength_unit / frequency_unit
# Calculate the conversion factor
return (wavelength**2 / speed_of_light).to(conversion_factor_unit).value
# -----------------------------------------------------------------
def setup(self, image):
"""
This function ...
:param image:
:return:
"""
# Call the setup function of the base class
super(UnitConverter, self).setup()
# Create a unit object
self.target_unit = u.Unit(self.config.to_unit)
# Set the image reference
self.image = image
# -----------------------------------------------------------------
def get_quantity(entry, default_unit=None):
"""
This function ...
:param entry:
:param default_unit:
:return:
"""
splitted = entry.split()
value = float(splitted[0])
try: unit = splitted[1]
except IndexError: unit = default_unit
# Create a quantity object and return it
if unit is not None: value = value * Unit(unit)
return value
# -----------------------------------------------------------------
def project_azimuth_to_pa(azimuth, inclination):
"""
This function ...
:param azimuth:
:param inclination:
"""
# Get the azimuth angle and inclination in radians
azimuth_radian = azimuth.to("radian").value
i_radian = inclination.to("radian").value
denominator = math.sqrt(math.cos(azimuth_radian)**2 * math.cos(i_radian)**2 + math.sin(azimuth_radian)**2)
cos_pa = math.cos(azimuth_radian) * math.cos(i_radian) / denominator
sin_pa = math.sin(azimuth_radian) / denominator
pa_radian = math.atan2(sin_pa, cos_pa) * Unit("radian")
return pa_radian.to("deg")
# -----------------------------------------------------------------
def deproject_pa_to_azimuth(pa, inclination):
"""
This function ...
:param pa:
:param inclination:
:return:
"""
# Get the PA and inclination in radians
pa_radian = pa.to("radian").value
i_radian = inclination.to("radian").value
denominator = math.sqrt(math.cos(pa_radian)**2 + math.sin(pa_radian)**2 * math.cos(i_radian)**2)
cos_azimuth = math.cos(pa_radian) / denominator
sin_azimuth = math.sin(pa_radian) * math.cos(inclination) / denominator
azimuth_radian = math.atan2(sin_azimuth, cos_azimuth) * Unit("radian")
return azimuth_radian.to("deg")
# -----------------------------------------------------------------
def project_tilt_to_pa(tilt, inclination):
"""
This function ...
:param tilt:
:param inclination:
:return:
"""
# Get the tilt angle and inclination in radians
tilt_radian = tilt.to("radian").value
i_radian = inclination.to("radian").value
denominator = math.sqrt(math.sin(tilt_radian)**2 * math.sin(i_radian)**2 + math.cos(tilt_radian)**2)
cos_pa = math.sin(tilt_radian) * math.sin(i_radian) / denominator
sin_pa = math.cos(tilt_radian) / denominator
pa_radian = math.atan2(sin_pa, cos_pa) * Unit("radian")
return pa_radian.to("deg")
# -----------------------------------------------------------------
def spectral_factor_hz_to_micron(wavelength):
"""
This function ...
:param wavelength:
:return:
"""
wavelength_unit = "micron"
frequency_unit = "Hz"
# Convert string units to Unit objects
if isinstance(wavelength_unit, basestring): wavelength_unit = Unit(wavelength_unit)
if isinstance(frequency_unit, basestring): frequency_unit = Unit(frequency_unit)
conversion_factor_unit = wavelength_unit / frequency_unit
# Calculate the conversion factor
factor = (wavelength ** 2 / speed_of_light).to(conversion_factor_unit).value
return 1. / factor
# -----------------------------------------------------------------
def fluxdensity_to_luminosity(fluxdensity, wavelength, distance):
"""
This function ...
:param fluxdensity:
:param wavelength:
:param distance:
:return:
"""
luminosity = (fluxdensity * 4. * math.pi * distance ** 2.).to("W/Hz")
# 3 ways:
#luminosity_ = luminosity.to("W/micron", equivalencies=spectral_density(wavelength)) # does not work
luminosity_ = (speed_of_light * luminosity / wavelength**2).to("W/micron")
luminosity = luminosity.to("W/Hz").value * spectral_factor_hz_to_micron(wavelength) * Unit("W/micron")
#print(luminosity_, luminosity) # is OK!
return luminosity
# -----------------------------------------------------------------
def _make_quantity(data, unit):
"""
Get a LogQuantity if the a LogUnit is used rather than a regular Quantity.
Parameters
----------
data: numpy.ndarray
the data
unit: ~astropy.unit.Unit or ~astropy.unit.LogUnit
The data units
Returns
-------
~astropy.unit.Quantity or ~astropy.unit.LogQuantity
depending on the unit type
"""
if isinstance(unit, LogUnit):
return LogQuantity(data, unit=unit)
else:
return Quantity(data, unit=unit)
def _custom_unit_changed(self):
if self.custom_units is None:
return
unit = self.custom_units.text()
if unit == '':
self.label_status.setText('')
else:
try:
u.Unit(unit)
except:
self.label_status.setText('Invalid units')
self.label_status.setStyleSheet('color: red')
else:
self.label_status.setText('Valid units')
self.label_status.setStyleSheet('color: green')