{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "674a745f-bace-43db-8a6f-851849399816",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-03T19:42:17.168174Z",
     "iopub.status.busy": "2024-12-03T19:42:17.167874Z",
     "iopub.status.idle": "2024-12-03T19:42:17.174423Z",
     "shell.execute_reply": "2024-12-03T19:42:17.173796Z",
     "shell.execute_reply.started": "2024-12-03T19:42:17.168154Z"
    }
   },
   "source": [
    "# Using SDSS-V Targeting Flags with `semaphore`\n",
    "\n",
    "## Learning Goals\n",
    "By the end of this tutorial, you will:\n",
    "- How to select spectra by their targeting information\n",
    "- How to get the targeting information of a specific target\n",
    "\n",
    "## Introduction\n",
    "The SDSS-V Survey uses a complex targeting schema, where targets are selected through many cartons associated with the different SDSS-V mappers and programs. \n",
    "\n",
    "In this short tutorial, we will briefly cover determining which targets in a summary file are associated with a particular carton, program, or master. It also describes how to determine the targeting information for a single source. \n",
    "\n",
    "## Imports\n",
    "\n",
    "This tutorial requires two commonly used python packages and two SDSS specific packages\n",
    "- numpy for filtering of arrays\n",
    "- astropy for reading fits tables\n",
    "- sdss-access for accessing data on the SAS\n",
    "- semaphore for exploring the flags\n",
    "\n",
    "Missing packages can be installed via `pip install numpy astropy sdss-access sdss-semaphore`\n",
    "\n",
    "\n",
    "NOTE: You may want to set the `$SAS_BASE_DIR` environment variable before starting this notebook, other wise `$HOME/sas` will be created and used\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d4717f7f-a615-446b-8062-7a19fe563701",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ['SDSSC2BV'] ='1' # to set the DR19 Carton to Bit Mapping Version\n",
    "\n",
    "import numpy as np\n",
    "from astropy.io import fits\n",
    "from sdss_access import Access\n",
    "from sdss_semaphore.targeting import TargetingFlags\n",
    "\n",
    "# setup sdss_access for DR19\n",
    "access = Access(release='DR19')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a358f90e-4919-485b-8dfc-d83bdbb70f92",
   "metadata": {},
   "source": [
    "## Load the Data\n",
    "We will use `sdss-access` to find/download the file. `sdss-access` has extensive [documentation](https://sdss-access.readthedocs.io/en/latest/), but we should mention a few things. First if you are a member of the collaboration accessing proprietary data, you need to set up a a [.netrc file](https://sdss-access.readthedocs.io/en/latest/auth.html). After that usage is the same for everyone.\n",
    "\n",
    "You need specify the file species you need, in this case 'mwmStar', then a series of key word arguments that vary depending on the file species. A list of file species and their keyword arugments is [available](https://sdss-access.readthedocs.io/en/latest/paths.html).\n",
    "\n",
    "The code below show an example of retreiving a summary file. access.full() builds the file path, access.exists() checks if the file exists, and access.commit() will retrieve the file and save it to your local SAS (`$SAS_BASE_DIR`) with the same structure as the SAS (so it will be many files deep)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "073c1b92-9875-4053-b9d2-fd91de16b179",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/jdonor/sas/sdsswork/mwm/spectro/astra/0.6.0/summary/mwmAllVisit-0.6.0.fits.gz\n"
     ]
    }
   ],
   "source": [
    "filename = access.full('mwmAllVisit',v_astra = '0.6.0')\n",
    "if not access.exists('',full=filename):\n",
    "    # if the file does not exist locally, this code will download the data.\n",
    "    access.remote()\n",
    "    access.add('mwmAllVisit',v_astra = '0.6.0')\n",
    "    access.set_stream()\n",
    "    access.commit()\n",
    "print(filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d11336e-a473-4f81-829d-68ea03bb7ef5",
   "metadata": {},
   "source": [
    "### Open the file\n",
    "Now use `astropy.fitsio` to load the data\n",
    "- HDU 1 contains the BOSS Spectra\n",
    "- HDU 2 contains the APOGEE Spectra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "006c5f44-0492-4347-a6ab-bf9d47161d91",
   "metadata": {},
   "outputs": [],
   "source": [
    "hdul = fits.open(filename)\n",
    "hdul[0].header\n",
    "boss_data = hdul[1].data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d781c79-a7da-4254-854e-55d9e75a1a08",
   "metadata": {},
   "source": [
    "### Converting the stored column to a set of Targeting Flags\n",
    "Now use the semaphore package to convert the fits column to Targeting Flags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bfe63802-18fe-452b-81c5-a7b675d55889",
   "metadata": {},
   "outputs": [],
   "source": [
    "flags = TargetingFlags(boss_data[\"SDSS5_TARGET_FLAGS\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a67f079c-546f-4431-8da7-b330e4ff523c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mwm_snc_100pc_0.1.0: 33542\n",
      "mwm_snc_250pc_0.1.0: 2019\n",
      "mwm_cb_300pc_0.1.0: 123\n",
      "mwm_cb_cvcandidates_0.1.0: 834\n",
      "mwm_halo_sm_0.1.0: 1642\n",
      "mwm_halo_bb_0.1.0: 31926\n",
      "mwm_yso_s1_0.1.0: 9514\n",
      "mwm_yso_s2_0.1.0: 44\n",
      "mwm_yso_s2-5_0.1.0: 3\n",
      "mwm_yso_s3_0.1.0: 15969\n",
      "mwm_yso_ob_0.1.0: 16\n",
      "mwm_yso_cluster_0.1.0: 16760\n",
      "mwm_rv_long-fps_0.1.0: 2507\n",
      "mwm_rv_long-bplates_0.1.0: 586\n",
      "mwm_ob_cepheids_0.1.0: 128\n",
      "mwm_rv_short-fps_0.1.0: 7162\n",
      "mwm_rv_short-bplates_0.1.0: 847\n",
      "mwm_ob_core_0.1.0: 101779\n",
      "mwm_rv_short-rm_0.1.0: 153\n",
      "mwm_rv_long-rm_0.1.0: 53\n",
      "ops_std_boss_0.1.0: 132780\n",
      "mwm_wd_core_0.1.0: 36240\n",
      "mwm_gg_core_0.1.0: 5264\n",
      "mwm_planet_tess_0.1.0: 4783\n",
      "bhm_aqmes_med_0.1.0: 1691\n",
      "mwm_cb_gaiagalex_0.1.0: 42430\n",
      "bhm_aqmes_med-faint_0.1.0: 3354\n",
      "mwm_tessrgb_core_0.1.0: 4707\n",
      "bhm_aqmes_wide3_0.1.0: 1718\n",
      "bhm_aqmes_wide3-faint_0.1.0: 2550\n",
      "bhm_aqmes_wide2_0.1.0: 1109\n",
      "bhm_aqmes_wide2-faint_0.1.0: 736\n",
      "bhm_aqmes_bonus-dark_0.1.0: 16725\n",
      "bhm_aqmes_bonus-bright_0.1.0: 1703\n",
      "bhm_csc_boss-dark_0.1.0: 38684\n",
      "bhm_csc_boss-bright_0.1.0: 3355\n",
      "bhm_csc_apogee_0.1.0: 247\n",
      "bhm_gua_dark_0.1.0: 9214\n",
      "bhm_gua_bright_0.1.0: 2404\n",
      "ops_std_eboss_0.1.0: 72758\n",
      "bhm_rm_core_0.1.0: 14296\n",
      "bhm_rm_known-spec_0.1.0: 27447\n",
      "bhm_rm_var_0.1.0: 5352\n",
      "bhm_rm_ancillary_0.1.0: 6669\n",
      "ops_std_boss-red_0.1.0: 12666\n",
      "bhm_spiders_agn-efeds_0.1.0: 786\n",
      "mwm_cb_uvex1_0.1.0: 20115\n",
      "mwm_cb_uvex2_0.1.0: 43381\n",
      "mwm_dust_core_0.1.0: 39\n",
      "mwm_cb_uvex3_0.1.0: 130\n",
      "mwm_cb_uvex4_0.1.0: 774\n",
      "mwm_cb_uvex5_0.1.0: 1133\n",
      "mwm_legacy_ir2opt_0.1.1: 21330\n",
      "ops_apogee_stds_0.1.2: 5070\n",
      "ops_std_boss_tic_0.1.3: 92123\n",
      "mwm_rv_long_bplates_0.1.4: 774\n",
      "ops_std_eboss_0.5.0: 72720\n",
      "ops_std_boss_0.5.0: 125936\n",
      "ops_std_boss_red_0.5.0: 12666\n",
      "ops_std_apogee_0.5.0: 5071\n",
      "mwm_cb_gaiagalex_apogee_0.5.0: 126\n",
      "mwm_cb_gaiagalex_boss_0.5.0: 42438\n",
      "mwm_cb_cvcandidates_boss_0.5.0: 834\n",
      "mwm_galactic_core_0.5.0: 5270\n",
      "mwm_cb_uvex3_0.5.0: 788\n",
      "mwm_cb_uvex4_0.5.0: 1189\n",
      "mwm_snc_100pc_boss_0.5.0: 33562\n",
      "mwm_snc_250pc_apogee_0.5.0: 1988\n",
      "mwm_snc_250pc_boss_0.5.0: 2019\n",
      "mwm_halo_sm_boss_0.5.0: 1640\n",
      "mwm_yso_disk_apogee_0.5.0: 9514\n",
      "mwm_yso_disk_boss_0.5.0: 12320\n",
      "mwm_yso_embedded_apogee_0.5.0: 45\n",
      "mwm_yso_nebula_apogee_0.5.0: 3\n",
      "mwm_yso_variable_apogee_0.5.0: 15975\n",
      "mwm_yso_variable_boss_0.5.0: 15599\n",
      "mwm_yso_ob_apogee_0.5.0: 16\n",
      "mwm_yso_ob_boss_0.5.0: 16\n",
      "mwm_yso_cmz_apogee_0.5.0: 244\n",
      "mwm_yso_cluster_apogee_0.5.0: 16763\n",
      "mwm_yso_cluster_boss_0.5.0: 22482\n",
      "mwm_rv_short_fps_0.5.0: 7163\n",
      "mwm_legacy_ir2opt_0.5.0: 21340\n",
      "mwm_dust_core_0.5.0: 36\n",
      "mwm_wd_core_0.5.0: 36240\n",
      "mwm_tess_planet_0.5.0: 5239\n",
      "mwm_snc_100pc_apogee_0.5.0: 1821\n",
      "mwm_cb_uvex1_0.5.0: 19771\n",
      "mwm_cb_uvex2_0.5.0: 43297\n",
      "mwm_tessrgb_core_0.5.0: 4707\n",
      "ops_std_boss_tic_0.5.0: 92133\n",
      "mwm_rv_long_fps_0.5.0: 2507\n",
      "mwm_halo_bb_boss_0.5.0: 31503\n",
      "mwm_cb_300pc_apogee_0.5.0: 96\n",
      "mwm_cb_300pc_boss_0.5.0: 123\n",
      "bhm_spiders_agn_gaiadr2_0.5.0: 5669\n",
      "bhm_spiders_agn_lsdr8_0.5.0: 3349\n",
      "bhm_spiders_agn_efeds_stragglers_0.5.0: 703\n",
      "bhm_spiders_agn_ps1dr2_0.5.0: 5845\n",
      "bhm_spiders_agn_skymapperdr2_0.5.0: 1588\n",
      "bhm_spiders_agn_supercosmos_0.5.0: 1879\n",
      "bhm_spiders_clusters_ps1dr2_0.5.0: 3\n",
      "bhm_gua_bright_0.5.0: 5055\n",
      "bhm_gua_dark_0.5.0: 19733\n",
      "mwm_cb_uvex5_0.5.0: 1133\n",
      "bhm_rm_known_spec_0.5.0: 27917\n",
      "bhm_rm_core_0.5.0: 14487\n",
      "bhm_rm_var_0.5.0: 5240\n",
      "bhm_rm_ancillary_0.5.0: 6667\n",
      "ops_std_boss_lsdr8_0.5.0: 50495\n",
      "ops_std_boss_ps1dr2_0.5.0: 124139\n",
      "ops_std_boss_gdr2_0.5.0: 122551\n",
      "bhm_aqmes_med_0.5.0: 1793\n",
      "bhm_aqmes_med_faint_0.5.0: 3826\n",
      "bhm_csc_boss_dark_0.5.0: 40560\n",
      "bhm_csc_boss_bright_0.5.0: 3546\n",
      "bhm_csc_apogee_0.5.0: 364\n",
      "mwm_yso_pms_apogee_0.5.1: 17838\n",
      "mwm_yso_pms_boss_0.5.1: 18395\n",
      "mwm_ob_core_0.5.1: 102006\n",
      "mwm_ob_cepheids_0.5.1: 129\n",
      "mwm_halo_bb_boss_0.5.1: 31576\n",
      "mwm_halo_sm_boss_0.5.1: 1642\n",
      "mwm_erosita_stars_0.5.2: 3015\n",
      "mwm_erosita_compact_gen_0.5.2: 955\n",
      "mwm_erosita_compact_var_0.5.2: 444\n",
      "openfibertargets_nov2020_25_0.5.2-test: 23078\n",
      "openfibertargets_nov2020_28a_0.5.2-test: 2027\n",
      "openfibertargets_nov2020_28b_0.5.2-test: 3129\n",
      "openfibertargets_nov2020_28c_0.5.2-test: 2172\n",
      "openfibertargets_nov2020_35a_0.5.2-test: 11742\n",
      "openfibertargets_nov2020_35b_0.5.2-test: 577\n",
      "openfibertargets_nov2020_5_0.5.2-test: 689\n",
      "openfibertargets_nov2020_6b_0.5.2-test: 3691\n",
      "openfibertargets_nov2020_6c_0.5.2-test: 17927\n",
      "openfibertargets_nov2020_10_0.5.3: 42\n",
      "openfibertargets_nov2020_1000_0.5.3: 33\n",
      "openfibertargets_nov2020_1001a_0.5.3: 2673\n",
      "openfibertargets_nov2020_1001b_0.5.3: 154\n",
      "openfibertargets_nov2020_11_0.5.3: 3344\n",
      "openfibertargets_nov2020_12_0.5.3: 2191\n",
      "openfibertargets_nov2020_14_0.5.3: 759\n",
      "openfibertargets_nov2020_15_0.5.3: 4317\n",
      "openfibertargets_nov2020_17_0.5.3: 5235\n",
      "openfibertargets_nov2020_18_0.5.3: 2246\n",
      "openfibertargets_nov2020_22_0.5.3: 258\n",
      "openfibertargets_nov2020_24_0.5.3: 27351\n",
      "openfibertargets_nov2020_25_0.5.3: 23078\n",
      "openfibertargets_nov2020_26_0.5.3: 190\n",
      "openfibertargets_nov2020_27_0.5.3: 18453\n",
      "openfibertargets_nov2020_28a_0.5.3: 2027\n",
      "openfibertargets_nov2020_28b_0.5.3: 3129\n",
      "openfibertargets_nov2020_28c_0.5.3: 2172\n",
      "openfibertargets_nov2020_29_0.5.3: 199\n",
      "openfibertargets_nov2020_3_0.5.3: 668\n",
      "openfibertargets_nov2020_30_0.5.3: 6\n",
      "openfibertargets_nov2020_31_0.5.3: 16\n",
      "openfibertargets_nov2020_32_0.5.3: 3019\n",
      "openfibertargets_nov2020_33_0.5.3: 568\n",
      "openfibertargets_nov2020_35a_0.5.3: 11742\n",
      "openfibertargets_nov2020_35b_0.5.3: 577\n",
      "openfibertargets_nov2020_35c_0.5.3: 1163\n",
      "openfibertargets_nov2020_46_0.5.3: 28610\n",
      "openfibertargets_nov2020_8_0.5.3: 10619\n",
      "openfibertargets_nov2020_9_0.5.3: 7742\n",
      "openfibertargets_nov2020_19a_0.5.3: 7537\n",
      "openfibertargets_nov2020_19b_0.5.3: 18\n",
      "openfibertargets_nov2020_19c_0.5.3: 46\n",
      "manual_nsbh_apogee_0.5.3: 10\n",
      "manual_nsbh_boss_0.5.3: 27\n",
      "manual_bhm_spiders_comm_0.5.3: 512\n",
      "bhm_aqmes_bonus_bright_0.5.4: 1848\n",
      "bhm_aqmes_bonus_core_0.5.4: 8421\n",
      "bhm_aqmes_bonus_faint_0.5.4: 20471\n",
      "bhm_aqmes_wide2_0.5.4: 2875\n",
      "bhm_aqmes_wide2_faint_0.5.4: 3879\n",
      "mwm_cb_300pc_boss_0.5.4: 18525\n",
      "openfibertargets_nov2020_5_0.5.3: 689\n",
      "openfibertargets_nov2020_6a_0.5.3: 812\n",
      "openfibertargets_nov2020_6b_0.5.3: 3728\n",
      "openfibertargets_nov2020_6c_0.5.3: 18156\n",
      "manual_bright_target_offsets_1_0.5.3: 1667\n",
      "manual_bright_target_offsets_1_g13_0.5.3: 1222\n",
      "manual_bright_targets_g13_0.5.3: 1222\n",
      "manual_bright_targets_g13_offset_fixed_1_0.5.3: 1222\n",
      "manual_bright_targets_g13_offset_fixed_3_0.5.3: 1222\n",
      "manual_bright_targets_g13_offset_fixed_5_0.5.3: 1222\n",
      "manual_bright_targets_g13_offset_fixed_7_0.5.3: 1222\n",
      "manual_fps_position_stars_0.5.3: 13591\n",
      "manual_fps_position_stars_10_0.5.3: 53958\n",
      "openfibertargets_nov2020_34a_0.5.13: 213184\n",
      "openfibertargets_nov2020_34b_0.5.13: 13240\n",
      "bhm_csc_boss_0.5.14: 47318\n",
      "bhm_csc_boss_0.5.15: 47045\n",
      "bhm_csc_apogee_0.5.15: 2363\n",
      "bhm_colr_galaxies_lsdr8_0.5.16: 2395\n",
      "mwm_yso_cluster_apogee_0.5.17: 16763\n",
      "mwm_yso_cluster_boss_0.5.17: 22482\n",
      "mwm_yso_cmz_apogee_0.5.17: 244\n",
      "mwm_yso_disk_apogee_0.5.17: 9514\n",
      "mwm_yso_disk_boss_0.5.17: 12320\n",
      "mwm_yso_embedded_apogee_0.5.17: 45\n",
      "mwm_yso_nebula_apogee_0.5.17: 3\n",
      "mwm_yso_variable_apogee_0.5.17: 15975\n",
      "mwm_yso_variable_boss_0.5.17: 15599\n",
      "mwm_rv_long_fps_0.5.18: 2507\n",
      "mwm_rv_short_fps_0.5.18: 7163\n",
      "mwm_cb_300pc_apogee_0.5.19: 72\n",
      "mwm_cb_300pc_boss_0.5.19: 7140\n",
      "manual_fps_position_stars_apogee_10_0.5.13: 53958\n",
      "manual_offset_mwmhalo_off00_0.5.13: 148\n",
      "manual_offset_mwmhalo_off05_0.5.13: 148\n",
      "manual_offset_mwmhalo_off10_0.5.13: 148\n",
      "manual_offset_mwmhalo_off20_0.5.13: 148\n",
      "manual_offset_mwmhalo_off30_0.5.13: 148\n",
      "manual_offset_mwmhalo_offa_0.5.13: 148\n",
      "manual_offset_mwmhalo_offb_0.5.13: 148\n",
      "manual_bright_target_offsets_3_0.5.20: 1667\n",
      "ops_std_apogee_0.5.22: 6412\n",
      "manual_bhm_spiders_comm_lco_0.5.23: 2439\n",
      "ops_std_boss_lsdr10_1.0.5: 49174\n",
      "mwm_yso_cluster_apogee_1.0.6: 16763\n",
      "mwm_yso_cluster_boss_1.0.6: 22482\n",
      "mwm_yso_disk_apogee_1.0.6: 8735\n",
      "mwm_yso_disk_boss_1.0.6: 11542\n",
      "mwm_yso_cmz_apogee_1.0.6: 195\n",
      "mwm_yso_variable_apogee_1.0.6: 10185\n",
      "mwm_yso_variable_boss_1.0.6: 10015\n",
      "mwm_yso_embedded_apogee_1.0.6: 45\n",
      "mwm_yso_pms_apogee_zari18pms_1.0.6: 6567\n",
      "mwm_yso_pms_boss_sagitta_edr3_1.0.6: 14108\n",
      "mwm_yso_pms_boss_zari18pms_1.0.6: 6596\n",
      "openfibertargets_nov2020_11_1.0.7: 3344\n",
      "openfibertargets_nov2020_18_1.0.7: 2246\n",
      "openfibertargets_nov2020_26_1.0.7: 190\n",
      "openfibertargets_nov2020_27_1.0.7: 18453\n",
      "openfibertargets_nov2020_30_1.0.7: 6\n",
      "mwm_yso_nebula_apogee_1.0.8: 3\n",
      "manual_mwm_halo_distant_kgiant_1.0.7: 2645\n",
      "manual_mwm_halo_mp_bbb_1.0.7: 1526\n",
      "openfibertargets_nov2020_10_1.0.7: 42\n",
      "openfibertargets_nov2020_1000_1.0.7: 33\n",
      "openfibertargets_nov2020_12_1.0.7: 2191\n",
      "openfibertargets_nov2020_14_1.0.7: 759\n",
      "openfibertargets_nov2020_15_1.0.7: 4317\n",
      "openfibertargets_nov2020_22_1.0.7: 258\n",
      "openfibertargets_nov2020_24_1.0.7: 27351\n",
      "openfibertargets_nov2020_29_1.0.7: 199\n",
      "openfibertargets_nov2020_3_1.0.7: 668\n",
      "openfibertargets_nov2020_31_1.0.7: 16\n",
      "openfibertargets_nov2020_34a_1.0.7: 213190\n",
      "openfibertargets_nov2020_34b_1.0.7: 13240\n",
      "openfibertargets_nov2020_35b_1.0.7: 577\n",
      "openfibertargets_nov2020_35c_1.0.7: 1163\n",
      "openfibertargets_nov2020_46_1.0.7: 28610\n",
      "openfibertargets_nov2020_5_1.0.7: 689\n",
      "openfibertargets_nov2020_6a_1.0.7: 812\n",
      "openfibertargets_nov2020_6b_1.0.7: 3728\n",
      "openfibertargets_nov2020_6c_1.0.7: 18156\n",
      "openfibertargets_nov2020_8_1.0.7: 10619\n",
      "openfibertargets_nov2020_9_1.0.7: 7742\n",
      "openfibertargets_nov2020_28a_1.0.7: 2027\n",
      "openfibertargets_nov2020_33_1.0.10: 568\n",
      "bhm_rm_core_1.0.12: 14421\n",
      "bhm_rm_known_spec_1.0.12: 28053\n",
      "bhm_rm_var_1.0.12: 5240\n",
      "bhm_rm_ancillary_1.0.12: 6667\n",
      "ops_std_boss_gdr2_1.0.13: 122551\n",
      "ops_std_apogee_1.0.14: 6413\n",
      "ops_std_eboss_1.0.15: 72677\n",
      "ops_std_boss_1.0.16: 125936\n",
      "ops_std_boss_red_1.0.16: 12666\n",
      "ops_std_boss_tic_1.0.16: 90586\n",
      "mwm_snc_100pc_apogee_1.0.17: 1962\n",
      "mwm_snc_100pc_boss_1.0.17: 30087\n",
      "mwm_bin_gaia_astb_apogee_1.0.19: 253\n",
      "mwm_bin_gaia_astb_boss_1.0.19: 1357\n",
      "mwm_bin_gaia_sb_apogee_1.0.19: 99\n",
      "mwm_bin_gaia_sb_boss_1.0.19: 407\n",
      "mwm_yso_pms_apogee_sagitta_edr3_1.0.20: 14084\n",
      "mwm_snc_100pc_boss_1.0.24: 30087\n",
      "mwm_snc_100pc_boss_1.0.28: 30087\n",
      "mwm_yso_disk_apogee_single_1.0.33: 8735\n",
      "mwm_yso_disk_boss_single_1.0.33: 11542\n",
      "mwm_yso_embedded_apogee_single_1.0.33: 45\n",
      "mwm_yso_nebula_apogee_single_1.0.33: 3\n",
      "mwm_yso_variable_apogee_single_1.0.33: 10185\n",
      "mwm_yso_variable_boss_single_1.0.33: 10015\n",
      "mwm_yso_cmz_apogee_single_1.0.33: 195\n",
      "mwm_yso_cluster_apogee_single_1.0.33: 16763\n",
      "mwm_yso_cluster_boss_single_1.0.33: 22482\n",
      "mwm_yso_pms_apogee_sagitta_edr3_single_1.0.33: 14084\n",
      "mwm_yso_pms_apogee_zari18pms_single_1.0.33: 6567\n",
      "mwm_yso_pms_boss_sagitta_edr3_single_1.0.33: 14108\n",
      "mwm_yso_pms_boss_zari18pms_single_1.0.33: 6596\n",
      "ops_std_boss_ps1dr2_1.0.29: 124124\n",
      "bhm_csc_boss_1.0.37: 33430\n",
      "bhm_csc_apogee_1.0.37: 2507\n",
      "bhm_gua_dark_1.0.37: 31205\n",
      "bhm_gua_bright_1.0.37: 7314\n",
      "bhm_spiders_clusters_lsdr10_1.0.37: 11\n",
      "bhm_spiders_agn_lsdr10_1.0.37: 8996\n",
      "bhm_spiders_agn_hard_1.0.37: 27\n",
      "bhm_spiders_agn_gaiadr3_1.0.37: 10729\n",
      "bhm_spiders_agn_tda_1.0.37: 136\n",
      "bhm_aqmes_med_1.0.37: 1786\n",
      "bhm_aqmes_med_faint_1.0.37: 3824\n",
      "bhm_aqmes_wide2_1.0.37: 2876\n",
      "bhm_aqmes_wide2_faint_1.0.37: 3879\n",
      "bhm_aqmes_bonus_core_1.0.37: 8416\n",
      "bhm_aqmes_bonus_bright_1.0.37: 1842\n",
      "bhm_aqmes_bonus_faint_1.0.37: 20469\n",
      "manual_mwm_crosscalib_apogee_1.0.35: 20\n",
      "manual_mwm_crosscalib_yso_apogee_1.0.35: 459\n",
      "manual_mwm_crosscalib_yso_boss_1.0.35: 459\n",
      "manual_mwm_halo_distant_bhb_boss_1.0.35: 3116\n",
      "manual_mwm_halo_distant_bhb_boss_single_1.0.35: 3116\n",
      "manual_mwm_halo_distant_kgiant_far_boss_1.0.35: 795\n",
      "manual_mwm_halo_distant_kgiant_far_boss_single_1.0.35: 795\n",
      "manual_mwm_halo_distant_kgiant_near_boss_1.0.35: 1850\n",
      "manual_mwm_halo_distant_kgiant_near_boss_single_1.0.35: 1850\n",
      "manual_mwm_halo_mp_wise_apogee_1.0.35: 42\n",
      "manual_mwm_halo_mp_wise_apogee_single_1.0.35: 42\n",
      "manual_mwm_halo_mp_wise_boss_1.0.35: 919\n",
      "manual_mwm_halo_mp_wise_boss_single_1.0.35: 919\n",
      "manual_mwm_halo_vmp_wise_apogee_1.0.35: 37\n",
      "manual_mwm_halo_vmp_wise_apogee_single_1.0.35: 37\n",
      "manual_mwm_halo_vmp_wise_boss_1.0.35: 528\n",
      "manual_mwm_halo_vmp_wise_boss_single_1.0.35: 528\n",
      "bhm_colr_galaxies_lsdr10_1.0.38: 1862\n",
      "manual_mwm_validation_cool_apogee_1.0.35: 24\n",
      "manual_mwm_validation_cool_boss_1.0.35: 1907\n",
      "manual_mwm_validation_hot_boss_1.0.35: 9\n",
      "mwm_ob_core_boss_1.0.39: 12305\n",
      "mwm_ob_core_boss_single_1.0.39: 12305\n",
      "mwm_ob_cepheids_boss_1.0.39: 129\n",
      "mwm_bin_rv_long_apogee_1.0.39: 513\n",
      "mwm_bin_rv_short_mdwarf_apogee_18epoch_1.0.39: 8341\n",
      "mwm_bin_rv_short_mdwarf_apogee_12epoch_1.0.39: 8341\n",
      "mwm_bin_rv_short_mdwarf_apogee_08epoch_1.0.39: 8341\n",
      "mwm_bin_rv_short_subgiant_apogee_1.0.39: 2406\n",
      "mwm_bin_rv_short_rgb_apogee_1.0.39: 1352\n",
      "mwm_halo_distant_rrl_boss_single_1.0.39: 2311\n",
      "mwm_halo_distant_rrl_boss_1.0.39: 2311\n",
      "mwm_halo_vmp_xp_boss_single_1.0.39: 864\n",
      "mwm_halo_nmp_xp_boss_single_1.0.39: 3511\n",
      "mwm_halo_vmp_xp_apogee_single_1.0.39: 23\n",
      "mwm_halo_mp_xp_apogee_single_1.0.39: 70\n",
      "mwm_halo_nmp_xp_apogee_single_1.0.39: 132\n",
      "mwm_halo_vmp_xp_boss_1.0.39: 864\n",
      "mwm_halo_mp_xp_boss_1.0.39: 1888\n",
      "mwm_halo_nmp_xp_boss_1.0.39: 3511\n",
      "mwm_halo_vmp_xp_apogee_1.0.39: 23\n",
      "mwm_halo_mp_xp_apogee_1.0.39: 70\n",
      "mwm_halo_nmp_xp_apogee_1.0.39: 132\n",
      "mwm_halo_local_high_apogee_single_1.0.39: 166\n",
      "mwm_halo_local_high_boss_single_1.0.39: 26104\n",
      "mwm_halo_local_low_boss_single_1.0.39: 3076\n",
      "mwm_halo_local_high_apogee_1.0.39: 166\n",
      "mwm_halo_local_high_boss_1.0.39: 26104\n",
      "mwm_halo_local_low_boss_1.0.39: 3076\n",
      "mwm_erosita_stars_boss_1.0.40: 6242\n",
      "mwm_erosita_compact_boss_1.0.40: 311\n",
      "mwm_erosita_compact_boss_shallow_1.0.40: 311\n",
      "mwm_halo_mp_xp_boss_single_1.0.41: 1888\n",
      "mwm_snc_ext_main_apogee_1.0.41: 475\n",
      "mwm_snc_ext_filler_apogee_1.0.41: 6928\n",
      "mwm_snc_ext_main_boss_1.0.41: 4893\n",
      "mwm_snc_ext_filler_boss_1.0.41: 92955\n",
      "mwm_monitor_n188_apogee_long_1.0.42: 5\n",
      "mwm_monitor_m67_apogee_short_1.0.42: 17\n",
      "mwm_wd_pwd_boss_1.0.42: 36521\n",
      "mwm_wd_gaia_boss_1.0.42: 33650\n",
      "mwm_cb_galex_mag_boss_1.0.42: 26435\n",
      "mwm_cb_galex_vol_boss_1.0.42: 38554\n",
      "mwm_cb_xmmom_boss_1.0.42: 649\n",
      "mwm_cb_swiftuvot_boss_1.0.42: 573\n",
      "mwm_astar_core_boss_1.0.43: 47827\n",
      "mwm_astar_core_boss_single_1.0.43: 47827\n",
      "mwm_bin_vis_apogee_1.0.44: 829\n",
      "mwm_bin_vis_boss_1.0.44: 16893\n",
      "mwm_galactic_core_dist_apogee_1.0.44: 3647\n",
      "mwm_dust_core_dist_apogee_1.0.45: 42\n",
      "manual_mwm_nsbh_apogee_1.0.46: 10\n",
      "manual_mwm_nsbh_boss_1.0.46: 27\n",
      "mwm_cb_cvcandidates_boss_1.0.47: 834\n",
      "mwm_legacy_ir2opt_boss_1.0.47: 24505\n",
      "bhm_rm_core_1.0.48: 14362\n",
      "bhm_rm_known_spec_1.0.48: 28034\n",
      "bhm_rm_var_1.0.48: 5240\n",
      "bhm_rm_ancillary_1.0.48: 6667\n",
      "mwm_tess_2min_apogee_1.0.49: 6869\n",
      "mwm_tess_rgb_apogee_1.0.49: 8060\n",
      "manual_mwm_planet_gaia_astrometry_apogee_1.0.46: 4417\n",
      "manual_mwm_planet_known_apogee_1.0.46: 10\n",
      "manual_mwm_planet_tess_eb_apogee_1.0.46: 98\n",
      "manual_mwm_planet_tess_pc_apogee_1.0.46: 74\n"
     ]
    }
   ],
   "source": [
    "# Let's count the number of targets assigned to each carton (and skip empty cartons)\n",
    "for label, count in flags.count(skip_empty=True).items():\n",
    "    print(f\"{label}: {count}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb80bcc3-b8fd-4172-84a8-dc823e7532ca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-03T18:52:35.910511Z",
     "iopub.status.busy": "2024-12-03T18:52:35.910045Z",
     "iopub.status.idle": "2024-12-03T18:52:35.916475Z",
     "shell.execute_reply": "2024-12-03T18:52:35.915853Z",
     "shell.execute_reply.started": "2024-12-03T18:52:35.910492Z"
    }
   },
   "source": [
    "# Flags have attributes\n",
    "\n",
    "The counts above are for targets assigned to a specific carton, with a specific version. Sometimes we don't care about the carton version and we just want to count by the effective number of targets assigned to that carton (over all versions), or we might want to count by the number of targets assigned to each mapper, or some other attribute.\n",
    "\n",
    "The `TargetingFlags.mapping` attribute contains a reference dictionary for all cartons, including their attributes. Here's how we can count targets by those attributes:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ac783161-1840-40e9-af23-7263a6db464d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "na: 0\n",
      "mwm: 460,377\n",
      "ops: 325,569\n",
      "bhm: 65,114\n",
      "open: 363,471\n"
     ]
    }
   ],
   "source": [
    "for mapper, count in flags.count_by_attribute(\"mapper\").items():\n",
    "    print(f\"{mapper}: {count:,}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c3fe2de3-3ab3-44c3-ac2a-a8ea9981fb10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "na: 0\n",
      "mwm_snc: 131,249\n",
      "mwm_cb: 83,708\n",
      "mwm_halo: 73,865\n",
      "mwm_yso: 59,667\n",
      "mwm_rv: 10,209\n",
      "mwm_ob: 102,320\n",
      "ops_std: 281,742\n",
      "mwm_wd: 37,834\n",
      "mwm_gg: 5,264\n",
      "mwm_planet: 10,269\n",
      "bhm_aqmes: 4,337\n",
      "bhm_filler: 41,024\n",
      "mwm_tessrgb: 8,065\n",
      "bhm_csc: 52,291\n",
      "ops_sky: 0\n",
      "bhm_rm: 29,196\n",
      "bhm_spiders: 13,190\n",
      "mwm_dust: 81\n",
      "mwm_legacy: 24,789\n",
      "mwm_galactic: 6,336\n",
      "mwm_filler: 50,052\n",
      "mwm_tess_ob: 0\n",
      "mwm_erosita: 8,323\n",
      "open_fiber: 363,471\n",
      "commissioning: 58,684\n",
      "SKY: 0\n",
      "mwm_tessob: 0\n",
      "mwm_magcloud: 0\n",
      "mwm_bin: 14,175\n",
      "mwm_validation: 2,413\n",
      "mwm_monitor: 22\n"
     ]
    }
   ],
   "source": [
    "for program, count in flags.count_by_attribute(\"program\").items():\n",
    "    print(f\"{program}: {count:,}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bad159f7-ae21-41d3-ab49-9679c07f9363",
   "metadata": {},
   "source": [
    "# I own a carton. Where are my spectra?\n",
    "\n",
    "You can efficiently construct boolean masks to select spectra that are assigned to a carton, program, mapper, or any other attribute.\n",
    "\n",
    "The easiest way to do this is with the following methods:\n",
    "- `TargetingFlags.in_carton_pk`: If you know the database primary key (`targetdb.carton.pk`) of your carton\n",
    "- `TargetingFlags.in_carton_name`: If you know the name of your carton.\n",
    "- `TargetingFlags.in_carton_label`: If you know the name and version (name_ver) of your carton\n",
    "- `TargetingFlags.in_mapper`: To select by mapper (e.g., BHM, MWM)\n",
    "- `TargetingFlags.in_program`: To select by program name.\n",
    "- `TargetingFlags.in_alt_name`: To select by alternative name, curated with thanks by Marina Koukel.\n",
    "- `TargetingFlags.in_alt_program`: To select by alternative program name, curated with thanks by Marina Koukel.\n",
    "\n",
    "If you're not sure what your carton's alternative name or program is, you might find these properties useful:\n",
    "\n",
    "- `TargetingFlags.all_mappers`: a tuple of all unique mapper names;\n",
    "- `TargetingFlags.all_programs`: a tuple of all unique program names;\n",
    "- `TargetingFlags.all_carton_names`: a tuple of all unique carton names;\n",
    "- `TargetingFlags.all_alt_carton_names`: a tuple of all unique alternative names;\n",
    "- `TargetingFlags.all_alt_programs`: a tuple of all unique alternative program names;\n",
    "\n",
    "or you can look this up from `TargetingFlags.mapping`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b4ab29a1-e204-491c-a0e1-098a9f50190c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mappers:\n",
      "\tna\n",
      "\tbhm\n",
      "\tmwm\n",
      "\tops\n",
      "\topen\n",
      "Programs\n",
      "\tmwm_filler\n",
      "\tmwm_tessob\n",
      "\tmwm_magcloud\n",
      "\topen_fiber\n",
      "\tmwm_legacy\n",
      "\tSKY\n",
      "\tmwm_validation\n",
      "\tmwm_erosita\n",
      "\tmwm_tessrgb\n",
      "\tna\n",
      "\tbhm_rm\n",
      "\tmwm_halo\n",
      "\tmwm_yso\n",
      "\tmwm_monitor\n",
      "\tcommissioning\n",
      "\tmwm_snc\n",
      "\tmwm_tess_ob\n",
      "\tmwm_planet\n",
      "\tmwm_gg\n",
      "\tmwm_bin\n",
      "\tmwm_rv\n",
      "\tbhm_filler\n",
      "\tmwm_cb\n",
      "\tbhm_spiders\n",
      "\tbhm_csc\n",
      "\tmwm_galactic\n",
      "\tops_std\n",
      "\tbhm_aqmes\n",
      "\tmwm_dust\n",
      "\tmwm_ob\n",
      "\tmwm_wd\n",
      "\tops_sky\n",
      "Cartons names\n",
      "\tops_apogee_stds\n",
      "\tmwm_yso_ob\n",
      "\tbhm_spiders_agn_gaiadr2\n",
      "\tmwm_snc_100pc\n",
      "\tmwm_yso_s2-5\n",
      "\tmwm_yso_pms_apogee_sagitta_edr3_single\n",
      "\tmwm_halo_sm\n",
      "\tmwm_bin_rv_short_mdwarf_apogee_08epoch\n",
      "\tbhm_aqmes_bonus-bright\n",
      "\tmwm_cb_gaiagalex_apogee\n",
      "\topenfibertargets_nov2020_26\n",
      "\tmwm_monitor_n188_apogee_long\n",
      "\tmwm_astar_core_boss\n",
      "\tmwm_yso_cluster_boss\n",
      "\tops_sky_boss_best\n",
      "\tmwm_erosita_compact_boss\n",
      "\tmanual_offset_mwmhalo_offa\n",
      "\tmanual_bright_target_offsets_3\n",
      "\tmanual_mwm_halo_distant_kgiant_far_boss_single\n",
      "\tmwm_rv_long-fps\n",
      "\tmwm_magcloud_agb_apogee\n",
      "\topenfibertargets_nov2020_17\n",
      "\tops_sky_boss\n",
      "\tmwm_legacy_ir2opt\n",
      "\tmwm_ob_cepheids_boss\n",
      "\tops_std_boss_red\n",
      "\tmanual_mwm_crosscalib_apogee\n",
      "\topenfibertargets_nov2020_35c\n",
      "\topenfibertargets_nov2020_33\n",
      "\tmanual_mwm_halo_distant_kgiant_far_boss\n",
      "\tmwm_cb_cvcandidates_boss\n",
      "\tops_std_boss_ps1dr2\n",
      "\tbhm_rm_known_spec\n",
      "\tmwm_rv_long-rm\n",
      "\tmwm_rv_short-rm\n",
      "\tmwm_yso_cmz_apogee_single\n",
      "\tmwm_snc_250pc_boss\n",
      "\tmwm_yso_disk_boss\n",
      "\tmwm_bin_rv_short_mdwarf_apogee_12epoch\n",
      "\tops_std_boss-red\n",
      "\topenfibertargets_nov2020_25\n",
      "\tmwm_bin_rv_short_rgb_apogee\n",
      "\tbhm_aqmes_wide3-faint\n",
      "\tbhm_spiders_clusters_efeds_stragglers\n",
      "\topenfibertargets_nov2020_24\n",
      "\topenfibertargets_nov2020_9\n",
      "\tmwm_yso_disk_apogee_single\n",
      "\tmanual_mwm_halo_mp_wise_apogee_single\n",
      "\tmanual_fps_position_stars_apogee_10\n",
      "\tmanual_bright_target_offsets_1\n",
      "\tmwm_yso_cmz\n",
      "\tbhm_spiders_agn_skymapperdr2\n",
      "\tmanual_bright_targets_g13_offset_fixed_7\n",
      "\tmwm_galactic_core_dist_apogee\n",
      "\tmwm_cb_300pc_apogee\n",
      "\tmwm_ob_core\n",
      "\tmwm_snc_ext_main_apogee\n",
      "\tmwm_gg_core\n",
      "\tmwm_cb_uvex4\n",
      "\tmwm_halo_local_low_boss\n",
      "\tmwm_halo_local_low_apogee\n",
      "\tmwm_yso_cmz_apogee\n",
      "\topenfibertargets_nov2020_47a\n",
      "\tops_std_boss_lsdr10\n",
      "\tmanual_bright_targets_g13_offset_fixed_1\n",
      "\tmanual_mwm_planet_transiting_bd_apogee\n",
      "\tmwm_halo_bb_boss\n",
      "\tmwm_halo_vmp_xp_boss\n",
      "\tmanual_mwm_magcloud_massive_apogee\n",
      "\tmwm_yso_cluster\n",
      "\tmwm_snc_100pc_apogee\n",
      "\tmwm_wd_pwd_boss\n",
      "\tmwm_cb_uvex1\n",
      "\tmwm_halo_nmp_xp_boss\n",
      "\tmanual_fps_position_stars\n",
      "\tbhm_rm_core\n",
      "\tmwm_erosita_compact_gen\n",
      "\tbhm_rm_xrayqso\n",
      "\tmanual_offset_mwmhalo_off10\n",
      "\tmwm_dust_core_dist_apogee\n",
      "\tbhm_spiders_agn_lsdr10\n",
      "\tmanual_mwm_validation_rv_apogee\n",
      "\tmanual_mwm_planet_harps_apogee\n",
      "\tmwm_cb_300pc\n",
      "\tbhm_aqmes_bonus_bright\n",
      "\tmwm_planet_tess\n",
      "\tbhm_aqmes_wide2\n",
      "\tmwm_magcloud_rgb_boss\n",
      "\tmwm_halo_local_high_apogee\n",
      "\tmwm_yso_variable_apogee\n",
      "\tmanual_mwm_halo_vmp_wise_apogee_single\n",
      "\tmwm_halo_distant_rrl_boss\n",
      "\tmwm_monitor_n188_apogee_short\n",
      "\tmwm_halo_local_high_boss_single\n",
      "\tmwm_yso_cluster_apogee\n",
      "\tmwm_cb_galex_mag_boss\n",
      "\tna\n",
      "\tmanual_mwm_planet_sophie_apogee\n",
      "\tmwm_cb_uvex5\n",
      "\tmwm_monitor_m15_apogee_long\n",
      "\tbhm_aqmes_med\n",
      "\topenfibertargets_nov2020_28b\n",
      "\tmanual_mwm_tess_ob_apogee\n",
      "\tmwm_halo_mp_xp_apogee\n",
      "\topenfibertargets_nov2020_15\n",
      "\topenfibertargets_nov2020_12\n",
      "\tmanual_mwm_halo_distant_kgiant_near_boss\n",
      "\tmanual_mwm_halo_distant_bhb_boss_single\n",
      "\tmwm_yso_variable_boss\n",
      "\tmwm_cb_uvex3\n",
      "\tmwm_rv_long_bplates\n",
      "\tmanual_mwm_halo_distant_bhb_boss\n",
      "\topenfibertargets_nov2020_19b\n",
      "\tmanual_mwm_planet_ca_legacy_apogee\n",
      "\topenfibertargets_nov2020_27\n",
      "\tbhm_csc_apogee\n",
      "\tbhm_aqmes_med_faint\n",
      "\tbhm_spiders_agn_supercosmos\n",
      "\tmwm_erosita_compact_var\n",
      "\tbhm_spiders_clusters_lsdr8\n",
      "\tmwm_snc_ext_filler_apogee\n",
      "\tmwm_tess_ob\n",
      "\tops_std_eboss\n",
      "\tmanual_nsbh_boss\n",
      "\topenfibertargets_nov2020_19a\n",
      "\tbhm_spiders_clusters_ps1dr2\n",
      "\tmwm_yso_pms_apogee_sagitta_edr3\n",
      "\tmwm_yso_s2\n",
      "\tmwm_erosita_stars\n",
      "\tmwm_bin_gaia_sb_boss\n",
      "\tbhm_csc_boss_dark\n",
      "\tmanual_mwm_planet_tess_eb_apogee\n",
      "\tmwm_snc_ext_main_boss\n",
      "\topenfibertargets_nov2020_3\n",
      "\tmwm_erosita_compact_boss_shallow\n",
      "\tmwm_legacy_ir2opt_boss\n",
      "\tmwm_yso_s1\n",
      "\tops_std_boss\n",
      "\tmwm_ob_core_boss_single\n",
      "\tmwm_rv_short-bplates\n",
      "\tmwm_cb_swiftuvot_boss\n",
      "\tops_std_apogee\n",
      "\tmanual_offset_mwmhalo_off00\n",
      "\tmanual_mwm_planet_known_apogee\n",
      "\tbhm_spiders_clusters-efeds-hsc-redmapper\n",
      "\tmwm_monitor_m15_apogee_short\n",
      "\tmanual_mwm_halo_vmp_wise_apogee\n",
      "\topenfibertargets_nov2020_1001a\n",
      "\tmanual_bhm_spiders_comm\n",
      "\tbhm_spiders_agn_ps1dr2\n",
      "\tmwm_yso_pms_apogee\n",
      "\tbhm_csc_boss-dark\n",
      "\tops_sky_apogee\n",
      "\tmwm_yso_variable_apogee_single\n",
      "\tmwm_astar_core_boss_single\n",
      "\tmwm_yso_pms_boss\n",
      "\topenfibertargets_nov2020_28a\n",
      "\topenfibertargets_nov2020_47e\n",
      "\tmanual_fps_position_stars_lco_apogee_10\n",
      "\tmanual_bright_targets_g13\n",
      "\tmwm_cb_xmmom_boss\n",
      "\tmwm_halo_mp_xp_apogee_single\n",
      "\topenfibertargets_nov2020_32\n",
      "\tmwm_yso_pms_apogee_zari18pms\n",
      "\tbhm_rm_ancillary\n",
      "\tmwm_yso_embedded_apogee_single\n",
      "\tmwm_yso_pms_boss_zari18pms\n",
      "\tmwm_tess_rgb_apogee\n",
      "\tbhm_spiders_agn_sep\n",
      "\topenfibertargets_nov2020_14\n",
      "\tbhm_aqmes_bonus_faint\n",
      "\tbhm_gua_bright\n",
      "\topenfibertargets_nov2020_8\n",
      "\tmwm_yso_cluster_apogee_single\n",
      "\tmanual_mwm_halo_distant_kgiant\n",
      "\tmwm_halo_nmp_xp_boss_single\n",
      "\tmanual_mwm_tess_ob\n",
      "\tmwm_yso_nebula_apogee_single\n",
      "\tmwm_yso_cluster_boss_single\n",
      "\tmwm_ob_core_boss\n",
      "\tmanual_mwm_magcloud_massive_boss\n",
      "\tmwm_galactic_core\n",
      "\tmwm_cb_300pc_boss\n",
      "\tbhm_rm_known-spec\n",
      "\tmwm_monitor_m67_apogee_short\n",
      "\tbhm_aqmes_wide3\n",
      "\topenfibertargets_nov2020_31\n",
      "\tmwm_yso_nebula_apogee\n",
      "\tmwm_wd_core\n",
      "\topenfibertargets_nov2020_1001b\n",
      "\topenfibertargets_nov2020_35a\n",
      "\tmanual_mwm_halo_vmp_wise_boss_single\n",
      "\topenfibertargets_nov2020_47b\n",
      "\tmwm_rv_short-fps\n",
      "\tmanual_offset_mwmhalo_off20\n",
      "\topenfibertargets_nov2020_6b\n",
      "\tops_std_boss_gdr2\n",
      "\tbhm_spiders_clusters-efeds-sdss-redmapper\n",
      "\tmwm_tess_planet\n",
      "\tmwm_halo_distant_rrl_boss_single\n",
      "\tmwm_halo_local_high_boss\n",
      "\tmwm_yso_s3\n",
      "\tmwm_halo_local_high_apogee_single\n",
      "\topenfibertargets_nov2020_30\n",
      "\tmwm_yso_variable_boss_single\n",
      "\tmwm_yso_ob_boss\n",
      "\tmanual_mwm_nsbh_apogee\n",
      "\tmwm_snc_250pc\n",
      "\topenfibertargets_nov2020_29\n",
      "\tmwm_bin_vis_apogee\n",
      "\topenfibertargets_nov2020_46\n",
      "\tmanual_mwm_planet_gpi_apogee\n",
      "\tmanual_offset_mwmhalo_off05\n",
      "\tmanual_mwm_planet_tess_pc_apogee\n",
      "\tmwm_rv_long-bplates\n",
      "\tmwm_rv_long_fps\n",
      "\topenfibertargets_nov2020_34b\n",
      "\tmanual_nsbh_apogee\n",
      "\tmwm_yso_pms_apogee_zari18pms_single\n",
      "\tmanual_mwm_planet_gaia_astrometry_apogee\n",
      "\tops_std_boss_tic\n",
      "\topenfibertargets_nov2020_28c\n",
      "\tmwm_halo_vmp_xp_apogee_single\n",
      "\tmwm_halo_nmp_xp_apogee\n",
      "\tmwm_halo_vmp_xp_boss_single\n",
      "\topenfibertargets_nov2020_47d\n",
      "\tmwm_wd_gaia_boss\n",
      "\tmanual_mwm_planet_sphere_apogee\n",
      "\tmwm_bin_gaia_astb_boss\n",
      "\topenfibertargets_nov2020_5\n",
      "\tmwm_halo_mp_xp_boss\n",
      "\tmwm_cb_cvcandidates_apogee\n",
      "\tmanual_mwm_validation_hot_apogee\n",
      "\tmwm_bin_gaia_sb_apogee\n",
      "\tmwm_halo_nmp_xp_apogee_single\n",
      "\tbhm_csc_boss-bright\n",
      "\tbhm_spiders_agn_hard\n",
      "\tmwm_erosita_stars_boss\n",
      "\tmwm_cb_gaiagalex\n",
      "\tbhm_csc_boss\n",
      "\tmwm_yso_disk_apogee\n",
      "\tmwm_halo_bb\n",
      "\tbhm_csc_boss_bright\n",
      "\tmanual_mwm_crosscalib_yso_apogee\n",
      "\tmanual_mwm_magcloud_symbiotic_apogee\n",
      "\tmanual_mwm_halo_distant_kgiant_near_boss_single\n",
      "\tmwm_monitor_m67_apogee_long\n",
      "\tmwm_tess_2min_apogee\n",
      "\tmwm_ob_cepheids\n",
      "\topenfibertargets_nov2020_6c\n",
      "\tmwm_yso_pms_boss_sagitta_edr3_single\n",
      "\tbhm_spiders_agn_lsdr8\n",
      "\tmwm_bin_vis_boss\n",
      "\tbhm_spiders_agn_efeds_stragglers\n",
      "\tmwm_yso_pms_boss_zari18pms_single\n",
      "\tmwm_cb_uvex2\n",
      "\tmwm_cb_gaiagalex_boss\n",
      "\topenfibertargets_nov2020_35b\n",
      "\tops_std_boss_lsdr8\n",
      "\topenfibertargets_nov2020_6a\n",
      "\tbhm_spiders_agn_gaiadr3\n",
      "\topenfibertargets_nov2020_1000\n",
      "\tmwm_cb_cvcandidates\n",
      "\tbhm_aqmes_wide2-faint\n",
      "\tmwm_tessrgb_core\n",
      "\topenfibertargets_nov2020_10\n",
      "\tbhm_aqmes_wide2_faint\n",
      "\topenfibertargets_nov2020_34a\n",
      "\tmanual_fps_position_stars_10\n",
      "\tmwm_halo_vmp_xp_apogee\n",
      "\tops_sky_apogee_best\n",
      "\tmanual_offset_mwmhalo_offb\n",
      "\tmwm_snc_ext_filler_boss\n",
      "\topenfibertargets_nov2020_19c\n",
      "\topenfibertargets_nov2020_18\n",
      "\tmwm_halo_local_low_boss_single\n",
      "\tmanual_bright_targets_g13_offset_fixed_3\n",
      "\tbhm_aqmes_bonus-dark\n",
      "\tmwm_bin_rv_short_subgiant_apogee\n",
      "\tmanual_mwm_crosscalib_yso_boss\n",
      "\tmwm_snc_250pc_apogee\n",
      "\tmanual_mwm_halo_vmp_wise_boss\n",
      "\tmwm_bin_gaia_astb_apogee\n",
      "\tops_sky_apogee_good\n",
      "\tbhm_spiders_agn-efeds\n",
      "\tmanual_mwm_validation_cool_apogee\n",
      "\tmwm_snc_100pc_boss\n",
      "\tmanual_mwm_halo_mp_bbb\n",
      "\tops_sky_boss_fallback\n",
      "\tmanual_bright_target_offsets_1_g13\n",
      "\tmwm_halo_mp_xp_boss_single\n",
      "\tmwm_bin_rv_short_mdwarf_apogee_18epoch\n",
      "\tmanual_mwm_halo_mp_wise_boss\n",
      "\tmwm_cb_galex_vol_boss\n",
      "\tmanual_mwm_halo_mp_wise_boss_single\n",
      "\tops_sky_boss_good\n",
      "\tmanual_mwm_nsbh_boss\n",
      "\tmwm_halo_sm_boss\n",
      "\topenfibertargets_nov2020_22\n",
      "\tmwm_yso_embedded_apogee\n",
      "\tmanual_bhm_spiders_comm_lco\n",
      "\topenfibertargets_nov2020_47c\n",
      "\tbhm_gua_dark\n",
      "\tmanual_mwm_validation_hot_boss\n",
      "\tmwm_dust_core\n",
      "\tmwm_halo_local_low_apogee_single\n",
      "\tbhm_rm_var\n",
      "\tmanual_offset_mwmhalo_off30\n",
      "\tmwm_yso_disk_boss_single\n",
      "\tmwm_yso_ob_apogee\n",
      "\tbhm_aqmes_med-faint\n",
      "\tmwm_rv_short_fps\n",
      "\tbhm_aqmes_bonus_core\n",
      "\tmanual_mwm_validation_cool_boss\n",
      "\tmwm_yso_pms_boss_sagitta_edr3\n",
      "\tbhm_spiders_clusters-efeds-erosita\n",
      "\tmanual_bright_targets_g13_offset_fixed_5\n",
      "\tmwm_bin_rv_long_apogee\n",
      "\tbhm_colr_galaxies_lsdr8\n",
      "\tbhm_spiders_agn_tda\n",
      "\tmanual_mwm_halo_mp_wise_apogee\n",
      "\tbhm_colr_galaxies_lsdr10\n",
      "\tbhm_spiders_clusters-efeds-ls-redmapper\n",
      "\tbhm_spiders_clusters_lsdr10\n",
      "\topenfibertargets_nov2020_11\n",
      "Alternative cartons names\n",
      "\tmwm_yso_ob\n",
      "\tmwm_ob_cepheids\n",
      "\tmanual_mwm_planet_ca_legacy\n",
      "\tmwm_validation_rv\n",
      "\tna\n",
      "\tmwm_halo_sm\n",
      "\tmwm_crosscalib_yso\n",
      "\tmwm_yso_embedded\n",
      "\tmwm_cb_cvcandidates\n",
      "\tmanual_mwm_halo_distant_kgiant\n",
      "\tmwm_validation_cool\n",
      "\tmwm_halo_local\n",
      "\tops_std\n",
      "\tmwm_monitor_apogee_m67\n",
      "\tmwm_openfiber\n",
      "\tmwm_galactic_core\n",
      "\tmanual_mwm_planet_sophie\n",
      "\tmwm_cb_swiftuvot\n",
      "\tmanual_mwm_planet_harps\n",
      "\tmwm_legacy_ir2opt\n",
      "\tmwm_magcloud_massive\n",
      "\tmanual_mwm_halo_distant_bhb\n",
      "\tmwm_tess_rgb\n",
      "\tmwm_magcloud_symbiotic\n",
      "\tmwm_validation_openfiber\n",
      "\tmwm_snc\n",
      "\tmwm_yso_variable\n",
      "\tmwm_tess_ob\n",
      "\tmwm_bin_openfiber\n",
      "\tbhm_filler\n",
      "\tmwm_monitor_apogee_m15\n",
      "\tmwm_bin_gaia_sb\n",
      "\tmwm_bin_vis\n",
      "\tbhm_aqmes\n",
      "\tmanual_fps_position_stars_apogee\n",
      "\tmanual_mwm_planet_transiting_bd\n",
      "\tmanual_mwm_planet_known\n",
      "\tmanual_mwm_nsbh\n",
      "\tops_sky\n",
      "\tmwm_yso_nebula\n",
      "\tmwm_erosita_stars\n",
      "\tmwm_bin_rv_short_mdwarf\n",
      "\tmwm_erosita_compact\n",
      "\tmwm_magcloud_agb\n",
      "\tmwm_tess_2min\n",
      "\tbhm_openfiber\n",
      "\tmwm_halo_distant_rrl\n",
      "\tmwm_yso_cmz\n",
      "\tmwm_magcloud_rgb\n",
      "\tmwm_snc_openfiber\n",
      "\tmanual_mwm_planet_gaia_astrometry\n",
      "\tmwm_ob_core\n",
      "\tbhm_csc\n",
      "\tmwm_validation_hot\n",
      "\tmwm_cb_galex\n",
      "\tmanual_offset_mwmhalo\n",
      "\tmanual_mwm_planet_gpi\n",
      "\tmwm_bin_rv_short_subgiant\n",
      "\tmwm_dust_core\n",
      "\tmwm_cb_xmmom\n",
      "\tmwm_yso_pms\n",
      "\tmwm_yso_cluster\n",
      "\tbhm_rm\n",
      "\tmwm_halo_mp_xp\n",
      "\tmwm_bin_rv_short\n",
      "\tmanual_mwm_planet_sphere\n",
      "\tmwm_yso_disk\n",
      "\tmwm_crosscalib\n",
      "\tmwm_halo_openfiber\n",
      "\tmanual_mwm_planet_tess_eb\n",
      "\tmwm_cb_300pc\n",
      "\tbhm_spiders\n",
      "\tmwm_bin_rv_short_rgb\n",
      "\tmanual_mwm_halo_mp_wise\n",
      "\tmwm_bin_gaia_astb\n",
      "\tmwm_monitor_apogee_n188\n",
      "\tmwm_cluster_openfiber\n",
      "\tmwm_bin_rv_long\n",
      "\tmanual_mwm_planet_tess_pc\n",
      "\tmwm_halo_bb\n",
      "\tmwm_wd\n",
      "Alternative programs\n",
      "\tsnc\n",
      "\taqmes\n",
      "\tnsbh\n",
      "\ttess\n",
      "\tcb\n",
      "\thalo\n",
      "\tplanet\n",
      "\tspiders\n",
      "\tdust\n",
      "\tna\n",
      "\tfiller\n",
      "\tlegacy\n",
      "\tcommissioning\n",
      "\tgalactic\n",
      "\tsky\n",
      "\tmonitor\n",
      "\tmagcloud\n",
      "\twd\n",
      "\tob\n",
      "\tstd\n",
      "\tcsc\n",
      "\tvalidation\n",
      "\tyso\n",
      "\tbin\n",
      "\ttessrgb\n",
      "\ttessob\n",
      "\trm\n",
      "\topen_fiber\n",
      "\terosita\n"
     ]
    }
   ],
   "source": [
    "print(\"Mappers:\")\n",
    "for mapper in flags.all_mappers:\n",
    "    print(f\"\\t{mapper}\")\n",
    "    \n",
    "print(\"Programs\")\n",
    "for program in flags.all_programs:\n",
    "    print(f\"\\t{program}\")\n",
    "\n",
    "print(\"Cartons names\")\n",
    "for name in flags.all_carton_names:\n",
    "    print(f\"\\t{name}\")\n",
    "\n",
    "print(\"Alternative cartons names\")\n",
    "for alt_name in flags.all_alt_carton_names:\n",
    "    print(f\"\\t{alt_name}\")\n",
    "\n",
    "print(\"Alternative programs\")\n",
    "for alt_program in flags.all_alt_programs:\n",
    "    print(f\"\\t{alt_program}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c31319da-9058-4048-a945-b648455ee8d2",
   "metadata": {},
   "source": [
    "### Select by Mapper\n",
    "If you want to check which rows are in a particular mapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dc3bc2a6-1347-491d-bf5c-82a4801a6a38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False,  True, ..., False,  True, False])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Select things assigned to MWM\n",
    "mwm_mask = flags.in_mapper(\"mwm\")\n",
    "mwm_mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1e135bbb-1117-46eb-874b-9d01db07eea3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are 460,377 things assigned to MWM\n"
     ]
    }
   ],
   "source": [
    "print(f\"There are {np.sum(flags.in_mapper('mwm')):,} things assigned to MWM\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f94340e8-a5eb-4441-b649-cd4b172f605d",
   "metadata": {},
   "source": [
    "### Select by Program\n",
    "If you want to check which rows are in a particular program"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c50cff92-38b8-4ecc-a66f-b0e4bddb4092",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, ..., False, False, False])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flags.in_program(\"mwm_wd\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ef53ab5-67a5-42ff-ba94-11410edd8799",
   "metadata": {},
   "source": [
    "### Select by name\n",
    "If you want to check which rows are in a particular carton"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1e3d52ef-cf6a-4197-9f6d-fc3313215c02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, ..., False, False, False])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flags.in_carton_name(\"mwm_snc_250pc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a57103e6-b591-4b9e-8479-55a9c2586129",
   "metadata": {},
   "source": [
    "## I have a target, how was it targeted?\n",
    "\n",
    "If you have a CATALOGID or SDSS_ID and would like to know the targeting cartons for that source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "409d591c-ecab-4b5e-b826-581400d42184",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mwm_yso_s3_0.1.0\n",
      "mwm_yso_cluster_0.1.0\n",
      "mwm_yso_variable_apogee_0.5.0\n",
      "mwm_yso_variable_boss_0.5.0\n",
      "mwm_yso_cluster_apogee_0.5.0\n",
      "mwm_yso_cluster_boss_0.5.0\n",
      "mwm_yso_cluster_apogee_0.5.17\n",
      "mwm_yso_cluster_boss_0.5.17\n",
      "mwm_yso_variable_apogee_0.5.17\n",
      "mwm_yso_variable_boss_0.5.17\n",
      "mwm_yso_cluster_apogee_1.0.6\n",
      "mwm_yso_cluster_boss_1.0.6\n",
      "mwm_yso_variable_apogee_1.0.6\n",
      "mwm_yso_variable_boss_1.0.6\n",
      "mwm_yso_variable_apogee_single_1.0.33\n",
      "mwm_yso_variable_boss_single_1.0.33\n",
      "mwm_yso_cluster_apogee_single_1.0.33\n",
      "mwm_yso_cluster_boss_single_1.0.33\n"
     ]
    }
   ],
   "source": [
    "catalogid = 27021598754184543\n",
    "idx = np.where((boss_data[\"CATALOGID21\"] == catalogid) |\n",
    "               (boss_data[\"CATALOGID25\"] == catalogid) | \n",
    "               (boss_data[\"CATALOGID31\"] == catalogid))[0]\n",
    "flag1 = TargetingFlags(boss_data[\"SDSS5_TARGET_FLAGS\"][idx])\n",
    "for label, count in flag1.count(skip_empty=True).items():\n",
    "    print(f\"{label}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "76c0d6e4-01a7-468e-86c3-86fef058bc34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mwm_yso_s3_0.1.0\n",
      "mwm_yso_cluster_0.1.0\n",
      "mwm_yso_variable_apogee_0.5.0\n",
      "mwm_yso_variable_boss_0.5.0\n",
      "mwm_yso_cluster_apogee_0.5.0\n",
      "mwm_yso_cluster_boss_0.5.0\n",
      "mwm_yso_cluster_apogee_0.5.17\n",
      "mwm_yso_cluster_boss_0.5.17\n",
      "mwm_yso_variable_apogee_0.5.17\n",
      "mwm_yso_variable_boss_0.5.17\n",
      "mwm_yso_cluster_apogee_1.0.6\n",
      "mwm_yso_cluster_boss_1.0.6\n",
      "mwm_yso_variable_apogee_1.0.6\n",
      "mwm_yso_variable_boss_1.0.6\n",
      "mwm_yso_variable_apogee_single_1.0.33\n",
      "mwm_yso_variable_boss_single_1.0.33\n",
      "mwm_yso_cluster_apogee_single_1.0.33\n",
      "mwm_yso_cluster_boss_single_1.0.33\n"
     ]
    }
   ],
   "source": [
    "sdss_id = 87406300\n",
    "idx = np.where(boss_data[\"SDSS_ID\"] == sdss_id)[0]\n",
    "flag1 = TargetingFlags(boss_data[\"SDSS5_TARGET_FLAGS\"][idx])\n",
    "for label, count in flag1.count(skip_empty=True).items():\n",
    "    print(f\"{label}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d17a6cf6-5370-42b5-8356-cdffcdd13bf5",
   "metadata": {},
   "source": [
    "## About this notebook\n",
    "\n",
    "This notebook was created to give an example of using the `semaphore` tool with the mwmAllVisit file\n",
    "\n",
    "**Authors**: Andy Casey (andrew.casey@monash.edu), Sean Morrison\n",
    "\n",
    "**Last Update**: 2025 Jul 9\n",
    "\n",
    "If you use `astropy`  for published research, please cite the\n",
    "authors. Follow this link for more information about citing `astropy`:\n",
    "\n",
    "* [Citing `astropy`](https://www.astropy.org/acknowledging.html)\n",
    "\n",
    "And of course please [cite](https://sdss.org/collaboration/citing-sdss/) SDSS when you use our data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdc00a83-f64e-4748-b745-819df91a92c2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  },
  "scenes_data": {
   "active_scene": "Default Scene",
   "init_scene": "",
   "scenes": [
    "Default Scene"
   ]
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
