Repository navigation
Expand file tree
/
Copy pathascii.py
More file actions
100 lines (75 loc) · 3.08 KB
/
Copy pathascii.py
File metadata and controls
100 lines (75 loc) · 3.08 KB
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
import os
from astropy.table import Table
from ..registers import data_loader
from ..parsing_utils import (generic_spectrum_from_table,
spectrum_from_column_mapping)
__all__ = ['ascii_identify', 'ascii_loader', 'ipac_identify', 'ipac_loader']
def ascii_identify(origin, *args, **kwargs):
"""Check if it's an ASCII file."""
name = os.path.basename(args[0])
if name.lower().split('.')[-1] in ['txt', 'ascii']:
return True
return False
@data_loader(
label="ASCII", identifier=ascii_identify, extensions=['txt', 'ascii'],
priority=-10
)
def ascii_loader(file_name, column_mapping=None, **kwargs):
"""
Load spectrum from ASCII file.
Parameters
----------
file_name: str
The path to the ASCII file.
column_mapping : dict
A dictionary describing the relation between the ASCII file columns
and the arguments of the `Spectrum` class, along with unit
information. The dictionary keys should be the ASCII file column names
while the values should be a two-tuple where the first element is the
associated `Spectrum` keyword argument, and the second element is the
unit for the ASCII file column::
column_mapping = {'FLUX': ('flux', 'Jy')}
Returns
-------
data: Spectrum
The spectrum that is represented by the data in this table.
"""
tab = Table.read(file_name, format='ascii')
# If no column mapping is given, attempt to parse the ascii files using
# unit information
if column_mapping is None:
return generic_spectrum_from_table(tab)
return spectrum_from_column_mapping(tab, column_mapping)
def ipac_identify(*args, **kwargs):
"""Check if it's an IPAC-style ASCII file."""
name = os.path.basename(args[0])
if name.lower().split('.')[-1] in ['txt', 'dat']:
return True
return False
@data_loader(label="IPAC", identifier=ipac_identify, extensions=['txt', 'dat'])
def ipac_loader(file_name, column_mapping=None, **kwargs):
"""
Load spectrum from IPAC-style ASCII file
Parameters
----------
file_name: str
The path to the IPAC-style ASCII file.
column_mapping : dict
A dictionary describing the relation between the IPAC-style ASCII
file columns and the arguments of the `Spectrum` class, along with
unit information. The dictionary keys should be the IPAC-style ASCII
file column names while the values should be a two-tuple where the
first element is the associated `Spectrum` keyword argument, and the
second element is the unit for the IPAC-style ASCII file column::
column_mapping = {'FLUX': ('flux', 'Jy')}
Returns
-------
data: Spectrum
The spectrum that is represented by the data in this table.
"""
tab = Table.read(file_name, format='ascii.ipac')
# If no column mapping is given, attempt to parse the ascii files using
# unit information
if column_mapping is None:
return generic_spectrum_from_table(tab)
return spectrum_from_column_mapping(tab, column_mapping)