aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/utils/wcs.py
blob: b4a9ed17002d83b2ba983410a7bb21b91e8d0ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Copyright (c) 2017 Weitian LI <weitian@aaronly.me>
# MIT license

"""
Create WCS for sky projection.
"""

import numpy as np
from astropy.wcs import WCS


def make_wcs(center, size, pixelsize, frame="ICRS", projection="TAN"):
    """
    Make WCS for sky projection usages, etc.

    Parameters
    ----------
    center : (xcenter, ycenter) float tuple
        The equatorial/galactic coordinate of the sky/image center [ deg ].
    size : (xsize, ysize) int tuple
        The size (width, height) of the sky/image.
    pixelsize : float
        The pixel size of the sky/image [ arcmin ]
    frame : str, "ICRS" or "Galactic"
        The coordinate frame, only one of ``ICRS`` or ``Galactic``.
    projection : str, "TAN" or "CAR"
        The projection algorithm used by the sky/image, currently only
        support ``TAN`` (tangential), ``CAR`` (Cartesian).

    Returns
    -------
    w : `~astropy.wcs.WCS`
        Created WCS header/object
    """
    xcenter, ycenter = center  # [ deg ]
    xsize, ysize = size
    delt = pixelsize / 60.0  # [ deg ]
    if projection.upper() not in ["TAN", "CAR"]:
        raise ValueError("unsupported projection: " % projection)
    if frame.upper() == "ICRS":
        ctype = ["RA---" + projection.upper(), "DEC--" + projection.upper()]
    elif frame.upper() == "GALACTIC":
        ctype = ["GLON-" + projection.upper(), "GLAT-" + projection.upper()]
    else:
        raise ValueError("unknown frame: " % frame)

    w = WCS(naxis=2)
    w.wcs.ctype = ctype
    w.wcs.crval = np.array([xcenter, ycenter])
    w.wcs.crpix = np.array([xsize/2.0-0.5, ysize/2.0-0.5])
    w.wcs.cdelt = np.array([-delt, delt])
    w.wcs.cunit = ["deg", "deg"]
    w.wcs.equinox = 2000.0
    return w