Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Create a new WCS from provided tabular data. This defaults to being
a GWCS object.
"""
orig_array = u.Quantity(array)
# TODO: Input arrays must be strictly ascending. This is not always the
# case for a spectral axis (e.g. when in frequency space). Thus, we
# convert to wavelength to create the wcs.
if orig_array.unit.physical_type != 'length' and \
orig_array.unit.is_equivalent(u.AA, equivalencies=u.spectral()):
array = orig_array.to(u.AA, equivalencies=u.spectral())
coord_frame = cf.CoordinateFrame(naxes=1,
axes_type=('SPECTRAL',),
axes_order=(0,))
spec_frame = cf.SpectralFrame(unit=array.unit, axes_order=(0,))
# In order for the world_to_pixel transformation to automatically convert
# input units, the equivalencies in the look up table have to be extended
# with spectral unit information.
SpectralTabular1D = type("SpectralTabular1D", (Tabular1D,),
{'input_units_equivalencies': {'x0': u.spectral()}})
forward_transform = SpectralTabular1D(np.arange(len(array)),
lookup_table=array)
forward_transform.inverse = SpectralTabular1D(
array, lookup_table=np.arange(len(array)))
class SpectralGWCS(GWCS):
def pixel_to_world(self, *args, **kwargs):
return super().pixel_to_world(*args, **kwargs).to(
orig_array.unit, equivalencies=u.spectral())
def tabular_wcs(xarray):
coordinateframe = cf.CoordinateFrame(naxes=1, axes_type=('SPECTRAL',),
axes_order=(0,))
specframe = cf.SpectralFrame(unit=xarray.unit, axes_order=(0,))
transform = Tabular1D(np.arange(len(xarray)), xarray.value)
tabular_gwcs = gwcs.wcs.WCS([(coordinateframe, transform), (specframe, None)])
return tabular_gwcs
def from_array(array):
"""
Create a new WCS from provided tabular data. This defaults to being
a GWCS object.
"""
array = u.Quantity(array)
coord_frame = cf.CoordinateFrame(naxes=1,
axes_type=('SPECTRAL',),
axes_order=(0,))
spec_frame = cf.SpectralFrame(unit=array.unit, axes_order=(0,))
forward_transform = Tabular1D(np.arange(len(array)), array.value)
forward_transform.inverse = Tabular1D(array.value, np.arange(len(array)))
tabular_gwcs = gwcs.wcs.WCS(forward_transform=forward_transform,
input_frame=coord_frame,
output_frame=spec_frame)
return WCSWrapper(wcs=tabular_gwcs)
def __init__(self, axes_order=(0,), reference_frame=None, unit=None,
axes_names=None, name=None, axis_physical_types=None,
reference_position=None):
super(SpectralFrame, self).__init__(naxes=1, axes_type="SPECTRAL", axes_order=axes_order,
axes_names=axes_names, reference_frame=reference_frame,
unit=unit, name=name,
reference_position=reference_position,
axis_physical_types=axis_physical_types)
elif isinstance(self, CelestialFrame):
if isinstance(self.reference_frame, coord.Galactic):
ph_type = "pos.galactic.lon", "pos.galactic.lat"
elif isinstance(self.reference_frame, (coord.GeocentricTrueEcliptic,
coord.GCRS,
coord.PrecessedGeocentric)):
ph_type = "pos.bodyrc.lon", "pos.bodyrc.lat"
elif isinstance(self.reference_frame, coord.builtin_frames.BaseRADecFrame):
ph_type = "pos.eq.ra", "pos.eq.dec"
elif isinstance(self.reference_frame, coord.builtin_frames.BaseEclipticFrame):
ph_type = "pos.ecliptic.lon", "pos.ecliptic.lat"
else:
ph_type = tuple("custom:{}".format(t) for t in self.axes_names)
elif isinstance(self, SpectralFrame):
if self.unit[0].physical_type == "frequency":
ph_type = ("em.freq",)
elif self.unit[0].physical_type == "length":
ph_type = ("em.wl",)
elif self.unit[0].physical_type == "energy":
ph_type = ("em.energy",)
elif self.unit[0].physical_type == "speed":
ph_type = ("spect.dopplerVeloc",)
logging.warning("Physical type may be ambiguous. Consider "
"setting the physical type explicitly as "
"either 'spect.dopplerVeloc.optical' or "
"'spect.dopplerVeloc.radio'.")
else:
ph_type = ("custom:{}".format(self.unit[0].physical_type),)
elif isinstance(self, TemporalFrame):
def from_tree(cls, node, ctx):
node = cls._from_tree(node, ctx)
if 'reference_position' in node:
node['reference_position'] = node['reference_position'].upper()
return SpectralFrame(**node)
return CelestialFrame(**node)
@classmethod
def to_tree(cls, frame, ctx):
return cls._to_tree(frame, ctx)
@classmethod
def assert_equal(cls, old, new):
cls._assert_equal(old, new)
assert old.reference_position == new.reference_position # nosec
class SpectralFrameType(FrameType):
name = "spectral_frame"
types = [SpectralFrame]
version = "1.0.0"
@classmethod
def from_tree(cls, node, ctx):
node = cls._from_tree(node, ctx)
if 'reference_position' in node:
node['reference_position'] = node['reference_position'].upper()
return SpectralFrame(**node)
@classmethod
def to_tree(cls, frame, ctx):
node = cls._to_tree(frame, ctx)
if frame.reference_position is not None: