Ejemplo n.º 1
0
int main(int argc, char **argv)
{
  FILE *fp;
  meta_parameters *metaSrc, *metaTrg;
  envi_header *envi;
  extern int currArg;          // Pre-initialized to 1
  radiometry_t radiometry=r_AMP;
  filter_type_t filter_type;
  char srcImage[255], trgImage[255], *inFile, outFile[255], filter_str[25];
  int startX_src, startY_src, startX_trg, startY_trg, lines, samples, size;
  int subset=FALSE, filter=FALSE, geotiff=FALSE, line_count, sample_count;
  double lat_UL, lon_UL, lat_LR, lon_LR, yLine, xSample;
  float mean, scale;
  register float *img, *filtered_img=NULL;
  register int x, y, l;

  /* parse command line */
  logflag=quietflag=FALSE;
  while (currArg < (argc-2)) {
    char *key = argv[currArg++];
    if (strmatch(key,"-startX")) {
      CHECK_ARG(1);
      startX_src = atoi(GET_ARG(1));
      subset = TRUE;
    }
    else if (strmatch(key,"-startY")) {
      CHECK_ARG(1);
      startY_src = atoi(GET_ARG(1));
      subset = TRUE;
    }
    else if (strmatch(key,"-lines")) {
      CHECK_ARG(1);
      lines = atoi(GET_ARG(1));
      subset = TRUE;
    }
    else if (strmatch(key,"-samples")) {
      CHECK_ARG(1);
      samples = atoi(GET_ARG(1));
      subset = TRUE;
    }
    else if (strmatch(key,"-filter")) {
      CHECK_ARG(2);
      strcpy(filter_str, GET_ARG(2));
      size = atoi(GET_ARG(1));
      filter = TRUE;
    }
    else if (strmatch(key,"-geotiff")) {
      geotiff = TRUE;
    }
    else {
      printf( "\n**Invalid option:  %s\n", argv[currArg-1]);
      usage(argv[0]);
    }
  }

  if ((argc-currArg)<2) {printf("Insufficient arguments.\n"); usage(argv[0]);}

  strcpy (srcImage, argv[currArg]);
  strcpy (trgImage, argv[currArg+1]);

  asfSplashScreen(argc, argv);

  // Ingesting CEOS files into ASF internal format
  asfPrintStatus("Ingesting source image: %s ...\n", srcImage);
  asf_import(radiometry, FALSE, FALSE, FALSE, "CEOS", "", "geocoded_image", NULL, NULL,
	     -99.0, -99.0, NULL, NULL, NULL, TRUE, NULL, srcImage, "", srcImage);
  metaSrc = meta_read(srcImage);

  asfPrintStatus("Ingesting target image: %s ...\n", trgImage);
  asf_import(radiometry, FALSE, FALSE, FALSE, "CEOS", "", "geocoded_image", NULL, NULL,
	     -99.0, -99.0, NULL, NULL, NULL, TRUE, NULL, trgImage, "", trgImage);
  metaTrg = meta_read(trgImage);

  // Check subset values for source image
  line_count = metaSrc->general->line_count;
  sample_count = metaSrc->general->sample_count;
  if (subset) {
    if (startX_src < 0 || startX_src > sample_count)
      startX_src = 0;
    if (startY_src < 0 || startY_src > line_count)
      startY_src = 0;
    if (lines < 0 || (lines-startY_src) > line_count)
      lines = line_count - startY_src;
    if (samples < 0 || (samples-startX_src) > sample_count)
      samples = sample_count - startX_src;
  }
  else {
    startX_src = startY_src = 0;
    lines = line_count;
    samples = sample_count;
  }

  // Assign filter
  if (filter) {
    if (strcmp(uc(filter_str), "AVERAGE") == 0)
      filter_type = AVERAGE;
    else if (strcmp(uc(filter_str), "SOBEL") == 0)
      filter_type = SOBEL;
    else if (strcmp(uc(filter_str), "FROST") == 0)
      filter_type = FROST;
    else if (strcmp(uc(filter_str), "LEE") == 0)
      filter_type = LEE;
    else if (strcmp(uc(filter_str), "GAMMA_MAP") == 0)
      filter_type = GAMMA_MAP;
    else {
      asfPrintWarning("Unsupported filter type '%s'- ignoring the filter"
		      " settings\n", filter_str);
      filter = FALSE;
    }
    if (size%2 == 0 && filter_type != AVERAGE) {
      size--;
      asfPrintWarning("Filter kernel must have an odd number of lines and"
		      "samples!\n");
    }
  }

  // Allocate some memory for the subsets
  line_count = metaTrg->general->line_count;
  sample_count = metaTrg->general->sample_count;
  img = (float *) MALLOC(sizeof(float)*lines*samples);
  if (filter)
    filtered_img = (float *) MALLOC(sizeof(float)*lines*samples);

  // Determine geographic location of subset
  meta_get_latLon(metaSrc, startY_src, startX_src, 0.0, &lat_UL, &lon_UL);
  meta_get_latLon(metaSrc, startY_src+lines, startX_src+samples, 0.0,
		  &lat_LR, &lon_LR);
  meta_get_lineSamp(metaTrg, lat_UL, lon_UL, 0.0, &yLine, &xSample);
  startX_trg = (int) (xSample + 0.5);
  startY_trg = (int) (yLine + 0.5);

  // READ IN SUBSETS
  // Read target image subset first to determine average brightness
  asfPrintStatus("\nGenerating subset for target image ...\n");
  asfPrintStatus("start line: %d, start sample: %d, lines: %d, samples: %d\n",
		 startY_trg, startX_trg, lines, samples);
  inFile = appendExt(trgImage, ".img");
  sprintf(outFile, "%s_sub.img", trgImage);
  fp = FOPEN(inFile, "rb");
  read_subset(fp, metaTrg, startX_trg, startY_trg, samples, lines, 0.0,
	      &mean, img);
  FCLOSE(fp);

  // Compute scale factor
  mean *= -1;
  scale = 1.0 / (lines * samples);

  // Subtract this average off of target image
  for (y=0; y<lines; y++) {
    l = samples * y;
    for (x=0; x<samples; x++)
      img[l+x] = (img[l+x] + mean) * scale;
  }

  if (filter) {
    asfPrintStatus("\nFiltering target image subset with %s (%dx%d) ...\n",
		   uc(filter_str), size, size);
    filter_image(img, filtered_img, filter_type, size, lines, samples);
  }

  // Update metadata and write target subset to file
  metaTrg->general->line_count = lines;
  metaTrg->general->sample_count = samples;
  metaTrg->general->start_line = startY_trg;
  metaTrg->general->start_sample = startX_trg;
  meta_write(metaTrg, outFile);
  envi = meta2envi(metaTrg);
  write_envi_header(outFile, outFile, metaTrg, envi);

  fp = FOPEN(outFile, "wb");
  if (filter)
    put_float_lines(fp, metaTrg, 0, lines, filtered_img);
  else
    put_float_lines(fp, metaTrg, 0, lines, img);
  FCLOSE(fp);

  // Read source image subset applying for brightness
  asfPrintStatus("\nGenerating subset for source image ...\n");
  asfPrintStatus("start line: %d, start sample: %d, lines: %d, samples: %d\n",
		 startY_src, startX_src, lines, samples);
  inFile = appendExt(srcImage, ".img");
  sprintf(outFile, "%s_sub.img", srcImage);
  fp = FOPEN(inFile, "rb");
  read_subset(fp, metaSrc, startX_src, startY_src, samples, lines, mean,
	      NULL, img);
  FCLOSE(fp);

  if (filter) {
    asfPrintStatus("\nFiltering source image subset with %s (%dx%d) ...\n",
		   uc(filter_str), size, size);
    filter_image(img, filtered_img, filter_type, size, lines, samples);
  }

  // Update metadata and write source subset to file
  metaSrc->general->line_count = lines;
  metaSrc->general->sample_count = samples;
  metaSrc->general->start_line = startY_src;
  metaSrc->general->start_sample = startX_src;
  meta_write(metaSrc, outFile);
  envi = meta2envi(metaSrc);
  write_envi_header(outFile, outFile, metaSrc, envi);

  fp = FOPEN(outFile, "wb");
  if (filter)
    put_float_lines(fp, metaSrc, 0, lines, filtered_img);
  else
    put_float_lines(fp, metaSrc, 0, lines, img);
  FCLOSE(fp);

  // Clean up
  FREE(img);
  meta_free(metaSrc);
  meta_free(metaTrg);

  // Exporting subsets to GeoTIFF
  if (geotiff) {
    output_format_t output_format=GEOTIFF;
    scale_t sample_mapping=SIGMA;

    asfPrintStatus("\nExporting source image subset to GeoTIFF ...\n");
    sprintf(outFile, "%s_sub", srcImage);
    asf_export(output_format, sample_mapping, outFile, outFile);
    asfPrintStatus("\nExporting target image subset to GeoTIFF ...\n");
    sprintf(outFile, "%s_sub", trgImage);
    asf_export(output_format, sample_mapping, outFile, outFile);
  }

  return (0);
}
int gxx_angle_gen_write_image
(
    const char *image_filename, /* I: Image file name */
    const short *azimuth,       /* I: Array of azimuth angles */
    const short *zenith,        /* I: Array of zenith angles */
    gxx_angle_gen_TYPE sat_or_sun_type, /* I: Image type to write */
    int band_index,             /* I: Output band index */
    int band_number,            /* I: Name of the band */
    int num_lines,              /* I: Number of image lines */
    int num_samps,              /* I: Number of image samples */
    DBL_XY ul_corner,           /* I: Image upper left corner */
    double pixel_size,          /* I: Image pixel size */
    const gxx_projection_TYPE *projection /* I: Image framing information */
)        
{
    FILE *output_file;           /* Output file pointer */
    char ang_filename[PATH_MAX]; /* Output angle file name */
    int count;                   /* Total number of samples */
    int status;                  /* Status placeholder */
    char msg[STRLEN];

    /* Check the input band index */
    if (band_index < 0 || band_index > MAXBANDS)
    {
        sprintf(msg, "Invalid band index %d", band_index);
        xxx_LogStatus(PROGRAM, __FILE__, __LINE__, msg);
        return ERROR;
    }

    /* Calculate the total number of samples */
    count = num_lines * num_samps;
    
    if (sat_or_sun_type == GXX_ANGLE_GEN_SATELLITE)
    {
        /* Construct satellite view angle output file name */
        status = snprintf(ang_filename, sizeof(ang_filename), 
            "%s_sensor_B%02d.img", image_filename, band_number);
    }
    else
    {
        /* Construct solar angle output file name */
        status = snprintf(ang_filename, sizeof(ang_filename), 
            "%s_solar_B%02d.img", image_filename, band_number);
    }

    if (status < 0 || (unsigned int)status >= sizeof(ang_filename))
    {
        sprintf(msg, "Error creating the image filename from filename %s", 
            image_filename);
        xxx_LogStatus(PROGRAM, __FILE__, __LINE__, msg);
        return ERROR;
    }

    /* Open output file */
    output_file = fopen(ang_filename, "wb");
    if (!output_file)
    {
        sprintf(msg, "Error opening output view angle band file %s", 
            ang_filename);
        xxx_LogStatus(PROGRAM, __FILE__, __LINE__, msg);
        return ERROR;
    }

    /* Write the Zenith Angle layer */
    if (fwrite(zenith, sizeof(short), count, output_file) 
        != (unsigned int)count)
    {
        sprintf(msg, "Error writing zenith angle layer for band %d to file %s", 
            band_number, ang_filename);
        xxx_LogStatus(PROGRAM, __FILE__, __LINE__, msg);
        fclose(output_file);
        return ERROR;
    }

    /* Write the Azimuth Angle layer */
    if (fwrite(azimuth, sizeof(short), count, output_file) 
        != (unsigned int)count)
    {
        sprintf(msg, "Error writing azimuth angle layer for band %d to file %s",
                band_number, ang_filename);
        xxx_LogStatus(PROGRAM, __FILE__, __LINE__, msg);
        fclose(output_file);
        return ERROR;
    }

    /* Close the output image file */
    if (fclose(output_file) != 0)
    {
        xxx_LogStatus(PROGRAM, __FILE__, __LINE__, 
            "Error closing the image file");
        return ERROR;
    }
   
    /* Write the envi header */
    if (write_envi_header(ang_filename, num_lines, num_samps, ul_corner,
                          pixel_size, projection) != SUCCESS)
    {
        xxx_LogStatus(PROGRAM, __FILE__, __LINE__,
            "Error creating the image header");
        return ERROR;
    }

    return SUCCESS;
}