From 0e968084d7e72c55050f7553611eeea314ddafe5 Mon Sep 17 00:00:00 2001 From: Tomas Stolker Date: Sun, 29 Oct 2023 12:56:17 +0100 Subject: [PATCH] Added the write_json and write_csv parameters --- calistar/calistar.py | 104 ++++++++++++++++++++++++++++++------------- docs/tutorial.ipynb | 13 +++--- 2 files changed, 82 insertions(+), 35 deletions(-) diff --git a/calistar/calistar.py b/calistar/calistar.py index 2f22ab1..b3f6487 100644 --- a/calistar/calistar.py +++ b/calistar/calistar.py @@ -167,12 +167,22 @@ def __init__( @typechecked def target_star( self, + write_json: bool = True, ) -> Dict[str, Union[str, float]]: """ Function for retrieving the the astrometric and photometric properties of a target star of interest. The function returns a dictionary with the properties, but it - also stores the data in a JSON file in the working folder. + also (optionally) stores the data in a JSON file in the + working folder. + + Parameters + ---------- + write_json : bool + Write the target properties to a JSON file (default: True). + The file will be stored in the working folder and starts + with ``target_``. The filename contains also the Gaia + release and the Gaia source ID of the target. Returns ------- @@ -377,16 +387,7 @@ def target_star( print(f"G-band extinction = {gaia_result['ag_gspphot'][0]:.2f}") if "non_single_star" in gaia_result.columns: - if gaia_result["non_single_star"][0] == 0: - print("\nNon single star = False") - - elif gaia_result["non_single_star"][0] == 1: - print("\nNon single star = True") - - else: - warnings.warn( - f"The 'non_single_star' value is {gaia_result['non_single_star'][0]}" - ) + print(f"\nNon single star = {gaia_result['non_single_star'][0]}") if "classprob_dsc_combmod_star" in gaia_result.columns: print( @@ -530,10 +531,12 @@ def target_star( float(vizier_result["e_W4mag"]), ) - json_file = f"target_{self.gaia_release.lower()}_{self.gaia_source}.json" + if write_json: + json_file = f"target_{self.gaia_release.lower()}_{self.gaia_source}.json" + print(f"\nStoring output: {json_file}") - with open(json_file, "w", encoding="utf-8") as open_file: - json.dump(target_dict, open_file, indent=4) + with open(json_file, "w", encoding="utf-8") as open_file: + json.dump(target_dict, open_file, indent=4) return target_dict @@ -542,12 +545,13 @@ def find_calib( self, search_radius: float = 0.1, g_mag_range: Optional[Tuple[float, float]] = None, + write_csv: bool = True, ) -> pd.DataFrame: """ Function for finding calibration stars. The function returns a ``DataFrame`` with the sources that are queried from the Gaia - catalog, but it also stores the data in a CSV file in the - working folder. The table also contains 2MASS and WISE + catalog, but it also (optionally) stores the data in a CSV file + in the working folder. The table also contains 2MASS and WISE magnitudes, and data from The Washington Visual Double Star Catalog. It is recommended to open the CSV file in a spreadsheet editor for easy visualization. @@ -573,6 +577,11 @@ def find_calib( :math:`\\pm` 1.0 mag (i.e. ``g_mag_range=(-1.0, 1.0)``) is used if the argument of ``g_mag_range`` is set to ``None``. + write_csv : bool + Write the table with found source to a CSV file (default: + True). The file will be stored in the working folder and + starts with ``calib_find_``. The filename contains also + the Gaia release and the Gaia source ID of the target. Returns ------- @@ -580,9 +589,12 @@ def find_calib( A ``DataFrame`` with the table of queried sources. """ - print("\n-> Finding calibration stars...\n") + json_file = Path(f"target_{self.gaia_release.lower()}_{self.gaia_source}.json") - json_file = f"target_{self.gaia_release.lower()}_{self.gaia_source}.json" + if not json_file.exists(): + self.target_star(write_json=True) + + print("\n-> Finding calibration stars...\n") with open(json_file, "r", encoding="utf-8") as open_file: target_dict = json.load(open_file) @@ -610,6 +622,8 @@ def find_calib( mag_low = target_dict[f"GAIA/GAIA{self.gaia_idx}.G"][0] + g_mag_range[0] mag_upp = target_dict[f"GAIA/GAIA{self.gaia_idx}.G"][0] + g_mag_range[1] + print(f"G mag search range = ({mag_low:.2f}, {mag_upp:.2f})") + gaia_query = f""" SELECT *, DISTANCE({target_dict['Gaia RA'][0]}, {target_dict['Gaia Dec'][0]}, ra, dec) AS ang_sep @@ -814,11 +828,14 @@ def find_calib( cal_df = cal_df.drop(index=drop_indices) cal_df["Gaia ID"] = cal_df["Gaia ID"].astype("int") - output_file = f"calib_find_{self.gaia_release.lower()}_{self.gaia_source}.csv" + if write_csv: + output_file = ( + f"calib_find_{self.gaia_release.lower()}_{self.gaia_source}.csv" + ) - print(f"Storing output: {output_file}") + print(f"Storing output: {output_file}") - cal_df.to_csv(path_or_buf=output_file, header=True, index=False) + cal_df.to_csv(path_or_buf=output_file, header=True, index=False) return cal_df @@ -827,13 +844,14 @@ def select_calib( self, filter_names: Optional[List[str]] = None, mag_diff: Union[float, Dict[str, float]] = 0.1, + write_csv: bool = True, ) -> pd.DataFrame: """ Function for selecting the calibration stars. The function returns a ``DataFrame`` with the selected sources, but it also - stores the data in a CSV file in the working folder. It is - recommended to open the CSV file in a spreadsheet editor for - easy visualization. + (optionally) stores the data in a CSV file in the working + folder. It is recommended to open the CSV file in a spreadsheet + editor for easy visualization. Parameters ---------- @@ -855,6 +873,11 @@ def select_calib( should be the filter names that are listed in ``filter_names`` and the values are the allowed magnitude differences for each filter. + write_csv : bool + Write the table with found source to a CSV file (default: + True). The file will be stored in the working folder and + starts with ``calib_select_``. The filename contains also + the Gaia release and the Gaia source ID of the target. Returns ------- @@ -862,9 +885,12 @@ def select_calib( The ``DataFrame`` with the selected calibration stars. """ - print("\n-> Selecting calibration stars...\n") + json_file = Path(f"target_{self.gaia_release.lower()}_{self.gaia_source}.json") - json_file = f"target_{self.gaia_release.lower()}_{self.gaia_source}.json" + if not json_file.exists(): + self.target_star(write_json=True) + + print("\n-> Selecting calibration stars...\n") with open(json_file, "r", encoding="utf-8") as open_file: target_dict = json.load(open_file) @@ -886,8 +912,23 @@ def select_calib( f"dictionary of 'mag_diff', {list(mag_diff.keys())}." ) + csv_file = Path( + f"calib_find_{self.gaia_release.lower()}_{self.gaia_source}.csv" + ) + + if not csv_file.exists(): + err_msg = ( + "The CSV file with pre-selected calibration " + "sources is not found. Please make sure to run " + "the 'find_calib()' method before " + "'select_calib()', and set the argument of " + "'write_csv' to True." + ) + + raise FileNotFoundError(err_msg) + cal_df = pd.read_csv( - filepath_or_buffer=f"calib_find_{self.gaia_release.lower()}_{self.gaia_source}.csv", + filepath_or_buffer=csv_file, header=0, index_col=False, ) @@ -909,10 +950,13 @@ def select_calib( print(f"Number of selected sources: {len(cal_df)}") - output_file = f"calib_select_{self.gaia_release.lower()}_{self.gaia_source}.csv" + if write_csv: + output_file = ( + f"calib_select_{self.gaia_release.lower()}_{self.gaia_source}.csv" + ) - print(f"Storing output: {output_file}") + print(f"Storing output: {output_file}") - cal_df.to_csv(path_or_buf=output_file, header=True, index=False) + cal_df.to_csv(path_or_buf=output_file, header=True, index=False) return cal_df diff --git a/docs/tutorial.ipynb b/docs/tutorial.ipynb index 3d8b739..968aaa2 100644 --- a/docs/tutorial.ipynb +++ b/docs/tutorial.ipynb @@ -109,7 +109,7 @@ "Metallicity = -0.82\n", "G-band extinction = 0.00\n", "\n", - "Non single star = False\n", + "Non single star = 0\n", "Single star probability from DSC-Combmod = 0.99\n", "Astrometric excess noise = 0.17\n", "\n", @@ -134,7 +134,9 @@ "WISE W1 mag = 5.573 +/- 0.176\n", "WISE W2 mag = 5.452 +/- 0.052\n", "WISE W3 mag = 5.629 +/- 0.015\n", - "WISE W4 mag = 5.481 +/- 0.043\n" + "WISE W4 mag = 5.481 +/- 0.043\n", + "\n", + "Storing output: target_dr3_6843672087120107264.json\n" ] } ], @@ -173,13 +175,14 @@ "\n", "-> Finding calibration stars...\n", "\n", - "Radius of search cone = 3.0 deg\n" + "Radius of search cone = 3.0 deg\n", + "G mag search range = (5.59, 8.59)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "8f8c0b509ac647208faa2da1d3e497ee", + "model_id": "e5ec293c824843a1ad9bd811282cc5db", "version_major": 2, "version_minor": 0 }, @@ -295,7 +298,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "c0a1d6243ca54fdfae01980423bff424", + "model_id": "ad99b176c962443aa32eedf98fc21b63", "version_major": 2, "version_minor": 0 },