コード例 #1
0
def output_emission_spectra(filename, longitude, latitude, velocity, em_mean,
                            em_std, ems):
    """
    Write the emission spectrum (velocity, flux and opacity) to a votable format
    file.

    :param filename:  The filename to be created
    :param longitude: The galactic longitude of the target object
    :param latitude: The galactic latitude of the target object
    :param velocity:
    :param em_mean:
    :param em_std:
    :param ems:
    """
    table = Table(meta={'name': filename, 'id': 'emission'})
    table.add_column(
        Column(name='velocity',
               data=velocity,
               unit='m/s',
               description='velocity relative to LSRK'))
    table.add_column(Column(name='em_mean', data=em_mean, unit='K'))
    table.add_column(Column(name='em_std', data=em_std, unit='K'))
    for i in range(len(ems)):
        table.add_column(
            Column(name='em_' + str(i), data=ems[i].flux, unit='K'))

    votable = from_table(table)
    votable.infos.append(Info('ra', 'longitude', longitude.value))
    votable.infos.append(Info('dec', 'latitude', latitude.value))
    writeto(votable, filename)
コード例 #2
0
ファイル: make_testdata.py プロジェクト: eteq/pyvo
def votablefile_errorstatus():
    votablefile = _votablefile()

    info = Info(name='QUERY_STATUS', value='ERROR')
    info.content = 'ERROR'
    votablefile.resources[0].infos[0] = info

    return votablefile
コード例 #3
0
def output_spectra(spectrum, opacity, filename, longitude, latitude, em_mean,
                   em_std, temp_bright, beam_area, sigma_tau, sigma_tau_smooth,
                   mean):
    """
    Write the spectrum (velocity, flux and opacity) to a votable format file.

    :param spectrum: The spectrum to be output.
    :param opacity:  The opacity to be output.
    :param filename:  The filename to be created
    :param longitude: The galactic longitude of the target object
    :param latitude: The galactic latitude of the target object
    """
    table = Table(meta={'name': filename, 'id': 'opacity'})
    table.add_column(Column(name='plane', data=spectrum.plane))
    table.add_column(
        Column(name='velocity',
               data=spectrum.velocity,
               unit='m/s',
               description='velocity relative to LSRK'))
    table.add_column(Column(name='opacity', data=opacity))

    #following smooth should be using same parameter with line 950
    hann_window = np.hanning(31)
    hann_kernel = CustomKernel(hann_window)
    smooth_y = convolve(opacity, hann_kernel, boundary='extend')
    table.add_column(
        Column(name='smooth_opacity',
               data=smooth_y,
               description='opacity smooth with hanning window 31 channels.'))

    table.add_column(
        Column(name='flux',
               data=spectrum.flux,
               unit='Jy',
               description='Flux per beam'))
    table.add_column(Column(name='temp_brightness', data=temp_bright,
                            unit='K'))
    table.add_column(
        Column(
            name='sigma_tau',
            data=sigma_tau,
            description='Noise in the absorption profile, before smoothing'))
    table.add_column(
        Column(name='sigma_tau_smooth',
               data=sigma_tau_smooth,
               description='Noise in the absorption profile, after smoothing'))
    if len(em_mean) > 0:
        # The emission may not be available, so don't include it if not
        table.add_column(Column(name='em_mean', data=em_mean, unit='K'))
        table.add_column(Column(name='em_std', data=em_std, unit='K'))

    votable = from_table(table)
    votable.infos.append(Info('ra', 'longitude', longitude.value))
    votable.infos.append(Info('dec', 'latitude', latitude.value))
    votable.infos.append(Info('beam_area', 'beam_area', beam_area))
    votable.infos.append(Info('cont', 'continuum', mean))
    writeto(votable, filename)
コード例 #4
0
ファイル: ssa.py プロジェクト: lwymarie/specdb
def build_metaquery(def_infos):
    """ Generate a VOTable for a METADATA query
    Parameters
    ----------
    def_infos

    Returns
    -------
    votable : VOTable

    """
    # Begin
    votable = empty_vo(rtype='results')
    resource = votable.resources[0]
    # Info
    sinfo = Info(name='QUERY_STATUS', value="OK", content="Successful search")
    resource.infos.append(sinfo)
    for dinfo in def_infos:
        resource.infos.append(dinfo)
    # Input Params
    iparams = input_params(votable)
    for iparam in iparams:
        resource.params.append(iparam)
    # Output Param
    output_params, _ = metaquery_param(votable)
    for oparam in output_params:
        resource.params.append(oparam)
    # Return
    return votable
