Package: angles
The Angles module defines several classes for representing angles, and positions
on a sphere. It also has several functions for performing common operations
on angles, such as unit conversion, normalization, creating string
representations and others.The position of M100 reported by SIMBAD is “12 22 54.899 +15 49 20.57”. We can
easily parse these coordinates as follows:>>>from__future__importprint_function>>>fromanglesimportAngularPosition>>>a=AngularPosition.from_hd("12 22 54.899 +15 49 20.57")>>>a.alpha3.24157813039>>>a.delta0.276152636198>>>print(a.alpha)+12HH22MM54.899SS>>>print(a.delta)+15DD49MM20.570SS>>>a.alpha.hms.hms(1,12,22,54.899)>>>a.delta.dms.dms(1,15,49,20.57)>>>a.alpha.dms.dms(1,185,43,43.485)>>>a.delta.hms.hms(1,1,3,17.371)>>>a.alpha.r,a.alpha.d,a.alpha.h,a.alpha.arcs(3.2415781303913653,185.72874583333328,12.381916388888886,668623.4849999998)>>>a.delta.r,a.delta.d,a.delta.h,a.delta.arcs(0.27615263619797403,15.822380555555556,1.0548253703703705,56960.57)InstallationUsepiporeasy_install:$ pip install anglesor,$ easy_install anglesTests are in the filetest_angles.py.ExamplesSome examples are given below. For more details see docstrings of functions and
classes.Unit conversionConvert between radians, degrees, hours and arc-seconds.>>>importmath>>>fromanglesimportr2d,r2arcs,h2r,h2d,d2arcs>>>r2d(math.pi)180.0>>>r2arcs(math.pi)648000.0>>>h2r(12.0)3.141592653589793>>>h2d(12.0)180.0>>>d2arcs(1.0)3600.0Normalizing anglesNormalize value between two limits usingnormalize.>>>fromanglesimportnormalize>>>normalize(-180,-180,180)-180.0>>>normalize(180,-180,180)-180.0>>>normalize(180,-180,180,b=True)180.0>>>normalize(181,-180,180)-179.0>>>normalize(181,-180,180,b=True)179.0>>>normalize(-180,0,360)180.0>>>normalize(36,0,24)12.0>>>normalize(368.5,-180,180)8.5>>>normalize(-100,-90,90)80.0>>>normalize(-100,-90,90,b=True)-80.0>>>normalize(100,-90,90,b=True)80.0>>>normalize(181,-90,90,b=True)-1.0>>>normalize(270,-90,90,b=True)-90.0>>>normalize(271,-90,90,b=True)-89.0Normalizing angles on a sphereSimplify point on sphere to simplest representation usingnormalize_sphere.>>>fromanglesimportnormalize_sphere>>>normalize_sphere(180,91)(0.0,89.0000000000001)>>>normalize_sphere(180,-91)(0.0,-89.0000000000001)>>>normalize_sphere(0,91)(180.0,89.0000000000001)>>>normalize_sphere(0,-91)(180.0,-89.0000000000001)>>>normalize_sphere(120,280)(119.99999999999999,-80.00000000000003)>>>normalize_sphere(375,45)# 25 hours ,45 degrees(14.999999999999966,44.99999999999999)>>>normalize_sphere(-375,-45)(345.00000000000006,-44.99999999999999)Sexagesimal representationConvert decimal value into sexagesimal representation.>>>fromanglesimportdeci2sexa>>>deci2sexa(-11.2345678)(-1,11,14,4.444)>>>deci2sexa(-11.2345678,pre=5)(-1,11,14,4.44408)>>>deci2sexa(-11.2345678,pre=4)(-1,11,14,4.4441)>>>deci2sexa(-11.2345678,pre=4,trunc=True)(-1,11,14,4.444)>>>deci2sexa(-11.2345678,pre=1)(-1,11,14,4.4)>>>deci2sexa(-11.2345678,pre=0)(-1,11,14,4.0)>>>deci2sexa(-11.2345678,pre=-1)(-1,11,14,0.0)>>>x=23+59/60.0+59.99999/3600.0>>>deci2sexa(x,pre=3,lower=0,upper=24)(1,24,0,0.0)>>>deci2sexa(x,pre=3,lower=0,upper=24,upper_trim=True)(1,0,0,0.0)>>>deci2sexa(x,pre=5,lower=0,upper=24,upper_trim=True)(1,23,59,59.99999)Formatting anglesFormat an angle into various string representations usingfmt_angle.>>>fromanglesimportfmt_angle>>>fmt_angle(12.348978659,pre=4,trunc=True)'+12 20 56.3231'>>>fmt_angle(12.348978659,pre=5)'+12 20 56.32317'>>>fmt_angle(12.348978659,s1='HH ',s2='MM ',s3='SS',pre=5)'+12HH 20MM 56.32317SS'>>>x=23+59/60.0+59.99999/3600.0>>>fmt_angle(x)'+24 00 00.000'>>>fmt_angle(x,lower=0,upper=24,upper_trim=True)'+00 00 00.000'>>>fmt_angle(x,pre=5)'+23 59 59.99999'>>>fmt_angle(-x,lower=0,upper=24,upper_trim=True)'+00 00 00.000'>>>fmt_angle(-x)'-24 00 00.000'Parsing sexagesimal stringsParse a sexagesimal number from a string usingphmsdms.>>>fromanglesimportphmsdms>>>phmsdms("12")=={...'parts':[12.0,None,None],...'sign':1,...'units':'degrees',...'vals':[12.0,0.0,0.0]...}True>>>phmsdms("12h")=={...'parts':[12.0,None,None],...'sign':1,...'units':'hours',...'vals':[12.0,0.0,0.0]...}True>>>phmsdms("12d13m14.56")=={...'parts':[12.0,13.0,14.56],...'sign':1,...'units':'degrees',...'vals':[12.0,13.0,14.56]...}True>>>phmsdms("12d14.56ss")=={...'parts':[12.0,None,14.56],...'sign':1,...'units':'degrees',...'vals':[12.0,0.0,14.56]...}True>>>phmsdms("14.56ss")=={...'parts':[None,None,14.56],...'sign':1,...'units':'degrees',...'vals':[0.0,0.0,14.56]...}True>>>phmsdms("12h13m12.4s")=={...'parts':[12.0,13.0,12.4],...'sign':1,...'units':'hours',...'vals':[12.0,13.0,12.4]...}True>>>phmsdms("12:13:12.4s")=={...'parts':[12.0,13.0,12.4],...'sign':1,...'units':'degrees',...'vals':[12.0,13.0,12.4]...}TrueParse string containing angular positionParse coordinates of a point on sphere usingpposition.>>>fromanglesimportpposition>>>ra,de=pposition("12 22 54.899 +15 49 20.57")>>>ra12.38191638888889>>>de15.822380555555556>>>pposition("12 22 54.899 +15 49 20.57",details=True)# doctest: +SKIP{'y':15.822380555555556,'x':12.38191638888889,'numvals':6,'raw_x':{'vals':[12.0,22.0,54.899],'units':'degrees','parts':[12.0,22.0,54.899],'sign':1},'raw_y':{'vals':[15.0,49.0,20.57],'units':'degrees','parts':[15.0,49.0,20.57],'sign':1}}Separation angle along a great circleFind angular separation along a great circle usingsep. This function
uses vectors to find the angle of separation.>>>fromanglesimportr2d,d2r,sep>>>r2d(sep(0,0,0,d2r(90.0)))90.0>>>r2d(sep(0,d2r(45.0),0,d2r(90.0)))45.00000000000001>>>r2d(sep(0,d2r(-45.0),0,d2r(90.0)))135.0>>>r2d(sep(0,d2r(-90.0),0,d2r(90.0)))180.0>>>r2d(sep(d2r(45.0),d2r(-90.0),d2r(45.0),d2r(90.0)))180.0>>>r2d(sep(0,0,d2r(90.0),0))90.0>>>r2d(sep(0,d2r(45.0),d2r(90.0),d2r(45.0)))60.00000000000001>>>importmath>>>90.0*math.cos(d2r(45.0))# Distance along latitude circle.63.63961030678928Bearing between two pointsFind bearing of one point with respect to another usingbear. Likesepthis function uses vectors.>>>fromanglesimportbear,r2d,d2r>>>bear(0,0,0,-d2r(90.0))3.141592653589793>>>bear(0,-d2r(90.0),0,0)0.0>>>bear(0,-d2r(45.0),0,0)0.0>>>bear(0,-d2r(89.678),0,0)0.0>>>r2d(bear(d2r(45.0),d2r(45.0),d2r(46.0),d2r(45.0)))89.64644212193384>>>r2d(bear(d2r(45.0),d2r(45.0),d2r(44.0),d2r(45.0)))-89.64644212193421Angle classClass for representing an angle, conversion between different
units, generating string representations.>>>from__future__importprint_function>>>fromanglesimportAngle>>>a=Angle(sg="12h34m16.592849219")>>>a.r,a.d,a.h,a.arcs# doctest: +NORMALIZE_WHITESPACE(3.291152306055805,188.56913687174583,12.571275791449722,678848.892738285)>>>a.hms.sign,a.hms.hh,a.hms.mm,a.hms.ss(1,12,34,16.593)>>>a.hms.hms(1,12,34,16.593)>>>a.h12.571275791449722>>>a.dms.sign,a.dms.dd,a.dms.mm,a.dms.ss(1,188,34,8.893)>>>a.dms.dms(1,188,34,8.893)>>>a.d188.56913687174583>>>print(a.ounit)hours>>>print(a)+123416.593>>>a.pre,a.trunc(3,False)>>>a.pre=4>>>print(a)+123416.5928>>>a.pre=3>>>a.trunc=True>>>print(a)+123416.592>>>a.ounit="degrees">>>print(a)+1883408.892>>>a.ounit="radians">>>print(a)# doctest: +SKIP3.29115230606>>>a.ounit="degrees">>>a.s1="DD ">>>a.s2="MM ">>>a.s3="SS">>>print(a)+188DD34MM08.892SS>>>a=Angle(r=10)>>>a.d,a.h,a.r,a.arcs,a.ounit# doctest: +NORMALIZE_WHITESPACE(572.9577951308232,38.197186342054884,10,2062648.0624709637,'radians')>>>a.d=10>>>a.d,a.h,a.r,a.arcs,a.ounit# doctest: +NORMALIZE_WHITESPACE(10.0,0.6666666666666666,0.17453292519943295,36000.0,'radians')>>>a.dms.mm=60>>>a.d,a.h,a.r,a.arcs,a.ounit# doctest: +NORMALIZE_WHITESPACE(11.0,0.7333333333333333,0.19198621771937624,39600.0,'radians')>>>a.dms.dms=(1,12,10,5.234)>>>a.d,a.h,a.r,a.arcs,a.ounit# doctest: +NORMALIZE_WHITESPACE(12.168120555555557,0.8112080370370371,0.21237376747404604,43805.234000000004,'radians')>>>a.hms.hms=(1,1,1,1)>>>a.d,a.h,a.r,a.arcs,a.ounit# doctest: +NORMALIZE_WHITESPACE(15.254166666666668,1.0169444444444444,0.2662354329813017,54915.00000000001,'radians')>>>print(a)# doctest: +SKIP0.266235432981>>>a.ounit='hours'>>>print(a)+010101.000>>>a.ounit='degrees'>>>print(a)+151515.000Class for longitudinal anglesA subclass ofAnglethat is normalized to the range[0, 24), i.e., a
Right Ascension like angle. Theounitattribute is always “hours”.>>>from__future__importprint_function>>>fromanglesimportAlphaAngle>>>a=AlphaAngle(d=180.5)>>>print(a)+12HH02MM00.000SS>>>a=AlphaAngle(h=12.0)>>>print(a)+12HH00MM00.000SS>>>a=AlphaAngle(h=-12.0)>>>a=AlphaAngle("12h14m23.4s")>>>print(a)+12HH14MM23.400SS>>>a.r,a.d,a.h,a.arcs(3.204380873430289,183.5975,12.239833333333333,660951.0)>>>a=AlphaAngle(h=12.54678345)>>>a.hms.hms(1,12,32,48.42)>>>a.hms.sign,a.hms.hh,a.hms.mm,a.hms.ss(1,12,32,48.42)>>>print(a)+12HH32MM48.420SS>>>a.pre=5>>>a.hms.hms(1,12,32,48.42042)>>>print(a)+12HH32MM48.42042SS>>>a.s1=" : ">>>a.s2=" : ">>>a.s3="">>>print(a)+12:32:48.42042>>>a.pre=3>>>a.dms.dms(1,188,12,6.306)>>>a=AlphaAngle(h=25.0)>>>print(a)+01HH00MM00.000SS>>>a=AlphaAngle(h=-1.0)>>>print(a)+23HH00MM00.000SS>>>a.hms.hh=23>>>a.hms.mm=59>>>a.hms.ss=59.99999>>>a.hms.hms(1,0,0,0.0)>>>print(a)+00HH00MM00.000SS>>>a.pre=5>>>a.hms.hms(1,23,59,59.99999)>>>print(a)+23HH59MM59.99999SSClass for latitudinal anglesA subclass ofAnglethat is normalized to the range[-90,90], i.e., a
Declination like angle. Theounitattribute is always “degrees”.>>>from__future__importprint_function>>>fromanglesimportDeltaAngle>>>a=DeltaAngle(d=-45.0)>>>print(a)-45DD00MM00.000SS>>>a=DeltaAngle(d=180.0)>>>print(a)+00DD00MM00.000SS>>>a=DeltaAngle(h=12.0)>>>print(a)+00DD00MM00.000SS>>>a=DeltaAngle(sg="91d")>>>print(a)+89DD00MM00.000SS>>>a=DeltaAngle("12d23m14.2s")>>>print(a)+12DD23MM14.200SS>>>a.r,a.d,a.h,a.arcs(0.2161987825813487,12.387277777777777,0.8258185185185185,44594.2)>>>a=DeltaAngle(d=12.1987546)>>>a.dms.dms(1,12,11,55.517)>>>a.pre=5>>>a.dms.dms(1,12,11,55.51656)>>>a.dms.dd,a.dms.mm,a.dms.ss(12,11,55.51656)>>>a.pre=0>>>a.dms.dms(1,12,11,56.0)>>>a=DeltaAngle(d=12.3459876)>>>a.s1=" : ">>>a.s2=" : ">>>a.s3="">>>print(a)+12:20:45.555>>>a=DeltaAngle(d=-91.0)>>>print(a)-89DD00MM00.000SS>>>a=DeltaAngle(d=91.0)>>>print(a)+89DD00MM00.000SS>>>a.dms.sign=1>>>a.dms.dd=89>>>a.dms.mm=59>>>a.dms.ss=59.9999>>>a.pre=3>>>print(a)+90DD00MM00.000SS>>>a.pre=5>>>print(a)+89DD59MM59.99990SS>>>a.dms.dms=(1,0,0,0.0)>>>a.dms.dd=89>>>a.dms.mm=60>>>a.dms.ss=60>>>a.pre=3>>>print(a)+89DD59MM00.000SSClass for points on a unit sphereA class for representing a point on a sphere. The input angle values are
normalized to get the simplest representation of the coordinates of the point.>>>from__future__importprint_function>>>fromanglesimportAngularPosition,r2d>>>a=AngularPosition.from_hd("12 22 54.899 +15 49 20.57")>>>print(a)+12HH22MM54.899SS+15DD49MM20.570SS>>>a=AngularPosition.from_hd("12dd 22 54.899 +15 49 20.57")>>>print(a)+00HH49MM31.660SS+15DD49MM20.570SS>>>a=AngularPosition.from_hd("12d 22 54.899 +15 49 20.57")>>>print(a)+00HH49MM31.660SS+15DD49MM20.570SS>>>a=AngularPosition(alpha=165,delta=-91)# alpha should flip by 180 degrees>>>round(a.alpha.d,12),round(a.delta.d,12)(345.0,-89.0)>>>a.delta.d=-91# alpha should now do another 180 flip and come back to 165>>>round(a.alpha.d,12),round(a.delta.d,12)(165.0,-89.0)>>>a.delta.d=89# there should be no change in normalized angles>>>round(a.alpha.d,12),round(a.delta.d,12)(165.0,89.0)>>>a.alpha.d=-180# alpha should normalize to 180 delta shouldn't change>>>round(a.alpha.d,12),round(a.delta.d,12)(180.0,89.0)>>>pos1=AngularPosition(alpha=12.0,delta=90.0)>>>pos2=AngularPosition(alpha=12.0,delta=0.0)>>>r2d(pos2.bear(pos1))0.0>>>r2d(pos1.bear(pos2))0.0>>>r2d(pos1.sep(pos2))90.0>>>pos1.alpha.h=0.0>>>pos2.alpha.h=0.0>>>r2d(pos1.sep(pos2))90.0>>>r2d(pos2.bear(pos1))0.0>>>r2d(pos1.bear(pos2))0.0CreditsSome of the functions are adapted from theTPMC library byJeffrey
W. Percival.LicenseReleased under BSD; see LICENSE.txt.For comments and suggestions, email to userprasanthhnin thegmail.comdomain.