def manual_insert_many():

    sourceids = [
        #'2890062263458259968',
        #'2072431332214824576',
        #'1833519030401513984',
        '5404435280772350848'
    ]

    comments = [
        #"V-shaped. Cool if planet (probably is not, since V-shaped).  Neighborhood plot looks fine -- Platais5 is definitely a real thing though Prot cleanup required.   Star Prot 2.5 or 3 days from SPOC LC.  Teff 3250K, it's an M dwarf.  Smearing expected here on transit, given the 2 hour transit?  Needs some RVs to verify.  SG1 finds VPC, detecting event on target.",
        #"Good youngest HJ cand.  Teff 7800K; an early F/late A-star.  Rp=22Re.  One could set a mass upper limit via RVs, and get a DT observation to confirm.  Cluster membership from Gaia blind kinematics is good (actual cluster plot would be better).  Needs recon spectroscopy to check if high RV scatter."
        #"This candidate HJ orbits a Teff 5850K G-dwarf.  The star's rotation period appears to be 2.5-3 days. i.e., consistent with an age of ~100 Myr give or take.  And the transit looks really good.  Kinematically, the str is in the outskirts of the Alessi12 cluster, at least as discussed by KC19.  It is one of the closest stars reported in the cluster, and so its proper motions are pretty different from the other cluster members.  Needs specFU (e.g., for Li) to verify youth."
        "Probably multi. Prot 3d? is consistent with the youth, b/c Teff 6500K... but also consistent with a good deal older than 100 Myr.   Neighborhood plot looks ok.  Definitely worth getting Li, spectra, etc to check"
    ]

    for s, c in zip(sourceids, comments):

        ticid = gaiadr2_to_tic(s)

        insert_single_candidate(ticid=ticid,
                                comment=c,
                                nbhd_rating=0,
                                init_priority=0,
                                pending_photometry_observations='to_assess',
                                pending_spectroscopic_observations='to_assess')
import os
import pandas as pd
from astrobase.services.identifiers import gaiadr2_to_tic

csvpath = os.path.join(
    '~/Dropbox/proj/earhart/results/glue_ngc2516_manual_rotator_viz',
    'close_in_prot_and_color_chiron_five.csv'
)

df = pd.read_csv(csvpath)

ticids = []

for s in df.source_id:
    ticids.append(gaiadr2_to_tic(s))

outdf = pd.DataFrame({
    'source_id': df.source_id,
    'ticid': ticids
})

