def flux_to_curves(self, bands, z=0., d=phys.pc2cm(10.), magnification=1.): """ :param bands: :param z: :param d: :param magnification: :return: """ from pystella.rf.lc import SetLightCurve from pystella.rf import band curves = SetLightCurve(self.Name) for bname in bands: if isinstance(bname, str): b = band.band_by_name(bname) else: b = bname try: lc = self.flux_to_curve(b, z=z, d=d, magnification=magnification) curves.add(lc) except ValueError as ex: logger.error("Could not compute light curve {} for band {} due to {}.". format(self.Name, bname, ex)) return curves
def read_curves_kurtenkov(path=sn_path): jd = 2457000 lc_data = np.loadtxt(os.path.join(path, 'lrn_aa26564-15_p5.csv'), skiprows=2, usecols=(0, 1, 2, 3), dtype=[('JD', '<f4'), ('b', 'S1'), ('mag', '<f4'), ('err', '<f4')]) # mshift = 24.43 # Distance Module to M31 curves = SetLightCurve('Red Nova') bnames = np.unique(lc_data['b']) for i, n in enumerate(bnames): b = band.band_by_name(n) d = lc_data[lc_data['b'] == n, ] time = d['JD'] + jd mags = d['mag'] errs = d['err'] # filter bad values # is_good = mags < 30. # t = time[is_good] # mags = mags[is_good] # errs = errs[is_good] # add lc = LightCurve(b, time, mags, errs=errs) # lc.tshift = -tshift # lc.mshift = -mshift curves.add(lc) return curves
def read_curves_kurtenkov(path=sn_path): jd = 2457000 lc_data = np.loadtxt(os.path.join(path, 'lrn_aa26564-15_p5.csv'), skiprows=2, usecols=(0, 1, 2, 3), dtype=[('JD', '<f4'), ('b', '|S1'), ('mag', '<f4'), ('err', '<f4')]) # mshift = 24.43 # Distance Module to M31 curves = SetLightCurve('Red Nova') bnames = np.unique(lc_data['b']) for i, n in enumerate(bnames): b = band.band_by_name(n.decode("utf-8")) d = lc_data[lc_data['b'] == n, ] time = d['JD'] + jd mags = d['mag'] errs = d['err'] # filter bad values # is_good = mags < 30. # t = time[is_good] # mags = mags[is_good] # errs = errs[is_good] # add lc = LightCurve(b, time, mags, errs=errs) # lc.tshift = -tshift # lc.mshift = -mshift curves.add(lc) return curves
def load(dic=None): """ Load points from dat-files. :return SetLightCurves: """ from pystella.util.math import is_number fname = None tshift = 0. mshift = 0. mag_lim = 99. arg = [] is_debug = False if dic is not None: is_debug = dic.get('is_debug', False) mag_lim = dic.get('mag_lim', mag_lim) if 'args' in dic: arg = dic['args'] if len(arg) > 0: fname = arg.pop(0) fname = os.path.expanduser(fname) if len(arg) > 0: s = arg.pop(0) if is_number(s): tshift = float(s) elif len(arg) > 0: tshift = float(arg.pop(0)) if len(arg) > 0: mshift = float(arg.pop(0)) print("Load {0} tshift={1} mshift={2}".format(fname, tshift, mshift)) # read data # tbl = read_table_header_float(fname) tbl, cols_data = read_obs_table_header( fname, include_names=band.band_get_names_alias(), is_out=is_debug) curves = table2curves(os.path.basename(fname), tbl) # remove bad data res_curves = SetLightCurve(curves.Name) for lc_orig in curves: is_good = lc_orig.Mag < mag_lim t = lc_orig.Time[is_good] + tshift m = lc_orig.Mag[is_good] + mshift e = None if lc_orig.IsErr: e = lc_orig.Err[is_good] lc = LightCurve(lc_orig.Band, t, m, e) res_curves.add(lc) # res_curves.set_tshift(tshift) # res_curves.set_mshift(mshift) return res_curves
def read_curves_tt(self): block = self.read() header = 'Mbol MU MB MV MI MR'.split() curves = SetLightCurve(self.name) time = block['time'] for col in header: b = band.band_by_name(col.replace('M', '')) mag = block[col] lc = LightCurve(b, time, mag) curves.add(lc) return curves
def read_curves(self): block = self.load() header = 'Mbol MU MB MV MI MR'.split() curves = SetLightCurve(self.name) time = block['time'] for col in header: b = band.band_by_name(col.replace('M', '')) mag = block[col] lc = LightCurve(b, time, mag) curves.add(lc) return curves
def test_SetLightCurve_BandNames(self): bands = ["U", "B", "V"] curves = SetLightCurve() for b in bands: curves.add(lc_create(b)) self.assertItemsEqual( bands, curves.BandNames, msg="Error for band names.\n \ Now band is %s but lc.Band.Name is %s." % (" ".join(bands), " ".join(curves.BandNames)), )
def table2curves(name, tbl, bands=None): time = tbl['time'] curves = SetLightCurve(name) if bands is None: bands = [n for n in tbl.dtype.names if band.band_is_exist(n)] for bname in bands: b = band.band_by_name(bname) mag = tbl[bname] lc = LightCurve(b, time, mag) curves.add(lc) return curves
def read_curves(path=sn_path): fs = {'U': 'pav_U.csv', 'B': 'pav_B.csv', 'V': 'pav_V.csv', 'R': 'pav_R.csv', 'I': 'pav_I.csv'} fs = dict((k, os.path.join(path, v)) for k, v in fs.items()) curves = SetLightCurve('Sn87A') for n, fname in fs.items(): data = np.loadtxt(fname, comments='#', skiprows=1, delimiter="\t", usecols=(0, 1)) b = band.band_by_name(n) t = data[:, 0] mags = data[:, 1] lc = LightCurve(b, t, mags) curves.add(lc) return curves
def read_curves_gri(self): block = self.load(ext='gri', line_header=1) # header = 'L_bol Mu MB Mg Mr Mi'.split() # header = 'MB MV'.split() header = 'L_bol Mu MB MV Mg Mr Mi J H K '.split() # header = 'MB MV '.split() # header = 'L_bol L_ubvgri Mu MB MV Mg Mr Mi'.split() curves = SetLightCurve(self.name) time = block['time'] for col in header: b = band.band_by_name(col.replace('M', '').replace('L_', '')) mag = block[col] lc = LightCurve(b, time, mag) curves.add(lc) return curves
def read_curves_master(path=sn_path): header = 'V I R' bnames = map(str.strip, header.split()) curves = SetLightCurve('Red Nova') for i, n in enumerate(bnames): b = band.band_by_name(n) d = np.loadtxt(os.path.join(path, n + '.txt'), dtype=[('JD', '<f4'), ('mag', '<f4'), ('err', '<f4')]) time = d['JD'] mags = d['mag'] errs = d['err'] lc = LightCurve(b, time, mags, errs=errs) curves.add(lc) return curves
def read_curves_master(path=sn_path): header = 'V I R' bnames = map(str.strip, header.split()) curves = SetLightCurve('Red Nova') for i, n in enumerate(bnames): b = band.band_by_name(n) d = np.loadtxt(os.path.join(path, n + '.txt'), dtype=[('JD', '<f4'), ('mag', '<f4'), ('err', '<f4')]) time = d['JD'] mags = d['mag'] errs = d['err'] lc = LightCurve(b, time, mags, errs=errs) curves.add(lc) return curves
def read_curves_gri(self): block = self.read(ext='gri', line_header=1) # header = 'L_bol Mu MB Mg Mr Mi'.split() # header = 'MB MV'.split() header = 'L_bol Mu MB MV Mg Mr Mi J H K '.split() # header = 'MB MV '.split() # header = 'L_bol L_ubvgri Mu MB MV Mg Mr Mi'.split() curves = SetLightCurve(self.name) time = block['time'] for col in header: b = band.band_by_name(col.replace('M', '').replace('L_', '')) mag = block[col] lc = LightCurve(b, time, mag) curves.add(lc) return curves
def flux_to_curves(self, bands, z=0., d=rf.pc_to_cm(10.), magnification=1.): """ :param bands: :param z: :param d: :param magnification: :return: """ curves = SetLightCurve(self.name) for n in bands: b = band.band_by_name(n) lc = self.flux_to_curve(b, z=z, d=d, magnification=magnification) curves.add(lc) return curves
def fit_curves(curves, times=None, Ntime=None, is_RBF=False): """ Compute gaussian process for the Set of Light Curves and return new Set of LCs with approximated magnitudes :param curves: Initial light curves :param times: new time points [optional, None] :param Ntime: number point for linspace(min(t), max(t), Ntime) [optional, None] :param is_RBF: use RationalQuadratic in the kernel :return: """ res = SetLightCurve(curves.Name + '_gp') for lc in curves: lc_new, gp = FitGP.fit_lc(lc, times=times, Ntime=Ntime, is_RBF=is_RBF) res.add(lc_new) return res
def table2curves(name, tbl, bands=None, colt=('time', 'JD', 'MJD'), is_filter_zero=True): # time = None for nm in colt: if nm in tbl.dtype.names: time = tbl[nm] break else: raise ValueError( "THe table should contain a column with name in [{0}]".format( ', '.join(colt))) curves = SetLightCurve(name) if bands is None: bands = [n for n in tbl.dtype.names if band.is_exist(n)] for bname in bands: b = band.band_by_name(bname) mag = tbl[bname] mask = ~np.isnan(mag) # filter if is_filter_zero: mask = np.logical_and(mask, mag != 0) # filter out values not equal 0 mask = np.logical_and(mask, mag < 99) t = time[mask] m = mag[mask] for err_name in (prefix + bname for prefix in err_prefix): if err_name in tbl.dtype.names: err = tbl[err_name] e = err[mask] lc = LightCurve(b, t, m, e) break else: lc = LightCurve(b, t, m) curves.add(lc) return curves
def read_curves(path=sn_path): header = 'U B V I R' cols = map(str.strip, header.split()) lc_data = np.loadtxt(os.path.join(path, '1999emubvir.dat'), skiprows=1, usecols=range(1, 12)) time = lc_data[:, 0] curves = SetLightCurve('Sn99em') for i, n in enumerate(cols): b = band.band_by_name(n) mags = lc_data[:, 2*i+1] errs = lc_data[:, 2*i+2] # filter bad values is_good = mags < 30. t = time[is_good] mags = mags[is_good] errs = errs[is_good] # add lc = LightCurve(b, t, mags, errs=errs) curves.add(lc) return curves
def read_curves(path=sn_path): header = 'U B V R I' cols = map(str.strip, header.split()) lc_data = np.loadtxt(os.path.join(path, '1999emubvir.dat'), skiprows=1, usecols=range(1, 12)) time = lc_data[:, 0] curves = SetLightCurve('Sn99em') for i, n in enumerate(cols): b = band.band_by_name(n) mags = lc_data[:, 2*i+1] errs = lc_data[:, 2*i+2] # filter bad values is_good = mags < 30. t = time[is_good] mags = mags[is_good] errs = errs[is_good] # add lc = LightCurve(b, t, mags, errs=errs) curves.add(lc) return curves
def read_curves_master_abs_mag(path=sn_path): jd = 2457036 header = 'V I R' bnames = map(str.strip, header.split()) curves = SetLightCurve('Red Nova') for i, n in enumerate(bnames): b = band.band_by_name(n) time = np.loadtxt(os.path.join(path, n + '_jd.txt')) + jd mags = np.loadtxt(os.path.join(path, n + '_mag.txt')) errs = np.loadtxt(os.path.join(path, n + '_err.txt')) # filter bad values # is_good = mags < 30. # t = time[is_good] # mags = mags[is_good] # errs = errs[is_good] # add lc = LightCurve(b, time, mags, errs=errs) curves.add(lc) return curves
def read_curves_master_abs_mag(path=sn_path): jd = 2457036 header = 'V I R' bnames = map(str.strip, header.split()) curves = SetLightCurve('Red Nova') for i, n in enumerate(bnames): b = band.band_by_name(n) time = np.loadtxt(os.path.join(path, n + '_jd.txt')) + jd mags = np.loadtxt(os.path.join(path, n + '_mag.txt')) errs = np.loadtxt(os.path.join(path, n + '_err.txt')) # filter bad values # is_good = mags < 30. # t = time[is_good] # mags = mags[is_good] # errs = errs[is_good] # add lc = LightCurve(b, time, mags, errs=errs) curves.add(lc) return curves
def to_curves(self): ph = pandas.DataFrame.from_dict(self.photometry) def add(d, k, v): if k in d: d[k].append(v) else: d[k] = [v] times = {} mags = {} err = {} # {'magnitude': '6.36', 'u_time': 'MJD', 'time': '46849.44', 'band': 'V', 'source': '44,62'} if 'e_magnitude' in ph.keys(): for b, t, m, e in zip(ph.band, ph.time, ph.magnitude, ph.e_magnitude): add(times, b, t) add(mags, b, m) add(err, b, e) else: for b, t, m in zip(ph.band, ph.time, ph.magnitude): add(times, b, t) add(mags, b, m) bands = list(times.keys()) band.Band.load_settings() curves = SetLightCurve(self.Name) for bname in bands: if band.is_exist(bname): tt = list(map(float, times[bname])) mm = list(map(float, mags[bname])) if len(err) > 0: ee = list(map(float, err[bname])) lc = LightCurve(bname, tt, mm, ee) else: lc = LightCurve(bname, tt, mm) curves.add(lc) return curves
def flux_to_curves(self, bands, z: float = 0., d: float = phys.pc2cm(10.), magnification: float = 1.): """ Compute the light curves for bands using the spectral data. Parameters ---------- - bands: broadbands - z: redshift. Default: 0 - d: distance [cm]. Default: 10 pc in cm - magnification: lens magnification. Default: 1 Returns ------- SetLightCurve """ from pystella.rf.lc import SetLightCurve from pystella.rf import band curves = SetLightCurve(self.Name) for bname in bands: if isinstance(bname, str): b = band.band_by_name(bname) else: b = bname try: lc = self.flux_to_curve(b, z=z, d=d, magnification=magnification) curves.add(lc) except ValueError as ex: logger.error( "Could not compute light curve {} for band {} due to {}.". format(self.Name, bname, ex)) return curves
def load(dic=None): """ Load points from dat-files. Reader data-file with mix bname data, like: >>% head photometry.txt jd filter mag mage 2457059.6228778586 V 17.493766309999998 0.0592200135089 2457059.6244578934 V 17.539956019999998 0.0542402986717 2457059.6261980557 g 17.782871193345898 0.0454000142503 2457059.6287036575 g 17.7782469177482 0.0395424488201 :return SetLightCurves: """ fname = None tshift = 0. mshift = 0. mag_lim = 30. skiprows = 1. arg = [] if dic is not None: mag_lim = dic.get('mag_lim', 30.) skiprows = dic.get('skiprows', 1) if 'args' in dic: arg = dic['args'] if len(arg) > 0: fname = arg.pop(0) fname = os.path.expanduser(fname) if len(arg) > 0: s = arg.pop(0) if s.isnumeric(): tshift = float(s) elif len(arg) > 0: tshift = float(arg.pop(0)) if len(arg) > 0: mshift = float(arg.pop(0)) print("Load {0} tshift={1} mshift={2}".format(fname, tshift, mshift)) # read data dtype = [('time', '<f4'), ('b', 'str'), ('mag', '<f4'), ('err', '<f4')] # lc_data = np.loadtxt(fname, skiprows=skiprows, dtype=dtype, comments='#') # jd filter mag mage # lc_data = np.genfromtxt(fname, skip_header=skiprows, dtype=dtype, comments='#') # jd filter mag mage lc_data = np.genfromtxt(fname, skip_header=skiprows, dtype=None, names=[v[0] for v in dtype], comments='#') b_tot = lc_data['b'] bnames = np.unique(b_tot) curves = SetLightCurve() for bname in bnames: if band.is_exist(bname.decode()): # filter of the current band d = lc_data[np.where(lc_data['b'] == bname)] # is_good = list(map(lambda x: x == bname, b_tot)) # t = (lc_data['time'])[is_good] # m = lc_data['mag'][is_good] # e = lc_data['err'][is_good] # add light curve b = band.band_by_name(bname.decode()) lc = LightCurve(b, d['time'], d['mag'], d['err']) # lc = LightCurve(b, t, m, e) curves.add(lc) else: print('Could read the light curve. There is no band: {}. ' 'You may try to add it to dir data/bands'.format(bname)) # remove bad data res_curves = SetLightCurve(curves.Name) for lc_orig in curves: is_good = lc_orig.Mag < mag_lim t = lc_orig.Time[is_good] m = lc_orig.Mag[is_good] e = None if lc_orig.IsErr: e = lc_orig.Err[is_good] lc = LightCurve(lc_orig.Band, t, m, e) res_curves.add(lc) res_curves.set_tshift(tshift) res_curves.set_mshift(mshift) return res_curves