def plotObservationsComposite(obs, map, title, file_name): pylab.clf() colors = [ 'r', 'g', 'b', 'c', 'm', '#660099', '#ff9900', '#006666' ] ob_ids = np.unique1d(obs['id']) ttu_label = False for ob_id in ob_ids: ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) if ob_id[0] == "P": ob_num = int(ob_id[1]) - 1 pylab.plot(ob_xs, ob_ys, 'o', mfc=colors[ob_num], mec=colors[ob_num], ms=3, label="NSSL MM (%s)" % ob_id) else: if not ttu_label: label = "TTU Sticknet" ttu_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'ko', ms=3, label=label) drawPolitical(map, scale_len=5) pylab.legend(loc=3, numpoints=1, prop={'size':'medium'}) pylab.title(title) pylab.savefig(file_name) return
def plotObservations(obs, map, title, file_name, refl=None): pylab.clf() if refl is not None: nx, ny = refl.shape xs, ys = np.meshgrid(np.arange(nx) * 1000, np.arange(ny) * 1000) map.contour(xs, ys, refl, levels=np.arange(20, 80, 20), colors='k') obs_x, obs_y = map(obs['longitude'], obs['latitude']) for ob, x, y in zip(obs, obs_x, obs_y): pylab.plot(x, y, 'ko', ms=3.) pylab.text(x - 500, y + 500, "%4.1f" % ob['temp'], size='small', va='bottom', ha='right', color='r') pylab.text(x - 500, y - 500, "%4.1f" % ob['dewp'], size='small', va='top', ha='right', color='g') pylab.text(x + 500, y - 500, ob['id'], size='small', va='top', ha='left', color='#808080') good_idxs = np.where((obs['wind_spd'] >= 0.) & (obs['wind_dir'] >= 0.)) u = -obs['wind_spd'] * np.sin(obs['wind_dir'] * np.pi / 180) * 1.94 v = -obs['wind_spd'] * np.cos(obs['wind_dir'] * np.pi / 180) * 1.94 u_rot, v_rot = map.rotate_vector(u, v, obs['longitude'], obs['latitude']) pylab.barbs(obs_x[good_idxs], obs_y[good_idxs], u_rot[good_idxs], v_rot[good_idxs]) drawPolitical(map, scale_len=5) pylab.title(title) pylab.savefig(file_name) return
def plotTiming(vortex_prob, vortex_times, times, map, grid_spacing, tornado_track, title, file_name, obs=None, centers=None, min_prob=0.1): nx, ny = vortex_prob.shape gs_x, gs_y = grid_spacing xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) time_color_map = matplotlib.cm.Accent time_color_map.set_under('#ffffff') vortex_times = np.where(vortex_prob >= min_prob, vortex_times, -1) track_xs, track_ys = map(*reversed(tornado_track)) pylab.figure(figsize=(10, 8)) pylab.axes((0.025, 0.025, 0.95, 0.925)) pylab.pcolormesh(xs, ys, vortex_times, cmap=time_color_map, vmin=times.min(), vmax=times.max()) tick_labels = [ (datetime(2009, 6, 5, 18, 0, 0) + timedelta(seconds=int(t))).strftime("%H%M") for t in times ] bar = pylab.colorbar()#orientation='horizontal', aspect=40) bar.locator = FixedLocator(times) bar.formatter = FixedFormatter(tick_labels) bar.update_ticks() pylab.plot(track_xs, track_ys, 'mv-', lw=2.5, mfc='k', ms=8) drawPolitical(map, scale_len=(xs[-1, -1] - xs[0, 0]) / 10.) pylab.title(title) pylab.savefig(file_name) pylab.close()
def plotRadTilt(plot_data, plot_cmaps, plot_titles, grid, file_name, base_ref=None): subplot_base = 220 n_plots = 4 xs, ys, gs_x, gs_y, map = grid pylab.figure(figsize=(12,12)) pylab.subplots_adjust(left=0.02, right=0.98, top=0.98, bottom=0.02, wspace=0.04) for plot in range(n_plots): pylab.subplot(subplot_base + plot + 1) cmap, vmin, vmax = plot_cmaps[plot] cmap.set_under("#ffffff", alpha=0.0) pylab.pcolormesh(xs - gs_x / 2, ys - gs_y / 2, plot_data[plot] >= -90, vmin=0, vmax=1, cmap=mask_cmap) pylab.pcolormesh(xs - gs_x / 2, ys - gs_y / 2, plot_data[plot], vmin=vmin, vmax=vmax, cmap=cmap) pylab.colorbar() if base_ref is not None: pylab.contour(xs, ys, base_ref, levels=[10.], colors='k', lw=0.5) # if plot == 0: # pylab.contour(xs, ys, refl_88D[:, :, 0], levels=np.arange(10, 80, 10), colors='#808080', lw=0.5) # pylab.plot(radar_x, radar_y, 'ko') pylab.title(plot_titles[plot]) drawPolitical(map) pylab.savefig(file_name) pylab.close() return
def plotObservationsComposite(obs, map, scale_len, title, file_name): pylab.figure(figsize=(10,8)) pylab.axes((0, 0, 1, 0.95)) colors = [ 'r', 'g', 'b', 'c', 'm', '#660099', '#ff9900', '#006666' ] ob_ids = np.unique1d(obs['id']) ttu_label = False asos_label = False sndg_label = False for ob_id in ob_ids: ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) if ob_id[0] == "P": ob_num = int(ob_id[1]) - 1 pylab.plot(ob_xs, ob_ys, 'o', mfc=colors[ob_num], mec=colors[ob_num], ms=3, label="NSSL MM (%s)" % ob_id) elif ob_id[0] == "K": if not asos_label: label = "ASOS" asos_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'k*', ms=5, label=label) elif ob_id[0] == "1" or ob_id[0] == "2": if not ttu_label: label = "TTU Sticknet" ttu_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'o', mfc="#999999", mec="#999999", ms=3, label=label) elif these_obs[0]['obtype'] == "SNDG": if not sndg_label: label = "Sounding" sndg_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'k^', ms=4, label=label) drawPolitical(map, scale_len=scale_len) line_objects = [ l for l in sorted(pylab.gca().lines, key=lambda x: x.get_label()) if l.get_label()[0] != "_" ] pylab.legend(line_objects, [ l.get_label() for l in line_objects], loc=2, numpoints=1, prop={'size':'medium'}) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def makeplot(refl, winds, w, stride, map, gs, title, file_name, box=None): pylab.figure() axmain = pylab.axes((0, 0.025, 1, 0.9)) gs_x, gs_y = gs nx, ny = refl.shape xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) pylab.contourf(xs, ys, refl, levels=np.arange(10, 80, 10)) pylab.colorbar() pylab.contour(xs, ys, w, levels=np.arange(-10, 0, 2), colors='#666666', style='--') pylab.contour(xs, ys, w, levels=np.arange(2, 12, 2), colors='#666666', style='-') u, v = winds wind_slice = tuple([ slice(None, None, stride) ] * 2) pylab.quiver(xs[wind_slice], ys[wind_slice], u[wind_slice], v[wind_slice]) if box: lb_y, lb_x = [ b.start for b in box ] ub_y, ub_x = [ b.stop for b in box ] box_xs = gs_x * np.array([ lb_x, lb_x, ub_x, ub_x, lb_x]) box_ys = gs_y * np.array([ lb_y, ub_y, ub_y, lb_y, lb_y]) map.plot(box_xs, box_ys, '#660099') axins = zoomed_inset_axes(pylab.gca(), 4, loc=4) pylab.sca(axins) pylab.contourf(xs[box], ys[box], refl[box], levels=np.arange(10, 80, 10)) pylab.contour(xs[box], ys[box], w[box], levels=np.arange(-10, 0, 2), colors='#666666', style='--') pylab.contour(xs[box], ys[box], w[box], levels=np.arange(2, 12, 2), colors='#666666', style='-') pylab.quiver(xs[box], ys[box], u[box], v[box]) drawPolitical(map) pylab.xlim([lb_x * gs_x, ub_x * gs_x - 1]) pylab.ylim([lb_y * gs_y, ub_y * gs_y - 1]) mark_inset(axmain, axins, loc1=1, loc2=3, fc='none', ec='k') pylab.sca(axmain) drawPolitical(map) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotProbability(vortex_prob, map, lower_bound, grid_spacing, tornado_track, title, file_name, obs=None, centers=None, min_prob=0.1, objects=None): nx, ny = vortex_prob.shape gs_x, gs_y = grid_spacing lb_x, lb_y = lower_bound # print lb_x, lb_y xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) prob_color_map = matplotlib.cm.RdYlBu_r prob_color_map.set_under('#ffffff') track_xs, track_ys = map(*reversed(tornado_track)) pylab.figure(figsize=(10, 8)) pylab.axes((0.025, 0.025, 0.95, 0.925)) # Plot Vortex Probability pylab.pcolormesh(xs, ys, vortex_prob, cmap=prob_color_map, vmin=min_prob, vmax=1.0) pylab.colorbar()#orientation='horizontal', aspect=40) pylab.plot(track_xs, track_ys, 'mv-', lw=2.5, mfc='k', ms=8) if centers: lines_x, lines_y = reorganizeCenters(centers, range(14400, 18300, 300)) for line_x, line_y in zip(lines_x, lines_y): pylab.plot(line_x - lb_y, line_y - lb_x, 'ko-', markersize=2, linewidth=1) if obs is not None: pylab.contour(x, y, obs, levels=[0.95], colors='k') if objects: for obj in objects: obj_t, obj_y, obj_x = removeNones(obj, (13,) + vortex_prob.shape) if findSize(obj) >= 8: pylab.plot(1000 * np.array([obj_x.start, obj_x.start, obj_x.stop, obj_x.stop, obj_x.start]), 1000 * np.array([obj_y.start, obj_y.stop, obj_y.stop, obj_y.start, obj_y.start]), color='k', zorder=10) drawPolitical(map, scale_len=0) # scale_len=(xs[-1, -1] - xs[0, 0]) / 10000.) pylab.title(title) pylab.savefig(file_name) pylab.close() return
def plotVorticity(vorticity, winds, map, title, file_name, stride=4): u, v = winds pylab.figure() thin_data = tuple([ slice(None, None, stride) ] * 2) gs_x, gs_y = goshen_1km_gs nx, ny = vorticity.shape xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) pylab.contourf(xs, ys, vorticity, levels=np.arange(0.0075, 0.034, 0.0025)) pylab.colorbar() pylab.quiver(xs[thin_data], ys[thin_data], u[thin_data], v[thin_data]) drawPolitical(map) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotVorticity(vorticity, winds, map, title, file_name, stride=4): u, v = winds pylab.figure() thin_data = tuple([slice(None, None, stride)] * 2) gs_x, gs_y = goshen_1km_gs nx, ny = vorticity.shape xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) pylab.contourf(xs, ys, vorticity, levels=np.arange(0.0075, 0.034, 0.0025)) pylab.colorbar() pylab.quiver(xs[thin_data], ys[thin_data], u[thin_data], v[thin_data]) drawPolitical(map) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotObservationsComposite(obs, map, title, file_name): pylab.clf() colors = ['r', 'g', 'b', 'c', 'm', '#660099', '#ff9900', '#006666'] ob_ids = np.unique1d(obs['id']) ttu_label = False for ob_id in ob_ids: ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) if ob_id[0] == "P": ob_num = int(ob_id[1]) - 1 pylab.plot(ob_xs, ob_ys, 'o', mfc=colors[ob_num], mec=colors[ob_num], ms=3, label="NSSL MM (%s)" % ob_id) else: if not ttu_label: label = "TTU Sticknet" ttu_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'ko', ms=3, label=label) drawPolitical(map, scale_len=5) pylab.legend(loc=3, numpoints=1, prop={'size': 'medium'}) pylab.title(title) pylab.savefig(file_name) return
def main(): base_time = datetime(2009, 6, 5, 18, 0, 0) epoch = datetime(1970, 1, 1, 0, 0, 0) base_epoch = (base_time - epoch).total_seconds() times_seconds = range(14700, 18300, 300) times = [ base_time + timedelta(seconds=t) for t in times_seconds ] bounds = (slice(100, 180), slice(90, 170)) rev_bounds = [ 0 ] rev_bounds.extend(bounds[::-1]) rev_bounds = tuple(rev_bounds) proj = setupMapProjection(goshen_1km_proj, goshen_1km_gs, bounds=bounds) map = Basemap(**proj) obs_file_names = ['psu_straka_mesonet.pkl', 'ttu_sticknet.pkl', 'asos.pkl', 'soundings_clip.pkl'] all_obs = loadObs(obs_file_names, times, map, (goshen_1km_proj['width'], goshen_1km_proj['height']), sounding_obs=['soundings_clip.pkl']) refl_base = "hdf/KCYS/1km/goshen.hdfrefl2d" refl_times = np.array([ int(f[-6:]) for f in glob.glob("%s??????" % refl_base) ]) refl_keep_times = [] refl_data = {} for tt in times_seconds: idx = np.argmin(np.abs(refl_times - tt)) if refl_times[idx] > tt and idx > 0: idx -= 1 file_name = "%s%06d" % (refl_base, refl_times[idx]) hdf = nio.open_file(file_name, mode='r', format='hdf') refl_keep_times.append(refl_times[idx]) refl_data[tt] = hdf.variables['refl2d'][rev_bounds] for time, reg in inflow_stations.iteritems(): pylab.figure() gs_x, gs_y = goshen_1km_gs nx, ny = refl_data[time].shape xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) pylab.contourf(xs, ys, refl_data[time], levels=np.arange(10, 80, 10)) for region, stations in reg.iteritems(): if region != 'sounding': for station in stations: idxs = np.where((all_obs['id'] == station) & (all_obs['time'] == base_epoch + time)) ob_xs, ob_ys = map(all_obs['longitude'][idxs], all_obs['latitude'][idxs]) if region == 'inflow': color='r' elif region == 'outflow': color='b' wdir = all_obs['wind_dir'][idxs] wspd = all_obs['wind_spd'][idxs] u = -wspd * np.sin(wdir * np.pi / 180.) * 1.94 v = -wspd * np.cos(wdir * np.pi / 180.) * 1.94 pylab.plot(ob_xs, ob_ys, "%so" % color) pylab.barbs(ob_xs, ob_ys, u, v) drawPolitical(map, scale_len=10) pylab.savefig("inflow_stations_%06d.png" % time) pylab.close() return
def makeplot(refl, winds, w, stride, map, gs, title, file_name, box=None): pylab.figure() axmain = pylab.axes((0, 0.025, 1, 0.9)) gs_x, gs_y = gs nx, ny = refl.shape xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) pylab.contourf(xs, ys, refl, levels=np.arange(10, 80, 10)) pylab.colorbar() pylab.contour(xs, ys, w, levels=np.arange(-10, 0, 2), colors='#666666', style='--') pylab.contour(xs, ys, w, levels=np.arange(2, 12, 2), colors='#666666', style='-') u, v = winds wind_slice = tuple([slice(None, None, stride)] * 2) pylab.quiver(xs[wind_slice], ys[wind_slice], u[wind_slice], v[wind_slice]) if box: lb_y, lb_x = [b.start for b in box] ub_y, ub_x = [b.stop for b in box] box_xs = gs_x * np.array([lb_x, lb_x, ub_x, ub_x, lb_x]) box_ys = gs_y * np.array([lb_y, ub_y, ub_y, lb_y, lb_y]) map.plot(box_xs, box_ys, '#660099') axins = zoomed_inset_axes(pylab.gca(), 4, loc=4) pylab.sca(axins) pylab.contourf(xs[box], ys[box], refl[box], levels=np.arange(10, 80, 10)) pylab.contour(xs[box], ys[box], w[box], levels=np.arange(-10, 0, 2), colors='#666666', style='--') pylab.contour(xs[box], ys[box], w[box], levels=np.arange(2, 12, 2), colors='#666666', style='-') pylab.quiver(xs[box], ys[box], u[box], v[box]) drawPolitical(map) pylab.xlim([lb_x * gs_x, ub_x * gs_x - 1]) pylab.ylim([lb_y * gs_y, ub_y * gs_y - 1]) mark_inset(axmain, axins, loc1=1, loc2=3, fc='none', ec='k') pylab.sca(axmain) drawPolitical(map) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotConfusion(confusion, map, grid_spacing, title, file_name, inset=None, fudge=16): pylab.figure() axmain = pylab.axes() gs_x, gs_y = grid_spacing confusion_cmap_dict = { 'red':( (0.0, 0.5, 0.5), (0.25, 0.5, 1.0), (0.5, 1.0, 0.0), (0.75, 0.0, 1.0), (1.0, 1.0, 1.0), ), 'green':( (0.0, 0.5, 0.5), (0.25, 0.5, 0.0), (0.5, 0.0, 1.0), (0.75, 1.0, 1.0), (1.0, 1.0, 1.0), ), 'blue':( (0.0, 0.5, 0.5), (0.25, 0.5, 1.0), (0.5, 1.0, 0.0), (0.75, 0.0, 1.0), (1.0, 1.0, 1.0), ), } confusion_cmap = LinearSegmentedColormap('confusion', confusion_cmap_dict, 256) nx, ny = confusion.shape xs, ys = np.meshgrid( gs_x * np.arange(nx), gs_y * np.arange(ny) ) pylab.pcolormesh(xs, ys, confusion, cmap=confusion_cmap, vmin=0, vmax=3) tick_locs = [ 0.375 + 0.75 * l for l in range(4) ] tick_labels = [ "Correct\nNegative", "False\nAlarm", "Miss", "Hit" ] bar = pylab.colorbar() bar.locator = FixedLocator(tick_locs) bar.formatter = FixedFormatter(tick_labels) pylab.setp(pylab.getp(bar.ax, 'ymajorticklabels'), fontsize='large') bar.update_ticks() drawPolitical(map, scale_len=25) if inset: lb_y, lb_x = [ b.start for b in inset ] ub_y, ub_x = [ b.stop + fudge for b in inset ] inset_exp = (slice(lb_y, ub_y), slice(lb_x, ub_x)) axins = zoomed_inset_axes(pylab.gca(), 2, loc=4) pylab.sca(axins) pylab.pcolormesh(xs[inset_exp], ys[inset_exp], confusion[inset_exp], cmap=confusion_cmap, vmin=0, vmax=3) drawPolitical(map) pylab.xlim([lb_x * gs_x, (ub_x - 1) * gs_x]) pylab.ylim([lb_y * gs_y, (ub_y - 1) * gs_y]) mark_inset(axmain, axins, loc1=1, loc2=3, fc='none', ec='k') pylab.sca(axmain) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotSoundingObservationsComposite(obs, map, scale_len, dimensions, title, file_name, radars=None): figure = pylab.figure(figsize=(10,8)) x_size, y_size = dimensions grid_spec = GridSpec(2, 2, width_ratios=[3, 1], height_ratios=[1, 3]) colors = [ 'r', 'g', 'b', 'c', 'm', '#660099', '#006666', '#99ff00', '#ff9900', '#666666' ] names = [ "Sounding (NSSL)", "Sounding (NCAR)", "Sounding (NCAR)", "Sounding (NCAR)", "Sounding (NSSL)", "Sounding (NSSL)" ] ob_ids = np.unique1d(obs['id']) bottom = 0.075 top = 0.95 stretch_fac = (1 - top + bottom) / 2. - 0.011 pylab.subplots_adjust(left=0.1 + stretch_fac, bottom=bottom, right=0.9 - stretch_fac, top=top, hspace=0.075, wspace=0.075) ax_xy = figure.add_subplot(grid_spec[2]) ax_xz = figure.add_subplot(grid_spec[0], sharex=ax_xy) ax_yz = figure.add_subplot(grid_spec[3], sharey=ax_xy) def labelMe(do_label, label): if do_label: return not do_label, label else: return do_label, None color_num = 0 prof_label = True sfc_label = True asos_label = True ttu_label = True for ob_id in ob_ids: plot_vertical = False ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) if np.any(ob_xs >= 0) and np.any(ob_xs <= x_size) and np.any(ob_ys >= 0) and np.any(ob_ys <= y_size): if these_obs['obtype'][0] == "PROF": prof_label, label = labelMe(prof_label, "Profiler") color = "#ff9900" marker = 'o' ms = 3 plot_vertical = True elif these_obs['obtype'][0] == "SNDG": color = colors[color_num] label = "Sounding"# % ob_id marker = 'o' ms = 3 plot_vertical = True color_num += 1 elif ob_id[0] == "P": color = "#999999" sfc_label, label = labelMe(sfc_label, "Sounding") marker = 'o' ms = 3 elif ob_id[0] == "K": asos_label, label = labelMe(asos_label, "ASOS") color = 'k' marker = '*' ms = 5 elif ob_id[0] == "1" or ob_id[0] == "2": ttu_label, label = labelMe(ttu_label, "TTU Sticknet") color = "#003300" marker = 'o' ms = 3 ax_xy.plot(ob_xs, ob_ys, marker, mfc=color, mec=color, ms=ms, label=label)[0] if plot_vertical: ax_xz.plot(ob_xs, these_obs['elevation'] / 1000., marker, mfc=color, mec=color, ms=ms) ax_yz.plot(these_obs['elevation'] / 1000., ob_ys, marker, mfc=color, mec=color, ms=ms) dummy, zlim = ax_xz.get_ylim() for ob_id in ob_ids: ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) plot_vertical = False if np.any(ob_xs >= 0) and np.any(ob_xs <= x_size) and np.any(ob_ys >= 0) and np.any(ob_ys <= y_size): if these_obs['obtype'][0] == "PROF": color = "#ff9900" plot_vertical = True elif these_obs['obtype'][0] == "SNDG": color = "#999999" plot_vertical = True if plot_vertical: ax_xy.plot(ob_xs[0], ob_ys[0], 'x', color=color, ms=6) ax_xz.plot([ob_xs[0], ob_xs[0]], [0, zlim], '--', color=color, lw=0.5) ax_yz.plot([0, zlim], [ob_ys[0], ob_ys[0]], '--', color=color, lw=0.5) if radars: rad_label = True for radar in radars: rad_label, label = labelMe(rad_label, "Radar") (lat, lon), range = radar radar_x, radar_y = map(lon, lat) ax_xy.plot(radar_x, radar_y, 'b<', ms=4, label=label) ax_xy.add_patch(Circle((radar_x, radar_y), range, fc='none', ec='k')) ax_xz.set_ylim(0, zlim) ax_yz.set_xlim(0, zlim) pylab.sca(ax_xy) drawPolitical(map, scale_len=scale_len) ax_xz.set_xlabel("x") ax_xz.set_ylabel("z (km)") ax_yz.set_xlabel("z (km)") ax_yz.set_ylabel("y", rotation=-90) line_objects = [ l for l in sorted(ax_xy.lines, key=lambda x: x.get_label()) if l.get_label()[0] != "_" ] ax_xy.legend(line_objects, [ l.get_label() for l in line_objects], loc=1, numpoints=1, ncol=2, prop={'size':'medium'}, bbox_to_anchor=(1.28, 1.07, 0.33, 0.33), handletextpad=0, columnspacing=0) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotConfusion(confusion, map, grid_spacing, title, file_name, inset=None, fudge=16): pylab.figure() axmain = pylab.axes() gs_x, gs_y = grid_spacing confusion_cmap_dict = { 'red': ( (0.0, 0.5, 0.5), (0.25, 0.5, 1.0), (0.5, 1.0, 0.0), (0.75, 0.0, 1.0), (1.0, 1.0, 1.0), ), 'green': ( (0.0, 0.5, 0.5), (0.25, 0.5, 0.0), (0.5, 0.0, 1.0), (0.75, 1.0, 1.0), (1.0, 1.0, 1.0), ), 'blue': ( (0.0, 0.5, 0.5), (0.25, 0.5, 1.0), (0.5, 1.0, 0.0), (0.75, 0.0, 1.0), (1.0, 1.0, 1.0), ), } confusion_cmap = LinearSegmentedColormap('confusion', confusion_cmap_dict, 256) nx, ny = confusion.shape xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) pylab.pcolormesh(xs, ys, confusion, cmap=confusion_cmap, vmin=0, vmax=3) tick_locs = [0.375 + 0.75 * l for l in range(4)] tick_labels = ["Correct\nNegative", "False\nAlarm", "Miss", "Hit"] bar = pylab.colorbar() bar.locator = FixedLocator(tick_locs) bar.formatter = FixedFormatter(tick_labels) pylab.setp(pylab.getp(bar.ax, 'ymajorticklabels'), fontsize='large') bar.update_ticks() drawPolitical(map, scale_len=25) if inset: lb_y, lb_x = [b.start for b in inset] ub_y, ub_x = [b.stop + fudge for b in inset] inset_exp = (slice(lb_y, ub_y), slice(lb_x, ub_x)) axins = zoomed_inset_axes(pylab.gca(), 2, loc=4) pylab.sca(axins) pylab.pcolormesh(xs[inset_exp], ys[inset_exp], confusion[inset_exp], cmap=confusion_cmap, vmin=0, vmax=3) drawPolitical(map) pylab.xlim([lb_x * gs_x, (ub_x - 1) * gs_x]) pylab.ylim([lb_y * gs_y, (ub_y - 1) * gs_y]) mark_inset(axmain, axins, loc1=1, loc2=3, fc='none', ec='k') pylab.sca(axmain) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotObservationsComposite(obs, map, scale_len, title, file_name): pylab.figure(figsize=(10, 8)) pylab.axes((0, 0, 1, 0.95)) colors = ['r', 'g', 'b', 'c', 'm', '#660099', '#ff9900', '#006666'] ob_ids = np.unique1d(obs['id']) ttu_label = False asos_label = False sndg_label = False for ob_id in ob_ids: ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) if ob_id[0] == "P": ob_num = int(ob_id[1]) - 1 pylab.plot(ob_xs, ob_ys, 'o', mfc=colors[ob_num], mec=colors[ob_num], ms=3, label="NSSL MM (%s)" % ob_id) elif ob_id[0] == "K": if not asos_label: label = "ASOS" asos_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'k*', ms=5, label=label) elif ob_id[0] == "1" or ob_id[0] == "2": if not ttu_label: label = "TTU Sticknet" ttu_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'o', mfc="#999999", mec="#999999", ms=3, label=label) elif these_obs[0]['obtype'] == "SNDG": if not sndg_label: label = "Sounding" sndg_label = True else: label = None pylab.plot(ob_xs[0], ob_ys[0], 'k^', ms=4, label=label) drawPolitical(map, scale_len=scale_len) line_objects = [ l for l in sorted(pylab.gca().lines, key=lambda x: x.get_label()) if l.get_label()[0] != "_" ] pylab.legend(line_objects, [l.get_label() for l in line_objects], loc=2, numpoints=1, prop={'size': 'medium'}) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plotSoundingObservationsComposite(obs, map, scale_len, dimensions, title, file_name, radars=None): figure = pylab.figure(figsize=(10, 8)) x_size, y_size = dimensions grid_spec = GridSpec(2, 2, width_ratios=[3, 1], height_ratios=[1, 3]) colors = [ 'r', 'g', 'b', 'c', 'm', '#660099', '#006666', '#99ff00', '#ff9900', '#666666' ] names = [ "Sounding (NSSL)", "Sounding (NCAR)", "Sounding (NCAR)", "Sounding (NCAR)", "Sounding (NSSL)", "Sounding (NSSL)" ] ob_ids = np.unique1d(obs['id']) bottom = 0.075 top = 0.95 stretch_fac = (1 - top + bottom) / 2. - 0.011 pylab.subplots_adjust(left=0.1 + stretch_fac, bottom=bottom, right=0.9 - stretch_fac, top=top, hspace=0.075, wspace=0.075) ax_xy = figure.add_subplot(grid_spec[2]) ax_xz = figure.add_subplot(grid_spec[0], sharex=ax_xy) ax_yz = figure.add_subplot(grid_spec[3], sharey=ax_xy) def labelMe(do_label, label): if do_label: return not do_label, label else: return do_label, None color_num = 0 prof_label = True sfc_label = True asos_label = True ttu_label = True for ob_id in ob_ids: plot_vertical = False ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) if np.any(ob_xs >= 0) and np.any(ob_xs <= x_size) and np.any( ob_ys >= 0) and np.any(ob_ys <= y_size): if these_obs['obtype'][0] == "PROF": prof_label, label = labelMe(prof_label, "Profiler") color = "#ff9900" marker = 'o' ms = 3 plot_vertical = True elif these_obs['obtype'][0] == "SNDG": color = colors[color_num] label = "Sounding" # % ob_id marker = 'o' ms = 3 plot_vertical = True color_num += 1 elif ob_id[0] == "P": color = "#999999" sfc_label, label = labelMe(sfc_label, "Sounding") marker = 'o' ms = 3 elif ob_id[0] == "K": asos_label, label = labelMe(asos_label, "ASOS") color = 'k' marker = '*' ms = 5 elif ob_id[0] == "1" or ob_id[0] == "2": ttu_label, label = labelMe(ttu_label, "TTU Sticknet") color = "#003300" marker = 'o' ms = 3 ax_xy.plot(ob_xs, ob_ys, marker, mfc=color, mec=color, ms=ms, label=label)[0] if plot_vertical: ax_xz.plot(ob_xs, these_obs['elevation'] / 1000., marker, mfc=color, mec=color, ms=ms) ax_yz.plot(these_obs['elevation'] / 1000., ob_ys, marker, mfc=color, mec=color, ms=ms) dummy, zlim = ax_xz.get_ylim() for ob_id in ob_ids: ob_idxs = np.where(obs['id'] == ob_id)[0] these_obs = obs[ob_idxs] ob_xs, ob_ys = map(these_obs['longitude'], these_obs['latitude']) plot_vertical = False if np.any(ob_xs >= 0) and np.any(ob_xs <= x_size) and np.any( ob_ys >= 0) and np.any(ob_ys <= y_size): if these_obs['obtype'][0] == "PROF": color = "#ff9900" plot_vertical = True elif these_obs['obtype'][0] == "SNDG": color = "#999999" plot_vertical = True if plot_vertical: ax_xy.plot(ob_xs[0], ob_ys[0], 'x', color=color, ms=6) ax_xz.plot([ob_xs[0], ob_xs[0]], [0, zlim], '--', color=color, lw=0.5) ax_yz.plot([0, zlim], [ob_ys[0], ob_ys[0]], '--', color=color, lw=0.5) if radars: rad_label = True for radar in radars: rad_label, label = labelMe(rad_label, "Radar") (lat, lon), range = radar radar_x, radar_y = map(lon, lat) ax_xy.plot(radar_x, radar_y, 'b<', ms=4, label=label) ax_xy.add_patch( Circle((radar_x, radar_y), range, fc='none', ec='k')) ax_xz.set_ylim(0, zlim) ax_yz.set_xlim(0, zlim) pylab.sca(ax_xy) drawPolitical(map, scale_len=scale_len) ax_xz.set_xlabel("x") ax_xz.set_ylabel("z (km)") ax_yz.set_xlabel("z (km)") ax_yz.set_ylabel("y", rotation=-90) line_objects = [ l for l in sorted(ax_xy.lines, key=lambda x: x.get_label()) if l.get_label()[0] != "_" ] ax_xy.legend(line_objects, [l.get_label() for l in line_objects], loc=1, numpoints=1, ncol=2, prop={'size': 'medium'}, bbox_to_anchor=(1.28, 1.07, 0.33, 0.33), handletextpad=0, columnspacing=0) pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def main(): base_time = datetime(2009, 6, 5, 18, 0, 0) epoch = datetime(1970, 1, 1, 0, 0, 0) base_epoch = (base_time - epoch).total_seconds() times_seconds = range(14700, 18300, 300) times = [base_time + timedelta(seconds=t) for t in times_seconds] bounds = (slice(100, 180), slice(90, 170)) rev_bounds = [0] rev_bounds.extend(bounds[::-1]) rev_bounds = tuple(rev_bounds) proj = setupMapProjection(goshen_1km_proj, goshen_1km_gs, bounds=bounds) map = Basemap(**proj) obs_file_names = [ 'psu_straka_mesonet.pkl', 'ttu_sticknet.pkl', 'asos.pkl', 'soundings_clip.pkl' ] all_obs = loadObs(obs_file_names, times, map, (goshen_1km_proj['width'], goshen_1km_proj['height']), sounding_obs=['soundings_clip.pkl']) refl_base = "hdf/KCYS/1km/goshen.hdfrefl2d" refl_times = np.array( [int(f[-6:]) for f in glob.glob("%s??????" % refl_base)]) refl_keep_times = [] refl_data = {} for tt in times_seconds: idx = np.argmin(np.abs(refl_times - tt)) if refl_times[idx] > tt and idx > 0: idx -= 1 file_name = "%s%06d" % (refl_base, refl_times[idx]) hdf = nio.open_file(file_name, mode='r', format='hdf') refl_keep_times.append(refl_times[idx]) refl_data[tt] = hdf.variables['refl2d'][rev_bounds] for time, reg in inflow_stations.iteritems(): pylab.figure() gs_x, gs_y = goshen_1km_gs nx, ny = refl_data[time].shape xs, ys = np.meshgrid(gs_x * np.arange(nx), gs_y * np.arange(ny)) pylab.contourf(xs, ys, refl_data[time], levels=np.arange(10, 80, 10)) for region, stations in reg.iteritems(): if region != 'sounding': for station in stations: idxs = np.where((all_obs['id'] == station) & (all_obs['time'] == base_epoch + time)) ob_xs, ob_ys = map(all_obs['longitude'][idxs], all_obs['latitude'][idxs]) if region == 'inflow': color = 'r' elif region == 'outflow': color = 'b' wdir = all_obs['wind_dir'][idxs] wspd = all_obs['wind_spd'][idxs] u = -wspd * np.sin(wdir * np.pi / 180.) * 1.94 v = -wspd * np.cos(wdir * np.pi / 180.) * 1.94 pylab.plot(ob_xs, ob_ys, "%so" % color) pylab.barbs(ob_xs, ob_ys, u, v) drawPolitical(map, scale_len=10) pylab.savefig("inflow_stations_%06d.png" % time) pylab.close() return
def plot_map(radar_data, grid_spacing, title, file_name, color_bar='refl', topo=None, aux_field=None, vectors=None, obs=None): # bounds = (slice(80, 160), slice(90, 170)) bounds = (slice(None), slice(None)) # bounds = (slice(242, 327), slice(199, 284)) pylab.figure() pylab.subplots_adjust(left=0.02, right=0.98, top=0.90, bottom=0.02) nx, ny = radar_data[bounds[::-1]].shape proj = setupMapProjection(goshen_1km_proj, goshen_1km_gs, bounds=bounds) map = Basemap(**proj) radar_x, radar_y = map([-104.299004], [41.561497]) if topo is not None: topo_data, topo_lats, topo_lons = topo topo_lats, topo_lons = np.meshgrid(topo_lats, topo_lons) topo_x, topo_y = map(topo_lons, topo_lats) map.contourf(topo_x, topo_y, topo_data, cmap=pylab.get_cmap('gray')) if color_bar == 'refl': levels = range(10, 85, 5) color_map = NWSRef #pylab.get_cmap('jet') elif color_bar == 'radv': levels = range(-35, 40, 5) color_map = pylab.get_cmap('RdBu') elif color_bar == 'pt': levels = range(296, 321, 2) color_map = pylab.get_cmap('jet') print nx, ny x, y = np.meshgrid(grid_spacing * np.arange(nx), grid_spacing * np.arange(ny)) map.contourf(x, y, radar_data[bounds[::-1]], levels=levels, cmap=color_map) # map.plot(radar_x, radar_y, 'ko') pylab.colorbar() if aux_field is not None: aux_levels, aux_data = aux_field CS = map.contour(x, y, aux_data[bounds[::-1]], levels=aux_levels, colors='k', lw=0.75) # pylab.clabel(CS, fmt='%.0f', inline_spacing=1, fontsize="12px") drawPolitical(map, scale_len=25) if obs is not None: for stid, ob in obs.iteritems(): potential_temperature = (5. / 9. * (ob['temperature'] - 32) + 273.15) * (29.5250192 / ob['pressure']) ** (2. / 7.) ob_x, ob_y = map(ob['Longitude'], ob['Latitude']) ob_ax_x, ob_ax_y = (pylab.gca().transData + pylab.gca().transAxes.inverted()).transform(np.array([ob_x, ob_y])) if ob_ax_x > 0 and ob_ax_x <= 1 and ob_ax_y > 0 and ob_ax_y <= 1: pylab.gca().add_patch(Circle((ob_x, ob_y), 4000, fc='none', ec='k')) pylab.text(ob_x, ob_y, "%5.1f" % potential_temperature, size='x-small', ha='right', va='bottom') if vectors is not None: stride = 4 u, v = vectors pylab.quiver(x[::stride, ::stride], y[::stride, ::stride], u[::stride, ::stride], v[::stride, ::stride]) pylab.title(title) pylab.savefig(file_name) return
def plot_map(radar_data, grid_spacing, title, file_name, color_bar='refl', topo=None, aux_field=None, vectors=None, obs=None): # bounds = (slice(80, 160), slice(90, 170)) bounds = (slice(None), slice(None)) # bounds = (slice(242, 327), slice(199, 284)) pylab.figure() pylab.subplots_adjust(left=0.02, right=0.98, top=0.90, bottom=0.02) nx, ny = radar_data[bounds[::-1]].shape proj = setupMapProjection(goshen_1km_proj, goshen_1km_gs, bounds=bounds) map = Basemap(**proj) radar_x, radar_y = map([-104.299004], [41.561497]) if topo is not None: topo_data, topo_lats, topo_lons = topo topo_lats, topo_lons = np.meshgrid(topo_lats, topo_lons) topo_x, topo_y = map(topo_lons, topo_lats) map.contourf(topo_x, topo_y, topo_data, cmap=pylab.get_cmap('gray')) if color_bar == 'refl': levels = range(10, 85, 5) color_map = NWSRef #pylab.get_cmap('jet') elif color_bar == 'radv': levels = range(-35, 40, 5) color_map = pylab.get_cmap('RdBu') elif color_bar == 'pt': levels = range(296, 321, 2) color_map = pylab.get_cmap('jet') print nx, ny x, y = np.meshgrid(grid_spacing * np.arange(nx), grid_spacing * np.arange(ny)) map.contourf(x, y, radar_data[bounds[::-1]], levels=levels, cmap=color_map) # map.plot(radar_x, radar_y, 'ko') pylab.colorbar() if aux_field is not None: aux_levels, aux_data = aux_field CS = map.contour(x, y, aux_data[bounds[::-1]], levels=aux_levels, colors='k', lw=0.75) # pylab.clabel(CS, fmt='%.0f', inline_spacing=1, fontsize="12px") drawPolitical(map, scale_len=25) if obs is not None: for stid, ob in obs.iteritems(): potential_temperature = (5. / 9. * (ob['temperature'] - 32) + 273.15) * (29.5250192 / ob['pressure'])**(2. / 7.) ob_x, ob_y = map(ob['Longitude'], ob['Latitude']) ob_ax_x, ob_ax_y = (pylab.gca().transData + pylab.gca().transAxes.inverted()).transform( np.array([ob_x, ob_y])) if ob_ax_x > 0 and ob_ax_x <= 1 and ob_ax_y > 0 and ob_ax_y <= 1: pylab.gca().add_patch( Circle((ob_x, ob_y), 4000, fc='none', ec='k')) pylab.text(ob_x, ob_y, "%5.1f" % potential_temperature, size='x-small', ha='right', va='bottom') if vectors is not None: stride = 4 u, v = vectors pylab.quiver(x[::stride, ::stride], y[::stride, ::stride], u[::stride, ::stride], v[::stride, ::stride]) pylab.title(title) pylab.savefig(file_name) return