diff --git a/csst_dfs_commons/utils/fits.py b/csst_dfs_commons/utils/fits.py index de93ac65c623b39949cb74fcfe8a6493ac03f04e..b89ec8f9b46c98a36f85a54a955326d20dac06a3 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