int main(int argc,char *argv[]) { char infile1[256], infile2[256], infile3[256]; // Input file name char outfile[256]; // Output file name char tmpPath[1024]; browse_type_t mode = NOTYPE; int i,j; int sample_count; double scale; extern int currArg; strcpy(tmpPath, ""); // Parse command line if ((argc-currArg)<1) { printf("Insufficient arguments.\n"); usage(""); } while (currArg < (argc-2)) { char *key = argv[currArg++]; if (strmatches(key, "-sentinel", "--sentinel", NULL)) { CHECK_ARG(1); scale = atof(GET_ARG(1)); mode = SENTINEL_DUAL; } else if (strmatches(key, "-tmpDir", "--tmpDir", NULL)) { CHECK_ARG(1); strcpy(tmpPath, GET_ARG(1)); } else if (strmatches(key, "-log", "--log", NULL)) { CHECK_ARG(1); strcpy(logFile,GET_ARG(1)); fLog = FOPEN(logFile, "a"); logflag = TRUE; } else if (strmatches(key, "-quiet", "--quiet", "-q", NULL)) quietflag = TRUE; else { --currArg; break; } } if ((argc-currArg) < 2) { printf("Insufficient arguments.\n"); usage(argv[0]); } if (mode == NOTYPE && argc == 4) mode = PALSAR_FBD; else if (mode == NOTYPE && argc == 5) mode = PALSAR_PLR; if (!quietflag) asfSplashScreen(argc, argv); if (mode == PALSAR_FBD) { asfPrintStatus("Creating colorized browse image from PALSAR FBD data\n"); create_name(infile1,argv[1],".img"); create_name(infile2,argv[2],".img"); create_name(outfile,argv[3],".img"); meta_parameters *meta1 = meta_read(infile1); meta_parameters *meta2 = meta_read(infile2); if (meta1->general->line_count != meta2->general->line_count || meta1->general->sample_count != meta2->general->sample_count) { asfPrintError("Images must be the same size!!!\n"); exit(1); } strcpy(meta1->general->bands,"HH"); strcpy(meta2->general->bands,"HV"); int pixel_count = meta1->general->line_count*meta1->general->sample_count; float *buf1 = MALLOC(pixel_count * sizeof(float)); float *buf2 = MALLOC(pixel_count * sizeof(float)); float *buf3 = MALLOC(pixel_count * sizeof(float)); unsigned char *cbuf1, *cbuf2, *cbuf3; FILE *fp1 = FOPEN(infile1, "r"); FILE *fp2 = FOPEN(infile2, "r"); FILE *ofp = FOPEN(outfile, "w"); char ofile1[256]; char ofile2[256]; strcpy(ofile1,argv[1]); strcat(ofile1,"_DB.img"); strcpy(ofile2,argv[2]); strcat(ofile2,"_DB.img"); printf("Creating output DB files %s and %s\n",ofile1,ofile2); FILE *ofp1 = FOPEN(ofile1, "w"); FILE *ofp2 = FOPEN(ofile2, "w"); get_float_lines(fp1,meta1,0,meta1->general->line_count, buf1); get_float_lines(fp2,meta2,0,meta2->general->line_count, buf2); /* Convert data from sigma0 to dB */ sample_count = 0; for (i=0; i<meta1->general->line_count; ++i) { for (j=0; j<meta1->general->sample_count; ++j) { if (meta_is_valid_double(buf1[sample_count])) { if (buf1[sample_count] != 0) buf1[sample_count] = 10.0 * log10f(buf1[sample_count]); if (buf2[sample_count] != 0) buf2[sample_count] = 10.0 * log10f(buf2[sample_count]); } sample_count++; } } put_float_lines(ofp1, meta1, 0,meta1->general->line_count,buf1); put_float_lines(ofp2, meta2, 0,meta1->general->line_count,buf2); meta_write(meta1, ofile1); meta_write(meta2, ofile2); fclose(fp1); fclose(fp2); fclose(ofp1); fclose(ofp2); /* Scale the data to a byte range using given min/max values */ cbuf1 = my_floats_to_bytes(buf1,(long long) pixel_count, 0.0,MINMAX,-30.0,-1.0); cbuf2 = my_floats_to_bytes(buf2,(long long) pixel_count, 0.0,MINMAX,-30.0,-10.0); /* cbuf1 = my_floats_to_bytes(buf1,(long long) pixel_count, 0.0,MINMAX,-31.0,7.1); cbuf2 = my_floats_to_bytes(buf2,(long long) pixel_count, 0.0,MINMAX,-31.0,7.1); */ strcpy(ofile1,argv[1]); strcat(ofile1,"_byte.img"); strcpy(ofile2,argv[2]); strcat(ofile2,"_byte.img"); printf("Creating output byte files %s and %s\n",ofile1,ofile2); ofp1 = FOPEN(ofile1, "w"); ofp2 = FOPEN(ofile2, "w"); meta1->general->data_type=REAL32; meta2->general->data_type=REAL32; sample_count = 0; for (i=0; i<meta1->general->line_count; ++i) { for (j=0; j<meta1->general->sample_count; ++j) { buf1[sample_count] = (float) cbuf1[sample_count]; buf2[sample_count] = (float) cbuf2[sample_count]; sample_count++; } } put_float_lines(ofp1,meta1,0,meta1->general->line_count,buf1); put_float_lines(ofp2,meta2,0,meta2->general->line_count,buf2); meta_write(meta1, ofile1); meta_write(meta2, ofile2); fclose(ofp1); fclose(ofp2); /* Create the third band for the color image */ sample_count = 0; for (i=0; i<meta1->general->line_count; ++i) { for (j=0; j<meta1->general->sample_count; ++j) { if (buf2[sample_count] != 0) { /* buf3[sample_count] = (buf1[sample_count] / buf2[sample_count]); */ buf3[sample_count] = (buf1[sample_count] - buf2[sample_count]); if (buf3[sample_count] < 1) buf3[sample_count] = 1; else if (buf3[sample_count] > 255) buf3[sample_count] = 255; } else buf3[sample_count] = 0; sample_count++; } } cbuf3 = my_floats_to_bytes(buf3,(long long) pixel_count, 0.0,SIGMA ,-25.0,-10.0); sample_count = 0; for (i=0; i<meta1->general->line_count; ++i) { for (j=0; j<meta1->general->sample_count; ++j) { buf3[sample_count] = (float) cbuf3[sample_count]; sample_count++; } } /* Finally, create the 3 banded image we were looking for */ strcpy(meta1->general->bands,"HH,HV,DIV"); meta1->general->band_count=3; put_band_float_lines(ofp,meta1,0,0,meta1->general->line_count,buf1); put_band_float_lines(ofp,meta1,1,0,meta1->general->line_count,buf2); put_band_float_lines(ofp,meta1,2,0,meta1->general->line_count,buf3); meta_write(meta1,outfile); } else if (mode == PALSAR_PLR) { /* Mode 1 - Create Color Browse from 3 bands using 3sigma stretch */ asfPrintStatus("Creating colorized browse image from PALSAR PLR data\n"); create_name(infile1,argv[1],".img"); create_name(infile2,argv[2],".img"); create_name(infile3,argv[3],".img"); create_name(outfile,argv[4],".img"); meta_parameters *meta1 = meta_read(infile1); meta_parameters *meta2 = meta_read(infile2); meta_parameters *meta3 = meta_read(infile3); if (meta1->general->line_count != meta2->general->line_count || meta1->general->sample_count != meta2->general->sample_count) { asfPrintError("Images must be the same size!!!\n"); exit(1); } if (meta3->general->line_count != meta2->general->line_count || meta3->general->sample_count != meta2->general->sample_count) { asfPrintError("Images must be the same size!!!\n"); exit(1); } int pixel_count = meta1->general->line_count*meta1->general->sample_count; float *buf1 = MALLOC(pixel_count * sizeof(float)); float *buf2 = MALLOC(pixel_count * sizeof(float)); float *buf3 = MALLOC(pixel_count * sizeof(float)); float *buf4 = MALLOC(pixel_count * sizeof(float)); unsigned char *cbuf1, *cbuf2, *cbuf3, *cbuf4; FILE *fp1 = FOPEN(infile1, "r"); FILE *fp2 = FOPEN(infile2, "r"); FILE *fp3 = FOPEN(infile3, "r"); FILE *ofp = FOPEN(outfile, "w"); get_float_lines(fp1,meta1,0,meta1->general->line_count, buf1); get_float_lines(fp2,meta2,0,meta2->general->line_count, buf2); get_float_lines(fp3,meta3,0,meta3->general->line_count, buf3); /* Convert data from sigma0 to dB */ sample_count = 0; for (i=0; i<meta1->general->line_count; ++i) { for (j=0; j<meta1->general->sample_count; ++j) { if (meta_is_valid_double(buf1[sample_count])) { if (buf1[sample_count] != 0) buf1[sample_count] = 10.0 * log10f(buf1[sample_count]); if (buf2[sample_count] != 0) buf2[sample_count] = 10.0 * log10f(buf2[sample_count]); if (buf3[sample_count] != 0) buf3[sample_count] = 10.0 * log10f(buf3[sample_count]); } sample_count++; } } /* Scale the data to a byte range using 3-sigma stretch values */ cbuf1 = my_floats_to_bytes(buf1,(long long) pixel_count, 0.0,SIGMA3,-30.0,-1.0); cbuf2 = my_floats_to_bytes(buf2,(long long) pixel_count, 0.0,SIGMA3,-30.0,-10.0); cbuf3 = my_floats_to_bytes(buf3,(long long) pixel_count, 0.0,SIGMA3,-30.0,-10.0); meta1->general->data_type=REAL32; //meta2->general->data_type=ASF_BYTE; //meta3->general->data_type=ASF_BYTE; sample_count = 0; for (i=0; i<meta1->general->line_count; ++i) { for (j=0; j<meta1->general->sample_count; ++j) { buf1[sample_count] = (float) cbuf1[sample_count]; buf2[sample_count] = (float) cbuf2[sample_count]; buf3[sample_count] = (float) cbuf3[sample_count]; sample_count++; } } /* Finally, create the 3 banded image we were looking for */ strcpy(meta1->general->bands,"HH,HV,VV"); meta1->general->band_count=3; put_band_float_lines(ofp,meta1,0,0,meta1->general->line_count,buf1); put_band_float_lines(ofp,meta1,1,0,meta1->general->line_count,buf2); put_band_float_lines(ofp,meta1,2,0,meta1->general->line_count,buf3); meta_write(meta1,outfile); } else if (mode == SENTINEL_DUAL) { asfPrintStatus("Creating colorized browse image from Sentinel dual-pol " "data\n"); if (strlen(tmpPath) > 0) { create_name(infile1,argv[5],".img"); create_name(infile2,argv[6],".img"); create_name(outfile,argv[7],".tif"); } else { create_name(infile1,argv[3],".img"); create_name(infile2,argv[4],".img"); create_name(outfile,argv[5],".tif"); } // Create temporary directory char tmpDir[1024]; if (strlen(tmpPath) > 0) sprintf(tmpDir, "%s%cbrowse-", tmpPath, DIR_SEPARATOR); else strcpy(tmpDir, "browse-"); strcat(tmpDir, time_stamp_dir()); create_clean_dir(tmpDir); asfPrintStatus("Temp dir is: %s\n", tmpDir); // Calculate ratio image char tmpRatio[512], tmpRed[512], tmpGreen[512], tmpBlue[512], tmpIn[512]; char *inFiles[2]; inFiles[0] = (char *) MALLOC(sizeof(char)*255); inFiles[1] = (char *) MALLOC(sizeof(char)*255); strcpy(inFiles[0], infile1); strcpy(inFiles[1], infile2); sprintf(tmpRatio, "%s%cdiv.img", tmpDir, DIR_SEPARATOR); raster_calc(tmpRatio, "a/b", 2, inFiles); // Resample all three bands and scale to byte meta_parameters *metaIn = meta_read(tmpRatio); double scaleFactor = 1.0/(scale/metaIn->general->x_pixel_size); meta_free(metaIn); sprintf(tmpIn, "%s%cred.img", tmpDir, DIR_SEPARATOR); resample(infile1, tmpIn, scaleFactor, scaleFactor); sprintf(tmpRed, "%s%cred_byte.img", tmpDir, DIR_SEPARATOR); floats_to_bytes_from_file(tmpIn, tmpRed, NULL, -40.0, SIGMA); sprintf(tmpIn, "%s%cgreen.img", tmpDir, DIR_SEPARATOR); resample(infile2, tmpIn, scaleFactor, scaleFactor); sprintf(tmpGreen, "%s%cgreen_byte.img", tmpDir, DIR_SEPARATOR); floats_to_bytes_from_file(tmpIn, tmpGreen, NULL, -40.0, SIGMA); sprintf(tmpIn, "%s%cblue.img", tmpDir, DIR_SEPARATOR); resample(tmpRatio, tmpIn, scaleFactor, scaleFactor); sprintf(tmpBlue, "%s%cblue_byte.img", tmpDir, DIR_SEPARATOR); floats_to_bytes_from_file(tmpIn, tmpBlue, NULL, -40.0, SIGMA); // Layer stack the bands char tmpBrowse[512]; sprintf(tmpBrowse, "%s%cbrowse.img", tmpDir, DIR_SEPARATOR); FILE *fpOut = FOPEN(tmpBrowse, "w"); meta_parameters *metaOut = meta_read(tmpRed); metaOut->general->band_count = 3; metaIn = meta_read(tmpRed); int line_count = metaIn->general->line_count; int sample_count = metaIn->general->sample_count; float *buf = (float *) MALLOC(sizeof(float)*line_count*sample_count); FILE *fpIn = FOPEN(tmpBlue, "r"); get_float_lines(fpIn, metaIn, 0, line_count, buf); put_band_float_lines(fpOut, metaOut, 0, 0, line_count, buf); FCLOSE(fpIn); fpIn = FOPEN(tmpGreen, "r"); get_float_lines(fpIn, metaIn, 0, line_count, buf); put_band_float_lines(fpOut, metaOut, 1, 0, line_count, buf); FCLOSE(fpIn); fpIn = FOPEN(tmpRed, "r"); get_float_lines(fpIn, metaIn, 0, line_count, buf); put_band_float_lines(fpOut, metaOut, 2, 0, line_count, buf); FCLOSE(fpIn); FCLOSE(fpOut); FREE(buf); strcpy(metaOut->general->bands, "red,green,blue"); meta_write(metaOut, tmpBrowse); // Export to GeoTIFF char *band_names[3] = { "blue", "green", "red" }; asf_export_bands(GEOTIFF, NONE, TRUE, FALSE, FALSE, FALSE, FALSE, tmpBrowse, outfile, band_names, NULL, NULL); // Clean up asfPrintStatus("Removing temporary directory: %s\n", tmpDir); remove_dir(tmpDir); meta_free(metaIn); meta_free(metaOut); } else asfPrintError("Mode is not defined!\n"); asfPrintStatus("Done.\n"); exit(EXIT_SUCCESS); }
int asf_phase_unwrap(char *algorithm, char *interferogram, char *metaFile, char *demFile, char *baseline, double filter_strength, int flattening, char *mask, char *unwrapped_phase) { int ii; char tmp[255]; char **inFiles = (char **) MALLOC(sizeof(char *)*2); for (ii=0; ii<2; ii++) inFiles[ii] = (char *) MALLOC(sizeof(char)*50); // Simulated phase image from DEM asfPrintStatus("\n Simulating a phase image from DEM ...\n\n"); check_return(dem2phase(demFile, baseline, "dem_phase.img"), "creating simulated phase (dem2phase)"); // Deramping and multilooking interferogram asfPrintStatus(" Deramping interferogram ...\n\n"); check_return(deramp(interferogram, baseline, "igramd", 0), "deramping interferogram (deramp)"); asfPrintStatus("\n Multilooking the interferogram ...\n\n"); check_return(multilook("igramd", "ml", "a_cpx.meta", NULL), "multilooking interferogram (multilook)"); // Remove known topographic phase asfPrintStatus("\n Removing known topographic phase ...\n"); if (flattening == 1) { sprintf(inFiles[0], "ml_phase.img"); sprintf(inFiles[1], "dem_phase.img"); check_return(raster_calc("ml_dem_phase.img", "\'(a-b)%6.2831853-3.14159265\' ", 2, inFiles), "subtracting terrain induced phase (raster_calc)"); } // Get rolling on the phase unwrapping if (strncmp(algorithm, "escher", 6)==0) { if (flattening == 1) sprintf(tmp, "ml_dem_phase.img"); else sprintf(tmp, "ml_phase.img"); if (filter_strength < 0.0 || filter_strength > 3.0) { asfPrintWarning("phase filter value out of range - set to value " "of 1.6\n"); filter_strength = 1.6; } if (filter_strength > 0.0) { asfPrintStatus("\n Filtering the phase ...\n\n"); check_return(phase_filter(tmp, filter_strength, "filtered_phase"), "phase filtering (phase_filter)"); sprintf(tmp, "filtered_phase"); } if (strcmp(tmp, "ml_phase.img") != 0) { asfPrintStatus("\n Cleaning up filtering result ...\n"); check_return(zeroify(tmp, "ml_phase.img", "escher_in_phase.img"), "phase value cosmetics (zeroify)"); sprintf(tmp, "escher_in_phase.img"); } if (flattening == 1) { asfPrintStatus(" Performing phase unwrapping ...\n"); check_return(escher(tmp,"unwrap_dem"), "phase unwrapping (escher)"); asfPrintStatus(" Adding known topographic phase again ...\n"); sprintf(inFiles[0], "unwrap_dem.img"); sprintf(inFiles[1], "dem_phase.img"); check_return(raster_calc("unwrap_phase.img", "\'(a+b)*(a/a)*(b/b)\' ", 2, inFiles), "adding terrain induced phase back (raster_calc)"); } else check_return(escher(tmp,"unwrap"), "phase unwrapping (escher)"); asfPrintStatus(" Reramping unwrapped phase ...\n"); check_return(deramp("unwrap", baseline, "unwrap_nod", 1), "reramping unwrapped phase (deramp)"); meta_parameters *m2 = meta_read("unwrap_phase.meta"); m2->general->data_type = BYTE; meta_write(m2, "unwrap_dem_mask.meta"); meta_free(m2); /* char inFile[256]; char outFile[256]; strcpy(inFile, "unwrap_dem_mask"); strcpy(outFile, "unwrap_mask"); asfPrintStatus("Writing unwrapping mask to file ...\n"); check_return(asf_export_with_lut(TIF, TRUNCATE, "unwrapping_mask.lut", inFile, outFile), "colorized phase unwrapping mask (asf_export)"); */ } else if (strncmp(algorithm, "snaphu", 6)==0) { asfPrintError("function still needs to be connected again\n"); /* sprintf(cmd, "make_snaphu_conf %s.phase unwrap.phase", interferogram); asfSystem(cmd); if (cfg->unwrap->tiles_azimuth == 0 || cfg->unwrap->tiles_range == 0) { meta = meta_init("a.meta"); meta_get_latLon(meta, cfg->coreg->master_offset, 1, 0, &lat1, &lon1); meta_get_latLon(meta, cfg->coreg->master_offset, 1, 0, &lat2, &lon1); cfg->unwrap->tiles_azimuth = (int) (fabs(lat1-lat2)*cfg->unwrap->tiles_per_degree); cfg->unwrap->tiles_range = (int) (fabs(cfg->unwrap->tiles_per_degree*0.8)); } if (cfg->unwrap->flattening==1) check_return(snaphu(cfg->unwrap->algorithm, "ml_phase.img", "ml_amp.img", cfg->coreg->master_power, cfg->coreg->slave_power, "snaphu.conf", "unwrap_phase.img", cfg->unwrap->tiles_azimuth, cfg->unwrap->tiles_range, cfg->unwrap->overlap_azimuth, cfg->unwrap->overlap_range, cfg->unwrap->procs, 1), "phase unwrapping (snaphu)"); else check_return(snaphu(cfg->unwrap->algorithm, "ml_phase.img", "ml_amp.img", cfg->coreg->master_power, cfg->coreg->slave_power, "snaphu.conf", "unwrap_phase.img", cfg->unwrap->tiles_azimuth, cfg->unwrap->tiles_range, cfg->unwrap->overlap_azimuth, cfg->unwrap->overlap_range, cfg->unwrap->procs, 0), "phase unwrapping (snaphu)"); link("ml_phase.meta", "unwrap_phase.meta"); */ } for (ii=0; ii<2; ii++) FREE(inFiles[ii]); FREE(inFiles); return(0); }