Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/pip/requests-2.28.1
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonreusch committed Jul 10, 2022
2 parents 7f60d80 + 2005097 commit 3f1c8f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
41 changes: 29 additions & 12 deletions nuztf/irsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from astropy.coordinates import SkyCoord
from astropy.table import Table
from astroquery.exceptions import RemoteServiceError
from astroquery.ned import Ned
from astroquery.ipac.ned import Ned

from ztfquery.io import LOCALSOURCE
from ztfquery.lightcurve import LCQuery
Expand All @@ -27,6 +27,7 @@
from nuztf.parse_nu_gcn import find_gcn_no, parse_gcn_circular

logger = logging.getLogger(__name__)
logger.setLevel("DEBUG")


def format_date(t, atel=True):
Expand All @@ -46,19 +47,25 @@ def format_date(t, atel=True):
def load_irsa(ra_deg: float, dec_deg: float, radius_arcsec: float = 0.5, **kwargs):
df = LCQuery.from_position(ra_deg, dec_deg, radius_arcsec, **kwargs).data

mask = df.catflags > 0
print(len(df))

flags = list(set(df.catflags))
if len(df) == 0:
return None

logger.info(
f"Found {len(df)} datapoints, masking {np.sum(mask)} datapoints with bad flags."
)
else:
mask = df.catflags > 0

flags = list(set(df.catflags))

logger.info(
f"Found {len(df)} datapoints, masking {np.sum(mask)} datapoints with bad flags."
)

for flag in sorted(flags):
logger.debug(f"{np.sum(df.catflags == flag)} datapoints with flag {flag}")
for flag in sorted(flags):
logger.debug(f"{np.sum(df.catflags == flag)} datapoints with flag {flag}")

df = df.drop(df[mask].index)
return df
df = df.drop(df[mask].index)
return df


def plot_irsa_lightcurve(
Expand All @@ -84,6 +91,9 @@ def plot_irsa_lightcurve(

if source_coords is None:

logger.info(f"Trying to resolve {source_name} and query for coordinates.")
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)

# Try ampel to find ZTF coordinates

if is_ztf_name(name=source_name):
Expand Down Expand Up @@ -113,6 +123,9 @@ def plot_irsa_lightcurve(
# Otherwise try NED

else:
logger.info(
"Source name is neither as ZTF, nor a TNS name. Querying NED instead."
)
result_table = Ned.query_object(source_name)

if len(result_table) == 0:
Expand Down Expand Up @@ -162,12 +175,13 @@ def plot_irsa_lightcurve(

# Try to find a catalogue source nearby using coordinates

if np.logical_and("ZTF" in source_name, source_coords is not None):
if ("ZTF" in source_name) and source_coords:

c = SkyCoord(source_coords[0], source_coords[1], unit=u.deg, frame="icrs")

r = 0.5 * u.arcsecond

logger.info("Querying NED")
result_table = Ned.query_region(c, radius=r)

if len(result_table) == 1:
Expand Down Expand Up @@ -207,7 +221,7 @@ def plot_irsa_lightcurve(
df = pd.read_csv(cache_path)

else:

logger.info("Querying IPAC for a lightcurve")
df = load_irsa(source_coords[0], source_coords[1], radius_arcsec=radius_arcsec)

logger.debug(f"Saving to {cache_path}")
Expand Down Expand Up @@ -296,6 +310,9 @@ def plot_irsa_lightcurve(

if check_obs:

logger.info(
f"Getting most recent ZTF observation, looking back {check_obs_lookback_weeks} weeks."
)
mro = get_most_recent_obs(
ra=source_coords[0],
dec=source_coords[1],
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ligo.skymap == 0.5.3
lxml==4.9.1
matplotlib==3.5.1
mocpy==0.11.0
numpy==1.22.0
numpy==1.23.0
pandas==1.3.5
pre_commit == 2.17.0
python-ligo-lw==1.7.1
Expand Down
7 changes: 6 additions & 1 deletion tests/test_irsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@

class TestIrsa(unittest.TestCase):
def setUp(self):
logging.getLogger("irsa").setLevel(logging.INFO)
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.INFO)

def test_lightcurve(self):
self.logger.info("\n\n Testing IRSA \n\n")
self.logger.info("Getting lightcurve from IPAC.")

res = load_irsa(77.358185, 5.693148, 0.5, TIME=[58204.1, 59678.9])

res = res[res["programid"] == 1]
expected = 374

self.assertEqual(len(res), 375)
self.logger.info(f"Found {len(res)} entries. Expected: {expected}")

self.assertEqual(len(res), expected)

src_names = ["PKS1502+106", "SN2021gpw"]
nu_names = ["IC190730A", "IC211216B"]
Expand Down

0 comments on commit 3f1c8f8

Please sign in to comment.