def example_spot_ensemble_velocity(): import slocum.query.utils as query_utils n = 17 k = 3 fcst = xray.Dataset() times = pd.date_range('2015-01-03 06:00:00Z', periods=n, freq='6h').values fcst['time'] = ('time', times) fcst['latitude'] = ('latitude', [-8]) fcst['longitude'] = ('longitude', [115]) fcst['wind_speed'] = (('time', 'realization', 'longitude', 'latitude'), np.zeros((n, k, 1, 1)), {'units': 'knots'}) fcst['wind_from_direction'] = (('time', 'realization', 'longitude', 'latitude'), np.zeros((n, k, 1, 1)), {'units': 'radians'}) fcst['wind_speed'].values[:, 0, 0, 0] = np.arange(n) fcst['wind_speed'].values[:, 1, 0, 0] = n - 1 - np.arange(n) fcst['wind_speed'].values[:, 2, 0, 0] = np.ones(n) fcst['wind_from_direction'].values[:, 0, 0, 0] = np.linspace(0., 2 * np.pi, n) fcst['wind_from_direction'].values[:, 1, 0, 0] = np.linspace(2 * np.pi, 0, n) fcst['wind_from_direction'].values[:, 2, 0, 0] = np.pi * np.ones(n) spot_plot = spot_velocity(fcst, query_utils.get_variable('wind')) plt.show()
def decompress_dataset(payload): """ Unpacks a dataset that has been packed using compress_dataset() """ payload = zlib.decompress(payload) version = np.fromstring(payload[0], dtype=np.uint8)[0] payload = payload[1:] if version > _VERSION: raise ValueError("The forecast was compressed using a" "newer version than the version currently " "installed. Consider upgrading slocum") elif version < _VERSION: # TODO: Allow queries to specify the version, so that users # with older versions can request forecasts they can still read. raise NotImplementedError("Backward comaptibility is not currently " "supported. Your version of slocum is newer " "than the server, consider rolling back") # this iterates through the payload and yields individual variables output = xray.Dataset() while len(payload): var_name, packed, payload = _split_single_variable(payload) variable = utils.get_variable(var_name) output.update(variable.decompress(packed, output), inplace=True) logging.debug("Decoded %s" % var_name) return xray.decode_cf(output)
def plot_interactive_forecast(fcst, variable_name=None, **kwdargs): variable_name = variable_name or utils.infer_variable(fcst) variable = query_utils.get_variable(variable_name) if isinstance(variable, schemes.VelocityVariable): interactive.InteractiveVelocity(fcst, variable, **kwdargs) else: raise NotImplementedError("Only support velocity variables at " "the moment.")
def test_roundtrip(self): ds = create_ensemble_data() for vn in utils.available_variables(ds): v = utils.get_variable(vn) orig = ds.copy(deep=True) expected = v.decompress(v.compress(ds), ds) # make sure two passes yields the same result actual = v.decompress(v.compress(expected), ds) actual.identical(expected)
def main(): fcst = test_data() fcst = compress.decompress_dataset(compress.compress_dataset(fcst)) for vn in ['current', 'wind']: variable = query_utils.get_variable(vn) if isinstance(variable, schemes.VelocityVariable): interactive.InteractiveVelocity(fcst, variable) plt.show()
def plot_gridded_forecast(fcst, variable_name=None, **kwdargs): """ Creates an interactive plot of a gridded forecast. """ variable_name = variable_name or utils.infer_variable(fcst) variable = query_utils.get_variable(variable_name) if isinstance(variable, schemes.VelocityVariable): velocity.VelocityField(fcst, variable, **kwdargs) else: raise NotImplementedError("Only support velocity variables at " "the moment.")
def plot_spot(fcst, variable_name=None): """ Takes a forecast that is assumed to hold a spot forecast and infers the type of variable then uses the corresponding plot utilities. """ variable_name = variable_name or utils.infer_variable(fcst) variable = query_utils.get_variable(variable_name) if isinstance(variable, schemes.VelocityVariable): spot.spot_velocity(fcst, variable) else: raise NotImplementedError("Only support velocity variables at " "the moment.")
def example_spot_single_velocity(): import slocum.query.utils as query_utils n = 8 fcst = xray.Dataset() times = pd.date_range('2015-01-03 06:00:00Z', periods=n, freq='6h').values fcst['time'] = ('time', times) fcst['latitude'] = ('latitude', [-8]) fcst['longitude'] = ('longitude', [115]) fcst['wind_speed'] = (('time', 'longitude', 'latitude'), np.arange(float(n)).reshape((n, 1, 1)), {'units': 'knots'}) fcst['wind_from_direction'] = (('time', 'longitude', 'latitude'), np.linspace(-np.pi, np.pi, n).reshape((n, 1, 1)), {'units': 'radians'}) spot_plot = spot_velocity(fcst, query_utils.get_variable('wind')) plt.show()