| Conditions | 2 |
| Total Lines | 104 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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-2017 MetPy Developers. |
||
| 115 | def cross_section(isentlev, num, left_lat, left_lon, right_lat, right_lon): |
||
| 116 | """Plot an isentropic cross-section.""" |
||
| 117 | # get the coordinates of the endpoints for the cross-section |
||
| 118 | left_coord = np.array((float(left_lat), float(left_lon))) |
||
| 119 | right_coord = np.array((float(right_lat), float(right_lon))) |
||
| 120 | |||
| 121 | # Calculate data for the inset isentropic map |
||
| 122 | isent_anal = mcalc.isentropic_interpolation(float(isentlev) * units.kelvin, lev, tmp, |
||
| 123 | spech, tmpk_out=True) |
||
| 124 | isentprs = isent_anal[0] |
||
| 125 | isenttmp = isent_anal[1] |
||
| 126 | isentspech = isent_anal[2] |
||
| 127 | isentrh = mcalc.relative_humidity_from_specific_humidity(isentspech, isenttmp, isentprs) |
||
| 128 | |||
| 129 | # Find index values for the cross section slice |
||
| 130 | iright = lat_lon_2d_index(lat, lon, right_coord[0], right_coord[1]) |
||
| 131 | ileft = lat_lon_2d_index(lat, lon, left_coord[0], left_coord[1]) |
||
| 132 | |||
| 133 | # Get the cross-section slice data |
||
| 134 | cross_data = mcalc.extract_cross_section(ileft, iright, lat, lon, tmp, uwnd, vwnd, spech, |
||
| 135 | num=num) |
||
| 136 | cross_lat = cross_data[0] |
||
| 137 | cross_lon = cross_data[1] |
||
| 138 | cross_t = cross_data[2] |
||
| 139 | cross_u = cross_data[3] |
||
| 140 | cross_v = cross_data[4] |
||
| 141 | cross_spech = cross_data[5] |
||
| 142 | |||
| 143 | # Calculate theta and RH on the cross-section |
||
| 144 | cross_theta = mcalc.potential_temperature(lev[:, np.newaxis], cross_t) |
||
| 145 | cross_rh = mcalc.relative_humidity_from_specific_humidity(cross_spech, cross_t, |
||
| 146 | lev[:, np.newaxis]) |
||
| 147 | |||
| 148 | # Create figure for ploting |
||
| 149 | fig = plt.figure(1, figsize=(17., 12.)) |
||
| 150 | |||
| 151 | # Plot the cross section |
||
| 152 | ax1 = plt.axes() |
||
| 153 | ax1.set_yscale('symlog') |
||
| 154 | ax1.grid() |
||
| 155 | cint = np.arange(250, 450, 5) |
||
| 156 | |||
| 157 | # Determine whether to label x-axis with lat or lon values |
||
| 158 | if np.abs(left_lon - right_lon) > np.abs(left_lat - right_lat): |
||
| 159 | cs = ax1.contour(cross_lon, lev[::-1], cross_theta[::-1, :], cint, colors='tab:red') |
||
| 160 | cf = ax1.contourf(cross_lon, lev[::-1], cross_rh[::-1, :], range(10, 106, 5), |
||
| 161 | cmap=plt.cm.gist_earth_r) |
||
| 162 | ax1.barbs(cross_lon[4::4], lev, cross_u[:, 4::4], cross_v[:, 4::4], length=6) |
||
| 163 | plt.xlabel('Longitude (Degrees East)') |
||
| 164 | else: |
||
| 165 | cs = ax1.contour(cross_lat[::-1], lev[::-1], cross_theta[::-1, ::-1], cint, |
||
| 166 | colors='tab:red') |
||
| 167 | cf = ax1.contourf(cross_lat[::-1], lev[::-1], cross_rh[::-1, ::-1], range(10, 106, 5), |
||
| 168 | cmap=plt.cm.gist_earth_r) |
||
| 169 | ax1.barbs(cross_lat[::-4], lev, cross_u[:, ::-4], cross_v[:, ::-4], length=6) |
||
| 170 | plt.xlim(cross_lat[0], cross_lat[-1]) |
||
| 171 | plt.xlabel('Latitude (Degrees North)') |
||
| 172 | |||
| 173 | # Label the cross section axes |
||
| 174 | plt.clabel(cs, fontsize=10, inline=1, inline_spacing=7, |
||
| 175 | fmt='%i', rightside_up=True, use_clabeltext=True) |
||
| 176 | cb = plt.colorbar(cf, orientation='horizontal', extend=max, aspect=65, shrink=0.75, |
||
| 177 | pad=0.06, extendrect='True') |
||
| 178 | cb.set_label('Relative Humidity', size='x-large') |
||
| 179 | plt.ylabel('Pressure (hPa)') |
||
| 180 | ax1.set_yticklabels(np.arange(1000, 50, -50)) |
||
| 181 | plt.ylim(lev[0], lev[-1]) |
||
| 182 | plt.yticks(np.arange(1000, 50, -50)) |
||
| 183 | |||
| 184 | # Add a title |
||
| 185 | plt.title(('NARR Isentropic Cross-Section: ' + str(left_coord[0]) + ' N, ' + |
||
| 186 | str(left_coord[1]) + ' E to ' + str(right_coord[0]) + ' N, ' + |
||
| 187 | str(right_coord[1]) + ' E'), loc='left') |
||
| 188 | plt.title('VALID: {:s}'.format(str(vtimes[0])), loc='right') |
||
| 189 | |||
| 190 | # Add Inset Map |
||
| 191 | ax2 = fig.add_axes([0.125, 0.643, 0.25, 0.25], projection=crs) |
||
| 192 | |||
| 193 | # Coordinates to limit map area |
||
| 194 | bounds = [(-122., -75., 25., 50.)] |
||
| 195 | |||
| 196 | # Limit extent of inset map |
||
| 197 | ax2.set_extent(*bounds, crs=ccrs.PlateCarree()) |
||
| 198 | ax2.coastlines('50m', edgecolor='black', linewidth=0.75) |
||
| 199 | ax2.add_feature(states_provinces, edgecolor='black', linewidth=0.5) |
||
| 200 | |||
| 201 | # Plot the surface |
||
| 202 | clevisent = np.arange(0, 1000, 25) |
||
| 203 | cs = ax2.contour(tlons, tlats, isentprs[0, :, :], clevisent, |
||
| 204 | colors='k', linewidths=1.0, linestyles='solid') |
||
| 205 | plt.clabel(cs, fontsize=10, inline=1, inline_spacing=7, |
||
| 206 | fmt='%i', rightside_up=True, use_clabeltext=True) |
||
| 207 | |||
| 208 | # Plot RH |
||
| 209 | cf = ax2.contourf(tlons, tlats, isentrh[0, :, :], range(10, 106, 5), |
||
| 210 | cmap=plt.cm.gist_earth_r) |
||
| 211 | |||
| 212 | # Convert endpoints of cross-section line |
||
| 213 | left = crs.transform_point(left_coord[1], left_coord[0], ccrs.PlateCarree()) |
||
| 214 | right = crs.transform_point(right_coord[1], right_coord[0], ccrs.PlateCarree()) |
||
| 215 | |||
| 216 | # Plot the cross section line |
||
| 217 | plt.plot([left[0], right[0]], [left[1], right[1]], color='r') |
||
| 218 | plt.show() |
||
| 219 | |||
| 223 |