Skip to content

Commit

Permalink
Use an explicit class instance for simbad queries
Browse files Browse the repository at this point in the history
Importing Simbad and using what looks like a module/class
methods actually instantiates a secret object, and causes
all sorts of problems, so never do that.
  • Loading branch information
mfisherlevine committed Jul 20, 2022
1 parent eb01fb6 commit 5cba9e9
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions spectractor/extractor/targets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from astropy.coordinates import SkyCoord, Distance
import astropy.units as u
from astropy.time import Time
from astroquery.simbad import SimbadClass

import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
Expand Down Expand Up @@ -194,7 +195,6 @@ def __init__(self, label, verbose=False):
"""
Target.__init__(self, label, verbose=verbose)
self.my_logger = set_logger(self.__class__.__name__)
self.simbad = None
self.load()

def load(self):
Expand All @@ -212,28 +212,31 @@ def load(self):
>>> print(s.radec_position.dec)
-66d02m22.635s
"""
# currently (pending a new release) astroquery has a race
# condition at import time, so putting here rather than at the
# module level so that multiple test runners don't run the race
from astroquery.simbad import Simbad
patchSimbadURL(Simbad)
Simbad.add_votable_fields('flux(U)', 'flux(B)', 'flux(V)', 'flux(R)', 'flux(I)', 'flux(J)', 'sptype',
'parallax', 'pm', 'z_value')
# explicitly make a class instance here because:
# when using ``from astroquery.simbad import Simbad`` and then using
# ``Simbad...`` methods secretly makes an instance, which stays around,
# has a connection go stale, and then raises an exception seemingly
# at some random time later
simbadQuerier = SimbadClass()
patchSimbadURL(simbadQuerier)

simbadQuerier.add_votable_fields('flux(U)', 'flux(B)', 'flux(V)', 'flux(R)', 'flux(I)', 'flux(J)', 'sptype',
'parallax', 'pm', 'z_value')
astroquery_label = self.label
if getCalspec.is_calspec(self.label):
calspec = getCalspec.Calspec(self.label)
astroquery_label = calspec.Astroquery_Name
simbad = Simbad.query_object(astroquery_label)
self.simbad = simbad
if simbad is not None:
simbad_table = simbadQuerier.query_object(astroquery_label)

if simbad_table is not None:
if self.verbose or True:
self.my_logger.info(f'\n\tSimbad:\n{simbad}')
self.radec_position = SkyCoord(simbad['RA'][0] + ' ' + simbad['DEC'][0], unit=(u.hourangle, u.deg))
self.my_logger.info(f'\n\tSimbad:\n{simbad_table}')
self.radec_position = SkyCoord(simbad_table['RA'][0] + ' ' + simbad_table['DEC'][0], unit=(u.hourangle, u.deg))
else:
self.my_logger.warning(f'Target {self.label} not found in Simbad')
self.get_radec_position_after_pm(date_obs="J2000")
if not np.ma.is_masked(simbad['Z_VALUE']):
self.redshift = float(simbad['Z_VALUE'])
raise RuntimeError(f"Target {self.label} not found in Simbad")
self.get_radec_position_after_pm(simbad_table, date_obs="J2000")
if not np.ma.is_masked(simbad_table['Z_VALUE']):
self.redshift = float(simbad_table['Z_VALUE'])
else:
self.redshift = 0
self.load_spectra()
Expand Down Expand Up @@ -335,14 +338,14 @@ def load_spectra(self):
f"\n\tEmission spectrum ? {self.emission_spectrum}"
f"\n\tLines: {[l.label for l in self.lines.lines]}")

def get_radec_position_after_pm(self, date_obs):
target_pmra = self.simbad[0]['PMRA'] * u.mas / u.yr
def get_radec_position_after_pm(self, simbad_table, date_obs):
target_pmra = simbad_table[0]['PMRA'] * u.mas / u.yr
if np.isnan(target_pmra):
target_pmra = 0 * u.mas / u.yr
target_pmdec = self.simbad[0]['PMDEC'] * u.mas / u.yr
target_pmdec = simbad_table[0]['PMDEC'] * u.mas / u.yr
if np.isnan(target_pmdec):
target_pmdec = 0 * u.mas / u.yr
target_parallax = self.simbad[0]['PLX_VALUE'] * u.mas
target_parallax = simbad_table[0]['PLX_VALUE'] * u.mas
if target_parallax == 0 * u.mas:
target_parallax = 1e-4 * u.mas
target_coord = SkyCoord(ra=self.radec_position.ra, dec=self.radec_position.dec,
Expand Down

0 comments on commit 5cba9e9

Please sign in to comment.