def decorator(func): io_registry.register_writer(label, Spectrum1D, func) @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper
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
def __init__(self): self.mjdMid = None self.mjdSpan = None self.tStart = None self.tStop = None self.ncoeff = None self.coeffs = None self.obs = None self.fileName = None self.fileFormat = None self.newFileName = None self.polycoTable = None self.polycoFormat = [{'format': 'tempo', 'read_method' : tempo_polyco_table_reader, 'write_method' : tempo_polyco_table_writer},] # Register the table built-in reading and writing format for fmt in self.polycoFormat: if fmt['format'] not in registry.get_formats()['Format']: if fmt['read_method'] != None: registry.register_reader(fmt['format'], table.Table, fmt['read_method']) if fmt['write_method'] != None: registry.register_writer(fmt['format'], table.Table, fmt['write_method'])
def add_polyco_file_format(self, formatName, methodMood, readMethod = None, writeMethod = None): """ Add a polyco file format and its reading/writing method to the class. Then register it to the table reading. Parameters --------- formatName : str The name for the format. methodMood : str ['r','w','rw']. 'r' represent as reading 'w' represent as writting 'rw' represent as reading and writting readMethod : method The method for reading the file format. writeMethod : method The method for writting the file to disk. """ # Check if the format already exist. if (formatName in [f['format'] for f in self.polycoFormat] or formatName in registry.get_formats()['Format']): errorMssg = 'Format name \''+formatName+ '\' is already exist. ' raise Exception(errorMssg) pFormat = {'format' : formatName} if methodMood == 'r': if readMethod == None: raise BaseException('Argument readMethod should not be \'None\'.') pFormat['read_method'] = readMethod pFormat['write_method'] = writeMethod registry.register_reader(pFormat['format'], table.Table, pFormat['read_method']) elif methodMood == 'w': if writeMethod == None: raise BaseException('Argument writeMethod should not be \'None\'.') pFormat['read_method'] = readMethod pFormat['write_method'] = writeMethod registry.register_writer(pFormat['format'], table.Table, pFormat['write_method']) elif methodMood == 'rw': if readMethod == None or writeMethod == None: raise BaseException('Argument readMethod and writeMethod ' 'should not be \'None\'.') pFormat['read_method'] = readMethod pFormat['write_method'] = writeMethod registry.register_reader(pFormat['format'], table.Table, pFormat['read_method']) registry.register_writer(pFormat['format'], table.Table, pFormat['write_method']) self.polycoFormat.append(pFormat)
def export_data_item(self, data_item, filename, fmt): """ Exports the currently selected data item to an ECSV file. Parameters ---------- data_item : `~specviz.core.items.PlotDataItem` Data item containing the spectrum to be exported to disk filename : `str` Path of the file to be created on export fmt : `str` Format to be used by IO registry for writing `~specutils.Spectrum1D` """ # TODO: the current release of specutils doesn't support exporting # very well (it's untested, and probably does not match the attributes # of the Spectrum1D object). So, create some temporary export formats. def generic_export(spectrum, path): """ Creates a temporary export format for use in writing out data. """ from astropy.table import QTable import astropy.units as u data = { 'spectral_axis': spectrum.spectral_axis, 'flux': spectrum.flux, 'mask': spectrum.mask if spectrum.mask is not None else u.Quantity(np.ones(spectrum.spectral_axis.shape)) } if spectrum.uncertainty is not None: data['uncertainty'] = spectrum.uncertainty.array * spectrum.uncertainty.unit meta = {} if spectrum.meta is not None and 'header' in spectrum.meta: meta.update({'header': {k: v for k, v in spectrum.meta['header'].items()}}) tab = QTable(data, meta=meta) tab.write(path, format='ascii.ecsv') # Below should be used when specutils has proper write capabilities # all_formats = io_registry.get_formats(Spectrum1D)['Format'].data # writable_formats = io_registry.get_formats(Spectrum1D)['Write'].data # # write_mask = [True if x == 'Yes' else False for x in writable_formats] # all_formats = all_formats[np.array(write_mask)] # all_filters = ";;".join(list(all_formats)) try: io_registry.register_writer('*.ecsv', Spectrum1D, generic_export) except io_registry.IORegistryError: pass data_item.data_item.spectrum.write(filename, format=fmt)
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)
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_)
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)
def test_register_writer(): io_registry.register_writer('test1', TestData, empty_writer) io_registry.register_writer('test2', TestData, empty_writer) assert io_registry.get_writer('test1', TestData) == empty_writer assert io_registry.get_writer('test2', TestData) == empty_writer io_registry.unregister_writer('test1', TestData) with pytest.raises(io_registry.IORegistryError): io_registry.get_writer('test1', TestData) assert io_registry.get_writer('test2', TestData) == empty_writer io_registry.unregister_writer('test2', TestData) with pytest.raises(io_registry.IORegistryError): io_registry.get_writer('test2', TestData)
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'")
# 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)
# 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)
tabledata_format : str, optional 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)
""" 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)
def write_moog(self,filename): fmt = "{:10.3f}{:10.5f}{:10.3f}{:10.3f}{}{}{}{}" space = " "*10 with open(filename,'w') as f: f.write("\n") for line in self: C6 = space if np.ma.is_masked(line['damp_vdw']) or np.isnan(line['damp_vdw']) else "{:10.3f}".format(line['damp_vdw']) D0 = space if np.ma.is_masked(line['dissoc_E']) or np.isnan(line['dissoc_E']) else "{:10.3f}".format(line['dissoc_E']) comments = '' if np.ma.is_masked(line['comments']) else line['comments'] if 'equivalent_width' in line.colnames: EW = space if np.ma.is_masked(line['equivalent_width']) or np.isnan(line['equivalent_width']) else "{:10.3f}".format(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): return isinstance(args[0], basestring) 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)
def test_write_return(): """Most writers will return None, but other values are not forbidden.""" io_registry.register_writer('test', TestData, lambda *args: True) res = TestData.write(TestData(), format="test") assert res is True
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__
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(ilwdchar_compat=ilwdchar_compat, **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)
'NIRISSGrismModel', 'OutlierParsModel', 'PathlossModel', 'PersistenceSatModel', 'PixelAreaModel', 'NirspecSlitAreaModel', 'NirspecMosAreaModel', 'NirspecIfuAreaModel', 'PhotomModel', 'FgsPhotomModel', 'MiriImgPhotomModel', 'MiriMrsPhotomModel', 'NircamPhotomModel', 'NirissPhotomModel', 'NirspecPhotomModel', 'NirspecFSPhotomModel', 'PsfMaskModel', 'QuadModel', 'RampModel', 'MIRIRampModel', 'RampFitOutputModel', 'ReadnoiseModel', 'ReferenceFileModel', 'ReferenceCubeModel', 'ReferenceImageModel', 'ReferenceQuadModel', 'RegionsModel', 'ResetModel', 'ResolutionModel', 'MiriResolutionModel', 'RSCDModel', 'SaturationModel', 'SlitDataModel', 'SlitModel', 'SpecModel', 'SourceModelContainer', '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 = dict(locals()) _defined_models = {k: _local_dict[k] for k in _all_models}
def add_polyco_file_format(self, formatName, methodMood, readMethod=None, writeMethod=None): """ Add a polyco file format and its reading/writing method to the class. Then register it to the table reading. Parameters --------- formatName : str The name for the format. methodMood : str ['r','w','rw']. 'r' represent as reading 'w' represent as writting 'rw' represent as reading and writting readMethod : method The method for reading the file format. writeMethod : method The method for writting the file to disk. """ # Check if the format already exist. if (formatName in [f["format"] for f in self.polycoFormat] or formatName in registry.get_formats()["Format"]): errorMssg = "Format name '" + formatName + "' is already exist. " raise ValueError(errorMssg) pFormat = {"format": formatName} if methodMood == "r": if readMethod is None: raise ValueError("Argument readMethod should not be 'None'.") pFormat["read_method"] = readMethod pFormat["write_method"] = writeMethod registry.register_reader(pFormat["format"], table.Table, pFormat["read_method"]) elif methodMood == "w": if writeMethod is None: raise ValueError("Argument writeMethod should not be 'None'.") pFormat["read_method"] = readMethod pFormat["write_method"] = writeMethod registry.register_writer(pFormat["format"], table.Table, pFormat["write_method"]) elif methodMood == "rw": if readMethod is None or writeMethod is None: raise ValueError("Argument readMethod and writeMethod " "should not be 'None'.") pFormat["read_method"] = readMethod pFormat["write_method"] = writeMethod registry.register_reader(pFormat["format"], table.Table, pFormat["read_method"]) registry.register_writer(pFormat["format"], table.Table, pFormat["write_method"]) self.polycoFormat.append(pFormat)
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)
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)
# Get defaults and then override with user-supplied values write_kwargs = PANDAS_FMTS[pandas_fmt]['write'].copy() write_kwargs.update(kwargs) df = tbl.to_pandas() write_method = getattr(df, 'to_' + pandas_fmt) if not overwrite: try: # filespec is not always a path-like exists = os.path.exists(filespec) except TypeError: # skip invalid arguments pass else: if exists: # only error if file already exists raise OSError(NOT_OVERWRITING_MSG.format(filespec)) return write_method(filespec, **write_kwargs) for pandas_fmt, defaults in PANDAS_FMTS.items(): fmt = PANDAS_PREFIX + pandas_fmt # Full format specifier if 'read' in defaults: func = functools.partial(_pandas_read, fmt) io_registry.register_reader(fmt, Table, func) if 'write' in defaults: func = functools.partial(_pandas_write, fmt) io_registry.register_writer(fmt, Table, func)
) # 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)
from time import sleep from collections import OrderedDict import numpy as np from astropy.tests.helper import catch_warnings, raises from astropy.utils.data import get_pkg_data_filename from astropy.table import Table from astropy.io.registry import (register_identifier, register_reader, register_writer) from .. import PydlutilsException, PydlutilsUserWarning from ..yanny import (write_ndarray_to_yanny, yanny, is_yanny, read_table_yanny, write_table_yanny) register_identifier('yanny', Table, is_yanny) register_reader('yanny', Table, read_table_yanny) register_writer('yanny', Table, write_table_yanny) class YannyTestCase(object): """Based on astropy.io.fits.tests.FitsTestCase. """ save_temp = False def setup(self): self.temp_dir = mkdtemp(prefix='yanny-test-') # Ignore deprecation warnings--this only affects Python 2.5 and 2.6, # since deprecation warnings are ignored by defualt on 2.7 warnings.simplefilter('ignore') warnings.simplefilter('always', UserWarning) # raise ValueError("I am setting up a subclass of YannyTestCase!") with open(get_pkg_data_filename("t/yanny_data.json")) as js:
if table_id is None: table_id = f'table{id(table)}' jskwargs = jskwargs or {} jsv = JSViewer(**jskwargs) sortable_columns = [ i for i, col in enumerate(table.columns.values()) if col.info.dtype.kind in 'iufc' ] html_options = { 'table_id': table_id, 'table_class': table_class, 'css': css, 'cssfiles': jsv.css_urls, 'jsfiles': jsv.jquery_urls, 'js': jsv.html_js(table_id=table_id, sort_columns=sortable_columns) } if htmldict: html_options.update(htmldict) if max_lines < len(table): table = table[:max_lines] table.write(filename, format='html', htmldict=html_options, overwrite=overwrite) io_registry.register_writer('jsviewer', Table, write_table_jsviewer)
def test_register_writer_invalid(): io_registry.register_writer('test', TestData, empty_writer) with pytest.raises(io_registry.IORegistryError) as exc: io_registry.register_writer('test', TestData, empty_writer) assert (str(exc.value) == "Writer for format 'test' and class 'TestData' " "is already defined")
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)
def test_register_writer_force(): io_registry.register_writer('test', TestData, empty_writer) io_registry.register_writer('test', TestData, empty_writer, force=True)
# 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)
if channel.frametype: params.setdefault('frameType', channel.frametype) if channel.sample_rate: params.setdefault('sampleFrequency', channel.sample_rate.to('Hz').value) if channel.frequency_range: low, hi = channel.frequency_range.to('Hz').value params.setdefault('searchFrequencyRange', (low, hi)) if 'qlow' in params or 'qhigh' in params: qlow = params.pop('qlow', 'sqrt(11)') qhigh = params.pop('qhigh', 64) params.setdefault('searchQRange', (qlow, qhigh)) # write params for key in ['channelName', 'frameType']: if key not in params: raise KeyError("No %r defined for %s" % (key, str(channel))) for key, value in params.iteritems(): key = '%s:' % str(key) if isinstance(value, tuple): value = '[%s]' % ' '.join(map(str, value)) elif isinstance(value, float) and value.is_integer(): value = int(value) elif isinstance(value, str): value = repr(value) print(' {0: <30} {1}'.format(key, value), file=file) print('}', file=file) # -- registry ----------------------------------------------------------------- registry.register_reader('omega-scan', ChannelList, read_omega_scan_config) registry.register_writer('omega-scan', ChannelList, write_omega_scan_config)
# 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 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 .. import TimeSeries, 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)
# (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 Spectrum. While these methods are available 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 Spectrum __author__ = 'Duncan Macleod <*****@*****.**>' __version__ = version.version register_reader('hdf', Spectrum, Spectrum.from_hdf5) register_writer('hdf', Spectrum, Spectrum.to_hdf5) register_identifier('hdf', Spectrum, identify_hdf5)
def read_asciitable(filename, **kwargs): from .ui import read return read(filename, **kwargs) io_registry.register_reader('ascii', Table, read_asciitable) def write_asciitable(table, filename, **kwargs): from .ui import write return write(table, filename, **kwargs) io_registry.register_writer('ascii', Table, write_asciitable) def io_read(format, filename, **kwargs): from .ui import read format = re.sub(r'^ascii\.', '', format) return read(filename, format=format, **kwargs) def io_write(format, table, filename, **kwargs): from .ui import write format = re.sub(r'^ascii\.', '', format) return write(table, filename, format=format, **kwargs) def io_identify(suffix, origin, filepath, fileobj, *args, **kwargs):
def write_table_jsviewer(table, filename, table_id=None, max_lines=5000, table_class="display compact", jskwargs=None, css=DEFAULT_CSS, htmldict=None): if table_id is None: table_id = 'table{id}'.format(id=id(table)) jskwargs = jskwargs or {} jsv = JSViewer(**jskwargs) sortable_columns = [i for i, col in enumerate(table.columns.values()) if col.dtype.kind in 'iufc'] html_options = { 'table_id': table_id, 'table_class': table_class, 'css': css, 'cssfiles': jsv.css_urls, 'jsfiles': jsv.jquery_urls, 'js': jsv.html_js(table_id=table_id, sort_columns=sortable_columns) } if htmldict: html_options.update(htmldict) if max_lines < len(table): table = table[:max_lines] table.write(filename, format='html', htmldict=html_options) io_registry.register_writer('jsviewer', Table, write_table_jsviewer)
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)
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)
mycosmo = file_reader(filename) # ← read file ↓ build Cosmology return Cosmology.from_format(mycosmo, format="mypackage", **kwargs) def write_myformat(cosmology, file, *, overwrite=False): """Write files in format 'myformat'. Parameters ---------- cosmology : `~astropy.cosmology.Cosmology` instance file : str, bytes, or `~os.PathLike` overwrite : bool (optional, keyword-only) Whether to overwrite an existing file. Default is False. """ cosmo = cosmology.to_format( "mypackage") # ← convert Cosmology ↓ write file file_writer(file, cosmo, overwrite=overwrite) def myformat_identify(origin, filepath, fileobj, *args, **kwargs): """Identify if object uses ``myformat`` (JSON).""" return filepath is not None and filepath.endswith(".myformat") # ------------------------------------------------------------------- # Register read/write/identify methods with Astropy Unified I/O io_registry.register_reader("myformat", Cosmology, read_myformat) io_registry.register_writer("myformat", Cosmology, write_myformat) io_registry.register_identifier("myformat", Cosmology, myformat_identify)
def is_fits(origin, filepath, fileobj, *args, **kwargs): """ Wrapper around astropy.io.fits.connect.is_fits that handles the extra extension. Can be removed if fts is added to astropy.io as a recognized FITS extension. """ 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) 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__
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) {} 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(f"File exists: {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)
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): return "<NIKAFits(filename={})>".format(self._filename) def __getitem__(self, key):
if is_column_keyword(key.upper()) or key.upper() in REMOVE_KEYWORDS: log.warn("Meta-data keyword {0} will be ignored since it " "conflicts with a FITS reserved keyword".format(key)) if isinstance(value, list): for item in value: try: table_hdu.header.append((key, item)) except ValueError: log.warn("Attribute `{0}` of type {1} cannot be written " "to FITS files - skipping".format(key, type(value))) else: try: table_hdu.header[key] = value except ValueError: log.warn("Attribute `{0}` of type {1} cannot be written to " "FITS files - skipping".format(key, type(value))) # Write out file table_hdu.writeto(output) try: 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) except: # FITS readers/writers have already been registered pass
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(NOT_OVERWRITING_MSG.format(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)
# 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)
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}')
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)
"""Writer function which writes the NDCube object to FITS file 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)
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)
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)
# 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)
def add_polyco_file_format(self, formatName, methodMood, readMethod=None, writeMethod=None): """ Add a polyco file format and its reading/writting method to the class. Then register it to the table reading. Parameters --------- formatName : str The name for the format. methodMood : str ['r','w','rw']. 'r' represent as reading 'w' represent as writting 'rw' represent as reading and writting readMethod : method The method for reading the file format. writeMethod : method The method for writting the file to disk. """ # Check if the format already exist. if (formatName in [f['format'] for f in self.polycoFormat] or formatName in registry.get_formats()['Format']): errorMssg = 'Format name \'' + formatName + '\' is already exist. ' raise Exception(errorMssg) pFormat = {'format': formatName} if methodMood == 'r': if readMethod == None: raise BaseException( 'Argument readMethod should not be \'None\'.') pFormat['read_method'] = readMethod pFormat['write_method'] = writeMethod registry.register_reader(pFormat['format'], table.Table, pFormat['read_method']) elif methodMood == 'w': if writeMethod == None: raise BaseException( 'Argument writeMethod should not be \'None\'.') pFormat['read_method'] = readMethod pFormat['write_method'] = writeMethod registry.register_writer(pFormat['format'], table.Table, pFormat['write_method']) elif methodMood == 'rw': if readMethod == None or writeMethod == None: raise BaseException('Argument readMethod and writeMethod' 'should not be \'None\'.') pFormat['read_method'] = readMethod pFormat['write_method'] = writeMethod registry.register_reader(pFormat['format'], table.Table, pFormat['read_method']) registry.register_writer(pFormat['format'], table.Table, pFormat['write_method']) self.polycoFormat.append(pFormat)
extension. """ 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:
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)
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)