END_TEST

/* Test routine which writes new-style meta files.  */
START_TEST(test_meta_read_write_new_format)
{
  /* Get something to write.  */
  char in_file[512];
  meta_parameters *meta;
  meta_parameters *meta_reread;	/* For re-read structure.  */

  sprintf(in_file,"%s/test_file_new_style.meta",_TEST_DATA_DIR);
  meta = meta_read(in_file);
  meta_write(meta, "test_output/test_output_file.meta");
  meta_reread = meta_read("test_output/test_output_file.meta");
  
  /* Test a few random or not-so-random fields for equality.  */
  fail_unless(!strcmp(meta->general->sensor, meta_reread->general->sensor),
	      "sensor elements from original and written-then-reread metadata structures don't match");
  fail_unless(meta->projection->type == meta_reread->projection->type,
	      "projection->type fields from original and written-then-reread metadata structures not equal");
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.atct.rlocal,
				       meta_reread->projection
				                    ->param.atct.rlocal),
	      "projection->param.atct.rlocal fields from original and written-then-reread metadata structures not equal");

  if ( unlink("test_output/test_output_file.meta") == -1 ) {
    perror("failed to delete intra-test temporary file test_output_file.meta");
    fail("");
  }
}
Exemple #2
0
void debugWritePatch_Line(int lineNo, complexFloat *line, char *basename,
                          int n_range, int n_az)
{
  FILE *fp;
  meta_parameters *meta;
  char outname[320],name[320];
  char *mode;

  strcpy(outname,g.out);
  strcat(strcat(outname,"_"),basename);
  strcat(strcpy(name,outname),".img");

  if (lineNo == 0) {
      meta = meta_read(g.in1);
      meta->general->line_count = n_range;
      meta->general->sample_count = n_az;
      meta->general->data_type = COMPLEX_REAL32;
      meta->general->image_data_type = COMPLEX_IMAGE;
      meta_write(meta, name);
  } else {
      meta = meta_read(name);
  }

  mode = lineNo == 0 ? "wb" : "ab";
  fp = fopenImage(name,mode);
  put_complexFloat_line(fp, meta, lineNo, line);
  FCLOSE(fp);

  if (lineNo == meta->general->line_count - 1)
    patchToRGBImage(outname, TRUE);

  meta_free(meta);
}
Exemple #3
0
int main(int argc, char *argv[])
{
  // Allocate some memory
  char *inFile = (char *) MALLOC(sizeof(char)*512);
  char *outFile = (char *) MALLOC(sizeof(char)*512);

  // Parse command line
  if (argc < 3) {
    printf("Insufficient arguments.\n"); 
    usage(argv[0]);
  }  
  strcpy(inFile, argv[1]);
  strcpy(outFile, argv[2]);
  int grid_line_count = atoi(argv[3]);

  asfSplashScreen (argc, argv);

  // Deal with metadata
  meta_parameters *metaIn = meta_read(inFile);
  meta_parameters *metaOut = meta_read(inFile);
  int nl = metaIn->general->line_count;
  int ns = metaIn->general->sample_count;
  metaOut->general->data_type = ASF_BYTE;
  meta_write(metaOut, outFile);

  // Replace image with grid
  float on_grid = 0.0;
  float off_grid = 255.0;
  float *outLine = (float *) MALLOC(sizeof(float)*ns);
  float *inLine = (float *) MALLOC(sizeof(float)*ns);

  FILE *fpIn = FOPEN(appendExt(inFile, ".img"), "rb");
  FILE *fpOut = FOPEN(appendExt(outFile, ".img"), "wb");
  int ii, kk;;
  for (ii=0; ii<nl; ii++) {
    get_float_line(fpIn, metaIn, ii, inLine);
    for (kk=0; kk<ns; kk++) {
      if (ii == 0 || ii == nl -1 || ii % (ns/grid_line_count) == 0)
	outLine[kk] = on_grid;
      else if (kk % (ns/grid_line_count) == 0)
	outLine[kk] = on_grid;
      else
	outLine[kk] = off_grid;
    }
    outLine[0] = outLine[ns-1] = on_grid;
    put_float_line(fpOut, metaOut, ii, outLine);
    asfLineMeter(ii, nl);
  }

  // Clean up
  FREE(inFile);
  FREE(outFile);
  meta_free(metaIn);
  meta_free(metaOut);

  exit(0);
}
Exemple #4
0
int
dem_to_mask(char *inDemFile, char *outMaskFile, float cutoff)
{
    meta_parameters *inDemMeta = meta_read(inDemFile);
    meta_parameters *outDemMeta = meta_read(inDemFile);

    // out metadata will differ from in only in the data type
    outDemMeta->general->data_type = ASF_BYTE;

    int x_size = inDemMeta->general->sample_count;
    int y_size = inDemMeta->general->line_count;

    float *maskbuffer = MALLOC(sizeof(float) * x_size);
    float *floatbuffer = MALLOC(sizeof(float) * x_size);

    FILE *in = fopenImage(inDemFile, "rb");
    FILE *out = fopenImage(outMaskFile, "wb");

    float mv = masked_value();
    float umv = unmasked_value();

    int y,x;
    for (y=0; y<y_size; y++) 
    {
        get_float_line(in, inDemMeta, y, floatbuffer);

        for (x=0; x < x_size; x++)            
            maskbuffer[x] = floatbuffer[x] <= cutoff ? mv : umv;

        put_float_line(out, outDemMeta, y, maskbuffer);
        asfLineMeter(y, y_size);
    }

    FCLOSE(in);
    FCLOSE(out);

    FREE(floatbuffer);
    FREE(maskbuffer);

    meta_write(outDemMeta, outMaskFile);

    meta_free(inDemMeta);
    meta_free(outDemMeta);

    asfPrintStatus("Created Mask file '%s' from DEM '%s'.\n",
                   outMaskFile, inDemFile);

    return 0;
}
Exemple #5
0
/*
 * Call fftMatch_proj on projected data, and fftMatch on data that is not
 * projected.
 */
