5
5
__license__ = "MIT"
6
6
7
7
import bisect
8
- import copy
9
- import json
10
8
import warnings
11
9
from collections import defaultdict
12
10
@@ -69,7 +67,6 @@ def __init__(
69
67
pressureLevelDataFile = None ,
70
68
timezone = None ,
71
69
unit_system = "metric" ,
72
- load_previous_data = None ,
73
70
):
74
71
"""Constructor for the EnvironmentAnalysis class.
75
72
Parameters
@@ -104,10 +101,6 @@ def __init__(
104
101
unit_system : str, optional
105
102
Unit system to be used when displaying results.
106
103
Options are: SI, metric, imperial. Default is metric.
107
- load_previous_data : str, optional
108
- If True, the class will try to load the data from a previous ran Environment Analysis.
109
- Use .json file resulted from ... as input.
110
- Default is None.
111
104
Returns
112
105
-------
113
106
None
@@ -126,146 +119,20 @@ def __init__(
126
119
self .surfaceDataFile = surfaceDataFile
127
120
self .pressureLevelDataFile = pressureLevelDataFile
128
121
self .preferred_timezone = timezone
129
- self .load_previous_data = load_previous_data
130
122
self .unit_system = unit_system
131
123
132
124
# Manage units and timezones
133
125
self .__init_data_parsing_units ()
134
126
self .__find_preferred_timezone ()
135
127
self .__localize_input_dates ()
136
128
137
- # Parse data files, surface goes first to calculate elevation
138
- if load_previous_data is None :
139
- self .surfaceDataDict = {}
140
- self .parseSurfaceData ()
141
- self .pressureLevelDataDict = {}
142
- self .parsePressureLevelData ()
143
-
144
- # Convert units
145
- self .set_unit_system (unit_system )
146
- else : # In case of loading previous data, still need to test if all the other functions will work!
147
- # Opening JSON file
148
- try :
149
- with open (self .load_previous_data ) as json_file :
150
- self .loaded_data = json .load (json_file )
151
- except :
152
- raise RuntimeError (
153
- "Unable to read json file from previous ran Environment Analysis. Please try again."
154
- )
155
-
156
- # Load dictionary
157
- self .surfaceDataDict = self .loaded_data ["surfaceDataDict" ]
158
- self .pressureLevelDataDict = self .loaded_data ["pressureLevelDataDict" ]
159
-
160
- # Fix old masked values on surface data
161
- for days in self .surfaceDataDict .keys ():
162
- for hours in self .surfaceDataDict [days ].keys ():
163
- for variables in self .surfaceDataDict [days ][hours ].keys ():
164
- if self .surfaceDataDict [days ][hours ][variables ] == "--" :
165
- # TODO: is there any other value that we can use to prevent "NaN"?
166
- self .surfaceDataDict [days ][hours ][variables ] = float ("NaN" )
167
-
168
- # Initialize variables again, to accomplish to the data available at the loaded file
169
- self .elevation = self .loaded_data ["elevation" ]
170
- self .latitude = self .loaded_data ["latitude" ]
171
- self .longitude = self .loaded_data ["longitude" ]
172
-
173
- # Convert back to Function objects, then we will be able to process set_unit_system
174
- ## Use the units on the files, and then convert to the final/selected units
175
- decodeDict = {
176
- "pressure" : [
177
- "Height Above Sea Level (m)" ,
178
- "Pressure ({})" .format (self .loaded_data ["unit_system" ]["pressure" ]),
179
- ],
180
- "temperature" : [
181
- "Height Above Sea Level (m)" ,
182
- "Temperature ({})" .format (
183
- self .loaded_data ["unit_system" ]["temperature" ]
184
- ),
185
- ],
186
- "windDirection" : [
187
- "Height Above Sea Level (m)" ,
188
- "Wind Direction ({})" .format (
189
- self .loaded_data ["unit_system" ]["angle" ]
190
- ),
191
- ],
192
- "windHeading" : [
193
- "Height Above Sea Level (m)" ,
194
- "Wind Heading ({})" .format (
195
- self .loaded_data ["unit_system" ]["angle" ]
196
- ),
197
- ],
198
- "windSpeed" : [
199
- "Height Above Sea Level (m)" ,
200
- "Wind Speed ({})" .format (
201
- self .loaded_data ["unit_system" ]["wind_speed" ]
202
- ),
203
- ],
204
- "windVelocityX" : [
205
- "Height Above Sea Level (m)" ,
206
- "Wind Velocity X ({})" .format (
207
- self .loaded_data ["unit_system" ]["velocity" ]
208
- ),
209
- ],
210
- "windVelocityY" : [
211
- "Height Above Sea Level (m)" ,
212
- "Wind Velocity Y ({})" .format (
213
- self .loaded_data ["unit_system" ]["velocity" ]
214
- ),
215
- ],
216
- }
217
-
218
- # TODO: document this
219
- for days in self .pressureLevelDataDict .keys ():
220
- for hours in self .pressureLevelDataDict [days ].keys ():
221
- for variable in decodeDict .keys ():
222
- lst = self .pressureLevelDataDict [days ][hours ][variable ]
223
- self .pressureLevelDataDict [days ][hours ][variable ] = Function (
224
- source = np .column_stack (
225
- ([item [0 ] for item in lst ], [item [1 ] for item in lst ])
226
- ),
227
- inputs = [decodeDict [variable ][0 ]],
228
- outputs = [decodeDict [variable ][1 ]],
229
- interpolation = "linear" ,
230
- extrapolation = "constant" ,
231
- )
129
+ self .surfaceDataDict = {}
130
+ self .parseSurfaceData ()
131
+ self .pressureLevelDataDict = {}
132
+ self .parsePressureLevelData ()
232
133
233
- print (
234
- "Information of the data loaded from previous Environment Analysis.\n "
235
- )
236
- print (
237
- "Available dates: " ,
238
- self .loaded_data ["start_date" ],
239
- " to " ,
240
- self .loaded_data ["end_date" ],
241
- )
242
- print (
243
- "Available hours: " ,
244
- self .loaded_data ["start_hour" ],
245
- " to " ,
246
- self .loaded_data ["end_hour" ],
247
- )
248
- print ("Latitude" , self .loaded_data ["latitude" ])
249
- print ("Longitude" , self .loaded_data ["longitude" ])
250
- print ("Elevation:" , self .loaded_data ["elevation" ])
251
- print ("Surface data file: " , self .loaded_data ["surfaceDataFile" ])
252
- print (
253
- "Pressure level data file: " , self .loaded_data ["pressureLevelDataFile" ]
254
- )
255
- print ("File timezone: " , self .loaded_data ["timeZone" ])
256
- print ("File unit system: " , self .loaded_data ["unit_system" ])
257
- print ()
258
- if self .loaded_data ["unit_system" ] != self .unit_system :
259
- print (
260
- "The unit system of the loaded data is different from the selected unit system. Therefore all the values will be converted to the following unit system: "
261
- )
262
- # Convert units
263
- self .set_unit_system (unit_system )
264
- print (self .unit_system )
265
- print ()
266
- print (
267
- "Alright, the previous data file were completely loaded, all set to go!"
268
- )
134
+ # Convert units
135
+ self .set_unit_system (unit_system )
269
136
270
137
# Initialize result variables
271
138
self .average_max_temperature = 0
@@ -2903,13 +2770,38 @@ def exportMeanProfiles(self, filename="export_env_analysis"):
2903
2770
2904
2771
@classmethod
2905
2772
def load (self , filename = "EnvAnalysisDict" ):
2773
+ """Load a previously saved Environment Analysis file.
2774
+ Example: EnvA = EnvironmentAnalysis.load("filename").
2775
+
2776
+ Parameters
2777
+ ----------
2778
+ filename : str, optional
2779
+ Name of the previous saved file, by default "EnvAnalysisDict"
2780
+
2781
+ Returns
2782
+ -------
2783
+ EnvironmentAnalysis object
2784
+
2785
+ """
2906
2786
encoded_class = open (filename ).read ()
2907
2787
return jsonpickle .decode (encoded_class )
2908
2788
2909
2789
def save (self , filename = "EnvAnalysisDict" ):
2790
+ """Save the Environment Analysis object to a file so it can be used later.
2791
+
2792
+ Parameters
2793
+ ----------
2794
+ filename : str, optional
2795
+ Name of the file where to be saved, by default "EnvAnalysisDict"
2796
+
2797
+ Returns
2798
+ -------
2799
+ None
2800
+ """
2910
2801
encoded_class = jsonpickle .encode (self )
2911
2802
file = open (filename , "w" )
2912
2803
file .write (encoded_class )
2913
2804
file .close ()
2805
+ print ("Your Environment Analysis file was saved, check it out: " + filename )
2914
2806
2915
2807
return None
0 commit comments