Completed
Pull Request — master (#501)
by
unknown
52s
created

plot_vad()   A

Complexity

Conditions 4

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 55
rs 9.078

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