fits.py 1.13 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
3
4
from astropy_healpix import HEALPix
from astropy.coordinates import ICRS
from astropy import units as u
from astropy.coordinates import SkyCoord
Wei Shoulin's avatar
Wei Shoulin committed
5
from astropy.io import fits
Wei Shoulin's avatar
Wei Shoulin committed
6

Wei Shoulin's avatar
Wei Shoulin committed
7
def get_header_value(key: str, headers, default_value = None):
Wei Shoulin's avatar
Wei Shoulin committed
8
    try:
Wei Shoulin's avatar
Wei Shoulin committed
9
10
11
12
13
14
15
16
17
18
        ret_value = None
        if not isinstance(headers, list):
            headers = [headers]
        for header in headers:
            if key in header:
                ret_value = header[key]
                if type(ret_value) == str:
                    return ret_value.strip()
        if ret_value is None:
            return default_value
Wei Shoulin's avatar
Wei Shoulin committed
19
20
        else:
            return ret_value
Wei Shoulin's avatar
Wei Shoulin committed
21
    except Exception as e:
Wei Shoulin's avatar
Wei Shoulin committed
22
23
24
25
26
        return default_value

def get_healpix_id(ra, dec, nside=32):
    hp = HEALPix(nside=nside, order='nested', frame=ICRS())
    coord = SkyCoord(ra = ra * u.deg, dec = dec * u.deg, frame='icrs')
Wei Shoulin's avatar
h    
Wei Shoulin committed
27
28
29
30
31
32
    return hp.skycoord_to_healpix(coord)

def get_healpix_ids(ra, dec, radius, nside=32):
    hp = HEALPix(nside=nside, order='nested', frame=ICRS())
    coord = SkyCoord(ra = ra * u.deg, dec = dec * u.deg, frame='icrs')
    return hp.cone_search_skycoord(coord, radius = radius * u.deg)