int fftMatch_either(char *inFile1, char *inFile2, float *offsetX,
                    float *offsetY, float *certainty)
{
        meta_parameters *refMeta = meta_read(inFile1);
        meta_parameters *testMeta = meta_read(inFile2);
        if (!refMeta->projection || !testMeta->projection) {
                asfPrintStatus("Data are not map projected.\n");
                return fftMatch(inFile1, inFile2, NULL, offsetX, offsetY,
                                certainty);
        } else {
                asfPrintStatus("Data are map projected.\n");
                return fftMatch_proj(inFile1, inFile2, offsetX, offsetY,
                                certainty);
        }
}
Exemple #6
0
int fftMatch_proj(char *inFile1, char *inFile2, float *offsetX, float *offsetY, float *certainty)
{
  // Determine the offsets based on metadata
  meta_parameters *refMeta = meta_read(inFile1);
  if (!refMeta->projection)
    asfPrintError("File (%s) is not map projected!\n", inFile1);
  meta_parameters *testMeta = meta_read(inFile2);
  if (!testMeta->projection)
    asfPrintError("File (%s) is not map projected!\n", inFile2);
  double testStartX = testMeta->projection->startX + 
    testMeta->general->start_sample*testMeta->projection->perX;
  double testStartY = testMeta->projection->startY +
    testMeta->general->start_line*testMeta->projection->perY;
  double refStartX = refMeta->projection->startX +
    refMeta->general->start_sample*refMeta->projection->perX;
  double refStartY = refMeta->projection->startY +
    refMeta->general->start_line*refMeta->projection->perY;
  float diffX = (testStartX - refStartX) / testMeta->projection->perX;
  float diffY = (testStartY - refStartY) / testMeta->projection->perY;
  meta_free(refMeta);
  meta_free(testMeta);

  // Figure out what FFT parameters are going to be
  int size = 512;
  double tol = -1;
  float diff; 
  if (fabs(diffX) > fabs(diffY))
    diff = fabs(diffX);
  else
    diff = fabs(diffY);
  while ((size/4) < diff) {
    size *= 2; 
  }
  int overlap = size / 2;

  // Determine the offsets based on mapping
  float offX, offY, cert;
  asfPrintStatus("Determing offsets by FFT matching\n\n");
  fftMatch_gridded(inFile1, inFile2, NULL, &offX, &offY, &cert, size, tol, 
    overlap);
  *certainty = cert;

  // Compare both offsets
  *offsetX = diffX - offX;
  *offsetY = diffY - offY;

  return (0);
}
Exemple #7
0
int to_sr_pixsiz(const char *infile, const char *outfile, double pixel_size)
{
   meta_parameters *inMeta = meta_read(infile);
   int ret;

   if (inMeta->sar && inMeta->sar->image_type == 'G') {
       // ground range image
       ret = gr2sr_pixsiz(infile, outfile, pixel_size);
   }
   else if (inMeta->sar && inMeta->sar->image_type == 'S') {
       // already a slant range image, just copy it
       copyImgAndMeta(infile, outfile);
       ret = 0; // success
   }
   else if ((inMeta->sar && inMeta->sar->image_type == 'P') ||
            (inMeta->projection)) {
       // projected image
       ret = proj_to_sr(infile, outfile, pixel_size);
   }
   else if (inMeta->sar && inMeta->transform &&
            inMeta->sar->image_type == 'R') {
       // georeferenced, ALOS most likely
       ret = proj_to_sr(infile, outfile, pixel_size);
   }
   else {
       asfPrintError("Couldn't figure out what kind of image this is.\n");
       ret = 1;
   }

   meta_free(inMeta);
   return ret;
}
Exemple #8
0
void floats_to_bytes_from_file_ext(const char *inFile, const char *outFile,
  char *band, float mask, scale_t scaling, float scale_factor)
{
  FILE *fp;
  meta_parameters *meta;
  float *float_data;
  unsigned char *byte_data;
  int ii, band_number;
  long long pixel_count;
  long offset;

  meta = meta_read(inFile);
  band_number = (!band || strlen(band) == 0 || strcmp(band, "???")==0) ? 0 :
    get_band_number(meta->general->bands, meta->general->band_count, band);
  pixel_count = meta->general->line_count * meta->general->sample_count;
  offset = meta->general->line_count * band_number;
  float_data = (float *) MALLOC(sizeof(float) * pixel_count);
  fp = FOPEN(inFile, "rb");
  get_float_lines(fp, meta, offset, meta->general->line_count, float_data);
  FCLOSE(fp);
  byte_data = floats_to_bytes_ext(float_data, pixel_count, mask, scaling,
    scale_factor);
  for (ii=0; ii<pixel_count; ii++)
    float_data[ii] = (float) byte_data[ii];
  meta->general->data_type = ASF_BYTE;
  meta_write(meta, outFile);
  fp = FOPEN(outFile, "wb");
  put_float_lines(fp, meta, offset, meta->general->line_count, float_data);
  FCLOSE(fp);
  FREE(float_data);
  FREE(byte_data);
  meta_free(meta);
}
Exemple #9
0
meta_parameters *read_asf_meta(const char *meta_name)
{
  meta_parameters *meta = meta_read(meta_name);

  // If the ASF internal format file is a single-band image with RGB color map
  // in the metadata, then store the color map as an ASF style look-up table
  if (meta->colormap) {
    int i;
    meta_colormap *mc = meta->colormap;
    char lut_file[256];
    char *lut_loc = (char *)MALLOC(sizeof(char)*(strlen(get_asf_share_dir())+128));
    sprintf(lut_loc, "%s%clook_up_tables", get_asf_share_dir(), DIR_SEPARATOR);
    sprintf(lut_file,"%s%c%s", lut_loc, DIR_SEPARATOR, EMBEDDED_ASF_COLORMAP_LUT_FILE);
    FILE *lutFP = (FILE *)FOPEN(lut_file, "wt");
    fprintf(lutFP, "# Look up table type: %s\n", mc->look_up_table);
    fprintf(lutFP, "# Originating source: %s\n", meta_name);
    fprintf(lutFP, "# Index   Red   Green   Blue\n");
    for (i=0; i<mc->num_elements; i++) {
      fprintf(lutFP, "%03d    %03d    %03d    %03d\n",
              i, mc->rgb[i].red, mc->rgb[i].green, mc->rgb[i].blue);
    }
    fprintf(lutFP, "\n");
    FCLOSE(lutFP);
  }

  return meta;
}
Exemple #10
0
/******************************************************
 * meta_init_old:
 * Reads in a new meta_parameters record from disk with
 * the given filename.  If no .meta exists, it calls
 * meta_create to construct one.
 * THIS SHOULD EVENTUALLY BE REMOVED AND raw_init SHOULD
 * TAKE ITS NAME   */
