コード例 #1
0
ファイル: test_registry.py プロジェクト: Cadair/astropy
def test_read_identifier(tmpdir):

    io_registry.register_identifier(
        'test1', TestData,
        lambda o, path, fileobj, *x, **y: path.endswith('a'))
    io_registry.register_identifier(
        'test2', TestData,
        lambda o, path, fileobj, *x, **y: path.endswith('b'))

    # Now check that we got past the identifier and are trying to get
    # the reader. The io_registry.get_reader will fail but the error message
    # will tell us if the identifier worked.

    filename = tmpdir.join("testfile.a").strpath
    open(filename, 'w').close()
    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData.read(filename)
    assert str(exc.value).startswith(
        "No reader defined for format 'test1' and class 'TestData'")

    filename = tmpdir.join("testfile.b").strpath
    open(filename, 'w').close()
    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData.read(filename)
    assert str(exc.value).startswith(
        "No reader defined for format 'test2' and class 'TestData'")
コード例 #2
0
ファイル: test_registry.py プロジェクト: Cadair/astropy
def test_register_identifier():

    io_registry.register_identifier('test1', TestData, empty_identifier)
    io_registry.register_identifier('test2', TestData, empty_identifier)

    io_registry.unregister_identifier('test1', TestData)
    io_registry.unregister_identifier('test2', TestData)
コード例 #3
0
ファイル: table.py プロジェクト: diegobersanetti/gwpy
def inherit_io_registrations(cls):
    parent = cls.__mro__[1]
    for row in registry.get_formats(data_class=parent):
        name = row["Format"]
        # read
        if row["Read"].lower() == "yes":
            registry.register_reader(
                name,
                cls,
                registry.get_reader(name, parent),
                force=False,
            )
        # write
        if row["Write"].lower() == "yes":
            registry.register_writer(
                name,
                cls,
                registry.get_writer(name, parent),
                force=False,
            )
        # identify
        if row["Auto-identify"].lower() == "yes":
            registry.register_identifier(
                name,
                cls,
                registry._identifiers[(name, parent)],
                force=False,
            )
    return cls
コード例 #4
0
ファイル: data_factories.py プロジェクト: schryer/glue
def astropy_tabular_data(*args, **kwargs):
    """
    Build a data set from a table. We restrict ourselves to tables
    with 1D columns.

    All arguments are passed to
        astropy.table.Table.read(...).
    """
    from distutils.version import LooseVersion
    from astropy import __version__
    if LooseVersion(__version__) < LooseVersion("0.2"):
        raise RuntimeError("Glue requires astropy >= v0.2. Please update")

    result = Data()

    # Read the table
    from astropy.table import Table

    # Add identifiers for ASCII data
    from astropy.io import registry
    if LooseVersion(__version__) < LooseVersion("0.3"):
        registry.register_identifier('ascii', Table, _ascii_identifier_v02,
                                     force=True)
    else:
        # Basically, we always want the plain ascii reader for now.
        # But astropy will complain about ambiguous formats (or use another reader)
        # unless we remove other registry identifiers and set up our own reader

        nope = lambda *a, **k: False
        registry.register_identifier('ascii.glue', Table, _ascii_identifier_v03,
                                     force=True)
        registry.register_identifier('ascii.csv', Table, nope, force=True)
        registry.register_identifier('ascii.fast_csv', Table, nope, force=True)
        registry.register_identifier('ascii', Table, nope, force=True)
        registry.register_reader('ascii.glue', Table,
                                 lambda path: Table.read(path, format='ascii'),
                                 force=True)

    # Import FITS compatibility (for Astropy 0.2.x)
    from ..external import fits_io

    table = Table.read(*args, **kwargs)

    # Loop through columns and make component list
    for column_name in table.columns:
        c = table[column_name]
        u = c.unit if hasattr(c, 'unit') else c.units

        if table.masked:
            # fill array for now
            try:
                c = c.filled(fill_value=np.nan)
            except ValueError:  # assigning nan to integer dtype
                c = c.filled(fill_value=-1)

        nc = Component.autotyped(c, units=u)
        result.add_component(nc, column_name)

    return result
コード例 #5
0
ファイル: registers.py プロジェクト: astropy/specutils
    def decorator(func):
        io_registry.register_reader(label, dtype, func)

        if identifier is None:
            # If the identifier is not defined, but the extensions are, create
            # a simple identifier based off file extension.
            if extensions is not None:
                logging.info("'{}' data loader provided for {} without "
                             "explicit identifier. Creating identifier using "
                             "list of compatible extensions".format(
                                 label, dtype.__name__))
                id_func = lambda *args, **kwargs: any([args[1].endswith(x)
                                                          for x in extensions])
            # Otherwise, create a dummy identifier
            else:
                logging.warning("'{}' data loader provided for {} without "
                             "explicit identifier or list of compatible "
                             "extensions".format(label, dtype.__name__))
                id_func = lambda *args, **kwargs: True
        else:
            id_func = identifier_wrapper(identifier)

        io_registry.register_identifier(label, dtype, id_func)

        # Include the file extensions as attributes on the function object
        func.extensions = extensions

        # Include priority on the loader function attribute
        func.priority = priority

        # Sort the io_registry based on priority
        sorted_loaders = sorted(io_registry._readers.items(),
                                key=lambda item: getattr(item[1], 'priority', 0))

        # Update the registry with the sorted dictionary
        io_registry._readers.clear()
        io_registry._readers.update(sorted_loaders)

        logging.debug("Successfully loaded reader \"{}\".".format(label))

        # Automatically register a SpectrumList reader for any data_loader that
        # reads Spectrum1D objects. TODO: it's possible that this
        # functionality should be opt-in rather than automatic.
        if dtype is Spectrum1D:
            def load_spectrum_list(*args, **kwargs):
                return SpectrumList([ func(*args, **kwargs) ])

            # Add these attributes to the SpectrumList reader as well
            load_spectrum_list.extensions = extensions
            load_spectrum_list.priority = priority

            io_registry.register_reader(label, SpectrumList, load_spectrum_list)
            io_registry.register_identifier(label, SpectrumList, id_func)
            logging.debug("Created SpectrumList reader for \"{}\".".format(label))

        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
コード例 #6
0
ファイル: registers.py プロジェクト: keflavich/specutils
    def decorator(func):
        io_registry.register_reader(label, dtype, func)
        io_registry.register_identifier(label, dtype, identifier)

        logging.info("Successfully loaded reader \"{}\".".format(label))

        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
コード例 #7
0
ファイル: hdf5.py プロジェクト: Cadair/astropy
def register_hdf5():
    """
    Register HDF5 with Unified I/O.
    """
    from astropy.io import registry as io_registry
    from astropy.table import Table

    io_registry.register_reader('hdf5', Table, read_table_hdf5)
    io_registry.register_writer('hdf5', Table, write_table_hdf5)
    io_registry.register_identifier('hdf5', Table, is_hdf5)
コード例 #8
0
ファイル: ascii.py プロジェクト: WanduiAlbert/gwpy
def register_ascii(obj):
    """Register ASCII I/O methods for given type obj

    This factory method registers 'txt' and 'csv' I/O formats with
    a reader, writer, and auto-identifier
    """
    for form, delim in formats.iteritems():
        read_, write_ = ascii_io_factory(obj, delim)
        register_identifier(form, obj, identify_factory(form, '%s.gz' % form))
        register_writer(form, obj, write_)
        register_reader(form, obj, read_)
