forked from yaoyuhan/ForcePhotZTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalt2_fit.py
84 lines (64 loc) · 2.4 KB
/
salt2_fit.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 4 16:22:05 2019
@author: yuhanyao
"""
import os
import sncosmo
import numpy as np
def add_ZTFfilters():
directory='/Users/yuhanyao/Documents/ForcePhotZTF/data/'
bandsP48 = {'p48i': 'P48_I.dat',
'p48r': 'P48_R.dat',
'p48g': 'P48_g.dat'}
fileDirectory = directory+'filters/P48/'
for bandName, fileName in bandsP48.items():
filePath = os.path.join(fileDirectory, fileName)
if not os.path.exists(filePath):
raise IOError("No such file: %s" % filePath)
b = np.loadtxt(filePath)
band = sncosmo.Bandpass(b[:, 0], b[:, 1], name=bandName)
sncosmo.registry.register(band, force=True)
# the following functions are partly borrowed from
# https://gist.github.com/ufeindt/cf3a4dd6484f4e96aec1
def _get_bandmag(band, magsys, t=0, rest_frame=True, **kwargs):
"""
Returns mag at max for the model, band and magsys
Arguments:
model -- sncosmo model, e.g. SALT2
band -- sncosmo band object or string, e.g. 'bessellb'
magsys -- magnitude system, e.g. 'ab'
Keyword arguments:
t -- time relative to t0 (observer-frame), at which to evaluate
rest_frame -- default: True, overrides the redshifts
"""
model = sncosmo.Model(source='salt2')
if rest_frame:
kwargs['z'] = 0
model.set(**kwargs)
return model.bandmag(band, magsys, kwargs['t0'] + t)
def _get_bandmag_gradient(band, magsys, param, sig, fixed_param,
t=0, rest_frame=True):
"""
Return gradient of _get_bandmag as function of param
param, sig must be dictionaries of means and uncertainties
Best use odicts to make sure that the order of the components is correct
"""
model = sncosmo.Model(source='salt2')
out = []
if rest_frame:
if 'z' in param.keys():
param['z'] = 0
if 'z' in fixed_param.keys():
fixed_param['z'] = 0
model.set(**fixed_param)
for key,val in param.items():
model.set(**param)
h = sig[key] / 100.
model.set(**{key: val - h})
m0 = model.bandmag(band, magsys, param['t0'] + t)
model.set(**{key: val + h})
m1 = model.bandmag(band, magsys, param['t0'] + t)
out.append((m1 - m0) / (2. * h))
return np.array(out)