コード例 #1
0
def test_gradient_2d(deriv_2d_data):
    """Test gradient with 2D arrays."""
    res = gradient(deriv_2d_data.f, x=(deriv_2d_data.y, deriv_2d_data.x))
    truth = (np.array([[-0.25, -0.25, -0.25], [1.75, 1.75, 1.75],
                       [4.75, 4.75, 4.75], [5.75, 5.75, 5.75]]),
             np.array([[-3, -1, 4], [-3, -1, 4], [-3, -1, 4], [-3, -1, 4]]))
    assert_array_almost_equal(res, truth, 5)
コード例 #2
0
ファイル: test_calc_tools.py プロジェクト: kenpryor67/MetPy
def test_gradient_x_deprecation(deriv_2d_data):
    """Test deprecation of x keyword argument."""
    with pytest.warns(MetpyDeprecationWarning):
        res = gradient(deriv_2d_data.f, x=(deriv_2d_data.y, deriv_2d_data.x))
    truth = (np.array([[-0.25, -0.25, -0.25], [1.75, 1.75, 1.75],
                       [4.75, 4.75, 4.75], [5.75, 5.75, 5.75]]),
             np.array([[-3, -1, 4], [-3, -1, 4], [-3, -1, 4], [-3, -1, 4]]))
    assert_array_almost_equal(res, truth, 5)
コード例 #3
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_gradient_xarray_pint_conversion(test_da_xy):
    """Test the 2D gradient calculation with a 2D DataArray and implicit pint conversion."""
    data = test_da_xy.isel(time=0, isobaric=2)
    deriv_y, deriv_x = gradient(data, coordinates=(data.metpy.y, data.metpy.x))

    truth_x = np.ones_like(data) * -6.993007e-07 * units('kelvin / meter')
    truth_y = np.ones_like(data) * -2.797203e-06 * units('kelvin / meter')

    assert_array_almost_equal(deriv_x, truth_x, 12)
    assert_array_almost_equal(deriv_y, truth_y, 12)
コード例 #4
0
def test_gradient_xarray_pint_conversion(test_da_xy):
    """Test the 2D gradient calculation with a 2D DataArray and implicit pint conversion."""
    data = test_da_xy.isel(time=0, isobaric=2)
    deriv_y, deriv_x = gradient(data, coordinates=(data.metpy.y, data.metpy.x))

    truth_x = np.ones_like(data) * -6.993007e-07 * units('kelvin / meter')
    truth_y = np.ones_like(data) * -2.797203e-06 * units('kelvin / meter')

    assert_array_almost_equal(deriv_x, truth_x, 12)
    assert_array_almost_equal(deriv_y, truth_y, 12)
コード例 #5
0
ファイル: test_calc_tools.py プロジェクト: kenpryor67/MetPy
def test_gradient_restricted_axes(deriv_2d_data):
    """Test 2D gradient with 3D arrays and manual specification of axes."""
    res = gradient(deriv_2d_data.f[..., None],
                   coordinates=(deriv_2d_data.y, deriv_2d_data.x),
                   axes=(0, 1))
    truth = (np.array([[[-0.25], [-0.25], [-0.25]], [[1.75], [1.75], [1.75]],
                       [[4.75], [4.75], [4.75]], [[5.75], [5.75], [5.75]]]),
             np.array([[[-3], [-1], [4]], [[-3], [-1], [4]], [[-3], [-1], [4]],
                       [[-3], [-1], [4]]]))
    assert_array_almost_equal(res, truth, 5)
コード例 #6
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_gradient_2d(deriv_2d_data):
    """Test gradient with 2D arrays."""
    res = gradient(deriv_2d_data.f, coordinates=(deriv_2d_data.y, deriv_2d_data.x))
    truth = (np.array([[-0.25, -0.25, -0.25],
                       [1.75, 1.75, 1.75],
                       [4.75, 4.75, 4.75],
                       [5.75, 5.75, 5.75]]),
             np.array([[-3, -1, 4],
                       [-3, -1, 4],
                       [-3, -1, 4],
                       [-3, -1, 4]]))
    assert_array_almost_equal(res, truth, 5)
コード例 #7
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_gradient_x_deprecation(deriv_2d_data):
    """Test deprecation of x keyword argument."""
    with pytest.warns(MetpyDeprecationWarning):
        res = gradient(deriv_2d_data.f, x=(deriv_2d_data.y, deriv_2d_data.x))
    truth = (np.array([[-0.25, -0.25, -0.25],
                       [1.75, 1.75, 1.75],
                       [4.75, 4.75, 4.75],
                       [5.75, 5.75, 5.75]]),
             np.array([[-3, -1, 4],
                       [-3, -1, 4],
                       [-3, -1, 4],
                       [-3, -1, 4]]))
    assert_array_almost_equal(res, truth, 5)
コード例 #8
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_gradient_restricted_axes(deriv_2d_data):
    """Test 2D gradient with 3D arrays and manual specification of axes."""
    res = gradient(deriv_2d_data.f[..., None], coordinates=(deriv_2d_data.y, deriv_2d_data.x),
                   axes=(0, 1))
    truth = (np.array([[[-0.25], [-0.25], [-0.25]],
                       [[1.75], [1.75], [1.75]],
                       [[4.75], [4.75], [4.75]],
                       [[5.75], [5.75], [5.75]]]),
             np.array([[[-3], [-1], [4]],
                       [[-3], [-1], [4]],
                       [[-3], [-1], [4]],
                       [[-3], [-1], [4]]]))
    assert_array_almost_equal(res, truth, 5)
コード例 #9
0
def test_gradient_xarray(test_da_xy):
    """Test the 3D gradient calculation with a 4D DataArray in each axis usage."""
    deriv_x, deriv_y, deriv_p = gradient(test_da_xy,
                                         axes=('x', 'y', 'isobaric'))
    deriv_x_alt1, deriv_y_alt1, deriv_p_alt1 = gradient(test_da_xy,
                                                        axes=('x', 'y',
                                                              'vertical'))
    deriv_x_alt2, deriv_y_alt2, deriv_p_alt2 = gradient(test_da_xy,
                                                        axes=(3, 2, 1))

    truth_x = xr.full_like(test_da_xy, -6.993007e-07)
    truth_x.attrs['units'] = 'kelvin / meter'

    truth_y = xr.full_like(test_da_xy, -2.797203e-06)
    truth_y.attrs['units'] = 'kelvin / meter'

    partial = xr.DataArray(np.array([0.04129204, 0.03330003, 0.02264402]),
                           coords=(('isobaric', test_da_xy['isobaric']), ))
    _, truth_p = xr.broadcast(test_da_xy, partial)
    truth_p.coords['crs'] = test_da_xy['crs']
    truth_p.attrs['units'] = 'kelvin / hectopascal'

    # Assert results match expectations
    xr.testing.assert_allclose(deriv_x, truth_x)
    assert deriv_x.metpy.units == truth_x.metpy.units
    xr.testing.assert_allclose(deriv_y, truth_y)
    assert deriv_y.metpy.units == truth_y.metpy.units
    xr.testing.assert_allclose(deriv_p, truth_p)
    assert deriv_p.metpy.units == truth_p.metpy.units

    # Assert alternative specifications give same results (up to attribute differences)
    xr.testing.assert_equal(deriv_x_alt1, deriv_x)
    xr.testing.assert_equal(deriv_y_alt1, deriv_y)
    xr.testing.assert_equal(deriv_p_alt1, deriv_p)
    xr.testing.assert_equal(deriv_x_alt2, deriv_x)
    xr.testing.assert_equal(deriv_y_alt2, deriv_y)
    xr.testing.assert_equal(deriv_p_alt2, deriv_p)
コード例 #10
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_gradient_xarray(test_da_xy):
    """Test the 3D gradient calculation with a 4D DataArray in each axis usage."""
    deriv_x, deriv_y, deriv_p = gradient(test_da_xy, axes=('x', 'y', 'isobaric'))
    deriv_x_alt1, deriv_y_alt1, deriv_p_alt1 = gradient(test_da_xy,
                                                        axes=('x', 'y', 'vertical'))
    deriv_x_alt2, deriv_y_alt2, deriv_p_alt2 = gradient(test_da_xy, axes=(3, 2, 1))

    truth_x = xr.full_like(test_da_xy, -6.993007e-07)
    truth_x.attrs['units'] = 'kelvin / meter'

    truth_y = xr.full_like(test_da_xy, -2.797203e-06)
    truth_y.attrs['units'] = 'kelvin / meter'

    partial = xr.DataArray(
        np.array([0.04129204, 0.03330003, 0.02264402]),
        coords=(('isobaric', test_da_xy['isobaric']),)
    )
    _, truth_p = xr.broadcast(test_da_xy, partial)
    truth_p.coords['crs'] = test_da_xy['crs']
    truth_p.attrs['units'] = 'kelvin / hectopascal'

    # Assert results match expectations
    xr.testing.assert_allclose(deriv_x, truth_x)
    assert deriv_x.metpy.units == truth_x.metpy.units
    xr.testing.assert_allclose(deriv_y, truth_y)
    assert deriv_y.metpy.units == truth_y.metpy.units
    xr.testing.assert_allclose(deriv_p, truth_p)
    assert deriv_p.metpy.units == truth_p.metpy.units

    # Assert alternative specifications give same results
    xr.testing.assert_identical(deriv_x_alt1, deriv_x)
    xr.testing.assert_identical(deriv_y_alt1, deriv_y)
    xr.testing.assert_identical(deriv_p_alt1, deriv_p)
    xr.testing.assert_identical(deriv_x_alt2, deriv_x)
    xr.testing.assert_identical(deriv_y_alt2, deriv_y)
    xr.testing.assert_identical(deriv_p_alt2, deriv_p)
コード例 #11
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_gradient_xarray_implicit_axes(test_da_xy):
    """Test the 2D gradient calculation with a 2D DataArray and no axes specified."""
    data = test_da_xy.isel(time=0, isobaric=2)
    deriv_y, deriv_x = gradient(data)

    truth_x = xr.full_like(data, -6.993007e-07)
    truth_x.attrs['units'] = 'kelvin / meter'

    truth_y = xr.full_like(data, -2.797203e-06)
    truth_y.attrs['units'] = 'kelvin / meter'

    xr.testing.assert_allclose(deriv_x, truth_x)
    assert deriv_x.metpy.units == truth_x.metpy.units

    xr.testing.assert_allclose(deriv_y, truth_y)
    assert deriv_y.metpy.units == truth_y.metpy.units
コード例 #12
0
def test_gradient_xarray_implicit_axes(test_da_xy):
    """Test the 2D gradient calculation with a 2D DataArray and no axes specified."""
    data = test_da_xy.isel(time=0, isobaric=2)
    deriv_y, deriv_x = gradient(data)

    truth_x = xr.full_like(data, -6.993007e-07)
    truth_x.attrs['units'] = 'kelvin / meter'

    truth_y = xr.full_like(data, -2.797203e-06)
    truth_y.attrs['units'] = 'kelvin / meter'

    xr.testing.assert_allclose(deriv_x, truth_x)
    assert deriv_x.metpy.units == truth_x.metpy.units

    xr.testing.assert_allclose(deriv_y, truth_y)
    assert deriv_y.metpy.units == truth_y.metpy.units
コード例 #13
0
 def thermal_gradient(self, temp_arr_file):
     #read temperature array data
     temp_arr = np.load(temp_arr_file, allow_pickle=True)
     #get the thermal graident, grad: 2D array
     x_delta = 1
     y_delta = 1    
     grad = mpcalc.gradient(temp_arr, deltas=(y_delta, x_delta))
     #plot the gradient 
     fig, ax = plt.subplots()
     im2 = ax.imshow(np.array(grad[0]), cmap='RdGy', vmin=-0.5, vmax=0.5)
     im1 = ax.imshow(np.array(grad[1]), cmap='RdGy', interpolation='nearest', 
                      alpha=.4, vmin=-0.5, vmax=0.5)
     #save the plot
     fig.colorbar(im1)
     fname = os.path.split(temp_arr_file)[1].replace('npy', 'png')
     fig.savefig(fname)
     if os.path.exists(fname):
         print(os.path.abspath(fname))
     else:
         print("file not found")
     plt.close("all")
コード例 #14
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_3d_gradient_2d_data_no_axes(deriv_4d_data):
    """Test for failure of 3D gradient with 2D data and no axes parameter."""
    test = deriv_4d_data[0, 0]
    with pytest.raises(ValueError) as exc:
        gradient(test, deltas=(1, 1, 1))
    assert 'must match the number of dimensions' in str(exc.value)
