Writing an HDF file with C, FORTRAN and Python

Language

PYTHON

import numpy
from pyhdf.SD import SD, SDC
 
# Open file for writing
fileName = "SDS.hdf"
filehdf  = SD(fileName, SDC.WRITE | SDC.CREATE)
 
# Init the data buffer to write : a vector of values from 0 to 24, separated by 1
data = numpy.arange(25)
# Make it a 2D array
data.shape = (5, 5)
 
# Create the dataset : set its name, data type et dimensions
sds_name = "SDStemplate"
sds = filehdf.create(sds_name, SDC.INT32, data.shape)
 
# Write the data to it
sds[:] = data
 
# Terminate access to the data set
sds.endaccess()
 
# Close the file
filehdf.end()

C

#include "mfhdf.h"
#define FILE_NAME "SDS.hdf"
#define SDS_NAME  "SDStemplate"
#define X_LENGTH  5
#define Y_LENGTH  5
 
main( )
{
    /************************* Variable declaration **************************/
    int32 sd_id, sds_id, sds_index;
    intn  status;
    int32 start[2], edges[2];
    int32 data[Y_LENGTH][X_LENGTH];
    int   i, j;
    /********************* End of variable declaration ***********************/
 
    /*
    * Data set data initialization.
    */
    for ( j = 0; j < Y_LENGTH; j++ ) {
        for ( i = 0; i < X_LENGTH; i++ )
            data[j][i] = ( i + j ) + 1;
    }
 
    /*
    * Open the file and initialize the SD interface.
    */
    sd_id = SDstart ( FILE_NAME, DFACC_WRITE );
    /*
     * Attach to the first data set.
    */
    sds_index = 0;
    sds_id = SDselect ( sd_id, sds_index );
    /*
    * Define the location and size of the data to be written to the data set.
    */
    start[0] = 0;
    start[1] = 0;
    edges[0] = Y_LENGTH;
    edges[1] = X_LENGTH;
 
    /*
    * Write the stored data to the data set. The third argument is set to NULL
    * to specify contiguous data elements. The last argument must
    * be explicitly cast to a generic pointer since SDwritedata is designed
    * to write generic data.
    */
    status = SDwritedata ( sds_id, start, NULL, edges, ( VOIDP ) data );
    /*
    * Terminate access to the data set.
    */
    status = SDendaccess ( sds_id );
    /*
    * Terminate access to the SD interface and close the file.
    */
    status = SDend ( sd_id );
}

FORTRAN

program  write_dataprogram  write_data
    implicit none
 
C
C     Parameter declaration.
C
    character * 7  FILE_NAME
    character * 11 SDS_NAME
    integer      X_LENGTH, Y_LENGTH, RANK
    parameter   ( FILE_NAME = "SDS.hdf",
    +             SDS_NAME  = "SDStemplate",
    +             X_LENGTH  = 5,
    +             Y_LENGTH  = 16,
    +             RANK      = 2 )
    integer       DFACC_WRITE, DFNT_INT32
    parameter   ( DFACC_WRITE = 2,
    +             DFNT_INT32  = 24 )
 
C
C     Function declaration.
C
    integer sfstart, sfselect, sfwdata, sfendacc, sfend
 
C**** Variable declaration *******************************************
C
    integer sd_id, sds_id, sds_index, status
    integer start ( 2 ), edges ( 2 ), stride ( 2 )
    integer i, j
    integer data( X_LENGTH, Y_LENGTH )
C
C**** End of variable declaration ************************************
C
 
C
C     Data set data initialization.
C
    do  20 j = 1, Y_LENGTH
        do  10 i = 1, X_LENGTH
            data ( i, j ) = i + j - 1
 10         continue
 20     continue
 
C
C     Open the file and initialize the SD interface.
C
    sd_id = sfstart( FILE_NAME, DFACC_WRITE )
C
C     Attach to the first data set.
C
    sds_index = 0
    sds_id = sfselect(sd_id, sds_index)
C
C     Define the location and size of the data to be written
C     to the data set. Note that setting values of the array stride to 1
C     specifies the contiguous writing of data.
C
    start  ( 1 ) = 0
    start  ( 2 ) = 0
    edges  ( 1 ) = X_LENGTH
    edges  ( 2 ) = Y_LENGTH
    stride ( 1 ) = 1
    stride ( 2 ) = 1
C
C     Write the stored data to the data set named in SDS_NAME.
C     Note that the routine sfwdata is used instead of sfwcdata
C     to write the numeric data.
C
    status = sfwdata ( sds_id, start, stride, edges, data )
C
C     Terminate access to the data set.
C
    status = sfendacc ( sds_id )
C
C     Terminate access to the SD interface and close the file.
C
    status = sfend ( sd_id )
    end

More news

Highlights

AERIS/ICARE was migrated his good old FTP server to SFTP

For security reason, we are abandoning the FTP protocol in favor of SFTP on our distribution server. Depending of the way you are using this service, you can have to change the commands you are used to. Note that not all applications support the SFTP protocol, and some additional tools may need to be installed […]

01.03.2024

Tutorials

How to convert a matplotlib figure to a numpy array or a PIL image

Language/Format: Python
Description: For manipulating a figure build with matplotlib, it is sometimes requested to convert it in a format understandable by other python libraries. This can be useful for using scipy image filters or manually adding annotations for example.
This page details how to convert a matplotlib figure to a numpy 3D array of RGBA values, or directly to a PIL ( Python Imaging Library ) Image.
Author(s): Nicolas Pascal (ICARE)

10.02.2017

Tutorials

Reading a NetCDF file with Matlab and R

Language/Format: MATLAB
Description: This page gives pieces of code to read data in a NetCDF file
Author(s): Aminata NDIAYE (ICARE)

15.12.2015

Search