def _pcolormesh(radar, field, sweep=0, cmap=None, norm=None, fig=None, ax=None): """ """ # Parse figure and axis if fig is None: fig = plt.gcf() if ax is None: ax = plt.gca() # Parse radar coordinates # Convert angles to radians and range to kilometers _range = radar.range["data"] / 1000.0 azimuth = np.radians(radar.get_azimuth(sweep)) # Compute radar sweep coordinates AZI, RNG = np.meshgrid(azimuth, _range, indexing="ij") X = RNG * np.sin(AZI) Y = RNG * np.cos(AZI) # Parse radar data data = radar.get_field(sweep, field) # Create quadmesh qm = ax.pcolormesh(X, Y, data, cmap=cmap, norm=norm, shading="flat", alpha=None, rasterized=True) # Create title s0, sf = radar.get_start_end(sweep) sweep_time = datetimes_from_radar(radar)[(s0 + sf) / 2] title = "{} {:.1f} deg\n{}Z\n{}".format( radar.metadata["instrument_name"], radar.fixed_angle["data"][sweep], sweep_time.isoformat(), field ) ax.set_title(title) return qm
def _parse_datastreams(radar, filename, version='-9999'): """ """ time = os.path.basename(filename).split('.')[0] date = datetimes_from_radar(radar).min().date().strftime('%Y%m%d') datastream = 'sgpcsaprsur{}.00 : {} : {}.{}'.format( FN, version, date, time) return datastream
def _pcolormesh( radar, field, sweep=0, cmap=None, norm=None, fig=None, ax=None): """ """ # Parse axis and figure parameters if fig is None: fig = plt.gcf() if ax is None: ax = plt.gca() # Parse radar coordinates # Convert angles to radians and range to kilometers _range = radar.range['data'] / 1000.0 azimuth = np.radians(radar.get_azimuth(sweep)) # Compute index of maximum range rf = np.abs(_range - MAX_RANGE).argmin() # Compute radar sweep coordinates AZI, RNG = np.meshgrid(azimuth, _range[:rf+1], indexing='ij') X = RNG * np.sin(AZI) Y = RNG * np.cos(AZI) # Parse radar data data = radar.get_field(sweep, field)[:,:rf+1] # Create quadmesh qm = ax.pcolormesh( X, Y, data, cmap=cmap, norm=norm, shading='flat', alpha=None, rasterized=True) # Create title s0, sf = radar.get_start_end(sweep) sweep_time = datetimes_from_radar(radar)[(s0 + sf) / 2] title = '{} {:.1f} deg\n{}Z\n{}'.format( radar.metadata['instrument_name'], radar.fixed_angle['data'][sweep], sweep_time.isoformat(), field) ax.set_title(title) return qm
def process_radar(radar, domain, weight, outdir, gatefilter=None, debug=False, verbose=False): """ """ if verbose: print('Processing file: {}'.format(os.path.basename(filename))) # Read radar data radar = read(filename, exclude_fields=EXCLUDE_FIELDS) # Create gatefilter from significant detection gf = GateFilter(radar) gf.exclude_below(SD_FIELD, 1, op='or', inclusive=False) if debug: print('Number of sweeps: {}'.format(radar.nsweeps)) # Grid radar data grid = grid_radar( radar, domain, weight=weight, fields=FIELDS, gatefilter=gf, toa=TOA, max_range=MAX_RANGE, gqi_field=None, legacy=True, debug=debug, verbose=verbose) # Add new metadata _add_metadata(grid, filename) # ARM file name protocols date_stamp = datetimes_from_radar(radar).min().strftime('%Y%m%d.%H%M%S') fname = 'nexradwsr88d{}{}.{}.{}.cdf'.format(QF, FN, DL, date_stamp) # Write MMCG NetCDF file grid_io.write_grid( os.path.join(outdir, fname), grid, format=FORMAT, write_proj_coord_sys=False, proj_coord_sys=None, arm_time_variables=True, write_point_x_y_z=False, write_point_lon_lat_alt=False) return
def multipanel(radar, outdir, dpi=90, debug=False, verbose=False): """ """ # Create figure instance subs = {'xlim': (-100, 100), 'ylim': (-100, 100)} fig, axes = plt.subplots(nrows=len(SWEEPS), ncols=9, subplot_kw=subs) fig.subplots_adjust(wspace=0.4, hspace=0.6) if debug: print 'Number of sweeps: {}'.format(radar.nsweeps) # Loop over specified sweeps for i, sweep in enumerate(SWEEPS): if verbose: print 'Plotting sweep index: {}'.format(sweep) # (a) Reflectivity qma = _pcolormesh( radar, REFL_FIELD, sweep=sweep, cmap=CMAP_REFL, norm=NORM_REFL, fig=fig, ax=axes[i,0]) # (b) Doppler velocity qmb = _pcolormesh( radar, VDOP_FIELD, sweep=sweep, cmap=CMAP_VDOP, norm=NORM_VDOP, fig=fig, ax=axes[i,1]) # (c) Corrected Doppler velocity qmc = _pcolormesh( radar, VDOP_CORR_FIELD, sweep=sweep, cmap=CMAP_VDOP, norm=NORM_VDOP, fig=fig, ax=axes[i,2]) # (d) Spectrum width qmd = _pcolormesh( radar, SPW_FIELD, sweep=sweep, cmap=CMAP_SPW, norm=NORM_SPW, fig=fig, ax=axes[i,3]) # (e) Copolar correlation coefficient qme = _pcolormesh( radar, RHOHV_FIELD, sweep=sweep, cmap=CMAP_RHOHV, norm=NORM_RHOHV, fig=fig, ax=axes[i,4]) # (f) Differential reflectivity qmf = _pcolormesh( radar, ZDR_FIELD, sweep=sweep, cmap=CMAP_ZDR, norm=NORM_ZDR, fig=fig, ax=axes[i,5]) # (g) Differential phase qmg = _pcolormesh( radar, PHIDP_FIELD, sweep=sweep, cmap=CMAP_PHIDP, norm=NORM_PHIDP, fig=fig, ax=axes[i,6]) # (h) Normalized coherent power qmh = _pcolormesh( radar, NCP_FIELD, sweep=sweep, cmap=CMAP_NCP, norm=NORM_NCP, fig=fig, ax=axes[i,7]) # (i) Radar significant detection qmi = _pcolormesh( radar, SD_FIELD, sweep=sweep, cmap=CMAP_NCP, norm=NORM_NCP, fig=fig, ax=axes[i,8]) # Create colour bars qm = [qma, qmb, qmc, qmd, qme, qmf, qmg, qmh, qmi] _create_colorbars(fig, axes, qm, TICKS) # Format plot axes for ax in axes.flat: ax.xaxis.set_major_locator(MultipleLocator(20)) ax.xaxis.set_minor_locator(MultipleLocator(10)) ax.yaxis.set_major_locator(MultipleLocator(20)) ax.yaxis.set_minor_locator(MultipleLocator(10)) ax.set_xlabel('Eastward Range (km)') ax.set_ylabel('Northward Range (km)') ax.grid(which='major') # Define image file name date_stamp = datetimes_from_radar(radar).min().strftime('%Y%m%d.%H%M%S') filename = '{}.png'.format(date_stamp) # Save figure fig.savefig(os.path.join(outdir, filename), format='png', dpi=dpi, bbox_inches='tight') # Close figure and axes to free memory plt.cla() plt.clf() plt.close(fig) return
def multipanel(radar, outdir, dpi=90, debug=False, verbose=False): """ """ # Create figure instance subs = {'xlim': (-100, 100), 'ylim': (-100, 100)} fig, axes = plt.subplots(nrows=len(SWEEPS), ncols=3, subplot_kw=subs) fig.subplots_adjust(wspace=0.6, hspace=0.6) if debug: print('Number of sweeps: {}'.format(radar.nsweeps)) # Loop over specified sweeps for i, sweep in enumerate(SWEEPS): if verbose: print('Plotting sweep index: {}'.format(sweep)) # (a) Reflectivity qma = _pcolormesh( radar, REFL_FIELD, sweep=sweep, cmap=CMAP_REFL, norm=NORM_REFL, fig=fig, ax=axes[i,0]) # (b) Doppler velocity qmb = _pcolormesh( radar, VDOP_FIELD, sweep=sweep+1, cmap=CMAP_VDOP, norm=NORM_VDOP, fig=fig, ax=axes[i,1]) # (c) Spectrum width qmc = _pcolormesh( radar, SPW_FIELD, sweep=sweep+1, cmap=CMAP_SPW, norm=NORM_SPW, fig=fig, ax=axes[i,2]) # Create color bars qm = [qma, qmb, qmc] _create_colorbars(fig, axes, qm, TICKS) # Format plot axes for ax in axes.flat: ax.xaxis.set_major_locator(MultipleLocator(50)) ax.xaxis.set_minor_locator(MultipleLocator(10)) ax.yaxis.set_major_locator(MultipleLocator(50)) ax.yaxis.set_minor_locator(MultipleLocator(10)) ax.set_xlabel('Eastward Range (km)') ax.set_ylabel('Northward Range (km)') ax.grid(which='major') # Define image file name date_stamp = datetimes_from_radar(radar).min().strftime('%Y%m%d.%H%M%S') fname = '{}.png'.format(date_stamp) # Save figure fig.savefig(os.path.join(outdir, fname), format='png', dpi=dpi, bbox_inches='tight', frameon=True, transparent=False) # Close figure to free memory plt.cla() plt.clf() plt.close(fig) return
def multipanel(radar, outdir, dpi=90, debug=False, verbose=False): """ """ # Create figure instance subs = {"xlim": (-117, 117), "ylim": (-117, 117)} fig, axes = plt.subplots(nrows=len(SWEEPS), ncols=7, subplot_kw=subs) fig.subplots_adjust(wspace=0.5, hspace=0.6) if debug: print("Number of sweeps: {}".format(radar.nsweeps)) # Loop over specified sweeps for i, sweep in enumerate(SWEEPS): if verbose: print("Plotting sweep index: {}".format(sweep)) # (a) Reflectivity qma = _pcolormesh(radar, REFL_FIELD, sweep=sweep, cmap=CMAP_REFL, norm=NORM_REFL, fig=fig, ax=axes[i, 0]) # (b) Doppler velocity qmb = _pcolormesh(radar, VDOP_FIELD, sweep=sweep, cmap=CMAP_VDOP, norm=NORM_VDOP, fig=fig, ax=axes[i, 1]) # (c) Spectrum width qmc = _pcolormesh(radar, SPW_FIELD, sweep=sweep, cmap=CMAP_SPW, norm=NORM_SPW, fig=fig, ax=axes[i, 2]) # (d) Copolar correlation coefficient qmd = _pcolormesh(radar, RHOHV_FIELD, sweep=sweep, cmap=CMAP_RHOHV, norm=NORM_RHOHV, fig=fig, ax=axes[i, 3]) # (e) Differential reflectivity qme = _pcolormesh(radar, ZDR_FIELD, sweep=sweep, cmap=CMAP_ZDR, norm=NORM_ZDR, fig=fig, ax=axes[i, 4]) # (f) Differential phase qmf = _pcolormesh(radar, PHIDP_FIELD, sweep=sweep, cmap=CMAP_PHIDP, norm=NORM_PHIDP, fig=fig, ax=axes[i, 5]) # (g) Normalized coherent power qmg = _pcolormesh(radar, NCP_FIELD, sweep=sweep, cmap=CMAP_NCP, norm=NORM_NCP, fig=fig, ax=axes[i, 6]) # Create color bars qm = [qma, qmb, qmc, qmd, qme, qmf, qmg] _create_colorbars(fig, axes, qm, TICKS) # Format plot axes for ax in axes.flat: ax.xaxis.set_major_locator(MultipleLocator(50)) ax.xaxis.set_minor_locator(MultipleLocator(10)) ax.yaxis.set_major_locator(MultipleLocator(50)) ax.yaxis.set_minor_locator(MultipleLocator(10)) ax.set_xlabel("Eastward Range (km)") ax.set_ylabel("Northward Range (km)") ax.grid(which="major") # Define image file name date_stamp = datetimes_from_radar(radar).min().strftime("%Y%m%d.%H%M%S") fname = "{}.png".format(date_stamp) # Save figure fig.savefig(os.path.join(outdir, fname), format="png", dpi=dpi, bbox_inches="tight") # Close figure to free memory plt.cla() plt.clf() plt.close(fig) return
def process_file(filename, outdir, debug=False, verbose=False): """ """ # Read radar data if USE_RADX: radar = read_radx(filename) else: radar = read(filename, exclude_fields=None) # Radar VCP check if CHECK_VCP: if NSWEEPS is not None and radar.nsweeps != NSWEEPS: return if verbose: print 'Processing file: {}'.format(os.path.basename(filename)) if debug: print 'Number of sweeps: {}'.format(radar.nsweeps) if USE_RADX: # Create file metadata object meta = FileMetadata( 'nexrad_archive', field_names=None, additional_metadata=None, file_field_names=False, exclude_fields=None) # Remove unnecessary fields for field in REMOVE_FIELDS: radar.fields.pop(field, None) # Rename fields to default Py-ART names for field in radar.fields.keys(): default_field = meta.get_field_name(field) radar.fields[default_field] = radar.fields.pop(field, None) # Step 1: Determine radar significant detection # Since NEXRAD WSR-88D Level II data is already processed to some degree, # this amounts to essentially removing salt and pepper noise gf = noise._significant_features( radar, REFL_FIELD, gatefilter=None, size_bins=SIZE_BINS, size_limits=SIZE_LIMITS, structure=STRUCTURE, remove_size_field=False, fill_value=None, size_field=None, debug=debug) gf = noise.significant_detection( radar, gatefilter=gf, remove_small_features=False, size_bins=SIZE_BINS, size_limits=SIZE_LIMITS, fill_holes=FILL_HOLES, dilate=DILATE, structure=STRUCTURE, iterations=1, rays_wrap_around=False, min_ncp=None, detect_field=None, debug=debug, verbose=verbose) # Step 2: Doppler velocity correction if DEALIAS == 'phase': vdop_corr = dealias_unwrap_phase( radar, gatefilter=gf, unwrap_unit='sweep', nyquist_vel=None, rays_wrap_around=True, keep_original=False, vel_field=None, corr_vel_field=VDOP_CORR_FIELD) elif DEALIAS == 'region': vdop_corr = dealias_region_based( radar, gatefilter=gf, interval_splits=INTERVAL_SPLITS, interval_limits=None, skip_between_rays=2, skip_along_ray=2, centered=True, nyquist_vel=None, rays_wrap_around=True, keep_original=False, vel_field=None, corr_vel_field=VDOP_CORR_FIELD) else: raise ValueError('Unsupported velocity correction routine') radar.add_field(VDOP_CORR_FIELD, vdop_corr, replace_existing=True) # Step 3: Reflectivity correction # Currently no correction procedures are applied to the reflectivity field # due to minimal attenuation at S-band refl_corr = radar.fields[REFL_FIELD].copy() radar.add_field(REFL_CORR_FIELD, refl_corr, replace_existing=True) # Step 4: Interpolate missing gates basic_fixes.interpolate_missing( radar, fields=FILL_FIELDS, interp_window=FILL_WINDOW, interp_sample=FILL_SAMPLE, kind='mean', rays_wrap_around=False, fill_value=None, debug=debug, verbose=verbose) # Add metadata _add_metadata(radar, filename) # ARM file name protocols date_stamp = datetimes_from_radar(radar).min().strftime('%Y%m%d.%H%M%S') fname = 'nexradwsr88d{}cmac{}.{}.{}.cdf'.format(QF, FN, DL, date_stamp) # Write CMAC NetCDF file write_cfradial(os.path.join(outdir, fname), radar, format=FORMAT, arm_time_variables=True) return
def process_file(filename, outdir, debug=False, verbose=False): """ """ if verbose: print 'Processing file: {}'.format(os.path.basename(filename)) # Read radar data radar = read_mdv(filename, exclude_fields=EXLUDE_FIELDS) if debug: print 'Number of sweeps: {}'.format(radar.nsweeps) # Step 1: Remove last 7 gates from each ray # These gates are reserved for signal testing only for field in radar.fields.keys(): if verbose: print 'Removing signal testing gates: {}'.format(field) radar.fields[field]['data'][:,-7:] = np.ma.masked # Step 2: Radar significant detection # Includes Doppler velocity coherency, Doppler velocity phasor coherency, # and significant echo boundary detection gf = noise.velocity_coherency( radar, gatefilter=None, text_bins=VDOP_TEXT_BINS, text_limits=VDOP_TEXT_LIMITS, texture_window=TEXTURE_WINDOW, texture_sample=TEXTURE_SAMPLE, max_texture=None, nyquist=None, rays_wrap_around=False, remove_small_features=False, fill_value=None, vdop_field=VDOP_FIELD, text_field=None, coherent_field=None, debug=debug, verbose=verbose) gf = noise.velocity_phasor_coherency( radar, gatefilter=gf, text_bins=PHASOR_TEXT_BINS, text_limits=PHASOR_TEXT_LIMITS, texture_window=TEXTURE_WINDOW, texture_sample=TEXTURE_SAMPLE, max_texture=None, rays_wrap_around=False, remove_small_features=False, fill_value=None, vdop_field=VDOP_FIELD, phasor_field=None, text_field=None, coherent_field=None, debug=debug, verbose=verbose) gf = noise.echo_boundaries( radar, gatefilter=gf, texture_window=TEXTURE_WINDOW, texture_sample=TEXTURE_SAMPLE, min_texture=None, bounds_percentile=BOUNDS_PERCENTILE, remove_small_features=False, rays_wrap_around=False, fill_value=None, sqi_field=NCP_FIELD, text_field=None, bounds_field=None, debug=debug, verbose=verbose) gf = noise.significant_detection( radar, gatefilter=gf, remove_small_features=True, size_bins=SIZE_BINS, size_limits=SIZE_LIMITS, fill_holes=FILL_HOLES, dilate=DILATE, structure=STRUCTURE, iterations=ITERATIONS, min_ncp=MIN_NCP, ncp_field=NCP_FIELD, detect_field=None, debug=debug, verbose=verbose) # Step 3: Compute radar texture fields texture_fields.add_textures( radar, fields=TEXTURE_FIELDS, gatefilter=None, texture_window=TEXTURE_WINDOW, texture_sample=TEXTURE_SAMPLE, min_sweep=None, max_sweep=None, min_range=None, max_range=None, min_ncp=None, rays_wrap_around=False, fill_value=None, ncp_field=NCP_FIELD) # Step 4: Doppler velocity correction if DEALIAS.upper() == 'REGION': vdop_corr = dealias_region_based( radar, gatefilter=gf, interval_splits=INTERVAL_SPLITS, interval_limits=None, skip_between_rays=2, skip_along_ray=2, centered=True, nyquist_vel=None, rays_wrap_around=True, keep_original=False, vel_field=VDOP_FIELD, corr_vel_field=CORR_VDOP_FIELD) else: raise ValueError('Unsupported velocity correction routine') radar.add_field(CORR_VDOP_FIELD, vdop_corr, replace_existing=False) # TODO # Step 5: Reflectivity correction refl_corr = radar.fields[REFL_FIELD].copy() radar.add_field(CORR_REFL_FIELD, refl_corr, replace_existing=False) # Step 6: Interpolate missing gates basic_fixes.interpolate_missing( radar, fields=FILL_FIELDS, interp_window=FILL_WINDOW, interp_sample=FILL_SAMPLE, kind='mean', rays_wrap_around=False, fill_value=None, debug=debug, verbose=verbose) # Step 7: Remove unwanted fields before writing for field in REMOVE_FIELDS: if verbose: print 'Removing radar field before writing: {}'.format(field) radar.fields.pop(field, None) # Parse metadata _add_metadata(radar, filename) # ARM file name protocols date = datetimes_from_radar(radar).min().strftime('%Y%m%d.%H%M%S') fname = 'sgpcsaprsurcmac{}.{}.{}.cdf'.format(FN, DL, date) # Write CMAC NetCDF file write_cfradial(os.path.join(outdir, fname), radar, format=FORMAT, arm_time_variables=True) return