コード例 #9
0
ファイル: data_factories.py プロジェクト: ChrisBeaumont/glue
def tabular_data(*args, **kwargs):
    """
    Build a data set from a table. We restrict ourselves to tables
    with 1D columns.

    All arguments are passed to
        astropy.table.Table.read(...).
    """
    from distutils.version import LooseVersion
    from astropy import __version__
    if LooseVersion(__version__) < LooseVersion("0.2"):
        raise RuntimeError("Glue requires astropy >= v0.2. Please update")

    result = Data()

    # Read the table
    from astropy.table import Table

    # Add identifiers for ASCII data
    from astropy.io import registry

    def ascii_identifier(origin, args, kwargs):
        # should work with both Astropy 0.2 and 0.3
        if isinstance(args[0], basestring):
            return args[0].endswith(('csv', 'tsv', 'txt', 'tbl', 'dat',
                                     'csv.gz', 'tsv.gz', 'txt.gz', 'tbl.gz',
                                     'dat.gz'))
        else:
            return False
    registry.register_identifier('ascii', Table, ascii_identifier,
                                 force=True)

    # Import FITS compatibility (for Astropy 0.2.x)
    from ..external import fits_io

    table = Table.read(*args, **kwargs)

    # Loop through columns and make component list
    for column_name in table.columns:
        c = table[column_name]
        u = c.units

        if table.masked:
            # fill array for now
            try:
                c = c.filled(fill_value=np.nan)
            except ValueError:  # assigning nan to integer dtype
                c = c.filled(fill_value=-1)

        c = Component(np.asarray(c), units=u)
        result.add_component(c, column_name)

    return result
コード例 #10
0
ファイル: hdf5.py プロジェクト: paulaltin/gwpy
def register_hdf5_array_io(array_type, format='hdf5', identify=True):
    """Registry read() and write() methods for the HDF5 format
    """
    def from_hdf5(*args, **kwargs):
        kwargs.setdefault('array_type', array_type)
        return array_from_hdf5(*args, **kwargs)
    def to_hdf5(*args, **kwargs):
        kwargs.setdefault('array_type', array_type)
        return array_to_hdf5(*args, **kwargs)
    registry.register_reader(format, array_type, from_hdf5)
    registry.register_writer(format, array_type, to_hdf5)
    if identify:
        registry.register_identifier(format, array_type, hdf5io.identify_hdf5)
コード例 #11
0
ファイル: identify.py プロジェクト: gitter-badger/gwpy
def register_identifier(format='gwf'):
    """Register a frame-file identifier for the given format.
    """
    registry.register_identifier(format, TimeSeries, identify_gwf)
    registry.register_identifier(format, TimeSeriesDict, identify_gwf)
    registry.register_identifier(format, StateVector, identify_gwf)
    registry.register_identifier(format, StateVectorDict, identify_gwf)
コード例 #12
0
ファイル: decorators.py プロジェクト: spacetelescope/specviz
    def decorator(func):
        logging.info("Added {} to loader registry.".format(label))

        func.loader_wrapper = True

        format = label #"-".join(label.lower().split())
        io_registry.register_reader(format, Spectrum1DRef, func)
        io_registry.register_identifier(format, Spectrum1DRef,
                                        identifier)

        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
コード例 #13
0
ファイル: test_registry.py プロジェクト: Cadair/astropy
def test_write_identifier():

    io_registry.register_identifier('test1', TestData, lambda o, *x, **y: x[0].startswith('a'))
    io_registry.register_identifier('test2', TestData, lambda o, *x, **y: x[0].startswith('b'))

    # Now check that we got past the identifier and are trying to get
    # the reader. The io_registry.get_writer will fail but the error message
    # will tell us if the identifier worked.

    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData().write('abc')
    assert str(exc.value).startswith(
        "No writer defined for format 'test1' and class 'TestData'")

    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData().write('bac')
    assert str(exc.value).startswith(
        "No writer defined for format 'test2' and class 'TestData'")
コード例 #14
0
ファイル: test_registry.py プロジェクト: Cadair/astropy
def test_identifier_origin():

    io_registry.register_identifier('test1', TestData, lambda o, *x, **y: o == 'read')
    io_registry.register_identifier('test2', TestData, lambda o, *x, **y: o == 'write')
    io_registry.register_reader('test1', TestData, empty_reader)
    io_registry.register_writer('test2', TestData, empty_writer)

    # There should not be too many formats defined
    TestData.read()
    TestData().write()

    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData.read(format='test2')
    assert str(exc.value).startswith(
        "No reader defined for format 'test2' and class 'TestData'")

    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData().write(format='test1')
    assert str(exc.value).startswith(
        "No writer defined for format 'test1' and class 'TestData'")
コード例 #15
0
ファイル: registries.py プロジェクト: spacetelescope/specviz
    def _load_yaml(self):
        """
        Loads yaml files as custom loaders.
        """
        cur_path = os.path.join(os.path.dirname(__file__), '..', 'io',
                                'yaml_loaders')
        usr_path = os.path.join(os.path.expanduser('~'), '.specviz')
        lines_path = os.path.join(os.path.dirname(__file__), '../data/linelists')

        # This order determines priority in case of duplicates; paths higher
        # in this list take precedence
        check_paths = [usr_path, cur_path, lines_path]

        if not os.path.exists(usr_path):
            os.mkdir(usr_path)

        for path in check_paths:
            for file_name in [x for x in os.listdir(path)
                              if x.endswith('yaml')]:
                f_path = os.path.join(path, file_name)
                custom_loader = yaml.load(open(f_path, 'r'))
                custom_loader.set_filter()

                # Figure out which of the two generic loaders to associate
                # this yaml file with
                if any(ext in custom_loader.extension for ext in ['fits']):
                    loader = FitsYamlRegister(custom_loader)
                elif any(ext in custom_loader.extension
                         for ext in ['txt', 'data']):
                    loader = AsciiYamlRegister(custom_loader)

                try:
                    io_registry.register_reader(custom_loader.name,
                                                Spectrum1DRef,
                                                loader.reader)
                    io_registry.register_identifier(custom_loader.name,
                                                    Spectrum1DRef,
                                                    loader.identify)
                except io_registry.IORegistryError as e:
                    logging.error(e)
コード例 #16
0
ファイル: data_factories.py プロジェクト: Spencerx/glue
def tabular_data(*args, **kwargs):
    """
    Build a data set from a table. We restrict ourselves to tables
    with 1D columns.

    All arguments are passed to
        astropy.table.Table.read(...).
    """
    from distutils.version import LooseVersion
    from astropy import __version__
    if LooseVersion(__version__) < LooseVersion("0.2"):
        raise RuntimeError("Glue requires astropy >= v0.2. Please update")

    result = Data()

    # Read the table
    from astropy.table import Table

    # Add identifiers for ASCII data
    from astropy.io import registry
    if LooseVersion(__version__) < LooseVersion("0.3"):
        registry.register_identifier('ascii', Table, _ascii_identifier_v02,
                                     force=True)
    else:
        registry.register_identifier('ascii', Table, _ascii_identifier_v03,
                                     force=True)
        # Clobber the identifier
        # added in astropy/astropy/pull/1935
        registry.register_identifier('ascii.csv', Table, lambda *a, **k: False,
                                     force=True)

    # Import FITS compatibility (for Astropy 0.2.x)
    from ..external import fits_io

    table = Table.read(*args, **kwargs)

    # Loop through columns and make component list
    for column_name in table.columns:
        c = table[column_name]
        u = c.units

        if table.masked:
            # fill array for now
            try:
                c = c.filled(fill_value=np.nan)
            except ValueError:  # assigning nan to integer dtype
                c = c.filled(fill_value=-1)

        nc = Component.autotyped(c, units=u)
        result.add_component(nc, column_name)

    return result
コード例 #17
0
    )  # extra info -> meta


def to_myformat(cosmology, file, *, overwrite=False, **kwargs):
    """Return the cosmology as a ``mycosmo``."""
    # Cosmology provides a nice method "mapping", so all that needs to
    # be done here is initialize from the dictionary
    mapping = cosmology.to_format("mapping")
    # correct entries
    mapping["hubble_parameter"] = mapping.pop("H0")
    mapping["Omega_matter_initial"] = mapping.pop("Om0")
    ...  # keep remapping

    return MyCosmology(**mapping)


def mypackage_identify(origin, format, *args, **kwargs):
    itis = False
    if origin == "read":
        itis = isinstance(args[1], MyCosmology) and (format in (None, "mypackage"))

    return itis


# -------------------------------------------------------------------
# Register to/from_format & identify methods with Astropy Unified I/O

