MISR Toolkit  1.5.1
MtkSurfaceBRFRegression.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkSurfaceBRFRegression =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2005, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrToolkit.h"
18 #include "MisrError.h"
19 #include <stdio.h> /* for printf */
20 #include <getopt.h> /* for getopt_long */
21 #include <strings.h> /* for strncasecmp */
22 #include <HdfEosDef.h> /* Definition of GDopen */
23 
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"
34 #define AGP_LW_LAND 1
35 #define GLITTER_THRESHOLD_DEFAULT 40.0
36 #define MAX_NUMBER_GP_GMP_FIELD 5
37 #define NUMBER_BAND 4
38 #define NUMBER_CAMERA 9
39 #define OUTPUT_GRIDNAME "SurfaceBRFRegression"
40 
41 /* -------------------------------------------------------------------------*/
42 /* Structure to contain command-line arguments. */
43 /* -------------------------------------------------------------------------*/
44 
45 typedef struct {
46  char *proj_map_file; /* Projection/map info file */
47  char *land_file; /* AS_LAND file */
48  char *terrain_file; /* GRP_TERRAIN file*/
49  char *output_basename; /* Output basename*/
50  char agp_file[MAX_FILENAME]; /* AGP file (optional) */
51  char geom_file[MAX_FILENAME]; /* GP_GMP file (optional) */
52  float glitter_threshold; /* Threshold for determining
53  glitter contamination. */
54  int band; /* Band to process. -1 = all */
55  int output; /* 4 to output HDF4/HDF-EOS2, 5 for HDF5/HDF-EOS5 */
56 } argr_type; /* Argument parse result */
57 
58 #define ARGR_TYPE_INIT {NULL, NULL, NULL, NULL, {0}, {0}, GLITTER_THRESHOLD_DEFAULT, -1, 4}
59 
60 /* -------------------------------------------------------------------------*/
61 /* Local function prototypes */
62 /* -------------------------------------------------------------------------*/
63 
64 int process_args(int argc, char *argv[], argr_type *argr);
65 
66 /* -------------------------------------------------------------------------*/
67 /* Main program. */
68 /* -------------------------------------------------------------------------*/
69 
70 int main( int argc, char *argv[] ) {
71  MTKt_status status; /* Return status */
72  MTKt_status status_code = MTK_FAILURE; /* Return code of this function */
73  argr_type argr = ARGR_TYPE_INIT; /* Command-line arguments */
74  char *band_name[NUMBER_BAND] = {"Blue","Green","Red","NIR"};
75  char *camera_name[NUMBER_CAMERA] = {"Df","Cf","Bf","Af","An",
76  "Aa","Ba","Ca","Da"};
78  /* Target map information. */
79  MTKt_GCTPProjInfo target_proj_info = MTKT_GCTPPROJINFO_INIT;
80  /* Target projection information. */
81  MTKt_Region region = MTKT_REGION_INIT; /* SOM region containing target map. */
82  MTKt_DataBuffer land_brf_data = MTKT_DATABUFFER_INIT;
83  /* LandBRF data */
84  MTKt_DataBuffer land_brf_mask = MTKT_DATABUFFER_INIT;
85  /* Valid mask for LandBRF data */
86  MTKt_DataBuffer land_brf_mask_upsampled = MTKT_DATABUFFER_INIT;
87  /* Valid mask for LandBRF data, upsampled to
88  full resolution of terrain data. */
89  MTKt_DataBuffer land_brf_sigma = MTKT_DATABUFFER_INIT;
90  /* Land BRF sigma */
91  MTKt_MapInfo land_brf_map_info = MTKT_MAPINFO_INIT;
92  /* Map information for LandBRF data */
93  MTKt_DataBuffer toa_brf_data = MTKT_DATABUFFER_INIT;
94  /* Top-of-atmosphere BRF data */
95  MTKt_MapInfo toa_rad_rdqi_map_info = MTKT_MAPINFO_INIT;
96  /* Map information for terrain RDQI. */
97  MTKt_DataBuffer toa_rad_rdqi = MTKT_DATABUFFER_INIT;
98  /* Top-of-atmosphere radiance data quality indicator */
99  MTKt_DataBuffer toa_brf_mask = MTKT_DATABUFFER_INIT;
100  /* Valid mask for TOA BRF. */
101  MTKt_DataBuffer toa_brf_data_1100 = MTKT_DATABUFFER_INIT;
102  /* Top-of-atmosphere BRF data at 1100 meters per pixel. */
103  MTKt_DataBuffer toa_brf_mask_1100 = MTKT_DATABUFFER_INIT;
104  /* Valid mask for 1100 meter TOA BRF. */
105  MTKt_MapInfo toa_brf_map_info = MTKT_MAPINFO_INIT;
106  /* Map information for terrain-projected radiance. */
108  regression_coeff = MTKT_REGRESSION_COEFF_INIT;
109  /* Regression coefficients. */
111  /* Temporary buffer to hold smoothed data. */
112  MTKt_MapInfo regression_coeff_map_info = MTKT_MAPINFO_INIT;
113  /* Map info for regression coefficients. */
114  MTKt_DataBuffer regressed_data = MTKT_DATABUFFER_INIT;
115  /* Regression result. */
116  MTKt_DataBuffer regressed_mask = MTKT_DATABUFFER_INIT;
117  /* Valid mask for regression result. */
119  /* Temporary latitude array for reprojection. */
121  /* Temporary longitude array for reprojection. */
122  MTKt_DataBuffer line_coords = MTKT_DATABUFFER_INIT;
123  /* Temporary line coord array for reprojection. */
124  MTKt_DataBuffer sample_coords = MTKT_DATABUFFER_INIT;
125  /* Temporary sample coord array for reprojection. */
126  MTKt_DataBuffer resampled_regressed_brf_data = MTKT_DATABUFFER_INIT;
127  /* Final result, resampled to target map. */
128  MTKt_DataBuffer resampled_regressed_brf_mask = MTKT_DATABUFFER_INIT;
129  /* Valid mask for final result. */
130  MTKt_DataBuffer resampled_land_brf_data = MTKT_DATABUFFER_INIT;
131  /* Input LandBRF, resampled to target map. */
132  MTKt_DataBuffer resampled_land_brf_mask = MTKT_DATABUFFER_INIT;
133  /* Valid mask for final result. */
134  MTKt_DataBuffer resampled_toa_brf_data = MTKT_DATABUFFER_INIT;
135  /* Input TOA BRF, resampled to target map. */
136  MTKt_DataBuffer resampled_toa_brf_mask = MTKT_DATABUFFER_INIT;
137  /* Valid mask for final result. */
138  MTKt_DataBuffer glitter_data = MTKT_DATABUFFER_INIT;
139  /* Glitter angle field. */
140  MTKt_DataBuffer agp_land_water_id_data = MTKT_DATABUFFER_INIT;
141  /* AGP land/water identifier. */
142  MTKt_MapInfo agp_land_water_id_map_info = MTKT_MAPINFO_INIT;
143  /* Map information for AGP land/water id field. */
144  MTKt_DataBuffer glitter_mask = MTKT_DATABUFFER_INIT;
145  /* Mask indicating glitter contaminated locations. */
146  MTKt_MapInfo glitter_map_info = MTKT_MAPINFO_INIT;
147  /* Map information for glitter angle field. */
148  MTKt_DataBuffer resampled_glitter_data = MTKT_DATABUFFER_INIT;
149  /* Glitter mask, resampled to target map. */
150  int glitter_filter = 0; /* Flag indicating if glitter filtering should
151  be done. */
152  int path; /* Orbit path number. */
153  int camera; /* Camera index. */
154  int iband; /* Loop iterator. */
155  int iline; /* Loop iterator. */
156  int isample; /* Loop iterator. */
157  char outputfile[MAX_FILENAME];
158  int32 fid = FAIL; /* HDF-EOS file identifier. */
159  int32 gid = FAIL; /* HDF-EOS grid identifier. */
160  int32 hstatus; /* HDF-EOS status code */
161  float64 upleft[2]; /* min X, max Y corner of target map. */
162  float64 lowright[2]; /* max X, min Y corner of target map. */
163  char *dimlist_ul_lr = "YDim,XDim";
164  /* HDF-EOS dimension list for origin UL or
165  origin LR maps.*/
166  char *dimlist_ur_ll = "XDim,YDim";
167  /* HDF-EOS dimension list for origin UR or
168  origin LL maps.*/
169  char *dimlist; /* HDF-EOS dimension list. */
170  int32 edge[2]; /* Size of HDF-EOS data to write. */
171  char fieldname[MAX_FIELDNAME]; /* Fieldname to read/write. */
172  char gridname[MAX_FIELDNAME]; /* Gridname to read. */
173  int size_x; /* Size of map along X axis. */
174  int size_y; /* Size of map along Y axis. */
175  int32 tile_rank = 2; /* HDF tile rank */
176  int32 tile_dims[2] = {64,64}; /* Tile dimensions. */
177  int32 comp_code = HDFE_COMP_DEFLATE; /* GZIP compression code. */
178  intn comp_parm[1] = {5}; /* GZIP compression level. */
179  float32 fill_float32 = -9999.0; /* Fill value for float32 fields. */
180  float32 fill_uint8 = 0; /* Fill value for uint8 fields. */
181 
182  /* ------------------------------------------------------------------ */
183  /* Parse command-line arguments. */
184  /* ------------------------------------------------------------------ */
185 
186  if (process_args(argc, argv, &argr))
188 
189  /* ------------------------------------------------------------------ */
190  /* Read projection / map information for target area. */
191  /* ------------------------------------------------------------------ */
192 
193  status = MtkGCTPProjInfoRead(argr.proj_map_file, &target_proj_info);
194  if (status != MTK_SUCCESS) {
195  printf("\n\nCheck that projection/map info filename is correct: %s\n",argr.proj_map_file);
196  MTK_ERR_MSG_JUMP("Trouble with MtkGCTPProjInfoRead\n");
197  }
198 
199  status = MtkGenericMapInfoRead(argr.proj_map_file, &target_map_info);
200  if (status != MTK_SUCCESS) {
201  MTK_ERR_MSG_JUMP("Trouble with MtkGenericMapInfoRead\n");
202  }
203 
204  /* ------------------------------------------------------------------ */
205  /* Get orbit path number of input file. */
206  /* ------------------------------------------------------------------ */
207 
208  status = MtkFileToPath(argr.land_file, &path);
209  if (status != MTK_SUCCESS) {
210  printf("\n\nCheck that AS_LAND filename is correct: %s\n",argr.land_file);
211  MTK_ERR_MSG_JUMP("Trouble with MtkFileToPath(land)\n");
212  }
213 
214  /* ------------------------------------------------------------------ */
215  /* Get camera identifier from terrain input file. */
216  /* ------------------------------------------------------------------ */
217 
218  {
220 
221  status = MtkFileAttrGet(argr.terrain_file,"Camera",&attrbuf);
222  if (status != MTK_SUCCESS) {
223  printf("\n\nCheck that GRP_TERRAIN filename is correct: %s\n",argr.terrain_file);
224  MTK_ERR_MSG_JUMP("Trouble with MtkFileAttrGet(terrain_file)\n");
225  }
226 
227  camera = attrbuf.data.i32[0][0] - 1;
228  }
229 
230  /* ------------------------------------------------------------------ */
231  /* Setup SOM region containing the target map. */
232  /* ------------------------------------------------------------------ */
233 
234  status = MtkSetRegionByGenericMapInfo(&target_map_info,
235  &target_proj_info,
236  path,
237  &region);
238  if (status != MTK_SUCCESS) {
239  MTK_ERR_MSG_JUMP("Trouble with MtkSetRegionByGenericMapInfo\n");
240  }
241 
242  /* ------------------------------------------------------------------ */
243  /* Create HDF-EOS file for result. */
244  /* ------------------------------------------------------------------ */
245 
246  if (target_map_info.origin_code == MTKe_ORIGIN_UL ||
247  target_map_info.origin_code == MTKe_ORIGIN_LR) {
248  dimlist = dimlist_ul_lr;
249  } else {
250  dimlist = dimlist_ur_ll;
251  }
252 
253  edge[0] = target_map_info.size_line;
254  edge[1] = target_map_info.size_sample;
255 
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;
260 
261  size_x = (int)floor((target_map_info.max_x - target_map_info.min_x) /
262  target_map_info.resolution_x + 0.5);
263  size_y = (int)floor((target_map_info.max_y - target_map_info.min_y) /
264  target_map_info.resolution_y + 0.5);
265 
266  snprintf(outputfile,MAX_FILENAME,"%s_%s.hdf",argr.output_basename,
267  camera_name[camera]);
268 
269  if (argr.output == 4) {
270  fid = GDopen(outputfile,DFACC_CREATE);
271  if (fid == FAIL) {
272  MTK_ERR_MSG_JUMP("Trouble with GDopen\n");
273  }
274 
275  hstatus = GDcreate(fid,OUTPUT_GRIDNAME,size_x, size_y,
276  upleft, lowright);
277 
278  gid = GDattach(fid, OUTPUT_GRIDNAME);
279  if (gid == FAIL) {
280  MTK_ERR_MSG_JUMP("Trouble with GDattach\n");
281  }
282 
283  hstatus = GDdeforigin(gid,target_map_info.origin_code);
284  if (hstatus == FAIL) {
285  MTK_ERR_MSG_JUMP("Trouble with GDdeforigin\n");
286  }
287 
288  hstatus = GDdefpixreg(gid,target_map_info.pix_reg_code);
289  if (hstatus == FAIL) {
290  MTK_ERR_MSG_JUMP("Trouble with GDdefpixreg\n");
291  }
292 
293  hstatus = GDdefproj(gid,target_proj_info.proj_code,
294  target_proj_info.zone_code, target_proj_info.sphere_code,
295  target_proj_info.proj_param);
296  if (hstatus == FAIL) {
297  MTK_ERR_MSG_JUMP("Trouble with GDdefproj\n");
298  }
299  } else { /* HDF5 output rather than 4 */
300  /*
301  fid = HE5_GDopen(outputfile,DFACC_CREATE);
302  if (fid == FAIL) {
303  MTK_ERR_MSG_JUMP("Trouble with HE5_GDopen\n");
304  }
305 
306  hstatus = HE5_GDcreate(fid,OUTPUT_GRIDNAME,size_x, size_y,
307  upleft, lowright);
308 
309  gid = HE5_GDattach(fid, OUTPUT_GRIDNAME);
310  if (gid == FAIL) {
311  MTK_ERR_MSG_JUMP("Trouble with HE5_GDattach\n");
312  }
313 
314  hstatus = HE5_GDdeforigin(gid,target_map_info.origin_code);
315  if (hstatus == FAIL) {
316  MTK_ERR_MSG_JUMP("Trouble with HE5_GDdeforigin\n");
317  }
318 
319  hstatus = HE5_GDdefpixreg(gid,target_map_info.pix_reg_code);
320  if (hstatus == FAIL) {
321  MTK_ERR_MSG_JUMP("Trouble with HE5_GDdefpixreg\n");
322  }
323 
324  hstatus = HE5_GDdefproj(gid,target_proj_info.proj_code,
325  target_proj_info.zone_code, target_proj_info.sphere_code,
326  target_proj_info.proj_param);
327  if (hstatus == FAIL) {
328  MTK_ERR_MSG_JUMP("Trouble with HE5_GDdefproj\n");
329  } */
330  }
331 
332 
333  /* ------------------------------------------------------------------ */
334  /* Calculate pixel lat/lon locations for target map. */
335  /* ------------------------------------------------------------------ */
336 
337  status = MtkGCTPCreateLatLon(&target_map_info,
338  &target_proj_info,
339  &latitude,
340  &longitude);
341  if (status != MTK_SUCCESS) {
342  MTK_ERR_MSG_JUMP("Trouble with MtkGCTPCreateLatLon\n");
343  }
344 
345  /* ------------------------------------------------------------------ */
346  /* If AGP and geometric parameters are provided... */
347  /* ------------------------------------------------------------------ */
348 
349  if (argr.agp_file[0] && argr.geom_file[0]) {
350  glitter_filter = 1;
351 
352  printf("Calculating glitter mask...\n");
353 
354  /* ------------------------------------------------------------------ */
355  /* Read glitter angle for the target area. */
356  /* ------------------------------------------------------------------ */
357 
358  snprintf(fieldname,MAX_FIELDNAME,"%sGlitter",camera_name[camera]);
359  status = MtkReadData(argr.geom_file, GP_GMP_GRID_NAME, fieldname,
360  region, &glitter_data, &glitter_map_info);
361  if (status != MTK_SUCCESS) {
362  printf("\n\nCheck that GP_GMP filename is correct: %s\n",argr.geom_file);
363  MTK_ERR_MSG_JUMP("Trouble with MtkReadData(geom)\n");
364  }
365 
366  /* ------------------------------------------------------------------ */
367  /* Read AGP land/water identifier for the target area. */
368  /* ------------------------------------------------------------------ */
369 
370  status = MtkReadData(argr.agp_file, AGP_GRID_NAME,
372  region, &agp_land_water_id_data,
373  &agp_land_water_id_map_info);
374  if (status != MTK_SUCCESS) {
375  printf("\n\nCheck that AGP filename is correct: %s\n",argr.agp_file);
376  MTK_ERR_MSG_JUMP("Trouble with MtkReadData(agp)\n");
377  }
378 
379  /* ------------------------------------------------------------------ */
380  /* Generate glitter mask at 1.1 km. */
381  /* Glitter mask is set to 1 where location is glitter contaminated. */
382  /* Otherwise set to 0. */
383  /* ------------------------------------------------------------------ */
384 
385  status = MtkDataBufferAllocate(agp_land_water_id_data.nline,
386  agp_land_water_id_data.nsample,
387  MTKe_uint8, &glitter_mask);
388  if (status != MTK_SUCCESS) {
389  MTK_ERR_MSG_JUMP("Trouble with MtkDataBufferAllocate(glitter_mask)\n");
390  }
391 
392  for (iline = 0; iline < agp_land_water_id_data.nline; iline++) {
393  for (isample = 0; isample < agp_land_water_id_data.nsample; isample++) {
394  if (agp_land_water_id_data.data.u8[iline][isample] != AGP_LW_LAND &&
395  glitter_data.data.d[iline/16][isample/16] > 0 &&
396  glitter_data.data.d[iline/16][isample/16] < argr.glitter_threshold) {
397  glitter_mask.data.u8[iline][isample] = 1;
398  } else {
399  glitter_mask.data.u8[iline][isample] = 0;
400  }
401  }
402  }
403 
404  /* ------------------------------------------------------------------ */
405  /* Free memory. */
406  /* ------------------------------------------------------------------ */
407 
408  MtkDataBufferFree(&agp_land_water_id_data);
409  MtkDataBufferFree(&glitter_data);
410 
411  /* ------------------------------------------------------------------ */
412  /* Reproject data to the target map. */
413  /* ------------------------------------------------------------------ */
414 
415  status = MtkTransformCoordinates(agp_land_water_id_map_info,
416  latitude,
417  longitude,
418  &line_coords,
419  &sample_coords);
420  if (status != MTK_SUCCESS) {
421  MTK_ERR_MSG_JUMP("Trouble with MtkTransformCoordinates\n");
422  }
423 
424  status = MtkResampleNearestNeighbor(glitter_mask,
425  line_coords,
426  sample_coords,
427  &resampled_glitter_data);
428  if (status != MTK_SUCCESS) {
429  MTK_ERR_MSG_JUMP("Trouble with MtkResampleCubicConvolution\n");
430  }
431 
432  /* ------------------------------------------------------------------ */
433  /* Write reprojected glitter mask to HDF-EOS file. */
434  /* ------------------------------------------------------------------ */
435 
436  snprintf(fieldname,MAX_FIELDNAME,"Glitter_Mask_%s",camera_name[camera]);
437  hstatus = GDdeffield(gid, fieldname, dimlist, DFNT_UINT8, 0);
438  if (hstatus == FAIL) {
439  MTK_ERR_MSG_JUMP("Trouble with GDdeffield(glitter)\n");
440  }
441 
442  hstatus = GDsetfillvalue(gid, fieldname, &fill_uint8);
443  if (hstatus == FAIL) {
444  MTK_ERR_MSG_JUMP("Trouble with GDsetfillvalue()\n");
445  }
446 
447  hstatus = GDsettilecomp(gid, fieldname, tile_rank,
448  tile_dims, comp_code, comp_parm);
449  if (hstatus == FAIL) {
450  MTK_ERR_MSG_JUMP("Trouble with GDsettilecomp()\n");
451  }
452 
453  hstatus = GDwritefield(gid, fieldname, NULL, NULL, edge,
454  resampled_glitter_data.dataptr);
455  if (hstatus == FAIL) {
456  MTK_ERR_MSG_JUMP("Trouble with GDwritefield(glitter)\n");
457  }
458 
459  /* ------------------------------------------------------------------ */
460  /* Free memory. */
461  /* ------------------------------------------------------------------ */
462 
463  MtkDataBufferFree(&line_coords);
464  MtkDataBufferFree(&sample_coords);
465  MtkDataBufferFree(&resampled_glitter_data);
466 
467  /* ------------------------------------------------------------------ */
468  /* End if AGP and geometric parameters are provided. */
469  /* ------------------------------------------------------------------ */
470 
471  }
472 
473  /* ------------------------------------------------------------------ */
474  /* Detect land file type */
475  /* ------------------------------------------------------------------ */
476 
477  int is_netcdf = 0;
478  {
479  int ncid;
480  int status = nc_open(argr.land_file, NC_NOWRITE, &ncid);
481  if (status == NC_NOERR) {
482  is_netcdf = 1;
483  }
484  }
485 
486  /* ------------------------------------------------------------------ */
487  /* For each band to process... */
488  /* ------------------------------------------------------------------ */
489 
490  for (iband = 0 ; iband < NUMBER_BAND ; iband++) {
491  if (argr.band < 0 || iband == argr.band) {
492 
493  printf("Processing band %d (%s)...\n",iband, band_name[iband]);
494 
495  /* ------------------------------------------------------------------ */
496  /* Read LandBRF for the target area. */
497  /* ------------------------------------------------------------------ */
498 
499  if (is_netcdf) {
500  snprintf(fieldname,MAX_FIELDNAME,"%s[%d][%d]",LAND_BRF_FIELD_NAME_NC,iband,camera);
501  status = MtkReadData(argr.land_file, LAND_BRF_GRID_NAME_NC, fieldname,
502  region, &land_brf_data, &land_brf_map_info); // Try netCDF
503  } else {
504  snprintf(fieldname,MAX_FIELDNAME,"%s[%d][%d]",LAND_BRF_FIELD_NAME_HDF,iband,camera);
505  status = MtkReadData(argr.land_file, LAND_BRF_GRID_NAME_HDF, fieldname,
506  region, &land_brf_data, &land_brf_map_info); // Try HDF
507  }
508  if (status != MTK_SUCCESS) {
509  MTK_ERR_MSG_JUMP("Trouble with MtkReadData(LandBRF)\n");
510  }
511 
512  /* ------------------------------------------------------------------ */
513  /* Generate mask indicating valid LandBRF. */
514  /* ------------------------------------------------------------------ */
515 
516  status = MtkDataBufferAllocate(land_brf_data.nline, land_brf_data.nsample,
517  MTKe_uint8,&land_brf_mask);
518  if (status != MTK_SUCCESS) {
519  MTK_ERR_MSG_JUMP("Trouble with MtkDataBufferAllocate(toa_brf_mask)\n");
520  }
521 
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;
528  } else {
529  land_brf_mask.data.u8[iline][isample] = 1;
530  }
531  } else {
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;
535  } else {
536  land_brf_mask.data.u8[iline][isample] = 1;
537  }
538  }
539  }
540  }
541 
542  /* ------------------------------------------------------------------ */
543  /* Read TOA BRF for the target area. */
544  /* ------------------------------------------------------------------ */
545 
546  snprintf(gridname,MAX_FIELDNAME,"%sBand",band_name[iband]);
547  snprintf(fieldname,MAX_FIELDNAME,"%s BRF",band_name[iband]);
548 
549  status = MtkReadData(argr.terrain_file, gridname, fieldname,
550  region, &toa_brf_data, &toa_brf_map_info);
551  if (status != MTK_SUCCESS) {
552  MTK_ERR_MSG_JUMP("Trouble with MtkReadData(Terrain)\n");
553  }
554 
555  /* ------------------------------------------------------------------ */
556  /* Read terrain-projected radiance data quality indicators for the */
557  /* the target area. */
558  /* ------------------------------------------------------------------ */
559 
560  snprintf(gridname,MAX_FIELDNAME,"%sBand",band_name[iband]);
561  snprintf(fieldname,MAX_FIELDNAME,"%s RDQI",band_name[iband]);
562 
563  status = MtkReadData(argr.terrain_file, gridname, fieldname,
564  region, &toa_rad_rdqi, &toa_rad_rdqi_map_info);
565  if (status != MTK_SUCCESS) {
566  MTK_ERR_MSG_JUMP("Trouble with MtkReadData(Terrain)\n");
567  }
568 
569  /* ------------------------------------------------------------------ */
570  /* Generate mask indicating valid TOA BRF. */
571  /* TOA BRF is considered valid where the terrain-projected radiance */
572  /* data quality is less than 2. */
573  /* ------------------------------------------------------------------ */
574 
575  status = MtkDataBufferAllocate(toa_brf_data.nline, toa_brf_data.nsample,
576  MTKe_uint8,&toa_brf_mask);
577  if (status != MTK_SUCCESS) {
578  MTK_ERR_MSG_JUMP("Trouble with MtkDataBufferAllocate(toa_brf_mask)\n");
579  }
580 
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;
585  } else {
586  toa_brf_mask.data.u8[iline][isample] = 0;
587  }
588  }
589  }
590 
591  /* ------------------------------------------------------------------ */
592  /* Free memory. */
593  /* ------------------------------------------------------------------ */
594 
595  MtkDataBufferFree(&toa_rad_rdqi);
596 
597  /* ------------------------------------------------------------------ */
598  /* Generate Land BRF sigma. */
599  /* Set all to 1.0. This gives equal weight to all LandBRF values. */
600  /* ------------------------------------------------------------------ */
601 
602  status = MtkDataBufferAllocate(land_brf_data.nline, land_brf_data.nsample,
603  MTKe_float,&land_brf_sigma);
604  if (status != MTK_SUCCESS) {
605  MTK_ERR_MSG_JUMP("Trouble with MtkDataBufferAllocate(land_brf_sigma)\n");
606  }
607 
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;
611  }
612  }
613 
614  /* ------------------------------------------------------------------ */
615  /* Downsample terrain data to resolution of LandBRF. */
616  /* ------------------------------------------------------------------ */
617 
618  status = MtkDownsample(&toa_brf_data,
619  &toa_brf_mask,
620  (land_brf_map_info.resolution /
621  toa_brf_map_info.resolution),
622  &toa_brf_data_1100,
623  &toa_brf_mask_1100);
624  if (status != MTK_SUCCESS) {
625  MTK_ERR_MSG_JUMP("Trouble with MtkDownsample\n");
626  }
627 
628  /* ------------------------------------------------------------------ */
629  /* If glitter information is available, filter out locations which */
630  /* are glitter contaminated. */
631  /* ------------------------------------------------------------------ */
632 
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;
638  } else {
639  toa_brf_mask_1100.data.u8[iline][isample] = 1;
640  }
641  }
642  }
643  }
644 
645  /* ------------------------------------------------------------------ */
646  /* Generate regression coefficients, for mapping TOA BRF to LandBRF. */
647  /* ------------------------------------------------------------------ */
648 
649  status = MtkRegressionCoeffCalc(&toa_brf_data_1100,
650  &toa_brf_mask_1100,
651  &land_brf_data,
652  &land_brf_sigma,
653  &land_brf_mask,
654  &land_brf_map_info,
655  16,
656  &regression_coeff,
657  &regression_coeff_map_info);
658  if (status != MTK_SUCCESS) {
659  MTK_ERR_MSG_JUMP("Trouble with MtkRegressionCoeffCalc()\n");
660  }
661 
662  /* ------------------------------------------------------------------ */
663  /* Free memory. */
664  /* ------------------------------------------------------------------ */
665 
666  MtkDataBufferFree(&land_brf_sigma);
667  MtkDataBufferFree(&toa_brf_data_1100);
668  MtkDataBufferFree(&toa_brf_mask_1100);
669 
670  /* ------------------------------------------------------------------ */
671  /* Smooth regression coefficients. */
672  /* ------------------------------------------------------------------ */
673 
674  status = MtkSmoothData(&regression_coeff.slope,
675  &regression_coeff.valid_mask,
676  3, 3,
677  &smooth_tmp);
678  if (status != MTK_SUCCESS) {
679  MTK_ERR_MSG_JUMP("Trouble with MtkSmoothData(slope)\n");
680  }
681 
682  for (iline = 0; iline < regression_coeff.valid_mask.nline; iline++) {
683  for (isample = 0; isample < regression_coeff.valid_mask.nsample; isample++) {
684  if (regression_coeff.valid_mask.data.u8[iline][isample]) {
685  regression_coeff.slope.data.f[iline][isample] =
686  smooth_tmp.data.f[iline][isample];
687  }
688  }
689  }
690 
691  MtkDataBufferFree(&smooth_tmp);
692 
693  status = MtkSmoothData(&regression_coeff.intercept,
694  &regression_coeff.valid_mask,
695  3, 3,
696  &smooth_tmp);
697  if (status != MTK_SUCCESS) {
698  MTK_ERR_MSG_JUMP("Trouble with MtkSmoothData(intercept)\n");
699  }
700 
701  for (iline = 0; iline < regression_coeff.valid_mask.nline; iline++) {
702  for (isample = 0; isample < regression_coeff.valid_mask.nsample; isample++) {
703  if (regression_coeff.valid_mask.data.u8[iline][isample]) {
704  regression_coeff.intercept.data.f[iline][isample] =
705  smooth_tmp.data.f[iline][isample];
706  }
707  }
708  }
709 
710  MtkDataBufferFree(&smooth_tmp);
711 
712  /* ------------------------------------------------------------------ */
713  /* Apply regression to full resolution TOA BRF. */
714  /* ------------------------------------------------------------------ */
715 
716  status = MtkApplyRegression(&toa_brf_data,
717  &toa_brf_mask,
718  &toa_brf_map_info,
719  &regression_coeff,
720  &regression_coeff_map_info,
721  &regressed_data,
722  &regressed_mask);
723  if (status != MTK_SUCCESS) {
724  MTK_ERR_MSG_JUMP("Trouble with MtkApplyRegression\n");
725  }
726 
727  /* ------------------------------------------------------------------ */
728  /* Free memory. */
729  /* ------------------------------------------------------------------ */
730 
731  MtkRegressionCoeffFree(&regression_coeff);
732 
733  /* ------------------------------------------------------------------ */
734  /* Mask out locations where LandBRF is not available. */
735  /* ------------------------------------------------------------------ */
736 
737  status = MtkUpsampleMask(&land_brf_mask,
738  (land_brf_map_info.resolution /
739  toa_brf_map_info.resolution),
740  &land_brf_mask_upsampled);
741  if (status != MTK_SUCCESS) {
742  MTK_ERR_MSG_JUMP("Trouble with MtkUpsampleMask\n");
743  }
744 
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;
749  }
750  }
751  }
752 
753  MtkDataBufferFree(&land_brf_mask_upsampled);
754 
755  /* ------------------------------------------------------------------ */
756  /* Reproject LandBRF to the target map. */
757  /* ------------------------------------------------------------------ */
758 
759  status = MtkTransformCoordinates(land_brf_map_info,
760  latitude,
761  longitude,
762  &line_coords,
763  &sample_coords);
764  if (status != MTK_SUCCESS) {
765  MTK_ERR_MSG_JUMP("Trouble with MtkTransformCoordinates\n");
766  }
767 
768  status = MtkResampleCubicConvolution(&land_brf_data,
769  &land_brf_mask,
770  &line_coords,
771  &sample_coords,
772  -0.5,
773  &resampled_land_brf_data,
774  &resampled_land_brf_mask);
775  if (status != MTK_SUCCESS) {
776  MTK_ERR_MSG_JUMP("Trouble with MtkResampleCubicConvolution\n");
777  }
778 
779  /* ------------------------------------------------------------------ */
780  /* Free memory */
781  /* ------------------------------------------------------------------ */
782 
783  MtkDataBufferFree(&resampled_land_brf_mask);
784  MtkDataBufferFree(&land_brf_data);
785  MtkDataBufferFree(&land_brf_mask);
786  MtkDataBufferFree(&line_coords);
787  MtkDataBufferFree(&sample_coords);
788 
789  /* ------------------------------------------------------------------ */
790  /* Reproject TOA BRF to target map. */
791  /* ------------------------------------------------------------------ */
792 
793  status = MtkTransformCoordinates(toa_brf_map_info,
794  latitude,
795  longitude,
796  &line_coords,
797  &sample_coords);
798  if (status != MTK_SUCCESS) {
799  MTK_ERR_MSG_JUMP("Trouble with MtkTransformCoordinates\n");
800  }
801 
802  status = MtkResampleCubicConvolution(&toa_brf_data,
803  &toa_brf_mask,
804  &line_coords,
805  &sample_coords,
806  -0.5,
807  &resampled_toa_brf_data,
808  &resampled_toa_brf_mask);
809  if (status != MTK_SUCCESS) {
810  MTK_ERR_MSG_JUMP("Trouble with MtkResampleCubicConvolution\n");
811  }
812 
813  /* ------------------------------------------------------------------ */
814  /* Free memory */
815  /* ------------------------------------------------------------------ */
816 
817  MtkDataBufferFree(&resampled_toa_brf_mask);
818  MtkDataBufferFree(&toa_brf_mask);
819  MtkDataBufferFree(&toa_brf_data);
820 
821  /* ------------------------------------------------------------------ */
822  /* Reproject regressed surface BRF values to target map. */
823  /* The regressed data is in the same grid as the TOA BRF, so it uses */
824  /* the same line/sample coordinates as above. */
825  /* ------------------------------------------------------------------ */
826 
827  status = MtkResampleCubicConvolution(&regressed_data,
828  &regressed_mask,
829  &line_coords,
830  &sample_coords,
831  -0.5,
832  &resampled_regressed_brf_data,
833  &resampled_regressed_brf_mask);
834  if (status != MTK_SUCCESS) {
835  MTK_ERR_MSG_JUMP("Trouble with MtkResampleCubicConvolution\n");
836  }
837 
838 
839  /* ------------------------------------------------------------------ */
840  /* Free memory */
841  /* ------------------------------------------------------------------ */
842 
843  MtkDataBufferFree(&regressed_data);
844  MtkDataBufferFree(&regressed_mask);
845 
846  MtkDataBufferFree(&line_coords);
847  MtkDataBufferFree(&sample_coords);
848 
849  /* ------------------------------------------------------------------ */
850  /* Write regressed BRF data to HDF-EOS file. */
851  /* ------------------------------------------------------------------ */
852 
853  snprintf(fieldname,MAX_FIELDNAME,"Regressed_BRF_%s_%s",
854  camera_name[camera], band_name[iband]);
855 
856 
857  hstatus = GDdeffield(gid, fieldname, dimlist, DFNT_FLOAT32, 0);
858  if (hstatus == FAIL) {
859  MTK_ERR_MSG_JUMP("Trouble with GDdeffield\n");
860  }
861 
862  hstatus = GDsetfillvalue(gid, fieldname, &fill_float32);
863  if (hstatus == FAIL) {
864  MTK_ERR_MSG_JUMP("Trouble with GDsetfillvalue()\n");
865  }
866 
867  hstatus = GDsettilecomp(gid, fieldname, tile_rank,
868  tile_dims, comp_code, comp_parm);
869  if (hstatus == FAIL) {
870  MTK_ERR_MSG_JUMP("Trouble with GDsettilecomp()\n");
871  }
872 
873  hstatus = GDwritefield(gid, fieldname, NULL, NULL, edge,
874  resampled_regressed_brf_data.dataptr);
875  if (hstatus == FAIL) {
876  MTK_ERR_MSG_JUMP("Trouble with GDwritefield\n");
877  }
878 
879  /* ------------------------------------------------------------------ */
880  /* Write regressed BRF mask to HDF-EOS file. */
881  /* ------------------------------------------------------------------ */
882 
883  snprintf(fieldname,MAX_FIELDNAME,"Regressed_BRF_Mask_%s_%s",
884  camera_name[camera], band_name[iband]);
885 
886 
887  hstatus = GDdeffield(gid, fieldname, dimlist, DFNT_UINT8, 0);
888  if (hstatus == FAIL) {
889  MTK_ERR_MSG_JUMP("Trouble with GDdeffield\n");
890  }
891 
892  hstatus = GDsetfillvalue(gid, fieldname, &fill_uint8);
893  if (hstatus == FAIL) {
894  MTK_ERR_MSG_JUMP("Trouble with GDsetfillvalue()\n");
895  }
896 
897  hstatus = GDsettilecomp(gid, fieldname, tile_rank,
898  tile_dims, comp_code, comp_parm);
899  if (hstatus == FAIL) {
900  MTK_ERR_MSG_JUMP("Trouble with GDsettilecomp()\n");
901  }
902 
903  hstatus = GDwritefield(gid, fieldname, NULL, NULL, edge,
904  resampled_regressed_brf_mask.dataptr);
905  if (hstatus == FAIL) {
906  MTK_ERR_MSG_JUMP("Trouble with GDwritefield\n");
907  }
908 
909  /* ------------------------------------------------------------------ */
910  /* Write input LandBRF to HDF-EOS file. */
911  /* ------------------------------------------------------------------ */
912 
913  snprintf(fieldname,MAX_FIELDNAME,"LandBRF_%s_%s",
914  camera_name[camera], band_name[iband]);
915 
916 
917  hstatus = GDdeffield(gid, fieldname, dimlist, DFNT_FLOAT32, 0);
918  if (hstatus == FAIL) {
919  MTK_ERR_MSG_JUMP("Trouble with GDdeffield\n");
920  }
921 
922  hstatus = GDsetfillvalue(gid, fieldname, &fill_float32);
923  if (hstatus == FAIL) {
924  MTK_ERR_MSG_JUMP("Trouble with GDsetfillvalue()\n");
925  }
926 
927  hstatus = GDsettilecomp(gid, fieldname, tile_rank,
928  tile_dims, comp_code, comp_parm);
929  if (hstatus == FAIL) {
930  MTK_ERR_MSG_JUMP("Trouble with GDsettilecomp()\n");
931  }
932 
933  hstatus = GDwritefield(gid, fieldname, NULL, NULL, edge,
934  resampled_land_brf_data.dataptr);
935  if (hstatus == FAIL) {
936  MTK_ERR_MSG_JUMP("Trouble with GDwritefield\n");
937  }
938 
939  /* ------------------------------------------------------------------ */
940  /* Write input TOA BRF to HDF-EOS file. */
941  /* ------------------------------------------------------------------ */
942 
943  snprintf(fieldname,MAX_FIELDNAME,"TOA_BRF_%s_%s",
944  camera_name[camera], band_name[iband]);
945 
946  hstatus = GDdeffield(gid, fieldname, dimlist, DFNT_FLOAT32, 0);
947  if (hstatus == FAIL) {
948  MTK_ERR_MSG_JUMP("Trouble with GDdeffield\n");
949  }
950 
951  hstatus = GDsetfillvalue(gid, fieldname, &fill_float32);
952  if (hstatus == FAIL) {
953  MTK_ERR_MSG_JUMP("Trouble with GDsetfillvalue()\n");
954  }
955 
956  hstatus = GDsettilecomp(gid, fieldname, tile_rank,
957  tile_dims, comp_code, comp_parm);
958  if (hstatus == FAIL) {
959  MTK_ERR_MSG_JUMP("Trouble with GDsettilecomp()\n");
960  }
961 
962  hstatus = GDwritefield(gid, fieldname, NULL, NULL, edge,
963  resampled_toa_brf_data.dataptr);
964  if (hstatus == FAIL) {
965  MTK_ERR_MSG_JUMP("Trouble with GDwritefield\n");
966  }
967 
968  /* ------------------------------------------------------------------ */
969  /* Free memory. */
970  /* ------------------------------------------------------------------ */
971 
972  MtkDataBufferFree(&resampled_regressed_brf_data);
973  MtkDataBufferFree(&resampled_regressed_brf_mask);
974  MtkDataBufferFree(&resampled_land_brf_data);
975  MtkDataBufferFree(&resampled_toa_brf_data);
976 
977  /* ------------------------------------------------------------------ */
978  /* End loop for each band to process. */
979  /* ------------------------------------------------------------------ */
980 
981  }
982  }
983 
984  /* ------------------------------------------------------------------ */
985  /* Free memory. */
986  /* ------------------------------------------------------------------ */
987 
988  if (glitter_filter) {
989  MtkDataBufferFree(&glitter_mask);
990  }
991  MtkDataBufferFree(&latitude);
992  MtkDataBufferFree(&longitude);
993 
994  /* ------------------------------------------------------------------ */
995  /* Close HDF-EOS file. */
996  /* ------------------------------------------------------------------ */
997 
998  hstatus = GDdetach(gid);
999  if (hstatus == FAIL) {
1000  MTK_ERR_MSG_JUMP("Trouble with GDdetach\n");
1001  }
1002  gid = FAIL;
1003 
1004  hstatus = GDclose(fid);
1005  if (hstatus == FAIL) {
1006  MTK_ERR_MSG_JUMP("Trouble with GDclose\n");
1007  }
1008  fid = FAIL;
1009 
1010  printf("Wrote output to %s\n",outputfile);
1011  printf("Completed normally.\n");
1012  return 0;
1013 
1014 ERROR_HANDLE:
1015  if (gid != FAIL) {
1016  hstatus = GDdetach(gid);
1017  }
1018  if (fid != FAIL) {
1019  hstatus = GDclose(fid);
1020  }
1021 
1022  MtkDataBufferFree(&glitter_data);
1023  MtkDataBufferFree(&glitter_mask);
1024  MtkDataBufferFree(&agp_land_water_id_data);
1025  MtkDataBufferFree(&resampled_glitter_data);
1026  MtkDataBufferFree(&resampled_toa_brf_mask);
1027  MtkDataBufferFree(&resampled_land_brf_mask);
1028  MtkDataBufferFree(&resampled_toa_brf_data);
1029  MtkDataBufferFree(&resampled_land_brf_data);
1030  MtkDataBufferFree(&resampled_regressed_brf_data);
1031  MtkDataBufferFree(&resampled_regressed_brf_mask);
1032  MtkDataBufferFree(&latitude);
1033  MtkDataBufferFree(&longitude);
1034  MtkDataBufferFree(&regressed_data);
1035  MtkDataBufferFree(&regressed_mask);
1036  MtkDataBufferFree(&smooth_tmp);
1037  MtkRegressionCoeffFree(&regression_coeff);
1038  MtkDataBufferFree(&toa_brf_data_1100);
1039  MtkDataBufferFree(&toa_brf_mask_1100);
1040  MtkDataBufferFree(&toa_brf_mask);
1041  MtkDataBufferFree(&toa_rad_rdqi);
1042  MtkDataBufferFree(&toa_brf_data);
1043  MtkDataBufferFree(&land_brf_sigma);
1044  MtkDataBufferFree(&land_brf_mask_upsampled);
1045  MtkDataBufferFree(&land_brf_mask);
1046  MtkDataBufferFree(&land_brf_data);
1047  MtkDataBufferFree(&line_coords);
1048  MtkDataBufferFree(&sample_coords);
1049 
1050  printf("Failed: status code = %d\n",status_code);
1051 
1052  return status_code;
1053 }
1054 
1055 void usage(char *func) {
1056  fprintf(stderr,
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",
1061  func);
1062 
1063  fprintf(stderr,
1064 "\n"
1065 "Perform Top-of-atmosphere BRF to LandBRF regression and reproject the result to the\n"
1066 "given map projection.\n"
1067 "\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"
1074 "\n"
1075 "All output fields have identical map projection and size.\n"
1076 "\n"
1077  );
1078 
1079  fprintf(stderr,
1080 "COMMAND-LINE OPTIONS\n"
1081 "\n"
1082 "--help\n"
1083 " Returns this usage info.\n"
1084 "\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"
1088 "\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"
1094 "\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"
1098 "\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"
1105 "\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"
1109 "\n"
1110  );
1111 
1112  fprintf(stderr,
1113 "COMMAND-LINE ARGUMENTS\n"
1114 "\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"
1118 "\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"
1134 "\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"
1140 "\n"
1141 " Possible values for pix_reg_code are:\n"
1142 " CENTER - Center\n"
1143 " CORNER - Corner\n"
1144 "\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"
1148 "\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"
1166 "\n"
1167 "\n"
1168 "<AS_LAND file>\n"
1169 " MISR Land Surface parameters (AS_LAND).\n"
1170 "\n"
1171 "<GRP_TERRAIN>\n"
1172 " MISR Terrain-projected Radiance Product (GRP_TERRAIN).\n"
1173 "\n"
1174 "<output basename>\n"
1175 " Basename for output file.\n"
1176 "\n"
1177 "Examples:\n"
1178 "\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"
1183 "\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"
1191 "\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"
1199 "\n"
1200  );
1201 }
1202 
1203 int process_args(int argc, char *argv[], argr_type *argr) {
1204 
1205  MTKt_status status_code = MTK_FAILURE;
1206  extern char *optarg;
1207  extern int optind;
1208  int ch;
1209  argr->band = -1;
1210 
1211  /* options descriptor */
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'},
1219  { 0, 0, 0, 0 }
1220  };
1221 
1222  while ((ch = getopt_long(argc, argv, "a:g:b:t:hwo:", longopts, NULL)) != -1) {
1223 
1224  switch(ch) {
1225  case 'h':
1227  break;
1228  case 'a' :
1229  strncpy(argr->agp_file,optarg,MAX_FILENAME);
1230  break;
1231  case 'g' :
1232  strncpy(argr->geom_file,optarg,MAX_FILENAME);
1233  break;
1234  case 'b' :
1235  argr->band = (int)atol(optarg);
1236  break;
1237  case 't' :
1238  argr->glitter_threshold = atof(optarg);
1239  break;
1240  case 'o' :
1241  argr->output = atoi(optarg);
1242  if (argr->output != 4 && argr->output != 5) {
1243  MTK_ERR_MSG_JUMP("Invalid arguments");
1244  }
1245  break;
1246  default:
1247  MTK_ERR_MSG_JUMP("Invalid arguments");
1248  }
1249  }
1250 
1251  if (argc-optind != 4) MTK_ERR_CODE_JUMP(MTK_OUTBOUNDS);
1252 
1253  argr->proj_map_file = argv[optind++];
1254  argr->land_file = argv[optind++];
1255  argr->terrain_file = argv[optind++];
1256  argr->output_basename = argv[optind++];
1257 
1258  return MTK_SUCCESS;
1259  ERROR_HANDLE:
1260  usage(argv[0]);
1261  return status_code;
1262 }
1263 
intn GDdefpixreg(int32, int32)
MTKt_double ** d
Definition: MisrUtil.h:94
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
Definition: MisrMapQuery.h:116
#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
Definition: MisrMapQuery.h:115
Generic map information.
Definition: MisrMapQuery.h:98
MTKt_DataBufferType data
Definition: MisrUtil.h:104
intn GDclose(int32)
#define NUMBER_CAMERA
#define MTKT_MAPINFO_INIT
Definition: MisrMapQuery.h:79
#define GP_GMP_GRID_NAME
#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...
#define DFNT_UINT8
Definition: hntdefs.h:43
GCTP projection information.
Definition: MisrMapQuery.h:123
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...
#define MTKT_REGION_INIT
Definition: MisrSetRegion.h:46
MTKt_DataBuffer valid_mask
void usage(char *func)
#define ARGR_TYPE_INIT
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
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 ...
Definition: MtkSmoothData.c:36
Map Information.
Definition: MisrMapQuery.h:65
char * terrain_file
intn GDdetach(int32)
intn GDdeforigin(int32, int32)
JCOPY_OPTION option
Definition: transupp.h:131
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_float ** f
Definition: MisrUtil.h:93
MTKt_DataBuffer slope
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 AGP_GRID_NAME
#define NC_NOERR
Definition: netcdf.h:313
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
char agp_file[MAX_FILENAME]
int32 GDcreate(int32, char *, int32, int32, float64 [], float64 [])
void * dataptr
Definition: MisrUtil.h:106
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...
char * geom_file
MTKt_uint8 ** u8
Definition: MisrUtil.h:86
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 [])
char * proj_map_file
MTKt_DataBuffer intercept
int process_args(int argc, char *argv[], argr_type *argr)
#define NUMBER_BAND
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.
Definition: MtkReadData.c:62
MTKt_status MtkRegressionCoeffFree(MTKt_RegressionCoeff *regressbuf)
Free memory for regression coefficients.
double proj_param[15]
Definition: MisrMapQuery.h:127
char * output_basename
intn GDsettilecomp(int32, char *, int32, int32 *, int32, intn *)
#define DFNT_FLOAT32
Definition: hntdefs.h:36
#define MTK_ERR_MSG_JUMP(msg)
Definition: MisrError.h:169
intn GDsetfillvalue(int32, char *, VOIDP)
#define NC_NOWRITE
Definition: netcdf.h:201
#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 OUTPUT_GRIDNAME
#define HDFE_COMP_DEFLATE
Definition: HdfEosDef.h:78
MTKt_int32 ** i32
Definition: MisrUtil.h:89
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.
Definition: MtkDownsample.c:36
#define AGP_LW_LAND
MTKt_status
Definition: MisrError.h:11
#define MTKT_GENERICMAPINFO_INIT
Definition: MisrMapQuery.h:120
#define LAND_BRF_GRID_NAME_HDF
#define MAX_FIELDNAME
Region of interest.
Definition: MisrSetRegion.h:41
int32 GDopen(char *, intn)
#define DFACC_CREATE
Definition: hdf.h:46
#define MTKT_GCTPPROJINFO_INIT
Definition: MisrMapQuery.h:130
#define MAX_FILENAME
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.
Definition: MtkFileToPath.c:35
#define FAIL
Definition: hdf.h:94

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51