Skip to content

Commit d4babbe

Browse files
Merge branch 'enh/env_analysis_euroc' into enh/env_analysis_wind_direction
2 parents 2492116 + 58bd0c4 commit d4babbe

File tree

6 files changed

+52
-94
lines changed

6 files changed

+52
-94
lines changed
Binary file not shown.
Binary file not shown.

docs/user/requirements.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The following packages are needed in order to run RocketPy:
2727
- timezonefinder
2828
- simplekml
2929
- ipywidgets >= 7.6.3
30-
- json
30+
- jsonpickle
3131

3232

3333
All of these packages, with the exception of netCDF4, should be automatically installed when RocketPy is installed using either ``pip`` or ``conda``.
@@ -50,6 +50,7 @@ The packages needed can be installed via ``pip`` by running the following lines
5050
pip install pytz
5151
pip install timezonefinder
5252
pip install simplekml
53+
pip install jsonpickle
5354
5455
Installing Required Packages Using ``conda``
5556
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ requests
88
pytz
99
timezonefinder
1010
simplekml
11-
json
11+
jsonpickle

rocketpy/EnvironmentAnalysis.py

Lines changed: 48 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from collections import defaultdict
1111

1212
import ipywidgets as widgets
13+
import jsonpickle
1314
import matplotlib.ticker as mtick
1415
import netCDF4
1516
import numpy as np
@@ -67,7 +68,6 @@ def __init__(
6768
pressureLevelDataFile=None,
6869
timezone=None,
6970
unit_system="metric",
70-
load_previous_data=None,
7171
):
7272
"""Constructor for the EnvironmentAnalysis class.
7373
Parameters
@@ -102,10 +102,6 @@ def __init__(
102102
unit_system : str, optional
103103
Unit system to be used when displaying results.
104104
Options are: SI, metric, imperial. Default is metric.
105-
load_previous_data : str, optional
106-
If True, the class will try to load the data from a previous ran Environment Analysis.
107-
Use .json file resulted from ... as input.
108-
Default is None.
109105
Returns
110106
-------
111107
None
@@ -125,58 +121,20 @@ def __init__(
125121
self.pressureLevelDataFile = pressureLevelDataFile
126122
self.preferred_timezone = timezone
127123
self.unit_system = unit_system
128-
self.load_previous_data = load_previous_data
129124

130125
# Manage units and timezones
131126
self.__init_data_parsing_units()
132127
self.__find_preferred_timezone()
133128
self.__localize_input_dates()
134129

135130
# Parse data files, surface goes first to calculate elevation
136-
if load_previous_data is None:
137-
self.surfaceDataDict = {}
138-
self.parseSurfaceData()
139-
self.pressureLevelDataDict = {}
140-
self.parsePressureLevelData()
141-
142-
# Convert units
143-
self.set_unit_system(unit_system)
144-
else:
145-
# Opening JSON file
146-
try:
147-
with open(self.load_previous_data) as json_file:
148-
self.loaded_data = json.load(json_file)
149-
except:
150-
raise RuntimeError(
151-
"Unable to read json file from previous ran Environment Analysis. Please try again."
152-
)
131+
self.surfaceDataDict = {}
132+
self.parseSurfaceData()
133+
self.pressureLevelDataDict = {}
134+
self.parsePressureLevelData()
153135

154-
self.surfaceDataDict = self.loaded_data["surfaceDataDict"]
155-
self.pressureLevelDataDict = self.loaded_data["pressureLevelDataDict"]
156-
print(
157-
"Information of the data loaded from previous Environment Analysis.\n"
158-
)
159-
print(
160-
"Available dates: ",
161-
self.loaded_data["start_date"],
162-
" to ",
163-
self.loaded_data["end_date"],
164-
)
165-
print(
166-
"Available hours: ",
167-
self.loaded_data["start_hour"],
168-
" to ",
169-
self.loaded_data["end_hour"],
170-
)
171-
print("Latitude", self.loaded_data["latitude"])
172-
print("Longitude", self.loaded_data["longitude"])
173-
print("Elevation:", self.loaded_data["elevation"])
174-
print("Surface data file: ", self.loaded_data["surfaceDataFile"])
175-
print(
176-
"Pressure level data file: ", self.loaded_data["pressureLevelDataFile"]
177-
)
178-
print("User timezone: ", self.loaded_data["preferred_timezone"])
179-
print("User unit system: ", self.loaded_data["unit_system"])
136+
# Convert units
137+
self.set_unit_system(unit_system)
180138

181139
# Initialize result variables
182140
self.average_max_temperature = 0
@@ -628,7 +586,7 @@ def parsePressureLevelData(self):
628586
variablePointsArray = np.array([heightAboveSeaLevelArray, valueArray]).T
629587
variableFunction = Function(
630588
variablePointsArray,
631-
inputs="Height Above Ground Level (m)",
589+
inputs="Height Above Ground Level (m)", # TODO: Check if it is really AGL or ASL here, see 3 lines above
632590
outputs=key,
633591
extrapolation="constant",
634592
)
@@ -3025,7 +2983,15 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
30252983
Exports the mean profiles of the weather data to a file in order to it
30262984
be used as inputs on Environment Class by using the CustomAtmosphere
30272985
model.
3028-
TODO: Improve docs
2986+
2987+
Parameters
2988+
----------
2989+
filename : str, optional
2990+
Name of the file where to be saved, by default "EnvAnalysisDict"
2991+
2992+
Returns
2993+
-------
2994+
None
30292995
"""
30302996

