Esempio n. 1
0
def get_spectra(spectra_name, targetid):
    spectra = read_spectra(spectra_name)
    spectra = spectra.select(targets=[targetid])

    if 'brz' not in spectra.bands:
        spectra = coadd_cameras(spectra)

    return spectra.wave['brz'], spectra.flux['brz'].T, spectra.ivar['brz'].T
Esempio n. 2
0
def main(args=None):

    log = get_logger()

    if args is None:
        args = parse()

    if args.lin_step is not None and args.log10_step is not None:
        print(
            "cannot have both linear and logarthmic bins :-), choose either --lin-step or --log10-step"
        )
        return 12
    if args.coadd_cameras and (args.lin_step is not None
                               or args.log10_step is not None):
        print(
            "cannot specify a new wavelength binning along with --coadd-cameras option"
        )
        return 12

    log.info("reading spectra ...")
    spectra = read_spectra(args.infile)

    if args.coadd_cameras:
        log.info("coadding cameras ...")
        spectra = coadd_cameras(spectra, cosmics_nsig=args.nsig)
    else:
        log.info("coadding ...")
        coadd(spectra, cosmics_nsig=args.nsig)

    if args.lin_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              linear_step=args.lin_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)
    if args.log10_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              log10_step=args.log10_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)

    log.info("writing {} ...".format(args.outfile))
    write_spectra(args.outfile, spectra)

    log.info("done")
Esempio n. 3
0
def main(args=None):

    log = get_logger()

    if args is None:
        args = parse()

    if args.lin_step is not None and args.log10_step is not None:
        log.critical(
            "cannot have both linear and logarthmic bins :-), choose either --lin-step or --log10-step"
        )
        return 12
    if args.coadd_cameras and (args.lin_step is not None
                               or args.log10_step is not None):
        log.critical(
            "cannot specify a new wavelength binning along with --coadd-cameras option"
        )
        return 12

    if len(args.infile) == 0:
        log.critical("You must specify input files")
        return 12

    log.info("reading input ...")

    # inspect headers
    input_is_frames = False
    input_is_spectra = False
    for filename in args.infile:
        ifile = fitsio.FITS(filename)
        head = ifile[0].read_header()
        identified = False
        if "EXTNAME" in head and head["EXTNAME"] == "FLUX":
            print(filename, "is a frame")
            input_is_frames = True
            identified = True
            ifile.close()
            continue
        for hdu in ifile:
            head = hdu.read_header()
            if "EXTNAME" in head and head["EXTNAME"].find("_FLUX") >= 0:
                print(filename, "is a spectra")
                input_is_spectra = True
                identified = True
                break
        ifile.close()
        if not identified:
            log.error(
                "{} not identified as frame of spectra file".format(filename))
            sys.exit(1)

    if input_is_frames and input_is_spectra:
        log.error("cannot combine input spectra and frames")
        sys.exit(1)

    if input_is_spectra:
        spectra = read_spectra(args.infile[0])
        for filename in args.infile[1:]:
            log.info("append {}".format(filename))
            spectra.update(read_spectra(filename))
    else:  # frames
        frames = dict()
        cameras = {}
        for filename in args.infile:
            frame = read_frame(filename)
            night = frame.meta['NIGHT']
            expid = frame.meta['EXPID']
            camera = frame.meta['CAMERA']
            frames[(night, expid, camera)] = frame
            if args.coadd_cameras:
                cam, spec = camera[0], camera[1]
                # Keep a list of cameras (b,r,z) for each exposure + spec
                if (night, expid) not in cameras.keys():
                    cameras[(night, expid)] = {spec: [cam]}
                elif spec not in cameras[(night, expid)].keys():
                    cameras[(night, expid)][spec] = [cam]
                else:
                    cameras[(night, expid)][spec].append(cam)

        if args.coadd_cameras:
            # If not all 3 cameras are available, remove the incomplete sets
            for (night, expid), camdict in cameras.items():
                for spec, camlist in camdict.items():
                    log.info("Found {} for SP{} on NIGHT {} EXP {}".format(
                        camlist, spec, night, expid))
                    if len(camlist) != 3 or np.any(
                            np.sort(camlist) != np.array(['b', 'r', 'z'])):
                        for cam in camlist:
                            frames.pop((night, expid, cam + spec))
                            log.warning(
                                "Removing {}{} from Night {} EXP {}".format(
                                    cam, spec, night, expid))
        #import pdb
        #pdb.set_trace()
        spectra = frames2spectra(frames)

        #- hacks to make SpectraLite like a Spectra
        spectra.fibermap = Table(spectra.fibermap)

        del frames  #- maybe free some memory

    if args.coadd_cameras:
        log.info("coadding cameras ...")
        spectra = coadd_cameras(spectra, cosmics_nsig=args.nsig)
    else:
        log.info("coadding ...")
        coadd(spectra, cosmics_nsig=args.nsig)

    if args.lin_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              linear_step=args.lin_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)
    if args.log10_step is not None:
        log.info("resampling ...")
        spectra = resample_spectra_lin_or_log(spectra,
                                              log10_step=args.log10_step,
                                              wave_min=args.wave_min,
                                              wave_max=args.wave_max,
                                              fast=args.fast,
                                              nproc=args.nproc)

    #- Add input files to header
    if spectra.meta is None:
        spectra.meta = dict()

    for i, filename in enumerate(args.infile):
        spectra.meta['INFIL{:03d}'.format(i)] = os.path.basename(filename)

    log.info("writing {} ...".format(args.outfile))
    write_spectra(args.outfile, spectra)

    log.info("done")
