Skip to content

Commit

Permalink
--n-qso support modified
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi0395 committed Aug 5, 2024
1 parent 0d42bb1 commit 0915e41
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
1 change: 0 additions & 1 deletion qsoabsfind/absfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from .config import load_constants
from .spec import QSOSpecRead
from numba import jit
from astropy.table import Table

constants = load_constants()
lines, oscillator_parameters, speed_of_light = constants.lines, constants.oscillator_parameters, constants.speed_of_light
Expand Down
1 change: 0 additions & 1 deletion qsoabsfind/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,5 @@ def save_results_to_fits(results, input_file, output_file, headers, absorber):

_, _, _, metadata = read_fits_file(input_file, index=np.array(results['index_spec']))
qso_hdu = fits.BinTableHDU(metadata, name='METADATA')
import pdb;pdb.set_trace()
hdul = fits.HDUList([fits.PrimaryHDU(header=hdr), hdu, qso_hdu])
hdul.writeto(output_file, overwrite=True)
6 changes: 5 additions & 1 deletion qsoabsfind/parallel_convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import pkg_resources
from .config import load_constants
from .utils import read_nqso_from_header

def get_package_versions():
"""
Expand Down Expand Up @@ -144,7 +145,7 @@ def parallel_convolution_method_absorber_finder_QSO_spectra(fits_file, spec_indi
def main():
parser = argparse.ArgumentParser(description='Run convolution-based adaptive S/N method to search for metal doublets in SDSS/DESI-like QSO spectra in parallel.')
parser.add_argument('--input-fits-file', type=str, required=True, help='Path to the input FITS file.')
parser.add_argument('--n-qso', type=str, required=True, help="Number of QSO spectra to process, or a bash-like sequence (e.g., '1-1000', '1-1000:10').")
parser.add_argument('--n-qso', type=str, required=False, help="Number of QSO spectra to process, or a bash-like sequence (e.g., '1-1000', '1-1000:10'). If not provided, code will run all the spectra")
parser.add_argument('--absorber', type=str, required=True, help='Absorber name for searching doublets (MgII, CIV).')
parser.add_argument('--constant-file', type=str, help='Path to the constants .py file, please follow the exact same structure as qsoabsfind.constants, i.e the default parameter that the code uses')
parser.add_argument('--output', type=str, required=True, help='Path to the output FITS file.')
Expand Down Expand Up @@ -194,6 +195,9 @@ def main():
# Start timing
start_time = time.time()

if not args.n_qso:
nqso = read_nqso_from_header(args.input_fits_file)
args.n_qso = nqso
# Parse the QSO sequence
spec_indices = parse_qso_sequence(args.n_qso)

Expand Down
33 changes: 33 additions & 0 deletions qsoabsfind/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .config import load_constants
import matplotlib.pyplot as plt
import os
from astropy.io import fits

# Configure logging
#logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
Expand Down Expand Up @@ -348,3 +349,35 @@ def plot_absorber(lam, residual, absorber, zabs, xlabel='obs wave (ang)', ylabel
print(f"Plot saved as {plot_path}")
else:
plt.show()

def read_nqso_from_header(file_path, hdu_name='METADATA'):
"""
Read the NAXIS1 value from the header of a specified HDU in a FITS file.
Parameters:
- file_path: str, path to the FITS file.
- hdu_name: str, name of the HDU from which to read NAXIS1 (default: 'METADATA').
Returns:
- naxis1_value: int, value of NAXIS1 from the specified HDU header.
"""
# Check if the file exists
if not os.path.isfile(file_path):
raise FileNotFoundError(f"The FITS file {file_path} does not exist.")

# Open the FITS file in read-only mode and load headers only
with fits.open(file_path, mode='readonly') as hdul:
# Attempt to access the specified HDU by name
try:
# Load only the header of the specified HDU
header = hdul[hdu_name].header

# Read the NAXIS1 value from the header
naxis1_value = header.get('NAXIS1', None)

if naxis1_value is None:
raise KeyError(f"NAXIS1 not found in the '{hdu_name}' HDU header.")
return naxis1_value

except KeyError:
raise ValueError(f"No '{hdu_name}' HDU found in {file_path}.")

0 comments on commit 0915e41

Please sign in to comment.