import IPython; IPython.embed()
def get_neighborhood_information(
    source_id,
    overwrite=0,
    min_n_nbhrs=1000,
    manual_gmag_limit=None
    ):
    """
    Given a source_id for a star (potentially a field star), acquire
    information necessary for neighborhood diagnostic plots.

    Parameters:

        source_id: Gaia DR2 source_id

        overwrite: Whether the Gaia cache gets overwritten.

        manual_gmag_limit: G < manual_gmag_limit for the neighborhood
    """

    #
    # Get the targetname
    #
    ticid = gaiadr2_to_tic(str(source_id))
    toiid = ticid_to_toiid(ticid)

    if isinstance(toiid, str):
        targetname = toiid
    else:
        targetname = 'TIC{}.01'.format(ticid)

    #
    # Get Gaia information for target.
    #
    enforce_all_sourceids_viable = True
    savstr = '_nbhdonly'

    target_d = objectid_search(
        source_id,
        columns=('source_id', 'ra','dec', 'ra_error', 'dec_error',
                 'phot_g_mean_mag', 'phot_bp_mean_mag', 'phot_rp_mean_mag',
                 'l','b', 'parallax, parallax_error', 'pmra','pmra_error',
                 'pmdec','pmdec_error', 'radial_velocity'),
        forcefetch=True,
        gaia_mirror='vizier'
    )
    target_df = pd.read_csv(target_d['result'])
    assert len(target_df) == 1

    # now acquire the mean properties of the group, and query the neighborhood
    # based on those properties. the number of neighbor stars to randomly
    # select is min(5* the number of group members, 5000). (cutoff group
    # bounds based on parallax because further groups more uncertain).
    bounds = {}
    params = ['parallax', 'ra', 'dec']

    plx_mean = float(target_df.parallax)

    n_nbhrs = 0
    n_std = 5
    n_std_incr = 10
    n_std_max = 200

    if plx_mean > 10:
        n_std = 5
        n_std_incr = 20
        n_std_max = 1000

    while n_nbhrs < min_n_nbhrs:

        if n_std > n_std_max:
            return None

        LOGINFO('trying when bounding by {} stdevns'.format(n_std))

        for param in params:
            mult = 1 if 'parallax' in param else 2
            bounds[param+'_upper'] = (
                float(target_df[param]) + mult*n_std*float(target_df[param + '_error'])
            )
            bounds[param+'_lower'] = (
                float(target_df[param]) - mult*n_std*float(target_df[param + '_error'])
            )

        if bounds['ra_upper'] > 360:
            bounds['ra_upper'] = 359.99
        if bounds['ra_lower'] < 0:
            bounds['ra_lower'] = 0
        if bounds['parallax_lower'] < 0:
            bounds['parallax_lower'] = 0

        n_max = int(1e4)

        if manual_gmag_limit is None:
            manual_gmag_limit = 17

        groupname = '{}'.format(source_id)
        # only force overwrite if iterating
        if n_nbhrs == 0:
            nbhd_df = query_neighborhood(bounds, groupname, n_max=n_max,
                                         overwrite=overwrite,
                                         manual_gmag_limit=manual_gmag_limit)
        else:
            nbhd_df = query_neighborhood(bounds, groupname, n_max=n_max,
                                         overwrite=True,
                                         manual_gmag_limit=manual_gmag_limit)

        n_nbhrs = len(nbhd_df)
        LOGINFO(42*'=')
        LOGINFO('Got {} neighborhods, when minimum was {}'.
              format(n_nbhrs, min_n_nbhrs))
        LOGINFO(42*'=')

        n_std += n_std_incr

    n_std = 3
    pmdec_min = np.nanmean(nbhd_df['pmdec']) - n_std*np.nanstd(nbhd_df['pmdec'])
    pmdec_max = np.nanmean(nbhd_df['pmdec']) + n_std*np.nanstd(nbhd_df['pmdec'])
    pmra_min = np.nanmean(nbhd_df['pmra']) - n_std*np.nanstd(nbhd_df['pmra'])
    pmra_max = np.nanmean(nbhd_df['pmra']) + n_std*np.nanstd(nbhd_df['pmra'])

    pmdec_min = min((pmdec_min, float(target_df['pmdec'])))
    pmdec_max = max((pmdec_max, float(target_df['pmdec'])))
    pmra_min = min((pmra_min, float(target_df['pmra'])))
    pmra_max = max((pmra_max, float(target_df['pmra'])))

    return (targetname, groupname, target_df, nbhd_df,
            pmdec_min, pmdec_max, pmra_min, pmra_max)