Esempio n. 4
0
                allfmap = None
                allwave = None
                allflux = None
                allivar = None
                allmask = None
                allres = None

                print('Tile: {} - {}'.format(tile_number, obsdate))

                for cafile, zbfile in match_files(cafiles, zbfiles):
                    print('  - Petal {}'.format(get_petal_id(cafile)))

                    # Access data per petal.
                    zbest = Table.read(zbfile, 'ZBEST')
                    pspectra = read_spectra(cafile)
                    cspectra = coadd_cameras(pspectra)
                    fibermap = cspectra.fibermap

                    # Apply standard event selection.
                    isTGT = fibermap['OBJTYPE'] == 'TGT'
                    isGAL = zbest['SPECTYPE'] == 'GALAXY'
                    isBGS = fibermap['SV1_BGS_TARGET'] & bgs_mask.mask(
                        sv1_bgs_bits) != 0
                    isGoodFiber = fibermap['FIBERSTATUS'] == 0
                    isGoodZbest = (zbest['DELTACHI2'] > 25.) & (zbest['ZWARN']
                                                                == 0)
                    select = isTGT & isGAL & isBGS & isGoodFiber & isGoodZbest

                    print('     + selected: {}'.format(np.sum(select)))

                    # Accumulate spectrum data.
def get_spectra(spectra_name,
                zbest_name,
                lambda_width,
                index_to_fit,
                template_dir=None,
                archetypes_dir=None):
    """
    Get the spectra and the best fit model from a given spectra and zbest file.

    Args:
        spectra_name (str): The name of the spectra file.
        zbest_name (str): The name of the zbest file associated to the spectra
                          file.
        lambda_width (float): The width in wavelength (in Angstrom) considered
                              for the fitting arount the MgII peak
        index_to_fit (boolean numpy array): boolean array of size 500 specifing
                                            which spectra have to be used
        add_linear_term (boolean): Add a linear term to the Gaussian peak term
                                   to fit the continuum.
        template_dir (str): If the redrock template variable is not loaded by
                            the desi environment, specify the template path
        archetypes_dir (str): If not None, use the archetypes templates in the
                              path specified

    Returns:
        target_id (numpy array): Array containing target id of the the object
                                 to fit
        redshift_redrock (numpy array): Array containing the redshift of the the
                                        object to fit
        flux (numpy array): Array containing the full flux arrays of every
                            object to fit
        ivar_flux (numpy array): Array containing the inverse variance arrays
                                 of every object to fit
        model_flux (numpy array): Array containing the best fit redrock model
                                  for every object to fit
        wavelength (numpy array): Array containing the wavelength
        index_with_fit (boolean numpy array): boolean array of index_to_fit size
                                              masking index where mgII fitter is
                                              not apply
    """
    spectra = read_spectra(spectra_name)
    spectra = spectra.select(
        targets=spectra.fibermap["TARGETID"][index_to_fit])

    if 'brz' not in spectra.bands:
        spectra = coadd_cameras(spectra)

    zbest = Table.read(zbest_name, 'ZBEST')[index_to_fit]
    if archetypes_dir is not None:
        model_wave, model_flux = create_model(spectra,
                                              zbest,
                                              archetype_fit=True,
                                              archetypes_dir=archetypes_dir,
                                              template_dir=template_dir)
    else:
        model_wave, model_flux = create_model(spectra,
                                              zbest,
                                              archetype_fit=False,
                                              archetypes_dir=None,
                                              template_dir=template_dir)

    redshift_redrock = zbest["Z"]
    wavelength = spectra.wave['brz']

    mgii_peak_1, mgii_peak_2 = 2803.5324, 2796.3511
    mean_mgii_peak = (mgii_peak_1 + mgii_peak_2) / 2
    non_visible_peak = (redshift_redrock + 1) * mean_mgii_peak < np.min(
        wavelength) + lambda_width / 2
    non_visible_peak |= (redshift_redrock + 1) * mean_mgii_peak > np.max(
        wavelength) - lambda_width / 2

    index_with_fit = ~non_visible_peak

    target_id = spectra.fibermap["TARGETID"][index_with_fit]
    redshift_redrock = redshift_redrock[index_with_fit]
    flux = spectra.flux['brz'][index_with_fit]
    ivar_flux = spectra.ivar['brz'][index_with_fit]
    model_flux = model_flux[index_with_fit]
    return target_id, redshift_redrock, flux, ivar_flux, model_flux, wavelength, index_with_fit
