Debian Codegraph @

gavo/stc/conform.py

raw
1 """
2 "Conforming" of STCSpecs, i.e., bringing one to the system and units of the
3 other.
4
5 You can only conform STCSpecs rather than individual components, since
6 usually you need the whole information for the transformation (e.g.,
7 space and time for velocities.
8 """
9
10 #c Copyright 2008-2025, the GAVO project <[email protected]>
11 #c
12 #c This program is free software, covered by the GNU GPL. See the
13 #c COPYING file in the source distribution.
14
15
16 from gavo.stc import sphermath
17 from gavo.stc import times
18 from gavo.stc import units
19 from gavo.stc import common
20
21
22 class SphercLoader(object):
23 """A hack to delay loading of spherc.
24
25 We should probably use one of the many lazy import solutions and use
26 it for both what we're doing here and in coords.AstWCSLoader.
27 """
28 def __getattr__(self, *args):
29 from gavo.stc import spherc
30 globals()["spherc"] = spherc
31 return getattr(spherc, *args)
32
33 spherc = SphercLoader()
34
35
36 _conformedAttributes = [("time", "timeAs"), ("place", "areas"),
37 ("freq", "freqAs"), ("redshift", "redshiftAs"), ("velocity", "velocityAs")]
38
39
40 def _transformAreas(areas, sTrafo, srcFrame, destFrame):
41 newAreas = []
42 for a in areas:
43 if a.frame is not srcFrame:
44 raise common.STCError("Cannot transform areas in frame different from"
45 " from the position frame.")
46 newAreas.append(a.getTransformed(sTrafo, destFrame))
47 return newAreas
48
49 i = [0]
50
51 def iterSpatialChanges(baseSTC, sysSTC, slaComp=False):
52 """yields changes to baseSTC to bring places and velocities to the
53 system and units of sysSTC.
54
55 sixTrans is a sphermath.SVConverter instance.
56
57 If the frame or units are not defined in sysSTC, there are taken from
58 baseSTC.
59 """
60 if baseSTC.place is None or sysSTC.place is None:
61 return # nothing to conform in space
62 sixTrans = sphermath.SVConverter.fromSTC(baseSTC, slaComp=slaComp)
63 destFrame, srcFrame = sysSTC.place.frame, baseSTC.place.frame
64
65 bPlace, bVel = baseSTC.place, baseSTC.velocity
66 trafo = spherc.getTrafoFunction(baseSTC.place.frame.asTriple(),
67 sysSTC.place.frame.asTriple(), sixTrans)
68 if bPlace.value:
69 sv = trafo(sixTrans.to6(baseSTC.place.value,
70 getattr(baseSTC.velocity, "value", None)), sixTrans)
71 pos, vel = sixTrans.from6(sv)
72 else:
73 pos, vel = None, None
74
75 # build spatial items to change if necessary
76 if bPlace:
77 yield "place", bPlace.change(value=pos, frame=destFrame)
78 if baseSTC.areas:
79 yield "areas", _transformAreas(baseSTC.areas,
80 sixTrans.getPlaceTransformer(trafo), srcFrame, destFrame)
81
82 # build velocity items to change if necessary
83 if bVel:
84 yield "velocity", bVel.change(value=vel, frame=destFrame)
85 if baseSTC.velocityAs:
86 yield "velocityAs", _transformAreas(baseSTC.velocityAs,
87 sixTrans.getVelocityTransformer(trafo, bPlace.value), srcFrame,
88 destFrame)
89
90
91 def iterTemporalChanges(baseSTC, sysSTC):
92 if baseSTC.time is None or sysSTC.time is None:
93 return # nothing to conform in time
94 destFrame = sysSTC.time.frame
95 transform = times.getTransformFromSTC(baseSTC, sysSTC)
96 if transform is None:
97 return # we're already conforming
98 if baseSTC.time.value:
99 yield "time", baseSTC.time.change(value=transform(
100 baseSTC.time.value), frame=destFrame)
101 if baseSTC.timeAs:
102 yield "timeAs", tuple(ta.getTransformed(transform, destFrame)
103 for ta in baseSTC.timeAs)
104
105
106 def conformUnits(baseSTC, sysSTC):
107 """returns baseSTC in the units of sysSTC.
108 """
109 changes = []
110 for attName, dependentName in _conformedAttributes:
111 # TODO: For time, we'd probably have to turn JD, MJD, ISO into
112 # each other. Let's wait for sane STC.
113 changes.extend(units.iterUnitAdapted(baseSTC, sysSTC,
114 attName, dependentName))
115 return baseSTC.change(**dict(changes))
116
117
118 def conformSystems(baseSTC, sysSTC, relativistic=False, slaComp=False):
119 """conforms places and velocities in fromSTC with toSTC including
120 precession and reference frame fixing.
121 """
122 changes = [("astroSystem", sysSTC.astroSystem)]
123 changes.extend(iterSpatialChanges(baseSTC, sysSTC, slaComp=slaComp))
124 changes.extend(iterTemporalChanges(baseSTC, sysSTC))
125 return conformUnits(baseSTC.change(**dict(changes)), sysSTC)
126
127
128 def conform(baseSTC, sysSTC, **kwargs):
129 """returns baseSTC in the units and the system of sysSTC.
130 """
131 if baseSTC.astroSystem==sysSTC.astroSystem:
132 return conformUnits(baseSTC, sysSTC)
133 else:
134 return conformSystems(baseSTC, sysSTC, **kwargs)
135
136
137 def getSimple2Converter(srcSTC, destSTC, slaComp=True):
138 """returns a function that transforms lat and long in srcSTC to lat and
139 long in destSTC.
140
141 srcSTC and destSTC are ASTs. Of course, all this
142 only works if srcSTC and destSTC are both SPHER2-flavored.
143 """
144 sixTrans = sphermath.SVConverter.fromSTC(srcSTC, slaComp=slaComp)
145 trafo = spherc.getTrafoFunction(srcSTC.place.frame.asTriple(),
146 destSTC.place.frame.asTriple(), sixTrans)
147
148 def convert(ra, dec):
149 pos, vel = sixTrans.from6(
150 trafo(sixTrans.to6((ra, dec), None), sixTrans))
151 return pos[0], pos[1]
152
153 return convert