コード例 #15
0
ファイル: test_calc_tools.py プロジェクト: kenpryor67/MetPy
def test_gradient_4d(deriv_4d_data):
    """Test gradient with 4D arrays."""
    res = gradient(deriv_4d_data, deltas=(1, 1, 1, 1))
    truth = tuple(factor * np.ones_like(deriv_4d_data)
                  for factor in (48., 16., 4., 1.))
    assert_array_almost_equal(res, truth, 8)
コード例 #16
0
index_normal = np.logical_and(index_normal_all.values, index_SPV_normal.values)
index_ninio_normal = np.logical_and(index_ninio_all.values,
                                    index_SPV_normal.values)
index_ninia_normal = np.logical_and(index_ninia_all.values,
                                    index_SPV_normal.values)

hgt = xr.open_dataset(PATH_DATA + FILE_HGT_S4, chunks={'latitude': 10})
hgt = hgt - hgt.mean(dim='longitude')
# retain hgt over ASL region
hgt = hgt.sel(**{
    'latitude': slice(-50, -80),
    'longitude': slice(165, 355)
}).compute()
# get gradient
grad = calc.gradient(hgt.z,
                     axes=[2, 3],
                     coordinates=[hgt.latitude.values, hgt.longitude.values])
#Lap_hgt = calc.laplacian(hgt.z, axes=[2, 3], coordinates=[hgt.latitude.values, hgt.longitude.values])

month = ['Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb']
seas = ['ASO', 'SON', 'OND', 'NDJ', 'DJF']
lat_min = np.empty([7, len(hgt.realiz.values)])
lon_min = np.empty([7, len(hgt.realiz.values)])
lat_max = np.empty([7, len(hgt.realiz.values)])
lon_max = np.empty([7, len(hgt.realiz.values)])
val_min = np.empty([7, len(hgt.realiz.values)])
val_max = np.empty([7, len(hgt.realiz.values)])

for i in np.arange(0, 7):
    for j in np.arange(0, len(hgt.realiz.values)):
        aux = np.where(hgt.z.values[i,
コード例 #17
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_gradient_4d(deriv_4d_data):
    """Test gradient with 4D arrays."""
    res = gradient(deriv_4d_data, deltas=(1, 1, 1, 1))
    truth = tuple(factor * np.ones_like(deriv_4d_data) for factor in (48., 16., 4., 1.))
    assert_array_almost_equal(res, truth, 8)
コード例 #18
0
ファイル: Gradient.py プロジェクト: sartools/MetPy
import metpy.calc as mpcalc
from metpy.units import units

###########################################
# Create some test data to use for our example
data = np.array([[23, 24, 23], [25, 26, 25], [27, 28, 27], [24, 25, 24]
                 ]) * units.degC

# Create an array of x position data (the coordinates of our temperature data)
x = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) * units.kilometer

y = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) * units.kilometer

###########################################
# Calculate the gradient using the coordinates of the data
grad = mpcalc.gradient(data, x=(y, x))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])

###########################################
# It's also possible that we do not have the position of data points, but know
# that they are evenly spaced. We can then specify a scalar delta value for each
# axes.
x_delta = 2 * units.km
y_delta = 1 * units.km
grad = mpcalc.gradient(data, deltas=(y_delta, x_delta))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])

###########################################
# Finally, the deltas can be arrays for unevenly spaced data.
コード例 #19
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_3d_gradient_3d_data_no_axes(deriv_4d_data):
    """Test 3D gradient with 3D data and no axes parameter."""
    test = deriv_4d_data[0]
    res = gradient(test, deltas=(1, 1, 1))
    truth = tuple(factor * np.ones_like(test) for factor in (16., 4., 1.))
    assert_array_almost_equal(res, truth, 8)
コード例 #20
0
ファイル: Gradient.py プロジェクト: akrherz/MetPy
                 [24, 25, 24]]) * units.degC

# Create an array of x position data (the coordinates of our temperature data)
x = np.array([[1, 2, 3],
              [1, 2, 3],
              [1, 2, 3],
              [1, 2, 3]]) * units.kilometer

y = np.array([[1, 1, 1],
              [2, 2, 2],
              [3, 3, 3],
              [4, 4, 4]]) * units.kilometer

