Skip to main content

export - Exporting data extensions in fits file


Context


In Astronomy the de facto standard for images and data is FITS. I would like to export heterogenous data sets into a single fits file.


Attempt


I am interested in exporting into a fits file some data, say.


 dat = Table[i, {i, 5}]

(* {1,2,3,4,5} *)


and additionally some metadata, say a range of $\nu$s:



 rnus = ("nu" <> ToString[#] -> 0.5 + # & /@ ( Range[5]))

(* {nu1->1.5,nu2->2.5,nu3->3.5,nu4->4.5,nu5->5.5} *)


Following closely the documentation I can write:


 Export["image.fits", {"Data" -> dat, 
"Metadata" -> Join[rnus, {"Object" -> "something"}]}, "Rules"]

(* image.fits *)


and read back accordingly both the data:


 Import["image.fits", "RawData"][[1]]


(* {1,2,3,4,5} *)


and the metadata:


  "NU1" /. Import["image.fits", "Metadata"]
"OBJECT" /. Import["image.fits", "Metadata"]

(* 1.5 ) ( Something *)


BUT, what I would like to do is to write different (large) tables of different sizes into a single fits file. So using a single keyword for each element is unrealistic.


If the table were of the same size I could join them into a single list, as in


   Export["image.fits", {"Data" -> {dat,dat}}, "Rules"]     


but in my case the tables are not of the same size.


In principle the documentation says "For FITS files that contain multiple images or data extensions, the above elements are taken to be lists of the respective expressions."


Question



How can I write multiple images or data extensions into a unique fits file? Or in other words, what does the documentation mean?



Update


As requested, here is an example of fits with an extension containing {1,2,3,4,5} and {1,2,3,4,5,6} :


https://dl.dropboxusercontent.com/u/659996/test.fits



or it can be produced in python as


python
import pyfits
import numpy
data=numpy.array([1.,2.,3.,4.,5.])
pyfits.writeto("test.fits",data)
data=numpy.array([1.,2.,3.,4.,5.,6.])
pyfits.append("test.fits",data)
exit

Answer




This is a bit of a shot in the dark but this general approach may work with a bit of tweaking. (note another edit fix..)


 dat = Table[i, {i, 5}]
rnus = ("nu" <> ToString[#] -> 0.5 + # & /@ (Range[5]))
BinaryWrite[g = OpenWrite["test.fits", BinaryFormat -> True],
ExportString[{"Data" -> dat, "Metadata" -> Join[rnus,
{"Object" -> "something", "NEXTEND" -> 1}]}, {"FITS", "Rules"}]]
BinaryWrite[g,
StringReplace[
ExportString[
{"Data" -> dat,

"Metadata" -> Join[rnus, {"Object" -> "something"}]},
{"FITS", "Rules"}],
"SIMPLE = T" ->
"XTENSION= 'IMAGE ' "]]
Close[g];

Edit -- note that StringReplace must preserve the string length.


Here's what my reader shows (changing the data type to integer) .. note mathematica has added a bunch of extra metadata compared to the example.


enter image description here


Approach 2



This is a more low level approach, forgoing Export


 stringpad[s_, n_] := StringJoin[s, Table[" ", {n - StringLength@s}]];
writefitshead[f_][hdat_] := (
BinaryWrite[f, stringpad[StringJoin[stringpad[#[[1]], 8],
If[Length[#] > 1, StringJoin["= ", #[[2]], " "], ""],
If[Length[#] > 2, StringJoin["/", #[[3]]], ""]], 80]] & /@ hdat;
BinaryWrite[f, stringpad["", 80]] & /@ Range[ 36 - Length[hdat]];)
bytelen["Real64"] = 8;
bytelen["Integer16"] = 2;
bytelen["Integer8"] = 1;

writefitsdat[f_, type_][dat_] := (
BinaryWrite[f, dat, type, ByteOrdering -> 1] ;
BinaryWrite[f,
Table[0, {i, 2880 - Mod[Length[dat] bytelen[type], 2880]}], "Integer8"] );
f = OpenWrite["mytest.fits", BinaryFormat -> True];
writefitshead[f]@{
{"SIMPLE", "T", "conforms to FITS standard "},
{"BITPIX", "-64", "array data type"},
{"NAXIS", "1", " number of array dimensions"},
{"NAXIS1", "5"},

{"EXTEND", "T"},
{"END"}};
writefitsdat[f, "Real64"]@Table[i, {i, 5}]; (*note the type here has to match BITPIX*)
writefitshead[f]@{
{"XTENSION", "'IMAGE '", "Image extension "},
{"BITPIX", "-64 ", " array data type "},
{"NAXIS", "1", "number of array dimensions"},
{"NAXIS1", "6"},
{"PCOUNT", "0", "number of parameters"},
{"GCOUNT", "1"},

{"END"}};
writefitsdat[f, "Real64"]@Table[i, {i, 6}];
Close[f];

Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.