Scroll to navigation

SD(3pm) User Contributed Perl Documentation SD(3pm)

NAME

PDL::IO::HDF::SD - PDL interface to the HDF4 SD library.

SYNOPSIS

  use PDL;
  use PDL::IO::HDF::SD;
  
  #      
  # Creating and writing an HDF file
  #
  # Create an HDF file:
  my $hdf = PDL::IO::HDF::SD->new("-test.hdf");
  # Define some data
  my $data = sequence(short, 500, 5);
  # Put data in file as 'myData' dataset with the names 
  #    of dimensions ('dim1' and 'dim2')
  $hdf->SDput("myData", $data , ['dim1','dim2']);
  # Put some local attributes in 'myData'
  #
  # Set the fill value to 0
  my $res = $hdf->SDsetfillvalue("myData", 0);
  # Set the valid range from 0 to 2000
  $res = $hdf->SDsetrange("myData", [0, 2000]);
  # Set the default calibration for 'myData' (scale factor = 1, other = 0)
  $res = $hdf->SDsetcal("myData");
  # Set a global text attribute
  $res = $hdf->SDsettextattr('This is a global text test!!', "myGText" );
  # Set a local text attribute for 'myData'
  $res = $hdf->SDsettextattr('This is a local text testl!!', "myLText", "myData" );
  # Set a global value attribute (you can put all values you want)
  $res = $hdf->SDsetvalueattr( PDL::short( 20 ), "myGValue");
  # Set a local value attribute (you can put all values you want)
  $res = $hdf->SDsetvalueattr( PDL::long( [20, 15, 36] ), "myLValues", "myData" );
  # Close the file
  $hdf->close();
  #
  # Reading from an HDF file:
  #
  # Open an HDF file in read only mode:
  my $hdf = PDL::IO::HDF::SD->new("test.hdf");
  # Get a list of all datasets:
  my @dataset_list = $hdf->SDgetvariablename();
  # Get a list of the names of all global attributes:
  my @globattr_list = $hdf->SDgetattributenames();
  # Get a list of the names of all local attributes for a dataset:
  my @locattr_list = $hdf->SDgetattributenames("myData");
  # Get the value of local attribute for a dataset:
  my $value = $hdf->SDgetattribut("myLText","myData");
  # Get a PDL var of the entire dataset 'myData':
  my $data = $hdf->SDget("myData");
  # Apply the scale factor of 'myData'
  $data *= $hdf->SDgetscalefactor("myData");
  # Get the fill value and fill the PDL var in with BAD:
  $data->inplace->setvaltobad( $hdf->SDgetfillvalue("myData") );
  # Get the valid range of a dataset:
  my @range = $hdf->SDgetrange("myData");
 
  #Now you can do what you want with your data
  $hdf->close();

DESCRIPTION

This library provides functions to read, write, and manipulate HDF4 files with HDF's SD interface.

For more information on HDF4, see http://hdf.ncsa.uiuc.edu/

There have been a lot of changes starting with version 2.0, and these may affect your code. PLEASE see the 'Changes' file for a detailed description of what has been changed. If your code used to work with the circa 2002 version of this module, and does not work anymore, reading the 'Changes' is your best bet.

In the documentation, the terms dataset and SDS (Scientific Data Set) are used interchangeably.

CLASS METHODS

new

    Open or create a new HDF object.

    Arguments:
        1 : The name of the file.
            if you want to write to it, prepend the name with the '+' character : "+name.hdf"
            if you want to create it, prepend the name with the '-' character : "-name.hdf"
            otherwise the file will be open in read only mode
    
    Returns the hdf object (die on error)

    my $hdf = PDL::IO::HDF::SD->new("file.hdf");

Chunking

    Accessor for the chunking mode on this HDF file.
    
    'Chunking' is an internal compression and tiling the HDF library can
        perform on an SDS.
        
    This variable only affects they way SDput() works, and is ON by default.
    
    The code modifications enabled by this flag automatically partition the 
        dataset to chunks of at least 100x100 values in size. The logic on this
        is pretty fancy, and would take a while to doc out here. If you
        _really_ have to know how it auto-partitions the data, then look at
        the code.
    
    Someday over the rainbow, I'll add some features for better control of the
        chunking parameters, if the need arises. For now, it's just stupid easy 
        to use.

    Arguments:
        1 (optional): new value for the chunking flag.

    # See if chunking is currently on for this file:
    my $chunkvar = $hdf->Chunking();
    # Turn the chunking off:
    my $newvar = $hdf->Chunking( 0 );
    
    # Turn the chunking back on:
    my $newvar = $hdf->Chunking( 1 );

