def _add_iris_coord(cube, name, points, dim, calendar=None): """ Add a Coord to a Cube from a Pandas index or columns array. If no calendar is specified for a time series, Gregorian is assumed. """ units = Unit("unknown") if calendar is None: calendar = iris.unit.CALENDAR_GREGORIAN # Convert pandas datetime objects to python datetime obejcts. if isinstance(points, pandas.tseries.index.DatetimeIndex): points = np.array([i.to_datetime() for i in points]) # Convert datetime objects to Iris' current datetime representation. if points.dtype == object: dt_types = (datetime.datetime, netcdftime.datetime) if all([isinstance(i, dt_types) for i in points]): units = Unit("hours since epoch", calendar=calendar) points = units.date2num(points) points = np.array(points) if (np.issubdtype(points.dtype, np.number) and iris.util.monotonic(points, strict=True)): coord = DimCoord(points, units=units) coord.rename(name) cube.add_dim_coord(coord, dim) else: coord = AuxCoord(points, units=units) coord.rename(name) cube.add_aux_coord(coord, dim)
def setUp(self): self.s = mock.Mock(units=Unit('1'), nbounds=0) self.c = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.eta = mock.Mock(units=Unit('m'), nbounds=0) self.depth = mock.Mock(units=Unit('m'), nbounds=0) self.depth_c = mock.Mock(units=Unit('m'), nbounds=0, shape=(1, )) self.kwargs = dict(s=self.s, c=self.c, eta=self.eta, depth=self.depth, depth_c=self.depth_c)
def test_gregorian_calendar_conversion_setup(self): # Reproduces a situation where a unit's gregorian calendar would not # match (using the `is` operator) to the literal string 'gregorian', # causing an `is not` test to return a false negative. cal_str = iris.unit.CALENDAR_GREGORIAN calendar = self.MyStr(cal_str) self.assertIsNot(calendar, cal_str) u1 = Unit('hours since 1970-01-01 00:00:00', calendar=calendar) u2 = Unit('hours since 1969-11-30 00:00:00', calendar=calendar) u1point = np.array([8.], dtype=np.float32) expected = np.array([776.], dtype=np.float32) result = u1.convert(u1point, u2) return expected, result
def setUp(self): self.section = { 'year': 2007, 'month': 1, 'day': 15, 'hour': 0, 'minute': 3, 'second': 0 } self.unit = Unit('hours since epoch', calendar=CALENDAR_GREGORIAN) dt = datetime(self.section['year'], self.section['month'], self.section['day'], self.section['hour'], self.section['minute'], self.section['second']) self.point = self.unit.date2num(dt)
def add_bounded_time_coords(aux_coords_and_dims, grib): t_bounds = grib.phenomenon_bounds('hours') period = Unit('hours').convert(t_bounds[1] - t_bounds[0], grib._forecastTimeUnit) aux_coords_and_dims.append( (DimCoord(standard_name='forecast_period', units=grib._forecastTimeUnit, points=grib._forecastTime + 0.5 * period, bounds=[grib._forecastTime, grib._forecastTime + period]), None)) aux_coords_and_dims.append( (DimCoord(standard_name='time', units=Unit('hours since epoch', CALENDAR_GREGORIAN), points=0.5 * (t_bounds[0] + t_bounds[1]), bounds=t_bounds), None))
def setUp(self): self.fp = DimCoord(5, standard_name='forecast_period', units='hours') self.fp_test_bounds = np.array([[1.0, 9.0]]) self.unit = Unit('hours since epoch') self.frt = DimCoord(10, standard_name='forecast_reference_time', units=self.unit)
def setUp(self): nt = 10 data = np.arange(nt, dtype=np.float32) cube = Cube(data, standard_name='air_temperature', units='K') # Temporal coordinate. t_units = Unit('hours since 1970-01-01 00:00:00', calendar='gregorian') t_coord = DimCoord(points=np.arange(nt), standard_name='time', units=t_units) cube.add_dim_coord(t_coord, 0) # Increasing 1D time-series cube. self.series_inc_cube = cube self.series_inc = CubeSignature(self.series_inc_cube) # Decreasing 1D time-series cube. self.series_dec_cube = self.series_inc_cube.copy() self.series_dec_cube.remove_coord('time') t_tmp = DimCoord(points=t_coord.points[::-1], standard_name='time', units=t_units) self.series_dec_cube.add_dim_coord(t_tmp, 0) self.series_dec = CubeSignature(self.series_dec_cube) # Scalar 0D time-series cube with scalar time coordinate. cube = Cube(0, standard_name='air_temperature', units='K') cube.add_aux_coord( DimCoord(points=nt, standard_name='time', units=t_units)) self.scalar_cube = cube
def make_cube(self, calendar): n_times = 10 cube = Cube(np.arange(n_times)) time_coord = DimCoord(np.arange(n_times), standard_name='time', units=Unit('days since 1980-12-25', calendar=calendar)) cube.add_dim_coord(time_coord, 0) return cube
def test_non_scalar(self): coord = DimCoord([0, 1], 'time', bounds=[[0, 1], [1, 2]], units=Unit('hours since epoch', calendar='standard')) with self.assertRaisesRegexp( ValueError, 'Expected length one time ' 'coordinate, got 2 points'): set_time_range(coord, mock.sentinel.grib)
def setUp(self): self.cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. self.cube.rename('air_temperature') coord = DimCoord(23, 'time', bounds=[0, 100], units=Unit('days since epoch', calendar='standard')) self.cube.add_aux_coord(coord)
def add_sonde_metadata(cubelist_original, a=6371229.0): # metadata for these cubes is stored as attributes # this function converts it to coordinates as in the UKMO # a is the radius of earth as read from the GeogCS used for lat & lon with the ukmo data I am looking at #cubelist = cubelist_original.copy() cubelist = cubelist_original cube = cubelist[0] # all cubes in cubelist should have the same metadata in this regard T = cube.attributes['launchTime'] # string detailing the time of launch year = int(T[:4]) month = int(T[5:7]) day = int((T[8:10])) hour = int((T[11:13])) # reads times from string t = np.mod(hour, 6) # hours after one of the 6-hourly verification times, 00, 06, 12 or 18 UTC time_release = datetime.datetime(year, month, day, hour) # sonde relese time time_mod = time_release + datetime.timedelta(hours=(3 - np.abs(t - 3)) * np.sign(t - 2.5)) # create datetime object rounded to the nearest verification time # subtracts (t<3) or adds (t>=3) hours to nearest 6 # if this doesn't make sense to you get a pen & paper and work it out time_elapsed = time_mod - datetime.datetime(1970, 1, 1) # convert to appropriate units t_hours = float(time_elapsed.days * 24 + time_elapsed.seconds / (60 * 60)) # convert this list of strings into an interpretable object tm = iris.coords.DimCoord(t_hours, standard_name='time', units=Unit('hours since 1970-01-01 00:00:00', calendar='gregorian')) # create time coordinate # lat = iris.coords.DimCoord(np.array(round(cube.attributes['stationLatitude'],1)), standard_name = 'latitude', units = 'degrees', coord_system=GeogCS(a)) # lon = iris.coords.DimCoord(np.array(round(cube.attributes['stationLongitude'],1)), standard_name = 'longitude', units = 'degrees', coord_system=GeogCS(a)) # these are now no longer desired and will be replaced by cubes # make scalar cube of time_release, and add this to cubelist cubelist.append(make_time_cube(time_release, cube)) for cube in cubelist: cube.add_aux_coord(tm) # and delete 'launchTime' attribute, now that this is a coordinate del cube.attributes['launchTime'] # this will allow cubes to be merged along the time dimension return cubelist
def test_days(self, mock_set): lower = 4 upper = 6 self.coord.bounds = [lower, upper] self.coord.units = Unit('days since epoch', calendar='standard') set_time_range(self.coord, mock.sentinel.grib) mock_set.assert_any_call(mock.sentinel.grib, 'indicatorOfUnitForTimeRange', 1) mock_set.assert_any_call(mock.sentinel.grib, 'lengthOfTimeRange', (upper - lower) * 24)
def add_ECAN_metadata(filepath, filename, cubelist_original, a=6371229.0): # metadata for these cubes is stored in an annoying format # this function converts it to that identical to the UKMO # a is the radius of earth as read from the GeogCS used for lat & lon with the ukmo data I am looking at #cubelist = cubelist_original.copy() cubelist = cubelist_original metalist = iris.load(filepath + filename, ['AN_TIME', 'LAT', 'LON']) T = metalist[0].data # list of strings detailing the time of launch year = int(T[0] + T[1] + T[2] + T[3]) month = int(T[4] + T[5]) day = int((T[6] + T[7])) hour = int((T[9] + T[10])) # reads times from string time = datetime.datetime(year, month, day, hour) - datetime.datetime( 1970, 1, 1) t_hours = float(time.days * 24 + time.seconds / (60 * 60)) # convert this list of strings into an interpretable object tm = iris.coords.DimCoord(t_hours, standard_name='time', units=Unit('hours since 1970-01-01 00:00:00', calendar='gregorian')) # create time coordinate # lat = iris.coords.DimCoord(round(metalist[1].data, 1), standard_name = 'latitude', units = 'degrees', coord_system=GeogCS(a)) # lon = iris.coords.DimCoord(round(metalist[2].data, 1), standard_name = 'longitude', units = 'degrees', coord_system=GeogCS(a)) # no longer desired and replaced by cubes for cube in cubelist: cube.add_aux_coord(tm) cube.attributes['source'] = 'ECMWF Analysis Data' lat = iris.cube.Cube(metalist[1].data, standard_name='latitude', units='degrees', attributes=cube.attributes, aux_coords_and_dims=[(tm, None)]) lon = iris.cube.Cube(metalist[2].data, standard_name='longitude', units='degrees', attributes=cube.attributes, aux_coords_and_dims=[(tm, None)]) # add latitude and longitude as scalar cubes cubelist.append(lat) cubelist.append(lon) return cubelist
def test_multiple_points(self, mock_set): # Add time coord with multiple points. coord = DimCoord([23, 24, 25], 'time', bounds=[[22, 23], [23, 24], [24, 25]], units=Unit('days since epoch', calendar='standard')) self.cube.add_aux_coord(coord, 0) with self.assertRaisesRegexp(ValueError, 'Expected length one time coordinate'): _product_definition_template_8_and_11(self.cube, mock.sentinel.grib)
def test_no_bounds(self, mock_set): # Add time coord with no bounds. coord = DimCoord(23, 'time', units=Unit('days since epoch', calendar='standard')) self.cube.add_aux_coord(coord) with self.assertRaisesRegexp( ValueError, 'Expected time coordinate with two bounds, ' 'got 0 bounds'): _product_definition_template_8_and_11(self.cube, mock.sentinel.grib)
def setUp(self): self.unit_by_indicator = { 0: Unit('minutes'), 1: Unit('hours'), 2: Unit('days'), 10: Unit('3 hours'), 11: Unit('6 hours'), 12: Unit('12 hours'), 13: Unit('seconds') }
def setUp(self): self.section = {'year': 2007, 'month': 1, 'day': 15, 'hour': 0, 'minute': 3, 'second': 0} self.unit = Unit('hours since epoch', calendar=CALENDAR_GREGORIAN) dt = datetime(self.section['year'], self.section['month'], self.section['day'], self.section['hour'], self.section['minute'], self.section['second']) self.point = self.unit.date2num(dt)
def test_other_cell_methods(self, mock_set): cube = stock.lat_lon_cube() # Rename cube to avoid warning about unknown discipline/parameter. cube.rename('air_temperature') coord = DimCoord(23, 'time', bounds=[0, 24], units=Unit('hours since epoch')) cube.add_aux_coord(coord) # Add one time cell method and another unrelated one. cell_method = CellMethod(method='mean', coords=['elephants']) cube.add_cell_method(cell_method) cell_method = CellMethod(method='sum', coords=['time']) cube.add_cell_method(cell_method) _product_definition_template_8_and_11(cube, mock.sentinel.grib) mock_set.assert_any_call(mock.sentinel.grib, 'numberOfTimeRange', 1)
def test_360_day_calendar(self, mock_set): cube = self.cube # End bound is 1972-05-07 10:27:07 coord = DimCoord(23.0, 'time', bounds=[0.452, 20314.452], units=Unit('hours since epoch', calendar='360_day')) cube.add_aux_coord(coord) grib = mock.sentinel.grib _product_definition_template_8_and_11(cube, grib) mock_set.assert_any_call(grib, "yearOfEndOfOverallTimeInterval", 1972) mock_set.assert_any_call(grib, "monthOfEndOfOverallTimeInterval", 5) mock_set.assert_any_call(grib, "dayOfEndOfOverallTimeInterval", 7) mock_set.assert_any_call(grib, "hourOfEndOfOverallTimeInterval", 10) mock_set.assert_any_call(grib, "minuteOfEndOfOverallTimeInterval", 27) mock_set.assert_any_call(grib, "secondOfEndOfOverallTimeInterval", 7)
def make_time_cube(time, cube): """ Make scalar cube of datetime object :param time: datetime object :param cube: arbitrary cube :return: scalar cube """ time_elapsed = time - datetime.datetime(1970, 1, 1) # convert to appropriate units t_hours = float(time_elapsed.days * 24 + time_elapsed.seconds / (60 * 60)) # convert this list of strings into an interpretable object # return scalar cube of time return iris.cube.Cube(t_hours, standard_name='time', units=Unit('hours since 1970-01-01 00:00:00', calendar='gregorian'), attributes=cube.attributes)
def test_basic(self): result = translate_phenomenon(self.metadata, None, None, None, probability=self.probability) # Check metadata. thresh_coord = DimCoord([22.0], standard_name='air_temperature', long_name='', units='K') self.assertEqual( self.metadata, { 'standard_name': None, 'long_name': 'probability_of_air_temperature_<prob_type>', 'units': Unit(1), 'aux_coords_and_dims': [(thresh_coord, None)] })
def setUp(self): self.sigma = mock.Mock(units=Unit('1'), nbounds=0) self.eta = mock.Mock(units=Unit('m'), nbounds=0) self.depth = mock.Mock(units=Unit('m'), nbounds=0) self.depth_c = mock.Mock(units=Unit('m'), nbounds=0, shape=(1, )) self.nsigma = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.zlev = mock.Mock(units=Unit('m'), nbounds=0) self.kwargs = dict(sigma=self.sigma, eta=self.eta, depth=self.depth, depth_c=self.depth_c, nsigma=self.nsigma, zlev=self.zlev)
def setUp(self): self.s = mock.Mock(units=Unit('1'), nbounds=0) self.eta = mock.Mock(units=Unit('m'), nbounds=0) self.depth = mock.Mock(units=Unit('m'), nbounds=0) self.a = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.b = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.depth_c = mock.Mock(units=Unit('m'), nbounds=0, shape=(1, )) self.kwargs = dict(s=self.s, eta=self.eta, depth=self.depth, a=self.a, b=self.b, depth_c=self.depth_c) self.factory = OceanSFactory(**self.kwargs)
class Test(tests.IrisTest): def setUp(self): self.section = { 'year': 2007, 'month': 1, 'day': 15, 'hour': 0, 'minute': 3, 'second': 0 } self.unit = Unit('hours since epoch', calendar=CALENDAR_GREGORIAN) dt = datetime(self.section['year'], self.section['month'], self.section['day'], self.section['hour'], self.section['minute'], self.section['second']) self.point = self.unit.date2num(dt) def _check(self, section, standard_name=None): expected = DimCoord(self.point, standard_name=standard_name, units=self.unit) # The call being tested. coord = reference_time_coord(section) self.assertEqual(coord, expected) def test_start_of_forecast(self): section = deepcopy(self.section) section['significanceOfReferenceTime'] = 1 self._check(section, 'forecast_reference_time') def test_observation_time(self): section = deepcopy(self.section) section['significanceOfReferenceTime'] = 3 self._check(section, 'time') def test_unknown_significance(self): section = deepcopy(self.section) section['significanceOfReferenceTime'] = 0 emsg = 'unsupported significance' with self.assertRaisesRegexp(TranslationError, emsg): self._check(section)
class Test(tests.IrisTest): def setUp(self): self.section = {'year': 2007, 'month': 1, 'day': 15, 'hour': 0, 'minute': 3, 'second': 0} self.unit = Unit('hours since epoch', calendar=CALENDAR_GREGORIAN) dt = datetime(self.section['year'], self.section['month'], self.section['day'], self.section['hour'], self.section['minute'], self.section['second']) self.point = self.unit.date2num(dt) def _check(self, section, standard_name=None): expected = DimCoord(self.point, standard_name=standard_name, units=self.unit) # The call being tested. coord = reference_time_coord(section) self.assertEqual(coord, expected) def test_start_of_forecast(self): section = deepcopy(self.section) section['significanceOfReferenceTime'] = 1 self._check(section, 'forecast_reference_time') def test_observation_time(self): section = deepcopy(self.section) section['significanceOfReferenceTime'] = 3 self._check(section, 'time') def test_unknown_significance(self): section = deepcopy(self.section) section['significanceOfReferenceTime'] = 0 emsg = 'unsupported significance' with self.assertRaisesRegexp(TranslationError, emsg): self._check(section)
def convert_time_since_to_std_time(time_array, units): # Strip out any extra colons and commas old_time = Unit(units.replace("since:", "since").replace(",", "")) dt = old_time.num2date(time_array) return cis_standard_time_unit.date2num(dt)
def test_b(self): new_b = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.factory.update(self.b, new_b) self.assertIs(self.factory.b, new_b)
def test_depth(self): new_depth = mock.Mock(units=Unit('m'), nbounds=0) self.factory.update(self.depth, new_depth) self.assertIs(self.factory.depth, new_depth)
def test_non_gregorian_calendar_conversion_dtype(self): data = np.arange(4, dtype=np.float32) u1 = Unit('hours since 2000-01-01 00:00:00', calendar='360_day') u2 = Unit('hours since 2000-01-02 00:00:00', calendar='360_day') result = u1.convert(data, u2) self.assertEqual(result.dtype, np.float32)
def test_a(self): new_a = mock.Mock(units=Unit('1'), nbounds=0, shape=(1, )) self.factory.update(self.a, new_a) self.assertIs(self.factory.a, new_a)
def test_depth_c_incompatible_units(self): new_depth_c = mock.Mock(units=Unit('Pa'), nbounds=0, shape=(1, )) with self.assertRaises(ValueError): self.factory.update(self.depth_c, new_depth_c)
def test_depth_c_non_scalar(self): new_depth_c = mock.Mock(units=Unit('m'), nbounds=0, shape=(10, )) with self.assertRaises(ValueError): self.factory.update(self.depth_c, new_depth_c)
def test_depth_c(self): new_depth_c = mock.Mock(units=Unit('m'), nbounds=0, shape=(1, )) self.factory.update(self.depth_c, new_depth_c) self.assertIs(self.factory.depth_c, new_depth_c)
def test_b_non_scalar(self): new_b = mock.Mock(units=Unit('1'), nbounds=0, shape=(10, )) with self.assertRaises(ValueError): self.factory.update(self.b, new_b)