meta_parameters *meta_init(const char *fName)
{
  if (extExists(fName,".meta")) /*Read .meta file if possible*/
    return meta_read(fName);
  else
    return meta_create(fName);
}
Exemple #11
0
int main(int argc, char **argv)
{
	char las_name[256];
	char meta_name[256];
	meta_parameters *meta;
	
/* Parse command line */
	currArg=1;
	if (argc-currArg < 2)
		{printf("Insufficient arguments.\n"); usage(argv[0]);}
	if (argc-currArg > 2)
		{printf("Excessive arguments.\n"); usage(argv[0]);}
	strcpy(las_name, argv[currArg]);
	strcpy(meta_name,argv[currArg+1]);
	
/* Read .ddr & .meta file info & put into meta structures */ 
	meta = meta_read(las_name);
	meta->general->image_data_type = IMAGE;

/* write it out new style */
	meta_write(meta, meta_name);

/* Clean and report */
	meta_free(meta);
	printf("***Wrote %s.meta from %s.ddr and %s.meta.\n",
	       meta_name,las_name,las_name);

	return 0;
}
Exemple #12
0
/*
  DebugWritePatch:
  Outputs the current patch trans array, and
  converts it to amplitude and phase.
*/
void debugWritePatch(const patch *p,char *basename)
{
  FILE *fp;
  char name[1024],outname[1024];
  meta_parameters *meta;
  int i;

  strcpy(outname,g.out);
  strcat(strcat(outname,"_"),basename);
  strcat(strcpy(name,outname),".img");


  meta = meta_read(g.in1);
  meta->general->line_count = p->n_range;
  meta->general->sample_count = p->n_az;
  meta->general->data_type = COMPLEX_REAL32;
  meta->general->image_data_type = COMPLEX_IMAGE;
  meta_write(meta, name);

  fp = fopenImage(name,"wb");

  for (i=0; i<p->n_range; ++i)
    put_complexFloat_line(fp, meta, i, p->trans+i*p->n_az);

  FCLOSE(fp);
  meta_free(meta);

  patchToRGBImage(outname, TRUE);
}
END_TEST

/* Test the part of meta_read that parses old files.  */
START_TEST(test_meta_read_old_format)
{
  char in_file[512];
  meta_parameters *meta;
  sprintf(in_file,"%s/test_file_old_style.meta",_TEST_DATA_DIR);
  meta = meta_read(in_file);

  /* Check a random fields to make sure things are working.  */
  fail_unless(meta->sar->azimuth_time_per_pixel == 0.015099817313, "azPixTime field from geo block not read correctly");
  fail_unless(meta->general->line_count == 8262, "nl from ddr read incorrectly");
  
  /* Check a not-so-random field: things from projection params block
     are currently partly holdover from deprecated code and use a
     union.  */
  fail_unless(meta->projection->type == 'P' 
	      && meta->projection->param.ps.slon == -158.3591,
	      "ps_lon field from param->ps block not read correctly");
  
  /* Another not-so-random field check: state vector blocks currently
     use wierd field names in the data file and map strangely into a
     dynamicly allocated internal structure, lots of possibility for
     error.  */
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->state_vectors->vecs[1].vec.pos.y,-359150.94171),
	      "Y position element of second state vector not read correctly");

  meta_free(meta);
}
END_TEST

START_TEST(test_ps) /* Polar Stereo */
{
  char error_string[256];
  char in_file[512];
  meta_parameters *meta;
  sprintf(in_file,"%s/new_style_ps.meta",_TEST_DATA_DIR);
  meta = meta_read(in_file);
  meta_write(meta,"test_output/out_ps.meta");
/* look at a couple fields real quick */
  sprintf(error_string,"meta->sar->dopRangeQuad is %-16.11g, should be 0.0001246",meta->sar->range_doppler_coefficients[2]);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->sar->range_doppler_coefficients[2],0.0001246),error_string);
  sprintf(error_string,"meta->sar->dopAzQuad is %-16.11g, should be 2e-07",meta->sar->azimuth_doppler_coefficients[2]);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->sar->azimuth_doppler_coefficients[2],2e-07),error_string);

