X

NV5 Geospatial Blog

Each month, NV5 Geospatial posts new blog content across a variety of categories. Browse our latest posts below to learn about important geospatial information or use the search bar to find a specific topic or author. Stay informed of the latest blog posts, events, and technologies by joining our email list!



Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

6/3/2025

Rethinking the Reliability of Type 1a Supernovae   How do astronomers measure the universe? It all starts with distance. From gauging the size of a galaxy to calculating how fast the universe is expanding, measuring cosmic distances is essential to understanding everything in the sky. For nearby stars, astronomers use... Read More >

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

5/26/2025

Whether you’re new to remote sensing or a seasoned expert, there is no doubt that large language models (LLMs) like OpenAI’s ChatGPT or Google’s Gemini can be incredibly useful in many aspects of research. From exploring the electromagnetic spectrum to creating object detection models using the latest deep learning... Read More >

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

4/28/2025

When every second counts, the ability to process geospatial data rapidly and accurately isn’t just helpful, it’s critical. Geospatial Intelligence (GEOINT) has always played a pivotal role in defense, security, and disaster response. But in high-tempo operations, traditional workflows are no longer fast enough. Analysts are... Read More >

Thermal Infrared Echoes: Illuminating the Last Gasp of a Dying Star

Thermal Infrared Echoes: Illuminating the Last Gasp of a Dying Star

4/24/2025

This blog was written by Eli Dwek, Emeritus, NASA Goddard Space Flight Center, Greenbelt, MD and Research Fellow, Center for Astrophysics, Harvard & Smithsonian, Cambridge, MA. It is the fifth blog in a series showcasing our IDL® Fellows program which supports passionate retired IDL users who may need support to continue their work... Read More >

A New Era of Hyperspectral Imaging with ENVI® and Wyvern’s Open Data Program

A New Era of Hyperspectral Imaging with ENVI® and Wyvern’s Open Data Program

2/25/2025

This blog was written in collaboration with Adam O’Connor from Wyvern.   As hyperspectral imaging (HSI) continues to grow in importance, access to high-quality satellite data is key to unlocking new insights in environmental monitoring, agriculture, forestry, mining, security, energy infrastructure management, and more.... Read More >

1345678910Last
11632 Rate this article:
No rating

Writing an image with a palette to an HDF5 file

Anonym

I was corresponding with Debbie Mao (NCAR) yesterday about how to write an image with a palette to an HDF5 file, when I realized I was doing it wrong. Here's an example of a more correct way to do this, by attaching the palette as an attribute to the image. Start by reading an image from the IDL distro:

 infile = file_which('avhrr.png') image = read_png(infile, r, g, b) palette = transpose([[r],[g],[b]])

I want to save image and palette to an HDF5 file. Create an HDF5 file with:

 file = 'image_and_palette_example.h5' fid = h5f_create(file)

For convenience, create a group to hold the image and its palette:

 group_id = h5g_create(fid, 'AVHRR image')

Next, define HDF5 datatypes and dataspaces for these two variables:

 image_datatype_id = h5t_idl_create(image) palette_datatype_id = h5t_idl_create(palette) image_dataspace_id = h5s_create_simple(size(image, /dimensions)) palette_dataspace_id = h5s_create_simple(size(palette, /dimensions))

(Hopefully my verbose variable names are descriptive.) Now create datasets for image and palette and write them to the HDF5 file:

 image_id = h5d_create(group_id, 'image data', image_datatype_id, image_dataspace_id) palette_id = h5d_create(group_id, 'image palette', palette_datatype_id, palette_dataspace_id) h5d_write, image_id, reverse(image,2) h5d_write, palette_id, palette

I've chosen to flip the image (like using the ORDER keyword) so it previews nicely in the HDF5 file. The drawback of this choice is that the image will need to be flipped again when it's read into IDL. The datatype and dataspace identifiers are no longer needed, so close them:

 h5s_close, image_dataspace_id h5t_close, image_datatype_id h5s_close, palette_dataspace_id h5t_close, palette_datatype_id

Now comes the interesting part: we need to associate the palette with the image so it displays properly in HDFView and H5_BROWSER. The steps for doing this aren't covered well in the IDL docs, but they are described in the HDF5 developer docs. (Aside: the IDL docs provide only a high-level overview of HDF5, HDF4 and HDF-EOS. I've always gone back to the documentation provided by The HDF Group (THG) for more detailed information on using these formats. The IDL API is very similar to the C API provided by THG, so it's pretty straightforward to translate THG's docs to IDL.) Start by assigning the attribute "CLASS = PALETTE" to the image's palette:

 attribute_name = 'CLASS' attribute_data = 'PALETTE' attribute_datatype_id = h5t_idl_create(attribute_data) attribute_dataspace_id = h5s_create_scalar() attribute_id = h5a_create(palette_id, attribute_name, attribute_datatype_id, attribute_dataspace_id) h5a_write, attribute_id, attribute_data h5a_close, attribute_id

Next, assign the attribute "CLASS = IMAGE" to the image:

 attribute_name = 'CLASS' attribute_data = 'IMAGE' attribute_datatype_id = h5t_idl_create(attribute_data) attribute_dataspace_id = h5s_create_scalar() attribute_id = h5a_create(image_id, attribute_name, attribute_datatype_id, attribute_dataspace_id) h5a_write, attribute_id, attribute_data h5a_close, attribute_id

Finally (and this is key), assign a reference to the palette as an attribute to the image, like this:

 attribute_name = 'PALETTE' attribute_data = h5r_create(group_id, 'image palette') attribute_datatype_id = h5t_reference_create() attribute_dataspace_id = h5s_create_scalar() attribute_id = h5a_create(image_id, attribute_name, attribute_datatype_id, attribute_dataspace_id) h5a_write, attribute_id, attribute_data h5a_close, attribute_id

There are other HDF5 attributes that could be assigned to the image and to the palette (see the reference above), but I'm using the minimum number of attributes needed to make this work. End by closing any open identifiers:

 h5d_close, image_id h5d_close, palette_id h5g_close, group_id h5f_close, fid

The code I've used is in hdf5_write_ex.pro; download it and run it:

 IDL> hdf5_write_ex

The program produces an HDF5 file named image_and_palette_example.h5. Here's a screenshot of the file displayed in HDFView: Viewing an image with a palette in HDFView Note how the palette is correctly associated with the image.

Please login or register to post comments.