def readlist(param): """Returns list from epar parameter. It accepts the following for *param* A filename prefixed by @, for example:: readlist('@images') will read names from the file images. A comma separated list of filenames """ # Ensure parameter is a string without leading or trailing whitespace try: param = str(param).strip() except: raise SaltIOError('Cannot convert input argument to string.') if param[0] == '@': try: f = open(param[1:], 'r') output = [name.strip() for name in f] except: raise SaltIOError('Could not read from input file ' + param[1:]) else: output = [name.strip() for name in param.split(',')] return output
def cleanpropcode(pids, propids): """Create the list of appropriate proprosal codes""" props = [] # Make a list of all the Propcodes that are observed for pid in propids: props.extend(pid.upper().split(',')) # Check to make sure that the requested propocodes # are in that night's observation or set the propocodes # to all of that nights observatoins if (pids[0].upper() != 'ALL'): for pid in pids: for pid in pid.split(','): if (pid.upper().strip() not in set(props)): raise SaltIOError( 'Propcode ' + pid.upper() + ' is not recorded in the observation log ') else: pids = set(props) pids = removebadpids(pids) if not pids: raise SaltIOError('Propcode list is empty') return pids
def filesexist(infiles, path, mode): """check files in list exist""" if (path != ''): if (path[len(path) - 1] != '/'): path += '/' for fileitem in infiles: if (mode == 'r'): if (not os.path.isfile(path + fileitem)): raise SaltIOError('file ' + path + fileitem + ' does not exist') elif (mode == 'w'): if (os.path.isfile(path + fileitem)): raise SaltIOError('file ' + path + fileitem + ' already exists')
def filedefined(filetype, file): """has a file been defined?""" file = file.strip() if (len(file) == 0 or file.count(' ') > 0): raise SaltIOError('filetype ' + filetype + 'file(s) ' + file + ' not specified')
def closeascii(file): """close ASCII file""" try: file.close() except: raise SaltIOError('Cannot close ASCII file ' + file)
def readgaindb(gaindb): """read gain database file""" dbspeed = [] dbrate = [] dbgain = [] dbnoise = [] dbbias = [] dbamp = [] try: gainfile = open(gaindb, 'r') for line in gainfile: if (len(line.strip()) > 0 and line[0] != '#'): line = line.rstrip('\r\n') line = re.sub("\s+", ",", line) line.rstrip(',') entries = line.split(',') dbspeed.append(entries[0]) dbrate.append(entries[1]) dbgain.append(entries[2]) dbnoise.append(entries[3]) dbbias.append(entries[4]) dbamp.append(entries[5].strip('amp')) except: raise SaltIOError('Cannot read gain database file ' + gaindb) return dbspeed, dbrate, dbgain, dbnoise, dbbias, dbamp
def readresponse(response_file): """Read in the response file""" try: response = np.loadtxt(response_file, usecols=[0], unpack=True) except Exception as e: raise SaltIOError('Could not read %s due to %s' % (response_file, e)) return response.reshape(len(y), 1)
def readsrcfile(srcfile): """Read in the src file and return the parameters extracted from that file """ #set up some variables amp = {} x = {} y = {} x_o = {} y_o = {} r = {} br1 = {} br2 = {} tgt_btype = 'region' cmp_btype = 'region' # check extraction region defintion file exists srcfile = srcfile.strip() saltio.fileexists(srcfile) #try loading the region file try: region = np.round(np.loadtxt(srcfile)) except Exception, e: msg = 'SLOTTOOLs--ERROR: Unable to load array from %s' % srcfile raise SaltIOError(msg)
def copy(file1, file2): """copy file""" try: shutil.copy2(file1, file2) except Exception, e: raise SaltIOError('Could not copy %s to %s due to %s' % (file1, file2, e))
def move(file1, file2): """move file""" try: shutil.move(file1, file2) except Exception, e: raise SaltIOError('Could not move %s to %s due to %s' % (file1, file2, e))
def newfitstable(table, infile=None): """write FITS table""" struct = '' try: struct = fits.BinTableHDU.from_columns(table) except Exception, e: raise SaltIOError('Cannot create new table because %s' % e)
def changedir(path): """change working directory""" path = path.strip() try: os.chdir(path) except: raise SaltIOError('Could not move to directory ' + path)
def rem(keyword, hdu, file): """delete keyword""" try: del hdu.header[keyword] except: raise SaltIOError('Cannot delete keyword ' + keyword + ' in ' + file)
def readccdgeom(geomfile): """read CCD geometry definition file""" gap = 0. xshift = [0., 0.] yshift = [0., 0.] rot = [0., 0.] try: gfile = open(geomfile, 'r') for line in gfile: if (len(line.strip()) > 0 and line[0] != '#'): line = line.rstrip('\r\n') line = line.rstrip().lstrip() line = re.sub("\s+", ",", line) pars = line.split(',') gap = float(pars[1]) xshift[0] = float(pars[2]) yshift[0] = float(pars[3]) rot[0] = float(pars[4]) if (len(pars) == 8): xshift[1] = float(pars[5]) yshift[1] = float(pars[6]) rot[1] = float(pars[7]) except Exception, e: raise SaltIOError( 'Cannot read geometry definition parameters in file %s because %s' % (geomfile, e))
def openupdatefits(infile): """open FITS file for updating""" try: struct = fits.open(infile, mode='update') except: raise SaltIOError('Cannot open ' + infile + ' as a FITS file') struct = None return struct
def tmpfile(path): """create a temporary file name""" try: tempfile.tempdir = path infile = tempfile.mktemp() except Exception, e: infile = '' raise SaltIOError('Cannot create temporary file name because %s' % e)
def pathexists(path): """check that a path exists and name ends with a /""" path = path.strip() if (path[-1] != '/'): path += '/' if (not os.path.exists(path)): raise SaltIOError('Path ' + path[:-1] + ' does not exist') return path
def fitscolumns(columns): """construct FITS table columns""" table = '' try: table = fits.ColDefs(columns) except: raise SaltIOError('Cannot define table columns') return table
def openfits(infile, mode='copyonwrite', memmap=False): """open FITS file""" try: struct = fits.open(infile, mode=mode, memmap=memmap) except Exception, e: msg = 'Cannot open %s as a FITS file because %s' % (infile, e) raise SaltIOError(msg) struct = None
def put(keyword, value, hdu, infile=None): """change existing keyword value""" try: hdu.header[keyword] = value except: if infile is None: infile = getimagename(hdu, base=False) raise SaltIOError('Cannot update keyword ' + keyword + ' in ' + infile)
def writefits(struct, outfile, clobber=True): """write to FITS file""" if (os.path.isfile(outfile) and clobber): delete(outfile) try: struct.writeto(outfile) except Exception, e: raise SaltIOError('Cannot write %s because %s' % (outfile, e))
def prepare(struct, file, keyprep): """has file been prepared?""" try: prep = struct[0].header[keyprep] except: raise SaltIOError('File ' + file + ' has not been prepared for SALT IRAF/PyRAF tools')
def match(keyword, value1, hdu, file=None): """does keyword match prediction""" if file is None: file = getimagename(hdu, base=False) value2 = get(keyword, hdu, file) if value1 != value2: raise SaltIOError(file + '[' + keyword + '] .not. = ' + value1)
def getimagename(hdu, base=True): """Return the file name of an hdu""" try: name = hdu._file.name except: raise SaltIOError('Cannot determine file name') if base: return os.path.basename(name) return name
def exist(keyword, hdu, file=None): """does keyword exist""" if file is None: file = getimagename(hdu, base=False) try: value = hdu.header[keyword] except: raise SaltIOError(keyword + ' does not exist in file ' + file)
def copy(new, old, key): """copy keyword""" if found(key, old): try: oldcard = old.header.cards new.header[key] = (oldcard[key].value, oldcard[key].comment) except: raise SaltIOError('Cannot COPY KEYWORD ' + key)
def history(struct, message, infile=None): """add history keyword""" struct.header.add_history(str(message)) try: pass except Exception, e: if infile is None: infile = getimagename(struct, base=False) raise SaltIOError('Cannot write HISTORY keyword to %s because %s' % (infile, e))
def readheader(struct, hdu): """read image from HDU structure""" headerdata = [] try: headerdata = struct[hdu].header except: raise SaltIOError('Cannot read header data from HDU ' + str(hdu)) return headerdata
def symlink(infile, linkfile, clobber): """create symbolic link""" # delete file if one of the same name already exists if (os.path.exists(linkfile) and not clobber): raise SaltIOError('file ' + linkfile + ' exists, use clobber=y') if clobber: try: os.remove(linkfile) except: pass # create symbolic link try: os.symlink(infile, linkfile) except: raise SaltIOError('could not create symbolic link from ' + infile + ' to ' + linkfile)
def readimage(struct, hdu): """read image from HDU structure""" imagedata = [] try: imagedata = struct[hdu].data except: raise SaltIOError('Cannot read image data from HDU ' + str(hdu)) return imagedata