def gal2cel(regfile):
    """
    Converts a region file from galactic to celestial coordinates including
    position angle reference from the center of the box (right now only works
    on box regions)

    Requires pyregion with the ShapeList.write() function implemented...
    not clear if that exists in 1.0
    """
    reg = pyregion.open(regfile)

    for R in reg:
        if R.name == 'box':
            x, y, dx, dy, angle = R.coord_list

            posn = coords.Position([x, y], system='galactic')
            ra, dec = posn.j2000()

            newang = posang.posang(x - dx, y, x + dx, y, system='galactic')

            coord_list = [ra, dec, dx, dy, angle - newang - 90]

            R.coord_format = 'fk5'
            R.coord_list = coord_list
            R.params = coord_list

    reg.write(regfile[:-4] + "_fk5.reg")
Beispiel #2
0
def gal2cel(regfile):
    """
    Converts a region file from galactic to celestial coordinates including
    position angle reference from the center of the box (right now only works
    on box regions)

    Requires pyregion with the ShapeList.write() function implemented...
    not clear if that exists in 1.0
    """
    reg = pyregion.open(regfile)

    for R in reg:
        if R.name == "box":
            x, y, dx, dy, angle = R.coord_list

            # posn = coords.Position([x,y],system='galactic')
            # ra,dec = posn.j2000()
            posn = coordinates.Galactic(x * u.deg, y * u.deg)
            ra, dec = posn.fk5.ra.deg, posn.fk5.dec.deg

            newang = posang.posang(x - dx, y, x + dx, y, system="galactic")

            coord_list = [ra, dec, dx, dy, angle - newang - 90]

            R.coord_format = "fk5"
            R.coord_list = coord_list
            R.params = coord_list

    reg.write(regfile[:-4] + "_fk5.reg")
Beispiel #3
0
    def rotcrop_cube(x1, y1, x2, y2, cubename, outname, xwidth=25, ywidth=25,
            in_system='galactic',  out_system='equatorial', clobber=True, 
            newheader=None, xcen=None, ycen=None):
        """
        Crop a data cube and then rotate it with montage

        """

        cubefile = pyfits.open(cubename)

        if xcen is None and ycen is None:
            pos1 = coords.Position([x1,y1],system=in_system)
            pos2 = coords.Position([x2,y2],system=in_system)

            if cubefile[0].header.get('CTYPE1')[:2] == 'RA':
                x1,y1 = pos1.j2000()
                x2,y2 = pos2.j2000()
                coord_system = 'celestial'
            elif  cubefile[0].header.get('CTYPE1')[:4] == 'GLON':
                x1,y1 = pos1.galactic()
                x2,y2 = pos2.galactic()
                coord_system = 'galactic'

            xcen = (x1+x2)/2.0
            ycen = (y1+y2)/2.0
            print xcen,ycen,xwidth,ywidth,coord_system
        else:
            coord_system = in_system

        sc = subcube(cubefile[0].data, xcen, xwidth, ycen, ywidth, 
                widthunits='pixels', units="wcs", header=cubefile[0].header,
                return_HDU=True)
        # note: there should be no security risk here because pyfits' writeto
        # will not overwrite by default
        tempcube = tempfile.mktemp(suffix='.fits')
        sc.writeto(tempcube)
        
        pa = posang.posang(x1,y1,x2,y2,system=coord_system) - 90

        if newheader is None:
            newheader = sc.header.copy()
            cd11 = newheader.get('CDELT1') if newheader.get('CDELT1') else newheader.get('CD1_1')
            cd22 = newheader.get('CDELT2') if newheader.get('CDELT2') else newheader.get('CD2_2')
            cd12 = newheader.get('CD1_2') if newheader.get('CD1_2') else 0.0
            cd21 = newheader.get('CD2_1') if newheader.get('CD2_1') else 0.0
            cdelt = numpy.sqrt(cd11**2+cd12**2)

            tempheader = tempfile.mktemp(suffix='.hdr')
            ycensign = "+" if numpy.sign(ycen) >= 0 else "-"
            montage.mHdr("%s %1s%s" % (xcen, ycensign, numpy.abs(ycen)), xwidth*cdelt,
                    tempheader, system=out_system, height=ywidth*cdelt,
                    pix_size=cdelt*3600.0, rotation=pa)
            os.system("sed -i bck '/END/d' %s" % (tempheader))
            newheader2 = pyfits.Header()
            newheader2.fromTxtFile(tempheader)
            #newheader2.fromtextfile(tempheader)
            for key in ('CRPIX3','CRVAL3','CDELT3','CD3_3','CUNIT3','WCSTYPE3','CTYPE3'):
                if newheader.get(key):
                    newheader2.update(key,newheader.get(key))
            if newheader.get('CD3_3') and newheader2.get('CDELT3') is None:
                newheader2.update('CDELT3',newheader.get('CD3_3'))
            newheader2.toTxtFile(tempheader,clobber=True)
            #if newheader2.get('CDELT3') is None:
            #    raise Exception("No CD3_3 or CDELT3 in header.")
        else:
            if isinstance(newheader,str):
                newheader2 = pyfits.Header()
                newheader2.fromTxtFile(newheader)
            tempheader = tempfile.mktemp(suffix='.hdr')
            newheader2.toTxtFile(tempheader,clobber=True)


        montage.wrappers.reproject_cube(tempcube,outname,header=tempheader,clobber=clobber)
        #print "\n",outname
        #os.system('imhead %s | grep CDELT' % outname)

        # AWFUL hack because montage removes CDELT3
        tempcube = pyfits.open(outname)
        tempcube.header = newheader2
        #if tempcube.header.get('CDELT3') is None:
        #    raise Exception("No CD3_3 or CDELT3 in header.")
        #print tempcube.header.get('CDELT3')
        tempcube.writeto(outname,clobber=True)
        #print tempcube.get('CDELT3')
        #print "\n",outname
        #os.system('imhead %s | grep CDELT' % outname)

        #print "\nnewheader2"
        #print newheader2.ascard
        #print
        
        return
