Debian Codegraph @

gavo/stc/tapstc.py

raw symbols SCIP
1 """
2 The subset of STC proposed by the TAP spec.
3
4 Use this rather than the full-blown STC library for TAP and friends.
5 TAP's semi-sanitation of STC needs some special handling anyway,
6 and this is much faster than the full-blown mess.
7 """
8
9 #c Copyright 2008-2025, the GAVO project <[email protected]>
10 #c
11 #c This program is free software, covered by the GNU GPL. See the
12 #c COPYING file in the source distribution.
13
14
15 import functoolsscip-python python python-stdlib 3.11 functools/__init__:
16
17 from gavoscip-python python gavodachs 2.13 gavo/__init__: import utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:
18 from gavo.stcscip-python python gavodachs 2.13 `gavo.stc`/__init__: import commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:
19 from gavo.stcscip-python python gavodachs 2.13 `gavo.stc`/__init__: import stcsastscip-python python gavodachs 2.13 `gavo.stc.stcsast`/__init__:
20 from gavo.utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__: import pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:
21
22
23 TAP_SYSTEMSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TAP_SYSTEMS. = setscip-python python python-stdlib 3.11 builtins/set#(
24 ['ICRS', 'FK4', 'FK5', 'GALACTIC', 'RELOCATABLE', 'UNKNOWN', '', "BROKEN",
25 'J2000', 'B1950',
26 "UNKNOWNFrame"])
27
28 # universally ignored
29 TAP_REFPOSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TAP_REFPOS. = setscip-python python python-stdlib 3.11 builtins/set#(
30 ['BARYCENTER', 'GEOCENTER', 'HELIOCENTER', 'LSR', 'TOPOCENTER',
31 'RELOCATABLE', 'UNKNOWNREFPOS'])
32
33 # only SPHERICAL2 supported, all others raise errors
34 TAP_FLAVORSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TAP_FLAVORS. = setscip-python python python-stdlib 3.11 builtins/set#(
35 ["CARTESIAN2", "CARTESIAN3", "SPHERICAL2"])
36
37
38 ################## transformations between TAP STC reference systems
39
40 UNIVERSALLY_COMPATIBLEscip-python python gavodachs 2.13 `gavo.stc.tapstc`/UNIVERSALLY_COMPATIBLE. = setscip-python python python-stdlib 3.11 builtins/set#(['RELOCATABLE', 'UNKNOWN', '', "BROKEN", None])
41
42 TRANSFORMSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TRANSFORMS. = {
43 # From "ICRS" (really, FK5 J2000, and the lo-prec) to...
44 'FK4': (1.5651864333666516, -0.0048590552804904244, -1.5763681043529187),
45 'FK5': None,
46 'ICRS': None,
47 'GALACTIC': (1.3463560974407338, -1.0973190018372752, 0.57477052472873258),
48 }
49
50
51 def _getEulersForscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_getEulersFor().(framescip-python python gavodachs 2.13 `gavo.stc.tapstc`/_getEulersFor().(frame)):
52 if not framescip-python python gavodachs 2.13 `gavo.stc.tapstc`/_getEulersFor().(frame) in TRANSFORMSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TRANSFORMS.:
53 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCValueErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCValueError#("Unknown reference frame: %s"%framescip-python python gavodachs 2.13 `gavo.stc.tapstc`/_getEulersFor().(frame))
54 return TRANSFORMSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TRANSFORMS.[framescip-python python gavodachs 2.13 `gavo.stc.tapstc`/_getEulersFor().(frame)]
55
56
57 def getPGSphereTrafoscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(fromSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(fromSys), toSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(toSys)):
58 """returns a pgsphere expression fragment to turn a pgsphere geometry
59 from fromSys to toSys.
60
61 fromSys and toSys are system designations like for TAP.
62
63 If the systems are "compatible" (i.e., no transformation is deemed necessary),
64 None is returned. An STCValueError is raised for incomprehensible system
65 specifications.
66 """
67 if (fromSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(fromSys) in UNIVERSALLY_COMPATIBLEscip-python python gavodachs 2.13 `gavo.stc.tapstc`/UNIVERSALLY_COMPATIBLE.
68 or toSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(toSys) in UNIVERSALLY_COMPATIBLEscip-python python gavodachs 2.13 `gavo.stc.tapstc`/UNIVERSALLY_COMPATIBLE.):
69 return None
70 if fromSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(fromSys)=='ICRS':
71 template = "+strans(%.12f,%.12f,%.12f)"
72 angles = _getEulersForscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_getEulersFor().(toSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(toSys))
73 elif toSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(toSys)=='ICRS':
74 angles = _getEulersForscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_getEulersFor().(fromSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(fromSys))
75 template = "-strans(%.12f,%.12f,%.12f)"
76 else:
77 t1 = getPGSphereTrafoscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(fromSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(fromSys), 'ICRS')
78 t2 = getPGSphereTrafoscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().('ICRS', toSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getPGSphereTrafo().(toSys))
79 return "%s%s"%(t1 or "", t2 or "")
80 if angles:
81 return template%angles
82 return None
83
84
85 def getTAPSTCscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getTAPSTC().(stcInstancescip-python python gavodachs 2.13 `gavo.stc.tapstc`/getTAPSTC().(stcInstance)):
86 """returns a tap system identifier for an STC AstroSystem.
87
88 This is stc.astroSystem.spaceFrame.refFrame if existing and
89 TAP-defined, UNKNOWN otherwise.
90 """
91 rf = None
92 if stcInstancescip-python python gavodachs 2.13 `gavo.stc.tapstc`/getTAPSTC().(stcInstance).astroSystem and stcInstancescip-python python gavodachs 2.13 `gavo.stc.tapstc`/getTAPSTC().(stcInstance).astroSystem.spaceFrame:
93 rf = stcInstancescip-python python gavodachs 2.13 `gavo.stc.tapstc`/getTAPSTC().(stcInstance).astroSystem.spaceFrame.refFrame
94 if rf not in TAP_SYSTEMSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TAP_SYSTEMS.:
95 return "UNKNOWN"
96 return rf
97
98
99 @functoolsscip-python python python-stdlib 3.11 functools/__init__:.lru_cachescip-python python python-stdlib 3.11 functools/lru_cache().()
100 def getSTCForTAPscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier)):
101 """returns an stc AST for a tap reference system identifier.
102
103 The tapIdentifier is any string in which the first item is the reference
104 system. Everything else is ignored (this is because it seems someone
105 intended additional specs like "BARYCENTER" to be legal, although
106 there really is nothing we can do about them).
107 """
108 if tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier):
109 tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier) = utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.identifierPatternscip-python python gavodachs 2.13 `gavo.utils.texttricks`/identifierPattern..findallscip-python python python-stdlib 3.11 re/Pattern#findall().(tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier))[0]
110 if tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier) in ["BROKEN", '', "UNKNOWN", None]:
111 tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier) = "UNKNOWNFrame"
112 ast = stcsastscip-python python gavodachs 2.13 `gavo.stc.stcsast`/__init__:.parseSTCSscip-python python gavodachs 2.13 `gavo.stc.stcsast`/parseSTCS().("Position %s"%tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier))
113 if tapIdentifierscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSTCForTAP().(tapIdentifier)=='BROKEN':
114 ast.broken = True
115 return ast
116
117
118
119 ############################# TAP simplified STC-S
120 # The simplified STC-S is a simple regular grammar for the position specs
121 # plus a context-free part for set operations. The regular part we
122 # do with REs, for the rest there's a simple recursive descent parser.
123 #
124 # From the literal we either create a pgsphere geometry ready for ingestion
125 # or, when operators enter, an GeomExpr object. Since at least pgsphere
126 # does not support geometric operators, this must be handled in the morph code.
127 # To make this clear, its flatten method just raises an Exception.
128 #
129 # The regions expressible in pgsphere are returned as pgsphre objects.
130 # This is because I feel all this should only be used for ingesting data
131 # ("table upload") and thus carrying around the frame is pointless;
132 # you can, however, retrieve a guess for the frame from the returned
133 # object's cooSys attribute (which is convenient for the morphers).
134
135
136 class GeomExprscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#(objectscip-python python python-stdlib 3.11 builtins/object#):
137 """a sentinel object to be processed by morphers.
138 """
139 def __init__scip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(self), framescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(frame), operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(operator), operandsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(operands)):
140 selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(self).cooSysscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#cooSys. = framescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(frame)
141 selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(self).operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operator., selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(self).operandsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operands. = operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(operator).upper(), operandsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(operands)
142 selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(self).typescip-python python python-stdlib 3.11 builtins/type# = selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#__init__().(self).operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operator.
143
144 def flattenscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#flatten().(selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#flatten().(self)):
145 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCSParseErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCSParseError#("Cannot serialize STC-S. Did you use"
146 " Union or Intersection outside of CONTAINS or INTERSECTS?")
147
148 def _flatLogicscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(self), templatescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(template), operandscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(operand)):
149 if isinstance(operandscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(operand), GeomExprscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#):
150 return operandscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(operand).asLogicscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(templatescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(template))
151 else:
152 return templatescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(template)%operandscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(operand).asPgSphere()
153
154 def asLogicscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self), templatescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(template)):
155 if selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self).operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operator.=="UNION":
156 logOp = " OR "
157 elif selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self).operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operator.=="INTERSECTION":
158 logOp = " AND "
159 elif selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self).operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operator.=="NOT":
160 return "NOT (%s)"%selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self)._flatLogicscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(templatescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(template), selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self).operandsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operands.[0])
161 else:
162 raise NotImplementedErrorscip-python python python-stdlib 3.11 builtins/NotImplementedError#("No logic for operator '%s'"%selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self).operatorscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operator.)
163 return logOp.joinscip-python python python-stdlib 3.11 builtins/str#join().(
164 '(%s)'%selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self)._flatLogicscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#_flatLogic().(templatescip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(template), op) for op in selfscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#asLogic().(self).operandsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#operands.)
165
166
167 def _make_pgspositionscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsposition().(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsposition().(coords)):
168 if len(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsposition().(coords))!=2:
169 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCSParseErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCSParseError#("STC-S points want two coordinates.")
170 return pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPointscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoint#(*coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsposition().(coords))
171
172
173 def _make_pgscirclescip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgscircle().(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgscircle().(coords)):
174 if len(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgscircle().(coords))!=3:
175 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCSParseErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCSParseError#("STC-S circles want three numbers.")
176 return pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SCirclescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SCircle#(pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPointscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoint#(*coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgscircle().(coords)[:2]), coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgscircle().(coords)[2])
177
178
179 def _make_pgsboxscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsbox().(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsbox().(coords)):
180 if len(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsbox().(coords))!=4:
181 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCSParseErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCSParseError#("STC-S boxes want four numbers.")
182 x,y,w,h = coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgsbox().(coords)
183 return pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPolyscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoly#((
184 pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPointscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoint#(x-w/2, y-h/2),
185 pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPointscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoint#(x-w/2, y+h/2),
186 pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPointscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoint#(x+w/2, y+h/2),
187 pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPointscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoint#(x+w/2, y-h/2)))
188
189
190 def _make_pgspolygonscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgspolygon().(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgspolygon().(coords)):
191 if len(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgspolygon().(coords))<6 or len(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgspolygon().(coords))%2:
192 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCSParseErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCSParseError#(
193 "STC-S polygons want at least three number pairs")
194 return pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPolyscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoly#(
195 [pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.SPointscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/SPoint#(*p) for p in utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.iterConsecutivePairsscip-python python gavodachs 2.13 `gavo.utils.codetricks`/iterConsecutivePairs().(coordsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_make_pgspolygon().(coords))])
196
197
198 def _makePgSphereInstancescip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(matchscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(match)):
199 """returns a utils.pgsphere instance from a match of simpleStatement in
200 the simple STCS parser below.
201 """
202 if matchscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(match)["flavor"] and matchscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(match)["flavor"].strip().upper()!="SPHERICAL2":
203 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCSParseErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCSParseError#("Only SPHERICAL2 STC-S supported here")
204 refFrame = 'UnknownFrame'
205 if matchscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(match)["frame"]:
206 refFrame = matchscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(match)["frame"].strip()
207 # refFrame gets thrown away here; to use it, we'd have to generate
208 # ADQL nodes, and that would be clumsy for uploads. See rant above.
209 handler = globals()["_make_pgs%s"%matchscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(match)["shape"].lower()]
210 res = handler(
211 tuplescip-python python python-stdlib 3.11 builtins/tuple#(floatscip-python python python-stdlib 3.11 builtins/float#(s)*utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.DEGscip-python python gavodachs 2.13 `gavo.utils.mathtricks`/spherToCart(). for s in matchscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(match)["coords"].strip().split() if s))
212 res.cooSys = refFrame
213 return res
214
215 def _makeREscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makeRE().(stringsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makeRE().(strings)):
216 return "|".joinscip-python python python-stdlib 3.11 builtins/str#join().(sortedscip-python python python-stdlib 3.11 builtins/sorted().(stringsscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makeRE().(strings), keyscip-python python python-stdlib 3.11 builtins/sorted().(key)=lambda s: -len(s)))
217
218 @functoolsscip-python python python-stdlib 3.11 functools/__init__:.lru_cachescip-python python python-stdlib 3.11 functools/lru_cache().()
219 def getSimpleSTCSParserscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().():
220 from gavo.utils.parsetricksscip-python python gavodachs 2.13 `gavo.utils.parsetricks`/__init__: import (
221 Regexscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Regex#, CaselessKeywordscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/CaselessKeyword#, OneOrMorescip-python python gavodachs 2.13 `gavo.imp.pyparsing`/OneOrMore#, Forwardscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Forward#, Suppressscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Suppress#,
222 Optionalscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Optional#, ParseExceptionscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParseException#, ParseSyntaxExceptionscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParseSyntaxException#,
223 pyparsingWhitecharsscip-python python gavodachs 2.13 `gavo.utils.parsetricks`/pyparsingWhitechars().)
224
225 with pyparsingWhitecharsscip-python python gavodachs 2.13 `gavo.utils.parsetricks`/pyparsingWhitechars().(" \t\n\r"):
226 frameRE = _makeREscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makeRE().(TAP_SYSTEMSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TAP_SYSTEMS.)
227 refposRE = _makeREscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makeRE().(TAP_REFPOSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TAP_REFPOS.)
228 flavorRE = _makeREscip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makeRE().(TAP_FLAVORSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/TAP_FLAVORS.)
229 systemRE = (r"\s*"
230 r"(?P<frame>%s)?\s*"
231 r"(?P<refpos>%s)?\s*"
232 r"(?P<flavor>%s)?\s*")%(
233 frameRE, refposRE, flavorRE)
234 coordsRE = r"(?P<coords>(%s\s*)+)"%utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.floatREscip-python python gavodachs 2.13 `gavo.utils.texttricks`/floatRE.
235
236 simpleStatement = Regexscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Regex#(r"(?i)\s*"
237 "(?P<shape>position|circle|box|polygon)"
238 +systemRE
239 +coordsRE)
240 simpleStatement.setNamescip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParserElement#setName().("STC-S geometry")
241 simpleStatement.addParseActionscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParserElement#addParseAction().(lambda s,p,t: _makePgSphereInstancescip-python python gavodachs 2.13 `gavo.stc.tapstc`/_makePgSphereInstance().(t))
242 system = Regexscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Regex#("(?i)"+systemRE)
243 system.setNamescip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParserElement#setName().("STC-S system spec")
244 region = Forwardscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Forward#()
245 notExpr = CaselessKeywordscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/CaselessKeyword#("NOT") + Suppressscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Suppress#('(') + region + Suppressscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Suppress#(')')
246 notExpr.addParseActionscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParserElement#addParseAction().(lambda s,p,t: GeomExprscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#("UNKNOWN", "NOT", (t[1],)))
247 opExpr = (
248 (CaselessKeywordscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/CaselessKeyword#("UNION") | CaselessKeywordscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/CaselessKeyword#("INTERSECTION"))("op")
249 + Optionalscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Optional#(Regexscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Regex#(frameRE))("frame")
250 + Optionalscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Optional#(Regexscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Regex#(refposRE)) + Optionalscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Optional#(Regexscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Regex#(flavorRE))
251 + Suppressscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Suppress#("(")
252 + region + OneOrMorescip-python python gavodachs 2.13 `gavo.imp.pyparsing`/OneOrMore#(region)
253 + Suppressscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/Suppress#(")"))
254 opExpr.addParseActionscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParserElement#addParseAction().(
255 lambda s,p,t: GeomExprscip-python python gavodachs 2.13 `gavo.stc.tapstc`/GeomExpr#(strscip-python python python-stdlib 3.11 builtins/str#(t["frame"]), t[0].upper(), t[2:]))
256 region << (simpleStatement | opExpr | notExpr)
257
258 def parsescip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().(sscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().(s)):
259 if isinstance(sscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().(s), pgspherescip-python python gavodachs 2.13 `gavo.utils.pgsphere`/__init__:.PgSAdapterscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/PgSAdapter#):
260 return sscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().(s)
261 if sscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().(s) is None or not sscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().(s).strip(): # special service: Null values
262 return None
263
264 try:
265 res = utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__:.pyparseStringscip-python python gavodachs 2.13 `gavo.utils.parsetricks`/pyparseString().(region, sscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().(s), parseAll=True)[0]
266 if not res.cooSys or res.cooSys.lower()=='unknownframe': # Sigh.
267 res.cooSys = "UNKNOWN"
268 return res
269 except (ParseExceptionscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParseException#, ParseSyntaxExceptionscip-python python gavodachs 2.13 `gavo.imp.pyparsing`/ParseSyntaxException#) as msg:
270 raise commonscip-python python gavodachs 2.13 `gavo.stc.common`/__init__:.STCSParseErrorscip-python python gavodachs 2.13 `gavo.stc.common`/STCSParseError#("Invalid STCS (%s)"%strscip-python python python-stdlib 3.11 builtins/str#(msg))
271
272 return parsescip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().parse().
273
274
275 parseSimpleSTCSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/parseSimpleSTCS. = getSimpleSTCSParserscip-python python gavodachs 2.13 `gavo.stc.tapstc`/getSimpleSTCSParser().()
276
277
278 def simpleSTCSToPolygonscip-python python gavodachs 2.13 `gavo.stc.tapstc`/simpleSTCSToPolygon().(stcsExprscip-python python gavodachs 2.13 `gavo.stc.tapstc`/simpleSTCSToPolygon().(stcsExpr)):
279 """returns an spoly for an STC-S region specification.
280
281 This is used to let people upload VOTables with REGION items.
282 """
283 if stcsExprscip-python python gavodachs 2.13 `gavo.stc.tapstc`/simpleSTCSToPolygon().(stcsExpr) is None:
284 return None
285 return parseSimpleSTCSscip-python python gavodachs 2.13 `gavo.stc.tapstc`/parseSimpleSTCS.(stcsExprscip-python python gavodachs 2.13 `gavo.stc.tapstc`/simpleSTCSToPolygon().(stcsExpr)).asPolyscip-python python gavodachs 2.13 `gavo.utils.pgsphere`/PgSAdapter#asPoly().()
286
287
288 if __name__scip-python python gavodachs 2.13 `gavo.stc.tapstc`/=="__main__":
289 # compute the Euler angles given above. pgsphere has its rotation
290 # matrices opposite to ours (ccw rather than cw), hence the negation
291 from kapteynscip-python python gavodachs 2.13 kapteyn/__init__: import celestial
292 from gavo.stcscip-python python gavodachs 2.13 `gavo.stc`/__init__: import sphermathscip-python python gavodachs 2.13 `gavo.stc.sphermath`/__init__:
293 printscip-python python python-stdlib 3.11 builtins/print().("FK4:", tuplescip-python python python-stdlib 3.11 builtins/tuple#(-a for a in
294 sphermathscip-python python gavodachs 2.13 `gavo.stc.sphermath`/__init__:.getEulerAnglesFromMatrixscip-python python gavodachs 2.13 `gavo.stc.sphermath`/getEulerAnglesFromMatrix().(
295 celestial.skymatrix("icrs", "fk4 B1950.0")[0])))
296 printscip-python python python-stdlib 3.11 builtins/print().("GAL:", tuplescip-python python python-stdlib 3.11 builtins/tuple#(-a for a in
297 sphermathscip-python python gavodachs 2.13 `gavo.stc.sphermath`/__init__:.getEulerAnglesFromMatrixscip-python python gavodachs 2.13 `gavo.stc.sphermath`/getEulerAnglesFromMatrix().(
298 celestial.skymatrix("icrs", "galactic")[0])))
299