【问题标题】:Issue trying to convert txt file to fits file尝试将 txt 文件转换为适合文件的问题
【发布时间】:2021-06-30 17:53:51
【问题描述】:

我是新来的,这几天我一直在为此苦苦挣扎。所以我有一个带有 cds 数据表的 txt 文件,我需要将其转换为 fit 文件,以便在特定程序中打开它。根据我阅读的 many 帮助网站以及完成此操作的代码示例,我需要的代码基本上类似于我在下面粘贴的内容,无需使用 open() 或 close () 函数(尽管我也尝试过)。我正在使用 jupyter notebooks 来运行它,到目前为止它还没有停止运行;我总是不得不打断它,我认为它卡在了 read() 行上。我还将发布我在下面遇到的错误。事件虽然它从来没有真正起作用,但它以某种方式创建并保存了一个新的 fit 文件,就像我想要的那样,但表中只有 22 行数据,但格式有点不对劲,即使文件提供了正确的 cd 信息表文件。如果您有任何建议,请告诉我。

代码

from astropy.table import Table
from astropy.io import fits
from astropy.io import ascii

data = ascii.read(r"C:\...", format='cds')
# (comment: At this line I also tried Table.read() instead of ascii.read() as suggested and "ascii.cds" for format)

data.write("new_table.fits", format='fits')

错误

OverflowError                             Traceback (most recent call last)
~\anaconda\lib\site-packages\astropy\io\ascii\core.py in _convert_vals(self, cols)
    971                         raise TypeError('converter type does not match column type')
--> 972                     col.data = converter_func(col.str_vals)
    973                     col.type = converter_type

~\anaconda\lib\site-packages\astropy\io\ascii\core.py in generic_converter(vals)
    915     def generic_converter(vals):
--> 916         return numpy.array(vals, numpy_type)
    917 

OverflowError: Python int too large to convert to C long

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-54-f59c9c65394f> in <module>
      3 from astropy.io import ascii
      4 
----> 5 data = ascii.read(r"C:\....", format='cds')
      6 # Table.read() instead of ascii.read(), the url instead of the file path, and "cds" for format
      7 

~\anaconda\lib\site-packages\astropy\io\ascii\ui.py in read(table, guess, **kwargs)
    321         else:
    322             reader = get_reader(**new_kwargs)
--> 323             dat = reader.read(table)
    324             _read_trace.append({'kwargs': copy.deepcopy(new_kwargs),
    325                                 'Reader': reader.__class__,

~\anaconda\lib\site-packages\astropy\io\ascii\cds.py in read(self, table)
    325                     return table
    326         else:
--> 327             return super().read(table)

~\anaconda\lib\site-packages\astropy\io\ascii\core.py in read(self, table)
   1228             self.meta['table'].update(self.header.table_meta)
   1229 
-> 1230         table = self.outputter(cols, self.meta)
   1231         self.cols = self.header.cols
   1232 

~\anaconda\lib\site-packages\astropy\io\ascii\core.py in __call__(self, cols, meta)
   1000         # Sets col.data to numpy array and col.type to io.ascii Type class (e.g.
   1001         # FloatType) for each col.
-> 1002         self._convert_vals(cols)
   1003 
   1004         t_cols = [numpy.ma.MaskedArray(x.data, mask=x.mask)

~\anaconda\lib\site-packages\astropy\io\ascii\core.py in _convert_vals(self, cols)
    972                     col.data = converter_func(col.str_vals)
    973                     col.type = converter_type
--> 974                 except (TypeError, ValueError) as err:
    975                     col.converters.pop(0)
    976                     last_err = err

KeyboardInterrupt: 

【问题讨论】:

  • 文件有多大?

标签: python jupyter-notebook file-conversion astropy fits


【解决方案1】:

想到的两件事。

第一,您可能想要检查文件内容中是否存在比sys.maxsize 中指示的值更长的数值。对于这个,您可以像这样轻松地为特定列设置转换器

>>> import numpy as np
>>> converters = {'col1': [ascii.convert_numpy(np.int64)]}
>>> ascii.read('file.dat', converters=converters)  

“卡住”的另一件事。您可以尝试读取 doc 中显示的块中的文件。例如:

tbls = ascii.read('large_table.csv',
                  format='csv',
                  guess=False,
                  fast_reader={'chunk_size': 100 * 1000000,
                               'chunk_generator': True})

out_tbls = []

# At this point the file is actually read in chunks.
for tbl in tbls:
    bright = tbl['Vmag'] < 8.0
    if np.count_nonzero(bright):
        out_tbls.append(tbl[bright])

【讨论】:

  • 谢谢,这很有帮助。现在我将减少数据,至少我可以让程序正常工作,但我会试试这个。我应该更清楚一点,我对此的主要关注不是溢出,而是程序只是连续运行的事实。我总是必须停止/中断内核,当我这样做时,我会得到我发布的输出(在“处理上述异常期间,发生另一个异常:”下方)。即使我修剪文件并因此没有溢出问题时也会发生这种情况。有谁知道为什么会这样?
  • 嗨,没有输入文件,我可能只能猜测发生了什么。但是,也许您可​​以尝试使用仅包含 1-2 行的输入文件进行读取?从那里开始,如果没有任何反应(例如,您不需要停止/中断内核),那么,我想您可以检查脚本的内存使用情况。它可能会变得迟缓,因为 python 试图占用一些内存空间
  • 是的,我就是这么做的;我想我可以处理溢出,但另一件事一定是一个更深层次的问题。即使表中只有大约 6 行,我在中断之前等待的时间最长可能是 10-15 分钟,结果与上面相同。问题是,如上所示,底部有一个我不明白但我认为没有错误的异常?它主要显示了我停止它时正在运行的代码的输出。文件中的数据如此之少,难道我只是没有让它运行足够长的时间吗?
  • 你指的例外是这个(except (TypeError, ValueError) as err:)吗?如果是,那么我想您现在可以忽略它。嗯,你在运行脚本时检查过内存使用情况吗?可能,查看任务管理器就足够了。原因是因为读取 ASCII 表的默认进程内存效率不高,并且可能暂时需要比文件大小更多的内存(高达 5 到 10 倍) )。在临时内存需求超过可用内存的情况下,这可能会在使用磁盘缓存时导致显着减速。
猜你喜欢
  • 2019-08-29
  • 2016-10-02
  • 2015-12-11
  • 2020-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 2017-02-09
相关资源
最近更新 更多