コード例 #5
0
ファイル: make_testdata.py プロジェクト: eteq/pyvo
def _votablefile():
    table = Table(
        [[23, 42, 1337],
         [b'Illuminatus', b"Don't panic, and always carry a towel", b'Elite']],
        names=('1', '2'))

    table['1'].meta['ucd'] = 'foo;bar'

    table['2'].meta['utype'] = 'foobar'

    votablefile = VOTableFile.from_table(table)

    info = Info(name='QUERY_STATUS', value='OK')
    info.content = 'OK'
    votablefile.resources[0].infos.append(info)

    return votablefile
コード例 #6
0
def output_spectra(spectrum, opacity, filename, longitude, latitude, em_mean,
                   em_std, temp_bright, beam_area, sigma_tau, src_id):
    """
    Write the spectrum (velocity, flux and opacity) to a votable format file.

    :param spectrum: The spectrum to be output.
    :param opacity:  The opacity to be output.
    :param filename:  The filename to be created
    :param longitude: The galactic longitude of the target object
    :param latitude: The galactic latitude of the target object
    """
    table = Table(meta={'name': filename, 'id': 'opacity'})
    table.add_column(Column(name='plane', data=spectrum.plane))
    table.add_column(
        Column(name='velocity', data=spectrum.velocity, unit='m/s'))
    table.add_column(Column(name='opacity', data=opacity))
    table.add_column(
        Column(name='flux',
               data=spectrum.flux,
               unit='Jy',
               description='Flux per beam'))
    table.add_column(Column(name='temp_brightness', data=temp_bright,
                            unit='K'))
    table.add_column(
        Column(name='sigma_tau',
               data=sigma_tau,
               description='Noise in the absorption profile'))
    if len(em_mean) > 0:
        # The emission may not be available, so don't include it if not
        table.add_column(Column(name='em_mean', data=em_mean, unit='K'))
        table.add_column(Column(name='em_std', data=em_std, unit='K'))

    votable = from_table(table)
    votable.infos.append(Info('longitude', 'longitude', longitude.value))
    votable.infos.append(Info('latitude', 'latitude', latitude.value))
    votable.infos.append(Info('beam_area', 'beam_area', beam_area))
    votable.infos.append(Info('src_gname', 'gname', src_id))
    writeto(votable, filename)
コード例 #7
0
ファイル: make_testdata.py プロジェクト: eteq/pyvo
def votablefile_dataset():
    table = Table([[
        'image/fits', 'application/x-votable+xml',
        'application/x-votable+xml;content=datalink'
    ],
                   [
                       b'http://example.com/querydata/image.fits',
                       b'http://example.com/querydata/votable.xml',
                       b'http://example.com/querydata/votable-datalink.xml'
                   ]],
                  names=('dataformat', 'dataurl'))

    table['dataformat'].meta['ucd'] = 'meta.code.mime'
    table['dataurl'].meta['utype'] = 'Access.Reference'
    table['dataurl'].meta['ucd'] = 'meta.dataset;meta.ref.url'

    votablefile = VOTableFile.from_table(table)

    info = Info(name='QUERY_STATUS', value='OK')
    info.content = 'OK'
    votablefile.resources[0].infos.append(info)

    return votablefile
