Package pyraf :: Module iraffunctions
[hide private]
[frames] | no frames]

Module iraffunctions

source code

module iraffunctions.py -- IRAF emulation tasks and functions

This is not usually used directly -- the relevant public classes and functions get included in iraf.py. The implementations are kept here to avoid possible problems with name conflicts in iraf.py (which is the home for all the IRAF task and package names.) Private routines have names beginning with '_' and do not get imported by the iraf module.

The exception is that iraffunctions can be used directly for modules that must be compiled and executed early, before the pyraf module initialization is complete.

$Id: iraffunctions.py 807 2007-11-27 20:38:53Z sontag $

R. White, 2000 January 20

Functions [hide private]
 
setVerbose(value=1, **kw)
Set verbosity level when running tasks
source code
 
_writeError(msg)
Write a message to stderr
source code
 
Init(doprint=1, hush=0, savefile=None)
Basic initialization of IRAF environment
source code
 
_getIrafEnv(file='/usr/local/bin/cl', vars=('IRAFARCH','iraf'))
Returns dictionary of environment vars defined in cl startup file
source code
 
saveToFile(savefile, **kw)
Save IRAF environment to pickle file
source code
 
restoreFromFile(savefile, doprint=1, **kw)
Initialize IRAF environment from pickled save file (or file handle)
source code
 
_addPkg(pkg)
Add an IRAF package to the packages list
source code
 
_addTask(task, pkgname=None)
Add an IRAF task to the tasks list
source code
 
addLoaded(pkg)
Add an IRAF package to the loaded pkgs list
source code
 
load(pkgname, args=(), kw=None, doprint=1, hush=0, save=1)
Load an IRAF package by name
source code
 
run(taskname, args=(), kw=None, save=1)
Run an IRAF task by name
source code
 
getAllTasks(taskname)
Returns list of names of all IRAF tasks that may match taskname
source code
 
getAllPkgs(pkgname)
Returns list of names of all IRAF pkgs that may match pkgname
source code
 
getTask(taskname, found=0)
Find an IRAF task by name using minimum match
source code
 
getPkg(pkgname, found=0)
Find an IRAF package by name using minimum match
source code
 
getTaskList()
Returns list of names of all defined IRAF tasks
source code
 
getTaskObjects()
Returns list of all defined IrafTask objects
source code
 
getPkgList()
Returns list of names of all defined IRAF packages
source code
 
getLoadedList()
Returns list of names of all loaded IRAF packages
source code
 
getVarDict()
Returns dictionary all IRAF variables
source code
 
getVarList()
Returns list of names of all IRAF variables
source code
 
listAll(hidden=0, **kw)
List IRAF packages, tasks, and variables
source code
 
listPkgs(**kw)
List IRAF packages
source code
 
listLoaded(**kw)
List loaded IRAF packages
source code
 
listTasks(pkglist=None, hidden=0, **kw)
List IRAF tasks, optionally specifying a list of packages to include
source code
 
listCurrent(n=1, hidden=0, **kw)
List IRAF tasks in current package (equivalent to '?' in the cl)
source code
 
listVars(prefix="", equals="\t= ", **kw)
List IRAF variables
source code
 
clParGet(paramname, pkg=None, native=1, mode=None, prompt=1)
Return value of IRAF parameter
source code
 
envget(var, default=None)
Get value of IRAF or OS environment variable
source code
 
mktemp(root)
Make a temporary filename starting with root
source code
 
isNullFile(s)
Returns true if this is the CL null file
source code
 
substr(s, first, last)
Return sub-string using IRAF 1-based indexing
source code
 
strlen(s)
Return length of string
source code
 
isindef(s)
Returns true if argument is INDEF
source code
 
stridx(test, s)
Return index of first occurrence of any of the characters in 'test' that are in 's' using IRAF 1-based indexing
source code
 
strldx(test, s)
Return index of last occurrence of any of the characters in 'test' that are in 's' using IRAF 1-based indexing
source code
 
strlwr(s)
Return string converted to lower case
source code
 
strupr(s)
Return string converted to upper case
source code
 
strstr(str1, str2)
Search for first occurrence of 'str1' in 'str2', returns index of the start of 'str1' or zero if not found.
source code
 