Esempio n. 6
0
def get_spectra(spectra_name,
                zbest_name,
                lambda_width,
                qso_target=True,
                template_dir=None,
                archetypes_dir=None):

    spectra = read_spectra(spectra_name)
    if 'brz' not in spectra.bands:
        spectra = coadd_cameras(spectra)
    zbest = Table.read(zbest_name, 'ZBEST')
    if archetypes_dir is not None:
        model_wave, model_flux = create_model(spectra,
                                              zbest,
                                              archetype_fit=True,
                                              archetypes_dir=archetypes_dir,
                                              template_dir=template_dir)
    else:
        model_wave, model_flux = create_model(spectra,
                                              zbest,
                                              archetype_fit=False,
                                              archetypes_dir=None,
                                              template_dir=template_dir)

    fiber_status = spectra.fibermap["FIBERSTATUS"]
    target_id = spectra.fibermap["TARGETID"]
    sv1_desi_target = spectra.fibermap["SV1_DESI_TARGET"]
    wavelength = spectra.wave['brz']
    flux = spectra.flux['brz']
    ivar_flux = spectra.ivar['brz']

    redshift_redrock = zbest["Z"]
    spec_type = zbest["SPECTYPE"]

    fiber_ok = fiber_status == 0
    redrock_galaxies = (spec_type == "GALAXY")

    mgii_peak_1 = 2803.5324
    mgii_peak_2 = 2796.3511
    mean_mgii_peak = (mgii_peak_1 + mgii_peak_2) / 2
    non_visible_peak = (redshift_redrock + 1) * mean_mgii_peak < np.min(
        wavelength) + lambda_width / 2
    non_visible_peak |= (redshift_redrock + 1) * mean_mgii_peak > np.max(
        wavelength) - lambda_width / 2

    galaxies_to_test = redrock_galaxies & fiber_ok & (~non_visible_peak)

    if (qso_target):
        galaxies_to_test &= ((sv1_desi_target & 4) != 0)

    print("nb galaxies to test: ",
          len(galaxies_to_test[galaxies_to_test == True]))

    target_id_to_test = target_id[galaxies_to_test]
    redshift_redrock_to_test = redshift_redrock[galaxies_to_test]
    flux_to_test = flux[galaxies_to_test]
    ivar_flux_to_test = ivar_flux[galaxies_to_test]
    model_flux_to_test = model_flux[galaxies_to_test]

    print("Flux and RR best fit obtained")

    return (target_id_to_test, redshift_redrock_to_test, flux_to_test,
            ivar_flux_to_test, model_flux_to_test, wavelength)