def get_group_and_neighborhood_information(
    source_id,
    overwrite=0,
    force_groupname=None,
    force_references=None,
    force_cdips_match=True,
    manual_gmag_limit=None,
    CATALOG_VERSION=0.6):
    """
    Given a source_id for a cluster member, acquire information necessary for
    neighborhood diagnostic plots. (Namely, find all the group members, then do
    a Gaia query of everything).

    Parameters:

        source_id: Gaia DR2 source_id

        overwrite: Whether the Gaia cache gets overwritten.

        Optional kwargs: force_groupname and force_references. If passed, for
        example as the arrays ["kc19group_1222"] and ["Kounkel_2019"], plot
        generation will be forced without verifying that the target "source_id"
        is a member of the group.
    """

    if not isinstance(source_id, np.int64):
        source_id = np.int64(source_id)

    cdips_df = get_cdips_pub_catalog(ver=CATALOG_VERSION)
    row = cdips_df[cdips_df.source_id == source_id]

    if pd.isnull(row['cluster'].iloc[0]):
        LOGWARNING(f'Did not find any group matches for GAIA DR2 {source_id}.')
        return None

    #
    # Get numpy arrays of references and cluster names for this star.
    # This is needed b/c multiple memberships are comma-separated.
    #
    if 'reference' in row:
        references = np.array(row['reference'].iloc[0].split(','))
    elif 'reference_id' in row:
        references = np.array(row['reference_id'].iloc[0].split(','))
    clusters = np.array(row['cluster'].iloc[0].split(','))
    if force_references and force_groupname:
        references = force_references
        clusters = np.array([force_groupname])

    assert len(references) == len(clusters)

    if row is None and force_cdips_match:
        LOGINFO('Failed to get CDIPS target list match for {}'.format(source_id))
        return None

    #
    # Given the numpy array of clusters, find the best cluster membership list
    # to make the report with.
    #
    from cdips.catalogbuild.membership_lists import RANKED_MEMBERSHIP_DICT

    references_in_ensembles = RANKED_MEMBERSHIP_DICT[CATALOG_VERSION]['isgroup']
    references_in_field = RANKED_MEMBERSHIP_DICT[CATALOG_VERSION]['isfield']

    is_in_group = np.any(np.in1d(references, references_in_ensembles))
    is_in_field  = np.any(np.in1d(references, references_in_field))

    if is_in_group:
        # At least one of the references given corresponds to an actual coeval
        # group, and not a list of young field stars.  In this case, take the
        # highest precedence group (lowest index) as the one to make the plot
        # for.
        referencename = references_in_ensembles[
            int(min(np.argwhere(np.in1d(references_in_ensembles, references))))
        ]

        groupname = clusters[references == referencename][0]

    else:
        LOGWARNING(f'Did not find any group matches for GAIA DR2 {source_id}.')
        return None

    #
    # Get the targetname
    #
    ticid = gaiadr2_to_tic(str(source_id))
    toiid = ticid_to_toiid(ticid)

    if isinstance(toiid, str):
        targetname = toiid
    else:
        targetname = 'TIC{}.01'.format(ticid)

    #
    # Get the group members!
    #

    # (We avoided the field star case earlier.)
    cdips_df = cdips_df[~pd.isnull(cdips_df.cluster)]

    group_df = cdips_df[
        # avoids e.g., "kc19group_981" matching on "kc19group_98". deals with
        # cases beginning of string, middle of string, and end of string.
        (
            (cdips_df.cluster.str.contains(groupname+','))
            |
            (cdips_df.cluster.str.contains(','+groupname+','))
            |
            (cdips_df.cluster.str.contains(','+groupname+'$'))
            |
            (cdips_df.cluster == groupname)
        )
        &
        (cdips_df.reference_id.str.contains(referencename))
    ]

    group_source_ids = np.array(group_df['source_id']).astype(np.int64)
    np.testing.assert_array_equal(group_df['source_id'], group_source_ids)

    #
    # Given the source ids, get all the relevant Gaia information.
    #
    enforce_all_sourceids_viable = True
    savstr = f'_{groupname.replace(" ","_")}_{referencename}'

    group_df_dr2 = given_source_ids_get_gaia_data(
        group_source_ids, groupname.replace(" ","_"), overwrite=overwrite,
        enforce_all_sourceids_viable=enforce_all_sourceids_viable,
        n_max=min((len(group_source_ids), 10000)),
        savstr=savstr
    )

    target_d = objectid_search(
        source_id,
        columns=('source_id', 'ra','dec', 'phot_g_mean_mag',
                 'phot_bp_mean_mag', 'phot_rp_mean_mag', 'l','b',
                 'parallax, parallax_error', 'pmra','pmra_error',
                 'pmdec','pmdec_error', 'radial_velocity'),
        forcefetch=True,
        gaia_mirror='vizier'
    )
    target_df = pd.read_csv(target_d['result'])
    assert len(target_df) == 1

    # now acquire the mean properties of the group, and query the neighborhood
    # based on those properties. the number of neighbor stars to randomly
    # select is min(5* the number of group members, 5000). (cutoff group
    # bounds based on parallax because further groups more uncertain).
    bounds = {}
    params = ['parallax', 'ra', 'dec']

    plx_mean = group_df_dr2['parallax'].mean()
    if plx_mean > 5:
        n_std = 5
    elif plx_mean > 3:
        n_std = 4
    else:
        n_std = 3

    LOGINFO(f'bounding by {n_std} stdevns')

    for param in params:
        bounds[param+'_upper'] = np.minimum(
            group_df_dr2[param].mean() + n_std*group_df_dr2[param].std(),
            360-1e-5
        )
        bounds[param+'_lower'] = np.maximum(
            group_df_dr2[param].mean() - n_std*group_df_dr2[param].std(),
            1e-5
        )

    if bounds['parallax_lower'] < 0:
        bounds['parallax_lower'] = 0

    assert bounds['ra_upper'] < 360
    assert bounds['ra_lower'] > 0
    assert bounds['parallax_lower'] >= 0

    n_max = min((50*len(group_df_dr2), 10000))

    if manual_gmag_limit is None:
        manual_gmag_limit = np.nanpercentile(group_df_dr2.phot_g_mean_mag,95)

    mstr = savstr
    nbhd_df = query_neighborhood(bounds, groupname.replace(" ","_"), n_max=n_max,
                                 overwrite=overwrite,
                                 manual_gmag_limit=manual_gmag_limit,
                                 mstr=mstr)

    # ensure no overlap between the group members and the neighborhood sample.
    common = group_df_dr2.merge(nbhd_df, on='source_id', how='inner')
    snbhd_df = nbhd_df[~nbhd_df.source_id.isin(common.source_id)]

    n_std = 5
    pmdec_min = group_df_dr2['pmdec'].mean() - n_std*group_df_dr2['pmdec'].std()
    pmdec_max = group_df_dr2['pmdec'].mean() + n_std*group_df_dr2['pmdec'].std()
    pmra_min = group_df_dr2['pmra'].mean() - n_std*group_df_dr2['pmra'].std()
    pmra_max = group_df_dr2['pmra'].mean() + n_std*group_df_dr2['pmra'].std()

    pmdec_min = min((pmdec_min, float(target_df['pmdec'])))
    pmdec_max = max((pmdec_max, float(target_df['pmdec'])))
    pmra_min = min((pmra_min, float(target_df['pmra'])))
    pmra_max = max((pmra_max, float(target_df['pmra'])))

    return (targetname, groupname, referencename, group_df_dr2, target_df,
            snbhd_df, pmdec_min, pmdec_max, pmra_min, pmra_max)
