From b29c39d3073b241ef9d3473d7759327207f95576 Mon Sep 17 00:00:00 2001 From: shoulinwei Date: Mon, 28 Oct 2024 17:14:52 +0800 Subject: [PATCH] add compute_ra_dec_range --- csst_dfs_commons/utils/fits.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/csst_dfs_commons/utils/fits.py b/csst_dfs_commons/utils/fits.py index de93ac6..b89ec8f 100644 --- a/csst_dfs_commons/utils/fits.py +++ b/csst_dfs_commons/utils/fits.py @@ -3,6 +3,7 @@ from astropy.coordinates import ICRS from astropy import units as u from astropy.coordinates import SkyCoord from astropy.io import fits +from astropy import wcs from csst_dfs_commons.models.constants import PI, DEFAULT_NSIDE @@ -110,3 +111,27 @@ def dfs_heapix_sql_condition(ra, dec, radius, ra_col = 'ra', dec_col = 'dec', br whereSql = f"{distance} <= 4*pow(pi()*{radius}/180.0/2, 2) and {whereZoneSql}" return whereSql + +def compute_ra_dec_range(hdulist): + header = hdulist[1].header + fits_wcs = wcs.WCS(header) + + # Get the shape of the data + naxis1 = header['NAXIS1'] + naxis2 = header['NAXIS2'] + + # Get the pixel coordinates of all corners + corners = [(0, 0), (naxis1, 0), (naxis1, naxis2), (0, naxis2)] + + # Convert pixel coordinates of corners to RA and Dec + corners_coords = fits_wcs.all_pix2world(corners, 0) + + # Extract RA and Dec values + ras = [coord[0] for coord in corners_coords] + decs = [coord[1] for coord in corners_coords] + + # Compute the min and max of RA and Dec + min_ra, max_ra = min(ras), max(ras) + min_dec, max_dec = min(decs), max(decs) + + return min_ra, max_ra, min_dec, max_dec -- GitLab