Esempio n. 7
0
def desibalfinder(specfilename, altbaldir=None, altzdir=None, zfileroot='zbest', overwrite=True, verbose=False, release=None, format='healpix'): 
    '''
    Find BALs in DESI quasars
    1. Identify all objects classified as quasars that are in the redshift
       range to identify BAL features 
    2. Create an output BAL catalog and populate it with basic QSO data
    3. Run the BAL finder on each quasar and add results to the table

    Parameters
    ----------
    specfilename : filename
        FITS file with DESI spectra
    altbaldir : string, optional
        alternate output directory for catalog (default is None)
    altzdir : string, optional
        alternate directory to find redshift catalog (default in None, which will look in the coadd directory) 
    zfileroot : string, optional 
        root name for redshift catalog 
    overwrite : bool, optional
        Overwrite the BAL catalog if it exists? (default is True)
    verbose : bool, optional
        Provide verbose output? (default is False)
    release : string, optional
        Specifies the data release (default if None)
    format : string, optional 
        Specifies either healpix or tile based (default is healpix)

    Returns
    -------
    none
    ''' 

    if release is None:
        release = 'everest'
   
    if zfileroot is None: 
        if release == 'everest': 
            zfileroot = 'redrock' 
        else: 
            zfileroot = 'zbest' 

    # Define some variable names based on the type of input file
    if 'spectra-' in specfilename:
        specshort = specfilename[specfilename.rfind('spectra-'):specfilename.rfind('.fits')]
        zshort = specshort.replace('spectra', zfileroot)
    elif 'coadd-' in specfilename:
        specshort = specfilename[specfilename.rfind('coadd-'):specfilename.rfind('.fits')]
        zshort = specshort.replace('coadd', zfileroot)
    else:
        print("Error in desibalfinder(): unable to parse {}".format(specfilename))
        return

    zfilename = specfilename.replace(specshort, zshort)

    if altzdir is not None: 
        zfilename = os.path.join(altzdir, zshort+".fits") 

    # Define output file name and check if it exists if overwrite=False
    if 'spectra-' in specfilename:
        balshort = specshort.replace('spectra-', 'baltable-')
    elif 'coadd-' in specfilename:
        balshort = specshort.replace('coadd-', 'baltable-')
    else:
        print("Error in desibalfinder(): unable to interpret {}".format(specfilename))
        return

    baltmp = specfilename.replace(specshort, balshort)

    if altbaldir is not None:
        balfilename = os.path.join(altbaldir + "/", baltmp[baltmp.rfind("baltable-")::])
    else:
        balfilename = baltmp

    print("Output BAL catalog:", balfilename)

    if os.path.isfile(balfilename) and not overwrite:
        print("desibal(): Warning {0} exists and overwrite = False, so not re-running balfinder".format(balfilename))
        return

    # Read in the DESI spectra
    specobj = desispec.io.read_spectra(specfilename)
    # See if 3 cameras are coadded, and coadd them if they are not:
    # (at least some of the mock data are not coadded)
    if 'brz' not in specobj.wave.keys():
        try:
            specobj = coadd_cameras(specobj, cosmics_nsig=None)
        except:
            wave_min = np.min(specobj.wave['b'])
            wave_max = np.max(specobj.wave['z'])
            specobj = resample_spectra_lin_or_log(specobj,linear_step=0.8, wave_min =wave_min, wave_max =wave_max, fast = True)
            specobj = coadd_cameras(specobj, cosmics_nsig=None)
            print("coadded_cameras using lispace resample")
            
    zs = fitsio.read(zfilename)

    print("Read file {}".format(zfilename))

    # Identify the spectra classified as quasars based on zs and within 
    # the nominal redshift range for BALs
    # BAL_ZMIN = 1.57
    # BAL_ZMAX = 5.0
    zmask = zs['Z'] >= bc.BAL_ZMIN
    zmask = zmask*(zs['Z'] <= bc.BAL_ZMAX)
    
    if 'QSO' in np.unique(zs['SPECTYPE']) :
        zmask = zmask*(zs['SPECTYPE'] == 'QSO')
    else :
        zmask = zmask*(zs['SPECTYPE'] == 'bQSO')

    zqsos = []
    dd = defaultdict(list)
    # Create a dictionary with the index of each TARGETID in zs in 
    # the BAL redshift range. 
    # Create an array of indices in zs of those quasars 
    for index, item in enumerate(zs['TARGETID']):
        if zmask[index]: 
            dd[item].append(index)
            zqsos.append(index)

    if verbose: 
      print("Found {} quasars".format(np.sum(zmask)))

    fm = specobj.fibermap
    # Create a list of the indices in specobj on which to run balfinder
    # Note afterburners catalogs have different lengths from redrock catalogs
    if altzdir is None: 
        qsos = [index for item in fm["TARGETID"] for index in dd[item] if item in dd]
    else: 
        qsos = []
        for zindx in zqsos:
            tid = zs['TARGETID'][zindx]
            qindx = np.where(tid == fm['TARGETID'])[0][0]
            qsos.append(qindx)

    # Initialize the BAL table with all quasars in 'qsos'
    baltable.initbaltab_desi(fm[qsos], zs[zqsos], balfilename, overwrite=overwrite, release=release)

    balhdu = fits.open(balfilename) 

    # Read in the eigenspectra 
    pcaeigen = fitsio.read(bc.pcaeigenfile)

    # Loop through the QSOs and run the BAL finder
    for i in range(len(qsos)): 
        qso = qsos[i]
        zspec = zs['Z'][dd[fm['TARGETID'][qso]]][0]
        qsospec = np.zeros(len(specobj.wave['brz']),dtype={'names':('wave', 'flux', 'ivar', 'model'), 'formats':('>f8', '>f8', '>f8', '>f8')})
        qsospec['wave'] = specobj.wave['brz']
        qsospec['flux'] = specobj.flux['brz'][qso]
        qsospec['ivar'] = specobj.ivar['brz'][qso]
        qsospec['model'] = np.zeros(len(specobj.wave['brz'])) # add to match SDSS format
        targetid = fm['TARGETID'][qso]
        info, pcaout, mask = fitbal.calcbalparams(qsospec, pcaeigen, zspec)
        # update baltable
        balhdu = baltable.updatebaltab_desi(targetid, balhdu, info, pcaout)
        if verbose: 
            print("{0} Processed {1} at z = {2:.2f}: AI_CIV = {3:.0f}, BI_CIV = {4:.0f}".format(i, targetid, zspec, info['AI_CIV'], info['BI_CIV']))


    balhdu.writeto(balfilename, overwrite=True)

    if verbose: 
        print("Wrote BAL catalog {0}".format(balfilename))

    lastupdate = "Last updated {0} UT by {1}".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()), getpass.getuser())
    fits.setval(balfilename, 'HISTORY', value=lastupdate, ext=1)