コード例 #8
0
def createFootprintsTable(catalog, xy0=None, insertColumn=4):
    """make a VOTable of SourceData table and footprints

    Parameters:
    -----------
    catalog : `lsst.afw.table.SourceCatalog`
            Source catalog from which to display footprints.
    xy0 : tuple or list or None
        Pixel origin to subtract off from the footprint coordinates.
        If None, the value used is (0,0)
    insertColumn : `int`
        Column at which to insert the "family_id" and "category" columns

    Returns:
    --------
    `astropy.io.votable.voTableFile`
        VOTable object to upload to Firefly
    """
    if xy0 is None:
        xy0 = afwGeom.Point2I(0, 0)

    _catalog = afwTable.SourceCatalog(catalog.table.clone())
    _catalog.extend(catalog, deep=True)
    sourceTable = _catalog.asAstropy()

    # Change int64 dtypes so they convert to VOTable
    for colName in sourceTable.colnames:
        if sourceTable[colName].dtype.num == 9:
            sourceTable[colName].dtype = np.dtype('long')

    inputColumnNames = sourceTable.colnames

    x0, y0 = xy0
    spanList = []
    peakList = []
    familyList = []
    categoryList = []
    fpxll = []
    fpyll = []
    fpxur = []
    fpyur = []
    for record in catalog:
        footprint = record.getFootprint()
        recordId = record.getId()
        spans = footprint.getSpans()
        scoords = [(s.getY()-y0, s.getX0()-x0, s.getX1()-x0) for s in spans]
        scoords = np.array(scoords).flatten()
        scoords = np.ma.MaskedArray(scoords, mask=np.zeros(len(scoords),
                                                           dtype=np.bool))
        fpbbox = footprint.getBBox()
        corners = [(c.getX()-x0, c.getY()-y0) for c in fpbbox.getCorners()]
        fpxll.append(corners[0][0])
        fpyll.append(corners[0][1])
        fpxur.append(corners[2][0])
        fpyur.append(corners[2][1])
        peaks = footprint.getPeaks()
        pcoords = [(p.getFx()-x0, p.getFy()-y0) for p in peaks]
        pcoords = np.array(pcoords).flatten()
        pcoords = np.ma.MaskedArray(pcoords, mask=np.zeros(len(pcoords),
                                                           dtype=np.bool))
        fpbbox = footprint.getBBox()
        parentId = record.getParent()
        nChild = record.get('deblend_nChild')
        if parentId == 0:
            familyList.append(recordId)
            if nChild > 0:
                # blended parent
                categoryList.append('blended parent')
            else:
                # isolated
                categoryList.append('isolated')
        else:
            # deblended child
            familyList.append(parentId)
            categoryList.append('deblended child')
        spanList.append(scoords)
        peakList.append(pcoords)

    sourceTable.add_column(Column(np.array(familyList)),
                           name='family_id',
                           index=insertColumn)
    sourceTable.add_column(Column(np.array(categoryList)),
                           name='category',
                           index=insertColumn+1)
    sourceTable.add_column(Column(np.array(spanList)), name='spans')
    sourceTable.add_column(Column(np.array(peakList)), name='peaks')
    sourceTable.add_column(Column(np.array(fpxll)), name='footprint_corner1_x')
    sourceTable.add_column(Column(np.array(fpyll)), name='footprint_corner1_y')
    sourceTable.add_column(Column(np.array(fpxur)), name='footprint_corner2_x')
    sourceTable.add_column(Column(np.array(fpyur)), name='footprint_corner2_y')

    outputVO = from_table(sourceTable)
    outTable = outputVO.get_first_table()

    outTable.infos.append(Info(name='contains_lsst_footprints', value='true'))
    outTable.infos.append(Info(name='contains_lsst_measurements', value='true'))
    outTable.infos.append(Info(name='FootPrintColumnNames',
                               value='id;footprint_corner1_x;footprint_corner1_y;' +
                               'footprint_corner2_x;footprint_corner2_y;spans;peaks'))
    outTable.infos.append(Info(name='pixelsys', value='zero-based'))
    # Check whether the coordinates are included and are valid
    if (('slot_Centroid_x' in inputColumnNames) and
            ('slot_Centroid_y' in inputColumnNames) and
            np.isfinite(outTable.array['slot_Centroid_x']).any() and
            np.isfinite(outTable.array['slot_Centroid_y']).any()):
        coord_column_string = 'slot_Centroid_x;slot_Centroid_y;ZERO_BASED'
    elif (('coord_ra' in inputColumnNames) and
            ('coord_dec' in inputColumnNames) and
            np.isfinite(outTable.array['coord_ra']).any() and
            np.isfinite(outTable.array['coord_dec']).any()):
        coord_column_string = 'coord_ra;coord_dec;EQ_J2000'
    elif (('base_SdssCentroid_x' in inputColumnNames) and
            ('base_SdssCentroid_y' in inputColumnNames) and
            np.isfinite(outTable.array['base_SdssCentroid_x']).any() and
            np.isfinite(outTable.array['base_SdssCentroid_y']).any()):
        coord_column_string = 'base_SdssCentroid_x;base_SdssCentroid_y;ZERO_BASED'
    elif (('base_NaiveCentroid_x' in inputColumnNames) and
            ('base_NaiveCentroid_y' in inputColumnNames) and
            np.isfinite(outTable.array['base_NaiveCentroid_x']).any() and
            np.isfinite(outTable.array['base_NaiveCentroid_y']).any()):
        coord_column_string = 'base_NaiveCentroid_x;base_NaiveCentroid_y;ZERO-BASED'
    else:
        raise RuntimeError('No valid coordinate columns in catalog')
    outTable.infos.append(Info(name='CatalogCoordColumns',
                               value=coord_column_string))

    for f in outTable.fields:
        if f.datatype == 'bit':
            f.datatype = 'boolean'

    outTable._config['version_1_3_or_later'] = True
    outputVO.set_all_tables_format('binary2')

    return(outputVO)
