fatiando / boule

Reference ellipsoids for geodesy and geophysics
https://www.fatiando.org/boule
BSD 3-Clause "New" or "Revised" License
38 stars 17 forks source link

Better __repr__ or .info() method #181

Closed MarkWieczorek closed 1 month ago

MarkWieczorek commented 7 months ago

Description of the desired feature: Printing the contents of a boule ellipsoid gives the following:

In [1]: print(boule.WGS84)
Ellipsoid(name='WGS84', semimajor_axis=6378137, flattening=0.0033528106647474805, geocentric_grav_const=398600441800000.0, angular_velocity=7.292115e-05, long_name='World Geodetic System (1984)', reference='Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy (2nd, corr. ed. 2006 edition ed.). Wien\u202f; New York: Springer.')

As this is a really long string, it is somewhat difficult to read if you a looking for a particular element. I propose that we provide an easier way to print the entirety of the ellipsoid contents for human readibility. This could either be by explicitly defining __repr__ for each ellispoid class or by providing a simple method such as Ellipsoid.info(). The output would look something like this:

In [1]: print(boule.WGS84)
Class: Ellipsoid
name = 'WGS84'
long_name = 'World Geodetic System (1984)'
semimajor_axis = 6378137
flattening = 0.0033528106647474805
geocentric_grav_const = 398600441800000.0
angular_velocity = 7.292115e-05
reference = 'Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy (2nd, corr. ed. 2006 edition ed.). Wien\u202f; New York: Springer.'

If desired, we could also append the units, but if we did this, we should probably define the units for each attribute in the class.

Are you willing to help implement and maintain this feature? After we decide on how to do this, the changes would be trivial to make...

VascoSch92 commented 7 months ago

Suggestion:

In [1]: print(boule.WGS84)
Class: Ellipsoid
 - name = 'WGS84'
 - long name = 'World Geodetic System (1984)'
 - semimajor axis = 6378137 km
 - flattening = 0.0033528106647474805
 - geocentric gravitational constant = 398600441800000.0
 - angular velocity = 7.292115e-05
 - reference = 'Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy (2nd, corr. ed. 2006 edition ed.). Wien\u202f; New York: Springer.'

Three other things:

  1. in reference you can see that you have \u202f after Wien. Is it possible that there is no a regular space between Wien and ; ?
  2. could be also interesting to add a method info() which print all the information about the ellipsoid, i.e., all the property are executed and printed in a nice way :-)
  3. printing directly in a table format could also be interesting
leouieda commented 7 months ago

Completely agree that we need a better print(bl.WGS84). The __repr__ is created automatically by attrs and I'd prefer to leave it as is, since __repr__ is supposed to allow eval(a.__repr__) == a which is what ours currently does (see this stackoverflow answer). But there is also __str__ which is used to make a nice representation of an object. If __str__ is defined, then print(a) will call a.__str__ instead of __repr__.

If implementing __str__, we can make the output nicer by not needing the key : value format:

>>> print(bl.WGS49)
World Geodetic System (1984) - WGS84
Oblate ellipsoid:
  •  Semi-major axis: 6378137 m
  •       Flattening: 0.0033528106647474805
  •               GM: 398600441800000.0 m³/s²
  • Angular velocity: 7.292115e-05 rad/s
From: Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy 
      (2nd, corr. ed. 2006 edition ed.). Wien; New York: Springer.

Another thing we can add in the future is a __repr_html__ to display the ellipsoid using HTML in Jupyter. But that's for another PR when we have this one implemented.

VascoSch92 commented 7 months ago

I agree with the ouput format proposed by @leouieda . The only "problem" I see is that formatting the reference in a nice way can be tricky as we don't know where is what. Option can be

  1. the reference is printed just on one line
  2. the reference is given with some structure, see under for examples, such that we can control the various part of the reference and we can pretty print it

For point 2:

reference = {
    "author": "Hofmann-Wellenhof, B., & Moritz, H.",
    "title": "Physical Geodesy",
    "edition": "2nd, corr. ed. 2006",
    "publisher": "Springer",
    "year": 2006,
    "location": "Wien; New York"
}

or using a small class

Reference(
    author="Hofmann-Wellenhof, B., & Moritz, H.", 
    title="Physical Geodesy", 
    edition="2nd, corr. ed. 2006",
    publisher="Springer", 
    year=2006, 
    location="Wien; New York"
)
MarkWieczorek commented 7 months ago

