Skip to content
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

Add __str__ method to classes #213

Merged
merged 7 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion boule/_ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
Module for defining and setting the reference ellipsoid.
"""
import textwrap
from warnings import warn

import attr
Expand Down Expand Up @@ -88,7 +89,16 @@
... ),
... )
>>> print(ellipsoid) # doctest: +ELLIPSIS
Ellipsoid(name='WGS84', ...)
WGS84 - World Geodetic System 1984
Oblate ellipsoid:
Semimajor axis: 6378137 m
Flattening: 0.0033528106647474805
GM: 398600441800000.0 m³/s²
Angular velocity: 7.292115e-05 rad/s
Source:
Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy (2nd,
corr. ed. 2006 edition ed.). Wien ; New York: Springer.

>>> print(ellipsoid.long_name)
World Geodetic System 1984

Expand Down Expand Up @@ -394,6 +404,25 @@
)
return result

def __str__(self):
s = self.name + " - " + self.long_name + "\n"
s += "Oblate ellipsoid:\n"
s += f" Semimajor axis: {self.semimajor_axis} m\n"
s += f" Flattening: {self.flattening}\n"
s += f" GM: {self.geocentric_grav_const} m³/s²\n"
s += f" Angular velocity: {self.angular_velocity} rad/s"
santisoler marked this conversation as resolved.
Show resolved Hide resolved
if self.reference is not None:
s += "\nSource:\n"
s += textwrap.fill(
self.reference, width=72, initial_indent=" ", subsequent_indent=" "
)
if self.comments is not None:
s += "\nComments:\n"
s += textwrap.fill(

Check warning on line 421 in boule/_ellipsoid.py

View check run for this annotation

Codecov / codecov/patch

boule/_ellipsoid.py#L420-L421

Added lines #L420 - L421 were not covered by tests
self.comments, width=72, initial_indent=" ", subsequent_indent=" "
)
return s

def geocentric_radius(self, latitude, geodetic=True):
r"""
Radial distance from the center of the ellipsoid to its surface.
Expand Down
6 changes: 3 additions & 3 deletions boule/_realizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
angular_velocity=7292115e-11,
reference=(
"Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy "
"(2nd, corr. ed. 2006 edition ed.). Wien; New York: Springer."
"(2nd, corr. ed. 2006 edition ed.). Wien; New York: Springer."
),
)

Expand All @@ -79,7 +79,7 @@
angular_velocity=7292115e-11,
reference=(
"Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy "
"(2nd, corr. ed. 2006 edition ed.). Wien; New York: Springer."
"(2nd, corr. ed. 2006 edition ed.). Wien; New York: Springer."
),
)

Expand Down Expand Up @@ -120,7 +120,7 @@
reference=(
"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), 1. "
"Earth, Moon, and Planets, 106, 1-13. "
"doi:10.1007/s11038-009-9342-7"
),
)
Expand Down
26 changes: 25 additions & 1 deletion boule/_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
Define the reference sphere (ellipsoid with 0 flattening).
"""
import textwrap
from warnings import warn

import attr
Expand Down Expand Up @@ -81,7 +82,12 @@
... angular_velocity=0.5,
... )
>>> print(sphere) # doctest: +ELLIPSIS
Sphere(name='Moon', ...)
Moon - That's no moon
Spheroid:
Radius: 1 m
GM: 2 m³/s²
Angular velocity: 0.5 rad/s

>>> print(sphere.long_name)
That's no moon

Expand Down Expand Up @@ -280,6 +286,24 @@
"""
return self.geocentric_grav_const / self.radius

def __str__(self):
s = self.name + " - " + self.long_name + "\n"
s += "Spheroid:\n"
s += f" Radius: {self.radius} m\n"
s += f" GM: {self.geocentric_grav_const} m³/s²\n"
s += f" Angular velocity: {self.angular_velocity} rad/s"
santisoler marked this conversation as resolved.
Show resolved Hide resolved
if self.reference is not None:
s += "\nSource:\n"
s += textwrap.fill(

Check warning on line 297 in boule/_sphere.py

View check run for this annotation

Codecov / codecov/patch

boule/_sphere.py#L296-L297

Added lines #L296 - L297 were not covered by tests
self.reference, width=72, initial_indent=" ", subsequent_indent=" "
)
if self.comments is not None:
s += "\nComments:\n"
s += textwrap.fill(

Check warning on line 302 in boule/_sphere.py

View check run for this annotation

Codecov / codecov/patch

boule/_sphere.py#L301-L302

Added lines #L301 - L302 were not covered by tests
self.comments, width=72, initial_indent=" ", subsequent_indent=" "
)
return s

def normal_gravity(self, latitude, height, si_units=False):
r"""
Normal gravity of the sphere at the given latitude and height.
Expand Down
75 changes: 56 additions & 19 deletions boule/_triaxialellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
Define a reference triaxial ellipsoid.
"""
import textwrap
from warnings import warn

