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

MNT: Fix env plots max heights #433

Merged
merged 8 commits into from
Oct 9, 2023
440 changes: 220 additions & 220 deletions docs/notebooks/getting_started.ipynb

Large diffs are not rendered by default.

59 changes: 34 additions & 25 deletions rocketpy/environment/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class Environment:
Environment.topographic_profile_activated : bool
True if the user already set a topographic profile. False otherwise.
Environment.max_expected_height : float
Maximum altitude in meters to keep weather data.
Used especially for plotting range.
Can be altered as desired.
Maximum altitude in meters to keep weather data. The altitude must be
above sea level (ASL). Especially useful for controlling plottings.
Can be altered as desired by doing `max_expected_height = number`.
Environment.pressure_ISA : Function
Air pressure in Pa as a function of altitude as defined by the
`International Standard Atmosphere ISO 2533`. Only defined after load
Expand Down Expand Up @@ -270,6 +270,7 @@ def __init__(
elevation=0,
datum="SIRGAS2000",
timezone="UTC",
max_expected_height=80000.0,
):
"""Initialize Environment class, saving launch rail length,
launch date, location coordinates and elevation. Note that
Expand Down Expand Up @@ -308,7 +309,12 @@ def __init__(
timezone : string, optional
Name of the time zone. To see all time zones, import pytz and run
print(pytz.all_timezones). Default time zone is "UTC".

max_expected_height : float, optional
Maximum altitude in meters to keep weather data. The altitude must
be above sea level (ASL). Especially useful for visualization.
Can be altered as desired by doing `max_expected_height = number`.
Depending on the atmospheric model, this value may be automatically
mofified.

Returns
-------
Expand All @@ -319,15 +325,18 @@ def __init__(
self.air_gas_constant = 287.05287 # in J/K/Kg
self.standard_g = 9.80665

# Initialize launch site details
self.elevation = elevation
self.set_elevation(elevation)
self._max_expected_height = max_expected_height

# Initialize plots and prints objects
self.prints = _EnvironmentPrints(self)
self.plots = _EnvironmentPlots(self)

# Initialize atmosphere
self.set_atmospheric_model("standard_atmosphere")

# Save latitude and longitude
if latitude != None and longitude != None:
self.set_location(latitude, longitude)
else:
self.latitude, self.longitude = None, None

# Save date
if date != None:
self.set_date(date, timezone)
Expand All @@ -341,15 +350,6 @@ def __init__(
self.datum = datum
self.ellipsoid = self.set_earth_geometry(datum)

# Set gravity model
self.gravity = self.set_gravity_model(gravity)

# Initialize plots and prints objects
self.prints = _EnvironmentPrints(self)

# Initialize atmosphere
self.set_atmospheric_model("standard_atmosphere")

# Save latitude and longitude
self.latitude = latitude
self.longitude = longitude
Expand All @@ -374,9 +374,8 @@ def __init__(
self.initial_hemisphere = convert[4]
self.initial_ew = convert[5]

# Save elevation
self.elevation = elevation
self.set_elevation(elevation)
# Set gravity model
self.gravity = self.set_gravity_model(gravity)

# Recalculate Earth Radius (meters)
self.earth_radius = self.calculate_earth_radius(
Expand All @@ -385,9 +384,6 @@ def __init__(
flattening=self.ellipsoid.flattening,
)

# Initialize plots and prints object
self.plots = _EnvironmentPlots(self)

return None

def set_date(self, date, timezone="UTC"):
Expand Down Expand Up @@ -485,6 +481,19 @@ def set_gravity_model(self, gravity):
0, self.max_expected_height, 100
)

@property
def max_expected_height(self):
return self._max_expected_height

@max_expected_height.setter
def max_expected_height(self, value):
if value < self.elevation:
raise ValueError(
"Max expected height cannot be lower than the surface elevation"
)
self._max_expected_height = value
self.plots.grid = np.linspace(self.elevation, self.max_expected_height)

@funcify_method("height (m)", "gravity (m/s²)")
def somigliana_gravity(self, height):
"""Computes the gravity acceleration with the Somigliana formula.
Expand Down
17 changes: 11 additions & 6 deletions rocketpy/plots/environment_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def __init__(self, environment):
"""
# Create height grid
self.grid = np.linspace(environment.elevation, environment.max_expected_height)

self.environment = environment

return None

def __wind(self, ax):
Expand Down Expand Up @@ -65,6 +63,7 @@ def __wind(self, ax):
axup.set_xlabel("Wind Direction (°)", color="#1f77b4")
axup.tick_params("x", colors="#1f77b4")
axup.set_xlim(0, 360)
ax.set_ylim(self.grid[0], self.grid[-1])
ax.set_ylabel("Height Above Sea Level (m)")
ax.grid(True)

Expand Down Expand Up @@ -100,6 +99,7 @@ def __density_speed_of_sound(self, ax):
)
axup.set_xlabel("Density (kg/m³)", color="#1f77b4")
axup.tick_params("x", colors="#1f77b4")
ax.set_ylim(self.grid[0], self.grid[-1])
ax.set_ylabel("Height Above Sea Level (m)")
ax.grid(True)

Expand Down Expand Up @@ -132,6 +132,7 @@ def __wind_components(self, ax):
ax.set_ylabel("Height Above Sea Level (m)")
ax.set_xlabel("Wind Speed (m/s)")
ax.grid(True)
ax.set_ylim(self.grid[0], self.grid[-1])

return ax

Expand Down Expand Up @@ -167,6 +168,7 @@ def __pressure_temperature(self, ax):
axup.tick_params("x", colors="#1f77b4")
ax.set_ylabel("Height Above Sea Level (m)")
ax.grid(True)
ax.set_ylim(self.grid[0], self.grid[-1])

return ax

Expand All @@ -179,14 +181,17 @@ def gravity_model(self):
None
"""
# Create figure
plt.figure(figsize=(9, 9))
plt.figure(figsize=(4.5, 4.5))

