Skip to content

Commit

Permalink
Improve __str__ handling of optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Wieczorek committed Oct 4, 2024
1 parent 3816e84 commit 5317d93
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 54 deletions.
39 changes: 27 additions & 12 deletions boule/_ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ class Ellipsoid:
... ),
... )
>>> 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 @@ -396,17 +405,23 @@ def gravity_pole(self):
return result

def __str__(self):
str = self.name + " - " + self.long_name + "\n"
str += "Oblate ellipsoid:\n"
str += f" Semimajor axis: {self.semimajor_axis} m\n"
str += f" Flattening: {self.flattening}\n"
str += f" GM: {self.geocentric_grav_const} m³/s²\n"
str += f" Angular velocity: {self.angular_velocity} rad/s\n"
str += "Source:\n"
str += textwrap.fill(
self.reference, width=72, initial_indent=" ", subsequent_indent=" "
)
return str
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"
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"""
Expand Down
33 changes: 22 additions & 11 deletions boule/_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ class Sphere:
... 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 @@ -282,16 +287,22 @@ def reference_normal_gravitational_potential(self):
return self.geocentric_grav_const / self.radius

def __str__(self):
str = self.name + " - " + self.long_name + "\n"
str += "Spheroid:\n"
str += f" Radius: {self.radius} m\n"
str += f" GM: {self.geocentric_grav_const} m³/s²\n"
str += f" Angular velocity: {self.angular_velocity} rad/s\n"
str += "Source:\n"
str += textwrap.fill(
self.reference, width=72, initial_indent=" ", subsequent_indent=" "
)
return str
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"
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"""
Expand Down
84 changes: 53 additions & 31 deletions boule/_triaxialellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,44 +85,59 @@ class TriaxialEllipsoid:
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 @@ -327,18 +342,25 @@ def meridional_flattening(self):
return (self.semimajor_axis - self.semiminor_axis) / self.semimajor_axis

def __str__(self):
str = self.name + " - " + self.long_name + "\n"
str += "Triaxial ellipsoid:\n"
str += f" Semimajor axis: {self.semimajor_axis} m\n"
str += f" Semimedium axis: {self.semimedium_axis} m\n"
str += f" Semiminor axis: {self.semiminor_axis} m\n"
str += f" GM: {self.geocentric_grav_const} m³/s²\n"
str += f" Angular velocity: {self.angular_velocity} rad/s\n"
str += "Source:\n"
str += textwrap.fill(
self.reference, width=72, initial_indent=" ", subsequent_indent=" "
)
return str
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"
s += f" GM: {self.geocentric_grav_const} m³/s²\n"
s += f" Angular velocity: {self.angular_velocity} rad/s"
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"""
Expand Down

0 comments on commit 5317d93

Please sign in to comment.