impose_frame_attributes#
- astropy.coordinates.impose_frame_attributes(**kwargs)[source]#
Assume a value of one or more frame attributes.
- Parameters:
- kwargs
This function accepts any keyword arguments and will override any frame attribute with a name matching that of the keyword name.
Examples
>>> import astropy.units as u >>> from astropy.coordinates import EarthLocation, SkyCoord >>> from astropy.coordinates.attributes import impose_frame_attributes
>>> location = EarthLocation(-5466045*u.m, -2404388*u.m, 2242133*u.m)
Say you have two coordinates that represent points in images which are very close in time but not exactly the same:
>>> coord1 = SkyCoord(10, 20, unit=u.deg, obstime="2026-01-01T00:00:00", location=location, frame="altaz") >>> coord2 = SkyCoord(10, 20, unit=u.deg, obstime="2026-01-01T00:01:00", location=location, frame="altaz")
If you transform these to other frames, they will give slightly different results:
>>> coord1.transform_to("icrs") <SkyCoord (ICRS): (ra, dec) in deg (36.10412254, 80.47633131)> >>> coord2.transform_to("icrs") <SkyCoord (ICRS): (ra, dec) in deg (36.35158456, 80.47671193)>
If however, you are happy to ignore this small change in observer location, and you want to avoid more complex transformations or other effects through the transform graph, you can do this:
>>> with impose_frame_attributes(obstime="2026-01-01T00:00:00"): ... coord2.transform_to("icrs") <SkyCoord (ICRS): (ra, dec) in deg (36.10412254, 80.47633131)>
Another example is computing the separation between two
HCRSframes with slightly different obstimes.>>> import astropy.units as u >>> from astropy.coordinates import SkyCoord
>>> coord1 = SkyCoord(10*u.deg, 20*u.deg, frame='hcrs', obstime='2026-01-01 00:00:00') >>> coord2 = SkyCoord(20*u.deg, 30*u.deg, frame='hcrs', obstime='2026-01-01 00:00:00.001')
When you try and compute the separation an error is raised because of the origin shift.
>>> print(coord1.separation(coord2)) ... astropy.units.errors.UnitsError: The input HCRS coordinates do not have length units. This probably means you created coordinates with lat/lon but no distance. Heliocentric<->ICRS transforms cannot function in this case because there is an origin shift.
However, using this context manager:
>>> with impose_frame_attributes(obstime=coord1.obstime): ... print(coord1.separation(coord2)) 13d28m54.20360928s
Both of these examples are trivial when constructing coordinates like this, but when operating on a large number of coordinates read from a file or with coordinate transforms in packages such as
reproject, it becomes harder to make these changes manually.