Skip to content

Commit 4206b85

Browse files
Merge pull request #433 from RocketPy-Team/mnt/fix-env-plots-max-heights
MNT: Fix env plots max heights
2 parents 8993108 + fd48bba commit 4206b85

File tree

4 files changed

+283
-272
lines changed

4 files changed

+283
-272
lines changed

docs/notebooks/getting_started.ipynb

Lines changed: 220 additions & 220 deletions
Large diffs are not rendered by default.

rocketpy/environment/environment.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ class Environment:
9393
Environment.topographic_profile_activated : bool
9494
True if the user already set a topographic profile. False otherwise.
9595
Environment.max_expected_height : float
96-
Maximum altitude in meters to keep weather data.
97-
Used especially for plotting range.
98-
Can be altered as desired.
96+
Maximum altitude in meters to keep weather data. The altitude must be
97+
above sea level (ASL). Especially useful for controlling plottings.
98+
Can be altered as desired by doing `max_expected_height = number`.
9999
Environment.pressure_ISA : Function
100100
Air pressure in Pa as a function of altitude as defined by the
101101
`International Standard Atmosphere ISO 2533`. Only defined after load
@@ -270,6 +270,7 @@ def __init__(
270270
elevation=0,
271271
datum="SIRGAS2000",
272272
timezone="UTC",
273+
max_expected_height=80000.0,
273274
):
274275
"""Initialize Environment class, saving launch rail length,
275276
launch date, location coordinates and elevation. Note that
@@ -308,7 +309,12 @@ def __init__(
308309
timezone : string, optional
309310
Name of the time zone. To see all time zones, import pytz and run
310311
print(pytz.all_timezones). Default time zone is "UTC".
311-
312+
max_expected_height : float, optional
313+
Maximum altitude in meters to keep weather data. The altitude must
314+
be above sea level (ASL). Especially useful for visualization.
315+
Can be altered as desired by doing `max_expected_height = number`.
316+
Depending on the atmospheric model, this value may be automatically
317+
mofified.
312318
313319
Returns
314320
-------
@@ -319,15 +325,18 @@ def __init__(
319325
self.air_gas_constant = 287.05287 # in J/K/Kg
320326
self.standard_g = 9.80665
321327

328+
# Initialize launch site details
329+
self.elevation = elevation
330+
self.set_elevation(elevation)
331+
self._max_expected_height = max_expected_height
332+
333+
# Initialize plots and prints objects
334+
self.prints = _EnvironmentPrints(self)
335+
self.plots = _EnvironmentPlots(self)
336+
322337
# Initialize atmosphere
323338
self.set_atmospheric_model("standard_atmosphere")
324339

325-
# Save latitude and longitude
326-
if latitude != None and longitude != None:
327-
self.set_location(latitude, longitude)
328-
else:
329-
self.latitude, self.longitude = None, None
330-
331340
# Save date
332341
if date != None:
333342
self.set_date(date, timezone)
@@ -341,15 +350,6 @@ def __init__(
341350
self.datum = datum
342351
self.ellipsoid = self.set_earth_geometry(datum)
343352

344-
# Set gravity model
345-
self.gravity = self.set_gravity_model(gravity)
346-
347-
# Initialize plots and prints objects
348-
self.prints = _EnvironmentPrints(self)
349-
350-
# Initialize atmosphere
351-
self.set_atmospheric_model("standard_atmosphere")
352-
353353
# Save latitude and longitude
354354
self.latitude = latitude
355355
self.longitude = longitude
@@ -374,9 +374,8 @@ def __init__(
374374
self.initial_hemisphere = convert[4]
375375
self.initial_ew = convert[5]
376376

377-
# Save elevation
378-
self.elevation = elevation
379-
self.set_elevation(elevation)
377+
# Set gravity model
378+
self.gravity = self.set_gravity_model(gravity)
380379

381380
# Recalculate Earth Radius (meters)
382381
self.earth_radius = self.calculate_earth_radius(
@@ -385,9 +384,6 @@ def __init__(
385384
flattening=self.ellipsoid.flattening,
386385
)
387386

388-
# Initialize plots and prints object
389-
self.plots = _EnvironmentPlots(self)
390-
391387
return None
392388

393389
def set_date(self, date, timezone="UTC"):
@@ -485,6 +481,19 @@ def set_gravity_model(self, gravity):
485481
0, self.max_expected_height, 100
486482
)
487483

484+
@property
485+
def max_expected_height(self):
486+
return self._max_expected_height
487+
488+
@max_expected_height.setter
489+
def max_expected_height(self, value):
490+
if value < self.elevation:
491+
raise ValueError(
492+
"Max expected height cannot be lower than the surface elevation"
493+
)
494+
self._max_expected_height = value
495+
self.plots.grid = np.linspace(self.elevation, self.max_expected_height)
496+
488497
@funcify_method("height (m)", "gravity (m/s²)")
489498
def somigliana_gravity(self, height):
490499
"""Computes the gravity acceleration with the Somigliana formula.

