11import getgfs , dateutil .parser , pickle , warnings
22from datetime import datetime , date , timedelta
3- from pathlib import Path
3+ from pathlib import Path
44import numpy as np
55
66__copyright__ = """
2222
2323"""
2424
25- Path ("data/wind" ).mkdir (parents = True ,exist_ok = True )
25+ Path ("data/wind" ).mkdir (parents = True , exist_ok = True )
26+
2627
2728class Wind :
28- def __init__ (self ,datetime = (date .today ()- timedelta (days = 2 )).strftime ("%Y%m%d %H:%M" ),default = np .array ([0 ,0 ,0 ]),variable = True ,cache = False ):
29+ def __init__ (
30+ self ,
31+ datetime = (date .today () - timedelta (days = 2 )).strftime ("%Y%m%d %H:%M" ),
32+ default = np .array ([0 , 0 , 0 ]),
33+ variable = True ,
34+ cache = False ,
35+ ):
2936 """Initites wind object so wind for some position can be searched
3037
3138 Note
@@ -36,17 +43,16 @@ def __init__(self,datetime=(date.today()-timedelta(days=2)).strftime("%Y%m%d %H:
3643 datetime (string): datetime of flight, can be in 'any' format as long as datetime parser can work it out (for now this will need to be UTC)
3744 cache (bool, optional): cache lat/long/time profile, useful for stats models where downloading for each run is a bottle neck. Defaults to False.
3845 """
39- self .launch_time = dateutil .parser .parse (datetime ).timestamp ()
40- self .cache = cache
41- self .default = default
42- self .variable = variable
46+ self .launch_time = dateutil .parser .parse (datetime ).timestamp ()
47+ self .cache = cache
48+ self .default = default
49+ self .variable = variable
4350
44- self .forecast = getgfs .Forecast ("0p25" ,"1hr" )
45- self .profiles = {} # (lat,long,datetime):interp profile
46- self .points = [] # tuples (lat,long,forecast)
51+ self .forecast = getgfs .Forecast ("0p25" , "1hr" )
52+ self .profiles = {} # (lat,long,datetime):interp profile
53+ self .points = [] # tuples (lat,long,forecast)
4754
48-
49- def get_wind (self ,lat ,long ,alt ,flight_time = 0 ):
55+ def get_wind (self , lat , long , alt , flight_time = 0 ):
5056 """[summary]
5157
5258 Args:
@@ -58,36 +64,72 @@ def get_wind(self,lat,long,alt,flight_time=0):
5864 Returns:
5965 tuple: returns u and v components of wind
6066 """
61- if self .variable == True :
62- lat = [float (self .forecast .coords ["lat" ]["resolution" ])* n + float (self .forecast .coords ["lat" ]["minimum" ]) for n in range (0 ,int (self .forecast .coords ["lat" ]["grads_size" ]))][self .forecast .value_to_index ("lat" ,lat )]
63- long = [float (self .forecast .coords ["lon" ]["resolution" ])* n + float (self .forecast .coords ["lon" ]["minimum" ]) for n in range (0 ,int (self .forecast .coords ["lon" ]["grads_size" ]))][self .forecast .value_to_index ("lon" ,long )]
64- request_timestamp = self .launch_time + flight_time
65- request_datetime = datetime .fromtimestamp (request_timestamp ).strftime ("%Y-%m-%d %H:%M" )
66- forecast_to_use = self .forecast .datetime_to_forecast (request_datetime )
67- if (lat ,long , forecast_to_use ) not in self .points :
68- if self .cache == False or not Path ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use )).is_file ():
67+ if self .variable == True :
68+ lat = [
69+ float (self .forecast .coords ["lat" ]["resolution" ]) * n
70+ + float (self .forecast .coords ["lat" ]["minimum" ])
71+ for n in range (0 , int (self .forecast .coords ["lat" ]["grads_size" ]))
72+ ][self .forecast .value_to_index ("lat" , lat )]
73+ long = [
74+ float (self .forecast .coords ["lon" ]["resolution" ]) * n
75+ + float (self .forecast .coords ["lon" ]["minimum" ])
76+ for n in range (0 , int (self .forecast .coords ["lon" ]["grads_size" ]))
77+ ][self .forecast .value_to_index ("lon" , long )]
78+ request_timestamp = self .launch_time + flight_time
79+ request_datetime = datetime .fromtimestamp (request_timestamp ).strftime (
80+ "%Y-%m-%d %H:%M"
81+ )
82+ forecast_to_use = self .forecast .datetime_to_forecast (request_datetime )
83+ if (lat , long , forecast_to_use ) not in self .points :
84+ if (
85+ self .cache == False
86+ or not Path (
87+ "data/wind/%s_%s_%s.pkl" % (lat , long , forecast_to_use )
88+ ).is_file ()
89+ ):
6990 try :
70- self .profiles [(lat ,long ,forecast_to_use )]= self .forecast .get_windprofile (request_datetime ,lat ,long )
71- self .points .append ((lat ,long ,forecast_to_use ))
91+ self .profiles [
92+ (lat , long , forecast_to_use )
93+ ] = self .forecast .get_windprofile (request_datetime , lat , long )
94+ self .points .append ((lat , long , forecast_to_use ))
7295 except Exception as e :
73- warnings .warn ("Wind could not be found - defaults used, gfspy gave the message:" )
96+ warnings .warn (
97+ "Wind could not be found - defaults used, gfspy gave the message:"
98+ )
7499 print (e )
75100 return self .default
76-
77- if self .cache == True :
78- with open ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use ),"wb" ) as dump_file :
79- pickle .dump (self .profiles [(lat ,long ,forecast_to_use )],dump_file )
101+
102+ if self .cache == True :
103+ with open (
104+ "data/wind/%s_%s_%s.pkl" % (lat , long , forecast_to_use ),
105+ "wb" ,
106+ ) as dump_file :
107+ pickle .dump (
108+ self .profiles [(lat , long , forecast_to_use )], dump_file
109+ )
80110 else :
81- with open ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use ),"rb" ) as dump_file :
82- self .profiles [(lat ,long ,forecast_to_use )]= pickle .load (dump_file )
83- self .points .append ((lat ,long ,forecast_to_use ))
84-
85- return np .array ([self .profiles [(lat ,long ,forecast_to_use )][0 ](alt ),self .profiles [(lat ,long ,forecast_to_use )][1 ](alt ),0 ])
111+ with open (
112+ "data/wind/%s_%s_%s.pkl" % (lat , long , forecast_to_use ), "rb"
113+ ) as dump_file :
114+ self .profiles [(lat , long , forecast_to_use )] = pickle .load (
115+ dump_file
116+ )
117+ self .points .append ((lat , long , forecast_to_use ))
118+
119+ return np .array (
120+ [
121+ self .profiles [(lat , long , forecast_to_use )][0 ](alt ),
122+ self .profiles [(lat , long , forecast_to_use )][1 ](alt ),
123+ 0 ,
124+ ]
125+ )
86126 else :
87127 return self .default
88- if __name__ == "__main__" :
89- w = Wind ("20210428 15:00" ,cache = True )
90- print (w .get (2938 ,29328 ,20866566 ))
128+
129+
130+ if __name__ == "__main__" :
131+ w = Wind ("20210428 15:00" , cache = True )
132+ print (w .get (2938 , 29328 , 20866566 ))
91133 """times=np.linspace(0,60*60*12,1000)
92134 x=[]
93135 for t in times:
0 commit comments