I have looked to find a solution for my issue, but couldn't. I have a FITS data cube, and I need to crop it by PyFITS. When I do it by my script, finally I'll have a 2-D FITS image! The first dimension is energy, and the second and thirds are longitude and latitude, respectively.
My script is as below:
#!/usr/bin/env python
import pyfits
import os
import sys
def CropFitsFile( src, dst, xs, xe, ys, ye):
fh = pyfits.open(src)
for eng in range(0,2):
img = fh[0].data[eng,ys:ye,xs:xe]
header = fh[0].header
newfh=pyfits.PrimaryHDU(data=img,header=header)
if os.path.exists(dst):
os.remove(dst)
newfh.writeto(dst)
if __name__ == "__main__":
CropFitsFile(
src=sys.argv[1],
dst=sys.argv[2],
xs=int(sys.argv[3]),
xe=int(sys.argv[4]),
ys=int(sys.argv[5]),
ye=int(sys.argv[6])
)