コード例 #9
0
ファイル: ssa.py プロジェクト: lwymarie/specdb
    def querydata(self, POS=None, SIZE=None, TIME=None, BAND=None, FORMAT='HDF5',
                  TOP=None, MAXREC=5000, TARGETCLASS='QSO'):
        """ Perform an SSA-like query on the specdb catalog

        Parameters
        ----------
        POS : str, optional
          position on the sky RA,DEC;coordinate system
          Only ICRS is accepted for now
        SIZE : float, optional
          Search radius in deg
        TIME : str, optional
          Not implemented currently
        BAND : str, optional
        FORMAT : str, optional
          Specifies format of dataset that would be returned

        Returns
        -------
        result : VOTable

        """

        # Default Infos
        def_infos = []
        def_infos.append(Info(name='SERVICE_PROTOCOL', value=1.1, content="SSAP"))

        # METADATA??
        if FORMAT == 'METADATA':
            votable = build_metaquery(def_infos)
            return votable

        def_infos.append(Info(name='REQUEST', value='queryData'))
        def_infos.append(Info(name='serviceName', value='ssap'))
        def_infos.append(Info(name='FORMAT', value=FORMAT))

        # SIZE
        size_unit = u.deg
        if SIZE is None:
            SIZE = 1e-3  # deg
        def_infos.append(Info(name='SIZE', value=SIZE, content="Search radius adopted (deg)"))

        # Other
        if TIME is not None:
            warnings.warn("TIME parameter is not yet implemented")
        if BAND is not None:
            warnings.warn("BAND parameter is not yet implemented")

        status = 'OK'
        query_status = []
        qinfos = []

        # Parse POS
        if POS is None:
            status = 'ERROR'
            qinfos.append(Info(name='QUERY_STATUS', value="ERROR", content="POS not provided and not a METADATA query"))
            spl = []
        else:
            spl = POS.split(';')
            scoord = spl[0]
        def_infos.append(Info(name='POS', value=POS))

        if len(spl) > 1:
            coord_sys = spl[1]
        else:
            coord_sys = 'ICRS'
        if coord_sys not in ['ICRS']:
            status = 'ERROR'
            qinfos.append(Info(name='QUERY_STATUS', value="ERROR", content="Coordinate system {:s} not implemented".format(coord_sys)))
            #raise IOError("Coordinate system {:s} not implemented".format(coord_sys))

        # Return if failed
        if status != 'OK':
            votable = empty_vo()
            votable.resources[0].infos += qinfos
            votable.resources[0].infos += def_infos
            return votable
        else:
            qinfos.append(Info(name='QUERY_STATUS', value="OK", content="Successful search"))

        # Build Table
        ra,dec = scoord.split(',')
        coord = SkyCoord(ra=ra, dec=dec, unit='deg')


        # Perform query
        _, subcat, IDs = self.specdb.qcat.query_position(coord, SIZE*size_unit, max_match=MAXREC)

        if IDs.size > 0:
            # Grab meta params
            metaparams, pIDs = metaquery_param()

            # Grab sub-catalog
            gd_groups = self.specdb.qcat.groups_containing_IDs(IDs)

            # Loop on groups
            all_vometa = []
            for group in gd_groups:
                # Grab meta from group
                flag_group = self.specdb.qcat.group_dict[group]
                gdID = np.where(subcat['flag_group'].data & flag_group)[0]
                meta_group = self.specdb[group].meta_from_ids(IDs[gdID], first=False)
                meta_attr = self.specdb[group].meta_attr
                # Convert to SSA VO
                ssavo_meta = meta_to_ssa_vo(group, meta_group, meta_attr, subcat[gdID],
                                            self.specdb.idkey,
                                            self.specdb.qcat.cat_attr)
                all_vometa.append(ssavo_meta)
            vometa = vstack(all_vometa)
            # Generate true VOTable
            votable = from_table(vometa)
            # Update fields
            tbl = votable.resources[0].tables[0]
            for param in metaparams:
                try:
                    field = tbl.get_field_by_id(param.ID)
                except KeyError:
                    print("Need field with ID={:s}".format(param.ID))
                else:
                    field.utype = param.utype
                    if hasattr(param, 'ucd'):
                        field.ucd = param.ucd
                    if hasattr(param, 'unit'):
                        field.unit = param.unit
            # Add Parameters
            #pub_param = Param(tbl, name="Publisher", utype="ssa:Curation.Publisher", ucd=" meta.curation",
            #                  datatype="char", arraysize="*", value="JXP")
            #tbl.params.append(pub_param)
        else:  # Generate a dummy table
            votable = empty_vo()

        # INFO
        for qinfo in qinfos:
            votable.resources[0].infos.append(qinfo)
        for dinfo in def_infos:
            votable.resources[0].infos.append(dinfo)

        # Return
        return votable
