summaryrefslogtreecommitdiff
path: root/wcsutil/getinput.py
blob: a2d97816d65da2e1fae7c3e03661640c90cb9729 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import pyfits
from pytools import irafglob, fileutil, parseinput

def parseSingleInput(f=None, ext=None):
    if isinstance(f, str):
        # create an HSTWCS object from a filename
        if ext != None:
            filename = f
            if isinstance(ext,tuple):
                if ext[0] == '':
                    extnum = ext[1] # handle ext=('',1)
                else:
                    extnum = ext
            else:
                extnum = int(ext)
        elif ext == None:
            filename, ext = fileutil.parseFilename(f)
            ext = fileutil.parseExtn(ext)
            if ext[0] == '':
                extnum = int(ext[1]) #handle ext=('',extnum)
            else:
                extnum = ext
        phdu = pyfits.open(filename)
        hdr0 = pyfits.getheader(filename)
        try:
            ehdr = pyfits.getheader(filename, ext=extnum)
        except (IndexError,KeyError):
            print 'Unable to get extension.', extnum
            raise

    elif isinstance(f, pyfits.HDUList):
        phdu = f
        if ext == None:
            extnum = 0
        else:
            extnum = ext
        ehdr = f[extnum].header
        hdr0 = f[0].header
        filename = hdr0.get('FILENAME', "")
        
    else:
        raise ValueError('Input must be a file name string or a pyfits file object')

    return filename, hdr0, ehdr, phdu


def parseMultipleInput(input):
    if isinstance(input, str):
        if input[0] == '@':
            # input is an @ file
            filelist = irafglob.irafglob(input)
        else:
            try:
                filelist, output = parseinput.parseinput(input)
            except IOError: raise
    elif isinstance(input, list):
        if isinstance(input[0], wcsutil.HSTWCS):
            # a list of HSTWCS objects
            return input
        else:
            filelist = input[:]
    return filelist