/* look at the good stuff */
  sprintf(error_string,"meta->projection->param.ps.slat is %-16.11g, should be 72.374152778",meta->projection->param.ps.slat);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.ps.slat,72.374152778),error_string);
  sprintf(error_string,"meta->geo->proj->param.ps.slat is %-16.11g, should be 72.374152778",meta->geo->proj->param.ps.slat);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->geo->proj->param.ps.slat,72.374152778),error_string);

  sprintf(error_string,"meta->projection->param.ps.slon is %-16.11g, should be -158.3591",meta->projection->param.ps.slon);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.ps.slon,-158.3591),error_string);
  sprintf(error_string,"meta->geo->proj->param.ps.slon is %-16.11g, should be -158.3591",meta->geo->proj->param.ps.slon);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->geo->proj->param.ps.slon,-158.3591),error_string);
  meta_free(meta);
}
Exemple #15
0
static void add_pixels(FloatImage *out, char *file,
                       double start_x, double start_y,
                       double per_x, double per_y)
{
    meta_parameters *meta = meta_read(file);

    if (!meta) {
        asfPrintError("Couldn't read metadata for: %s!\n", file);
    }

    // figure out where in the giant image these pixels will go
    int start_line, start_sample;

    // this should work even if per_x / per_y are negative...
    start_sample = (int) ((meta->projection->startX - start_x) / per_x + .5);
    start_line = (int) ((meta->projection->startY - start_y) / per_y + .5);

    int ns = meta->general->sample_count;
    int nl = meta->general->line_count;

    asfPrintStatus("  Location in combined is S:%d-%d, L:%d-%d\n",
        start_sample, start_sample + ns,
        start_line, start_line + nl);

    if (start_sample + ns > out->size_x || start_line + nl > out->size_y) {
        asfPrintError("Image extents were not calculated correctly!\n");
    }

    FILE *img = fopenImage(file, "rb");
    if (!img) {
        asfPrintError("Couldn't open image file: %s!\n", file);
    }

    float *line = MALLOC(sizeof(float)*ns);

    int y;
    for (y=0; y<nl; ++y) {
        get_float_line(img, meta, y, line);

        int x;
        for (x=0; x<ns; ++x) {
            float v = line[x];

            // don't write out "no data" values
            if (v != meta->general->no_data)
                float_image_set_pixel(out, x + start_sample, y + start_line, v);
        }

        asfLineMeter(y, nl);
    }

    fclose(img);
    free(line);
    meta_free(meta);
}
static void ingest_polarimetry_data(char *inFile, char *inBaseName, 
				    char *outFile, char band, int create)
{
  FILE *fpIn, *fpOut;
  meta_parameters *meta = NULL;
  char tmp[10];
  int ii, kk;
  float *power = NULL;
  char *byteBuf = NULL;
  
  fpIn = FOPEN(inFile, "rb");
  append_ext_if_needed(outFile, ".img", NULL);
  if (create)
    fpOut = FOPEN(outFile, "wb");
  else
    fpOut = FOPEN(outFile, "ab");
  
  if (create) {
    meta = import_airsar_meta(inFile, inBaseName, TRUE);
    meta->general->data_type = REAL32;
    meta->general->band_count = 1;
    sprintf(meta->general->bands, "AMP-%c", band);
    meta->general->image_data_type = IMAGE_LAYER_STACK;
  }
  else {
    meta = meta_read(outFile);
    meta->general->band_count += 1;
    sprintf(tmp, ",AMP-%c", band);
    strcat(meta->general->bands, tmp);
  }      

  power = (float *) MALLOC(sizeof(float)*meta->general->sample_count);
  byteBuf = (char *) MALLOC(sizeof(char)*10);
  airsar_header *header = read_airsar_header(inFile);
  long offset = header->first_data_offset;
  FSEEK(fpIn, offset, SEEK_SET);
  for (ii=0; ii<meta->general->line_count; ii++) {
    for (kk=0; kk<meta->general->sample_count; kk++) {
      FREAD(byteBuf, sizeof(char), 10, fpIn);
      power[kk] = sqrt(((float)byteBuf[1]/254.0 + 1.5) * pow(2, byteBuf[0]));
    }
    put_float_line(fpOut, meta, ii, power);
    asfLineMeter(ii, meta->general->line_count);
  }
  FCLOSE(fpIn);
  FCLOSE(fpOut);
  meta_write(meta, outFile);
  if (power)
    FREE(power);
  if (byteBuf)
    FREE(byteBuf);
  if (meta)
    meta_free(meta);
}
Exemple #17
0
// Convert metadata to text
// NOTE: inFile will either be a leader data file, a geotiff,
// or an ASF metadata file
void meta2text(char *inFile, FILE *outFP)
{
    double ulLong=0.0, urLong=0.0, lrLong=0.0, llLong=0.0; // Clockwise polygon
    double ulLat=0.0, urLat=0.0, lrLat=0.0, llLat=0.0;
    int no_location_info=1;
    meta_parameters *meta = NULL;

    if (isgeotiff(inFile)) {
        int i, ignore[MAX_BANDS];
        for (i=0; i<MAX_BANDS; i++) ignore[i] = 0; // Default to ignoring no bands
        meta = read_generic_geotiff_metadata(inFile, ignore, NULL);
    }
    else if (isleader(inFile)) {
        meta = meta_create(inFile);
    }
    else if (ismetadata(inFile)) {
        meta = meta_read(inFile);
    }
    if (meta && meta->location) {
        meta_location *ml = meta->location; // Convenience pointer
        no_location_info = 0; // false ...location info was found
        ulLong = ml->lon_start_near_range;
        ulLat  = ml->lat_start_near_range;
        urLong = ml->lon_start_far_range;
        urLat  = ml->lat_start_far_range;
        lrLong = ml->lon_end_far_range;
        lrLat  = ml->lat_end_far_range;
        llLong = ml->lon_end_near_range;
        llLat  = ml->lat_end_near_range;
    }
    meta_free(meta);

    if (no_location_info)
      asfPrintWarning("No location coordinates found in %s\n", inFile);
    fprintf(outFP, "# File type        , polygon\n");
    // Use inFile for name ...for lack of a better idea
    fprintf(outFP, "# Polygon ID (name), %s\n", inFile);
    fprintf(outFP, "#\n");
    fprintf(outFP, "# Latitude, Longitude\n");
    if (no_location_info) {
      fprintf(outFP, "# WARNING: No location information found in "
              "source file (%s)\n", inFile);
      fprintf(outFP, "#          Values shown below are invalid\n");
    }
    fprintf(outFP, "%f, %f\n", ulLat, ulLong);
    fprintf(outFP, "%f, %f\n", urLat, urLong);
    fprintf(outFP, "%f, %f\n", lrLat, lrLong);
    fprintf(outFP, "%f, %f\n", llLat, llLong);
    fprintf(outFP, "\n");
    // FCLOSE() is called by the calling function

    return;
}
Exemple #18
0
void loadWrappedPhase(char *f)
{
  FILE * fd;
  meta_parameters *meta;

  fd = FOPEN(f, "rb");
  meta = meta_read(f);
  get_float_lines(fd, meta, 0, len, phase);
  FCLOSE(fd);

  return;
}
Exemple #19
0
bool outOfBounds(int x1, int y1, int srcSize, char *maskFile)
{
  meta_parameters *meta;
  if (x1 - srcSize/2 + 1 < 0) return TRUE;
  if (y1 - srcSize/2 + 1 < 0) return TRUE;
  if (x1 + srcSize/2  >= samples) return TRUE;
  if (y1 + srcSize/2  >= lines) return TRUE;
  if (maskFile) {
    meta = meta_read(maskFile);
    if (!maskFile[x1+meta->general->sample_count*y1]) return TRUE;
  }
  return FALSE;
}
Exemple #20
0
int main (int argc, char *argv[]) {

	const char		*const_str = "ThisStringIsUsedToTestWriteOfMetadata";
	char			*str;
	uint8_t			size, type;
	struct  bitio	*bd;

	bd = bopen("metadata_test.dat", 'w');
	if (bd == NULL)
		goto error;

	meta_write(bd, 1, (void*)const_str, strlen(const_str) + 1);
	meta_write(bd, 2, (void*)const_str, 2);
	meta_finalize(bd);

	bclose(bd);

	bd = bopen("metadata_test.dat", 'r');
	if (bd == NULL)
		goto error;

	str = meta_read(bd, &type, &size);
	if (strcmp(str, const_str) != 0 || type != 1 || size != strlen(str) + 1)
		goto error;

	str = meta_read(bd, &type, &size);
	if (str[0] != const_str[0] || str[1] != const_str[1] || type != 2 || size != 2)
		goto error;

	bclose(bd);
	unlink("metadata_test.dat");
	exit(EXIT_SUCCESS);
error:
	bclose(bd);
	unlink("metadata_test.dat");
	exit(EXIT_FAILURE);
}
Exemple #21
0
int combine(char **infiles, int n_inputs, char *outfile)
{
  int ret, ii, size_x, size_y;
  double start_x, start_y;
  double per_x, per_y;

  // Determine image parameters
  determine_extents(infiles, n_inputs, &size_x, &size_y, &start_x, &start_y,
		    &per_x, &per_y);
  
  asfPrintStatus("\nCombined image size: %dx%d LxS\n", size_y, size_x);
  asfPrintStatus("  Start X,Y: %f,%f\n", start_x, start_y);
  asfPrintStatus("    Per X,Y: %lg,%lg\n", per_x, per_y);
  
  // float_image will handle cacheing of the large output image
  FloatImage *out = float_image_new(size_x, size_y);
  
  // loop over the input images, last to first, so that the files listed
  // first have their pixels overwrite files listed later on the command line
  for (ii=n_inputs-1; ii>=0; ii--) {
    asfPrintStatus("\nProcessing %s... \n", infiles[ii]);
      
    // Add this image's pixels
    add_pixels(out, infiles[ii], start_x, start_y, per_x, per_y);
  }
  
  asfPrintStatus("Writing metadata.\n");
  
  meta_parameters *meta_out = meta_read(infiles[0]);
  
  meta_out->projection->startX = start_x;
  meta_out->projection->startY = start_y;
  meta_out->general->line_count = size_y;
  meta_out->general->sample_count = size_x;
  
  meta_write(meta_out, outfile);
  meta_free(meta_out);

  char *outfile_full = appendExt(outfile, ".img");
  asfPrintStatus("Saving image (%s).\n", outfile_full);
  ret = float_image_store(out, outfile_full, fibo_be);
  if (ret!=0) 
    asfPrintError("Error storing output image!\n");
  float_image_free(out);
  free(outfile_full);
  
  return ret;
}
Exemple #22
0
void 
loadWrappedPhase(char *f)
{
  FILE * fd;
  meta_parameters *meta;

  printf ("   loading wrapped phase...\n");

  fd = FOPEN(f, "rb");
  meta = meta_read(f);
  get_float_lines(fd, meta, 0, len, phase);
  FCLOSE(fd);

  printf("   wrapped phase data loaded...\n");
  return;
}
/* Function 'readSubset' returning a subset of image. The data type of the
   output image is float, regardless what the input data type is. It errors
   on complex data. The function expects to have the memory for the returning 
   image array already allocated. 
*/
void readSubset(char *fileName, int width, int height, int posX, int posY, 
		float *subset)
{
  FILE *fp;
  char dataFileName[255];
  int ii, kk;
  float *buffer;
  meta_parameters *meta = meta_read(fileName);
  int lines = meta->general->line_count;
  int samples = meta->general->sample_count;

  /* Check whether input parameters are feasible */
  assert (width > 0);
  assert (height > 0);
  assert (width < lines);
  assert (height < samples);
  assert (posX > 0);
  assert (posY > 0);
  assert ((posX+width) < samples);
  assert ((posY+height) < lines);
  if (meta->general->data_type > 5)
    printErr("   ERROR: 'readSubset' does not work on complex data!\n");

  /* Allocate memory for buffers */
  buffer = (float *) MALLOC(height*samples*sizeof(float));

  /* Open image file */
  create_name(dataFileName, fileName, ".img");
  fp = FOPEN(dataFileName, "rb");

  /* Read the appropriate data chunk */
  get_float_lines(fp, meta, posY, height, buffer);

  /* Fill in the subset buffer */
  for (ii=0; ii<height; ii++) 
    for (kk=0; kk<width; kk++) 
      subset[ii*width+kk] = buffer[ii*samples+posX+kk];

  /* Clean up */
  FCLOSE(fp);
  FREE(buffer);
  meta_free(meta);
}
static int is_asf_complex_data(const char *meta_file)
{
    char *ext = findExt(meta_file);
    if (ext && strcmp_case(ext, ".meta")==0) {
        meta_parameters *meta = meta_read(meta_file);
        if (meta->general->data_type == COMPLEX_BYTE ||
            meta->general->data_type == COMPLEX_INTEGER16 ||
            meta->general->data_type == COMPLEX_INTEGER32 ||
            meta->general->data_type == COMPLEX_REAL32 ||
            meta->general->data_type == COMPLEX_REAL64)
        {
            meta_free(meta);
            return 1;
        }
        meta_free(meta);
    }

    return 0;
}
Exemple #25
0
// if a file is a .img file, test it for overlap, otherwise do nothing
static void process_file(const char *file, int level,
                         char *overlapping_dems[], int *next_dem_number,
                         meta_parameters *meta, int *n_dems_total)
{
    char *base = get_filename(file);
    char *ext = findExt(base);
    if (ext && strcmp_case(ext, ".img") == 0)
    {
        char *does;
        ++(*n_dems_total);

        char *meta_filename = appendExt(file, ".meta");
        meta_parameters *meta_dem = meta_read(meta_filename);

        if (meta_dem->general->image_data_type != DEM) {
            does = "Not a DEM";
        } else if (test_overlap(meta, meta_dem)) {
            // overlaps!
            overlapping_dems[*next_dem_number] = STRDUP(file);
            ++(*next_dem_number);
            does = "Overlaps";
        } else {
            does = "No";
        }

        free(meta_filename);
        meta_free(meta_dem);

        asfPrintStatus("  %s%s - %s\n", spaces(level), base, does);
    }
    else if (ext && (strcmp_case(ext, ".meta") == 0 ||
                     strcmp_case(ext, ".ddr") == 0)) {
        // silently ignore the .meta, etc files -- they will be picked up
        // when processing the corresponding .img file
        ;
    }
    else {
        // loudly ignore other stuff in the directory
        asfPrintStatus("  %s%s (ignored)\n", spaces(level), base);
    }
    FREE(base);
}
Exemple #26
0
int main(int argc, char **argv)
{
  char meta_name[1024], xml_name[1024];
  meta_parameters *meta=NULL;
  extern int currArg; /* from cla.h in asf.h... initialized to 1 */
  logflag = 0;

  // Parse command line args
  while (currArg < (argc-2))
    {
      char *key=argv[currArg++];
      if (strmatch(key,"-log")) {
    sprintf(logFile, "%s", argv[currArg]);
    logflag = 1;
      }
      else {
    printf("\n   ***Invalid option:  %s\n\n",
           argv[currArg-1]);
    usage(argv[0]);
      }
    }
  if ((argc-currArg) < 2) {
    printf("Insufficient arguments.\n");
    usage(argv[0]);
  }

  create_name(meta_name, argv[currArg], ".meta");
  create_name(xml_name, argv[currArg+1], ".xml");

  asfSplashScreen(argc, argv);

  meta = meta_read(meta_name);
  meta_write_xml(meta, xml_name);

  // Clean and report
  meta_free(meta);
  asfPrintStatus("   Converted metadata file (%s) to XML file (%s)\n\n",
		 meta_name, xml_name);

  return 0;
}
END_TEST

