Creation of an HDF file with C, Fortran and Python :

Language

PYTHON

#!/usr/local/bin/python
#-*- coding: utf-8 -*-
 
from numpy import *
from pyhdf.SD import *
 
X_LENGTH=5
Y_LENGTH=5
 
#Data set data initialization
data=array([[0]*X_LENGTH]*Y_LENGTH)
 
for i in range(0,Y_LENGTH):
    for j in range(0,X_LENGTH):
        data[i][j]=i+j
 
# The hdf file to read
filename='SDS.hdf'
 
# Creation HDF file
filehdf=SD(filename,SDC.WRITE|SDC.CREATE)
 
sds = filehdf.create('my_sds', SDC.INT32,data.shape)
 
# Assign values to sds
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
#define RANK          2  /* Number of dimensions of the SDS */
 
main( )
{
/************************* Variable declaration **************************/
int32 sd_id, sds_id;    /* SD interface and data set identifiers */
int32 dim_sizes[2];     /* sizes of the SDS dimensions */
intn  status;           /* status returned by some routines; has value
SUCCEED or FAIL */
/********************* End of variable declaration ***********************/
 
/*
* Create the file and initialize the SD interface.
*/
sd_id = SDstart (FILE_NAME, DFACC_CREATE);
/*
* Define the dimensions of the array to be created.
*/
dim_sizes[0] = Y_LENGTH;
dim_sizes[1] = X_LENGTH;
 
/*
* Create the data set with the name defined in SDS_NAME. Note that
* DFNT_INT32 indicates that the SDS data is of type int32. Refer to
* Table 2E for definitions of other types.
*/
sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes);
 
/*
* 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  create_SDS
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 = 5,
+             RANK     = 2)
integer      DFACC_CREATE, DFNT_INT32
parameter   (DFACC_CREATE = 4,
+             DFNT_INT32 = 24)
C
C     Function declaration.
C
integer sfstart, sfcreate, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
integer sd_id, sds_id, dim_sizes(2)
integer status
C
C**** End of variable declaration ************************************
C
C
C     Create the file and initialize the SD interface.
C
sd_id = sfstart(FILE_NAME, DFACC_CREATE)
C
C     Define dimensions of the array to be created.
C
dim_sizes(1) = X_LENGTH
dim_sizes(2) = Y_LENGTH
C
C     Create the array with the name defined in SDS_NAME.
C     Note that DFNT_INT32 indicates that the SDS data is of type
C     integer. Refer to Tables 2E and 2I for the definition of other types.
C
sds_id = sfcreate(sd_id, SDS_NAME, DFNT_INT32, RANK,  dim_sizes)
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