|
11 | 11 | from collections import defaultdict |
12 | 12 |
|
13 | 13 | import ipywidgets as widgets |
| 14 | +import jsonpickle |
14 | 15 | import matplotlib.ticker as mtick |
15 | 16 | import netCDF4 |
16 | 17 | import numpy as np |
@@ -2900,64 +2901,15 @@ def exportMeanProfiles(self, filename="export_env_analysis"): |
2900 | 2901 |
|
2901 | 2902 | return None |
2902 | 2903 |
|
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) |
2943 | 2908 |
|
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() |
2962 | 2914 |
|
2963 | 2915 | return None |
0 commit comments