{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "07b57859", "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "\n", "Contains the necessary scripts to actually download the FITS files that are in your JWST csv.\n", "\n", "\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "id": "240cc56f-e1b6-47f7-93ee-dc1a0918f5af", "metadata": {}, "outputs": [], "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(\"jwst_FINAL.csv\")\n", "\n", "df = df.rename(columns={'sci_data_set_name': 'obs_id'})\n", "\n", "# Effective integration time should be more than 30 seconds\n", "df = df[df['effinttm'] > 30]\n", "\n", "df = df[df['exp_type'] == \"NRC_IMAGE\"]\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 = 'targ_ra'\n", "DEC_NAME = 'targ_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", "df = df.groupby([RA_NAME, DEC_NAME]).apply(lambda x: x.drop_duplicates(subset='detector', keep='first'))\n", "\n", "multi_index_df = df.index.to_frame().groupby(level=0).first().reset_index(drop=True)\n", "multi_index_df = multi_index_df.drop(columns=[2])\n", "\n", "df = df.reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 173, "id": "cd89a849-6ef4-493a-910f-0b385e254eb2", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|█████████████████████████████████████████| 117/117 [13:48<00:00, 7.08s/it]\n" ] } ], "source": [ "import requests\n", "import csv\n", "\n", "# Function to search datasets using the given endpoint\n", "def search_datasets(dataset_ids):\n", " # Base URL for the search API\n", " base_url = 'https://mast.stsci.edu/search/jwst/api/v0.1/list_products'\n", " \n", " # List to store search results\n", " search_results = []\n", " \n", " ids_str = ','.join(dataset_ids)\n", "\n", " # Construct the search URL\n", " search_url = f\"{base_url}?dataset_ids={ids_str}\"\n", "\n", " # Make the API request\n", " response = requests.get(search_url)\n", "\n", " # Check if the request was successful\n", " if response.status_code == 200:\n", " # Parse the JSON response\n", " data = response.json()\n", " search_results.append(data)\n", " else:\n", " # Handle errors\n", " print(f\"Error: Unable to fetch data for dataset ID {dataset_id}\")\n", " \n", " return search_results\n", "\n", "# Example usage\n", "dataset_ids_csv = list(df['fileSetName'])\n", "\n", "sz_chunk = 10\n", "chunks = [dataset_ids_csv[i:i+sz_chunk] for i in range(0,len(dataset_ids_csv), sz_chunk)]\n", "\n", "all_results = []\n", "\n", "for chunk in tqdm(chunks):\n", " all_results.append(search_datasets(chunk))" ] }, { "cell_type": "code", "execution_count": 179, "id": "b6aad3d4-fdd0-4799-a84c-692d1c94463d", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|███████████████████████████████████████| 117/117 [00:00<00:00, 3803.87it/s]\n" ] } ], "source": [ "new_all_results = []\n", "\n", "for result in tqdm(all_results):\n", " l = result[0]['products']\n", " new_all_results.extend(l)" ] }, { "cell_type": "code", "execution_count": 181, "id": "0c2caafa-d649-4e2a-92a6-24680ee06cb7", "metadata": {}, "outputs": [], "source": [ "new_all_results_df = pd.DataFrame(new_all_results)" ] }, { "cell_type": "code", "execution_count": 184, "id": "e189e901-dbad-4689-8454-ee9e1ccab09a", "metadata": {}, "outputs": [], "source": [ "detectors = ['NRCA1_FULL', 'NRCA2_FULL', 'NRCA3_FULL', 'NRCA4_FULL', 'NRCB1_FULL', 'NRCB2_FULL', 'NRCB3_FULL', 'NRCB4_FULL']\n", "\n", "\n", "resultsdf = new_all_results_df[new_all_results_df['category'] == '1b']\n", "resultsdf = resultsdf[resultsdf['filters'].isin(detectors)]" ] }, { "cell_type": "code", "execution_count": 189, "id": "1f46cbf4-5b53-437d-8e9b-f7d126839ccc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | product_key | \n", "access | \n", "dataset | \n", "instrument_name | \n", "filters | \n", "filename | \n", "uri | \n", "authz_primary_identifier | \n", "authz_secondary_identifier | \n", "file_suffix | \n", "category | \n", "size | \n", "type | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
28 | \n", "jw02561001002_06101_00001_jw02561001002_06101_... | \n", "PUBLIC | \n", "jw02561001002_06101_00001 | \n", "NIRCAM | \n", "NRCA3_FULL | \n", "jw02561001002_06101_00001_nrca3_uncal.fits | \n", "jw02561001002_06101_00001/jw02561001002_06101_... | \n", "jw02561001002_06101_00001_nrca3_uncal.fits | \n", "jw02561001002_06101_00001_nrca3_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
50 | \n", "jw02561001002_06101_00001_jw02561001002_06101_... | \n", "PUBLIC | \n", "jw02561001002_06101_00001 | \n", "NIRCAM | \n", "NRCB3_FULL | \n", "jw02561001002_06101_00001_nrcb3_uncal.fits | \n", "jw02561001002_06101_00001/jw02561001002_06101_... | \n", "jw02561001002_06101_00001_nrcb3_uncal.fits | \n", "jw02561001002_06101_00001_nrcb3_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
72 | \n", "jw02561001002_06101_00001_jw02561001002_06101_... | \n", "PUBLIC | \n", "jw02561001002_06101_00001 | \n", "NIRCAM | \n", "NRCA2_FULL | \n", "jw02561001002_06101_00001_nrca2_uncal.fits | \n", "jw02561001002_06101_00001/jw02561001002_06101_... | \n", "jw02561001002_06101_00001_nrca2_uncal.fits | \n", "jw02561001002_06101_00001_nrca2_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
93 | \n", "jw02561001002_06101_00001_jw02561001002_06101_... | \n", "PUBLIC | \n", "jw02561001002_06101_00001 | \n", "NIRCAM | \n", "NRCB2_FULL | \n", "jw02561001002_06101_00001_nrcb2_uncal.fits | \n", "jw02561001002_06101_00001/jw02561001002_06101_... | \n", "jw02561001002_06101_00001_nrcb2_uncal.fits | \n", "jw02561001002_06101_00001_nrcb2_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
114 | \n", "jw02561001002_06101_00001_jw02561001002_06101_... | \n", "PUBLIC | \n", "jw02561001002_06101_00001 | \n", "NIRCAM | \n", "NRCA4_FULL | \n", "jw02561001002_06101_00001_nrca4_uncal.fits | \n", "jw02561001002_06101_00001/jw02561001002_06101_... | \n", "jw02561001002_06101_00001_nrca4_uncal.fits | \n", "jw02561001002_06101_00001_nrca4_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
216718 | \n", "jw02130007001_03101_00002_jw02130007001_03101_... | \n", "PUBLIC | \n", "jw02130007001_03101_00002 | \n", "NIRCAM | \n", "NRCB1_FULL | \n", "jw02130007001_03101_00002_nrcb1_uncal.fits | \n", "jw02130007001_03101_00002/jw02130007001_03101_... | \n", "jw02130007001_03101_00002_nrcb1_uncal.fits | \n", "jw02130007001_03101_00002_nrcb1_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
216740 | \n", "jw02130007001_03101_00002_jw02130007001_03101_... | \n", "PUBLIC | \n", "jw02130007001_03101_00002 | \n", "NIRCAM | \n", "NRCA4_FULL | \n", "jw02130007001_03101_00002_nrca4_uncal.fits | \n", "jw02130007001_03101_00002/jw02130007001_03101_... | \n", "jw02130007001_03101_00002_nrca4_uncal.fits | \n", "jw02130007001_03101_00002_nrca4_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
216786 | \n", "jw02130007001_03101_00002_jw02130007001_03101_... | \n", "PUBLIC | \n", "jw02130007001_03101_00002 | \n", "NIRCAM | \n", "NRCA1_FULL | \n", "jw02130007001_03101_00002_nrca1_uncal.fits | \n", "jw02130007001_03101_00002/jw02130007001_03101_... | \n", "jw02130007001_03101_00002_nrca1_uncal.fits | \n", "jw02130007001_03101_00002_nrca1_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
216808 | \n", "jw02130007001_03101_00002_jw02130007001_03101_... | \n", "PUBLIC | \n", "jw02130007001_03101_00002 | \n", "NIRCAM | \n", "NRCA3_FULL | \n", "jw02130007001_03101_00002_nrca3_uncal.fits | \n", "jw02130007001_03101_00002/jw02130007001_03101_... | \n", "jw02130007001_03101_00002_nrca3_uncal.fits | \n", "jw02130007001_03101_00002_nrca3_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
216830 | \n", "jw02130007001_03101_00002_jw02130007001_03101_... | \n", "PUBLIC | \n", "jw02130007001_03101_00002 | \n", "NIRCAM | \n", "NRCA2_FULL | \n", "jw02130007001_03101_00002_nrca2_uncal.fits | \n", "jw02130007001_03101_00002/jw02130007001_03101_... | \n", "jw02130007001_03101_00002_nrca2_uncal.fits | \n", "jw02130007001_03101_00002_nrca2_uncal.fits | \n", "_uncal | \n", "1b | \n", "75553920 | \n", "science | \n", "
7537 rows × 13 columns
\n", "