Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

import numpy as np 

import matplotlib.pyplot as plt 

from scipy import interpolate 

import numpy.lib.recfunctions as rf 

 

 

class Lims: 

""" 

class to handle light curve of SN 

 

Parameters 

--------------- 

Li_files : str 

light curve reference file 

mag_to_flux_files : str 

files of magnitude to flux 

band : str 

band considered 

SNR : float 

Signal-To-Noise Ratio cut 

mag_range : pair(float),opt 

mag range considered 

Default : (23., 27.5) 

dt_range : pair(float) 

difference time range considered (cadence) 

Default : (0.5, 25.) 

""" 

 

def __init__(self, Li_files, mag_to_flux_files, band, SNR, 

mag_range=(23., 27.5), dt_range=(0.5, 25.)): 

 

self.band = band 

self.SNR = SNR 

self.lims = [] 

self.mag_to_flux = [] 

self.mag_range = mag_range 

self.dt_range = dt_range 

 

for val in Li_files: 

self.lims.append(self.get_lims(self.band, np.load(val), SNR)) 

for val in mag_to_flux_files: 

self.mag_to_flux.append(np.load(val)) 

self.interp() 

 

def get_lims(self, band, tab, SNR): 

""" 

Estimations of the limits 

 

Parameters 

--------------- 

band : str 

band to consider 

tab : numpy array 

table of data 

SNR : float 

Signal-to-Noise Ratio cut 

 

Returns: 

----------- 

dict of limits with redshift and band as keys. 

 

""" 

 

lims = {} 

 

for z in np.unique(tab['z']): 

 

idx = (tab['z'] == z) & (tab['band'] == 'LSST::'+band) 

idx &= (tab['flux_e'] > 0.) 

sel = tab[idx] 

 

if len(sel) > 0: 

li2 = np.sqrt(np.sum(sel['flux_e']**2)) 

lim = 5. * li2 / SNR 

if z not in lims.keys(): 

lims[z] = {} 

lims[z][band] = lim 

 

return lims 

 

def mesh(self, mag_to_flux): 

""" 

Mesh grid to estimate five-sigma depth values (m5) from mags input. 

 

Parameters 

--------------- 

mag_to_flux : magnitude to flux values 

 

Returns 

----------- 

m5 values 

time difference dt (cadence) 

metric=sqrt(dt)*F5 where F5 is the 5-sigma flux 

 

""" 

dt = np.linspace(self.dt_range[0], self.dt_range[1], 100) 

m5 = np.linspace(self.mag_range[0], self.mag_range[1], 50) 

ida = mag_to_flux['band'] == self.band 

fa = interpolate.interp1d( 

mag_to_flux[ida]['m5'], mag_to_flux[ida]['flux_e']) 

f5 = fa(m5) 

F5, DT = np.meshgrid(f5, dt) 

M5, DT = np.meshgrid(m5, dt) 

metric = np.sqrt(DT) * F5 

 

return M5, DT, metric 

 

def interp(self): 

""" 

Estimate a grid of interpolated values 

in the plane (m5, cadence, metric) 

 

Parameters 

--------------- 

None 

 

""" 

 

M5_all = [] 

DT_all = [] 

metric_all = [] 

 

for val in self.mag_to_flux: 

M5, DT, metric = self.mesh(val) 

M5_all.append(M5) 

DT_all.append(DT) 

metric_all.append(metric) 

 

sorted_keys = [] 

for i in range(len(self.lims)): 

sorted_keys.append(np.sort([k for k in self.lims[i].keys()])[::-1]) 

figa, axa = plt.subplots() 

 

for kk, lim in enumerate(self.lims): 

fmt = {} 

ll = [lim[zz][self.band] for zz in sorted_keys[kk]] 

cs = axa.contour(M5_all[kk], DT_all[kk], metric_all[kk], ll) 

 

points_values = None 

for io, col in enumerate(cs.collections): 

if col.get_segments(): 

 

myarray = col.get_segments()[0] 

res = np.array(myarray[:, 0], dtype=[('m5', 'f8')]) 

res = rf.append_fields(res, 'cadence', myarray[:, 1]) 

res = rf.append_fields( 

res, 'z', [sorted_keys[kk][io]]*len(res)) 

if points_values is None: 

points_values = res 

else: 

points_values = np.concatenate((points_values, res)) 

self.points_ref = points_values 

 

plt.close(figa) # do not display 

 

def interp_griddata(self, data): 

""" 

Estimate metric interpolation for data (m5,cadence) 

 

Parameters 

--------------- 

data : data where interpolation has to be done (m5,cadence) 

 

Returns 

----------- 

griddata interpolation (m5,cadence,metric) 

 

""" 

 

ref_points = self.points_ref 

res = interpolate.griddata((ref_points['m5'], ref_points['cadence']), ref_points['z'], ( 

data['m5_mean'], data['cadence_mean']), method='cubic') 

return res 

 

 

class GenerateFakeObservations: 

""" Class to generate Fake observations 

 

Parameters 

--------- 

config: yaml-like 

configuration file (parameter choice: filter, cadence, m5,Nseasons, ...) 

list : str,opt 

Name of the columns used. 

Default : 'observationStartMJD', 'fieldRA', 'fieldDec','filter','fiveSigmaDepth','visitExposureTime','numExposures','visitTime','season' 

 

Returns 

--------- 

recordarray of observations with the fields: 

MJD, Ra, Dec, band,m5,Nexp, ExpTime, Season 

""" 

 