Another problem is that the new ellipsoids can have up to 4 references (one for each of the input parameters). One possible solution would be not to print the reference string at all for __str__.

MarkWieczorek commented 1 month ago

What do you think of this layout for __str__ and print?

Callisto2024 - Callisto spheroid (2024)
Spheroid:
  Radius: 2410300 m
  GM: 7179292000000.0 m³/s²
  Angular velocity: 4.357108150919352e-06 rad/s
Source:
  Radius, GM: Anderson, J. D., et al. (2001). Shape, mean radius,
  gravity field, and interior structure of Callisto. Icarus, 153(1),
  157–161. https://doi.org/10.1006/icar.2001.6664; Angular velocity:
  Satellites and the Orientation of the Pole of Jupiter, personal
  communication to Horizons/NAIF. Accessed via JPL Solar System
  Dynamics, https://ssd.jpl.nasa.gov, JUP365.

Mars2009 - Mars ellipsoid (2009)
Oblate ellipsoid:
  Semimajor axis: 3395428 m
  Flattening: 0.005227617843759314
  GM: 42828372000000.0 m³/s²
  Angular velocity: 7.0882181e-05 rad/s
Source:
  Ardalan, A. A., Karimi, R., & Grafarend, E. W. (2009). A New Reference
  Equipotential Surface, and Reference Ellipsoid for the Planet Mars.
  Earth, Moon, and Planets, 106, 1-13. doi:10.1007/s11038-009-9342-7

Io2024 - Io equilibrium triaxial ellipsoid (2024)
Triaxial ellipsoid:
  Semimajor axis: 1829700 m
  Semimedium axis: 1819200 m
  Semiminor axis: 1815800 m
  GM: 5959910000000.0 m³/s²
  Angular velocity: 4.125530833185668e-05 rad/s
Source:
  Semi-axis: Thomas, P. C., et al. (1998). The Shape of Io from Galileo
  Limb Measurements. Icarus, 135(1), 175–180.
  https://doi.org/10.1006/icar.1998.5987; GM: Anderson, J. D., et al.
  (2001). Io's gravity field and interior structure. J. Geophys. Res.,
  106, 32963–32969. https://doi.org/10.1029/2000JE001367; Angular
  velocity: R. A. Jacobson (2021), The Orbits of the Regular Jovian
  Satellites and the Orientation of the Pole of Jupiter, personal
  communication to Horizons/NAIF. Accessed via JPL Solar System
  Dynamics, https://ssd.jpl.nasa.gov, JUP365.

The "Source" section is a little long when there are mutliple references, but I don't think that we can do anything about it. The only option would be to remove it altogether, and give a not useful partial reference.

Also, for the bi-axial ellipsoid, we could also provide the semiminor axis, which is derived from the flattening and semimajor axis. I'll make a PR if everyone is happy.

santisoler commented 1 month ago

I like the idea of @VascoSch92 to have a dataclass for each reference. This way we can choose to only output the minimal information about each reference like authors' names, year and doi.

Alternatively, the reference property of ellipsoids could be either a single string or a list of strings. This way we can separate each one of them in the output. I do like wrapping the text to 72 characters to support most of the terminals. If each ellipsoid host a list of references, then we can add some indentation to the wrapped lines for each reference.

For example:

Io2024 - Io equilibrium triaxial ellipsoid (2024)
Triaxial ellipsoid:
  •           Semimajor axis: 1829700 m
  •          Semimedium axis: 1819200 m
  •           Semiminor axis: 1815800 m
  • Semimajor axis longitude: 0.0°
  •                       GM: 5959910000000.0 m³/s²
  •         Angular velocity: 4.125530833185668e-05 rad/s
Source:
  Semi-axis: Thomas, P. C., et al. (1998). The Shape of Io from Galileo
    Limb Measurements. Icarus, 135(1), 175–180.
    https://doi.org/10.1006/icar.1998.5987
  GM: Anderson, J. D., et al. (2001). Io's gravity field and interior
    structure. J. Geophys. Res., 106, 32963–32969.
    https://doi.org/10.1029/2000JE001367
  Angular velocity: R. A. Jacobson (2021), The Orbits of the Regular
    Jovian Satellites and the Orientation of the Pole of Jupiter,
    personal communication to Horizons/NAIF. Accessed via JPL Solar
    System Dynamics, https://ssd.jpl.nasa.gov, JUP365.
Comments:
  Best-fit equilibrium shape