SDgetvariablenames

    get the list of datasets.

    No arguments
    Returns the list of dataset or undef on error.

    my @DataList = $hdfobj->SDgetvariablenames();

SDgetattributenames

    Get a list of the names of the global or SDS attributes.

    Arguments:
        1 (optional) : The name of the SD dataset from which you want to get 
            the attributes. This arg is optional, and without it, it will 
            return the list of global attribute names.
        
    Returns a list of names or undef on error.

    # For global attributes :
    my @attrList = $hdf->SDgetattributenames();
    # For SDS attributes :
    my @attrList = $hdf->SDgetattributenames("dataset_name");

SDgetattribute

    Get a global or SDS attribute value.

    Arguments:
        1 : The name of the attribute.
        2 (optional): The name of the SDS from which you want to get the attribute
            value. Without this arg, it returns the global attribute value of that name.
    
    Returns an attribute value or undef on error.

    # for global attributs :
    my $attr = $hdf->SDgetattribute("attr_name");
    # for local attributs :
    my $attr = $hdf->SDgetattribute("attr_name", "dataset_name");

SDgetfillvalue

    Get the fill value of an SDS.

    Arguments:
        1 : The name of the SDS from which you want to get the fill value.
    
    Returns the fill value or undef on error.

    my $fillvalue = $hdf->SDgetfillvalue("dataset_name");

SDgetrange

    Get the valid range of an SDS.

    Arguments:
        1 : the name of the SDS from which you want to get the valid range.
    
    Returns a list of two elements [min, max] or undef on error.

    my @range = $hdf->SDgetrange("dataset_name");

SDgetscalefactor

    Get the scale factor of an SDS.

    Arguments:
        1 : The name of the SDS from which you want to get the scale factor.
    
    Returns the scale factor or undef on error.

    my $scale = $hdf->SDgetscalefactor("dataset_name");

SDgetdimsize

    Get the dimensions of a dataset.

    Arguments:
        1 : The name of the SDS from which you want to get the dimensions.
        
    Returns an array of n dimensions with their sizes or undef on error.

    my @dim = $hdf->SDgetdimsize("dataset_name");

SDgetunlimiteddimsize

    Get the actual dimensions of an SDS with 'unlimited' dimensions.

    Arguments:
        1 : The name of the SDS from which you want to the dimensions.
    
    Returns an array of n dimensions with the dim sizes or undef on error.

    my @dims = $hdf->SDgetunlimiteddimsize("dataset_name");

SDgetdimnames

    Get the names of the dimensions of a dataset.

    Arguments:
        1 : the name of a dataset you want to get the dimensions'names .
    
    Returns an array of n dimensions with their names or an empty list if error.

    my @dim_names = $hdf->SDgetdimnames("dataset_name");

SDgetcal

    Get the calibration factor from an SDS.

    Arguments:
        1 : The name of the SDS
    
    Returns (scale factor, scale factor error, offset, offset error, data type), or undef on error.

    my ($cal, $cal_err, $off, $off_err, $d_type) = $hdf->SDgetcal("dataset_name");

SDget

    Get a the data from and SDS, or just a slice of that SDS.

    Arguments:
        1 : The name of the SDS you want to get.
        2 (optional): The start array ref of the slice.
        3 (optional): The size array ref of the slice (HDF calls this the 'edge').
        4 (optional): The stride array ref of the slice.
    
    Returns a PDL of data if ok, PDL::null on error.
    
    If the slice arguments are not given, this function will read the entire
        SDS from the file.
    
    The type of the returned PDL variable is the PDL equivalent of what was
        stored in the HDF file.

    # Get the entire SDS:
    my $pdldata = $hdf->SDget("dataset_name");
    # get a slice of the dataset
    my $start = [10,50,10];  # the start position of the slice is [10, 50, 10]
    my $edge = [20,20,20];   # read 20 values on each dimension from @start
    my $stride = [1, 1, 1];  # Don't skip values
    my $pdldata = $hdf->SDget( "dataset_name", $start, $edge, $stride );

SDsetfillvalue

    Set the fill value for an SDS.

    Arguments:
        1 : The name of the SDS.
        2 : The fill value.
    
    Returns true on success, undef on error.

    my $res = $hdf->SDsetfillvalue("dataset_name",$fillvalue);