Esempio n. 8
0
                                 'NIGHT', 'EXPID', 'MJD', 'TILEID'),
                                'formats': ('>i8', '>f8', '>f8', '>i8', '>i8',
                                            '>f8', '>i8')
                            })
        specdata['TARGETID'] = qhdu[1].data['TARGETID'][specmask]
        specdata['TARGET_RA'] = qhdu[1].data['TARGET_RA'][specmask]
        specdata['TARGET_DEC'] = qhdu[1].data['TARGET_DEC'][specmask]
        specdata['NIGHT'] = qhdu[1].data['NIGHT'][specmask]
        specdata['EXPID'] = qhdu[1].data['EXPID'][specmask]
        specdata['MJD'] = qhdu[1].data['MJD'][specmask]
        specdata['TILEID'] = qhdu[1].data['TILEID'][specmask]

        specobj = desispec.io.read_spectra(specfile)
        print("Coadding ", specfile)
        if 'brz' not in specobj.wave.keys():
            specobj = coadd_cameras(specobj, cosmics_nsig=None)
        fm = specobj.fibermap
        # Identify these QSOs in the coadd data
        qsos = np.zeros(len(specdata), dtype=int)
        for i in range(len(specdata)):
            qsos[i] = np.where(targetids[i] == fm['TARGETID'])[0][0]
        balfilename = 'baltmp.fits'
        baltable.initbaltab_desi(specdata, zdata, balfilename, overwrite=True)
        print("Initialized temporary BAL catalog ", balfilename, " with ",
              len(specdata), "entries")
        balhdu = fits.open(balfilename)

        # Loop through the QSOs and run the BAL finder
        for i in range(len(qsos)):
            targetid = targetids[i]
            qso = qsos[i]  # index in specobj for QSO targetid
