{ "cells": [ { "cell_type": "code", "execution_count": 354, "id": "09ca516e-3bbb-4a2b-9c6e-984c9a7e801a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symmetric?\n", "True\n", "6555\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "from astropy.coordinates import SkyCoord\n", "from astropy import units as u\n", "from sklearn.cluster import AgglomerativeClustering\n", "import matplotlib.pyplot as plt\n", "import matplotlib.patches as patches\n", "import os\n", "import numpy as np\n", "from astropy.io import fits\n", "from astropy.wcs import WCS\n", "from tqdm import tqdm\n", "\n", "df = pd.read_csv(\"~/hst_FINAL.csv\")\n", "\n", "df = df.rename(columns={'sci_data_set_name': 'obs_id'})\n", "\n", "# Create a shortened identifier.\n", "\n", "\"\"\"\n", "The data downloading process looks like the following:\n", "\n", "1. Use MastMissions to query the list of observations and their metadata, like ra/dec\n", "\n", "2. Filtering process to make sure there are no overlapping observations.\n", "\n", "3. Use Observations to pull the names of the data files associated with each observation.\n", "\n", "4. Pull the data by wget all those file links.\n", "\n", "5. Preprocess.\n", "\n", "Note that the data file names use the first 6 chars of obs_id from this observations array\n", "that we have created. That's why we create the shortened identifier, to match\n", "observations to product file names. This will be used later.\n", "\"\"\"\n", "\n", "df['obs_id_short'] = df['obs_id'].str[:6]\n", "\n", "RA_NAME = 'sci_ra'\n", "DEC_NAME = 'sci_dec'\n", "\n", "assert df[RA_NAME].isna().sum() < 10\n", "assert df[DEC_NAME].isna().sum() < 10\n", "\n", "df = df.dropna(subset=[RA_NAME, DEC_NAME])\n", "\n", "# Sample data: let's create an example array of latitudes and longitudes\n", "# MAKE SURE TO PUT RA=LON, DEC=LAT\n", "latitudes = np.array(df[DEC_NAME]) # Example latitudes\n", "longitudes = np.array(df[RA_NAME]) # Example longitudes\n", "\n", "n_points = len(latitudes)\n", "\n", "# Repeat each point n_points times for lat1, lon1\n", "lat1 = np.repeat(latitudes, n_points)\n", "lon1 = np.repeat(longitudes, n_points)\n", "\n", "# Tile the whole array n_points times for lat2, lon2\n", "lat2 = np.tile(latitudes, n_points)\n", "lon2 = np.tile(longitudes, n_points)\n", "\n", "# Calculates angular separation between two spherical coords\n", "# This can be lat/lon or ra/dec\n", "# Taken from astropy\n", "def angular_separation_deg(lon1, lat1, lon2, lat2):\n", " lon1 = np.deg2rad(lon1)\n", " lon2 = np.deg2rad(lon2)\n", " lat1 = np.deg2rad(lat1)\n", " lat2 = np.deg2rad(lat2)\n", " \n", " sdlon = np.sin(lon2 - lon1)\n", " cdlon = np.cos(lon2 - lon1)\n", " slat1 = np.sin(lat1)\n", " slat2 = np.sin(lat2)\n", " clat1 = np.cos(lat1)\n", " clat2 = np.cos(lat2)\n", "\n", " num1 = clat2 * sdlon\n", " num2 = clat1 * slat2 - slat1 * clat2 * cdlon\n", " denominator = slat1 * slat2 + clat1 * clat2 * cdlon\n", "\n", " return np.rad2deg(np.arctan2(np.hypot(num1, num2), denominator))\n", "\n", "# Compute the pairwise angular separations\n", "angular_separations = angular_separation_deg(lon1, lat1, lon2, lat2)\n", "\n", "# Reshape the result into a matrix form\n", "angular_separations_matrix = angular_separations.reshape(n_points, n_points)\n", "\n", "def check_symmetric(a, rtol=1e-05, atol=1e-07):\n", " return np.allclose(a, a.T, rtol=rtol, atol=atol)\n", "\n", "print(\"Symmetric?\")\n", "print(check_symmetric(angular_separations_matrix))\n", "print(len(angular_separations_matrix))" ] }, { "cell_type": "code", "execution_count": 355, "id": "d143cb03-8501-4a02-b77f-8efb6ce99a7d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6555\n" ] } ], "source": [ "print(len(angular_separations_matrix))" ] }, { "cell_type": "code", "execution_count": 356, "id": "85edd94e-5591-4c7c-8251-b8c976962f72", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "128 369\n", "60 284\n", "156 198\n", "99 163\n", "78 118\n", " ... \n", "2025 1\n", "605 1\n", "731 1\n", "1129 1\n", "561 1\n", "Length: 2215, dtype: int64\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "100%|█████████████████████████████████████| 2215/2215 [00:00<00:00, 3215.33it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Max subset with minimum distance: 2367\n" ] } ], "source": [ "HUBBLE_FOV = 0.057\n", "#JWST_FOV = 0.0172222\n", "\n", "THRESH = 2 * HUBBLE_FOV\n", "\n", "clustering = AgglomerativeClustering(n_clusters=None, metric='precomputed', linkage='single', distance_threshold=THRESH)\n", "labels = clustering.fit_predict(angular_separations_matrix)\n", "\n", "df['label'] = labels\n", "\n", "print(pd.Series(labels).value_counts())\n", "\n", "def max_subset_with_min_distance(points, min_distance):\n", " subset = []\n", " for i, row in points.iterrows():\n", " if all(angular_separation_deg(row[RA_NAME], row[DEC_NAME], existing_point[RA_NAME], existing_point[DEC_NAME]) >= min_distance for existing_point in subset):\n", " subset.append(row)\n", " return subset\n", "\n", "all_subsets = []\n", "\n", "for label in tqdm(np.unique(labels)):\n", " cds = df[labels == label]\n", " subset = max_subset_with_min_distance(cds, THRESH)\n", " all_subsets.extend(subset)\n", "\n", "print(\"Max subset with minimum distance:\", len(all_subsets))\n", "\n", "df = pd.DataFrame(all_subsets)" ] }, { "cell_type": "code", "execution_count": 357, "id": "55ae58fc-5c2f-46ec-8fa7-d7229fd61b5f", "metadata": {}, "outputs": [], "source": [ "from astroquery.mast import Observations\n", "\n", "# Query for data with the specified obs_id\n", "result = Observations.query_criteria(obs_id=df[\"obs_id\"])" ] }, { "cell_type": "code", "execution_count": 358, "id": "5a29aaf9-dd68-4fa2-910a-8d399f4580cd", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|███████████████████████████████████████| 2143/2143 [07:54<00:00, 4.52it/s]\n" ] } ], "source": [ "from astropy.table import unique, vstack, Table\n", "\n", "matched_obs = result\n", "\n", "# Split the observations into \"chunks\"\n", "sz_chunk = 1\n", "chunks = [matched_obs[i:i+sz_chunk] for i in range(0,len(matched_obs), sz_chunk)]\n", "\n", "# Get the list of products for each chunk\n", "t = []\n", "for chunk in tqdm(chunks):\n", " out = Observations.get_product_list(chunk)\n", " out = out[out['productSubGroupDescription'] == 'RAW']\n", " out = out[out['dataRights'] == 'PUBLIC']\n", " out = out.to_pandas()\n", " if len(out) > 0:\n", " t.append(out.iloc[0])" ] }, { "cell_type": "code", "execution_count": 359, "id": "7ab7dd7d-189f-402a-a796-22d859fe15ac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 2118 unique files, which are 72.9 GB in size.\n" ] } ], "source": [ "files = pd.DataFrame(t)\n", "\n", "'''\n", "# Ensure we only keep raw data files\n", "files = files[files['productSubGroupDescription'] == 'RAW']\n", "\n", "# Create a shortened identified\n", "files['obs_id_short'] = files['obs_id'].str[:6]\n", "files = files.drop_duplicates(subset='obs_id_short')\n", "'''\n", "\n", "print(f\"There are {len(files)} unique files, which are {sum(files['size'])/10**9:.1f} GB in size.\")" ] }, { "cell_type": "code", "execution_count": 360, "id": "94c7dc65-9002-458f-b53f-1120fd8c7be1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | obsID | \n", "obs_collection | \n", "dataproduct_type | \n", "obs_id | \n", "description | \n", "type | \n", "dataURI | \n", "productType | \n", "productGroupDescription | \n", "productSubGroupDescription | \n", "productDocumentationURL | \n", "project | \n", "prvversion | \n", "proposal_id | \n", "productFilename | \n", "size | \n", "parent_obsid | \n", "dataRights | \n", "calib_level | \n", "filters | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "26113888 | \n", "HST | \n", "image | \n", "jdq318n8q | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jdq318n8q_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "15213 | \n", "jdq318n8q_raw.fits | \n", "34344000 | \n", "26114025 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "24979066 | \n", "HST | \n", "image | \n", "jcoz32dvq | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jcoz32dvq_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "13671 | \n", "jcoz32dvq_raw.fits | \n", "34344000 | \n", "24981158 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "25874266 | \n", "HST | \n", "image | \n", "jdba3nvyq | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jdba3nvyq_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "14840 | \n", "jdba3nvyq_raw.fits | \n", "34344000 | \n", "25874435 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "24042703 | \n", "HST | \n", "image | \n", "j96l03bcq | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/j96l03bcq_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "10438 | \n", "j96l03bcq_raw.fits | \n", "34344000 | \n", "24825269 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "26270229 | \n", "HST | \n", "image | \n", "jdrz3ytfq | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jdrz3ytfq_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "15446 | \n", "jdrz3ytfq_raw.fits | \n", "34344000 | \n", "26270246 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
0 | \n", "24085175 | \n", "HST | \n", "image | \n", "jb2p31fnq | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jb2p31fnq_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "11613 | \n", "jb2p31fnq_raw.fits | \n", "34344000 | \n", "24831290 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "68563813 | \n", "HST | \n", "image | \n", "jenm44ygq | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jenm44ygq_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "16711 | \n", "jenm44ygq_raw.fits | \n", "34344000 | \n", "68565137 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "25857452 | \n", "HST | \n", "image | \n", "jd9r08r2q | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jd9r08r2q_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "14658 | \n", "jd9r08r2q_raw.fits | \n", "34344000 | \n", "25857751 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "24127301 | \n", "HST | \n", "image | \n", "jcdm96g1q | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/jcdm96g1q_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "13364 | \n", "jcdm96g1q_raw.fits | \n", "34344000 | \n", "24839150 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
0 | \n", "24035175 | \n", "HST | \n", "image | \n", "j94se5qmq | \n", "DADS RAW file - Raw exposure COS/NICMOS/STIS/W... | \n", "S | \n", "mast:HST/product/j94se5qmq_raw.fits | \n", "SCIENCE | \n", "NaN | \n", "RAW | \n", "NaN | \n", "CALACS | \n", "NaN | \n", "10189 | \n", "j94se5qmq_raw.fits | \n", "34344000 | \n", "24035175 | \n", "PUBLIC | \n", "1 | \n", "F606W | \n", "
2118 rows × 20 columns
\n", "