New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading and saving fits file multiply file size by 2 #16325
Comments
Welcome to Astropy 👋 and thank you for your first issue! A project member will respond to you as soon as possible; in the meantime, please double-check the guidelines for submitting issues and make sure you've provided the requested details. GitHub issues in the Astropy repository are used to track bug reports and feature requests; If your issue poses a question about how to use Astropy, please instead raise your question in the Astropy Discourse user forum and close this issue. If you feel that this issue has not been responded to in a timely manner, please send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail feedback@astropy.org. |
Thank you for reporting this. Without knowing the details too well, any chance you might be reading single precision data and that it's being re-exported as double precision ? |
I was wondering that as well, but in the case where I am writing the HDUL directly after reading, I couldn't be sure if astropy was converting precision somehow when reading? |
Hi @lelouedec , I've downloaded the file you linked above 20240120_004831_s4h1A.fts and executed your commands with the last development version of astropy: I was unable to reproduce your issue. Do you mind checking on your side with a recent version of astropy? Thanks! |
Hey so I retried opening and then saving the HDUL and it is saved with same size. But if I do the following :
it becomes twice the size still. Weird thing is : filea.writeto saves it as 4MB, while filea[0].filebytes() returns 8MB
there must be something to do with references , first test.fts is 4.2MB, and last one saved is 8MB |
Your fits file contains an ImageHDU as Primary header and a BinTableHDU as first extension. When your read and save the file nothing changes (at least with recent version of astropy... :) ) When you extract the image extension it is saved with
as @neutrinoceros said. I guess there is a way to tell astropy the type we want to write back? ... sorry I cannot find it right now.... |
Ah I see! there was indeed some kind of conversion under the hood when taking data and header and putting them back in a ImageHDU/directly doing fits.writeto(data,header). I am also trying to see if it is possible to do it immediately with astropy. I have a few thousands file I already saved with wrong size, and it would be great to just reopen them and save them correctly ^^ |
Your data is stored as int32 but with BSCALE=1.0 / BZERO=0.0 which causes a conversion to float64 when you access
So when you access To avoid the conversion:
|
Ah, I see! Thanks a lot for the help. I am going to write a small script that converts all the previously saved fit files to 32-bit precision! Which should also be float 32 right ? Thanks again |
Hi humans 👋 - this issue was labeled as Close? approximately 16 hours ago. If you think this issue should not be closed, a maintainer should remove the Close? label - otherwise, I will close this issue in 7 days. If you believe I commented on this issue incorrectly, please report this here |
Per the workflows above, it should become int32 again – float32 might result in loss of precision. hdul = fits.open('20240120_004831_s4h1A.fts', do_not_scale_image_data=True) |
I'm going to close this issue as per my previous message, but if you feel that this issue should stay open, then feel free to re-open and remove the Close? label. If this is the first time I am commenting on this issue, or if you believe I closed this issue incorrectly, please report this here |
Description
We are processing a lot of fits files for a dataset creation and noticed that the files once loaded and then saved after modification were twice the size for same data and slightly different header.
To verify the behavior I just tried the following:
and
For both the with a freshly downloaded raw file, the resulted file is 8.4 MB instead of the original 4.2MB.
Expected behavior
Saved and loaded files should be the same size.
How to Reproduce
use code above and one file like this one :
https://stereo-ssc.nascom.nasa.gov/pub/ins_data/secchi/L0/a/img/hi_1/20240120/20240120_004831_s4h1A.fts
Versions
astropy==5.3
The text was updated successfully, but these errors were encountered: