Subject: HDF Scientific Data

From: [email-masked]
Date: 22 Jun 1995 04:39:06 GMT

I am attempting to read a HDF SDS file with a C program on a VAX/AXP
 with OpenVMS operating system. I can read this file with a FORTRAN routine
 and using HDF calls in IDL. However, I cannot get this C program to work.
 It comes up with an access violation while in the DFSDgetdata routine.

      Is there an example available somewhere that will allow me to read
 a HDF SDS file. I checked the NCSA site, but could not find an example.
 
      If someone knows a location where I can find a simple C program to 
 assist me to read a HDF SDS file, please let me know. OR if you see an
 obvious error in the program below, let me know.

       Thanks in advance,

   Kelly Dean
   CSU/CIRA

============================sdstoppm.c=====================================

#include "ppm.h"
#include "df.h"

#define TRUE 1
#define FALSE 0

#ifndef VMS
void
#endif
main( argc, argv )
    int argc;
    char* argv[];
    {
    pixel* pixrow;
    register pixel* pP;
    int argn, headerskip, rowskip, rows, cols, row, i;
    register int col;
    int order;
    int val1, val2, val3;
    char* usage = "[-n] [HDFfile]";

    char *bp;                   /* Byte pointer in image */
    char hdf_file[80];          /* HDF input file name */
    int err_ret;                /* error number on DF call */
/*    int16 *image; */          /* image array */
    int16 image[2000][4000];
    int   inRank;               /* ranking */
    int32 Dims[2];              /* image dimnsions */

#ifdef VMS
    getredirection (&argc, &argv);
#endif

    ppm_init( &argc, argv );

    argn = 1;

    if ( argn != argc )
 {
        strcpy(hdf_file,argv[argn]);
        err_ret = DFSDgetdims(hdf_file, &inRank, &Dims, 2);
 if (err_ret != 0 ) 
          {
   fprintf( stderr, "\n Error Opening SDS file > %d\n", err_ret);
   exit (1);
          }
        else
          {
          fprintf( stderr, "\n Reading > %s \n", hdf_file );
          fprintf( stderr, "\n Dimensions > %d x %d\n", Dims[0], Dims[1] );
   ++argn;
          }
        err_ret = DFSDgetdata(hdf_file, inRank, Dims, &image );
 if (err_ret != 0 ) 
         {
  fprintf( stderr, "\n Error reading SDS file > %d\n", err_ret);
  exit (1);
         }
        else
         {
  fprintf( stderr,"\n Successfully read SDS image > %s \n",argv[argn] );
         } 
        cols = Dims[0];
        rows = Dims[1];
        }

    if ( argn != argc )
 pm_usage( usage );

    ppm_writeppminit( stdout, cols, rows, 255, 0 );
    pixrow = ppm_allocrow( cols );

        fprintf( stderr, "\n Converting to PPM \n");

    for ( row = 0; row < rows; ++row)      /* top to bottom */
/*    for ( row = rows-1; row >= 0; row--)  */ /* bottom to top */
 {
 bp = ( char * ) &image[row*cols]; 
 for ( col = 0, pP = pixrow; col < cols; ++col, ++pP )
   {
   val1 = ( (*bp)*3 ) / 4;    /* red */
   if ( val1 == EOF )
              pm_error( "EOF / read error - color red" );
   val2 = ( (*bp)*3+1 ) / 4;  /* green */
   if ( val2 == EOF )
              pm_error( "EOF / read error - color green" );
   val3 = ( (*bp)*3+2 ) / 4;  /* blue */
   if ( val3 == EOF )
              pm_error( "EOF / read error - color blue" );
   PPM_ASSIGN( *pP, val1, val2, val3 );
          bp++;
   }
 ppm_writeppmrow( stdout, pixrow, cols,  255, 0 );
 }

    pm_close( stdout );

    exit( 0 );
    }

---

From: [email-masked] (Doug Ilg)
Date: 22 Jun 1995 15:41:42 GMT

In article <[email-masked]>, [email-masked] (Edward Hartnett) writes:
|> >>>>> "d" == dean  <[email-masked]> writes:
|>  
|>     d>         strcpy(hdf_file,argv[argn]);
|>     d>         err_ret = DFSDgetdims(hdf_file, &inRank, &Dims, 2);
|>     d>           fprintf( stderr, "\n Reading > %s \n", hdf_file );
|>     d>           fprintf( stderr, "\n Dimensions > %d x %d\n", Dims[0], Dims[1] );
|>     d>         err_ret = DFSDgetdata(hdf_file, inRank, Dims, &image );
|> 
|> I think you need to use a 1-based instead of a 0-based array for the
|> dimensional sizes (i.e. the Dims array).

No, HDF uses 0-based arrays.

I haven't had a chance to look over the whole program yet, but it looks like
you could do without the & before Dims in your call to DFSDgetdims.  Since
Dims is an array, it is equivalent to a pointer.

There is a sample DFSD program in the HDF manual called "Getting Started With HDF."

Also, I would urge you to use the newer "SD" interface to HDF SDSs.  It's a
bit more complicated, but it is much more capable.

			-Doug

---

From: [email-masked] (Edward Hartnett)
Date: 22 Jun 1995 15:05:39 GMT

>>>>> "d" == dean  <[email-masked]> writes:

    d>      I am attempting to read a HDF SDS file with a C program on a VAX/AXP
    d>  with OpenVMS operating system. I can read this file with a FORTRAN routine
    d>  and using HDF calls in IDL. However, I cannot get this C program to work.
    d>  It comes up with an access violation while in the DFSDgetdata routine.

    d>       Is there an example available somewhere that will allow me to read
    d>  a HDF SDS file. I checked the NCSA site, but could not find an example.
 
    d>       If someone knows a location where I can find a simple C program to 
    d>  assist me to read a HDF SDS file, please let me know. OR if you see an
    d>  obvious error in the program below, let me know.

    d>         strcpy(hdf_file,argv[argn]);
    d>         err_ret = DFSDgetdims(hdf_file, &inRank, &Dims, 2);
    d>           fprintf( stderr, "\n Reading > %s \n", hdf_file );
    d>           fprintf( stderr, "\n Dimensions > %d x %d\n", Dims[0], Dims[1] );
    d>         err_ret = DFSDgetdata(hdf_file, inRank, Dims, &image );

I think you need to use a 1-based instead of a 0-based array for the
dimensional sizes (i.e. the Dims array).

--