def _add_forecast_reference_time(input_time: Coord, advected_cube: Cube) -> None: """Add or replace a forecast reference time on the advected cube""" try: advected_cube.remove_coord("forecast_reference_time") except CoordinateNotFoundError: pass frt_coord_name = "forecast_reference_time" frt_coord_spec = TIME_COORDS[frt_coord_name] frt_coord = input_time.copy() frt_coord.rename(frt_coord_name) frt_coord.convert_units(frt_coord_spec.units) frt_coord.points = round_close(frt_coord.points, dtype=frt_coord_spec.dtype) advected_cube.add_aux_coord(frt_coord)
def pad_coord(coord: Coord, width: int, method: str) -> Coord: """ Construct a new coordinate by extending the current coordinate by the padding width. Args: coord: Original coordinate which will be used as the basis of the new extended coordinate. width: The width of padding in grid cells (the extent of the neighbourhood radius in grid cells in a given direction). method: A string determining whether the coordinate is being expanded or contracted. Options: 'remove' to remove points from coord; 'add' to add points to coord. Returns: Coordinate with expanded or contracted length, to be added to the padded or unpadded iris cube. Raises: ValueError: Raise an error if non-uniform increments exist between grid points. """ orig_points = coord.points increment = orig_points[1:] - orig_points[:-1] if np.isclose(np.sum(np.diff(increment)), 0): increment = increment[0] else: msg = "Non-uniform increments between grid points: " "{}.".format( increment) raise ValueError(msg) if method == "add": num_of_new_points = len(orig_points) + width + width new_points = np.linspace( orig_points[0] - width * increment, orig_points[-1] + width * increment, num_of_new_points, dtype=np.float32, ) elif method == "remove": end_width = -width if width != 0 else None new_points = np.float32(orig_points[width:end_width]) new_points = new_points.astype(orig_points.dtype) new_points_bounds = np.array( [new_points - 0.5 * increment, new_points + 0.5 * increment], dtype=np.float32).T return coord.copy(points=new_points, bounds=new_points_bounds)
def iris_time_to_datetime(time_coord: Coord, point_or_bound: str = "point") -> List[datetime]: """ Convert iris time to python datetime object. Working in UTC. Args: time_coord: Iris time coordinate element(s). Returns: The time element(s) recast as a python datetime object. """ coord = time_coord.copy() coord.convert_units("seconds since 1970-01-01 00:00:00") if point_or_bound == "point": datetime_list = [value.point for value in coord.cells()] elif point_or_bound == "bound": datetime_list = [value.bound for value in coord.cells()] return datetime_list
def grid_spacing(coord: Coord) -> float: """Calculate grid spacing along a given spatial axis""" new_coord = coord.copy() new_coord.convert_units("m") return np.float32(np.diff((new_coord).points)[0])