strlstr(str1, str2)
Search for last occurrence of 'str1' in 'str2', returns index of the start of 'str1' or zero if not found.
source code
 
frac(x)
Return fractional part of x
source code
 
real(x)
Return real/float representation of x
source code
 
integer(x)
Return integer representation of x
source code
 
mod(a, b)
Return a modulo b
source code
 
nint(x)
Return nearest integer of x
source code
 
radix(value, base=10, length=0)
Convert integer value to string expressed using given base
source code
 
sin(value)
Trigonometric sine function
source code
 
cos(value)
Trigonometric cosine function
source code
 
tan(value)
Trigonometric tangent function
source code
 
atan2(x, y)
Trigonometric 2-argument arctangent function
source code
 
exp(value)
Exponential function
source code
 
log(value)
Natural log function
source code
 
log10(value)
Base 10 log function
source code
 
sqrt(value)
Square root function
source code
 
absvalue(value)
Absolute value function
source code
 
minimum(*args)
Minimum of list of arguments
source code
 
maximum(*args)
Maximum of list of arguments
source code
 
osfn(filename)
Convert IRAF virtual path name to OS pathname
source code
 
clSexagesimal(d, m, s=0)
Convert d:m:s value to float
source code
 
clDms(x, digits=1, seconds=1, deg=None)
Convert float to d:m:s.s
source code
 
defpar(paramname)
Returns true if parameter is defined
source code
 
access(filename)
Returns true if file exists
source code
 
_denode(filename)
Remove IRAF "node!" specification from filename
source code
 
_imextn()
Returns list of image types and extensions
source code
 
_checkext(ext, extlist)
Returns image type if ext is in extlist, else returns None
source code
 
_searchext(root, extlist)
Returns image type if file root.ext is found (ext from extlist)
source code
 
imaccess(filename)
Returns true if image matching name exists and is readable
source code
 
defvar(varname)
Returns true if CL variable is defined
source code
 
deftask(taskname)
Returns true if CL task is defined
source code
 
defpac(pkgname)
Returns true if CL package is defined and loaded
source code
 
curpack()
Returns name of current CL package
source code
 
curPkgbinary()
Returns name pkgbinary directory for current CL package
source code
 
bool2str(value)
Convert IRAF boolean value to a string
source code
 
boolean(value)
Convert Python native types (string, int, float) to IRAF boolean
source code
 
fscan(locals, line, *namelist, **kw)
fscan function sets parameters from a string or list parameter
source code
 
fscanf(locals, line, format, *namelist, **kw)
fscanf function sets parameters from a string/list parameter with format
source code
 
_weirdEOF(locals, namelist) source code
 
