def test_log_interp_no_extrap(): """Test interpolating with log x-scale setting out of bounds value error.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([1e7]) with pytest.raises(ValueError): log_interp(x_interp, x_log, y_log, fill_value=None)
def test_log_interp_3d(): """Test interpolating with log x-scale 3 dimensions along second axis.""" x_log = np.ones((3, 4, 3)) * np.array([1e3, 1e4, 1e5, 1e6]).reshape(-1, 1) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interp(x_interp, x_log, y_log, axis=1) assert_array_almost_equal(y_interp[0, :, 0], y_interp_truth, 7)
def test_log_interp_4d(): """Test interpolating with log x-scale 4 dimensions.""" x_log = np.ones((2, 2, 3, 4)) * np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interp(x_interp, x_log, y_log, axis=3) assert_array_almost_equal(y_interp[0, 0, 0, :], y_interp_truth, 7)
def test_log_interp_units(): """Test interpolating with log x-scale with units.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) * units.hPa y_log = (np.log(x_log.m) * 2 + 3) * units.degC x_interp = np.array([5e5, 5e6, 5e7]) * units.Pa y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) * units.degC y_interp = log_interp(x_interp, x_log, y_log) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_log_interp_2d(): """Test interpolating with log x-scale in 2 dimensions.""" x_log = np.array([[1e3, 1e4, 1e5, 1e6], [1e3, 1e4, 1e5, 1e6]]) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interp(x_interp, x_log, y_log, axis=1) assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
def test_log_interp(): """Test deprecated log_interp function.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interp(x_interp, x_log, y_log) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_log_interp_set_nan_below(): """Test interpolating with log x-scale setting out of bounds below data to nan.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 x_interp = 1e2 y_interp_truth = np.nan with pytest.warns(Warning): y_interp = log_interp(x_interp, x_log, y_log) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_log_interp(): """Test deprecated log_interp function.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) with pytest.warns(MetpyDeprecationWarning): y_interp = log_interp(x_interp, x_log, y_log) assert_array_almost_equal(y_interp, y_interp_truth, 7)
def test_log_interp_2args(): """Test interpolating with log x-scale with 2 arguments.""" x_log = np.array([1e3, 1e4, 1e5, 1e6]) y_log = np.log(x_log) * 2 + 3 y_log2 = np.log(x_log) * 2 + 3 x_interp = np.array([5e3, 5e4, 5e5]) y_interp_truth = np.array([20.0343863828, 24.6395565688, 29.2447267548]) y_interp = log_interp(x_interp, x_log, y_log, y_log2) assert_array_almost_equal(y_interp[1], y_interp_truth, 7) assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
def get_isobaric_variables(data, var_list, plevs, outfile, dtype, compression, complevel): """Gets isobaric variables from a list""" # use wrf-python to get data for each of the variables var_def = get_variables() # get pressure array and attach units p = getvar(data, 'p', ALL_TIMES) p_np = np.array(p.data) * units(p.units) for i in range(len(var_list)): name = var_list[i] var_data = getvar(data, var_def[name][2], ALL_TIMES) iso_data = log_interp(plevs, p_np, var_data.data, axis=1) # write each of the variables to the output file pres_data = outfile.createVariable( var_list[i], dtype, ('time', 'pressure_levels', 'lat', 'lon'), zlib=compression, complevel=complevel) pres_data.units = var_data.units if var_list[i] == 'height': pres_data.decription = 'height [MSL] of isobaric surfaces' elif var_list[i] == 'uwnd': pres_data.description = 'u-wind component on isobaric surfaces' elif var_list[i] == 'vwnd': pres_data.description = 'v-wind component on isobaric surfaces' elif var_list[i] == 'wwnd': pres_data.description = 'w-wind component on isobaric surfaces' elif var_list[i] == 'temp': pres_data.description = 'temperature on isobaric surfaces' elif var_list[i] == 'dewpt': pres_data.description = 'dewpoint temperature on isobaric surfaces' elif var_list[i] == 'avor': pres_data.description = 'absolute vorticity on isobaric surfaces' else: pres_data.description = var_data.description pres_data[:] = iso_data
hgt = data.variables['height'][:] * units(data.variables['height'].units) #################################### # Array of desired pressure levels plevs = [700.] * units.hPa ##################################### # **Interpolate The Data** # # Now that the data is ready, we can interpolate to the new isobaric levels. The data is # interpolated from the irregular pressure values for each sigma level to the new input # mandatory isobaric levels. `mcalc.log_interp` will interpolate over a specified dimension # with the `axis` argument. In this case, `axis=1` will correspond to interpolation on the # vertical axis. isobaric_levels = mcalc.log_interp(plevs, pres, hgt, temperature, axis=1) #################################### # The interpolated data is output in a list, so we will pull out each variable for plotting. height = isobaric_levels[0] temp = isobaric_levels[1] #################################### # **Plotting the Data for 700 hPa.** # Set up our projection crs = ccrs.LambertConformal(central_longitude=-100.0, central_latitude=45.0) # Set up our array of latitude and longitude values and transform to # the desired projection.
#################################### # Array of desired pressure levels plevs = [700.] * units.hPa ##################################### # **Interpolate The Data** # # Now that the data is ready, we can interpolate to the new isobaric levels. The data is # interpolated from the irregular pressure values for each sigma level to the new input # mandatory isobaric levels. `mpcalc.log_interp` will interpolate over a specified dimension # with the `axis` argument. In this case, `axis=1` will correspond to interpolation on the # vertical axis. The interpolated data is output in a list, so we will pull out each # variable for plotting. height, temp = mpcalc.log_interp(plevs, pres, hgt, temperature, axis=1) #################################### # **Plotting the Data for 700 hPa.** # Set up our projection crs = ccrs.LambertConformal(central_longitude=-100.0, central_latitude=45.0) # Set up our array of latitude and longitude values and transform to # the desired projection. tlatlons = crs.transform_points(ccrs.PlateCarree(), lon, lat) tlons = tlatlons[:, :, 0] tlats = tlatlons[:, :, 1] # Set the forecast hour FH = 1