예제 #1
0
def test_gini_dataset(filename, bounds, data_var, proj_attrs):
    """Test the dataset interface for GINI."""
    f = GiniFile(get_test_data(filename))
    ds = f.to_dataset()

    # 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)

    xmin, xmax, ymin, ymax = ds.img_extent
    assert_almost_equal(xmin, x0, 4)
    assert_almost_equal(xmax, x1, 4)
    assert_almost_equal(ymin, y0, 4)
    assert_almost_equal(ymax, y1, 4)

    # Check the projection metadata
    proj_name = ds.variables[data_var].grid_mapping
    proj_var = ds.variables[proj_name]
    for attr, val in proj_attrs.items():
        assert getattr(proj_var, 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)
예제 #2
0
파일: test_gini.py 프로젝트: ahill818/MetPy
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[-1, -1] + (lon[-1, -1] - lon[-1, -2]), f.proj_info.lo2, 4)
    assert_almost_equal(lat[-1, -1] + (lat[-1, -1] - lat[-2, -1]), f.proj_info.la2, 4)
예제 #3
0
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)
예제 #4
0
def get_closest_satellite_vis(time):
    """
    Get the super national 8 km visible satellite image. The image closest to
    the requested time will be returned.

    time : datetime object of image
    """

    catalog = TDSCatalog(
        'http://thredds.ucar.edu/thredds/catalog/satellite/VIS/'
        'SUPER-NATIONAL_8km/current/catalog.xml')

    # Figure out the closest image to the requested time in the catalog
    datasets = list(catalog.datasets)
    dt_stamps = []
    for dataset in datasets:
        fmt = 'SUPER-NATIONAL_8km_VIS_%Y%m%d_%H%M.gini'
        dt_stamps.append(datetime.strptime(dataset, fmt))
    closest_time = min(dt_stamps, key=lambda x: abs(x - time))
    index = dt_stamps.index(closest_time)

    # Request that image and convert it to a netCDF like dataset
    satellite = list(catalog.datasets.values())[index]
    sat_data = GiniFile(
        urllib.request.urlopen(satellite.access_urls['HTTPServer']))
    ds = sat_data.to_dataset()

    # Pull out the variables we need
    x = ds.variables['x'][:]
    y = ds.variables['y'][:]
    channel_data = ds.variables['Visible']
    time_var = ds.variables['time']
    proj_var = ds.variables[channel_data.grid_mapping]

    # Make a globe and projection for this dataset
    globe = ccrs.Globe(ellipse='sphere',
                       semimajor_axis=proj_var.earth_radius,
                       semiminor_axis=proj_var.earth_radius)

    proj = ccrs.Stereographic(
        central_longitude=proj_var.straight_vertical_longitude_from_pole,
        central_latitude=proj_var.latitude_of_projection_origin,
        true_scale_latitude=proj_var.standard_parallel,
        globe=globe)

    timestamp = netCDF4.num2date(time_var[:].squeeze(), time_var.units)
    return timestamp, globe, proj, x, y, channel_data
def get_closest_satellite_vis(time):
    """
    Get the super national 8 km visible satellite image. The image closest to
    the requested time will be returned.

    time : datetime object of image
    """

    catalog = TDSCatalog('http://thredds.ucar.edu/thredds/catalog/satellite/VIS/'
                         'SUPER-NATIONAL_8km/current/catalog.xml')

    # Figure out the closest image to the requested time in the catalog
    datasets = list(catalog.datasets)
    dt_stamps = []
    for dataset in datasets:
        fmt = 'SUPER-NATIONAL_8km_VIS_%Y%m%d_%H%M.gini'
        dt_stamps.append(datetime.strptime(dataset, fmt))
    closest_time = min(dt_stamps, key=lambda x: abs(x - time))
    index = dt_stamps.index(closest_time)

    # Request that image and convert it to a netCDF like dataset
    satellite = list(catalog.datasets.values())[index]
    sat_data = GiniFile(urllib.request.urlopen(satellite.access_urls['HTTPServer']))
    ds = sat_data.to_dataset()

    # Pull out the variables we need
    x = ds.variables['x'][:]
    y = ds.variables['y'][:]
    channel_data = ds.variables['Visible']
    time_var = ds.variables['time']
    proj_var = ds.variables[channel_data.grid_mapping]

    # Make a globe and projection for this dataset
    globe = ccrs.Globe(ellipse='sphere', semimajor_axis=proj_var.earth_radius,
                       semiminor_axis=proj_var.earth_radius)

    proj = ccrs.Stereographic(central_longitude=proj_var.straight_vertical_longitude_from_pole,
                          central_latitude=proj_var.latitude_of_projection_origin,
                         true_scale_latitude=proj_var.standard_parallel, globe=globe)

    timestamp = netCDF4.num2date(time_var[:].squeeze(), time_var.units)
    return timestamp, globe, proj, x, y, channel_data
