forked from bjing2016/habsim
-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
executable file
·187 lines (160 loc) · 6.47 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from flask import Flask, jsonify, request, Response, render_template, send_from_directory
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
import elev
from datetime import datetime, timezone
import simulate
from gefs import listdir_gefs, open_gefs
@app.route('/sim/which')
def whichgefs():
simulate.refresh()
return simulate.currgefs
@app.route('/sim/status')
def status():
f = open_gefs('whichgefs')
s = f.readline()
f.close()
return s
@app.route('/sim/ls')
def ls():
files = listdir_gefs()
return jsonify({
"count": len(files),
"files": files
})
'''
Returns a json object representing the flight path, given a UTC launch time (yr, mo, day, hr, mn),
a location (lat, lon), a launch elevation (alt), a drift coefficient (coeff),
a maximum duration in hrs (dur), a step interval in seconds (step), and a GEFS model number (model)
Return format is a list of [loc1, loc2 ...] where each loc is a list [lat, lon, altitude, u-wind, v-wind]
u-wind is wind towards the EAST: wind vector in the positive X direction
v-wind is wind towards the NORTH: wind vector in the positve Y direction
'''
@app.route('/sim/singlepredicth')
def singlepredicth():
args = request.args
yr, mo, day, hr, mn = int(args['yr']), int(args['mo']), int(args['day']), int(args['hr']), int(args['mn'])
lat, lon = float(args['lat']), float(args['lon'])
rate, dur, step = float(args['rate']), float(args['dur']), float(args['step'])
model = int(args['model'])
coeff = float(args['coeff'])
alt = float(args['alt'])
#simulate.refresh()
try:
path = simulate.simulate(datetime(yr, mo, day, hr, mn).replace(tzinfo=timezone.utc), lat, lon, rate, step, dur, alt, model, coefficient=coeff)
except:
return "error"
return jsonify(path)
@app.route('/sim/singlepredict')
def singlepredict():
args = request.args
timestamp = datetime.utcfromtimestamp(float(args['timestamp'])).replace(tzinfo=timezone.utc)
lat, lon = float(args['lat']), float(args['lon'])
rate, dur, step = float(args['rate']), float(args['dur']), float(args['step'])
model = int(args['model'])
coeff = float(args['coeff'])
alt = float(args['alt'])
#simulate.refresh()
try:
path = simulate.simulate(timestamp, lat, lon, rate, step, dur, alt, model, coefficient=coeff)
except:
return "error"
return jsonify(path)
def singlezpb(timestamp, lat, lon, alt, equil, eqtime, asc, desc, model):
try:
simulate.refresh()
dur = 0 if equil == alt else (equil - alt) / asc / 3600
rise = simulate.simulate(timestamp, lat, lon, asc, 240, dur, alt, model, elevation=False)
if len(rise) > 0:
timestamp, lat, lon, alt, __, __, __, __= rise[-1]
timestamp = datetime.utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc)
coast = simulate.simulate(timestamp, lat, lon, 0, 240, eqtime, alt, model)
if len(coast) > 0:
timestamp, lat, lon, alt, __, __, __, __ = coast[-1]
timestamp = datetime.utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc)
dur = (alt) / desc / 3600
fall = simulate.simulate(timestamp, lat, lon, -desc, 240, dur, alt, model)
return (rise, coast, fall)
except Exception as e:
if str(e) == "alt out of range":
return "alt error"
return "error"
@app.route('/sim/singlezpb')
def singlezpbh():
args = request.args
timestamp = datetime.utcfromtimestamp(float(args['timestamp'])).replace(tzinfo=timezone.utc)
lat, lon = float(args['lat']), float(args['lon'])
alt = float(args['alt'])
equil = float(args['equil'])
eqtime = float(args['eqtime'])
asc, desc = float(args['asc']), float(args['desc'])
model = int(args['model'])
path = singlezpb(timestamp, lat, lon, alt, equil, eqtime, asc, desc, model)
return jsonify(path)
@app.route('/sim/spaceshot')
def spaceshot():
args = request.args
timestamp = datetime.utcfromtimestamp(float(args['timestamp'])).replace(tzinfo=timezone.utc)
lat, lon = float(args['lat']), float(args['lon'])
alt = float(args['alt'])
equil = float(args['equil'])
eqtime = float(args['eqtime'])
asc, desc = float(args['asc']), float(args['desc'])
paths = list()
for model in range(1,21):
paths.append(singlezpb(timestamp, lat, lon, alt, equil, eqtime, asc, desc, model))
return jsonify(paths)
'''
Given a lat and lon, returns the elevation as a string
'''
@app.route('/sim/elev')
def elevation():
lat, lon = float(request.args['lat']), float(request.args['lon'])
return str(elev.getElevation(lat, lon))
'''
Given a time (yr, mo, day, hr, mn), a location (lat, lon), and an altitude (alt)
returns a json object of [u-wind, v-wind, du/dh, dv/dh], where
u-wind = [u-wind-1, u-wind-2, u-wind-3...u-wind-20]
v-wind = [v-wind-1, v-wind-2, v-wind-3...v-wind-20]
du/dh = [du/dh-1, du/dh-2, du/dh-3...du/dh-20]
dv/dh = [dv/dh-1, dv/dh-2, dv/dh-3...dv/dh-20]
where the numbers are the GEFS model from which the data is extracted.
'''
@app.route('/sim/windensemble')
def windensemble():
#simulate.refresh()
args = request.args
lat, lon = float(args['lat']), float(args['lon'])
alt = float(args['alt'])
yr, mo, day, hr, mn = int(args['yr']), int(args['mo']), int(args['day']), int(args['hr']), int(args['mn'])
time = datetime(yr, mo, day, hr, mn).replace(tzinfo=timezone.utc)
uList = list()
vList = list()
duList = list()
dvList = list()
levels = simulate.GFSHIST if yr < 2019 else simulate.GEFS
for i in range(1, 21):
u, v, du, dv = simulate.get_wind(time,lat,lon,alt, i, levels)
uList.append(u)
vList.append(v)
duList.append(du)
dvList.append(dv)
return jsonify([uList, vList, duList, dvList])
'''
Given a time (yr, mo, day, hr, mn), a location (lat, lon), an altitude (alt),
and a model (model) returns a json object of u-wind, v-wind, du/dh, dv/dh for that location
extracted from that model.
'''
@app.route('/sim/wind')
def wind():
#simulate.refresh()
args = request.args
lat, lon = float(args['lat']), float(args['lon'])
model = int(args['model'])
alt = float(args['alt'])
yr, mo, day, hr, mn = int(args['yr']), int(args['mo']), int(args['day']), int(args['hr']), int(args['mn'])
levels = simulate.GFSHIST if yr < 2019 else simulate.GEFS
time = datetime(yr, mo, day, hr, mn).replace(tzinfo=timezone.utc)
u, v, du, dv = simulate.get_wind(time,lat,lon,alt, model, levels)
return jsonify([u, v, du, dv])