In [1]:
    
import numpy as np
import os
%matplotlib inline
import matplotlib.pyplot as plt
    
In [2]:
    
import sncosmo
    
    
In [3]:
    
from scipy.interpolate import (InterpolatedUnivariateSpline as Spline1d,
                               RectBivariateSpline as Spline2d,
                               splmake, spleval, interp1d)
    
In [4]:
    
class SUGARSource(sncosmo.Source):
    """
    SUGAR source model
    """
    def __init__(self, sugar_modelDir='model_data'):
        self._modelDir  = sugar_modelDir
        self.name = 'SUGAR'
        self._parameters = np.array([1., 1., 1., 1., 1., 1.])
        self.param_names_latex = ['q1', 'q2', 'q3', 'Av', 'deltaM']
        self.version = 'v0.1'
        self.name = None
        self.model_data_filename = os.path.join(sugar_modelDir, 'SUGAR_model.asci')
        self.model_data = np.loadtxt(self.model_data_filename)
        self._numPhase = len(self._phase)
        self._numWave = len(self._wave)
        # M0, alpha1, alpha2, alpha3 = interpModel('model_data/', 'SUGAR_model.asci')
        self._M0Interp = self._splineIt(2)
        self._alpha1Interp = self._splineIt(3)
        self._alpha2Interp = self._splineIt(4)
        self._alpha3Interp = self._splineIt(5)
        cl = np.reshape(self.model_data[:, 6], (self._numWave, self._numPhase))
        self._colorlawInterp = interp1d(self._wave, cl[:, 0])
        
    def M0(self, phase, wave):
        return self._M0Interp(wave, phase)
    def alpha1(self, phase, wave):
        return self._alpha1Interp(wave, phase)
    def alpha2(self, phase, wave):
        return self._alpha2Interp(wave, phase)
    def alpha3 (self, phase, wave):
        return self._alpha3Interp(wave, phase)
    def color_law(self, wave):
        return self._colorlawInterp(wave)
    def _splineIt(self, columnNum):
        mval = self.model_data[:, columnNum]
        modelVal = np.reshape(mval, (self._numWave, self._numPhase))
        return Spline2d(self._wave, self._phase, modelVal, kx=1, ky=1)
    
    @property    
    def _wave(self):
        return np.unique(self.model_data[:, 1])
    
    @property    
    def _phase(self):
        return np.unique(self.model_data[:, 0])
    
    @property
    def _param_names(self):
        return ['q1', 'q2', 'q3', 'Av', 'DeltaM']
    
    # Required Functions
    def minwave(self):
        return self._wave[0]
    def maxwave(self):
        return self._wave[-1]
    def minphase(self):
        return self._phase[0]
    def maxphase (self):
        return self._phase[-1]
    
    def _flux(self, phase, wave):
        #print ('===========================================')
        phase = np.ravel(phase)
        wave = np.ravel(wave)
        #print "HOIOOISD OODSDOU ", len(phase) , len(wave)
        M0 = self.M0(phase, wave)
        common_shape = np.shape(M0)
        lenphase = common_shape[1]
        alpha1 = self.alpha1(phase, wave) 
        alpha2 = self.alpha2(phase, wave) 
        alpha3 = self.alpha3(phase, wave) 
        cl = np.reshape(np.repeat(self.color_law(wave), lenphase),common_shape)
        mags =  M0 + \
                self._parameters[0] * alpha1 +\
                self._parameters[1] * alpha2 +\
                self._parameters[2] * alpha3 +\
                self._parameters[3] * cl +\
                self._parameters[4] * np.ones(shape=(len(wave), len(phase)))
        
        
        #print(np.shape(mags))
        #print 'func', (np.shape(cl))
        return 10.0 ** ( -0.4 * mags.T)
    
In [5]:
    
#np.ravel([2, 3])
    
In [6]:
    
#len(np.ravel(2))
    
In [7]:
    
s = SUGARSource()
    
In [8]:
    
s.parameters
    
    Out[8]:
In [9]:
    
s.set(q1=3.)
    
In [10]:
    
s.parameters
    
    Out[10]:
In [11]:
    
testphase = [-4., 0., 4.]
testwave = np.arange(4000., 8000.)
    
In [12]:
    
np.shape(s.M0(phase=testphase, wave=testwave))
    
    Out[12]:
In [13]:
    
phaseflux = s.flux(phase=testphase, wave=testwave)
    
In [14]:
    
np.shape(phaseflux)
    
    Out[14]:
In [16]:
    
plt.plot(testwave, phaseflux[0])
    
    Out[16]:
    
How do single phases behave ?
In [17]:
    
phaseflux = s._flux(0., testwave)
print np.shape(phaseflux)
    
    
In [19]:
    
plt.plot(testwave, phaseflux[0])
    
    Out[19]:
    
In [20]:
    
s.flux([0.], testwave)
    
    Out[20]:
In [21]:
    
model = sncosmo.Model(source=s)
    
In [22]:
    
model.flux(time=[0.], wave=np.arange(5500., 8000., 100))
    
    Out[22]:
In [25]:
    
model = sncosmo.Model(source=s)
model.set(z=0.5)
print (model.minwave(), model.maxwave())
fig, ax = plt.subplots()
ax.plot ( np.arange(5500., 12500, 100), model.flux(time=[0.], wave=np.arange(5500., 12500., 100))[0])
    
    
    Out[25]:
    
In [27]:
    
fig, ax = plt.subplots()
ax.plot(np.arange(-5., 10., 1.), 
        model.bandflux(time=np.arange(10.), band=['desr', 'desr','desr'], zp=25., zpsys='ab'),'ko')
#ax.plot(np.arange(-5., 10., 1.), 
#        model.bandflux(time=np.arange(-5., 10., 1.), band=['desi'], zp=25., zpsys='ab'),'rs')
    
    
    
In [50]:
    
waves = np.arange(4000., 8000.)
plt.plot(waves, s.color_law(waves))
    
    Out[50]:
    
In [51]:
    
import pandas as pd
    
In [131]:
    
df = pd.DataFrame(d, columns=['phase', 'wave', 'M0', 'alpha1', 'alpha2', 'alpha3', 'colorlaw','deltaM'])
    
In [132]:
    
df.head()
    
    Out[132]:
In [133]:
    
x = df.query('phase > -0.5 and phase < 0.5')
    
In [134]:
    
fig = x.plot(x='wave', y=['M0', 'alpha1', 'alpha2', 'alpha3', 'colorlaw'], 
       layout=(3,3), 
       subplots=True)
    
    
In [136]:
    
plt.plot(s._wave, s.alpha1(0., s._wave)[:, 0])
    
    Out[136]:
    
In [116]:
    
plt.plot(s._wave, s.color_law(s._wave))
    
    Out[116]:
    
In [115]:
    
np.shape(s.alpha1(0., s._wave))
    
    Out[115]:
In [161]:
    
np.shape(np.ones(shape=(len(s._wave), len(s._phase))))
    
    Out[161]:
In [ ]:
    
np.broadcast_arrays(np.ones)
    
In [179]:
    
np.asarray(3)
    
    Out[179]:
In [180]:
    
np.ravel(3)
    
    Out[180]:
In [181]:
    
help(np.ravel)
    
    
In [ ]: