我打开了一个.fits图片:
scaled_flat1 = pyfits.open('scaled_flat1.fit')
scaled_flat1a = scaled_flat1[0].data
Run Code Online (Sandbox Code Playgroud)
当我打印它的形状时:
print scaled_flat1a.shape
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
(1, 1, 510, 765)
Run Code Online (Sandbox Code Playgroud)
我希望它阅读:
(510,765)
Run Code Online (Sandbox Code Playgroud)
我怎么摆脱之前的两个呢?
我看了看并找到了解决这个问题的方法,但我什么都没有.
我正在通过matplotlib生成矩形FITS图像,然后使用AstroPy(或PyFITS)将WCS坐标应用于它们.我的图片中银河系的经度和纬度,所以标题关键字适合我的地图应该是GLON-CAR和GLAT-CAR(笛卡尔投影).我已经看过在SAO DS9中使用相同地图投影的其他地图,并且坐标工作得很好...... 网格应该是完全正交的.可在此处找到FITS标准预测.
但是当我生成我的地图时,坐标根本不是笛卡儿.这是我的地图(左)和大致相同区域(右)的另一个参考地图的并排比较.两者都列出GLON-CAR,并GLAT-CAR在FITS头,但是当在SAO DS9看着我的是扭曲(注意坐标网格是什么SAO DS9基于在FITS头,或至少某处存储在FITS文件中的数据):

