1- import getgfs , dateutil .parser , pickle
2- from datetime import datetime
1+ import getgfs , dateutil .parser , pickle , warnings
2+ from datetime import datetime , date
33from pathlib import Path
4+ import numpy as np
45
56__copyright__ = """
67
2425Path ("data/wind" ).mkdir (parents = True ,exist_ok = True )
2526
2627class Wind :
27- def __init__ (self ,datetime ,cache = False ):
28+ def __init__ (self ,datetime = date . today (). strftime ( "%Y%m%d %H:%M" ), default = np . array ([ 0 , 0 ]), variable = True ,cache = False ):
2829 """Initites wind object so wind for some position can be searched
2930
3031 Note
@@ -37,33 +38,59 @@ def __init__(self,datetime,cache=False):
3738 """
3839 self .launch_time = dateutil .parser .parse (datetime ).timestamp ()
3940 self .cache = cache
41+ self .default = default
42+ self .variable = variable
4043
41- self .forecast = getgfs .Forecast ("0p25" )
44+ self .forecast = getgfs .Forecast ("0p25" , "1hr" )
4245 self .profiles = {}#(lat,long,datetime):interp profile
4346 self .points = []#tuples (lat,long,forecast)
4447
4548
46- def get (self ,lat ,long ,alt ,flight_time ):
47- 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 )]
48- 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 )]
49- request_timestamp = self .launch_time + flight_time
50- request_datetime = datetime .fromtimestamp (request_timestamp ).strftime ("%Y-%m-%d %H:%M" )
51- forecast_to_use = self .forecast .datetime_to_forecast (request_datetime )
52- if (lat ,long , forecast_to_use ) not in self .points :
53- if self .cache == False or not Path ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use )).is_file ():
54- self .profiles [(lat ,long ,forecast_to_use )]= self .forecast .get_windprofile (request_datetime ,lat ,long )
55- self .points .append ((lat ,long ,forecast_to_use ))
56-
57- if self .cache == True :
58- with open ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use ),"wb" ) as dump_file :
59- pickle .dump (self .profiles [(lat ,long ,forecast_to_use )],dump_file )
60- else :
61- with open ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use ),"rb" ) as dump_file :
62- self .profiles [(lat ,long ,forecast_to_use )]= pickle .load (dump_file )
63- self .points .append ((lat ,long ,forecast_to_use ))
64-
65- return self .profiles [(lat ,long ,forecast_to_use )][0 ](alt ),self .profiles [(lat ,long ,forecast_to_use )][2 ](alt )
49+ def get (self ,lat ,long ,alt ,flight_time = 0 ):
50+ """[summary]
6651
52+ Args:
53+ lat (float): latitude
54+ long (float): longitude
55+ alt (float): altitude
56+ flight_time (float, optional): time into flight. Defaults to 0.
57+
58+ Returns:
59+ tuple: returns u and v components of wind
60+ """
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 ():
69+ 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 ))
72+ except Exception as e :
73+ warnings .warn ("Wind could not be found - defaults used, gfspy gave the message %s" % e )
74+ return self .default
75+
76+ if self .cache == True :
77+ with open ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use ),"wb" ) as dump_file :
78+ pickle .dump (self .profiles [(lat ,long ,forecast_to_use )],dump_file )
79+ else :
80+ with open ("data/wind/%s_%s_%s.pkl" % (lat ,long ,forecast_to_use ),"rb" ) as dump_file :
81+ self .profiles [(lat ,long ,forecast_to_use )]= pickle .load (dump_file )
82+ self .points .append ((lat ,long ,forecast_to_use ))
83+
84+ return self .profiles [(lat ,long ,forecast_to_use )][0 ](alt ),self .profiles [(lat ,long ,forecast_to_use )][1 ](alt )
85+ else :
86+ return self .default
6787if __name__ == "__main__" :
68- w = Wind ("20210321 15:00" ,cache = True )
69- print (w .get (0 ,0 ,0 ,0 ))
88+ w = Wind ("20210325 15:00" ,cache = True )
89+ print (w .get (2938 ,29328 ,20866566 ))
90+ """times=np.linspace(0,60*60*12,1000)
91+ x=[]
92+ for t in times:
93+ x.append(w.get(37,20,1000,t)[0])
94+ import matplotlib.pyplot as plt
95+ plt.plot(times,x)
96+ plt.show()"""
0 commit comments