Exemplo n.º 1
0
def parse_deimos_mask_file(msk_file):
    '''Parse the standard DEIMOS mask file
    '''
    # Read
    f = open(msk_file, 'r')
    lines = f.readlines()
    f.close()
    #
    flg_mask = 0
    flg_select = 1
    mask_dict = dict(INSTR='DEIMOS')
    all_targ = []
    all_obs = []
    for kk, line in enumerate(lines):
        # Searching for mask name, center
        if flg_mask == 0:
            if 'Mask' in line:
                flg_mask = 1
                continue
        # Mask info
        if flg_mask == 1:
            prs = line.strip().split(' ')
            #
            cnt = 0
            for jj, iprs in enumerate(prs):
                if len(iprs) == 0:
                    continue
                if cnt == 0:
                    mask_dict['MASK_NAME'] = iprs
                elif cnt == 1:
                    mask_dict['MASK_RA'] = iprs
                elif cnt == 2:
                    mask_dict['MASK_DEC'] = iprs
                elif cnt == 3:
                    mask_dict['MASK_EPOCH'] = float(iprs)
                elif cnt == 4:
                    mask_dict['MASK_PA'] = float(iprs[3:])
                cnt += 1
            flg_mask = 2
            continue
        # Observation info
        if 'OBS' in line:
            prs = line.strip().split(' ')
            gdprs = [iprs for iprs in prs if len(iprs) > 0]
            obs_dict = {}
            obs_dict['DATE'] = gdprs[2]
            obs_dict['TEXP'] = float(gdprs[3])
            obs_dict['DISPERSER'] = gdprs[4]
            obs_dict['CONDITIONS'] = gdprs[5]
            #
            all_obs.append(obs_dict)

        # Done
        if 'Non' in line:
            break
        # Dummy line?
        if (len(line.strip()) == 0) or (line[0] == '#'):
            continue
        # Selected object
        prs = line.strip().split(' ')
        gdprs = [iprs for iprs in prs if len(iprs) > 0]
        if int(gdprs[6]) < 0:  # QSO or Star
            continue
        targ_dict = {}
        targ_dict['ID'] = int(gdprs[0])
        targ_dict['RAS'] = gdprs[1]
        targ_dict['DECS'] = gdprs[2]
        targ_dict['EPOCH'] = float(gdprs[3])
        # Add to list
        all_targ.append(targ_dict)

    # Generate Tables
    # Obs
    if len(all_obs) > 0:
        obs_tab = xxul.dict_list_to_table(all_obs)
        obs_tab['TEXP'].unit = u.s
    else:
        obs_tab = None
    # Targ
    targ_tab = xxul.dict_list_to_table(all_targ)

    # Return
    return mask_dict, targ_tab, obs_tab
Exemplo n.º 2
0
def mmt_targets(field, path=None):
    '''Read files related to MMT targets

    Parameters:
    -----------
    field: tuple
      (Name, ra, dec)

    Returns:
    ----------
    Target and observing info 
    '''
    if path is None:
        path = '/Galx_Spectra/MMT/'

    # Targets
    targ_path = field[0] + path

    # Target file
    targ_file = glob.glob(targ_path + '*.targ')
    if len(targ_file) != 1:
        raise ValueError('Wrong number of MMT target files')
    else:
        targ_file = targ_file[0]

    # Read PI, program info [NOT IMPLEMENTED]
    #f = open(msk_file, 'r')
    #lines = f.readlines()
    #f.close()

    # Read target table
    tab = ascii.read(targ_file, comment='#')
    # Restrict to targets
    itarg = np.where(tab['type'] == 'TARGET')
    targs = tab[itarg]
    # Polish
    nrow = len(targs)
    targs.rename_column('ra', 'RAS')
    targs.rename_column('dec', 'DECS')
    targs.add_column(Column([0.] * nrow, name='TARG_RA'))
    targs.add_column(Column([0.] * nrow, name='TARG_DEC'))
    # Get RA/DEC in degrees
    for k, row in enumerate(targs):
        rad, decd = xra.stod1((row['RAS'], row['DECS']))
        targs[k]['TARG_RA'] = rad.value
        targs[k]['TARG_DEC'] = decd.value
    targs.rename_column('objid', 'TARG_ID')
    targs.rename_column('mag', 'TARG_MAG')
    targs.add_column(Column([0.] * nrow, name='EPOCH'))
    targs.add_column(Column(['SDSS'] * nrow, name='TARG_IMG'))
    targs.add_column(Column(['HECTOSPEC'] * nrow, name='INSTR'))

    targ_mask = {}
    cnames = ['MASK_NAME', 'MASK_ID']
    smsk = '--'
    msk_val = [smsk] * len(cnames)
    for kk, cname in enumerate(cnames):
        targ_mask[cname] = [msk_val[kk]] * nrow

    # Now the 'mask' files
    mask_files = glob.glob(targ_path + '*.cat')
    all_obs = []
    all_masks = []
    for mask_file in mask_files:
        print('Reading MMT mask file: {:s}'.format(mask_file))
        i0 = mask_file.rfind('/')
        mask_nm = mask_file[i0 + 1:mask_file.find('.cat')]
        # Grab info from spectrum file
        #xdb.set_trace()
        spec_fil = glob.glob(mask_file[:i0 + 1] + 'spHect-' + mask_nm +
                             '*.fits.gz')
        if len(spec_fil) != 1:
            print('spec_fil -- Not found!'.format(spec_fil))
            ras, decs = xra.dtos1((field[1], field[2]))
            pa = 0.
        else:
            header = fits.open(spec_fil[0])[0].header
            if header['APERTURE'] != mask_nm:
                raise ValueError('Mask doesnt match!')
            pa = header['POSANGLE']
            ras = header['CAT-RA']
            decs = header['CAT-DEC']
        # Continuing
        mask_dict = dict(
            INSTR='HECTOSPEC',
            MASK_NAME=mask_nm,
            MASK_RA=ras,
            MASK_DEC=decs,
            MASK_EPOCH=2000.,
            MASK_PA=pa)  # SHOULD GRAB PA, RA, DEC FROM SPECTRA FITS HEADER
        all_masks.append(mask_dict)
        # Read obs
        f = open(mask_file, 'r')
        lines = f.readlines()
        f.close()
        iall_obs = []
        for line in lines:
            if 'OBS' in line:
                prs = line.strip().split(' ')
                gdprs = [iprs for iprs in prs if len(iprs) > 0]
                obs_dict = {}
                obs_dict['DATE'] = gdprs[2]
                obs_dict['TEXP'] = float(gdprs[3])
                obs_dict['DISPERSER'] = gdprs[4]
                obs_dict['CONDITIONS'] = gdprs[5]
                #
                iall_obs.append(obs_dict)
        obs_tab = xxul.dict_list_to_table(iall_obs)
        obs_tab['TEXP'].unit = u.s
        # Read observed targets
        obs_targ = ascii.read(mask_file, comment='#')
        gdt = np.where(obs_targ['flag'] == 1)[0]
        for gdi in gdt:
            mtt = np.where(targs['TARG_ID'] == int(obs_targ['objid'][gdi]))[0]
            if len(mtt) != 1:
                raise ValueError('Multiple matches?!')
            targ_mask['MASK_NAME'][mtt[0]] = mask_nm
        all_obs.append(obs_tab)
    # Finish
    mask = np.array([False] * len(targs))
    bad = np.where(np.array(targ_mask['names']) == '--')[0]
    if len(bad) > 0:
        mask[bad] = True
    #
    clm = MaskedColumn(targ_mask['names'], name='MASK_NAME', mask=mask)
    targs.add_column(clm)

    return all_masks, all_obs, targs
Exemplo n.º 3
0
def hecto_targets(field, obs_path, hecto_path=None):
    '''Read files related to Hectospec targets

    Parameters:
    -----------
    field : tuple
      (Name, ra, dec)
    obs_path : str, optional
      Path to the observing tree
    hecto_path : str, optional
      Path within the file tree to Hectospec data

    Returns:
    ----------
    Target and observing info 
    '''
    if hecto_path is None:
        hecto_path = '/Galx_Spectra/Hectospec/'

    # Targets
    targ_path = obs_path + field[0] + hecto_path

    # Target file
    targ_file = glob.glob(targ_path + '*.targ')
    if len(targ_file) != 1:
        raise ValueError('Wrong number of Hectospec target files')
    else:
        targ_file = targ_file[0]

    # Read PI, program info [NOT IMPLEMENTED]
    #f = open(msk_file, 'r')
    #lines = f.readlines()
    #f.close()

    # Read target table
    tab = ascii.read(targ_file, comment='#')
    # Restrict to targets
    itarg = np.where(tab['type'] == 'TARGET')
    targs = tab[itarg]
    # Polish
    nrow = len(targs)
    targs.rename_column('ra', 'RAS')
    targs.rename_column('dec', 'DECS')
    targs.add_column(Column([0.] * nrow, name='TARG_RA'))
    targs.add_column(Column([0.] * nrow, name='TARG_DEC'))
    # Get RA/DEC in degrees
    for k, row in enumerate(targs):
        coord = ltu.radec_to_coord((row['RAS'], row['DECS']))
        targs[k]['TARG_RA'] = coord.ra.value
        targs[k]['TARG_DEC'] = coord.dec.value
    # ID/Mag (not always present)
    targ_coord = SkyCoord(ra=targs['TARG_RA'] * u.deg,
                          dec=targs['TARG_DEC'] * u.deg)
    try:
        targs.rename_column('objid', 'TARG_ID')
    except KeyError:
        targs.add_column(Column([0] * nrow, name='TARG_ID'))
        targs.add_column(Column([0.] * nrow, name='TARG_MAG'))
        flg_id = 0
    else:
        flg_id = 1
        targs.rename_column('mag', 'TARG_MAG')
    targs.add_column(Column([0.] * nrow, name='EPOCH'))
    targs.add_column(Column(['SDSS'] * nrow, name='TARG_IMG'))
    targs.add_column(Column(['HECTOSPEC'] * nrow, name='INSTR'))

    targ_mask = {}
    cnames = ['MASK_NAME', 'MASK_ID']
    smsk = '--'
    msk_val = [smsk] * len(cnames)
    for kk, cname in enumerate(cnames):
        targ_mask[cname] = [msk_val[kk]] * nrow

    # Now the 'mask' files
    mask_files = glob.glob(targ_path + '*.cat')
    all_obs = []
    all_masks = []
    for mask_file in mask_files:
        print('Reading Hectospec mask file: {:s}'.format(mask_file))
        i0 = mask_file.rfind('/')
        mask_nm = mask_file[i0 + 1:mask_file.find('.cat')]
        # Grab info from spectrum file
        #xdb.set_trace()
        spec_fil = glob.glob(mask_file[:i0 + 1] + 'spHect-' + mask_nm +
                             '.*.fits.gz')
        if len(spec_fil) == 0:
            raise ValueError('Mask not found! {:s}'.format(spec_fil))
            #ras, decs = xra.dtos1((field[1],field[2]))
            #pa=0.
        else:
            header = fits.open(spec_fil[0])[0].header
            if header['APERTURE'] != mask_nm:
                raise ValueError('Mask doesnt match!')
            pa = header['POSANGLE']
            ras = header['CAT-RA']
            decs = header['CAT-DEC']
        # Continuing
        mask_dict = dict(
            INSTR='HECTOSPEC',
            MASK_NAME=mask_nm,
            MASK_RA=ras,
            MASK_DEC=decs,
            MASK_EPOCH=2000.,
            MASK_PA=pa)  # SHOULD GRAB PA, RA, DEC FROM SPECTRA FITS HEADER
        all_masks.append(mask_dict)
        # Read obs
        f = open(mask_file, 'r')
        lines = f.readlines()
        f.close()
        iall_obs = []
        for line in lines:
            if 'OBS' in line:
                prs = line.strip().split(' ')
                gdprs = [iprs for iprs in prs if len(iprs) > 0]
                obs_dict = {}
                obs_dict['DATE'] = gdprs[2]
                obs_dict['TEXP'] = float(gdprs[3])
                obs_dict['DISPERSER'] = gdprs[4]
                obs_dict['CONDITIONS'] = gdprs[5]
                #
                iall_obs.append(obs_dict)
        obs_tab = xxul.dict_list_to_table(iall_obs)
        obs_tab['TEXP'].unit = u.s
        # Read observed targets
        obs_targ = ascii.read(mask_file, comment='#')
        gdt = np.where(obs_targ['flag'] == 1)[0]
        # Match to target list
        obs_coord = SkyCoord(ra=obs_targ['ra'][gdt] * u.hour,
                             dec=obs_targ['dec'][gdt] * u.deg)
        idx, d2d, d3d = coords.match_coordinates_sky(obs_coord,
                                                     targ_coord,
                                                     nthneighbor=1)
        gdm = np.where(d2d < 1. * u.arcsec)[0]
        if len(gdm) != len(gdt):
            raise ValueError('No match')
        else:
            for ii in range(len(gdm)):
                targ_mask['MASK_NAME'][idx[ii]] = mask_nm
                if flg_id == 0:
                    targs['TARG_ID'][idx[ii]] = int(obs_targ['objid'][gdt[ii]])
        """
        for gdi in gdt:
            mtt = np.where(targs['TARG_ID']==
                int(obs_targ['objid'][gdi]))[0]
            if len(mtt) != 1:
                raise ValueError('Multiple matches?!')
            targ_mask['MASK_NAME'][mtt[0]] = mask_nm
        """
        all_obs.append(obs_tab)
    # Add columns to targs
    for tt, cname in enumerate(cnames):
        mask = np.array([False] * len(targs))
        bad = np.where(np.array(targ_mask[cname]) == msk_val[tt])[0]
        if len(bad) > 0:
            mask[bad] = True
        #
        clm = MaskedColumn(targ_mask[cname], name=cname, mask=mask)
        targs.add_column(clm)

    # Look for ID duplicates (rare)
    gdobj = targs['TARG_ID'] > 0
    idval = np.array(targs[gdobj]['TARG_ID']).astype(int)
    uni, counts = np.unique(idval, return_counts=True)
    if len(uni) != np.sum(gdobj):
        warnings.warn("Found duplicated ID values in Hectospect cat files")
        warnings.warn("Modifying these by hand!")
        dup = np.where(counts > 1)[0]
        # Fix by-hand
        for idup in dup:
            dobj = np.where(targs['TARG_ID'] == uni[idup])[0]
            if len(dobj) == 1:
                xdb.set_trace()
            # Confirm RA/DEC are different
            dcoord = SkyCoord(ra=targs['TARG_RA'][dobj] * u.deg,
                              dec=targs['TARG_DEC'][dobj] * u.deg)
            idx, d2d, d3d = coords.match_coordinates_sky(dcoord,
                                                         dcoord,
                                                         nthneighbor=2)
            if np.sum(d2d < 1 * u.arcsec) > 0:
                raise ValueError("Two with the same RA/DEC.  Deal")
            else:
                for ii in range(1, len(dobj)):
                    # Increment
                    print('Setting TARG_ID to {:d} from {:d}'.format(
                        (ii + 1) * targs['TARG_ID'][dobj[ii]],
                        targs['TARG_ID'][dobj[ii]]))
                    targs['TARG_ID'][
                        dobj[ii]] = (ii + 1) * targs['TARG_ID'][dobj[ii]]
    # Double check
    idval = np.array(targs[gdobj]['TARG_ID']).astype(int)
    uni, counts = np.unique(idval, return_counts=True)
    if len(uni) != np.sum(gdobj):
        raise ValueError("Cannot happen")

    # Finish
    return all_masks, all_obs, targs