/********************************************************
 * Test all possible projections                       */
START_TEST(test_atct) /* along track cross track */
{
  char error_string[256];
  char in_file[512];
  meta_parameters *meta;
  sprintf(in_file,"%s/new_style_atct.meta",_TEST_DATA_DIR);
  meta = meta_read(in_file);
  meta_write(meta,"test_output/out_atct.meta");
/* look at a couple fields real quick */
  sprintf(error_string,"meta->sar->dopRangeQuad is %-16.11g, should be 0.0001246",meta->sar->range_doppler_coefficients[2]);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->sar->range_doppler_coefficients[2],0.0001246),error_string);
  sprintf(error_string,"meta->sar->dopAzQuad is %-16.11g, should be 2e-07",meta->sar->azimuth_doppler_coefficients[2]);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->sar->azimuth_doppler_coefficients[2],2e-07),error_string);

/* look at the good stuff */
  sprintf(error_string,"meta->projection->param.atct.rlocal is %-16.11g, should be 6000000.0",meta->projection->param.atct.rlocal);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.atct.rlocal,6000000.0),error_string);
  sprintf(error_string,"meta->geo->proj->param.atct.rlocal is %-16.11g, should be 6000000.0",meta->geo->proj->param.atct.rlocal);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->geo->proj->param.atct.rlocal,6000000.0),error_string);

  sprintf(error_string,"meta->projection->param.atct.alpha1 is %-16.11g, should be 0.6",meta->projection->param.atct.alpha1);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.atct.alpha1,0.6),error_string);
  sprintf(error_string,"meta->geo->proj->param.atct.alpha1 is %-16.11g, should be 0.6",meta->geo->proj->param.atct.alpha1);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->geo->proj->param.atct.alpha1,0.6),error_string);

  sprintf(error_string,"meta->projection->param.atct.alpha2 is %-16.11g, should be -0.44",meta->projection->param.atct.alpha2);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.atct.alpha2,-0.44),error_string);
  sprintf(error_string,"meta->geo->proj->param.atct.alpha2 is %-16.11g, should be -0.44",meta->geo->proj->param.atct.alpha2);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->geo->proj->param.atct.alpha2,-0.44),error_string);

  sprintf(error_string,"meta->projection->param.atct.alpha3 is %-16.11g, should be 0.6",meta->projection->param.atct.alpha3);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.atct.alpha3,0.6),error_string);
  sprintf(error_string,"meta->geo->proj->param.atct.alpha3 is %-16.11g, should be 0.6",meta->geo->proj->param.atct.alpha3);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->geo->proj->param.atct.alpha3,0.6),error_string);

  meta_free(meta);
}
Exemple #28
0
END_TEST