rocketpy/plots/environment_plots.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ def __init__(self, environment):
2929
"""
3030
# Create height grid
3131
self.grid = np.linspace(environment.elevation, environment.max_expected_height)
32-
3332
self.environment = environment
34-
3533
return None
3634

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

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

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

136137
return ax
137138

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

171173
return ax
172174

@@ -179,14 +181,17 @@ def gravity_model(self):
179181
None
180182
"""
181183
# Create figure
182-
plt.figure(figsize=(9, 9))
184+
plt.figure(figsize=(4.5, 4.5))
183185

184186
# Create gravity model subplot
185187
ax = plt.subplot(111)
186-
ax.plot(self.grid, [self.environment.gravity(i) for i in self.grid])
187-
ax.set_ylabel("Gravity (m/s²)")
188-
ax.set_xlabel("Height Above Sea Level (m)")
188+
gravity = [self.environment.gravity(i) for i in self.grid]
189+
ax.plot(gravity, self.grid)
190+
ax.set_ylabel("Height Above Sea Level (m)")
191+
ax.set_xlabel("Gravity Acceleration (m/s²)")
189192
ax.grid(True)
193+
ax.set_ylim(self.grid[0], self.grid[-1])
194+
plt.xticks(rotation=45)
190195

191196
plt.show()
192197

rocketpy/prints/environment_prints.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ def gravity_details(self):
3333
-------
3434
None
3535
"""
36+
elevation = self.environment.elevation
37+
max_expected_height = self.environment.max_expected_height
38+
surface_gravity = self.environment.gravity([elevation])
39+
ceiling_gravity = self.environment.gravity([max_expected_height])
3640
print("\nGravity Details\n")
41+
print(f"Acceleration of gravity at surface level: {surface_gravity:9.4f} m/s²")
3742
print(
38-
"Acceleration of Gravity at Lauch Site: "
39-
+ str(self.environment.gravity(self.environment.elevation))
40-
+ " m/s²"
43+
f"Acceleration of gravity at {max_expected_height/1000:7.3f} km (ASL): {ceiling_gravity:.4f} m/s²"
4144
)
42-
4345
return None
4446

4547
def launch_site_details(self):
@@ -176,27 +178,20 @@ def atmospheric_conditions(self):
176178
return None
177179

178180
def print_earth_details(self):
179-
"""[UNDER CONSTRUCTION]
181+
"""
180182
Function to print information about the Earth Model used in the
181183
Environment Class
182184
183185
"""
184-
# Print launch site details
185-
# print("Launch Site Details")
186-
# print("Launch Site Latitude: {:.5f}°".format(self.environment.latitude))
187-
# print("Launch Site Longitude: {:.5f}°".format(self.environment.longitude))
188-
# print("Reference Datum: " + self.environment.datum)
189-
# print("Launch Site UTM coordinates: {:.2f} ".format(self.environment.initial_east)
190-
# + self.environment.initial_ew + " {:.2f} ".format(self.environment.initial_north) + self.environment.initial_hemisphere
191-
# )
192-
# print("Launch Site UTM zone number:", self.environment.initial_utm_zone)
193-
# print("Launch Site Surface Elevation: {:.1f} m".format(self.environment.elevation))
194-
print(
195-
"Earth Radius at Launch site: {:.1f} m".format(
196-
self.environment.earth_radius
197-
)
198-
)
199-
print("Gravity acceleration at launch site: Still not implemented :(")
186+
print("\nEarth Model Details\n")
187+
earth_radius = self.environment.earth_radius
188+
semi_major_axis = self.environment.ellipsoid.semi_major_axis
189+
flattening = self.environment.ellipsoid.flattening
190+
semi_minor_axis = semi_major_axis * (1 - flattening)
191+
print(f"Earth Radius at Launch site: {earth_radius/1000:.2f} km")
192+
print(f"Semi-major Axis: {semi_major_axis/1000:.2f} km")
193+
print(f"Semi-minor Axis: {semi_minor_axis/1000:.2f} km")
194+
print(f"Flattening: {flattening:.4f}\n")
200195

201196
return None
202197

@@ -224,4 +219,6 @@ def all(self):
224219
self.atmospheric_conditions()
225220
print()
226221

222+
self.print_earth_details()
223+
227224
return None

0 commit comments

Comments
 (0)