Completed
Pull Request — master (#501)
by
unknown
01:01
created

plot_vad()   B

Complexity

Conditions 4

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 4
c 4
b 1
f 1
dl 0
loc 59
rs 8.9846

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# Copyright (c) 2008-2015 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
'''Make VAD plot from NEXRAD Level 3 VAD files'''
5
6
import datetime as dt
7
from io import StringIO
8
9
import matplotlib as mpl
10
import matplotlib.pyplot as plt
11
import numpy as np
12
import pandas as pd
13
14
from metpy.io import Level3File
15
16
17
def plot_vad(filelist, storm_motion=None):
18
    '''INPUT: list of NEXRAD VAD files
19
       OUTPUT: color-coded wind barbs plotted with height for
20
               the times given in the list of files
21
22
        plotting a storm-relative VAD is optional
23
        (requires a storm motion vector)'''
24
    vad_pd = pd.DataFrame()
25
    for files in filelist:
26
        vad_f = Level3File(files)
27
        vad_ft = vad_f.tab_pages
28
        lst = np.arange(0, len(vad_ft)-2)
29
        vad_list = [pd.read_table(StringIO(vad_ft[i]), sep='\s+', header=1,
30
                                           skiprows=[2]) for i in lst]
31
        vad = pd.concat(vad_list, axis=0)
32
        vad['TIME'] = vad_f.metadata['vol_time']
33
        # convert from altitude in feet to AGL in meters
34
        vad['AGL'] = ((vad['ALT']*100.)-vad_f.height)*0.3048
35
        vad_pd = pd.concat([vad_pd, vad], axis=0)
36
37
    # Begin Figure
38
    fig = plt.figure(figsize=(12, 10))
39
    ax = fig.add_subplot(111)
40
41
    uplot = vad_pd['U']
42
    vplot = vad_pd['V']
43
    time_plot = mpl.dates.date2num(vad_pd['TIME'].astype(dt.datetime))
44
45
    # Subtract storm motion vector from winds for storm-relative VAD
46
    if storm_motion is not None:
47
        uplot, vplot = vad_pd['U']-storm_motion[0], vad_pd['V']-storm_motion[1]
48
49
    # Set color params
50
    c = np.sqrt(uplot**2 + vplot**2)  # color by speed
51
    bounds = np.arange(0, 100, 1)  # min speed, max speed, interval
52
53
    # plot wind barbs
54
    cmap = plt.cm.gnuplot
55
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
56
    m = 2  # plot every mth wind barb
57
    b = plt.barbs(time_plot[::m], vad_pd['AGL'].iloc[::m], uplot.iloc[::m],
58
                  vplot.iloc[::m], c[::m], cmap=cmap, norm=norm)
59
60
    # Assign tick labels for x-axis
61
    starttime, endtime = vad_pd['TIME'].iloc[0], vad_pd['TIME'].iloc[-1]
62
    ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('%d/%H:%M UTC'))
63
    ax.set_xlim(starttime-dt.timedelta(minutes=2), endtime+dt.timedelta(minutes=2))
64
65
    # Plot asthetics
66
    fs = 14
67
    cbar = plt.colorbar(b, cmap=cmap, boundaries=bounds, norm=norm)  # Colorbar
68
    cbar.set_label('Wind Speed (m/s)', fontsize=fs)
69
    yticks = np.arange(0, 10500, 500)
70
    plt.yticks(yticks)
71
    plt.ylim(-500, 10500)
72
    plt.xlabel('Time (UTC)', fontsize=fs)
73
    plt.ylabel('Altitude AGL (m)', fontsize=fs)
74
    plt.grid('on')
75
    plt.title(vad_f.siteID+' Velocity Azimuth Display', fontsize=fs+3)
76