Beispiel #1
0
 def _is_match(elem):
     try:
         if elem.Name != name:
             return False
     except AttributeError:  # Name is not set
         return False
     for key, value in match.items():
         try:
             if get_param(elem, key).pcdata != value:
                 return False
         except ValueError:  # no Param with this Name
             return False
     return True
Beispiel #2
0
 def _is_match(arr):
     """Work out whether this `<Array>` element matches the request
     """
     parent = arr.parentNode
     if ((name is not None and not _match_name(arr, name))
             or (epoch is not None and not _match_time(parent, epoch))):
         return False
     for key, value in params.items():
         try:
             if get_param(parent, key).pcdata != value:
                 return False
         except ValueError:  # no Param with this Name
             return False
     return True
Beispiel #3
0
 def _is_match(arr):
     parent = arr.parentNode
     if (
         (name is not None and not _match_name(arr, name)) or
         (epoch is not None and not _match_time(parent, epoch))
     ):
         return False
     for key, value in params.items():
         try:
             if get_param(parent, key).pcdata != value:
                 return False
         except ValueError:  # no Param with this Name
             return False
     return True
Beispiel #4
0
def _update_metadata_from_ligolw(array, kwargs):
    from ligo.lw.ligolw import Time
    from ligo.lw.param import get_param

    parent = array.parentNode

    # pick out the epoch
    try:
        time, = parent.getElementsByTagName(Time.tagName)
    except ValueError:
        pass
    else:
        kwargs.setdefault('epoch', _get_time(time))

    # copy over certain other params, if they exist
    for key in ('channel', ):
        try:
            kwargs[key] = get_param(parent, key)
        except ValueError:
            pass

    return kwargs
Beispiel #5
0
def _parse_series(elem, creatorfunc, delta_target_unit_string):
    t, = elem.getElementsByTagName(ligolw.Time.tagName)
    a, = elem.getElementsByTagName(ligolw.Array.tagName)
    dims = a.getElementsByTagName(ligolw.Dim.tagName)
    f0 = ligolw_param.get_param(elem, u"f0")

    if t.Type != u"GPS":
        raise ValueError("epoch Type must be GPS")
    epoch = t.pcdata

    # Target units: inverse seconds
    inverse_seconds_unit = lal.Unit("s^-1")

    delta_target_unit = lal.Unit(delta_target_unit_string)

    # Parse units of f0 field
    f0_unit = lal.Unit(str(f0.Unit))

    # Parse units of deltaF field
    delta_unit = lal.Unit(str(dims[0].Unit))

    # Parse units of data
    sample_unit = lal.Unit(str(a.Unit))

    # Initialize data structure
    series = creatorfunc(str(a.Name), epoch,
                         f0.pcdata * float(f0_unit / inverse_seconds_unit),
                         dims[0].Scale * float(delta_unit / delta_target_unit),
                         sample_unit, len(a.array.T))

    # Assign data
    if np.iscomplexobj(series.data.data):
        series.data.data = a.array[1] + 1j * a.array[2]
    else:
        series.data.data = a.array[1]

    # Done!
    return series
Beispiel #6
0
def read_series(source, name, match=None):
    """Read a `Series` from LIGO_LW-XML

    Parameters
    ----------
    source : `file`, `str`, :class:`~ligo.lw.ligolw.Document`
        file path or open LIGO_LW-format XML file

    name : `str`
        name of the relevant `LIGO_LW` element to read

    match : `dict`, optional
        dict of (key, value) `Param` pairs to match correct LIGO_LW element,
        this is useful if a single file contains multiple `LIGO_LW` elements
        with the same name
    """
    from ligo.lw.ligolw import (LIGO_LW, Time, Array, Dim)
    from ligo.lw.param import get_param

    # read document
    xmldoc = read_ligolw(source, contenthandler=series_contenthandler())

    # parse match dict
    if match is None:
        match = dict()

    def _is_match(elem):
        try:
            if elem.Name != name:
                return False
        except AttributeError:  # Name is not set
            return False
        for key, value in match.items():
            try:
                if get_param(elem, key).pcdata != value:
                    return False
            except ValueError:  # no Param with this Name
                return False
        return True

    # parse out correct element
    matches = filter(_is_match, xmldoc.getElementsByTagName(LIGO_LW.tagName))
    try:
        elem, = matches
    except ValueError as exc:
        if not matches:
            exc.args = ("no LIGO_LW elements found matching request",)
        else:
            exc.args = ('multiple LIGO_LW elements found matching request, '
                        'please consider using `match=` to select the '
                        'correct element',)
        raise

    # get data
    array, = elem.getElementsByTagName(Array.tagName)

    # parse dimensions
    dims = array.getElementsByTagName(Dim.tagName)
    xdim = dims[0]
    x0 = xdim.Start
    dx = xdim.Scale
    xunit = xdim.Unit
    try:
        ndim = dims[1].n
    except IndexError:
        pass
    else:
        if ndim > 2:
            raise ValueError("Cannot parse LIGO_LW Array with {} "
                             "dimensions".format(ndim))

    # parse metadata
    array_kw = {
        'name': array.Name,
        'unit': array.Unit,
        'xunit': xunit,
    }
    try:
        array_kw['epoch'] = to_gps(
            elem.getElementsByTagName(Time.tagName)[0].pcdata)
    except IndexError:
        pass
    for key in ('channel',):
        try:
            array_kw[key] = get_param(elem, key)
        except ValueError:
            pass

    # build Series
    try:
        xindex, value = array.array
    except ValueError:  # not two dimensions stored
        return Series(array.array[0], x0=x0, dx=dx, **array_kw)
    return Series(value, xindex=xindex, **array_kw)