_isStruct(locals, name, checklegal=0)
Returns true if the variable `name' is of type struct
source code
 
scan(locals, *namelist, **kw)
Scan function sets parameters from line read from stdin
source code
 
scanf(locals, format, *namelist, **kw)
Formatted scan function sets parameters from line read from stdin
source code
 
nscan()
Return number of items read in last scan function
source code
 
set(*args, **kw)
Set IRAF environment variables
source code
 
reset(*args, **kw)
Set IRAF environment variables
source code
 
show(*args, **kw)
Print value of IRAF or OS environment variables
source code
 
unset(*args, **kw)
Unset IRAF environment variables
source code
 
time(**kw)
Print current time and date
source code
 
sleep(seconds=0, **kw)
Sleep for specified time
source code
 
beep(**kw)
Beep to terminal (even if output is redirected)
source code
 
clOscmd(s, **kw)
Execute a system-dependent command in the shell, returning status
source code
 
stty(terminal=None, **kw)
IRAF stty command (mainly not implemented)
source code
 
eparam(*args, **kw)
Edit parameters for tasks.
source code
 
tparam(*args, **kw)
Edit parameters for tasks.
source code
 
lparam(*args, **kw)
List parameters for tasks
source code
 
dparam(*args, **kw)
Dump parameters for task in executable form
source code
 
update(*args, **kw)
Update task parameters on disk
source code
 
unlearn(*args, **kw)
Unlearn task parameters -- restore to defaults
source code
 
edit(*args, **kw)
Edit text files
source code
 
clear(*args, **kw)
Clear screen if output is terminal
source code
 
flprcache(*args, **kw)
Flush process cache.
source code
 
prcache(*args, **kw)
Print process cache.
source code
 
gflush(*args, **kw)
Flush any buffered graphics output.
source code
 
pyexecute(filename, **kw)
Execute python code in filename (which may include IRAF path).
source code
 
history(n=20, *args, **kw)
Print history
source code
 
ehistory(*args, **kw)
Dummy history function
source code
 
clNoBackground(*args, **kw)
Dummy background function
source code
 
wait(*args, **kw)
Dummy background function
source code
 
kill(*args, **kw)
Dummy background function
source code
 
service(*args, **kw)
Dummy background function
source code
 
jobs(*args, **kw)
Dummy background function
source code
 
clDummy(*args, **kw)
Dummy do-nothing function
source code
 
language(*args, **kw)
Dummy do-nothing function
source code
 
cache(*args, **kw)
Dummy do-nothing function
source code
 
clbye(*args, **kw)
Dummy do-nothing function
source code
 
logout(*args, **kw)
Dummy do-nothing function
source code
 
keep(*args, **kw)
Dummy do-nothing function
source code
 
bye(*args, **kw)
Dummy do-nothing function
source code
 
_notImplemented(cmd, args, kw)
Dummy unimplemented function
source code
 
putlog(*args, **kw) source code
 
clAllocate(*args, **kw) source code
 
clDeallocate(*args, **kw) source code
 
clDevstatus(*args, **kw) source code
 
fprint(*args, **kw)
Error unimplemented function
source code
 
pkgHelp(pkgname=None, **kw)
Give help on package (equivalent to CL '? [taskname]')
source code
 
allPkgHelp(**kw)
Give help on all packages (equivalent to CL '??')
source code
 
_clProcedure(*args, **kw)
Core function for the CL task
source code
 
clProcedure(input=None, mode="", DOLLARnargs=0, **kw)
Run CL commands from a file (cl < input) -- OBSOLETE
source code
 
hidetask(*args, **kw)
Hide the CL task in package listings
source code
 
task(*args, **kw)
Define IRAF tasks
source code
 
redefine(*args, **kw)
Redefine an existing task
source code
 
package(pkgname=None, bin=None, PkgName='', PkgBinary='', **kw)
Define IRAF package, returning tuple with new package name and binary
source code
 
clPrint(*args, **kw)
CL print command -- emulates CL spacing and uses redirection keywords
source code
 
_quietConv(w, d, c, args, i)
Format codes that are quietly converted to %s
source code
 
_boolConv(w, d, c, args, i)
Boolean gets converted to upper case before printing
source code
 
_badConv(w, d, c, args, i)
Format codes that are converted to %s with warning
source code
 
_hConv(w, d, c, args, i)
Handle %h %m %H %M dd:mm:ss.s formats
source code
 
_rConv(w, d, c, args, i)
Handle W.DrN general radix format
source code
 
_wConv(w, d, c, args, i)
Handle %w format, which is supposed to generate spaces
source code
 
printf(format, *args, **kw)
Formatted print function
source code
 
pwd(**kw)
Print working directory
source code
 
chdir(directory=None, **kw)
Change working directory
source code
 
cd(directory=None, **kw)
Change working directory
source code
 
back(**kw)
Go back to previous working directory
source code
 
error(errno=0, errmsg='', task="error", _save=False, suppress=True)
Print error message
source code
 
errno(_save=None)
Returns status from last call to error()
source code
 
errcode(_save=None)
Returns status from last call to error()
source code
 
errmsg(_save=None)
Returns message from last call to error()
source code
 
errtask(_save=None)
Returns task from last call to error()
source code
 
clCompatibilityMode(verbose=0, _save=0)
Start up full CL-compatibility mode
source code
 
clArray(array_size, datatype, name="<anonymous>", mode="h", min=None, max=None, enum=None, prompt=None, init_value=None, strict=0)
Create an IrafPar object that can be used as a CL array
source code
 
clExecute(s, locals=None, mode="proc", local_vars_dict={}, local_vars_list=[], verbose=0, **kw)
Execute a single cl statement
source code
 
clLineToPython(line)
Returns the Python code corresponding to a single cl statement.
source code
 
Expand(instring, noerror=0)
Expand a string with embedded IRAF variables (IRAF virtual filename)
source code
 
_expand1(instring, noerror)
Expand a string with embedded IRAF variables (IRAF virtual filename)
source code
 
IrafTaskFactory(prefix='', taskname=None, suffix='', value=None, pkgname=None, pkgbinary=None, redefine=0, function=None)
Returns a new or existing IrafTask, IrafPset, or IrafPkg object
source code
 
IrafPsetFactory(prefix, taskname, suffix, value, pkgname, pkgbinary, redefine=0)
Returns a new or existing IrafPset object
source code
 
IrafPkgFactory(prefix, taskname, suffix, value, pkgname, pkgbinary, redefine=0)
Returns a new or existing IrafPkg object
source code
 
redirProcess(kw)
Process Stdout, Stdin, Stderr keywords used for redirection
source code
 
redirApply(redirKW)
Modify _sys.stdin, stdout, stderr using the redirKW dictionary
source code
 
redirReset(resetList, closeFHList)
Restore _sys.stdin, stdout, stderr to their original values
source code
Variables [hide private]
  _sys = sys
  _os = os
  _string = string
  _re = re
  _math = math
  _types = types
  _time = time
  _fnmatch = fnmatch
  _glob = glob
  _linecache = linecache
  _StringIO = StringIO
  _pickle = pickle
  _minmatch = minmatch
  _subproc = subproc
  _wutil = wutil
  _irafnames = irafnames
  _irafutils = irafutils
  _iraftask = iraftask
  _irafpar = irafpar
  _irafexecute = irafexecute
  _cl2py = cl2py
  _varDict = {}
  _tasks = {}
  _mmtasks = _minmatch.MinMatchDict()
  _pkgs = _minmatch.MinMatchDict()
  _loaded = {}
  loadedPath = []
  cl = None
  _unsavedVarsDict = {}
  _tmpfileCounter = 0
  _NullFileList = ["dev$null", "/dev/null"]
  _NullPath = None
  _radixDigits = list(_string.digits+ _string.uppercase)
  _denode_pat = _re.compile(r'[^/]*!')
  _nscan = 0
  _sttyArgs = _minmatch.MinMatchDict({'terminal': None, 'baud': ...
  _clearString = None
  _re_taskname = _re.compile(taskname)
  _reFormat = _re.compile(r"%(?P<w>-?\d*)(?P<d>(?:\.\d*)?)(?P<c>...
  _fDispatch = {}
  _backDir = None
  _exitCommands = {"logout": 1, "exit": 1, "quit": 1, ".exit": 1,}
  _clExecuteCount = 0
  __re_var_match = _re.compile(r'(?P<varname>[^$]*)\$')
  __re_var_paren = _re.compile(r'\((?P<varname>[^()]*)\)')

Imports: EOF, userIrafHome, IrafPkg, yes, INDEF, no, userWorkingHome, IrafError, IrafTask, epsilon, pyrafDir, Verbose, SubprocessError, sscanf, iraf, gki, irafecl, cPickle, help


Function Details [hide private]

setVerbose(value=1, **kw)

source code 

Set verbosity level when running tasks

Level 0 (default) prints almost nothing. Level 1 prints warnings. Level 2 prints info on progress.

saveToFile(savefile, **kw)

source code 

Save IRAF environment to pickle file

savefile may be a filename or a file handle. Set clobber keyword (or CL environment variable) to overwrite an existing file.

getTask(taskname, found=0)

source code 

Find an IRAF task by name using minimum match

Returns an IrafTask object. Name may be either fully qualified (package.taskname) or just the taskname. taskname is also allowed to be an IrafTask object, in which case it is simply returned. Does minimum match to allow abbreviated names. If found is set, returns None when task is not found; default is to raise exception if task is not found.

getPkg(pkgname, found=0)

source code 

Find an IRAF package by name using minimum match

Returns an IrafPkg object. pkgname is also allowed to be an IrafPkg object, in which case it is simply returned. If found is set, returns None when package is not found; default is to raise exception if package is not found.

listTasks(pkglist=None, hidden=0, **kw)

source code 

List IRAF tasks, optionally specifying a list of packages to include

Package(s) may be specified by name or by IrafPkg objects.

listCurrent(n=1, hidden=0, **kw)

source code 

List IRAF tasks in current package (equivalent to '?' in the cl)

If parameter n is specified, lists n most recent packages.

clParGet(paramname, pkg=None, native=1, mode=None, prompt=1)

source code 

Return value of IRAF parameter

Parameter can be a cl task parameter, a package parameter for any loaded package, or a fully qualified (task.param) parameter from any known task.

strstr(str1, str2)

source code 

Search for first occurrence of 'str1' in 'str2', returns index of the start of 'str1' or zero if not found. IRAF 1-based indexing

strlstr(str1, str2)

source code 

Search for last occurrence of 'str1' in 'str2', returns index of the start of 'str1' or zero if not found. IRAF 1-based indexing

radix(value, base=10, length=0)

source code 

Convert integer value to string expressed using given base

If length is given, field is padded with leading zeros to reach length. Note that if value is negative, this routine returns the actual bit-pattern of the twos-complement integer (even for base 10) rather than a signed value. This is consistent with IRAF's behavior.

clDms(x, digits=1, seconds=1, deg=None)

source code 

Convert float to d:m:s.s

Number of decimal places on seconds is set by digits. If seconds is false, prints just d:m.m (omits seconds). deg keyword is same as seconds (for backward compatibility).

_imextn()

source code 

Returns list of image types and extensions

The return value is (ktype, globlist) where ktype is the image kernel (oif, fxf, etc.) and globlist is a list of glob-style patterns that match extensions.

_checkext(ext, extlist)

source code 

Returns image type if ext is in extlist, else returns None

Assumes ext starts with a '.' (as returned by os.path.split) and that null extensions can't match.

boolean(value)

source code 

Convert Python native types (string, int, float) to IRAF boolean

Accepts integer/float values 0,1 or string 'yes','no' Also allows INDEF as value

fscan(locals, line, *namelist, **kw)

source code 

fscan function sets parameters from a string or list parameter

Uses local dictionary (passed as first argument) to set variables specified by list of following names. (This is a bit messy, but it is by far the cleanest approach I've thought of. I'm literally using call-by-name for these variables.)

Accepts an additional keyword argument strconv with names of conversion functions for each argument in namelist.

Returns number of arguments set to new values, which may be fewer than the number of variables if an unexpected character is encountered in 'line'. If there are too few space-delimited arguments on the input line, it does not set all the arguments. Returns EOF on end-of-file.

fscanf(locals, line, format, *namelist, **kw)

source code 

fscanf function sets parameters from a string/list parameter with format

Implementation is similar to fscan but is a bit simpler because special struct handling is not needed. Does not allow strconv keyword.

Returns number of arguments set to new values, which may be fewer than the number of variables if an unexpected character is encountered in 'line'. If there are too few space-delimited arguments on the input line, it does not set all the arguments. Returns EOF on end-of-file.

_isStruct(locals, name, checklegal=0)

source code 

Returns true if the variable `name' is of type struct