Esempio n. 9
0
def main(args=None):

    log = get_logger()

    if isinstance(args, (list, tuple, type(None))):
        args = parse(args)

    thedate = datetime.now().strftime('%Y-%m-%d')

    # Generate transient model if one is specified.
    trans_model = None
    if args.transient is not None:
        trans_model = transients.get_model(args.transient)

    # Generate list of HEALPix pixels to randomly sample the mocks.
    rng = np.random.RandomState(args.seed)
    nside = args.nside
    healpixels = _get_healpixels_in_footprint(nside=args.nside)
    npix = np.minimum(10 * args.nsim, len(healpixels))
    pixels = rng.choice(healpixels, size=npix, replace=False)
    ipix = iter(pixels)

    # Set up the template generator.
    fluxratio_range = 10**(-np.sort(args.magrange)[::-1] / 2.5)
    epoch_range = np.sort(args.epochrange)

    if args.host == 'bgs':
        maker = BGSMaker(seed=args.seed)
        tmpl_maker = BGS
    elif args.host == 'elg':
        maker = ELGMaker(seed=args.seed)
        tmpl_maker = ELG
    elif args.host == 'lrg':
        maker = LRGMaker(seed=args.seed)
        tmpl_maker = LRG
    else:
        raise ValueError('Unusable host type {}'.format(args.host))

    maker.template_maker = tmpl_maker(transient=trans_model,
                                      tr_fluxratio=fluxratio_range,
                                      tr_epoch=epoch_range)

    for j in range(args.nsim):
        # Loop until finding a non-empty healpixel with mock galaxies.
        tdata = []
        while len(tdata) == 0:
            pixel = next(ipix)
            tdata = maker.read(healpixels=pixel, nside=args.nside)

        # Generate spectral templates and write them to truth files.
        # Keep producing templates until we have enough to pass brightness cuts.
        wave = None
        flux, targ, truth, objtr = [], [], [], []

        ntosim = np.min([args.nspec, len(tdata['RA'])])
        ngood = 0

        while ngood < args.nspec:
            idx = rng.choice(len(tdata['RA']), ntosim)
            tflux, twave, ttarg, ttruth, tobj = maker.make_spectra(tdata,
                                                                   indx=idx)
            g, r, z, w1, w2 = [
                ttruth['FLUX_{}'.format(_)]
                for _ in ['G', 'R', 'Z', 'W1', 'W2']
            ]
            rfib = ttarg['FIBERFLUX_R']
            #            print(g, r, z, w1, w2, rfib)

            # Apply color cuts.
            is_bright = isBGS_colors(rfib, g, r, z, w1, w2, targtype='bright')
            is_faint = isBGS_colors(rfib, g, r, z, w1, w2, targtype='faint')
            is_wise = isBGS_colors(rfib, g, r, z, w1, w2, targtype='wise')

            keep = np.logical_or.reduce([is_bright, is_faint, is_wise])

            _ngood = np.count_nonzero(keep)
            if _ngood > 0:
                ngood += _ngood
                flux.append(tflux[keep, :])
                targ.append(ttarg[keep])
                truth.append(ttruth[keep])
                objtr.append(tobj[keep])

        wave = maker.wave
        flux = np.vstack(flux)[:args.nspec, :]
        targ = vstack(targ)[:args.nspec]
        truth = vstack(truth)[:args.nspec]
        objtr = vstack(objtr)[:args.nspec]

        # Set up and verify the TARGETID across all truth tables.
        n = len(truth)
        new_id = 10000 * pixel + 100 * j + np.arange(1, n + 1)
        targ['OBJID'][:] = new_id
        truth['TARGETID'][:] = new_id
        objtr['TARGETID'][:] = new_id

        assert (len(truth) == args.nspec)
        assert (np.all(targ['OBJID'] == truth['TARGETID']))
        assert (len(targ) == len(np.unique(targ['OBJID'])))
        assert (len(truth) == len(np.unique(truth['TARGETID'])))
        assert (len(objtr) == len(np.unique(objtr['TARGETID'])))

        truthfile = os.path.join(
            args.outdir,
            '{}_{}_{:04d}s_{:03d}_truth.fits'.format(args.host, thedate,
                                                     int(args.exptime), j + 1))
        write_templates(truthfile, flux, wave, targ, truth, objtr)
        log.info('Wrote {}'.format(truthfile))

        # Get observing conditions and generate spectra.
        obs = dict(AIRMASS=args.airmass,
                   EXPTIME=args.exptime,
                   MOONALT=args.moonalt,
                   MOONFRAC=args.moonfrac,
                   MOONSEP=args.moonsep,
                   SEEING=args.seeing)

        fcols = dict(BRICKID=targ['BRICKID'],
                     BRICK_OBJID=targ['OBJID'],
                     FLUX_G=targ['FLUX_G'],
                     FLUX_R=targ['FLUX_R'],
                     FLUX_Z=targ['FLUX_Z'],
                     FLUX_W1=targ['FLUX_W1'],
                     FLUX_W2=targ['FLUX_W2'],
                     FLUX_IVAR_G=targ['FLUX_IVAR_G'],
                     FLUX_IVAR_R=targ['FLUX_IVAR_R'],
                     FLUX_IVAR_Z=targ['FLUX_IVAR_Z'],
                     FLUX_IVAR_W1=targ['FLUX_IVAR_W1'],
                     FLUX_IVAR_W2=targ['FLUX_IVAR_W2'],
                     FIBERFLUX_G=targ['FIBERFLUX_G'],
                     FIBERFLUX_R=targ['FIBERFLUX_R'],
                     FIBERFLUX_Z=targ['FIBERFLUX_Z'],
                     FIBERTOTFLUX_G=targ['FIBERTOTFLUX_G'],
                     FIBERTOTFLUX_R=targ['FIBERTOTFLUX_R'],
                     FIBERTOTFLUX_Z=targ['FIBERTOTFLUX_Z'],
                     MW_TRANSMISSION_G=targ['MW_TRANSMISSION_G'],
                     MW_TRANSMISSION_R=targ['MW_TRANSMISSION_R'],
                     MW_TRANSMISSION_Z=targ['MW_TRANSMISSION_Z'],
                     EBV=targ['EBV'])

        specfile = os.path.join(
            args.outdir,
            '{}_{}_{:04d}s_{:03d}_spect.fits'.format(args.host, thedate,
                                                     int(args.exptime), j + 1))

        # redshifts = truth['TRUEZ'] if args.host=='bgs' else None
        redshifts = None

        sim_spectra(wave,
                    flux,
                    args.host,
                    specfile,
                    sourcetype=args.host,
                    obsconditions=obs,
                    meta=obs,
                    fibermap_columns=fcols,
                    targetid=truth['TARGETID'],
                    redshift=redshifts,
                    ra=targ['RA'],
                    dec=targ['DEC'],
                    seed=args.seed,
                    expid=j)

        if args.coadd_cameras:
            coaddfile = specfile.replace('spect', 'coadd')
            spectra = read_spectra(specfile)
            spectra = coadd_cameras(spectra)

            write_spectra(coaddfile, spectra)
            log.info('Wrote {}'.format(coaddfile))