io_registry.register_reader("mypackage", Cosmology, read_mypackage)
io_registry.register_writer("mypackage", Cosmology, write_mypackage)
io_registry.register_identifier("mypackage", Cosmology, mypackage_identify)
コード例 #18
0
ファイル: test_registry.py プロジェクト: Cadair/astropy
def test_register_identifier_force():
    io_registry.register_identifier('test', TestData, empty_identifier)
    io_registry.register_identifier('test', TestData, empty_identifier, force=True)
コード例 #19
0
ファイル: hdf5.py プロジェクト: gitter-badger/gwpy
                h5group = h5file.create_group(group)
        else:
            h5group = h5file

        # create dataset
        data = numpy.zeros((len(seglist), 4), dtype=int)
        for i, seg in enumerate(seglist):
            start, end = map(LIGOTimeGPS, seg)
            data[i, :] = (start.seconds, start.nanoseconds,
                          end.seconds, end.nanoseconds)
        if (not len(seglist) and
                LooseVersion(h5py.version.version).version[0] < 2):
            kwargs.setdefault('maxshape', (None, 4))
            kwargs.setdefault('chunks', (1, 1))
        dset = h5group.create_dataset(name, data=data,
                                      compression=compression, **kwargs)
    finally:
        if not isinstance(output, h5py.Group):
            h5file.close()

    return dset


register_reader('hdf', SegmentList, segmentlist_from_hdf5)
register_writer('hdf', SegmentList, segmentlist_to_hdf5)
register_identifier('hdf', SegmentList, identify_hdf5)

register_reader('hdf', DataQualityFlag, flag_from_hdf5)
register_writer('hdf', DataQualityFlag, flag_to_hdf5)
register_identifier('hdf', DataQualityFlag, identify_hdf5)
コード例 #20
0
ファイル: test_registry.py プロジェクト: jpmorgen/astropy
def test_register_identifier_invalid():
    io_registry.register_identifier('test', TestData, empty_identifier)
    with pytest.raises(io_registry.IORegistryError) as exc:
        io_registry.register_identifier('test', TestData, empty_identifier)
    assert (str(exc.value) == "Identifier for format 'test' and class "
            "'TestData' is already defined")
コード例 #21
0
            flags = tab.QUALITY.flatten()

    finally:
        hdus.close()

    dispersion_unit = tab.columns[_find_col_index(tab.columns,
                                                  'WAVELENGTH')].unit.lower()
    flux_unit = _get_unit(tab.columns[_find_col_index(tab.columns,
                                                      'FLUX')].unit)

    wcs = WCSTable(wave, dispersion_unit)
    spec = Spectrum1D(data=flux,
                      uncertainty=StdDevUncertainty(sigma, flux_unit),
                      meta=meta,
                      unit=flux_unit,
                      mask=(flags != 0),
                      wcs=wcs)
    #flags=flags)
    return spec


registry.register_reader('IUE-mxlo', Spectrum1D, read_IUE_mxlo)


def identify_IUE_mxlo(origin, *args, **kwargs):
    return (isinstance(args[0], six.string_types)
            and os.path.splitext(args[0].lower())[1] == '.mxlo')


registry.register_identifier('IUE-mxlo', Spectrum1D, identify_IUE_mxlo)
コード例 #22
0
        data quality flag to print
    fobj : `file`, `str`
        open file object, or file path, to write to
    header : `bool`, optional
        print header into the file, default: `True`
    coltype : `type`, optional
        numerical type in which to cast times before printing

    Notes
    -----
    In this format, only the
    :attr:`~gwpy.segments.flag.DataQualityFlag.active` segments are
    printed

    See Also
    --------
    :mod:`glue.segmentsUtils`
        for definition of the segwizard format, and the to/from functions
        used in this GWpy module
    """
    to_segwizard(flag.active, fobj, header=header, coltype=coltype)


registry.register_reader('segwizard', DataQualityFlag, flag_from_segwizard)
registry.register_writer('segwizard', DataQualityFlag, flag_to_segwizard)
registry.register_identifier('segwizard', DataQualityFlag, identify_segwizard)

registry.register_reader('segwizard', SegmentList, from_segwizard)
registry.register_writer('segwizard', SegmentList, to_segwizard)
registry.register_identifier('segwizard', SegmentList, identify_segwizard)
コード例 #23
0
ファイル: hdf5.py プロジェクト: gitter-badger/gwpy
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWpy.  If not, see <http://www.gnu.org/licenses/>.

"""This module attaches the HDF5 input output methods to the TimeSeries.

While these methods are avialable as methods of the class itself,
this module attaches them to the unified I/O registry, making it a bit
cleaner.
"""

from astropy.io.registry import (register_reader, register_writer,
                                 register_identifier)

from ... import version
from ...io.hdf5 import identify_hdf5
from ..core import TimeSeries
from ..statevector import StateVector

__author__ = 'Duncan Macleod <*****@*****.**>'
__version__ = version.version

register_reader('hdf', TimeSeries, TimeSeries.from_hdf5)
register_writer('hdf', TimeSeries, TimeSeries.to_hdf5)
register_identifier('hdf', TimeSeries, identify_hdf5)

register_reader('hdf', StateVector, StateVector.from_hdf5)
register_writer('hdf', StateVector, StateVector.to_hdf5)
register_identifier('hdf', StateVector, identify_hdf5)
コード例 #24
0
        The format of table data to write.  Must be one of ``tabledata``
        (text representation), ``binary`` or ``binary2``.  Default is
        ``tabledata``.  See :ref:`votable-serialization`.
    """

    # Only those columns which are instances of BaseColumn or Quantity can be written
    unsupported_cols = input.columns.not_isinstance((BaseColumn, Quantity))
    if unsupported_cols:
        unsupported_names = [col.info.name for col in unsupported_cols]
        raise ValueError(
            'cannot write table with mixin column(s) {0} to VOTable'.format(
                unsupported_names))

    # Check if output file already exists
    if isinstance(output, str) and os.path.exists(output):
        if overwrite:
            os.remove(output)
        else:
            raise OSError("File exists: {0}".format(output))

    # Create a new VOTable file
    table_file = from_table(input, table_id=table_id)

    # Write out file
    table_file.to_xml(output, tabledata_format=tabledata_format)


io_registry.register_reader('votable', Table, read_table_votable)
io_registry.register_writer('votable', Table, write_table_votable)
io_registry.register_identifier('votable', Table, is_votable)
コード例 #25
0
        """
        if ((filepath is not None) and
                filepath.lower().endswith(('.fts', '.fts.gz'))):

            return True

        else:
            return fits.connect.is_fits(origin, filepath, fileobj,
                                        *args, **kwargs)
else:
    is_fits = fits.connect.is_fits

if _ASTROPY_LT_1_3:
    registry.register_reader('fits', CCDData, fits_ccddata_reader)
    registry.register_writer('fits', CCDData, fits_ccddata_writer)
    registry.register_identifier('fits', CCDData, is_fits)
else:
    with registry.delay_doc_updates(CCDData):
        registry.register_reader('fits', CCDData, fits_ccddata_reader)
        registry.register_writer('fits', CCDData, fits_ccddata_writer)
        registry.register_identifier('fits', CCDData, is_fits)

try:
    CCDData.read.__doc__ = fits_ccddata_reader.__doc__
except AttributeError:
    CCDData.read.__func__.__doc__ = fits_ccddata_reader.__doc__

try:
    CCDData.write.__doc__ = fits_ccddata_writer.__doc__
except AttributeError:
    CCDData.write.__func__.__doc__ = fits_ccddata_writer.__doc__
コード例 #26
0
    nevents = tree.GetEntries()
    for i in range(nevents):
        tree.GetEntry()
        burst = sngl_burst_from_root(tree, columns=columns)
        if filt is None or filt(burst):
            append(burst)

    return out


def identify_omicron(*args, **kwargs):
    """Determine an input object as an Omicron-format ROOT file.
    """
    fp = args[3]
    if isinstance(fp, file):
        fp = fp.name
    elif isinstance(fp, CacheEntry):
        fp = fp.path
    # identify string
    if (isinstance(fp, (unicode, str)) and fp.endswith('root')
            and 'omicron' in fp.lower()):
        return True
        # identify cache object
    else:
        return False


registry.register_reader('omicron', lsctables.SnglBurstTable, table_from_root)
registry.register_identifier('omicron', lsctables.SnglBurstTable,
                             identify_omicron)
コード例 #27
0
ファイル: hdf5.py プロジェクト: gitter-badger/gwpy
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWpy.  If not, see <http://www.gnu.org/licenses/>.
"""This module attaches the HDF5 input output methods to the TimeSeries.

