Example #1
0
def makeEmptyCEATemplate(raSizeDeg, decSizeDeg,meanRa = 180., meanDec = 0.,\
                         pixScaleXarcmin = 0.5, pixScaleYarcmin=0.5):
    assert(meanDec == 0.,'mean dec other than zero not implemented yet')
    
    
    cdelt1 = -pixScaleXarcmin/60.
    cdelt2 = pixScaleYarcmin/60.
    naxis1 = numpy.int(raSizeDeg/pixScaleXarcmin*60.+0.5)
    naxis2 = numpy.int(decSizeDeg/pixScaleYarcmin*60.+0.5)
    refPix1 = naxis1/2.
    refPix2 = naxis2/2.
    pv2_1 = 1.0
    cardList = pyfits.CardList()
    cardList.append(pyfits.Card('NAXIS', 2))
    cardList.append(pyfits.Card('NAXIS1', naxis1))
    cardList.append(pyfits.Card('NAXIS2', naxis2))
    cardList.append(pyfits.Card('CTYPE1', 'RA---CEA'))
    cardList.append(pyfits.Card('CTYPE2', 'DEC--CEA'))
    cardList.append(pyfits.Card('CRVAL1', meanRa))
    cardList.append(pyfits.Card('CRVAL2', meanDec))
    cardList.append(pyfits.Card('CRPIX1', refPix1+1))
    cardList.append(pyfits.Card('CRPIX2', refPix2+1))
    cardList.append(pyfits.Card('CDELT1', cdelt1))
    cardList.append(pyfits.Card('CDELT2', cdelt2))
    cardList.append(pyfits.Card('CUNIT1', 'DEG'))
    cardList.append(pyfits.Card('CUNIT2', 'DEG'))
    hh = pyfits.Header(cards=cardList)
    wcs = astLib.astWCS.WCS(hh, mode='pyfits')
    data = numpy.zeros([naxis2,naxis1])
    ltMap = liteMap.liteMapFromDataAndWCS(data,wcs)
    
    return ltMap
Example #2
0
def mkfitshdr(cards=None, usedefaults=True):
	"""
	Make a FITS file header of all arguments supplied in the dict **cards**.

	If **usedefaults** is set, also add default header items:
	- Program filename and pasth (from sys.argv[0])
	- Current working dir
	- Program filesize, mtime and ctime
	- Git revision of executable (if available)
	- epoch (time())
	- utctime / localtime
	- hostid

	@params [in] cards Dict containing key=value pairs for the header
	@params [in] usedefaults Also store default parameters in header
	@return pyfits header object
	"""

	clist = pyfits.CardList()

	if (usedefaults):
		clist.append(pyfits.Card(key='progname', 
								value=os.path.basename(sys.argv[0]),
								comment='Program filename') )
		clist.append(pyfits.Card(key='progpath', 
								value=os.path.dirname(sys.argv[0]),
								comment='Program path') )
		grev = git_rev(sys.argv[0])
		if (grev):
			clist.append(pyfits.Card(key='gitrev', 
								value=grev,
								comment='Program git revision') )
		clist.append(pyfits.Card(key='progsize', 
								value=os.path.getsize(sys.argv[0]),
								comment='Program filesize (bytes)') )
		clist.append(pyfits.Card(key='mtime', 
								value=os.path.getmtime(sys.argv[0]),
								comment='Program last last modification time') )
		clist.append(pyfits.Card(key='ctime', 
								value=os.path.getctime(sys.argv[0]),
								comment='Program metadata change time' ) )
		clist.append(pyfits.Card(key='curdir', 
								value=os.path.realpath(os.path.curdir),
								comment='Current working dir') )
		clist.append(pyfits.Card(key='epoch', value=time(),
								comment='Current seconds since epoch from time.time()') )
		# No comments for the last two fields because they are too large
		clist.append(pyfits.Card(key='utctime', value=asctime(gmtime(time()))) )
		clist.append(pyfits.Card(key='loctime', value=asctime(localtime(time()))) )
		clist.append(pyfits.Card(key='hostid', value=os.uname()[1],
								comment='Hostname from os.uname()') )

	if (cards):
		for key, val in cards.iteritems():
			clist.append(pyfits.Card(key, val) )

	return pyfits.Header(cards=clist)
