def test_vararray(): votable = tree.VOTableFile() resource = tree.Resource() votable.resources.append(resource) table = tree.Table(votable) resource.tables.append(table) tabarr = [] heads = ['headA', 'headB', 'headC'] types = ["char", "double", "int"] vals = [["A", 1.0, 2], ["B", 2.0, 3], ["C", 3.0, 4]] for i in range(len(heads)): tabarr.append( tree.Field(votable, name=heads[i], datatype=types[i], arraysize="*")) table.fields.extend(tabarr) table.create_arrays(len(vals)) for i in range(len(vals)): values = tuple(vals[i]) table.array[i] = values buff = io.BytesIO() votable.to_xml(buff)
def test_make_Fields(): votable = tree.VOTableFile() # ...with one resource... resource = tree.Resource() votable.resources.append(resource) # ... with one table table = tree.Table(votable) resource.tables.append(table) table.fields.extend( [tree.Field(votable, name='Test', datatype="float", unit="mag")])
def _votableparse(source, columns=None, invalid='mask', pedantic=False, table_number=None, filename=None, version="1.1"): try: import astropy.io.votable.tree as votabletree import astropy.io.votable.table as votabletable from astropy.utils.xml import iterparser #from astropy.io.votable.exceptions import W22 from astropy.io.votable.exceptions import W03, W06, W20, W21, W42, W46, W47, W49, E10 for warning in (W03, W06, W20, W21, W42, W46, W47, W49, E10): warnings.simplefilter("ignore", warning) # MJG : 021913 - commented out to get CDS responses to work # warnings.simplefilter("error", W22) except ImportError: raise RuntimeError("astropy votable not available") invalid = invalid.lower() assert invalid in ('exception', 'mask') chunk_size = votabletree.DEFAULT_CHUNK_SIZE if pedantic is None: pedantic = votabletable.PEDANTIC() config = { 'columns': columns, 'invalid': invalid, 'pedantic': pedantic, 'chunk_size': chunk_size, 'table_number': table_number, 'filename': filename, 'version_1_1_or_later': True } if filename is None and isinstance(source, basestring): config['filename'] = source if filename is None: config['filename'] = 'dal_query' with iterparser.get_xml_iterator(source) as iterator: return votabletree.VOTableFile(config=config, pos=(1, 1), version=version).parse( iterator, config)
def test_build_from_scratch(tmpdir): # Create a new VOTable file... votable = tree.VOTableFile() # ...with one resource... resource = tree.Resource() votable.resources.append(resource) # ... with one table table = tree.Table(votable) resource.tables.append(table) # Define some fields table.fields.extend([ tree.Field(votable, ID="filename", name='filename', datatype="char", arraysize='1'), tree.Field(votable, ID="matrix", name='matrix', datatype="double", arraysize="2x2") ]) # Now, use those field definitions to create the numpy record arrays, with # the given number of rows table.create_arrays(2) # Now table.array can be filled with data table.array[0] = ('test1.xml', [[1, 0], [0, 1]]) table.array[1] = ('test2.xml', [[0.5, 0.3], [0.2, 0.1]]) # Now write the whole thing to a file. # Note, we have to use the top-level votable file object votable.to_xml(str(tmpdir.join("new_votable.xml"))) votable = parse(str(tmpdir.join("new_votable.xml"))) table = votable.get_first_table() assert_array_equal( table.array.mask, np.array([(False, [[False, False], [False, False]]), (False, [[False, False], [False, False]])], dtype=[('filename', '?'), ('matrix', '?', (2, 2))]))
def test_resource_structure(): # Based on issue #1223, as reported by @astro-friedel and @RayPlante from astropy.io.votable import tree as vot vtf = vot.VOTableFile() r1 = vot.Resource() vtf.resources.append(r1) t1 = vot.Table(vtf) t1.name = "t1" t2 = vot.Table(vtf) t2.name = 't2' r1.tables.append(t1) r1.tables.append(t2) r2 = vot.Resource() vtf.resources.append(r2) t3 = vot.Table(vtf) t3.name = "t3" t4 = vot.Table(vtf) t4.name = "t4" r2.tables.append(t3) r2.tables.append(t4) r3 = vot.Resource() vtf.resources.append(r3) t5 = vot.Table(vtf) t5.name = "t5" t6 = vot.Table(vtf) t6.name = "t6" r3.tables.append(t5) r3.tables.append(t6) buff = io.BytesIO() vtf.to_xml(buff) buff.seek(0) vtf2 = parse(buff) assert len(vtf2.resources) == 3 for r in range(len(vtf2.resources)): res = vtf2.resources[r] assert len(res.tables) == 2 assert len(res.resources) == 0
import astropy.io.votable.tree as vo from scipy import misc import numpy as np import glob from astropy.io.votable import parse nm = glob.glob('./*.png')[0] imArr = misc.imread(nm).astype(np.int32) r, c = imArr.shape # print imArr.dtype votable = vo.VOTableFile() resource = vo.Resource() votable.resources.append(resource) # appending resource into votable table = vo.Table(votable) resource.tables.append(table) table.description = 'Lette A from non-MNIST set' group = vo.Group(table) table.groups.append(group) para = vo.Param(votable, name='label', datatype='char', arraysize='1', value='A') group.entries.append(para) # table.fields.extend([ \