In [16]:
import os
import sys
from pyraf import iraf
from astropy.io import fits

In [49]:
def wspectext_allap(filepath, apaxis=0):
    nrow, ncol = fits.open(filepath)[0].data.shape
    print '@Cham: nrow = %d, ncol=%d'%(nrow, ncol)
    
    iraf.noao()
    iraf.noao.onedspec()
    
    filename = filepath.split(os.path.sep)[-1]
    
    # determine dirname & dirpath
    dirname = filepath.split(os.path.sep)[-1].replace('.fits','')
    if os.path.dirname(filepath) == '':
        dirpath = dirname
    else:
        dirpath = os.path.dirname(filepath) + os.path.sep + dirname

    if os.path.exists(dirpath):
        # if dirpath exists
        print '@Cham: directory exists ... (%s)'%dirpath
    else:
        # if dirpath doesn't exist, mkdir
        os.mkdir(dirpath)
        print '@Cham: mkdir %s'%dirpath
    
    # execute wspectest
    if not apaxis == 1:
        for apnum in xrange(1, nrow+1):
            _input = '%s[1:%d, %d]'%(filename, ncol, apnum)
            _output = dirpath + os.sep + filename.replace('.fits', '_%04d.dat'%apnum)
            print '@Cham: wspectext running ... [%s]'%_output
            iraf.onedspec.wspectext(input=_input, output=_output, header='no')
    else:
        for apnum in xrange(1, ncol+1):
            _input = '%s[%d, 1:%d]'%(filename, apnum, nrow)
            _output = dirpath + os.sep + filename.replace('.fits', '_%04d.dat'%apnum)
            print '@Cham: wspectext running ... [%s]'%_output
            iraf.onedspec.wspectext(input=_input, output=_output, header='no')
    print '@Cham: ----------------------'
    print '@Cham: mission complete!'
    print '@Cham: ----------------------'


103 4096

In [ ]:
if __name__ == '__main__':
    # ipython py_wspectxt ./w*s.fits
    if len(sys.argv) < 2:
        print '@Cham: not enough arguments ...'
    elif len(sys.argv) == 2:
        wspectext_allap(sys.argv[1])
    elif len(sys.argv) >= 3:
        if sys.argv[-1]=='0' or sys.argv[-1]=='1':
            # apaxis specified
            for i in xrange(1, len(sys.argv)-1):
                wspectext_allap(sys.argv[i], sys.argv[-1])
        else:
            # apaxis not specified
            for i in xrange(1, len(sys.argv)):
                wspectext_allap(sys.argv[i])

In [ ]:


In [ ]: