Giter VIP home page Giter VIP logo

Comments (5)

keatonb avatar keatonb commented on May 28, 2024 1

I took a stab at this and I'm pretty happy with the aesthetics of the result. There are probably more clever ways to do some of this. I will note that these pixels don't appear dead-on position when I compare to where the flux is distributed in the TPFs. Perhaps I've been a bit careless in the translation from RA,Dec to image location, or perhaps get_coordinates() has limited accuracy.

import numpy as np
import matplotlib.pyplot as plt
from lightkurve import KeplerTargetPixelFile
from astroquery.skyview import SkyView
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.wcs import WCS
from astropy.visualization import ImageNormalize,PercentileInterval,SqrtStretch

#What to inspect?
kic = 11090674
quarter = 4
tpf = KeplerTargetPixelFile.from_archive(kic, quarter=quarter)

#Requires some wcs projection for the axis
#I get one from the image header that I want to plot pixel locations over
c = SkyCoord(ra=tpf.ra*u.degree, dec=tpf.dec*u.degree, frame='icrs')
images = SkyView.get_images(position=c, survey=['2MASS-J'], radius=1.*u.arcmin)
wcs = WCS(images[0][0].header)

#Get median RA,Dec for each pixel
pixra = np.median(tpf.get_coordinates()[0],axis=0)
pixdec = np.median(tpf.get_coordinates()[1],axis=0)

#Convert RA,DEC to pixel coordinates in image frame
pixels = wcs.wcs_world2pix(pixra*u.degree,pixdec*u.degree,1)
#Reshape for plotting
xy = np.reshape(pixels,(2,pixels[0].size)).T
npixels = len(xy)

#Spacing between pixels is also spacing between corners of pixels
dx = np.median(np.diff(pixels)[0])
dy = np.median(np.diff(pixels)[1])

#Define locations of corners relative to pixel centers
corners = np.array([[1.,1.],[1.,-1.],[-1.,-1.],[-1.,1],[1.,1.]])
offsetmatrix = np.array(((dx,-dy), (dy, dx)))/2.
#There must be a more elegant way to do this...
for i in range(len(corners)):
    corners[i] = np.cross(offsetmatrix,corners[i])

#Which pixels have data or are in mask
# 0 = no data; 1 = data, 2 = mask
d = np.zeros(npixels,dtype=int)
d[np.isfinite(tpf.flux[0]).flatten()] += 1
d[tpf.pipeline_mask.flatten()] += 1
colors = ['None','lightblue','red']
lws = [0,1,2]
zorders = [0,1,2]

#Plot reference image
f = plt.figure(figsize=(4.2,4))
ax = plt.subplot(projection=wcs)
norm = ImageNormalize(images[0][0].data, interval=PercentileInterval(99.9),
                      stretch=SqrtStretch())
ax.imshow(images[0][0].data,origin='lower',norm=norm,cmap='gray_r')

#Plot boundaries of each pixel
for i in range(npixels):
    ccoords = xy[i]+corners
    ax.plot(ccoords[:,0],ccoords[:,1],c=colors[d[i]],lw=lws[d[i]],zorder=zorders[d[i]])

ax.set_title('KIC'+str(kic)+', Q'+str(quarter),size=14,pad=15)
ax.set_xlabel('Right Ascension',size=12)
ax.set_ylabel('Declination',size=12)
ax.set_aspect(1)
plt.tight_layout(rect=[.22,-.3,1,1.3]) #matplotlib problem
plt.show()

result:
kic11090674_q4_keplerpixels

Let me know if you'd like me to proceed to implement an KeplerTargetPixelFile.overlay_on_sky() function like this.

from lightkurve.

christinahedges avatar christinahedges commented on May 28, 2024

This would be nice, I was just about to add an issue saying we need to add the WCS solution for each target pixel file. (Some gotchas include note all TPFs having WCS in them). This would allow us to make the above function, right?

from lightkurve.

barentsen avatar barentsen commented on May 28, 2024

I've been dreaming about a tpf.get_wcs() function that returns an AstroPy WCS object, which is somewhat non-trivial to instantiate otherwise. Here is the code I'm talking about: https://gist.github.com/barentsen/46a4b44223ae05377702

Not sure this is exactly what we need to enable an astropy wcsaxis to overlay the image, I'd have to be reminded how that all works. What'd you think, @chedges?

from lightkurve.

barentsen avatar barentsen commented on May 28, 2024

Many thanks @keatonb! I expect it will take us a bit of time to look carefully at your code because of other activities this and next week, but this is great input!!

from lightkurve.

keatonb avatar keatonb commented on May 28, 2024

So, any suggestions on how or whether to implement this?

Has anyone quantified the accuracy of the get_coordinates() function? Is this affected by the same systematic offsets of up to 10 pixels described in KeplerGO/K2fov#15? Unless we could provide ~sub-pixel accuracy with get_coordinates(), I would be hesitant to provide this overlay functionality.

from lightkurve.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.