/* This example shows how to read a data field and two geo-location fields in HDF-EOS2 swath data. */ #include #include #include #include #include int main(int argc, char **argv) { int32 swathfile1; int32 swath1; int32 datafield1rank; int32 datafield1dimsize[32]; int32 datafield1type; char datafield1dimname[512]; int16 *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 'MAC03S0.A2006153.0000.002.2007098161727.hdf' using swath API */ if ((swathfile1 = SWopen("MAC03S0.A2006153.0000.002.2007098161727.hdf", DFACC_RDONLY)) == -1) { fprintf(stderr, "error: cannot open swath 'MAC03S0.A2006153.0000.002.2007098161727.hdf'\n"); return -1; } /* Open a swath named 'MODIS_Swath_Type_GEO' */ if ((swath1 = SWattach(swathfile1, "MODIS_Swath_Type_GEO")) == -1) { fprintf(stderr, "error: cannot attach to 'MODIS_Swath_Type_GEO'\n"); return -1; } /* Retrieve information about 'Height' datafield */ if ((SWfieldinfo(swath1, "Height", &datafield1rank, datafield1dimsize, &datafield1type, datafield1dimname)) == -1) { fprintf(stderr, "error: cannot get the field info for 'Height'\n"); return -1; } /* Allocate buffer for 'Height' */ if ((datafield1data = malloc(sizeof(int16) * 2030 * 11)) == NULL) { fprintf(stderr, "error: cannot allocate memory for 'Height'\n"); return -1; } /* Read data from 'Height' */ if ((SWreadfield(swath1, "Height", NULL, NULL, NULL, datafield1data)) == -1) { fprintf(stderr, "error: cannot read field 'Height'\n"); return -1; } /* Dump data from 'Height' */ for (i = 0; i < 20; ++i) { for (j = 0; j < 2; ++j) { printf("%d ", datafield1data[j + 11 * i]); } printf("\n"); } /* Release the buffer for 'Height' */ 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) * 2030 * 11)) == 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 + 11 * 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) * 2030 * 11)) == 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 + 11 * i]); } printf("\n"); } /* Release the buffer for 'Latitude' */ free(geofield2data); /* Close the swath named 'MODIS_Swath_Type_GEO' */ if ((SWdetach(swath1)) == -1) { fprintf(stderr, "error: cannot detach from 'MODIS_Swath_Type_GEO'\n"); return -1; } /* Close 'MAC03S0.A2006153.0000.002.2007098161727.hdf' */ if ((SWclose(swathfile1)) == -1) { fprintf(stderr, "error: cannot close swath 'MAC03S0.A2006153.0000.002.2007098161727.hdf'\n"); return -1; } return 0; }