Changeset 38


Ignore:
Timestamp:
Jun 10, 2014 10:40:25 AM (10 years ago)
Author:
JohnLightfoot
Message:

TestLine added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/skygenerator.py

    r33 r38  
    1414    """
    1515    freq = wavenumber * sc.c * 100.0
    16     jnu = 2.0 * sc.h * pow(freq,3) / (pow(sc.c,2) *
    17       (math.exp((sc.h * freq) / (sc.k * temperature)) - 1.0))
     16    if freq > 0:
     17        jnu = 2.0 * sc.h * pow(freq,3) / (pow(sc.c,2) *
     18          (math.exp((sc.h * freq) / (sc.k * temperature)) - 1.0))
     19    else:
     20        jnu = 0.0
    1821
    1922    return jnu
     23
     24class BB_spectrum(object):
     25    """Class to generate BB spectrum.
     26    """
     27    def __init__(self, temperature, frequency_axis, cutoffmin, cutoffmax,
     28      emissivity):
     29        self.temperature = temperature
     30        self.frequency_axis = frequency_axis
     31        self.cutoffmin = cutoffmin
     32        self.cutoffmax = cutoffmax
     33        self.emissivity = emissivity
     34
     35    def calculate(self):
     36        spectrum = np.zeros(np.shape(self.frequency_axis))
     37        # ignore floating point errors
     38        old_settings = np.seterr(all='ignore')
     39
     40        for iwn,wn in enumerate(self.frequency_axis):
     41            # simulate real-life 'rounded' cutoffs numerically 
     42            f1 = 1.0 / (1.0 + pow(self.cutoffmin/wn, 18) +
     43              pow(wn/self.cutoffmax, 24))
     44            spectrum[iwn] = black_body(self.temperature, wn) * f1 * \
     45              self.emissivity
     46
     47        # restore fp behaviour
     48        ignore = np.seterr(**old_settings)
     49
     50        return spectrum
     51
     52
     53class TestLine(object):
     54    """Class to return spectrum with line at wn.
     55    """
     56    def __init__(self, line_wn, frequency_axis):
     57        self.line_wn = line_wn
     58        self.frequency_axis = frequency_axis
     59
     60    def calculate(self):
     61        spectrum = np.zeros(np.shape(self.frequency_axis))
     62        spectrum[self.frequency_axis==self.line_wn] = 1.0e-15
     63        return spectrum
    2064
    2165
     
    3377
    3478        fts = self.previous_results['fts']
    35         frequency_axis = fts['fts_wn']
    36         nspec = len(frequency_axis)
     79        fts_wn_truncated = fts['fts_wn_truncated']
    3780
    3881        beamsgenerator = self.previous_results['beamsgenerator']
     
    4285        # skymodel is complex so that its fft can hold truncated version
    4386        # of infinitesimally sampled map - does that make sense?
    44         skymodel = np.zeros([npix, npix, nspec], np.complex)
     87        skymodel = np.zeros([npix, npix, len(fts_wn_truncated)], np.complex)
    4588
    4689        sky = self.parameters['substages']['Sky']
     
    79122              emissivity)
    80123
     124            spectrum_func = BB_spectrum(temperature, fts_wn_truncated,
     125              cutoffmin, cutoffmax, emissivity)
     126#            spectrum_func = TestLine(50.0, fts_wn_truncated)
     127
    81128            if type.upper().strip() == 'POINT':
    82129                source_spectrum = self._create_point_source(xpos, ypos,
    83                   temperature,
    84                   cutoffmin, cutoffmax, emissivity, skymodel,
    85                   spatial_axis, frequency_axis)
     130                  skymodel, spatial_axis, fts_wn_truncated, spectrum_func)
    86131            else:
    87132                source_spectrum = None
     
    92137        self.result['sky model'] = skymodel
    93138        self.result['spatial axis'] = spatial_axis
    94         self.result['frequency axis'] = frequency_axis
     139        self.result['frequency axis'] = fts_wn_truncated
    95140
    96141        return self.result
    97142
    98     def _create_point_source(self, xpos, ypos, temperature, cutoffmin,
    99       cutoffmax, emissivity, skymodel, spatial_axis, frequency_axis):
     143    def _create_point_source(self, xpos, ypos, skymodel, spatial_axis,
     144      frequency_axis, spectrum_function):
    100145        """Create a point source.
    101146        """
     
    128173            shift[:,i] *= shifty
    129174
    130         # calculate black-body spectrum, modified for cutoffs in
    131         # sensitivity and source emissivity
    132         bbmod = np.zeros(np.shape(frequency_axis))
    133         # ignore floating point errors
    134         old_settings = np.seterr(all='ignore')
    135         for iwn,wn in enumerate(frequency_axis):
    136             # simulate real-life 'rounded' cutoffs numerically 
    137             f1 = 1.0 / (1.0 + pow(cutoffmin/wn, 18) + pow(wn/cutoffmax, 24))
    138             bbmod[iwn] = black_body(temperature, wn) * f1 * emissivity
    139         # restore fp behaviour
    140         ignore = np.seterr(**old_settings)
     175        # calculate spectrum
     176        spectrum = spectrum_function.calculate()
    141177
    142178        # go through freq planes and add point source to each
     
    144180            # create point in frequency space
    145181            temp = np.zeros([nx,nx])
    146             temp[0,0] = bbmod[iwn]
     182            temp[0,0] = spectrum[iwn]
    147183            # 2d fft
    148184            temp = np.fft.fft2(temp)
     
    151187            # transform back
    152188            temp = np.fft.ifft2(temp)
    153             temp = np.real(temp)
    154189
    155190            # add to sky model
     
    158193        # return spectrum
    159194        axis = co.Axis(data=frequency_axis, title='wavenumber', units='cm-1')
    160         spectrum = co.Spectrum(data=bbmod, axis=axis, title='Source spectrum',
    161           units='W sr-1 m-2 Hz-1')
     195        spectrum = co.Spectrum(data=spectrum, axis=axis,
     196          title='Source spectrum', units='W sr-1 m-2 Hz-1')
    162197
    163198        return spectrum
Note: See TracChangeset for help on using the changeset viewer.