import attr
Expand Down Expand Up @@ -84,44 +85,59 @@
We can define an ellipsoid by setting the 5 key numerical parameters:

>>> ellipsoid = TriaxialEllipsoid(
... name="VESTA",
... name="Vesta",
... long_name="Vesta Triaxial Ellipsoid",
... semimajor_axis=286_300,
... semimedium_axis=278_600,
... semiminor_axis=223_200,
... geocentric_grav_const=1.729094e10,
... angular_velocity=326.71050958367e-6,
... semimajor_axis=280_413,
... semimedium_axis=274_572,
... semiminor_axis=231_253,
... geocentric_grav_const=17.288e9,
... angular_velocity=3.267e-4,
... semimajor_axis_longitude=8.29,
... reference=(
... "Russell, C. T., Raymond, C. A., Coradini, A., McSween, "
... "H. Y., Zuber, M. T., Nathues, A., et al. (2012). Dawn at "
... "Vesta: Testing the Protoplanetary Paradigm. Science. "
... "doi:10.1126/science.1219381"
... "Karimi, R., Azmoudeh Ardalan, A., & Vasheghani Farahani, S. "
... "(2017). The size, shape and orientation of the asteroid "
... "Vesta based on data from the Dawn mission. Earth and "
... "Planetary Science Letters, 475, 71–82. "
... "https://doi.org/10.1016/j.epsl.2017.07.033"
... ),
... )
>>> print(ellipsoid) # doctest: +ELLIPSIS
TriaxialEllipsoid(name='VESTA', ...)
Vesta - Vesta Triaxial Ellipsoid
Triaxial ellipsoid:
Semimajor axis: 280413 m
Semimedium axis: 274572 m
Semiminor axis: 231253 m
Semiminor axis longitude: 8.29
GM: 17288000000.0 m³/s²
Angular velocity: 0.0003267 rad/s
Source:
Karimi, R., Azmoudeh Ardalan, A., & Vasheghani Farahani, S. (2017).
The size, shape and orientation of the asteroid Vesta based on data
from the Dawn mission. Earth and Planetary Science Letters, 475,
71–82. https://doi.org/10.1016/j.epsl.2017.07.033

>>> print(ellipsoid.long_name)
Vesta Triaxial Ellipsoid

The class then defines several derived attributes based on the input
parameters:

>>> print(f"{ellipsoid.mean_radius:.0f} m")
259813 m
260344 m
>>> print(f"{ellipsoid.semiaxes_mean_radius:.0f} m")
262700 m
262079 m
>>> print(f"{ellipsoid.area:.10e} m²")
8.6562393883e+11 m²
8.6210266337e+11 m²
>>> print(f"{ellipsoid.area_equivalent_radius:0.0f} m")
262458 m
261924 m
>>> print(f"{ellipsoid.volume_equivalent_radius:.0f} m")
261115 m
261124 m
>>> print(f"{ellipsoid.mass:.10e} kg")
2.5906746775e+20 kg
2.5902341819e+20 kg
>>> print(f"{ellipsoid.mean_density:.0f} kg/m³")
3474 kg/m³
3473 kg/m³
>>> print(f"{ellipsoid.volume * 1e-9:.0f} km³")
74573626 km³
74581373 km³

"""

Expand Down Expand Up @@ -325,6 +341,27 @@
"""
return (self.semimajor_axis - self.semiminor_axis) / self.semimajor_axis

def __str__(self):
s = self.name + " - " + self.long_name + "\n"
s += "Triaxial ellipsoid:\n"
s += f" Semimajor axis: {self.semimajor_axis} m\n"
s += f" Semimedium axis: {self.semimedium_axis} m\n"
s += f" Semiminor axis: {self.semiminor_axis} m\n"
s += f" Semiminor axis longitude: {self.semimajor_axis_longitude}\n"
MarkWieczorek marked this conversation as resolved.
Show resolved Hide resolved
s += f" GM: {self.geocentric_grav_const} m³/s²\n"
s += f" Angular velocity: {self.angular_velocity} rad/s"
santisoler marked this conversation as resolved.
Show resolved Hide resolved
if self.reference is not None:
s += "\nSource:\n"
s += textwrap.fill(
self.reference, width=72, initial_indent=" ", subsequent_indent=" "
)
if self.comments is not None:
s += "\nComments:\n"
s += textwrap.fill(

Check warning on line 360 in boule/_triaxialellipsoid.py

View check run for this annotation

Codecov / codecov/patch

boule/_triaxialellipsoid.py#L359-L360

Added lines #L359 - L360 were not covered by tests
self.comments, width=72, initial_indent=" ", subsequent_indent=" "
)
return s

def geocentric_radius(self, longitude, latitude):
r"""
Radial distance from the center of the ellipsoid to its surface.
Expand Down