def _check_coordinates(coordinates: Union[xr.DataArray, TimeSeries]): """Check if the coordinates have the correct format.""" if isinstance(coordinates, xr.DataArray): ut.xr_check_coords( coordinates, dict( c={"values": ["x", "y", "z"]}, time={"dtype": "timedelta64", "optional": True}, ), ) else: # todo: check time series shape pass
def _check_and_normalize_orientation(orientation: xr.DataArray) -> xr.DataArray: """Check if the orientation has the correct format and normalize it.""" ut.xr_check_coords( orientation, dict( c={"values": ["x", "y", "z"]}, v={"values": [0, 1, 2]}, time={"dtype": "timedelta64", "optional": True}, ), ) orientation = xr.apply_ufunc( normalize, orientation, input_core_dims=[["c"]], output_core_dims=[["c"]], ) # vectorize test if orthogonal if not ut.xr_is_orthogonal_matrix(orientation, dims=["c", "v"]): raise ValueError("Orientation vectors must be orthogonal") return orientation
def __init__( self, orientation: types_orientation = None, coordinates: types_coordinates = None, time: types_timeindex = None, time_ref: pd.Timestamp = None, construction_checks: bool = True, ): """Construct a cartesian coordinate system. Parameters ---------- orientation : Matrix of 3 orthogonal column vectors which represent the coordinate systems orientation. Keep in mind, that the columns of the corresponding orientation matrix is equal to the normalized orientation vectors. So each orthogonal transformation matrix can also be provided as orientation. Passing a scipy.spatial.transform.Rotation object is also supported. coordinates : Coordinates of the origin time : Time data for time dependent coordinate systems. If the provided coordinates and orientations contain only a single value, the coordinate system is considered to be static and the provided value won't be stored. If this happens, a warning will be emitted. time_ref : Reference Timestamp to use if time is Timedelta or pint.Quantity. construction_checks : If 'True', the validity of the data will be verified Returns ------- LocalCoordinateSystem Cartesian coordinate system """ if orientation is None: orientation = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) if coordinates is None: coordinates = np.array([0, 0, 0]) time, time_ref = build_time_index(time, time_ref) orientation = self._build_orientation(orientation, time) coordinates = self._build_coordinates(coordinates, time) if time is not None and not ("time" in coordinates.coords or "time" in orientation.coords): warnings.warn( "Neither the coordinates nor the orientation are time dependent. " "Provided time is dropped") if construction_checks: ut.xr_check_coords( coordinates, dict( c={"values": ["x", "y", "z"]}, time={ "dtype": "timedelta64", "optional": True }, ), ) ut.xr_check_coords( orientation, dict( c={"values": ["x", "y", "z"]}, v={"values": [0, 1, 2]}, time={ "dtype": "timedelta64", "optional": True }, ), ) orientation = xr.apply_ufunc( normalize, orientation, input_core_dims=[["c"]], output_core_dims=[["c"]], ) # vectorize test if orthogonal if not ut.xr_is_orthogonal_matrix(orientation, dims=["c", "v"]): raise ValueError("Orientation vectors must be orthogonal") # unify time axis if (("time" in orientation.coords) and ("time" in coordinates.coords) and (not np.all(orientation.time.data == coordinates.time.data))): time_union = ut.get_time_union([orientation, coordinates]) orientation = ut.xr_interp_orientation_in_time( orientation, time_union) coordinates = ut.xr_interp_coordinates_in_time( coordinates, time_union) coordinates.name = "coordinates" orientation.name = "orientation" self._dataset = xr.merge([coordinates, orientation], join="exact") if "time" in self._dataset and time_ref is not None: self._dataset.weldx.time_ref = time_ref
def test_xr_check_coords_exception(dax, ref_dict, exception_type): """Test weldx.utility.xr_check_coords function.""" with pytest.raises(exception_type): ut.xr_check_coords(dax, ref_dict)
def test_xr_check_coords(coords, ref_dict): """Test weldx.utility.xr_check_coords function.""" assert ut.xr_check_coords(coords, ref_dict)