-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflows_photometry.py
executable file
·191 lines (157 loc) · 5.87 KB
/
flows_photometry.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
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
#!/usr/bin/env python
import os
import sys
import glob
import argparse
import numpy as np
import pandas as pd
def get_metadata(file):
"""Obtains info from an image reduced with the FLOWS pipeline.
Parameters
----------
file: str
'photometry.ecsv' file in an image directory.
Returns
-------
filt: str
Filter used.
mjd: float
Time of observation.
"""
filt = mjd = None
with open(file, 'r') as info_file:
for line in info_file.readlines():
if 'photfilter' in line:
filt = line.split()[-1].split('}')[0]
if 'obstime-bmjd' in line:
mjd = line.split()[-1].split('}')[0]
return filt, mjd
def get_magnitude(file):
"""Extracts photometry from an image reduced with the FLOWS pipeline.
Parameters
----------
file: str
'photometry.ecsv' file in an image directory.
Returns
-------
mag: float
SN magnitude.
mag_err: float
SN magnitude error.
sub: int
Whether the image was template subtracted:
'-1' is template subtracted and '0' is no subtraction.
"""
init_df = pd.read_csv(file, comment='#')
df = init_df[init_df.starid==-1]
sub = -1
if len(df)==0:
df = init_df[init_df.starid==0]
sub = 0
mag, mag_err = df.mag.values[0], df.mag_error.values[0]
return mag, mag_err, sub
def flows_photometry(target, subtraction=1):
"""Extracts FLOWS photometry.
Parameters
----------
target: str
Name of the target. Used for the output photometry file.
subtraction: int, default '1'
Whether to extract the photometry from template-subtracted
images (-1), unsubtracted images (0) or both (1).
"""
img_directories = glob.glob(f'{target}/*')
phot_dir = {'mjd':[], 'mag':[], 'mag_err':[], 'filt':[], 'subtraction':[]}
for directory in img_directories:
if os.path.isdir(directory) is False:
continue # not a directory, so skip
# get all the files in an image directory and get the photometry file
dir_files = glob.glob(f'{directory}/*')
try:
phot_file = [file for file in dir_files if file.endswith('photometry.ecsv')][0]
except:
print(f'Skipping {directory} - no "photometry.ecsv" file')
continue # skip this image
filt, mjd = get_metadata(phot_file)
mag, mag_err, sub = get_magnitude(phot_file)
mag = np.round(mag, 2)
mag_err = np.round(mag_err, 2)
mjd = np.round(float(mjd), 2)
phot_dir['mjd'].append(mjd)
phot_dir['mag'].append(mag)
phot_dir['mag_err'].append(mag_err)
phot_dir['filt'].append(filt)
phot_dir['subtraction'].append(sub)
phot_df = pd.DataFrame(phot_dir)
# sort by filter, then by mjd
sorter_dict = {'B':0, 'V':1, 'R':2, 'I':3,
'gp':4, 'rp':5, 'ip':6,
'Y':7, 'J':8, 'H':9, 'K':10}
phot_df['filt_num'] = [sorter_dict[filt] for filt in phot_df.filt.values]
phot_df.sort_values(['filt_num', 'mjd'], inplace=True, ignore_index=True)
# mask by subtraction
if subtraction in [-1, 0]:
phot_df = phot_df[phot_df.subtraction==subtraction]
phot_df.to_csv(f'{target}_phot.csv', index=False)
def create_snoopy_file(target, z, ra, dec):
"""Creates a snpy file from FLOWS photometry
Parameters
----------
target : str
Name of the target. Used for the output photometry file.
z : float
Target's redshift.
ra : float
Target's right ascension.
dec : float
Target's declination.
"""
phot_df = pd.read_csv(f'{target}_phot.csv')
with open(f'{target}_snpy.dat', 'w') as outfile:
# metadata
outfile.write(f'{target} {z} {ra} {dec}\n')
for filt in phot_df.filt.unique():
# add each filter
outfile.write(f'filter {filt}\n')
filt_df = phot_df[phot_df.filt==filt]
for t, m, me in zip(filt_df.mjd.values,
filt_df.mag.values,
filt_df.mag_err.values):
outfile.write(f'{t}\t{m}\t{me}\n')
def main(args=None):
description = f"FLOWS photometry"
usage = "flows_photometry <directory> [options]"
if not args:
args = sys.argv[1:] if sys.argv[1:] else ["--help"]
parser = argparse.ArgumentParser(prog='flows_photometry',
usage=usage,
description=description
)
parser.add_argument("directory",
help="name of the directory"
)
parser.add_argument("-s",
"--subtraction",
dest="subtraction",
action="store",
default=1,
choices=[-1, 0, 1],
type=int,
help=("Whether to extract the photometry from template-subtracted "
"images (-1), unsubtracted images (0) or both (1; deafault).")
)
parser.add_argument("--snpy",
dest="snpy",
nargs='+',
type=str,
required=False,
help=("Creates a snpy file. Redshift, right ascension and declination "
"must be given (in that order), e.g. '--snpy 0.0532 12.340 0.344'.")
)
args = parser.parse_args(args)
flows_photometry(args.directory, args.subtraction)
if args.snpy is not None:
z, ra, dec = args.snpy
create_snoopy_file(args.directory, z, ra, dec)
if __name__ == "__main__":
main(sys.argv[1:])