From ca3a8751787245d3d01c099a5317256e67232e54 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 5 Mar 2026 14:01:38 -0500 Subject: [PATCH 1/6] Added a new method to the HeasarcClass class called 'count_rows'. It constructs a simple ADQL query that uses the COUNT(*) function to request the number of rows for a specified table, and then returns the result as an integer. --- astroquery/heasarc/core.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/astroquery/heasarc/core.py b/astroquery/heasarc/core.py index f92121ff70..ba6f1b7536 100644 --- a/astroquery/heasarc/core.py +++ b/astroquery/heasarc/core.py @@ -958,5 +958,27 @@ def _s3_tree_download(client, bucket_name, path, local): path = key.replace(f's3://{self.S3_BUCKET}/', '') _s3_tree_download(self.s3_client, self.S3_BUCKET, path, location) + def count_rows(self, catalog: str) -> int: + """ + Returns the number of rows in the HEASARC-hosted catalog specified by + the `catalog` argument. + + Parameters + ---------- + catalog : `str` + The name of the catalog to query for a total number of rows. To list + the available catalogs, use + :meth:`~astroquery.heasarc.HeasarcClass.list_catalogs`. + + Returns + ------- + num_rows : `int` + The total number of rows in the specified catalog. + """ + count_return = self.query_tap(f"SELECT COUNT(*) FROM {catalog}") + + num_rows = count_return["count"][0] + return num_rows + Heasarc = HeasarcClass() From 5d4c0631ea41fbba417c1313cf2056805e37b2b5 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 5 Mar 2026 14:08:49 -0500 Subject: [PATCH 2/6] Added a couple of comments to the count_rows method. --- astroquery/heasarc/core.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/astroquery/heasarc/core.py b/astroquery/heasarc/core.py index ba6f1b7536..b073e5788f 100644 --- a/astroquery/heasarc/core.py +++ b/astroquery/heasarc/core.py @@ -958,7 +958,7 @@ def _s3_tree_download(client, bucket_name, path, local): path = key.replace(f's3://{self.S3_BUCKET}/', '') _s3_tree_download(self.s3_client, self.S3_BUCKET, path, location) - def count_rows(self, catalog: str) -> int: + def count_rows(self, catalog: str) -> np.int64: """ Returns the number of rows in the HEASARC-hosted catalog specified by the `catalog` argument. @@ -972,11 +972,14 @@ def count_rows(self, catalog: str) -> int: Returns ------- - num_rows : `int` + num_rows : `np.int64` The total number of rows in the specified catalog. """ + # Send a query requesting a count of the number of rows in the + # specified catalog. count_return = self.query_tap(f"SELECT COUNT(*) FROM {catalog}") + # Extract an integer number of rows from the returned table. num_rows = count_return["count"][0] return num_rows From 1305057e8ff515500f08ec42d1b0d033f011921e Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 5 Mar 2026 14:24:50 -0500 Subject: [PATCH 3/6] Added a tiny test to check the new count_rows method of Heasarc. --- astroquery/heasarc/tests/test_heasarc_remote.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/astroquery/heasarc/tests/test_heasarc_remote.py b/astroquery/heasarc/tests/test_heasarc_remote.py index 4d765c7583..04d4dc35d7 100644 --- a/astroquery/heasarc/tests/test_heasarc_remote.py +++ b/astroquery/heasarc/tests/test_heasarc_remote.py @@ -273,6 +273,13 @@ def test_deprecated_pars(self, pars): Heasarc.query_region(pos, mission="xmmmaster", **{keyword: value}) + def test_row_count(self): + """Test the convenience method that uses ADQL's COUNT function to + return the number of rows in a catalog. + """ + cat = "suzamaster" + assert Heasarc.count_rows(cat) == 3055 + @pytest.mark.remote_data class TestHeasarcBrowse: From 50b5cfc65f1628c5cb9ec4526a73938897317cfa Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 5 Mar 2026 14:35:04 -0500 Subject: [PATCH 4/6] Added a demonstration of the count_rows method to the Heasarc class docs. --- docs/heasarc/heasarc.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/heasarc/heasarc.rst b/docs/heasarc/heasarc.rst index 5aa7a28b36..3072f50cb8 100644 --- a/docs/heasarc/heasarc.rst +++ b/docs/heasarc/heasarc.rst @@ -422,6 +422,16 @@ in the XMM master catalog ``xmmmaster``: public_date Public Date mjd ra Right Ascension (Pointing Position) degree +Get Length Of Catalog +--------------------- +To easily find out the number of rows in a catalog, use the `~astroquery.heasarc.HeasarcClass.count_rows` +method. Here, for instance, we fetch the number of rows in the Suzaku 'master' observation table (``suzamaster``): + +.. doctest-remote-data:: + + >>> from astroquery.heasarc import Heasarc + >>> Heasarc.count_rows('suzamaster') + Reference/API ============= From 1a4edcaeef66480d85b0ad1c2fd1b05dfff48601 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 5 Mar 2026 14:50:37 -0500 Subject: [PATCH 5/6] Added output line to the count_rows demo in heasarc.rst --- docs/heasarc/heasarc.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/heasarc/heasarc.rst b/docs/heasarc/heasarc.rst index 3072f50cb8..8216d23576 100644 --- a/docs/heasarc/heasarc.rst +++ b/docs/heasarc/heasarc.rst @@ -431,6 +431,7 @@ method. Here, for instance, we fetch the number of rows in the Suzaku 'master' o >>> from astroquery.heasarc import Heasarc >>> Heasarc.count_rows('suzamaster') + np.int64(3055) Reference/API ============= From 6ae40c1ed264822bcde8405047ab5404c820243b Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 5 Mar 2026 15:37:57 -0500 Subject: [PATCH 6/6] Added an entry in the change log --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 9be6958c7a..0d41814cb3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -54,6 +54,7 @@ heasarc - Add ``query_constraints`` to allow querying of different catalog columns. [#3403] - Add support for uploading tables when using TAP directly through ``query_tap``. [#3403] - Add automatic guessing for the data host in ``download_data``. [#3403] +- Include method to count the number of rows in a specified table. [#3549] gaia ^^^^