Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to ComparisonViewer for release version of ImageWidget #108

Merged
merged 12 commits into from
Jun 29, 2023
Merged
6 changes: 2 additions & 4 deletions stellarphot/io/tess.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the GAIA aperture server hardcoded. Seemed to make sense.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Makes me want to vomit, but....
DEFAULT_TABLE_LOCATION = "who.the.heck.knows"
TOI_TABLE_URL = "https://exofop.ipac.caltech.edu/tess/download_toi.php?output=csv"
GAIA_APERTURE_SERVER = "https://www.astro.louisville.edu/"
TIC_regex = re.compile(r"[tT][iI][cC][^\d]?(?P<star>\d+)(?P<planet>\.\d\d)?")


Expand Down Expand Up @@ -406,9 +407,6 @@ class TessTargetFile:
within 2.5 arcminutes the TESS target. If not provided, a temporary
file will be created.

aperture_server : str, optional
The URL of the aperture server. default: https://www.astro.louisville.edu/

Attributes
----------

Expand Down Expand Up @@ -442,9 +440,9 @@ class TessTargetFile:
magnitude : float
depth : float
file : str = ""
aperture_server : str = "https://www.astro.louisville.edu/"

def __post_init__(self):
self.aperture_server = GAIA_APERTURE_SERVER
if not self.file:
self.file = NamedTemporaryFile()
self._path = Path(self.file.name)
Expand Down
38 changes: 29 additions & 9 deletions stellarphot/visualization/comparison_functions.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed redundant import, fixed if v_angle: to not evaluate as ambiguous in current astropy, added a parameter to allow overwriting of all output files by ComparisonViewer.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from astropy.table import Table
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.nddata import CCDData
from astropy import units as u
from astropy.coordinates.name_resolve import NameResolveError
Expand Down Expand Up @@ -188,12 +187,12 @@ def mag_scale(cmag, apass, v_angle, RD_angle,
"""
high_mag = apass['r_mag'] < cmag + dimmer_dmag
low_mag = apass['r_mag'] > cmag - brighter_dmag
if v_angle:
if len(v_angle)>0:
JuanCab marked this conversation as resolved.
Show resolved Hide resolved
good_v_angle = v_angle > 1.0 * u.arcsec
else:
good_v_angle = True

if RD_angle:
if len(RD_angle)>0:
JuanCab marked this conversation as resolved.
Show resolved Hide resolved
good_RD_angle = RD_angle > 1.0 * u.arcsec
else:
good_RD_angle = True
Expand Down Expand Up @@ -392,6 +391,9 @@ class ComparisonViewer:
aperture_output_file : str, optional
File to save aperture information to. Defaults to None.

overwrite_outputs: bool, optional
Whether to overwrite existing output files. Defaults to True.

Attributes
----------

Expand All @@ -410,6 +412,9 @@ class ComparisonViewer:
iw : `ginga.util.grc.RemoteClient`
Ginga widget.

overwrite_outputs: bool
Whether to overwrite existing output files. Defaults to True.

target_coord : `astropy.coordinates.SkyCoord`
Coordinates of the target.

Expand All @@ -432,7 +437,8 @@ def __init__(self,
dim_mag_limit=17,
targets_from_file=None,
object_coordinate=None,
aperture_output_file=None):
aperture_output_file=None,
overwrite_outputs=True):

self._label_name = 'labels'
self._circle_name = 'target circle'
Expand All @@ -450,6 +456,7 @@ def __init__(self,
self.box, self.iw = self._viewer()

self.aperture_output_file = aperture_output_file
self.overwrite_outputs = overwrite_outputs

if file:
self._file_chooser.set_file(file, directory=directory)
Expand Down Expand Up @@ -564,7 +571,8 @@ def _make_observers(self):
def _save_variables_to_file(self, button=None, filename=''):
if not filename:
filename = 'variables.csv'
self.variables.write(filename)
# Export variables as CSV (overwrite existing file if it exists)
self.variables.write(filename, overwrite=self.overwrite_outputs)

def _show_label_button_handler(self, change):
value = change['new']
Expand All @@ -580,8 +588,8 @@ def _show_label_button_handler(self, change):
def _save_aperture_to_file(self, button=None, filename=''):
if not filename:
filename = self.aperture_output_file

self.generate_table().write(filename)
# Export aperture file as CSV (overwrite existing file if it exists)
self.generate_table().write(filename, overwrite=self.overwrite_outputs)

def _make_control_bar(self):
self._show_labels_button = ipw.ToggleButton(description='Click to show labels')
Expand Down Expand Up @@ -707,11 +715,23 @@ def save_tess_files(self, button=None):
"""
if self._field_name.value:
self.tess_field_view()
self.iw.save(self._field_name.value, overwrite=True)
# Remove output file if it exists
if Path(self._field_name.value).exists() and self.overwrite_outputs:
Path(self._field_name.value).unlink()
try:
self.iw.save(self._field_name.value)
except:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually it is better to not use a bare except, I think, in case you accidentally capture an error message that is really a bug. I think we could get away with no try/except here, anticipating that if an error is raised it will probably be for something real, and should be reported to the user with the exception details/traceback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh crap, I forgot to capture the specific error. I ended up wrapping all the file creation calls that could fail due to attempting to overwrite a file without permission with a try..except OSError. That should be specific enough. I notice the errors still show up on the Jupyterlab log page, so may not really be much easier for users.

print("Error saving full field image.")

if self._zoom_name.value:
self.tess_field_zoom_view()
self.iw.save(self._zoom_name.value, overwrite=True)
# Remove output file if it exists
if Path(self._zoom_name.value).exists() and self.overwrite_outputs:
Path(self._zoom_name.value).unlink()
try:
self.iw.save(self._zoom_name.value)
except:
print("Error saving zoomed in full field image.")

def generate_table(self):
"""
Expand Down