Example #3
0
def resampleToTanProjection(imageData,
                            imageWCS,
                            outputPixDimensions=[600, 600]):
    """Resamples an image and WCS to a tangent plane projection. Purely for plotting purposes
    (e.g., ensuring RA, dec. coordinate axes perpendicular).
    
    @type imageData: numpy array
    @param imageData: image data array
    @type imageWCS: astWCS.WCS
    @param imageWCS: astWCS.WCS object
    @type outputPixDimensions: list
    @param outputPixDimensions: [width, height] of output image in pixels
    @rtype: dictionary
    @return: image data (numpy array), updated astWCS WCS object for image, in format {'data', 'wcs'}.
    
    """

    RADeg, decDeg = imageWCS.getCentreWCSCoords()
    xPixelScale = imageWCS.getXPixelSizeDeg()
    yPixelScale = imageWCS.getYPixelSizeDeg()
    xSizeDeg, ySizeDeg = imageWCS.getFullSizeSkyDeg()
    xSizePix = int(round(outputPixDimensions[0]))
    ySizePix = int(round(outputPixDimensions[1]))
    xRefPix = xSizePix / 2.0
    yRefPix = ySizePix / 2.0
    xOutPixScale = xSizeDeg / xSizePix
    yOutPixScale = ySizeDeg / ySizePix
    cardList = pyfits.CardList()
    cardList.append(pyfits.Card('NAXIS', 2))
    cardList.append(pyfits.Card('NAXIS1', xSizePix))
    cardList.append(pyfits.Card('NAXIS2', ySizePix))
    cardList.append(pyfits.Card('CTYPE1', 'RA---TAN'))
    cardList.append(pyfits.Card('CTYPE2', 'DEC--TAN'))
    cardList.append(pyfits.Card('CRVAL1', RADeg))
    cardList.append(pyfits.Card('CRVAL2', decDeg))
    cardList.append(pyfits.Card('CRPIX1', xRefPix + 1))
    cardList.append(pyfits.Card('CRPIX2', yRefPix + 1))
    cardList.append(pyfits.Card('CDELT1', -xOutPixScale))
    cardList.append(pyfits.Card(
        'CDELT2', xOutPixScale))  # Makes more sense to use same pix scale
    cardList.append(pyfits.Card('CUNIT1', 'DEG'))
    cardList.append(pyfits.Card('CUNIT2', 'DEG'))
    newHead = pyfits.Header(cards=cardList)
    newWCS = astWCS.WCS(newHead, mode='pyfits')
    newImage = numpy.zeros([ySizePix, xSizePix])

    tanImage = resampleToWCS(newImage,
                             newWCS,
                             imageData,
                             imageWCS,
                             highAccuracy=True,
                             onlyOverlapping=False)

    return tanImage
