22 #include <HdfEosDef.h> 24 #define MAX_FILENAME 5000 25 #define MAX_FIELDNAME 1000 26 #define MAX_LENGTH 1000 27 #define GP_GMP_GRID_NAME "GeometricParameters" 28 #define LAND_BRF_FIELD_NAME_NC "Bidirectional_Reflectance_Factor" // For land version 23 and later 29 #define LAND_BRF_GRID_NAME_NC "1.1_KM_PRODUCTS" // For land version 23 and later 30 #define LAND_BRF_FIELD_NAME_HDF "LandBRF" // For land version 22 and earlier 31 #define LAND_BRF_GRID_NAME_HDF "SubregParamsLnd" // For land version 22 and earlier 32 #define AGP_GRID_NAME "Standard" 33 #define AGP_LAND_WATER_ID_FIELD_NAME "SurfaceFeatureID" 35 #define GLITTER_THRESHOLD_DEFAULT 40.0 36 #define MAX_NUMBER_GP_GMP_FIELD 5 38 #define NUMBER_CAMERA 9 39 #define OUTPUT_GRIDNAME "SurfaceBRFRegression" 49 char *output_basename;
58 #define ARGR_TYPE_INIT {NULL, NULL, NULL, NULL, {0}, {0}, GLITTER_THRESHOLD_DEFAULT, -1, 4} 70 int main(
int argc,
char *argv[] ) {
74 char *band_name[
NUMBER_BAND] = {
"Blue",
"Green",
"Red",
"NIR"};
150 int glitter_filter = 0;
163 char *dimlist_ul_lr =
"YDim,XDim";
166 char *dimlist_ur_ll =
"XDim,YDim";
176 int32 tile_dims[2] = {64,64};
178 intn comp_parm[1] = {5};
179 float32 fill_float32 = -9999.0;
180 float32 fill_uint8 = 0;
195 printf(
"\n\nCheck that projection/map info filename is correct: %s\n",argr.
proj_map_file);
210 printf(
"\n\nCheck that AS_LAND filename is correct: %s\n",argr.
land_file);
223 printf(
"\n\nCheck that GRP_TERRAIN filename is correct: %s\n",argr.
terrain_file);
227 camera = attrbuf.
data.
i32[0][0] - 1;
248 dimlist = dimlist_ul_lr;
250 dimlist = dimlist_ur_ll;
256 upleft[0] = target_map_info.
min_x;
257 upleft[1] = target_map_info.
max_y;
258 lowright[0] = target_map_info.
max_x;
259 lowright[1] = target_map_info.
min_y;
261 size_x = (int)floor((target_map_info.
max_x - target_map_info.
min_x) /
263 size_y = (int)floor((target_map_info.
max_y - target_map_info.
min_y) /
267 camera_name[camera]);
284 if (hstatus ==
FAIL) {
289 if (hstatus ==
FAIL) {
296 if (hstatus ==
FAIL) {
352 printf(
"Calculating glitter mask...\n");
358 snprintf(fieldname,
MAX_FIELDNAME,
"%sGlitter",camera_name[camera]);
360 region, &glitter_data, &glitter_map_info);
362 printf(
"\n\nCheck that GP_GMP filename is correct: %s\n",argr.
geom_file);
372 region, &agp_land_water_id_data,
373 &agp_land_water_id_map_info);
375 printf(
"\n\nCheck that AGP filename is correct: %s\n",argr.
agp_file);
386 agp_land_water_id_data.
nsample,
392 for (iline = 0; iline < agp_land_water_id_data.
nline; iline++) {
393 for (isample = 0; isample < agp_land_water_id_data.
nsample; isample++) {
395 glitter_data.
data.
d[iline/16][isample/16] > 0 &&
397 glitter_mask.
data.
u8[iline][isample] = 1;
399 glitter_mask.
data.
u8[iline][isample] = 0;
427 &resampled_glitter_data);
436 snprintf(fieldname,
MAX_FIELDNAME,
"Glitter_Mask_%s",camera_name[camera]);
438 if (hstatus ==
FAIL) {
443 if (hstatus ==
FAIL) {
448 tile_dims, comp_code, comp_parm);
449 if (hstatus ==
FAIL) {
453 hstatus =
GDwritefield(gid, fieldname, NULL, NULL, edge,
454 resampled_glitter_data.
dataptr);
455 if (hstatus ==
FAIL) {
491 if (argr.
band < 0 || iband == argr.
band) {
493 printf(
"Processing band %d (%s)...\n",iband, band_name[iband]);
502 region, &land_brf_data, &land_brf_map_info);
506 region, &land_brf_data, &land_brf_map_info);
522 for (iline = 0; iline < land_brf_data.
nline; iline++) {
523 for (isample = 0; isample < land_brf_data.
nsample; isample++) {
524 if (0 == is_netcdf) {
525 if (land_brf_data.
data.
f[iline][isample] > 65532.0 ||
526 land_brf_data.
data.
f[iline][isample] == 0.0) {
527 land_brf_mask.
data.
u8[iline][isample] = 0;
529 land_brf_mask.
data.
u8[iline][isample] = 1;
532 if (land_brf_data.
data.
f[iline][isample] == -9999 ||
533 land_brf_data.
data.
f[iline][isample] == 0.0) {
534 land_brf_mask.
data.
u8[iline][isample] = 0;
536 land_brf_mask.
data.
u8[iline][isample] = 1;
550 region, &toa_brf_data, &toa_brf_map_info);
561 snprintf(fieldname,
MAX_FIELDNAME,
"%s RDQI",band_name[iband]);
564 region, &toa_rad_rdqi, &toa_rad_rdqi_map_info);
581 for (iline = 0; iline < toa_brf_data.
nline; iline++) {
582 for (isample = 0; isample < toa_brf_data.
nsample; isample++) {
583 if (toa_rad_rdqi.
data.
u8[iline][isample] < 2) {
584 toa_brf_mask.
data.
u8[iline][isample] = 1;
586 toa_brf_mask.
data.
u8[iline][isample] = 0;
608 for (iline = 0; iline < land_brf_data.
nline; iline++) {
609 for (isample = 0; isample < land_brf_data.
nsample; isample++) {
610 land_brf_sigma.
data.
f[iline][isample] = 1.0;
633 if (glitter_filter) {
634 for (iline = 0; iline < toa_brf_data_1100.
nline; iline++) {
635 for (isample = 0; isample < toa_brf_data_1100.
nsample; isample++) {
636 if (glitter_mask.
data.
u8[iline][isample]) {
637 toa_brf_mask_1100.
data.
u8[iline][isample] = 0;
639 toa_brf_mask_1100.
data.
u8[iline][isample] = 1;
657 ®ression_coeff_map_info);
685 regression_coeff.
slope.
data.
f[iline][isample] =
686 smooth_tmp.
data.
f[iline][isample];
705 smooth_tmp.
data.
f[iline][isample];
720 ®ression_coeff_map_info,
740 &land_brf_mask_upsampled);
745 for (iline = 0; iline < regressed_mask.
nline; iline++) {
746 for (isample = 0; isample < regressed_mask.
nsample; isample++) {
747 if (land_brf_mask_upsampled.
data.
u8[iline][isample] == 0) {
748 regressed_mask.
data.
u8[iline][isample] = 0;
773 &resampled_land_brf_data,
774 &resampled_land_brf_mask);
807 &resampled_toa_brf_data,
808 &resampled_toa_brf_mask);
832 &resampled_regressed_brf_data,
833 &resampled_regressed_brf_mask);
854 camera_name[camera], band_name[iband]);
858 if (hstatus ==
FAIL) {
863 if (hstatus ==
FAIL) {
868 tile_dims, comp_code, comp_parm);
869 if (hstatus ==
FAIL) {
873 hstatus =
GDwritefield(gid, fieldname, NULL, NULL, edge,
874 resampled_regressed_brf_data.
dataptr);
875 if (hstatus ==
FAIL) {
884 camera_name[camera], band_name[iband]);
888 if (hstatus ==
FAIL) {
893 if (hstatus ==
FAIL) {
898 tile_dims, comp_code, comp_parm);
899 if (hstatus ==
FAIL) {
903 hstatus =
GDwritefield(gid, fieldname, NULL, NULL, edge,
904 resampled_regressed_brf_mask.
dataptr);
905 if (hstatus ==
FAIL) {
914 camera_name[camera], band_name[iband]);
918 if (hstatus ==
FAIL) {
923 if (hstatus ==
FAIL) {
928 tile_dims, comp_code, comp_parm);
929 if (hstatus ==
FAIL) {
933 hstatus =
GDwritefield(gid, fieldname, NULL, NULL, edge,
934 resampled_land_brf_data.
dataptr);
935 if (hstatus ==
FAIL) {
944 camera_name[camera], band_name[iband]);
947 if (hstatus ==
FAIL) {
952 if (hstatus ==
FAIL) {
957 tile_dims, comp_code, comp_parm);
958 if (hstatus ==
FAIL) {
962 hstatus =
GDwritefield(gid, fieldname, NULL, NULL, edge,
963 resampled_toa_brf_data.
dataptr);
964 if (hstatus ==
FAIL) {
988 if (glitter_filter) {
999 if (hstatus ==
FAIL) {
1005 if (hstatus ==
FAIL) {
1010 printf(
"Wrote output to %s\n",outputfile);
1011 printf(
"Completed normally.\n");
1050 printf(
"Failed: status code = %d\n",status_code);
1057 "\nUsage: %s [--help] [--band=<band number>] [--glitter-threshold=<value>]\n" 1058 " [--agp=<AGP filename> --geom=<GP_GMP filename>]\n" 1059 " <projection/map info file> <AS_LAND file> <GRP_TERRAIN file>\n" 1060 " <output basename>\n",
1065 "Perform Top-of-atmosphere BRF to LandBRF regression and reproject the result to the\n" 1066 "given map projection.\n" 1068 "Result is written to an HDF-EOS file in the following fields:\n" 1069 " Regressed_BRF_<cam>_<band> Final result of the regression.\n" 1070 " Regressed_BRF_Mask_<cam>_<band> Mask indicating valid data.\n" 1071 " TOA_BRF_<cam>_<band> Input TOA BRF from GRP_TERRAIN file (reprojected).\n" 1072 " LandBRF_<cam>_<band> Input LandBRF from AS_LAND file (reprojected).\n" 1073 " Glitter_Mask_<cam> Mask indicating where sun glint is filtered.\n" 1075 "All output fields have identical map projection and size.\n" 1080 "COMMAND-LINE OPTIONS\n" 1083 " Returns this usage info.\n" 1085 "--band=<band number>\n" 1086 " Specifies the band to process. Bands are identified by integer values as\n" 1087 " follows: 0 = blue 1 = green 2 = red 3 = nir\n" 1089 "--agp=<AGP filename>\n" 1090 " Optional parameter, specifying a MISR Ancillary Geographic Parameters\n" 1091 " (AGP) file. If provided, the AGP land/water identifier is used in\n" 1092 " combination with the geometric parameters to filter possible sun glint\n" 1093 " contamination from the top-of-atmosphere BRF. Requires --geom.\n" 1095 "--geom=<GP_GMP filename>\n" 1096 " Optional parameter, specifying a MISR Geometric Parameters file (GP_GMP).\n" 1097 " This is required if --agp is set.\n" 1099 "--glitter-threshold=<value>\n" 1100 " Glitter angle at which a water surface is considered to be glitter\n" 1101 " contaminated in the top-of-atmosphere BRF. Default is 40.0 degrees.\n" 1102 " Glitter angle the angle between a vector from the observed point to\n" 1103 " the camera and a vector pointing in the specular reflection direction.\n" 1104 " Small glitter angles indicate the possibility of observing sun glint.\n" 1106 "--output=<type number>\n" 1107 " Integer dentifier for output file HDF type. Use 4 for HDF4/HDF-EOS2 or use \n" 1108 " 5 for HDF5/HDF-EOS5. Defaults to 4.\n" 1113 "COMMAND-LINE ARGUMENTS\n" 1115 "<projection/map info file>\n" 1116 " Text file specifying map projection for the output. Parameters are\n" 1117 " specified as name = value pairs. Parameter names are as follows:\n" 1119 " proj_code is the GCTP projection code.\n" 1120 " utm_zone is the UTM zone number for UTM projections only.\n" 1121 " sphere_code is GCTP spheroid code.\n" 1122 " proj_param(n) is the nth GCTP projection parameter. (1 <= n <= 15)\n" 1123 " min_corner_x is the minimum X coordinate at the edge of the map.\n" 1124 " min_corner_y is the minimum Y coordinate at the edge of the map.\n" 1125 " resolution_x is the size of a pixel along the X-axis.\n" 1126 " resolution_y is the size of a pixel along the Y-axis.\n" 1127 " number_pixel_x is the size of the map in pixels, along the X-axis.\n" 1128 " number_pixel_y is the size of the map in pixels, along the Y-axis.\n" 1129 " origin_code defines the corner of the map at which pixel 0,0 is located.\n" 1130 " pix_reg_code defines whether a pixel value is related to the corner or\n" 1131 " center of the corresponding area of that pixel on the map. If the\n" 1132 " corner is used, then it is always the corner corresponding to the\n" 1133 " corner of the origin.\n" 1135 " Possible values for origin_code are:\n" 1136 " UL - Upper Left (min X, max Y); Line=Y, Sample=X\n" 1137 " UR - Upper Right (max X, max Y); Line=X, Sample=Y\n" 1138 " LL - Lower Left (min X, min Y); Line=X, Sample=Y\n" 1139 " LR - Lower Right (max X, min Y); Line=Y, Sample=X\n" 1141 " Possible values for pix_reg_code are:\n" 1142 " CENTER - Center\n" 1143 " CORNER - Corner\n" 1145 " Unrecognized parameter names are ignored.\n" 1146 " Lines starting with a '#' character are ignored.\n" 1147 " Anything after the name = value pair on a line is ignored.\n" 1149 " Example projection/map info file:\n" 1150 " # Albers equal-area conic projection parameters\n" 1151 " proj_code = 3 # Albers equal-area conic\n" 1152 " sphere_code = 8 # GRS80\n" 1153 " proj_param(3) = 29030000.0 # Latitude of the first standard parallel\n" 1154 " proj_param(4) = 45030000.0 # Latitude of the second standard parallel\n" 1155 " proj_param(5) = -96000000.0 # Longitude of the central meridian\n" 1156 " proj_param(6) = 23000000.0 # Latitude of the projection origin\n" 1157 " # Map information\n" 1158 " min_corner_x = 1930612.449614\n" 1159 " min_corner_y = 2493633.488881\n" 1160 " resolution_x = 250.0\n" 1161 " resolution_y = 250.0\n" 1162 " number_pixel_x = 1311\n" 1163 " number_pixel_y = 2078\n" 1164 " origin_code = UL\n" 1165 " pix_reg_code = CENTER\n" 1169 " MISR Land Surface parameters (AS_LAND).\n" 1172 " MISR Terrain-projected Radiance Product (GRP_TERRAIN).\n" 1174 "<output basename>\n" 1175 " Basename for output file.\n" 1179 " MtkSurfaceBRFRegression --band=2 proj_map_info.txt \\\n" 1180 " MISR_AM1_AS_LAND_P011_O013824_F06_0019.b52-56.hdf \\\n" 1181 " MISR_AM1_GRP_TERRAIN_GM_P011_O013824_DF_F03_0024.b52-56.hdf \\\n" 1182 " BRF_O013824_maine\n" 1184 " MtkSurfaceBRFRegression \\\n" 1185 " --geom=MISR_AM1_GP_GMP_P011_O013824_F03_0013.hdf \\\n" 1186 " --agp=MISR_AM1_AGP_P011_F01_24.hdf \\\n" 1187 " proj_map_info.txt \\\n" 1188 " MISR_AM1_AS_LAND_P011_O013824_F06_0019.b52-56.hdf \\\n" 1189 " MISR_AM1_GRP_TERRAIN_GM_P011_O013824_DF_F03_0024.b52-56.hdf \\\n" 1190 " BRF_O013824_maine\n" 1192 " MtkSurfaceBRFRegression --glitter-threshold=30.0\\\n" 1193 " --geom=MISR_AM1_GP_GMP_P011_O013824_F03_0013.hdf \\\n" 1194 " --agp=MISR_AM1_AGP_P011_F01_24.hdf \\\n" 1195 " proj_map_info.txt \\\n" 1196 " MISR_AM1_AS_LAND_P011_O013824_F06_0019.b52-56.hdf \\\n" 1197 " MISR_AM1_GRP_TERRAIN_GM_P011_O013824_DF_F03_0024.b52-56.hdf \\\n" 1198 " BRF_O013824_maine\n" 1206 extern char *optarg;
1212 static struct option longopts[] = {
1213 {
"agp", required_argument, 0,
'a' },
1214 {
"geom", required_argument, 0,
'g'},
1215 {
"band", required_argument, 0,
'b' },
1216 {
"help", no_argument, 0,
'h' },
1217 {
"glitter-threshold", required_argument, 0,
't'},
1218 {
"output", required_argument, 0,
'o'},
1222 while ((ch = getopt_long(argc, argv,
"a:g:b:t:hwo:", longopts, NULL)) != -1) {
1235 argr->
band = (int)atol(optarg);
1241 argr->
output = atoi(optarg);
intn GDdefpixreg(int32, int32)
MTKt_status MtkGCTPProjInfoRead(const char *Filename, MTKt_GCTPProjInfo *Proj_info)
Initialize a MTKt_GCTPProjInfo structure using data from an external file.
MTKt_PixRegCode pix_reg_code
#define AGP_LAND_WATER_ID_FIELD_NAME
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
int32 GDattach(int32, char *)
MTKt_status MtkSetRegionByGenericMapInfo(const MTKt_GenericMapInfo *Map_info, const MTKt_GCTPProjInfo *Proj_info, int Path, MTKt_Region *Region)
Create an MtkRegion structure that contains the given map.
MTKt_OriginCode origin_code
#define MTKT_MAPINFO_INIT
#define LAND_BRF_FIELD_NAME_HDF
#define LAND_BRF_FIELD_NAME_NC
MTKt_status MtkRegressionCoeffCalc(const MTKt_DataBuffer *Data1, const MTKt_DataBuffer *Valid_mask1, const MTKt_DataBuffer *Data2, const MTKt_DataBuffer *Data2_sigma, const MTKt_DataBuffer *Valid_mask2, const MTKt_MapInfo *Map_info, int Regression_size_factor, MTKt_RegressionCoeff *Regression_coeff, MTKt_MapInfo *Regression_map_info)
Calculate linear regression coefficients for translating values in data buffer 1 to corresponding val...
GCTP projection information.
MTKt_status MtkTransformCoordinates(MTKt_MapInfo mapinfo, MTKt_DataBuffer latbuf, MTKt_DataBuffer lonbuf, MTKt_DataBuffer *linebuf, MTKt_DataBuffer *samplebuf)
Transforms latitude/longitude coordinates into line/sample coordinates for a given mapinfo...
MTKt_DataBuffer valid_mask
#define MTK_ERR_CODE_JUMP(code)
MTKt_status MtkSmoothData(const MTKt_DataBuffer *Data, const MTKt_DataBuffer *Valid_mask, int Width_line, int Width_sample, MTKt_DataBuffer *Data_smoothed)
Smooth the given array with a boxcar average of the specified width. The algorithm is similar to the ...
intn GDdeforigin(int32, int32)
2-dimensional Data Buffer
MTKt_status MtkApplyRegression(const MTKt_DataBuffer *Source, const MTKt_DataBuffer *Source_mask, const MTKt_MapInfo *Source_map_info, const MTKt_RegressionCoeff *Regression_coeff, const MTKt_MapInfo *Regression_coeff_map_info, MTKt_DataBuffer *Regressed, MTKt_DataBuffer *Regressed_mask)
Apply regression to given data. Uses MtkResampleCubicConvolution to resample regression coefficients ...
#define MTKT_DATABUFFER_INIT
char agp_file[MAX_FILENAME]
int32 GDcreate(int32, char *, int32, int32, float64 [], float64 [])
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_status MtkGCTPCreateLatLon(const MTKt_GenericMapInfo *Map_info, const MTKt_GCTPProjInfo *Proj_info, MTKt_DataBuffer *Latitude, MTKt_DataBuffer *Longitude)
Create an array of latitude and longitude values corresponding to each pixel in the given map...
MTKt_status MtkFileAttrGet(const char *filename, const char *attrname, MTKt_DataBuffer *attrbuf)
Get a file attribute.
MTKt_status MtkResampleCubicConvolution(const MTKt_DataBuffer *Source, const MTKt_DataBuffer *Source_mask, const MTKt_DataBuffer *Line, const MTKt_DataBuffer *Sample, float A, MTKt_DataBuffer *Resampled, MTKt_DataBuffer *Resampled_mask)
Resample source data at the given coordinates using interpolation by cubic convolution.
intn GDdefproj(int32, int32, int32, int32, float64 [])
MTKt_DataBuffer intercept
int process_args(int argc, char *argv[], argr_type *argr)
MTKt_status MtkReadData(const char *filename, const char *gridname, const char *fieldname, MTKt_Region region, MTKt_DataBuffer *databuf, MTKt_MapInfo *mapinfo)
Reads any grid/field from any MISR product file and performs unpacking or unscaling. It also reads any MISR conventional product file.
MTKt_status MtkRegressionCoeffFree(MTKt_RegressionCoeff *regressbuf)
Free memory for regression coefficients.
intn GDsettilecomp(int32, char *, int32, int32 *, int32, intn *)
#define MTK_ERR_MSG_JUMP(msg)
intn GDsetfillvalue(int32, char *, VOIDP)
#define LAND_BRF_GRID_NAME_NC
MTKt_status MtkGenericMapInfoRead(const char *Filename, MTKt_GenericMapInfo *Map_info)
Initialize a MTKt_GenericMapInfo structure using data from an external file.
intn GDdeffield(int32, char *, char *, int32, int32)
#define MTKT_REGRESSION_COEFF_INIT
int main(int argc, char *argv[])
intn GDwritefield(int32, char *, int32 [], int32 [], int32 [], VOIDP)
#define HDFE_COMP_DEFLATE
MTKt_status MtkResampleNearestNeighbor(MTKt_DataBuffer srcbuf, MTKt_DataBuffer linebuf, MTKt_DataBuffer samplebuf, MTKt_DataBuffer *resampbuf)
Perform nearest neighbor resampling.
MTKt_status MtkDownsample(const MTKt_DataBuffer *Source, const MTKt_DataBuffer *Source_mask, int Size_factor, MTKt_DataBuffer *Result, MTKt_DataBuffer *Result_mask)
Downsample data by averaging pixels.
#define MTKT_GENERICMAPINFO_INIT
#define LAND_BRF_GRID_NAME_HDF
int32 GDopen(char *, intn)
#define MTKT_GCTPPROJINFO_INIT
MTKt_status MtkUpsampleMask(const MTKt_DataBuffer *Source_mask, int Size_factor, MTKt_DataBuffer *Result_mask)
Upsample a mask by nearest neighbor sampling.
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
MTKt_status MtkFileToPath(const char *filename, int *path)
Read path number from file.