from astropy.table import Table
import os, errno, shutil
from astropy.io import fits as pyfits
import pdb
# Load up directory aliases
[docs]
module_dir = os.path.dirname(__file__)
[docs]
dir_alias_file = module_dir + '/../data/directory_aliases.txt'
[docs]
dir_alias = Table.read(dir_alias_file, format='ascii.fast_no_header')
dir_alias.rename_column('col1', 'dir')
dir_alias.rename_column('col2', 'alias')
[docs]
def rmall(files):
"""Remove list of files without confirmation."""
for file in files:
if os.access(file, os.F_OK): os.remove(file)
return
[docs]
def mkdir(dir):
"""Make directory if it doesn't already exist."""
try:
os.makedirs(dir)
except OSError as exc:
if exc.errno == errno.EEXIST:
pass
else:
raise
return
[docs]
def getcwd():
"""
IRAF doesn't like long file names. This reduces them.
"""
curdir = os.getcwd()
for ii in range(len(dir_alias)):
curdir = curdir.replace(dir_alias['dir'][ii], dir_alias['alias'][ii])
curdir += '/'
return curdir
[docs]
def trimdir(olddir):
"""
IRAF doesn't like long file names. This reduces them.
"""
for ii in range(len(dir_alias)):
olddir = olddir.replace(dir_alias['dir'][ii], dir_alias['alias'][ii])
return olddir
[docs]
def cp_change_prefix(arg1,arg2):
"""
Takes files beginning with arg1 and replaces them with arg2
Must be in the directory where files live
"""
# Find files in this directory beginning with arg1
files = os.listdir(".")
# Ignore files beginning with '.'
files=[filename for filename in files if filename[0] != '.']
ln = len(arg1)
for ff in range(len(files)):
pre = files[ff][0:ln]
if pre == arg1:
suf = files[ff][len(arg1):]
newFile = arg2 + suf
shutil.copy(files[ff], newFile)
return
[docs]
def cp_change_suffix(arg1,arg2):
"""
Takes files ending with arg1 and replaces them with arg2
Must be in the directory where files live
"""
# Find files in this directory ending with arg1
files = os.listdir(".")
# Ignore files beginning with '.'
files=[filename for filename in files if filename[0] != '.']
ln = len(arg1)
for ff in range(len(files)):
suf = files[ff][len(files[ff])-len(arg1):]
if suf == arg1:
pre = files[ff][0:len(files[ff])-len(arg1)]
newFile = pre + arg2
shutil.copy(files[ff], newFile)
return