while循环用于处理FITS文件(python)(while loops for processing FITS files(python))

编程入门 行业动态 更新时间:2024-10-27 06:33:32
while循环用于处理FITS文件(python)(while loops for processing FITS files(python)) python

我是python编程的新手,我正在尝试使用python中的一段代码创建处理数千个文件的设置。 我创建了一个练习文件夹来执行此操作。它有两个FITS文件(FITS1.fits和FITS2.fits)。 我做了以下操作,将它们放在.txt文件中:

ls > practice.txt

这是我接下来做的:

$ python import numpy import pyfits import matplotlib.pyplot as plt from matplotlib import pylab from pylab import * import asciidata a = asciidata.open('practice.txt') print a[0][0] #To test to see if practice.txt really contains my FITS files FITS1.fits i = 0 while i <=1 #Now I attempt a while loop to read data from columns in FITS files, plot the numbers desired, save and show the figures. I chose i <=1 because there are only two FITS files in the text(also because of zero-indexing). b = pyfits.getdata(a[0][i]) # "i" will be the index used to use a different file when the while loop gets to the end time = b['TIME'] #'TIME' is a column in the FITS file brightness = b['SAP_FLUX'] plt.plot(time, brightness) xlabel('Time(days)') ylabel('Brightness (e-/s)') title(a[0][i]) pylab.savefig('a[0][i].png') #Here am I lost on how to get the while loop to name the saved figure something different every time. It takes the 'a[0][i].png' as a string and not as the index I am trying to make it be. pylab.show() i=i+1 # I placed this here, hoping that when the while loop gets to this point, it would start over again with a different "i" value

按两次输入后,我按预期看到第一个数字。 然后我将关闭它并看到第二个。 但是,只保存了第一个数字。 有没有人对如何改变我的循环以做我需要它有任何建议?

I am new at programming in python and am in the process of trying to create a setup of processing thousands of files with one piece of code in python. I created a practice folder to do this in. In it are two FITS files (FITS1.fits and FITS2.fits). I did the following to put them both in a .txt file:

ls > practice.txt

Here is what I did next:

$ python import numpy import pyfits import matplotlib.pyplot as plt from matplotlib import pylab from pylab import * import asciidata a = asciidata.open('practice.txt') print a[0][0] #To test to see if practice.txt really contains my FITS files FITS1.fits i = 0 while i <=1 #Now I attempt a while loop to read data from columns in FITS files, plot the numbers desired, save and show the figures. I chose i <=1 because there are only two FITS files in the text(also because of zero-indexing). b = pyfits.getdata(a[0][i]) # "i" will be the index used to use a different file when the while loop gets to the end time = b['TIME'] #'TIME' is a column in the FITS file brightness = b['SAP_FLUX'] plt.plot(time, brightness) xlabel('Time(days)') ylabel('Brightness (e-/s)') title(a[0][i]) pylab.savefig('a[0][i].png') #Here am I lost on how to get the while loop to name the saved figure something different every time. It takes the 'a[0][i].png' as a string and not as the index I am trying to make it be. pylab.show() i=i+1 # I placed this here, hoping that when the while loop gets to this point, it would start over again with a different "i" value

After pressing enter twice, I see the first figure as expected. Then I will close it and see the second. However, only the first figure is saved. Does anyone have any suggestions on how I can change my loop to do what I need it to?

最满意答案

在你的代码中,i被视为字母i,而不是变量。 如果你想保留这个命名,你可以这样做:

FileName = 'a[0][%s].png' % i pylab.savefig(FileName)

In your code the i is being treated as the letter i, not the variable. If you wanted to keep this naming you could do something like:

FileName = 'a[0][%s].png' % i pylab.savefig(FileName)

更多推荐

本文发布于:2023-07-05 15:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1039118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   python   FITS   files   processing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!