Beispiel #4
0
    def rotcrop_cube(x1, y1, x2, y2, cubename, outname, xwidth=25, ywidth=25,
                     in_system='galactic',  out_system='equatorial',
                     clobber=True, newheader=None, xcen=None, ycen=None):
        """
        Crop a data cube and then rotate it with montage

        """

        cubefile = fits.open(cubename)

        if xcen is None and ycen is None:
            pos1 = coords.Position([x1,y1],system=in_system)
            pos2 = coords.Position([x2,y2],system=in_system)

            if cubefile[0].header.get('CTYPE1')[:2] == 'RA':
                x1,y1 = pos1.j2000()
                x2,y2 = pos2.j2000()
                coord_system = 'celestial'
            elif  cubefile[0].header.get('CTYPE1')[:4] == 'GLON':
                x1,y1 = pos1.galactic()
                x2,y2 = pos2.galactic()
                coord_system = 'galactic'

            xcen = (x1+x2)/2.0
            ycen = (y1+y2)/2.0
            print xcen,ycen,xwidth,ywidth,coord_system
        else:
            coord_system = in_system

        sc = subcube(cubefile[0].data, xcen, xwidth, ycen, ywidth, 
                widthunits='pixels', units="wcs", header=cubefile[0].header,
                return_HDU=True)
        # note: there should be no security risk here because fits' writeto
        # will not overwrite by default
        tempcube = tempfile.mktemp(suffix='.fits')
        sc.writeto(tempcube)
        
        pa = posang.posang(x1,y1,x2,y2,system=coord_system) - 90

        if newheader is None:
            newheader = sc.header.copy()
            cd11 = newheader.get('CDELT1') if newheader.get('CDELT1') else newheader.get('CD1_1')
            cd22 = newheader.get('CDELT2') if newheader.get('CDELT2') else newheader.get('CD2_2')
            cd12 = newheader.get('CD1_2') if newheader.get('CD1_2') else 0.0
            cd21 = newheader.get('CD2_1') if newheader.get('CD2_1') else 0.0
            cdelt = numpy.sqrt(cd11**2+cd12**2)

            tempheader = tempfile.mktemp(suffix='.hdr')
            ycensign = "+" if numpy.sign(ycen) >= 0 else "-"
            montage.mHdr("%s %1s%s" % (xcen, ycensign, numpy.abs(ycen)), xwidth*cdelt,
                    tempheader, system=out_system, height=ywidth*cdelt,
                    pix_size=cdelt*3600.0, rotation=pa)
            os.system("sed -i bck '/END/d' %s" % (tempheader))
            newheader2 = fits.Header()
            newheader2.fromTxtFile(tempheader)
            #newheader2.fromtextfile(tempheader)
            for key in ('CRPIX3','CRVAL3','CDELT3','CD3_3','CUNIT3','WCSTYPE3','CTYPE3'):
                if newheader.get(key):
                    newheader2[key] = newheader.get(key)
            if newheader.get('CD3_3') and newheader2.get('CDELT3') is None:
                newheader2['CDELT3'] = newheader.get('CD3_3')
            newheader2.toTxtFile(tempheader,clobber=True)
            #if newheader2.get('CDELT3') is None:
            #    raise Exception("No CD3_3 or CDELT3 in header.")
        else:
            if isinstance(newheader,str):
                newheader2 = fits.Header()
                newheader2.fromTxtFile(newheader)
            tempheader = tempfile.mktemp(suffix='.hdr')
            newheader2.toTxtFile(tempheader,clobber=True)


        montage.wrappers.reproject_cube(tempcube,outname,header=tempheader,clobber=clobber)
        #print "\n",outname
        #os.system('imhead %s | grep CDELT' % outname)

        # AWFUL hack because montage removes CDELT3
        tempcube = fits.open(outname)
        tempcube.header = newheader2
        #if tempcube.header.get('CDELT3') is None:
        #    raise Exception("No CD3_3 or CDELT3 in header.")
        #print tempcube.header.get('CDELT3')
        tempcube.writeto(outname,clobber=True)
        #print tempcube.get('CDELT3')
        #print "\n",outname
        #os.system('imhead %s | grep CDELT' % outname)

        
        return
