Skip to content

Commit 7b24b80

Browse files
ENH: saving EnvAnalysisDict into .json
1 parent d482d96 commit 7b24b80

1 file changed

Lines changed: 74 additions & 5 deletions

File tree

rocketpy/EnvironmentAnalysis.py

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(
6767
pressureLevelDataFile=None,
6868
timezone=None,
6969
unit_system="metric",
70+
load_previous_data=None,
7071
):
7172
"""Constructor for the EnvironmentAnalysis class.
7273
Parameters
@@ -101,11 +102,15 @@ def __init__(
101102
unit_system : str, optional
102103
Unit system to be used when displaying results.
103104
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.
104109
Returns
105110
-------
106111
None
107112
"""
108-
warnings.warn("Please notice this class is still under development")
113+
warnings.warn("Please notice this class is still under development, and some features may not work as expected as they were not exhaustively tested yet.")
109114

110115
# Save inputs
111116
self.start_date = start_date
@@ -117,17 +122,39 @@ def __init__(
117122
self.surfaceDataFile = surfaceDataFile
118123
self.pressureLevelDataFile = pressureLevelDataFile
119124
self.preferred_timezone = timezone
125+
self.load_previous_data = load_previous_data
120126

121127
# Manage units and timezones
122128
self.__init_data_parsing_units()
123129
self.__find_preferred_timezone()
124130
self.__localize_input_dates()
125131

126132
# Parse data files, surface goes first to calculate elevation
127-
self.surfaceDataDict = {}
128-
self.parseSurfaceData()
129-
self.pressureLevelDataDict = {}
130-
self.parsePressureLevelData()
133+
if load_previous_data is None:
134+
self.surfaceDataDict = {}
135+
self.parseSurfaceData()
136+
self.pressureLevelDataDict = {}
137+
self.parsePressureLevelData()
138+
else:
139+
# Opening JSON file
140+
try:
141+
with open(self.load_previous_data) as json_file:
142+
self.loaded_data = json.load(json_file)
143+
except:
144+
raise RuntimeError("Unable to read json file from previous ran Environment Analysis. Please try again.")
145+
146+
self.surfaceDataDict = self.loaded_data["surfaceDataDict"]
147+
self.pressureLevelDataDict = self.loaded_data["pressureLevelDataDict"]
148+
print("Information of the data loaded from previous Environment Analysis.\n")
149+
print("Available dates: ", self.loaded_data["start_date"], " to ", self.loaded_data["end_date"])
150+
print("Available hours: ", self.loaded_data["start_hour"], " to ", self.loaded_data["end_hour"])
151+
print("Latitude", self.loaded_data["latitude"])
152+
print("Longitude", self.loaded_data["longitude"])
153+
print("Elevation:", self.loaded_data["elevation"])
154+
print("Surface data file: ", self.loaded_data["surfaceDataFile"])
155+
print("Pressure level data file: ", self.loaded_data["pressureLevelDataFile"])
156+
print("User timezone: ", self.loaded_data["preferred_timezone"])
157+
print("User unit system: ", self.loaded_data["unit_system"])
131158

132159
# Convert units
133160
self.set_unit_system(unit_system)
@@ -2745,3 +2772,45 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
27452772
print("You can use it in the future by using the customAtmosphere atmospheric model.")
27462773

27472774
return None
2775+
2776+
def saveEnvAnalysisDict(self, filename="EnvAnalysisDict"):
2777+
"""
2778+
Saves the Environment Analysis dictionary to a file in order to it
2779+
be load again in the future.
2780+
TODO: Improve docs
2781+
"""
2782+
2783+
self.EnvAnalysisDict = {
2784+
"start_date": self.start_date,
2785+
"end_date": self.end_date,
2786+
"start_hour": self.start_hour,
2787+
"end_hour": self.end_hour,
2788+
"latitude": self.latitude,
2789+
"longitude": self.longitude,
2790+
"elevation": self.elevation,
2791+
"timeZone": self.preferred_timezone,
2792+
"unit_system": self.unit_system,
2793+
# "maxExpectedHeight": 80000, # TODO: Implement this parameter at EnvAnalysis Class
2794+
"surfaceDataFile": self.surfaceDataFile,
2795+
"pressureLevelDataFile": self.pressureLevelDataFile,
2796+
"surfaceDataDict": self.surfaceDataDict,
2797+
"pressureLevelDataDict": self.pressureLevelDataDict,
2798+
}
2799+
2800+
# Convert to json
2801+
f = open(filename+".json","w")
2802+
2803+
# write json object to file
2804+
f.write(json.dumps(
2805+
self.EnvAnalysisDict,
2806+
sort_keys=False,
2807+
indent=4,
2808+
default=str)
2809+
)
2810+
2811+
# close file
2812+
f.close()
2813+
print("Your Environment Analysis file was saved, check it out: " + filename + ".json")
2814+
print("You can use it in the future by using the customAtmosphere atmospheric model.")
2815+
2816+
return None

0 commit comments

Comments
 (0)