IMPORTANT NOTICE: An upgrade of the ICARE network architecture is scheduled on Thursday, September 27, 2018 from 9:30 AM to 4:00 PM CEST (UTC +2:00).
All ICARE data services will be unavailable during this operation. We apologize for any inconvenience this may cause and thank you for your patience.

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