10
10
from collections import defaultdict
11
11
12
12
import ipywidgets as widgets
13
+ import jsonpickle
13
14
import matplotlib .ticker as mtick
14
15
import netCDF4
15
16
import numpy as np
@@ -67,7 +68,6 @@ def __init__(
67
68
pressureLevelDataFile = None ,
68
69
timezone = None ,
69
70
unit_system = "metric" ,
70
- load_previous_data = None ,
71
71
):
72
72
"""Constructor for the EnvironmentAnalysis class.
73
73
Parameters
@@ -102,10 +102,6 @@ def __init__(
102
102
unit_system : str, optional
103
103
Unit system to be used when displaying results.
104
104
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.
109
105
Returns
110
106
-------
111
107
None
@@ -125,58 +121,20 @@ def __init__(
125
121
self .pressureLevelDataFile = pressureLevelDataFile
126
122
self .preferred_timezone = timezone
127
123
self .unit_system = unit_system
128
- self .load_previous_data = load_previous_data
129
124
130
125
# Manage units and timezones
131
126
self .__init_data_parsing_units ()
132
127
self .__find_preferred_timezone ()
133
128
self .__localize_input_dates ()
134
129
135
130
# 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 ()
153
135
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 )
180
138
181
139
# Initialize result variables
182
140
self .average_max_temperature = 0
@@ -628,7 +586,7 @@ def parsePressureLevelData(self):
628
586
variablePointsArray = np .array ([heightAboveSeaLevelArray , valueArray ]).T
629
587
variableFunction = Function (
630
588
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
632
590
outputs = key ,
633
591
extrapolation = "constant" ,
634
592
)
@@ -3025,7 +2983,15 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
3025
2983
Exports the mean profiles of the weather data to a file in order to it
3026
2984
be used as inputs on Environment Class by using the CustomAtmosphere
3027
2985
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
3029
2995
"""
3030
2996
3031
2997
self .process_temperature_profile_over_average_day ()
@@ -3077,8 +3043,6 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
3077
3043
# "maxExpectedHeight": 80000, # TODO: Implement this parameter at EnvAnalysis Class
3078
3044
"surfaceDataFile" : self .surfaceDataFile ,
3079
3045
"pressureLevelDataFile" : self .pressureLevelDataFile ,
3080
- # "surfaceDataDict": self.surfaceDataDict, # TODO: Too large, make it optional
3081
- # "pressureLevelDataDict": self.pressureLevelDataDict, # TODO: Too large, make it optional
3082
3046
"atmosphericModelPressureProfile" : organized_pressure_dict ,
3083
3047
"atmosphericModelTemperatureProfile" : organized_temperature_dict ,
3084
3048
"atmosphericModelWindVelocityXProfile" : organized_windX_dict ,
@@ -3106,47 +3070,40 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
3106
3070
3107
3071
return None
3108
3072
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").
3115
3077
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"
3132
3082
3133
- # Convert to json
3134
- f = open (filename + ".json" , "w" )
3083
+ Returns
3084
+ -------
3085
+ EnvironmentAnalysis object
3135
3086
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 )
3140
3090
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 )
3151
3108
3152
3109
return None
0 commit comments