New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orbits around bodies without predefined reference frame do not have str representation #476

Closed
Juanlu001 opened this Issue Oct 22, 2018 · 3 comments

Comments

2 participants
@Juanlu001
Copy link
Member

Juanlu001 commented Oct 22, 2018

Comes from #474, by @TimothySHamilton

For example, for any orbit around Moon:

In [32]: orbit_moon_enter = Orbit.from_vectors(Moon, r, v)

In [33]: orbit_moon_enter
Out[33]: 
In [34]: str(orbit_moon_enter)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/.miniconda36/envs/poliastro37/lib/python3.7/site-packages/poliastro/frames.py in get_frame(attractor, plane, obstime)
    242     try:
--> 243         frames = _FRAME_MAPPING[attractor]
    244     except KeyError:

KeyError: Moon (☾)

During handling of the above exception, another exception occurred:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-34-12bb16b34e33> in <module>()
----> 1 str(orbit_moon_enter)

~/.miniconda36/envs/poliastro37/lib/python3.7/site-packages/poliastro/twobody/orbit.py in __str__(self)
    357         return ORBIT_FORMAT.format(
    358             r_p=self.r_p.to(unit).value, r_a=self.r_a.to(unit), inc=self.inc.to(u.deg),
--> 359             frame=self.frame.__class__.__name__,
    360             body=self.attractor,
    361             epoch=self.epoch, scale=self.epoch.scale.upper(),

~/.miniconda36/envs/poliastro37/lib/python3.7/site-packages/poliastro/twobody/orbit.py in frame(self)
     86         """
     87         if self._frame is None:
---> 88             self._frame = get_frame(self.attractor, self._plane, self.epoch)
     89 
     90         return self._frame

~/.miniconda36/envs/poliastro37/lib/python3.7/site-packages/poliastro/frames.py in get_frame(attractor, plane, obstime)
    243         frames = _FRAME_MAPPING[attractor]
    244     except KeyError:
--> 245         raise NotImplementedError("Frames for orbits around custom bodies are not yet supported")
    246 
    247     try:

NotImplementedError: Frames for orbits around custom bodies are not yet supported

This should be easy to fix, having two templates for the string representation and falling back to a simpler one when there is no frame. And then, actually implementing frames for all sorts of bodies: #109.

@AntoniiaK

This comment has been minimized.

Copy link
Contributor

AntoniiaK commented Dec 14, 2018

Hello!
Do you want remove these exceptions

try:
     frames = _FRAME_MAPPING[attractor]
 except KeyError:
     raise NotImplementedError("Frames for orbits around custom bodies are not yet supported")

 try:
     frame_class = frames[plane]
 except KeyError:
     raise NotImplementedError(
         "A frame with plane {} around body {} is not yet implemented".format(plane, attractor))

And get:
plar

@Juanlu001

This comment has been minimized.

Copy link
Member

Juanlu001 commented Dec 14, 2018

@AntoniiaK rather than removing them, I was thinking of chaning this code:

def __str__(self):
if self.a > 1e7 * u.km:
unit = u.au
else:
unit = u.km
return ORBIT_FORMAT.format(
r_p=self.r_p.to(unit).value, r_a=self.r_a.to(unit), inc=self.inc.to(u.deg),
frame=self.frame.__class__.__name__,
body=self.attractor,
epoch=self.epoch, scale=self.epoch.scale.upper(),
)

Into something like:

try:
    return ORBIT_FORMAT.format( 
         r_p=self.r_p.to(unit).value, r_a=self.r_a.to(unit), inc=self.inc.to(u.deg), 
         frame=self.frame.__class__.__name__, 
         body=self.attractor, 
         epoch=self.epoch, scale=self.epoch.scale.upper(), 
    )
except NotImplementedError:
    return ORBIT_NOFRAME_FORMAT.format( 
         r_p=self.r_p.to(unit).value, r_a=self.r_a.to(unit), inc=self.inc.to(u.deg), 
         body=self.attractor, 
         epoch=self.epoch, scale=self.epoch.scale.upper(), 
    )

so we can still raise an appropriate error when the user tries to get a frame that is not implemented yet.

@Juanlu001

This comment has been minimized.

Copy link
Member

Juanlu001 commented Dec 16, 2018

Closed in #506.

@Juanlu001 Juanlu001 closed this Dec 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment