/* This example shows how to read a data field and two geo-location fields in HDF-EOS5 swath data. */

#include <HE5_HdfEosDef.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
	hid_t swathfile1;
	hid_t swath1;
	int datafield1rank;
	hsize_t datafield1dimsize[32];
	hid_t datafield1type;
	char datafield1dimname[512];
	char datafield1maxdimname[512];
	float *datafield1data;
	int geofield1rank;
	hsize_t geofield1dimsize[32];
	hid_t geofield1type;
	char geofield1dimname[512];
	char geofield1maxdimname[512];
	float *geofield1data;
	int geofield2rank;
	hsize_t geofield2dimsize[32];
	hid_t geofield2type;
	char geofield2dimname[512];
	char geofield2maxdimname[512];
	float *geofield2data;
	hsize_t i, j;

	/* Open 'MLS-Aura_L2GP-BrO_v01-52-c01_2007d029.he5' using swath API */
	if ((swathfile1 = HE5_SWopen("MLS-Aura_L2GP-BrO_v01-52-c01_2007d029.he5", H5F_ACC_RDONLY)) == -1) {
		fprintf(stderr, "error: cannot open swath 'MLS-Aura_L2GP-BrO_v01-52-c01_2007d029.he5'\n");
		return -1;
	}

	/* Open a swath named 'BrO' */
	if ((swath1 = HE5_SWattach(swathfile1, "BrO")) == -1) {
		fprintf(stderr, "error: cannot attach to 'BrO'\n");
		return -1;
	}

	/* Retrieve information about 'L2gpValue' datafield */
	if ((HE5_SWfieldinfo(swath1, "L2gpValue", &datafield1rank, datafield1dimsize, &datafield1type, datafield1dimname, datafield1maxdimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for 'L2gpValue'\n");
		return -1;
	}
	/* Allocate buffer for 'L2gpValue' */
	if ((datafield1data = malloc(sizeof(float) * 3494 * 37)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for 'L2gpValue'\n");
		return -1;
	}
	/* Read data from 'L2gpValue' */
	if ((HE5_SWreadfield(swath1, "L2gpValue", NULL, NULL, NULL, datafield1data)) == -1) {
		fprintf(stderr, "error: cannot read field 'L2gpValue'\n");
		return -1;
	}
	/* Dump data from 'L2gpValue' */
	for (i = 0; i < 30; ++i) {
		for (j = 0; j < 1; ++j) {
			printf("%f ", datafield1data[j + 37 * i]);
		}
		printf("\n");
	}
	/* Release the buffer for 'L2gpValue' */
	free(datafield1data);

	/* Retrieve information about 'Longitude' geolocation field */
	if ((HE5_SWfieldinfo(swath1, "Longitude", &geofield1rank, geofield1dimsize, &geofield1type, geofield1dimname, geofield1maxdimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for 'Longitude'\n");
		return -1;
	}
	/* Allocate buffer for 'Longitude' */
	if ((geofield1data = malloc(sizeof(float) * 3494)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for 'Longitude'\n");
		return -1;
	}
	/* Read data from 'Longitude' */
	if ((HE5_SWreadfield(swath1, "Longitude", NULL, NULL, NULL, geofield1data)) == -1) {
		fprintf(stderr, "error: cannot read field 'Longitude'\n");
		return -1;
	}
	/* Dump data from 'Longitude' */
	for (i = 0; i < 30; ++i) {
		printf("%f ", geofield1data[i]);
	}
	printf("\n");
	/* Release the buffer for 'Longitude' */
	free(geofield1data);

	/* Retrieve information about 'Latitude' geolocation field */
	if ((HE5_SWfieldinfo(swath1, "Latitude", &geofield2rank, geofield2dimsize, &geofield2type, geofield2dimname, geofield2maxdimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for 'Latitude'\n");
		return -1;
	}
	/* Allocate buffer for 'Latitude' */
	if ((geofield2data = malloc(sizeof(float) * 3494)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for 'Latitude'\n");
		return -1;
	}
	/* Read data from 'Latitude' */
	if ((HE5_SWreadfield(swath1, "Latitude", NULL, NULL, NULL, geofield2data)) == -1) {
		fprintf(stderr, "error: cannot read field 'Latitude'\n");
		return -1;
	}
	/* Dump data from 'Latitude' */
	for (i = 0; i < 30; ++i) {
		printf("%f ", geofield2data[i]);
	}
	printf("\n");
	/* Release the buffer for 'Latitude' */
	free(geofield2data);

	/* Close the swath named 'BrO' */
	if ((HE5_SWdetach(swath1)) == -1) {
		fprintf(stderr, "error: cannot detach from 'BrO'\n");
		return -1;
	}

	/* Close 'MLS-Aura_L2GP-BrO_v01-52-c01_2007d029.he5' */
	if ((HE5_SWclose(swathfile1)) == -1) {
		fprintf(stderr, "error: cannot close swath 'MLS-Aura_L2GP-BrO_v01-52-c01_2007d029.he5'\n");
		return -1;
	}

	return 0;
}