Esempio n. 10
0
    def putPhotometry(target_id,
                      spectra_in,
                      data_override=None,
                      coadd_camera=False):
        spectra = spectra_in.select(targets=[int(target_id)])

        # At this point there should only be one Spectrum
        # This should be the case of everything derived from "coadd" or from
        # SpectraPairsIterator
        if spectra.num_spectra() > 1:
            raise IndexError

        # The bands of interest
        bands = speclite.filters.load_filters('BASS-g', 'BASS-r', 'MzLS-z')

        # coadd_cameras does not have an MJD.  Rely on getting this from the original.
        fibermap = spectra.fibermap

        if 'MJD' in fibermap.keys():
            mjd = fibermap['MJD'][0]
        elif 'LAST_MJD' in fibermap.keys():
            mjd = fibermap['LAST_MJD'][0]
        else:
            log.error('we are screwed')

        # combines the arms into one spectrum
        if coadd_camera:
            spectra = coadd_cameras(spectra)
            if 'brz' not in spectra.bands:
                log.warning(
                    'postPhotometry failed because brz not available but carrying on'
                )
                return

        objID = 'DESI{}'.format(target_id)

        # this for statement should be superfluous as there is only one index
        #         for index, mjd in enumerate(fibermap.iterrows('MJD')):
        index = 0
        fluxes = []
        fluxerrs = []
        for b in bands:
            ok = numpy.logical_and(spectra.mask['brz'][index, :] == 0,
                                   spectra.ivar[spectra.bands[0]][index, :])
            padspec = b.pad_spectrum(spectra.flux[spectra.bands[0]][index, ok],
                                     spectra.wave[spectra.bands[0]][ok])
            fluxes.append(b.get_ab_maggies(padspec[0], padspec[1]) * 1e-17)
            padspec = b.pad_spectrum(spectra.ivar[spectra.bands[0]][index, ok],
                                     spectra.wave[spectra.bands[0]][ok])
            fluxerrs.append(
                numpy.sqrt(b.get_ab_maggies(1 / padspec[0], padspec[1])) *
                1e-17)

        filters = ['sdssg', 'sdssr', 'sdssz']

        w = numpy.where(numpy.isfinite(fluxes))[0]

        if len(w) != 0:

            data = {
                "mjd": str(mjd),
                "obj_id": objID,
                "instrument_id": SkyPortal.instrument_id(),
                "origin": "DESI",
                "magsys": 'ab',
                "filter": filters[w[0]],
                "group_ids": [SkyPortal.group_id('DESI')],
                "flux": fluxes[w[0]],
                "fluxerr": fluxerrs[w[0]],
                "zp": 0
            }

            if data_override is not None:
                for k, v in data_override.items():
                    if isinstance(v, dict) and k in data:
                        data[k].update(v)
                        data_override[k] = data[k]
                data.update(data_override)

            try:
                response = SkyPortal.api('PUT',
                                         '{}/api/photometry'.format(
                                             SkyPortal.url),
                                         data=data)
            except ResponseError as err:
                raise
