/*******************************************************************/
/*                                                                 */
/* sd_prom_ext.c                                                   */
/*     Promote (move) an existing data array to an external file   */
/*    Files created:                                               */
/*       extl.hdf, extl.dat                                        */
/*                                                                 */
/*******************************************************************/

#include "mfhdf.h"
#include <stdio.h>
#include <string.h>
#include <fcntl.h>

#define  NELTS  6
#define  NROWS  3
#define  RANK   2

char *fn = "extl.hdf";
char *dfn = "extl.dat";
int32  dims[RANK] = {NROWS, NELTS};
int32 start[RANK] = {0, 0};
int32 data1[NROWS][NELTS] = { 5, 8, 11, 14, 17, 20,
                            25, 28, 31, 34, 37, 40,
                            45, 48, 51, 54, 57, 60};
                        
main()   {

    int ret, i, sds_idx;
    int32 fid, sds_id;
    int32 irank, in_nt, indims[2], inatts;
    int32 indata[NROWS][NELTS], indt[NROWS][NELTS], *p;
    char iname[20];
    int  num, sz;
    FILE *fp, *fopen();

    fid = SDstart(fn, DFACC_CREATE);
    printf ("\nSDstart fid: %i\n", fid);
    sds_id = SDcreate(fid, "mydata", DFNT_INT32, RANK, dims);
    printf ("SDcreate sds_id: %i\n", sds_id);
    ret = SDwritedata(sds_id, start, NULL, dims, (VOIDP)data1);
    printf ("SDwritedata returns: %i\n", ret);
    ret = SDendaccess(sds_id);
    printf ("SDendaccess returns: %i\n", ret);
    ret = SDend(fid);
    printf ("SDend returns: %i\n", ret);

/*  read back HDF file */

    fid = SDstart(fn, DFACC_RDWR);
    printf ("SDstart fid: %i\n", fid);
    sds_idx = SDnametoindex(fid, "mydata");
    printf ("SDnametoindex sds_idx: %i\n", sds_idx);
    sds_id = SDselect(fid, sds_idx);
    printf ("SDselect sds_id: %i\n", sds_id);

/* promote to external file */
    ret = SDsetexternalfile(sds_id, "extl.dat",0);
    printf("SDsetexternalfile returns: %d\n", ret);
    ret = SDendaccess(sds_id);
    printf ("SDendaccess returns: %i\n", ret);
    ret = SDend(fid);
    printf ("SDend returns: %i\n", ret);

/* read external file */
    printf ("\nRead back the contents of the external file.\n");
    printf ("NOTE that if you are on a Big Endian machine\n");
    printf ("the values printed below will be correct.  However,\n");
    printf ("if on a Little Endian machine, they will not.  This is\n");
    printf ("because the DFNT_INT32 datatype is Big Endian. To read\n");
    printf ("the external file on a Little Endian machine, you will\n");
    printf ("need to first convert the data to Little Endian.\n");
  
    sz = NROWS * NELTS;
    fp = fopen(dfn, "r");
    num = fread((void *)indt, sizeof(int32), sz, fp);
    if (num != sz) printf("Read error, num=%d.\n", num); 
    printf("\nSize: %i\n", sz);
    printf("Data:\n ");
    p = &indt[0][0];
    for (i=0; i<num; i++) {
       printf("%d ", *p);
       p++;
    }
    printf("\n\n");
    fclose(fp);

}
