def exportData( dataTuple ,name): dataK, labelK, data_errorK, dataL, labelL = dataTuple # concatenate all arrays from dataTuple and add labels for errors data_full = np.concatenate( (np.concatenate( ( dataK, data_errorK ) , axis = 1 ), dataL), axis = 1 ) error_label = [] for i in labelK: error_label += [i + "_error"] label_full = labelK + error_label + labelL votable = VOTableFile() resource = Resource() votable.resources.append(resource) table = Table(votable) resource.tables.append(table) fields = [] for i in range( data_full.shape[1] ): fields += [Field( votable, name = label_full[i], datatype='float' )] table.fields.extend( fields ) table.create_arrays( data_full.shape[0] ) for i in range( data_full.shape[0] ): table.array[i] = tuple( data_full[i,:] ) votable.to_xml(name +".xml")
def test_exec_sync(): # save results in a file # create the VOTable result # example from http://docs.astropy.org/en/stable/io/votable/ votable = VOTableFile() resource = Resource() votable.resources.append(resource) table = Table(votable) resource.tables.append(table) table.fields.extend([ Field(votable, name="filename", datatype="char", arraysize="*"), Field(votable, name="matrix", datatype="double", arraysize="2x2")]) table.create_arrays(2) table.array[0] = ('test1.xml', [[1, 0], [0, 1]]) table.array[1] = ('test2.xml', [[0.5, 0.3], [0.2, 0.1]]) buffer = BytesIO() votable.to_xml(buffer) cadc = Cadc(auth_session=requests.Session()) response = Mock() response.to_table.return_value = buffer.getvalue() cadc.cadctap.search = Mock(return_value=response) output_file = '{}/test_vooutput.xml'.format(tempfile.tempdir) cadc.exec_sync('some query', output_file=output_file) actual = parse(output_file) assert len(votable.resources) == len(actual.resources) == 1 assert len(votable.resources[0].tables) ==\ len(actual.resources[0].tables) == 1 actual_table = actual.resources[0].tables[0] try: # TODO remove when astropy LTS upgraded from astropy.utils.diff import report_diff_values assert report_diff_values(table, actual_table, fileobj=sys.stdout) except ImportError: pass
def table_from_scratch(): from astropy.io.votable.tree import VOTableFile, Resource, Table, Field # Create a new VOTable file... votable = VOTableFile() # ...with one resource... resource = Resource() votable.resources.append(resource) # ... with one table table = Table(votable) resource.tables.append(table) # Define some fields table.fields.extend([ Field(votable, ID="filename", datatype="char"), Field(votable, ID="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 out = io.StringIO() votable.to_xml(out)
def createTableFromObject(data, path="", names=[], dtypes=[], sizes=[]): path_tmp = "/Users/cjimenez/Documents/PHD/data/tmp/" # Create a new VOTable file... votable = VOTableFile() # ...with one resource... resource = Resource() votable.resources.append(resource) # ... with one table table = Table(votable) resource.tables.append(table) # Define some fields fields = [] for idx, val in enumerate(names): fields.append(Field(votable, name=val, datatype=dtypes[idx])) table.fields.extend(fields) # Now, use those field definitions to create the numpy record arrays, with # the given number of rows table.create_arrays(len(data)) # Now table.array can be filled with data for idx, val in enumerate(data): table.array[idx] = val # Now write the whole thing to a file. # Note, we have to use the top-level votable file object votable.to_xml(path_tmp + path)
def write_set(self, filename, votype='ascii', overwrite=False): ''' Write all tables to a VOT file Required Arguments: *filename*: [ string ] The VOT file to write the tables to Optional Keyword Arguments: *votype*: [ 'ascii' | 'binary' ] Whether to write the tables as ASCII or binary tables ''' if os.path.exists(filename): if overwrite: os.remove(filename) else: raise Exception("File exists: %s" % filename) vo_table = VOTableFile() resource = Resource() vo_table.resources.append(resource) for table_key in self.tables: resource.tables.append(_to_table(self.tables[table_key], vo_table)) if votype is 'binary': vo_table.get_first_table().format = 'binary' vo_table.set_all_tables_format('binary') vo_table.to_xml(filename)
def votable_xml_string(version): votable_file = VOTableFile(version=version) votable_file.resources.append(Resource()) xml_bytes = io.BytesIO() votable_file.to_xml(xml_bytes) xml_bytes.seek(0) bstring = xml_bytes.read() s = bstring.decode("utf-8") return s
print np.array(table[sys.argv[2]])[i_r] print i_r from astropy.io.votable.tree import VOTableFile, Resource, Table, Field # Create a new VOTable file... votable = VOTableFile() # ...with one resource... resource = Resource() votable.resources.append(resource) # ... with one table table_out = Table(votable) resource.tables.append(table_out) table_out.fields.extend(fields) table_out.create_arrays(len(i_r)) for n in col_names: table_out.array[n] = table[n][i_r] votable.to_xml (sys.argv[4]) #table = vot.get_first_table().to_table(use_names_over_ids=True) #print table
f3 = Field(t, name='intensity_err', datatype=fits_pixel_type, ucd='stat.error;phot.flux.density;em.MIR', unit='erg.s-1.cm-1.sr-1') f3.description = 'error (standard deviation)' f4 = Field(t, name='transmission_reference', datatype=fits_pixel_type, ucd='phys.transmission;em.MIR', unit='') f4.description = 'reference modeled atmospheric transmission spectrum' # Add the fields to the table t.fields.extend([f1, f2, f3, f4]) # Allocate the space for the four columns in the table t.create_arrays(pixel_data.shape[1]) # Copy over each row of the table. Is there a better way? for i in range(pixel_data.shape[1]): values = tuple(pixel_data[:, i]) t.array[i] = values # Output the XML VOTable file with open(outputfile, mode) as f: vt.to_xml(f) sys.exit(0)
def save_table(table, tabname): results = VOTableFile() resource = Resource() results.resources.append(resource) resource.tables.append(from_table(table).get_first_table()) results.to_xml(tabname)
def sort_write(sortname, spect, fitsdict, filesort, space=3): """ Write out an xml and ascii file that contains the details of the file sorting. By default, the filename is printed first, followed by the filetype. After these, all parameters listed in the 'keyword' item in the settings file will be printed Parameters ---------- sortname : str The filename to be used to save the list of sorted files spect : dict Properties of the spectrograph. fitsdict : dict Contains relevant information from fits header files filesort : dict Details of the sorted files space : int Keyword to set how many blank spaces to place between keywords """ msgs.info("Preparing to write out the data sorting details") nfiles = fitsdict['filename'].size # Specify which keywords to print after 'filename' and 'filetype' prord = ['filename', 'frametype', 'target', 'exptime', 'naxis0', 'naxis1', 'filter1', 'filter2'] prdtp = ["char", "char", "char", "double", "int", "int", "char", "char"] # Now insert the remaining keywords: fkey = spect['keyword'].keys() for i in fkey: if i not in prord: prord.append(i) # Append the type of value this keyword holds typv = type(fitsdict[i][0]) if typv is int or typv is np.int_: prdtp.append("int") elif isinstance(fitsdict[i][0], basestring) or typv is np.string_: prdtp.append("char") elif typv is float or typv is np.float_: prdtp.append("double") else: msgs.bug("I didn't expect useful headers to contain type {0:s}".format(typv).replace('<type ', '').replace('>', '')) # Open a VOTable for writing votable = VOTableFile() resource = Resource() votable.resources.append(resource) table = Table(votable) resource.tables.append(table) # Define VOTable fields tabarr=[] # Insert the filename and filetype first for i in xrange(len(prord)): tabarr.append(Field(votable, name=prord[i], datatype=prdtp[i], arraysize="*")) table.fields.extend(tabarr) table.create_arrays(nfiles) filtyp = filesort.keys() for i in xrange(nfiles): values = () for pr in prord: if pr == 'frametype': addval = "" for ft in filtyp: if i in filesort[ft]: if len(addval) != 0: addval += "," addval += ft addval = (addval,) else: addval = (fitsdict[pr][i],) values = values + addval table.array[i] = values #osspl = sortname.split('.') #if len(osspl) > 1: # fname = sortname #else: fname = sortname+'.xml' votable.to_xml(fname) msgs.info("Successfully written sorted data information file:"+msgs.newline() + "{0:s}".format(fname)) # ASCII file (JXP) jxpord = ['filename', 'date', 'frametype', 'target', 'exptime', 'binning', 'dichroic', 'disperser', 'cdangle', 'decker'] # Generate the columns clms = [] for pr in jxpord: try: lidx = prord.index(pr) except ValueError: msgs.warn('{:s} keyword not used'.format(pr)) else: clm = [] for i in xrange(nfiles): clm.append(table.array[i][lidx]) clms.append(Column(clm, name=pr)) # Create Table jxp_tbl = tTable(clms) # Write jxp_name = fname.replace('.xml', '.lst') jxp_tbl.write(jxp_name, format='ascii.fixed_width') return
for i in firstdata, nvssdata, wenssdata, sumssdata: if len(i.rawcatdata) > 0: externalsurveys.append(i) #VO output.. if keepvo > 0: votable = VOTableFile() resource = Resource() votable.resources.append(resource) resource.tables.append( catalogue.makevo(votable, description=telescope + ' Data')) for surveydata in externalsurveys: resource.tables.append( surveydata.makevo(votable, description=surveydata.survey.name + ' Data')) votable.to_xml(options.outputdir + '/' + inname + '.vo') outfiles.append(options.outputdir + '/' + inname + '.vo') print '**** vo catalogue containg all available source lists is in ' + inname + '.vo' #Make the required plots for each survey. for surveydata in externalsurveys: outfiles = outfiles + surveydata.docomparison(options.outputdir + '/' + inname + '_') #Print out the best flux comparison (and attempt 2 frequency flux compare) outfiles = outfiles + fluxcompare(externalsurveys, options.outputdir + '/') #Output image stats outfiles = outfiles + [ catalogue.getimstats(options.outputdir + '/' + inname + '_STATS.txt', externalsurveys, [localdr, numsources])
c = SkyCoord(x[2] + 'h' + x[3] + 'm' + x[4] + 's', x[5] + x[6] + 'd' + x[7] + 'm' + x[8] + 's', 'icrs') island_id = "" ra_deg_cont = c.ra.deg dec_deg_cont = c.dec.degree freq = 1408 flux_peak_err = 0 flux_int_err = 0 maj_axis = maj_axis_deconv min_axis = min_axis_deconv pos_ang = pos_ang_deconv maj_axis_err = 0 min_axis_err = 0 pos_ang_err = 0 chi_squared_fit = -1 rms_fit_gauss = -1 spectral_index = 0 spectral_curvature = 0 flag_c1 = 0 flag_c2 = 0 flag_c3 = 0 flag_c4 = 0 z=island_id,component_id,component_name,ra_hms_cont,dec_dms_cont,ra_deg_cont,dec_deg_cont,ra_err,dec_err,freq,flux_peak, \ flux_peak_err,flux_int,flux_int_err,maj_axis,min_axis,pos_ang,maj_axis_err,min_axis_err,pos_ang_err, \ maj_axis_deconv,min_axis_deconv,pos_ang_deconv,chi_squared_fit,rms_fit_gauss,spectral_index,spectral_curvature, \ rms_image,flag_c1,flag_c2,flag_c3,flag_c4,comment table.array[r] = z votable.to_xml(XMLFilename)
def visibility(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now # Create a new VOTable file... votable = VOTableFile() # ...with one resource... resource = Resource() votable.resources.append(resource) # ... with one table table = Table(votable) resource.tables.append(table) resource.description ="European Space Astronomy Centre. INTEGRAL SOC - " \ "Object Visibility Simple Access Protocol (ObjVisSAP)" resource.infos.append(Info(name="QUERY_STATUS", value="OK")) resource.infos.append(Info(name="SERVICE PROTOCOL", value="1.0")) resource.infos.append(Info(name="REQUEST", value="queryData")) resource.infos.append( Info(name="s_ra", value="%s" % request.GET.get("s_ra"))) resource.infos.append( Info(name="s_dec", value="%s" % request.GET.get("s_dec"))) resource.infos.append( Info(name="t_min", value="%s" % request.GET.get("t_min"))) resource.infos.append( Info(name="t_max", value="%s" % request.GET.get("t_max"))) # Define some fields # table.fields.extend([ # Field(votable, name="filename", datatype="char", arraysize="*"), # Field(votable, name="matrix", datatype="double", arraysize="2x2")]) table.fields.extend([ Field(votable, name="t_start", datatype="double", ucd="time.start", utype="Char.TimeAxis.Coverage.Bounds.Limits.StartTime"), Field(votable, name="t_stop", datatype="double", ucd="time.start", utype="Char.TimeAxis.Coverage.Bounds.Limits.StartTime"), Field(votable, name="t_visibility", datatype="double", ucd="time.start", utype="Char.TimeAxis.Coverage.Bounds.Limits.StartTime"), ]) results = VisibilityCalculator.getVisibilityIntervals( request.GET.get("s_ra"), request.GET.get("s_dec"), request.GET.get("t_min"), request.GET.get("t_max")) number_of_intervals = len(results) table.create_arrays(number_of_intervals) for i in range(0, number_of_intervals): table.array[i] = (results[i][0], results[i][1], results[i][2]) # Now write the whole thing to a file to be streamed # Note, we have to use the top-level votable file object xml_now = "/tmp/new_votable_%s.xml" % now votable.to_xml(xml_now) stream = open(xml_now).read() os.remove(xml_now) return HttpResponse(stream, content_type='text/xml')