code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def pivot_wavelength_ee(bpass):
from scipy.integrate import simps
return np.sqrt(simps(bpass.resp, bpass.wlen) /
simps(bpass.resp / bpass.wlen**2, bpass.wlen)) | Compute pivot wavelength assuming equal-energy convention.
`bpass` should have two properties, `resp` and `wlen`. The units of `wlen`
can be anything, and `resp` need not be normalized in any particular way. |
def interpolated_halfmax_points(x, y):
from scipy.interpolate import interp1d
from scipy.optimize import fmin
x = np.asarray(x)
y = np.asarray(y)
halfmax = 0.5 * y.max()
# Guess from the actual samples.
delta = y - halfmax
guess1 = 0
while delta[guess1] < 0:
guess1 +=... | Given a curve y(x), find the x coordinates of points that have half the
value of max(y), using linear interpolation. We're assuming that y(x) has
a bandpass-ish shape, i.e., a single maximum and a drop to zero as we go
to the edges of the function's domain. We also assume that x is sorted
increasingly. |
def get_std_registry():
from six import itervalues
reg = Registry()
for fn in itervalues(builtin_registrars):
fn(reg)
return reg | Get a Registry object pre-filled with information for standard
telescopes. |
def pivot_wavelength(self):
wl = self.registry._pivot_wavelengths.get((self.telescope, self.band))
if wl is not None:
return wl
wl = self.calc_pivot_wavelength()
self.registry.register_pivot_wavelength(self.telescope, self.band, wl)
return wl | Get the bandpass' pivot wavelength.
Unlike calc_pivot_wavelength(), this function will use a cached
value if available. |
def calc_halfmax_points(self):
d = self._ensure_data()
return interpolated_halfmax_points(d.wlen, d.resp) | Calculate the wavelengths of the filter half-maximum values. |
def halfmax_points(self):
t = self.registry._halfmaxes.get((self.telescope, self.band))
if t is not None:
return t
t = self.calc_halfmax_points()
self.registry.register_halfmaxes(self.telescope, self.band, t[0], t[1])
return t | Get the bandpass' half-maximum wavelengths. These can be used to
compute a representative bandwidth, or for display purposes.
Unlike calc_halfmax_points(), this function will use a cached value if
available. |
def mag_to_fnu(self, mag):
if self.native_flux_kind == 'flam':
return flam_ang_to_fnu_cgs(self.mag_to_flam(mag), self.pivot_wavelength())
raise PKError('dont\'t know how to get f_ν from mag for bandpass %s/%s',
self.telescope, self.band) | Convert a magnitude in this band to a f_ν flux density.
It is assumed that the magnitude has been computed in the appropriate
photometric system. The definition of "appropriate" will vary from
case to case. |
def synphot(self, wlen, flam):
from scipy.interpolate import interp1d
from scipy.integrate import romberg
d = self._ensure_data()
mflam = interp1d(wlen, flam,
kind='linear',
bounds_error=False, fill_value=0)
mresp = in... | `wlen` and `flam` give a tabulated model spectrum in wavelength and f_λ
units. We interpolate linearly over both the model and the bandpass
since they're both discretely sampled.
Note that quadratic interpolation is both much slower and can blow up
fatally in some cases. The latter issu... |
def blackbody(self, T):
from scipy.integrate import simps
d = self._ensure_data()
# factor of pi is going from specific intensity (sr^-1) to unidirectional
# inner factor of 1e-8 is Å to cm
# outer factor of 1e-8 is f_λ in cm^-1 to f_λ in Å^-1
from .cgs import ... | Calculate the contribution of a blackbody through this filter. *T* is the
blackbody temperature in Kelvin. Returns a band-averaged spectrum in
f_λ units.
We use the composite Simpson's rule to integrate over the points at
which the filter response is sampled. Note that this is a differe... |
def bands(self, telescope):
q = self._seen_bands.get(telescope)
if q is None:
return []
return list(q) | Return a list of bands associated with the specified telescope. |
def register_pivot_wavelength(self, telescope, band, wlen):
if (telescope, band) in self._pivot_wavelengths:
raise AlreadyDefinedError('pivot wavelength for %s/%s already '
'defined', telescope, band)
self._note(telescope, band)
self._pi... | Register precomputed pivot wavelengths. |
def register_halfmaxes(self, telescope, band, lower, upper):
if (telescope, band) in self._halfmaxes:
raise AlreadyDefinedError('half-max points for %s/%s already '
'defined', telescope, band)
self._note(telescope, band)
self._halfmaxes... | Register precomputed half-max points. |
def register_bpass(self, telescope, klass):
if telescope in self._bpass_classes:
raise AlreadyDefinedError('bandpass class for %s already '
'defined', telescope)
self._note(telescope, None)
self._bpass_classes[telescope] = klass
... | Register a Bandpass class. |
def get(self, telescope, band):
klass = self._bpass_classes.get(telescope)
if klass is None:
raise NotDefinedError('bandpass data for %s not defined', telescope)
bp = klass()
bp.registry = self
bp.telescope = telescope
bp.band = band
return ... | Get a Bandpass object for a known telescope and filter. |
def _load_data(self, band):
# `band` should be 'nuv' or 'fuv'
df = bandpass_data_frame('filter_galex_' + band + '.dat', 'wlen resp')
df.resp *= df.wlen # QE -> EE response convention.
return df | From Morrissey+ 2005, with the actual data coming from
http://www.astro.caltech.edu/~capak/filters/. According to the latter,
these are in QE units and thus need to be multiplied by the wavelength
when integrating per-energy. |
def _load_data(self, band):
# `band` should be 'Lp'.
df = bandpass_data_frame('filter_mko_' + band + '.dat', 'wlen resp')
# Put in increasing wavelength order:
df = df[::-1]
df.index = np.arange(df.shape[0])
df.wlen *= 1e4 # micron to Angstrom
df.resp *= ... | Filter responses for MKO NIR filters as specified in Tokunaga+ 2002 (see
also Tokunaga+ 2005). I downloaded the L' profile from
http://irtfweb.ifa.hawaii.edu/~nsfcam/hist/filters.2006.html.
Pivot wavelengths from Tokunaga+ 2005 (Table 2) confirm that the
profile is in QE convention, alt... |
def mag_to_fnu(self, mag):
return cgs.cgsperjy * self._zeropoints[self.band] * 10**(-0.4 * mag) | Compute F_ν for an MKO IR filter band. There are some problems here since
"MKO" is filters, not a photometric system, but people try to make
Vega = 0. |
def _load_data(self, band):
h = bandpass_data_fits('sdss3_filter_responses.fits')
section = 'ugriz'.index(band[0]) + 1
d = h[section].data
if d.wavelength.dtype.isnative:
df = pd.DataFrame({'wlen': d.wavelength, 'resp': d.respt})
else:
df = pd.Dat... | Filter responses for SDSS. Data table from
https://www.sdss3.org/binaries/filter_curves.fits, as linked from
https://www.sdss3.org/instruments/camera.php#Filters. SHA1 hash of the
file is d3f638c41e21489ba7d6dbe7bb8217d938f21184. "Determined by Jim
Gunn in June 2001." Doi+ 2010 have upda... |
def mag_to_fnu(self, mag):
# `band` should be 'up', 'gp', 'rp', 'ip', or 'zp'.
if len(band) != 2 or band[1] != 'p':
raise ValueError('band: ' + band)
return abmag_to_fnu_cgs(mag) | SDSS *primed* magnitudes to F_ν. The primed magnitudes are the "USNO"
standard-star system defined in Smith+ (2002AJ....123.2121S) and
Fukugita+ (1996AJ....111.1748F). This system is anchored to the AB
magnitude system, and as far as I can tell it is not known to have
measurable offsets ... |
def _load_data(self, band):
d = bandpass_data_fits('sw' + self._band_map[band] + '_20041120v106.arf')[1].data
# note:
# data.WAVE_MIN[i] < data.WAVE_MIN[i+1], but
# data.WAVE_MIN[i] > data.WAVE_MAX[i] (!)
# data.WAVE_MIN[i] = data.WAVE_MAX[i+1] (!)
wmid =... | In-flight effective areas for the Swift UVOT, as obtained from the CALDB.
See Breeveld+ 2011. XXX: confirm that these are equal-energy, not
quantum-efficiency. |
def _load_data(self, band):
# `band` should be 1, 2, 3, or 4.
df = bandpass_data_frame('filter_wise_' + str(band) + '.dat', 'wlen resp uncert')
df.wlen *= 1e4 # micron to Angstrom
df.uncert *= df.resp / 1000. # parts per thou. to absolute values.
lo, hi = self._filter_su... | From the WISE All-Sky Explanatory Supplement, IV.4.h.i.1, and Jarrett+
2011. These are relative response per erg and so can be integrated
directly against F_nu spectra. Wavelengths are in micron,
uncertainties are in parts per thousand. |
def invoke (self, args, **kwargs):
if len (args) not in (3, 6):
raise multitool.UsageError ('c2m expected exactly 3 or 6 arguments')
year = int (args[0])
month = int (args[1])
import astropy.time
if len (args) == 3:
day = float (args[2])
... | c2m - UTC calendar to MJD[TAI] |
def clean_comment_body(body):
body = _parser.unescape(body)
body = re.sub(r'<a [^>]+>(.+?)</a>', r'\1', body)
body = body.replace('<br>', '\n')
body = re.sub(r'<.+?>', '', body)
return body | Returns given comment HTML as plaintext.
Converts all HTML tags and entities within 4chan comments
into human-readable text equivalents. |
def _create_wcs (fitsheader):
wcsmodule = _load_wcs_module ()
is_pywcs = hasattr (wcsmodule, 'UnitConverter')
wcs = wcsmodule.WCS (fitsheader)
wcs.wcs.set ()
wcs.wcs.fix () # I'm interested in MJD computation via datfix()
if hasattr (wcs, 'wcs_pix2sky'):
wcs.wcs_pix2world = wcs.wc... | For compatibility between astropy and pywcs. |
def sanitize_unicode(item):
if isinstance(item, text_type):
return item.encode('utf8')
if isinstance(item, dict):
return dict((sanitize_unicode(k), sanitize_unicode(v)) for k, v in six.iteritems(item))
if isinstance(item,(list, tuple)):
return item.__class__(sanitize_unicode(x) ... | Safely pass string values to the CASA tools.
item
A value to be passed to a CASA tool.
In Python 2, the bindings to CASA tasks expect to receive all string values
as binary data (:class:`str`) and not Unicode. But :mod:`pwkit` often uses
the ``from __future__ import unicode_literals`` statement ... |
def datadir(*subdirs):
import os.path
data = None
if 'CASAPATH' in os.environ:
data = os.path.join(os.environ['CASAPATH'].split()[0], 'data')
if data is None:
# The Conda CASA directory layout:
try:
import casadef
except ImportError:
pass
... | Get a path within the CASA data directory.
subdirs
Extra elements to append to the returned path.
This function locates the directory where CASA resource data files (tables
of time offsets, calibrator models, etc.) are stored. If called with no
arguments, it simply returns that path. If argument... |
def logger(filter='WARN'):
import os, shutil, tempfile
cwd = os.getcwd()
tempdir = None
try:
tempdir = tempfile.mkdtemp(prefix='casautil')
try:
os.chdir(tempdir)
sink = tools.logsink()
sink.setlogfile(sanitize_unicode(os.devnull))
t... | Set up CASA to write log messages to standard output.
filter
The log level filter: less urgent messages will not be shown. Valid values
are strings: "DEBUG1", "INFO5", ... "INFO1", "INFO", "WARN", "SEVERE".
This function creates and returns a CASA ”log sink” object that is
configured to write ... |
def _get_extended(scene, resp):
root = ElementTree.fromstring(resp.text)
items = root.findall("eemetadata:metadataFields/eemetadata:metadataField", NAMESPACES)
scene['extended'] = {item.attrib.get('name').strip(): xsi.get(item[0]) for item in items}
return scene | Parse metadata returned from the metadataUrl of a USGS scene.
:param scene:
Dictionary representation of a USGS scene
:param resp:
Response object from requests/grequests |
def _async_requests(urls):
session = FuturesSession(max_workers=30)
futures = [
session.get(url)
for url in urls
]
return [ future.result() for future in futures ] | Sends multiple non-blocking requests. Returns
a list of responses.
:param urls:
List of urls |
def metadata(dataset, node, entityids, extended=False, api_key=None):
api_key = _get_api_key(api_key)
url = '{}/metadata'.format(USGS_API)
payload = {
"jsonRequest": payloads.metadata(dataset, node, entityids, api_key=api_key)
}
r = requests.post(url, payload)
response = r.json()
... | Request metadata for a given scene in a USGS dataset.
:param dataset:
:param node:
:param entityids:
:param extended:
Send a second request to the metadata url to get extended metadata on the scene.
:param api_key: |
def reraise_context(fmt, *args):
import sys
if len(args):
cstr = fmt % args
else:
cstr = text_type(fmt)
ex = sys.exc_info()[1]
if isinstance(ex, EnvironmentError):
ex.strerror = '%s: %s' % (cstr, ex.strerror)
ex.args = (ex.errno, ex.strerror)
else:
... | Reraise an exception with its message modified to specify additional
context.
This function tries to help provide context when a piece of code
encounters an exception while trying to get something done, and it wishes
to propagate contextual information farther up the call stack. It only
makes sense... |
def copy(self):
new = self.__class__()
new.__dict__ = dict(self.__dict__)
return new | Return a shallow copy of this object. |
def to_pretty(self, format='str'):
if format == 'str':
template = '%-*s = %s'
elif format == 'repr':
template = '%-*s = %r'
else:
raise ValueError('unrecognized value for "format": %r' % format)
d = self.__dict__
maxlen = 0
f... | Return a string with a prettified version of this object’s contents.
The format is a multiline string where each line is of the form ``key
= value``. If the *format* argument is equal to ``"str"``, each
``value`` is the stringification of the value; if it is ``"repr"``, it
is its :func:... |
def in_casapy (helper, asdm=None, ms=None, tbuff=None):
if asdm is None:
raise ValueError ('asdm')
if ms is None:
raise ValueError ('ms')
if tbuff is None:
raise ValueError ('tbuff')
helper.casans.importevla (asdm=asdm, vis=ms, ocorr_mode='co', online=True,
... | This function is run inside the weirdo casapy IPython environment! A
strange set of modules is available, and the
`pwkit.environments.casa.scripting` system sets up a very particular
environment to allow encapsulated scripting. |
def write_stream (stream, holders, defaultsection=None, extrapos=(), sha1sum=False, **kwargs):
if sha1sum:
import hashlib
sha1 = hashlib.sha1 ()
else:
sha1 = None
inifile.write_stream (stream,
_format_many (holders, defaultsection, extrapos, sha1),
... | `extrapos` is basically a hack for multi-step processing. We have some flux
measurements that are computed from luminosities and distances. The flux
value is therefore an unwrapped Uval, which doesn't retain memory of any
positivity constraint it may have had. Therefore, if we write out such a
value usi... |
def _process_hdu (self, hdu):
"We've hacked the load order a bit to get t0 and mjd0 in _process_main()."
if hdu.name == 'EVENTS':
pass
else:
super (Events, self)._process_hdu (hduf _process_hdu (self, hdu):
"We've hacked the load order a bit to get t0 and mjd0 in... | We've hacked the load order a bit to get t0 and mjd0 in _process_main(). |
def output(self, kind, line):
"*line* should be bytes"
self.destination.write(b''.join([
self._cyan,
b't=%07d' % (time.time() - self._t0),
self._reset,
self._kind_prefixes[kind],
self.markers[kind],
line,
self._reset,
... | *line* should be bytes |
def output_stderr(self, text):
"*text* should be bytes"
binary_stderr.write(b''.join([
self._red,
b't=%07d' % (time.time() - self._t0),
self._reset,
b' ',
text,
]))
binary_stderr.flush(f output_stderr(self, text):
"*text... | *text* should be bytes |
def get_boards(board_name_list, *args, **kwargs):
if isinstance(board_name_list, basestring):
board_name_list = board_name_list.split()
return [Board(name, *args, **kwargs) for name in board_name_list] | Given a list of boards, return :class:`basc_py4chan.Board` objects.
Args:
board_name_list (list): List of board names to get, eg: ['b', 'tg']
Returns:
dict of :class:`basc_py4chan.Board`: Requested boards. |
def get_all_boards(*args, **kwargs):
# Use https based on how the Board class instances are to be instantiated
https = kwargs.get('https', args[1] if len(args) > 1 else False)
# Dummy URL generator, only used to generate the board list which doesn't
# require a valid board name
url_generator =... | Returns every board on 4chan.
Returns:
dict of :class:`basc_py4chan.Board`: All boards. |
def get_thread(self, thread_id, update_if_cached=True, raise_404=False):
# see if already cached
cached_thread = self._thread_cache.get(thread_id)
if cached_thread:
if update_if_cached:
cached_thread.update()
return cached_thread
res = se... | Get a thread from 4chan via 4chan API.
Args:
thread_id (int): Thread ID
update_if_cached (bool): Whether the thread should be updated if it's already in our cache
raise_404 (bool): Raise an Exception if thread has 404'd
Returns:
:class:`basc_py4chan.Thre... |
def thread_exists(self, thread_id):
return self._requests_session.head(
self._url.thread_api_url(
thread_id=thread_id
)
).ok | Check if a thread exists or has 404'd.
Args:
thread_id (int): Thread ID
Returns:
bool: Whether the given thread exists on this board. |
def get_threads(self, page=1):
url = self._url.page_url(page)
return self._request_threads(url) | Returns all threads on a certain page.
Gets a list of Thread objects for every thread on the given page. If a thread is
already in our cache, the cached version is returned and thread.want_update is
set to True on the specific thread object.
Pages on 4chan are indexed from 1 onwards.
... |
def get_all_thread_ids(self):
json = self._get_json(self._url.thread_list())
return [thread['no'] for page in json for thread in page['threads']] | Return the ID of every thread on this board.
Returns:
list of ints: List of IDs of every thread on this board. |
def get_all_threads(self, expand=False):
if not expand:
return self._request_threads(self._url.catalog())
thread_ids = self.get_all_thread_ids()
threads = [self.get_thread(id, raise_404=False) for id in thread_ids]
return filter(None, threads) | Return every thread on this board.
If not expanded, result is same as get_threads run across all board pages,
with last 3-5 replies included.
Uses the catalog when not expanding, and uses the flat thread ID listing
at /{board}/threads.json when expanding for more efficient resource usa... |
def refresh_cache(self, if_want_update=False):
for thread in tuple(self._thread_cache.values()):
if if_want_update:
if not thread.want_update:
continue
thread.update() | Update all threads currently stored in our cache. |
def in_casapy(helper, ms=None, plotdest=None):
import numpy as np, os
if ms is None:
raise ValueError('ms')
if plotdest is None:
raise ValueError('plotdest')
opac = helper.casans.plotweather(vis=ms, plotName=plotdest)
opac = np.asarray(opac)
with open(helper.temppath('opac... | This function is run inside the weirdo casapy IPython environment! A
strange set of modules is available, and the
`pwkit.environments.casa.scripting` system sets up a very particular
environment to allow encapsulated scripting. |
def get_node(dataset, node):
if node is None:
cur_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(cur_dir, "..", "data")
dataset_path = os.path.join(data_dir, "datasets.json")
with open(dataset_path, "r") as f:
dataset... | .. todo:: Move to more appropriate place in module. |
def count_events (env, evtpath, filter):
with env.slurp (argv=['dmstat', '%s%s[cols energy]' % (evtpath, filter)], linebreak=True) as s:
for etype, payload in s:
if etype != 'stdout':
continue
if b'good:' not in payload:
continue
ret... | TODO: this can probably be replaced with simply reading the file
ourselves! |
def prepend_path(orig, text, pathsep=os.pathsep):
if orig is None:
orig = ''
if not len(orig):
return text
return ''.join([text, pathsep, orig]) | Returns a $PATH-like environment variable with `text` prepended. `orig` is
the original variable value, or None. `pathsep` is the character
separating path elements, defaulting to `os.pathsep`.
Example:
newpath = cli.prepend_path(oldpath, '/mypackage/bin')
See also `prepend_environ_path`. |
def prepend_environ_path(env, name, text, pathsep=os.pathsep):
env[name] = prepend_path(env.get(name), text, pathsep=pathsep)
return env | Prepend `text` into a $PATH-like environment variable. `env` is a
dictionary of environment variables and `name` is the variable name.
`pathsep` is the character separating path elements, defaulting to
`os.pathsep`. The variable will be created if it is not already in `env`.
Returns `env`.
Example:... |
def make_figure9_plot(shlib_path, use_lowlevel=True, **kwargs):
import omega as om
if use_lowlevel:
out_vals = do_figure9_calc_lowlevel(shlib_path, **kwargs)
else:
out_vals = do_figure9_calc_highlevel(shlib_path, **kwargs)
freqs = out_vals[:,OUT_VAL_FREQ]
tot_ints = out_vals[:... | Reproduce Figure 9 of the Fleischman & Kuznetsov (2010) paper, using our
low-level interfaces. Uses OmegaPlot, of course.
Input parameters, etc., come from the file ``Flare071231a.pro`` that is
distributed with the paper’s Supplementary Data archive.
Invoke with something like::
from pwkit impo... |
def new_for_fk10_fig9(cls, shlib_path):
inst = (cls(shlib_path)
.set_thermal_background(2.1e7, 3e9)
.set_bfield(48)
.set_edist_powerlaw(0.016, 4.0, 3.7, 5e9/3)
.set_freqs(100, 0.5, 50)
.set_hybrid_parameters(12, 12)
.set_ignore_q_t... | Create a calculator initialized to reproduce Figure 9 from FK10.
This is mostly to provide a handy way to create a new
:class:`Calculator` instance that is initialized with reasonable
values for all of its parameters. |
def set_bfield(self, B_G):
if not (B_G > 0):
raise ValueError('must have B_G > 0; got %r' % (B_G,))
self.in_vals[IN_VAL_B] = B_G
return self | Set the strength of the local magnetic field.
**Call signature**
*B_G*
The magnetic field strength, in Gauss
Returns
*self* for convenience in chaining. |
def set_bfield_for_s0(self, s0):
if not (s0 > 0):
raise ValueError('must have s0 > 0; got %r' % (s0,))
B0 = 2 * np.pi * cgs.me * cgs.c * self.in_vals[IN_VAL_FREQ0] / (cgs.e * s0)
self.in_vals[IN_VAL_B] = B0
return self | Set B to probe a certain harmonic number.
**Call signature**
*s0*
The harmonic number to probe at the lowest frequency
Returns
*self* for convenience in chaining.
This just proceeds from the relation ``nu = s nu_c = s e B / 2 pi m_e
c``. Since *s* and *nu* ... |
def set_edist_powerlaw(self, emin_mev, emax_mev, delta, ne_cc):
if not (emin_mev >= 0):
raise ValueError('must have emin_mev >= 0; got %r' % (emin_mev,))
if not (emax_mev >= emin_mev):
raise ValueError('must have emax_mev >= emin_mev; got %r, %r' % (emax_mev, emin_mev))
... | Set the energy distribution function to a power law.
**Call signature**
*emin_mev*
The minimum energy of the distribution, in MeV
*emax_mev*
The maximum energy of the distribution, in MeV
*delta*
The power-law index of the distribution
*ne_cc*
... |
def set_edist_powerlaw_gamma(self, gmin, gmax, delta, ne_cc):
if not (gmin >= 1):
raise ValueError('must have gmin >= 1; got %r' % (gmin,))
if not (gmax >= gmin):
raise ValueError('must have gmax >= gmin; got %r, %r' % (gmax, gmin))
if not (delta >= 0):
... | Set the energy distribution function to a power law in the Lorentz factor
**Call signature**
*gmin*
The minimum Lorentz factor of the distribution
*gmax*
The maximum Lorentz factor of the distribution
*delta*
The power-law index of the distribution
... |
def set_freqs(self, n, f_lo_ghz, f_hi_ghz):
if not (f_lo_ghz >= 0):
raise ValueError('must have f_lo_ghz >= 0; got %r' % (f_lo_ghz,))
if not (f_hi_ghz >= f_lo_ghz):
raise ValueError('must have f_hi_ghz >= f_lo_ghz; got %r, %r' % (f_hi_ghz, f_lo_ghz))
if not n >= ... | Set the frequency grid on which to perform the calculations.
**Call signature**
*n*
The number of frequency points to sample.
*f_lo_ghz*
The lowest frequency to sample, in GHz.
*f_hi_ghz*
The highest frequency to sample, in GHz.
Returns
*... |
def set_obs_angle(self, theta_rad):
self.in_vals[IN_VAL_THETA] = theta_rad * 180 / np.pi # rad => deg
return self | Set the observer angle relative to the field.
**Call signature**
*theta_rad*
The angle between the ray path and the local magnetic field,
in radians.
Returns
*self* for convenience in chaining. |
def set_one_freq(self, f_ghz):
if not (f_ghz >= 0):
raise ValueError('must have f_lo_ghz >= 0; got %r' % (f_lo_ghz,))
self.in_vals[IN_VAL_NFREQ] = 1
self.in_vals[IN_VAL_FREQ0] = f_ghz * 1e9 # GHz -> Hz
self.in_vals[IN_VAL_LOGDFREQ] = 1.0
return self | Set the code to calculate results at just one frequency.
**Call signature**
*f_ghz*
The frequency to sample, in GHz.
Returns
*self* for convenience in chaining. |
def set_padist_gaussian_loss_cone(self, boundary_rad, expwidth):
self.in_vals[IN_VAL_PADIST] = PADIST_GLC
self.in_vals[IN_VAL_LCBDY] = boundary_rad * 180 / np.pi # rad => deg
self.in_vals[IN_VAL_DELTAMU] = expwidth
return self | Set the pitch-angle distribution to a Gaussian loss cone.
**Call signature**
*boundary_rad*
The angle inside which there are no losses, in radians.
*expwidth*
The characteristic width of the Gaussian loss profile
*in direction-cosine units*.
Returns
... |
def set_thermal_background(self, T_K, nth_cc):
if not (T_K >= 0):
raise ValueError('must have T_K >= 0; got %r' % (T_K,))
if not (nth_cc >= 0):
raise ValueError('must have nth_cc >= 0; got %r, %r' % (nth_cc,))
self.in_vals[IN_VAL_T0] = T_K
self.in_vals[I... | Set the properties of the background thermal plasma.
**Call signature**
*T_K*
The temperature of the background plasma, in Kelvin.
*nth_cc*
The number density of thermal electrons, in cm^-3.
Returns
*self* for convenience in chaining.
Note that th... |
def set_trapezoidal_integration(self, n):
if not (n >= 2):
raise ValueError('must have n >= 2; got %r' % (n,))
self.in_vals[IN_VAL_INTEG_METH] = n + 1
return self | Set the code to use trapezoidal integration.
**Call signature**
*n*
Use this many nodes
Returns
*self* for convenience in chaining. |
def find_rt_coefficients_tot_intens(self, depth0=None):
j_O, alpha_O, j_X, alpha_X = self.find_rt_coefficients(depth0=depth0)
j_I = j_O + j_X
alpha_I = 0.5 * (alpha_O + alpha_X) # uhh... right?
return (j_I, alpha_I) | Figure out total-intensity emission and absorption coefficients for the
current parameters.
**Argument**
*depth0* (default None)
A first guess to use for a good integration depth, in cm. If None,
the most recent value is used.
**Return value**
A tuple ``(j... |
def try_open (*args, **kwargs):
try:
return io.open (*args, **kwargs)
except IOError as e:
if e.errno == 2:
return None
raise | Simply a wrapper for io.open(), unless an IOError with errno=2 (ENOENT) is
raised, in which case None is retured. |
def make_path_func (*baseparts):
from os.path import join
base = join (*baseparts)
def path_func (*args):
return join (base, *args)
return path_func | Return a function that joins paths onto some base directory. |
def djoin (*args):
from os.path import join
i = 0
alen = len (args)
while i < alen and (args[i] == '' or args[i] == '.'):
i += 1
if i == alen:
return '.'
return join (*args[i:]) | dotless' join, for nicer paths. |
def rellink (source, dest):
from os.path import isabs, dirname, relpath, abspath
if isabs (source):
os.symlink (source, dest)
elif isabs (dest):
os.symlink (abspath (source), dest)
else:
os.symlink (relpath (source, dirname (dest)), dest) | Create a symbolic link to path *source* from path *dest*. If either
*source* or *dest* is an absolute path, the link from *dest* will point to
the absolute path of *source*. Otherwise, the link to *source* from *dest*
will be a relative link. |
def ensure_dir (path, parents=False):
if parents:
from os.path import dirname
parent = dirname (path)
if len (parent) and parent != path:
ensure_dir (parent, True)
try:
os.mkdir (path)
except OSError as e:
if e.errno == 17: # EEXIST
retur... | Returns a boolean indicating whether the directory already existed. Will
attempt to create parent directories if *parents* is True. |
def ensure_symlink (src, dst):
try:
os.symlink (src, dst)
except OSError as e:
if e.errno == 17: # EEXIST
return True
raise
return False | Ensure the existence of a symbolic link pointing to src named dst. Returns
a boolean indicating whether the symlink already existed. |
def expand (self, user=False, vars=False, glob=False, resolve=False):
from os import path
from glob import glob
text = text_type (self)
if user:
text = path.expanduser (text)
if vars:
text = path.expandvars (text)
if glob:
res... | Return a new :class:`Path` with various expansions performed. All
expansions are disabled by default but can be enabled by passing in
true values in the keyword arguments.
user : bool (default False)
Expand ``~`` and ``~user`` home-directory constructs. If a username is
unmatched or ``$HOME`` is unset, no cha... |
def format (self, *args, **kwargs):
return self.__class__ (str (self).format (*args, **kwargs)) | Return a new path formed by calling :meth:`str.format` on the
textualization of this path. |
def get_parent (self, mode='naive'):
if mode == 'textual':
return self.parent
if mode == 'resolved':
return self.resolve ().parent
if mode == 'naive':
from os.path import pardir
if not len (self.parts):
return self.__cla... | Get the path of this path’s parent directory.
Unlike the :attr:`parent` attribute, this function can correctly
ascend into parent directories if *self* is ``"."`` or a sequence of
``".."``. The precise way in which it handles these kinds of paths,
however, depends on the *mode* paramete... |
def make_relative (self, other):
if self.is_absolute ():
return self
from os.path import relpath
other = self.__class__ (other)
return self.__class__ (relpath (text_type (self), text_type (other))) | Return a new path that is the equivalent of this one relative to the path
*other*. Unlike :meth:`relative_to`, this will not throw an error if *self* is
not a sub-path of *other*; instead, it will use ``..`` to build a relative
path. This can result in invalid relative paths if *other* contains ... |
def scandir (self):
if hasattr (os, 'scandir'):
scandir = os.scandir
else:
from scandir import scandir
return scandir (path_type (self)) | Iteratively scan this path, assuming it’s a directory. This requires and
uses the :mod:`scandir` module.
`scandir` is different than `iterdir` because it generates `DirEntry`
items rather than Path instances. DirEntry objects have their
properties filled from the directory info itself, ... |
def copy_to (self, dest, preserve='mode'):
# shutil.copyfile() doesn't let the destination be a directory, so we
# have to manage that possibility ourselves.
import shutil
dest = Path (dest)
if dest.is_dir ():
dest = dest / self.name
if preserve ==... | Copy this path — as a file — to another *dest*.
The *preserve* argument specifies which meta-properties of the file
should be preserved:
``none``
Only copy the file data.
``mode``
Copy the data and the file mode (permissions, etc).
``all``
Preserve... |
def ensure_dir (self, mode=0o777, parents=False):
if parents:
p = self.parent
if p == self:
return False # can never create root; avoids loop when parents=True
p.ensure_dir (mode, True)
made_it = False
try:
self.mkdir (mo... | Ensure that this path exists as a directory.
This function calls :meth:`mkdir` on this path, but does not raise an
exception if it already exists. It does raise an exception if this
path exists but is not a directory. If the directory is created,
*mode* is used to set the permissions of... |
def ensure_parent (self, mode=0o777, parents=False):
return self.parent.ensure_dir (mode, parents) | Ensure that this path's *parent* directory exists.
Returns a boolean whether the parent directory was created. Will
attempt to create superior parent directories if *parents* is true. |
def rellink_to (self, target, force=False):
target = self.__class__ (target)
if force:
self.try_unlink ()
if self.is_absolute ():
target = target.absolute () # force absolute link
return self.symlink_to (target.make_relative (self.parent)) | Make this path a symlink pointing to the given *target*, generating the
proper relative path using :meth:`make_relative`. This gives different
behavior than :meth:`symlink_to`. For instance, ``Path
('a/b').symlink_to ('c')`` results in ``a/b`` pointing to the path
``c``, whereas :meth:`rellink_to` results in it poi... |
def rmtree (self, errors='warn'):
import shutil
if errors == 'ignore':
ignore_errors = True
onerror = None
elif errors == 'warn':
ignore_errors = False
from .cli import warn
def onerror (func, path, exc_info):
... | Recursively delete this directory and its contents. The *errors* keyword
specifies how errors are handled:
"warn" (the default)
Print a warning to standard error.
"ignore"
Ignore errors. |
def try_unlink (self):
try:
self.unlink ()
return True
except OSError as e:
if e.errno == 2:
return False # ENOENT
raise | Try to unlink this path. If it doesn't exist, no error is returned. Returns
a boolean indicating whether the path was really unlinked. |
def try_open (self, null_if_noexist=False, **kwargs):
try:
return self.open (**kwargs)
except IOError as e:
if e.errno == 2:
if null_if_noexist:
import io, os
return io.open (os.devnull, **kwargs)
re... | Call :meth:`Path.open` on this path (passing *kwargs*) and return the
result. If the file doesn't exist, the behavior depends on
*null_if_noexist*. If it is false (the default), ``None`` is returned.
Otherwise, :data:`os.devnull` is opened and returned. |
def as_hdf_store (self, mode='r', **kwargs):
from pandas import HDFStore
return HDFStore (text_type (self), mode=mode, **kwargs) | Return the path as an opened :class:`pandas.HDFStore` object. Note that the
:class:`HDFStore` constructor unconditionally prints messages to
standard output when opening and closing files, so use of this
function will pollute your program’s standard output. The *kwargs* are
forwarded to ... |
def read_astropy_ascii (self, **kwargs):
from astropy.io import ascii
return ascii.read (text_type (self), **kwargs) | Open as an ASCII table, returning a :class:`astropy.table.Table` object.
Keyword arguments are passed to :func:`astropy.io.ascii.open`; valid
ones likely include:
- ``names = <list>`` (column names)
- ``format`` ('basic', 'cds', 'csv', 'ipac', ...)
- ``guess = True`` (guess tabl... |
def read_fits (self, **kwargs):
from astropy.io import fits
return fits.open (text_type (self), **kwargs) | Open as a FITS file, returning a :class:`astropy.io.fits.HDUList` object.
Keyword arguments are passed to :func:`astropy.io.fits.open`; valid
ones likely include:
- ``mode = 'readonly'`` (or "update", "append", "denywrite", "ostream")
- ``memmap = None``
- ``save_backup = False`... |
def read_fits_bintable (self, hdu=1, drop_nonscalar_ok=True, **kwargs):
from astropy.io import fits
from .numutil import fits_recarray_to_data_frame as frtdf
with fits.open (text_type (self), mode='readonly', **kwargs) as hdulist:
return frtdf (hdulist[hdu].data, drop_nonsc... | Open as a FITS file, read in a binary table, and return it as a
:class:`pandas.DataFrame`, converted with
:func:`pkwit.numutil.fits_recarray_to_data_frame`. The *hdu* argument
specifies which HDU to read, with its default 1 indicating the first
FITS extension. The *drop_nonscalar_ok* arg... |
def read_hdf (self, key, **kwargs):
# This one needs special handling because of the "key" and path input.
import pandas
return pandas.read_hdf (text_type (self), key, **kwargs) | Open as an HDF5 file using :mod:`pandas` and return the item stored under
the key *key*. *kwargs* are passed to :func:`pandas.read_hdf`. |
def read_inifile (self, noexistok=False, typed=False):
if typed:
from .tinifile import read_stream
else:
from .inifile import read_stream
try:
with self.open ('rb') as f:
for item in read_stream (f):
yield item
... | Open assuming an “ini-file” format and return a generator yielding data
records using either :func:`pwkit.inifile.read_stream` (if *typed* is
false) or :func:`pwkit.tinifile.read_stream` (if it’s true). The
latter version is designed to work with numerical data using the
:mod:`pwkit.msmt... |
def read_json (self, mode='rt', **kwargs):
import json
with self.open (mode=mode) as f:
return json.load (f, **kwargs) | Use the :mod:`json` module to read in this file as a JSON-formatted data
structure. Keyword arguments are passed to :func:`json.load`. Returns the
read-in data structure. |
def read_lines (self, mode='rt', noexistok=False, **kwargs):
try:
with self.open (mode=mode, **kwargs) as f:
for line in f:
yield line
except IOError as e:
if e.errno != 2 or not noexistok:
raise | Generate a sequence of lines from the file pointed to by this path, by
opening as a regular file and iterating over it. The lines therefore
contain their newline characters. If *noexistok*, a nonexistent file
will result in an empty sequence rather than an exception. *kwargs*
are passed ... |
def read_numpy (self, **kwargs):
import numpy as np
with self.open ('rb') as f:
return np.load (f, **kwargs) | Read this path into a :class:`numpy.ndarray` using :func:`numpy.load`.
*kwargs* are passed to :func:`numpy.load`; they likely are:
mmap_mode : None, 'r+', 'r', 'w+', 'c'
Load the array using memory-mapping
allow_pickle : bool = True
Whether Pickle-format data are allowed; potential securi... |
def read_numpy_text (self, dfcols=None, **kwargs):
import numpy as np
if dfcols is not None:
kwargs['unpack'] = True
retval = np.loadtxt (text_type (self), **kwargs)
if dfcols is not None:
import pandas as pd
if isinstance (dfcols, six.stri... | Read this path into a :class:`numpy.ndarray` as a text file using
:func:`numpy.loadtxt`. In normal conditions the returned array is
two-dimensional, with the first axis spanning the rows in the file and
the second axis columns (but see the *unpack* and *dfcols* keywords).
If *dfcols* is... |
def read_pandas (self, format='table', **kwargs):
import pandas
reader = getattr (pandas, 'read_' + format, None)
if not callable (reader):
raise PKError ('unrecognized Pandas format %r: no function pandas.read_%s',
format, format)
with s... | Read using :mod:`pandas`. The function ``pandas.read_FORMAT`` is called
where ``FORMAT`` is set from the argument *format*. *kwargs* are
passed to this function. Supported formats likely include
``clipboard``, ``csv``, ``excel``, ``fwf``, ``gbq``, ``html``,
``json``, ``msgpack``, ``pickl... |
def read_pickle (self):
gen = self.read_pickles ()
value = gen.next ()
gen.close ()
return value | Open the file, unpickle one object from it using :mod:`pickle`, and return
it. |
def read_pickles (self):
try:
import cPickle as pickle
except ImportError:
import pickle
with self.open (mode='rb') as f:
while True:
try:
obj = pickle.load (f)
except EOFError:
... | Generate a sequence of objects by opening the path and unpickling items
until EOF is reached. |
def read_text(self, encoding=None, errors=None, newline=None):
with self.open (mode='rt', encoding=encoding, errors=errors, newline=newline) as f:
return f.read() | Read this path as one large chunk of text.
This function reads in the entire file as one big piece of text and
returns it. The *encoding*, *errors*, and *newline* keywords are
passed to :meth:`open`.
This is not a good way to read files unless you know for sure that they
are sm... |
def read_toml(self, encoding=None, errors=None, newline=None, **kwargs):
import pytoml
with self.open (mode='rt', encoding=encoding, errors=errors, newline=newline) as f:
return pytoml.load (f, **kwargs) | Read this path as a TOML document.
The `TOML <https://github.com/toml-lang/toml>`_ parsing is done with
the :mod:`pytoml` module. The *encoding*, *errors*, and *newline*
keywords are passed to :meth:`open`. The remaining *kwargs* are passed
to :meth:`toml.load`.
Returns the dec... |
def read_yaml (self, encoding=None, errors=None, newline=None, **kwargs):
import yaml
with self.open (mode='rt', encoding=encoding, errors=errors, newline=newline) as f:
return yaml.load (f, **kwargs) | Read this path as a YAML document.
The YAML parsing is done with the :mod:`yaml` module. The *encoding*,
*errors*, and *newline* keywords are passed to :meth:`open`. The
remaining *kwargs* are passed to :meth:`yaml.load`.
Returns the decoded data structure. |
def write_pickles (self, objs):
try:
import cPickle as pickle
except ImportError:
import pickle
with self.open (mode='wb') as f:
for obj in objs:
pickle.dump (obj, f) | *objs* must be iterable. Write each of its values to this path in sequence
using :mod:`cPickle`. |
def write_yaml (self, data, encoding=None, errors=None, newline=None, **kwargs):
import yaml
with self.open (mode='wt', encoding=encoding, errors=errors, newline=newline) as f:
return yaml.dump (data, stream=f, **kwargs) | Read *data* to this path as a YAML document.
The *encoding*, *errors*, and *newline* keywords are passed to
:meth:`open`. The remaining *kwargs* are passed to :meth:`yaml.dump`. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.