Debian Codegraph @

gavo/stc/stcsgen.py

raw symbols SCIP
1 """
2 Converting ASTs to STC-S.
3
4 The strategy here is to first generate an STCS CST, remove defaults from it
5 and then flatten out the whole thing.
6
7 The AST serializers here either return a dictionary, which is then
8 updated to the current node's dictionary, or a tuple of key and value,
9 which is then added to the current dictionary.
10 """
11
12 #c Copyright 2008-2025, the GAVO project <[email protected]>
13 #c
14 #c This program is free software, covered by the GNU GPL. See the
15 #c COPYING file in the source distribution.
16
17
18 import datetimescip-python python python-stdlib 3.11 datetime/__init__:
19 import itertoolsscip-python python python-stdlib 3.11 itertools/__init__:
20
21 from gavoscip-python python gavodachs 2.13 gavo/__init__: import utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:
22 from gavo.stcscip-python python gavodachs 2.13 `gavo.stc`/__init__: import commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:
23 from gavo.stcscip-python python gavodachs 2.13 `gavo.stc`/__init__: import dmscip-python python gavodachs 2.13 `gavo.stc.dm`/__init__:
24 from gavo.stcscip-python python gavodachs 2.13 `gavo.stc`/__init__: import stcsscip-python python gavodachs 2.13 `gavo.stc.stcs`/__init__:
25 from gavo.stcscip-python python gavodachs 2.13 `gavo.stc`/__init__: import syslibscip-python python gavodachs 2.13 `gavo.stc.syslib`/__init__:
26
27
28 def _combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().(*dictsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().(dicts)):
29 """updates the first dictionary with all further ones and returns it.
30
31 If duplicate keys exist, later arguments will overwrite values set
32 by earlier arguments.
33 """
34 res = dictsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().(dicts)[0]
35 for d in dictsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().(dicts)[1:]:
36 res.update(d)
37 return res
38
39
40 ############## Reference Frames to CST
41
42 def refPosToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(node)):
43 return {
44 "refpos": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(node).standardOrigin,
45 "planetaryEphemeris": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(node).planetaryEphemeris,}
46
47 stcsFlavorsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/stcsFlavors. = {
48 (2, "SPHERICAL"): "SPHER2",
49 (3, "SPHERICAL"): "SPHER3",
50 (3, "UNITSPHERE"): "UNITSPHER",
51 (1, "CARTESIAN"): "CART1",
52 (2, "CARTESIAN"): "CART2",
53 (3, "CARTESIAN"): "CART3",
54 }
55
56 def _computeFlavorscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_computeFlavor().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_computeFlavor().(node)):
57 try:
58 return stcsFlavorsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/stcsFlavors.[(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_computeFlavor().(node).nDim, nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_computeFlavor().(node).flavor)]
59 except KeyErrorscip-python python python-stdlib 3.11 builtins/KeyError#:
60 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCValueErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCValueError#("Coordinate Frame %s cannot"
61 " be represented it STC-S"%nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_computeFlavor().(node))
62
63 # Simple translations of STC-X reference frame literals to STC-S
64 _frameTransscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_frameTrans. = {
65 "GALACTIC_II": "GALACTIC",
66 None: "UNKNOWNFrame",
67 }
68
69 def _spaceFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(node)):
70 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(node) is None:
71 return {"frame": "UNKNOWNFrame"}
72
73 frame = nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(node).refFrame
74 frame = _frameTransscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_frameTrans..getscip-python python python-stdlib 3.11 builtins/dict#get().(frame, frame)
75 return _combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().({
76 "flavor": _computeFlavorscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_computeFlavor().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(node)),
77 "frame": frame,
78 "equinox": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(node).equinox,},
79 refPosToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(node).refPos))
80
81 def _timeFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeFrameToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeFrameToCST().(node)):
82 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeFrameToCST().(node) is None:
83 return {}
84 return _combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().({
85 "timescale": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeFrameToCST().(node).timeScale,},
86 refPosToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeFrameToCST().(node).refPos))
87
88 def _spectralFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spectralFrameToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spectralFrameToCST().(node)):
89 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spectralFrameToCST().(node) is None:
90 return {}
91 return refPosToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spectralFrameToCST().(node).refPos)
92
93 def _redshiftFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftFrameToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftFrameToCST().(node)):
94 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftFrameToCST().(node) is None:
95 return {}
96 return _combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().({
97 "redshiftType": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftFrameToCST().(node).type,
98 "dopplerdef": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftFrameToCST().(node).dopplerDef,},
99 refPosToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/refPosToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftFrameToCST().(node).refPos))
100
101
102 ############### Coordinates to CST
103
104 def _wiggleToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(node), nDimscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(nDim)):
105 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(node) is None:
106 return
107 if isinstance(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(node), dmscip-python python gavodachs 2.13 `gavo.stc.dm`/__init__:.CooWigglescip-python python gavodachs 2.13 `gavo.stc.dm`/CooWiggle#):
108 return nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(node).values
109 elif isinstance(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(node), dmscip-python python gavodachs 2.13 `gavo.stc.dm`/__init__:.RadiusWigglescip-python python gavodachs 2.13 `gavo.stc.dm`/RadiusWiggle#):
110 return tuplescip-python python python-stdlib 3.11 builtins/tuple#((r,)*nDimscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(nDim) for r in nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(node).radii)
111 else:
112 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCValueErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCValueError#("Cannot serialize %s wiggles into STC-S"%
113 nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(node).__class__.__name__)
114
115
116 def _makeCooTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().(cooTypescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().(cooType)):
117 """returns a function returning a CST fragment for a coordinate.
118 """
119 def toCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node)):
120 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node).frame is None: # no frame, no coordinates.
121 return {}
122 nDim = nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node).frame.nDim
123 return {
124 "error": _wiggleToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node).error, nDim),
125 "resolution": _wiggleToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node).resolution, nDim),
126 "pixSize": _wiggleToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node).pixSize, nDim),
127 "unit": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node).getUnitString(),
128 "type": cooTypescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().(cooType),
129 "pos": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().(node).value or None,}
130 return toCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().toCST().
131
132
133 def _makeIntervalCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntervalCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntervalCoos().(node)):
134 res = {}
135 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntervalCoos().(node).lowerLimit or nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntervalCoos().(node).upperLimit:
136 res["coos"] = [c for c in (nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntervalCoos().(node).lowerLimit, nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntervalCoos().(node).upperLimit)
137 if c is not None]
138 return res
139
140
141 def _makeTimeIntervalCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node)):
142 # Special-cased since these have (Start|Stop)Time
143 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).lowerLimit and nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).upperLimit:
144 typescip-python python python-stdlib 3.11 builtins/type#, coos = "TimeInterval", (nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).lowerLimit, nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).upperLimit)
145 elif nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).lowerLimit:
146 typescip-python python python-stdlib 3.11 builtins/type#, coos = "StartTime", (nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).lowerLimit,)
147 elif nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).upperLimit:
148 typescip-python python python-stdlib 3.11 builtins/type#, coos = "StopTime", (nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().(node).upperLimit,)
149 else:
150 typescip-python python python-stdlib 3.11 builtins/type#, coos = "TimeInterval", ()
151 return {
152 "type": typescip-python python python-stdlib 3.11 builtins/type#,
153 "coos": coos}
154
155
156 def _makeAreaTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().(areaTypescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().(areaType), cooMakerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().(cooMaker)=_makeIntervalCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntervalCoos().):
157 """returns a CST fragment for an area.
158
159 areaType this CST type of the node returned, cooMaker is a function
160 that receives the node and returns a dictionary containing at least
161 a coos key. It can set other keys as well (e.g.
162 _makeTimeIntervalCoos needs to override the type key).
163 """
164 def toCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().toCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().toCST().(node)):
165 return _combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().({
166 "fillfactor": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().toCST().(node).fillFactor,
167 "type": areaTypescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().(areaType)},
168 cooMakerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().(cooMaker)(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().toCST().(node)))
169 return toCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().toCST().
170
171
172 def _makePhraseTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(cooMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(cooMapper), areaMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(areaMapper), frameMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(frameMapper),
173 getASTItemsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(getASTItems)):
174 """returns a mapper building a CST fragment for a subphrase.
175
176 cooMapper and areaMapper are functions returning CST fragments
177 for coordinates and areas of this type, respectively.
178
179 getASTItems is a function that receives the AST root and has to
180 return either None (no matching items found in AST) or a pair
181 of coordinate and area, where area may be None. Use _makeASTItemsGetter
182 to build these functions.
183
184 The function returned expects the root of the AST as argument.
185 """
186 def toCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().toCST().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().toCST().(astRoot)):
187 items = getASTItemsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(getASTItems)(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().toCST().(astRoot))
188 if items is None:
189 return {}
190 coo, area = items
191 areaKeys = {}
192 if area:
193 areaKeys = areaMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(areaMapper)(area)
194 cooKeys = cooMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(cooMapper)(coo)
195 frame = coo.frame or area.frame
196 return _combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().(cooKeys,
197 areaKeys, # area keys come later to override cst key "type".
198 frameMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(frameMapper)(frame))
199 return toCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().toCST().
200
201
202 def _makeASTItemsGetterscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().(cooNamescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().(cooName), areaNamescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().(areaName)):
203 """returns a function that extracts coordinates and areas of
204 a certain type from an AST.
205
206 The function does all kinds of sanity checks and raises STCValueErrors
207 if those fail.
208
209 If all goes well, it will return a pair coo, area. coo is always
210 non-None, area may be None.
211 """
212 def getASTItemsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().getASTItems().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().getASTItems().(astRoot)):
213 areas, coo = getattrscip-python python python-stdlib 3.11 builtins/getattr().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().getASTItems().(astRoot), areaNamescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().(areaName)), getattrscip-python python python-stdlib 3.11 builtins/getattr().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().getASTItems().(astRoot), cooNamescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().(cooName))
214 if not areas and not coo:
215 return None
216 if len(areas)>1:
217 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCValueErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCValueError#("STC-S does not support more than one area"
218 " but %s has length %d"%(areaNamescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().(areaName), len(areas)))
219 if areas:
220 area = areas[0]
221 else:
222 area = None
223 return coo, area
224 return getASTItemsscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().getASTItems().
225
226
227 def _spatialCooToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node), getBasescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(getBase)=_makeCooTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().("Position")):
228 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node).frame is None:
229 return {}
230 cstNode = getBasescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(getBase)(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node))
231 cstNode["size"] = _wiggleToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_wiggleToCST().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node).size, nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node).frame.nDim)
232 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node).epoch is not None:
233 yearDef = nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node).yearDef
234 if yearDef is None:
235 yearDef = "J"
236 cstNode["epoch"] = yearDef+strscip-python python python-stdlib 3.11 builtins/str#(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().(node).epoch)
237 return cstNode
238
239
240 _timeToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeToCST. = _makePhraseTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(
241 _makeCooTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().("Time"),
242 _makeAreaTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().("TimeInterval", _makeTimeIntervalCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeTimeIntervalCoos().),
243 _timeFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeFrameToCST().,
244 _makeASTItemsGetterscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().("time", "timeAs"))
245 _simpleSpatialToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_simpleSpatialToCST. = _makePhraseTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(
246 _spatialCooToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().,
247 _makeAreaTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().("PositionInterval"),
248 _spaceFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().,
249 _makeASTItemsGetterscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().("place", "areas"))
250 _spectralToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spectralToCST. = _makePhraseTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(
251 _makeCooTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().("Spectral"),
252 _makeAreaTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().("SpectralInterval"),
253 _spectralFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spectralFrameToCST().,
254 _makeASTItemsGetterscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().("freq", "freqAs"))
255 _redshiftToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftToCST. = _makePhraseTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(
256 _makeCooTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().("Redshift"),
257 _makeAreaTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().("RedshiftInterval"),
258 _redshiftFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftFrameToCST().,
259 _makeASTItemsGetterscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().("redshift", "redshiftAs"))
260 _velocityToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_velocityToCST. = _makePhraseTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(
261 _makeCooTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCooTreeMapper().("VelocityInterval"),
262 _makeAreaTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().("VelocityInterval"),
263 lambda _: {}, # Frame provided by embedding position
264 _makeASTItemsGetterscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().("velocity", "velocityAs"))
265
266 def _sysIdToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_sysIdToCST().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_sysIdToCST().(astRoot)):
267 if astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_sysIdToCST().(astRoot).astroSystem.libraryId:
268 return syslibscip-python python gavodachs 2.13 `gavo.stc.syslib`/__init__:.stripIVOIDscip-python python gavodachs 2.13 `gavo.stc.syslib`/stripIVOID().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_sysIdToCST().(astRoot).astroSystem.libraryId)
269
270
271 def _makeAllSkyCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAllSkyCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAllSkyCoos().(node)):
272 return {"geoCoos": ()}
273
274 def _makeCircleCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCircleCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCircleCoos().(node)):
275 return {"geoCoos": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCircleCoos().(node).center+(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeCircleCoos().(node).radius,)}
276
277 def _makeEllipseCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeEllipseCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeEllipseCoos().(node)):
278 return {"geoCoos": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeEllipseCoos().(node).center+(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeEllipseCoos().(node).smajAxis, nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeEllipseCoos().(node).sminAxis, nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeEllipseCoos().(node).posAngle)}
279
280 def _makeBoxCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeBoxCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeBoxCoos().(node)):
281 return {"geoCoos": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeBoxCoos().(node).center+nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeBoxCoos().(node).boxsize}
282
283 def _makePolygonCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePolygonCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePolygonCoos().(node)):
284 return {"geoCoos": tuplescip-python python python-stdlib 3.11 builtins/tuple#(itertoolsscip-python python python-stdlib 3.11 itertools/__init__:.chainscip-python python python-stdlib 3.11 itertools/chain#(*nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePolygonCoos().(node).vertices))}
285
286 def _makeConvexCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeConvexCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeConvexCoos().(node)):
287 return {"geoCoos": tuplescip-python python python-stdlib 3.11 builtins/tuple#(itertoolsscip-python python python-stdlib 3.11 itertools/__init__:.chainscip-python python python-stdlib 3.11 itertools/chain#(*nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeConvexCoos().(node).vectors))}
288
289 def _makeSpaceIntervalCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpaceIntervalCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpaceIntervalCoos().(node)):
290 return {"geoCoos": nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpaceIntervalCoos().(node).lowerLimit+nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpaceIntervalCoos().(node).upperLimit}
291
292 _compoundGeosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_compoundGeos. = ["Union", "Difference", "Intersection", "Not"]
293
294 def _makeUnionCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeUnionCoos().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeUnionCoos().(node)):
295 children = []
296 for c in nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeUnionCoos().(node).children:
297 nodeName = c.__class__.__name__
298 base = {"subtype": nodeName}
299 if c in _compoundGeosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_compoundGeos.:
300 children.appendscip-python python python-stdlib 3.11 builtins/list#append().(_combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().(base, _makeUnionCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeUnionCoos().(c)))
301 else:
302 children.appendscip-python python python-stdlib 3.11 builtins/list#append().(_combinescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_combine().(base,
303 globals()["_make%sCoos"%nodeName](c)))
304 return {"children": children}
305
306 _makeDifferenceCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeDifferenceCoos. = _makeIntersectionCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeIntersectionCoos. = _makeNotCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeNotCoos. = _makeUnionCoosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeUnionCoos().
307
308 _geometryMappersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_geometryMappers. = dictscip-python python python-stdlib 3.11 builtins/dict#([(n, _makePhraseTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePhraseTreeMapper().(
309 _spatialCooToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialCooToCST().,
310 _makeAreaTreeMapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeAreaTreeMapper().(n, globals()["_make%sCoos"%n]),
311 _spaceFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().,
312 _makeASTItemsGetterscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeASTItemsGetter().("place", "areas")))
313 for n in ["AllSky", "Circle", "Ellipse", "Box", "Polygon", "Convex"]+
314 _compoundGeosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_compoundGeos.])
315
316 def _spatialToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot)):
317 args = {}
318 velocityArgs = _velocityToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_velocityToCST.(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot))
319 if velocityArgs:
320 args = {"velocity": velocityArgs}
321 node = (astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot).areas and astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot).areas[0]) or astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot).place
322
323 if not node:
324 # This means we have neither a place nor a geometry but still
325 # want a frame (e.g., velocity only)
326 if args:
327 args.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(_spaceFrameToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spaceFrameToCST().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot).astroSystem.spaceFrame))
328 args["type"] = "Position"
329 elif isinstance(node, (dmscip-python python gavodachs 2.13 `gavo.stc.dm`/__init__:.SpaceCooscip-python python gavodachs 2.13 `gavo.stc.dm`/SpaceCoo#, dmscip-python python gavodachs 2.13 `gavo.stc.dm`/__init__:.SpaceIntervalscip-python python gavodachs 2.13 `gavo.stc.dm`/SpaceInterval#)):
330 args.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(_simpleSpatialToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_simpleSpatialToCST.(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot)))
331 else: # Ok, it's a geometry
332 args.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(_geometryMappersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_geometryMappers.[node.__class__.__name__](astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRoot)))
333 return args
334
335
336 ############## Flattening of the CST
337
338 def _joinWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinWithNull().(strListscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinWithNull().(strList)):
339 res = " ".joinscip-python python python-stdlib 3.11 builtins/str#join().(s for s in strListscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinWithNull().(strList) if s is not None)
340 if not res:
341 return None
342 return res
343
344
345 def _joinKeysWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(node), kwListscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(kwList), flattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(flatteners)):
346 """returns a string made up of the non-null values in kwList in node.
347
348 To make things a bit more flexible, you can give lists in kwList.
349 Their elements will be inserted into the result as-is.
350
351 Flatteners is a dictionary mapping keys from kwList to functions
352
353 flat(val, node) -> string
354
355 that turn a value for the keyword to a complete string.
356 _makeKeywordFlattener and _makeNoKeywordFlattener generate such functions.
357 """
358 res = []
359 for key in kwListscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(kwList):
360 if isinstance(key, listscip-python python python-stdlib 3.11 builtins/list#):
361 res.extendscip-python python python-stdlib 3.11 builtins/list#extend().(key)
362 elif key not in nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(node):
363 pass
364 elif key in flattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(flatteners):
365 res.appendscip-python python python-stdlib 3.11 builtins/list#append().(flattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(flatteners)[key](nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(node)[key], nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(node)))
366 else:
367 res.appendscip-python python python-stdlib 3.11 builtins/list#append().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(node)[key])
368 return _joinWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinWithNull().(res)
369
370
371 def _flattenValuescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val), nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(node)=None):
372 """returns a sensible STC-S string representation for many sorts of
373 values, dispatched on their type.
374
375 This function can be used as a flattener.
376 """
377 if valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val) is None:
378 return ""
379 elif isinstance(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val), strscip-python python python-stdlib 3.11 builtins/str#):
380 return strscip-python python python-stdlib 3.11 builtins/str#(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val))
381 elif isinstance(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val), (intscip-python python python-stdlib 3.11 builtins/int#, floatscip-python python python-stdlib 3.11 builtins/float#)):
382 return strscip-python python python-stdlib 3.11 builtins/str#(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val))
383 elif isinstance(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val), (listscip-python python python-stdlib 3.11 builtins/list#, tuplescip-python python python-stdlib 3.11 builtins/tuple#)):
384 return " ".joinscip-python python python-stdlib 3.11 builtins/str#join().(_flattenValuescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(v) for v in valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val))
385 elif isinstance(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val), datetimescip-python python python-stdlib 3.11 datetime/__init__:.datetimescip-python python python-stdlib 3.11 datetime/datetime#):
386 return valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val).isoformatscip-python python python-stdlib 3.11 datetime/datetime#isoformat().()
387 elif isinstance(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val), commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.ColRefscip-python python gavodachs 2.13 `gavo.stc.common`/ColRef#):
388 return '"%s"'%valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val).destscip-python python gavodachs 2.13 `gavo.utils.stanxml`/Stub#dest.
389 else:
390 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCValueErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCValueError#("Cannot serialize %r to STC-S"%valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(val))
391
392
393 def _makePosValueFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().(keyscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().(key)):
394 """returns a flattener for position values of type key.
395
396 The trick is to suppress key when the node already represents a position
397 type. Thus, we generate "Time 2" rather than Time Time 2 analogous to
398 "TimeInterval [...] Time 2".
399 """
400 def flattenPositionscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().(val), nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().(node)):
401 if valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().(val) is not None:
402 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().(node)["type"]==keyscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().(key):
403 return _flattenValuescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().(val))
404 else:
405 return "%s %s"%(keyscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().(key), _flattenValuescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().(val)))
406 return flattenPositionscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().flattenPosition().
407
408
409 def _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().(keywordscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().(keyword)):
410 """returns a function returning a flattened value with keyword in front.
411 """
412 if keywordscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().(keyword):
413 fmtStr = "%s %%s"%keywordscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().(keyword)
414 else:
415 fmtStr = "%s"
416 def flattenscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().flatten().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().flatten().(val), nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().flatten().(node)):
417 if valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().flatten().(val) is not None and valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().flatten().(val)!=():
418 return fmtStr%(_flattenValuescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().flatten().(val)))
419 return flattenscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().flatten().
420
421
422 def _flattenRefPosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRefPos().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRefPos().(val), nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRefPos().(node)):
423 return _joinWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinWithNull().([nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRefPos().(node)["refpos"], nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRefPos().(node)["planetaryEphemeris"]])
424
425
426 # Keywords for items common to all coordinates.
427 _commonFlattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_commonFlatteners. = {
428 "fillfactor": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().("fillfactor"),
429 "unit": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().("unit"),
430 "error": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().("Error"),
431 "resolution": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().("Resolution"),
432 "size": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().("Size"),
433 "pixSize": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().("PixSize"),
434 "epoch": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().("Epoch"),
435 "coos": _flattenValuescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().,
436 "refpos": _flattenRefPosscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRefPos().,
437 }
438
439
440 def _make1DCooFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().(posKeyscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().(posKey), frameKeysscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().(frameKeys)):
441 """returns a flattener for 1-D coordinates (time, spectral, redshift).
442 """
443 flatteners = {
444 "pos": _makePosValueFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().(posKeyscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().(posKey)),
445 }
446 flatteners.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(_commonFlattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_commonFlatteners.)
447 keyList = ["type", "fillfactor"]+frameKeysscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().(frameKeys)+["coos", "pos",
448 "unit", "error", "resolution", "pixSize"]
449 def flattenscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().flatten().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().flatten().(node)):
450 return _joinKeysWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().flatten().(node), keyList, flatteners)
451 return flattenscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().flatten().
452
453
454 def _makeVelocityFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().():
455 """returns a flattener for velocities.
456
457 This is only used by _makePositionFlattener, since velocity is a subclause.
458 """
459 flatteners = {
460 "pos": _makePosValueFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().("VelocityInterval"),
461 }
462 flatteners.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(_commonFlattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_commonFlatteners.)
463
464 def flattenVelocityscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(val), nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(node)):
465 """custom flattener for velocities, used by _flattenPosition.
466 """
467 res = []
468 if valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(val):
469 if valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(val).get("coos") is not None:
470 res.extendscip-python python python-stdlib 3.11 builtins/list#extend().(["VelocityInterval", _joinKeysWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(val),
471 ["fillfactor", "coos"], flatteners)])
472 if valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(val).get("pos") is not None:
473 res.extendscip-python python python-stdlib 3.11 builtins/list#extend().(["Velocity", _joinKeysWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(val), ["pos"], flatteners)])
474 if not res:
475 res.appendscip-python python python-stdlib 3.11 builtins/list#append().("Velocity")
476 res.appendscip-python python python-stdlib 3.11 builtins/list#append().(_joinKeysWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().(val),
477 ["unit", "error", "resolution", "size", "pixSize"], flatteners))
478 return _joinWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinWithNull().(res)
479
480 return flattenVelocityscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().flattenVelocity().
481
482
483 def _makeSpatialFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().():
484 def flattenCompoundChildrenscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().(childListscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().(childList), nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().(node)):
485 """custom flattener for compounds (i.e., and, or, not)
486 """
487 res = []
488 for c in childListscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().(childList):
489 if "geoCoos" in c: # it's an atomic geometry
490 res.appendscip-python python python-stdlib 3.11 builtins/list#append().("%s %s"%(
491 c["subtype"], _flattenValuescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenValue().(c["geoCoos"])))
492 else: # it's a compound
493 res.appendscip-python python python-stdlib 3.11 builtins/list#append().("%s %s"%(c["subtype"],
494 flattenCompoundChildrenscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().(c["children"], nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().(node))))
495 if "geoCoos" in nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().(node): # it's atomic, no parens required
496 return " ".joinscip-python python python-stdlib 3.11 builtins/str#join().(res)
497 else:
498 return "(%s)"%(" ".joinscip-python python python-stdlib 3.11 builtins/str#join().(res))
499
500 flatteners = {
501 "pos": _makePosValueFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makePosValueFlattener().("Position"),
502 "geoCoos": _makeKeywordFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeKeywordFlattener().(""),
503 "velocity": _makeVelocityFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeVelocityFlattener().(),
504 "children": flattenCompoundChildrenscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenCompoundChildren().,
505 }
506 flatteners.updatescip-python python python-stdlib 3.11 typing/MutableMapping#update().(_commonFlattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_commonFlatteners.)
507
508 def flattenPositionscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenPosition().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenPosition().(node)):
509 """returns an STC-S representation of position and velocity.
510 """
511 return _joinKeysWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenPosition().(node), ["type", "fillfactor", "frame",
512 "equinox", "refpos", "flavor", "epoch", "coos", "geoCoos", "children",
513 "pos", "unit", "error", "resolution", "size", "pixSize", "velocity"],
514 flatteners)
515
516 return flattenPositionscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().flattenPosition().
517
518
519 # generate top-level flatteners
520
521 _flattenTimescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenTime. = _make1DCooFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().("Time", ["timescale", "refpos"])
522 _flattenSpectralscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSpectral. = _make1DCooFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().("Spectral", ["refpos"])
523 _flattenRedshiftscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRedshift. = _make1DCooFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_make1DCooFlattener().("Redshift",
524 ["refpos", "redshiftType", "dopplerdef"])
525 _flattenSpatialscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSpatial. = _makeSpatialFlattenerscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_makeSpatialFlattener().()
526
527 def _flattenSystemscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSystem().(nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSystem().(node)):
528 if nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSystem().(node):
529 return "System %s"%nodescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSystem().(node)
530
531
532 def _flattenCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cstscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cst)):
533 """returns a flattened string for an STCS CST.
534
535 Flattening destroys the tree.
536 """
537 return "\n".joinscip-python python python-stdlib 3.11 builtins/str#join().([s for s in (
538 _flattenTimescip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenTime.(cstscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cst).get("time", {})),
539 _flattenSpatialscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSpatial.(cstscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cst).get("space", {})),
540 _flattenSpectralscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSpectral.(cstscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cst).get("spectral", {})),
541 _flattenRedshiftscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenRedshift.(cstscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cst).get("redshift", {})),
542 _flattenSystemscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenSystem().(cstscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cst).get("libSystem")),)
543 if s])
544
545
546 def getSTCSscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSTCS().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSTCS().(astRoot)):
547 """returns an STC-S string for an AST.
548 """
549 cst = stcsscip-python python gavodachs 2.13 `gavo.stc.stcs`/__init__:.removeDefaultsscip-python python gavodachs 2.13 `gavo.stc.stcs`/removeDefaults().({
550 "time": _timeToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_timeToCST.(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSTCS().(astRoot)),
551 "space": _spatialToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSTCS().(astRoot)),
552 "spectral": _spectralToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spectralToCST.(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSTCS().(astRoot)),
553 "redshift": _redshiftToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_redshiftToCST.(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSTCS().(astRoot)),
554 "libSystem": _sysIdToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_sysIdToCST().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSTCS().(astRoot)),
555 })
556 return _flattenCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_flattenCST().(cst)
557
558
559 def getSpatialSystemscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSpatialSystem().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSpatialSystem().(astRoot)):
560 """returns a phrase for the spatial system (for manual STC-S generation).
561 """
562 cst = stcsscip-python python gavodachs 2.13 `gavo.stc.stcs`/__init__:.removeDefaultsscip-python python gavodachs 2.13 `gavo.stc.stcs`/removeDefaults().({"space": _spatialToCSTscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_spatialToCST().(astRootscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSpatialSystem().(astRoot))})
563 return _joinKeysWithNullscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_joinKeysWithNull().(cst["space"], ["frame",
564 "equinox", "refpos", "flavor", "epoch"], _commonFlattenersscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_commonFlatteners.)
565
566
567 def _boxMapperFactoryscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().(colDescscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().(colDesc)):
568 """A factory for Boxes.
569 """
570 if colDescscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().(colDesc)["dbtype"]!="box":
571 return
572
573 if colDescscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().(colDesc).original.stc:
574 systemString = getSpatialSystemscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/getSpatialSystem().(colDescscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().(colDesc).original.stc)
575 else:
576 systemString = "UNKNOWN"
577
578 def mapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().mapper().(valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().mapper().(val)):
579 if valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().mapper().(val) is None:
580 return ""
581 else:
582 return "Box %s %s %s %s %s"%((systemString,)+valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().mapper().(val)[0]+valscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().mapper().(val)[1])
583 colDescscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().(colDesc)["datatype"], colDescscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().(colDesc)["arraysize"] = "char", "*"
584 return mapperscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().mapper().
585 utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.registerDefaultMFscip-python python gavodachs 2.13 `gavo.utils.serializers`/registerDefaultMF.(_boxMapperFactoryscip-python python gavodachs 2.13 `gavo.stc.stcsgen`/_boxMapperFactory().)