/* 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 'amsua17_2003.007_02805_2302_0046_GC.eos' using swath API */
	if ((swathfile1 = SWopen("amsua17_2003.007_02805_2302_0046_GC.eos", DFACC_RDONLY)) == -1) {
		fprintf(stderr, "error: cannot open swath 'amsua17_2003.007_02805_2302_0046_GC.eos'\n");
		return -1;
	}

	/* Open a swath named 'Orbit 2805' */
	if ((swath1 = SWattach(swathfile1, "Orbit 2805")) == -1) {
		fprintf(stderr, "error: cannot attach to 'Orbit 2805'\n");
		return -1;
	}

	/* Retrieve information about '23800.37 MHz' datafield */
	if ((SWfieldinfo(swath1, "23800.37 MHz", &datafield1rank, datafield1dimsize, &datafield1type, datafield1dimname)) == -1) {
		fprintf(stderr, "error: cannot get the field info for '23800.37 MHz'\n");
		return -1;
	}
	/* Allocate buffer for '23800.37 MHz' */
	if ((datafield1data = malloc(sizeof(float32) * 783 * 30)) == NULL) {
		fprintf(stderr, "error: cannot allocate memory for '23800.37 MHz'\n");
		return -1;
	}
	/* Read data from '23800.37 MHz' */
	if ((SWreadfield(swath1, "23800.37 MHz", NULL, NULL, NULL, datafield1data)) == -1) {
		fprintf(stderr, "error: cannot read field '23800.37 MHz'\n");
		return -1;
	}
	/* Dump data from '23800.37 MHz' */
	for (i = 0; i < 20; ++i) {
		for (j = 0; j < 2; ++j) {
			printf("%f ", datafield1data[j + 30 * i]);
		}
		printf("\n");
	}
	/* Release the buffer for '23800.37 MHz' */
	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) * 783 * 30)) == 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 < 20; ++i) {
		for (j = 0; j < 2; ++j) {
			printf("%f ", geofield1data[j + 30 * 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) * 783 * 30)) == 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 < 20; ++i) {
		for (j = 0; j < 2; ++j) {
			printf("%f ", geofield2data[j + 30 * i]);
		}
		printf("\n");
	}
	/* Release the buffer for 'Latitude' */
	free(geofield2data);

	/* Close the swath named 'Orbit 2805' */
	if ((SWdetach(swath1)) == -1) {
		fprintf(stderr, "error: cannot detach from 'Orbit 2805'\n");
		return -1;
	}

	/* Close 'amsua17_2003.007_02805_2302_0046_GC.eos' */
	if ((SWclose(swathfile1)) == -1) {
		fprintf(stderr, "error: cannot close swath 'amsua17_2003.007_02805_2302_0046_GC.eos'\n");
		return -1;
	}

	return 0;
}

