WOLFRAM NOTEBOOK

In a Wolfram Community post from a few years back, Andrew Watters expressed an interest in Wolfram Language support for the HEALPix file format. HEALPix stands for Hierarchical Equal Area isoLatitude Pixelization of a sphere. It divides the surface of a sphere into pixels that all have the same area. This makes it a useful format for storing and analyzing data that is distributed across a sphere, such as astronomical or geographical maps. HEALPix is not yet natively supported as an Import/Export format, so this post shows how you can import HEALPix files via the Wolfram External Evaluation framework. This framework allows you to run code written in other programming languages directly within your Wolfram environment. This post also shows how to develop your own Import converter with the extensible Import/Export framework. Finally this post shows how to convert the raw HEALPix data to various common map projections like the Equirectangular, Mollweide, and Orthographic projections.
Step one is writing an Import function which takes care of importing files that are in the HEALPix format. Here I am using the External Evaluation framework to run a small piece of external code that reads a file and returns the raw HEALPix data:
In[]:=
Clear[ImportHEALPix];ImportHEALPix[file:(_String|_File|_URL)]:=Module[{session,fun},session=StartExternalSession[<|"System"->"Python","ID"->"healpix-community-post","Evaluator"-><|"Dependencies"->{"matplotlib","healpy"}|>,"SessionProlog"->{"from healpy.fitsfunc import read_map","from healpy.visufunc import cartview"}|>];fun=ExternalFunction[session,"lambda file: cartview(read_map(file),return_projected_map=True)"];fun[file]]
Here is an example of how it can be called with a URL or a file. Note that the HEALPix data is actually contained in another “container” file format called FITS. The FITS (Flexible Image Transport System) file format is a specialized standard for storing astronomical data. A FITS file has a structure with Header Data Units (HDUs) that contain descriptive ASCII headers (using keyword-value pairs) and optional data blocks which hold the actual data. FITS supports various data types, including integers and floating-point numbers. This allows it to store images of varying dimensions, tables, spectra, and other scientific data sets. The Wolfram Language can import any FITS file, including FITS files that contain HEALPix formatted datasets, but there is no built-in functionality to process the raw HEALPix data into anything else.
In[]:=
ImportHEALPix["https://lambda.gsfc.nasa.gov/data/map/dr5/dfp/ilc/wmap_ilc_9yr_v5.fits"]
Out[]=
NumericArray
Type: Real64
Dimensions: {400,800}
Data not saved. Save now
Now that we have a HEALPix file import function, we can register it with the Import/Export framework:
In[]:=
ImportExport`RegisterImport["HEALPix",ImportHEALPix]
We can now import any HEALPix file from a URL or by downloading it first. This specific URL points to a well-known image of the Cosmic Microwave Background (CMB) radiation. This radiation is a faint afterglow of the Big Bang, filling the entire universe with a uniform glow of microwave radiation. This radiation carries information about the conditions present when the universe was in its infancy, at about 380,000 years old. While incredibly uniform, the CMB contains tiny temperature fluctuations that are believed to be the initial seeds for the galaxies and large-scale structures we see in the universe today.
In[]:=
file=URLDownload["https://lambda.gsfc.nasa.gov/data/map/dr5/dfp/ilc/wmap_ilc_9yr_v5.fits"];
In[]:=
data=Import[file,"HEALPix"]
Out[]=
NumericArray
Type: Real64
Dimensions: {400,800}
Data not saved. Save now
In[]:=
image=Colorize[Image@Rescale@Normal@data,ColorFunction->"Rainbow"]
Out[]=
The image above is in the “Equirectangular” format, which is a map projection where latitude and longitude are spaced in a regular grid. This is the same map projection, but for the Earth:
A map projection for the CMB radiation that is more commonly used is the “Mollweide” projection. The Wolfram Language has a built-in, but undocumented, function which can transform any image from one map projection to another one. The code below implements a transformation from “Equirectangular” to “Mollweide”:
In[]:=
MollweideImageProjection[image_]:=ImageResize[GeoGraphics`GeoImageTransformation[image,"Equirectangular"->{"Mollweide","GeoRange"->{{-90,90},{-180,180}}},{{-180.,180.},{-90.,90.}}->Sqrt[2.]{{-2,2},{-1,1}},Background->RGBColor[1,1,1,0]],ImageDimensions[image]]
This results in the somewhat “famous” CMB radiation image. Note that it represents very small temperature variations; the wide range of colors exaggerate these variations.
In[]:=
MollweideImageProjection[image]
Out[]=
Another common projection is the “Orthographic” projection, which is implemented in the code below. With this projection you can really only see “one side” of spherical data (whether it is the Sky or the Earth), so this code shows a simultaneous “front and back” view:
In[]:=
DualOrthographicImageProjection[image_]:=ImageAssemble[{Map[ImageResize[GeoGraphics`GeoImageTransformation[image,"Equirectangular"->{"Orthographic","GeoRange"->{{-90,90},{-180,180}}},N[{#,{-90,90}}]->{{-1,1},{-1,1}},Background->RGBColor[1,1,1,0]],{512,512}]&,{{-180,180},{0,360}}]}]
In[]:=
DualOrthographicImageProjection[image]
Out[]=
Finally, using the built-in AnimationVideo function, you can generate a short video that animates this map projection:
In[]:=
video=AnimationVideo[ImageAssemble[{Map[ImageResize[GeoGraphics`GeoImageTransformation[image,"Equirectangular"->{"Orthographic","GeoRange"->{{-90,90},{-180,180}}},{N[#],{-90.,90.}}->{{-1,1},{-1,1}},Background->RGBColor[1,1,1,0]],{512,512}]&,{{-180+t,180+t},{t,360+t}}]}],{t,0,359}]
Out[]=
For more information on CMB radiation you can visit the website of the NASA Wilkinson Microwave Anisotropy Probe. This web site has a lot of background on CMB radiation and contains many more HEALPix data files to load and view:
In[]:=
file=URLDownload["https://lambda.gsfc.nasa.gov/data/map/dr5/skymaps/9yr/raw/wmap_iqumap_r9_9yr_K1_v5.fits"];data=Import[file,"HEALPix"];
In[]:=
image=MollweideImageProjection@Colorize[Image@data,ColorFunction->"Rainbow"]
Out[]=

Notes

CITE THIS NOTEBOOK

Wolfram Cloud

You are using a browser not supported by the Wolfram Cloud

Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.


I understand and wish to continue anyway »

You are using a browser not supported by the Wolfram Cloud. Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.