Repository navigation
Closed
Description
Following the logic of "Appending Tables" in the manual, I tried to add new rows to an existing fits file. Starting with S.fits:
In [1]: from astropy.io import fits as pf
In [2]: H=pf.open("S.fits",uint=True)
In [3]: H[1].data
Out[3]:
FITS_rec([ (-1, '1970-01-01T00:00:00.000000', nan, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)],
dtype=[('OBT_TIME', '>i8'), ('UTC_TIME', 'S26'), ('MJD_TIME', '>f8'), ('HK_STAT_SID', '>i2'), ('HK_STAT_MODE', '>i2'), ('FixVal13', '>i2'), ('HK_STAT_FPM_POWER', 'u1'), ('HK_STAT_DAT_BUFFER_OVERFLOW', 'u1'), ('HK_STAT_SCU_MAIN_RED', 'u1'), ('HK_STAT_LAST_SPWLINK_ERROR', '>i2'), ('HK_STAT_LAST_ERROR_ID', '>i2'), ('HK_STAT_LAST_ERROR_FREQUENCY', '>i2'), ('HK_STAT_NUM_CMD_RECEIVED', '>i2'), ('HK_STAT_NUM_CMD_EXECUTED', '>i2'), ('HK_STAT_NUM_DATA_SENT', '>i2'), ('HK_STAT_SCU_PROC_DUTY_CYCLE', '>i2'), ('HK_STAT_SCU_NUM_AHB_ERROR', '>i2'), ('HK_STAT_SCU_NUM_AHB_CORRECTABLE_ERROR', '>i2'), ('HK_TEMP_SEM_SCU', '>i4'), ('HK_TEMP_SEM_PCU', '>i4')])
In [4]: Z=pf.BinTableHDU.from_columns(H[1].columns, nrows=10)
In [5]: for c in Z.data.names: Z.data[c][1:]=Z.data[c][0]
In [6]: Z.data
Out[6]:
FITS_rec([ (-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0)],
dtype=[('OBT_TIME', '<i8'), ('UTC_TIME', 'S26'), ('MJD_TIME', '<f8'), ('HK_STAT_SID', '<i2'), ('HK_STAT_MODE', '<i2'), ('FixVal13', '<i2'), ('HK_STAT_FPM_POWER', 'u1'), ('HK_STAT_DAT_BUFFER_OVERFLOW', 'u1'), ('HK_STAT_SCU_MAIN_RED', 'u1'), ('HK_STAT_LAST_SPWLINK_ERROR', '<i2'), ('HK_STAT_LAST_ERROR_ID', '<i2'), ('HK_STAT_LAST_ERROR_FREQUENCY', '<i2'), ('HK_STAT_NUM_CMD_RECEIVED', '<i2'), ('HK_STAT_NUM_CMD_EXECUTED', '<i2'), ('HK_STAT_NUM_DATA_SENT', '<i2'), ('HK_STAT_SCU_PROC_DUTY_CYCLE', '<i2'), ('HK_STAT_SCU_NUM_AHB_ERROR', '<i2'), ('HK_STAT_SCU_NUM_AHB_CORRECTABLE_ERROR', '<i2'), ('HK_TEMP_SEM_SCU', '<i4'), ('HK_TEMP_SEM_PCU', '<i4')])
In [7]: H[1]=Z
In [8]: H.writeto("S2.fits")
In [9]: H.close()
In [10]: K=pf.open("S2.fits",uint=True)
In [11]: K[1].data
Out[11]:
FITS_rec([ (-1, '1970-01-01T00:00:00.000000', nan, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0)],
dtype=[('OBT_TIME', '>i8'), ('UTC_TIME', 'S26'), ('MJD_TIME', '>f8'), ('HK_STAT_SID', '>i2'), ('HK_STAT_MODE', '>i2'), ('FixVal13', '>i2'), ('HK_STAT_FPM_POWER', 'u1'), ('HK_STAT_DAT_BUFFER_OVERFLOW', 'u1'), ('HK_STAT_SCU_MAIN_RED', 'u1'), ('HK_STAT_LAST_SPWLINK_ERROR', '>i2'), ('HK_STAT_LAST_ERROR_ID', '>i2'), ('HK_STAT_LAST_ERROR_FREQUENCY', '>i2'), ('HK_STAT_NUM_CMD_RECEIVED', '>i2'), ('HK_STAT_NUM_CMD_EXECUTED', '>i2'), ('HK_STAT_NUM_DATA_SENT', '>i2'), ('HK_STAT_SCU_PROC_DUTY_CYCLE', '>i2'), ('HK_STAT_SCU_NUM_AHB_ERROR', '>i2'), ('HK_STAT_SCU_NUM_AHB_CORRECTABLE_ERROR', '>i2'), ('HK_TEMP_SEM_SCU', '>i4'), ('HK_TEMP_SEM_PCU', '>i4')])
The produced file is available here: S2.fits.
Or similarly:
In [17]: Z.writeto("S3.fits")
In [18]: K=pf.open("S3.fits",uint=True)
In [19]: K[1].data
Out[19]:
FITS_rec([ (-1, '1970-01-01T00:00:00.000000', nan, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0),
(-1, '1970-01-01T00:00:00.000000', nan, 0, 32768, 32768, -128.0, -128.0, -128.0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 0, 0)],
dtype=[('OBT_TIME', '>i8'), ('UTC_TIME', 'S26'), ('MJD_TIME', '>f8'), ('HK_STAT_SID', '>i2'), ('HK_STAT_MODE', '>i2'), ('FixVal13', '>i2'), ('HK_STAT_FPM_POWER', 'u1'), ('HK_STAT_DAT_BUFFER_OVERFLOW', 'u1'), ('HK_STAT_SCU_MAIN_RED', 'u1'), ('HK_STAT_LAST_SPWLINK_ERROR', '>i2'), ('HK_STAT_LAST_ERROR_ID', '>i2'), ('HK_STAT_LAST_ERROR_FREQUENCY', '>i2'), ('HK_STAT_NUM_CMD_RECEIVED', '>i2'), ('HK_STAT_NUM_CMD_EXECUTED', '>i2'), ('HK_STAT_NUM_DATA_SENT', '>i2'), ('HK_STAT_SCU_PROC_DUTY_CYCLE', '>i2'), ('HK_STAT_SCU_NUM_AHB_ERROR', '>i2'), ('HK_STAT_SCU_NUM_AHB_CORRECTABLE_ERROR', '>i2'), ('HK_TEMP_SEM_SCU', '>i4'), ('HK_TEMP_SEM_PCU', '>i4')])
In [14]: astropy.__version__
Out[14]: u'1.1.dev12815'
Activity
embray commentedon Jul 7, 2015
Thanks for the detailed report--this definitely looks like a bug. Probably somewhere in the
from_columnsstep.Though this is one of those cases where I would again strongly, strongly urge anyone to use the Astropy
Tableinterface where possible to do any kind of table manipulations, rather than trying to work with Numpy arrays like tables. For example this works:(Incidentally, @taldcroft do you know if there is an open issue to add a
Table.add_rowsmethod or something of the like? Being able to add multiple new rows to a table at once would seem more efficient than callingadd_rowa bunch of times in a loop...)taldcroft commentedon Jul 7, 2015
@embray - I think this came up once before. Right you can do:
This is the expected idiom for adding chunks of rows at a time.
The issue with having an
add_rowsis that multiple rows effectively constitutes a new table. Trying to extend the existingadd_rowmethod could (in theory) be done by allowing the currentvalsandmaskargs to be lists, but I worry this could get messy in the code and the API. (I might be wrong, I haven't thought about it in detail).taldcroft commentedon Jul 7, 2015
@embray - I think the FITS documentation should include your recommendation, in a big Note box right at the top in the Introduction, to use
Table.read / writefor most simple "I want to read / write a single-HDU bin table" cases. I know users who never realized this because they just go to the docs with the intent "I want to read a FITS file" and end up in the io.fits package docs.embray commentedon Jul 7, 2015
@taldcroft I agree--it's very much an FAQ, and should be in the FAQ along with a noticeable link in the docs. I should be getting some time soon to deal with the FITS table stuff once and for all, I think, too.
embray commentedon Jul 7, 2015
Thanks also for the vstack idiom--that seems fine. It does seem to me that could be packaged into method that returns a new table (though I also don't see how adding one row is any different from adding multiple rows when it comes to creating a new table, if you want I'll take a look at what's involved)
embray commentedon Mar 18, 2016
I'm not 100% sure but I think the bug reported here is the same: #3921
Ultimately it has to do with unsigned integer data not being updated correctly when writing out a FITS table.
saimn commentedon Jan 24, 2018
Reopening as #6956 does not fix this one.
cmarmo commentedon Dec 14, 2022
Hello, the files used to investigate the issue are no longer available.
I have reproduced the script above as follows
Running this script gives
Therefore, I'm under the impression that the bug has been fixed at some point.
cmarmo commentedon Dec 14, 2022
This is with
saimn commentedon Dec 16, 2022
Thanks for checking this @cmarmo, closing!