plot fits frames in sky coordinates base on their wcs.
The snippet can be accessed without any authentication.
Authored by
michitaro
Edited
overlook.py 4.18 KiB
# usage:
# python overlook.py --index 1 -o overlook.png output/01005/HSC-G/corr/CORR-0010198-* && firefox overlook.png
import argparse
import logging
logging.basicConfig(level=logging.INFO)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='+')
parser.add_argument('--index', '-i', type=int, default=1)
parser.add_argument('--out', '-o')
parser.add_argument('--fix', action='store_true')
args = parser.parse_args()
ccds = []
for f in args.files:
logging.info('loading %(f)s...' % locals())
with pyfits.open(f, memmap=True) as hdul:
header = hdul[args.index].header
if args.fix:
header = fixCdMatrix.makeCdMatrixFromPa(header)
ccd = Ccd(header, f)
ccd.drawFrame()
ccd.drawCcdName()
ccds.append(ccd)
xlim, ylim = Ccd.container_box(ccds)
pyplot.gca().set_xlim(*xlim)
pyplot.gca().set_ylim(*ylim)
if args.out:
logging.info('writing...')
pyplot.savefig(args.out)
import sys
import pyfits
import pywcs
import numpy
import os.path
import re
import collections
from matplotlib import pyplot
from matplotlib.patches import Patch, Path, PathPatch
def parseFilename(fname):
import os.path, re
m = re.match('.*-(\d+)-(\d+)\.fits$', os.path.basename(fname))
if m:
visit, ccd = map(int, m.groups((1, 2)))
else:
visit = 0
ccd = 0
return {'visit': visit, 'ccd': ccd}
class Ccd:
def __init__(self, header, fileName=None):
self.fileName = fileName
self.header = header
try:
if self.header['CUNIT1'] == 'degree': self.header['CUNIT1'] = 'deg'
if self.header['CUNIT2'] == 'degree': self.header['CUNIT2'] = 'deg'
except:
pass
self.wcs = pywcs.WCS(self.header)
def drawFrame(self):
ax = pyplot.gca()
nx = self.header['NAXIS1'] - 1
ny = self.header['NAXIS2'] - 1
div = 32
r = numpy.arange(div, dtype=float) / div
outline = \
[(t * nx, 0.) for t in r] + \
[(nx, t * ny) for t in r] + \
[((1. - t) * nx, ny) for t in r] + \
[(0., (1. - t) * ny) for t in r] + \
[(0., 0.)]
codes = [Path.MOVETO] + (4*div - 1) * [Path.LINETO] + [Path.CLOSEPOLY]
verts = self.wcs.all_pix2sky(outline, 0)
patch = PathPatch(
Path(verts, codes),
edgecolor=self.color(),
facecolor='none',
picker=True,
)
patch.ccd = self
ax.add_patch(patch)
def drawCcdName(self):
ax = pyplot.gca()
# ccdName = '%s\n%s' % (self.header['T_BEEID'], self.header['T_SDOID'])
# ccdName = self.header['T_SDOID']
ccdName = os.path.basename(self.fileName).split('-')[-1].split('.')[0]
x, y = self.wcs.all_pix2sky([(self.header['NAXIS1'] / 2, self.header['NAXIS2'] / 2)], 0)[0]
ax.text(x, y, ccdName)
def drawCrval(self):
ax = pyplot.gca()
ax.plot([self.wcs.wcs.crval[0]], [self.wcs.wcs.crval[1]], 'bh', markersize=10)
def color(self):
return '#007777'
return {
0: '#0000ff',
1: '#ff0000',
}[int(self.header['T_BEEID'])]
@classmethod
def container_box(cls, frames):
min_ra = 360.
max_ra = 0.
min_dec = 90.
max_dec = -90.
margin = 0.05
for frame in frames:
corners = [
[1, 1],
[frame.header['NAXIS1'], 1],
[frame.header['NAXIS1'], frame.header['NAXIS2']],
[1, frame.header['NAXIS2']],
[1, 1],
]
for ra, dec in frame.wcs.all_pix2sky(corners, 1):
if ra < min_ra: min_ra = ra
if ra > max_ra: max_ra = ra
if dec < min_dec: min_dec = dec
if dec > max_dec: max_dec = dec
return ((max_ra + margin, min_ra - margin), (min_dec - margin, max_dec + margin))
if __name__ == '__main__':
main()
Please register or sign in to comment