If checklegal is true, returns true only if variable is struct and does not currently have a legal value.

scan(locals, *namelist, **kw)

source code 

Scan function sets parameters from line read from stdin

This can be used either as a function or as a task (it accepts redirection and the _save keyword.)

scanf(locals, format, *namelist, **kw)

source code 

Formatted scan function sets parameters from line read from stdin

This can be used either as a function or as a task (it accepts redirection and the _save keyword.)

unset(*args, **kw)

source code 

Unset IRAF environment variables

This is not a standard IRAF task, but it is obviously useful. It makes the resulting variables undefined. It silently ignores variables that are not defined. It does not change the os environment variables.

eparam(*args, **kw)

source code 

Edit parameters for tasks. Starts up epar GUI.

tparam(*args, **kw)

source code 

Edit parameters for tasks. Starts up epar GUI.

flprcache(*args, **kw)

source code 

Flush process cache. Takes optional list of tasknames.

prcache(*args, **kw)

source code 

Print process cache. If args are given, locks tasks into cache.

pyexecute(filename, **kw)

source code 

Execute python code in filename (which may include IRAF path).

This is callable from within CL scripts. There is a corresponding pyexecute.cl task that runs outside the PyRAF environment and just prints a warning.

history(n=20, *args, **kw)

source code 

