Star.py 2.36 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
import galsim
Fang Yuedong's avatar
Fang Yuedong committed
2
3
import os
import sys
Fang Yuedong's avatar
Fang Yuedong committed
4
5
6
7
8
import numpy as np
import astropy.constants as cons
from astropy.table import Table
from scipy import interpolate

9
10
11
from ObservationSim.MockObject._util import integrate_sed_bandpass, getNormFactorForSpecWithABMAG, getObservedSED, getABMAG, tag_sed
from ObservationSim.MockObject.MockObject import MockObject

Fang Yuedong's avatar
Fang Yuedong committed
12

Fang Yuedong's avatar
Fang Yuedong committed
13
class Star(MockObject):
14
15
    def __init__(self, param, logger=None):
        super().__init__(param, logger=logger)
Fang Yuedong's avatar
Fang Yuedong committed
16
17
        if not hasattr(self, "mu"):
            self.mu = 1.
Fang Yuedong's avatar
Fang Yuedong committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31

    def unload_SED(self):
        """(Test) free up SED memory
        """
        del self.sed

    def getGSObj(self, psf, g1=0, g2=0, flux=None, filt=None, tel=None, exptime=150.):
        if flux == None:
            flux = self.getElectronFluxFilt(filt, tel, exptime)
        # star = galsim.Gaussian(sigma=1.e-8, flux=1.)
        star = galsim.DeltaFunction()
        star = star.withFlux(flux)
        final = galsim.Convolve(psf, star)
        return final
Fang Yuedong's avatar
Fang Yuedong committed
32

Fang Yuedong's avatar
Fang Yuedong committed
33
34
    def getGSObj_multiband(self, tel, psf_list, bandpass_list, filt, nphotons_tot=None, g1=0, g2=0, exptime=150.):
        if len(psf_list) != len(bandpass_list):
Fang Yuedong's avatar
Fang Yuedong committed
35
36
            raise ValueError(
                "!!!The number of PSF profiles and the number of bandpasses must be equal.")
Fang Yuedong's avatar
Fang Yuedong committed
37
38
39
40
41
        objs = []
        if nphotons_tot == None:
            nphotons_tot = self.getElectronFluxFilt(filt, tel, exptime)

        try:
Fang Yuedong's avatar
Fang Yuedong committed
42
43
            full = integrate_sed_bandpass(
                sed=self.sed, bandpass=filt.bandpass_full)
Fang Yuedong's avatar
Fang Yuedong committed
44
45
        except Exception as e:
            print(e)
46
            self.logger.error(e)
Fang Yuedong's avatar
Fang Yuedong committed
47
48
49
50
            return -1

        for i in range(len(bandpass_list)):
            bandpass = bandpass_list[i]
51
            psf = psf_list[i]
Fang Yuedong's avatar
Fang Yuedong committed
52
53
54
55
            try:
                sub = integrate_sed_bandpass(sed=self.sed, bandpass=bandpass)
            except Exception as e:
                print(e)
56
                self.logger.error(e)
Fang Yuedong's avatar
Fang Yuedong committed
57
                return -1
Fang Yuedong's avatar
Fang Yuedong committed
58

Fang Yuedong's avatar
Fang Yuedong committed
59
60
61
62
63
64
65
66
67
68
69
            ratio = sub/full

            if not (ratio == -1 or (ratio != ratio)):
                nphotons = ratio * nphotons_tot
            else:
                return -1
            star = galsim.DeltaFunction()
            star = star.withFlux(nphotons)
            star = galsim.Convolve(psf, star)
            objs.append(star)
        final = galsim.Sum(objs)
Fang Yuedong's avatar
Fang Yuedong committed
70
        return final