简体   繁体   English

通过PyFITS裁剪FITS数据立方体图像

[英]Crop FITS data Cube image through PyFITS

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. 我有一个FITS数据立方体,我需要通过PyFITS进行裁剪。 When I do it by my script, finally I'll have a 2-D FITS image! 当我通过脚本执行此操作时,最终将获得一个二维FITS图像! 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])
        )

If I understand correctly you want to slice a 3D array but keep the third dimensions (even if it's just size 1). 如果我正确理解,您想对3D数组进行切片,但要保留第三个维度(即使大小仅为1)。

This is a question about Numpy arrays. 这是关于Numpy数组的问题。 When you have an N-dimensional numpy array, passing a scalar index for one dimension returns an array of dimension N-1, sliced along the axis you indexed. 当您有N维的numpy数组时,传递一个维的标量索引将返回一个N-1维的数组,该数组沿索引的轴进行切片。 For example: 例如:

>>> arr = np.arange(27).reshape(3, 3, 3)
>>> arr
array([[[ 0,  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]]])
>>> arr[0]
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> arr[1]
array([[ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])

You can also slice along a different axis like: 您还可以沿不同的轴进行切片,例如:

>>> arr[:,0,:]
array([[ 0,  1,  2],
       [ 9, 10, 11],
       [18, 19, 20]])

If you want, for whatever reason, to return an N-dimensional array instead of an N-1 dimensional array, the easiest way is to explicitly request a slice of size 1, instead of using scalar index. 如果出于某种原因要返回N维数组而不是N-1维数组,最简单的方法是显式请求大小为1的切片,而不使用标量索引。 For example: 例如:

>>> arr[0:1]
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]])

I'll give the same advice I've given on other questions like this: this isn't really a question about PyFITS beyond the fact that your data came out of a FITS file. 对于其他类似问题,我将给出相同的建议:除了您的数据来自FITS文件之外,这实际上不是有关PyFITS的问题。 PyFITS, like most scientific Python libraries, returns data as numpy arrays. 像大多数科学的Python库一样,PyFITS返回的数据为numpy数组。 These are the main data structure used for numeric data in most scientific Python applications, so learning some basics of numpy are sort of a prerequisite, for better or worse, to doing data analysis in Python. 这些是大多数科学Python应用程序中用于数值数据的主要数据结构,因此学习numpy的一些基础是在Python中进行数据分析的先决条件(无论好坏)。 If you've ever used MATLAB, NumPy arrays are similar to arrays in MATLAB. 如果您曾经使用过MATLAB,则NumPy数组类似于MATLAB中的数组。 You can start with my short tutorial, but there are others (and probably better ones too :) github.com/embray/notebooks/blob/master/numpy.ipynb 您可以从我的简短教程开始,但是还有其他一些教程(可能还有更好的教程:) github.com/embray/notebooks/blob/master/numpy.ipynb

from astropy.io import fits

Ccube = fits.open('Cha_binned_ccube.fits', mode='update')
Ccube.info()
Ccube[0].shape
Ccube[0].data = Ccube[0].data[0:3,0:181,0:402]
Ccube[0].writeto('Cha_binned_ccube_resize.fits')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM