-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhp2cart.py
executable file
·66 lines (54 loc) · 1.94 KB
/
hp2cart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
# Reproject a healpix map into a Cartesian fits file
# NHW 17/01/16
import sys
import healpy as hp
import matplotlib as ml
ml.use('Agg') # So does not use display
import matplotlib.image as mpimg
import matplotlib.pyplot as plot
import os
import random
import numpy as np
from astropy.table import Table, Column
from astropy.io import fits
from optparse import OptionParser
usage="Usage: %prog [options] <file>\n"
parser = OptionParser(usage=usage)
parser.add_option('-i','--input',dest="input",default=None,
help="Healpix image <FILE>",metavar="FILE")
parser.add_option('-x','--xpix',dest="xpix",default=2000,
help="Number of pixels wide to make the image",type="float")
parser.add_option('-o','--output',dest="output",default="test.fits",
help="Output file <FILE>; default = test.fits",metavar="FILE")
(options, args) = parser.parse_args()
if not options.input:
print "Must specify an input file."
sys.exit(1)
# Number of pixels L-R, covering 360 deg of RA
xpix=options.xpix
# Resolution in deg
cdelt=360.0/xpix
data=hp.read_map(options.input)
#data=hp.read_map("red_map_hp.fits")
# Dummy figure because cartview always activates matplotlib
dummy_figure=plot.figure(1,figsize=(4,3))
axd = dummy_figure.add_subplot(111)
temp_arr=hp.cartview(data, fig=1, xsize=xpix, cbar=False, title="", coord=["C"], notext=True, return_projected_map=True)
# Unmask and flip
temp_arr=temp_arr.filled(np.nan)
hdulist=fits.open(options.input)
header=hdulist[0].header
# CRVAL2 needs to be zero in order for a plate caree (CAR) projection to resemble a Cartesian grid
header['CRVAL2']=0.0
header['CRPIX2']=xpix/4
header['CDELT2'] = cdelt
header['CTYPE2']='DEC--CAR'
# CRVAL1 should be zero, in the middle of the image
header['CRVAL1']=0.0
header['CRPIX1']=xpix/2
header['CDELT1'] = -cdelt
header['CTYPE1']='RA---CAR'
hdulist[0].data = temp_arr
hdulist[0].header = header
hdulist.writeto(options.output, clobber=True)