cube3d.py 3.51 KB
Newer Older
Shuai Feng's avatar
Shuai Feng 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import astropy.units as u
import numpy as np
from scipy.interpolate import interp1d
from .spec1d import *
import astropy.wcs

class Cube3D():
    """
    Class of 3-dimentional spectral cube
    """
    def __init__(self, config, stellar_map = None, gas_map = None):
        
        self.config= config
        
        self.nx    = config.nx
        self.ny    = config.ny
        self.dpix  = config.dpix
        self.fov_x = config.fov_x
        self.fov_y = config.fov_y
        
        self.wave  = config.wave
        self.nz    = len(self.wave)
        self.wave0 = np.min(self.wave)
        self.inst_fwhm = config.inst_fwhm
        
        self.flux  = np.zeros((self.nx,self.ny,self.nz))
        
        self.stellar_map = stellar_map
        self.gas_map     = gas_map
        
    def make_cube(self, stellar_tem = None, hii_tem = None):
        
        for i in range(self.nx):
            for j in range(self.ny):
                if self.stellar_map is not None:
                    ss = StellarContinuum(self.config, stellar_tem, mag = self.stellar_map.mag[i,j], 
                                          age = self.stellar_map.age[i,j], feh = self.stellar_map.feh[i,j], 
                                          vel = self.stellar_map.vel[i,j], vdisp = self.stellar_map.vdisp[i,j], 
                                          ebv = self.stellar_map.ebv[i,j])
                if self.gas_map is not None:
                    gg = HII_Region(self.config, hii_tem, halpha = self.gas_map.halpha[i,j], 
                                    logz = self.gas_map.zh[i,j], vel = self.gas_map.vel[i,j], 
                                    vdisp = self.gas_map.vdisp[i,j], ebv = self.gas_map.ebv[i,j])
                    self.flux[i,j,:] = ss.flux + gg.flux
                else:
                    self.flux[i,j,:] = ss.flux
                    
    def wcs_info(self):
        wcs = fits.Header()
        wcs_dict = {'CTYPE1': 'WAVE    ', 
                    'CUNIT1': 'Angstrom', 
                    'CDELT1': self.config.dlam, 
                    'CRPIX1': 1, 
                    'CRVAL1': np.min(self.wave), 
                    'CTYPE2': 'RA---TAN', 
                    'CUNIT2': 'deg', 
                    'CDELT2': self.dpix / 3600., 
                    'CRPIX2': np.round(self.ny / 2.), 
                    'CRVAL2': 0.5, 
                    'CTYPE3': 'DEC--TAN', 
                    'CUNIT3': 'deg', 
                    'CDELT3': self.dpix / 3600., 
                    'CRPIX3': np.round(self.nx / 2.), 
                    'CRVAL3': 1, 
                    'BUNIT' : '10**(-17)*erg/s/cm**2/Angstrom'}
        input_wcs = astropy.wcs.WCS(wcs_dict)
        self.wcs_header = input_wcs.to_header()
        
    def insert_spec(self, spec, dx = 0, dy = 0):
        x0 = np.int(np.round(self.config.nx / 2.))
        y0 = np.int(np.round(self.config.ny / 2.))
        self.flux[x0 + dx, y0 + dy, :] = self.flux[x0 + dx, y0 + dy, :] + spec.flux
        
    def savefits(self, filename, path = './'): 
        
        hdr = fits.Header()
        
        hdr['FILETYPE'] = 'SCICUBE'
        hdr['CODE']     = 'CSST-IFS-GEHONG'
        hdr['VERSION']  = '0.0.1'
        
        hdr['OBJECT']   = 'NGC1234'
        hdr['RA']       = 0.0
        hdr['DEC']      = 0.0
        
        hdu0 = fits.PrimaryHDU(header = hdr)
        
        self.wcs_info()
        hdr = self.wcs_header
        hdu1 = fits.ImageHDU(self.flux, header = hdr)
        
        # Output
        hdulist = fits.HDUList([hdu0, hdu1])
        hdulist.writeto(path + filename, overwrite = True)