H /= count
linwcs = lambda x, y, n: ((x - y) / n, (x + y) / 2)
cdeltaX, crvalX = linwcs(np.amin(glon), np.amax(glon), len(glon))
cdeltaY, crvalY = linwcs(np.amin(glat), np.amax(glat), len(glat))
w = wcs.WCS(naxis=2)
w.wcs.crpix = [len(glon) / 2, len(glat) / 2]
w.wcs.crval = [crvalX, crvalY]
w.wcs.cdelt = numpy.array([cdeltX, cdeltY])
w.wcs.ctype = ['GLON-TAN', 'GLAT-TAN']
header = w.to_header()
hdu = pyfits.PrimaryHDU(H, header=header)
hdu.writeto(filename)
Assign WCS coordinates to a FITS image