Skip to content

Commit

Permalink
Merge pull request #822 from rzellem/develop
Browse files Browse the repository at this point in the history
v1.3.2: Finalized the NEA scraper and updated the aperture sizing
  • Loading branch information
rzellem authored Aug 3, 2021
2 parents 946866f + d14c84a commit 48bfa22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
77 changes: 34 additions & 43 deletions exotic/exotic.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,8 @@ def __init__(self, planet=None, candidate=False):
# CONFIGURATIONS
self.requests_timeout = 16, 512 # connection timeout, response timeout in secs.

def planet_info(self, planet, fancy=False):
self.planet = planet
log_info(f"\nLooking up {self.planet} on the NASA Exoplanet Archive. Please wait....")
self.planet, candidate = self._new_scrape(filename="eaConf.json", target=self.planet)
def planet_info(self, fancy=False):
self.planet, candidate = self._new_scrape(filename="eaConf.json")

if not candidate:
with open("eaConf.json", "r") as confirmed:
Expand Down Expand Up @@ -326,7 +324,7 @@ def planet_names(self, filename="pl_names.json"):
wait=wait_exponential(multiplier=1, min=17, max=1024),
retry=(retry_if_exception_type(requests.exceptions.RequestException) |
retry_if_exception_type(ConnectionError)))
def _new_scrape(self, filename="eaConf.json", target=None):
def _new_scrape(self, filename="eaConf.json"):

# scrape_new()
uri_ipac_base = "https://exoplanetarchive.ipac.caltech.edu/TAP/sync?query="
Expand All @@ -345,28 +343,39 @@ def _new_scrape(self, filename="eaConf.json", target=None):
"format": "csv"
}

if target:
uri_ipac_query["where"] += f" and pl_name = '{target}'"
if not os.path.exists('pl_names.json') or time.time() - os.path.getmtime('pl_names.json') > 2592000:
self.planet_names(filename="pl_names.json")
if os.path.exists('pl_names.json'):
with open("pl_names.json", "r") as f:
planets = json.load(f)
for key, value in planets.items():
if self.planet.lower().replace(' ', '').replace('-', '') == key:
self.planet = value
break
log_info(f"\nLooking up {self.planet} on the NASA Exoplanet Archive. Please wait....")

if self.planet:
uri_ipac_query["where"] += f" and pl_name = '{self.planet}'"

default = self._tap_query(uri_ipac_base, uri_ipac_query)

# fill in missing columns
uri_ipac_query['where'] = 'tran_flag=1'

if target:
uri_ipac_query["where"] += f" and pl_name = '{target}'"
if self.planet:
uri_ipac_query["where"] += f" and pl_name = '{self.planet}'"

extra = self._tap_query(uri_ipac_base, uri_ipac_query)

if len(default) == 0:
target = input(
f"Cannot find target ({target}) in NASA Exoplanet Archive. Check case sensitivity and spacing and"
"\nre-enter the planet's name or type candidate if this is a planet candidate: ")
if target.strip().lower() == 'candidate':
target = user_input("\nPlease enter candidate planet's name: ", type_=str)
return target, True
self.planet = input(f"Cannot find target ({self.planet}) in NASA Exoplanet Archive. "
f"Check case sensitivity and spacing and "
"\nre-enter the planet's name or type candidate if this is a planet candidate: ")
if self.planet.strip().lower() == 'candidate':
self.planet = user_input("\nPlease enter candidate planet's name: ", type_=str)
return self.planet, True
else:
return self._new_scrape(filename="eaConf.json", target=target)
return self._new_scrape(filename="eaConf.json")
else:
# replaces NEA default with most recent publication
default.iloc[0] = extra.iloc[0]
Expand Down Expand Up @@ -406,7 +415,7 @@ def _new_scrape(self, filename="eaConf.json", target=None):
default.loc[default.pl_name == i, k] = 0

NASAExoplanetArchive.dataframe_to_jsonfile(default, filename)
return target, False
return self.planet, False

def _get_params(self, data):
# translate data from Archive keys to Ethan Keys
Expand Down Expand Up @@ -1230,17 +1239,19 @@ def fcn2min(pars):
return res.x


# Method calculates the flux of the star (uses the skybg_phot method to do backgorund sub)
def aperPhot(data, xc, yc, sub_data, r=5, dr=5):
# Method calculates the flux of the star (uses the skybg_phot method to do background sub)
def aperPhot(data, xc, yc, r=5, dr=5):
if dr > 0:
bgflux, sigmabg, Nbg = skybg_phot(data, xc, yc, r + 2, dr)
else:
bgflux, sigmabg, Nbg = 0, 0, 0

apertures = CircularAperture(positions=[(10, 10)], r=r)
phot_table = aperture_photometry(sub_data - bgflux, apertures, method='exact')
aperture = CircularAperture(positions=[(xc, yc)], r=r)
mask = aperture.to_mask(method='exact')[0]
data_cutout = mask.cutout(data)
aperture_sum = (mask.data * (data_cutout - bgflux)).sum()

return float(phot_table['aperture_sum']), bgflux
return aperture_sum, bgflux


def skybg_phot(data, xc, yc, r=10, dr=5, ptol=99, debug=False):
Expand Down Expand Up @@ -1792,16 +1803,7 @@ def main():

if not args.override:
nea_obj = NASAExoplanetArchive(planet=userpDict['pName'])
if not os.path.exists('pl_names.json') or time.time() - os.path.getmtime('pl_names.json') > 2592000:
nea_obj.planet_names(filename="pl_names.json")
if os.path.exists('pl_names.json'):
with open("pl_names.json", "r") as f:
planets = json.load(f)
for key, value in planets.items():
if userpDict['pName'].lower().replace(' ', '').replace('-', '') == key:
userpDict['pName'] = value
break
userpDict['pName'], CandidatePlanetBool, pDict = nea_obj.planet_info(planet=userpDict['pName'])
userpDict['pName'], CandidatePlanetBool, pDict = nea_obj.planet_info()
else:
pDict = userpDict
CandidatePlanetBool = False
Expand Down Expand Up @@ -2089,21 +2091,11 @@ def main():
apers *= sigma
annuli *= sigma

mesh_box_dict = {}
for j in range(len(compStarList) + 1):
if j == 0:
key = "target"
else:
key = f"comp{j}"
xv, yv = mesh_box([psf_data[key][i, 0], psf_data[key][i, 1]], 10)
mesh_box_dict[key] = imageData[yv, xv]

for a, aper in enumerate(apers):
for an, annulus in enumerate(annuli):
aper_data["target"][i][a][an], aper_data["target_bg"][i][a][an] = aperPhot(imageData,
psf_data['target'][i, 0],
psf_data['target'][i, 1],
mesh_box_dict['target'],
aper, annulus)

# loop through comp stars
Expand All @@ -2112,7 +2104,6 @@ def main():
aper_data[ckey][i][a][an], aper_data[ckey + "_bg"][i][a][an] = aperPhot(imageData,
psf_data[ckey][i, 0],
psf_data[ckey][i, 1],
mesh_box_dict[ckey],
aper, annulus)

# close file + delete from memory
Expand Down
2 changes: 1 addition & 1 deletion exotic/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.1"
__version__ = "1.3.2"

0 comments on commit 48bfa22

Please sign in to comment.