While these methods are avialable as methods of the class itself,
this module attaches them to the unified I/O registry, making it a bit
cleaner.
"""

from astropy.io.registry import (register_reader, register_writer,
                                 register_identifier)

from ... import version
from ...io.hdf5 import identify_hdf5
from ..core import TimeSeries
from ..statevector import StateVector

__author__ = 'Duncan Macleod <*****@*****.**>'
__version__ = version.version

register_reader('hdf', TimeSeries, TimeSeries.from_hdf5)
register_writer('hdf', TimeSeries, TimeSeries.to_hdf5)
register_identifier('hdf', TimeSeries, identify_hdf5)

register_reader('hdf', StateVector, StateVector.from_hdf5)
register_writer('hdf', StateVector, StateVector.to_hdf5)
register_identifier('hdf', StateVector, identify_hdf5)
コード例 #28
0
        raise NotImplementedError()


def write_fits_ldo(data, filename, overwrite=False):
    # Spectra may have HDUList objects instead of HDUs because they
    # have a beam table attached, so we want to try that first
    # (a more elegant way to write this might be to do "self._hdu_general.write"
    # and create a property `self._hdu_general` that selects the right one...)
    if hasattr(data, 'hdulist'):
        try:
            data.hdulist.writeto(filename, overwrite=overwrite)
        except TypeError:
            data.hdulist.writeto(filename, clobber=overwrite)
    elif hasattr(data, 'hdu'):
        try:
            data.hdu.writeto(filename, overwrite=overwrite)
        except TypeError:
            data.hdu.writeto(filename, clobber=overwrite)


io_registry.register_reader('fits', BaseSpectralCube, load_fits_cube)
io_registry.register_writer('fits', BaseSpectralCube, write_fits_cube)
io_registry.register_identifier('fits', BaseSpectralCube, is_fits)

io_registry.register_reader('fits', StokesSpectralCube, load_fits_cube)
io_registry.register_writer('fits', StokesSpectralCube, write_fits_cube)
io_registry.register_identifier('fits', StokesSpectralCube, is_fits)

io_registry.register_writer('fits', LowerDimensionalObject, write_fits_ldo)
io_registry.register_identifier('fits', LowerDimensionalObject, is_fits)
コード例 #29
0
    
    Parameters
    ----------
    ndcube_data : `ndcube.NDCube` object
        NDCube object

    filename : `str`
        Name of the file

    hdu_mask, hdu_uncertainty : str, optional
        If it is a string append this attribute to the HDUList as
        `astropy.io.fits.ImageHDU` with the string as extension name.
        Default is `MASK` for mask, `UNCERT` for uncertainty and `None` for flags.
    
    key_uncertainty_type : str, optional
        Th header key name for the class name of the uncertainty (if any) that
        is used to store the uncertainty type in the uncertainty type in the
        uncertainty hdu.
    
    """

    hdu = ndcube_data.to_hdu(hdu_mask, hdu_uncertainty, key_uncertainty_type)
    hdu.writeto(filename, **kwargs)


# Registering the writer function for writing NDCube files to fits format
# TODO: Implement fits reader for NDCube
with registry.delay_doc_updates(NDCube):
    registry.register_writer('fits', NDCube, fits_ndcube_writer)
    registry.register_identifier('fits', NDCube, fits.connect.is_fits)
コード例 #30
0
def test_write_toomanyformats():
    io_registry.register_identifier('test1', TestData, lambda o, *x, **y: True)
    io_registry.register_identifier('test2', TestData, lambda o, *x, **y: True)
    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData().write()
    assert str(exc.value) == "Format is ambiguous - options are: test1, test2"
コード例 #31
0
ファイル: clf.py プロジェクト: paulaltin/gwpy
        group = channel.group
        if not out.has_section(group):
            out.add_section(group)
        for param, value in channel.params.iteritems():
            out.set(group, param, value)
        if channel.sample_rate:
            entry = '%s %s' % (str(channel),
                               str(channel.sample_rate.to('Hz').value))
        else:
            entry = str(channel)
        try:
            cl = out.get(group, 'channels')
        except configparser.NoOptionError:
            out.set(group, 'channels', '\n%s' % entry)
        else:
            out.set(group, 'channels', cl + '\n%s' % entry)
    if isinstance(fobj, file):
        close = False
    else:
        fobj = open(fobj, 'w')
        close = True
    out.write(fobj)
    if close:
        fobj.close()


registry.register_reader('ini', ChannelList, read_channel_list_file)
registry.register_identifier('ini', ChannelList,
                             identify_factory('.ini', '.clf'))
registry.register_writer('ini', ChannelList, write_channel_list_file)
コード例 #32
0
ファイル: tables.py プロジェクト: gitter-badger/glue-1
def astropy_tabular_data(*args, **kwargs):
    """
    Build a data set from a table. We restrict ourselves to tables
    with 1D columns.

    All arguments are passed to
        astropy.table.Table.read(...).
    """
    from distutils.version import LooseVersion
    from astropy import __version__
    if LooseVersion(__version__) < LooseVersion("0.2"):
        raise RuntimeError("Glue requires astropy >= v0.2. Please update")

    result = Data()

    # Read the table
    from astropy.table import Table

    # Add identifiers for ASCII data
    from astropy.io import registry
    if LooseVersion(__version__) < LooseVersion("0.3"):
        registry.register_identifier('ascii',
                                     Table,
                                     _ascii_identifier_v02,
                                     force=True)
    else:
        # Basically, we always want the plain ascii reader for now.
        # But astropy will complain about ambiguous formats (or use another reader)
        # unless we remove other registry identifiers and set up our own reader

        nope = lambda *a, **k: False
        registry.register_identifier('ascii.glue',
                                     Table,
                                     _ascii_identifier_v03,
                                     force=True)
        registry.register_identifier('ascii.csv', Table, nope, force=True)
        registry.register_identifier('ascii.fast_csv', Table, nope, force=True)
        registry.register_identifier('ascii', Table, nope, force=True)
        registry.register_reader('ascii.glue',
                                 Table,
                                 lambda path: Table.read(path, format='ascii'),
                                 force=True)

    try:
        table = Table.read(*args, **kwargs)
    except:
        # In Python 3, as of Astropy 0.4, if the format is not specified, the
        # automatic format identification will fail (astropy/astropy#3013).
        # This is only a problem for ASCII formats however, because it is due
        # to the fact that the file object in io.ascii does not rewind to the
        # start between guesses (due to a bug), so here we can explicitly try
        # the ASCII format if the format keyword was not already present.
        if 'format' not in kwargs:
            table = Table.read(*args, format='ascii.glue', **kwargs)
        else:
            raise

    # Loop through columns and make component list
    for column_name in table.columns:
        c = table[column_name]
        u = c.unit if hasattr(c, 'unit') else c.units

        if table.masked:
            # fill array for now
            try:
                c = c.filled(fill_value=np.nan)
            except ValueError:  # assigning nan to integer dtype
                c = c.filled(fill_value=-1)

        nc = Component.autotyped(c, units=u)
        result.add_component(nc, column_name)

    return result
コード例 #33
0
ファイル: segwizard.py プロジェクト: gitter-badger/gwpy
        data quality flag to print
    fobj : `file`, `str`
        open file object, or file path, to write to
    header : `bool`, optional
        print header into the file, default: `True`
    coltype : `type`, optional
        numerical type in which to cast times before printing

    Notes
    -----
    In this format, only the
    :attr:`~gwpy.segments.flag.DataQualityFlag.active` segments are
    printed

    See Also
    --------
    :mod:`glue.segmentsUtils`
        for definition of the segwizard format, and the to/from functions
        used in this GWpy module
    """
    to_segwizard(flag.active, fobj, header=header, coltype=coltype)


registry.register_reader('segwizard', DataQualityFlag, flag_from_segwizard)
registry.register_writer('segwizard', DataQualityFlag, flag_to_segwizard)
registry.register_identifier('segwizard', DataQualityFlag, identify_segwizard)

registry.register_reader('segwizard', SegmentList, from_segwizard)
registry.register_writer('segwizard', SegmentList, to_segwizard)
registry.register_identifier('segwizard', SegmentList, identify_segwizard)
コード例 #34
0
    #integer(kind=4) :: column_pointer(code_uvt_last) = code_null ! Back pointer to the columns...
    #integer(kind=4) :: column_size(code_uvt_last) = 0  ! Number of columns for each
    #! In the data, we instead have the codes for each column
    #! integer(kind=4) :: column_codes(nlead+ntrail)         ! Start column for each ...
    #! integer(kind=4) :: column_types(nlead+ntrail) /0,1,2/ ! Number of columns for each: 1 real*4, 2 real*8
    #! Leading / Trailing information codes
    #!
    #integer(kind=4) :: order = 0          ! 13  Stoke/Channel ordering
    #integer(kind=4) :: nfreq = 0          ! 14  ! 0 or = nchan*nstokes
    #integer(kind=4) :: atoms(4)           ! 15-18 Atom description
    #!
    #real(kind=8), pointer :: freqs(:) => null()     ! (nchan*nstokes) = 0d0
    #integer(kind=4), pointer :: stokes(:) => null() ! (nchan*nstokes) or (nstokes) = code_stoke
    #!
    #real(kind=8), pointer :: ref(:) => null()
    #real(kind=8), pointer :: val(:) => null()
    #real(kind=8), pointer :: inc(:) => null()

    lf.seek(1024)
    real_dims = dims[:ndim]
    data = np.fromfile(lf, count=np.product(real_dims),
                       dtype='float32').reshape(real_dims[::-1])
    data[data == bval] = np.nan

    return data, header


io_registry.register_reader('lmv', BaseSpectralCube, load_lmv_cube)
io_registry.register_reader('class_lmv', BaseSpectralCube, load_lmv_cube)
io_registry.register_identifier('lmv', BaseSpectralCube, is_lmv)
コード例 #35
0
        the default `~astropy.io.fits.PrimaryHDU`.

    kwd :
        All additional keywords are passed to :py:mod:`astropy.io.fits`

    Raises
    ------
    ValueError
        - If ``self.mask`` is set but not a `numpy.ndarray`.
        - If ``self.uncertainty`` is set but not a
          `~astropy.nddata.StdDevUncertainty`.
        - If ``self.uncertainty`` is set but has another unit then
          ``self.data``.

    NotImplementedError
        Saving flags is not supported.
    """
    hdu = ccd_data.to_hdu(
        hdu_mask=hdu_mask, hdu_uncertainty=hdu_uncertainty,
        key_uncertainty_type=key_uncertainty_type, hdu_flags=hdu_flags,
        as_image_hdu=as_image_hdu)
    if as_image_hdu:
        hdu.insert(0, fits.PrimaryHDU())
    hdu.writeto(filename, **kwd)


with registry.delay_doc_updates(CCDData):
    registry.register_reader('fits', CCDData, fits_ccddata_reader)
    registry.register_writer('fits', CCDData, fits_ccddata_writer)
    registry.register_identifier('fits', CCDData, fits.connect.is_fits)
コード例 #36
0
ファイル: test_connect.py プロジェクト: adivijaykumar/astropy
def setup_module(module):
    """Setup module for tests."""
    io_registry.register_reader("json", Cosmology, read_json)
    io_registry.register_writer("json", Cosmology, write_json)
    io_registry.register_identifier("json", Cosmology, json_identify)
コード例 #37
0
ファイル: test_registry.py プロジェクト: jpmorgen/astropy
def test_register_identifier_force():
    io_registry.register_identifier('test', TestData, empty_identifier)
    io_registry.register_identifier('test',
                                    TestData,
                                    empty_identifier,
                                    force=True)