30312997
self.process_temperature_profile_over_average_day()
@@ -3077,8 +3043,6 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
30773043
# "maxExpectedHeight": 80000, # TODO: Implement this parameter at EnvAnalysis Class
30783044
"surfaceDataFile": self.surfaceDataFile,
30793045
"pressureLevelDataFile": self.pressureLevelDataFile,
3080-
# "surfaceDataDict": self.surfaceDataDict, # TODO: Too large, make it optional
3081-
# "pressureLevelDataDict": self.pressureLevelDataDict, # TODO: Too large, make it optional
30823046
"atmosphericModelPressureProfile": organized_pressure_dict,
30833047
"atmosphericModelTemperatureProfile": organized_temperature_dict,
30843048
"atmosphericModelWindVelocityXProfile": organized_windX_dict,
@@ -3106,47 +3070,40 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
31063070

31073071
return None
31083072

3109-
def saveEnvAnalysisDict(self, filename="EnvAnalysisDict"):
3110-
"""
3111-
Saves the Environment Analysis dictionary to a file in order to it
3112-
be load again in the future.
3113-
TODO: Improve docs
3114-
"""
3073+
@classmethod
3074+
def load(self, filename="EnvAnalysisDict"):
3075+
"""Load a previously saved Environment Analysis file.
3076+
Example: EnvA = EnvironmentAnalysis.load("filename").
31153077
3116-
self.EnvAnalysisDict = {
3117-
"start_date": self.start_date.strftime("%Y-%m-%d"),
3118-
"end_date": self.end_date.strftime("%Y-%m-%d"),
3119-
"start_hour": self.start_hour,
3120-
"end_hour": self.end_hour,
3121-
"latitude": self.latitude,
3122-
"longitude": self.longitude,
3123-
"elevation": self.elevation,
3124-
"timeZone": str(self.preferred_timezone),
3125-
"unit_system": self.unit_system,
3126-
# "maxExpectedHeight": 80000, # TODO: Implement this parameter at EnvAnalysis Class
3127-
"surfaceDataFile": self.surfaceDataFile,
3128-
"pressureLevelDataFile": self.pressureLevelDataFile,
3129-
"surfaceDataDict": self.surfaceDataDict,
3130-
"pressureLevelDataDict": self.pressureLevelDataDict,
3131-
}
3078+
Parameters
3079+
----------
3080+
filename : str, optional
3081+
Name of the previous saved file, by default "EnvAnalysisDict"
31323082
3133-
# Convert to json
3134-
f = open(filename + ".json", "w")
3083+
Returns
3084+
-------
3085+
EnvironmentAnalysis object
31353086
3136-
# write json object to file
3137-
f.write(
3138-
json.dumps(self.EnvAnalysisDict, sort_keys=False, indent=4, default=str)
3139-
)
3087+
"""
3088+
encoded_class = open(filename).read()
3089+
return jsonpickle.decode(encoded_class)
31403090

3141-
# close file
3142-
f.close()
3143-
print(
3144-
"Your Environment Analysis file was saved, check it out: "
3145-
+ filename
3146-
+ ".json"
3147-
)
3148-
print(
3149-
"You can use it in the future by using the customAtmosphere atmospheric model."
3150-
)
3091+
def save(self, filename="EnvAnalysisDict"):
3092+
"""Save the Environment Analysis object to a file so it can be used later.
3093+
3094+
Parameters
3095+
----------
3096+
filename : str, optional
3097+
Name of the file where to be saved, by default "EnvAnalysisDict"
3098+
3099+
Returns
3100+
-------
3101+
None
3102+
"""
3103+
encoded_class = jsonpickle.encode(self)
3104+
file = open(filename, "w")
3105+
file.write(encoded_class)
3106+
file.close()
3107+
print("Your Environment Analysis file was saved, check it out: " + filename)
31513108

31523109
return None

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"pytz",
1717
"timezonefinder",
1818
"simplekml",
19-
"json",
19+
"jsonpickle",
2020
],
2121
maintainer="RocketPy Developers",
2222
author="Giovani Hidalgo Ceotto",

0 commit comments

Comments
 (0)