Skip to content

Commit 106d113

Browse files
committed
fixed docstring, moved new logic to function
1 parent 16b6daa commit 106d113

1 file changed

Lines changed: 91 additions & 64 deletions

File tree

py/picca/io.py

Lines changed: 91 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,90 @@
2828
from .pk1d.prep_pk1d import spectral_resolution_desi
2929

3030

31+
def find_order(in_dir, delta_attributes):
32+
"""Finds the order of the polynomial used for the continuum fitting from the delta_attributes file
33+
34+
Args:
35+
in_dir: str
36+
Directory to spectra files. If mode is "spec-mock-1D", then it is
37+
the filename of the fits file contianing the mock spectra
38+
delta_attributes: str or None - default: None
39+
Filename for the delta attributes file. This will be used to read the
40+
order of the polynomial used for the continuum fitting, which is needed
41+
for the projection of the delta field. If None, the code will look for it
42+
at the standard position.
43+
44+
Returns:
45+
order: int or None
46+
Order of the log10(lambda) polynomial for the continuum fit.
47+
None will result in an error if the deltas are projected or if the distortion
48+
matrix is computed
49+
"""
50+
if delta_attributes is None:
51+
delta_attributes = in_dir + "/../Log/delta_attributes.fits.gz"
52+
userprint(f"WARNING: delta_attributes file not given, setting to {delta_attributes}")
53+
userprint(f"Reading delta attributes from {delta_attributes}")
54+
try:
55+
with fitsio.FITS(delta_attributes) as hdul:
56+
order = hdul["FIT_METADATA"].read_header()['FITORDER']
57+
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
58+
# this exception clause deals with deprecated delta_attributes files that do not have the FITORDER keyword
59+
# in the FIT_METADATA header. It attemps to find it elsewhere
60+
# This should be removed after a while, and simply crash
61+
except KeyError as e:
62+
userprint(f"WARNING: KeyError encountered: {str(e)}")
63+
userprint(F"WARNING: Checking for FITORDER in the STACK_DELTAS extension")
64+
userprint(f"WARNING: This is deprecated and will lead to an error in the future, please update your delta_attributes file")
65+
try:
66+
# first we try to find it in the STACK_DELTAS header, which is where it used to be in older versions of picca
67+
with fitsio.FITS(delta_attributes) as hdul:
68+
order = hdul["STACK_DELTAS"].read_header()['FITORDER']
69+
userprint("WARNING: Found FITORDER in STACK_DELTAS header, continuing the analysis")
70+
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
71+
# otherwise we try to find it in the delta config file
72+
except KeyError as e:
73+
userprint(f"WARNING: KeyError encountered: {str(e)}")
74+
userprint("WARNING: Attempting to find FITORDER from the delta config file")
75+
config_file = in_dir + "/../.config.ini"
76+
config = ConfigParser()
77+
config.read(config_file)
78+
if "expected flux" in config and "order" in config["expected flux"]:
79+
order = config["expected flux"].getint("order")
80+
userprint("WARNING: Found `order` in delta config file, continuing the analysis")
81+
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
82+
else:
83+
order = None
84+
userprint("WARNING: `order` not found in delta config file")
85+
userprint(
86+
"WARNING: Setting order=None, this will lead to an error if the deltas are projected" \
87+
"or if the distortion matrix is computed")
88+
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
89+
# this exception clause deals with the case where the delta_attributes file is not found at all,
90+
# which can happen if the user used non-standard placing of the logs. It attempts to find the order
91+
# in the delta config file, but this is deprecated and should be removed after a while
92+
# This should be removed after a while, and simply crash
93+
except OSError as e:
94+
userprint(f"WARNING: OSError encountered: {str(e)}")
95+
userprint("WARNING: Attempting to find FITORDER from the delta config file")
96+
userprint(f"WARNING: This is deprecated and will lead to an error in the future, please pass a delta_attributes file")
97+
config_file = in_dir + "/../.config.ini"
98+
config = ConfigParser()
99+
config.read(config_file)
100+
if "expected flux" in config and "order" in config["expected flux"]:
101+
order = config["expected flux"].getint("order")
102+
userprint("WARNING: Found `order` in delta config file, continuing the analysis")
103+
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
104+
else:
105+
order = None
106+
userprint("WARNING: `order` not found in delta config file")
107+
userprint(
108+
"WARNING: Setting order=None, this will lead to an error if the deltas are projected" \
109+
"or if the distortion matrix is computed")
110+
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
111+
112+
return order
113+
114+
31115
def read_dlas(filename,obj_id_name='THING_ID'):
32116
"""Reads the DLA catalog from a fits file.
33117
@@ -257,8 +341,10 @@ def read_delta_file(filename, z_min_qso=0, z_max_qso=10, rebin_factor=None, orde
257341
Specifies the maximum redshift for QSOs
258342
rebin_factor: int - default: None
259343
Factor to rebin the lambda grid by. If None, no rebinning is done.
260-
order: int - default: 1
261-
Order of the polynomial used for the continuum fitting. This is needed for the projection of the
344+
order: 0, 1 or None - default: None
345+
Order of the log10(lambda) polynomial for the continuum fit
346+
None will result in the code crashing if the deltas are projected
347+
or if they are used to compute the distortion matrix
262348
Returns:
263349
deltas:
264350
A dictionary with the data. Keys are the healpix numbers of each
@@ -344,7 +430,8 @@ def read_deltas(in_dir,
344430
delta_attributes: str or None - default: None
345431
Filename for the delta attributes file. This will be used to read the
346432
order of the polynomial used for the continuum fitting, which is needed
347-
for the projection of the delta field. If None, the order will be set to 1
433+
for the projection of the delta field. If None, the code will look for it
434+
at the standard position.
348435
349436
Returns:
350437
The following variables:
@@ -372,67 +459,7 @@ def read_deltas(in_dir,
372459
if rebin_factor is not None:
373460
userprint(f"Rebinning deltas by a factor of {rebin_factor}\n")
374461

375-
if delta_attributes is None:
376-
delta_attributes = in_dir + "/../Log/delta_attributes.fits.gz"
377-
userprint(f"WARNING: delta_attributes file not given, setting to {delta_attributes}")
378-
userprint(f"Reading delta attributes from {delta_attributes}")
379-
try:
380-
with fitsio.FITS(delta_attributes) as hdul:
381-
order = hdul["FIT_METADATA"].read_header()['FITORDER']
382-
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
383-
# this exception clause deals with deprecated delta_attributes files that do not have the FITORDER keyword
384-
# in the FIT_METADATA header. It attemps to find it elsewhere
385-
# This should be removed after a while, and simply crash
386-
except KeyError as e:
387-
userprint(f"WARNING: KeyError encountered: {str(e)}")
388-
userprint(F"WARNING: Checking for FITORDER in the STACK_DELTAS extension")
389-
userprint(f"WARNING: This is deprecated and will lead to an error in the future, please update your delta_attributes file")
390-
try:
391-
# first we try to find it in the STACK_DELTAS header, which is where it used to be in older versions of picca
392-
with fitsio.FITS(delta_attributes) as hdul:
393-
order = hdul["STACK_DELTAS"].read_header()['FITORDER']
394-
userprint("WARNING: Found FITORDER in STACK_DELTAS header, continuing the analysis")
395-
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
396-
# otherwise we try to find it in the delta config file
397-
except KeyError as e:
398-
userprint(f"WARNING: KeyError encountered: {str(e)}")
399-
userprint("WARNING: Attempting to find FITORDER from the delta config file")
400-
config_file = in_dir + "/../.config.ini"
401-
config = ConfigParser()
402-
config.read(config_file)
403-
if "expected flux" in config and "order" in config["expected flux"]:
404-
order = config["expected flux"].getint("order")
405-
userprint("WARNING: Found `order` in delta config file, continuing the analysis")
406-
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
407-
else:
408-
order = None
409-
userprint("WARNING: `order` not found in delta config file")
410-
userprint(
411-
"WARNING: Setting order=None, this will lead to an error if the deltas are projected" \
412-
"or if the distortion matrix is computed")
413-
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
414-
# this exception clause deals with the case where the delta_attributes file is not found at all,
415-
# which can happen if the user used non-standard placing of the logs. It attempts to find the order
416-
# in the delta config file, but this is deprecated and should be removed after a while
417-
# This should be removed after a while, and simply crash
418-
except OSError as e:
419-
userprint(f"WARNING: OSError encountered: {str(e)}")
420-
userprint("WARNING: Attempting to find FITORDER from the delta config file")
421-
userprint(f"WARNING: This is deprecated and will lead to an error in the future, please pass a delta_attributes file")
422-
config_file = in_dir + "/../.config.ini"
423-
config = ConfigParser()
424-
config.read(config_file)
425-
if "expected flux" in config and "order" in config["expected flux"]:
426-
order = config["expected flux"].getint("order")
427-
userprint("WARNING: Found `order` in delta config file, continuing the analysis")
428-
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
429-
else:
430-
order = None
431-
userprint("WARNING: `order` not found in delta config file")
432-
userprint(
433-
"WARNING: Setting order=None, this will lead to an error if the deltas are projected" \
434-
"or if the distortion matrix is computed")
435-
userprint(f"Setting order={order} for the polynomial used for the continuum fitting")
462+
order = find_order(in_dir, delta_attributes)
436463

437464
arguments = [(f, z_min_qso, z_max_qso, rebin_factor, order) for f in files]
438465
pool = Pool(processes=nproc)

0 commit comments

Comments
 (0)