Exemplo n.º 4
0
def hecto_targets(field, obs_path, hecto_path=None):
    '''Read files related to Hectospec targets

    Parameters:
    -----------
    field : tuple
      (Name, ra, dec)
    obs_path : str, optional
      Path to the observing tree
    hecto_path : str, optional
      Path within the file tree to Hectospec data

    Returns:
    ----------
    Target and observing info 
    '''
    if hecto_path is None:
        hecto_path = '/Galx_Spectra/Hectospec/'

    # Targets
    targ_path = obs_path+field[0]+hecto_path

    # Target file
    targ_file = glob.glob(targ_path+'*.targ')
    if len(targ_file) != 1:
        raise ValueError('Wrong number of Hectospec target files')
    else:
        targ_file = targ_file[0]

    # Read PI, program info [NOT IMPLEMENTED]
    #f = open(msk_file, 'r')
    #lines = f.readlines()
    #f.close()

    # Read target table
    tab = ascii.read(targ_file,comment='#')
    # Restrict to targets
    itarg = np.where(tab['type']=='TARGET')
    targs = tab[itarg]
    # Polish
    nrow = len(targs)
    targs.rename_column('ra','RAS')
    targs.rename_column('dec','DECS')
    targs.add_column(Column([0.]*nrow,name='TARG_RA'))
    targs.add_column(Column([0.]*nrow,name='TARG_DEC'))
    # Get RA/DEC in degrees
    for k,row in enumerate(targs):
        coord = ltu.radec_to_coord((row['RAS'], row['DECS']))
        targs[k]['TARG_RA'] = coord.ra.value
        targs[k]['TARG_DEC'] = coord.dec.value
    # ID/Mag (not always present)
    targ_coord = SkyCoord(ra=targs['TARG_RA']*u.deg, dec=targs['TARG_DEC']*u.deg)
    try:
        targs.rename_column('objid','TARG_ID')
    except KeyError:
        targs.add_column(Column([0]*nrow,name='TARG_ID'))
        targs.add_column(Column([0.]*nrow,name='TARG_MAG'))
        flg_id = 0
    else:
        flg_id = 1
        targs.rename_column('mag','TARG_MAG')
    targs.add_column(Column([0.]*nrow,name='EPOCH'))
    targs.add_column(Column(['SDSS']*nrow,name='TARG_IMG'))
    targs.add_column(Column(['HECTOSPEC']*nrow,name='INSTR'))

    targ_mask = {}
    cnames = ['MASK_NAME', 'MASK_ID']
    smsk = '--'
    msk_val = [smsk]*len(cnames)
    for kk,cname in enumerate(cnames):
        targ_mask[cname] = [msk_val[kk]]*nrow

    # Now the 'mask' files
    mask_files = glob.glob(targ_path+'*.cat')
    all_obs = []
    all_masks = []
    for mask_file in mask_files:
        print('Reading Hectospec mask file: {:s}'.format(mask_file))
        i0 = mask_file.rfind('/')
        mask_nm = mask_file[i0+1:mask_file.find('.cat')]
        # Grab info from spectrum file
        #xdb.set_trace()
        spec_fil = glob.glob(mask_file[:i0+1]+'spHect-'+mask_nm+'.*.fits.gz')
        if len(spec_fil) == 0:
            raise ValueError('Mask not found! {:s}'.format(spec_fil))
            #ras, decs = xra.dtos1((field[1],field[2]))
            #pa=0.
        else:
            header = fits.open(spec_fil[0])[0].header
            if header['APERTURE'] != mask_nm:
                raise ValueError('Mask doesnt match!')
            pa = header['POSANGLE']
            ras = header['CAT-RA']
            decs = header['CAT-DEC']
        # Continuing
        mask_dict = dict(INSTR='HECTOSPEC',MASK_NAME=mask_nm,
            MASK_RA=ras, MASK_DEC=decs, MASK_EPOCH=2000.,
            MASK_PA=pa) # SHOULD GRAB PA, RA, DEC FROM SPECTRA FITS HEADER
        all_masks.append(mask_dict)
        # Read obs
        f = open(mask_file, 'r')
        lines = f.readlines()
        f.close()
        iall_obs = []
        for line in lines:
            if 'OBS' in line:
                prs = line.strip().split(' ')
                gdprs = [iprs for iprs in prs if len(iprs)>0]
                obs_dict = {}
                obs_dict['DATE'] = gdprs[2]
                obs_dict['TEXP'] = float(gdprs[3])
                obs_dict['DISPERSER'] = gdprs[4]
                obs_dict['CONDITIONS'] = gdprs[5]
                # 
                iall_obs.append(obs_dict) 
        obs_tab = xxul.dict_list_to_table(iall_obs)
        obs_tab['TEXP'].unit = u.s
        # Read observed targets
        obs_targ = ascii.read(mask_file,comment='#')
        gdt = np.where(obs_targ['flag'] == 1)[0]
        # Match to target list
        obs_coord = SkyCoord(ra=obs_targ['ra'][gdt]*u.hour, dec=obs_targ['dec'][gdt]*u.deg)
        idx, d2d, d3d = coords.match_coordinates_sky(obs_coord, targ_coord, nthneighbor=1)
        gdm = np.where(d2d < 1.*u.arcsec)[0]
        if len(gdm) != len(gdt):
            raise ValueError('No match')
        else:
            for ii in range(len(gdm)):
                targ_mask['MASK_NAME'][idx[ii]] = mask_nm
                if flg_id == 0:
                    targs['TARG_ID'][idx[ii]] = int(obs_targ['objid'][gdt[ii]])
        """
        for gdi in gdt:
            mtt = np.where(targs['TARG_ID']==
                int(obs_targ['objid'][gdi]))[0]
            if len(mtt) != 1:
                raise ValueError('Multiple matches?!')
            targ_mask['MASK_NAME'][mtt[0]] = mask_nm
        """
        all_obs.append(obs_tab)
    # Add columns to targs
    for tt,cname in enumerate(cnames):
        mask = np.array([False]*len(targs))
        bad = np.where(np.array(targ_mask[cname])==msk_val[tt])[0]
        if len(bad)>0:
            mask[bad]=True
        #
        clm = MaskedColumn(targ_mask[cname],name=cname, mask=mask)
        targs.add_column(clm)

    # Look for ID duplicates (rare)
    gdobj = targs['TARG_ID'] > 0
    idval = np.array(targs[gdobj]['TARG_ID']).astype(int)
    uni, counts = np.unique(idval, return_counts=True)
    if len(uni) != np.sum(gdobj):
        warnings.warn("Found duplicated ID values in Hectospect cat files")
        warnings.warn("Modifying these by hand!")
        dup = np.where(counts>1)[0]
        # Fix by-hand
        for idup in dup:
            dobj = np.where(targs['TARG_ID'] == uni[idup])[0]
            if len(dobj) == 1:
                xdb.set_trace()
            # Confirm RA/DEC are different
            dcoord = SkyCoord(ra=targs['TARG_RA'][dobj]*u.deg,
                              dec=targs['TARG_DEC'][dobj]*u.deg)
            idx, d2d, d3d = coords.match_coordinates_sky(dcoord, dcoord, nthneighbor=2)
            if np.sum(d2d < 1*u.arcsec) > 0:
                raise ValueError("Two with the same RA/DEC.  Deal")
            else:
                for ii in range(1,len(dobj)):
                    # Increment
                    print('Setting TARG_ID to {:d} from {:d}'.format(
                            (ii+1)*targs['TARG_ID'][dobj[ii]],targs['TARG_ID'][dobj[ii]]))
                    targs['TARG_ID'][dobj[ii]] = (ii+1)*targs['TARG_ID'][dobj[ii]]
    # Double check
    idval = np.array(targs[gdobj]['TARG_ID']).astype(int)
    uni, counts = np.unique(idval, return_counts=True)
    if len(uni) != np.sum(gdobj):
        raise ValueError("Cannot happen")

    # Finish
    return all_masks, all_obs, targs
