/* This example shows how to read an HDF4 attribute from an HDF-EOS2 hybrid file. */

#include <mfhdf.h>
#include <hdf.h>
#include <HdfEosDef.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
	int32 swathfile1;
	int32 swath1;
	int32 sdfile1;
	int32 datafield1rank;
	int32 datafield1dimsize[32];
	int32 datafield1type;
	char datafield1dimname[512];
	int16 *datafield1data;
	int32 datafield1sdsindex;
	int32 datafield1sds;
	char datafield1sds_attr0name[H4_MAX_NC_NAME + 1];
	int32 datafield1sds_attr0type;
	int32 datafield1sds_attr0count;
	float32 *datafield1sds_attr0data;
	int32 i, j;

	/* Open 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf' using swath API */
	if ((swathfile1 = SWopen("AMSR_E_L2_Ocean_B01_200206182340_A.hdf", DFACC_RDONLY)) == -1) {
		fprintf(stderr, "error: cannot open swath 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf'\n");
		return -1;
	}

	/* Open 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf' and initializes an SDS interface */
	if ((sdfile1 = SDstart("AMSR_E_L2_Ocean_B01_200206182340_A.hdf", DFACC_READ)) == -1) {
		fprintf(stderr, "error: cannot open 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf' using SD interface\n");
		return -1;
	}

	/* Open a swath named 'Swath1' */
	if ((swath1 = SWattach(swathfile1, "Swath1")) == -1) {
		fprintf(stderr, "error: cannot attach to 'Swath1'\n");
		return -1;
	}

	/* Retrieve information about 'High_res_cloud' datafield */
	if ((SWfieldinfo(swath1, "High_res_cloud", &datafield1rank, datafield1dimsize, &datafield1type, datafield1dimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for 'High_res_cloud'\n");
		return -1;
	}
	/* Allocate buffer for 'High_res_cloud' */
	if ((datafield1data = malloc(sizeof(int16) * 2001 * 196)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for 'High_res_cloud'\n");
		return -1;
	}
	/* Read data from 'High_res_cloud' */
	if ((SWreadfield(swath1, "High_res_cloud", NULL, NULL, NULL, datafield1data)) == -1) {
		fprintf(stderr, "error: cannot read field 'High_res_cloud'\n");
		return -1;
	}
	/* Dump data from 'High_res_cloud' */
	for (i = 0; i < 10; ++i) {
		for (j = 0; j < 2; ++j) {
			printf("%d ", datafield1data[j + 196 * i]);
		}
		printf("\n");
	}
	/* Release the buffer for 'High_res_cloud' */
	free(datafield1data);

	/* Determine the index of a data set for 'High_res_cloud' */
	if ((datafield1sdsindex = SDnametoindex(sdfile1, "High_res_cloud")) == -1) {
		fprintf(stderr, "error: cannot determines the index of a data set for 'High_res_cloud'\n");
		return -1;
	}
	/* Obtain the data set identifier of 'High_res_cloud' */
	if ((datafield1sds = SDselect(sdfile1, datafield1sdsindex)) == -1) {
		fprintf(stderr, "error: cannot obtain the data set identifier of 'High_res_cloud'\n");
		return -1;
	}
	/* Retrieve information about attribute0 */
	if ((SDattrinfo(datafield1sds, 0, datafield1sds_attr0name, &datafield1sds_attr0type, &datafield1sds_attr0count)) == -1) {
		fprintf(stderr, "error: cannot retrieve information about attribute0\n");
		return -1;
	}
	/* Allocate buffer for attribute0 */
	if ((datafield1sds_attr0data = malloc(sizeof(float32) * datafield1sds_attr0count)) == NULL) {
		fprintf(stderr, "error: cannot allocate buffer for attribute0\n");
		return -1;
	}
	/* Read the values of attribute0 */
	if ((SDreadattr(datafield1sds, 0, datafield1sds_attr0data)) == -1) {
		fprintf(stderr, "error: cannot read the values of attribute0\n");
		return -1;
	}
	for (i = 0; i < datafield1sds_attr0count; ++i) {
		printf("%s [%d] %f\n", datafield1sds_attr0name, (int)i, datafield1sds_attr0data[i]);
	}
	/* Release the buffer for attribute0 */
	free(datafield1sds_attr0data);


	/* Close the swath named 'Swath1' */
	if ((SWdetach(swath1)) == -1) {
		fprintf(stderr, "error: cannot detach from 'Swath1'\n");
		return -1;
	}

	/* Close 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf' */
	if ((SWclose(swathfile1)) == -1) {
		fprintf(stderr, "error: cannot close swath 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf'\n");
		return -1;
	}
	/* Terminate access to an SDS interface from 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf' */
	if ((SDend(sdfile1)) == -1) {
		fprintf(stderr, "error: cannot terminate access to an SDS interface\n");
		return -1;
	}

	return 0;
}

