Skip to content

Commit 17d192d

Browse files
committed
help
1 parent 9d868bb commit 17d192d

2 files changed

Lines changed: 70 additions & 31 deletions

File tree

campyros/new_wind.py

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import getgfs, dateutil.parser, pickle
2-
from datetime import datetime
1+
import getgfs, dateutil.parser, pickle, warnings
2+
from datetime import datetime, date
33
from pathlib import Path
4+
import numpy as np
45

56
__copyright__ = """
67
@@ -24,7 +25,7 @@
2425
Path("data/wind").mkdir(parents=True,exist_ok=True)
2526

2627
class 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
6787
if __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()"""

campyros/tests/test.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
)
99
import campyros as pyro
1010
from campyros import statistical as stats
11+
from campyros import new_wind as wind
1112
import csv
1213
import time
1314
import numpy as np
1415
import pandas as pd
16+
from datetime import date, timedelta
1517

1618
__copyright__ = """
1719
@@ -136,7 +138,7 @@
136138
alt_poll_interval=1,
137139
parachute=parachute,
138140
)
139-
141+
"""
140142
run = martlet4.run(debug=False)
141143
142144
test_output = pyro.from_json("campyros/tests/test.json")
@@ -166,10 +168,10 @@
166168
parachute_ind_run = ind
167169
if "Cleared rail" in ev:
168170
rail_ind_run = ind
169-
171+
"""
170172

171173
class ExampleTest(unittest.TestCase):
172-
def test_time(self):
174+
"""def test_time(self):
173175
self.assertAlmostEqual(run_time // 5, run.time.max() // 5, places=0)
174176
175177
def test_apogee(self):
@@ -214,8 +216,18 @@ def test_stats(self):
214216
True,
215217
ran,
216218
msg="Statistical model run failed, no further information automatically available",
217-
)
218-
219+
)"""
220+
221+
def test_wind(self):
222+
wind1=wind.Wind((date.today()-timedelta(days=1)).strftime("%Y%m%d %H:%M"))
223+
wind1_val=wind1.get(239,923,292728238)
224+
self.assertIsInstance(wind1_val,type((0.0,0.0)))
225+
self.assertEqual(2,len(wind1_val))
226+
227+
wind2=wind.Wind(variable=False)
228+
wind2_val=wind2.get(-239,-923,-292728238)
229+
self.assertEqual(0,wind2_val[0])
230+
self.assertEqual(0,wind2_val[1])
219231

220232
if __name__ == "__main__":
221233
unittest.main()

0 commit comments

Comments
 (0)