'int'对象没有属性'deg'

时间:2017-12-26 00:33:04

标签: python-3.x csv

在我的程序中,我使用的文件csv的值如下:

0.3987,-38.0349 1.7577,-66.5549 3.0406,-62.0202 3.3333,-26.5936 ... ...

from astroquery.skyview import SkyView
from astropy.coordinates import ICRS, Galactic, FK4, FK5, SkyCoord
from astropy.table import Table
from matplotlib import pyplot

import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import pandas as pd
import csv
import io, os
import astropy.units as u
import pyfits
import numpy as np
import matplotlib

catalogue = pd.read_csv('cat7.csv', delimiter=(','))
catalogue.columns = ['ra', 'dec']
df1 = catalogue[['ra','dec']]
ra = []
dec = []

for r in df1['ra']:
    ra.append(r)
for d in df1['dec']:
    dec.append(d)

os.mkdir('images')
path = os.getcwd()
tree = path+'/images/'

for i in range(1, len(df1)):    
    img = SkyView.get_images(position=SkyCoord(ra[i], dec[i], frame='icrs', unit=(u.deg, u.deg), obstime='J2000'), survey=['DSS2 Blue', '2MASS-J', '2MASS-K', 'WISE 3.4', 'WISE 22', 'GALEX Far UV'],pixels='150,150',coordinates='J2000',grid=False, gridlabels=False)

    for h in img:

        x = plt.figure(figsize=(18, 14))
        ax1 = plt.subplot2grid((3, 3), (0, 0))
        ax1.imshow(img[0][0].data, cmap='gray')
        ax1.set_xlabel('DSS2 Blue')
        ax1.set_xticks([])
        ax1.set_yticks([])

        ax2 = plt.subplot2grid((3, 3), (0, 1))
        ax2.imshow(img[1][0].data, cmap='gray')
        ax2.set_xlabel('2MASS-J')
        ax2.set_xticks([])
        ax2.set_yticks([])

        ax3 = plt.subplot2grid((3, 3), (0, 2))
        ax3.imshow(img[2][0].data, cmap='gray')
        ax3.set_xlabel('2MASS-K')
        ax3.set_xticks([])
        ax3.set_yticks([])

        ax4 = plt.subplot2grid((3, 3), (1, 0))
        ax4.imshow(img[3][0].data, cmap='gray')
        ax4.set_xlabel('WISE 3.4')
        ax4.set_xticks([])
        ax4.set_yticks([])

        ax5 = plt.subplot2grid((3, 3), (1, 1))
        ax5.imshow(img[4][0].data, cmap='gray')
        ax5.set_xlabel('WISE 22')
        ax5.set_xticks([])
        ax5.set_yticks([])

        ax6 = plt.subplot2grid((3, 3), (1, 2))
        ax6.imshow(img[5][0].data, cmap='gray')
        ax6.set_xlabel('GALEX Far UV')
        ax6.set_xticks([])
        ax6.set_yticks([])

        try:

            for filename in os.listdir('galaxies'):
                if filename.endswith('.fits'):
                    try:
                        fit = pyfits.open('galaxies/' + filename)
                        flux = fit[0].data
                        flux = np.array(flux, dtype=np.float64)
                        header = fit[0].header
                        start_wave = header['CRVAL1']
                        step = header['CDELT1']
                        w0, dw, n = start_wave, step, len(flux)
                        w = start_wave + step * n
                        wave = np.linspace(w0, w, n, endpoint=False)
                        data = Table([wave, flux], names=(str(header['CRVAL1']), str(step)))
                        until = 5660 - start_wave + 901
                        until = until/step

                        u = int(until)
                        j = (flux[0:u])
                        media1 = sum(flux[0:u])/len(j)
                        media1 = media1+100
                        inc = u + 5
                        k = (flux[u:inc])
                        media_h = sum(flux[u:inc])/len(k)

                        hdu_name = fit
                        name  = hdu_name[0].header['TARGET']
                        ra_fit = hdu_name[0].header['OBSRA']
                        dec_fit = hdu_name[0].header['OBSDEC']
                        m = max(k)+100

                        if(int(ra_fit) == int(ra[i]) and int(dec_fit) == int(dec[i])):                                                             
                            ax7 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
                            ax7.plot(wave, flux, label = name, color='black')
                            ax7.annotate('hα',xy=(6562,m),xytext=(6545,m))
                            plt.ylabel('Counts')
                            plt.xlabel('Wavelength (Å)')
                            x.savefig(tree+name, format='png')
                            plt.close(x)


                    except:
                        continue
        except:
            continue

我需要将这些数字与一些文件.fits进行比较。它的作品,但它只工作一次!循环“for”的第二次显示错误:'int'对象没有属性'deg'。如果在“img = Sky ...”之前放置“尝试”,它会跳转此功能!与其他数字相比如何?

2 个答案:

答案 0 :(得分:1)

你的第21个字母字母让你有点不知所措:

import astropy.units as u
...
... , unit=(u.deg, u.deg), ...
...
    u = int(until)

将其更改为import astropy.units as units,您会更开心。或者至少,您不会通过为其分配整数值来废弃该符号。

答案 1 :(得分:0)

正如J_H所说,有必要将导入astropy.units中的值“u”改为“units”作为单位和... unit =(units.deg,u.deg),....之后代码工作得很好。感谢!!!