| 1 |
""" |
| 2 |
Tests for SODA functionality. |
| 3 |
|
| 4 |
There's a lot of tests like these in productstest, but for the old, |
| 5 |
atomic parameter-style dlget stuff. This should go ca. 2017. |
| 6 |
""" |
| 7 |
|
| 8 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 9 |
#c |
| 10 |
#c This program is free software, covered by the GNU GPL. See the |
| 11 |
#c COPYING file in the source distribution. |
| 12 |
|
| 13 |
|
| 14 |
import io |
| 15 |
import gc |
| 16 |
import os |
| 17 |
import re |
| 18 |
|
| 19 |
from gavo.helpers import testhelpers |
| 20 |
|
| 21 |
from gavo import api |
| 22 |
from gavo import rscdef |
| 23 |
from gavo import svcs |
| 24 |
from gavo import utils |
| 25 |
from gavo import votable |
| 26 |
from gavo.helpers import trialhelpers |
| 27 |
from gavo.rscdef import regtest |
| 28 |
from gavo.protocols import datalink |
| 29 |
from gavo.protocols import products #noflake: for mapper registration |
| 30 |
from gavo.protocols import soda |
| 31 |
from gavo.utils import fitstools |
| 32 |
from gavo.utils import pgsphere |
| 33 |
from gavo.utils import pyfits |
| 34 |
|
| 35 |
import tresc |
| 36 |
|
| 37 |
class SODAElementTest(testhelpers.VerboseTest): |
| 38 |
resources = [("prodtestTable", tresc.prodtestTable)] |
| 39 |
parent = None |
| 40 |
|
| 41 |
def testMetaMakerSyntaxError(self): |
| 42 |
mm = api.parseFromString(datalink.MetaMaker, |
| 43 |
'<metaMaker>\n' |
| 44 |
' <code>\n' |
| 45 |
' return return\n' |
| 46 |
' </code>\n' |
| 47 |
'</metaMaker>\n') |
| 48 |
self.assertRaisesWithMsg( |
| 49 |
api.BadCode, |
| 50 |
regtest.EqualingRE(r"Bad source code in function \(invalid syntax" |
| 51 |
r" \(<generated code \d+>, line 4\)\)"), |
| 52 |
mm.compile, |
| 53 |
(self,)) |
| 54 |
|
| 55 |
def testStandardDescGenWorks(self): |
| 56 |
ivoid = rscdef.getStandardPubDID( |
| 57 |
os.path.join(api.getConfig("inputsDir"), |
| 58 |
"data/a.imp")) |
| 59 |
dge = api.parseFromString(datalink.DescriptorGenerator, |
| 60 |
'<descriptorGenerator procDef="//soda#fromStandardPubDID">' |
| 61 |
'<bind name="contentQualifier">"http://g-vo.org/#ha"</bind>' |
| 62 |
'</descriptorGenerator>' |
| 63 |
) |
| 64 |
dg = dge.compile(dge) |
| 65 |
res = dg(ivoid, {}) |
| 66 |
self.assertEqual(res.accref, "data/a.imp") |
| 67 |
self.assertEqual(res.owner, "X_test") |
| 68 |
self.assertEqual(res.mime, "text/plain") |
| 69 |
self.assertEqual(res.accessPath, "data/a.imp") |
| 70 |
self.assertEqual(res.contentQualifier, "http://g-vo.org/#ha") |
| 71 |
|
| 72 |
def testProductsGenerator(self): |
| 73 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 74 |
allowed="dlget"> |
| 75 |
<datalinkCore> |
| 76 |
<dataFunction procDef="//soda#generateProduct"/> |
| 77 |
<metaMaker><code>yield MS(InputKey, name="ignored")</code></metaMaker> |
| 78 |
</datalinkCore> |
| 79 |
</service>""") |
| 80 |
res = trialhelpers.runSvcWith(svc, "form", {"ID": rscdef.getStandardPubDID( |
| 81 |
"data/b.imp"), "ignored": "0.4"}) |
| 82 |
self.assertEqual(b"".join(res.iterData()), b'alpha: 03 34 33.45' |
| 83 |
b'\ndelta: 42 34 59.7\nobject: michael\nembargo: 2003-12-31\n') |
| 84 |
|
| 85 |
def testProductsGeneratorMimecheck(self): |
| 86 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 87 |
allowed="dlget"> |
| 88 |
<datalinkCore> |
| 89 |
<dataFunction procDef="//soda#generateProduct"> |
| 90 |
<bind name="requireMimes">["application/fits"]</bind></dataFunction> |
| 91 |
<metaMaker><code>yield MS(InputKey, name="ignored")</code></metaMaker> |
| 92 |
</datalinkCore> |
| 93 |
</service>""") |
| 94 |
self.assertRaisesWithMsg(api.ValidationError, |
| 95 |
"Field PUBDID: Document type not supported: text/plain", |
| 96 |
trialhelpers.runSvcWith, |
| 97 |
(svc, "form", {"ID": rscdef.getStandardPubDID("data/b.imp"), |
| 98 |
"ignored": "0.5"})) |
| 99 |
|
| 100 |
def testAccrefPrefixDeny(self): |
| 101 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 102 |
allowed="dlmeta"> |
| 103 |
<datalinkCore> |
| 104 |
<descriptorGenerator procDef="//soda#fromStandardPubDID"> |
| 105 |
<bind name="accrefPrefix">'nodata'</bind> |
| 106 |
</descriptorGenerator> |
| 107 |
</datalinkCore> |
| 108 |
</service>""") |
| 109 |
res = trialhelpers.runSvcWith(svc, "dlmeta", |
| 110 |
{"ID": rscdef.getStandardPubDID("data/b.imp")}) |
| 111 |
row = list(next(votable.parseBytes(res[1])))[0] |
| 112 |
self.assertEqual(row[3], |
| 113 |
'AuthenticationFault: This Datalink' |
| 114 |
' service not available with this pubDID') |
| 115 |
self.assertEqual(row[5], "#this") |
| 116 |
|
| 117 |
def testAccrefPrefixAccept(self): |
| 118 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 119 |
allowed="dlmeta"> |
| 120 |
<datalinkCore> |
| 121 |
<descriptorGenerator procDef="//soda#fromStandardPubDID"> |
| 122 |
<bind name="accrefPrefix">'data/b'</bind> |
| 123 |
<bind name="contentQualifier">'#junk'</bind> |
| 124 |
</descriptorGenerator> |
| 125 |
</datalinkCore> |
| 126 |
</service>""") |
| 127 |
res = trialhelpers.runSvcWith(svc, "dlmeta", |
| 128 |
{"ID": rscdef.getStandardPubDID("data/b.imp")}) |
| 129 |
row = list(next(votable.parseBytes(res[1])))[0] |
| 130 |
self.assertTrue(row[1].endswith("/getproduct/data/b.imp")) |
| 131 |
self.assertEqual(row[5], "#this") |
| 132 |
self.assertEqual(row[9], "#junk") |
| 133 |
|
| 134 |
def testPlainAccrefWorks(self): |
| 135 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 136 |
allowed="dlmeta"> |
| 137 |
<datalinkCore> |
| 138 |
<descriptorGenerator procDef="//soda#fromStandardPubDID"> |
| 139 |
<bind name="accrefPrefix">'data/b'</bind> |
| 140 |
</descriptorGenerator> |
| 141 |
</datalinkCore> |
| 142 |
</service>""") |
| 143 |
res = trialhelpers.runSvcWith(svc, "dlmeta", {"ID": "data/b.imp"}) |
| 144 |
row = list(next(votable.parseBytes(res[1])))[0] |
| 145 |
self.assertTrue(row[1].endswith("/getproduct/data/b.imp")) |
| 146 |
self.assertEqual(row[5], "#this") |
| 147 |
|
| 148 |
def testProductsGeneratorFailure(self): |
| 149 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 150 |
allowed="dlget"> |
| 151 |
<datalinkCore> |
| 152 |
<dataFunction procDef="//soda#generateProduct"> |
| 153 |
<code>descriptor.data = None |
| 154 |
</code></dataFunction> |
| 155 |
<metaMaker><code>yield MS(InputKey, name="ignored") |
| 156 |
</code></metaMaker> |
| 157 |
</datalinkCore> |
| 158 |
</service>""") |
| 159 |
self.assertRaisesWithMsg(api.ReportableError, |
| 160 |
"Internal Error: a first data function did not create data.", |
| 161 |
trialhelpers.runSvcWith, |
| 162 |
(svc, "form", {"ID": rscdef.getStandardPubDID("data/b.imp"), |
| 163 |
"ignored": "0.4"})) |
| 164 |
|
| 165 |
def testProductsMogrifier(self): |
| 166 |
svc = api.parseFromString(svcs.Service, """<service id="foo"> |
| 167 |
<datalinkCore> |
| 168 |
<dataFunction procDef="//soda#generateProduct"/> |
| 169 |
<inputKey name="addto" type="integer" multiplicity="single"/> |
| 170 |
<dataFunction> |
| 171 |
<setup> |
| 172 |
<code> |
| 173 |
from gavo.protocols import products |
| 174 |
class MogrifiedProduct(products.ProductBase): |
| 175 |
def __init__(self, input, offset): |
| 176 |
self.input, self.offset = input, offset |
| 177 |
products.ProductBase.__init__(self, input.rAccref) |
| 178 |
|
| 179 |
def iterData(self): |
| 180 |
for chunk in self.input.iterData(): |
| 181 |
yield bytes(c+self.offset |
| 182 |
for c in chunk) |
| 183 |
</code> |
| 184 |
</setup> |
| 185 |
<code> |
| 186 |
descriptor.data = MogrifiedProduct(descriptor.data, |
| 187 |
args["addto"]) |
| 188 |
</code> |
| 189 |
</dataFunction></datalinkCore> |
| 190 |
</service>""") |
| 191 |
res = b"".join(trialhelpers.runSvcWith(svc, "form", { |
| 192 |
"ID": [rscdef.getStandardPubDID("data/b.imp")], |
| 193 |
"addto": ["4"]}).iterData()) |
| 194 |
self.assertEqual(res, |
| 195 |
b"eptle>$47$78$77289\x0ehipxe>$86$78$9=2;\x0e" |
| 196 |
b"sfnigx>$qmgleip\x0eiqfevks>$6447156175\x0e") |
| 197 |
|
| 198 |
def testAccrefFilter(self): |
| 199 |
svc = api.parseFromString(svcs.Service, """<service id="uh"> |
| 200 |
<datalinkCore> |
| 201 |
<descriptorGenerator procDef="//soda#fits_genDesc"> |
| 202 |
<bind key="accrefPrefix">"test"</bind> |
| 203 |
</descriptorGenerator> |
| 204 |
</datalinkCore></service>""") |
| 205 |
|
| 206 |
self.assertRaisesWithMsg(svcs.ForbiddenURI, |
| 207 |
"This Datalink service not available with this pubDID" |
| 208 |
" (pubDID: ivo://x-testing/~?goo/boo)", |
| 209 |
trialhelpers.runSvcWith, |
| 210 |
(svc, "dlget", {"ID": [rscdef.getStandardPubDID("goo/boo")]})) |
| 211 |
|
| 212 |
self.assertRaisesWithMsg(api.NotFoundError, |
| 213 |
"The authority in the dataset identifier" |
| 214 |
" 'ivo://great.scott/goo/boo' could not be located" |
| 215 |
" in the authorities managed here", |
| 216 |
trialhelpers.runSvcWith, |
| 217 |
(svc, "dlget", {"ID": ["ivo://great.scott/goo/boo"]})) |
| 218 |
|
| 219 |
def testDescGenNone(self): |
| 220 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 221 |
allowed="dlmeta"> |
| 222 |
<datalinkCore> |
| 223 |
<descriptorGenerator> |
| 224 |
<code> |
| 225 |
return None |
| 226 |
</code> |
| 227 |
</descriptorGenerator> |
| 228 |
</datalinkCore> |
| 229 |
</service>""") |
| 230 |
res = trialhelpers.runSvcWith(svc, "dlmeta", |
| 231 |
{"ID": "ivo://ivoa.net/data/b.imp"}) |
| 232 |
row = list(next(votable.parseBytes(res[1])))[0] |
| 233 |
self.assertEqual(row[0], "ivo://ivoa.net/data/b.imp") |
| 234 |
self.assertEqual(row[3], "NotFoundFault: dataset 'ivo://ivoa.net/data/" |
| 235 |
"b.imp' could not be located in this site's data holdings") |
| 236 |
|
| 237 |
def testDescGenRaised(self): |
| 238 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 239 |
allowed="dlmeta"> |
| 240 |
<datalinkCore> |
| 241 |
<descriptorGenerator> |
| 242 |
<code> |
| 243 |
from gavo import svcs |
| 244 |
raise svcs.UnknownURI("This does not exist") |
| 245 |
</code> |
| 246 |
</descriptorGenerator> |
| 247 |
</datalinkCore> |
| 248 |
</service>""") |
| 249 |
res = trialhelpers.runSvcWith(svc, "dlmeta", |
| 250 |
{"ID": "ivo://ivoa.net/data/b.imp"}) |
| 251 |
row = list(next(votable.parseBytes(res[1])))[0] |
| 252 |
self.assertEqual(row[0], "ivo://ivoa.net/data/b.imp") |
| 253 |
self.assertEqual(row[3], "NotFoundFault: This does not exist") |
| 254 |
|
| 255 |
|
| 256 |
def _dissectSODAFile(sodaFile): |
| 257 |
"""returns mime and content for a soda-returned File. |
| 258 |
|
| 259 |
It also calls cleanup(), if it's there -- basically, that's stuff |
| 260 |
twisted does for us in actual action. |
| 261 |
""" |
| 262 |
content = sodaFile.getContent() |
| 263 |
sodaFile.remove() |
| 264 |
if hasattr(sodaFile, "cleanup"): |
| 265 |
sodaFile.cleanup(None) |
| 266 |
return sodaFile.type, content |
| 267 |
|
| 268 |
|
| 269 |
############### Start FITS tests |
| 270 |
|
| 271 |
class SODAFITSTest(testhelpers.VerboseTest): |
| 272 |
resources = [("fitsTable", tresc.fitsTable)] |
| 273 |
|
| 274 |
def testNotFound(self): |
| 275 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 276 |
allowed="dlmeta, dlget"> |
| 277 |
<datalinkCore> |
| 278 |
<descriptorGenerator procDef="//soda#fits_genDesc"> |
| 279 |
<bind key="accrefPrefix">"data/"</bind> |
| 280 |
</descriptorGenerator> |
| 281 |
</datalinkCore></service>""") |
| 282 |
svc.parent = testhelpers.getTestRD() |
| 283 |
|
| 284 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 285 |
"ID": ["ivo://junky.ivorn/made/up"]}) |
| 286 |
self.assertEqual("application/x-votable+xml;content=datalink", mime) |
| 287 |
self.assertTrue(b"<TR><TD>ivo://junky.ivorn/made/up</TD><TD></TD>" |
| 288 |
b"<TD></TD><TD>NotFoundFault: The authority in the dataset identifier 'ivo://junky.ivorn/made/up' could not be located in the authorities managed here</TD>" in data) |
| 289 |
|
| 290 |
def testMakeDescriptor(self): |
| 291 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 292 |
allowed="dlmeta,dlget"> |
| 293 |
<datalinkCore> |
| 294 |
<descriptorGenerator procDef="//soda#fits_genDesc"> |
| 295 |
<bind key="accrefPrefix">"data/"</bind> |
| 296 |
</descriptorGenerator> |
| 297 |
<metaMaker procDef="//soda#fits_makeWCSParams"/> |
| 298 |
<metaMaker><code> |
| 299 |
assert descriptor.hdr["EQUINOX"]==2000. |
| 300 |
assert ([int(n) for n in |
| 301 |
descriptor.skyWCS.wcs_world2pix([(166, 20)], 0)[0]] |
| 302 |
==[7261, 7984]) |
| 303 |
yield descriptor.makeLink("http://dc.g-vo.org", |
| 304 |
semantics="#sentinel", |
| 305 |
contentType="text/english") |
| 306 |
</code></metaMaker> |
| 307 |
</datalinkCore></service>""") |
| 308 |
svc.parent = testhelpers.getTestRD() |
| 309 |
|
| 310 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 311 |
"ID": [rscdef.getStandardPubDID("data/ex.fits")]}) |
| 312 |
tree = testhelpers.getXMLTree(data) |
| 313 |
self.assertEqual(tree.xpath("//PARAM[@name='RA']")[0].get("unit"), |
| 314 |
"deg") |
| 315 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']")[0].get("xtype"), |
| 316 |
"interval") |
| 317 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']")[0].get("datatype"), |
| 318 |
"double") |
| 319 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']")[0].get("arraysize"), |
| 320 |
"2") |
| 321 |
self.assertEqual(tree.xpath("//PARAM[@name='RA']/VALUES/MIN" |
| 322 |
)[0].get("value")[:7], "168.243") |
| 323 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']/VALUES/MAX" |
| 324 |
)[0].get("value")[:10], "22.2192872") |
| 325 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']/DESCRIPTION" |
| 326 |
)[0].text, "The latitude coordinate") |
| 327 |
# If the next test fails with "list index out of range", |
| 328 |
# there's been a problem in the meta maker above -- perhaps |
| 329 |
# a failed assertion? See your logs. |
| 330 |
self.assertEqual(tree.xpath("//TR[TD='#sentinel']/TD" |
| 331 |
)[1].text, "http://dc.g-vo.org") |
| 332 |
|
| 333 |
def testScaledDataCutout(self): |
| 334 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 335 |
allowed="dlmeta,dlget"> |
| 336 |
<datalinkCore> |
| 337 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 338 |
<FEED source="//soda#fits_standardDLFuncs"/> |
| 339 |
</datalinkCore> |
| 340 |
</service>""") |
| 341 |
svc.parent = testhelpers.getTestRD() |
| 342 |
|
| 343 |
product = trialhelpers.runSvcWith(svc, "dlget", { |
| 344 |
"ID": [rscdef.getStandardPubDID("data/ex.fits")], |
| 345 |
"PIXEL_1": ["2 3"], |
| 346 |
"PIXEL_2": ["1 3"]}) |
| 347 |
bytes = product.getContent() |
| 348 |
product._cleanup(None) |
| 349 |
self.assertTrue(b"NAXIS1 = 2" in bytes) |
| 350 |
self.assertTrue(b"NAXIS2 = 3" in bytes) |
| 351 |
self.assertTrue(b"BZERO = 32768" in bytes, |
| 352 |
"Scaling info lost in cutout") |
| 353 |
|
| 354 |
def testMakeCubeDescriptor(self): |
| 355 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 356 |
allowed="dlmeta,dlget"> |
| 357 |
<datalinkCore> |
| 358 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 359 |
<metaMaker procDef="//soda#fits_makeWCSParams"> |
| 360 |
<bind key="axisMetaOverrides">{ |
| 361 |
"RA": {"ucd": "pos.eq.ra;meta.special"}, |
| 362 |
3: {"name": "ANGSTROMS", "unit": "0.1nm"}} |
| 363 |
</bind> |
| 364 |
</metaMaker> |
| 365 |
<FEED source="//soda#fits_standardBANDCutout" |
| 366 |
spectralAxis="3" |
| 367 |
wavelengthOverride="0.1nm"/> |
| 368 |
</datalinkCore></service>""") |
| 369 |
svc.parent = testhelpers.getTestRD() |
| 370 |
|
| 371 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 372 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")]}) |
| 373 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 374 |
self.assertEqual(tree.xpath("//PARAM[@name='RA']")[0].get("ucd"), |
| 375 |
"pos.eq.ra;meta.special") |
| 376 |
self.assertEqual(tree.xpath("//PARAM[@name='RA']/VALUES/MIN" |
| 377 |
)[0].get("value")[:11], "359.3580942") |
| 378 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']/VALUES/MAX" |
| 379 |
)[0].get("value")[:11], "30.98484850") |
| 380 |
self.assertEqual(tree.xpath("//PARAM[@name='ANGSTROMS']/VALUES/MIN" |
| 381 |
)[0].get("value"), "3749.0") |
| 382 |
self.assertEqual(tree.xpath("//PARAM[@name='ANGSTROMS']/VALUES/MAX" |
| 383 |
)[0].get("value"), "3755.0") |
| 384 |
lmaxPar = tree.xpath("//PARAM[@name='BAND']")[0] |
| 385 |
self.assertEqual(lmaxPar.get("ucd"), "em.wl") |
| 386 |
self.assertEqual(lmaxPar.get("unit"), "m") |
| 387 |
self.assertEqual(lmaxPar.xpath("VALUES/MAX")[0].get("value"), |
| 388 |
"3.7550000000000005e-07") |
| 389 |
|
| 390 |
def testCutoutNoSpatialCube(self): |
| 391 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 392 |
allowed="dlmeta,dlget"> |
| 393 |
<datalinkCore> |
| 394 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 395 |
<metaMaker procDef="//soda#fits_makeWCSParams"> |
| 396 |
<bind name="axisMetaOverrides">{3:{}}</bind> |
| 397 |
</metaMaker> |
| 398 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 399 |
<dataFunction procDef="//soda#fits_doWCSCutout"/> |
| 400 |
<dataFormatter procDef="//soda#fits_formatHDUs"/> |
| 401 |
</datalinkCore></service>""") |
| 402 |
|
| 403 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 404 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 405 |
"COO_3": ["3753 3755"], |
| 406 |
})) |
| 407 |
|
| 408 |
self.assertEqual(mime, "application/fits") |
| 409 |
hdr = fitstools.readPrimaryHeaderQuick(io.BytesIO(data)) |
| 410 |
self.assertEqual(hdr["NAXIS1"], 11) |
| 411 |
self.assertEqual(hdr["NAXIS2"], 7) |
| 412 |
self.assertEqual(hdr["NAXIS3"], 2) |
| 413 |
|
| 414 |
def testCutoutBANDCube(self): |
| 415 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 416 |
allowed="dlmeta,dlget"> |
| 417 |
<datalinkCore> |
| 418 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 419 |
<metaMaker procDef="//soda#fits_makeWCSParams"/> |
| 420 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 421 |
<FEED source="//soda#fits_standardBANDCutout" |
| 422 |
spectralAxis="3" |
| 423 |
wavelengthOverride="0.1nm"/> |
| 424 |
<dataFunction procDef="//soda#fits_doWCSCutout"/> |
| 425 |
<dataFormatter procDef="//soda#fits_formatHDUs"/> |
| 426 |
</datalinkCore></service>""") |
| 427 |
|
| 428 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 429 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 430 |
"BAND": ["3.755e-7 +Inf"], |
| 431 |
"RA": ["359.359 +Inf"], |
| 432 |
"DEC": ["30.39845 +Inf"], |
| 433 |
})) |
| 434 |
|
| 435 |
self.assertEqual(mime, "application/fits") |
| 436 |
hdr = fitstools.readPrimaryHeaderQuick(io.BytesIO(data)) |
| 437 |
self.assertEqual(hdr["NAXIS1"], 8) |
| 438 |
self.assertEqual(hdr["NAXIS2"], 7) |
| 439 |
self.assertEqual(hdr["NAXIS3"], 2) |
| 440 |
|
| 441 |
def testCutoutCube(self): |
| 442 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 443 |
allowed="dlmeta,dlget"> |
| 444 |
<datalinkCore> |
| 445 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 446 |
<metaMaker procDef="//soda#fits_makeWCSParams"> |
| 447 |
<bind name="axisMetaOverrides">{3:{}}</bind> |
| 448 |
</metaMaker> |
| 449 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 450 |
<dataFunction procDef="//soda#fits_doWCSCutout"/> |
| 451 |
<dataFormatter procDef="//soda#fits_formatHDUs"/> |
| 452 |
</datalinkCore></service>""") |
| 453 |
|
| 454 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 455 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 456 |
"RA": ["359.36 359.359"], |
| 457 |
"DEC": ["30.9845 30.985"], |
| 458 |
"COO_3": ["3753 3755"], |
| 459 |
})) |
| 460 |
|
| 461 |
self.assertEqual(mime, "application/fits") |
| 462 |
hdr = fitstools.readPrimaryHeaderQuick(io.BytesIO(data)) |
| 463 |
self.assertEqual(hdr["NAXIS1"], 4) |
| 464 |
self.assertEqual(hdr["NAXIS2"], 2) |
| 465 |
self.assertEqual(hdr["NAXIS3"], 2) |
| 466 |
|
| 467 |
_geoDLServiceLiteral = """<service id="foo" |
| 468 |
allowed="dlmeta,dlget"> |
| 469 |
<datalinkCore> |
| 470 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 471 |
<metaMaker procDef="//soda#fits_makeWCSParams"/> |
| 472 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 473 |
<FEED source="//soda#fits_Geometries"/> |
| 474 |
<dataFunction procDef="//soda#fits_doWCSCutout"/> |
| 475 |
<dataFormatter procDef="//soda#fits_formatHDUs"/> |
| 476 |
</datalinkCore></service>""" |
| 477 |
|
| 478 |
def testCutoutPOLYGON(self): |
| 479 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 480 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 481 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 482 |
"POLYGON": ["359.36 30.9845 359.359 30.9845" |
| 483 |
" 359.359 30.985"] |
| 484 |
})) |
| 485 |
|
| 486 |
self.assertEqual(mime, "application/fits") |
| 487 |
with io.BytesIO(data) as f: |
| 488 |
hdr = fitstools.readPrimaryHeaderQuick(f) |
| 489 |
self.assertEqual(hdr["NAXIS1"], 4) |
| 490 |
self.assertEqual(hdr["NAXIS2"], 2) |
| 491 |
self.assertEqual(hdr["NAXIS3"], 4) |
| 492 |
|
| 493 |
def testPOLYGONClashesRA(self): |
| 494 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 495 |
self.assertRaisesWithMsg(api.ValidationError, |
| 496 |
"Field RA: Attempt to cut out along axis 1 that has been" |
| 497 |
" modified before.", |
| 498 |
trialhelpers.runSvcWith, |
| 499 |
(svc, "dlget", { |
| 500 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 501 |
"RA": ["359.36 359.359"], |
| 502 |
"POLYGON": ["359.36 30.9845 359.359 30.9845" |
| 503 |
" 359.359 30.985"] |
| 504 |
})) |
| 505 |
|
| 506 |
def testEmptyCutout(self): |
| 507 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 508 |
self.assertRaisesWithMsg(soda.EmptyData, |
| 509 |
"", |
| 510 |
trialhelpers.runSvcWith, |
| 511 |
(svc, "dlget", { |
| 512 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 513 |
"CIRCLE": ["36 48 0.0004"], |
| 514 |
})) |
| 515 |
|
| 516 |
def testCutoutCIRCLE(self): |
| 517 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 518 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 519 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 520 |
"CIRCLE": ["359.36 30.984 0.0004"] |
| 521 |
})) |
| 522 |
|
| 523 |
self.assertEqual(mime, "application/fits") |
| 524 |
hdr = fitstools.readPrimaryHeaderQuick(io.BytesIO(data)) |
| 525 |
# TODO: I'd really expect NAXIS1==NAXIS2, but I cannot find a mistake |
| 526 |
# in my reasoning up to here. Hm. |
| 527 |
self.assertEqual(hdr["NAXIS1"], 4) |
| 528 |
self.assertEqual(hdr["NAXIS2"], 3) |
| 529 |
self.assertEqual(hdr["NAXIS3"], 4) |
| 530 |
|
| 531 |
def testCIRCLEClashesPOLYGON(self): |
| 532 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 533 |
self.assertRaisesWithMsg(api.ValidationError, |
| 534 |
"Field CIRCLE: Attempt to cut out along axis 1 that has been" |
| 535 |
" modified before.", |
| 536 |
trialhelpers.runSvcWith, |
| 537 |
(svc, "dlget", { |
| 538 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 539 |
"CIRCLE": ["359.36 30.9845 0.0004"], |
| 540 |
"POLYGON": ["359.36 30.9845 359.359 30.9845" |
| 541 |
" 359.359 30.985"] |
| 542 |
})) |
| 543 |
|
| 544 |
def testKindPar(self): |
| 545 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 546 |
allowed="dlmeta,dlget"> |
| 547 |
<datalinkCore> |
| 548 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 549 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 550 |
<FEED source="//soda#fits_genKindPar"/> |
| 551 |
</datalinkCore></service>""") |
| 552 |
mime, data = trialhelpers.runSvcWith(svc, "dlget", { |
| 553 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 554 |
"KIND": ["HEADER"],}) |
| 555 |
self.assertEqual(mime, "application/fits-header") |
| 556 |
self.assertEqual(len(data), 2880) |
| 557 |
|
| 558 |
def testCutoutHeader(self): |
| 559 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 560 |
allowed="dlmeta,dlget"> |
| 561 |
<datalinkCore> |
| 562 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 563 |
<metaMaker procDef="//soda#fits_makeWCSParams"> |
| 564 |
<bind name="axisMetaOverrides">{3:{}}</bind> |
| 565 |
</metaMaker> |
| 566 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 567 |
<dataFunction procDef="//soda#fits_doWCSCutout"/> |
| 568 |
<FEED source="//soda#fits_genKindPar"/> |
| 569 |
</datalinkCore></service>""") |
| 570 |
mime, data = trialhelpers.runSvcWith(svc, "dlget", { |
| 571 |
"ID": rscdef.getStandardPubDID("data/excube.fits"), |
| 572 |
"COO_3": ["3753 400000"], |
| 573 |
"KIND": "HEADER",}) |
| 574 |
self.assertEqual(mime, "application/fits-header") |
| 575 |
self.assertTrue(b"NAXIS3 = 2" in data) |
| 576 |
|
| 577 |
def testFITSNoSTC(self): |
| 578 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 579 |
allowed="dlmeta,dlget"> |
| 580 |
<datalinkCore> |
| 581 |
<FEED source="//soda#fits_standardDLFuncs" |
| 582 |
accrefPrefix="" stcs=""/> |
| 583 |
</datalinkCore></service>""") |
| 584 |
svc.parent = testhelpers.getTestRD() |
| 585 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 586 |
"ID": rscdef.getStandardPubDID("data/excube.fits")}) |
| 587 |
self.assertTrue(b"<DATA><TABLEDATA>" in data) |
| 588 |
|
| 589 |
def testPixelMeta(self): |
| 590 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 591 |
allowed="dlmeta,dlget"> |
| 592 |
<datalinkCore> |
| 593 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 594 |
<FEED source="//soda#fits_genPixelPar"/> |
| 595 |
</datalinkCore></service>""") |
| 596 |
svc.parent = testhelpers.getTestRD() |
| 597 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 598 |
"ID": rscdef.getStandardPubDID("data/excube.fits")}) |
| 599 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 600 |
self.assertEqual(tree.xpath("//PARAM[@name='PIXEL_3']/VALUES/MAX" |
| 601 |
)[0].get("value"), "4") |
| 602 |
self.assertEqual(tree.xpath("//PARAM[@name='PIXEL_1']" |
| 603 |
)[0].get("datatype"), "int") |
| 604 |
|
| 605 |
def testPixelCutout(self): |
| 606 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 607 |
allowed="dlmeta,dlget"> |
| 608 |
<datalinkCore> |
| 609 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 610 |
<FEED source="//soda#fits_standardDLFuncs" |
| 611 |
stcs="" accrefPrefix=""/> |
| 612 |
</datalinkCore></service>""") |
| 613 |
svc.parent = testhelpers.getTestRD() |
| 614 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 615 |
"ID": rscdef.getStandardPubDID("data/excube.fits"), |
| 616 |
"PIXEL_1": ["4 4"], |
| 617 |
"PIXEL_3": ["2 2"]})) |
| 618 |
self.assertEqual(mime, "application/fits") |
| 619 |
self.assertTrue(b"NAXIS1 = 1" in data) |
| 620 |
self.assertTrue(b"NAXIS2 = 7" in data) |
| 621 |
self.assertTrue(b"NAXIS3 = 1" in data) |
| 622 |
|
| 623 |
|
| 624 |
class FITSAdaptationTests(testhelpers.VerboseTest): |
| 625 |
resources = [("fitsTable", tresc.fitsTable)] |
| 626 |
|
| 627 |
def testNoScaleWhen3D(self): |
| 628 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 629 |
allowed="dlmeta,dlget"> |
| 630 |
<datalinkCore> |
| 631 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 632 |
<FEED source="//soda#fits_standardDLFuncs" |
| 633 |
spectralAxis="1"/> |
| 634 |
</datalinkCore></service>""") |
| 635 |
svc.parent = testhelpers.getTestRD() |
| 636 |
self.assertRaisesWithMsg(api.ValidationError, |
| 637 |
"Field scale: The following parameter(s) are not accepted by this service: scale", |
| 638 |
trialhelpers.runSvcWith, |
| 639 |
(svc, "dlget", {"SCALE": ["2"], |
| 640 |
"ID": rscdef.getStandardPubDID("data/excube.fits"),})) |
| 641 |
|
| 642 |
def testNoBandByDefault(self): |
| 643 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 644 |
allowed="dlmeta,dlget"> |
| 645 |
<datalinkCore> |
| 646 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 647 |
<FEED source="//soda#fits_standardDLFuncs"/> |
| 648 |
</datalinkCore></service>""") |
| 649 |
svc.parent = testhelpers.getTestRD() |
| 650 |
self.assertRaisesWithMsg(api.ValidationError, |
| 651 |
"Field band: The following parameter(s) are not accepted by this service: band", |
| 652 |
trialhelpers.runSvcWith, |
| 653 |
(svc, "dlget", {"BAND": ["2 4"], |
| 654 |
"ID": rscdef.getStandardPubDID("data/excube.fits"),})) |
| 655 |
|
| 656 |
|
| 657 |
class SODAArtificialTests(testhelpers.VerboseTest): |
| 658 |
# these are tests on totally artificial FITS headers as per |
| 659 |
# data/cores#make-fits-desc |
| 660 |
|
| 661 |
def testNonLambdaCutout(self): |
| 662 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 663 |
allowed="dlmeta,dlget"> |
| 664 |
<datalinkCore> |
| 665 |
<descriptorGenerator procDef="data/cores#make-fits-desc"/> |
| 666 |
<FEED source="//soda#fits_standardBANDCutout" |
| 667 |
spectralAxis="3"/> |
| 668 |
<dataFormatter procDef="data/cores#debug-formatter"/> |
| 669 |
</datalinkCore></service>""") |
| 670 |
|
| 671 |
mime, data = trialhelpers.runSvcWith(svc, "dlget", { |
| 672 |
"ID": ["NAXIS3=300&CRVAL3=400&CRPIX3=1&CDELT3=0.5&CUNIT3=MHz"], |
| 673 |
"BAND": ["0.7425 0.7444"], |
| 674 |
}) |
| 675 |
self.assertEqual(re.sub(r"\.[0-9]*", "", data), "(3, 6, 9)") |
| 676 |
|
| 677 |
_velCubeId = ( |
| 678 |
"NAXIS=3&NAXIS3=400&CUNIT3=km%2fs&CTYPE3=VRAD&CRVAL3=4710.6" |
| 679 |
"&CRPIX3=30&CDELT3=-3") |
| 680 |
|
| 681 |
def testVelocityCutout(self): |
| 682 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 683 |
allowed="dlmeta,dlget"> |
| 684 |
<datalinkCore> |
| 685 |
<descriptorGenerator procDef="data/cores#make-fits-desc"/> |
| 686 |
<FEED source="//soda#fits_standardDLFuncs" velocityAxis="3"> |
| 687 |
<PRUNE name="makeHDUList"/> |
| 688 |
<PRUNE name="doWCSCutout"/> |
| 689 |
<PRUNE name="formatHDUs"/> |
| 690 |
</FEED> |
| 691 |
<dataFormatter procDef="data/cores#debug-formatter"/> |
| 692 |
</datalinkCore></service>""") |
| 693 |
mime, data = trialhelpers.runSvcWith(svc, "dlget", { |
| 694 |
"ID": [self._velCubeId], |
| 695 |
"VELOCITY": ["4710.6e3 4790000"], |
| 696 |
}) |
| 697 |
self.assertEqual(data, '(3, 3, 30)') |
| 698 |
|
| 699 |
# second test: Make sure no automatic 3rd axis param is present |
| 700 |
# (see testAutomatic3rdAxis) |
| 701 |
self.assertRaisesWithMsg(api.ValidationError, |
| 702 |
"Field vrad_3: The following parameter(s) are not accepted by this service: vrad_3", |
| 703 |
trialhelpers.runSvcWith, |
| 704 |
(svc, "dlget", { |
| 705 |
"ID": [self._velCubeId], |
| 706 |
"VRAD_3": ["4710 4790"], |
| 707 |
})) |
| 708 |
|
| 709 |
def testAutomatic3rdAxis(self): |
| 710 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 711 |
allowed="dlmeta,dlget"> |
| 712 |
<datalinkCore> |
| 713 |
<descriptorGenerator procDef="data/cores#make-fits-desc"/> |
| 714 |
<FEED source="//soda#fits_standardDLFuncs" velocityAxis="3" |
| 715 |
axisMetaOverrides="{3:{}}"> |
| 716 |
<PRUNE name="makeHDUList"/> |
| 717 |
<PRUNE name="formatHDUs"/> |
| 718 |
</FEED> |
| 719 |
<dataFormatter procDef="data/cores#debug-formatter"/> |
| 720 |
</datalinkCore></service>""") |
| 721 |
mime, data = trialhelpers.runSvcWith(svc, "dlget", { |
| 722 |
"ID": [self._velCubeId], |
| 723 |
"VRAD_3": ["4710 4790"], |
| 724 |
}) |
| 725 |
self.assertEqual(data, '(3, 3, 31)') |
| 726 |
|
| 727 |
|
| 728 |
class DatalinkSTCTest(testhelpers.VerboseTest): |
| 729 |
resources = [("fitsTable", tresc.fitsTable)] |
| 730 |
|
| 731 |
def testSTCDefsPresent(self): |
| 732 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 733 |
allowed="dlmeta,dlget"> |
| 734 |
<datalinkCore> |
| 735 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 736 |
<metaMaker> |
| 737 |
<setup> |
| 738 |
<code> |
| 739 |
parSTC = stc.parseQSTCS("PositionInterval ICRS BARYCENTER" |
| 740 |
' "RA_MIN" "DEC_MIN" "RA_MAX" "DEC_MAX"') |
| 741 |
</code> |
| 742 |
</setup> |
| 743 |
<code> |
| 744 |
for name in ["RA", "DEC"]: |
| 745 |
for ik in genLimitKeys(MS(InputKey, name=name, stc=parSTC)): |
| 746 |
yield ik |
| 747 |
</code> |
| 748 |
</metaMaker> |
| 749 |
</datalinkCore></service>""") |
| 750 |
svc.parent = testhelpers.getTestRD() |
| 751 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 752 |
"ID": [rscdef.getStandardPubDID("data/ex.fits")]}) |
| 753 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 754 |
self.assertEqual(len(tree.xpath( |
| 755 |
"//GROUP[@utype='stc:CatalogEntryLocation']/PARAM")), 4) |
| 756 |
self.assertEqual(len(tree.xpath( |
| 757 |
"//GROUP[@utype='stc:CatalogEntryLocation']/PARAMref")), 4) |
| 758 |
self.assertEqual(tree.xpath( |
| 759 |
"//PARAM[@utype='stc:AstroCoordSystem.SpaceFrame.ReferencePosition']" |
| 760 |
)[0].get("value"), "BARYCENTER") |
| 761 |
|
| 762 |
# follow a reference |
| 763 |
id = tree.xpath("//PARAMref[@utype='stc:AstroCoordArea" |
| 764 |
".Position2VecInterval.HiLimit2Vec.C2']")[0].get("ref") |
| 765 |
self.assertEqual(tree.xpath("//PARAM[@ID='%s']"%id)[0].get("name"), |
| 766 |
"DEC_MAX") |
| 767 |
|
| 768 |
def testTwoSystems(self): |
| 769 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 770 |
allowed="dlmeta, dlget"> |
| 771 |
<datalinkCore> |
| 772 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 773 |
<metaMaker> |
| 774 |
<setup> |
| 775 |
<code> |
| 776 |
parSTC = stc.parseQSTCS("PositionInterval ICRS BARYCENTER" |
| 777 |
' "RA_MIN" "DEC_MIN" "RA_MAX" "DEC_MAX"') |
| 778 |
</code> |
| 779 |
</setup> |
| 780 |
<code> |
| 781 |
for name in ["RA", "DEC"]: |
| 782 |
for ik in genLimitKeys(MS(InputKey, name=name, stc=parSTC)): |
| 783 |
yield ik |
| 784 |
</code> |
| 785 |
</metaMaker> |
| 786 |
<metaMaker> |
| 787 |
<setup> |
| 788 |
<code> |
| 789 |
parSTC = stc.parseQSTCS("PositionInterval GALACTIC" |
| 790 |
' "LAMB_MIN" "BET_MIN" "LAMB_MAX" "BET_MAX"') |
| 791 |
</code> |
| 792 |
</setup> |
| 793 |
<code> |
| 794 |
for name in ["LAMB", "BET"]: |
| 795 |
for ik in genLimitKeys(MS(InputKey, name=name, stc=parSTC)): |
| 796 |
yield ik |
| 797 |
</code> |
| 798 |
</metaMaker> |
| 799 |
|
| 800 |
</datalinkCore></service>""") |
| 801 |
svc.parent = testhelpers.getTestRD() |
| 802 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 803 |
"ID": [rscdef.getStandardPubDID("data/ex.fits")]}) |
| 804 |
|
| 805 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 806 |
self.assertEqual(len(tree.xpath( |
| 807 |
"//GROUP[@utype='stc:CatalogEntryLocation']")), 2) |
| 808 |
ids = [el.get("ref") for el in |
| 809 |
tree.xpath("//PARAMref[@utype='stc:AstroCoordArea" |
| 810 |
".Position2VecInterval.HiLimit2Vec.C2']")] |
| 811 |
names = set(tree.xpath("//PARAM[@ID='%s']"%id)[0].get("name") |
| 812 |
for id in ids) |
| 813 |
self.assertEqual(names, set(["DEC_MAX", "BET_MAX"])) |
| 814 |
|
| 815 |
|
| 816 |
def _iterWCSSamples(): |
| 817 |
for center in [ |
| 818 |
(0, 0), |
| 819 |
(10, 45), |
| 820 |
(120, -30), |
| 821 |
(240, 87), |
| 822 |
(0, -90), |
| 823 |
(180, 90)]: |
| 824 |
yield center, testhelpers.computeWCSKeys(center, (2, 3), cutCrap=True) |
| 825 |
|
| 826 |
|
| 827 |
def _getFakeDescriptorForWCS(wcsDict): |
| 828 |
"""returns something good enough to stand in for a FITSDescriptor |
| 829 |
in SODA code from stuff generated by computeWCSKeys. |
| 830 |
""" |
| 831 |
res = testhelpers.StandIn(hdr=wcsDict, |
| 832 |
changingAxis=lambda *args: None, |
| 833 |
slices=[]) |
| 834 |
soda.ensureSkyWCS(res) |
| 835 |
return res |
| 836 |
|
| 837 |
|
| 838 |
class _MetaProcBase(testhelpers.TestResource): |
| 839 |
procId = None |
| 840 |
|
| 841 |
def make(self, deps): |
| 842 |
proc = api.makeStruct(datalink.MetaMaker, |
| 843 |
procDef=api.resolveCrossId(self.procId) |
| 844 |
).compile(parent=testhelpers.getTestRD()) |
| 845 |
return lambda arg: proc(None, arg) |
| 846 |
|
| 847 |
|
| 848 |
class _POLYGONMetaProc(_MetaProcBase): |
| 849 |
procId = "//soda#fits_makePOLYGONMeta" |
| 850 |
|
| 851 |
|
| 852 |
class SODA_POLYGONMetaTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 853 |
resources = [('proc', _POLYGONMetaProc())] |
| 854 |
|
| 855 |
# generated through maketruth below |
| 856 |
truth = { |
| 857 |
(0, 0): '359.0021009128 -1.4964326084 359.0021009128 1.4994301037 0.9998984794 1.4994291918 0.9998984794 -1.4964316983', |
| 858 |
(10, 45): '8.62481612 43.4950994178 8.5509805405 46.4905074605 11.4519220644 46.490470762 11.3779386981 43.4950663641', |
| 859 |
(120, -30): '118.8301216734 -31.4913388619 118.8649164101 -28.4956276477 121.1373577096 -28.4956087361 121.1722221187 -31.4913175209', |
| 860 |
(240, 87): '227.4781529243 85.3942399052 206.3749183808 88.1983560305 273.6779932638 88.1972488451 252.546147226 85.3938074824', |
| 861 |
(0, -90): '213.690067526 -88.2014209215 326.3628534376 -88.1989264651 33.690067526 -88.1978189291 146.2569707456 -88.2003118463' , |
| 862 |
(180, 90): '146.309932474 88.2014209215 33.6371465624 88.1989264651 326.309932474 88.1978189291 213.7430292544 88.2003118463', |
| 863 |
} |
| 864 |
|
| 865 |
def _produceKeys(self, sample): |
| 866 |
pos, header = sample |
| 867 |
desc = _getFakeDescriptorForWCS(header) |
| 868 |
return list(self.proc(desc)) |
| 869 |
|
| 870 |
def _runTest(self, sample): |
| 871 |
keys = self._produceKeys(sample) |
| 872 |
key = testhelpers.pickSingle(keys) |
| 873 |
self.assertEqual(key.type, "double precision(*)") |
| 874 |
self.assertEqual(key.values.max, |
| 875 |
self.truth[sample[0]]) |
| 876 |
|
| 877 |
def getTruthFor(self, sample): |
| 878 |
key = self._produceKeys(sample)[0] |
| 879 |
points = [float(v) for v in key.values.max.split()] |
| 880 |
return "%r: %r"%(sample[0], key.values.max), [ |
| 881 |
('center', sample[0][0], sample[0][1])]+[ |
| 882 |
('vertex',)+p for p in utils.grouped(2, points)] |
| 883 |
|
| 884 |
samples = list(_iterWCSSamples()) |
| 885 |
|
| 886 |
|
| 887 |
class _CIRCLEMetaProc(_MetaProcBase): |
| 888 |
procId = "//soda#fits_makeCIRCLEMeta" |
| 889 |
|
| 890 |
|
| 891 |
class SODA_CIRCLEMetaTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 892 |
resources = [('proc', _CIRCLEMetaProc())] |
| 893 |
|
| 894 |
# generated through maketruth below |
| 895 |
truth = { |
| 896 |
(0, 0): '0. 0. 1.8021810709', |
| 897 |
(10, 45): '10. 45. 1.8021810709', |
| 898 |
(120, -30): '120. -30. 1.8021810709', |
| 899 |
(240, 87): '240. 87. 1.8021810709', |
| 900 |
(0, -90): '180. -90. 1.8021810709', |
| 901 |
(180, 90): '180. 90. 1.8021810709', |
| 902 |
} |
| 903 |
|
| 904 |
def _produceKeys(self, sample): |
| 905 |
pos, header = sample |
| 906 |
desc = _getFakeDescriptorForWCS(header) |
| 907 |
return list(self.proc(desc)) |
| 908 |
|
| 909 |
def _runTest(self, sample): |
| 910 |
keys = self._produceKeys(sample) |
| 911 |
key = testhelpers.pickSingle(keys) |
| 912 |
self.assertEqual(key.type, "double precision(3)") |
| 913 |
self.assertEqual(key.values.max, |
| 914 |
self.truth[sample[0]]) |
| 915 |
|
| 916 |
def getTruthFor(self, sample): |
| 917 |
from gavo.utils import pgsphere |
| 918 |
|
| 919 |
key = self._produceKeys(sample)[0] |
| 920 |
circ = pgsphere.SCircle.fromDALI(key.values.max) |
| 921 |
points = [float(v) for v in circ.asPoly().asSODA().split()] |
| 922 |
return "%r: %r"%(sample[0], key.values.max), [ |
| 923 |
('center', sample[0][0], sample[0][1])]+[ |
| 924 |
('vertex',)+p for p in utils.grouped(2, points)] |
| 925 |
|
| 926 |
samples = list(_iterWCSSamples()) |
| 927 |
|
| 928 |
|
| 929 |
class _CIRCLESliceProc(_MetaProcBase): |
| 930 |
procId = "//soda#fits_makeCIRCLESlice" |
| 931 |
|
| 932 |
def make(self, deps): |
| 933 |
return api.makeStruct(datalink.DataFunction, |
| 934 |
procDef=api.resolveCrossId(self.procId) |
| 935 |
).compile(parent=testhelpers.getTestRD()) |
| 936 |
|
| 937 |
|
| 938 |
class SODA_CIRCLECutoutIntervalTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 939 |
resources = [('proc', _CIRCLESliceProc())] |
| 940 |
|
| 941 |
def _runTest(self, sample): |
| 942 |
pos, header = sample |
| 943 |
desc = _getFakeDescriptorForWCS(header) |
| 944 |
soda.ensureSkyWCS(desc) |
| 945 |
args = {"CIRCLE": pgsphere.SCircle.fromDALI(pos+(0.5,))} |
| 946 |
_ = self.proc(desc, args) |
| 947 |
self.assertEqual( |
| 948 |
desc.slices, |
| 949 |
[(1, 250, 750), (2, 333, 667)]) |
| 950 |
|
| 951 |
samples = list(_iterWCSSamples()) |
| 952 |
|
| 953 |
|
| 954 |
class SODAPOSTest(testhelpers.VerboseTest): |
| 955 |
resources = [("fitsTable", tresc.fitsTable)] |
| 956 |
|
| 957 |
def _getSvc(self): |
| 958 |
return api.parseFromString(svcs.Service, """<service id="foo" |
| 959 |
allowed="dlmeta,dlget"> |
| 960 |
<datalinkCore> |
| 961 |
<descriptorGenerator procDef="//soda#fits_genDesc"/> |
| 962 |
<FEED source="//soda#fits_standardDLFuncs"/> |
| 963 |
</datalinkCore></service>""") |
| 964 |
|
| 965 |
def testPOSError(self): |
| 966 |
self.assertRaisesWithMsg(api.ValidationError, |
| 967 |
"Field POS: Invalid SIAPv2 geometry: 'Circle 30 40'" |
| 968 |
" (expected a SIAPv2 shape name)", |
| 969 |
trialhelpers.runSvcWith, |
| 970 |
(self._getSvc(), |
| 971 |
"dlget", { |
| 972 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 973 |
"POS": ["Circle 30 40"], |
| 974 |
})) |
| 975 |
|
| 976 |
def testPOSRange(self): |
| 977 |
mime, data = _dissectSODAFile( |
| 978 |
trialhelpers.runSvcWith(self._getSvc(), |
| 979 |
"dlget", { |
| 980 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 981 |
"POS": ["RANGE 359.359 359.36 30.9845 30.985"], |
| 982 |
})) |
| 983 |
|
| 984 |
self.assertEqual(mime, "application/fits") |
| 985 |
hdr = fitstools.readPrimaryHeaderQuick(io.BytesIO(data)) |
| 986 |
self.assertEqual(hdr["NAXIS1"], 4) |
| 987 |
self.assertEqual(hdr["NAXIS2"], 2) |
| 988 |
self.assertEqual(hdr["NAXIS3"], 4) |
| 989 |
|
| 990 |
def testPOSCircle(self): |
| 991 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(self._getSvc(), "dlget", { |
| 992 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 993 |
"POS": ["CIRCLE 359.359 30.984 0.001"], |
| 994 |
})) |
| 995 |
hdr = fitstools.readPrimaryHeaderQuick(io.BytesIO(data)) |
| 996 |
self.assertEqual(hdr["NAXIS1"], 7) |
| 997 |
self.assertEqual(hdr["NAXIS2"], 7) |
| 998 |
|
| 999 |
def testPOSPolygon(self): |
| 1000 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(self._getSvc(), "dlget", { |
| 1001 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")], |
| 1002 |
"POS": ["POLYGON 359.359 30.984 359.3592 30.985 359.3593 30.986"], |
| 1003 |
})) |
| 1004 |
hdr = fitstools.readPrimaryHeaderQuick(io.BytesIO(data)) |
| 1005 |
self.assertEqual(hdr["NAXIS1"], 2) |
| 1006 |
self.assertEqual(hdr["NAXIS2"], 4) |
| 1007 |
|
| 1008 |
|
| 1009 |
################ let's run a few of them on compressed FITSes, too |
| 1010 |
|
| 1011 |
class CFITSTable(tresc.RDDataResource): |
| 1012 |
"""puts ex.fits.fz (a compressed FITS) input the products table. |
| 1013 |
""" |
| 1014 |
dataId = "import_compressed_fits" |
| 1015 |
|
| 1016 |
_cfitsTable = CFITSTable() |
| 1017 |
|
| 1018 |
|
| 1019 |
class SODACFITSTest(testhelpers.VerboseTest): |
| 1020 |
resources = [("cfitsTable", _cfitsTable)] |
| 1021 |
|
| 1022 |
_geoDLServiceLiteral = """<service id="foo" |
| 1023 |
allowed="dlmeta,dlget"> |
| 1024 |
<datalinkCore> |
| 1025 |
<descriptorGenerator procDef="//soda#fits_genDesc"> |
| 1026 |
<bind key="qnd">False</bind> |
| 1027 |
</descriptorGenerator> |
| 1028 |
<metaMaker procDef="//soda#fits_makeWCSParams"/> |
| 1029 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 1030 |
<FEED source="//soda#fits_Geometries"/> |
| 1031 |
<dataFunction procDef="//soda#fits_doWCSCutout"/> |
| 1032 |
<dataFormatter procDef="//soda#fits_formatHDUs"/> |
| 1033 |
</datalinkCore></service>""" |
| 1034 |
|
| 1035 |
def testMakeDescriptor(self): |
| 1036 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 1037 |
allowed="dlmeta,dlget"> |
| 1038 |
<datalinkCore> |
| 1039 |
<descriptorGenerator procDef="//soda#fits_genDesc"> |
| 1040 |
<bind key="accrefPrefix">"data/"</bind> |
| 1041 |
<bind key="qnd">False</bind> |
| 1042 |
</descriptorGenerator> |
| 1043 |
<metaMaker procDef="//soda#fits_makeWCSParams"/> |
| 1044 |
<metaMaker><code> |
| 1045 |
assert descriptor.hdr["EQUINOX"]==2000. |
| 1046 |
assert ([int(n) for n in |
| 1047 |
descriptor.skyWCS.wcs_world2pix([(166, 20)], 0)[0]] |
| 1048 |
==[7261, 7984]) |
| 1049 |
yield descriptor.makeLink("http://dc.g-vo.org", |
| 1050 |
semantics="#sentinel", |
| 1051 |
contentType="text/english") |
| 1052 |
</code></metaMaker> |
| 1053 |
</datalinkCore></service>""") |
| 1054 |
svc.parent = testhelpers.getTestRD() |
| 1055 |
|
| 1056 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 1057 |
"ID": [rscdef.getStandardPubDID("data/ex.fits.fz")]}) |
| 1058 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 1059 |
self.assertEqual(tree.xpath("//PARAM[@name='RA']")[0].get("unit"), |
| 1060 |
"deg") |
| 1061 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']")[0].get("xtype"), |
| 1062 |
"interval") |
| 1063 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']")[0].get("datatype"), |
| 1064 |
"double") |
| 1065 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']")[0].get("arraysize"), |
| 1066 |
"2") |
| 1067 |
self.assertEqual(tree.xpath("//PARAM[@name='RA']/VALUES/MIN" |
| 1068 |
)[0].get("value")[:7], "168.243") |
| 1069 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']/VALUES/MAX" |
| 1070 |
)[0].get("value")[:8], "22.21928") |
| 1071 |
self.assertEqual(tree.xpath("//PARAM[@name='DEC']/DESCRIPTION" |
| 1072 |
)[0].text, "The latitude coordinate") |
| 1073 |
# there's been a problem in the meta maker above -- perhaps |
| 1074 |
# a failed assertion? See your logs. |
| 1075 |
self.assertEqual(tree.xpath("//TR[TD='#sentinel']/TD" |
| 1076 |
)[1].text, "http://dc.g-vo.org") |
| 1077 |
self.assertEqual( |
| 1078 |
tree.xpath("//TR[TD[6]='#this']/TD[10]")[0].text, |
| 1079 |
'#image') |
| 1080 |
|
| 1081 |
def testCutoutPOLYGON(self): |
| 1082 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 1083 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 1084 |
"ID": [rscdef.getStandardPubDID("data/ex.fits.fz")], |
| 1085 |
"POLYGON": ["168.2464 22.2147 168.2454 22.2147 168.2464 22.2149"] |
| 1086 |
})) |
| 1087 |
|
| 1088 |
self.assertEqual(mime, "application/fits") |
| 1089 |
with pyfits.open(io.BytesIO(data)) as hdus: |
| 1090 |
hdr = hdus[1].header |
| 1091 |
self.assertEqual(hdr["NAXIS1"], 4) |
| 1092 |
self.assertEqual(hdr["NAXIS2"], 2) |
| 1093 |
|
| 1094 |
def testEmptyCutout(self): |
| 1095 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 1096 |
self.assertRaisesWithMsg(soda.EmptyData, |
| 1097 |
"", |
| 1098 |
trialhelpers.runSvcWith, |
| 1099 |
(svc, "dlget", { |
| 1100 |
"ID": [rscdef.getStandardPubDID("data/ex.fits.fz")], |
| 1101 |
"CIRCLE": ["160 48 0.0004"], |
| 1102 |
})) |
| 1103 |
|
| 1104 |
def testCutoutCIRCLE(self): |
| 1105 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 1106 |
mime, data = _dissectSODAFile(trialhelpers.runSvcWith(svc, "dlget", { |
| 1107 |
"ID": [rscdef.getStandardPubDID("data/ex.fits.fz")], |
| 1108 |
"CIRCLE": ["168.2455 22.2161 0.0002"] |
| 1109 |
})) |
| 1110 |
|
| 1111 |
self.assertEqual(mime, "application/fits") |
| 1112 |
hdr = pyfits.open(io.BytesIO(data))[1].header |
| 1113 |
# TODO: I'd really expect NAXIS1==NAXIS2, but I cannot find a mistake |
| 1114 |
# in my reasoning up to here. Hm. |
| 1115 |
self.assertEqual(hdr["NAXIS1"], 2) |
| 1116 |
self.assertEqual(hdr["NAXIS2"], 3) |
| 1117 |
|
| 1118 |
def testCIRCLEClashesPOLYGON(self): |
| 1119 |
svc = api.parseFromString(svcs.Service, self._geoDLServiceLiteral) |
| 1120 |
self.assertRaisesWithMsg(api.ValidationError, |
| 1121 |
"Field CIRCLE: Attempt to cut out along axis 1 that has been" |
| 1122 |
" modified before.", |
| 1123 |
trialhelpers.runSvcWith, |
| 1124 |
(svc, "dlget", { |
| 1125 |
"ID": [rscdef.getStandardPubDID("data/ex.fits.fz")], |
| 1126 |
"CIRCLE": ["168.245 22.215 0.0004"], |
| 1127 |
"POLYGON": ["168.244 22.210 168.244 22.213 168.246 22.213"] |
| 1128 |
})) |
| 1129 |
|
| 1130 |
def testKindPar(self): |
| 1131 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 1132 |
allowed="dlmeta,dlget"> |
| 1133 |
<datalinkCore> |
| 1134 |
<descriptorGenerator procDef="//soda#fits_genDesc"> |
| 1135 |
<bind key="qnd">False</bind> |
| 1136 |
</descriptorGenerator> |
| 1137 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 1138 |
<FEED source="//soda#fits_genKindPar"/> |
| 1139 |
</datalinkCore></service>""") |
| 1140 |
mime, data = trialhelpers.runSvcWith(svc, "dlget", { |
| 1141 |
"ID": [rscdef.getStandardPubDID("data/ex.fits.fz")], |
| 1142 |
"KIND": ["HEADER"],}) |
| 1143 |
self.assertEqual(mime, "application/fits-header") |
| 1144 |
self.assertEqual(len(data), 11520) |
| 1145 |
|
| 1146 |
def testCutoutHeader(self): |
| 1147 |
svc = api.parseFromString(svcs.Service, """<service id="foo" |
| 1148 |
allowed="dlmeta,dlget"> |
| 1149 |
<datalinkCore> |
| 1150 |
<descriptorGenerator procDef="//soda#fits_genDesc"> |
| 1151 |
<bind name="qnd">False</bind> |
| 1152 |
</descriptorGenerator> |
| 1153 |
<metaMaker procDef="//soda#fits_makeWCSParams"/> |
| 1154 |
<dataFunction procDef="//soda#fits_makeHDUList"/> |
| 1155 |
<FEED source="//soda#fits_genPixelPar"/> |
| 1156 |
<FEED source="//soda#fits_genKindPar"/> |
| 1157 |
</datalinkCore></service>""") |
| 1158 |
mime, data = trialhelpers.runSvcWith(svc, "dlget", { |
| 1159 |
"ID": rscdef.getStandardPubDID("data/ex.fits.fz"), |
| 1160 |
"PIXEL_2": ["1 2"], |
| 1161 |
"KIND": "HEADER",}) |
| 1162 |
self.assertEqual(mime, "application/fits-header") |
| 1163 |
self.assertTrue(b"NAXIS2 = 2" in data) |
| 1164 |
self.assertEqual(len(data), 11520) |
| 1165 |
|
| 1166 |
|
| 1167 |
################ Start SDM tests |
| 1168 |
|
| 1169 |
|
| 1170 |
class _SDMMetaTable(testhelpers.TestResource): |
| 1171 |
resources = [("ssaTable", tresc.ssaTestTable)] |
| 1172 |
|
| 1173 |
def make(self, deps): |
| 1174 |
mediaType, doc = trialhelpers.runSvcWith( |
| 1175 |
api.resolveCrossId("data/ssatest#dl"), "dlmeta", |
| 1176 |
{"ID": 'ivo://test.inv/test1', "RESPONSEFORMAT": "votable/td"}) |
| 1177 |
return testhelpers.getXMLTree(doc, debug=False) |
| 1178 |
|
| 1179 |
|
| 1180 |
class SDMMetaTest(testhelpers.VerboseTest): |
| 1181 |
resources = [("tree", _SDMMetaTable())] |
| 1182 |
|
| 1183 |
def testDataOrigin(self): |
| 1184 |
self.assertEqual( |
| 1185 |
self.tree.xpath("//INFO[@name='citation']")[0].get("value"), |
| 1186 |
"http://localhost:8080/data/ssatest/dl/howtocite") |
| 1187 |
self.assertEqual( |
| 1188 |
self.tree.xpath("//INFO[@name='request']")[0].get("value"), |
| 1189 |
"http://localhost:8080/?ID=ivo%3A%2F%2Ftest.inv%2F" |
| 1190 |
"test1&RESPONSEFORMAT=votable%2Ftd") |
| 1191 |
|
| 1192 |
def testColumnsPresent(self): |
| 1193 |
self.assertEqual( |
| 1194 |
[f.get("name") for f in self.tree.xpath("//FIELD")], |
| 1195 |
['ID', 'access_url', 'service_def', 'error_message', |
| 1196 |
'description', 'semantics', 'content_type', 'content_length', |
| 1197 |
'local_semantics', 'content_qualifier']) |
| 1198 |
|
| 1199 |
def testThisContentQualifier(self): |
| 1200 |
# semantics is field 6, content_qualifier 10 at this time. |
| 1201 |
self.assertEqual( |
| 1202 |
self.tree.xpath("//TR[TD[6]='#this']/TD[10]")[0].text, |
| 1203 |
'#spectrum') |
| 1204 |
|
| 1205 |
|
| 1206 |
class SDMDatalinkTest(testhelpers.VerboseTest): |
| 1207 |
resources = [("ssaTable", tresc.ssaTestTable)] |
| 1208 |
|
| 1209 |
def runService(self, params, render="dlget"): |
| 1210 |
return trialhelpers.runSvcWith( |
| 1211 |
api.resolveCrossId("data/ssatest#dl"), render, params) |
| 1212 |
|
| 1213 |
def testRejectWithoutPUBDID(self): |
| 1214 |
self.assertRaisesWithMsg(api.ValidationError, |
| 1215 |
"Field ID: ID is mandatory with dlget", |
| 1216 |
self.runService, |
| 1217 |
({},)) |
| 1218 |
|
| 1219 |
def testVOTDelivery(self): |
| 1220 |
res = self.runService( |
| 1221 |
{"ID": 'ivo://test.inv/test1', "FORMAT": "application/x-votable+xml"}) |
| 1222 |
mime, payload = res |
| 1223 |
self.assertEqual(mime, "application/x-votable+xml") |
| 1224 |
self.assertTrue(b'xmlns:spec="http://www.ivoa.net/xml/SpectrumModel/v1.01' |
| 1225 |
in payload) |
| 1226 |
self.assertTrue(b'utype="spec:Spectrum">' in payload) |
| 1227 |
self.assertTrue(b'QJtoAAAAAABAm2g' in payload) |
| 1228 |
|
| 1229 |
def testTextDelivery(self): |
| 1230 |
res = self.runService( |
| 1231 |
{"ID": 'ivo://test.inv/test1', |
| 1232 |
"FORMAT": "text/plain"}) |
| 1233 |
mime, payload = res |
| 1234 |
self.assertTrue(isinstance(payload, bytes)) |
| 1235 |
self.assertTrue(b"1754.0\t1754.0\n1755.0\t1753.0\n" |
| 1236 |
b"1756.0\t1752.0" in payload) |
| 1237 |
|
| 1238 |
def testCutoutFull(self): |
| 1239 |
res = self.runService( |
| 1240 |
{"ID": ['ivo://test.inv/test1'], |
| 1241 |
"FORMAT": ["text/plain"], |
| 1242 |
"BAND": ["1.762e-7 1.764e-7"]}) |
| 1243 |
mime, payload = res |
| 1244 |
self.assertEqual(payload, |
| 1245 |
b'1762.0\t1746.0\n1763.0\t1745.0\n') |
| 1246 |
self.assertFalse(b'<TR>' in payload) |
| 1247 |
|
| 1248 |
def testCutoutHalfopen(self): |
| 1249 |
res = self.runService( |
| 1250 |
{"ID": ['ivo://test.inv/test1'], |
| 1251 |
"FORMAT": ["application/x-votable+xml;serialization=tabledata"], |
| 1252 |
"BAND": ["1.927e-7 +Inf"]}) |
| 1253 |
mime, payload = res |
| 1254 |
self.assertTrue(b'xmlns:spec="http://www.ivoa.net/xml/SpectrumModel/v1.01' |
| 1255 |
in payload) |
| 1256 |
self.assertTrue(b'<TR><TD>1927.0</TD><TD>1581.0</TD>' in payload) |
| 1257 |
self.assertFalse(b'<TR><TD>1756.0</TD>' in payload) |
| 1258 |
tree = testhelpers.getXMLTree(payload, debug=False) |
| 1259 |
self.assertEqual(tree.xpath("//PARAM[@utype=" |
| 1260 |
"'spec:Spectrum.Char.SpectralAxis.Coverage.Bounds.Start']" |
| 1261 |
)[0].get("value"), "1.927e-07") |
| 1262 |
self.assertAlmostEqual(float(tree.xpath("//PARAM[@utype=" |
| 1263 |
"'spec:Spectrum.Char.SpectralAxis.Coverage.Bounds.Extent']" |
| 1264 |
)[0].get("value")), 1e-10) |
| 1265 |
|
| 1266 |
def testNoSwappingLimits(self): |
| 1267 |
self.assertRaisesWithMsg(soda.EmptyData, |
| 1268 |
"", |
| 1269 |
self.runService, |
| 1270 |
({"ID": 'ivo://test.inv/test1', |
| 1271 |
"FORMAT": "application/x-votable+xml", |
| 1272 |
"BAND": ["1.764e-7 1.762e-7"]},)) |
| 1273 |
|
| 1274 |
def testEmptyCutoutFails(self): |
| 1275 |
self.assertRaisesWithMsg(api.ValidationError, |
| 1276 |
"Field BAND: [-inf, 1.927e-08] has no overlaps with BAND's range [1e-07, 5e-07]", |
| 1277 |
self.runService, |
| 1278 |
({"ID": 'ivo://test.inv/test1', |
| 1279 |
"FORMAT": "application/x-votable+xml", |
| 1280 |
"BAND": "-Inf 1.927e-8"},)) |
| 1281 |
|
| 1282 |
def testBlankInIntervalIgnored(self): |
| 1283 |
res = self.runService( |
| 1284 |
{"ID": ['ivo://test.inv/test1'], |
| 1285 |
"FORMAT": ["text/plain"], |
| 1286 |
"BAND": " "}) |
| 1287 |
mime, payload = res |
| 1288 |
self.assertEqual(len(payload), 2450) |
| 1289 |
|
| 1290 |
def testIncompleteArgumentRejected(self): |
| 1291 |
self.assertRaisesWithMsg( |
| 1292 |
api.ValidationError, |
| 1293 |
"Field BAND: Bad parameter literal ['23 '] (Invalid literal for" |
| 1294 |
" double[2] (field anonymous): '<1 token(s)>')", |
| 1295 |
self.runService, ({ |
| 1296 |
"ID": ['ivo://test.inv/test1'], |
| 1297 |
"responseFORMAT": ["text/plain"], |
| 1298 |
"BAND": ["23 "]},)) |
| 1299 |
|
| 1300 |
def testOriginalCalibOk(self): |
| 1301 |
mime, payload = self.runService( |
| 1302 |
{"ID": 'ivo://test.inv/test1', |
| 1303 |
"FORMAT": "text/plain", |
| 1304 |
"FLUXCALIB": "UNCALIBRATED"}) |
| 1305 |
self.assertTrue(payload.endswith(b"1928.0 1580.0\n")) |
| 1306 |
|
| 1307 |
def testNormalize(self): |
| 1308 |
mime, payload = trialhelpers.runSvcWith( |
| 1309 |
api.resolveCrossId("data/ssatest#dl"), |
| 1310 |
"ssap.xml", |
| 1311 |
{"ID": ['ivo://test.inv/test1'], |
| 1312 |
"FORMAT": "application/x-votable+xml;serialization=tabledata", |
| 1313 |
"BAND": ["1.9e-7 1.92e-7"], |
| 1314 |
"FLUXCALIB": "RELATIVE"}) |
| 1315 |
self.assertTrue(b"<TD>1900.0</TD><TD>0.91676" in payload) |
| 1316 |
tree = testhelpers.getXMLTree(payload, debug=False) |
| 1317 |
self.assertEqual(tree.xpath( |
| 1318 |
"//PARAM[@utype='spec:Spectrum.Char.FluxAxis.Calibration']")[0].get( |
| 1319 |
"value"), |
| 1320 |
"RELATIVE") |
| 1321 |
|
| 1322 |
def testBadCalib(self): |
| 1323 |
self.assertRaisesWithMsg(api.ValidationError, |
| 1324 |
"Field FLUXCALIB: Value 'ferpotschket' is not consistent with legal values (RELATIVE, UNCALIBRATED)", |
| 1325 |
self.runService, |
| 1326 |
({"ID": 'ivo://test.inv/test1', |
| 1327 |
"RESPONSEFORMAT": "text/plain", |
| 1328 |
"FLUXCALIB": ["ferpotschket"]}, |
| 1329 |
"dlget")) |
| 1330 |
|
| 1331 |
def testBadPubDID(self): |
| 1332 |
self.assertRaisesWithMsg(svcs.UnknownURI, |
| 1333 |
"No spectrum with this pubDID known here (pubDID: ivo://test.inv/bad)", |
| 1334 |
self.runService, |
| 1335 |
({"ID": 'ivo://test.inv/bad'},)) |
| 1336 |
|
| 1337 |
def testRandomParamFails(self): |
| 1338 |
self.assertRaisesWithMsg(api.ValidationError, |
| 1339 |
"Field warp: The following parameter(s) are" |
| 1340 |
" not accepted by this service: warp", |
| 1341 |
self.runService, |
| 1342 |
({"ID": 'ivo://test.inv/test1', |
| 1343 |
"warp": "infinity"},)) |
| 1344 |
|
| 1345 |
def testCoreForgetting(self): |
| 1346 |
gns = testhelpers.getMemDiffer() |
| 1347 |
self.runService( |
| 1348 |
{"ID": ['ivo://test.inv/test1'], |
| 1349 |
"FORMAT": ["text/plain"], "BAND": ["1.762e-7 1.764e-7"]}) |
| 1350 |
gc.collect() |
| 1351 |
ns = gns() |
| 1352 |
self.assertEqual(ns, [], "Spectrum cutout left garbage.") |
| 1353 |
|
| 1354 |
|
| 1355 |
class _RenderedSDMTable(testhelpers.TestResource): |
| 1356 |
resources = [("ssaTable", tresc.ssaTestTable)] |
| 1357 |
|
| 1358 |
def make(self, ignored): |
| 1359 |
doc = trialhelpers.runSvcWith( |
| 1360 |
api.resolveCrossId("data/ssatest#dl"), "dlget", |
| 1361 |
{"ID": 'ivo://test.inv/test1', "FORMAT": "application/x-votable+xml"})[1] |
| 1362 |
return testhelpers.getXMLTree(doc, debug=False) |
| 1363 |
|
| 1364 |
|
| 1365 |
class SDMResultTest(testhelpers.VerboseTest): |
| 1366 |
resources = [("tree", _RenderedSDMTable())] |
| 1367 |
|
| 1368 |
def testDataOrigin(self): |
| 1369 |
self.assertEqual( |
| 1370 |
self.tree.xpath("//INFO[@name='citation']")[0].get("value"), |
| 1371 |
"http://localhost:8080/data/ssatest/dl/howtocite") |
| 1372 |
|
| 1373 |
def testParamValue(self): |
| 1374 |
self.assertEqual( |
| 1375 |
self.tree.xpath("//PARAM[@name='ssa_location']")[0].get("value"), |
| 1376 |
"10.100000002783013 15.199999999820202") |
| 1377 |
|
| 1378 |
def testFieldUCD(self): |
| 1379 |
self.assertEqual( |
| 1380 |
self.tree.xpath("//FIELD[@ID='flux']")[0].get("ucd"), |
| 1381 |
"phot.flux.density;em.wl") |
| 1382 |
|
| 1383 |
def testSpecGroup(self): |
| 1384 |
self.assertEqual( |
| 1385 |
self.tree.xpath("//GROUP[@utype='spec:spectrum.data']" |
| 1386 |
"/FIELDref[@utype='spec:spectrum.data.spectralaxis.value']" |
| 1387 |
)[0].get("ref"), |
| 1388 |
"spectral") |
| 1389 |
|
| 1390 |
def testData(self): |
| 1391 |
self.assertTrue("QJtoAAAA" |
| 1392 |
in self.tree.xpath("//STREAM")[0].text) |
| 1393 |
|
| 1394 |
class SDMAccrefDatalinkTest(testhelpers.VerboseTest): |
| 1395 |
resources = [("ssaTable", tresc.ssaTestTable)] |
| 1396 |
|
| 1397 |
def runService(self, params): |
| 1398 |
return trialhelpers.runSvcWith(api.resolveCrossId("data/ssatest#dlaccref"), "dlget", params) |
| 1399 |
|
| 1400 |
def testBasic(self): |
| 1401 |
res = self.runService( |
| 1402 |
{"ID": 'data/spec1.ssatest', |
| 1403 |
"FORMAT": "text/plain"}) |
| 1404 |
mime, payload = res |
| 1405 |
self.assertTrue(isinstance(payload, bytes)) |
| 1406 |
self.assertTrue(b"1754.0\t1754.0\n1755.0\t1753.0\n" |
| 1407 |
b"1756.0\t1752.0" in payload) |
| 1408 |
|
| 1409 |
|
| 1410 |
def maketruth(testclass): |
| 1411 |
"""writes a VOTable representative of the test class. |
| 1412 |
|
| 1413 |
This is because there's nothing like visualisation for getting |
| 1414 |
an idea whether these spherical geometry things actually make sense. |
| 1415 |
|
| 1416 |
This writes 000truth.vot and 000truth.txt, the latter for the class' truth |
| 1417 |
attribute. Open truth.vot in topcat to see what's happening. |
| 1418 |
|
| 1419 |
testclass needs to have a gettruth method for this to work. It has |
| 1420 |
to return, for each test, |
| 1421 |
""" |
| 1422 |
from gavo.votable import V |
| 1423 |
|
| 1424 |
tests = testclass("test00") |
| 1425 |
txt, rows = [], [] |
| 1426 |
|
| 1427 |
for sample in _iterWCSSamples(): |
| 1428 |
truthVal, truthPoints = tests.getTruthFor(sample) |
| 1429 |
txt.append(truthVal) |
| 1430 |
rows.extend(truthPoints) |
| 1431 |
|
| 1432 |
vot = V.VOTABLE[V.RESOURCE[ |
| 1433 |
votable.DelayedTable( |
| 1434 |
V.TABLE[ |
| 1435 |
V.FIELD(name="ptype", datatype="char", arraysize="*"), |
| 1436 |
V.FIELD(name="ra", datatype="double", unit="deg"), |
| 1437 |
V.FIELD(name="dec", datatype="double", unit="deg")], |
| 1438 |
rows, V.BINARY)]] |
| 1439 |
with open("000truth.vot", "w") as f: |
| 1440 |
votable.write(vot, f) |
| 1441 |
|
| 1442 |
with open("000truth.txt", "w") as f: |
| 1443 |
f.write(" %s\n"%(",\n ".join(txt))) |
| 1444 |
|
| 1445 |
|
| 1446 |
if __name__=="__main__": |
| 1447 |
# maketruth(SODA_CIRCLEMetaTest) |
| 1448 |
testhelpers.main(SDMDatalinkTest) |