def main(uploadnamestr='sectors_12_thru_13_clear_threshold'):
    """
    ----------
    Args:

        uploadnamestr: used as unique identifying string in file names. Must
        match that from merge_for_exofoptess.

    """

    dfpath = os.path.join(
        exofopdir, "{}_{}_w_sourceid.csv".format(DATESTR, uploadnamestr))
    df = pd.read_csv(dfpath, sep='|')

    ticstrs = []
    for ix, r in df.iterrows():

        source_id = np.int64(r['source_id'])

        if str(r['target']).startswith('TIC'):
            ticstr = str(r['target']).replace(r'.01', '')
        else:
            ticid = gaiadr2_to_tic(str(source_id))
            if ticid is None:
                raise ValueError(
                    'failed to get TICID for {}'.format(source_id))
            ticstr = 'TIC' + ticid

        ticstrs.append(ticstr)

        #
        # first, get and rename the vetting reports
        #
        reportpaths = glob(
            os.path.join(hlspreportdir, '*{}*pdf'.format(source_id)))

        if len(reportpaths) == 0:
            raise AssertionError(
                'need a vetting report for {}'.format(source_id))

        for reportpath in reportpaths:

            reportname = os.path.basename(reportpath).replace('.pdf', '')
            reportname = reportname.replace('_hlsp_cdips_tess_ffi', '')

            lettercode = "O"  # other
            dstname = (
                '{ticstr}{lettercode}-lb{datestamp}_{reportname}.pdf'.format(
                    ticstr=ticstr,
                    lettercode=lettercode,
                    datestamp=DATESTR,
                    reportname=reportname))

            dstpath = os.path.join(exofop_upload_dir, dstname)

            if not os.path.exists(dstpath):
                shutil.copyfile(reportpath, dstpath)
                print('copy {} -> {}'.format(reportpath, dstpath))
            else:
                print('found {}'.format(dstpath))

    df['ticstrs'] = ticstrs

    #
    # make a text file that describes each file (file name, data tag,
    # optional group name, proprietary period, and description.
    #
    files_to_upload = glob(os.path.join(exofop_upload_dir, '*'))

    fnames = [
        os.path.basename(x) for x in files_to_upload if not x.endswith('.txt')
    ]

    description = []
    tags = []

    for f in fnames:

        ticstr = f.split('O-')[0]

        sdf = df[df['ticstrs'] == ticstr]

        assert len(sdf) == 1

        tags.append(str(sdf['tag'].iloc[0]))

        if 'vet_gaia' in f:
            sectornum = f.split('-')[2].lstrip('0')
            description.append('CDIPS pipeline report (S{})'.format(sectornum))

    txt_df = pd.DataFrame({
        'fname':
        fnames,
        'tag':
        tags,
        'groupname':
        np.ones(len(fnames)) * np.nan,
        'proprietary_period':
        np.zeros(len(fnames)).astype(int),
        'description':
        description
    })

    txtfiles = glob(os.path.join(exofop_upload_dir, '*.txt'))
    for f in txtfiles:
        os.remove(f)

    outpath = os.path.join(
        exofop_upload_dir, 'lb{datestamp}-{uploadnumber}.txt'.format(
            datestamp=DATESTR, uploadnumber=str(uploadnumber).zfill(3)))
    txt_df.to_csv(outpath, sep="|", header=False, index=False)
    print('made {}'.format(outpath))

    #
    # copy the directory to the one that we will actually use for the upload
    # and compression.
    #

    files_to_upload = glob(os.path.join(exofop_upload_dir, '*'))

    dsts = []
    for f in files_to_upload:
        src = f
        dst = os.path.join(uploaddir, os.path.basename(src))
        if not os.path.exists(dst):
            shutil.copyfile(src, dst)
            print('copied {}->{}'.format(src, dst))
        else:
            print('found {}'.format(dst))
        dsts.append(dst)
