TARDIS-Spectrum-Filtering 1.0.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- TARDIS_Spectrum_Filtering/TARDIS_Spectrum_Filtering.py +134 -0
- TARDIS_Spectrum_Filtering/__init__.py +5 -0
- TARDIS_Spectrum_Filtering/common.py +7 -0
- tardis_spectrum_filtering-1.0.0.dist-info/METADATA +37 -0
- tardis_spectrum_filtering-1.0.0.dist-info/RECORD +9 -0
- tardis_spectrum_filtering-1.0.0.dist-info/WHEEL +6 -0
- tardis_spectrum_filtering-1.0.0.dist-info/entry_points.txt +2 -0
- tardis_spectrum_filtering-1.0.0.dist-info/licenses/LICENSE +22 -0
- tardis_spectrum_filtering-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Main module."""
|
|
2
|
+
import os
|
|
3
|
+
import xml.etree.ElementTree as et
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import numpy as np
|
|
6
|
+
import requests
|
|
7
|
+
import yaml
|
|
8
|
+
from astropy import units as u
|
|
9
|
+
|
|
10
|
+
# Function to get Filter URL from TARDIS config file
|
|
11
|
+
def get_url_from_config(config_file_path):
|
|
12
|
+
|
|
13
|
+
with open(config_file_path, 'r') as f:
|
|
14
|
+
config = yaml.safe_load(f)
|
|
15
|
+
telescope = config['filter']['Telescope_Name']
|
|
16
|
+
instrument = config['filter']['Instrument']
|
|
17
|
+
filter_id = config['filter']['Filter_ID']
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
name = f"{telescope}/{instrument}.{filter_id}"
|
|
21
|
+
safe_name = name.replace('/', '.')
|
|
22
|
+
url = f"https://svo2.cab.inta-csic.es/theory/fps/fps.php?ID={name}"
|
|
23
|
+
|
|
24
|
+
return url, safe_name
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Function to check if the filter file is valid
|
|
28
|
+
def check_filter(filter_name):
|
|
29
|
+
|
|
30
|
+
root = et.parse(f"Filters/{filter_name}.xml")
|
|
31
|
+
|
|
32
|
+
info = root.find('INFO')
|
|
33
|
+
|
|
34
|
+
check = info.get('value')
|
|
35
|
+
|
|
36
|
+
if check == 'ERROR':
|
|
37
|
+
return False
|
|
38
|
+
elif info is None:
|
|
39
|
+
return False
|
|
40
|
+
else:
|
|
41
|
+
return True
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Function to download the filter file
|
|
45
|
+
def download_filter(url, filename):
|
|
46
|
+
req = requests.get(url, timeout = 10)
|
|
47
|
+
|
|
48
|
+
with open((f'Filters/{filename}.xml'), 'wb') as f:
|
|
49
|
+
|
|
50
|
+
# Chunking to avoid large memory consumption
|
|
51
|
+
for chunk in req.iter_content(chunk_size=8192):
|
|
52
|
+
if chunk:
|
|
53
|
+
f.write(chunk)
|
|
54
|
+
|
|
55
|
+
if check_filter(filename) == True:
|
|
56
|
+
print("Filter URL is valid.")
|
|
57
|
+
return filename
|
|
58
|
+
elif check_filter(filename) == False:
|
|
59
|
+
print("Filter URL is not valid. Removing invalid filter file.")
|
|
60
|
+
os.remove(f'Filters/{filename}.xml')
|
|
61
|
+
raise ValueError("Invalid Filter URL. The filter file has been removed.")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# Function to get wavelength and transmission values from filter file
|
|
65
|
+
def get_filter(filter_name):
|
|
66
|
+
|
|
67
|
+
# Parse XML File from Filters Directory
|
|
68
|
+
root = et.parse(f"Filters/{filter_name}.xml")
|
|
69
|
+
|
|
70
|
+
# Get wavelength and transmission values in one array (Will be in aleternating order)
|
|
71
|
+
all_vals = np.array([float(x.text) for x in root.findall('.//TD')])
|
|
72
|
+
|
|
73
|
+
# Separate wavelength and transmission values
|
|
74
|
+
wl = all_vals[0::2] * u.AA
|
|
75
|
+
tr = all_vals[1::2]
|
|
76
|
+
return wl, tr
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# Function to interpolate filter to match TARDIS Spectrum
|
|
80
|
+
def interp_filter(spectrum_to_filter, filter_name):
|
|
81
|
+
#Interpolate filter transmission values to match TARDIS Spectrum
|
|
82
|
+
wl, tr = get_filter(filter_name)
|
|
83
|
+
return np.interp(spectrum_to_filter, wl, tr)
|
|
84
|
+
|
|
85
|
+
# Function to apply filter to TARDIS Spectrum
|
|
86
|
+
def apply_filter(spectrum, spectrum_virtual, spectrum_integrated, chosen_filter):
|
|
87
|
+
|
|
88
|
+
# Interpolate filter transmission values to match TARDIS Spectrum
|
|
89
|
+
prepared_filter = interp_filter(spectrum.wavelength, chosen_filter)
|
|
90
|
+
|
|
91
|
+
# Apply filter to TARDIS Spectrum
|
|
92
|
+
filtered_spectrum = spectrum.luminosity_density_lambda * prepared_filter
|
|
93
|
+
filtered_spectrum_virt = spectrum_virtual.luminosity_density_lambda * prepared_filter
|
|
94
|
+
filtered_spec_integ = spectrum_integrated.luminosity_density_lambda * prepared_filter
|
|
95
|
+
return filtered_spectrum, filtered_spectrum_virt, filtered_spec_integ
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# Function to plot original TARDIS Spectrum
|
|
99
|
+
def plot_original_spectrum(spectrum, spectrum_virtual, spectrum_integrated):
|
|
100
|
+
# Plot TARDIS Spectrum before filtering
|
|
101
|
+
plt.figure()
|
|
102
|
+
plt.plot(spectrum.wavelength, spectrum.luminosity_density_lambda)
|
|
103
|
+
plt.plot(spectrum.wavelength, spectrum_virtual.luminosity_density_lambda)
|
|
104
|
+
plt.plot(spectrum.wavelength, spectrum_integrated.luminosity_density_lambda)
|
|
105
|
+
plt.xlabel("Wavelength (Angstrom)")
|
|
106
|
+
plt.ylabel("Luminosity Density (erg/s/Angstrom)")
|
|
107
|
+
plt.title("Unfiltered TARDIS Spectrum")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# Function to plot filter transmission curve
|
|
111
|
+
def plot_filter(spectrum, chosen_filter):
|
|
112
|
+
|
|
113
|
+
# Interpolate filter transmission values to match TARDIS Spectrum
|
|
114
|
+
prepared_filter = interp_filter(spectrum.wavelength, chosen_filter)
|
|
115
|
+
|
|
116
|
+
# Plot the filter transmission curve
|
|
117
|
+
plt.figure()
|
|
118
|
+
plt.plot(spectrum.wavelength, prepared_filter)
|
|
119
|
+
plt.title("Filter Transmission Curve")
|
|
120
|
+
plt.xlabel("Wavelength (Angstrom)")
|
|
121
|
+
plt.ylabel("Transmission")
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# Function to plot the filtered spectrum
|
|
125
|
+
def plot_filtered_spectrum(spectrum, spectrum_virtual, spectrum_integrated, chosen_filter):
|
|
126
|
+
|
|
127
|
+
plt.figure()
|
|
128
|
+
plt.plot(spectrum.wavelength, apply_filter(spectrum, spectrum_virtual, spectrum_integrated, chosen_filter)[0])
|
|
129
|
+
plt.plot(spectrum.wavelength, apply_filter(spectrum, spectrum_virtual, spectrum_integrated, chosen_filter)[1])
|
|
130
|
+
plt.plot(spectrum.wavelength, apply_filter(spectrum, spectrum_virtual, spectrum_integrated, chosen_filter)[2])
|
|
131
|
+
plt.xlabel("Wavelength (Angstrom)")
|
|
132
|
+
plt.ylabel("Luminosity Density (erg/s/Angstrom)")
|
|
133
|
+
plt.title("Filtered TARDIS Example Model Spectrum")
|
|
134
|
+
plt.show()
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: TARDIS_Spectrum_Filtering
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Package used to apply telescope filters to spectra from Stars or Supernovae
|
|
5
|
+
Author-email: Clyde Watson <clyde.n.watson@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
Project-URL: Homepage, https://github.com/ClydeME/TARDIS_Spectrum_Filtering
|
|
8
|
+
Keywords: TARDIS_Spectrum_Filtering
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Natural Language :: English
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Python: >=3.8
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: numpy
|
|
21
|
+
Provides-Extra: all
|
|
22
|
+
Requires-Dist: spectra-filtering[extra]; extra == "all"
|
|
23
|
+
Provides-Extra: extra
|
|
24
|
+
Requires-Dist: pandas; extra == "extra"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# spectra-filtering
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[](https://pypi.python.org/pypi/spectra-filtering)
|
|
31
|
+
[](https://anaconda.org/conda-forge/spectra-filtering)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
**Package used to apply telescope filters to spectra from Stars or Supernovae**
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
- Free software: MIT License
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
TARDIS_Spectrum_Filtering/TARDIS_Spectrum_Filtering.py,sha256=9m6ajMDodJ9E99B-6pFLUsc05Q7J-7IkSNqQCPFEC0U,4807
|
|
2
|
+
TARDIS_Spectrum_Filtering/__init__.py,sha256=y_joxtHrPuHZgVWumIkTeL6xQSMtGSZMl37BKLOKiwM,149
|
|
3
|
+
TARDIS_Spectrum_Filtering/common.py,sha256=UkxoF8RazqaFu7o-ZH3HUoPu7oVGAAB3Ja02Zm6IW9I,189
|
|
4
|
+
tardis_spectrum_filtering-1.0.0.dist-info/licenses/LICENSE,sha256=J8qEL369U2AjyyxQeLNe50xQVZj1CVfxpcR-KZDYSqk,1071
|
|
5
|
+
tardis_spectrum_filtering-1.0.0.dist-info/METADATA,sha256=tNAEtN-Z4VXsKN-Wz2zqwLh-qVpkUi1CkdP3iChVpeg,1375
|
|
6
|
+
tardis_spectrum_filtering-1.0.0.dist-info/WHEEL,sha256=Mk1ST5gDzEO5il5kYREiBnzzM469m5sI8ESPl7TRhJY,110
|
|
7
|
+
tardis_spectrum_filtering-1.0.0.dist-info/entry_points.txt,sha256=VxSb86QTkEoasQ0E8LoEnYMdvYEQ0Ji1sXpvx6CH6II,81
|
|
8
|
+
tardis_spectrum_filtering-1.0.0.dist-info/top_level.txt,sha256=JAeZtUL5WUopZQbw6RgjDU0dO7I7Fytpfil1lz4lLjM,26
|
|
9
|
+
tardis_spectrum_filtering-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Clyde Watson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
TARDIS_Spectrum_Filtering
|