def test_interpolate_to_points(method, test_data): r"""Test main grid interpolation function.""" xp, yp, z = test_data obs_points = np.vstack([xp, yp]).transpose() * 10 with get_test_data('interpolation_test_points.npz') as fobj: test_points = np.load(fobj)['points'] extra_kw = {} if method == 'cressman': extra_kw['search_radius'] = 200 extra_kw['minimum_neighbors'] = 1 elif method == 'barnes': extra_kw['search_radius'] = 400 extra_kw['minimum_neighbors'] = 1 extra_kw['gamma'] = 1 elif method == 'shouldraise': with pytest.raises(ValueError): interpolate_to_points( obs_points, z, test_points, interp_type=method, **extra_kw) return img = interpolate_to_points(obs_points, z, test_points, interp_type=method, **extra_kw) with get_test_data('{0}_test.npz'.format(method)) as fobj: truth = np.load(fobj)['img'].reshape(-1) assert_array_almost_equal(truth, img)
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_interpolate(method, test_coords, boundary_coords): r"""Test deprecated main interpolate function.""" xp, yp = test_coords xp *= 10 yp *= 10 z = np.array([0.064, 4.489, 6.241, 0.1, 2.704, 2.809, 9.604, 1.156, 0.225, 3.364]) extra_kw = {} if method == 'cressman': extra_kw['search_radius'] = 200 extra_kw['minimum_neighbors'] = 1 elif method == 'barnes': extra_kw['search_radius'] = 400 extra_kw['minimum_neighbors'] = 1 extra_kw['gamma'] = 1 if boundary_coords is not None: extra_kw['boundary_coords'] = boundary_coords with pytest.warns(MetpyDeprecationWarning): _, _, img = interpolate(xp, yp, z, hres=10, interp_type=method, **extra_kw) with get_test_data('{0}_test.npz'.format(method)) as fobj: truth = np.load(fobj)['img'] assert_array_almost_equal(truth, img)
def test_gini_ak_regional(): 'Test reading of AK Regional Gini file' f = GiniFile(get_test_data('AK-REGIONAL_8km_3.9_20160408_1445.gini')) pdb = f.prod_desc assert pdb.source == 1 assert pdb.creating_entity == 'GOES-15' assert pdb.sector_id == 'Alaska Regional' assert pdb.channel == 'IR (3.9 micron)' assert pdb.num_records == 408 assert pdb.record_len == 576 assert pdb.datetime == datetime(2016, 4, 8, 14, 45, 20) assert pdb.projection == GiniProjection.polar_stereographic assert pdb.nx == 576 assert pdb.ny == 408 assert_almost_equal(pdb.la1, 42.0846, 4) assert_almost_equal(pdb.lo1, -175.641, 4) proj = f.proj_info assert proj.reserved == 0 assert_almost_equal(proj.lov, 210.0, 1) assert_almost_equal(proj.dx, 7.9375, 4) assert_almost_equal(proj.dy, 7.9375, 4) assert proj.proj_center == 0 pdb2 = f.prod_desc2 assert pdb2.scanning_mode == [False, False, False] assert_almost_equal(pdb2.lat_in, 0.0, 4) assert pdb2.resolution == 8 assert pdb2.compression == 0 assert pdb2.version == 1 assert pdb2.pdb_size == 512 assert pdb2.nav_cal == 0 assert f.data.shape, (pdb.num_records == pdb.record_len)
def station_test_data(variable_names, proj_from=None, proj_to=None): with get_test_data('station_data.txt') as f: all_data = np.loadtxt(f, skiprows=1, delimiter=',', usecols=(1, 2, 3, 4, 5, 6, 7, 17, 18, 19), dtype=np.dtype([('stid', '3S'), ('lat', 'f'), ('lon', 'f'), ('slp', 'f'), ('air_temperature', 'f'), ('cloud_fraction', 'f'), ('dewpoint', 'f'), ('weather', '16S'), ('wind_dir', 'f'), ('wind_speed', 'f')])) all_stids = [s.decode('ascii') for s in all_data['stid']] data = np.concatenate([all_data[all_stids.index(site)].reshape(1, ) for site in all_stids]) value = data[variable_names] lon = data['lon'] lat = data['lat'] if proj_from is not None and proj_to is not None: try: proj_points = proj_to.transform_points(proj_from, lon, lat) return proj_points[:, 0], proj_points[:, 1], value except Exception as e: print(e) return None return lon, lat, value
def test_latlon(): """Test our handling of lat/lon information.""" data = xr.open_dataset(get_test_data('irma_gfs_example.nc', as_file_obj=False)) img = ImagePlot() img.data = data img.field = 'Temperature_isobaric' img.level = 500 * units.hPa img.time = datetime(2017, 9, 5, 15, 0, 0) contour = ContourPlot() contour.data = data contour.field = 'Geopotential_height_isobaric' contour.level = img.level contour.time = img.time panel = MapPanel() panel.projection = 'lcc' panel.area = 'us' panel.plots = [img, contour] pc = PanelContainer() pc.panel = panel pc.draw() return pc.figure
def test_raw_gini(filename, pdb, pdb2, proj_info): """Test raw GINI parsing.""" f = GiniFile(get_test_data(filename)) assert f.prod_desc == pdb assert f.prod_desc2 == pdb2 assert f.proj_info == proj_info assert f.data.shape == (pdb.num_records, pdb.record_len)
def basic_test(): 'Basic test of GINI reading' f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini')) pdb = f.prod_desc eq_(pdb.source, 1) eq_(pdb.creating_entity, 'GOES-15') eq_(pdb.sector_id, 'West CONUS') eq_(pdb.channel, 'WV (6.5/6.7 micron)') eq_(pdb.num_records, 1280) eq_(pdb.record_len, 1100) eq_(pdb.datetime, datetime(2015, 12, 8, 22, 0, 19, 0)) eq_(pdb.projection, GiniProjection.lambert_conformal) eq_(pdb.nx, 1100) eq_(pdb.ny, 1280) assert_almost_equal(pdb.la1, 12.19, 4) assert_almost_equal(pdb.lo1, -133.4588, 4) proj = f.proj_info eq_(proj.reserved, 0) assert_almost_equal(proj.lov, -95.0, 4) assert_almost_equal(proj.dx, 4.0635, 4) assert_almost_equal(proj.dy, 4.0635, 4) eq_(proj.proj_center, 0) pdb2 = f.prod_desc2 eq_(pdb2.scanning_mode, [False, False, False]) assert_almost_equal(pdb2.lat_in, 25.0, 4) eq_(pdb2.resolution, 4) eq_(pdb2.compression, 0) eq_(pdb2.version, 1) eq_(pdb2.pdb_size, 512) eq_(f.data.shape, (pdb.num_records, pdb.record_len))
def test_tracks(self): 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 test_gini_mercator(): 'Test reading of GINI file with Mercator projection (from HI)' f = GiniFile(get_test_data('HI-REGIONAL_4km_3.9_20160616_1715.gini')) pdb = f.prod_desc assert pdb.source == 1 assert pdb.creating_entity == 'GOES-15' assert pdb.sector_id == 'Hawaii Regional' assert pdb.channel == 'IR (3.9 micron)' assert pdb.num_records == 520 assert pdb.record_len == 560 assert pdb.datetime == datetime(2016, 6, 16, 17, 15, 18) assert pdb.projection == GiniProjection.mercator assert pdb.nx == 560 assert pdb.ny == 520 assert_almost_equal(pdb.la1, 9.343, 4) assert_almost_equal(pdb.lo1, -167.315, 4) proj = f.proj_info assert proj.resolution == 0 assert_almost_equal(proj.la2, 28.0922, 4) assert_almost_equal(proj.lo2, -145.878, 4) assert proj.di == 0 assert proj.dj == 0 pdb2 = f.prod_desc2 assert pdb2.scanning_mode == [False, False, False] assert_almost_equal(pdb2.lat_in, 20.0, 4) assert pdb2.resolution == 4 assert pdb2.compression == 0 assert pdb2.version == 1 assert pdb2.pdb_size == 512 assert pdb2.nav_cal == 0 assert f.data.shape, (pdb.num_records == pdb.record_len)
def test_gini_basic(): 'Basic test of GINI reading' f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini')) pdb = f.prod_desc assert pdb.source == 1 assert pdb.creating_entity == 'GOES-15' assert pdb.sector_id == 'West CONUS' assert pdb.channel == 'WV (6.5/6.7 micron)' assert pdb.num_records == 1280 assert pdb.record_len == 1100 assert pdb.datetime == datetime(2015, 12, 8, 22, 0, 19) assert pdb.projection == GiniProjection.lambert_conformal assert pdb.nx == 1100 assert pdb.ny == 1280 assert_almost_equal(pdb.la1, 12.19, 4) assert_almost_equal(pdb.lo1, -133.4588, 4) proj = f.proj_info assert proj.reserved == 0 assert_almost_equal(proj.lov, -95.0, 4) assert_almost_equal(proj.dx, 4.0635, 4) assert_almost_equal(proj.dy, 4.0635, 4) assert proj.proj_center == 0 pdb2 = f.prod_desc2 assert pdb2.scanning_mode == [False, False, False] assert_almost_equal(pdb2.lat_in, 25.0, 4) assert pdb2.resolution == 4 assert pdb2.compression == 0 assert pdb2.version == 1 assert pdb2.pdb_size == 512 assert f.data.shape, (pdb.num_records == pdb.record_len)
def test_gini_xarray(filename, bounds, data_var, proj_attrs, image, dt): """Test that GINIFile can be passed to XArray as a datastore.""" f = GiniFile(get_test_data(filename)) ds = xr.open_dataset(f) # Check our calculated x and y arrays x0, x1, y0, y1 = bounds x = ds.variables['x'] assert_almost_equal(x[0], x0, 4) assert_almost_equal(x[-1], x1, 4) # Because the actual data raster has the top row first, the maximum y value is y[0], # while the minimum y value is y[-1] y = ds.variables['y'] assert_almost_equal(y[-1], y0, 4) assert_almost_equal(y[0], y1, 4) # Check the projection metadata proj_name = ds.variables[data_var].attrs['grid_mapping'] proj_var = ds.variables[proj_name] for attr, val in proj_attrs.items(): assert proj_var.attrs[attr] == val, 'Values mismatch for ' + attr # Check the lower left lon/lat corner assert_almost_equal(ds.variables['lon'][-1, 0], f.prod_desc.lo1, 4) assert_almost_equal(ds.variables['lat'][-1, 0], f.prod_desc.la1, 4) # Check a pixel of the image to make sure we're decoding correctly x_ind, y_ind, val = image assert val == ds.variables[data_var][x_ind, y_ind] # Check time decoding assert np.asarray(dt, dtype='datetime64[ms]') == ds.variables['time']
def get_upper_air_data(date, station): """Get upper air observations from the test data cache. Parameters ---------- time : datetime The date and time of the desired observation. station : str The three letter ICAO identifier of the station for which data should be downloaded. Returns ------- dict : upper air data """ sounding_key = '{0:%Y-%m-%dT%HZ}_{1:}'.format(date, station) sounding_files = {'2016-05-22T00Z_DDC': 'may22_sounding.txt', '2013-01-20T12Z_OUN': 'jan20_sounding.txt', '1999-05-04T00Z_OUN': 'may4_sounding.txt', '2002-11-11T00Z_BNA': 'nov11_sounding.txt', '2010-12-09T12Z_BOI': 'dec9_sounding.txt'} fname = sounding_files[sounding_key] fobj = get_test_data(fname) def to_float(s): # Remove all whitespace and replace empty values with NaN if not s.strip(): s = 'nan' return float(s) # Skip dashes, column names, units, and more dashes for _ in range(4): fobj.readline() # Initiate lists for variables arr_data = [] # Read all lines of data and append to lists only if there is some data for row in fobj: level = to_float(row[0:7]) values = (to_float(row[7:14]), to_float(row[14:21]), to_float(row[21:28]), to_float(row[42:49]), to_float(row[49:56])) if any(np.invert(np.isnan(values[1:]))): arr_data.append((level,) + values) p, z, t, td, direc, spd = np.array(arr_data).T p = p * units.hPa z = z * units.meters t = t * units.degC td = td * units.degC direc = direc * units.degrees spd = spd * units.knots u, v = wind_components(spd, direc) return {'pressure': p, 'height': z, 'temperature': t, 'dewpoint': td, 'direction': direc, 'speed': spd, 'u_wind': u, 'v_wind': v}
def test_inverse_distance_to_points(method, test_data, test_points): r"""Test inverse distance interpolation to grid function.""" xp, yp, z = test_data obs_points = np.vstack([xp, yp]).transpose() extra_kw = {} if method == 'cressman': extra_kw['r'] = 20 extra_kw['min_neighbors'] = 1 test_file = 'cressman_r20_mn1.npz' elif method == 'barnes': extra_kw['r'] = 40 extra_kw['kappa'] = 100 test_file = 'barnes_r40_k100.npz' elif method == 'shouldraise': extra_kw['r'] = 40 with pytest.raises(ValueError): inverse_distance_to_points( obs_points, z, test_points, kind=method, **extra_kw) return img = inverse_distance_to_points(obs_points, z, test_points, kind=method, **extra_kw) with get_test_data(test_file) as fobj: truth = np.load(fobj)['img'].reshape(-1) assert_array_almost_equal(truth, img)
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_parse_file_object(): """Test the parser reading from a file-like object.""" input_file = get_test_data('metar_20190701_1200.txt', mode='rt') df = parse_metar_file(input_file) test = df[df.station_id == 'KOKC'] assert test.air_temperature.values == 21 assert test.dew_point_temperature.values == 21 assert test.altimeter.values == 30.03
def test_str(): 'Test the str representation of GiniFile' f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini')) truth = ('GiniFile: GOES-15 West CONUS WV (6.5/6.7 micron)\n' '\tTime: 2015-12-08 22:00:19\n\tSize: 1280x1100\n' '\tProjection: lambert_conformal\n' '\tLower Left Corner (Lon, Lat): (-133.4588, 12.19)\n\tResolution: 4km') assert str(f) == truth
def test_gini_str(): 'Test the str representation of GiniFile' f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini')) truth = ('GiniFile: GOES-15 West CONUS WV (6.5/6.7 micron)\n' '\tTime: 2015-12-08 22:00:19\n\tSize: 1280x1100\n' '\tProjection: lambert_conformal\n' '\tLower Left Corner (Lon, Lat): (-133.4588, 12.19)\n\tResolution: 4km') assert str(f) == truth
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 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 test_msg15(): """Check proper decoding of message type 15.""" f = Level2File( get_test_data('KTLX20130520_201643_V06.gz', as_file_obj=False)) data = f.clutter_filter_map['data'] assert isinstance(data[0][0], list) assert f.clutter_filter_map['datetime'] == datetime( 2013, 5, 19, 0, 0, 0, 315000)
def test_gini_pathlib(): """Test that GiniFile works with `pathlib.Path` instances.""" from pathlib import Path src = Path( get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini', as_file_obj=False)) f = GiniFile(src) assert f.prod_desc.sector_id == 'West CONUS'
def test_doubled_file(): """Test for #489 where doubled-up files didn't parse at all.""" with contextlib.closing( get_test_data('Level2_KFTG_20150430_1419.ar2v')) as infile: data = infile.read() fobj = BytesIO(data + data) f = Level2File(fobj) assert len(f.sweeps) == 12
def test_unidata_composite(): """Test reading radar composites in GINI format made by Unidata.""" f = GiniFile(get_test_data('Level3_Composite_dhr_1km_20180309_2225.gini')) # Check the time stamp assert datetime(2018, 3, 9, 22, 25) == f.prod_desc.datetime # Check data value assert 66 == f.data[2160, 2130]
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_no_field_error(): """Make sure we get a useful error when the field is not set.""" data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False)) contour = ContourPlot() contour.data = data contour.level = 700 * units.hPa with pytest.raises(ValueError): contour.draw()
def _wrapped_urlopen(self, url): r"""Method to wrap urlopen and look to see if the request should be redirected.""" from metpy.cbook import get_test_data filename = self.url_map.get(url) if filename is None: return self._urlopen(url) else: return open(get_test_data(filename, False), 'rb')
def _wrapped_urlopen(self, url): r"""Wrap urlopen and look to see if the request should be redirected.""" from metpy.cbook import get_test_data filename = self.url_map.get(url) if filename is None: return self._urlopen(url) else: return open(get_test_data(filename, False), 'rb')
def test_no_field_error_barbs(): """Make sure we get a useful error when the field is not set.""" data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False)) barbs = BarbPlot() barbs.data = data barbs.level = 700 * units.hPa with pytest.raises(TraitError): barbs.draw()
def test_natural_neighbor(test_data, test_grid): r"""Test natural neighbor interpolation function.""" xp, yp, z = test_data xg, yg = test_grid img = natural_neighbor(xp, yp, z, xg, yg) truth = np.load(get_test_data('nn_bbox0to100.npz'))['img'] assert_array_almost_equal(truth, img)
def test_level2(fname, voltime, num_sweeps, mom_first, mom_last, expected_logs, caplog): """Test reading NEXRAD level 2 files from the filename.""" caplog.set_level(logging.WARNING, 'metpy.io.nexrad') f = Level2File(get_test_data(fname, as_file_obj=False)) assert f.dt == voltime assert len(f.sweeps) == num_sweeps assert len(f.sweeps[0][0][-1]) == mom_first assert len(f.sweeps[-1][0][-1]) == mom_last assert len(caplog.records) == expected_logs
def test_vector_packet(self): 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_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 'This product may not parse correctly' in caplog.records[0].message
def test_interpolate_to_points_invalid(test_data): """Test that interpolate_to_points raises when given an invalid method.""" xp, yp, z = test_data obs_points = np.vstack([xp, yp]).transpose() * 10 with get_test_data('interpolation_test_points.npz') as fobj: test_points = np.load(fobj)['points'] with pytest.raises(ValueError): interpolate_to_points(obs_points, z, test_points, interp_type='shouldraise')
def test_natural_neighbor_to_points(test_data, test_points): r"""Test natural neighbor interpolation to grid function.""" xp, yp, z = test_data obs_points = np.vstack([xp, yp]).transpose() img = natural_neighbor_to_points(obs_points, z, test_points) with get_test_data('nn_bbox0to100.npz') as fobj: truth = np.load(fobj)['img'].reshape(-1) assert_array_almost_equal(truth, img)
def test_gini_dataset(): "Test the dataset interface for GINI" f = GiniFile(get_test_data("WEST-CONUS_4km_WV_20151208_2200.gini")) ds = f.to_dataset() assert "x" in ds.variables assert "y" in ds.variables assert "WV" in ds.variables assert hasattr(ds.variables["WV"], "grid_mapping") assert ds.variables["WV"].grid_mapping in ds.variables assert_almost_equal(ds.variables["lon"][0, 0], f.prod_desc.lo1, 4) assert_almost_equal(ds.variables["lat"][0, 0], f.prod_desc.la1, 4)
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_parse_no_pint_objects_in_df(): """Test that there are no Pint quantities in dataframes created by parser.""" input_file = get_test_data('metar_20190701_1200.txt', mode='rt') metar_str = ( 'KSLK 011151Z AUTO 21005KT 1/4SM FG VV002 14/13 A1013 RMK AO2 SLP151 70043 ' 'T01390133 10139 20094 53002=') for df in (parse_metar_file(input_file), parse_metar_to_dataframe(metar_str)): for column in df: assert not isinstance(df[column][0], units.Quantity)
def test_natural_neighbor_to_grid(test_data, test_grid): r"""Test natural neighbor interpolation to grid function.""" xp, yp, z = test_data xg, yg = test_grid img = natural_neighbor_to_grid(xp, yp, z, xg, yg) with get_test_data('nn_bbox0to100.npz') as fobj: truth = np.load(fobj)['img'] assert_array_almost_equal(truth, img)
def test_gini_mercator_upper_corner(): """Test that the upper corner of the Mercator coordinates is correct.""" f = GiniFile(get_test_data('HI-REGIONAL_4km_3.9_20160616_1715.gini')) ds = xr.open_dataset(f) lat = ds.variables['lat'] lon = ds.variables['lon'] # 2nd corner lat/lon are at the "upper right" corner of the pixel, so need to add one # more grid point assert_almost_equal(lon[0, -1] + (lon[0, -1] - lon[0, -2]), f.proj_info.lo2, 4) assert_almost_equal(lat[0, -1] + (lat[0, -1] - lat[1, -1]), f.proj_info.la2, 4)
def test_dataset(): 'Test the dataset interface for GINI' f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini')) ds = f.to_dataset() assert 'x' in ds.variables assert 'y' in ds.variables assert 'WV' in ds.variables assert hasattr(ds.variables['WV'], 'grid_mapping') assert ds.variables['WV'].grid_mapping in ds.variables assert_almost_equal(ds.variables['lon'][0, 0], f.prod_desc.lo1, 4) assert_almost_equal(ds.variables['lat'][0, 0], f.prod_desc.la1, 4)
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_gini_dataset(): 'Test the dataset interface for GINI' f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini')) ds = f.to_dataset() assert 'x' in ds.variables assert 'y' in ds.variables assert 'WV' in ds.variables assert hasattr(ds.variables['WV'], 'grid_mapping') assert ds.variables['WV'].grid_mapping in ds.variables assert_almost_equal(ds.variables['lon'][0, 0], f.prod_desc.lo1, 4) assert_almost_equal(ds.variables['lat'][0, 0], f.prod_desc.la1, 4)
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_gini_ak_regional_dataset(): 'Test the dataset interface for GINI of a AK REGIONAL file' f = GiniFile(get_test_data('AK-REGIONAL_8km_3.9_20160408_1445.gini')) ds = f.to_dataset() assert 'x' in ds.variables assert 'y' in ds.variables assert 'IR' in ds.variables assert hasattr(ds.variables['IR'], 'grid_mapping') assert ds.variables['IR'].grid_mapping in ds.variables assert_almost_equal(ds.variables['lon'][0, 0], f.prod_desc.lo1, 4) assert_almost_equal(ds.variables['lat'][0, 0], f.prod_desc.la1, 4)
def test_gini_mercator_upper_corner(): """Test that the upper corner of the Mercator coordinates is correct.""" f = GiniFile(get_test_data('HI-REGIONAL_4km_3.9_20160616_1715.gini')) ds = f.to_dataset() lat = ds.variables['lat'] lon = ds.variables['lon'] # 2nd corner lat/lon are at the "upper right" corner of the pixel, so need to add one # more grid point assert_almost_equal(lon[0, -1] + (lon[0, -1] - lon[0, -2]), f.proj_info.lo2, 4) assert_almost_equal(lat[0, -1] + (lat[0, -1] - lat[1, -1]), f.proj_info.la2, 4)
def test_parse_file(): """Test the parser on an entire file.""" input_file = get_test_data('metar_20190701_1200.txt', as_file_obj=False) df = parse_metar_file(input_file) # Check counts (non-NaN) of various fields counts = df.count() assert counts.station_id == 8980 assert counts.latitude == 8968 assert counts.longitude == 8968 assert counts.elevation == 8968 assert counts.date_time == 8980 assert counts.wind_direction == 8577 assert counts.wind_speed == 8844 assert counts.wind_gust == 347 assert counts.visibility == 8486 assert counts.current_wx1 == 1090 assert counts.current_wx2 == 82 assert counts.current_wx3 == 1 assert counts.low_cloud_type == 7361 assert counts.low_cloud_level == 3867 assert counts.medium_cloud_type == 1646 assert counts.medium_cloud_level == 1641 assert counts.high_cloud_type == 632 assert counts.high_cloud_level == 626 assert counts.highest_cloud_type == 37 assert counts.highest_cloud_level == 37 assert counts.cloud_coverage == 8980 assert counts.air_temperature == 8779 assert counts.dew_point_temperature == 8740 assert counts.altimeter == 8458 assert counts.remarks == 8980 assert (df.current_wx1_symbol != 0).sum() == counts.current_wx1 assert (df.current_wx2_symbol != 0).sum() == counts.current_wx2 assert (df.current_wx3_symbol != 0).sum() == counts.current_wx3 assert counts.air_pressure_at_sea_level == 8378 assert counts.eastward_wind == 8577 assert counts.northward_wind == 8577 # KVPZ 011156Z AUTO 27005KT 10SM CLR 23/19 A3004 RMK AO2 SLP166 test = df[df.station_id == 'KVPZ'] assert test.air_temperature.values == 23 assert test.dew_point_temperature.values == 19 assert test.altimeter.values == 30.04 assert_almost_equal(test.eastward_wind.values, 5) assert_almost_equal(test.northward_wind.values, 0) assert test.air_pressure_at_sea_level.values == 1016.76 # Check that this ob properly gets all lines paku = df[df.station_id == 'PAKU'] assert_almost_equal(paku.air_temperature.values, [9, 12]) assert_almost_equal(paku.dew_point_temperature.values, [9, 10]) assert_almost_equal(paku.altimeter.values, [30.02, 30.04])
def test_parse_file_object(): """Test the parser reading from a file-like object.""" input_file = get_test_data('metar_20190701_1200.txt', mode='rt') # KOKC 011152Z 18006KT 7SM FEW080 FEW250 21/21 A3003 RMK AO2 SLP155 T02060206... df = parse_metar_file(input_file) test = df[df.station_id == 'KOKC'] assert_almost_equal(test.visibility.values, 11265.408) assert test.air_temperature.values == 21 assert test.dew_point_temperature.values == 21 assert test.altimeter.values == 30.03 assert_almost_equal(test.eastward_wind.values, 0) assert_almost_equal(test.northward_wind.values, 6)
def test_natural_neighbor(test_data, test_grid): r"""Test deprecated natural neighbor interpolation function.""" xp, yp, z = test_data xg, yg = test_grid with pytest.warns(MetpyDeprecationWarning): img = natural_neighbor(xp, yp, z, xg, yg) with get_test_data('nn_bbox0to100.npz') as fobj: truth = np.load(fobj)['img'] assert_array_almost_equal(truth, img)
def test_standard_surface(): """Test to read a standard surface file.""" def dtparse(string): return datetime.strptime(string, '%y%m%d/%H%M') skip = ['text'] gsf = GempakSurface(get_test_data('gem_std.sfc')) gstns = gsf.sfjson() gempak = pd.read_csv(get_test_data('gem_std.csv'), index_col=['STN', 'YYMMDD/HHMM'], parse_dates=['YYMMDD/HHMM'], date_parser=dtparse) for stn in gstns: idx_key = (stn['properties']['station_id'], stn['properties']['date_time']) gemsfc = gempak.loc[idx_key, :] for param, val in stn['values'].items(): if param not in skip: assert val == pytest.approx(gemsfc[param.upper()])
def test_declarative_upa_obs_convert_barb_units(): """Test making a full upperair observation plot.""" data = pd.read_csv(get_test_data('UPA_obs.csv', as_file_obj=False)) data.units = '' data.units = { 'pressure': 'hPa', 'height': 'meters', 'temperature': 'degC', 'dewpoint': 'degC', 'direction': 'degrees', 'speed': 'knots', 'station': None, 'time': None, 'u_wind': 'knots', 'v_wind': 'knots', 'latitude': 'degrees', 'longitude': 'degrees' } obs = PlotObs() obs.data = data obs.time = datetime(1993, 3, 14, 0) obs.level = 500 * units.hPa obs.fields = ['temperature', 'dewpoint', 'height'] obs.locations = ['NW', 'SW', 'NE'] obs.formats = [None, None, lambda v: format(v, '.0f')[:3]] obs.vector_field = ('u_wind', 'v_wind') obs.vector_field_length = 7 obs.vector_plot_units = 'm/s' obs.reduce_points = 0 # Panel for plot with Map features panel = MapPanel() panel.layout = (1, 1, 1) panel.area = (-124, -72, 20, 53) panel.projection = 'lcc' panel.layers = ['coastline', 'borders', 'states', 'land'] panel.plots = [obs] # Bringing it all together pc = PanelContainer() pc.size = (15, 10) pc.panels = [panel] pc.draw() obs.level = 300 * units.hPa return pc.figure
def test_gini_ak_regional_dataset(): 'Test the dataset interface for GINI of a AK REGIONAL file' f = GiniFile(get_test_data('AK-REGIONAL_8km_3.9_20160408_1445.gini')) ds = f.to_dataset() assert 'x' in ds.variables assert 'y' in ds.variables assert 'IR' in ds.variables assert hasattr(ds.variables['IR'], 'grid_mapping') assert ds.variables['IR'].grid_mapping in ds.variables assert_almost_equal(ds.variables['lon'][0, 0], f.prod_desc.lo1, 4) assert_almost_equal(ds.variables['lat'][0, 0], f.prod_desc.la1, 4) assert_almost_equal(ds.variables['Polar_Stereographic'].longitude_of_projection_origin, 210.0) assert_almost_equal(ds.variables['Polar_Stereographic'].latitude_of_projection_origin, 90.0)