Skip to content

Commit b9a164f

Browse files
authored
ENH: Environment object from EnvironmentAnalysis (#813)
* ENH: add ```env``` method to ```EnvironmentAnalysis``` class This method allows the user to create a ```Environment``` object from the ```EnvironmentAnalysis``` object * ENH: fixing non-bijective pressure profiles * DOC: adding docstring to get_environemnt_object * TST: adding a test for get_environement_object * DOC: updating the CHANGELOG * ENH: converting the units into SI for Environment object * ENH: using numpy.percentile to recalculate the pressure profile * DOC: changing method name from get_environment_object to create_environment_object * DOC: fixing the test name to match method name * TST: fixing the method call in the test
1 parent 6e78dfc commit b9a164f

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Attention: The newest changes should be on top -->
3737
- ENH: Create a rocketpy file to store flight simulations [#800](https://github.com/RocketPy-Team/RocketPy/pull/800)
3838
- ENH: Support for the RSE file format has been added to the library [#798](https://github.com/RocketPy-Team/RocketPy/pull/798)
3939
- ENH: Introduce Net Thrust with pressure corrections [#789](https://github.com/RocketPy-Team/RocketPy/pull/789)
40+
- ENH: Environment object from EnvironmentAnalysis [#813](https://github.com/RocketPy-Team/RocketPy/pull/813)
4041

4142
### Changed
4243

rocketpy/environment/environment_analysis.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,3 +2885,79 @@ def save(self, filename="env_analysis_dict"):
28852885
file.write(encoded_class)
28862886
file.close()
28872887
print("Your Environment Analysis file was saved, check it out: " + filename)
2888+
2889+
def create_environment_object(
2890+
self, gravity=None, date=None, datum="SIRGAS2000", max_expected_height=80000.0
2891+
):
2892+
"""Creates an Environment object with the data from the Environment Analysis instance.
2893+
It uses the average values from the data.
2894+
2895+
Parameters
2896+
----------
2897+
gravity : int, float, callable, string, array, optional
2898+
Surface gravitational acceleration. Positive values point the
2899+
acceleration down. If None, the Somigliana formula is used.
2900+
See :meth:`Environment.set_gravity_model` for more information.
2901+
date : list or tuple, optional
2902+
List or tuple of length 4, stating (year, month, day, hour) in the
2903+
time zone used in the Environment Analysis instance.
2904+
Alternatively, can be a ``datetime`` object specifying launch
2905+
date and time. The dates are stored as follows:
2906+
2907+
- :attr:`Environment.local_date`: Local time of launch in
2908+
the time zone specified in the Environment Analysis instance.
2909+
2910+
- :attr:`Environment.datetime_date`: UTC time of launch.
2911+
2912+
Default is None.
2913+
See :meth:`Environment.set_date` for more information.
2914+
datum : string, optional
2915+
The desired reference ellipsoidal model, the following options are
2916+
available: "SAD69", "WGS84", "NAD83", and "SIRGAS2000". The default
2917+
is "SIRGAS2000".
2918+
max_expected_height : float, optional
2919+
Maximum altitude in meters to keep weather data. The altitude must
2920+
be above sea level (ASL). Especially useful for visualization. Can
2921+
be altered as desired by running ``max_expected_height = number``.
2922+
"""
2923+
elevation_si = convert_units(
2924+
self.converted_elevation, self.unit_system["length"], "m"
2925+
)
2926+
altitude_si = convert_units(self.altitude_list, self.unit_system["length"], "m")
2927+
# Recalculating pressure profile using numpy.percentile
2928+
pressures = [
2929+
day_dict[hour]["pressure"](self.altitude_list)
2930+
for day_dict in self.converted_pressure_level_data.values()
2931+
for hour in day_dict.keys()
2932+
]
2933+
pressure_profile = np.percentile(pressures, 50, axis=0)
2934+
pressure_si = convert_units(
2935+
pressure_profile, self.unit_system["pressure"], "Pa"
2936+
)
2937+
temperature_si = convert_units(
2938+
self.average_temperature_profile, self.unit_system["temperature"], "K"
2939+
)
2940+
wind_velocity_x_si = convert_units(
2941+
self.average_wind_velocity_x_profile, self.unit_system["wind_speed"], "m/s"
2942+
)
2943+
wind_velocity_y_si = convert_units(
2944+
self.average_wind_velocity_y_profile, self.unit_system["wind_speed"], "m/s"
2945+
)
2946+
env = Environment(
2947+
gravity,
2948+
date,
2949+
self.latitude,
2950+
self.longitude,
2951+
elevation_si,
2952+
datum,
2953+
self.preferred_timezone,
2954+
max_expected_height,
2955+
)
2956+
env.set_atmospheric_model(
2957+
type="custom_atmosphere",
2958+
pressure=list(zip(altitude_si, pressure_si)),
2959+
temperature=list(zip(altitude_si, temperature_si)),
2960+
wind_u=list(zip(altitude_si, wind_velocity_x_si)),
2961+
wind_v=list(zip(altitude_si, wind_velocity_y_si)),
2962+
)
2963+
return env

tests/integration/test_environment_analysis.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import matplotlib as plt
66
import pytest
7+
from rocketpy import Environment
78

89
plt.rcParams.update({"figure.max_open_warning": 0})
910

@@ -55,3 +56,9 @@ def test_exports(mock_show, env_analysis): # pylint: disable=unused-argument
5556
os.remove("env_analysis_dict")
5657
os.remove("wind_rose.gif")
5758
os.remove("export_env_analysis.json")
59+
60+
61+
@pytest.mark.slow
62+
@patch("matplotlib.pyplot.show")
63+
def test_create_environment_object(mock_show, env_analysis): # pylint: disable=unused-argument
64+
assert isinstance(env_analysis.create_environment_object(), Environment)

0 commit comments

Comments
 (0)