예제 #1
0
def create_fitsheader(naxis=None,
                      dtype=None,
                      fromdata=None,
                      extname=None,
                      crval=(0., 0.),
                      crpix=None,
                      ctype=('RA---TAN', 'DEC--TAN'),
                      cunit='deg',
                      cd=None,
                      cdelt=None,
                      pa=None,
                      equinox=2000.):
    """
    Helper to create a FITS header.

    Parameters
    ----------
    naxis : array, optional
        (NAXIS1,NAXIS2,...) tuple, which specifies the data dimensions.
    dtype : data-type, optional
        Desired data type for the data stored in the FITS file
    fromdata : array_like, optional
        An array from which the dimensions and typewill be extracted. Note
        that following the FITS convention, the dimension along X is the
        second value of the array shape and that the dimension along the
        Y axis is the first one.
    extname : None or string
        if a string is specified ('' can be used), the returned header
        type will be an Image HDU (otherwise a Primary HDU).
    crval : 2 element array, optional
        Reference pixel values (FITS convention).
    crpix : 2 element array, optional
        Reference pixel (FITS convention).
    ctype : 2 element string array, optional
        Projection types.
    cunit : string or 2 element string array
        Units of the CD matrix (default is degrees/pixel).
    cd : 2 x 2 array
        FITS parameters
            CD1_1 CD1_2
            CD2_1 CD2_2
    cdelt : float or 2 element array
        Physical increment at the reference pixel.
    pa : float
        Position angle of the Y=AXIS2 axis (=-CROTA2).
    equinox : float
        Reference equinox

    Examples
    --------
    >>> map = Map.ones((10,100), unit='Jy/pixel')
    >>> map.header = create_fitsheader(naxis=(100,10), cd=[[-1,0],[0,1]])
    >>> map.header = create_fitsheader(fromdata=map)
    """

    if fromdata is None:
        if naxis is None:
            raise ValueError("Keywords 'naxis' or 'fromdata' must be specifie" \
                             'd.')
        if isinstance(naxis, np.ndarray) and naxis.size > 8:
            raise ValueError('First argument is naxis=(NAXIS1,NAXIS2,...)')
        if dtype is not None:
            typename = dtype.name
        else:
            typename = 'float64'
    else:
        array = fromdata
        if not isinstance(array, np.ndarray):
            raise TypeError('The input is not an ndarray.')
        if naxis is None:
            naxis = tuple(reversed(array.shape))
        if dtype is not None:
            array = arrays.astype(dtype)
        if array.dtype.itemsize == 1:
            typename = 'uint8'
        elif array.dtype.names is not None:
            typename = None
        else:
            typename = array.dtype.name

    if type(naxis) not in (list, tuple):
        naxis = (naxis, )
    numaxis = len(naxis)

    if extname is None:
        card = pyfits.createCard('simple', True)
    else:
        card = pyfits.createCard('xtension', 'IMAGE', 'Image extension')
    header = pyfits.Header([card])
    if typename is not None:
        header.update('bitpix', pyfits.PrimaryHDU.ImgCode[typename],
                      'array data type')
    header.update('naxis', numaxis, 'number of array dimensions')
    for dim in range(numaxis):
        header.update('naxis' + str(dim + 1), naxis[dim])
    if extname is None:
        header.update('extend', True)
    else:
        header.update('pcount', 0, 'number of parameters')
        header.update('gcount', 1, 'number of groups')
        header.update('extname', extname)

    if cd is not None:
        cd = np.asarray(cd, dtype=np.float64)
        if cd.shape != (2, 2):
            raise ValueError('The CD matrix is not a 2x2 matrix.')
    else:
        if cdelt is None:
            return header
        if _my_isscalar(cdelt):
            cdelt = (-cdelt, cdelt)
        if pa is None:
            pa = 0.
        theta = np.deg2rad(-pa)
        cd = np.diag(cdelt).dot(
            np.array([[np.cos(theta), np.sin(theta)],
                      [-np.sin(theta), np.cos(theta)]]))

    crval = np.asarray(crval, dtype=np.float64)
    if crval.size != 2:
        raise ValueError('CRVAL does not have two elements.')

    if crpix is None:
        crpix = (np.array(naxis) + 1) / 2.
    else:
        crpix = np.asarray(crpix, dtype=np.float64)
    if crpix.size != 2:
        raise ValueError('CRPIX does not have two elements.')

    ctype = np.asarray(ctype, dtype=np.string_)
    if ctype.size != 2:
        raise ValueError('CTYPE does not have two elements.')

    if _my_isscalar(cunit):
        cunit = (cunit, cunit)
    cunit = np.asarray(cunit, dtype=np.string_)
    if cunit.size != 2:
        raise ValueError('CUNIT does not have two elements.')

    header.update('crval1', crval[0])
    header.update('crval2', crval[1])
    header.update('crpix1', crpix[0])
    header.update('crpix2', crpix[1])
    header.update('cd1_1', cd[0, 0])
    header.update('cd2_1', cd[1, 0])
    header.update('cd1_2', cd[0, 1])
    header.update('cd2_2', cd[1, 1])
    header.update('ctype1', ctype[0])
    header.update('ctype2', ctype[1])
    header.update('cunit1', cunit[0])
    header.update('cunit2', cunit[1])
    header.update('equinox', 2000.)

    return header
예제 #2
0
#!/usr/bin/env python

import time
import pyfits
from AllSky340 import AllSky340

cam = AllSky340(port="/dev/tty.usbserial-A700dzlT", baudrate=460800, timeout=1)
cam.log_info("Testing camera communication.")

p = cam.ping()
if p:
    exp = 0.1
    cam.log_info("Taking %f sec test image." % exp)
    imag = cam.getImage(exp, light=True)
    now = time.localtime()
    date = time.strftime("%Y/%m/%d")
    sast = time.strftime("%H:%M:%S")

    # set up and create the FITS file
    cards = []
    cards.append(pyfits.createCard("DATEOBS", date, "Date of observation"))
    cards.append(
        pyfits.createCard("TIMEOBS", sast, "Time of observation (SAST)"))
    cards.append(pyfits.createCard("EXPTIME", exp, "Exposure time (s)"))
    header = pyfits.Header(cards=cards)
    pyfits.writeto("test.fits", imag, header=header, clobber=True)
예제 #3
0
#!/usr/bin/env python

import time
import pyfits
from AllSky340 import AllSky340

cam = AllSky340(port="/dev/tty.usbserial-A700dzlT", baudrate=460800, timeout=1)
cam.log_info("Testing camera communication.")

p = cam.ping()
if p:
    exp = 0.1
    cam.log_info("Taking %f sec test image." % exp)
    imag = cam.getImage(exp, light=True)
    now = time.localtime()
    date = time.strftime("%Y/%m/%d")
    sast = time.strftime("%H:%M:%S")

    # set up and create the FITS file
    cards = []
    cards.append(pyfits.createCard("DATEOBS", date, "Date of observation"))
    cards.append(pyfits.createCard("TIMEOBS", sast, "Time of observation (SAST)"))
    cards.append(pyfits.createCard("EXPTIME", exp, "Exposure time (s)"))
    header = pyfits.Header(cards=cards)
    pyfits.writeto("test.fits", imag, header=header, clobber=True)
예제 #4
0
def create_fitsheader(naxis=None, dtype=None, fromdata=None, extname=None,
                      crval=(0.,0.), crpix=None, ctype=('RA---TAN','DEC--TAN'),
                      cunit='deg', cd=None, cdelt=None, pa=None, equinox=2000.):
    """
    Helper to create a FITS header.

    Parameters
    ----------
    naxis : array, optional
        (NAXIS1,NAXIS2,...) tuple, which specifies the data dimensions.
    dtype : data-type, optional
        Desired data type for the data stored in the FITS file
    fromdata : array_like, optional
        An array from which the dimensions and typewill be extracted. Note
        that following the FITS convention, the dimension along X is the
        second value of the array shape and that the dimension along the
        Y axis is the first one.
    extname : None or string
        if a string is specified ('' can be used), the returned header
        type will be an Image HDU (otherwise a Primary HDU).
    crval : 2 element array, optional
        Reference pixel values (FITS convention).
    crpix : 2 element array, optional
        Reference pixel (FITS convention).
    ctype : 2 element string array, optional
        Projection types.
    cunit : string or 2 element string array
        Units of the CD matrix (default is degrees/pixel).
    cd : 2 x 2 array
        FITS parameters
            CD1_1 CD1_2
            CD2_1 CD2_2
    cdelt : float or 2 element array
        Physical increment at the reference pixel.
    pa : float
        Position angle of the Y=AXIS2 axis (=-CROTA2).
    equinox : float
        Reference equinox

    Examples
    --------
    >>> map = Map.ones((10,100), unit='Jy/pixel')
    >>> map.header = create_fitsheader(naxis=(100,10), cd=[[-1,0],[0,1]])
    >>> map.header = create_fitsheader(fromdata=map)
    """

    if fromdata is None:
        if naxis is None:
            raise ValueError("Keywords 'naxis' or 'fromdata' must be specifie" \
                             'd.')
        if isinstance(naxis, np.ndarray) and naxis.size > 8:
            raise ValueError('First argument is naxis=(NAXIS1,NAXIS2,...)')
        if dtype is not None:
            typename = dtype.name
        else:
            typename = 'float64'
    else:
        array = fromdata
        if not isinstance(array, np.ndarray):
            raise TypeError('The input is not an ndarray.')
        if naxis is None:
            naxis = tuple(reversed(array.shape))
        if dtype is not None:
            array = arrays.astype(dtype)
        if array.dtype.itemsize == 1:
            typename = 'uint8'
        elif array.dtype.names is not None:
            typename = None
        else:
            typename = array.dtype.name

    if type(naxis) not in (list, tuple):
        naxis = (naxis,)
    numaxis = len(naxis)

    if extname is None:
        card = pyfits.createCard('simple', True)
    else:
        card = pyfits.createCard('xtension', 'IMAGE', 'Image extension')
    header = pyfits.Header([card])
    if typename is not None:
        header.update('bitpix', pyfits.PrimaryHDU.ImgCode[typename],
                      'array data type')
    header.update('naxis', numaxis, 'number of array dimensions')
    for dim in range(numaxis):
        header.update('naxis'+str(dim+1), naxis[dim])
    if extname is None:
        header.update('extend', True)
    else:
        header.update('pcount', 0, 'number of parameters')
        header.update('gcount', 1, 'number of groups')
        header.update('extname', extname)

    if cd is not None:
        cd = np.asarray(cd, dtype=np.float64)
        if cd.shape != (2,2):
            raise ValueError('The CD matrix is not a 2x2 matrix.')
    else:
        if cdelt is None:
            return header
        if _my_isscalar(cdelt):
            cdelt = (-cdelt, cdelt)
        if pa is None:
            pa = 0.
        theta=np.deg2rad(-pa)
        cd = np.diag(cdelt).dot(np.array([[ np.cos(theta), np.sin(theta)],
                                          [-np.sin(theta), np.cos(theta)]]))

    crval = np.asarray(crval, dtype=np.float64)
    if crval.size != 2:
        raise ValueError('CRVAL does not have two elements.')

    if crpix is None:
        crpix = (np.array(naxis) + 1) / 2.
    else:
        crpix = np.asarray(crpix, dtype=np.float64)
    if crpix.size != 2:
        raise ValueError('CRPIX does not have two elements.')

    ctype = np.asarray(ctype, dtype=np.string_)
    if ctype.size != 2:
        raise ValueError('CTYPE does not have two elements.')

    if _my_isscalar(cunit):
        cunit = (cunit, cunit)
    cunit = np.asarray(cunit, dtype=np.string_)
    if cunit.size != 2:
        raise ValueError('CUNIT does not have two elements.')

    header.update('crval1', crval[0])
    header.update('crval2', crval[1])
    header.update('crpix1', crpix[0])
    header.update('crpix2', crpix[1])
    header.update('cd1_1' , cd[0,0])
    header.update('cd2_1' , cd[1,0])
    header.update('cd1_2' , cd[0,1])
    header.update('cd2_2' , cd[1,1])
    header.update('ctype1', ctype[0])
    header.update('ctype2', ctype[1])
    header.update('cunit1', cunit[0])
    header.update('cunit2', cunit[1])
    header.update('equinox', 2000.)

    return header