/* This example shows how to read a data field and two geo-location fields in HDF-EOS2 swath data. */

#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 datafield1rank;
	int32 datafield1dimsize[32];
	int32 datafield1type;
	char datafield1dimname[512];
	float32 *datafield1data;
	int32 geofield1rank;
	int32 geofield1dimsize[32];
	int32 geofield1type;
	char geofield1dimname[512];
	float32 *geofield1data;
	int32 geofield2rank;
	int32 geofield2dimsize[32];
	int32 geofield2type;
	char geofield2dimname[512];
	float32 *geofield2data;
	int32 i, j;

	/* Open 'MOP02-20000303-L2V5.7.1.val.hdf' using swath API */
	if ((swathfile1 = SWopen("MOP02-20000303-L2V5.7.1.val.hdf", DFACC_RDONLY)) == -1) {
		fprintf(stderr, "error: cannot open swath 'MOP02-20000303-L2V5.7.1.val.hdf'\n");
		return -1;
	}

	/* Open a swath named 'MOP02' */
	if ((swath1 = SWattach(swathfile1, "MOP02")) == -1) {
		fprintf(stderr, "error: cannot attach to 'MOP02'\n");
		return -1;
	}

	/* Retrieve information about 'CO Total Column' datafield */
	if ((SWfieldinfo(swath1, "CO Total Column", &datafield1rank, datafield1dimsize, &datafield1type, datafield1dimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for 'CO Total Column'\n");
		return -1;
	}
	/* Allocate buffer for 'CO Total Column' */
	if ((datafield1data = malloc(sizeof(float32) * 154649 * 2)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for 'CO Total Column'\n");
		return -1;
	}
	/* Read data from 'CO Total Column' */
	if ((SWreadfield(swath1, "CO Total Column", NULL, NULL, NULL, datafield1data)) == -1) {
		fprintf(stderr, "error: cannot read field 'CO Total Column'\n");
		return -1;
	}
	/* Dump data from 'CO Total Column' */
	for (i = 0; i < 15; ++i) {
		for (j = 0; j < 1; ++j) {
			printf("%f ", datafield1data[j + 2 * i]);
		}
		printf("\n");
	}
	/* Release the buffer for 'CO Total Column' */
	free(datafield1data);

	/* Retrieve information about 'Longitude' geolocation field */
	if ((SWfieldinfo(swath1, "Longitude", &geofield1rank, geofield1dimsize, &geofield1type, geofield1dimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for 'Longitude'\n");
		return -1;
	}
	/* Allocate buffer for 'Longitude' */
	if ((geofield1data = malloc(sizeof(float32) * 154649)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for 'Longitude'\n");
		return -1;
	}
	/* Read data from 'Longitude' */
	if ((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 < 15; ++i) {
		printf("%f ", geofield1data[i]);
	}
	printf("\n");
	/* Release the buffer for 'Longitude' */
	free(geofield1data);

	/* Retrieve information about 'Latitude' geolocation field */
	if ((SWfieldinfo(swath1, "Latitude", &geofield2rank, geofield2dimsize, &geofield2type, geofield2dimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for 'Latitude'\n");
		return -1;
	}
	/* Allocate buffer for 'Latitude' */
	if ((geofield2data = malloc(sizeof(float32) * 154649)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for 'Latitude'\n");
		return -1;
	}
	/* Read data from 'Latitude' */
	if ((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 < 15; ++i) {
		printf("%f ", geofield2data[i]);
	}
	printf("\n");
	/* Release the buffer for 'Latitude' */
	free(geofield2data);

	/* Close the swath named 'MOP02' */
	if ((SWdetach(swath1)) == -1) {
		fprintf(stderr, "error: cannot detach from 'MOP02'\n");
		return -1;
	}

	/* Close 'MOP02-20000303-L2V5.7.1.val.hdf' */
	if ((SWclose(swathfile1)) == -1) {
		fprintf(stderr, "error: cannot close swath 'MOP02-20000303-L2V5.7.1.val.hdf'\n");
		return -1;
	}

	return 0;
}