Esempio n. 11
0
    def postSpectra(target_id,
                    spectra_in,
                    data_override=None,
                    coadd_camera=False):

        # fix to an upstream bug in spectra
        keep = numpy.equal(spectra_in.fibermap['TARGETID'].data,
                           int(target_id))
        spectra = spectra_in[keep]

        # At this point there should only be one Spectrum
        # This should be the case of everything derived from "coadd" or from
        # SpectraPairsIterator

        if spectra.num_spectra() != 1:
            log.error(target_id,
                      int(target_id) in spectra_in.fibermap['TARGETID'].data)
            log.error(spectra.num_spectra())
            raise IndexError

#         spectra = spectra_in.select(targets=[int(target_id)])

# coadd_cameras does not have an MJD.  Rely on getting this from the original.
        fibermap = spectra.fibermap

        if 'MJD' in fibermap.keys():
            mjd = fibermap['MJD'][0]
        elif 'LAST_MJD' in fibermap.keys():
            mjd = fibermap['LAST_MJD'][0]
        else:
            log.error('we are screwed')
        t = Time(mjd, format='mjd')

        # combines the arms into one spectrum
        if coadd_camera:
            spectra = coadd_cameras(spectra)

        objID = 'DESI{}'.format(target_id)

        # this for statement should be superfluous as there is only one index
        #         for index, mjd in enumerate(fibermap.iterrows('MJD')):
        index = 0

        for band in spectra.bands:
            data = {
                "obj_id": objID,
                "group_ids": [SkyPortal.group_id('DESI')],
                "fluxes": spectra.flux[band][index, :].tolist(),
                #                   "errors": (1./np.sqrt(spectra.ivar[band][index,:])).tolist(),
                "wavelengths": spectra.wave[band].tolist(),
                "observed_at": t.isot,
                "instrument_id": SkyPortal.instrument_id()
            }

            if data_override is not None:
                for k, v in data_override.items():
                    if isinstance(v, dict) and k in data:
                        data[k].update(v)
                        data_override[k] = data[k]

                data.update(data_override)

            try:
                response = SkyPortal.api('POST',
                                         '{}/api/spectrum'.format(
                                             SkyPortal.url),
                                         data=data)
            except:
                raise