这是有问题的,因为如果投影错误,坐标分配算法将为每个像素分配不正确的坐标.
有没有人遇到这个,或者知道可能是什么问题?
我已经尝试过应用其他投影(只是为了看看它们在SAO DS9中的表现如何)并且它们很好......但我的笛卡尔和墨卡托投影并没有像他们应该的那样提出正交网格.
我不敢相信这会是AstroPy中的一个错误,但是我找不到任何其他原因...除非我在标题中的参数格式不正确,但我仍然看不出那会怎样导致问题我'经历.或者你会推荐使用其他东西吗?(我已经看过matplotlib底图但是在我的计算机上工作时遇到了一些麻烦).
我的标题代码如下:
from __future__ import division
import numpy as np
from astropy.io import fits as pyfits # or use 'import pyfits, same thing'
#(lots of code in between: defining variables and simple calculations...
#probably not relevant)
header['BSCALE'] = (1.00000, 'REAL = TAPE*BSCALE + BZERO')
header['BZERO'] = (0.0)
header['BUNIT'] = ('mag ', 'UNIT OF …Run Code Online (Sandbox Code Playgroud) 我有一个名为'my_cube.fits'的FITS文件,带有WCS.该文件具有关于轴1和2(X和Y)的空间信息以及关于轴3(Z)的光谱信息.当我使用astropy.io.fits加载它时,谱轴为0,空间轴为1和2.文件加载如下:
import astropy.io.fits as pyfits
filename = 'my_cube.fits'
my_data = pyfits.getdata(filename)
my_header = pyfits.getheader(filename)
Run Code Online (Sandbox Code Playgroud)
我一直在使用matplotlib来显示数据,我想知道如何使用它的WCS显示我的数据立方体的单个光谱帧.让我们说:
from astropy.wcs import WCS
from matplotlib import pyplot as plt
my_wcs = WCS(my_header)
fig = plt.figure()
ax = fig.add_subplot(111, projection=my_wcs)
ax.imshow(my_data[5, :, :])
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我有:
...
File "/usr/local/lib/python3.4/dist-packages/matplotlib/figure.py", line 1005, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/matplotlib/axes/_subplots.py", line 73, in __init__
self._axes_class.__init__(self, fig, self.figbox, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/wcsaxes/core.py", line 49, in __init__
self.patch = self.coords.frame.patch
File "/usr/local/lib/python3.4/dist-packages/wcsaxes/frame.py", line 129, in patch
self._update_patch_path() …Run Code Online (Sandbox Code Playgroud) 我一直在疯狂搜索文档,却找不到答案。
我正在用python生成FITS图像,需要为图像分配WCS坐标。我知道有很多方法可以通过将点源与已知目录进行匹配来完成此操作,但是在这种情况下,我正在生成尘埃图,因此,据我所知,点源匹配不起作用。
因此,图像是形状的二维Numpy数组(240,240)。它的写法是这样的(x和y坐标分配有点怪异,以某种方式起作用):
H, xedges, yedges = np.histogram2d(glat, glon, bins=[ybins, xbins], weights=Av)
count, x, y = np.histogram2d(glat, glon, bins=[ybins, xbins])
H/=count
hdu = pyfits.PrimaryHDU(H)
hdu.writeto(filename)
>>> print H.shape
(240,240)
Run Code Online (Sandbox Code Playgroud)
一切都可以正常工作。对于分配银河坐标,您似乎需要做的只是:
glon_coords = np.linspace(np.amin(glon), np.amax(glon), 240)
glat_coords = np.linspace(np.amin(glat), np.amax(glat), 240)
Run Code Online (Sandbox Code Playgroud)
但是我不了解FITS图像如何存储这些坐标,所以我不知道如何编写它们。我也尝试过在SAO DS9中分配它们,但是没有运气。我只需要将这些坐标分配给图像的简单方法。
感谢您的任何帮助,您可以提供。
我编写了用于校准图像(暗框和平场)的脚本......这是代码的一部分
for n in range(len(img)):
with pyfits.open(img[n], mode='update', memmap=True) as im:
imgg = im[0].data
header = im[0].header
imgg.astype(float)
imgg = (imgg - dd) / df
imgg[np.isnan(imgg)] = 1
imgg.astype(int)
plt.imshow(imgg, cmap=plt.cm.Greys_r, vmin=0.5, vmax=1.5)
plt.show()
Run Code Online (Sandbox Code Playgroud)
深色框架和平场图像的代码进行校正...这部分当我使用的绘图vmin和vmax,我得到正确的图片,但我不知道该怎么vmin和vmax工作。我需要在图像数据 ( imgg)上应用它,因为当我保存数据时,我得到的图像没有vmin和vmax...
有什么建议?
第二个问题......我如何保存适合文件中的数据更改?当我im.close()只在一个文件上使用这项工作但不循环工作时。
谢谢
编辑
好的,这里是完整的脚本
import numpy as np
import pyfits
from matplotlib import pyplot as plt
import glob
dark=glob.glob('.../ha/dark/*.fits')
flat=glob.glob('.../ha/flat/*.fits')
img=glob.glob('.../ha/*.fits')
sumd0 = pyfits.open(dark[0])
sumdd=sumd0[0].data
sumdd.astype(float)
for …Run Code Online (Sandbox Code Playgroud) 我有一个带有标题的ascii文件中的数据表,我想将我的ascii表转换为带头的fit文件
#ID ra dec x y Umag Bmag Vmag Rmag Imag
1.0 53.146 -27.8123 3422.98 3823.58 24.4528 24.7995 23.6266 22.64 20.8437
2.0 53.1064 -27.801 3953.49 3994.62 23.3284 22.6716 22.1762 21.6189 21.2141
3.0 53.1322 -27.7829 3608.34 4269.29 21.2676 20.1937 19.6743 19.0707 18.6983
4.0 53.1017 -27.8022 4017.09 3975.24 23.6987 22.84 21.9946 21.0781 19.8616
5.0 53.118 -27.8021 3798.98 3978.42 23.3087 22.1932 21.2205 20.1842 18.6448
6.0 53.1479 -27.8239 3397.92 3648.27 25.0347 24.598 23.7259 22.9945 21.9228
7.0 53.1334 -27.7758 3592.51 4375.76 21.5159 20.4777 19.6065 18.6609 …Run Code Online (Sandbox Code Playgroud) 我有多个表创建astropy.table.Table,例如:
from astropy.table import Table
import numpy as np
#table 1
ta=Table()
ta["test1"]=np.arange(0,100.)
#table 2
tb=Table()
tb["test2"]=np.arange(0,100.)
Run Code Online (Sandbox Code Playgroud)
我可以将它们单独保存到.fits文件中
ta.write('table1.fits')
tb.write('table2.fits')
Run Code Online (Sandbox Code Playgroud)
但是我希望将它们保存到同一个.fits文件中,每个文件都有不同的文件hdu.我怎样才能做到这一点?
我有一个包含许多列的FITS文件.该文件的简化示例的一部分如下所示:
A B C
100 1 90
100 2 90
100 3 90
100 4 90
211 40 70
211 41 70
211 42 70
211 43 70
211 44 70
Run Code Online (Sandbox Code Playgroud)
如果您注意到这里,A列和C列的前四行是相同的,但B列的变化范围是1到4.然后A列和C列的下5个值相同,但B列的变化范围是40到44.
我想做的是,写一个程序,创建一个这样的文件:
A B C
100 4 90
211 5 70
Run Code Online (Sandbox Code Playgroud)
也就是说,B列应包含A列和C列相同的值的数量!
我想知道在Python中如何做到这一点.它不一定需要处理FITS文件,如果有一些例程无法在FITS文件中使用,我也可以将其转换为ASCII格式.
到目前为止我尝试了什么:
我遇到了一个被调用的例程Collections,它有一个子例程,它调用Counter列表中相等的值并返回它们.
我试过了:
import collections
counter = collections.Counter(a)
counts = counter.values()
Run Code Online (Sandbox Code Playgroud)
但这只能给我A列中相同的值.有人能告诉我如何使用这个例程将这些值与C列进行比较吗?
我对 FITS 文件执行了一个非常简单的操作(数据是 numpy 数组格式),但我无法将其保存为新文件或覆盖现有文件。
我正在重写一些使用 numpy pyfits 模块处理天文 FITS 文件的旧代码 - 我想更新它以使用 astropy.io fits 模块。具体来说,我使用的一些数据是 3D 的,有些是 4D 的。4D 的东西只是一个约定 - 第 4 轴不包含有用的信息(可以在此处找到数据示例:http : //www.mpia.de/THINGS/Data_files/NGC_628_NA_CUBE_THINGS.FITS)。所以我更喜欢删除额外的轴,然后我的其余代码可以在没有任何特殊要求的情况下继续进行。
这是我使用的基于 pyfits 的旧代码,效果很好:
import numpy
import pyfits
filename = 'NGC628.fits'
outfile = 'NGC628_reshaped.fits'
# Get the shape of the file
fitsfile=pyfits.open(filename)
image = fitsfile[0].data
header =fitsfile[0].header
z = image.shape[1] # No. channels
y = image.shape[2] # No. x pixels
x = image.shape[3] # No. y pixels
newimage = numpy.reshape(image,[z,y,x])
pyfits.core.writeto(outfile,newimage,header, clobber=True) …Run Code Online (Sandbox Code Playgroud) pyfits ×9
python ×8
astropy ×4
fits ×4
numpy ×3
matplotlib ×2
astronomy ×1
physics ×1
python-3.x ×1
scipy ×1