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 fit_lc(lc, times=None, Ntime=None, is_RBF=False): """ Compute gaussian process for the light curve and return new LC with approximated magnitudes :param lc: Initial light curve :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: """ if is_RBF: gp = FitGP.lc2gpRBF(lc) else: gp = FitGP.lc2gp(lc) if times is None: # new time points times = lc.Time if Ntime is not None: times = np.linspace(min(times), max(times), Ntime) if type(times) is not np.ndarray: times = np.array(times) if times.ndim == 2: X_samples = times else: X_samples = times.reshape(-1, 1) # 2D array # Make the prediction on the meshed x-axis (ask for MSE as well) y_pred, sigma = gp.predict(X_samples, return_std=True) return LightCurve(lc.Band, times, y_pred, sigma), gp
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 comments = '#' 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, comments=comments) 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] 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
def LCBol(self, time): # todo check this code from pystella.rf import band from pystella.rf.lc import LightCurve b = band.BandUni() mags = self.MagBol(time) lc = LightCurve(b, time, mags) return lc
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(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 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 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.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(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 flux_to_curve(self, b, z=0., d=0., magnification=1.): from pystella.rf.lc import LightCurve if b is None: raise ValueError("Band must be defined.") if not self.is_time: return ValueError("No spectral time points.") times, mags = self.to_mags(b, z, d, magnification) times = (1. + z) * np.array(times) lc = LightCurve(b, times, mags) lc.attrs('d', d) lc.attrs('z', z) lc.attrs('magnification', magnification) return lc
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 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
def lc_create(bname, m=-19, dt=0.): n = 10 time = np.linspace(0.+dt, 200.+dt, n) mags = m * np.ones(n) return LightCurve(bname, time, mags)