コード例 #38
0
ファイル: ligolw.py プロジェクト: diegobersanetti/gwpy
        the file or document to write into

    attrs : `dict`, optional
        extra attributes to write into segment tables

    **kwargs
        keyword arguments to use when writing

    See also
    --------
    gwpy.io.ligolw.write_ligolw_tables
        for details of acceptable keyword arguments
    """
    if isinstance(flags, DataQualityFlag):
        flags = DataQualityDict({flags.name: flags})
    return write_tables(target, flags.to_ligolw_tables(**attrs or dict()),
                        **kwargs)


# -- register -----------------------------------------------------------------

# register methods for DataQualityDict
io_registry.register_reader('ligolw', DataQualityFlag, read_ligolw_flag)
io_registry.register_writer('ligolw', DataQualityFlag, write_ligolw)
io_registry.register_identifier('ligolw', DataQualityFlag, is_xml)

# register methods for DataQualityDict
io_registry.register_reader('ligolw', DataQualityDict, read_ligolw_dict)
io_registry.register_writer('ligolw', DataQualityDict, write_ligolw)
io_registry.register_identifier('ligolw', DataQualityDict, is_xml)
コード例 #39
0
ファイル: ligolw.py プロジェクト: gitter-badger/gwpy
            segsumtab.append(segsum)

    # write SegmentTable
    try:
        segtab = lsctables.SegmentTable.get_table(xmldoc)
    except ValueError:
        segtab = lsctables.New(lsctables.SegmentTable,
                               columns=[
                                   'process_id', 'segment_id',
                                   'segment_def_id', 'start_time',
                                   'start_time_ns', 'end_time', 'end_time_ns'
                               ])
        xmldoc.childNodes[-1].appendChild(segtab)
    for flag in flags.iterkeys():
        for aseg in flag.active:
            seg = lsctables.Segment()
            seg.segment_def_id = segdef.segment_def_id
            seg.set(map(LIGOTimeGPS, map(float, aseg)))
            seg.segment_id = lsctables.SegmentTable.get_next_id()
            seg.process_id = process_id
            segtab.append(seg)
    return xmldoc


registry.register_reader('ligolw', DataQualityFlag, read_flag)
registry.register_writer('ligolw', DataQualityFlag, write_ligolw)
registry.register_identifier('ligolw', DataQualityFlag, identify_ligolw)

registry.register_reader('ligolw', DataQualityDict, read_flag_dict)
registry.register_identifier('ligolw', DataQualityDict, identify_ligolw)
コード例 #40
0
ファイル: cache.py プロジェクト: paulaltin/gwpy
    cacheobj = args[3]
    if isinstance(cacheobj, Cache):
            return True
    try:
        from lal import Cache as LALCache
    except ImportError:
        pass
    else:
        if isinstance(cacheobj, LALCache):
            return True
    return False


registry.register_reader('lcf', TimeSeries, read_cache)
registry.register_reader('cache', TimeSeries, read_cache)
registry.register_identifier('lcf', TimeSeries, identify_cache_file)
registry.register_identifier('cache', TimeSeries, identify_cache)

# duplicate for state-vector
registry.register_reader('lcf', StateVector, read_state_cache)
registry.register_reader('cache', StateVector, read_state_cache)
registry.register_identifier('lcf', StateVector, identify_cache_file)
registry.register_identifier('cache', StateVector, identify_cache)

# TimeSeriesDict
registry.register_reader('lcf', TimeSeriesDict, read_dict_cache)
registry.register_reader('cache', TimeSeriesDict, read_dict_cache)
registry.register_reader('lcfmp', TimeSeriesDict, read_dict_cache)
registry.register_identifier('lcf', TimeSeriesDict, identify_cache_file)
registry.register_identifier('cache', TimeSeriesDict, identify_cache)
コード例 #41
0
ファイル: test_registry.py プロジェクト: Cadair/astropy
def test_register_identifier_invalid():
    io_registry.register_identifier('test', TestData, empty_identifier)
    with pytest.raises(io_registry.IORegistryError) as exc:
        io_registry.register_identifier('test', TestData, empty_identifier)
    assert (str(exc.value) == "Identifier for format 'test' and class "
                              "'TestData' is already defined")
コード例 #42
0
ファイル: dat.py プロジェクト: mcoughlin/gwpy
def read_dat(filepath, fcol=0, ampcol=1, **kwargs):
    """Read a `Spectrum` from a txt file
    """
    frequency, amplitude = numpy.loadtxt(filepath, usecols=[fcol, ampcol],
                                         unpack=True)
    return Spectrum(amplitude, frequencies=frequency, **kwargs)


def identify_dat(*args, **kwargs):
    """Identify the given file as a dat file, rather than anything else

    Returns
    -------
    True
        if the filename endswith .txt or .dat
    False
        otherwise
    """
    filename = args[1][0]
    if not isinstance(filename, basestring):
        filename = filename.name
    if filename.endswith('txt') or filename.endswith('dat'):
        return True
    return False


# register this file-reader with the Spectrum class
registry.register_reader('dat', Spectrum, read_dat, force=True)
registry.register_identifier('dat', Spectrum, identify_dat)
コード例 #43
0
ファイル: test_registry.py プロジェクト: Cadair/astropy
def test_write_toomanyformats():
    io_registry.register_identifier('test1', TestData, lambda o, *x, **y: True)
    io_registry.register_identifier('test2', TestData, lambda o, *x, **y: True)
    with pytest.raises(io_registry.IORegistryError) as exc:
        TestData().write()
    assert str(exc.value) == "Format is ambiguous - options are: test1, test2"
コード例 #44
0
ファイル: connect.py プロジェクト: tereyaedwards/the-office
    out = Table(list(zip(*rows)),
                names=('Format', 'Suffix', 'Write', 'Description'))
    for colname in ('Format', 'Description'):
        width = max(len(x) for x in out[colname])
        out[colname].format = '%-{0}s'.format(width)

    return out


# Specific
# ========


def read_csv(filename, **kwargs):
    from .ui import read
    kwargs['format'] = 'csv'
    return read(filename, **kwargs)


def write_csv(table, filename, **kwargs):
    from .ui import write
    kwargs['format'] = 'csv'
    return write(table, filename, **kwargs)


csv_identify = functools.partial(io_identify, '.csv')

io_registry.register_reader('csv', Table, read_csv)
io_registry.register_writer('csv', Table, write_csv)
io_registry.register_identifier('csv', Table, csv_identify)
コード例 #45
0
ファイル: nikamap.py プロジェクト: abeelen/nikamap
    return data


def identify_piic(origin, *args, **kwargs):
    data_file = Path(args[0])
    rgw_file = data_file.parent / (data_file.with_suffix("").name + "_rgw.fits")
    check = data_file.exists() & rgw_file.exists()
    if check:
        check &= fits.connect.is_fits("read", data_file.parent, data_file.open(mode="rb"))
        check &= fits.connect.is_fits("read", rgw_file.parent, rgw_file.open(mode="rb"))
    return check


with registry.delay_doc_updates(NikaMap):
    registry.register_reader("piic", NikaMap, piic_fits_nikamap_reader)
    registry.register_identifier("piic", NikaMap, identify_piic)

    registry.register_reader("idl", NikaMap, idl_fits_nikamap_reader)
    registry.register_writer("idl", NikaMap, idl_fits_nikamap_writer)
    registry.register_identifier("idl", NikaMap, fits.connect.is_fits)


class NikaFits(MutableMapping):
    def __init__(self, filename=None, **kwd):
        self._filename = filename
        self._kwd = kwd
        self.__data = {"1mm": None, "2mm": None, "1": None, "2": None, "3": None}
        with fits.open(filename, **kwd) as hdus:
            self.primary_header = hdus[0].header

    def __repr__(self):
コード例 #46
0
ファイル: hdf5.py プロジェクト: gitter-badger/gwpy
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GWpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWpy.  If not, see <http://www.gnu.org/licenses/>.
"""This module attaches the HDF5 input output methods to the Spectrogram.

While these methods are avialable as methods of the class itself,
this module attaches them to the unified I/O registry, making it a bit
cleaner.
"""

from astropy.io.registry import (register_reader, register_writer,
                                 register_identifier)

from ... import version
from ...io.hdf5 import identify_hdf5
from ..core import Spectrogram

__author__ = 'Duncan Macleod <*****@*****.**>'
__version__ = version.version

register_reader('hdf', Spectrogram, Spectrogram.from_hdf5)
register_writer('hdf', Spectrogram, Spectrogram.to_hdf5)
register_identifier('hdf', Spectrogram, identify_hdf5)
コード例 #47
0
ファイル: connect.py プロジェクト: Cadair/astropy
    try:
        import asdf
    except ImportError:
        raise Exception(
            "The asdf module is required to read and write ASDF files")

    if data_key and make_tree:
        raise ValueError("Options 'data_key' and 'make_tree' are not compatible")

    if make_tree:
        tree = make_tree(table)
    else:
        tree = { data_key or 'data' : table }

    with asdf.AsdfFile(tree) as af:
        af.write_to(filename, **kwargs)


def asdf_identify(origin, filepath, fileobj, *args, **kwargs):
    try:
        import asdf
    except ImportError:
        return False

    return filepath is not None and filepath.endswith('.asdf')


io_registry.register_reader('asdf', Table, read_table)
io_registry.register_writer('asdf', Table, write_table)
io_registry.register_identifier('asdf', Table, asdf_identify)
コード例 #48
0
        the file or document to write into

    attrs : `dict`, optional
        extra attributes to write into segment tables

    **kwargs
        keyword arguments to use when writing

    See also
    --------
    gwpy.io.ligolw.write_ligolw_tables
        for details of acceptable keyword arguments
    """
    if isinstance(flags, DataQualityFlag):
        flags = DataQualityDict({flags.name: flags})
    return write_tables(target, flags.to_ligolw_tables(**attrs or dict()),
                        **kwargs)


# -- register -----------------------------------------------------------------

# register methods for DataQualityDict
io_registry.register_reader('ligolw', DataQualityFlag, read_ligolw_flag)
io_registry.register_writer('ligolw', DataQualityFlag, write_ligolw)
io_registry.register_identifier('ligolw', DataQualityFlag, is_xml)

# register methods for DataQualityDict
io_registry.register_reader('ligolw', DataQualityDict, read_ligolw_dict)
io_registry.register_writer('ligolw', DataQualityDict, write_ligolw)
io_registry.register_identifier('ligolw', DataQualityDict, is_xml)
コード例 #49
0
ファイル: tableio.py プロジェクト: AHorneffer/LSMTool
    """
    Returns an empty sky model table.
    """
    outlines = ['Z, Z, 0.0, 0.0, 0.0\n']
    colNames = ['Name', 'Type', 'Ra', 'Dec', 'I']
    converters = {}
    nameCol = 'col{0}'.format(colNames.index('Name') + 1)
    converters[nameCol] = [ascii.convert_numpy('{}100'.format(numpy_type))]
    typeCol = 'col{0}'.format(colNames.index('Type') + 1)
    converters[typeCol] = [ascii.convert_numpy('{}100'.format(numpy_type))]
    table = Table.read(outlines,
                       guess=False,
                       format='ascii.no_header',
                       delimiter=',',
                       names=colNames,
                       comment='#',
                       data_start=0,
                       converters=converters)
    table.remove_rows(0)
    return table