# Create gravity model subplot
ax = plt.subplot(111)
ax.plot(self.grid, [self.environment.gravity(i) for i in self.grid])
ax.set_ylabel("Gravity (m/s²)")
ax.set_xlabel("Height Above Sea Level (m)")
gravity = [self.environment.gravity(i) for i in self.grid]
ax.plot(gravity, self.grid)
ax.set_ylabel("Height Above Sea Level (m)")
ax.set_xlabel("Gravity Acceleration (m/s²)")
ax.grid(True)
ax.set_ylim(self.grid[0], self.grid[-1])
plt.xticks(rotation=45)

plt.show()

Expand Down
39 changes: 18 additions & 21 deletions rocketpy/prints/environment_prints.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ def gravity_details(self):
-------
None
"""
elevation = self.environment.elevation
max_expected_height = self.environment.max_expected_height
surface_gravity = self.environment.gravity([elevation])
ceiling_gravity = self.environment.gravity([max_expected_height])
print("\nGravity Details\n")
print(f"Acceleration of gravity at surface level: {surface_gravity:9.4f} m/s²")
print(
"Acceleration of Gravity at Lauch Site: "
+ str(self.environment.gravity(self.environment.elevation))
+ " m/s²"
f"Acceleration of gravity at {max_expected_height/1000:7.3f} km (ASL): {ceiling_gravity:.4f} m/s²"
)

return None

def launch_site_details(self):
Expand Down Expand Up @@ -176,27 +178,20 @@ def atmospheric_conditions(self):
return None

def print_earth_details(self):
"""[UNDER CONSTRUCTION]
"""
Function to print information about the Earth Model used in the
Environment Class

"""
# Print launch site details
# print("Launch Site Details")
# print("Launch Site Latitude: {:.5f}°".format(self.environment.latitude))
# print("Launch Site Longitude: {:.5f}°".format(self.environment.longitude))
# print("Reference Datum: " + self.environment.datum)
# print("Launch Site UTM coordinates: {:.2f} ".format(self.environment.initial_east)
# + self.environment.initial_ew + " {:.2f} ".format(self.environment.initial_north) + self.environment.initial_hemisphere
# )
# print("Launch Site UTM zone number:", self.environment.initial_utm_zone)
# print("Launch Site Surface Elevation: {:.1f} m".format(self.environment.elevation))
print(
"Earth Radius at Launch site: {:.1f} m".format(
self.environment.earth_radius
)
)
print("Gravity acceleration at launch site: Still not implemented :(")
print("\nEarth Model Details\n")
earth_radius = self.environment.earth_radius
semi_major_axis = self.environment.ellipsoid.semi_major_axis
flattening = self.environment.ellipsoid.flattening
semi_minor_axis = semi_major_axis * (1 - flattening)
print(f"Earth Radius at Launch site: {earth_radius/1000:.2f} km")
print(f"Semi-major Axis: {semi_major_axis/1000:.2f} km")
print(f"Semi-minor Axis: {semi_minor_axis/1000:.2f} km")
print(f"Flattening: {flattening:.4f}\n")

return None

Expand Down Expand Up @@ -224,4 +219,6 @@ def all(self):
self.atmospheric_conditions()
print()

self.print_earth_details()

return None
Loading