def read_model_components(cfg_file): """Read model components from ``model_components/*.fits`` and return a list of 2D component images with containment masks. """ cfg = configobj.ConfigObj(cfg_file) column_names = ('Name', 'Type', 'GLON', 'GLAT', 'Sigma', 'Norm') column_types = ('S25', 'S25', np.float32, np.float32, np.float32, np.float32) component_table = Table(names=column_names, dtype=column_types) # Build data table for component in cfg.keys(): type_ = cfg[component]['Type'] glon = cfg[component]['GLON'] glat = cfg[component]['GLAT'] sigma = cfg[component]['Sigma'] norm = cfg[component]['Norm'] component_table.add_row([component, type_, glon, glat, sigma, norm]) if os.path.exists('model_components/'): read_fits_files(component_table) else: logging.error('No model components found. Please reuse morph_fit.') if os.path.exists('fit.reg'): read_region_file(component_table) else: compute_containment_radius(component_table) logging.info('Computing containment radii') return component_table
def reload(self): """ Reloads the value of this ``ConfigItem`` from the relevant configuration file. Returns ------- val The new value loaded from the configuration file. """ self.set(self.defaultvalue) baseobj = get_config(self.module, True) secname = baseobj.name cobj = baseobj # a ConfigObj's parent is itself, so we look for the parent with that while cobj.parent is not cobj: cobj = cobj.parent newobj = configobj.ConfigObj(cobj.filename, interpolation=False) if secname is not None: if secname not in newobj: return baseobj.get(self.name) newobj = newobj[secname] if self.name in newobj: baseobj[self.name] = newobj[self.name] return baseobj.get(self.name)
def is_unedited_config_file(content, template_content=None): """ Determines if a config file can be safely replaced because it doesn't actually contain any meaningful content, i.e. if it contains only comments or is completely empty. """ buffer = io.StringIO(content) raw_cfg = configobj.ConfigObj(buffer, interpolation=True) # If any of the items is set, return False return not any(len(v) > 0 for v in raw_cfg.values())
def obsdata(conf='observations.conf'): """Load the observation data.""" # C = configobj.ConfigObj(get_pkg_data_filename(conf)) C = configobj.ConfigObj(conf) # map things from ConfigObj to dictionary of useful objects obs = {} for key, val in C.iteritems(): if key == 'psrs' or key == 'pulsars': obs[key] = parse_pulsars(val) else: obs[key] = parse_telescope(key, val) return obs
def is_unedited_config_file(content, template_content=None): """ Determines if a config file can be safely replaced because it doesn't actually contain any meaningful content. To meet this criteria, the config file must be either: - All comments or completely empty - An exact match to a "legacy" version of the config file prior to Astropy 0.4, when APE3 was implemented and the config file contained commented-out values by default. """ # We want to calculate the md5sum using universal line endings, so # that even if the files had their line endings converted to \r\n # on Windows, this will still work. content = content.encode('latin-1') # The jquery_url setting, present in 0.3.2 and later only, is # effectively auto-generated by the build system, so we need to # ignore it in the md5sum calculation for 0.3.2. content = re.sub(br'\njquery_url\s*=\s*[^\n]+', b'', content) # First determine if the config file has any effective content buffer = io.BytesIO(content) buffer.seek(0) raw_cfg = configobj.ConfigObj(buffer, interpolation=True) for v in raw_cfg.values(): if len(v): break else: return True # Now determine if it matches the md5sum of a known, unedited # config file. known_configs = set([ '7d4b4f1120304b286d71f205975b1286', # v0.3.2 '5df7e409425e5bfe7ed041513fda3288', # v0.3 '8355f99a01b3bdfd8761ef45d5d8b7e5', # v0.2 '4ea5a84de146dc3fcea2a5b93735e634' # v0.2.1, v0.2.2, v0.2.3, v0.2.4, v0.2.5 ]) md5 = hashlib.md5() md5.update(content) digest = md5.hexdigest() return digest in known_configs
def __init__(self, fname=None, obsfile=None, size=2**25): obs = {} conf = configobj.ConfigObj(r"{0}".format(obsfile)) for key, val in conf.iteritems(): obs[key] = val self.dtype = obs["dtype"] self.sample_rate = float(obs["srate"]) * u.MHz dm = float(obs["dm"]) * u.pc / u.cm**3 self.dm = DispersionMeasure(dm) self.thread_ids = list(obs["threads"]) self.fedge = np.array(obs["fedge"]).astype('float') * u.MHz if 'forder' in obs: self.forder = np.array(obs["forder"]).astype('int') else: self.forder = np.ones(len(self.fedge)) if 'ntrack' in obs: self.ntrack = int(obs["ntrack"]) else: self.ntrack = 64 if 'nIF' in obs: self.nIF = int(obs["nIF"]) self.dt1 = 1 / self.sample_rate self.size = size self.f = self.fedge + self.forder * np.fft.rfftfreq( size, self.dt1)[:, np.newaxis] self.fref = np.amax(self.f) self.dmloss = self.dm.time_delay(np.amin(self.f), self.fref) self.samploss = int( np.ceil((self.dmloss * self.sample_rate).decompose()).value) self.step = int(size - 2**(np.ceil(np.log2(self.samploss)))) self.npol = len(self.thread_ids) print("{0} and {1} samples lost to de-dispersion".format( self.dmloss, self.samploss)) print("Taking blocks of {0}, steps of {1} samples".format( self.size, self.step)) if fname: self.open(fname)
def write_test_cfg_file(spacing=2.): """Write test fit.cfg file with 7 sources on a hexagonal grid. Please specify sigma, excess in fit.cfg and spacing of the sources. """ cfg = configobj.ConfigObj('fit.cfg') # Hexgonal test grid positions = np.array([[0, 0], [0, 1], [1, 0.5], [1, -0.5], [0, -1], [-1, -0.5], [-1, 0.5]]) * spacing # Set source parameters for i, pos in enumerate(positions): cfg['Source{0}'.format(i)] = {} cfg['Source{0}'.format(i)]['Type'] = 'Gaussian' cfg['Source{0}'.format(i)]['GLON'] = pos[0] + 180 cfg['Source{0}'.format(i)]['GLAT'] = pos[1] cfg['Source{0}'.format(i)]['Sigma'] = 1. cfg['Source{0}'.format(i)]['Norm'] = 1. cfg.write()
def get_config(packageormod=None, reload=False): """ Gets the configuration object or section associated with a particular package or module. Parameters ----------- packageormod : str or None The package for which to retrieve the configuration object. If a string, it must be a valid package name, or if `None`, the package from which this function is called will be used. reload : bool, optional Reload the file, even if we have it cached. Returns ------- cfgobj : ``configobj.ConfigObj`` or ``configobj.Section`` If the requested package is a base package, this will be the ``configobj.ConfigObj`` for that package, or if it is a subpackage or module, it will return the relevant ``configobj.Section`` object. Raises ------ RuntimeError If ``packageormod`` is `None`, but the package this item is created from cannot be determined. """ if packageormod is None: packageormod = find_current_module(2) if packageormod is None: msg1 = 'Cannot automatically determine get_config module, ' msg2 = 'because it is not called from inside a valid module' raise RuntimeError(msg1 + msg2) else: packageormod = packageormod.__name__ packageormodspl = packageormod.split('.') rootname = packageormodspl[0] secname = '.'.join(packageormodspl[1:]) cobj = _cfgobjs.get(rootname, None) if cobj is None or reload: cfgfn = None try: # This feature is intended only for use by the unit tests if _override_config_file is not None: cfgfn = _override_config_file else: cfgfn = path.join(get_config_dir(), rootname + '.cfg') cobj = configobj.ConfigObj(cfgfn, interpolation=False) except OSError as e: msg = ('Configuration defaults will be used due to ') errstr = '' if len(e.args) < 1 else (':' + str(e.args[0])) msg += e.__class__.__name__ + errstr msg += f' on {cfgfn}' warn(ConfigurationMissingWarning(msg)) # This caches the object, so if the file becomes accessible, this # function won't see it unless the module is reloaded cobj = configobj.ConfigObj(interpolation=False) _cfgobjs[rootname] = cobj if secname: # not the root package if secname not in cobj: cobj[secname] = {} return cobj[secname] else: return cobj
def get_config(packageormod=None, reload=False, rootname=None): """ Gets the configuration object or section associated with a particular package or module. Parameters ---------- packageormod : str or None The package for which to retrieve the configuration object. If a string, it must be a valid package name, or if ``None``, the package from which this function is called will be used. reload : bool, optional Reload the file, even if we have it cached. rootname : str or None Name of the root configuration directory. If ``None`` and ``packageormod`` is ``None``, this defaults to be the name of the package from which this function is called. If ``None`` and ``packageormod`` is not ``None``, this defaults to ``astropy``. Returns ------- cfgobj : ``configobj.ConfigObj`` or ``configobj.Section`` If the requested package is a base package, this will be the ``configobj.ConfigObj`` for that package, or if it is a subpackage or module, it will return the relevant ``configobj.Section`` object. Raises ------ RuntimeError If ``packageormod`` is `None`, but the package this item is created from cannot be determined. """ if packageormod is None: packageormod = find_current_module(2) if packageormod is None: msg1 = 'Cannot automatically determine get_config module, ' msg2 = 'because it is not called from inside a valid module' raise RuntimeError(msg1 + msg2) else: packageormod = packageormod.__name__ _autopkg = True else: _autopkg = False packageormodspl = packageormod.split('.') pkgname = packageormodspl[0] secname = '.'.join(packageormodspl[1:]) if rootname is None: if _autopkg: rootname = pkgname else: rootname = 'astropy' # so we don't break affiliated packages cobj = _cfgobjs.get(pkgname, None) if cobj is None or reload: cfgfn = None try: # This feature is intended only for use by the unit tests if _override_config_file is not None: cfgfn = _override_config_file else: cfgfn = path.join(get_config_dir(rootname=rootname), pkgname + '.cfg') cobj = configobj.ConfigObj(cfgfn, interpolation=False) except OSError: # This can happen when HOME is not set cobj = configobj.ConfigObj(interpolation=False) # This caches the object, so if the file becomes accessible, this # function won't see it unless the module is reloaded _cfgobjs[pkgname] = cobj if secname: # not the root package if secname not in cobj: cobj[secname] = {} return cobj[secname] else: return cobj
def main(args=None): """ This is the main function called by the `velplot` script. """ from astropy.utils.compat import argparse from astropy.extern.configobj import configobj, validate from pkg_resources import resource_stream parser = argparse.ArgumentParser( description='Creates a stacked velocity plot.\nTo dump a default ' 'configuration file: velplot -d\nTo dump an extended ' 'default configuration file: velplot -dd', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('config', help='path to the configuration file') config = resource_stream(__name__, '/config/velplot.cfg') config_extended = resource_stream(__name__, '/config/velplot_extended.cfg') spec = resource_stream(__name__, '/config/velplot_specification.cfg') if len(sys.argv) > 1: if sys.argv[1] == '-d': cfg = ConfigObj(config) cfg.filename = '{0}/velplot.cfg'.format(os.getcwd()) cfg.write() return elif sys.argv[1] == '-dd': cfg = ConfigObj(config_extended) cfg.filename = '{0}/velplot.cfg'.format(os.getcwd()) cfg.write() return args = parser.parse_args(args) try: cfg = configobj.ConfigObj(args.config, configspec=spec) validator = validate.Validator() cfg.validate(validator) except: raise IOError('Configuration file could not be read') figname = cfg['FIGURE'].pop('filename') # Create list of transitions: fname = cfg['FIGURE'].pop('transitions') print('Reading transitions from ', fname) fh = open(fname) transitions = list(fh) fh.close() # Don't include transitions that are commented out: transitions = [transition for transition in transitions if not transition.startswith('#')] # Initialise figure: velplot = VelocityPlot(transitions, **cfg['FIGURE']) fname = cfg['DATA'].pop('filename') if not fname: raise IOError('no data to plot!') # Get spectrum information and plot: spectrum = (Table.read(fname) if fname.endswith('fits') else ascii.read(fname)) wavelength = spectrum[cfg['DATA'].pop('wavelength_column')] flux = spectrum[cfg['DATA'].pop('flux_column')] error = spectrum[cfg['DATA'].pop('error_column')] continuum = spectrum[cfg['DATA'].pop('continuum_column')] velplot.plot_data(wavelength, flux, error, continuum, **cfg['DATA']) # Get model information and plot if specified: fname = cfg['MODEL'].pop('filename') if fname: ion = cfg['MODEL'].pop('ion_column') redshift = cfg['MODEL'].pop('redshift_column') logn = cfg['MODEL'].pop('logn_column') b = cfg['MODEL'].pop('b_column') if fname.endswith('f26'): model = read_f26(fname) absorbers = model.absorbers else: table = (Table.read(fname) if fname.endswith('fits') else ascii.read(fname)) absorbers = [Absorber(row[ion], row[redshift], row[logn], row[b]) for row in table] velplot.plot_models(absorbers, **cfg['MODEL']) # Save: if figname: print('Saving to {0}'.format(figname)) velplot.savefig(figname) # Display: velplot.display()
foldspec, icount = fold(a.foldtype, a.filename, a.obsfile, a.tstart, a.polyco, a.dtype, a.Tint*u.s, a.tbin*u.s, a.nchan, a.ngate, a.size, a.dedisperse, a.pulsedetect) # Write very basic fits file # Arrange to [time, pol, freq, phase] n = foldspec / icount[...,np.newaxis] if a.foldtype == 'fold': n = np.swapaxes(n, 1, 3) n = np.swapaxes(n, 2, 3) header = fits.Header(['filename', a.filename]) if a.tstart: header.set('T0', a.tstart) else: header.set('T0', 'beginning of file') header.set('polyco', a.polyco) header.set('Tint', a.Tint) header.set('dedisperse', a.dedisperse) obs = {} conf = configobj.ConfigObj(r"{0}".format(a.obsfile)) for key, val in conf.iteritems(): header.set('{0}'.format(key), '{0}'.format(val)) hdu = fits.PrimaryHDU(n, header) hdu.writeto('{0}_{1}_{2}s_{3}chan_{4}gate.fits' .format(a.foldtype, a.tstart, a.Tint, a.nchan, a.ngate))
def main(args=None): """ This is the main function called by the `ivelplot` script. """ from astropy.utils.compat import argparse from astropy.extern.configobj import configobj, validate from pkg_resources import resource_stream parser = argparse.ArgumentParser( description='An interactive environment for absorption line ' 'identification and Voigt profile \nfitting with VPFIT.\n' '\nTo dump a default configuration file: ivelplot -d' '\nTo dump an extended default configuration file: ' 'ivelplot -dd', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('config', help='path to the configuration file') parser.add_argument('-z', '--redshift', help='redshift') parser.add_argument('--search', action='store_true', help='display a general search list of ions') parser.add_argument('--lyman', action='store_true', help='display the Lyman series transitions') parser.add_argument('--galactic', action='store_true', help='display the common Galactic transitions') parser.add_argument('--agn', action='store_true', help='display the common AGN associated transitions') config = resource_stream(__name__, '/config/ivelplot.cfg') config_extended = resource_stream( __name__, '/config/ivelplot_extended.cfg') spec = resource_stream(__name__, '/config/ivelplot_specification.cfg') if len(sys.argv) > 1: if sys.argv[1] == '-d': cfg = configobj.ConfigObj(config) cfg.filename = '{0}/ivelplot.cfg'.format(os.getcwd()) cfg.write() return elif sys.argv[1] == '-dd': cfg = configobj.ConfigObj(config_extended) cfg.filename = '{0}/ivelplot.cfg'.format(os.getcwd()) cfg.write() return args = parser.parse_args(args) try: cfg = configobj.ConfigObj(args.config, configspec=spec) validator = validate.Validator() cfg.validate(validator) except: raise IOError('Configuration file could not be read') fname = cfg['WINDOW'].pop('transitions') if args.search: fh = resource_stream(__name__, '/data/search.dat') transitions = list(fh) fh.close() elif args.lyman: fh = resource_stream(__name__, '/data/lyman.dat') transitions = list(fh) fh.close() elif args.galactic: fh = resource_stream(__name__, '/data/galactic.dat') transitions = list(fh) fh.close() elif args.agn: fh = resource_stream(__name__, '/data/agn.dat') transitions = list(fh) fh.close() else: print('Reading transitions from ', fname) fh = open(fname) transitions = list(fh) fh.close() transitions = [transition for transition in transitions if not transition.startswith('#')] fname = cfg['DATA'].pop('filename') if not fname: raise IOError('no data to plot!') spectrum = Table.read(fname) if fname[-4:] == 'fits' else ascii.read(fname) wavelength = spectrum[cfg['DATA'].pop('wavelength_column')] flux = spectrum[cfg['DATA'].pop('flux_column')] error = spectrum[cfg['DATA'].pop('error_column')] continuum = spectrum[cfg['DATA'].pop('continuum_column')] redshift = float(args.redshift) if args.redshift is not None else 0 cfg['MODEL']['system_width'] = (cfg['WINDOW']['vmax'] - cfg['WINDOW']['vmin']) cfg['MODEL']['absorbers'] = None print(info) app = QApplication(sys.argv) app.aboutToQuit.connect(app.deleteLater) desktop = app.desktop() screen = desktop.screenGeometry() width = screen.width() / desktop.physicalDpiX() * 0.88 fontsize = 0.7 * width label_fontsize = 0.6 * width cfg['WINDOW']['width'] = width cfg['WINDOW']['fontsize'] = fontsize cfg['WINDOW']['label_fontsize'] = label_fontsize velocity_plot = InteractiveVelocityPlot( fname, transitions, wavelength, flux, error, continuum, redshift, **cfg) velocity_plot.window.show() output_stream = OutputStream() output_stream.text_written.connect(velocity_plot.on_output) sys.stdout = output_stream sys.exit(app.exec_())