<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""
=====================
desitarget.brightstar
=====================

Module for studying and masking bright stars in the sweeps
"""
from __future__ import (absolute_import, division)
#
from time import time
import numpy as np
import fitsio
from glob import glob
from astropy.coordinates import SkyCoord
from astropy import units as u

from . import __version__ as desitarget_version
from . import gitversion

from desiutil import depend
from desitarget import io
from desitarget.internal import sharedmem

def collect_bright_stars(band,maglim,numproc=4,rootdirname='/global/project/projectdirs/cosmo/data/legacysurvey/dr3.1/sweep/3.1',outfilename=False,verbose=True):
    """Extract a structure from the sweeps containing only bright stars in a given band to a given magnitude limit

    Parameters
    ----------
    band : :class:`str`
        A magnitude band from the sweeps, e.g., "G", "R", "Z"
    maglim : :class:`float`
        The upper limit in that magnitude band for which to assemble a list of bright stars
    numproc : :class:`int`, optional
        Number of processes over which to parallelize
    rootdirname : :class:`str`, optional, defaults to dr3
        Root directory containing either sweeps or tractor files...e.g. for dr3 this might be
        /global/project/projectdirs/cosmo/data/legacysurvey/dr3/sweeps/dr3.1
    outdirname : :class:`str`, optional, defaults to not writing anything to file
        File name to which to write the output structure of bright stars
    verbose : :class:`bool`, optional
        Send to write progress to screen

    Returns
    -------
    :class:`recarray`
        The structure of bright stars from the sweeps limited in the passed band to the
        passed maglim
    """

    #ADM use io.py to retrieve list of sweeps or tractor files
    infiles = io.list_sweepfiles(rootdirname)
    if len(infiles) == 0:
        infiles = io.list_tractorfiles(rootdirname)
    if len(infiles) == 0:
        print('FATAL: no sweep or tractor files found in {}'.format(rootdirname))
        sys.exit(1)

    #ADM set band to uppercase if passed as lower case
    band = band.upper()
    #ADM the band as an integer location
    bandint = "UGRIZY".find(band)

    #ADM change input magnitude to a flux to test against
    fluxlim = 10.**((22.5-maglim)/2.5)

    #ADM parallel formalism from this step forward is stolen cuts.select_targets

    #ADM function to grab the bright stars from a given file
    def _get_bright_stars(filename):
        '''Retrieves bright stars from a sweeps/Tractor file'''
        objs = io.read_tractor(filename)
        w = np.where(objs["DECAM_FLUX"][...,bandint] > fluxlim)
        if len(w[0]) > 0:
            return objs[w]

    #ADM counter for how many files have been processed
    #ADM critical to use np.ones because a numpy scalar allows in place modifications
    # c.f https://www.python.org/dev/peps/pep-3104/
    totfiles = np.ones((),dtype='i8')*len(infiles)
    nfiles = np.ones((), dtype='i8')
    t0 = time()
    def _update_status(result):
        '''wrapper function for the critical reduction operation,
        that occurs on the main parallel process'''
        if verbose and nfiles%25 == 0:
            elapsed = time() - t0<commit_msg>Allowed mask to be built from multiple passed bands<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""
=====================
desitarget.brightstar
=====================

Module for studying and masking bright stars in the sweeps
"""
from __future__ import (absolute_import, division)
#
from time import time
import numpy as np
import fitsio
from glob import glob
from astropy.coordinates import SkyCoord
from astropy import units as u
import sys

from . import __version__ as desitarget_version
from . import gitversion

from desiutil import depend
from desitarget import io
from desitarget.internal import sharedmem

def collect_bright_stars(bands,maglim,numproc=4,rootdirname='/global/project/projectdirs/cosmo/data/legacysurvey/dr3.1/sweep/3.1',outfilename=False,verbose=True):
    """Extract a structure from the sweeps containing only bright stars in a given band to a given magnitude limit

    Parameters
    ----------
    bands : :class:`str`
        A magnitude band from the sweeps, e.g., "G", "R", "Z"
        Can pass multiple bands as string, e.g. "GRZ", in which case maglim has to be a 
           list of the same length as the string
    maglim : :class:`float`
        The upper limit in that magnitude band for which to assemble a list of bright stars
        Can pass a list of magnitude limits, in which case bands has to be a string of the
           same length (e.g., "GRZ" for [12.3,12.7,12.6]
    numproc : :class:`int`, optional
        Number of processes over which to parallelize
    rootdirname : :class:`str`, optional, defaults to dr3
        Root directory containing either sweeps or tractor files...e.g. for dr3 this might be
        /global/project/projectdirs/cosmo/data/legacysurvey/dr3/sweeps/dr3.1
    outdirname : :class:`str`, optional, defaults to not writing anything to file
        File name to which to write the output structure of bright stars
    verbose : :class:`bool`, optional
        Send to write progress to screen

    Returns
    -------
    :class:`recarray`
        The structure of bright stars from the sweeps limited in the passed band(s) to the
        passed maglim(s). Only the following tags are retained:
    """

    #ADM use io.py to retrieve list of sweeps or tractor files
    infiles = io.list_sweepfiles(rootdirname)
    if len(infiles) == 0:
        infiles = io.list_tractorfiles(rootdirname)
    if len(infiles) == 0:
        print('FATAL: no sweep or tractor files found in {}'.format(rootdirname))
        sys.exit(1)

    #ADM force the input maglim to be a list (in case a single value was passed)
    if type(maglim) == type(16) or type(maglim) == type(16.):
        maglim = [maglim]

    #ADM set bands to uppercase if passed as lower case
    bands = bands.upper()
    #ADM the band as an integer location
    bandint = np.array([ "UGRIZY".find(band) for band in bands ])

    if len(bandint) != len(maglim):
        print('FATAL: bands has to be the same length as magint and {} does not equal {}'.format(len(bandint),len(maglim)))
        sys.exit(1)

    #ADM change input magnitude(s) to a flux to test against
    fluxlim = 10.**((22.5-np.array(maglim))/2.5)

    #ADM parallel formalism from this step forward is stolen from cuts.select_targets

    #ADM function to grab the bright stars from a given file
    def _get_bright_stars(filename):
        '''Retrieves bright stars from a sweeps/Tractor file'''
        objs = io.read_tractor(filename)
        #ADM Retain rows for which ANY band is brighter than maglim
        w = np.where(np.any(objs["DECAM_FLUX"][...,bandint] > fluxlim,axis=1))
        if len(w[0]) > 0:
            return objs[w]

    #ADM counter for how many files have been processed
    #ADM critical to use np.ones because a numpy scalar allows in place modifications
    # c.f https://www.python.org/dev/peps/pep-3104/
    totfiles = np.ones((),dtype='i8')*len(infiles)
    nfiles = np.ones((), dtype='i8')
    t0 = time()
    def _update_status(result):
        '''wrapper function for the critical reduction operation,
        that occurs on the main parallel process'''
        if verbose and nfiles%25 == 0:
            elapsed = time() - t0
{'fraction_non_alphanumeric': 0.1029470067397912, 'fraction_numerical': 0.011232985331042685, 'mean_word_length': 3.086393088552916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1524756', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2174, 'n_words': 849}