Repository navigation
Expand file tree
/
Copy pathvad.py
More file actions
128 lines (101 loc) · 4.46 KB
/
Copy pathvad.py
File metadata and controls
128 lines (101 loc) · 4.46 KB
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
from __future__ import print_function
import numpy as np
import sys
from vad_reader import download_vad, VADFile
from params import compute_parameters
from plot import plot_hodograph
from wsr88d import build_has_name
import re
import argparse
from datetime import datetime, timedelta
import json
import glob
"""
vad.py
Author: Tim Supinie (tsupinie@ou.edu)
Completed: May 2012
Modified: 26 April 2015
Fixed SRH calculations.
28 December 2015
Migrated to its own package, ravamped plot, fixed SRH calculations for real this time.
30 March 2016
Fixed RMS error circle size, and added Bunkers motion vector calculations.
"""
def is_vector(vec_str):
return bool(re.match(r"[\d]{3}/[\d]{2}", vec_str))
def parse_vector(vec_str):
return tuple(int(v) for v in vec_str.strip().split("/"))
def parse_time(time_str):
no_my = False
now = datetime.utcnow()
if '-' not in time_str:
no_my = True
year = now.year
month = now.month
time_str = "%d-%d-%s" % (year, month, time_str)
plot_time = datetime.strptime(time_str, '%Y-%m-%d/%H%M')
if plot_time > now:
if no_my:
if month == 1:
month = 12
year -= 1
else:
month -= 1
time_str = "%d-%d-%s" % (year, month, time_str)
plot_time = datetime.strptime(time_str, '%Y-%m-%d/%H%M')
else:
raise ValueError("Time '%s' is in the future." % time_str)
return plot_time
def vad_plotter(radar_id, storm_motion='right-mover', sfc_wind=None, time=None, fname=None, local_path=None,
cache_path=None, web=False, fixed=False):
plot_time = None
if time:
plot_time = parse_time(time)
elif local_path is not None:
raise ValueError("'-t' ('--time') argument is required when loading from the local disk.")
if not web:
print("Plotting VAD for %s ..." % radar_id)
if local_path is None:
vad = download_vad(radar_id, time=plot_time, cache_path=cache_path)
else:
iname = build_has_name(radar_id, plot_time)
vad = VADFile(open("%s/%s" % (local_path, iname), 'rb'))
vad.rid = radar_id
if not web:
print("Valid time:", vad['time'].strftime("%d %B %Y %H%M UTC"))
if sfc_wind:
sfc_wind = parse_vector(sfc_wind)
vad.add_surface_wind(sfc_wind)
params = compute_parameters(vad, storm_motion)
plot_hodograph(vad, params, fname=fname, web=web, fixed=fixed, archive=(local_path is not None))
def main():
ap = argparse.ArgumentParser()
ap.add_argument('radar_id', help="The 4-character identifier for the radar (e.g. KTLX, KFWS, etc.)")
ap.add_argument('-m', '--storm-motion', dest='storm_motion', help="Storm motion vector. It takes one of two forms. The first is either 'BRM' for the Bunkers right mover vector, or 'BLM' for the Bunkers left mover vector. The second is the form DDD/SS, where DDD is the direction the storm is coming from, and SS is the speed in knots (e.g. 240/25).", default='right-mover')
ap.add_argument('-s', '--sfc-wind', dest='sfc_wind', help="Surface wind vector. It takes the form DDD/SS, where DDD is the direction the storm is coming from, and SS is the speed in knots (e.g. 240/25).")
ap.add_argument('-t', '--time', dest='time', help="Time to plot. Takes the form DD/HHMM, where DD is the day, HH is the hour, and MM is the minute.")
ap.add_argument('-f', '--img-name', dest='img_name', help="Name of the file produced.")
ap.add_argument('-p', '--local-path', dest='local_path', help="Path to local data. If not given, download from the Internet.")
ap.add_argument('-c', '--cache-path', dest='cache_path', help="Path to local cache. Data downloaded from the Internet will be cached here.")
ap.add_argument('-w', '--web-mode', dest='web', action='store_true')
ap.add_argument('-x', '--fixed-frame', dest='fixed', action='store_true')
args = ap.parse_args()
np.seterr(all='ignore')
try:
vad_plotter(args.radar_id,
storm_motion=args.storm_motion,
sfc_wind=args.sfc_wind,
time=args.time,
fname=args.img_name,
local_path=args.local_path,
cache_path=args.cache_path,
web=args.web,
fixed=args.fixed
)
except:
if args.web:
print(json.dumps({'error':'error'}))
else:
raise
if __name__ == "__main__":
main()