START_TEST(test_get_float_line_from_real_star_8)
{
  meta_parameters *meta = meta_read(REAL_STAR_8_META_FILE);
  FILE *image_file_p = FOPEN(REAL_STAR_8_IMG_FILE, "r");
  /* Destination for data read from test file.  */
  float *dest = MALLOC(meta->general->sample_count * sizeof(float));
  int idx;			/* Index for samples.  */
  
  get_float_line(image_file_p, meta, TEST_LINE_NUMBER, dest);
  
  /* Check line data against its expected values.  */
  for ( idx = 0 ; idx < meta->general->sample_count ; idx++ )
    fail_unless(UNIT_TESTS_FLOAT_COMPARE(dest[idx], 
					 FIRST_FLOAT_SAMPLE_TEST_VALUE + idx),
		"unexpected floating point value in fetched line data");

  meta_free(meta);
  FCLOSE(image_file_p);
}
int
banded_float_image_store (BandedFloatImage *self, const char *file,
			  float_image_byte_order_t byte_order)
{
  int ii, retBands=0, ret;
  meta_parameters *meta;
  meta = meta_read(file);

  for (ii=0; ii<self->nbands; ii++) {
    if (ii == 0)
      retBands += float_image_band_store(self->images[0], file, meta, 0);
    else
      retBands += float_image_band_store(self->images[ii], file, meta, 1);
  }
  meta_free(meta);
  if (retBands == self->nbands)
    ret = TRUE;
  else
    ret = FALSE;
  
  return ret;
}
END_TEST

