def image(self, state): forecast_reference_time = to_datetime(state.initial_time) forecast_period = to_datetime( state.valid_time) - forecast_reference_time data_array = self.xarray_dataset[state.variable] try: data_array = data_array.sel( forecast_reference_time=forecast_reference_time, forecast_period=forecast_period, ) print(data_array) except KeyError as error: print(f"Caught KeyError: {error}") return self.empty() x = np.ma.masked_invalid(data_array.longitude)[:] y = np.ma.masked_invalid(data_array.latitude)[:] z = np.ma.masked_invalid(data_array)[:] if z.mask.all(): return self.empty() else: bokeh_data = forest.geo.stretch_image(x, y, z) bokeh_data.update( self.metadata( self.label, state.variable, getattr(data_array, "units", ""), forecast_reference_time, forecast_period, data_array.height, )) print(bokeh_data) return bokeh_data
def valid_times(self, pattern, variable, initial_time): if self.variable_id != variable: self.variable_id = variable self._cube = None self._parse_pattern(pattern) cube = self.cube valid_times = [ util.to_datetime(cell.point) for cell in cube.coord('time').cells() ] return valid_times
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 = util.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 initial_times(self, pattern, variable=None): self._parse_pattern(pattern) cube = self.cube for cell in cube.coord('time').cells(): init_time = util.to_datetime(cell.point) return [init_time]
def time_comp(select_time, time_cell): # data_time = util.to_datetime(time_cell.point) if abs((select_time - data_time).days) < 2: return True return False
def test_unsupported(self): with self.assertRaisesRegex(Exception, 'Unknown value'): util.to_datetime(12)
def test_datetime(self): now = dt.datetime.now() result = util.to_datetime(now) self.assertEqual(result, now)
def test__to_datetime(given, expect): assert util.to_datetime(given) == expect