예제 #6
0
def test_gini_dataset(filename, bounds, data_var, proj_attrs, image, dt):
    """Test the dataset interface for GINI."""
    f = GiniFile(get_test_data(filename))
    with pytest.warns(MetpyDeprecationWarning):
        ds = f.to_dataset()

    # 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)

    xmin, xmax, ymin, ymax = ds.img_extent
    assert_almost_equal(xmin, x0, 4)
    assert_almost_equal(xmax, x1, 4)
    assert_almost_equal(ymin, y0, 4)
    assert_almost_equal(ymax, y1, 4)

    # Check the projection metadata
    proj_name = ds.variables[data_var].grid_mapping
    proj_var = ds.variables[proj_name]
    for attr, val in proj_attrs.items():
        assert getattr(proj_var, 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 that the decoded date time will work properly
    assert f.prod_desc.datetime == dt
예제 #7
0
def test_gini_dataset(filename, bounds, data_var, proj_attrs):
    """Test the dataset interface for GINI."""
    f = GiniFile(get_test_data(filename))
    ds = f.to_dataset()

    # 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)

    y = ds.variables['y']
    assert_almost_equal(y[0], y0, 4)
    assert_almost_equal(y[-1], y1, 4)

    # Check the projection metadata
    proj_name = ds.variables[data_var].grid_mapping
    proj_var = ds.variables[proj_name]
    for attr, val in proj_attrs.items():
        assert getattr(proj_var, attr) == val, 'Values mismatch for ' + attr

    # Check the lon/lat corner
    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)
예제 #8
0
from metpy.cbook import get_test_data
from metpy.io import GiniFile
from metpy.plots import add_metpy_logo, add_timestamp, ctables

###########################################

# Open the GINI file from the test data
f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini'))
print(f)

###########################################

# Get a Dataset view of the data (essentially a NetCDF-like interface to the
# underlying data). Pull out the data, (x, y) coordinates, and the projection
# information.
ds = f.to_dataset()
x = ds.variables['x'][:]
y = ds.variables['y'][:]
dat = ds.variables['WV']
proj_var = ds.variables[dat.grid_mapping]
print(proj_var)

###########################################

# Create CartoPy projection information for the file
globe = ccrs.Globe(ellipse='sphere',
                   semimajor_axis=proj_var.earth_radius,
                   semiminor_axis=proj_var.earth_radius)
proj = ccrs.LambertConformal(
    central_longitude=proj_var.longitude_of_central_meridian,
    central_latitude=proj_var.latitude_of_projection_origin,
예제 #9
0
from metpy.cbook import get_test_data
from metpy.io import GiniFile
from metpy.plots import ctables

###########################################

# Open the GINI file from the test data
f = GiniFile(get_test_data('WEST-CONUS_4km_WV_20151208_2200.gini'))
print(f)

###########################################

# Get a Dataset view of the data (essentially a NetCDF-like interface to the
# underlying data). Pull out the data, (x, y) coordinates, and the projection
# information.
ds = f.to_dataset()
x = ds.variables['x'][:]
y = ds.variables['y'][:]
dat = ds.variables['WV']
proj_var = ds.variables[dat.grid_mapping]
print(proj_var)

###########################################

# Create CartoPy projection information for the file
globe = ccrs.Globe(ellipse='sphere', semimajor_axis=proj_var.earth_radius,
                   semiminor_axis=proj_var.earth_radius)
proj = ccrs.LambertConformal(central_longitude=proj_var.longitude_of_central_meridian,
                             central_latitude=proj_var.latitude_of_projection_origin,
                             standard_parallels=[proj_var.standard_parallel],
                             globe=globe)