def __init__(self, config, 

mjdCol='observationStartMJD', RaCol='fieldRA', 

DecCol='fieldDec', filterCol='filter', m5Col='fiveSigmaDepth', 

exptimeCol='visitExposureTime', nexpCol='numExposures', seasonCol='season'): 

 

self.mjdCol = mjdCol 

self.m5Col = m5Col 

self.filterCol = filterCol 

self.RaCol = RaCol 

self.DecCol = DecCol 

self.exptimeCol = exptimeCol 

self.seasonCol = seasonCol 

self.nexpCol = nexpCol 

 

# now make fake obs 

self.make_fake(config) 

 

def make_fake(self, config): 

""" Generate Fake observations 

 

Parameters 

--------- 

config: yaml-like 

configuration file (parameter choice: filter, cadence, m5,Nseasons, ...) 

 

 

""" 

bands = config['bands'] 

cadence = dict(zip(bands, config['Cadence'])) 

shift_days = dict( 

zip(bands, [config['shift_days']*io for io in range(len(bands))])) 

m5 = dict(zip(bands, config['m5'])) 

Nvisits = dict(zip(bands, config['Nvisits'])) 

Exposure_Time = dict(zip(bands, config['Exposure_Time'])) 

inter_season_gap = 300. 

 

Ra = config['Ra'] 

Dec = config['Dec'] 

rtot = [] 

# for season in range(1, config['nseasons']+1): 

for il, season in enumerate(config['seasons']): 

# mjd_min = config['MJD_min'] + float(season-1)*inter_season_gap 

mjd_min = config['MJD_min'][il] 

mjd_max = mjd_min+config['season_length'] 

 

for i, band in enumerate(bands): 

mjd = np.arange(mjd_min, mjd_max+cadence[band], cadence[band]) 

mjd += shift_days[band] 

m5_coadded = self.m5_coadd(m5[band], 

Nvisits[band], 

Exposure_Time[band]) 

myarr = np.array(mjd, dtype=[(self.mjdCol, 'f8')]) 

myarr = rf.append_fields(myarr, [self.RaCol, self.DecCol, self.filterCol], [ 

[Ra]*len(myarr), [Dec]*len(myarr), [band]*len(myarr)]) 

myarr = rf.append_fields(myarr, [self.m5Col, self.nexpCol, self.exptimeCol, self.seasonCol], [ 

[m5_coadded]*len(myarr), [Nvisits[band]]*len(myarr), [Nvisits[band]*Exposure_Time[band]]*len(myarr), [season]*len(myarr)]) 

rtot.append(myarr) 

 

res = np.copy(np.concatenate(rtot)) 

res.sort(order=self.mjdCol) 

 

self.Observations = res 

 

def m5_coadd(self, m5, Nvisits, Tvisit): 

""" Coadded m5 estimation 

 

Parameters 

--------- 

m5 : list(float) 

list of five-sigma depth values 

Nvisits : list(float) 

list of the number of visits 

Tvisit : list(float) 

list of the visit times 

 

Returns 

--------- 

m5_coadd : list(float) 

list of m5 coadded values 

 

""" 

m5_coadd = m5+1.25*np.log10(float(Nvisits)*Tvisit/30.) 

return m5_coadd 

 

 

class ReferenceData: 

""" 

class to handle light curve of SN 

 

Parameters 

--------------- 

Li_files : str 

light curve reference file 

mag_to_flux_files : str 

files of magnitude to flux 

band : str 

band considered 

z : float 

redshift considered 

""" 

 

def __init__(self, Li_files, mag_to_flux_files, band, z): 

 

self.band = band 

self.z = z 

self.fluxes = [] 

self.mag_to_flux = [] 

 

for val in Li_files: 

self.fluxes.append(self.interp_fluxes( 

self.band, np.load(val), self.z)) 

for val in mag_to_flux_files: 

self.mag_to_flux.append( 

self.interp_mag(self.band, np.load(val))) 

 

def interp_fluxes(self, band, tab, z): 

""" 

Flux interpolator 

 

Parameters 

--------------- 

band : str 

band considered 

tab : array 

reference data with (at least) fields z,band,time,DayMax 

z : float 

redshift considered 

 

Returns 

----- 

list (float) of interpolated fluxes (in e/sec) 

""" 

lims = {} 

idx = (np.abs(tab['z'] - z) < 1.e-5) & (tab['band'] == 'LSST::'+band) 

sel = tab[idx] 

selc = np.copy(sel) 

difftime = (sel['time']-sel['DayMax']) 

selc = rf.append_fields(selc, 'deltaT', difftime) 

return interpolate.interp1d(selc['deltaT'], selc['flux_e'], bounds_error=False, fill_value=0.) 

 

def interp_mag(self, band, tab): 

""" 

magnitude (m5) to flux (e/sec) interpolator 

 

Parameters 

--------------- 

band : str 

band considered 

tab : array 

reference data with (at least) fields band,m5,flux_e, 

z : float 

redshift considered 

 

Returns 

----- 

list (float) of interpolated fluxes (in e/sec) 

""" 

idx = tab['band'] == band 

sel = tab[idx] 

return interpolate.interp1d(sel['m5'], sel['flux_e'], bounds_error=False, fill_value=0.)