Beispiel #5
0
    def rotcrop_cube(
        x1,
        y1,
        x2,
        y2,
        cubename,
        outname,
        xwidth=25,
        ywidth=25,
        in_system="galactic",
        out_system="equatorial",
        clobber=True,
    ):
        """
        Crop a data cube and then rotate it with montage

        """

        cubefile = pyfits.open(cubename)

        if not coordsOK:
            raise ImportError("cubes.py requires coords for rotcrop_cube")
        pos1 = coords.Position([x1, y1], system=in_system)
        pos2 = coords.Position([x2, y2], system=in_system)

        if cubefile[0].header.get("CTYPE1")[:2] == "RA":
            x1, y1 = pos1.j2000()
            x2, y2 = pos2.j2000()
            coord_system = "celestial"
        elif cubefile[0].header.get("CTYPE1")[:2] == "GLON":
            x1, y1 = pos1.galactic()
            x2, y2 = pos2.galactic()
            coord_system = "galactic"

        xcen = (x1 + x2) / 2.0
        ycen = (y1 + y2) / 2.0

        sc = subcube(
            cubefile[0].data,
            xcen,
            xwidth,
            ycen,
            ywidth,
            widthunits="pixels",
            units="wcs",
            header=cubefile[0].header,
            return_HDU=True,
        )
        # note: there should be no security risk here because pyfits' writeto
        # will not overwrite by default
        tempcube = tempfile.mktemp(suffix=".fits")
        sc.writeto(tempcube)

        pa = posang.posang(x1, y1, x2, y2, system=coord_system) - 90

        newheader = sc.header.copy()
        cd11 = newheader.get("CDELT1") if newheader.get("CDELT1") else newheader.get("CD1_1")
        cd22 = newheader.get("CDELT2") if newheader.get("CDELT2") else newheader.get("CD2_2")
        cd12 = newheader.get("CD1_2") if newheader.get("CD1_2") else 0.0
        cd21 = newheader.get("CD2_1") if newheader.get("CD2_1") else 0.0
        cdelt = numpy.sqrt(cd11 ** 2 + cd12 ** 2)

        tempheader = tempfile.mktemp(suffix=".hdr")
        ycensign = "+" if numpy.sign(ycen) >= 0 else "-"
        montage.mHdr(
            "%s %1s%s" % (xcen, ycensign, numpy.abs(ycen)),
            xwidth * cdelt,
            tempheader,
            system=out_system,
            height=ywidth * cdelt,
            pix_size=cdelt * 3600.0,
            rotation=pa,
        )
        os.system("sed -i bck '/END/d' %s" % (tempheader))
        newheader2 = pyfits.Header()
        newheader2.fromTxtFile(tempheader)
        for key in ("CRPIX3", "CRVAL3", "CDELT3", "CD3_3", "CUNIT3", "WCSTYPE3", "CTYPE3"):
            if newheader.get(key):
                newheader2.update(key, newheader.get(key))
        if newheader.get("CD3_3") and newheader2.get("CDELT3") is None:
            newheader2.update("CDELT3", newheader.get("CD3_3"))
        newheader2.toTxtFile(tempheader, clobber=True)
        # if newheader2.get('CDELT3') is None:
        #    raise Exception("No CD3_3 or CDELT3 in header.")

        montage.wrappers.reproject_cube(tempcube, outname, header=tempheader, clobber=clobber)
        # print "\n",outname
        # os.system('imhead %s | grep CDELT' % outname)

        # AWFUL hack because montage removes CDELT3
        tempcube = pyfits.open(outname)
        tempcube.header = newheader2
        # if tempcube.header.get('CDELT3') is None:
        #    raise Exception("No CD3_3 or CDELT3 in header.")
        # print tempcube.header.get('CDELT3')
        tempcube.writeto(outname, clobber=True)
        # print tempcube.get('CDELT3')
        # print "\n",outname
        # os.system('imhead %s | grep CDELT' % outname)

        # print "\nnewheader2"
        # print newheader2.ascard
        # print

        return