rithwiks commited on
Commit
ac2ccbf
·
1 Parent(s): 12f956c

notebooks uncommented to filter and download data

Browse files
utils/jwst_downloading.ipynb ADDED
@@ -0,0 +1,1099 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "07b57859",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "\"\"\"\n",
11
+ "\n",
12
+ "FULLY UNCLEANED CODE\n",
13
+ "\n",
14
+ "Contains the necessary scripts to actually download the FITS files that are in your JWST csv.\n",
15
+ "\n",
16
+ "\n",
17
+ "\"\"\""
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": null,
23
+ "id": "240cc56f-e1b6-47f7-93ee-dc1a0918f5af",
24
+ "metadata": {},
25
+ "outputs": [],
26
+ "source": [
27
+ "import pandas as pd\n",
28
+ "import numpy as np\n",
29
+ "from astropy.coordinates import SkyCoord\n",
30
+ "from astropy import units as u\n",
31
+ "from sklearn.cluster import AgglomerativeClustering\n",
32
+ "import matplotlib.pyplot as plt\n",
33
+ "import matplotlib.patches as patches\n",
34
+ "import os\n",
35
+ "import numpy as np\n",
36
+ "from astropy.io import fits\n",
37
+ "from astropy.wcs import WCS\n",
38
+ "from tqdm import tqdm\n",
39
+ "\n",
40
+ "df = pd.read_csv(\"jwst_FINAL.csv\")\n",
41
+ "\n",
42
+ "df = df.rename(columns={'sci_data_set_name': 'obs_id'})\n",
43
+ "\n",
44
+ "# Effective integration time should be more than 30 seconds\n",
45
+ "df = df[df['effinttm'] > 30]\n",
46
+ "\n",
47
+ "df = df[df['exp_type'] == \"NRC_IMAGE\"]\n",
48
+ "\n",
49
+ "\"\"\"\n",
50
+ "The data downloading process looks like the following:\n",
51
+ "\n",
52
+ "1. Use MastMissions to query the list of observations and their metadata, like ra/dec\n",
53
+ "\n",
54
+ "2. Filtering process to make sure there are no overlapping observations.\n",
55
+ "\n",
56
+ "3. Use Observations to pull the names of the data files associated with each observation.\n",
57
+ "\n",
58
+ "4. Pull the data by wget all those file links.\n",
59
+ "\n",
60
+ "5. Preprocess.\n",
61
+ "\n",
62
+ "Note that the data file names use the first 6 chars of obs_id from this observations array\n",
63
+ "that we have created. That's why we create the shortened identifier, to match\n",
64
+ "observations to product file names. This will be used later.\n",
65
+ "\"\"\"\n",
66
+ "\n",
67
+ "df['obs_id_short'] = df['obs_id'].str[:6]\n",
68
+ "\n",
69
+ "RA_NAME = 'targ_ra'\n",
70
+ "DEC_NAME = 'targ_dec'\n",
71
+ "\n",
72
+ "assert df[RA_NAME].isna().sum() < 10\n",
73
+ "assert df[DEC_NAME].isna().sum() < 10\n",
74
+ "\n",
75
+ "df = df.dropna(subset=[RA_NAME, DEC_NAME])\n",
76
+ "\n",
77
+ "df = df.groupby([RA_NAME, DEC_NAME]).apply(lambda x: x.drop_duplicates(subset='detector', keep='first'))\n",
78
+ "\n",
79
+ "multi_index_df = df.index.to_frame().groupby(level=0).first().reset_index(drop=True)\n",
80
+ "multi_index_df = multi_index_df.drop(columns=[2])\n",
81
+ "\n",
82
+ "df = df.reset_index(drop=True)"
83
+ ]
84
+ },
85
+ {
86
+ "cell_type": "code",
87
+ "execution_count": 173,
88
+ "id": "cd89a849-6ef4-493a-910f-0b385e254eb2",
89
+ "metadata": {},
90
+ "outputs": [
91
+ {
92
+ "name": "stderr",
93
+ "output_type": "stream",
94
+ "text": [
95
+ "100%|█████████████████████████████████████████| 117/117 [13:48<00:00, 7.08s/it]\n"
96
+ ]
97
+ }
98
+ ],
99
+ "source": [
100
+ "import requests\n",
101
+ "import csv\n",
102
+ "\n",
103
+ "# Function to search datasets using the given endpoint\n",
104
+ "def search_datasets(dataset_ids):\n",
105
+ " # Base URL for the search API\n",
106
+ " base_url = 'https://mast.stsci.edu/search/jwst/api/v0.1/list_products'\n",
107
+ " \n",
108
+ " # List to store search results\n",
109
+ " search_results = []\n",
110
+ " \n",
111
+ " ids_str = ','.join(dataset_ids)\n",
112
+ "\n",
113
+ " # Construct the search URL\n",
114
+ " search_url = f\"{base_url}?dataset_ids={ids_str}\"\n",
115
+ "\n",
116
+ " # Make the API request\n",
117
+ " response = requests.get(search_url)\n",
118
+ "\n",
119
+ " # Check if the request was successful\n",
120
+ " if response.status_code == 200:\n",
121
+ " # Parse the JSON response\n",
122
+ " data = response.json()\n",
123
+ " search_results.append(data)\n",
124
+ " else:\n",
125
+ " # Handle errors\n",
126
+ " print(f\"Error: Unable to fetch data for dataset ID {dataset_id}\")\n",
127
+ " \n",
128
+ " return search_results\n",
129
+ "\n",
130
+ "# Example usage\n",
131
+ "dataset_ids_csv = list(df['fileSetName'])\n",
132
+ "\n",
133
+ "sz_chunk = 10\n",
134
+ "chunks = [dataset_ids_csv[i:i+sz_chunk] for i in range(0,len(dataset_ids_csv), sz_chunk)]\n",
135
+ "\n",
136
+ "all_results = []\n",
137
+ "\n",
138
+ "for chunk in tqdm(chunks):\n",
139
+ " all_results.append(search_datasets(chunk))"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": 179,
145
+ "id": "b6aad3d4-fdd0-4799-a84c-692d1c94463d",
146
+ "metadata": {},
147
+ "outputs": [
148
+ {
149
+ "name": "stderr",
150
+ "output_type": "stream",
151
+ "text": [
152
+ "100%|███████████████████████████████████████| 117/117 [00:00<00:00, 3803.87it/s]\n"
153
+ ]
154
+ }
155
+ ],
156
+ "source": [
157
+ "new_all_results = []\n",
158
+ "\n",
159
+ "for result in tqdm(all_results):\n",
160
+ " l = result[0]['products']\n",
161
+ " new_all_results.extend(l)"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "code",
166
+ "execution_count": 181,
167
+ "id": "0c2caafa-d649-4e2a-92a6-24680ee06cb7",
168
+ "metadata": {},
169
+ "outputs": [],
170
+ "source": [
171
+ "new_all_results_df = pd.DataFrame(new_all_results)"
172
+ ]
173
+ },
174
+ {
175
+ "cell_type": "code",
176
+ "execution_count": 184,
177
+ "id": "e189e901-dbad-4689-8454-ee9e1ccab09a",
178
+ "metadata": {},
179
+ "outputs": [],
180
+ "source": [
181
+ "detectors = ['NRCA1_FULL', 'NRCA2_FULL', 'NRCA3_FULL', 'NRCA4_FULL', 'NRCB1_FULL', 'NRCB2_FULL', 'NRCB3_FULL', 'NRCB4_FULL']\n",
182
+ "\n",
183
+ "\n",
184
+ "resultsdf = new_all_results_df[new_all_results_df['category'] == '1b']\n",
185
+ "resultsdf = resultsdf[resultsdf['filters'].isin(detectors)]"
186
+ ]
187
+ },
188
+ {
189
+ "cell_type": "code",
190
+ "execution_count": 189,
191
+ "id": "1f46cbf4-5b53-437d-8e9b-f7d126839ccc",
192
+ "metadata": {},
193
+ "outputs": [
194
+ {
195
+ "data": {
196
+ "text/html": [
197
+ "<div>\n",
198
+ "<style scoped>\n",
199
+ " .dataframe tbody tr th:only-of-type {\n",
200
+ " vertical-align: middle;\n",
201
+ " }\n",
202
+ "\n",
203
+ " .dataframe tbody tr th {\n",
204
+ " vertical-align: top;\n",
205
+ " }\n",
206
+ "\n",
207
+ " .dataframe thead th {\n",
208
+ " text-align: right;\n",
209
+ " }\n",
210
+ "</style>\n",
211
+ "<table border=\"1\" class=\"dataframe\">\n",
212
+ " <thead>\n",
213
+ " <tr style=\"text-align: right;\">\n",
214
+ " <th></th>\n",
215
+ " <th>product_key</th>\n",
216
+ " <th>access</th>\n",
217
+ " <th>dataset</th>\n",
218
+ " <th>instrument_name</th>\n",
219
+ " <th>filters</th>\n",
220
+ " <th>filename</th>\n",
221
+ " <th>uri</th>\n",
222
+ " <th>authz_primary_identifier</th>\n",
223
+ " <th>authz_secondary_identifier</th>\n",
224
+ " <th>file_suffix</th>\n",
225
+ " <th>category</th>\n",
226
+ " <th>size</th>\n",
227
+ " <th>type</th>\n",
228
+ " </tr>\n",
229
+ " </thead>\n",
230
+ " <tbody>\n",
231
+ " <tr>\n",
232
+ " <th>28</th>\n",
233
+ " <td>jw02561001002_06101_00001_jw02561001002_06101_...</td>\n",
234
+ " <td>PUBLIC</td>\n",
235
+ " <td>jw02561001002_06101_00001</td>\n",
236
+ " <td>NIRCAM</td>\n",
237
+ " <td>NRCA3_FULL</td>\n",
238
+ " <td>jw02561001002_06101_00001_nrca3_uncal.fits</td>\n",
239
+ " <td>jw02561001002_06101_00001/jw02561001002_06101_...</td>\n",
240
+ " <td>jw02561001002_06101_00001_nrca3_uncal.fits</td>\n",
241
+ " <td>jw02561001002_06101_00001_nrca3_uncal.fits</td>\n",
242
+ " <td>_uncal</td>\n",
243
+ " <td>1b</td>\n",
244
+ " <td>75553920</td>\n",
245
+ " <td>science</td>\n",
246
+ " </tr>\n",
247
+ " <tr>\n",
248
+ " <th>50</th>\n",
249
+ " <td>jw02561001002_06101_00001_jw02561001002_06101_...</td>\n",
250
+ " <td>PUBLIC</td>\n",
251
+ " <td>jw02561001002_06101_00001</td>\n",
252
+ " <td>NIRCAM</td>\n",
253
+ " <td>NRCB3_FULL</td>\n",
254
+ " <td>jw02561001002_06101_00001_nrcb3_uncal.fits</td>\n",
255
+ " <td>jw02561001002_06101_00001/jw02561001002_06101_...</td>\n",
256
+ " <td>jw02561001002_06101_00001_nrcb3_uncal.fits</td>\n",
257
+ " <td>jw02561001002_06101_00001_nrcb3_uncal.fits</td>\n",
258
+ " <td>_uncal</td>\n",
259
+ " <td>1b</td>\n",
260
+ " <td>75553920</td>\n",
261
+ " <td>science</td>\n",
262
+ " </tr>\n",
263
+ " <tr>\n",
264
+ " <th>72</th>\n",
265
+ " <td>jw02561001002_06101_00001_jw02561001002_06101_...</td>\n",
266
+ " <td>PUBLIC</td>\n",
267
+ " <td>jw02561001002_06101_00001</td>\n",
268
+ " <td>NIRCAM</td>\n",
269
+ " <td>NRCA2_FULL</td>\n",
270
+ " <td>jw02561001002_06101_00001_nrca2_uncal.fits</td>\n",
271
+ " <td>jw02561001002_06101_00001/jw02561001002_06101_...</td>\n",
272
+ " <td>jw02561001002_06101_00001_nrca2_uncal.fits</td>\n",
273
+ " <td>jw02561001002_06101_00001_nrca2_uncal.fits</td>\n",
274
+ " <td>_uncal</td>\n",
275
+ " <td>1b</td>\n",
276
+ " <td>75553920</td>\n",
277
+ " <td>science</td>\n",
278
+ " </tr>\n",
279
+ " <tr>\n",
280
+ " <th>93</th>\n",
281
+ " <td>jw02561001002_06101_00001_jw02561001002_06101_...</td>\n",
282
+ " <td>PUBLIC</td>\n",
283
+ " <td>jw02561001002_06101_00001</td>\n",
284
+ " <td>NIRCAM</td>\n",
285
+ " <td>NRCB2_FULL</td>\n",
286
+ " <td>jw02561001002_06101_00001_nrcb2_uncal.fits</td>\n",
287
+ " <td>jw02561001002_06101_00001/jw02561001002_06101_...</td>\n",
288
+ " <td>jw02561001002_06101_00001_nrcb2_uncal.fits</td>\n",
289
+ " <td>jw02561001002_06101_00001_nrcb2_uncal.fits</td>\n",
290
+ " <td>_uncal</td>\n",
291
+ " <td>1b</td>\n",
292
+ " <td>75553920</td>\n",
293
+ " <td>science</td>\n",
294
+ " </tr>\n",
295
+ " <tr>\n",
296
+ " <th>114</th>\n",
297
+ " <td>jw02561001002_06101_00001_jw02561001002_06101_...</td>\n",
298
+ " <td>PUBLIC</td>\n",
299
+ " <td>jw02561001002_06101_00001</td>\n",
300
+ " <td>NIRCAM</td>\n",
301
+ " <td>NRCA4_FULL</td>\n",
302
+ " <td>jw02561001002_06101_00001_nrca4_uncal.fits</td>\n",
303
+ " <td>jw02561001002_06101_00001/jw02561001002_06101_...</td>\n",
304
+ " <td>jw02561001002_06101_00001_nrca4_uncal.fits</td>\n",
305
+ " <td>jw02561001002_06101_00001_nrca4_uncal.fits</td>\n",
306
+ " <td>_uncal</td>\n",
307
+ " <td>1b</td>\n",
308
+ " <td>75553920</td>\n",
309
+ " <td>science</td>\n",
310
+ " </tr>\n",
311
+ " <tr>\n",
312
+ " <th>...</th>\n",
313
+ " <td>...</td>\n",
314
+ " <td>...</td>\n",
315
+ " <td>...</td>\n",
316
+ " <td>...</td>\n",
317
+ " <td>...</td>\n",
318
+ " <td>...</td>\n",
319
+ " <td>...</td>\n",
320
+ " <td>...</td>\n",
321
+ " <td>...</td>\n",
322
+ " <td>...</td>\n",
323
+ " <td>...</td>\n",
324
+ " <td>...</td>\n",
325
+ " <td>...</td>\n",
326
+ " </tr>\n",
327
+ " <tr>\n",
328
+ " <th>216718</th>\n",
329
+ " <td>jw02130007001_03101_00002_jw02130007001_03101_...</td>\n",
330
+ " <td>PUBLIC</td>\n",
331
+ " <td>jw02130007001_03101_00002</td>\n",
332
+ " <td>NIRCAM</td>\n",
333
+ " <td>NRCB1_FULL</td>\n",
334
+ " <td>jw02130007001_03101_00002_nrcb1_uncal.fits</td>\n",
335
+ " <td>jw02130007001_03101_00002/jw02130007001_03101_...</td>\n",
336
+ " <td>jw02130007001_03101_00002_nrcb1_uncal.fits</td>\n",
337
+ " <td>jw02130007001_03101_00002_nrcb1_uncal.fits</td>\n",
338
+ " <td>_uncal</td>\n",
339
+ " <td>1b</td>\n",
340
+ " <td>75553920</td>\n",
341
+ " <td>science</td>\n",
342
+ " </tr>\n",
343
+ " <tr>\n",
344
+ " <th>216740</th>\n",
345
+ " <td>jw02130007001_03101_00002_jw02130007001_03101_...</td>\n",
346
+ " <td>PUBLIC</td>\n",
347
+ " <td>jw02130007001_03101_00002</td>\n",
348
+ " <td>NIRCAM</td>\n",
349
+ " <td>NRCA4_FULL</td>\n",
350
+ " <td>jw02130007001_03101_00002_nrca4_uncal.fits</td>\n",
351
+ " <td>jw02130007001_03101_00002/jw02130007001_03101_...</td>\n",
352
+ " <td>jw02130007001_03101_00002_nrca4_uncal.fits</td>\n",
353
+ " <td>jw02130007001_03101_00002_nrca4_uncal.fits</td>\n",
354
+ " <td>_uncal</td>\n",
355
+ " <td>1b</td>\n",
356
+ " <td>75553920</td>\n",
357
+ " <td>science</td>\n",
358
+ " </tr>\n",
359
+ " <tr>\n",
360
+ " <th>216786</th>\n",
361
+ " <td>jw02130007001_03101_00002_jw02130007001_03101_...</td>\n",
362
+ " <td>PUBLIC</td>\n",
363
+ " <td>jw02130007001_03101_00002</td>\n",
364
+ " <td>NIRCAM</td>\n",
365
+ " <td>NRCA1_FULL</td>\n",
366
+ " <td>jw02130007001_03101_00002_nrca1_uncal.fits</td>\n",
367
+ " <td>jw02130007001_03101_00002/jw02130007001_03101_...</td>\n",
368
+ " <td>jw02130007001_03101_00002_nrca1_uncal.fits</td>\n",
369
+ " <td>jw02130007001_03101_00002_nrca1_uncal.fits</td>\n",
370
+ " <td>_uncal</td>\n",
371
+ " <td>1b</td>\n",
372
+ " <td>75553920</td>\n",
373
+ " <td>science</td>\n",
374
+ " </tr>\n",
375
+ " <tr>\n",
376
+ " <th>216808</th>\n",
377
+ " <td>jw02130007001_03101_00002_jw02130007001_03101_...</td>\n",
378
+ " <td>PUBLIC</td>\n",
379
+ " <td>jw02130007001_03101_00002</td>\n",
380
+ " <td>NIRCAM</td>\n",
381
+ " <td>NRCA3_FULL</td>\n",
382
+ " <td>jw02130007001_03101_00002_nrca3_uncal.fits</td>\n",
383
+ " <td>jw02130007001_03101_00002/jw02130007001_03101_...</td>\n",
384
+ " <td>jw02130007001_03101_00002_nrca3_uncal.fits</td>\n",
385
+ " <td>jw02130007001_03101_00002_nrca3_uncal.fits</td>\n",
386
+ " <td>_uncal</td>\n",
387
+ " <td>1b</td>\n",
388
+ " <td>75553920</td>\n",
389
+ " <td>science</td>\n",
390
+ " </tr>\n",
391
+ " <tr>\n",
392
+ " <th>216830</th>\n",
393
+ " <td>jw02130007001_03101_00002_jw02130007001_03101_...</td>\n",
394
+ " <td>PUBLIC</td>\n",
395
+ " <td>jw02130007001_03101_00002</td>\n",
396
+ " <td>NIRCAM</td>\n",
397
+ " <td>NRCA2_FULL</td>\n",
398
+ " <td>jw02130007001_03101_00002_nrca2_uncal.fits</td>\n",
399
+ " <td>jw02130007001_03101_00002/jw02130007001_03101_...</td>\n",
400
+ " <td>jw02130007001_03101_00002_nrca2_uncal.fits</td>\n",
401
+ " <td>jw02130007001_03101_00002_nrca2_uncal.fits</td>\n",
402
+ " <td>_uncal</td>\n",
403
+ " <td>1b</td>\n",
404
+ " <td>75553920</td>\n",
405
+ " <td>science</td>\n",
406
+ " </tr>\n",
407
+ " </tbody>\n",
408
+ "</table>\n",
409
+ "<p>7537 rows × 13 columns</p>\n",
410
+ "</div>"
411
+ ],
412
+ "text/plain": [
413
+ " product_key access \\\n",
414
+ "28 jw02561001002_06101_00001_jw02561001002_06101_... PUBLIC \n",
415
+ "50 jw02561001002_06101_00001_jw02561001002_06101_... PUBLIC \n",
416
+ "72 jw02561001002_06101_00001_jw02561001002_06101_... PUBLIC \n",
417
+ "93 jw02561001002_06101_00001_jw02561001002_06101_... PUBLIC \n",
418
+ "114 jw02561001002_06101_00001_jw02561001002_06101_... PUBLIC \n",
419
+ "... ... ... \n",
420
+ "216718 jw02130007001_03101_00002_jw02130007001_03101_... PUBLIC \n",
421
+ "216740 jw02130007001_03101_00002_jw02130007001_03101_... PUBLIC \n",
422
+ "216786 jw02130007001_03101_00002_jw02130007001_03101_... PUBLIC \n",
423
+ "216808 jw02130007001_03101_00002_jw02130007001_03101_... PUBLIC \n",
424
+ "216830 jw02130007001_03101_00002_jw02130007001_03101_... PUBLIC \n",
425
+ "\n",
426
+ " dataset instrument_name filters \\\n",
427
+ "28 jw02561001002_06101_00001 NIRCAM NRCA3_FULL \n",
428
+ "50 jw02561001002_06101_00001 NIRCAM NRCB3_FULL \n",
429
+ "72 jw02561001002_06101_00001 NIRCAM NRCA2_FULL \n",
430
+ "93 jw02561001002_06101_00001 NIRCAM NRCB2_FULL \n",
431
+ "114 jw02561001002_06101_00001 NIRCAM NRCA4_FULL \n",
432
+ "... ... ... ... \n",
433
+ "216718 jw02130007001_03101_00002 NIRCAM NRCB1_FULL \n",
434
+ "216740 jw02130007001_03101_00002 NIRCAM NRCA4_FULL \n",
435
+ "216786 jw02130007001_03101_00002 NIRCAM NRCA1_FULL \n",
436
+ "216808 jw02130007001_03101_00002 NIRCAM NRCA3_FULL \n",
437
+ "216830 jw02130007001_03101_00002 NIRCAM NRCA2_FULL \n",
438
+ "\n",
439
+ " filename \\\n",
440
+ "28 jw02561001002_06101_00001_nrca3_uncal.fits \n",
441
+ "50 jw02561001002_06101_00001_nrcb3_uncal.fits \n",
442
+ "72 jw02561001002_06101_00001_nrca2_uncal.fits \n",
443
+ "93 jw02561001002_06101_00001_nrcb2_uncal.fits \n",
444
+ "114 jw02561001002_06101_00001_nrca4_uncal.fits \n",
445
+ "... ... \n",
446
+ "216718 jw02130007001_03101_00002_nrcb1_uncal.fits \n",
447
+ "216740 jw02130007001_03101_00002_nrca4_uncal.fits \n",
448
+ "216786 jw02130007001_03101_00002_nrca1_uncal.fits \n",
449
+ "216808 jw02130007001_03101_00002_nrca3_uncal.fits \n",
450
+ "216830 jw02130007001_03101_00002_nrca2_uncal.fits \n",
451
+ "\n",
452
+ " uri \\\n",
453
+ "28 jw02561001002_06101_00001/jw02561001002_06101_... \n",
454
+ "50 jw02561001002_06101_00001/jw02561001002_06101_... \n",
455
+ "72 jw02561001002_06101_00001/jw02561001002_06101_... \n",
456
+ "93 jw02561001002_06101_00001/jw02561001002_06101_... \n",
457
+ "114 jw02561001002_06101_00001/jw02561001002_06101_... \n",
458
+ "... ... \n",
459
+ "216718 jw02130007001_03101_00002/jw02130007001_03101_... \n",
460
+ "216740 jw02130007001_03101_00002/jw02130007001_03101_... \n",
461
+ "216786 jw02130007001_03101_00002/jw02130007001_03101_... \n",
462
+ "216808 jw02130007001_03101_00002/jw02130007001_03101_... \n",
463
+ "216830 jw02130007001_03101_00002/jw02130007001_03101_... \n",
464
+ "\n",
465
+ " authz_primary_identifier \\\n",
466
+ "28 jw02561001002_06101_00001_nrca3_uncal.fits \n",
467
+ "50 jw02561001002_06101_00001_nrcb3_uncal.fits \n",
468
+ "72 jw02561001002_06101_00001_nrca2_uncal.fits \n",
469
+ "93 jw02561001002_06101_00001_nrcb2_uncal.fits \n",
470
+ "114 jw02561001002_06101_00001_nrca4_uncal.fits \n",
471
+ "... ... \n",
472
+ "216718 jw02130007001_03101_00002_nrcb1_uncal.fits \n",
473
+ "216740 jw02130007001_03101_00002_nrca4_uncal.fits \n",
474
+ "216786 jw02130007001_03101_00002_nrca1_uncal.fits \n",
475
+ "216808 jw02130007001_03101_00002_nrca3_uncal.fits \n",
476
+ "216830 jw02130007001_03101_00002_nrca2_uncal.fits \n",
477
+ "\n",
478
+ " authz_secondary_identifier file_suffix category \\\n",
479
+ "28 jw02561001002_06101_00001_nrca3_uncal.fits _uncal 1b \n",
480
+ "50 jw02561001002_06101_00001_nrcb3_uncal.fits _uncal 1b \n",
481
+ "72 jw02561001002_06101_00001_nrca2_uncal.fits _uncal 1b \n",
482
+ "93 jw02561001002_06101_00001_nrcb2_uncal.fits _uncal 1b \n",
483
+ "114 jw02561001002_06101_00001_nrca4_uncal.fits _uncal 1b \n",
484
+ "... ... ... ... \n",
485
+ "216718 jw02130007001_03101_00002_nrcb1_uncal.fits _uncal 1b \n",
486
+ "216740 jw02130007001_03101_00002_nrca4_uncal.fits _uncal 1b \n",
487
+ "216786 jw02130007001_03101_00002_nrca1_uncal.fits _uncal 1b \n",
488
+ "216808 jw02130007001_03101_00002_nrca3_uncal.fits _uncal 1b \n",
489
+ "216830 jw02130007001_03101_00002_nrca2_uncal.fits _uncal 1b \n",
490
+ "\n",
491
+ " size type \n",
492
+ "28 75553920 science \n",
493
+ "50 75553920 science \n",
494
+ "72 75553920 science \n",
495
+ "93 75553920 science \n",
496
+ "114 75553920 science \n",
497
+ "... ... ... \n",
498
+ "216718 75553920 science \n",
499
+ "216740 75553920 science \n",
500
+ "216786 75553920 science \n",
501
+ "216808 75553920 science \n",
502
+ "216830 75553920 science \n",
503
+ "\n",
504
+ "[7537 rows x 13 columns]"
505
+ ]
506
+ },
507
+ "execution_count": 189,
508
+ "metadata": {},
509
+ "output_type": "execute_result"
510
+ }
511
+ ],
512
+ "source": [
513
+ "resultsdf"
514
+ ]
515
+ },
516
+ {
517
+ "cell_type": "code",
518
+ "execution_count": 188,
519
+ "id": "f086c0f9-9ef6-4945-a53a-f3cf051b4dee",
520
+ "metadata": {},
521
+ "outputs": [],
522
+ "source": [
523
+ "resultsdf.to_csv(\"all_jwst_uris.csv\")"
524
+ ]
525
+ },
526
+ {
527
+ "cell_type": "code",
528
+ "execution_count": 145,
529
+ "id": "09ca516e-3bbb-4a2b-9c6e-984c9a7e801a",
530
+ "metadata": {},
531
+ "outputs": [
532
+ {
533
+ "name": "stdout",
534
+ "output_type": "stream",
535
+ "text": [
536
+ "Symmetric?\n",
537
+ "True\n",
538
+ "(475, 475)\n"
539
+ ]
540
+ }
541
+ ],
542
+ "source": [
543
+ "# Array of latitudes and longitudes\n",
544
+ "# MAKE SURE TO PUT RA=LON, DEC=LAT\n",
545
+ "latitudes = np.array(multi_index_df.groupby(level=0).first()[DEC_NAME]) # Example latitudes\n",
546
+ "longitudes = np.array(multi_index_df.groupby(level=0).first()[RA_NAME]) # Example longitudes\n",
547
+ "\n",
548
+ "n_points = len(latitudes)\n",
549
+ "\n",
550
+ "# Repeat each point n_points times for lat1, lon1\n",
551
+ "lat1 = np.repeat(latitudes, n_points)\n",
552
+ "lon1 = np.repeat(longitudes, n_points)\n",
553
+ "\n",
554
+ "# Tile the whole array n_points times for lat2, lon2\n",
555
+ "lat2 = np.tile(latitudes, n_points)\n",
556
+ "lon2 = np.tile(longitudes, n_points)\n",
557
+ "\n",
558
+ "# Calculates angular separation between two spherical coords\n",
559
+ "# This can be lat/lon or ra/dec\n",
560
+ "# Taken from astropy\n",
561
+ "def angular_separation_deg(lon1, lat1, lon2, lat2):\n",
562
+ " lon1 = np.deg2rad(lon1)\n",
563
+ " lon2 = np.deg2rad(lon2)\n",
564
+ " lat1 = np.deg2rad(lat1)\n",
565
+ " lat2 = np.deg2rad(lat2)\n",
566
+ " \n",
567
+ " sdlon = np.sin(lon2 - lon1)\n",
568
+ " cdlon = np.cos(lon2 - lon1)\n",
569
+ " slat1 = np.sin(lat1)\n",
570
+ " slat2 = np.sin(lat2)\n",
571
+ " clat1 = np.cos(lat1)\n",
572
+ " clat2 = np.cos(lat2)\n",
573
+ "\n",
574
+ " num1 = clat2 * sdlon\n",
575
+ " num2 = clat1 * slat2 - slat1 * clat2 * cdlon\n",
576
+ " denominator = slat1 * slat2 + clat1 * clat2 * cdlon\n",
577
+ "\n",
578
+ " return np.rad2deg(np.arctan2(np.hypot(num1, num2), denominator))\n",
579
+ "\n",
580
+ "# Compute the pairwise angular separations\n",
581
+ "angular_separations = angular_separation_deg(lon1, lat1, lon2, lat2)\n",
582
+ "\n",
583
+ "# Reshape the result into a matrix form\n",
584
+ "angular_separations_matrix = angular_separations.reshape(n_points, n_points)\n",
585
+ "\n",
586
+ "def check_symmetric(a, rtol=1e-05, atol=1e-07):\n",
587
+ " return np.allclose(a, a.T, rtol=rtol, atol=atol)\n",
588
+ "\n",
589
+ "print(\"Symmetric?\")\n",
590
+ "print(check_symmetric(angular_separations_matrix))\n",
591
+ "print(angular_separations_matrix.shape)"
592
+ ]
593
+ },
594
+ {
595
+ "cell_type": "code",
596
+ "execution_count": 84,
597
+ "id": "85edd94e-5591-4c7c-8251-b8c976962f72",
598
+ "metadata": {},
599
+ "outputs": [
600
+ {
601
+ "name": "stdout",
602
+ "output_type": "stream",
603
+ "text": [
604
+ "24 40\n",
605
+ "12 40\n",
606
+ "76 36\n",
607
+ "34 30\n",
608
+ "7 24\n",
609
+ " ..\n",
610
+ "221 1\n",
611
+ "166 1\n",
612
+ "139 1\n",
613
+ "176 1\n",
614
+ "20 1\n",
615
+ "Length: 311, dtype: int64\n"
616
+ ]
617
+ },
618
+ {
619
+ "name": "stderr",
620
+ "output_type": "stream",
621
+ "text": [
622
+ "100%|███████████████████████████████████████| 311/311 [00:00<00:00, 3456.86it/s]"
623
+ ]
624
+ },
625
+ {
626
+ "name": "stdout",
627
+ "output_type": "stream",
628
+ "text": [
629
+ "Max subset with minimum distance: 321\n",
630
+ "1110\n"
631
+ ]
632
+ },
633
+ {
634
+ "name": "stderr",
635
+ "output_type": "stream",
636
+ "text": [
637
+ "\n"
638
+ ]
639
+ }
640
+ ],
641
+ "source": [
642
+ "#HUBBLE_FOV = 0.057\n",
643
+ "JWST_FOV = 0.0366667\n",
644
+ "\n",
645
+ "THRESH = JWST_FOV\n",
646
+ "\n",
647
+ "clustering = AgglomerativeClustering(n_clusters=None, metric='precomputed', linkage='single', distance_threshold=THRESH)\n",
648
+ "labels = clustering.fit_predict(angular_separations_matrix)\n",
649
+ "\n",
650
+ "multi_index_df['label'] = labels\n",
651
+ "\n",
652
+ "print(pd.Series(labels).value_counts())\n",
653
+ "\n",
654
+ "def max_subset_with_min_distance(points, min_distance):\n",
655
+ " subset = []\n",
656
+ " for i, row in points.iterrows():\n",
657
+ " 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",
658
+ " subset.append(row)\n",
659
+ " return subset\n",
660
+ "\n",
661
+ "all_subsets = []\n",
662
+ "\n",
663
+ "for label in tqdm(np.unique(labels)):\n",
664
+ " cds = multi_index_df[multi_index_df['label'] == label]\n",
665
+ " subset = max_subset_with_min_distance(cds, THRESH)\n",
666
+ " all_subsets.extend(subset)\n",
667
+ "\n",
668
+ "print(\"Max subset with minimum distance:\", len(all_subsets))\n",
669
+ "\n",
670
+ "locations = pd.DataFrame(all_subsets)\n",
671
+ "\n",
672
+ "df = pd.merge(df, locations, on=[RA_NAME, DEC_NAME], how='right')\n",
673
+ "\n",
674
+ "print(len(df))"
675
+ ]
676
+ },
677
+ {
678
+ "cell_type": "code",
679
+ "execution_count": 107,
680
+ "id": "55a9d951-3210-4a7f-aca8-a0a80628a8ac",
681
+ "metadata": {},
682
+ "outputs": [],
683
+ "source": [
684
+ "detectors = ['nrca1', 'nrca2', 'nrca3', 'nrca4', 'nrcb1', 'nrcb2', 'nrcb3', 'nrcb4']\n",
685
+ "obsids_search = []\n",
686
+ "\n",
687
+ "for fileset in df['fileSetName']:\n",
688
+ " for detector in detectors:\n",
689
+ " obsids_search.append(fileset + \"_\" + detector)"
690
+ ]
691
+ },
692
+ {
693
+ "cell_type": "code",
694
+ "execution_count": 108,
695
+ "id": "55ae58fc-5c2f-46ec-8fa7-d7229fd61b5f",
696
+ "metadata": {},
697
+ "outputs": [],
698
+ "source": [
699
+ "from astroquery.mast import Observations\n",
700
+ "\n",
701
+ "# Query for data with the specified obs_id\n",
702
+ "result = Observations.query_criteria(obs_id=obsids_search)"
703
+ ]
704
+ },
705
+ {
706
+ "cell_type": "code",
707
+ "execution_count": 120,
708
+ "id": "5a29aaf9-dd68-4fa2-910a-8d399f4580cd",
709
+ "metadata": {},
710
+ "outputs": [
711
+ {
712
+ "name": "stderr",
713
+ "output_type": "stream",
714
+ "text": [
715
+ "100%|█████████████████████████████████████████| 108/108 [01:07<00:00, 1.60it/s]\n"
716
+ ]
717
+ },
718
+ {
719
+ "name": "stdout",
720
+ "output_type": "stream",
721
+ "text": [
722
+ "There are 0 unique files, which are 0.0 GB in size.\n"
723
+ ]
724
+ },
725
+ {
726
+ "ename": "ValueError",
727
+ "evalue": "no values provided to stack.",
728
+ "output_type": "error",
729
+ "traceback": [
730
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
731
+ "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
732
+ "Input \u001b[0;32mIn [120]\u001b[0m, in \u001b[0;36m<cell line: 25>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 21\u001b[0m files[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mobs_id_short\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m files[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mobs_id\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mstr[:\u001b[38;5;241m6\u001b[39m]\n\u001b[1;32m 23\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThere are \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(files)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m unique files, which are \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28msum\u001b[39m(files[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msize\u001b[39m\u001b[38;5;124m'\u001b[39m])\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m10\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m9\u001b[39m\u001b[38;5;132;01m:\u001b[39;00m\u001b[38;5;124m.1f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m GB in size.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 25\u001b[0m manifest \u001b[38;5;241m=\u001b[39m \u001b[43mObservations\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdownload_products\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfiles\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mobsID\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcurl_flag\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n",
733
+ "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/astroquery/mast/observations.py:715\u001b[0m, in \u001b[0;36mObservationsClass.download_products\u001b[0;34m(self, products, download_dir, cache, curl_flag, mrp_only, cloud_only, **filters)\u001b[0m\n\u001b[1;32m 712\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m oid \u001b[38;5;129;01min\u001b[39;00m products:\n\u001b[1;32m 713\u001b[0m product_lists\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_product_list(oid))\n\u001b[0;32m--> 715\u001b[0m products \u001b[38;5;241m=\u001b[39m \u001b[43mvstack\u001b[49m\u001b[43m(\u001b[49m\u001b[43mproduct_lists\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 717\u001b[0m \u001b[38;5;66;03m# apply filters\u001b[39;00m\n\u001b[1;32m 718\u001b[0m products \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfilter_products(products, mrp_only\u001b[38;5;241m=\u001b[39mmrp_only, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mfilters)\n",
734
+ "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/astropy/table/operations.py:677\u001b[0m, in \u001b[0;36mvstack\u001b[0;34m(tables, join_type, metadata_conflicts)\u001b[0m\n\u001b[1;32m 623\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 624\u001b[0m \u001b[38;5;124;03mStack tables vertically (along rows).\u001b[39;00m\n\u001b[1;32m 625\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 673\u001b[0m \u001b[38;5;124;03m 6 8\u001b[39;00m\n\u001b[1;32m 674\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 675\u001b[0m _check_join_type(join_type, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvstack\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 677\u001b[0m tables \u001b[38;5;241m=\u001b[39m \u001b[43m_get_list_of_tables\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtables\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# validates input\u001b[39;00m\n\u001b[1;32m 678\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(tables) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 679\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m tables[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;66;03m# no point in stacking a single table\u001b[39;00m\n",
735
+ "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/astropy/table/operations.py:60\u001b[0m, in \u001b[0;36m_get_list_of_tables\u001b[0;34m(tables)\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[38;5;66;03m# Make sure there is something to stack\u001b[39;00m\n\u001b[1;32m 59\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(tables) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 60\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mno values provided to stack.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 62\u001b[0m \u001b[38;5;66;03m# Convert inputs (Table, Row, or anything column-like) to Tables.\u001b[39;00m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;66;03m# Special case that Quantity converts to a QTable.\u001b[39;00m\n\u001b[1;32m 64\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m ii, val \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(tables):\n",
736
+ "\u001b[0;31mValueError\u001b[0m: no values provided to stack."
737
+ ]
738
+ }
739
+ ],
740
+ "source": [
741
+ "from astropy.table import unique, vstack, Table\n",
742
+ "\n",
743
+ "matched_obs = result\n",
744
+ "\n",
745
+ "# Split the observations into \"chunks\" of size five\n",
746
+ "sz_chunk = 5\n",
747
+ "chunks = [matched_obs[i:i+sz_chunk] for i in range(0,len(matched_obs), sz_chunk)]\n",
748
+ "\n",
749
+ "# Get the list of products for each chunk\n",
750
+ "t = []\n",
751
+ "for chunk in tqdm(chunks):\n",
752
+ " t.append(Observations.get_product_list(chunk))\n",
753
+ "\n",
754
+ "files = unique(vstack(t), keys='productFilename')\n",
755
+ "files = files.to_pandas()\n",
756
+ "\n",
757
+ "# Ensure we only keep raw data files\n",
758
+ "files = files[files['productSubGroupDescription'] == 'UNCAL']\n",
759
+ "\n",
760
+ "# Create a shortened identified\n",
761
+ "files['obs_id_short'] = files['obs_id'].str[:6]\n",
762
+ "\n",
763
+ "print(f\"There are {len(files)} unique files, which are {sum(files['size'])/10**9:.1f} GB in size.\")\n",
764
+ "\n",
765
+ "manifest = Observations.download_products(files['obsID'], curl_flag=True)"
766
+ ]
767
+ },
768
+ {
769
+ "cell_type": "code",
770
+ "execution_count": null,
771
+ "id": "b2307af8-d6ea-4969-8b8a-4d42f21d674f",
772
+ "metadata": {},
773
+ "outputs": [],
774
+ "source": [
775
+ "files[['obsID']].to_csv('list_of_hubble_filenames.csv')"
776
+ ]
777
+ },
778
+ {
779
+ "cell_type": "code",
780
+ "execution_count": null,
781
+ "id": "ab5d1fd5-502f-4b94-a8f1-23636043f779",
782
+ "metadata": {},
783
+ "outputs": [],
784
+ "source": [
785
+ "manifest = Observations.download_products(files['obsID'], curl_flag=True)"
786
+ ]
787
+ },
788
+ {
789
+ "cell_type": "code",
790
+ "execution_count": null,
791
+ "id": "b8b7937f-ca1d-4b4c-abef-1055cdf115ef",
792
+ "metadata": {},
793
+ "outputs": [],
794
+ "source": [
795
+ "wi"
796
+ ]
797
+ },
798
+ {
799
+ "cell_type": "code",
800
+ "execution_count": null,
801
+ "id": "556fdab2-517e-466c-a9ee-c7b629df5276",
802
+ "metadata": {},
803
+ "outputs": [],
804
+ "source": [
805
+ "import numpy as np\n",
806
+ "from astropy.io import fits\n",
807
+ "from astropy.table import Table\n",
808
+ "import glob\n",
809
+ "\n",
810
+ "def create_combined_hubble_file(short_obs_id):\n",
811
+ " \n",
812
+ " file_list = list(files[files['obs_id_short'] == short_obs_id]['productFilename'])\n",
813
+ " \n",
814
+ " ccd1_data = []\n",
815
+ " ccd2_data = []\n",
816
+ " ccd1_times = []\n",
817
+ " ccd2_times = []\n",
818
+ " ccd1_headers = []\n",
819
+ " ccd2_headers = []\n",
820
+ "\n",
821
+ " for file in file_list:\n",
822
+ " with fits.open(file) as hdul:\n",
823
+ " # Extract TIME-OBS from the primary header\n",
824
+ " time_obs = hdul[0].header['TIME-OBS']\n",
825
+ "\n",
826
+ " # Extract data and headers from HDUs\n",
827
+ " for hdu in [hdul[1], hdul[4]]:\n",
828
+ " if hdu.header['CCDCHIP'] == 1:\n",
829
+ " ccd1_data.append(hdu.data)\n",
830
+ " ccd1_times.append(time_obs)\n",
831
+ " ccd1_headers.append(hdul[0].header.copy())\n",
832
+ " elif hdu.header['CCDCHIP'] == 2:\n",
833
+ " ccd2_data.append(hdu.data)\n",
834
+ " ccd2_times.append(time_obs)\n",
835
+ " ccd2_headers.append(hdul[0].header.copy())\n",
836
+ "\n",
837
+ " # Sort the data based on TIME-OBS\n",
838
+ " ccd1_times, ccd1_data, ccd1_headers = zip(*sorted(zip(ccd1_times, ccd1_data, ccd1_headers)))\n",
839
+ " ccd2_times, ccd2_data, ccd2_headers = zip(*sorted(zip(ccd2_times, ccd2_data, ccd2_headers)))\n",
840
+ "\n",
841
+ " # Concatenate the data for each CCDCHIP\n",
842
+ " ccd1_concat = np.stack(ccd1_data)\n",
843
+ " ccd2_concat = np.stack(ccd2_data)\n",
844
+ "\n",
845
+ " # Function to create a new FITS file for a given CCDCHIP\n",
846
+ " def create_fits_file(output_file, ccd_data, ccd_headers, ccd_chip):\n",
847
+ "\n",
848
+ " primary_hdu = fits.PrimaryHDU()\n",
849
+ " primary_hdu.header['EXTEND'] = True\n",
850
+ " primary_hdu.header['CCDCHIP'] = ccd_chip\n",
851
+ "\n",
852
+ " metadata_hdus = [fits.ImageHDU(header=header) for header in ccd_headers]\n",
853
+ "\n",
854
+ " # Create ImageHDU with concatenated data\n",
855
+ " image_hdu = fits.ImageHDU(data=ccd_data, header=fits.Header({'CCDCHIP': ccd_chip}))\n",
856
+ "\n",
857
+ " # Create HDUList and write to a new FITS file\n",
858
+ " hdulist = fits.HDUList([primary_hdu] + [image_hdu] + metadata_hdus)\n",
859
+ " hdulist.writeto(output_file, overwrite=True)\n",
860
+ "\n",
861
+ " # Create FITS files for CCDCHIP 1 and 2\n",
862
+ " create_fits_file(f'{short_obs_id}_ccd1.fits', ccd1_concat, ccd1_headers, 1)\n",
863
+ " create_fits_file(f'{short_obs_id}_ccd2.fits', ccd2_concat, ccd2_headers, 2)\n",
864
+ "\n",
865
+ " print(\"New FITS files created successfully.\")"
866
+ ]
867
+ },
868
+ {
869
+ "cell_type": "code",
870
+ "execution_count": null,
871
+ "id": "d09be044-ce2e-4cb5-ae1f-9562f9ac6fa3",
872
+ "metadata": {},
873
+ "outputs": [],
874
+ "source": [
875
+ "def visualize_label(df, LABEL=None):\n",
876
+ " \n",
877
+ " cds = df\n",
878
+ " \n",
879
+ " if LABEL is not None:\n",
880
+ " cds = cds[labels == LABEL]\n",
881
+ "\n",
882
+ " ras, decs = [], []\n",
883
+ " cmap = []\n",
884
+ " \n",
885
+ " print(len(cds))\n",
886
+ "\n",
887
+ " for i, cd in cds.iterrows():\n",
888
+ " ras.append(cd[RA_NAME])\n",
889
+ " decs.append(cd[DEC_NAME])\n",
890
+ "\n",
891
+ " if LABEL is None:\n",
892
+ " # No gridlines, will be overwhelming\n",
893
+ "\n",
894
+ " # Compute the 2D histogram\n",
895
+ " # Number of bins for x and y; adjust these based on your dataset\n",
896
+ " bins = (30, 30)\n",
897
+ "\n",
898
+ " # Compute the histogram\n",
899
+ " hist, xedges, yedges = np.histogram2d(ras, decs, bins=bins)\n",
900
+ " \n",
901
+ " N_cutoff = 50\n",
902
+ " hist = np.clip(hist, 0, N_cutoff)\n",
903
+ "\n",
904
+ " # Generate a 2D histogram plot\n",
905
+ " plt.figure(figsize=(8, 6))\n",
906
+ " plt.imshow(hist, interpolation='nearest', origin='lower',\n",
907
+ " extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],\n",
908
+ " cmap='viridis') # Choose a colormap (e.g., 'viridis', 'plasma', 'inferno')\n",
909
+ "\n",
910
+ " # Add labels and title if necessary\n",
911
+ " plt.colorbar(label='Number of points in bin')\n",
912
+ " plt.xlabel('RA')\n",
913
+ " plt.ylabel('DEC')\n",
914
+ " plt.title(f'2D Histogram of Point Density, clipped to {N_cutoff} points')\n",
915
+ "\n",
916
+ " # Show the plot\n",
917
+ " plt.show()\n",
918
+ "\n",
919
+ " return\n",
920
+ " else:\n",
921
+ " fig = plt.figure()\n",
922
+ " ax = fig.gca()\n",
923
+ " ax.set_xticks(np.arange(np.min(ras), np.max(ras), THRESH))\n",
924
+ " ax.set_yticks(np.arange(np.min(decs), np.max(decs), THRESH))\n",
925
+ " plt.scatter(ras, decs, alpha=0.1)\n",
926
+ " plt.grid()\n",
927
+ " plt.show()\n",
928
+ "\n",
929
+ "visualize_label(df, 7)"
930
+ ]
931
+ },
932
+ {
933
+ "cell_type": "code",
934
+ "execution_count": null,
935
+ "id": "35a648ab-07cd-409e-be32-125ead927bdf",
936
+ "metadata": {},
937
+ "outputs": [],
938
+ "source": [
939
+ "from sklearn.model_selection import train_test_split\n",
940
+ "\n",
941
+ "data = list(range(len(labels)))\n",
942
+ "\n",
943
+ "# Perform the train-test split with an 80-20 ratio\n",
944
+ "train_indices, test_indices = train_test_split(data, test_size=0.2, random_state=42)"
945
+ ]
946
+ },
947
+ {
948
+ "cell_type": "code",
949
+ "execution_count": null,
950
+ "id": "8f6a0472-b8fc-4989-a506-39019914c853",
951
+ "metadata": {},
952
+ "outputs": [],
953
+ "source": [
954
+ "sdss_test = all_sdss_data[all_sdss_data['cluster_label'].isin(test_indices)]\n",
955
+ "sdss_train = all_sdss_data[all_sdss_data['cluster_label'].isin(train_indices)]"
956
+ ]
957
+ },
958
+ {
959
+ "cell_type": "code",
960
+ "execution_count": null,
961
+ "id": "bfbe8686-e580-4285-bd8f-43154d18182b",
962
+ "metadata": {},
963
+ "outputs": [],
964
+ "source": [
965
+ "len(sdss_test)"
966
+ ]
967
+ },
968
+ {
969
+ "cell_type": "code",
970
+ "execution_count": null,
971
+ "id": "cacb74dd-0d54-43d7-a301-ea4eb7b3e07e",
972
+ "metadata": {},
973
+ "outputs": [],
974
+ "source": [
975
+ "len(sdss_train)"
976
+ ]
977
+ },
978
+ {
979
+ "cell_type": "code",
980
+ "execution_count": null,
981
+ "id": "f0fb157d-1307-42d9-ac57-7e6aa3cecd84",
982
+ "metadata": {},
983
+ "outputs": [],
984
+ "source": [
985
+ "sdss_test"
986
+ ]
987
+ },
988
+ {
989
+ "cell_type": "code",
990
+ "execution_count": null,
991
+ "id": "62fb160e-74e5-4f50-96ee-109a6261370f",
992
+ "metadata": {},
993
+ "outputs": [],
994
+ "source": [
995
+ "test_data = pd.DataFrame(test_data)\n",
996
+ "train_data = pd.DataFrame(train_data)"
997
+ ]
998
+ },
999
+ {
1000
+ "cell_type": "code",
1001
+ "execution_count": null,
1002
+ "id": "e7b1379c-d5a9-4f3a-ac68-e8b5a46f4c8a",
1003
+ "metadata": {},
1004
+ "outputs": [],
1005
+ "source": [
1006
+ "\"\"\"\n",
1007
+ "Code to verify test/train pollution.\n",
1008
+ "\"\"\"\n",
1009
+ "\n",
1010
+ "\n",
1011
+ "import json\n",
1012
+ "from math import radians, sin, cos, sqrt, atan2, degrees\n",
1013
+ "import numpy as np\n",
1014
+ "import matplotlib.pyplot as plt\n",
1015
+ "\n",
1016
+ "\n",
1017
+ "# Function to load data from file\n",
1018
+ "def load_data(file_path):\n",
1019
+ " data = []\n",
1020
+ " with open(file_path, 'r') as f:\n",
1021
+ " for line in f:\n",
1022
+ " data.append(json.loads(line))\n",
1023
+ " return data\n",
1024
+ "\n",
1025
+ "# Load data\n",
1026
+ "train_file_path = '/Users/rithwik/Desktop/full_train.jsonl.txt'\n",
1027
+ "test_file_path = '/Users/rithwik/Desktop/full_test.jsonl.txt'\n",
1028
+ "#train_data = sdss_train\n",
1029
+ "#test_data = sdss_test\n",
1030
+ "\n",
1031
+ "# Define the threshold\n",
1032
+ "threshold = 0.09*3\n",
1033
+ "\n",
1034
+ "# Find test dataset rows with a minimum great circle distance less than the threshold\n",
1035
+ "close_pairs = []\n",
1036
+ "\n",
1037
+ "for i, test_point in test_data.iterrows():\n",
1038
+ " ra_test, dec_test = test_point['ra'], test_point['dec']\n",
1039
+ " distances = [(train_point, angular_separation_deg(ra_test, dec_test, train_point['ra'], train_point['dec'])) for i, train_point in train_data.iterrows()]\n",
1040
+ " closest_train_point, min_distance = min(distances, key=lambda x: x[1])\n",
1041
+ " if min_distance < threshold:\n",
1042
+ " close_pairs.append((test_point, closest_train_point, min_distance))\n",
1043
+ "\n",
1044
+ "close_pairs_summary = [\n",
1045
+ " {\n",
1046
+ " \"test_image_id\": test_point['image_id'],\n",
1047
+ " \"test_ra\": test_point['ra'],\n",
1048
+ " \"test_dec\": test_point['dec'],\n",
1049
+ " \"train_image_id\": closest_train_point['image_id'],\n",
1050
+ " \"train_ra\": closest_train_point['ra'],\n",
1051
+ " \"train_dec\": closest_train_point['dec'],\n",
1052
+ " \"min_distance_deg\": min_distance\n",
1053
+ " }\n",
1054
+ " for test_point, closest_train_point, min_distance in close_pairs\n",
1055
+ "]\n",
1056
+ "\n",
1057
+ "# Print the results\n",
1058
+ "result = \"Success\"\n",
1059
+ "for pair in close_pairs_summary:\n",
1060
+ " print(pair)\n",
1061
+ " result = \"FAIL\"\n",
1062
+ " \n",
1063
+ "print(f\"Done. result is {result}\")"
1064
+ ]
1065
+ },
1066
+ {
1067
+ "cell_type": "code",
1068
+ "execution_count": null,
1069
+ "id": "93cf2978-0799-4d6c-93ec-921f4f41e10c",
1070
+ "metadata": {},
1071
+ "outputs": [],
1072
+ "source": [
1073
+ "sdss_test.to_json('full_test1.jsonl', orient='records', lines=True)\n",
1074
+ "sdss_train.to_json('full_train1.jsonl', orient='records', lines=True)"
1075
+ ]
1076
+ }
1077
+ ],
1078
+ "metadata": {
1079
+ "kernelspec": {
1080
+ "display_name": "Python 3 (ipykernel)",
1081
+ "language": "python",
1082
+ "name": "python3"
1083
+ },
1084
+ "language_info": {
1085
+ "codemirror_mode": {
1086
+ "name": "ipython",
1087
+ "version": 3
1088
+ },
1089
+ "file_extension": ".py",
1090
+ "mimetype": "text/x-python",
1091
+ "name": "python",
1092
+ "nbconvert_exporter": "python",
1093
+ "pygments_lexer": "ipython3",
1094
+ "version": "3.10.13"
1095
+ }
1096
+ },
1097
+ "nbformat": 4,
1098
+ "nbformat_minor": 5
1099
+ }
utils/jwst_filtering.ipynb ADDED
The diff for this file is too large to render. See raw diff