gofio2ascii.py 1.83 KB
Newer Older
Monica Rainer's avatar
Monica Rainer committed
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
63
64
65
from astropy.io import fits,ascii
import numpy as np

lista = raw_input('List of s1d/ms1d fits files to convert in ascii (default = fits.lis):\n')

if lista == '':
	lista='fits.lis'

split = raw_input('Do you want to split the orders of the ms1d files (if any) ([y/n] default yes)? ')
if split == '':
    split = 'y'

lis = open(lista,'r')

with open(lista) as framelist:
    for frame in framelist:
        frame = frame.strip()
	calib_ascii = frame.replace('.fits','.txt')

        spettro = fits.open(frame)

        try:
            sdata = spettro[1].data
            wcalib = sdata.field(1)
            spec = sdata.field(2)
            snr = sdata.field(3)

            for o in xrange(50):

                wcalib[o] = wcalib[o][::-1]
                spec[o] = spec[o][::-1]
                snr[o] = snr[o][::-1]
                if split == 'y':
                    txt = '_' + str(o+32) + '.txt'
                    order_ascii = frame.replace('.fits',txt)
                    order = np.vstack((wcalib[o],spec[o],snr[o]))
                    ascii.write(np.transpose(order),order_ascii, format='no_header')
                    print 'Saved %s' % (order_ascii)

            if split == 'n':
                optCal = np.vstack((np.concatenate(np.flipud(wcalib)),np.concatenate(np.flipud(spec)),np.concatenate(np.flipud(snr))))
                ascii.write(np.transpose(optCal),calib_ascii, format='no_header')
                print 'Saved %s' % (calib_ascii)

        except:
            hea = spettro[0].header
            data = spettro[0].data

            start = hea['CRVAL1']
            step = hea['CDELT1']

            new = open(calib_ascii,'w')

            k = 0
            while k < len(data):

		wave = start + (k*step)
		new.write(str(wave) + '\t' + str(data[k]) + '\n')
		k = k+1

            new.close()

            print 'Saved %s' % (calib_ascii)