/
snanaio.py
525 lines (430 loc) · 18 KB
/
snanaio.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
from collections import OrderedDict as odict
import numpy as np
from astropy.io import fits
from astropy.table import Table, vstack
import yaml
__all__ = ['read_snana_ascii', 'read_snana_fits', 'read_snana_simlib']
def read_snana_fits(head_file, phot_file, snids=None, n=None):
"""Read the SNANA FITS format: two FITS files jointly representing
metadata and photometry for a set of SNe.
Parameters
----------
head_file : str
Filename of "HEAD" ("header") FITS file.
phot_file : str
Filename of "PHOT" ("photometry") FITS file.
snids : list of str, optional
If given, only return the single entry with the matching SNIDs.
n : int
If given, only return the first `n` entries.
Returns
-------
sne : list of `~astropy.table.Table`
Each item in the list is an astropy Table instance.
Notes
-----
If `head_file` contains a column 'SNID' containing strings, leading and
trailing whitespace is stripped from all the values in that column.
If `phot_file` contains a column 'FLT', leading and trailing whitespace
is stripped from all the values in that column.
Examples
--------
>>> sne = read_snana_fits('HEAD.fits', 'PHOT.fits')
>>> for sn in sne:
... sn.meta # Metadata in an OrderedDict.
... sn['MJD'] # MJD column
"""
# Should we memmap? Only if we're going to read only a part of the file
memmap = (snids is not None or n is not None)
# Get metadata for all the SNe
head_data = fits.getdata(head_file, 1, view=np.ndarray)
phot_data = fits.getdata(phot_file, 1, view=np.ndarray, memmap=memmap)
# Strip trailing whitespace characters from SNID.
if 'SNID' in head_data.dtype.names:
try:
head_data['SNID'][:] = np.char.strip(head_data['SNID'])
except TypeError:
pass
# Check which indicies to return.
if snids is None and n is None:
idx = range(len(head_data))
elif n is None:
if 'SNID' not in head_data.dtype.names:
raise RuntimeError('Specific snids requested, but head file does'
' not contain SNID column')
idx = []
for snid in snids:
i = np.flatnonzero(head_data['SNID'] == snid)
if len(i) != 1:
raise RuntimeError('Unique snid requested, but there are '
'{0:d} matching entries'.format(len(i)))
idx.append(i[0])
elif snids is None:
idx = range(n)
else:
raise ValueError("cannot specify both 'snids' and 'n' arguments")
# Loop over SNe in HEAD file
sne = []
for i in idx:
meta = odict(zip(head_data.dtype.names, head_data[i]))
j0 = head_data['PTROBS_MIN'][i] - 1
j1 = head_data['PTROBS_MAX'][i]
data = phot_data[j0:j1]
if 'FLT' in data.dtype.names:
data['FLT'][:] = np.char.strip(data['FLT'])
sne.append(Table(data, meta=meta, copy=False))
return sne
def read_snana_ascii(fname, default_tablename=None):
"""Read an SNANA-format ascii file.
Such files may contain metadata lines and one or more tables. See Notes
for a summary of the format.
Parameters
----------
fname : str
Filename of object to read.
default_tablename : str, optional
Default tablename, or the string that indicates a table row, when
a table starts with 'NVAR:' rather than 'NVAR_TABLENAME:'.
array : bool, optional
If True, each table is converted to a numpy array. If False, each
table is a dictionary of lists (each list is a column). Default is
True.
Returns
-------
meta : OrderedDict
Metadata from keywords.
tables : dict of `~astropy.table.Table`
Tables, indexed by table name.
Notes
-----
The file can contain one or more tables, as well as optional metadata.
Here is an example of the expected format::
META1: a
META2: 6
NVAR_SN: 3
VARNAMES: A B C
SN: 1 2.0 x
SN: 4 5.0 y
Behavior:
* Any strings ending in a colon (:) are treated as keywords.
* The start of a new table is indicated by a keyword starting with
'NVAR'.
* If the 'NVAR' is followed by an underscore (e.g., 'NVAR_TABLENAME'),
then 'TABLENAME' is taken to be the name of the table. Otherwise the
user *must specify* a ``default_tablename``. This is because data
rows are identified by the tablename.
* After a keyword starting with 'NVAR', the next keyword must be
'VARNAMES'. The strings following give the column names.
* Any other keywords anywhere in the file are treated as metadata. The
first string after the keyword is treated as the value for that keyword.
* **Note:** Newlines are treated as equivalent to spaces; they do not
indicate a new row. This is necessary because some SNANA-format files
have multiple metadata on a single row or single table rows split over
multiple lines, making newline characters meaningless.
Examples
--------
>>> from io import StringIO # StringIO behaves like a file
>>> f = StringIO('META1: a\\n'
... 'META2: 6\\n'
... 'NVAR_SN: 3\\n'
... 'VARNAMES: A B C\\n'
... 'SN: 1 2.0 x\\n'
... 'SN: 4 5.0 y\\n')
...
>>> meta, tables = read_snana_ascii(f)
The first object is a dictionary of metadata:
>>> meta
OrderedDict([('META1', 'a'), ('META2', 6)])
The second is a dictionary of all the tables in the file:
>>> tables['SN']
<Table rows=2 names=('A','B','C')>
array([(1, 2.0, 'x'), (4, 5.0, 'y')],
dtype=[('A', '<i8'), ('B', '<f8'), ('C', 'S1')])
If the file had an 'NVAR' keyword rather than 'NVAR_SN', for example::
NVAR: 3
VARNAMES: A B C
SN: 1 2.0 x
SN: 4 5.0 y
SN: 5 8.2 z
it can be read by supplying a default table name:
>>> meta, tables = read_snana_ascii(f, default_tablename='SN')
"""
meta = odict() # initialize structure to hold metadata.
tables = {} # initialize structure to hold data.
if isinstance(fname, str):
fh = open(fname, 'r')
else:
fh = fname
words = fh.read().split()
fh.close()
i = 0
nvar = None
tablename = None
while i < len(words):
word = words[i]
# If the word starts with 'NVAR', we are starting a new table.
if word.startswith('NVAR'):
nvar = int(words[i + 1])
# Infer table name. The name will be used to designate a data row.
if '_' in word:
pos = word.find('_') + 1
tablename = word[pos:].rstrip(':')
elif default_tablename is not None:
tablename = default_tablename
else:
raise ValueError(
'Table name must be given as part of NVAR keyword so '
'that rows belonging to this table can be identified. '
'Alternatively, supply the default_tablename keyword.')
table = odict()
tables[tablename] = table
i += 2
# If the word starts with 'VARNAMES', the following `nvar` words
# define the column names of the table.
elif word.startswith('VARNAMES') or word.startswith('VARLIST'):
# Check that nvar is defined and that no column names are defined
# for the current table.
if nvar is None or len(table) > 0:
raise Exception('NVAR must directly precede VARNAMES')
# Read the column names
for j in range(i + 1, i + 1 + nvar):
table[words[j]] = []
i += nvar + 1
# If the word matches the current tablename, we are reading a data row.
elif word.rstrip(':') == tablename:
for j, colname in enumerate(table.keys()):
table[colname].append(words[i + 1 + j])
i += nvar + 1
# Otherwise, we are reading metadata or some comment
# If the word ends with ":", it is metadata.
elif word[-1] == ':':
name = word[:-1] # strip off the ':'
if len(words) >= i + 2:
try:
val = int(words[i + 1])
except ValueError:
try:
val = float(words[i + 1])
except ValueError:
val = words[i + 1]
meta[name] = val
else:
meta[name] = None
i += 2
else:
# It is some comment; continue onto next word.
i += 1
# All values in each column are currently strings. Convert to int or
# float if possible.
for table in tables.values():
for colname, values in table.items():
try:
table[colname] = [int(val) for val in values]
except ValueError:
try:
table[colname] = [float(val) for val in values]
except ValueError:
pass
# All tables are dictionaries. Convert them to Tables
for tablename in tables.keys():
tables[tablename] = Table(tables[tablename])
return meta, tables
def read_snana_ascii_multi(fnames, default_tablename=None):
"""Like ``read_snana_ascii()``, but read from multiple files containing
the same tables and glue results together into big tables.
Parameters
----------
fnames : list of str
List of filenames.
Returns
-------
tables : dictionary of `~astropy.table.Table`
Tables indexed by table names.
Examples
--------
>>> tables = read_snana_ascii_multi(['data1.txt', 'data1.txt'])
"""
alltables = {}
for fname in fnames:
meta, tables = read_snana_ascii(fname,
default_tablename=default_tablename)
for key, table in tables.items():
if key in alltables:
alltables[key].append(table)
else:
alltables[key] = [table]
for key in alltables.keys():
alltables[key] = vstack(alltables[key])
return alltables
def _parse_meta_from_line(line):
"""Return dictionary from key, value pairs on a line. Helper function for
snana_read_simlib."""
meta = odict()
# Find position of all the colons
colon_pos = []
i = line.find(':')
while i != -1:
colon_pos.append(i)
i = line.find(':', i+1)
# Find position of start of words before colons
key_pos = []
for i in colon_pos:
j = line.rfind(' ', 0, i)
key_pos.append(j+1)
# append an extra key position so that we know when to end the last value.
key_pos.append(len(line))
# get the keys, values based on positions above.
for i in range(len(colon_pos)):
key = line[key_pos[i]: colon_pos[i]]
val = line[colon_pos[i]+1: key_pos[i+1]].strip()
try:
val = int(val)
except ValueError:
try:
val = float(val)
except ValueError:
pass
meta[key] = val
return meta
def read_snana_simlib(fname):
"""Read an SNANA 'simlib' (simulation library) ascii file.
Parameters
----------
fname : str
Filename.
Returns
-------
meta : `OrderedDict`
Global meta data, not associated with any one LIBID. If
DOCANA is present, it is stored in `meta['DOCUMENTATION']`.
observation_sets : `OrderedDict` of `astropy.table.Table`
keys are LIBIDs, values are observation sets.
Notes
-----
* Anything following '#' on each line is ignored as a comment.
* Keywords are space separated strings ending wth a colon.
* If a line starts with 'LIBID:', the following lines are associated
with the value of LIBID, until 'END_LIBID:' is encountered.
* While reading a given LIBID, lines starting with 'S' or 'T'
keywords are assumed to contain 12 space-separated values after
the keyword. These are (1) MJD, (2) IDEXPT, (3) FLT, (4) CCD GAIN,
(5) CCD NOISE, (6) SKYSIG, (7) PSF1, (8) PSF2, (9) PSF 2/1 RATIO,
(10) ZPTAVG, (11) ZPTSIG, (12) MAG.
* Column (2) may represent co-added observations in a '111*1' format. In
this case, the 'IDEXPT' column is split at the '*' into 'IDEXPT' and
'NEXPOSE'
* Other lines inside a 'LIBID:'/'END_LIBID:' pair are treated as metadata
for that LIBID.
* Any other keywords outside a 'LIBID:'/'END_LIBID:' pair are treated
as global header keywords and are returned in the `meta` dictionary.
Examples
--------
>>> meta, obs_sets = read_snana_simlib('filename')
The second object is a dictionary of astropy Tables indexed by LIBID:
>>> obs_sets.keys()
[0, 1, 2, 3, 4]
Each table (libid) has metadata:
>>> obs_sets[0].meta
OrderedDict([('LIBID', 0), ('RA', 52.5), ('DECL', -27.5), ('NOBS', 161),
('MWEBV', 0.0), ('PIXSIZE', 0.27)])
Each table has the following columns:
>>> obs_sets[0].colnames
['SEARCH', 'MJD', 'IDEXPT', 'FLT', 'CCD_GAIN', 'CCD_NOISE', 'SKYSIG',
'PSF1', 'PSF2', 'PSFRATIO', 'ZPTAVG', 'ZPTSIG', 'MAG']
"""
from astropy.table import Table
COLNAMES = ['SEARCH', 'MJD', 'IDEXPT', 'FLT', 'CCD_GAIN', 'CCD_NOISE',
'SKYSIG', 'PSF1', 'PSF2', 'PSFRATIO', 'ZPTAVG', 'ZPTSIG',
'MAG']
# Not used yet... if present in header, add to table.
SPECIAL = ['FIELD', 'TELESCOPE', 'PIXSIZE']
meta = odict() # global metadata
observation_sets = odict() # dictionary of tables indexed by LIBID
reading_obsset = False
reading_docana = False
docana = ''
with open(fname, 'r') as infile:
for line in infile.readlines():
# strip comments
idx = line.find('#')
if idx != -1:
line = line[0:idx]
# split on spaces.
words = line.split()
if len(words) == 0:
continue
# If we're not currently reading an obs set, check if this line
# is the start of one. If it isn't, update the global metadata.
if not reading_obsset:
if line[0:6] == 'LIBID:':
reading_obsset = True
current_meta = _parse_meta_from_line(line)
current_data = odict([(key, []) for key in COLNAMES])
else:
# If we're currently reading an documentation block, add
# it to the DOCANA string.
# If we're not, check if this line is the start of one,
# then update the global metadata.
if reading_docana:
if line[0:18] == 'DOCUMENTATION_END:':
reading_docana = False # And then skip line
meta.update(
odict({'DOCUMENTATION':
yaml.safe_load(docana)})
)
docana = ''
else:
docana += line + '\n'
else:
if line[0:14] == 'DOCUMENTATION:':
reading_docana = True # And then skip line
else:
meta.update(_parse_meta_from_line(line))
# If we are currently reading an obsset...
else:
# Check for the explicit end of the obs set.
if line[0:10] == 'END_LIBID:':
reading_obsset = False
observation_sets[current_meta['LIBID']] = \
Table(current_data, meta=current_meta)
# Sometimes there's not an explicit end, but the next one
# starts anyway.
elif line[0:6] == 'LIBID:':
observation_sets[current_meta['LIBID']] = \
Table(current_data, meta=current_meta)
current_meta = _parse_meta_from_line(line)
current_data = odict([(key, []) for key in COLNAMES])
# Otherwise, read the line into the current obs set.
elif line[0:2] in ['S:', 'T:']:
words = line.split()
colnames = ['SEARCH', 'MJD', 'IDEXPT', 'FLT', 'CCD_GAIN',
'CCD_NOISE', 'SKYSIG', 'PSF1', 'PSF2',
'PSFRATIO', 'ZPTAVG', 'ZPTSIG', 'MAG']
values = [words[0] == 'S:', float(words[1]), None,
words[3], float(words[4]), float(words[5]),
float(words[6]), float(words[7]),
float(words[8]), float(words[9]),
float(words[10]), float(words[11]),
float(words[12])]
if "*" in words[2]:
# 'IDEXPT' can be a co-add of the form: '2063*2'
# re-process assuming co-added expsoures
# ('IDEXPT' -> 'IDEXPT', 'NEXPOSE' )
colnames.insert(3, "NEXPOSE")
if 'NEXPOSE' not in current_data:
# add an empty list only on the first data line
current_data['NEXPOSE'] = []
values[2] = int(words[2].split('*')[0])
values.insert(3, int(words[2].split('*')[1]))
else:
values[2] = int(words[2])
for colname, val in zip(colnames, values):
current_data[colname].append(val)
else:
current_meta.update(_parse_meta_from_line(line))
# At the end, check for the case where there's not an explicit end
# to the last obs set:
if reading_obsset:
observation_sets[current_meta['LIBID']] = \
Table(current_data, meta=current_meta)
return meta, observation_sets