def image(self, state): if not self.valid(state): return gridded_forecast.empty_image() try: path, pts = self.locator.locate(self.pattern, state.variable, state.initial_time, state.valid_time, state.pressure) except SearchFail: print("Search failed: {}".format(self.pattern)) return gridded_forecast.empty_image() print(path, pts) units = self.read_units(path, state.variable) data = load_image_pts(path, state.variable, pts, pts) if (len(state.pressures) > 0) and (state.pressure is not None): level = "{} hPa".format(int(state.pressure)) else: level = "Surface" data.update( gridded_forecast.coordinates(state.valid_time, state.initial_time, state.pressures, state.pressure)) data["name"] = [self.name] data["units"] = [units] return data
def image(self, state): '''gets actual data. X and Y passed to :meth:`geo.stretch_image` must be 1D arrays. NWCSAF data are not on a regular grid so must be regridded. `values` passed to :meth:`geo.stretch_image` must be a NumPy Masked Array. :param state: Bokeh State object of info from UI :returns: Output data from :meth:`geo.stretch_image`''' data = empty_image() for nc in self.locator._sets: if str( datetime.datetime.strptime( nc.nominal_product_time.replace('Z', 'UTC'), '%Y-%m-%dT%H:%M:%S%Z') ) == state.valid_time and self.locator.varlist[ state.variable] in nc.variables: #regrid to regular grid x = nc['lon'][:].flatten() # lat & lon both 2D arrays y = nc['lat'][:].flatten() # z = nc[self.locator.varlist[state.variable]][:].flatten() #define grid xi, yi = np.meshgrid( np.linspace(x.min(), x.max(), nc.dimensions['nx'].size), np.linspace(y.min(), y.max(), nc.dimensions['ny'].size), ) zi = griddata(np.array([x, y]).transpose(), z, (xi, yi), method='linear', fill_value=np.nan) zi = np.ma.masked_invalid(zi, copy=False) zi = np.ma.masked_outside( zi, nc[self.locator.varlist[state.variable]].valid_range[0], nc[self.locator.varlist[state.variable]].valid_range[1], copy=False) data = geo.stretch_image(xi[0, :], yi[:, 0], zi) #data = geo.stretch_image(x[0,:], y[:,0], nc[state.variable][:]) data.update( coordinates(state.valid_time, state.initial_time, state.pressures, state.pressure)) data.update({ 'name': [str(nc[self.locator.varlist[state.variable]].long_name)], }) if 'units' in nc[self.locator.varlist[ state.variable]].ncattrs(): data.update({ 'units': [str(nc[self.locator.varlist[state.variable]].units)] }) return data
def test_surface_and_times(self, to_datetime): valid = datetime(2019, 10, 10, 9) initial = datetime(2019, 10, 10, 3) to_datetime.side_effect = [valid, initial] result = gridded_forecast.coordinates(sentinel.valid, sentinel.initial, [], None) self.assertEqual(to_datetime.mock_calls, [call(sentinel.valid), call(sentinel.initial)]) self.assertEqual( result, { 'valid': [valid], 'initial': [initial], 'length': ['T+6'], 'level': ['Surface'] })
def image(self, state): """ Main image loading function. This function will actually realise the data, """ if self.variable_id != state.variable: self.variable_id = state.variable self._cube = None valid_time = state.valid_time pressure = state.pressure selected_time = gridded_forecast._to_datetime(valid_time) # the guts of creating the bokeh object has been put into a separate # function so that it can be cached, so if image is called multiple # time the calculations are only done once (hopefully). cube = self.cube coord_names = [c1.name() for c1 in cube.coords()] if 'air_pressure' in coord_names and pressure is None: data = gridded_forecast.empty_image() return data data = _get_bokeh_image(cube, self.experiment_id, self.variable_id, self.institution_id, state.initial_time, self.member_id, selected_time, pressure) data.update(gridded_forecast.coordinates(str(selected_time), state.initial_time, state.pressures, pressure)) data.update({ 'name': [self._label], 'units': [str(cube.units)], 'experiment': [self.experiment_id], 'institution': [self.institution_id], 'memberid': [self.member_id], 'variableid': [self.variable_id] }) return data
def test_pressure(self, to_datetime): result = gridded_forecast.coordinates(None, None, [1000, 900], 900) self.assertEqual(result['level'], ['900 hPa'])
def test_surface_no_pressure(self, to_datetime): result = gridded_forecast.coordinates(None, None, [1000, 900], None) self.assertEqual(result['level'], ['Surface'])