Print history

Does not replicate the IRAF behavior of changing default number of lines to print. (That seems fairly useless to me.)

_clProcedure(*args, **kw)

source code 

Core function for the CL task

Gets passed to IrafPythonTask as function argument. Note I/O redirection has already been set up before calling this.

clProcedure(input=None, mode="", DOLLARnargs=0, **kw)

source code 

Run CL commands from a file (cl < input) -- OBSOLETE

This is obsolete, replaced by the IrafPythonTask version of the cl, using above _clProcedure function. It is being retained only for backward compatibility since translated versions of CL scripts could use it. New versions will not use it.

package(pkgname=None, bin=None, PkgName='', PkgBinary='', **kw)

source code 

Define IRAF package, returning tuple with new package name and binary

PkgName, PkgBinary are old default values. If Stdout=1 is specified, returns output as string array (normal task behavior) instead of returning PkgName, PkgBinary. This inconsistency is necessary to replicate the inconsistent behavior of the package command in IRAF.

Expand(instring, noerror=0)

source code 

Expand a string with embedded IRAF variables (IRAF virtual filename)

Allows comma-separated lists. Also uses os.path.expanduser to replace '~' symbols. Set noerror flag to silently replace undefined variables with just the variable name or null (so Expand('abc$def') = 'abcdef' and Expand('(abc)def') = 'def'). This is the IRAF behavior, though it is confusing and hides errors.