SDsetrange

    Set the valid range of an SDS.

    Arguments:
        1 : The name of the SDS
        2 : an anonymous array of two elements : [min, max].
    
    Returns true on success, undef on error.

    my $res = $hdf->SDsetrange("dataset_name", [$min, $max]);

SDsetcal

    Set the HDF calibration for an SDS.
    
    In HDF lingo, this means to define:
        scale factor
        scale factor error
        offset
        offset error

    Arguments:
        1 : The name of the SDS.
        2 (optional): the scale factor (default is 1)
        3 (optional): the scale factor error (default is 0)
        4 (optional): the offset (default is 0)
        5 (optional): the offset error (default is 0)
    
    Returns true on success, undef on error.
    
    NOTE: This is not required to make a valid HDF SDS, but is there if you want to use it.

    # Create the dataset:
    my $res = $hdf->SDsetcal("dataset_name");
    # To just set the scale factor:
    $res = $hdf->SDsetcal("dataset_name", $scalefactor);
    # To set all calibration parameters:
    $res = $hdf->SDsetcal("dataset_name", $scalefactor, $scale_err, $offset, $off_err);

SDsetcompress

    Set the internal compression on an SDS.

    Arguments:
        1 : The name of the SDS.
        2 (optional): The gzip compression level ( 1 - 9 ). If not
            specified, then 6 is used.
    
    Returns true on success, undef on failure.
    
    WARNING: This is a fairly buggy feature with many version of the HDF library.
    Please just use the 'Chunking' features instead, as they work far better, and
    are more reliable.

    my $res = $hdf->SDsetfillvalue("dataset_name",$deflate_value);

SDsettextattr

    Add a text HDF attribute, either globally, or to an SDS.

    Arguments:
        1 : The text you want to add.
        2 : The name of the attribute
        3 (optional): The name of the SDS.
        
    Returns true on success, undef on failure.

    # Set a global text attribute:
    my $res = $hdf->SDsettextattr("my_text", "attribut_name");
    # Set a local text attribute for 'dataset_name':
    $res = $hdf->SDsettextattr("my_text", "attribut_name", "dataset_name");

SDsetvalueattr

    Add a non-text HDF attribute, either globally, or to an SDS.

    Arguments:
        1 : A pdl of value(s) you want to store.
        2 : The name of the attribute.
        3 (optional): the name of the SDS.
    
    Returns true on success, undef on failure.

    my $attr = sequence( long, 4 );
    # Set a global attribute:
    my $res = $hdf->SDsetvalueattr($attribute, "attribute_name");
    # Set a local attribute for 'dataset_name':
    $res = $hdf->SDsetvalueattr($attribute, "attribute_name", "dataset_name");

SDsetdimname

    Set or rename the dimensions of an SDS.

    Arguments:
        1 : The name of the SDS.
        2 : An anonymous array with the dimensions names. For dimensions you want
            to leave alone, leave 'undef' placeholders.
    
    Returns true on success, undef on failure.

    # Rename all dimensions
    my $res = $hdf->SDsetdimname("dataset_name", ['dim1','dim2','dim3']);
    # Rename some dimensions
    $res = $hdf->SDsetdimname("dataset_name", ['dim1', undef ,'dim3']);

SDput

    Write to a SDS in an HDF file or create and write to it if it doesn't exist.

    Arguments:
        1 : The name of the SDS.
        2 : A pdl of data.
        3 (optional): An anonymous array of the dim names (only for creation)
        4 (optional): An anonymous array of the start of the slice to store 
            (only for putting a slice)
    Returns true on success, undef on failure.
    
    The datatype of the SDS in the HDF file will match the PDL equivalent as 
        much as possible.

    my $data = sequence( float, 10, 20, 30 ); #any value you want
    # Simple case: create a new dataset with a $data pdl
    my $result = $hdf->SDput("dataset_name", $data);
    # Above, but also naming the dims:
    $res = $hdf->SDput("dataset_name", $data, ['dim1','dim2','dim3']);
    # Just putting a slice in there:
    my $start = [x,y,z];
    $res = $hdf->SDput("dataset_name", $data->slice("..."), undef, $start);

close

    Close an HDF file.

    No arguments.

    my $result = $hdf->close();

CURRENT AUTHOR & MAINTAINER

Judd Taylor, Orbital Systems, Ltd. judd dot t at orbitalsystems dot com

PREVIOUS AUTHORS

Patrick Leilde patrick.leilde@ifremer.fr contribs of Olivier Archer olivier.archer@ifremer.fr

SEE ALSO

perl(1), PDL(1), PDL::IO::HDF(1).

2023-06-17 perl v5.36.0