def test_level3_pathlib(): """Test that reading with Level3File properly sets the filename from a Path.""" fname = Path( get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False)) f = Level3File(fname) assert f.filename == str(fname)
def main(argv=None): model = Model() while True: line = input() try: inf, outf = line.split(',') img = np.array(Level3File(inf).sym_block[0][0]['data'], dtype='float32') h, w = img.shape nw = FLAGS.img_width nh = h * nw // w img = cv2.resize(img, (nh, nw), interpolation=cv2.INTER_AREA) img = img[np.newaxis, np.newaxis, :, :, np.newaxis] img = preprocess.reshape_patch(img, FLAGS.patch_size) pred = model.inference(img) pred = preprocess.reshape_patch_back(pred[:, np.newaxis, :], FLAGS.patch_size) pred = cv2.resize(pred[0, 0, :, :, 0], (h, w), interpolation=cv2.INTER_CUBIC) imsave(outf, pred, metadata={'axis': 'YX'}) print('done') except Exception as e: print('failed:', e)
def test_tracks(): """Check that tracks are properly decoded.""" f = Level3File(get_test_data('nids/KOUN_SDUS34_NSTTLX_201305202016')) for data in f.sym_block[0]: if 'track' in data: x, y = np.array(data['track']).T assert len(x) assert len(y)
def task(worker_id): nonlocal block, workers, filepaths for i in range(worker_id, len(block), workers): img = np.array(Level3File(filepaths[i]).sym_block[0][0]['data'], dtype='uint8') block[i, :] = img if (i - worker_id) % 100 == 0: print("worker %d: finished %f" % (worker_id, i / len(block)))
def test_level3_files(fname): """Test opening a NEXRAD NIDS file.""" f = Level3File(fname) # If we have some raster data in the symbology block, feed it into the mapper to make # sure it's working properly (Checks for #253) if hasattr(f, 'sym_block'): block = f.sym_block[0][0] if 'data' in block: f.map_data(block['data'])
def test_power_removed_control(): """Test decoding new PRC product.""" f = Level3File(get_test_data('nids/KGJX_NXF_20200817_0600.nids')) assert f.prod_desc.prod_code == 113 assert f.metadata['rpg_cut_num'] == 1 assert f.metadata['cmd_generated'] == 0 assert f.metadata['el_angle'] == -0.2 assert f.metadata['clutter_filter_map_dt'] == datetime(2020, 8, 17, 4, 16) assert f.metadata['compression'] == 1 assert f.sym_block[0][0]
def test_bad_length(caplog): """Test reading a product with too many bytes produces a log message.""" fname = get_test_data('nids/KOUN_SDUS84_DAATLX_201305202016', as_file_obj=False) with open(fname, 'rb') as inf: data = inf.read() fobj = BytesIO(data + data) with caplog.at_level(logging.WARNING, 'metpy.io.nexrad'): Level3File(fobj) assert len(caplog.records) == 1 assert 'This product may not parse correctly' in caplog.records[0].message
def test_vector_packet(): """Check that vector packets are properly decoded.""" f = Level3File(get_test_data('nids/KOUN_SDUS64_NHITLX_201305202016')) for page in f.graph_pages: for item in page: if 'vectors' in item: x1, x2, y1, y2 = np.array(item['vectors']).T assert len(x1) assert len(x2) assert len(y1) assert len(y2)
def test_basic(): """Test reading one specific NEXRAD NIDS file based on the filename.""" f = Level3File( get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False)) assert f.metadata['prod_time'].replace(second=0) == datetime( 2014, 4, 7, 18, 5) assert f.metadata['vol_time'].replace(second=0) == datetime( 2014, 4, 7, 18, 5) assert f.metadata['msg_time'].replace(second=0) == datetime( 2014, 4, 7, 18, 6)
def test_basic(): """Test reading one specific NEXRAD NIDS file based on the filename.""" f = Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False)) assert f.metadata['prod_time'].replace(second=0) == datetime(2014, 4, 7, 18, 5) assert f.metadata['vol_time'].replace(second=0) == datetime(2014, 4, 7, 18, 5) assert f.metadata['msg_time'].replace(second=0) == datetime(2014, 4, 7, 18, 6) assert f.filename == get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False) # At this point, really just want to make sure that __str__ is able to run and produce # something not empty, the format is still up for grabs. assert str(f)
def test_new_gsm(): """Test parsing recent mods to the GSM product.""" f = Level3File(get_test_data('nids/KDDC-gsm.nids')) assert f.gsm_additional.vcp_supplemental == [ 'AVSET', 'SAILS', 'RxR Noise', 'CBT' ] assert f.gsm_additional.supplemental_cut_count == 2 truth = [False] * 16 truth[2] = True truth[5] = True assert f.gsm_additional.supplemental_cut_map == truth assert f.gsm_additional.supplemental_cut_map2 == [False] * 9 # Check that str() doesn't error out assert str(f)
def __init__(self, file, sensorData=None): self.file = file f = Level3File(self.file) dataDict = f.sym_block[0][0] # Pull the data dictionary out of the file object self.metadata = f.metadata self.datetime = f.metadata['prod_time'] # More convienent access to the production time datetime object self.data = np.ma.array(dataDict['data']) # Turn into an array, then mask if sensorData is not None: self.sensorData = sensorData else: self.sensorData = {'siteID':f.siteID,'lat': f.lat,'lon': f.lon, 'height':f.height} self.az = np.array(dataDict['start_az'] + [dataDict['end_az'][-1]]) # Grab azimuths and calculate a range based on number of gates self.rng = np.linspace(0, f.max_range, self.data.shape[-1] + 1) #range map - km^2 at each bin in range*azimuth rangeStep = f.max_range/(self.data.shape[-1] ) f = lambda x:(1.0/len(self.az))*((np.pi*(x+rangeStep)**2)-(np.pi*(x)**2)) self.rangeMap = np.ones(tuple(map(operator.add,self.data.shape,(0,1)))) * np.array([[f(xi) for xi in self.rng]])
Use MetPy to read information from a NEXRAD Level 3 (NIDS product) file and plot """ import matplotlib.pyplot as plt import numpy as np from metpy.cbook import get_test_data from metpy.io import Level3File from metpy.plots import add_metpy_logo, ctables ########################################### fig, axes = plt.subplots(1, 2, figsize=(15, 8)) add_metpy_logo(fig, 1200, 85, size='large') for v, ctable, ax in zip(('N0Q', 'N0U'), ('NWSReflectivity', 'NWSVelocity'), axes): # Open the file name = get_test_data('nids/KOUN_SDUS54_{}TLX_201305202016'.format(v), as_file_obj=False) f = Level3File(name) # Pull the data out of the file object datadict = f.sym_block[0][0] # Turn into an array, then mask data = np.ma.array(datadict['data']) data[data == 0] = np.ma.masked # Grab azimuths and calculate a range based on number of gates az = np.array(datadict['start_az'] + [datadict['end_az'][-1]]) rng = np.linspace(0, f.max_range, data.shape[-1] + 1) # Convert az,range to x,y xlocs = rng * np.sin(np.deg2rad(az[:, np.newaxis])) ylocs = rng * np.cos(np.deg2rad(az[:, np.newaxis]))
def test_tdwr_nids(fname): """Test opening a TDWR NIDS file.""" Level3File(fname)
def test_nids_super_res_width(): """Test decoding a super resolution spectrum width product.""" f = Level3File(get_test_data('nids/KLZK_H0W_20200812_1305')) width = f.map_data(f.sym_block[0][0]['data']) assert np.nanmax(width) == 15
def test_fobj(): """Test reading a specific NEXRAD NIDS files from a file object.""" Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids'))
def test_nwstg(): """Test reading a nids file pulled from the NWSTG.""" Level3File(get_test_data('nids/sn.last', as_file_obj=False))
def test_dhr(): """Test reading a time field for DHR product.""" f = Level3File(get_test_data('nids/KOUN_SDUS54_DHRTLX_201305202016')) assert f.metadata['avg_time'] == datetime(2013, 5, 20, 20, 18)
def test_tdwr(): """Test reading a specific TDWR file.""" f = Level3File(get_test_data('nids/Level3_SLC_TV0_20160516_2359.nids')) assert f.prod_desc.prod_code == 182
def test_nids_supplemental(fname, truth): """Checks decoding of supplemental scan fields for some nids products.""" f = Level3File(get_test_data(fname)) assert f.metadata['delta_time'] == truth[0] assert f.metadata['supplemental_scan'] == truth[1]