IrafTaskFactory(prefix='', taskname=None, suffix='', value=None, pkgname=None, pkgbinary=None, redefine=0, function=None)

source code 

Returns a new or existing IrafTask, IrafPset, or IrafPkg object

Type of returned object depends on value of suffix and value.

Returns a new object unless this task or package is already defined. In that case if the old task appears consistent with the new task, a reference to the old task is returned. Otherwise a warning is printed and a reference to a new task is returned.

If redefine keyword is set, the behavior is the same except a warning is printed if it does *not* exist.

IrafPsetFactory(prefix, taskname, suffix, value, pkgname, pkgbinary, redefine=0)

source code 

Returns a new or existing IrafPset object

Returns a new object unless this task is already defined. In that case if the old task appears consistent with the new task, a reference to the old task is returned. Otherwise a warning is printed and a reference to a new task is returned.

If redefine keyword is set, the behavior is the same except a warning is printed if it does *not* exist.

IrafPkgFactory(prefix, taskname, suffix, value, pkgname, pkgbinary, redefine=0)

source code 

Returns a new or existing IrafPkg object

Returns a new object unless this package is already defined, in which case a warning is printed and a reference to the existing task is returned. Redefine parameter currently ignored.

Returns a new object unless this package is already defined. In that case if the old package appears consistent with the new package, a reference to the old package is returned. Else if the old package has already been loaded, a warning is printed and the redefinition is ignored. Otherwise a warning is printed and a reference to a new package is returned.

If redefine keyword is set, the behavior is the same except a warning is printed if it does *not* exist.

redirProcess(kw)

source code 

Process Stdout, Stdin, Stderr keywords used for redirection

Removes the redirection keywords from kw Returns (redirKW, closeFHList) which are a dictionary of the filehandles for stdin, stdout, stderr and a list of filehandles to close after execution.

Image and Stdplot redirection not handled (but it isn't clear that these are ever used anyway)

redirApply(redirKW)

source code 

Modify _sys.stdin, stdout, stderr using the redirKW dictionary

Returns a list of the original filehandles so they can be restored (by redirReset)

redirReset(resetList, closeFHList)

source code 

Restore _sys.stdin, stdout, stderr to their original values

Also closes the filehandles in closeFHList. If a tuple with a StringIO pipe is included in closeFHList, that means the value should be returned as the return value of the function. Returns an array of lines (without newlines.)


Variables Details [hide private]

_sttyArgs

Value:
_minmatch.MinMatchDict({'terminal': None, 'baud': 9600, 'ncols': 80, '\
nlines': 24, 'show': no, 'all': no, 'reset': no, 'resize': no, 'clear'\
: no, 'ucasein': no, 'ucaseout': no, 'login': None, 'logio': None, 'lo\
gout': None, 'playback': None, 'verify': no, 'delay': 500,})

_reFormat

Value:
_re.compile(r"%(?P<w>-?\d*)(?P<d>(?:\.\d*)?)(?P<c>[a-zHM])")