|
3 | 3 | __copyright__ = "Copyright 20XX, RocketPy Team"
|
4 | 4 | __license__ = "MIT"
|
5 | 5 |
|
| 6 | +import matplotlib.pyplot as plt |
6 | 7 | import numpy as np
|
7 | 8 | from scipy.integrate import solve_ivp
|
8 | 9 |
|
9 | 10 | from .Environment import Environment
|
| 11 | +from .EnvironmentAnalysis import EnvironmentAnalysis |
10 | 12 | from .Function import Function
|
11 | 13 |
|
12 | 14 |
|
@@ -197,3 +199,135 @@ def du(z, u):
|
197 | 199 | velocityFunction()
|
198 | 200 |
|
199 | 201 | return altitudeFunction, velocityFunction, final_sol
|
| 202 | + |
| 203 | + |
| 204 | +def compareEnvironments(env1, env2, names=["env1", "env2"]): |
| 205 | + """Compares two environments and plots everything. |
| 206 | + Useful when comparing Environment Analysis results against forecasts. |
| 207 | +
|
| 208 | + Parameters |
| 209 | + ---------- |
| 210 | + env1 : Environment or EnvironmentAnalysis |
| 211 | + Environment to compare with. |
| 212 | + env2: Environment or EnvironmentAnalysis |
| 213 | + Environment to compare with. |
| 214 | +
|
| 215 | + Returns |
| 216 | + ------- |
| 217 | + diff: Dict |
| 218 | + Dictionary with the differences. |
| 219 | + """ |
| 220 | + |
| 221 | + # Raise TypeError if env1 or env2 are not Environment nor EnvironmentAnalysis |
| 222 | + if not isinstance(env1, Environment) and not isinstance(env1, EnvironmentAnalysis): |
| 223 | + raise TypeError("env1 must be an Environment or EnvironmentAnalysis object") |
| 224 | + |
| 225 | + if not isinstance(env2, Environment) and not isinstance(env2, EnvironmentAnalysis): |
| 226 | + raise TypeError("env2 must be an Environment or EnvironmentAnalysis object") |
| 227 | + |
| 228 | + # If instances are Environment Analysis, get the equivalent environment |
| 229 | + if isinstance(env1, EnvironmentAnalysis): |
| 230 | + env1.process_temperature_profile_over_average_day() |
| 231 | + env1.process_pressure_profile_over_average_day() |
| 232 | + env1.process_wind_velocity_x_profile_over_average_day() |
| 233 | + env1.process_wind_velocity_y_profile_over_average_day() |
| 234 | + print() |
| 235 | + if isinstance(env2, EnvironmentAnalysis): |
| 236 | + env2.process_temperature_profile_over_average_day() |
| 237 | + env2.process_pressure_profile_over_average_day() |
| 238 | + env2.process_wind_velocity_x_profile_over_average_day() |
| 239 | + env2.process_wind_velocity_y_profile_over_average_day() |
| 240 | + print() |
| 241 | + |
| 242 | + # Plot graphs |
| 243 | + print("\n\nAtmospheric Model Plots") |
| 244 | + # Create height grid |
| 245 | + grid = np.linspace(env1.elevation, env1.maxExpectedHeight) |
| 246 | + |
| 247 | + # Create figure |
| 248 | + plt.figure(figsize=(9, 9)) |
| 249 | + |
| 250 | + # Create wind speed and wind direction subplot |
| 251 | + ax1 = plt.subplot(221) |
| 252 | + ax1.plot( |
| 253 | + [env1.windSpeed(i) for i in grid], |
| 254 | + grid, |
| 255 | + "#ff7f0e", |
| 256 | + label="Speed of Sound " + names[0], |
| 257 | + ) |
| 258 | + ax1.plot( |
| 259 | + [env2.windSpeed(i) for i in grid], |
| 260 | + grid, |
| 261 | + "#ff7f0e", |
| 262 | + label="Speed of Sound " + names[1], |
| 263 | + ) |
| 264 | + ax1.set_xlabel("Wind Speed (m/s)", color="#ff7f0e") |
| 265 | + ax1.tick_params("x", colors="#ff7f0e") |
| 266 | + # ax1up = ax1.twiny() |
| 267 | + # ax1up.plot( |
| 268 | + # [env1.windDirection(i) for i in grid], |
| 269 | + # grid, |
| 270 | + # color="#1f77b4", |
| 271 | + # label="Density "+names[0], |
| 272 | + # ) |
| 273 | + # ax1up.plot( |
| 274 | + # [env2.windDirection(i) for i in grid], |
| 275 | + # grid, |
| 276 | + # color="#1f77b4", |
| 277 | + # label="Density "+names[1], |
| 278 | + # ) |
| 279 | + # ax1up.set_xlabel("Wind Direction (°)", color="#1f77b4") |
| 280 | + # ax1up.tick_params("x", colors="#1f77b4") |
| 281 | + # ax1up.set_xlim(0, 360) |
| 282 | + ax1.set_ylabel("Height Above Sea Level (m)") |
| 283 | + ax1.grid(True) |
| 284 | + |
| 285 | + # # Create density and speed of sound subplot |
| 286 | + # ax2 = plt.subplot(222) |
| 287 | + # ax2.plot( |
| 288 | + # [env1.speedOfSound(i) for i in grid], |
| 289 | + # grid, |
| 290 | + # "#ff7f0e", |
| 291 | + # label="Speed of Sound", |
| 292 | + # ) |
| 293 | + # ax2.set_xlabel("Speed of Sound (m/s)", color="#ff7f0e") |
| 294 | + # ax2.tick_params("x", colors="#ff7f0e") |
| 295 | + # ax2up = ax2.twiny() |
| 296 | + # ax2up.plot([env1.density(i) for i in grid], grid, color="#1f77b4", label="Density") |
| 297 | + # ax2up.set_xlabel("Density (kg/m³)", color="#1f77b4") |
| 298 | + # ax2up.tick_params("x", colors="#1f77b4") |
| 299 | + # ax2.set_ylabel("Height Above Sea Level (m)") |
| 300 | + # ax2.grid(True) |
| 301 | + |
| 302 | + # Create wind u and wind v subplot |
| 303 | + ax3 = plt.subplot(223) |
| 304 | + ax3.plot([env1.windVelocityX(i) for i in grid], grid, label="Wind U " + names[0]) |
| 305 | + ax3.plot([env1.windVelocityY(i) for i in grid], grid, label="Wind V " + names[0]) |
| 306 | + ax3.plot([env2.windVelocityX(i) for i in grid], grid, label="Wind U " + names[1]) |
| 307 | + ax3.plot([env2.windVelocityY(i) for i in grid], grid, label="Wind V " + names[1]) |
| 308 | + ax3.legend(loc="best").set_draggable(True) |
| 309 | + ax3.set_ylabel("Height Above Sea Level (m)") |
| 310 | + ax3.set_xlabel("Wind Speed (m/s)") |
| 311 | + ax3.grid(True) |
| 312 | + |
| 313 | + # Create pressure and temperature subplot |
| 314 | + ax4 = plt.subplot(224) |
| 315 | + ax4.plot([env1.pressure(i) / 100 for i in grid], grid, "#ff7f0e", label="Pressure") |
| 316 | + ax4.set_xlabel("Pressure (hPa)", color="#ff7f0e") |
| 317 | + ax4.tick_params("x", colors="#ff7f0e") |
| 318 | + ax4up = ax4.twiny() |
| 319 | + ax4up.plot( |
| 320 | + [env1.temperature(i) for i in grid], |
| 321 | + grid, |
| 322 | + color="#1f77b4", |
| 323 | + label="Temperature", |
| 324 | + ) |
| 325 | + ax4up.set_xlabel("Temperature (K)", color="#1f77b4") |
| 326 | + ax4up.tick_params("x", colors="#1f77b4") |
| 327 | + ax4.set_ylabel("Height Above Sea Level (m)") |
| 328 | + ax4.grid(True) |
| 329 | + |
| 330 | + plt.subplots_adjust(wspace=0.5, hspace=0.3) |
| 331 | + plt.show() |
| 332 | + |
| 333 | + return None # diff |
0 commit comments