| Conditions | 4 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | # Copyright (c) 2008-2015 MetPy Developers. |
||
| 16 | def plot_vad(filelist, storm_relative=False, storm_motion=[0., 0.]): |
||
| 17 | '''INPUT: list of NEXRAD VAD files |
||
| 18 | OUTPUT: color-coded wind barbs plotted with height for |
||
| 19 | the times given in the list of files |
||
| 20 | |||
| 21 | plotting a storm-relative VAD is optional |
||
| 22 | (requires a storm motion vector)''' |
||
| 23 | vad_pd = pd.DataFrame() |
||
| 24 | for files in filelist: |
||
| 25 | vad_f = Level3File(files) |
||
| 26 | vad_ft = vad_f.tab_pages |
||
| 27 | lst = np.arange(0, len(vad_ft)-2) |
||
| 28 | vad_list = [pd.read_table(StringIO(vad_ft[i]), sep='\s+', header=1, skiprows=[2]) for i in lst] |
||
| 29 | vad = pd.concat(vad_list, axis=0) |
||
| 30 | vad['TIME'] = vad_f.metadata['vol_time'] |
||
| 31 | # convert from altitude in feet to AGL in meters |
||
| 32 | vad['AGL'] = ((vad['ALT']*100.)-vad_f.height)*0.3048 |
||
| 33 | vad_pd = pd.concat([vad_pd, vad], axis=0) |
||
| 34 | |||
| 35 | # Begin Figure |
||
| 36 | fig = plt.figure(figsize=(12, 10)) |
||
| 37 | ax = fig.add_subplot(111) |
||
| 38 | |||
| 39 | uplot = vad_pd['U'] |
||
| 40 | vplot = vad_pd['V'] |
||
| 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: |
||
| 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], |
||
| 56 | vplot.iloc[::m], c[::m], cmap=cmap, norm=norm) |
||
| 57 | |||
| 58 | # Assign tick labels for x-axis |
||
| 59 | starttime, endtime = vad_pd['TIME'].iloc[0], vad_pd['TIME'].iloc[-1] |
||
| 60 | ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('%d/%H:%M UTC')) |
||
| 61 | ax.set_xlim(starttime-dt.timedelta(minutes=2), endtime+dt.timedelta(minutes=2)) |
||
| 62 | |||
| 63 | # Plot asthetics |
||
| 64 | fs = 14 |
||
| 65 | cbar = plt.colorbar(b, cmap=cmap, boundaries=bounds, norm=norm) # Colorbar |
||
| 66 | cbar.set_label('Wind Speed (m/s)', fontsize=fs) |
||
| 67 | yticks = np.arange(0, 10500, 500) |
||
| 68 | plt.yticks(yticks) |
||
| 69 | plt.ylim(-500, 10500) |
||
| 70 | plt.xlabel('Time (UTC)', fontsize=fs) |
||
| 71 | plt.ylabel('Altitude AGL (m)', fontsize=fs) |
||
| 72 | plt.grid('on') |
||
| 73 | plt.title(vad_f.siteID+' Velocity Azimuth Display', fontsize=fs+3) |
||
| 74 |