Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Velodiff #315

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ redrock Change Log
0.20.2 (unreleased)
-------------------

* No changes yet.
* Module file cleanup env vars moved to desimodules (PR `#313`_).

.. _`#313`: https://github.com/desihub/redrock/pull/313

0.20.1 (2024-06-04)
-------------------
Expand Down
6 changes: 0 additions & 6 deletions etc/redrock.module
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,3 @@ setenv [string toupper $product] $PRODUCT_DIR
#
# Add any non-standard Module code below this point.
#
# MPI or multiprocessing handle parallelism, not OpenMP via numpy/MKL
#
setenv KMP_AFFINITY disabled
setenv MPICH_GNI_FORK_MODE FULLCOPY
setenv OMP_NUM_THREADS 1
setenv HDF5_USE_FILE_LOCKING FALSE
2 changes: 2 additions & 0 deletions py/redrock/external/desi.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ def write_zbest(outfile, zbest, fibermap, exp_fibermap, tsnr2,

Modifies input tables.meta['EXTNAME']
"""
# header keywords for both PRIMARY and REDSHIFTS HDU
header = _get_header(templates, archetypes, spec_header)

zbest.meta.update(header)
zbest.meta['EXTNAME'] = 'REDSHIFTS'
fibermap.meta['EXTNAME'] = 'FIBERMAP'
exp_fibermap.meta['EXTNAME'] = 'EXP_FIBERMAP'
Expand Down
7 changes: 4 additions & 3 deletions py/redrock/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,19 +404,20 @@ def header2templatefiles(hdr, template_dir=None):
return filenames


def load_templates_from_header(hdr, template_dir=None):
def load_templates_from_header(hdr, template_dir=None, asdict=False):
"""Return templates matching header keywords

Args:
hdr: dict-like with keys TEMNAMnn+TEMVERnn and/or TEMFILnn

Options:
template_dir (str): full path to redrock-templates directory

asdict (bool): return dict keyed by (spectype, subtype) instead of list

Returns list of Template objects
"""
template_filenames = header2templatefiles(hdr, template_dir=template_dir)
return load_templates(template_filenames)
return load_templates(template_filenames, asdict=asdict)

def load_templates(template_path=None, zscan_galaxy=None, zscan_star=None, zscan_qso=None,
asdict=False):
Expand Down
34 changes: 23 additions & 11 deletions py/redrock/zfind.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def calc_deltachi2(chi2, z, zwarn, dvlimit=constants.max_velo_diff):
zwarn : array of zwarn values

Options:
dvlimit: exclude candidates that are closer than dvlimit [km/s]
dvlimit: exclude candidates that are closer than dvlimit [km/s],
uses minimum value of the pair

Returns (deltachi2, setzwarn) where `deltachi2` is array of chi2 differences
to next best good fit, and `setzwarn` is boolean array of whether
Expand All @@ -123,9 +124,14 @@ def calc_deltachi2(chi2, z, zwarn, dvlimit=constants.max_velo_diff):
nz = len(chi2)
deltachi2 = np.zeros(nz)
okfit = (zwarn & badfit_mask) == 0

# if no dvlimit passed, use constant threshold
if np.isscalar(dvlimit):
dvlimit = np.full(nz, dvlimit)

for i in range(len(chi2)-1):
dv = get_dv(z[i+1:], z[i])
ii = (np.abs(dv)>dvlimit) & okfit[i+1:]
ii = (np.abs(dv)>np.minimum(dvlimit[i],dvlimit[i+1:])) & okfit[i+1:]
if np.any(ii):
dchi2 = chi2[i+1:] - chi2[i]
deltachi2[i] = np.min(dchi2[ii])
Expand All @@ -139,9 +145,10 @@ def calc_deltachi2(chi2, z, zwarn, dvlimit=constants.max_velo_diff):
noti[i] = False
alldeltachi2 = np.absolute(chi2[noti] - chi2[i])
alldv = np.absolute(get_dv(z=z[noti], zref=z[i]))
alldvlimit = np.minimum(dvlimit[i], dvlimit[noti])
zwarn = np.any( okfit[noti] &
(alldeltachi2 < constants.min_deltachi2) &
(alldv >= dvlimit) )
(alldv >= alldvlimit) )
if zwarn:
setzwarn[i] = True

Expand Down Expand Up @@ -476,13 +483,6 @@ def zfind(targets, templates, mp_procs=1, nminima=3, archetypes=None, priors=Non
else:
spectype, subtype = parse_fulltype(fulltype)

# set max_velo_diff differently for STARs, but watch out
# for archtypes which have spectype as list instead of scalar
if (np.isscalar(spectype) and spectype.upper() == 'STAR') or spectype[0].upper() == 'STAR':
max_velo_diff = constants.max_velo_diff_star
else:
max_velo_diff = constants.max_velo_diff

#Have to create arrays of correct length since using dict of
#np arrays instead of astropy Table
nmin = len(tmp['chi2'])
Expand All @@ -496,6 +496,15 @@ def zfind(targets, templates, mp_procs=1, nminima=3, archetypes=None, priors=Non
tmp['subtype'] = np.array([subtype]).reshape((nmin, 1))

tmp['ncoeff'] = np.array([tmp['coeff'].shape[1]]*nmin).reshape((nmin, 1))

# set max_velo_diff differently for STARs, but watch out
# for archtypes which have spectype as list instead of scalar
if (np.isscalar(spectype) and spectype.upper() == 'STAR') or spectype[0].upper() == 'STAR':
max_velo_diff = constants.max_velo_diff_star
else:
max_velo_diff = constants.max_velo_diff
tmp['max_velo_diff'] = np.full((nmin,1), max_velo_diff)

tzfit.append(tmp)
del allresults[tid][fulltype]['zfit']

Expand Down Expand Up @@ -570,10 +579,13 @@ def zfind(targets, templates, mp_procs=1, nminima=3, archetypes=None, priors=Non

#- calc deltachi2 and set ZW.SMALL_DELTA_CHI2 flag
deltachi2, setzwarn = calc_deltachi2(tzfit['chi2'], tzfit['z'], tzfit['zwarn'],
dvlimit=max_velo_diff)
dvlimit=tzfit['max_velo_diff'])
tzfit['deltachi2'] = deltachi2
tzfit['zwarn'][setzwarn] |= ZW.SMALL_DELTA_CHI2

# remove max_velo_diff column
del tzfit['max_velo_diff']

# Store
# Here convert to astropy table
allzfit.append(astropy.table.Table(tzfit))
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Based on desimodules/22.2.
numpy<2.0
astropy
scipy
h5py
Expand Down
Loading