# Register the file reader, identifier, and writer functions with astropy.io
registry.register_reader('makesourcedb', Table, skyModelReader)
registry.register_identifier('makesourcedb', Table, skyModelIdentify)
registry.register_writer('makesourcedb', Table, skyModelWriter)
registry.register_writer('ds9', Table, ds9RegionWriter)
registry.register_writer('kvis', Table, kvisAnnWriter)
registry.register_writer('casa', Table, casaRegionWriter)
registry.register_writer('factor', Table, factorDirectionsWriter)
コード例 #50
0
    'PathlossModel', 'PersistenceSatModel', 'PixelAreaModel',
    'NirspecSlitAreaModel', 'NirspecMosAreaModel', 'NirspecIfuAreaModel',
    'FgsImgPhotomModel', 'MirImgPhotomModel', 'MirLrsPhotomModel',
    'MirMrsPhotomModel', 'NrcImgPhotomModel', 'NrcWfssPhotomModel',
    'NisImgPhotomModel', 'NisSossPhotomModel', 'NisWfssPhotomModel',
    'NrsFsPhotomModel', 'NrsMosPhotomModel', 'PsfMaskModel', 'QuadModel',
    'RampModel', 'MIRIRampModel', 'RampFitOutputModel', 'ReadnoiseModel',
    'ReferenceFileModel', 'ReferenceCubeModel', 'ReferenceImageModel',
    'ReferenceQuadModel', 'RegionsModel', 'ResetModel', 'ResolutionModel',
    'MiriResolutionModel', 'RSCDModel', 'SaturationModel', 'SlitDataModel',
    'SlitModel', 'SpecModel', 'SourceModelContainer', 'StepParsModel',
    'StrayLightModel', 'SuperBiasModel', 'SpecwcsModel', 'ThroughputModel',
    'TrapDensityModel', 'TrapParsModel', 'TrapsFilledModel', 'TsoPhotModel',
    'WavelengthrangeModel', 'WaveCorrModel', 'WfssBkgModel'
]

# Initialize the astropy.io registry,
# but only the first time this module is called

try:
    _defined_models
except NameError:
    with registry.delay_doc_updates(DataModel):
        registry.register_reader('datamodel', DataModel, ndmodel.read)
        registry.register_writer('datamodel', DataModel, ndmodel.write)
        registry.register_identifier('datamodel', DataModel, ndmodel.identify)

_all_models = __all__[1:]
_local_dict = locals()
_defined_models = {k: _local_dict[k] for k in _all_models}
コード例 #51
0
ファイル: ccddata.py プロジェクト: MSeifert04/ccdproc
    Raises
    -------
    ValueError
        - If ``self.mask`` is set but not a `numpy.ndarray`.
        - If ``self.uncertainty`` is set but not a
          `~astropy.nddata.StdDevUncertainty`.
        - If ``self.uncertainty`` is set but has another unit then
          ``self.data``.

    NotImplementedError
        Saving flags is not supported.
    """
    hdu = ccd_data.to_hdu(hdu_mask=hdu_mask, hdu_uncertainty=hdu_uncertainty,
                          hdu_flags=hdu_flags)
    hdu.writeto(filename, **kwd)


registry.register_reader('fits', CCDData, fits_ccddata_reader)
registry.register_writer('fits', CCDData, fits_ccddata_writer)
registry.register_identifier('fits', CCDData, fits.connect.is_fits)

try:
    CCDData.read.__doc__ = fits_ccddata_reader.__doc__
except AttributeError:
    CCDData.read.__func__.__doc__ = fits_ccddata_reader.__doc__

try:
    CCDData.write.__doc__ = fits_ccddata_writer.__doc__
except AttributeError:
    CCDData.write.__func__.__doc__ = fits_ccddata_writer.__doc__
コード例 #52
0
                                line['equivalent_width'])
                else:
                    EW = space
                f.write(
                    fmt.format(line['wavelength'], line['species'],
                               line['expot'], line['loggf'], C6, D0, EW,
                               line['comments']) + "\n")

    def write_latex(self,
                    filename,
                    sortby=['species', 'wavelength'],
                    write_cols=['wavelength', 'element', 'expot', 'loggf']):
        new_table = self.copy()
        new_table.sort(sortby)
        new_table = new_table[write_cols]
        new_table.write(filename, format='ascii.aastex')


## Add to astropy.io registry
def _moog_identifier(*args, **kwargs):
    try:  # python 2
        return isinstance(args[0],
                          basestring) and args[0].lower().endswith(".moog")
    except:  # python 3
        return isinstance(args[0], str) and args[0].lower().endswith(".moog")


registry.register_writer("moog", LineList, LineList.write_moog)
registry.register_reader("moog", LineList, LineList.read_moog)
registry.register_identifier("moog", LineList, _moog_identifier)
コード例 #53
0
ファイル: data_factories.py プロジェクト: DougBurke/glue
def astropy_tabular_data(*args, **kwargs):
    """
    Build a data set from a table. We restrict ourselves to tables
    with 1D columns.

    All arguments are passed to
        astropy.table.Table.read(...).
    """
    from distutils.version import LooseVersion
    from astropy import __version__
    if LooseVersion(__version__) < LooseVersion("0.2"):
        raise RuntimeError("Glue requires astropy >= v0.2. Please update")

    result = Data()

    # Read the table
    from astropy.table import Table

    # Add identifiers for ASCII data
    from astropy.io import registry
    if LooseVersion(__version__) < LooseVersion("0.3"):
        registry.register_identifier('ascii', Table, _ascii_identifier_v02,
                                     force=True)
    else:
        # Basically, we always want the plain ascii reader for now.
        # But astropy will complain about ambiguous formats (or use another reader)
        # unless we remove other registry identifiers and set up our own reader

        nope = lambda *a, **k: False
        registry.register_identifier('ascii.glue', Table, _ascii_identifier_v03,
                                     force=True)
        registry.register_identifier('ascii.csv', Table, nope, force=True)
        registry.register_identifier('ascii.fast_csv', Table, nope, force=True)
        registry.register_identifier('ascii', Table, nope, force=True)
        registry.register_reader('ascii.glue', Table,
                                 lambda path: Table.read(path, format='ascii'),
                                 force=True)

    # Import FITS compatibility (for Astropy 0.2.x)
    from ..external import fits_io

    try:
        table = Table.read(*args, **kwargs)
    except:
        # In Python 3, as of Astropy 0.4, if the format is not specified, the 
        # automatic format identification will fail (astropy/astropy#3013). 
        # This is only a problem for ASCII formats however, because it is due 
        # to the fact that the file object in io.ascii does not rewind to the 
        # start between guesses (due to a bug), so here we can explicitly try 
        # the ASCII format if the format keyword was not already present.
        if 'format' not in kwargs:
            table = Table.read(*args, format='ascii.glue', **kwargs)
        else:
            raise

    # Loop through columns and make component list
    for column_name in table.columns:
        c = table[column_name]
        u = c.unit if hasattr(c, 'unit') else c.units

        if table.masked:
            # fill array for now
            try:
                c = c.filled(fill_value=np.nan)
            except ValueError:  # assigning nan to integer dtype
                c = c.filled(fill_value=-1)

        nc = Component.autotyped(c, units=u)
        result.add_component(nc, column_name)

    return result
コード例 #54
0
from astropy.table import Table
from astropy.io.registry import (register_identifier, register_reader,
                                 register_writer)
from pydl.pydlutils.yanny import (is_yanny, read_table_yanny,
                                  write_table_yanny, yanny)
from pathlib import Path
import numpy as np
import ppv.config

register_identifier('yanny', Table, is_yanny)
register_reader('yanny', Table, read_table_yanny)
register_writer('yanny', Table, write_table_yanny)

platePlans = Table.read('../data/raw/platePlans.par', format='yanny',
                tablename='PLATEPLANS')

print('platePlans.par is read')

is_mwm_plate = np.array(['mwm' in prun for prun in platePlans['platerun']])
is_bhm_plate = np.array(['bhm' in prun for prun in platePlans['platerun']])
is_sdss5_plate = np.bitwise_or(is_mwm_plate, is_bhm_plate)


sdss5_plates = platePlans[is_sdss5_plate]
# parent in root directory of repository

dir_ = (Path.cwd().parent / ppv.config._src_dir) / 'data'
out_filename = (dir_ / 'platePlans_sdss5.fits').as_posix()

sdss5_plates.write(out_filename, overwrite='True', format='fits')
print(f'SDSS-V platePlans table written to {out_filename}')
コード例 #55
0
ファイル: ligolw.py プロジェクト: paulaltin/gwpy
        for vseg in flag.known:
            segsum = lsctables.SegmentSum()
            segsum.segment_def_id = segdef.segment_def_id
            segsum.set(map(LIGOTimeGPS, map(float, vseg)))
            segsum.comment = None
            segsum.segment_sum_id = lsctables.SegmentSumTable.get_next_id()
            segsum.process_id = process_id
            segsumtab.append(segsum)

        # write segment table (active segments)
        for aseg in flag.active:
            seg = lsctables.Segment()
            seg.segment_def_id = segdef.segment_def_id
            seg.set(map(LIGOTimeGPS, map(float, aseg)))
            seg.segment_id = lsctables.SegmentTable.get_next_id()
            seg.process_id = process_id
            segtab.append(seg)

    return xmldoc


# register methods for DataQualityDict
registry.register_reader('ligolw', DataQualityFlag, read_flag)
registry.register_writer('ligolw', DataQualityFlag, write_ligolw)
registry.register_identifier('ligolw', DataQualityFlag, identify_ligolw)

# register methods for DataQualityDict
registry.register_reader('ligolw', DataQualityDict, read_flag_dict)
registry.register_writer('ligolw', DataQualityDict, write_ligolw)
registry.register_identifier('ligolw', DataQualityDict, identify_ligolw)
コード例 #56
0
ファイル: registers.py プロジェクト: chris-simpson/specutils
    def decorator(func):
        io_registry.register_reader(label, dtype, func)

        if identifier is None:
            # If the identifier is not defined, but the extensions are, create
            # a simple identifier based off file extension.
            if extensions is not None:
                logging.info("'{}' data loader provided for {} without "
                             "explicit identifier. Creating identifier using "
                             "list of compatible extensions".format(
                                 label, dtype.__name__))
                id_func = lambda *args, **kwargs: any(
                    [args[1].endswith(x) for x in extensions])
            # Otherwise, create a dummy identifier
            else:
                logging.warning("'{}' data loader provided for {} without "
                                "explicit identifier or list of compatible "
                                "extensions".format(label, dtype.__name__))
                id_func = lambda *args, **kwargs: True
        else:
            id_func = identifier_wrapper(identifier)

        io_registry.register_identifier(label, dtype, id_func)

        # Include the file extensions as attributes on the function object
        func.extensions = extensions

        # Include priority on the loader function attribute
        func.priority = priority

        # Sort the io_registry based on priority
        sorted_loaders = sorted(
            io_registry._readers.items(),
            key=lambda item: getattr(item[1], 'priority', 0))

        # Update the registry with the sorted dictionary
        io_registry._readers.clear()
        io_registry._readers.update(sorted_loaders)

        logging.debug("Successfully loaded reader \"{}\".".format(label))

        # Automatically register a SpectrumList reader for any data_loader that
        # reads Spectrum1D objects. TODO: it's possible that this
        # functionality should be opt-in rather than automatic.
        if dtype is Spectrum1D:

            def load_spectrum_list(*args, **kwargs):
                return SpectrumList([func(*args, **kwargs)])

            # Add these attributes to the SpectrumList reader as well
            load_spectrum_list.extensions = extensions
            load_spectrum_list.priority = priority

            io_registry.register_reader(label, SpectrumList,
                                        load_spectrum_list)
            io_registry.register_identifier(label, SpectrumList, id_func)
            logging.debug(
                "Created SpectrumList reader for \"{}\".".format(label))

        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)

        return wrapper
コード例 #57
0
    output : str
        The filename to write the table to.
    overwrite : bool
        Whether to overwrite any existing file without warning.
    append : bool
        Whether to append the table to an existing file
    """

    # Encode any mixin columns into standard Columns.
    input = _encode_mixins(input)

    table_hdu = table_to_hdu(input, character_as_bytes=True)

    # Check if output file already exists
    if isinstance(output, str) and os.path.exists(output):
        if overwrite:
            os.remove(output)
        elif not append:
            raise OSError(f"File exists: {output}")

    if append:
        # verify=False stops it reading and checking the existing file.
        fits_append(output, table_hdu.data, table_hdu.header, verify=False)
    else:
        table_hdu.writeto(output)


