angles.py
34.5 KB
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
import math
import doctest
# ========================================================
# ========================================================
# === ANGLE
# ========================================================
# ========================================================
class Angle:
""" Class to convert angles for astronomy
Angle formats are:
-----------------
deg = degrees. e.g.
rad = radian. e.g.
Usage:
------
First, instanciate an object from the class:
angle = Angle()
Second, assign a date in any angle format:
angle.angle("-0d3m28.56s")
Third, get the converted angle:
rad = angle.rad()
deg = angle.deg()
arcmin = angle.arcmin()
arcsec = angle.arcsec()
dms = angle.dms()
uspzad = angle.sexagesimal(sexagesimal_format)
Informations:
-------------
help(Angle)
Angle().infos("doctest")
Angle().infos("doc_methods")
"""
# ========================================================
# === attributs
# ========================================================
# --- multiplicators for units
_r2d = 180.0/math.pi
_d2r = 1./_r2d
_r2h = _r2d/15.0
_h2r = 1./_r2h
_r2m = _r2d*60.0
_m2r = 1./_r2m
_r2s = _r2d*3600.0
_s2r = 1./_r2s
_r2mh = _r2m/15.0
_mh2r = 1./_r2mh
_r2sh = _r2s/15.0
_sh2r = 1./_r2sh
# --- correspondances between units and multiplicator ->r
_u2r = {}
_u2r["R"] = 1
_u2r["D"] = _d2r
_u2r["M"] = _m2r
_u2r["S"] = _s2r
_u2r["H"] = _h2r
_u2r["MH"] = _mh2r
_u2r["SH"] = _sh2r
# ========================================================
# === internal methods
# ========================================================
def _init(self,angle=""):
""" Initialize internal attributes.
Inputs:
-------
angle is an angle in any supported format (cf. help(Angle))
Usage:
------
objangle = Angle()
objangle._init()
"""
self._init_angle = angle
self._init_angleformat = []
self._computed_rad = 0
self._angle_redefined()
self._computed_sexagesimal_format = "D.3"
self._rad = 0
def _angle_redefined(self):
""" Initialize internal attributes when a radian angle is just defined or redefined.
"""
self._computed_deg = 0
self._computed_arcmin = 0
self._computed_arcsec = 0
self._computed_sexagesimal = 0
self._deg = 0
self._arcmin = 0
self._arcsec = 0
self._sexagesimal = 0
self._cos_rad_modulo = math.nan
self._sin_rad_modulo = math.nan
def _is_number(self,s):
""" Return True if the string is a number else return False.
Usage:
------
>>> objangle = Angle()
>>> objangle._is_number("3e5")
True
>>> objangle._is_number("3a5")
False
"""
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def _angle_compare(self, angle, operator):
""" Comparaison of angles for various operators.
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:param operator : Operator such as == != > >= < <=
:type operator : string
:return: The logic result of the comparison.
:rtype: bool
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1._angle_compare(objangle2,">")
12.345
'56d28m'
False
.. note:: Does not account for the modulo.
"""
if isinstance(angle, Angle) == False:
angle = Angle(angle)
if self._computed_rad == 0:
self.rad()
if angle._computed_rad == 0:
angle.rad()
res = False
if (self._computed_rad == 1) and (angle._computed_rad == 1):
toeval = str(self._rad)+" "+operator+" "+str(angle._rad)
res = eval(toeval)
return res
# ========================================================
# === angle methods
# ========================================================
def angle_angle2rad(self,angle):
""" Compute radian from any angle format
Inputs:
-------
angle is an angle in any supported format (cf. help(Angle))
Usage:
------
>>> objangle = Angle()
>>> objangle.angle_angle2rad("-57d45m34s")
([('-57', 'D'), ('45', 'M'), ('34', 'S')], -1.0080924796783026)
First integer is used to check the recognized input date format:
0 = Error, format not known
Related topics:
---------------
Prefer using objangle.angle() followed by objangle.rad()
"""
str_angle = str(angle).upper()
#print("str_angle = "+str_angle)
cars = "+-.0123456789"
# --- eliminate leading characters before the first numeric cheracter
k = 0
for car in str_angle:
if (car in cars):
break
k += 1
if (k==0):
# problem, no valid numeric value found.
pass
str_angle = str_angle[k:]
# ---
# "-0 45 ' 34 sec"
# angle_numb = [ "-0", "45", "34" ]
# angle_numb = [ " ", " ' ", " sec" ]
ind_numb = -1
ind_unit = -1
angle_numbs = []
angle_units = []
knumb = 0
kunit = 0
for car in str_angle:
if (car in cars) or (car=='E' and knumb>0):
# --- numeric case for value
if (knumb==0):
ind_numb += 1
angle_numbs.append('')
angle_numbs[ind_numb] += car
knumb += 1
kunit = 0
else:
# --- alpha case for units
if (kunit==0):
ind_unit += 1
angle_units.append('')
if ( (car!=" ") and (car!=":") ):
if (car=="'"):
car = "M"
if (car=='"'):
car = "S"
angle_units[ind_unit] += car
kunit += 1
knumb = 0
# --- reformat units
#print("angle_numbs={}".format(angle_numbs))
#print("angle_units={}".format(angle_units))
n_numb = len(angle_numbs)
n_unit = len(angle_units)
tmps = [];
for angle_unit in angle_units:
if angle_unit is '':
angle_unit = ' '
tmps.append(angle_unit)
angle_units = tmps.copy()
# --- simplify units
angle_fnumbs = []
angle_funits = []
if (n_unit==0) and (n_numb==0):
angle_fnumbs.append("0")
angle_funits.append("R")
elif (n_unit==0):
angle_fnumbs.append(angle_numbs[0])
angle_funits.append("D")
elif (n_unit==1):
angle_fnumbs.append(angle_numbs[0])
u1 = angle_units[0]
if (u1[0]=="R"):
angle_funits.append("R")
elif (u1[0]=="M"):
angle_funits.append("M")
if (n_numb==2):
angle_funits.append("S")
angle_fnumbs.append(angle_numbs[1])
elif (u1[0]=="S"):
angle_funits.append("S")
elif (u1[0]=="H"):
angle_funits.append("H")
if (n_numb==2):
angle_funits.append("MH")
angle_fnumbs.append(angle_numbs[1])
else:
angle_funits.append("D")
if (n_numb==2):
angle_funits.append("M")
angle_fnumbs.append(angle_numbs[1])
elif (n_numb==2):
# cases DM or MS HM
angle_fnumbs.append(angle_numbs[0])
angle_fnumbs.append(angle_numbs[1])
u1 = angle_units[0]
u2 = angle_units[1]
if (u1[0]=="M"):
angle_funits.append("M")
angle_funits.append("S")
elif (u1[0]=="H" and u2[0]=="S"):
angle_funits.append("H")
angle_funits.append("SH")
elif (u1[0]=="H"):
angle_funits.append("H")
angle_funits.append("MH")
else:
angle_funits.append("D")
angle_funits.append("M")
elif (n_numb==3):
# cases DMS or HMS
angle_fnumbs.append(angle_numbs[0])
angle_fnumbs.append(angle_numbs[1])
angle_fnumbs.append(angle_numbs[2])
u1 = angle_units[0]
if (u1[0]=="H"):
angle_funits.append("H")
angle_funits.append("MH")
angle_funits.append("SH")
else:
angle_funits.append("D")
angle_funits.append("M")
angle_funits.append("S")
# --- compute the angle
if (angle_fnumbs=="."):
angle_fnumbs="0."
#print(angle_fnumbs)
#print(angle_funits)
init_angleformat = list ( zip (angle_fnumbs, angle_funits) )
sign = 1
k=0
rad = 0
for init_angle in init_angleformat:
numb = init_angle[0]
unit = init_angle[1]
if (k==0) and ('-' in numb):
sign = -1
fnumb = math.fabs(float(numb))
mult = self._u2r[unit]
rad += fnumb*mult
rad *= sign
self._rad = rad
self._rad_modulo_2pi = math.fmod(rad,2*math.pi)
if (self._rad_modulo_2pi<0):
self._rad_modulo_2pi += (2*math.pi)
return init_angleformat, rad
def angle_rad2deg(self,rad):
""" Compute a angle in degrees from a angle in radian
Inputs:
-------
rad is an angle in radian.
Usage:
------
>>> objangle = Angle()
>>> objangle.angle_rad2deg(1)
(0, 57.29577951308232)
First integer is an error code. 0 = no problem.
Related topics:
---------------
Prefer using objangle.angle() followed by objangle.deg()
"""
error = 0
res = rad * self._r2d
return error, res
def angle_rad2sexagesimal(self,rad, sexagesimal_format):
""" Compute a sexagesimal format string from radian
Inputs:
-------
rad is an angle in radian
sexagesimal_format is
u (unit) = h,H,d,D (default=D). Capital mean module [0:360[, lower case means module [-180:180[
s (separator) = " ",:,"" (default="" means letters hms or dms)
p (plus/minus) = +,"" (default="")
z (zeros) = 0,"" (default="")
a (angle_limits) = "",90, (+/-limit if unit D,H, default="" means 360)
d (sec_digits) = "",".1",".2",... (default="")
--- style 1:
E.g. To Display a R.A.: "H0.2" => 23h07m42.49s
E.g. To Display a Decl.: "d+090.1" => +00d34m22.6s
E.g. To Display a H.A.: "h0.2" => -08h43m16.05s
--- style 2:
E.g. To Display a R.A.: "H 0.2" => 23 07 42.49
E.g. To Display a Decl.: "d +090.1" => -00 34 22.6
E.g. To Display a H.A.: "h 0.2" => -08 43 16.05
--- style 3:
E.g. To Display a R.A.: "H:0.2" => 23:07:42.49
E.g. To Display a Decl.: "d:+090.1" => -00:34:22.6
E.g. To Display a H.A.: "h:0.2" => -08:43:16.05
Usage:
------
>>> objangle = Angle()
>>> objangle.angle_rad2sexagesimal(-0.01,"d:-090.1")
(0, '-00:34:22.6')
First integer is used to check the recognized input date format:
0 = Error, format not known
Related topics:
---------------
Prefer using objangle.angle() followed by objangle.sexagesimal()
"""
symbols = str(sexagesimal_format)
digits = ""
unit=0; # 0=deg 1=hours
separator=0; # 0=hdms 1=space 2=:
modulo=0; # 0=360 1=180
sign=0; # 0=[0:modulo] 1=[-modulo/2:modulo/2]
zeros=0; # 0=spaces 1=leading zeros
k=0
for car in symbols:
if (car=='D'):
unit=0
modulo=0
if (car=='d'):
unit=0
modulo=1
if (car=='H'):
unit=1
modulo=0
if (car=='h'):
unit=1
modulo=1
if (car==' '):
separator=1
if (car=='_'):
separator=1
if (car==':'):
separator=2
if (car=='+'):
sign=1
if (self._is_number(car)) or car=='.':
digits = symbols[k:]
break
k += 1
# int unit=0; // 0=deg 1=hours
# int modulo=0; // 0=360 1=180
# int separator=0; // 0=hdms 1=space 2=:
# int sign=0; // 0=[0:modulo] 1=[-modulo/2:modulo/2]
# === trailing format (digits)
angle_limit = 360
nb_decimalsec = 2
ld = len(digits)
if (ld>0):
# e.g. digits = "090.3"
car = digits[0]
if car=='0':
zeros=1
digits = digits[1:]
# now digits = "90.3" and zeros=1
kd = digits.find(".")
if kd==-1:
if (self._is_number(digits)==True):
angle_limit = int(digits)
else:
if (self._is_number(digits[0:kd])==True):
angle_limit = int(digits[0:kd])
if (self._is_number(digits[kd+1:])==True):
nb_decimalsec = int(digits[kd+1:])
# now digits_angle_limit = "90", digits_nb_decimal="3"
#print("ld="+str(ld))
#print("symbols="+symbols)
#print("digits="+digits)
#print("unit="+str(unit))
#print("modulo="+str(modulo))
#print("separator="+str(separator))
#print("sign="+str(sign))
#print("zeros="+str(zeros))
#print("angle_limit="+str(angle_limit))
#print("nb_decimal="+str(nb_decimalsec))
error, deg = self.angle_rad2deg(rad)
# --- all angle in [0:360[
deg = math.fmod(deg, 360)
if (deg<0):
deg = math.fmod(deg+360, 360)
#print("deg 1 ="+str(deg))
# --- case modulo [-180:180[
if modulo==1:
if deg>180:
deg-=360
#print("deg 2 ="+str(deg))
# --- limit angle
if deg>angle_limit:
deg = angle_limit
elif deg<-angle_limit:
deg = -angle_limit
#print("deg 3 ="+str(deg))
# --- sign of the input angle
s = 1
if deg<0:
s = -1
deg = -deg
# --- case hour/deg
if (unit==1):
angle = deg/15
angle_limit = angle_limit/15
else:
angle = deg
#print("angle 1 ="+str(angle))
# --- compute the trheee components of xx mm ss
r = angle
xx = int(math.floor(r))
mm = 0
ss = 0
r = (r-xx)*60
mm = int(math.floor(r))
ss = (r-mm)*60
#print("xx ="+str(xx))
#print("mm ="+str(mm))
#print("ss ="+str(ss))
# === Compute the result
result = ""
# --- sign
if sign==1 and s>=0:
result += "+"
elif s<0:
result += "-"
# --- xx
n = 1+int(math.log10(angle_limit))
if zeros==0:
fstring = "{:d}"
else:
fstring = "{:0"+str(n)+"d}"
result += fstring.format(xx)
# --- separator xx mm
if separator==1:
result += " "
elif separator==2:
result += ":"
else:
if unit==0:
result += "d"
else:
result += "h"
# --- mm
if zeros==0:
fstring = "{:d}"
else:
fstring = "{:02d}"
result += fstring.format(mm)
# --- separator mm ss
if separator==1:
result += " "
elif separator==2:
result += ":"
else:
result += "m"
# --- ss
if zeros==0:
fstring = "{:."+str(nb_decimalsec)+"f}"
else:
if nb_decimalsec==0:
fstring = "{:0"+str(nb_decimalsec+2)+".0f}"
else:
fstring = "{:0"+str(nb_decimalsec+3)+"."+str(nb_decimalsec)+"f}"
#print("fstring="+fstring)
result += fstring.format(ss)
# --- separator ss
if separator==0:
result += "s"
# -- end
return 0, result
# ========================================================
# === get/set methods
# ========================================================
def angle(self, angle=""):
""" Set the input angle in any format
Inputs:
-------
angle is an angle in any supported format (cf. help(Angle))
Usage:
------
>>> objangle = Angle()
>>> objangle.angle("23d 27m")
'23d 27m'
Related topics:
---------------
After using objangle.angle() get conversions with methods as objangle.deg()
or objdate.rad().
"""
if angle != "":
if (angle != self._init_angle):
self._init(angle)
return self._init_angle
def rad(self):
""" Get the angle in radian
Usage:
------
>>> objangle = Angle()
>>> objangle.angle("-23 d 56'")
"-23 d 56'"
>>> objangle.rad()
-0.4177154676439762
Related topics:
---------------
Before use objdate.angle() to set the input angle.
"""
if (self._computed_rad == 0):
init_angleformat, rad = self.angle_angle2rad(self._init_angle)
self._init_angleformat = init_angleformat
self._computed_rad = 1
self._rad = rad
return self._rad
def deg(self):
""" Get the angle in degrees
Usage:
------
>>> objangle = Angle()
>>> objangle.angle("-23 d 56'")
"-23 d 56'"
>>> objangle.deg()
-23.933333333333334
Related topics:
---------------
Before use objdate.angle() to set the input angle.
"""
if (self._computed_deg == 0):
if (self._computed_rad == 0):
self.rad()
if (len(self._init_angleformat) > 0):
error, deg = self.angle_rad2deg(self._rad)
if error==0:
self._computed_deg = 1
self._deg = deg
return self._deg
return -1
return self._deg
def arcmin(self):
""" Get the angle in arcmin
Usage:
------
>>> objangle = Angle()
>>> objangle.angle("-23 d 56'")
"-23 d 56'"
>>> objangle.arcmin()
-1436.0
Related topics:
---------------
Before use objdate.angle() to set the input angle.
"""
if (self._computed_arcmin == 0):
if (self._computed_rad == 0):
self.rad()
if (len(self._init_angleformat) > 0):
error, deg = self.angle_rad2deg(self._rad)
arcmin = deg*60
if error==0:
self._computed_arcmin = 1
self._arcmin = arcmin
return self._arcmin
return -1
return self._arcmin
def arcsec(self):
""" Get the angle in arcsec
Usage:
------
>>> objangle = Angle()
>>> objangle.angle("-23 d 56'")
"-23 d 56'"
>>> objangle.arcsec()
-86160.0
Related topics:
---------------
Before use objdate.angle() to set the input angle.
"""
if (self._computed_arcsec == 0):
if (self._computed_rad == 0):
self.rad()
if (len(self._init_angleformat) > 0):
error, deg = self.angle_rad2deg(self._rad)
arcsec = deg*3600
if error==0:
self._computed_arcsec = 1
self._arcsec = arcsec
return self._arcsec
return -1
return self._arcsec
def sexagesimal(self,sexagesimal_format):
""" Get the angle in sexagesimal
Inputs:
-------
rad is an angle in radian
sexagesimal_format is
u (unit) = h,H,d,D (default=D). Capital mean module [0:360[, lower case means module [-180:180[
s (separator) = " ",:,"" (default="" means letters hms or dms)
p (plus/minus) = +,"" (default="")
z (zeros) = 0,"" (default="")
a (angle_limits) = "",90, (+/-limit if unit D,H, default="" means 360)
d (sec_digits) = "",".1",".2",... (default="")
--- style 1:
E.g. To Display a R.A.: "H0.2" => 23h07m42.49s
E.g. To Display a Decl.: "d+090.1" => +00d34m22.6s
E.g. To Display a H.A.: "h0.2" => -08h43m16.05s
--- style 2:
E.g. To Display a R.A.: "H 0.2" => 23 07 42.49
E.g. To Display a Decl.: "d +090.1" => -00 34 22.6
E.g. To Display a H.A.: "h 0.2" => -08 43 16.05
--- style 3:
E.g. To Display a R.A.: "H:0.2" => 23:07:42.49
E.g. To Display a Decl.: "d:+090.1" => -00:34:22.6
E.g. To Display a H.A.: "h:0.2" => -08:43:16.05
Usage:
------
>>> objangle = Angle()
>>> objangle.angle(-0.57)
-0.57
>>> objangle.sexagesimal("d:-090.1")
'-00:34:12.0'
Related topics:
---------------
Before use objdate.angle() to set the input angle.
"""
if (self._computed_sexagesimal == 0) or (sexagesimal_format != self._computed_sexagesimal_format):
if (self._computed_rad == 0):
self.rad()
if (len(self._init_angleformat) > 0):
error, sexagesimal = self.angle_rad2sexagesimal(self._rad, sexagesimal_format)
if error==0:
self._computed_sexagesimal = 1
self._sexagesimal = sexagesimal
self._computed_sexagesimal_format = sexagesimal_format
return self._sexagesimal
return -1
return self._sexagesimal
# ========================================================
# === debug methods
# ========================================================
def infos(self, action) -> None:
""" To get informations about this class
:param action: A command to run a debug action (see examples).
:type action: string
:Example:
Angle().infos("doctest")
Angle().infos("doc_methods")
Angle().infos("internal_attributes")
Angle().infos("public_methods")
"""
if (action == "doc_methods"):
publics = [x for x in dir(self) if x[0]!="_"]
for public in publics:
varname = "{}".format(public)
if (callable(getattr(self,varname))==True):
print("\n{:=^40}".format(" method "+varname+" "))
t = "Angle()."+varname+".__doc__"
tt =eval(t)
print(tt)
if (action == "doctest"):
if __name__ == "__main__":
print("\n{:~^40}".format("doctest"))
#doctest.testmod(verbose=True, extraglobs={'objangle': Angle()})
doctest.testmod(verbose=True)
if (action == "internal_attributes"):
internals = [x for x in dir(self) if x[0]=="_" and x[1]!="_"]
for internal in internals:
varname = "{}".format(internal)
#if (hasattr(self,varname)==True):
if (callable(getattr(self,varname))==False):
print(varname + "=" + str(getattr(self,varname)))
if (action == "public_methods"):
publics = [x for x in dir(self) if x[0]!="_"]
for public in publics:
varname = "{}".format(public)
if (callable(getattr(self,varname))==True):
print(varname)
# ========================================================
# === special methods
# ========================================================
def __init__(self, angle=""):
""" Object initialization where angle is the input in any format.
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : string
"""
self._init(angle)
def __add__(self, angle):
""" Add an angle to an angle.
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The result of the addition
:rtype: Angle()
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle = objangle1 + objangle2 ; objangle.sexagesimal("D")
12.345
'56d28m'
'68d48m42.00s'
"""
if isinstance(angle, Angle) == False:
angle = Angle(angle)
res = Angle()
if self._computed_rad == 0:
self.rad()
if angle._computed_rad == 0:
angle.rad()
if (self._computed_rad == 1) and (angle._computed_rad == 1):
rad = self._rad + angle._rad
deg = rad * self._r2d
res = Angle(deg)
return res
def __radd__(self, angle):
""" Right addition an angle to an angle.
"""
return self + angle
def __iadd__(self, angle):
""" Add an angle to an angle.
"""
return self + angle
def __sub__(self, angle):
""" Subtract an angle to an angle.
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The result of the subtraction
:rtype: Angle()
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle = objangle1 - objangle2 ; objangle.sexagesimal("D")
12.345
'56d28m'
'315d52m42.00s'
"""
if isinstance(angle, Angle) == False:
angle = Angle(angle)
res = Angle()
if self._computed_rad == 0:
self.rad()
if angle._computed_rad == 0:
angle.rad()
if (self._computed_rad == 1) and (angle._computed_rad == 1):
rad = self._rad - angle._rad
deg = rad * self._r2d
res = Angle(deg)
return res
def __rsub__(self, angle):
""" Right subtraction only an angle to an angle.
"""
if isinstance(angle, Angle) == True:
return self - angle
else:
return angle
def __isub__(self, angle):
""" Subtract an angle to an angle.
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The result of the subtraction
:rtype: Angle()
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1 -= objangle2 ; objangle1.sexagesimal("D")
12.345
'56d28m'
'315d52m42.00s'
"""
return self - angle
def __eq__(self, angle):
""" Comparaison of angles. Return True if angles are defined and equals.
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The logic result of the comparison.
:rtype: bool
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1 == objangle2
12.345
'56d28m'
False
.. note:: Does not account for the modulo.
"""
return self._angle_compare(angle, "==")
def __ne__(self, angle):
""" Comparaison of angles. Return True if angles are defined and not equals.
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The logic result of the comparison.
:rtype: bool
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1 != objangle2
12.345
'56d28m'
True
.. note:: Does not account for the modulo.
"""
return self._angle_compare(angle, "!=")
def __gt__(self, angle):
""" Comparaison of angles. Return True if self > angle
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The logic result of the comparison.
:rtype: bool
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1 > objangle2
12.345
'56d28m'
False
.. note:: Does not account for the modulo.
"""
return self._angle_compare(angle,">")
def __ge__(self, angle):
""" Comparaison of angles. Return True if self >= angle
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The logic result of the comparison.
:rtype: bool
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1 >= objangle2
12.345
'56d28m'
False
.. note:: Does not account for the modulo.
"""
return self._angle_compare(angle,">=")
def __lt__(self, angle):
""" Comparaison of angles. Return True if self < angle
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The logic result of the comparison.
:rtype: bool
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1 < objangle2
12.345
'56d28m'
True
.. note:: Does not account for the modulo.
"""
return self._angle_compare(angle,"<")
def __le__(self, angle):
""" Comparaison of angles. Return True if self <= angle
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: The logic result of the comparison.
:rtype: bool
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(12.345) ; objangle2.angle("56d28m") ; objangle1 <= objangle2
12.345
'56d28m'
True
.. note:: Does not account for the modulo.
"""
return self._angle_compare(angle,"<=")
def __mod__(self, angle):
""" Modulo of an angle by another angle. Return an angle
:param angle : An angle in any supported format (cf. help(Angle))
:type angle : Angle()
:return: An angle
:rtype: Angle()
:Example:
>>> objangle1 = Angle()
>>> objangle2 = Angle()
>>> objangle1.angle(30.56) ; objangle2.angle(2.34) ; (objangle1 % objangle2).deg()
30.56
2.34
0.14000000000000412
"""
if isinstance(angle, Angle) == False:
angle = Angle(angle)
if self._computed_deg == 0:
self.deg()
if angle._computed_deg == 0:
angle.deg()
anglemod = Angle()
if (self._computed_deg == 1) and (angle._computed_deg == 1):
deg = math.fmod(self._deg, angle._deg)
if (deg<0):
deg += angle._deg
anglemod.angle(deg)
return anglemod
def __mul__(self, multiplier):
""" multiplication of an angle by a float or int. Return an angle
:param multiplier: A real number
:type multiplier: float
:return: An angle
:rtype: Angle()
:Example:
>>> objangle = Angle()
>>> objangle.angle(30.43) ; (objangle*2).deg()
30.43
60.86000000000001
"""
if isinstance(multiplier, (int, float)) == False:
raise TypeError
return ""
if self._computed_deg == 0:
self.deg()
anglemult = Angle()
if (self._computed_deg == 1):
deg = self._deg * multiplier
anglemult.angle(deg)
return anglemult
def __rmul__(self, multiplier):
""" Right multiplication of an angle by a float or int.
"""
return self * multiplier
def __truediv__(self, divisor):
""" division of an angle by a float or int. Return an angle
:param divisor: A real number
:type divisor: float
:return: An angle
:rtype: Angle()
:Example:
>>> objangle = Angle()
>>> objangle.angle(30.43) ; (objangle/2).deg()
30.43
15.215000000000002
"""
if isinstance(divisor, (int, float)) == False:
raise TypeError
return ""
if (divisor==0):
raise ZeroDivisionError
if self._computed_deg == 0:
self.deg()
anglediv = Angle()
if (self._computed_deg == 1):
deg = self._deg / divisor
anglediv.angle(deg)
return anglediv
# ========================================================
# ========================================================
# ========================================================