def insert_candidate(source_id=None,
                     ticid=None,
                     manual_dict=None,
                     raise_error_if_duplicate=True):
    """
    Insert a candidate to the candidates.csv database by passing either Gaia
    DR2 source_id or otherwise ticid (string).

    Optional Arguments:
    ----------

    manual_dict: dict

        With keys:
        nbhd_rating (0-2, or -1 for null),
        init_priority (0-2),
        current_priority (0-2),
        pending_spectroscopic_observations (str, '--' if null),
        pending_photometry_observations (str, '--' if null),
        comment (str, '--' if null)
        candidate_provenance (str)
        isretired (0 or 1)

    raise_error_if_duplicate: boolean

        If attempting an insert on a source_id that already exists, an error
        will be raised, and the insert will not be performed. If false, a
        warning is raised, and the insert will not be performed.
    """

    #
    # Get identifiers (source_id, ticid, toi, targetid).
    #
    validate_source_id_ticid(source_id, ticid)

    if isinstance(source_id, str):
        ticid = gaiadr2_to_tic(source_id)
    elif isinstance(ticid, str):
        source_id = tic_to_gaiadr2(ticid)

    toiid = ticid_to_toiid(ticid)
    if isinstance(toiid, str):
        toiid = toiid.replace('.01', '')

    targetid = ticid_and_toiid_to_targetid(ticid, toiid)

    #
    # Get CDIPS & GaiaDR2 catalog information, or else assign nans.
    #

    cdips_r = get_cdips_pub_catalog_entry(source_id)

    iscdipstarget = 1 if isinstance(cdips_r, pd.DataFrame) else 0

    if isinstance(cdips_r, pd.DataFrame):
        assert len(cdips_r) == 1
        cdips_r = cdips_r.iloc[0]

        for col in cdips_r.index:
            if pd.isnull(cdips_r[col]):
                cdips_r[col] = -1

    else:
        cdips_cols = [
            'cluster', 'reference', 'ext_catalog_name', 'ra', 'dec', 'pmra',
            'pmdec', 'parallax', 'phot_g_mean_mag', 'phot_bp_mean_mag',
            'phot_rp_mean_mag', 'k13_name_match', 'unique_cluster_name',
            'how_match', 'not_in_k13', 'comment', 'logt', 'e_logt',
            'logt_provenance'
        ]

        cdips_r = pd.Series({'source_id': source_id})

        for col in cdips_cols:
            cdips_r[col] = '-1'

    #
    # Get TIC information, or else assign nans
    #

    ticcols = [
        'ID', 'GAIA', 'Bmag', 'Vmag', 'Jmag', 'Hmag', 'Kmag', 'Tmag', 'Teff',
        'logg', 'rad', 'mass'
    ]

    tic_r = get_tic_star_information(ticid, desiredcols=ticcols)

    if isinstance(tic_r, pd.DataFrame):
        assert len(tic_r) == 1
        tic_r = tic_r.iloc[0]

        if not tic_r.GAIA == source_id:
            errmsg = (
                'expected tic GAIA ID ({}) to match my GAIA ID ({})'.format(
                    tic_r.GAIA, source_id))
            raise AssertionError(errmsg)

        for col in ticcols:
            if pd.isnull(tic_r[col]):
                tic_r[col] = -1

    else:
        tic_r = pd.Series({'source_id': source_id})

        for col in ticcols:
            tic_r[col] = -1

    #
    # Get the fit information as uploaded to ExoFOP-TESS. By default, use the
    # TOI table information. Otherwise, use the CTOI table.
    #

    plproperties_r = get_exofop_toi_catalog_entry(ticid)
    plkeyd = {
        'rp': 'Planet Radius (R_Earth)',
        'rp_unc': 'Planet Radius (R_Earth) err',
        'period': 'Period (days)',
        'depth': 'Depth (mmag)'
    }
    if plproperties_r is None:
        plproperties_r = get_exofop_ctoi_catalog_entry(ticid)
        plkeyd = {
            'rp': 'Radius (R_Earth)',
            'rp_unc': 'Radius (R_Earth) Error',
            'period': 'Period (days)',
            'depth': 'Depth mmag'
        }

    if isinstance(plproperties_r, pd.DataFrame):
        assert len(plproperties_r) != 0
        if len(plproperties_r) > 1:
            print(42 * '-')
            print(f'WRN! Got multiple catalog entries for TIC{ticid}')
            print(42 * '-')
        plproperties_r = plproperties_r.iloc[0]

        for col in plproperties_r.index:
            if pd.isnull(plproperties_r[col]):
                plproperties_r[col] = -1

    else:
        plproperties_r = pd.DataFrame({v: -1
                                       for v in plkeyd.values()},
                                      index=[0])

    #
    # Get: nbhd_rating, init_priority, current_priority,
    # pending_spectroscopic_observations, pending_photometry_observations,
    # comment, candidate_provenance, isretired.
    #

    if isinstance(manual_dict, dict):

        # Dictionary was passed containing the manually entries that SHOULD be
        # manually written.

        d = manual_dict
        nbhd_rating = d['nbhd_rating']
        init_priority = d['init_priority']
        current_priority = d['current_priority']
        pending_spectroscopic_observations = d[
            'pending_spectroscopic_observations']
        pending_photometry_observations = d['pending_photometry_observations']
        comment = d['comment']
        candidate_provenance = d['candidate_provenance']
        isretired = d['isretired']

    else:

        # Set reasonable defaults. Raise warning.

        init_priority = 1
        nbhd_rating = init_priority
        current_priority = init_priority
        pending_spectroscopic_observations = ''
        pending_photometry_observations = ''
        comment = ''
        candidate_provenance = 'insert_candidate (w/out manual entries)'
        isretired = 0

        print(
            'WRN! For {}, did not get manual entries for PRIORITY, or COMMENT'.
            format(source_id))

    #
    # Construct and insert the new row.
    #

    refkey = 'reference' if 'reference' in cdips_r else 'reference_id'
    agekey = 'logt' if 'logt' in cdips_r else 'mean_age'

    new_row = pd.DataFrame(
        {
            'source_id': str(source_id),
            'ticid': str(ticid),
            'toi': str(toiid),
            'targetid': str(targetid),
            'iscdipstarget': iscdipstarget,
            'reference': cdips_r[refkey],
            'name': cdips_r.cluster,
            'age': cdips_r[agekey],
            'nbhd_rating': nbhd_rating if not pd.isnull(nbhd_rating) else '--',
            'init_priority': init_priority,
            'current_priority': current_priority,
            'pending_spectroscopic_observations':
            pending_spectroscopic_observations,
            'pending_photometry_observations': pending_photometry_observations,
            'comment': comment,
            'rp': plproperties_r[plkeyd['rp']],
            'rp_unc': plproperties_r[plkeyd['rp_unc']],
            'period': plproperties_r[plkeyd['period']],
            'depth': plproperties_r[plkeyd['depth']],
            'gaia_ra': cdips_r.ra,
            'gaia_dec': cdips_r.dec,
            'gaia_plx': cdips_r.parallax,
            'gaia_Gmag': cdips_r.phot_g_mean_mag,
            'gaia_Bmag': cdips_r.phot_bp_mean_mag,
            'gaia_Rmag': cdips_r.phot_rp_mean_mag,
            'tic_Bmag': tic_r.Bmag,
            'tic_Vmag': tic_r.Vmag,
            'tic_Jmag': tic_r.Jmag,
            'tic_Hmag': tic_r.Hmag,
            'tic_Kmag': tic_r.Kmag,
            'tic_Tmag': tic_r.Tmag,
            'tic_teff': tic_r.Teff if not pd.isnull(tic_r.Teff) else -1,
            'tic_logg': tic_r.logg,
            'tic_rstar': tic_r.rad,
            'tic_mstar': tic_r.mass,
            'candidate_provenance': candidate_provenance,
            'insert_time': pd.Timestamp.now(),
            'last_update_time': pd.Timestamp.now(),
            'isretired': isretired,
            'disposition': 'PC',
            'rot_quality': '--',
            'Prot': '--',
            'vsini': '--',
            'rot_amp': '--',
            'sig_Prot': '--',
            'Tdur': '--',
            'sig_Tdur': '--',
            'Mp_pred': '--',
            'K_orb': '--',
            'K_RM': '--',
            'K_orb/sig_Prot': '--',
            'K_RM/sig_Tdur': '--'
        },
        index=[0])

    cand_df = pd.read_csv(CAND_PATH, sep='|')

    if np.any(cand_df.source_id.astype(str).str.contains(str(source_id))):
        msg = ('Found existing candidates.csv entry for {}'.format(source_id))
        if raise_error_if_duplicate:
            raise AssertionError('ERR! : ' + msg)
        else:
            print('WRN! : ' + msg)
            print('WRN! Not doing the insert.')
            return None

    new_cand_df = pd.concat((cand_df, new_row), sort=False)

    new_cand_df = format_candidates_file(new_cand_df)
    save_candidates_csv_file(new_cand_df)