Exemplo n.º 5
0
def parse_deimos_mask_file(msk_file):
    '''Parse the standard DEIMOS mask file
    '''
    # Read
    f = open(msk_file, 'r')
    lines = f.readlines()
    f.close()
    #
    flg_mask = 0
    flg_select = 1
    mask_dict = dict(INSTR='DEIMOS')
    all_targ = []
    all_obs = []
    for kk,line in enumerate(lines):
        # Searching for mask name, center
        if flg_mask==0:
            if 'Mask' in line:
                flg_mask=1
                continue
        # Mask info
        if flg_mask==1:
            prs = line.strip().split(' ')
            # 
            cnt = 0
            for jj,iprs in enumerate(prs):
                if len(iprs) == 0:
                    continue
                if cnt==0:
                    mask_dict['MASK_NAME'] = iprs
                elif cnt==1:
                    mask_dict['MASK_RA'] = iprs
                elif cnt==2:
                    mask_dict['MASK_DEC'] = iprs
                elif cnt==3:
                    mask_dict['MASK_EPOCH'] = float(iprs)
                elif cnt==4:
                    mask_dict['MASK_PA'] = float(iprs[3:])
                cnt += 1
            flg_mask = 2
            continue
        # Observation info
        if 'OBS' in line:
            prs = line.strip().split(' ')
            gdprs = [iprs for iprs in prs if len(iprs)>0]
            obs_dict = {}
            obs_dict['DATE'] = gdprs[2]
            obs_dict['TEXP'] = float(gdprs[3])
            obs_dict['DISPERSER'] = gdprs[4]
            obs_dict['CONDITIONS'] = gdprs[5]
            # 
            all_obs.append(obs_dict)


        # Done
        if 'Non' in line:
            break
        # Dummy line?
        if (len(line.strip())==0) or (line[0]=='#'):
            continue
        # Selected object
        prs = line.strip().split(' ')
        gdprs = [iprs for iprs in prs if len(iprs)>0]
        if int(gdprs[6]) < 0: # QSO or Star
            continue
        targ_dict = {}
        targ_dict['ID'] = int(gdprs[0])
        targ_dict['RAS'] = gdprs[1]
        targ_dict['DECS'] = gdprs[2]
        targ_dict['EPOCH'] = float(gdprs[3])
        # Add to list
        all_targ.append(targ_dict)

    # Generate Tables
    # Obs
    if len(all_obs) > 0:
        obs_tab = xxul.dict_list_to_table(all_obs)
        obs_tab['TEXP'].unit = u.s
    else:
        obs_tab = None
    # Targ
    targ_tab = xxul.dict_list_to_table(all_targ)

    # Return
    return mask_dict, targ_tab, obs_tab 
