예제 #1
0
def test_interp_end_point():
    """Test interpolation with point at data endpoints."""
    x = np.array([1., 2., 3., 4.])
    y = x
    x_interp = np.array([1.0, 4.0])
    y_interp_truth = np.array([1.0, 4.0])
    y_interp = interp(x_interp, x, y)
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
예제 #2
0
def test_interp_decrease():
    """Test interpolation with decreasing interpolation points."""
    x = np.array([1., 2., 3., 4.])
    y = x
    x_interp = np.array([3.5000000, 2.5000000])
    y_interp_truth = np.array([3.5000000, 2.5000000])
    y_interp = interp(x_interp, x, y)
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
예제 #3
0
def test_interp_decrease_xp():
    """Test interpolation with decreasing order."""
    x = np.array([4., 3., 2., 1.])
    y = x
    x_interp = np.array([3.5000000, 2.5000000])
    y_interp_truth = np.array([3.5000000, 2.5000000])
    y_interp = interp(x_interp, x, y)
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
예제 #4
0
def test_interp():
    """Test deprecated interp function."""
    x = np.array([1., 2., 3., 4.])
    y = x
    x_interp = np.array([3.5000000, 2.5000000])
    y_interp_truth = np.array([3.5000000, 2.5000000])
    y_interp = interp(x_interp, x, y)
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
예제 #5
0
def test_interp_masked_units():
    """Test interpolating with masked arrays with units."""
    x = np.ma.array([1., 2., 3., 4.]) * units.m
    y = np.ma.array([50., 60., 70., 80.]) * units.degC
    x_interp = np.array([250., 350.]) * units.cm
    y_interp_truth = np.array([65., 75.]) * units.degC
    y_interp = interp(x_interp, x, y)
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
예제 #6
0
def test_interp():
    """Test deprecated interp function."""
    x = np.array([1., 2., 3., 4.])
    y = x
    x_interp = np.array([3.5000000, 2.5000000])
    y_interp_truth = np.array([3.5000000, 2.5000000])
    with pytest.warns(MetpyDeprecationWarning):
        y_interp = interp(x_interp, x, y)
    assert_array_almost_equal(y_interp, y_interp_truth, 7)
예제 #7
0
def test_interp_2args():
    """Test interpolation with 2 arguments."""
    x = np.array([1., 2., 3., 4.])
    y = x
    y2 = x
    x_interp = np.array([2.5000000, 3.5000000])
    y_interp_truth = np.array([2.5000000, 3.5000000])
    y_interp = interp(x_interp, x, y, y2)
    assert_array_almost_equal(y_interp[0], y_interp_truth, 7)
    assert_array_almost_equal(y_interp[1], y_interp_truth, 7)
예제 #8
0
def upper_air(config,
              station_id,
              sounding_station_id,
              date,
              use_nan_sounding=False,
              use_existing=True,
              save=True):
    """
    Retrieves upper-air data and interpolates to pressure levels. If use_nan_sounding is True, then if a retrieval
    error occurs, a blank sounding will be returned instead of an error.
    :param config:
    :param station_id: station ID of surface station used
    :param sounding_station_id: station ID of sounding station to use
    :param date: datetime
    :param use_nan_sounding: bool: if True, use sounding of NaNs instead of raising an error
    :param use_existing: bool: preferentially use existing soundings in sounding_data_dir
    :param save: bool: if True, save processed soundings to sounding_data_dir
    :return:
    """
    variables = ['height', 'temperature', 'dewpoint', 'u_wind', 'v_wind']

    # Define levels for interpolation: same as model data, except omitting lowest_p_level
    plevs = [600, 750, 850, 925]
    pres_interp = np.array([p for p in plevs if p <= config['lowest_p_level']])

    # Try retrieving the sounding, first checking for existing
    if config['verbose']:
        print('upper_air: retrieving sounding for %s' %
              datetime.strftime(date, '%Y%m%d%H'))
    nan_sounding = False
    retrieve_sounding = False
    sndg_data_dir = config['Obs']['sounding_data_dir']
    if not (os.path.isdir(sndg_data_dir)):
        os.makedirs(sndg_data_dir)
    sndg_file = '%s/%s_SNDG_%s.pkl' % (sndg_data_dir, station_id,
                                       datetime.strftime(date, '%Y%m%d%H'))
    if use_existing:
        try:
            data = read_pkl(sndg_file)
            if config['verbose']:
                print('    Read from file.')
        except:
            retrieve_sounding = True
    else:
        retrieve_sounding = True
    if retrieve_sounding:
        try:
            dset = WyomingUpperAir.request_data(
                date, config['Obs']['sounding_station_id'])
        except:
            # Try again
            try:
                dset = WyomingUpperAir.request_data(
                    date, config['Obs']['sounding_station_id'])
            except:
                if use_nan_sounding:
                    if config['verbose']:
                        print(
                            'upper_air: warning: unable to retrieve sounding; using nan.'
                        )
                    nan_sounding = True
                else:
                    raise ValueError('error retrieving sounding for %s' % date)

        # Retrieve pressure for interpolation to fixed levels
        if not nan_sounding:
            pressure = dset.variables['pressure']
            pres = np.array([p.magnitude
                             for p in list(pressure)])  # units are hPa

        # Get variables and interpolate; add to dictionary
        data = OrderedDict()
        for var in variables:
            if not nan_sounding:
                var_data = dset.variables[var]
                var_array = np.array([v.magnitude for v in list(var_data)])
                var_interp = interp(pres_interp, pres, var_array)
                data[var] = var_interp.tolist()
            else:
                data[var] = [np.nan] * len(pres_interp)

        # Save
        if save and not nan_sounding:
            with open(sndg_file, 'wb') as handle:
                pickle.dump(data, handle, protocol=2)

    return data