Message Boards Message Boards

1
|
6069 Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Read FITS unsigned integers with Mathematica 12?

Posted 4 years ago

I've been using Mathematica to process FITS for years, but it has changed in version 12. Import now yields an Association rather than an Image. OK, extract item 1 from the Association, that's an Image. But it isn't right. A small test file shows the problem:

ImageData[Import["test.fits"][[1]]]

yields a matrix full of tiny numbers like 2.81374*10^-307. This is impossible. The header has:

BITPIX = 32 / array data type

BSCALE = 1

BZERO = 2147483648

This is the conventional declaration of 32 bit unsigned integers. Apparently, these are misinterpreted as floating point.

Is this a bug, or have I missed another change?

Attachments:
POSTED BY: John Doty
2 Replies

Hi John,

There was indeed an issue with the Image object returned from FITS files for specific data types, but that issue is fixed and will be available with the soonest update.

Let me show you, what it looks like now.

The file that you have attached has a data type of "UnsignedInteger32".

In[1]:= f = FindFile["~/Downloads/test.fits"];
Import[f, {"DataType", 1}]

Out[2]= "UnsignedInteger32"

Now, if we look at what types does Image object support: enter image description here

We can't fit one into another, thus we try to put the data into the largest possible container; that is Real64. It is really easy to test that result is actually what it should be (let's forget about data types for a second):

In[3]:= d = Import[f, {"Data", 1}];
i = Import[f, {"Image", 1}] // ImageData;
AllTrue[Flatten[i - d], PossibleZeroQ]

Out[5]= True

The above is exactly what was broken in V12; Out[5]= False

Since we don't have any restriction on raw data type, you can import it as-is:

In[6]:= Import[f, {"Data", 1}]

Out[6]= {{4803970, 4803214, 4804757, 4805016, 4804905, 4805127, 
  4805053, 4805090, 4805164, 4805201}, {4805314, 4726765, 4726804, 
  4726687, 4726687, 4726609, 4726570, 4726494, 4726456, 
  4726190}, {4724560, 4727196, 4751536, 4752196, 4753859, 4753970, 
  4754007, 4754081, 4754007, 4753896}, {4754044, 4754081, 4754156, 
  4451552, 4451636, 4451594, 4451510, 4451468, 4451552, 
  4451468}, {4451510, 4451384, 4449760, 4450967, 4924137, 4929428, 
  4915686, 4905894, 4904530, 4905646}, {4903174, 4903051, 4903789, 
  4903174, 4902559, 4904158, 4905026, 4902928, 4902070, 
  4903420}, {4903174, 4903420, 4903174, 4902928, 4903420, 4902314, 
  4897832, 4900122, 4918296, 4923869}, {4903543, 4909155, 4913102, 
  4907144, 4909029, 4909281, 4925754, 4965549, 4998644, 
  5014451}, {4921998, 4894503, 4893913, 4893327, 4894857, 4892157, 
  4897712, 4960496, 5177710, 5027593}, {4900728, 4889152, 4889727, 
  4891809, 4890997, 4890417, 4894621, 4893093, 4886646, 4884844}}

This is also the way to a workaround for the issue in V12:

In[7]:= iManual = ImageData[Image[%, "Real64"]];
AllTrue[Flatten[iManual - d], PossibleZeroQ]

Out[8]= True

Hope this helps. Please feel free to get back if you have more questions.

Best,

Mikayel

POSTED BY: Mikayel Egibyan

Thank you Mikayel. It works. The only problem is that extracts exact integers, making calculations slow, but N[] fixes that.

POSTED BY: John Doty
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract