Skip to content

Commit

Permalink
Added the write_json and write_csv parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstolker committed Oct 29, 2023
1 parent eb1a12e commit 26034ae
Showing 1 changed file with 72 additions and 30 deletions.
102 changes: 72 additions & 30 deletions calistar/calistar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -573,16 +577,24 @@ 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
-------
pandas.DataFrame
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)
Expand Down Expand Up @@ -814,11 +826,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

Expand All @@ -827,13 +842,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
----------
Expand All @@ -855,16 +871,24 @@ 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
-------
pandas.DataFrame
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")

if not json_file.exists():
self.target_star(write_json=True)

json_file = f"target_{self.gaia_release.lower()}_{self.gaia_source}.json"
print("\n-> Selecting calibration stars...\n")

with open(json_file, "r", encoding="utf-8") as open_file:
target_dict = json.load(open_file)
Expand All @@ -886,8 +910,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,
)
Expand All @@ -909,10 +948,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

0 comments on commit 26034ae

Please sign in to comment.