-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_wiyn_dr1.py
126 lines (103 loc) · 5.28 KB
/
sub_wiyn_dr1.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
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
from __future__ import print_function
import glob
import os
import sys
from wiynSubtractExp import convname_from_diffname, diffname_from_inputs, subtractFiles
from wiynAssembleExp import makeLsstNamesAndFile
# The following supernovae need host-galaxy subtractions
# and the templates are available as of DR1.
sn_with_dr1_templates = {
'SN2012fm': {'J': 'SN2012fm_A_J_20131018.fits', 'H': 'SN2012fm_A_H_20131018.fits'},
'PSNJ07250042+2347030': {'J': 'PSNJ07250042+2347030_A_J_20131213.fits', 'H': 'PSNJ07250042+2347030_A_H_20131213.fits'},
'LSQ12fuk': {'J': 'LSQ12fuk_A_J_20131019.fits', 'H': 'LSQ12fuk_A_H_20131019.fits'},
'SN2011hb': {'J': 'SN2011hb_A_J_20131020.fits', 'H': 'SN2011hb_A_H_20131020.fits'},
# 'PTF11qzq': {'J': 'PTF11qzq_A_J_20130925.fits', 'H': 'PTF11qzq_A_H_20121007.fits'},
'PTF11mty': {'J': 'PTF11mty_A_J_20121007.fits', 'H': 'PTF11mty_A_H_20120925.fits'},
'SN2011iu': {'J': 'SN2011iu_A_J_20121001.fits', 'H': 'SN2011iu_A_H_20120925.fits'},
'SN2011gy': {'J': 'SN2011gy_A_J_20121028.fits', 'H': 'SN2011gy_A_H_20121028.fits'},
'PTF11owc': {'J': 'PTF11owc_A_J_20120402.fits', 'H': 'PTF11owc_A_H_20121125.fits'},
'SN2011ho': {'H': 'SN2011ho_A_H_20120402.fits'},
'PTF11qpc': {'H': 'PTF11qpc_A_H_20120402.fits'},
}
# repo_dir = os.path.join(os.getenv('SCRATCH'), 'tmp', 'test_dr1')
repo_dir = os.path.join(os.getenv('DR1BASE'), 'repo', 'test_dr1')
def find_and_generate_lsst_files(sn):
"""Find the files for a given SN, generate LSST-style fits versions."""
dr1_dir = os.path.join(os.getenv('DR1BASE'), 'stacks')
sn_search_regex = os.path.join(dr1_dir, "{}_*[0-9].fits".format(sn))
files = glob.glob(sn_search_regex)
for f in files:
makeLsstNamesAndFile(f)
def find_science_images(sn, f, repo_dir, dataset='calexp', verbose=False):
if dataset == 'deepDiff_differenceExp':
search_dataset = 'diff'
suffix = ''
elif dataset == 'calexp':
search_dataset = dataset
suffix = '.fits'
else:
search_dataset = dataset
suffix = ''
sn_search_regex = os.path.join(repo_dir, search_dataset, "{}_[ABC]_{}_*[0-9]{}".format(sn, f, suffix))
if verbose:
print("Searching for: ", sn_search_regex)
sn_files = glob.glob(sn_search_regex)
return sn_files
def test_subtractFiles():
science_image = 'PS1-12bwh_A_J_20121028.fits'
template_image = 'PS1-12bwh_A_J_20121130.fits'
out_image = 'PS1-12bwh_A_J_20121028_20121130.diff.fits'
scienceImage = os.path.join(repo_dir, 'calexp', science_image)
templateImage = os.path.join(repo_dir, 'calexp', template_image)
diff_file = diffname_from_inputs(os.path.basename(science_file),
os.path.basename(template_file))
conv_file = convname_from_diffname(os.path.basename(diff_file))
subtractFiles(science_file, template_file, diff_file, conv_file)
def filename_to_fileroot(filename):
fileroot = os.path.basename(filename)
fileroot = fileroot.replace('.lsst.fits', '')
fileroot = fileroot.replace('.fits', '')
return fileroot
def run_repo_based_subtraction(science_file, template_file, repo_dir, verbose=True):
"""Run a subtraction using the Butler"""
science_fileroot = filename_to_fileroot(science_file)
template_fileroot = filename_to_fileroot(template_file)
args = [repo_dir,
'--id', 'fileroot={}'.format(science_fileroot),
'--templateId', 'fileroot={}'.format(template_fileroot),
'--output', repo_dir,
'--configfile', 'diffimconfig.py',
'--clobber-config', '--clobber-versions',
]
from lsst.pipe.tasks.imageDifference import ImageDifferenceTask
if verbose:
print("Running ImageDifferenceTask.parseAndrun with args:")
print(args)
ImageDifferenceTask.parseAndRun(args=args)
def run_file_based_subtraction(science_file, template_file):
"""Run a subtraction just based on file names, write output to POSIX file system."""
diff_file = diffname_from_inputs(science_file, template_file)
conv_file = convname_from_diffname(diff_file)
print("subtractFiles({}, {}, {}, {})".format(science_file, template_file, diff_file, conv_file))
try:
subtractFiles(science_file, template_file, diff_file, conv_file)
except Exception as e:
print(e)
if __name__ == "__main__":
# for sn in sn_with_dr1_templates:
# find_and_generate_lsst_files(sn)
# Do we run subtraction directly from files
# or use the repo-based Butler interface
repo_based = True
#repo_dir = sys.argv[1]
for name, templates in sn_with_dr1_templates.items():
print("Processing {}".format(name))
for f in templates.keys():
template_file = os.path.join(repo_dir, 'calexp', templates[f])
for science_file in find_science_images(name, f, repo_dir):
if science_file == template_file:
continue
if repo_based:
run_repo_based_subtraction(science_file, template_file, repo_dir)
else:
run_file_based_subtraction(science_file, template_file)