Using MATLAB to Convert Volumetric Data from .csv to HDF5 File (.h5) Tutorial

Problem Statement

Convert a .csv file containing columns of time, latitude, longitude, altitude, and corresponding temperature data to an STK-compatible format. For this exercise, you will your MATLAB tools to read a .csv file, restructure its data, and write it to an STK-compatible HDF5 file (.h5).

Solution

External volumetric data must be provided to STK in the HDF5 file format. MATLAB provides built-in functions for reading, writing, and manipulating data from HDF5 files. You will use these functions in several specialized m-files located in <STK install folder/Help/stktraining/samples.

Take the following short steps to complete this task:

  1. Read the data from an example .csv file into a 5-column MATLAB matrix.
  2. Manipulate elements and corresponding indexes of this matrix to restructure it into individual time, latitude, longitude, and altitude arrays, with corresponding volumetric data assembled into a single 4-dimensional array.
  3. Create the header information necessary for the STK-compatible HDF5 file, including reference epoch, units of coordinate, and units of volumetric data.
  4. Add the header, times, grid coordinates, and corresponding volumetric data in HDF5 format to the .h5 file

All of these steps are accomplished by the main function found in:

<STK install folder/Help/stktraining/samples>/vmExampleWeatherCsv2Hdf5.m file

This main function calls two other functions, which can be found in the corresponding m-files also located in <STK install folder/Help/stktraining/samples>/:

csv2vmarr. This generic function can be used to read any .csv in which data is organized in columns such that the first column represents times in seconds since some reference epoch, the next three columns represent spatial coordinates, and the last column represents some volumetric data. The function produces four ordered arrays (without repeating elements), one for times and three for spatial coordinates, and a single 4-dimensional array of corresponding volumetric data.
writevmhdf5. This generic function can be used to write an STK-compatible HDF5 file given the file name and structures containing information about the header, times, grid and volumetric data. There is also an auxiliary local function defined inside vmExampleWeatherCsv2Hdf5.m file, called vmExampleWeatherWriteHdf5, which demonstrates how these structures for the header, times, grid and volumetric data can be prepared.

Using these MATLAB files the entire conversion process can be accomplished using a single call, such as:

('DynamicWeatherData.csv','DynamicWeatherDataForSTK.h5')

Just make sure that all m-files and data files are located in the same folder (you can copy all of them from <STK install folder>/Help/stktraining/samples to your preferred folder; and that current MATLAB path is set to this folder.

The resulting HDF5 file ( e.g., 'volumetricExampleScenarioDynamicWeatherData.vmc.h5 file') can be loaded into STK as described in Using External Volumetric Data in STK Tutorial.

CSV File Requirements

Before converting a csv file to an HDF5 file, ensure that the csv file follows these formatting requirements:

  • No extra commas after the data column (column 5).
  • No extra commas at the end of the file.

An example of an invalid format is:

0,1,1,1,5,,,
0,1,2,1,6
,,,,,

An example of a valid format is:

0,1,1,1,5
0,1,2,1,6