From 87dbd21f0e7742efff0fdf272135c18dea26ab2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brigitta=20Sip=C5=91cz?= Date: Wed, 22 May 2024 12:08:16 +1000 Subject: [PATCH] Merge pull request #550 from duytnguyendtn/time_nan Check for nan dateobs before converting to Time --- CHANGES.rst | 10 ++++++++++ pyvo/dal/sia.py | 12 ++++++++---- pyvo/dal/ssa.py | 12 ++++++++---- pyvo/dal/tests/data/sia/dataset.xml | 14 ++++++++++++++ pyvo/dal/tests/data/ssa/result.xml | 1 + pyvo/dal/tests/test_sia.py | 8 +++++++- pyvo/dal/tests/test_ssa.py | 5 +++-- 7 files changed, 51 insertions(+), 11 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 0dca0f92a..a98181f2b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,13 @@ +1.5.2 (unreleased) +================== + +Bug Fixes +--------- + +- Avoid Astropy Time error for SIAResult.dateobs when + VOX:Image_MJDateObs or ssa:DataID.Date is nan. [#550] + + 1.5.1 (2024-02-21) ================== diff --git a/pyvo/dal/sia.py b/pyvo/dal/sia.py index d2efcc1ee..2d72453cc 100644 --- a/pyvo/dal/sia.py +++ b/pyvo/dal/sia.py @@ -35,6 +35,7 @@ from astropy.coordinates import SkyCoord from astropy.time import Time from astropy.units import Quantity, Unit +import numpy as np from .query import DALResults, DALQuery, DALService, Record from .mimetype import mime2extension @@ -702,10 +703,13 @@ def dateobs(self): as an astropy.time.Time instance """ dateobs = self.getbyucd("VOX:Image_MJDateObs") - if dateobs: - return Time(dateobs, format="mjd") - else: - return None + try: + if not dateobs or np.isnan(dateobs): + return None + except TypeError: + # np.isnan can only check floats. If can't check for nan, pass it along + pass + return Time(dateobs, format="mjd") @property def naxes(self): diff --git a/pyvo/dal/ssa.py b/pyvo/dal/ssa.py index ea577e897..021b6df2b 100644 --- a/pyvo/dal/ssa.py +++ b/pyvo/dal/ssa.py @@ -39,6 +39,7 @@ from astropy.units import spectral as spectral_equivalencies from astropy.io.votable.tree import Field from astropy.table import Table +import numpy as np from .query import DALResults, DALQuery, DALService, Record from .mimetype import mime2extension @@ -698,10 +699,13 @@ def dateobs(self): observational data that went into the spectrum """ dateobs = self.getbyutype("ssa:DataID.Date", decode=True) - if dateobs: - return Time(dateobs, format="iso") - else: - return None + try: + if not dateobs or np.isnan(dateobs): + return None + except TypeError: + # np.isnan can only check floats. If can't check for nan, pass it along + pass + return Time(dateobs, format="iso") @property def instr(self): diff --git a/pyvo/dal/tests/data/sia/dataset.xml b/pyvo/dal/tests/data/sia/dataset.xml index eae592a5f..803d0033d 100644 --- a/pyvo/dal/tests/data/sia/dataset.xml +++ b/pyvo/dal/tests/data/sia/dataset.xml @@ -58,6 +58,20 @@ 5.2e-07 0.000403806 0.000406123 + + This should not be the dataurl + http://example.com/querydata/image.fits + image/fits + 153280 + + Test + 288.95078924817 + 15.0322239971381 + Test Observation + 3.8e-07 + 5.2e-07 + 0.000403806 0.000406123 + diff --git a/pyvo/dal/tests/data/ssa/result.xml b/pyvo/dal/tests/data/ssa/result.xml index b30669e79..ad60c5025 100644 --- a/pyvo/dal/tests/data/ssa/result.xml +++ b/pyvo/dal/tests/data/ssa/result.xml @@ -209,6 +209,7 @@ 1.0976232720625123E-25http://vaosa-vm1.aoc.nrao.edu/ivoa-dal/JhuSsapServlet?REQUEST=getData&FORMAT=votable&PubDID=ivo%3A%2F%2Fjhu%2Fsdss%2Fdr6%2Fspec%2F2.5%2380443408220422144application/x-votable+xmlSPECTRUM-1.0SPECTRUM4000sA10**(-17) erg s**(-1) cm**(-2) A**(-1)SDSS J120022.76+004741.46 GALAXYsdssSDSS DR6ivo://sdss/dr6/spec/2_5/#804434082204221442001-01-21 11:23:50Z6.2.5ivo://sdss/dr6/spec/2_5/#80443408220422144SDSS 2.5-M SPEC2 v4_5OPTICALSURVEYARCHIVALELTE VOivo://jhu/sdss/dr6/spec/2.5#80443408220422144PUBLIC180.094830 0.794849SDSS J120022.76+004741.46GALAXY0.1397970FK52000TAI180.094830 0.794849 8.441184510333563E-42http://vaosa-vm1.aoc.nrao.edu/ivoa-dal/JhuSsapServlet?REQUEST=getData&FORMAT=votable&PubDID=ivo%3A%2F%2Fjhu%2Fsdss%2Fdr6%2Fspec%2F2.5%2380443407868100608application/x-votable+xmlSPECTRUM-1.0SPECTRUM4000sA10**(-17) erg s**(-1) cm**(-2) A**(-1)SDSS J115918.12+005113.61 GALAXYsdssSDSS DR6ivo://sdss/dr6/spec/2_5/#804434078681006082001-01-21 11:23:50Z6.2.5ivo://sdss/dr6/spec/2_5/#80443407868100608SDSS 2.5-M SPEC2 v4_5OPTICALSURVEYARCHIVALELTE VOivo://jhu/sdss/dr6/spec/2.5#80443407868100608PUBLIC179.825520 0.853781SDSS J115918.12+005113.61GALAXY0.07704420FK52000TAI179.825520 0.853781 8.441184510333563E-42http://vaosa-vm1.aoc.nrao.edu/ivoa-dal/JhuSsapServlet?REQUEST=getData&FORMAT=votable&PubDID=ivo%3A%2F%2Fjhu%2Fsdss%2Fdr6%2Fspec%2F2.5%2380442261136998400application/x-votable+xmlSPECTRUM-1.0SPECTRUM4000sA10**(-17) erg s**(-1) cm**(-2) A**(-1)SDSS J115918.12+005113.61 GALAXYsdssSDSS DR6ivo://sdss/dr6/spec/2_5/#804422611369984002000-04-29 03:22:00Z6.2.5ivo://sdss/dr6/spec/2_5/#80442261136998400SDSS 2.5-M SPEC2 v4_5OPTICALSURVEYARCHIVALELTE VOivo://jhu/sdss/dr6/spec/2.5#80442261136998400PUBLIC179.825520 0.853781SDSS J115918.12+005113.61GALAXY0.07709410FK52000TAI179.825520 0.853781 +8.441184510333563E-42http://vaosa-vm1.aoc.nrao.edu/ivoa-dal/JhuSsapServlet?REQUEST=getData&FORMAT=votable&PubDID=ivo%3A%2F%2Fjhu%2Fsdss%2Fdr6%2Fspec%2F2.5%2380442261136998400application/x-votable+xmlSPECTRUM-1.0SPECTRUM4000sA10**(-17) erg s**(-1) cm**(-2) A**(-1)SDSS J115918.12+005113.61 GALAXYsdssSDSS DR6ivo://sdss/dr6/spec/2_5/#804422611369984006.2.5ivo://sdss/dr6/spec/2_5/#80442261136998400SDSS 2.5-M SPEC2 v4_5OPTICALSURVEYARCHIVALELTE VOivo://jhu/sdss/dr6/spec/2.5#80442261136998400PUBLIC179.825520 0.853781SDSS J115918.12+005113.61GALAXY0.07709410FK52000TAI179.825520 0.853781 diff --git a/pyvo/dal/tests/test_sia.py b/pyvo/dal/tests/test_sia.py index 7356f0d4b..ed92e1a09 100644 --- a/pyvo/dal/tests/test_sia.py +++ b/pyvo/dal/tests/test_sia.py @@ -61,9 +61,15 @@ class TestSIAService: @pytest.mark.filterwarnings("ignore::astropy.io.votable.exceptions.W42") @pytest.mark.filterwarnings("ignore::astropy.io.votable.exceptions.W49") def test_search(self): - service = SIAService('http://example.com/sia') + description = "A SIA service." + url = 'http://example.com/sia' + service = SIAService(url, capability_description=description) + assert service.baseurl == url + assert service.capability_description == description results = service.search(pos=(288, 15)) result = results[0] _test_result(result) + + assert results[1].dateobs is None diff --git a/pyvo/dal/tests/test_ssa.py b/pyvo/dal/tests/test_ssa.py index 1570eea77..9c78eee60 100644 --- a/pyvo/dal/tests/test_ssa.py +++ b/pyvo/dal/tests/test_ssa.py @@ -33,7 +33,7 @@ def callback(request, context): @pytest.mark.filterwarnings("ignore::astropy.io.votable.exceptions.W42") def test_search(): results = search('http://example.com/ssa', pos=(0.0, 0.0), diameter=1.0) - assert len(results) == 35 + assert len(results) == 36 class TestSSAService: @@ -44,4 +44,5 @@ def test_search(self): results = service.search(pos=(0.0, 0.0), diameter=1.0) - assert len(results) == 35 + assert len(results) == 36 + assert results[35].dateobs is None