Skip to content

Commit

Permalink
ENH: drafting compareEnvironments method
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-FernandesBR committed Sep 27, 2022
1 parent 0c63b61 commit 4e40c42
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 3 deletions.
5 changes: 2 additions & 3 deletions rocketpy/EnvironmentAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ def __init__(
pressureLevelDataFile=None,
timezone=None,
unit_system="metric",
load_previous_data=None,
forecast_comparaison=False,
forecast_comparison=False,
forecast_date=None,
maxExpectedAltitude=None,
):
Expand Down Expand Up @@ -213,7 +212,7 @@ def __init__(

# Processing forecast
self.forecast = None
if forecast_comparaison:
if forecast_comparison:
self.forecast = {}
hours = list(self.pressureLevelDataDict.values())[0].keys()
for hour in hours:
Expand Down
134 changes: 134 additions & 0 deletions rocketpy/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
__copyright__ = "Copyright 20XX, RocketPy Team"
__license__ = "MIT"

import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp

from .Environment import Environment
from .EnvironmentAnalysis import EnvironmentAnalysis
from .Function import Function


Expand Down Expand Up @@ -197,3 +199,135 @@ def du(z, u):
velocityFunction()

return altitudeFunction, velocityFunction, final_sol


def compareEnvironments(env1, env2, names=["env1", "env2"]):
"""Compares two environments and plots everything.
Useful when comparing Environment Analysis results against forecasts.
Parameters
----------
env1 : Environment or EnvironmentAnalysis
Environment to compare with.
env2: Environment or EnvironmentAnalysis
Environment to compare with.
Returns
-------
diff: Dict
Dictionary with the differences.
"""

# Raise TypeError if env1 or env2 are not Environment nor EnvironmentAnalysis
if not isinstance(env1, Environment) and not isinstance(env1, EnvironmentAnalysis):
raise TypeError("env1 must be an Environment or EnvironmentAnalysis object")

if not isinstance(env2, Environment) and not isinstance(env2, EnvironmentAnalysis):
raise TypeError("env2 must be an Environment or EnvironmentAnalysis object")

# If instances are Environment Analysis, get the equivalent environment
if isinstance(env1, EnvironmentAnalysis):
env1.process_temperature_profile_over_average_day()
env1.process_pressure_profile_over_average_day()
env1.process_wind_velocity_x_profile_over_average_day()
env1.process_wind_velocity_y_profile_over_average_day()
print()
if isinstance(env2, EnvironmentAnalysis):
env2.process_temperature_profile_over_average_day()
env2.process_pressure_profile_over_average_day()
env2.process_wind_velocity_x_profile_over_average_day()
env2.process_wind_velocity_y_profile_over_average_day()
print()

# Plot graphs
print("\n\nAtmospheric Model Plots")
# Create height grid
grid = np.linspace(env1.elevation, env1.maxExpectedHeight)

# Create figure
plt.figure(figsize=(9, 9))

# Create wind speed and wind direction subplot
ax1 = plt.subplot(221)
ax1.plot(
[env1.windSpeed(i) for i in grid],
grid,
"#ff7f0e",
label="Speed of Sound " + names[0],
)
ax1.plot(
[env2.windSpeed(i) for i in grid],
grid,
"#ff7f0e",
label="Speed of Sound " + names[1],
)
ax1.set_xlabel("Wind Speed (m/s)", color="#ff7f0e")
ax1.tick_params("x", colors="#ff7f0e")
# ax1up = ax1.twiny()
# ax1up.plot(
# [env1.windDirection(i) for i in grid],
# grid,
# color="#1f77b4",
# label="Density "+names[0],
# )
# ax1up.plot(
# [env2.windDirection(i) for i in grid],
# grid,
# color="#1f77b4",
# label="Density "+names[1],
# )
# ax1up.set_xlabel("Wind Direction (°)", color="#1f77b4")
# ax1up.tick_params("x", colors="#1f77b4")
# ax1up.set_xlim(0, 360)
ax1.set_ylabel("Height Above Sea Level (m)")
ax1.grid(True)

# # Create density and speed of sound subplot
# ax2 = plt.subplot(222)
# ax2.plot(
# [env1.speedOfSound(i) for i in grid],
# grid,
# "#ff7f0e",
# label="Speed of Sound",
# )
# ax2.set_xlabel("Speed of Sound (m/s)", color="#ff7f0e")
# ax2.tick_params("x", colors="#ff7f0e")
# ax2up = ax2.twiny()
# ax2up.plot([env1.density(i) for i in grid], grid, color="#1f77b4", label="Density")
# ax2up.set_xlabel("Density (kg/m³)", color="#1f77b4")
# ax2up.tick_params("x", colors="#1f77b4")
# ax2.set_ylabel("Height Above Sea Level (m)")
# ax2.grid(True)

# Create wind u and wind v subplot
ax3 = plt.subplot(223)
ax3.plot([env1.windVelocityX(i) for i in grid], grid, label="Wind U " + names[0])
ax3.plot([env1.windVelocityY(i) for i in grid], grid, label="Wind V " + names[0])
ax3.plot([env2.windVelocityX(i) for i in grid], grid, label="Wind U " + names[1])
ax3.plot([env2.windVelocityY(i) for i in grid], grid, label="Wind V " + names[1])
ax3.legend(loc="best").set_draggable(True)
ax3.set_ylabel("Height Above Sea Level (m)")
ax3.set_xlabel("Wind Speed (m/s)")
ax3.grid(True)

# Create pressure and temperature subplot
ax4 = plt.subplot(224)
ax4.plot([env1.pressure(i) / 100 for i in grid], grid, "#ff7f0e", label="Pressure")
ax4.set_xlabel("Pressure (hPa)", color="#ff7f0e")
ax4.tick_params("x", colors="#ff7f0e")
ax4up = ax4.twiny()
ax4up.plot(
[env1.temperature(i) for i in grid],
grid,
color="#1f77b4",
label="Temperature",
)
ax4up.set_xlabel("Temperature (K)", color="#1f77b4")
ax4up.tick_params("x", colors="#1f77b4")
ax4.set_ylabel("Height Above Sea Level (m)")
ax4.grid(True)

plt.subplots_adjust(wspace=0.5, hspace=0.3)
plt.show()

return None # diff

0 comments on commit 4e40c42

Please sign in to comment.