io_registry.register_reader('fits', Table, read_table_fits)
io_registry.register_writer('fits', Table, write_table_fits)
io_registry.register_identifier('fits', Table, is_fits)
コード例 #58
0
    # append row by row
    names = table.dtype.names
    for row in table:
        rowd = dict((n, row[n]) for n in names)
        gps = LIGOTimeGPS(rowd.pop('time', 0))
        frame.AppendFrEvent(
            FrEvent(
                str(name),
                str(rowd.pop('comment', '')),
                str(rowd.pop('inputs', '')),
                GPSTime(gps.gpsSeconds, gps.gpsNanoSeconds),
                float(rowd.pop('timeBefore', 0)),
                float(rowd.pop('timeAfter', 0)),
                int(rowd.pop('eventStatus', 0)),
                float(rowd.pop('amplitude', 0)),
                float(rowd.pop('probability', -1)),
                str(rowd.pop('statistics', '')),
                list(rowd.items()),  # remaining params as tuple
            ))

    # write frame to file
    io_gwf.write_frames(filename, [frame], **write_kw)


# -- registration -------------------------------------------------------------

for table_class in (Table, EventTable):
    io_registry.register_reader('gwf', table_class, table_from_gwf)
    io_registry.register_writer('gwf', table_class, table_to_gwf)
    io_registry.register_identifier('gwf', table_class, io_gwf.identify_gwf)
コード例 #59
0
ファイル: tableio.py プロジェクト: nudomarinero/LSMTool
    return outFile


def makeEmptyTable():
    """
    Returns an empty sky model table.
    """
    outlines = ['Z, Z, 0.0, 0.0, 0.0\n']
    colNames = ['Name', 'Type', 'Ra', 'Dec', 'I']
    converters = {}
    nameCol = 'col{0}'.format(colNames.index('Name')+1)
    converters[nameCol] = [ascii.convert_numpy('{}100'.format(numpy_type))]
    typeCol = 'col{0}'.format(colNames.index('Type')+1)
    converters[typeCol] = [ascii.convert_numpy('{}100'.format(numpy_type))]
    table = Table.read(outlines, guess=False, format='ascii.no_header', delimiter=',',
        names=colNames, comment='#', data_start=0, converters=converters)
    table.remove_rows(0)
    return table


# Register the file reader, identifier, and writer functions with astropy.io
registry.register_reader('makesourcedb', Table, skyModelReader)
registry.register_identifier('makesourcedb', Table, skyModelIdentify)
registry.register_writer('makesourcedb', Table, skyModelWriter)
registry.register_writer('ds9', Table, ds9RegionWriter)
registry.register_writer('kvis', Table, kvisAnnWriter)
registry.register_writer('casa', Table, casaRegionWriter)
registry.register_writer('factor', Table, factorDirectionsWriter)

コード例 #60
0
ファイル: connect.py プロジェクト: lpsinger/astropy
    except ImportError:
        raise Exception(
            "The asdf module is required to read and write ASDF files")

    if data_key and make_tree:
        raise ValueError(
            "Options 'data_key' and 'make_tree' are not compatible")

    if make_tree:
        tree = make_tree(table)
    else:
        tree = {data_key or 'data': table}

    with asdf.AsdfFile(tree) as af:
        af.write_to(filename, **kwargs)


def asdf_identify(origin, filepath, fileobj, *args, **kwargs):
    try:
        import asdf
    except ImportError:
        return False

    return filepath is not None and filepath.endswith('.asdf')


if not optional_deps.HAS_ASDF_ASTROPY:
    io_registry.register_reader('asdf', Table, read_table)
    io_registry.register_writer('asdf', Table, write_table)
    io_registry.register_identifier('asdf', Table, asdf_identify)