-
Notifications
You must be signed in to change notification settings - Fork 7
/
upload_spectra_fritz.py
296 lines (255 loc) · 10.3 KB
/
upload_spectra_fritz.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
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
# Author: Igor Andreoni
# email: [email protected]
import requests
import glob
import re
import numpy as np
from astropy.io import ascii, fits
from astropy.table import Table
from astropy.time import Time
# Copy the API token from your Fritz account
token = 'copied-from-fritz'
"""
Default observers and reducers (these are NOT necessary to be set,
since they will be used only when the -d option is called).
User IDs can be found at: https://fritz.science/api/user
"""
default_observer = [14,32]
default_reducer = [14]
def api(method, endpoint, data=None):
"""API request"""
headers = {'Authorization': f'token {token}'}
response = requests.request(method, endpoint, json=data, headers=headers)
return response
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1', 'Yes', 'True'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0', 'No', 'False'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def check_source_exists(source):
"""check if the source exists"""
response = api('HEAD', f'https://fritz.science/api/sources/{source}')
if response.status_code == 200:
print(f"Source {source} was found on Fritz")
else:
print(f"Source {source} does not exist on Fritz!!")
return response
def get_groups(source):
"""Get the groups a source belongs to"""
response = api('GET',
f'https://fritz.science/api/sources/{source}'
)
if response.status_code == 200:
groups = response.json()['data']['groups']
else:
print(f'HTTP code: {response.status_code}, {response.reason}')
return groups
def upload_spectrum(spec, observers, reducers, group_ids=[], date=None,
inst_id=3, ztfid=None, meta=None):
"""
Upload a spectrum to the Fritz marshal
----
Parameters
spec astropy table object
table with wavelength, flux, fluxerr
observers list of int
list of integers corresponding to observer usernames
reducers list of int
list of integers corresponding to reducer usernames
group_ids list of int
list of IDs of the groups to share the spectum with
date str
date (UT) of the spectrum
inst_id int
ID of the instrument (default DBSP)
ztfid str
ID of the ZTF source, e.g. ZTF20aclnxgz
"""
# Remove meta from spec, they are already stored
spec._meta = None
#Get rid of NaNs in lpipe output since fritz does not like NaNs and inf
good_rows = ~np.isnan(spec['flux'])
usespec = spec[good_rows]
good_rows = ~np.isinf(spec['flux'])
usespec = spec[good_rows]
good_rows = ~np.isnan(spec['fluxerr'])
usespec = spec[good_rows]
good_rows = ~np.isinf(spec['fluxerr'])
usespec = spec[good_rows]
data = {
"observed_by": observers,
"group_ids": group_ids,
# "assignment_id": 0,
"altdata": meta,
"observed_at": str(date),
"fluxes": list(usespec['flux']),
"errors": list(usespec['fluxerr']),
# "followup_request_id": 0,
"wavelengths": list(usespec['wavelength']),
"instrument_id": inst_id,
"reduced_by": reducers,
"origin": "",
"obj_id": ztfid
}
response = api('POST',
'https://fritz.science/api/spectrum',
data=data)
print(f'HTTP code: {response.status_code}, {response.reason}')
if response.status_code == 400:
print(f'JSON response: {response.json()}')
def crop_spectrum(spectrum):
"""
Crop the spectra
----
Parameters
spec astropy.table object
table with the spectrum
----
Returns
cropped spectrum
"""
# Define the ranges to crop the spectra
default_crop = str2bool(input("Are you happy with the default crop \
between 3,200A and 10,000A? \n"))
if default_crop is True:
crop_range = (3200, 10000)
else:
happy = False
while happy is False:
raw_range = input(f"Input the desired range (two numbers, \
comma-separated): \n").split(",")
crop_range = tuple([int(s) for s in raw_range])
print(f"Your range for cropping will be {crop_range}")
happy = str2bool(input(f"Please confirm that this \
is the range you want [y/n]:\n"))
spectrum = spectrum[spectrum['wavelength'] < np.max(crop_range)]
spectrum = spectrum[spectrum['wavelength'] > np.min(crop_range)]
return spectrum
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Upload spectra to Fritz.')
parser.add_argument('ID', nargs='+', help='Spectra filenames')
parser.add_argument('--date', dest='date', type=str,
required=True, help='Date of the observations (UT), \
for example 2020-11-10T00:00:00 or 2021-01-12')
parser.add_argument('--inst', dest='inst_id', type=int,
required=True, help='Instrument ID, \
for example. inst_id = 3 for DBSP, inst_id = 7 for LRIS. \
Instrument IDs can be found at: \
https://fritz.science/api/instrument')
parser.add_argument('-d', action='store_true',
help='Use default reducer ID and observer; \
please customize the code if you want to use this (unrequired) option')
args = parser.parse_args()
# Observers, reducers
if args.d is True:
#default observer
observers = default_observer
#default reducer
reducers = default_reducer
else:
print("users IDs can be found at: https://fritz.science/api/user")
observers = input("Enter comma-separated IDs (int) \
of the observers:\n")
observers = observers.split(",")
reducers = input("Enter comma-separated IDs (int) of the reducers:\n")
reducers = reducers.split(",")
# For each ID, check which files are available
for source_filename in args.ID:
files = glob.glob(f"{source_filename}")
if len(files) == 0:
print(f"No files named {source_filename} found")
filename = input(f"Enter the correct spectrum file name \
or enter 'c' to skip this source and continue: \n")
else:
filename = files[0]
if filename == 'c':
continue
elif filename[-4:] == 'fits':
hdul = fits.open(filename)
spec = hdul[-1].data
# Fix the table format
spec = Table(hdul[-1].data)
spec.rename_column("wave", "wavelength")
spec.rename_column("sigma", "fluxerr")
# build header
for i, hud in enumerate(hdul): # saves info from all headers - might be too much, but better safe than sorry
header = hdul[i].header
if i==0: # contains coadd file paths and software versions (possibly useful for debugging)
fileext = 'VERSION'
spec.meta = {'header': {fileext: dict(header)}}
else: # index headers by file extension name (eg. RED00XX/BLUE00XX = header from raw exposures, RED/BLUE/SPLICED = reduced spectrum metadata)
fileext = header['EXTNAME']
spec.meta['header'][str(fileext)] = dict(header)
try:
del spec.meta['header'][str(fileext)]['COMMENT'] # this is just info about the FITS file format, not needed or relevant
except:
pass
else:
# Read the file as ascii
spec = ascii.read(filename)
header = None
spec.rename_column("col1", "wavelength")
spec.rename_column("col2", "flux")
# Uncertainty for DBSP
if len(spec.colnames) > 2 and args.inst_id == 3:
spec.rename_column("col3", "fluxerr")
elif len(spec.colnames) > 2 and args.inst_id == 7:
spec.rename_column("col4", "fluxerr")
elif len(spec.colnames) == 2:
spec["fluxerr"] = np.zeros(len(spec))
# Crop DBSP spectra
if args.inst_id == 3:
print(f"The current wavelength range is between \
{np.round(np.min(spec['wavelength']))} and \
{np.round(np.max(spec['wavelength']))}.")
do_crop = str2bool(input("Do you want to crop the spectrum? \n"))
if do_crop is True:
spec = crop_spectrum(spec)
# Metadata
# For LRIS:
if args.inst_id == 7:
meta = spec._meta['comments']
# other instruments:
else:
try:
meta = spec.meta['header']
except KeyError:
print("WARNING! Unable to find header metadata")
print("No metadata will be uploaded")
meta = None
# Extract the source filename
if not "ZTF" in source_filename:
source = input(f"No 'ZTF' found in the file name, please enter \
the name of the source for the spectrum {source_filename}:\n")
else:
span = re.search("ZTF", source_filename).span()
source = source_filename[span[0]:span[0]+12]
response = check_source_exists(source)
if response.status_code != 200:
print(f"Skipping {source}...")
continue
# Groups
groups = get_groups(source)
group_ids = []
if len(groups) == 0:
print(f"{source} is not saved in any group")
else:
print(f"{source} was saved in the following groups:")
for g in groups:
print(f"{g['id']}, {g['name']}")
group_ids.append(g['id'])
print(f"The spectrum will be sent to groups {group_ids}")
ok_groups = str2bool(input("Are you happy with that?\n") or 'y')
if ok_groups is False:
group_ids = input("Please enter comma-separated group IDs (int)\n")
group_ids = group_ids.split(",")
group_ids = [int(g) for g in group_ids]
# Upload
print(f"Uploading spectrum {source_filename} for source {source} \
to group IDs {group_ids}")
upload_spectrum(spec, observers, reducers, group_ids=group_ids,
date=Time(args.date).iso, inst_id=args.inst_id,
ztfid=source, meta=meta)