Effacer les filtres
Effacer les filtres

fitswrite for uint16 array

3 vues (au cours des 30 derniers jours)
Alan Shotter
Alan Shotter le 26 Sep 2019
Commenté : Walter Roberson le 16 Nov 2023
I have a double precision 2D array, and need to output this to a uint16 fits file. While fitswrite supports conversion to int16, it will not work for uint16. Any way around this?
  3 commentaires
Enrico Segre
Enrico Segre le 24 Jan 2023
Modifié(e) : Walter Roberson le 16 Nov 2023
It's a FITS limitation, not a maltab limitation. A solution should be to write int16(im-32768), with BSCALE=1 and BZERO= 32768 in the FITS header. Found. e.g. on https://github.com/astropy/astropy/issues/362
David Nemati
David Nemati le 16 Nov 2023
Modifié(e) : David Nemati le 16 Nov 2023
I doubt this is a FITS limitation, @Enrico Segre. I received FITS files that consist of integers ranging roughly from 0 to 2^16-1, and the metadata says 1) that it's a good FITS file and 2) that it's 16 bits per pixel. I edited the arrays using Matlab. There was no problem reading them, but when I set the values as uint16 and then tried using fitswrite to save my changes, it threw an error.
I see the line in fitswrite.m,
datatypes = {'uint8','int16','int32','int64','single','double'};
I tried editing the cell array and adding in 'uint16', but when I tried to save the changes, Matlab told me "Mm, mmm, you are not going to fiddle with my system."
I would say this is a problem with how Matlab has defined FITS. What it can read is broader than what it can write.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 16 Nov 2023
Modifié(e) : Walter Roberson le 16 Nov 2023
The image pixels in a primary array or an image extension may have one of 5 supported data types:
  • 8-bit (unsigned) integer bytes
  • 16-bit (signed) integers
  • 32-bit (signed) integers
  • 32-bit single precision floating point real numbers
  • 64-bit double precision floating point real numbers
A 64-bit integer datatype has also been proposed and is currently in experimental use. Unsigned 16-bit and 32-bit integers are supported by subtracting an offset from the raw pixel values (e.g., 32768 (2**15) is subtracted from each unsigned 16-bit integer pixel value to shift the values into the range of a signed 16-bit integer) before writing them to the FITS file. This offset is then added to the pixels when reading the FITS image to restore the original values.
So @Enrico Segre was correct: fits files do not support unsigned 16 bit integers. Subtract 32768, then using BSCALE=1 and BZERO=32768 is the correct work-around
This is not aproblem with how MATLAB has defined FITS: the people who created FITS (NASA) say outright that unsigned 16 bit integers are not supported.
  2 commentaires
David Nemati
David Nemati le 16 Nov 2023
Thank you @Walter Roberson, this is quite enlightening.
Is there a way in Matlab to set the values of the BSCALE and BZERO of the FITS file I'm saving?
Walter Roberson
Walter Roberson le 16 Nov 2023
Have a look at toolbox/matlab/imagesci/+matlab/+io/+fits/setBscale.m -- but it looks like that does not affect the file.
To affect the file, see the code in writeKey.m in the same directory.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by