コード例 #10
0
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')
コード例 #11
0
def conversionVOTable(archivosdat, flag):
    try:
        os.makedirs(directorioTransformadosVOTable + flag)

        for i in range(len(archivosdat)):

            nombre = archivosdat[i][32:]
            nombreFits = nombre.replace('.dat', '')
            nombreVOTable = nombre.replace('.dat', '')

            fichero_fits = directorioTransformadosFits + flag + "/" + nombreFicheros + nombreFits + '.fits'
            votable_output = directorioTransformadosVOTable + flag + "/" + nombreFicheros + nombreVOTable + '.vot'
            votable_output2 = directorioTransformadosVOTable + flag + "/" + nombreFicheros + nombreVOTable + '_2.vot'

            fichero = nombreFicheros + nombreFits + '.fits'

            t = tb.read(fichero_fits, 2)
            t2 = tb.read(fichero_fits, 3)
            os.remove(fichero_fits)

            votable = from_table(t[0:1])
            votable2 = from_table(t2[0:1])

            writeto(votable, votable_output)
            writeto(votable2, votable_output2)

            tabla2 = parse_single_table(votable_output2)
            os.remove(votable_output2)

            votable = parse(votable_output)
            resource = votable.resources[0]
            resource.description = "Fichero " + fichero + " " + DescripcionVOTable
            resource.tables.append(tabla2)

            param = Param(votable,
                          name="TITLE",
                          datatype="char",
                          arraysize=str(len(fichero)),
                          value=fichero)
            param.description = "nombre del fichero"
            resource.params.append(param)

            param = Param(votable,
                          name="DATE",
                          datatype="char",
                          arraysize=str(len(diaExtraido)),
                          value=diaExtraido)
            param.description = "fecha de la deteccion"
            resource.params.append(param)

            for n in range(len(totalValores)):
                if (totalValores[n].isdigit()
                        or (totalValores[n].startswith('-')
                            and totalValores[n][1:].isdigit())):
                    param = Param(votable,
                                  name=totalCabeceras[n],
                                  datatype="int",
                                  value=totalValores[n])
                    param.description = totalDescripciones[n]
                    resource.params.append(param)
                elif (totalValores[n] == "True" or totalValores[n] == "False"):
                    param = Param(votable,
                                  name=totalCabeceras[n],
                                  datatype="boolean",
                                  value=totalValores[n])
                    param.description = totalDescripciones[n]
                    resource.params.append(param)
                else:
                    try:
                        if (float(totalValores[n])):
                            param = Param(votable,
                                          name=totalCabeceras[n],
                                          datatype="float",
                                          value=totalValores[n])
                            param.description = totalDescripciones[n]
                            resource.params.append(param)
                    except:
                        if (totalCabeceras[n][0:7] == "COMMENT"
                                or totalCabeceras[n][0:7] == "HISTORY"):
                            info = Info(name=totalCabeceras[n][0:7],
                                        value=totalValores[n])
                            resource.infos.append(info)
                        else:
                            param = Param(votable,
                                          name=totalCabeceras[n],
                                          datatype="char",
                                          arraysize=str(len(totalValores[n])),
                                          value=totalValores[n])
                            param.description = totalDescripciones[n]
                            resource.params.append(param)

            votable.to_xml(votable_output)
            stri = '    <FITS extnum="2">\n     <STREAM encoding="gzip" href="' + enlaces[
                i] + '"/>\n    </FITS>\n'
            stri2 = '    <FITS extnum="3">\n     <STREAM encoding="gzip" href="' + enlaces[
                i] + '"/>\n    </FITS>\n'

            f = open(votable_output, "r")
            leido = f.readlines()
            f.close()
            os.remove(votable_output)

            hayIni = 0
            hayFin = 0
            for n in range(len(leido)):
                if (leido[n][4:8] == "DATA"):
                    if (hayIni == 0):
                        Ini = n + 1
                        hayIni = 1
                    else:
                        Ini2 = n + 1
                if (leido[n][5:9] == "DATA"):
                    if (hayFin == 0):
                        Fin = n
                        hayFin = 1
                    else:
                        Fin2 = n

            parte1 = leido[:Ini]
            parte2 = leido[Fin:Ini2]
            parte3 = leido[Fin2:]

            file2 = open(votable_output, "w")

            for p1 in range(len(parte1)):
                if (parte1[p1][3:10] == "TABLE n"):
                    file2.write("  <TABLE nrows=\"" + str(len(t)) + "\">" +
                                "\n")
                else:
                    file2.write(parte1[p1])

            file2.write(stri)

            for p2 in range(len(parte2)):
                if (parte2[p2][3:10] == "TABLE n"):
                    file2.write("  <TABLE nrows=\"" + str(len(t2)) + "\">" +
                                "\n")
                else:
                    file2.write(parte2[p2])

            file2.write(stri2)

            for p3 in range(len(parte3)):
                file2.write(parte3[p3])

            file2.close()
    except:
        flogs.write("LOG: ERROR en la conversion a  VOTable de los " + flag +
                    "\n")
        flogs.close()
        shutil.rmtree(directorio)
        sys.exit(1)
コード例 #12
0
def conversionVOTable(archivosdat, flag, t_deteccioni, duracion):
    try:
        os.makedirs(directorioTransformadosVOTable + flag)

        for i in range(len(archivosdat)):
            nombre = archivosdat[i][32:]
            nombreFits = nombre.replace('.dat', '')
            nombreVOTable = nombre.replace('.dat', '')
            for cont in ['1', '2']:
                fichero_fits = directorioTransformadosFits + flag + "/" + estacion + '_' + t_deteccion[
                    i] + '.fits'
                votable_output = directorioTransformadosVOTable + flag + "/" + estacion + '_' + t_deteccion[
                    i] + '.' + cont + '.vot'
                votable_output2 = directorioTransformadosVOTable + flag + "/" + estacion + '_' + t_deteccion[
                    i] + '.' + cont + '_2.vot'

                fichero = estacion + '_' + t_deteccion[i]

                t = tb.read(fichero_fits, 2)
                t2 = tb.read(fichero_fits, 3)

                if (cont == '2'):
                    os.remove(fichero_fits)
                votable = from_table(t[0:1])
                votable2 = from_table(t2[0:1])

                writeto(votable, votable_output)
                writeto(votable2, votable_output2)

                tabla2 = parse_single_table(votable_output2)
                os.remove(votable_output2)

                votable = parse(votable_output)

                resource = votable.resources[0]
                resource.description = "File " + fichero + " " + DescripcionVOTable
                resource.tables.append(tabla2)

                param = Param(votable,
                              name="TITLE",
                              datatype="char",
                              arraysize=str(len(fichero)),
                              value=fichero)
                param.description = "Name of the file"
                resource.params.append(param)

                param = Param(votable,
                              name="DATE",
                              datatype="char",
                              arraysize=str(len(diaExtraido)),
                              value=diaExtraido)
                param.description = "Date of its detection (YYYY-MM-DD)"
                resource.params.append(param)

                param = Param(votable,
                              name="DURATION",
                              datatype="int",
                              value=duracion[i],
                              unit="ms")
                param.description = "Duration of the event"
                resource.params.append(param)

                for n in range(len(totalValores)):
                    flag_u = 0
                    index = totalDescripciones[n].find('(')
                    if (index != -1):
                        if (n == 41 or n == 44 or n == 45):
                            flag_u = 0
                        else:
                            unidad = totalDescripciones[n].split(
                                '(', 1)[1].split(')')[0]
                            flag_u = 1
                    if (totalValores[n].isdigit()
                            or (totalValores[n].startswith('-')
                                and totalValores[n][1:].isdigit())):
                        if (flag_u == 1):
                            param = Param(votable,
                                          name=totalCabeceras[n],
                                          datatype='int',
                                          value=totalValores[n],
                                          unit=unidad)
                        else:
                            param = Param(votable,
                                          name=totalCabeceras[n],
                                          datatype="int",
                                          value=totalValores[n])
                        param.description = totalDescripciones[n]
                        resource.params.append(param)
                    elif (totalValores[n] == "True"
                          or totalValores[n] == "False"):
                        param = Param(votable,
                                      name=totalCabeceras[n],
                                      datatype="boolean",
                                      value=totalValores[n])
                        param.description = totalDescripciones[n]
                        resource.params.append(param)
                    else:
                        try:
                            if (float(totalValores[n])):
                                if (flag_u == 1):
                                    param = Param(votable,
                                                  name=totalCabeceras[n],
                                                  datatype='float',
                                                  value=totalValores[n],
                                                  unit=unidad)
                                else:
                                    param = Param(votable,
                                                  name=totalCabeceras[n],
                                                  datatype="float",
                                                  value=totalValores[n])
                                param.description = totalDescripciones[n]
                                resource.params.append(param)
                        except:
                            if (totalCabeceras[n][0:7] == "COMMENT"
                                    or totalCabeceras[n][0:7] == "HISTORY"):
                                info = Info(name=totalCabeceras[n][0:7],
                                            value=totalValores[n])
                                resource.infos.append(info)
                            else:
                                param = Param(votable,
                                              name=totalCabeceras[n],
                                              datatype="char",
                                              arraysize=str(
                                                  len(totalValores[n])),
                                              value=totalValores[n])
                                param.description = totalDescripciones[n]
                                resource.params.append(param)
                votable.to_xml(votable_output)
                stri = '    <FITS extnum="2">\n     <STREAM encoding="gzip" href="' + enlaces[
                    i] + '"/>\n    </FITS>\n'
                stri2 = '    <FITS extnum="3">\n     <STREAM encoding="gzip" href="' + enlaces[
                    i] + '"/>\n    </FITS>\n'

                f = open(votable_output, "r")
                leido = f.readlines()
                f.close()
                os.remove(votable_output)

                hayIni = 0
                hayFin = 0
                for n in range(len(leido)):
                    if (leido[n][4:8] == "DATA"):
                        if (hayIni == 0):
                            Ini = n + 1
                            hayIni = 1
                        else:
                            Ini2 = n + 1
                    if (leido[n][5:9] == "DATA"):
                        if (hayFin == 0):
                            Fin = n
                            hayFin = 1
                        else:
                            Fin2 = n

                parte1 = leido[:Ini]
                parte2 = leido[Fin:Ini2]
                parte3 = leido[Fin2:]

                file2 = open(votable_output, "w")

                for p1 in range(len(parte1)):
                    if (parte1[p1][3:10] == "TABLE n"):
                        if (cont == '2'):
                            break
                        file2.write("  <TABLE nrows=\"" + str(len(t)) + "\">" +
                                    "\n")
                    else:
                        file2.write(parte1[p1])
                if (cont == '1'):
                    file2.write(stri)

                for p2 in range(len(parte2)):
                    if (cont == '1'):
                        break
                    if (parte2[p2][3:10] == "TABLE n"):
                        file2.write("  <TABLE nrows=\"" + str(len(t2)) +
                                    "\">" + "\n")
                    elif (p2 == 0 or p2 == 1):
                        continue
                    else:
                        file2.write(parte2[p2])

                if (cont == '2'):
                    file2.write(stri2)

                for p3 in range(len(parte3)):
                    file2.write(parte3[p3])
                file2.close()
    except:
        flogs.write("LOG: ERROR en la conversion a  VOTable de los " + flag +
                    "\n")
        flogs.close()
        shutil.rmtree(directorio)
        sys.exit(1)