From 5141837e84b75192d0a6eacd11c77b12f0a79398 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Wed, 25 Jan 2023 03:28:17 +0100 Subject: [PATCH 1/3] MAINT: move last plots from Environment class --- rocketpy/Environment.py | 53 +------ rocketpy/plots/environment_plots.py | 212 ++++++++++++++++++++-------- 2 files changed, 158 insertions(+), 107 deletions(-) diff --git a/rocketpy/Environment.py b/rocketpy/Environment.py index 77fb74bad..59046014c 100644 --- a/rocketpy/Environment.py +++ b/rocketpy/Environment.py @@ -12,7 +12,6 @@ import warnings from datetime import datetime, timedelta -import matplotlib.pyplot as plt import numpy as np import numpy.ma as ma import pytz @@ -3022,58 +3021,8 @@ def info(self): None """ - # All prints self.prints.all() - - # Plot graphs - print("\n\nAtmospheric Model Plots") - # Create height grid - grid = np.linspace(self.elevation, self.maxExpectedHeight) - - # Create figure - plt.figure(figsize=(9, 4.5)) - - # Create wind speed and wind direction subplot - ax1 = plt.subplot(121) - ax1.plot( - [self.windSpeed(i) for i in grid], grid, "#ff7f0e", label="Speed of Sound" - ) - ax1.set_xlabel("Wind Speed (m/s)", color="#ff7f0e") - ax1.tick_params("x", colors="#ff7f0e") - ax1up = ax1.twiny() - ax1up.plot( - [self.windDirection(i) for i in grid], - grid, - color="#1f77b4", - label="Density", - ) - 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(122) - ax2.plot( - [self.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( - [self.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) - - plt.subplots_adjust(wspace=0.5) - plt.show() + self.plots.info() return None def allInfo(self): diff --git a/rocketpy/plots/environment_plots.py b/rocketpy/plots/environment_plots.py index 2ce8a0e8b..8b62a5741 100644 --- a/rocketpy/plots/environment_plots.py +++ b/rocketpy/plots/environment_plots.py @@ -1,4 +1,4 @@ -__author__ = "Mateus Stano Junqueira" +__author__ = "Mateus Stano Junqueira, Guilherme Fernandes Alves" __copyright__ = "Copyright 20XX, RocketPy Team" __license__ = "MIT" @@ -39,104 +39,178 @@ def __init__(self, environment): return None - def atmospheric_model(self): - """Plots all atmospheric model graphs available + def __wind(self, ax): + """Adds wind speed and wind direction graphs to the same axis. Parameters ---------- - None + ax : matplotlib.pyplot.axis + Axis to add the graphs. - Return - ------ - None + Returns + ------- + ax : matplotlib.pyplot.axis + Axis with the graphs. """ - - # Create figure - plt.figure(figsize=(9, 9)) - - # Create wind speed and wind direction subplot - ax1 = plt.subplot(221) - ax1.plot( + ax.plot( [self.environment.windSpeed(i) for i in self.grid], self.grid, "#ff7f0e", label="Speed of Sound", ) - ax1.set_xlabel("Wind Speed (m/s)", color="#ff7f0e") - ax1.tick_params("x", colors="#ff7f0e") - ax1up = ax1.twiny() - ax1up.plot( + ax.set_xlabel("Wind Speed (m/s)", color="#ff7f0e") + ax.tick_params("x", colors="#ff7f0e") + axup = ax.twiny() + axup.plot( [self.environment.windDirection(i) for i in self.grid], self.grid, color="#1f77b4", label="Density", ) - 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) + axup.set_xlabel("Wind Direction (°)", color="#1f77b4") + axup.tick_params("x", colors="#1f77b4") + axup.set_xlim(0, 360) + ax.set_ylabel("Height Above Sea Level (m)") + ax.grid(True) - # Create density and speed of sound subplot - ax2 = plt.subplot(222) - ax2.plot( + return ax + + def __density_speed_of_sound(self, ax): + """Adds density and speed of sound graphs to the same axis. + + Parameters + ---------- + ax : matplotlib.pyplot.axis + Axis to add the graphs. + + Returns + ------- + ax : matplotlib.pyplot.axis + Axis with the graphs. + """ + ax.plot( [self.environment.speedOfSound(i) for i in self.grid], self.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( + ax.set_xlabel("Speed of Sound (m/s)", color="#ff7f0e") + ax.tick_params("x", colors="#ff7f0e") + axup = ax.twiny() + axup.plot( [self.environment.density(i) for i in self.grid], self.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) + axup.set_xlabel("Density (kg/m³)", color="#1f77b4") + axup.tick_params("x", colors="#1f77b4") + ax.set_ylabel("Height Above Sea Level (m)") + ax.grid(True) - # Create wind u and wind v subplot - ax3 = plt.subplot(223) - ax3.plot( + return ax + + def __wind_components(self, ax): + """Adds wind u and wind v graphs to the same axis. + + Parameters + ---------- + ax : matplotlib.pyplot.axis + Axis to add the graphs. + + Returns + ------- + ax : matplotlib.pyplot.axis + Axis with the graphs. + """ + ax.plot( [self.environment.windVelocityX(i) for i in self.grid], self.grid, label="Wind U", ) - ax3.plot( + ax.plot( [self.environment.windVelocityY(i) for i in self.grid], self.grid, label="Wind V", ) - 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) + ax.legend(loc="best").set_draggable(True) + ax.set_ylabel("Height Above Sea Level (m)") + ax.set_xlabel("Wind Speed (m/s)") + ax.grid(True) - # Create pressure and temperature subplot - ax4 = plt.subplot(224) - ax4.plot( + return ax + + def __pressure_temperature(self, ax): + """Adds pressure and temperature graphs to the same axis. + + Parameters + ---------- + ax : matplotlib.pyplot.axis + Axis to add the graphs. + + Returns + ------- + ax : matplotlib.pyplot.axis + Axis with the graphs. + """ + ax.plot( [self.environment.pressure(i) / 100 for i in self.grid], self.grid, "#ff7f0e", label="Pressure", ) - ax4.set_xlabel("Pressure (hPa)", color="#ff7f0e") - ax4.tick_params("x", colors="#ff7f0e") - ax4up = ax4.twiny() - ax4up.plot( + ax.set_xlabel("Pressure (hPa)", color="#ff7f0e") + ax.tick_params("x", colors="#ff7f0e") + axup = ax.twiny() + axup.plot( [self.environment.temperature(i) for i in self.grid], self.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) + axup.set_xlabel("Temperature (K)", color="#1f77b4") + axup.tick_params("x", colors="#1f77b4") + ax.set_ylabel("Height Above Sea Level (m)") + ax.grid(True) + + return ax + + def atmospheric_model(self): + """Plots all atmospheric model graphs available. This includes wind speed + and wind direction, density and speed of sound, wind u and wind v, and + pressure and temperature. + + Parameters + ---------- + None + + Return + ------ + None + """ + + # Create figure + plt.figure(figsize=(9, 9)) + + # Create wind speed and wind direction subplot + ax1 = plt.subplot(221) + ax1 = self.__wind(ax1) + ax1.legend(loc="best").set_draggable(True) + + # Create density and speed of sound subplot + ax2 = plt.subplot(222) + ax2 = self.__density_speed_of_sound(ax2) + ax2.legend(loc="best").set_draggable(True) + + # Create wind u and wind v subplot + ax3 = plt.subplot(223) + ax3 = self.__wind_components(ax3) + ax3.legend(loc="best").set_draggable(True) + + # Create pressure and temperature subplot + ax4 = plt.subplot(224) + ax4 = self.__pressure_temperature(ax4) + ax4.legend(loc="best").set_draggable(True) plt.subplots_adjust(wspace=0.5, hspace=0.3) plt.show() @@ -144,7 +218,8 @@ def atmospheric_model(self): return None def ensemble_member_comparison(self): - """Plots ensemble member comparisons. + """Plots ensemble member comparisons. It requires that the environment + model has been set as Ensemble. Parameters ---------- @@ -253,8 +328,35 @@ def ensemble_member_comparison(self): return None + def info(self): + """Plots a summary of the atmospheric model, including wind speed and + wind direction, density and speed of sound. This is important for the + Environment.info() method. + + Returns + ------- + None + """ + print("\nAtmospheric Model Plots\n") + plt.figure(figsize=(9, 4.5)) + # Create wind speed and wind direction subplot + ax1 = plt.subplot(121) + ax1 = self.__wind(ax1) + ax1.legend(loc="best").set_draggable(True) + + # Create density and speed of sound subplot + ax2 = plt.subplot(122) + ax2 = self.__density_speed_of_sound(ax2) + ax2.legend(loc="best").set_draggable(True) + + plt.subplots_adjust(wspace=0.5) + plt.show() + return None + def all(self): - """Prints out all graphs available about the Environment. + """Prints out all graphs available about the Environment. This includes + a complete description of the atmospheric model and the ensemble members + comparison if the atmospheric model is an ensemble. Parameters ---------- From 5160ca64532056ec83098315c6c000864b91c106 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Tue, 14 Feb 2023 01:12:53 +0100 Subject: [PATCH 2/3] FIX: adjust labels and legends --- rocketpy/plots/environment_plots.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/rocketpy/plots/environment_plots.py b/rocketpy/plots/environment_plots.py index 8b62a5741..070aa7daa 100644 --- a/rocketpy/plots/environment_plots.py +++ b/rocketpy/plots/environment_plots.py @@ -56,7 +56,7 @@ def __wind(self, ax): [self.environment.windSpeed(i) for i in self.grid], self.grid, "#ff7f0e", - label="Speed of Sound", + label="Wind Speed", ) ax.set_xlabel("Wind Speed (m/s)", color="#ff7f0e") ax.tick_params("x", colors="#ff7f0e") @@ -65,7 +65,7 @@ def __wind(self, ax): [self.environment.windDirection(i) for i in self.grid], self.grid, color="#1f77b4", - label="Density", + label="Wind Direction", ) axup.set_xlabel("Wind Direction (°)", color="#1f77b4") axup.tick_params("x", colors="#1f77b4") @@ -195,12 +195,10 @@ def atmospheric_model(self): # Create wind speed and wind direction subplot ax1 = plt.subplot(221) ax1 = self.__wind(ax1) - ax1.legend(loc="best").set_draggable(True) # Create density and speed of sound subplot ax2 = plt.subplot(222) ax2 = self.__density_speed_of_sound(ax2) - ax2.legend(loc="best").set_draggable(True) # Create wind u and wind v subplot ax3 = plt.subplot(223) @@ -210,7 +208,6 @@ def atmospheric_model(self): # Create pressure and temperature subplot ax4 = plt.subplot(224) ax4 = self.__pressure_temperature(ax4) - ax4.legend(loc="best").set_draggable(True) plt.subplots_adjust(wspace=0.5, hspace=0.3) plt.show() @@ -342,12 +339,10 @@ def info(self): # Create wind speed and wind direction subplot ax1 = plt.subplot(121) ax1 = self.__wind(ax1) - ax1.legend(loc="best").set_draggable(True) # Create density and speed of sound subplot ax2 = plt.subplot(122) ax2 = self.__density_speed_of_sound(ax2) - ax2.legend(loc="best").set_draggable(True) plt.subplots_adjust(wspace=0.5) plt.show() From 5d6239927a076c512c796426d1f2672017344173 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Tue, 14 Feb 2023 00:13:31 +0000 Subject: [PATCH 3/3] Fix code style issues with Black --- rocketpy/AeroSurfaces.py | 1 - rocketpy/EnvironmentAnalysis.py | 16 +++++++--------- rocketpy/Parachute.py | 2 -- rocketpy/plots/compare/compare.py | 1 - rocketpy/plots/compare/compare_flights.py | 4 ---- .../EPFL_Bella_Lui/bella_lui_flight_sim.py | 1 + .../acceptance/NDRT_2020/ndrt_2020_flight_sim.py | 1 + tests/test_environment.py | 1 - 8 files changed, 9 insertions(+), 18 deletions(-) diff --git a/rocketpy/AeroSurfaces.py b/rocketpy/AeroSurfaces.py index 6f8be379b..d8ca3435e 100644 --- a/rocketpy/AeroSurfaces.py +++ b/rocketpy/AeroSurfaces.py @@ -1464,7 +1464,6 @@ def geometricInfo(self): return None def aerodynamicInfo(self): - print(f"\nTail name: {self.name}") print(f"Tail Center of Pressure Position in Local Coordinates: {self.cp} m") print(f"Tail Lift Coefficient Slope: {self.clalpha:.3f} 1/rad") diff --git a/rocketpy/EnvironmentAnalysis.py b/rocketpy/EnvironmentAnalysis.py index 0b2a94b1b..5d1550304 100644 --- a/rocketpy/EnvironmentAnalysis.py +++ b/rocketpy/EnvironmentAnalysis.py @@ -637,7 +637,6 @@ def parsePressureLevelData(self): # Loop through time and save all values for timeIndex, timeNum in enumerate(timeNumArray): - dateString, hourString, dateTime = self.__timeNumToDateString( timeNum, timeNumArray.units, calendar="gregorian" ) @@ -820,7 +819,6 @@ def parseSurfaceData(self): # Loop through time and save all values for timeIndex, timeNum in enumerate(timeNumArray): - dateString, hourString, dateTime = self.__timeNumToDateString( timeNum, timeNumArray.units, calendar="gregorian" ) @@ -2138,7 +2136,7 @@ def plot_wind_gust_distribution_over_average_day(self): gs = fig.add_gridspec(nrows, ncols, hspace=0, wspace=0, left=0.12) axs = gs.subplots(sharex=True, sharey=True) x_min, x_max, y_min, y_max = 0, 0, 0, 0 - for (i, j) in [(i, j) for i in range(nrows) for j in range(ncols)]: + for i, j in [(i, j) for i in range(nrows) for j in range(ncols)]: hour = hours[i * ncols + j] ax = axs[i, j] ax.set_title(f"{float(hour):05.2f}".replace(".", ":"), y=0.8) @@ -2300,7 +2298,7 @@ def plot_sustained_surface_wind_speed_distribution_over_average_day( gs = fig.add_gridspec(nrows, ncols, hspace=0, wspace=0, left=0.12) axs = gs.subplots(sharex=True, sharey=True) x_min, x_max, y_min, y_max = 0, 0, 0, 0 - for (i, j) in [(i, j) for i in range(nrows) for j in range(ncols)]: + for i, j in [(i, j) for i in range(nrows) for j in range(ncols)]: hour = hours[i * ncols + j] ax = axs[i, j] ax.set_title(f"{float(hour):05.2f}".replace(".", ":"), y=0.8) @@ -2344,7 +2342,7 @@ def plot_sustained_surface_wind_speed_distribution_over_average_day( ) if windSpeedLimit: - for (i, j) in [(i, j) for i in range(nrows) for j in range(ncols)]: + for i, j in [(i, j) for i in range(nrows) for j in range(ncols)]: # Clear Sky Range Altitude Limits j] ax = axs[i, j] ax.vlines( @@ -2677,7 +2675,7 @@ def plot_wind_profile_over_average_day(self, clear_range_limits=False): gs = fig.add_gridspec(nrows, ncols, hspace=0, wspace=0, left=0.12) axs = gs.subplots(sharex=True, sharey=True) x_min, x_max, y_min, y_max = 0, 0, np.inf, 0 - for (i, j) in [(i, j) for i in range(nrows) for j in range(ncols)]: + for i, j in [(i, j) for i in range(nrows) for j in range(ncols)]: hour = hours[i * ncols + j] ax = axs[i, j] ax.plot(*self.average_wind_profile_at_given_hour[hour], "r-") @@ -2710,7 +2708,7 @@ def plot_wind_profile_over_average_day(self, clear_range_limits=False): ) if clear_range_limits: - for (i, j) in [(i, j) for i in range(nrows) for j in range(ncols)]: + for i, j in [(i, j) for i in range(nrows) for j in range(ncols)]: # Clear Sky Range Altitude Limits ax = axs[i, j] ax.fill_between( @@ -2819,7 +2817,7 @@ def plot_wind_heading_profile_over_average_day(self, clear_range_limits=False): gs = fig.add_gridspec(nrows, ncols, hspace=0, wspace=0, left=0.12) axs = gs.subplots(sharex=True, sharey=True) x_min, x_max, y_min, y_max = 0, 0, np.inf, 0 - for (i, j) in [(i, j) for i in range(nrows) for j in range(ncols)]: + for i, j in [(i, j) for i in range(nrows) for j in range(ncols)]: hour = hours[i * ncols + j] ax = axs[i, j] ax.plot(*self.average_wind_heading_profile_at_given_hour[hour], "r-") @@ -2844,7 +2842,7 @@ def plot_wind_heading_profile_over_average_day(self, clear_range_limits=False): ) if clear_range_limits: - for (i, j) in [(i, j) for i in range(nrows) for j in range(ncols)]: + for i, j in [(i, j) for i in range(nrows) for j in range(ncols)]: # Clear Sky range limits ax = axs[i, j] ax.fill_between( diff --git a/rocketpy/Parachute.py b/rocketpy/Parachute.py index 8e6ff9e80..9d924c1c7 100644 --- a/rocketpy/Parachute.py +++ b/rocketpy/Parachute.py @@ -141,13 +141,11 @@ def __str__(self): ) def info(self): - self.prints.all() return None def allInfo(self): - self.info() # self.plots.all() # Parachutes still doesn't have plots diff --git a/rocketpy/plots/compare/compare.py b/rocketpy/plots/compare/compare.py index d211fb0ca..e3e7daf0a 100644 --- a/rocketpy/plots/compare/compare.py +++ b/rocketpy/plots/compare/compare.py @@ -153,7 +153,6 @@ def create_comparison_figure( raise AttributeError(f"Invalid attribute {y_attributes[i]}.") for i, subplot in enumerate(ax): - # Set the labels for the x and y axis subplot.set_xlabel(x_labels[i]) subplot.set_ylabel(y_labels[i]) diff --git a/rocketpy/plots/compare/compare_flights.py b/rocketpy/plots/compare/compare_flights.py index 36cebf213..29f82e391 100644 --- a/rocketpy/plots/compare/compare_flights.py +++ b/rocketpy/plots/compare/compare_flights.py @@ -1204,7 +1204,6 @@ def compare_trajectories_3d( # Iterate through trajectories for index, flight in enumerate(flights): - x, y, z = flight # Update mx and min values to set the limits of the plot @@ -1383,7 +1382,6 @@ def __plot_xy( # Iterate through trajectories for index, flight in enumerate(flights): - x, y, _ = flight # Update mx and min values to set the limits of the plot @@ -1447,7 +1445,6 @@ def __plot_xz( # Iterate through trajectories for index, flight in enumerate(flights): - x, _, z = flight # Update mx and min values to set the limits of the plot @@ -1514,7 +1511,6 @@ def __plot_yz( # Iterate through trajectories for index, flight in enumerate(flights): - _, y, z = flight # Update mx and min values to set the limits of the plot diff --git a/tests/fixtures/acceptance/EPFL_Bella_Lui/bella_lui_flight_sim.py b/tests/fixtures/acceptance/EPFL_Bella_Lui/bella_lui_flight_sim.py index 5a6b97ea1..8f76951d5 100644 --- a/tests/fixtures/acceptance/EPFL_Bella_Lui/bella_lui_flight_sim.py +++ b/tests/fixtures/acceptance/EPFL_Bella_Lui/bella_lui_flight_sim.py @@ -112,6 +112,7 @@ position=parameters.get("tailDistanceToCM")[0], ) + # Parachute set-up def drogueTrigger(p, y): # p = pressure diff --git a/tests/fixtures/acceptance/NDRT_2020/ndrt_2020_flight_sim.py b/tests/fixtures/acceptance/NDRT_2020/ndrt_2020_flight_sim.py index fefab76ee..ece9545e2 100644 --- a/tests/fixtures/acceptance/NDRT_2020/ndrt_2020_flight_sim.py +++ b/tests/fixtures/acceptance/NDRT_2020/ndrt_2020_flight_sim.py @@ -122,6 +122,7 @@ position=parameters.get("transitiondistanceToCM")[0], ) + # Parachute set-up def drogueTrigger(p, y): # p = pressure diff --git a/tests/test_environment.py b/tests/test_environment.py index 352b5c77b..2b58ccba9 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -147,7 +147,6 @@ def test_gefs_atmosphere(mock_show, example_env_robust): @patch("matplotlib.pyplot.show") def test_info_returns(mock_show, example_env): - returned_plots = example_env.allPlotInfoReturned() returned_infos = example_env.allInfoReturned() expected_info = {