/* This example shows how to read a data field in HDF-EOS2 grid data. */ #include #include #include #include #include int main(int argc, char **argv) { int32 gridfile1; int32 grid1; int32 datafield1rank; int32 datafield1dimsize[32]; int32 datafield1type; char datafield1dimname[512]; int16 *datafield1data; int32 i, j; /* Open 'MCD43C4.A2000049.005.2006271205532.hdf' using grid API */ if ((gridfile1 = GDopen("MCD43C4.A2000049.005.2006271205532.hdf", DFACC_RDONLY)) == -1) { fprintf(stderr, "error: cannot open grid 'MCD43C4.A2000049.005.2006271205532.hdf'\n"); return -1; } /* Open a grid named 'MCD_CMG_BRDF_0.05Deg' */ if ((grid1 = GDattach(gridfile1, "MCD_CMG_BRDF_0.05Deg")) == -1) { fprintf(stderr, "error: cannot attach to 'MCD_CMG_BRDF_0.05Deg'\n"); return -1; } /* Retrieve information about 'Nadir_Reflectance_Band1' datafield */ if ((GDfieldinfo(grid1, "Nadir_Reflectance_Band1", &datafield1rank, datafield1dimsize, &datafield1type, datafield1dimname)) == -1) { fprintf(stderr, "error: cannot get the field info for 'Nadir_Reflectance_Band1'\n"); return -1; } /* Allocate buffer for 'Nadir_Reflectance_Band1' */ if ((datafield1data = malloc(sizeof(int16) * 3600 * 7200)) == NULL) { fprintf(stderr, "error: cannot allocate memory for 'Nadir_Reflectance_Band1'\n"); return -1; } /* Read data from 'Nadir_Reflectance_Band1' */ if ((GDreadfield(grid1, "Nadir_Reflectance_Band1", NULL, NULL, NULL, datafield1data)) == -1) { fprintf(stderr, "error: cannot read field 'Nadir_Reflectance_Band1'\n"); return -1; } /* Dump data from 'Nadir_Reflectance_Band1' */ for (i = 0; i < 36; ++i) { for (j = 0; j < 72; ++j) { printf("%d ", datafield1data[j + 7200 * i]); } printf("\n"); } /* Release the buffer for 'Nadir_Reflectance_Band1' */ free(datafield1data); /* Close the grid named 'MCD_CMG_BRDF_0.05Deg' */ if ((GDdetach(grid1)) == -1) { fprintf(stderr, "error: cannot detach from 'MCD_CMG_BRDF_0.05Deg'\n"); return -1; } /* Close 'MCD43C4.A2000049.005.2006271205532.hdf' */ if ((GDclose(gridfile1)) == -1) { fprintf(stderr, "error: cannot close grid 'MCD43C4.A2000049.005.2006271205532.hdf'\n"); return -1; } return 0; }