Skip to content

Commit ce2d65c

Browse files
Merge pull request #239 from RocketPy-Team/enh/jsonpickle
Enh: Using jsonpickle to serialize EnvAnal
2 parents 19ebc44 + cda9198 commit ce2d65c

2 files changed

Lines changed: 11 additions & 58 deletions

File tree

requirements.txt

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

rocketpy/EnvironmentAnalysis.py

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from collections import defaultdict
1212

1313
import ipywidgets as widgets
14+
import jsonpickle
1415
import matplotlib.ticker as mtick
1516
import netCDF4
1617
import numpy as np
@@ -2900,64 +2901,15 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
29002901

29012902
return None
29022903

2903-
def saveEnvAnalysisDict(self, filename="EnvAnalysisDict"):
2904-
"""
2905-
Saves the Environment Analysis dictionary to a file in order to it
2906-
be load again in the future.
2907-
TODO: Improve docs
2908-
"""
2909-
2910-
# Refactor Function Objects in the pressureLevelDict so it can be converted to .json file as lists
2911-
pressureLevelDataDictCopy = copy.deepcopy(self.pressureLevelDataDict)
2912-
2913-
for days in pressureLevelDataDictCopy.keys():
2914-
for hours in pressureLevelDataDictCopy[days].keys():
2915-
for variable in pressureLevelDataDictCopy[days][hours].keys():
2916-
pressureLevelDataDictCopy[days][hours][variable] = np.column_stack(
2917-
(
2918-
pressureLevelDataDictCopy[days][hours][
2919-
variable
2920-
].getSource()[:, 0],
2921-
pressureLevelDataDictCopy[days][hours][
2922-
variable
2923-
].getSource()[:, 1],
2924-
)
2925-
).tolist()
2926-
2927-
self.EnvAnalysisDict = {
2928-
"start_date": self.start_date.strftime("%Y-%m-%d"),
2929-
"end_date": self.end_date.strftime("%Y-%m-%d"),
2930-
"start_hour": self.start_hour,
2931-
"end_hour": self.end_hour,
2932-
"latitude": self.latitude,
2933-
"longitude": self.longitude,
2934-
"elevation": self.elevation,
2935-
"timeZone": str(self.preferred_timezone),
2936-
"unit_system": self.unit_system,
2937-
# "maxExpectedHeight": 80000, # TODO: Implement this parameter at EnvAnalysis Class
2938-
"surfaceDataFile": self.surfaceDataFile,
2939-
"pressureLevelDataFile": self.pressureLevelDataFile,
2940-
"surfaceDataDict": self.surfaceDataDict,
2941-
"pressureLevelDataDict": pressureLevelDataDictCopy,
2942-
}
2904+
@classmethod
2905+
def load(self, filename="EnvAnalysisDict"):
2906+
encoded_class = open(filename).read()
2907+
return jsonpickle.decode(encoded_class)
29432908

2944-
# Convert to json
2945-
f = open(filename + ".json", "w")
2946-
2947-
# write json object to file
2948-
f.write(
2949-
json.dumps(self.EnvAnalysisDict, sort_keys=False, indent=4, default=str)
2950-
)
2951-
2952-
# close file
2953-
f.close()
2954-
print(
2955-
"Your Environment Analysis file was saved, check it out: "
2956-
+ filename
2957-
+ ".json"
2958-
)
2959-
print(
2960-
"You can use it in the future by using the load_previous_data of EnvironmentAnalysis Class."
2961-
)
2909+
def save(self, filename="EnvAnalysisDict"):
2910+
encoded_class = jsonpickle.encode(self)
2911+
file = open(filename, "w")
2912+
file.write(encoded_class)
2913+
file.close()
29622914

29632915
return None

0 commit comments

Comments
 (0)