def test_galacticToEcliptic(self): """ Verify correctness of transformations from the Galactic to Ecliptic coordinate systems. """ ct=CoordinateTransformation(Transformations.GAL2ECL) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatGalacticToEcliptic[0,:], decimal=2) assert_array_almost_equal(y, self.expectedMatGalacticToEcliptic[1,:], decimal=2) assert_array_almost_equal(z, self.expectedMatGalacticToEcliptic[2,:], decimal=2) r, expectedLambda, expectedBeta = cartesianToSpherical(self.expectedMatGalacticToEcliptic[0,:], self.expectedMatGalacticToEcliptic[1,:], self.expectedMatGalacticToEcliptic[2,:]) lambdaEcl, betaEcl = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) # # Note the abs() in the line below is required to avoid an error when -pi and pi are compared. # The better solution of course is to write an assert function that can handle modulo 2*pi cases. # assert_array_almost_equal(abs(expectedLambda), abs(lambdaEcl), decimal=1) assert_array_almost_equal(expectedBeta, betaEcl, decimal=1) galon = 2.0*pi*rand(100) galat = -pi/2.0+pi*rand(100) lambdaEcl, betaEcl = ct.transformSkyCoordinates(galon, galat) phi=6.38/180.0*pi result = 0.4971*sin(galat)+0.8677*sin(galon-phi)*cos(galat) assert_array_almost_equal(result, sin(betaEcl), decimal=2)
def test_galacticToEcliptic(self): """ Verify correctness of transformations from the Galactic to Ecliptic coordinate systems. """ ct=CoordinateTransformation(Transformations.GAL2ECL) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatGalacticToEcliptic[0,:], decimal=2) assert_array_almost_equal(y, self.expectedMatGalacticToEcliptic[1,:], decimal=2) assert_array_almost_equal(z, self.expectedMatGalacticToEcliptic[2,:], decimal=2) r, expectedLambda, expectedBeta = cartesianToSpherical(self.expectedMatGalacticToEcliptic[0,:], self.expectedMatGalacticToEcliptic[1,:], self.expectedMatGalacticToEcliptic[2,:]) lambdaEcl, betaEcl = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) # # Note the abs() in the line below is required to avoid an error when -pi and pi are compared. # The better solution of course is to write an assert function that can handle modulo 2*pi cases. # assert_array_almost_equal(abs(expectedLambda), abs(lambdaEcl), decimal=1) assert_array_almost_equal(expectedBeta, betaEcl, decimal=1) galon = 2.0*pi*rand(100) galat = -pi/2.0+pi*rand(100) lambdaEcl, betaEcl = ct.transformSkyCoordinates(galon, galat) phi=6.38/180.0*pi result = 0.4971*sin(galat)+0.8677*sin(galon-phi)*cos(galat) assert_array_almost_equal(result, sin(betaEcl), decimal=2)
def test_galacticToIcrs(self): """ Verify correctness of transformations from the Galactic to ICRS coordinate systems. """ ct = CoordinateTransformation(Transformations.GAL2ICRS) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatIcrsToGal[:,0], decimal=2) assert_array_almost_equal(y, self.expectedMatIcrsToGal[:,1], decimal=2) assert_array_almost_equal(z, self.expectedMatIcrsToGal[:,2], decimal=2) r, expectedAlpha, expectedDelta = cartesianToSpherical(self.expectedMatIcrsToGal[:,0], self.expectedMatIcrsToGal[:,1], self.expectedMatIcrsToGal[:,2]) alpha, delta = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedAlpha, alpha, decimal=1) assert_array_almost_equal(expectedDelta, delta, decimal=1) alpha, delta = ct.transformSkyCoordinates(0.0,pi/2.0) assert_allclose(alpha/pi*180, 192.9, atol=1.0) assert_allclose(delta/pi*180, 27.1, atol=1.0)
def test_galacticToIcrs(self): """ Verify correctness of transformations from the Galactic to ICRS coordinate systems. """ ct = CoordinateTransformation(Transformations.GAL2ICRS) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatIcrsToGal[:,0], decimal=2) assert_array_almost_equal(y, self.expectedMatIcrsToGal[:,1], decimal=2) assert_array_almost_equal(z, self.expectedMatIcrsToGal[:,2], decimal=2) r, expectedAlpha, expectedDelta = cartesianToSpherical(self.expectedMatIcrsToGal[:,0], self.expectedMatIcrsToGal[:,1], self.expectedMatIcrsToGal[:,2]) alpha, delta = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedAlpha, alpha, decimal=1) assert_array_almost_equal(expectedDelta, delta, decimal=1) alpha, delta = ct.transformSkyCoordinates(0.0,pi/2.0) assert_allclose(alpha/pi*180, (192.9-360.0), atol=1.0) assert_allclose(delta/pi*180, 27.1, atol=1.0)
def test_eclipticToIcrs(self): """ Verify correctness of transformations from the Ecliptic to ICRS coordinate systems. """ ct=CoordinateTransformation(Transformations.ECL2ICRS) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatEclipticToIcrs[0,:], decimal=2) assert_array_almost_equal(y, self.expectedMatEclipticToIcrs[1,:], decimal=2) assert_array_almost_equal(z, self.expectedMatEclipticToIcrs[2,:], decimal=2) r, expectedAlpha, expectedDelta = cartesianToSpherical(self.expectedMatEclipticToIcrs[0,:], self.expectedMatEclipticToIcrs[1,:], self.expectedMatEclipticToIcrs[2,:]) alpha, delta = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedAlpha, alpha, decimal=1) assert_array_almost_equal(expectedDelta, delta, decimal=1) lambdaEcl = 2.0*pi*rand(100) betaEcl = -pi/2.0+pi*rand(100) alpha, delta = ct.transformSkyCoordinates(lambdaEcl, betaEcl) result = 0.9175*sin(betaEcl)+0.3978*sin(lambdaEcl)*cos(betaEcl) assert_array_almost_equal(result, sin(delta), decimal=2)
def test_icrsToEcliptic(self): """ Verify correctness of transformations from the ICRS to Ecliptic coordinate systems. """ ct=CoordinateTransformation(Transformations.ICRS2ECL) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatEclipticToIcrs[:,0], decimal=2) assert_array_almost_equal(y, self.expectedMatEclipticToIcrs[:,1], decimal=2) assert_array_almost_equal(z, self.expectedMatEclipticToIcrs[:,2], decimal=2) r, expectedLambda, expectedBeta = cartesianToSpherical(self.expectedMatEclipticToIcrs[:,0], self.expectedMatEclipticToIcrs[:,1], self.expectedMatEclipticToIcrs[:,2]) lambdaEcl, betaEcl = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedLambda, lambdaEcl, decimal=1) assert_array_almost_equal(expectedBeta, betaEcl, decimal=1) alpha = 2.0*pi*rand(100) delta = -pi/2.0+pi*rand(100) lambdaEcl, betaEcl = ct.transformSkyCoordinates(alpha, delta) result = 0.9175*sin(delta)-0.3978*sin(alpha)*cos(delta) assert_array_almost_equal(result, sin(betaEcl), decimal=2)
def test_eclipticToIcrs(self): """ Verify correctness of transformations from the Ecliptic to ICRS coordinate systems. """ ct=CoordinateTransformation(Transformations.ECL2ICRS) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatEclipticToIcrs[0,:], decimal=2) assert_array_almost_equal(y, self.expectedMatEclipticToIcrs[1,:], decimal=2) assert_array_almost_equal(z, self.expectedMatEclipticToIcrs[2,:], decimal=2) r, expectedAlpha, expectedDelta = cartesianToSpherical(self.expectedMatEclipticToIcrs[0,:], self.expectedMatEclipticToIcrs[1,:], self.expectedMatEclipticToIcrs[2,:]) alpha, delta = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedAlpha, alpha, decimal=1) assert_array_almost_equal(expectedDelta, delta, decimal=1) lambdaEcl = 2.0*pi*rand(100) betaEcl = -pi/2.0+pi*rand(100) alpha, delta = ct.transformSkyCoordinates(lambdaEcl, betaEcl) result = 0.9175*sin(betaEcl)+0.3978*sin(lambdaEcl)*cos(betaEcl) assert_array_almost_equal(result, sin(delta), decimal=2)
def test_icrsToEcliptic(self): """ Verify correctness of transformations from the ICRS to Ecliptic coordinate systems. """ ct=CoordinateTransformation(Transformations.ICRS2ECL) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatEclipticToIcrs[:,0], decimal=2) assert_array_almost_equal(y, self.expectedMatEclipticToIcrs[:,1], decimal=2) assert_array_almost_equal(z, self.expectedMatEclipticToIcrs[:,2], decimal=2) r, expectedLambda, expectedBeta = cartesianToSpherical(self.expectedMatEclipticToIcrs[:,0], self.expectedMatEclipticToIcrs[:,1], self.expectedMatEclipticToIcrs[:,2]) lambdaEcl, betaEcl = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedLambda, lambdaEcl, decimal=1) assert_array_almost_equal(expectedBeta, betaEcl, decimal=1) alpha = 2.0*pi*rand(100) delta = -pi/2.0+pi*rand(100) lambdaEcl, betaEcl = ct.transformSkyCoordinates(alpha, delta) result = 0.9175*sin(delta)-0.3978*sin(alpha)*cos(delta) assert_array_almost_equal(result, sin(betaEcl), decimal=2)
def ICRS_to_GAL(phi, theta, muphistar, mutheta): """ phi - The longitude-like angle of the position of the source (radians). theta - The latitude-like angle of the position of the source (radians). muphistar - Value of the proper motion in the longitude-like angle, multiplied by cos(latitude). mutheta - Value of the proper motion in the latitude-like angle. """ # transformation ICRS to GAL ctICRS2GAL = CoordinateTransformation(Transformations.ICRS2GAL) # ell, bee = ctICRS2GAL.transformSkyCoordinates(phi, theta) muellstar, mubee = ctICRS2GAL.transformProperMotions( phi, theta, muphistar, mutheta) return ell, bee, muellstar, mubee
def GAL_to_ICRS(phi, theta, muphistar, mutheta): """ phi - The longitude-like angle of the position of the source (radians). theta - The latitude-like angle of the position of the source (radians). muphistar - Value of the proper motion in the longitude-like angle, multiplied by cos(latitude). mutheta - Value of the proper motion in the latitude-like angle. """ # transformation ICRS to GAL ctGAL2ICRS = CoordinateTransformation(Transformations.GAL2ICRS) # ra, dec = ctGAL2ICRS.transformSkyCoordinates(phi, theta) murastar, mudec = ctGAL2ICRS.transformProperMotions( phi, theta, muphistar, mutheta) return ra, dec, murastar, mudec
def test_icrsToGalactic(self): """ Verify correctness of transformations from the ICRS to Galactic coordinate systems. """ ct=CoordinateTransformation(Transformations.ICRS2GAL) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatIcrsToGal[0,:], decimal=2) assert_array_almost_equal(y, self.expectedMatIcrsToGal[1,:], decimal=2) assert_array_almost_equal(z, self.expectedMatIcrsToGal[2,:], decimal=2) r, expectedGalon, expectedGalat = cartesianToSpherical(self.expectedMatIcrsToGal[0,:], self.expectedMatIcrsToGal[1,:], self.expectedMatIcrsToGal[2,:]) galon, galat = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedGalon, galon, decimal=1) assert_array_almost_equal(expectedGalat, galat, decimal=1)
def test_icrsToGalactic(self): """ Verify correctness of transformations from the ICRS to Galactic coordinate systems. """ ct=CoordinateTransformation(Transformations.ICRS2GAL) x, y, z = ct.transformCartesianCoordinates(self.basisVectorXValues, self.basisVectorYValues, self.basisVectorZValues) assert_array_almost_equal(x, self.expectedMatIcrsToGal[0,:], decimal=2) assert_array_almost_equal(y, self.expectedMatIcrsToGal[1,:], decimal=2) assert_array_almost_equal(z, self.expectedMatIcrsToGal[2,:], decimal=2) r, expectedGalon, expectedGalat = cartesianToSpherical(self.expectedMatIcrsToGal[0,:], self.expectedMatIcrsToGal[1,:], self.expectedMatIcrsToGal[2,:]) galon, galat = ct.transformSkyCoordinates(array([0.0,pi/2.0,0.0]), array([0.0, 0.0, pi/2.0])) assert_array_almost_equal(expectedGalon, galon, decimal=1) assert_array_almost_equal(expectedGalat, galat, decimal=1)
def plotCoordinateTransformationOnSky(transformation, outfile=None, myProjection='hammer', noTitle=False, noLabels=False, returnPlotObject=False): """ Produce a sky-plot in a given coordinate system with the meridians and paralles for another coordinate system overlayed. The coordinate systems are specified through the pygaia.coordinates.Transformations enum. For example for Transformations.GAL2ECL the sky plot will be in Ecliptic coordinates with the Galactic coordinate grid overlayed. Keywords -------- transformation - The coordinate transformation for which to make the plot (e.g., Transformations.GAL2ECL) outfile - Save plot to this output file (default is to plot on screen). Make sure an extension (.pdf, .png, etc) is included. myProjection - Use this map projection (default is 'hammer', see basemap documentation) noTitle - If true do not include the plot title. noLabels - If true do not include plot labels. returnPlotObject - If true return the matplotlib object used for plotting. Further plot elements can then be added. """ ct = CoordinateTransformation(transformation) parallels = arange(-80.0, 90.0, 10.0) meridians = arange(0.0, 375.0, 15.0) meridianMax = degreesToRadians(85.0) parallelsMax = degreesToRadians(179.0) fig = plt.figure(figsize=(12, 6)) basemapInstance = Basemap(projection=myProjection, lon_0=0, celestial=True) basemapInstance.drawmapboundary() for thetaDeg in parallels: phi = linspace(-pi, pi, 1001) theta = zeros_like(phi) + degreesToRadians(thetaDeg) phirot, thetarot = ct.transformSkyCoordinates(phi, theta) x, y = basemapInstance(radiansToDegrees(phirot), radiansToDegrees(thetarot)) indices = (phirot >= 0.0) xplot = x[indices] yplot = y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) plt.plot(xplot, yplot, 'b-') indices = (phirot < 0.0) xplot = x[indices] yplot = y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) plt.plot(xplot, yplot, 'b-') for phiDeg in meridians: theta = linspace(-meridianMax, meridianMax, 1001) phi = zeros_like(theta) + degreesToRadians(phiDeg) phirot, thetarot = ct.transformSkyCoordinates(phi, theta) x, y = basemapInstance(radiansToDegrees(phirot), radiansToDegrees(thetarot)) indices = (phirot >= 0.0) xplot = x[indices] yplot = y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) plt.plot(xplot, yplot, 'b-') indices = (phirot < 0.0) xplot = x[indices] yplot = y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) plt.plot(xplot, yplot, 'b-') if (not noTitle): plt.title("Sky projection in " + ct.transformationStrings[1] + " coordinates with the corresponding " + ct.transformationStrings[0] + " grid overlayed") if (not noLabels): for theta in arange(-60, 90, 30): phirot, thetarot = ct.transformSkyCoordinates( 0.0, degreesToRadians(theta)) x, y = basemapInstance(radiansToDegrees(phirot), radiansToDegrees(thetarot)) plt.text(x, y, "${0}$".format(theta), fontsize=16, va='bottom', ha='center', color='r') for phi in arange(-180, 0, 30): phirot, thetarot = ct.transformSkyCoordinates( degreesToRadians(phi), 0.0) x, y = basemapInstance(radiansToDegrees(phirot), radiansToDegrees(thetarot)) plt.text(x, y, "${0}$".format(phi), fontsize=16, va='bottom', ha='center', color='r') for phi in arange(30, 180, 30): phirot, thetarot = ct.transformSkyCoordinates( degreesToRadians(phi), 0.0) x, y = basemapInstance(radiansToDegrees(phirot), radiansToDegrees(thetarot)) plt.text(x, y, "${0}$".format(phi), fontsize=16, va='bottom', ha='center', color='r') if (outfile != None): plt.savefig(outfile) elif (returnPlotObject): return plt.gca(), basemapInstance else: plt.show()
def plotCoordinateTransformationOnSky(transformation, outfile=None, noTitle=False, noLabels=False, returnPlotObject=False, lc=plt.cm.tab10.colors[0], tc=plt.cm.tab10.colors[1], lonpos=True): """ Produce a sky-plot in a given coordinate system with the meridians and paralles for another coordinate system overlayed. The coordinate systems are specified through the pygaia.coordinates.Transformations enum. For example for Transformations.GAL2ECL the sky plot will be in Ecliptic coordinates with the Galactic coordinate grid overlayed. Keywords -------- transformation - The coordinate transformation for which to make the plot (e.g., Transformations.GAL2ECL). outfile - Save plot to this output file (default is to plot on screen). Make sure an extension (.pdf, .png, etc) is included. noTitle - If true do not include the plot title. noLabels - If true do not include plot labels. returnPlotObject - If true return the matplotlib object used for plotting. Further plot elements can then be added. lc - Colour for gridlines. tc - Colour for text labels. lonpos - If true use longitude labels between 0 and 360 degrees. """ ct = CoordinateTransformation(transformation) parallels=arange(-80.0,90.0,10.0) meridians=arange(0.0,375.0,15.0) meridianMax=degreesToRadians(85.0) parallelsMax=degreesToRadians(179.0) defaultProj = ccrs.PlateCarree() addtolabel = 0 if lonpos: addtolabel = 360 fig=plt.figure(figsize=(12,6)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.Mollweide()) ax.invert_xaxis() for thetaDeg in parallels: phi=linspace(-pi,pi,1001) theta=zeros_like(phi)+degreesToRadians(thetaDeg) phirot, thetarot = ct.transformSkyCoordinates(phi, theta) phirot[(phirot>pi)] = phirot[(phirot>pi)]-2*pi x ,y = radiansToDegrees(phirot), radiansToDegrees(thetarot) indices=(phirot>=0.0) xplot=x[indices] yplot=y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) ax.plot(xplot, yplot, '-', color=lc, transform=defaultProj) indices=(phirot<0.0) xplot=x[indices] yplot=y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) ax.plot(xplot, yplot, '-', color=lc, transform=defaultProj) for phiDeg in meridians: theta=linspace(-meridianMax,meridianMax,1001) phi=zeros_like(theta)+degreesToRadians(phiDeg) phirot, thetarot = ct.transformSkyCoordinates(phi, theta) phirot[(phirot>pi)] = phirot[(phirot>pi)]-2*pi x ,y = radiansToDegrees(phirot), radiansToDegrees(thetarot) indices=(phirot>=0.0) xplot=x[indices] yplot=y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) ax.plot(xplot, yplot, '-', color=lc, transform=defaultProj) indices=(phirot<0.0) xplot=x[indices] yplot=y[indices] if any(indices): xplot, yplot = _orderGridlinePoints(xplot, yplot) ax.plot(xplot, yplot, '-', color=lc, transform=defaultProj) if (not noTitle): plt.title("Sky projection in " + ct.transformationStrings[1] + " coordinates with the corresponding " + ct.transformationStrings[0] + " grid overlayed") if (not noLabels): for theta in arange(-60,90,30): phirot, thetarot=ct.transformSkyCoordinates(0.0,degreesToRadians(theta)) x, y = (radiansToDegrees(phirot), radiansToDegrees(thetarot)) ax.text(x, y, "${0}$".format(theta), fontsize=16, va='bottom', ha='center', color=tc, transform=defaultProj) for phi in arange(-150,0,30): phirot, thetarot=ct.transformSkyCoordinates(degreesToRadians(phi), 0.0) x, y = (radiansToDegrees(phirot), radiansToDegrees(thetarot)) ax.text(x, y, "${0}$".format(phi+addtolabel), fontsize=16, va='bottom', ha='center', color=tc, transform=defaultProj) for phi in arange(30,210,30): phirot, thetarot=ct.transformSkyCoordinates(degreesToRadians(phi), 0.0) x, y = (radiansToDegrees(phirot), radiansToDegrees(thetarot)) ax.text(x, y, "${0}$".format(phi), fontsize=16, va='bottom', ha='center', color=tc, transform=defaultProj) if (outfile != None): plt.savefig(outfile) elif (returnPlotObject): return plt.gca() else: plt.show()