START_TEST(test_utm) /* universal transverse mercator */
{
  char error_string[256];
  char in_file[512];
  meta_parameters *meta;
  sprintf(in_file,"%s/new_style_utm.meta",_TEST_DATA_DIR);
  meta = meta_read(in_file);
  meta_write(meta,"test_output/out_utm.meta");
/* look at a couple fields real quick */
  sprintf(error_string,"meta->sar->dopRangeQuad is %-16.11g, should be 0.0001246",meta->sar->range_doppler_coefficients[2]);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->sar->range_doppler_coefficients[2],0.0001246),error_string);
  sprintf(error_string,"meta->sar->dopAzQuad is %-16.11g, should be 2e-07",meta->sar->azimuth_doppler_coefficients[2]);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->sar->azimuth_doppler_coefficients[2],2e-07),error_string);
/* look at the good stuff */
  sprintf(error_string,"meta->projection->param.utm.zone is %d, should be 6",meta->projection->param.utm.zone);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->projection->param.utm.zone,6),error_string);
  sprintf(error_string,"meta->geo->proj->param.utm.zone is %d, should be 6",meta->geo->proj->param.utm.zone);
  fail_unless(UNIT_TESTS_FLOAT_COMPARE(meta->geo->proj->param.utm.zone,6),error_string);
  meta_free(meta);
}