Exemplo n.º 6
0
def mmt_targets(field, path=None):
    """Read files related to MMT targets

    Parameters:
    -----------
    field: tuple
      (Name, ra, dec)

    Returns:
    ----------
    Target and observing info 
    """
    if path is None:
        path = "/Galx_Spectra/MMT/"

    # Targets
    targ_path = field[0] + path

    # Target file
    targ_file = glob.glob(targ_path + "*.targ")
    if len(targ_file) != 1:
        raise ValueError("Wrong number of MMT target files")
    else:
        targ_file = targ_file[0]

    # Read PI, program info [NOT IMPLEMENTED]
    # f = open(msk_file, 'r')
    # lines = f.readlines()
    # f.close()

    # Read target table
    tab = ascii.read(targ_file, comment="#")
    # Restrict to targets
    itarg = np.where(tab["type"] == "TARGET")
    targs = tab[itarg]
    # Polish
    nrow = len(targs)
    targs.rename_column("ra", "RAS")
    targs.rename_column("dec", "DECS")
    targs.add_column(Column([0.0] * nrow, name="TARG_RA"))
    targs.add_column(Column([0.0] * nrow, name="TARG_DEC"))
    # Get RA/DEC in degrees
    for k, row in enumerate(targs):
        rad, decd = xra.stod1((row["RAS"], row["DECS"]))
        targs[k]["TARG_RA"] = rad.value
        targs[k]["TARG_DEC"] = decd.value
    targs.rename_column("objid", "TARG_ID")
    targs.rename_column("mag", "TARG_MAG")
    targs.add_column(Column([0.0] * nrow, name="EPOCH"))
    targs.add_column(Column(["SDSS"] * nrow, name="TARG_IMG"))
    targs.add_column(Column(["HECTOSPEC"] * nrow, name="INSTR"))

    targ_mask = {}
    cnames = ["MASK_NAME", "MASK_ID"]
    smsk = "--"
    msk_val = [smsk] * len(cnames)
    for kk, cname in enumerate(cnames):
        targ_mask[cname] = [msk_val[kk]] * nrow

    # Now the 'mask' files
    mask_files = glob.glob(targ_path + "*.cat")
    all_obs = []
    all_masks = []
    for mask_file in mask_files:
        print("Reading MMT mask file: {:s}".format(mask_file))
        i0 = mask_file.rfind("/")
        mask_nm = mask_file[i0 + 1 : mask_file.find(".cat")]
        # Grab info from spectrum file
        # xdb.set_trace()
        spec_fil = glob.glob(mask_file[: i0 + 1] + "spHect-" + mask_nm + "*.fits.gz")
        if len(spec_fil) != 1:
            print("spec_fil -- Not found!".format(spec_fil))
            ras, decs = xra.dtos1((field[1], field[2]))
            pa = 0.0
        else:
            header = fits.open(spec_fil[0])[0].header
            if header["APERTURE"] != mask_nm:
                raise ValueError("Mask doesnt match!")
            pa = header["POSANGLE"]
            ras = header["CAT-RA"]
            decs = header["CAT-DEC"]
        # Continuing
        mask_dict = dict(
            INSTR="HECTOSPEC", MASK_NAME=mask_nm, MASK_RA=ras, MASK_DEC=decs, MASK_EPOCH=2000.0, MASK_PA=pa
        )  # SHOULD GRAB PA, RA, DEC FROM SPECTRA FITS HEADER
        all_masks.append(mask_dict)
        # Read obs
        f = open(mask_file, "r")
        lines = f.readlines()
        f.close()
        iall_obs = []
        for line in lines:
            if "OBS" in line:
                prs = line.strip().split(" ")
                gdprs = [iprs for iprs in prs if len(iprs) > 0]
                obs_dict = {}
                obs_dict["DATE"] = gdprs[2]
                obs_dict["TEXP"] = float(gdprs[3])
                obs_dict["DISPERSER"] = gdprs[4]
                obs_dict["CONDITIONS"] = gdprs[5]
                #
                iall_obs.append(obs_dict)
        obs_tab = xxul.dict_list_to_table(iall_obs)
        obs_tab["TEXP"].unit = u.s
        # Read observed targets
        obs_targ = ascii.read(mask_file, comment="#")
        gdt = np.where(obs_targ["flag"] == 1)[0]
        for gdi in gdt:
            mtt = np.where(targs["TARG_ID"] == int(obs_targ["objid"][gdi]))[0]
            if len(mtt) != 1:
                raise ValueError("Multiple matches?!")
            targ_mask["MASK_NAME"][mtt[0]] = mask_nm
        all_obs.append(obs_tab)
    # Finish
    mask = np.array([False] * len(targs))
    bad = np.where(np.array(targ_mask["names"]) == "--")[0]
    if len(bad) > 0:
        mask[bad] = True
    #
    clm = MaskedColumn(targ_mask["names"], name="MASK_NAME", mask=mask)
    targs.add_column(clm)

    return all_masks, all_obs, targs
