-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoordinates.py
876 lines (747 loc) · 33.4 KB
/
coordinates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
'''
Fundamental Coordinate Data Structure:
np.array([[x1, x2, ..],
[y1, y2, ..],
[z1, z2, ..]]
np.array([[x1],
[y1],
[z1]]
'''
from ..libs import turtle
#from dataclasses import dataclass
from typing import Optional, Sequence, Tuple, Union, Any
import numpy as np
from numbers import Number
import datetime
import copy as _copy
# Mean value of proposed GP300 layout. Just a placeholder for the default GP300 origin.
#grd_origin_lat = 38.87253 # degree
#grd_origin_lon = 92.35731 # degree
#grd_origin_height = 2797.026 # meter
grd_origin_lat = 38.88849 # degree
grd_origin_lon = 92.28605 # degree
grd_origin_height = 2920.522 # meter
__all__ = ('Coordinates',
'CartesianRepresentation' , 'HorizontalRepresentation' , 'SphericalRepresentation',
'ECEF', 'Geodetic', 'LTP' , 'GRAND', 'HorizontalVector',
'_cartesian_to_spherical' ,
'_cartesian_to_horizontal',
'_spherical_to_cartesian' ,
'_spherical_to_horizontal',
'_horizontal_to_cartesian',
'_horizontal_to_spherical')
# Define functions to transform from one coordinate representation to
# another coordinate representation. Cartesian, Spherical, and Horizontal
# coordinate representation are defined.
def _cartesian_to_spherical(x: Union[Number, np.ndarray],
y: Union[Number, np.ndarray],
z: Union[Number, np.ndarray]) -> Tuple[Union[Number, np.ndarray]]:
'''Transform Cartesian coordinates to spherical
'''
rho2 = x**2 + y**2
rho = np.sqrt(rho2)
theta = np.rad2deg(np.arctan2(rho, z))
phi = np.rad2deg(np.arctan2(y, x))
r = np.sqrt(rho2 + z**2)
return theta, phi, r
# Horizontal has an axis fixed to geographic North, so it can not be
# converted like cartesian and spherical. If conversion is done, then
# the same origin for both and ENU basis for cartesian is assumed.
def _cartesian_to_horizontal(x: Union[Number, np.ndarray],
y: Union[Number, np.ndarray],
z: Union[Number, np.ndarray]) -> Tuple[Union[Number, np.ndarray]]:
'''Transform Cartesian coordinates to horizontal
'''
theta, phi, r = _cartesian_to_spherical(x, y, z)
return _spherical_to_horizontal(theta, phi, r)
def _spherical_to_cartesian(theta: Union[Number, np.ndarray],
phi : Union[Number, np.ndarray],
r : Union[Number, np.ndarray]) -> Tuple[Union[Number, np.ndarray]]:
'''Transform spherical coordinates to Cartesian
'''
cos_theta = np.cos(np.deg2rad(theta))
sin_theta = np.sin(np.deg2rad(theta))
x = r * np.cos(np.deg2rad(phi)) * sin_theta
y = r * np.sin(np.deg2rad(phi)) * sin_theta
z = r * cos_theta
return x, y, z
def _spherical_to_horizontal(theta: Union[Number, np.ndarray],
phi : Union[Number, np.ndarray],
r : Union[Number, np.ndarray]) -> Tuple[Union[Number, np.ndarray]]:
'''Transform spherical coordinates to horizontal
'''
#return 0.5 * np.pi - phi, 0.5 * np.pi - theta, r
return 90. - phi, 90. - theta, r
# Horizontal has an axis fixed to geographic North, so it can not be
# converted like cartesian and spherical. If conversion is done, then
# the same origin for both and ENU basis for cartesian is assumed.
def _horizontal_to_cartesian(azimuth : Union[Number, np.ndarray],
elevation: Union[Number, np.ndarray],
norm : Union[Number, np.ndarray]) -> Tuple[Union[Number, np.ndarray]]:
'''Transform horizontal coordinates to Cartesian
'''
theta, phi, r = _horizontal_to_spherical(azimuth, elevation, norm)
return _spherical_to_cartesian(theta, phi, r)
def _horizontal_to_spherical(azimuth : Union[Number, np.ndarray],
elevation: Union[Number, np.ndarray],
norm : Union[Number, np.ndarray]) -> Tuple[Union[Number, np.ndarray]]:
'''Transform horizontal coordinates to spherical
'''
#return 0.5 * np.pi - elevation, 0.5 * np.pi - azimuth, norm
return 90. - elevation, 90. - azimuth, norm
# -----------Base Representation------------
class Coordinates(np.ndarray):
'''
Generic container for a coordinates object
This object created is a standard np.ndarray of size (3, n)
where 3 is for 3D coordinates and n is the number of entries.
'''
def __new__(cls, n: Optional[int]=None):
'''
Create 3xn ndarray coordinates instance with n random entries for all 3D coordinate system.
n: number of coordinate points.
'''
if isinstance(n, int):
return super().__new__(cls, (3, n), dtype='f8')
else:
raise TypeError('Input number of coordinates point is type', type(n), 'Integer is required.')
# --------------Representation---------------
class CartesianRepresentation(Coordinates):
'''
Generic container for cartesian coordinates
'''
def __new__(cls,
arg: 'Other Representation' = None,
x : Union[Number, np.ndarray] = None,
y : Union[Number, np.ndarray] = None,
z : Union[Number, np.ndarray] = None):
'''
Create a Cartesian coordinates instance
Unspecified coordinates are initialized with entry 0 in 3xn ndarray.
n: number of coordinate points. 3xn np.ndarray object will be instantiated
which will then be replaced by input x, y, and z. 'n' has to be predefined.
'''
if isinstance(arg, SphericalRepresentation):
x, y, z = _spherical_to_cartesian(arg.theta, arg.phi, arg.r)
elif isinstance(arg, CartesianRepresentation):
x, y, z = arg.x, arg.y, arg.z
if isinstance(x, Number):
n = 1
elif isinstance(x, np.ndarray):
n = len(x)
assert n==len(y), 'Length of x and y array must be the same. \
x: %i, y: %i'%(len(x),len(y))
assert n==len(z), 'Length of x and z array must be the same. \
x: %i, z: %i'%(len(x),len(z))
else:
raise TypeError(type(x))
obj = super().__new__(cls, n) # create 3xn ndarray coordinates instance with random entries.
obj[0] = x # replace x-coordinates with input x. x can be int, float, or ndarray.
obj[1] = y # replace y-coordinates with input y. y can be int, float, or ndarray.
obj[2] = z # replace z-coordinates with input z. z can be int, float, or ndarray.
return obj
@property
def x(self):
return self[0]
@x.setter
def x(self, v):
self[0] = v
@property
def y(self):
return self[1]
@y.setter
def y(self, v):
self[1] = v
@property
def z(self):
return self[2]
@z.setter
def z(self, v):
self[2] = v
def cartesian_to_spherical(self):
theta, phi, r = _cartesian_to_spherical(self[0], self[1], self[2])
return SphericalRepresentation(theta=theta, phi=phi, r=r)
def _cartesian_to_horizontal(self):
return _cartesian_to_horizontal(self[0], self[1], self[2])
class SphericalRepresentation(Coordinates):
'''
Generic container for spherical coordinates
'''
def __new__(cls,
arg : 'Other Representation' = None,
theta: Union[Number, np.ndarray] = None,
phi : Union[Number, np.ndarray] = None,
r : Union[Number, np.ndarray] = None):
'''
Create a spherical coordinates instance.
Object with (3,n) ndarray is created which will later be filled with input
theta, phi, and r, where n is equal to len(theta). If predefined object
(like CartesianCoordinates,..) is given as an argument, it will be
first converted to spherical coordinates. Then (3,n) ndarray will be filled
with converted theta, phi, and r.
n: number of coordinate points. 3xn np.ndarray object will be instantiated
which will then be replaced by input theta, phi, and r. 'n' has to be predefined.
theta: angle from Z-axis towards XY plane. Also called zenith angle or colatitude. 0<=theta<=180 deg.
phi : angle from X-axis towards Y-axis in XY plane. 0<=phi<=360 deg.
r : magnitude of a vector or a distance to a point from the origin.
'''
if isinstance(arg, CartesianRepresentation):
theta, phi, r = _cartesian_to_spherical(arg.x, arg.y, arg.z)
elif isinstance(arg, SphericalRepresentation):
theta, phi, r = arg.theta, arg.phi, arg.r
if isinstance(theta, Number):
n = 1
elif isinstance(theta, np.ndarray):
n = len(theta)
assert n==len(phi), 'Length of theta and phi array must be the same. \
theta: %i, phi: %i'%(n,len(phi))
assert n==len(r), 'Length of theta and r array must be the same. \
theta: %i, r: %i'%(n,len(r))
else:
raise TypeError(type(x))
obj = super().__new__(cls, n) # create 3xn ndarray coordinates instance with random entries.
obj[0] = theta # replace 0-coordinates with input theta. theta can be int, float, or ndarray.
obj[1] = phi # replace 1-coordinates with input phi. phi can be int, float, or ndarray.
obj[2] = r # replace 2-coordinates with input r. r can be int, float, or ndarray.
return obj
@property
def theta(self):
return self[0]
@theta.setter
def theta(self, v):
self[0] = v
@property
def phi(self):
return self[1]
@phi.setter
def phi(self, v):
self[1] = v
@property
def r(self):
return self[2]
@r.setter
def r(self, v):
self[2] = v
def spherical_to_cartesian(self):
x, y, z = _spherical_to_cartesian(self[0], self[1], self[2])
return CartesianRepresentation(x=x, y=y, z=z)
def _spherical_to_horizontal(self):
return _spherical_to_horizontal(self[0], self[1], self[2])
class GeodeticRepresentation(Coordinates):
'''
Generic container for Geodetic coordinate system. Center of this frame
is the center of Earth. Geodetic representation w.r.t. the WGS84 ellipsoid.
Latitude: Angle north and south of the equator. +ve in the northern hemisphere,
-ve in the southern hemisphere. Range: -90 deg (South Pole)
to +90 deg (North Pole). In equator, latitude = 0.
Longitude: Angle east and west of the Prime Meridian. The Prime Meridian
is a north-south line that passes through Greenwich, UK.
+ve to the east of the Prime Meridian, -ve to the west.
Range: -180 deg to +180 deg.
Height: Also called altitude or elevation, this represents the height above
the Earth ellipsoid, measured in meters. The Earth ellipsoid is a
mathematical surface defined by a semi-major axis and a semi-minor axis.
The most common values for these two parameters are defined by
the World Geodetic Standard 1984 (WGS-84). The WGS-84 ellipsoid is
intended to correspond to mean sea level. A Geodetic height of zero
therefore roughly corresponds to sea level, with positive values increasing
away from the Earth’s center. The theoretical range of height values is
from the center of the Earth (about -6,371km) to positive infinity.
'''
def __new__(cls,
latitude : Union[Number, np.ndarray] = None,
longitude: Union[Number, np.ndarray] = None,
height : Union[Number, np.ndarray] = None):
'''
Create a new instance from another point instance or from
latitude, longitude, height values
'''
if isinstance(latitude, Number):
n = 1
elif isinstance(latitude, np.ndarray):
n = len(latitude)
assert n==len(longitude)
assert n==len(height)
else:
raise TypeError(type(x))
obj = super().__new__(cls, n) # create 3xn ndarray coordinates instance with random entries.
obj[0] = latitude # replace 0-position with input latitude. latitude can be int, float, or ndarray.
obj[1] = longitude # replace 1-position with input longitude. longitude can be int, float, or ndarray.
obj[2] = height # replace 2-position with input height. height can be int, float, or ndarray.
return obj
@property
def latitude(self):
return self[0]
@latitude.setter
def latitude(self, v):
self[0] = v
@property
def longitude(self):
return self[1]
@longitude.setter
def longitude(self, v):
self[1] = v
@property
def height(self):
return self[2]
@height.setter
def height(self, v):
self[2] = v
class HorizontalRepresentation(Coordinates):
'''
Generic container for horizontal coordinates.
'''
def __new__(cls,
azimuth : Union[Number, np.ndarray] = None,
elevation: Union[Number, np.ndarray] = None,
norm : Union[Number, np.ndarray] = 1.):
'''
Create a horizontal coordinates instance.
Object with (3,n) ndarray is created which will later be filled with input
azimuth, elevation, and norm, where n is equal to len(azimuth). If predefined
object (like CartesianCoordinates,..) is given as an argument, it will be
first converted to horizontal coordinates. Then (3,n) ndarray will be filled
with converted azimuth, elevation, and norm.
n : number of coordinate points. 3xn np.ndarray object will be instantiated
which will then be replaced by input azimuth, elevation, and norm.
'n' has to be predefined.
azimuth : angle from true North towards East.
elevation: angle from horizontal plane (NE plane) towards zenith.
norm : distance from the origin to the point.
'''
if isinstance(azimuth, Number):
n = 1
elif isinstance(azimuth, np.ndarray):
n = len(azimuth)
assert n==len(elevation), 'Length of azimuth and elevation array must be the same. \
azimuth: %i, elevation: %i'%(n,len(elevation))
else:
raise TypeError(type(x))
obj = super().__new__(cls, n) # create 3xn ndarray coordinates instance with random entries.
obj[0] = azimuth # replace 0-coordinates with input azimuth. azimuth can be int, float, or ndarray.
obj[1] = elevation # replace 1-coordinates with input elevation. elevation can be int, float, or ndarray.
obj[2] = norm # replace 2-coordinates with input norm. norm can be int, float, or ndarray.
return obj
@property
def azimuth(self):
return self[0]
@azimuth.setter
def azimuth(self, v):
self[0] = v
@property
def elevation(self):
return self[1]
@elevation.setter
def elevation(self, v):
self[1] = v
@property
def norm(self):
return self[2]
@norm.setter
def norm(self, v):
self[2] = v
def horizontal_to_cartesian(self):
return _horizontal_to_cartesian(self[0], self[1], self[2])
def horizontal_to_spherical(self):
return _horizontal_to_spherical(self[0], self[1], self[2])
# ------------------Frame---------------------
class Geodetic(GeodeticRepresentation):
'''
Generic container for Geodetic coordinate system. Center of this frame
is the center of Earth.
Latitude: Angle north and south of the equator. +ve in the northern hemisphere,
-ve in the southern hemisphere. Range: -90 deg (South Pole)
to +90 deg (North Pole). In equator, latitude = 0.
Longitude: Angle east and west of the Prime Meridian. The Prime Meridian
is a north-south line that passes through Greenwich, UK.
+ve to the east of the Prime Meridian, -ve to the west.
Range: -180 deg to +180 deg.
Height: Also called altitude or elevation, this represents the height above
the Earth ellipsoid, measured in meters. The Earth ellipsoid is a
mathematical surface defined by a semi-major axis and a semi-minor axis.
The most common values for these two parameters are defined by
the World Geodetic Standard 1984 (WGS-84). The WGS-84 ellipsoid is
intended to correspond to mean sea level. A Geodetic height of zero
therefore roughly corresponds to sea level, with positive values increasing
away from the Earth’s center. The theoretical range of height values is
from the center of the Earth (about -6,371km) to positive infinity.
'''
def __new__(cls,
arg : 'Coordinates Instance' = None,
latitude : Union[Number, np.ndarray] = None,
longitude : Union[Number, np.ndarray] = None,
height : Union[Number, np.ndarray] = None):
'''
Create a new instance from another point instance or from
latitude, longitude, height values
'''
if isinstance(arg, (Geodetic, GeodeticRepresentation)):
return Geodetic(latitude=arg.latitude, longitude=arg.longitude, height=arg.height)
if isinstance(latitude, (Number, np.ndarray)):
#Do nothing here. More check will be performed inside GeodeticRepresentaion.
pass
elif not isinstance(arg, type(None)):
if isinstance(arg, HorizontalVector):
#TO DO: write proper transformation.
pass
elif isinstance(arg, ECEF):
# allows ECEF instance as an input. Convert it to Geodetic.
latitude, longitude, height = turtle.ecef_to_geodetic(arg.T)
elif isinstance(arg, (LTP, GRAND)):
ecef = ECEF(arg)
geodetic = Geodetic(ecef)
latitude, longitude, height = geodetic.latitude, geodetic.longitude, geodetic.height
else:
raise TypeError(type(arg), type(latitude),
'Argument type must be either int, float, np.ndarray, \
ECEF, Geodetic, GRAND or HorizontalVector.')
else:
raise TypeError(type(arg), type(latitude),
'Argument type must be either int, float, np.ndarray, \
ECEF, Geodetic, GRAND or HorizontalVector.')
return super().__new__(cls, latitude=latitude, longitude=longitude, height=height)
def geodetic_to_horizontal(self):
pass
def geodetic_to_ecef(self):
return ECEF(self)
def geodetic_to_grand(self):
return GRAND(self)
class ECEF(CartesianRepresentation):
'''
Generic container for Earth-Centered Earth-Fixed (ECEF) coordinate system.
Center of Earth is the origin of this frame.
ECEF is a right-handed Cartesian coordinate system with the origin at the
Earth’s center. This coordinate frame is fixed with respect to the Earth
(i.e., rotates along with the Earth). Units are in meters. The three axis are
defined as follows:
x: Passes through the equator at the Prime Meridian (latitude = 0, longitude = 0).
y: Passes through the equator 90 degrees east of the Prime Meridian
(latitude = 0, longitude = 90 degrees).
z: Passes through the North Pole (latitude = 90 degrees, longitude = any value).
'''
def __new__(cls,
arg : 'Coordinates Instance' = None,
x : Union[Number, np.ndarray] = None,
y : Union[Number, np.ndarray] = None,
z : Union[Number, np.ndarray] = None):
if isinstance(arg, ECEF):
return arg
if isinstance(x, (Number, np.ndarray)):
#Do nothing here. More check will be performed inside CartesianRepresentaion.
pass
elif not isinstance(arg, type(None)):
if isinstance(arg, HorizontalVector):
#TO DO: write a proper transformation from Horizontal to ECEF.
pass
elif isinstance(arg, Geodetic):
# allows Geodtic instances as input. Convert from Geodetic to ECEF.
ecef = turtle.ecef_from_geodetic(arg.latitude, arg.longitude, arg.height)
if ecef.size==3:
x, y, z = ecef[0], ecef[1], ecef[2]
elif ecef.size>3:
x, y, z = ecef[:,0], ecef[:,1], ecef[:,2]
elif isinstance(arg, (LTP, GRAND)):
basis = arg.basis
origin = arg.location
ecef = np.matmul(basis.T, arg) + origin
x, y, z = ecef.x, ecef.y, ecef.z
else:
raise TypeError(type(arg), type(x),
'Type must be either int, float, np.ndarray, \
ECEF, Geodetic, GRAND or HorizontalVector.')
else:
raise TypeError(type(arg), type(x),
'Type must be either int, float, np.ndarray, \
ECEF, Geodetic, GRAND or HorizontalVector.')
return super().__new__(cls, x=x, y=y, z=z)
def ecef_to_geodetic(self):
return Geodetic(self)
def ecef_to_grand(self):
return GRAND(self)
# Reason for using 'GeodeticRepresentation' instead of 'Geodetic'
# directly is that 'Geodetic' uses 'HorizontalVector' which is not
# defined yet and causes an error.
grand_origin = GeodeticRepresentation(latitude = grd_origin_lat,
longitude= grd_origin_lon,
height = grd_origin_height)
class HorizontalVector(HorizontalRepresentation):
'''
Generic container for horizontal coordinates. Location of this
coordinate system must be provided in longitude and latitude.
Note: Starting direction is fixed towards the True North from the given lat and lon.
azimuth : angle (deg) starting from true North towards East.
elevation: angle (deg) from horizontal plane towards zenith.
'''
def __new__(cls,
arg : 'Coordinates Instance' = None,
azimuth : Union[Number, np.ndarray] = None,
elevation: Union[Number, np.ndarray] = None,
norm : Union[Number, np.ndarray] = 1.,
location : 'Coordinates Instance' = grand_origin): # Location of horizontal CS.
'''
Create a horizontal coordinates instance.
Object with (3,n) ndarray is created which will later be filled with input
azimuth, elevation, and norm, where n is equal to len(azimuth). If predefined
object (like CartesianCoordinates,..) is given as an argument, it will be
first converted to horizontal coordinates. Then (3,n) ndarray will be filled
with converted azimuth, elevation, and norm.
n: number of coordinate points. 3xn np.ndarray object will be instantiated
which will then be replaced by input azimuth, elevation, and norm.
'n' has to be predefined.
location: location of Horizontal coordinate system. Can be given in any known
coordinate system. It will be converted to Geodetic coordinate system.
'''
obj = LTP(location=location, orientation='ENU', magnetic=False)
ecef_loc = obj.location # location is already in ECEF cs.
ecef_basis = obj.basis # basis is already in ECEF cs.
cls.location= ecef_loc # used to convert back to ECEF, Geodetic etc.
cls.basis = ecef_basis # used to convert back to ECEF, Geodetic etc.
if isinstance(arg, (HorizontalVector, HorizontalRepresentation)):
return HorizontalVector(azimuth=arg.azimuth, elevation=arg.elevation, norm=arg.norm)
if isinstance(azimuth, (Number, np.ndarray)):
# check if input coordinates are of the right kind.
# Additional check will be performed inside HorizontalRepresentation.
pass
elif not isinstance(arg, type(None)):
if isinstance(arg, (ECEF, Geodetic, GRAND)):
if isinstance(arg, ECEF):
ecef = arg # No need to convert. ECEF is required.
elif isinstance(arg, (Geodetic, GRAND)):
ecef = ECEF(arg) # Convert from Geodetic input to ECEF.
# Positional vector from the new location is not used like for GRAND cs.
# see: turtle.ecef_to_horizontal()
pos_v = np.vstack((ecef.x, ecef.y, ecef.z))
# Projecting positional vectors to GRAND's cs basis.
# Converts completely from ECEF cs to the GRAND's cs.
# Below matrix multiplication is performed in turtle.ecef_to_horizontal()
enu_cord = np.matmul(ecef_basis, pos_v)
x, y, z = enu_cord[0], enu_cord[1], enu_cord[2] # x,y,z w.r.t to ENU basis.
r = np.sqrt(x*x + y*y + z*z)
azimuth = np.rad2deg(np.arctan2(x,y))
elevation= np.rad2deg(np.arcsin(z/r))
norm = r
else:
raise TypeError(type(arg), type(azimuth),
'Type must be either int, float, np.ndarray, \
ECEF, Geodetic, GRAND or HorizontalVector.')
else:
raise TypeError(type(arg), type(azimuth),
'Type must be either int, float, np.ndarray, \
ECEF, Geodetic, GRAND or HorizontalVector.')
return super().__new__(cls, azimuth, elevation, norm)
def horizontal_to_ecef(self):
rel = np.deg2rad(self.elevation)
raz = np.deg2rad(self.azimuth)
ce = np.cos(rel)
pos_v = np.vstack((self.norm*ce*np.sin(raz),
self.norm*ce*np.cos(raz),
self.norm*np.sin(rel)))
# Projecting horizontal direction vectors to ECEF's cs basis.
# Converts completely from Horizontal to ECEF cs.
# Below matrix multiplication is performed in turtle.ecef_to_horizontal()
ecef_cord = np.matmul(self.basis.T, pos_v) # basis is in ECEF frame.
x, y, z = ecef_cord[0], ecef_cord[1], ecef_cord[2] # x,y,z w.r.t to ECEF basis.
return ECEF(x=x, y=y, z=z)
def horizontal_to_geodetic(self):
ecef = self.horizontal_to_ecef()
return Geodetic(ecef)
def horizontal_to_grand(self):
ecef = self.horizontal_to_ecef()
return GRAND(arg=ecef, origin=self.origin)
class LTP(CartesianRepresentation):
'''
Calculates basis and orgin at a given latitude and longitude.
Basis and origin is calculated in ECEF frame.
'location' and 'orientation' are required.
'''
def __new__(cls,
arg: 'Coordinates Instance' = None, # input coordinate instance to convert to LTP
x : Union[Number, np.ndarray] = None, # x-coordinate at LTP
y : Union[Number, np.ndarray] = None, # y-coordinate at LTP
z : Union[Number, np.ndarray] = None, # z-coordinate at LTP
*args, **kwargs):
if isinstance(x, (Number, np.ndarray)):
return super().__new__(cls, x=x, y=y, z=z)
elif not isinstance(arg, type(None)):
if isinstance(arg, (LTP, ECEF, Geodetic, GRAND)):
if isinstance(arg, ECEF):
ecef = arg # No need to convert. ECEF is required.
elif isinstance(arg, (LTP, Geodetic, GRAND)):
ecef = ECEF(arg) # Convert from Geodetic input to ECEF.
placeholder = np.nan*np.ones(len(ecef.x))
return super().__new__(cls, x=placeholder, y=placeholder, z=placeholder)
else:
# return a placeholder with 1 entry. This is used if we just want to define LTP frame
# without giving any coordinates. Can also use np.empty((1,1)) instead of np.array([nan]).
# Do not use array with no entry because n>=1 is needed to instantiate 'Coordinates'.
return super().__new__(cls, x=np.array([np.nan]),
y=np.array([np.nan]),
z=np.array([np.nan]))
def __init__(self,
arg: 'Coordinates Instance' = None, # input coordinate instance to convert to LTP
x : Union[Number, np.ndarray] = None, # x-coordinate at LTP.
y : Union[Number, np.ndarray] = None, # y-coordinate at LTP
z : Union[Number, np.ndarray] = None, # z-coordinate at LTP
latitude : Union[Number, np.ndarray] = None, # latitude of LTP's location/origin
longitude: Union[Number, np.ndarray] = None, # longitude of LTP's location/origin
height : Union[Number, np.ndarray] = None, # height of LTP's location/origin
location : 'Coordinates Instance' = None, # location of LTP in Geodetic, GRAND, or ECEF
orientation: str = None, # orientation of LTP. 'NWU', 'ENU' etc
magnetic : bool = False, # shift orientation by magnetic declination?
magmodel : str = 'IGRF13', # if shift, which magnetic model to use?
declination: Optional[Number]= None, # or simply provide the magnetic declination
obstime : Union[str, datetime.date] ='2020-01-01', # calculate declination of what date?
rotation=None):
# Make sure the location is in the correct format. i.e ECEF, Geodetic, GeodeticRepresentation,
# or GRAND cs. OR latitude=deg, longitude=deg, height=meter.
if latitude!=None and longitude!=None and height!=None:
geodetic_loc = Geodetic(latitude=latitude, longitude=longitude, height=height)
elif isinstance(location, (ECEF, Geodetic, GeodeticRepresentation, GRAND)):
geodetic_loc = Geodetic(location)
else:
raise TypeError('Provide location of LTP in ECEF, Geodetic, or GRAND coordinate system instead of type %s.\n \
Location can also be given as latitude=deg, longitude=deg, height=meter.'%type(location))
# Make sure orientation is given as string.
if isinstance(orientation, str):
pass
else:
raise TypeError('Provide orientaion. \
Orientation must be string instead of %s. Example: ENU, NWU etc.'%type(orientation))
latitude = geodetic_loc.latitude
longitude = geodetic_loc.longitude
height = geodetic_loc.height
# Calculate magnetic field declination if magnetic=True. Used to define GRAND coordinate system.
if magnetic and declination is None:
from .geomagnet import Geomagnet
# Calculate a magnetic field declination at a given location at a give time.
geoB = Geomagnet(magmodel, location=geodetic_loc, obstime=obstime)
declination = geoB.declination
azimuth0 = 0 if declination is None else declination
def vector(name):
tag = name[0].upper()
if tag == 'E':
return turtle.ecef_from_horizontal(latitude, longitude, 90+azimuth0, 0)
elif tag == 'W':
return turtle.ecef_from_horizontal(latitude, longitude, 270+azimuth0, 0)
elif tag == 'N':
return turtle.ecef_from_horizontal(latitude, longitude, azimuth0, 0)
elif tag == 'S':
return turtle.ecef_from_horizontal(latitude, longitude, 180+azimuth0, 0)
elif tag == 'U':
return turtle.ecef_from_horizontal(latitude, longitude, 0, 90)
elif tag == 'D':
return turtle.ecef_from_horizontal(latitude, longitude, 0, -90)
else:
raise ValueError(f'Invalid frame orientation `{name}`')
# unit vectors (basis) in ECEF frame of reference.
# These are the basis of the GRAND coordinate system if orientation='NWU' and magnetic=True.
ux = vector(orientation[0])
uy = vector(orientation[1])
uz = vector(orientation[2])
# These objects share the same memory with arg and is overwritten if kept inside __new__.
# Problem is solved if __new__ is redefined and __init__ is added with below attributes
# in __init__ rather than in __new__.
self.location = ECEF(geodetic_loc)
self.basis = np.vstack((ux, uy, uz)) # unit vectors (basis) in ECEF frame.
self.orientation = orientation
self.magnetic = magnetic
self.declination = azimuth0
self.magmodel = magmodel
self.obstime = obstime
# Scripts below is used only if coordinates (x,y,z) in LTP's frame is required.
if not isinstance(arg, type(None)):
if isinstance(arg, (LTP, ECEF, Geodetic, GRAND)):
if isinstance(arg, ECEF):
ecef = arg # No need to convert. ECEF is required.
elif isinstance(arg, (LTP, Geodetic, GRAND)):
ecef = ECEF(arg) # Convert from Geodetic input to ECEF.
# Positional vectors wrt GRAND's cs origin. Still in ECEF cs.
pos_v = np.vstack((ecef.x - self.location.x,
ecef.y - self.location.y,
ecef.z - self.location.z))
# Projecting positional vectors to LTP's basis.
# Converts completely from ECEF cs to the LTP's frame.
ltp_cord = np.matmul(self.basis, pos_v)
x, y, z = ltp_cord[0], ltp_cord[1], ltp_cord[2]
if isinstance(x, (Number, np.ndarray)):
self.x = x
self.y = y
self.z = z
def ltp_to_ltp(self, ltp):
# convert self to ECEF frame. Then convert ecef to new ltp's frame.
ecef = ECEF(self)
basis = ltp.basis
location = ltp.location
pos_v = np.vstack((ecef.x - location.x,
ecef.y - location.y,
ecef.z - location.z))
ltp_cord = np.matmul(basis, pos_v)
x, y, z = ltp_cord[0], ltp_cord[1], ltp_cord[2]
return LTP(x=x, y=y, z=z,
location = location,
orientation = ltp.orientation,
magnetic = ltp.magnetic,
magmodel = ltp.magmodel,
declination = ltp.declination,
obstime = ltp.obstime,
rotation = None)
def ltp_to_ecef(self):
# Basis forms a rotational matrix. Transpose is the inverse of rotational matrix (real).
# Use inverse (transpose) of rotational matrix to convert from GRANDCS to ECEF.
return ECEF(self)
def ltp_to_geodetic(self):
# Convert from GRANDCS to ECEF, then from ECEF to Geodetic.
ecef = ECEF(self)
return Geodetic(ecef)
class GRAND(LTP):
'''
Class for the GRAND coordinate system (cs). This class instantiate coordinates
in GRAND's coordinate frame. Input can be either x, y, z coordinates value
in GRAND's cs or coordinates in ECEF or Geodetic system.
If the input coordinates are in ECEF or Geodetic (or any coordinate system
other than GRAND's cs) following procedure is performed. Convert everything
to ECEF frame to do all initial calculations and then finally project the
positional vector from GRAND's cs origin to it's basis vectors. Basis for NWU GRAND's
cs is calculated based on GRAND's origin. Basis forms a rotational matrix.
Transpose of the rotational matrix is it's inverse. Basis in this case are unit
vectors in ECEF frame that describes the NWU direction from GRAND's origin
(also in ECEF frame). To convert other cs to GRAND's cs, first transform the
coordinates in ECEF frame, then shift the origin of coordinates from
ECEF's origin to GRAND's origin. Now you get a positional vector from GRAND's origin
in ECEF frame. Take a dot product of this positional vector with the NWU basis unit
vectors to get the coordinates in GRAND coordinate system.
Use inverse (transpose) of rotational matrix to convert from GRAND cs to ECEF. Then
convert from ECEF to Geodetic.
'''
def __init__(self,
arg: 'Coordinates Instance' = None,
x : Union[Number, np.ndarray] = None,
y : Union[Number, np.ndarray] = None,
z : Union[Number, np.ndarray] = None,
latitude : Union[Number, np.ndarray] = None, # latitude of LTP's location/origin
longitude: Union[Number, np.ndarray] = None, # longitude of LTP's location/origin
height : Union[Number, np.ndarray] = None, # height of LTP's location/origin
location : 'Coordinates Instance' = grand_origin,
obstime : Union[str, datetime.date] = None,
rotation = None):
super().__init__(arg = arg, # input coordinate instance to convert to LTP
x = x, # x-coordinate at LTP.
y = y, # y-coordinate at LTP
z = z, # z-coordinate at LTP
latitude = latitude, # latitude of LTP's location/origin
longitude = longitude, # longitude of LTP's location/origin
height = height, # height of LTP's location/origin
location = location, # location of LTP in Geodetic, GRAND, or ECEF
orientation = 'NWU', # orientation of LTP. 'NWU', 'ENU' etc
magnetic = True, # shift orientation by magnetic declination?
magmodel = 'IGRF13', # if shift, which magnetic model to use?
declination = None, # or simply provide the magnetic declination
obstime = obstime, # calculate declination of what date?
rotation = rotation)
def grand_to_ecef(self):
# Basis forms a rotational matrix. Transpose is the inverse of rotational matrix (real).
# Use inverse (transpose) of rotational matrix to convert from GRANDCS to ECEF.
return self.ltp_to_ecef()
def grand_to_geodetic(self):
# Convert from GRANDCS to ECEF, then from ECEF to Geodetic.
return self.ltp_to_geodetic()