Exemple #7
0
from quicklooklc import quicklooklc
import numpy as np, pandas as pd
from astrobase.services.identifiers import gaiadr2_to_tic
from cdips_followup.paths import RESULTSDIR

df = pd.read_csv("/Users/luke/Dropbox/proj/cdips/data/cluster_data/"
                 "v07/Galli_2019_cut_confirmed_members_only.csv")

df['dr2_source_id'] = df['GaiaDR2'].str.replace('Gaia DR2 ', '')

basedir = os.path.join(RESULTSDIR, 'quicklooklc', "TAURUS")

for ix, r in df.iterrows():

    try:
        ticid = gaiadr2_to_tic(r.dr2_source_id)
        print(42 * '-')
        print(r.dr2_source_id, ticid)

        outdir = os.path.join(basedir, f'TIC{ticid}')
        if not os.path.exists(outdir):
            os.mkdir(outdir)
        else:
            print(f'found {outdir} skip')
            continue

        quicklooklc(
            ticid,
            cdips=0,
            outdir=outdir,
            spoc=1,
Exemple #8
0
def make_single_request_from_row(
        r, savstr, eventclass, ephem_dict=None,
        min_search_time=Time(dt.datetime.today().isoformat()),
        max_search_time=None, filtermode='ip', telescope_class=None,
        sites=None, ipp_value=1.0, force_acceptability=None
    ):
    #
    # require the passed dataframe row has the right format.
    #
    required_cols = ['source_id', 'period', 'epoch', 'duration']
    for _r in required_cols:
        if _r not in r:
            raise AssertionError(
                'need column {} in make_single_request_from_row'.format(_r)
            )
    suggested_cols = ['period_unc', 'epoch_unc', 'duration_unc',
                      'depth', 'depth_unc']
    for _s in suggested_cols:
        if _s not in r:
            r[_s] = None

    #
    # get identifier string. this is the TOI ID if it's available, otherwise
    # the TIC ID.
    #
    source_id = np.int64(r['source_id'])

    if 'toi_or_ticid' in r:
        identifier_str = r['toi_or_ticid']
    else:
        ticid = gaiadr2_to_tic(str(source_id))
        toiid = ticid_to_toiid(ticid)

        if isinstance(toiid, str):
            identifier_str = toiid
        else:
            identifier_str = 'TIC{}.01'.format(ticid)

    #
    # get gaia positions and PMs (the coordinates read in are slightly off)
    #
    jobstr = (
        "select top 1 g.ra, g.dec, g.pmra, g.pmdec, g.phot_g_mean_mag from "
        "gaiadr2.gaia_source as g where g.source_id = {:d}".
        format(source_id)
    )
    if DEBUG:
        print('Launching...\n{}'.format(jobstr))

    try:

        job = Gaia.launch_job(jobstr)
        gaia_r = job.get_results()

        if len(gaia_r) != 1:
            raise AssertionError('gaia match failed')

        ra, dec = float(gaia_r['ra']), float(gaia_r['dec'])
        pmra, pmdec = float(gaia_r['pmra']), float(gaia_r['pmdec'])
        phot_g_mean_mag = float(gaia_r['phot_g_mean_mag'])

    except AttributeError as e:

        print('Got AttributeError due to Gaia mirror timeout: {}'.
              format(repr(e)))

        gaia_r = gaia_objectid_search(source_id)
        df = pd.read_csv(gaia_r['result'])
        ra, dec = float(df['ra'].iloc[0]), float(df['dec'].iloc[0])
        pmra, pmdec = float(df['pmra'].iloc[0]), float(df['pmdec'].iloc[0])
        phot_g_mean_mag = float(df['phot_g_mean_mag'].iloc[0])

    #
    # shift by 42 arcseconds away from the center, in order to avoid CCD
    # amplifier lines.
    #
    c = SkyCoord(ra*u.deg, dec*u.deg, frame='icrs')

    shift_by = 42*u.arcsec # Bayliss shifted by ~30 arcsec. might as well further.
    shift_dir = 45*u.deg   # as long as it's some mix of "up" and "left"

    use_coord = c.directional_offset_by(shift_dir, shift_by)
    ra = use_coord.ra.value
    dec = use_coord.dec.value

    if telescope_class == '2m0':
        sites = ['Siding Spring Observatory', 'Haleakala Observatories']
    elif telescope_class == '1m0':
        sites = ['SAAO', 'Siding Spring Observatory', 'Cerro Tololo',
                 'McDonald Observatory', 'Wise Observatory']
    elif telescope_class == 'special':
        assert len(sites) >= 1
        pass
    else:
        raise ValueError

    if not isinstance(ephem_dict, dict):
        period, epoch, duration = r['period'], r['epoch'], r['duration']
    else:
        period, epoch, duration = (ephem_dict['period'],
                                   ephem_dict['epoch'],
                                   ephem_dict['duration'])

    this = get_requests_given_ephem(savstr, identifier_str,
                                    ra, dec, pmra, pmdec,
                                    phot_g_mean_mag, period,
                                    r['period_unc'], epoch,
                                    r['epoch_unc'], r['depth'],
                                    r['depth_unc'], duration,
                                    r['duration_unc'], sites=sites,
                                    min_search_time=min_search_time,
                                    max_search_time=max_search_time,
                                    eventclass=eventclass,
                                    filtermode=filtermode,
                                    telescope_class=telescope_class,
                                    ipp_value=ipp_value,
                                    force_acceptability=force_acceptability)

    return this
def test_gaiadrtwo2tic():

    assert gaiadr2_to_tic('6535499658122055552') == '402026209'