From a2826eb7b726a3e2702da1bcdc99be59931da361 Mon Sep 17 00:00:00 2001 From: Tomas Stolker Date: Sun, 29 Oct 2023 09:37:59 +0100 Subject: [PATCH] Added the write_json and write_csv parameters --- calistar/calistar.py | 91 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 20 deletions(-) diff --git a/calistar/calistar.py b/calistar/calistar.py index 2f22ab1..f8bb426 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 ------- @@ -530,10 +540,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 +554,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 +586,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 +598,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) @@ -814,11 +835,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 +851,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 +880,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 +892,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 +919,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 +957,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