def test_vector3array(self): class HasVec3Arr(properties.HasProperties): vec3 = properties.Vector3Array('simple vector array') hv3 = HasVec3Arr() hv3.vec3 = np.array([[1., 2., 3.]]) assert isinstance(hv3.vec3, vmath.Vector3Array) hv3.vec3 = ['east', 'south', [1., 1., 1.]] assert np.allclose(hv3.vec3, [[1., 0., 0.], [0., -1., 0.], [1., 1., 1.]]) hv3.vec3 = [1., 2., 3.] assert hv3.vec3.shape == (1, 3) with self.assertRaises(ValueError): hv3.vec3 = 'east' with self.assertRaises(ValueError): hv3.vec3 = ['diagonal'] with self.assertRaises(ValueError): hv3.vec3 = [[1., 2.]] class HasLenVec3Arr(properties.HasProperties): vec3 = properties.Vector3Array('length 5 vector', length=5) hv3 = HasLenVec3Arr() hv3.vec3 = [[0., 0., 1.], [1., 0., 0.]] assert np.allclose(hv3.vec3, [[0., 0., 5.], [5., 0., 0.]]) assert isinstance( properties.Vector3Array.from_json([[4., 5., 6.], [7., 8., 9.]]), vmath.Vector3Array) assert properties.Vector3Array('').equal( vmath.Vector3Array([[4., 5., 6.], [7., 8., 9.]]), vmath.Vector3Array([[4., 5., 6.], [7., 8., 9.]])) assert not properties.Vector3Array('').equal( vmath.Vector3Array([[4., 5., 6.], [7., 8., 9.]]), vmath.Vector3Array([[4., 5., 6.]])) assert not properties.Vector3Array('').equal( vmath.Vector3Array([[4., 5., 6.], [7., 8., 9.]]), np.array([[4., 5., 6.], [7., 8., 9.]])) assert not properties.Vector3Array('').equal('hi', 'hi') with self.assertRaises(TypeError): properties.Vector3Array('', shape=(3, )) with self.assertRaises(TypeError): properties.Vector3Array('', shape=('*', '*')) class HasShapeVec3Arr(properties.HasProperties): vec3 = properties.Vector3Array('', shape={(2, 3), (3, 3)}) hv3 = HasShapeVec3Arr(vec3=[[1., 2., 1.], [3., 4., 3.]]) hv3.vec3 = [[1., 2., 1.], [3., 4., 3.], [5., 6., 5.]] with self.assertRaises(ValueError): hv3.vec3 = [[1., 2., 1.], [3., 4., 3.], [5., 6., 5.], [7., 8., 7.]]
def from_json(value, **kwargs): return vmath.Vector3Array(value)
def get_LOS_vector(self, locations): """ calculate beta - the angle at earth center between reference point and satellite nadir """ if not isinstance(locations, list): locations = [locations] utmZone = self.location_UTM_zone refPoint = vmath.Vector3(self.ref.x, self.ref.y, 0) satAltitude = self.satellite_altitude satAzimuth = self.satellite_azimuth satIncidence = self.ref_incidence earthRadius = self.local_earth_radius DEG2RAD = np.pi / 180. alpha = satIncidence * DEG2RAD beta = (earthRadius / (satAltitude + earthRadius)) * np.sin(alpha) beta = alpha - np.arcsin(beta) beta = beta / DEG2RAD # calculate angular separation of (x,y) from satellite track passing # through (origx, origy) with azimuth satAzimuth # Long lat **NOT** lat long origy, origx = utm.to_latlon( refPoint.x, refPoint.y, np.abs(utmZone), northern=utmZone > 0 ) xy = np.array([ utm.to_latlon(u[0], u[1], np.abs(utmZone), northern=utmZone > 0) for u in locations ]) y = xy[:, 0] x = xy[:, 1] angdist = self._ang_to_gc(x, y, origx, origy, satAzimuth) # calculate beta2, the angle at earth center between roaming point and # satellite nadir track, assuming right-looking satellite beta2 = beta - angdist beta2 = beta2 * DEG2RAD # calculate alpha2, the new incidence angle alpha2 = np.sin(beta2) / ( np.cos(beta2) - (earthRadius / (earthRadius + satAltitude)) ) alpha2 = np.arctan(alpha2) alpha2 = alpha2 / DEG2RAD # calculate pointing vector satIncidence = 90 - alpha2 satAzimuth = 360 - satAzimuth los_x = -np.cos(satAzimuth * DEG2RAD) * np.cos(satIncidence * DEG2RAD) los_y = -np.sin(satAzimuth * DEG2RAD) * np.cos(satIncidence * DEG2RAD) los_z = np.sin(satIncidence * DEG2RAD) return vmath.Vector3Array([los_x, los_y, los_z])
import numpy as np import vectormath as vmath # Single Vectors v = vmath.Vector3(5, 0, 0) v.normalize() print(v) # >> [1, 0, 0] print(v.x) # >> 1.0 x = vmath.Vector3(0, 1, 0) print(x.cross(v)) # VectorArrays are much faster than a for loop over Vectors v_array = vmath.Vector3Array([[4, 0, 0], [0, 2, 0], [0, 0, 3]]) print(v_array.x) # >> [4, 0, 0] print(v_array.length) # >> [4, 2, 3] print(v_array.normalize()) # >> [[1, 0, 0], [0, 1, 0], [0, 0, 1]] # Vectors can be accessed individually or in slices print(type(v_array[1:])) # >> vectormath.Vector3Array print(type(v_array[2])) # >> vectormath.Vector3 # All these classes are just numpy arrays print(isinstance(v, np.ndarray)) # >> True print(type(v_array[1:, 1:])) # >> numpy.ndarray