Example #4
0
def makeEmptyCEATemplateAdvanced(ra0, dec0, \
                                 ra1, dec1,\
                                 pixScaleXarcmin = 0.5, \
                                 pixScaleYarcmin= 0.5):

    """
    ALL RA DEC IN DEGREES
    """
    assert(ra0<ra1)
    assert(dec0<dec1)
    refDec = (dec0+dec1)/2.
    cosRefDec =  numpy.cos(refDec/180.*numpy.pi)
    raSizeDeg  = (ra1 - ra0)*cosRefDec
    decSizeDeg = (dec1-dec0)
    
    cdelt1 = -pixScaleXarcmin/(60.*cosRefDec)
    cdelt2 = pixScaleYarcmin/(60.*cosRefDec)
    naxis1 = numpy.int(raSizeDeg/pixScaleXarcmin*60.+0.5)
    naxis2 = numpy.int(decSizeDeg/pixScaleYarcmin*60.+0.5)
    refPix1 = numpy.int(-ra1/cdelt1+0.5)
    refPix2 = numpy.int(numpy.sin(-dec0*numpy.pi/180.)\
                        *180./numpy.pi/cdelt2/cosRefDec**2+0.5)
    pv2_1 = cosRefDec**2
    cardList = pyfits.CardList()
    cardList.append(pyfits.Card('NAXIS', 2))
    cardList.append(pyfits.Card('NAXIS1', naxis1))
    cardList.append(pyfits.Card('NAXIS2', naxis2))
    cardList.append(pyfits.Card('EXTEND', True))
    cardList.append(pyfits.Card('CTYPE1', 'RA---CEA'))
    cardList.append(pyfits.Card('CTYPE2', 'DEC--CEA'))
    cardList.append(pyfits.Card('CRVAL1', 0))
    cardList.append(pyfits.Card('CRVAL2', 0))
    cardList.append(pyfits.Card('CRPIX1', refPix1+1))
    cardList.append(pyfits.Card('CRPIX2', refPix2+1))
    cardList.append(pyfits.Card('CDELT1', cdelt1))
    cardList.append(pyfits.Card('CDELT2', cdelt2))
    cardList.append(pyfits.Card('CUNIT1', 'DEG'))
    cardList.append(pyfits.Card('CUNIT2', 'DEG'))
    cardList.append(pyfits.Card('PV2_1', pv2_1))
    cardList.append(pyfits.Card('EQUINOX',2000))
    cardList.append(pyfits.Card('PC1_1',1))
    cardList.append(pyfits.Card('PC1_2',0))
    cardList.append(pyfits.Card('PC2_1',0))
    cardList.append(pyfits.Card('PC2_2',1))
    
    hh = pyfits.Header(cards=cardList)
    wcs = astLib.astWCS.WCS(hh, mode='pyfits')
    data = numpy.zeros([naxis2,naxis1])
    ltMap = liteMapFromDataAndWCS(data,wcs)
    
    return ltMap
Example #5
0
def combineHdr(headers):
    combHdr = []
    for i in range(len(headers)):
        if i == 0:
            final_card = headers[i]._header.ascardlist()
        final_keys = final_card.keys()
        if i > 0:
            card = headers[i]._header.ascardlist()
            keys = card.keys()
            for k in keys:
                if not k in final_keys:
                    final_card = final_card + [card[k]]
            final_card = pyfits.CardList(final_card)
    outHdr = Header(pyfits.Header(cards=final_card))
    return outHdr
Example #6
0
 def removeHdrEntries(self, keywords=['']):
     """
         Removes keywords from the Header
         
         Parameters:
         ---------------
         keywords : list of strings, optional
                     list of keywords that are removed from the header
     """
     keys = self._cardlist.keys()
     new_cards = []
     for k in keys:
         if not k in keywords:
             new_cards += [self._cardlist[k]]
     self._cardlist = pyfits.CardList(new_cards)
     self._header = pyfits.Header(self._cardlist)
Example #7
0
def copy_header(hdr):

    try:
        del hdr['TELESCOPE']
    except KeyError:
        a = 1
    
    try:
        del hdr['INSTRUMENT']
    except KeyError:
        a = 1
    
    try:
        del hdr['REST FREQUENCY']
    except KeyError:
        a = 1
    
    try:
        del hdr['TRANSITION']
    except KeyError:
        a = 1

    hdr_cardlist = hdr.ascard
    ncards = len(hdr_cardlist)
    
    new_hdr_cardlist = pyfits.CardList()

    for i in range(0, ncards):

        new_hdr_cardlist.append(pyfits.Card( 
                                    copy.deepcopy(hdr_cardlist[i].key), 
                                    copy.deepcopy(hdr_cardlist[i].value), 
                                    copy.deepcopy(hdr_cardlist[i].comment) 
                                            )
                                )
    
    new_hdr = pyfits.Header(new_hdr_cardlist)

    return new_hdr
Example #8
0
 def appendHeader(self, Header):
     new_cardlist = self._cardlist + Header.getHdrCardlist()
     self._cardlist = pyfits.CardList(new_cardlist)
     self._header = pyfits.Header(self._cardlist)
Example #9
0
 def copyHdrKey(self, Header, key):
     new_cardlist = self._cardlist + [Header.getHdrCard(key)]
     self._cardlist = pyfits.CardList(new_cardlist)
     self._header = pyfits.Header(self._cardlist)