###########################################
# Calculate the gradient using the coordinates of the data
grad = mpcalc.gradient(data, coordinates=(y, x))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])

###########################################
# It's also possible that we do not have the position of data points, but know
# that they are evenly spaced. We can then specify a scalar delta value for each
# axes.
x_delta = 2 * units.km
y_delta = 1 * units.km
grad = mpcalc.gradient(data, deltas=(y_delta, x_delta))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])

###########################################
# Finally, the deltas can be arrays for unevenly spaced data.
コード例 #21
0
ファイル: test_calc_tools.py プロジェクト: kenpryor67/MetPy
def test_3d_gradient_3d_data_no_axes(deriv_4d_data):
    """Test 3D gradient with 3D data and no axes parameter."""
    test = deriv_4d_data[0]
    res = gradient(test, deltas=(1, 1, 1))
    truth = tuple(factor * np.ones_like(test) for factor in (16., 4., 1.))
    assert_array_almost_equal(res, truth, 8)
コード例 #22
0
ファイル: test_calc_tools.py プロジェクト: kenpryor67/MetPy
def test_2d_gradient_4d_data_2_axes_2_deltas(deriv_4d_data):
    """Test 2D gradient of 4D data with 2 axes and 2 deltas."""
    res = gradient(deriv_4d_data, deltas=(1, 1), axes=(0, 1))
    truth = tuple(factor * np.ones_like(deriv_4d_data)
                  for factor in (48., 16.))
    assert_array_almost_equal(res, truth, 8)
コード例 #23
0
import metpy.calc as mpcalc
from metpy.units import units

###########################################
# Create some test data to use for our example
data = np.array([[23, 24, 23], [25, 26, 25], [27, 28, 27], [24, 25, 24]
                 ]) * units.degC

# Create an array of x position data (the coordinates of our temperature data)
x = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) * units.kilometer

y = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) * units.kilometer

###########################################
# Calculate the gradient using the coordinates of the data
grad = mpcalc.gradient(data, coordinates=(y, x))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])

###########################################
# It's also possible that we do not have the position of data points, but know
# that they are evenly spaced. We can then specify a scalar delta value for each
# axes.
x_delta = 2 * units.km
y_delta = 1 * units.km
grad = mpcalc.gradient(data, deltas=(y_delta, x_delta))
print('Gradient in y direction: ', grad[0])
print('Gradient in x direction: ', grad[1])

###########################################
# Finally, the deltas can be arrays for unevenly spaced data.
コード例 #24
0
ファイル: test_calc_tools.py プロジェクト: kenpryor67/MetPy
def test_3d_gradient_2d_data_no_axes(deriv_4d_data):
    """Test for failure of 3D gradient with 2D data and no axes parameter."""
    test = deriv_4d_data[0, 0]
    with pytest.raises(ValueError) as exc:
        gradient(test, deltas=(1, 1, 1))
    assert 'must match the number of dimensions' in str(exc.value)
コード例 #25
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_2d_gradient_4d_data_2_axes_2_deltas(deriv_4d_data):
    """Test 2D gradient of 4D data with 2 axes and 2 deltas."""
    res = gradient(deriv_4d_data, deltas=(1, 1), axes=(0, 1))
    truth = tuple(factor * np.ones_like(deriv_4d_data) for factor in (48., 16.))
    assert_array_almost_equal(res, truth, 8)
コード例 #26
0
ファイル: test_calc_tools.py プロジェクト: kenpryor67/MetPy
def test_2d_gradient_4d_data_2_axes_1_deltas(deriv_4d_data):
    """Test for failure of 2D gradient of 4D data with 2 axes and 1 deltas."""
    with pytest.raises(ValueError) as exc:
        gradient(deriv_4d_data, deltas=(1, ), axes=(1, 2))
    assert 'cannot be less than that of "axes"' in str(exc.value)
コード例 #27
0
ファイル: test_calc_tools.py プロジェクト: dopplershift/MetPy
def test_2d_gradient_4d_data_2_axes_1_deltas(deriv_4d_data):
    """Test for failure of 2D gradient of 4D data with 2 axes and 1 deltas."""
    with pytest.raises(ValueError) as exc:
        gradient(deriv_4d_data, deltas=(1, ), axes=(1, 2))
    assert 'cannot be less than that of "axes"' in str(exc.value)