Exemplo n.º 7
0
def parse_deimos_mask_file(msk_file):
    """Parse the standard DEIMOS mask file
    """
    # Read
    f = open(msk_file, "r")
    lines = f.readlines()
    f.close()
    #
    flg_mask = 0
    flg_select = 1
    mask_dict = dict(INSTR="DEIMOS")
    all_targ = []
    all_obs = []
    for kk, line in enumerate(lines):
        # Searching for mask name, center
        if flg_mask == 0:
            if "Mask" in line:
                flg_mask = 1
                continue
        # Mask info
        if flg_mask == 1:
            prs = line.strip().split(" ")
            #
            cnt = 0
            for jj, iprs in enumerate(prs):
                if len(iprs) == 0:
                    continue
                if cnt == 0:
                    mask_dict["MASK_NAME"] = iprs
                elif cnt == 1:
                    mask_dict["MASK_RA"] = iprs
                elif cnt == 2:
                    mask_dict["MASK_DEC"] = iprs
                elif cnt == 3:
                    mask_dict["MASK_EPOCH"] = float(iprs)
                elif cnt == 4:
                    mask_dict["MASK_PA"] = float(iprs[3:])
                cnt += 1
            flg_mask = 2
            continue
        # Observation info
        if "OBS" in line:
            prs = line.strip().split(" ")
            gdprs = [iprs for iprs in prs if len(iprs) > 0]
            obs_dict = {}
            obs_dict["DATE"] = gdprs[2]
            obs_dict["TEXP"] = float(gdprs[3])
            obs_dict["DISPERSER"] = gdprs[4]
            obs_dict["CONDITIONS"] = gdprs[5]
            #
            all_obs.append(obs_dict)

        # Done
        if "Non" in line:
            break
        # Dummy line?
        if (len(line.strip()) == 0) or (line[0] == "#"):
            continue
        # Selected object
        prs = line.strip().split(" ")
        gdprs = [iprs for iprs in prs if len(iprs) > 0]
        if int(gdprs[6]) < 0:  # QSO or Star
            continue
        targ_dict = {}
        targ_dict["ID"] = int(gdprs[0])
        targ_dict["RAS"] = gdprs[1]
        targ_dict["DECS"] = gdprs[2]
        targ_dict["EPOCH"] = float(gdprs[3])
        # Add to list
        all_targ.append(targ_dict)

    # Generate Tables
    # Obs
    if len(all_obs) > 0:
        obs_tab = xxul.dict_list_to_table(all_obs)
        obs_tab["TEXP"].unit = u.s
    else:
        obs_tab = None
    # Targ
    targ_tab = xxul.dict_list_to_table(all_targ)

    # Return
    return mask_dict, targ_tab, obs_tab