Skip to content

Enh: Using jsonpickle to serialize EnvAnal #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ requests
pytz
timezonefinder
simplekml
jsonpickle
68 changes: 10 additions & 58 deletions rocketpy/EnvironmentAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections import defaultdict

import ipywidgets as widgets
import jsonpickle
import matplotlib.ticker as mtick
import netCDF4
import numpy as np
Expand Down Expand Up @@ -2900,64 +2901,15 @@ def exportMeanProfiles(self, filename="export_env_analysis"):

return None

def saveEnvAnalysisDict(self, filename="EnvAnalysisDict"):
"""
Saves the Environment Analysis dictionary to a file in order to it
be load again in the future.
TODO: Improve docs
"""

# Refactor Function Objects in the pressureLevelDict so it can be converted to .json file as lists
pressureLevelDataDictCopy = copy.deepcopy(self.pressureLevelDataDict)

for days in pressureLevelDataDictCopy.keys():
for hours in pressureLevelDataDictCopy[days].keys():
for variable in pressureLevelDataDictCopy[days][hours].keys():
pressureLevelDataDictCopy[days][hours][variable] = np.column_stack(
(
pressureLevelDataDictCopy[days][hours][
variable
].getSource()[:, 0],
pressureLevelDataDictCopy[days][hours][
variable
].getSource()[:, 1],
)
).tolist()

self.EnvAnalysisDict = {
"start_date": self.start_date.strftime("%Y-%m-%d"),
"end_date": self.end_date.strftime("%Y-%m-%d"),
"start_hour": self.start_hour,
"end_hour": self.end_hour,
"latitude": self.latitude,
"longitude": self.longitude,
"elevation": self.elevation,
"timeZone": str(self.preferred_timezone),
"unit_system": self.unit_system,
# "maxExpectedHeight": 80000, # TODO: Implement this parameter at EnvAnalysis Class
"surfaceDataFile": self.surfaceDataFile,
"pressureLevelDataFile": self.pressureLevelDataFile,
"surfaceDataDict": self.surfaceDataDict,
"pressureLevelDataDict": pressureLevelDataDictCopy,
}
@classmethod
def load(self, filename="EnvAnalysisDict"):
encoded_class = open(filename).read()
return jsonpickle.decode(encoded_class)

# Convert to json
f = open(filename + ".json", "w")

# write json object to file
f.write(
json.dumps(self.EnvAnalysisDict, sort_keys=False, indent=4, default=str)
)

# close file
f.close()
print(
"Your Environment Analysis file was saved, check it out: "
+ filename
+ ".json"
)
print(
"You can use it in the future by using the load_previous_data of EnvironmentAnalysis Class."
)
def save(self, filename="EnvAnalysisDict"):
encoded_class = jsonpickle.encode(self)
file = open(filename, "w")
file.write(encoded_class)
file.close()

return None