/* Function 'readComplexSubset' returning a subset of image. This is the version of 'readSubset' that handles complex data. */ void readComplexSubset(char *fileName, int width, int height, int posX, int posY, complexFloat *subset) { FILE *fp; int ii, kk; complexFloat *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 < 6) printErr(" ERROR: 'readcomplexSubset' does only work on complex data!\n"); /* Allocate memory for buffers */ buffer = (complexFloat *) MALLOC(height*samples*sizeof(complexFloat)); /* Open image file */ fp = FOPEN(fileName, "rb"); /* Read the appropriate data chunk */ get_complexFloat_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); }
int main(int argc,char **argv) { int lines,samps; /* Number of image lines and samples */ int fftLen,start_line; /* FFT length, & line to start processing at*/ int x,y,i,k; /* Counters */ int f_lo1,f_lo2,f_hi1,f_hi2; /* Filter frequency indicies */ int chunk_size,chunk_int; /* Size of current datablock, & temp value*/ int last_chunk; /* Size of the last chunk */ int compensate_for_last_chunk=1; /* If last chunk = 0 dont loop for last chunk*/ char *inName, *parmName, *outName; /* Input filename */ float filtStart[2], filtEnd[2]; /* Filter start and stop variables */ float df, stop; /* Delta and counter variables */ complexFloat *inBuf,*outBuf; /* Input/Output Image Buffers */ complexFloat *fftBuf; /* FFT Buffer for the image */ float *ampBuf,*phsBuf; /* Amplitude and Phase Buffers */ float *time_vector,A,B,shift; /* Time vector, & freq modulation shift vars*/ float chunk_float; /* Temporary value */ FILE *inF, *freqF, *outF1; /* Input and Output file pointers */ float cur_time, f_s; /* Current time to increment the time vector by */ meta_parameters *inMeta, *outMeta; /* Meta info about the images */ /* Usage is shown if the user doesn't give 3 arguements */ if(argc!=4) { usage(argv[0]); } StartWatch(); printf("Program cpx_filter\n"); /* Get the filename and filter start and end frequencies from the command line*/ inName=argv[1]; if(findExt(inName)==NULL) inName = appendExt(argv[1],".cpx"); outName=argv[2]; parmName=argv[3]; /* Get input metadata. Make sure data_type is complex */ inMeta = meta_read(inName); if (inMeta->general->data_type < COMPLEX_BYTE) { switch (inMeta->general->data_type) { case ASF_BYTE: inMeta->general->data_type=COMPLEX_BYTE; break; case INTEGER16: inMeta->general->data_type=COMPLEX_INTEGER16; break; case INTEGER32: inMeta->general->data_type=COMPLEX_INTEGER32; break; case REAL32: inMeta->general->data_type=COMPLEX_REAL32; break; case REAL64: inMeta->general->data_type=COMPLEX_REAL64; break; } meta_write (inMeta, inName); } /* Open the frequency parameter file and read the parameters */ if((freqF=FOPEN(parmName,"r"))==NULL) { printf("Frequency Parameter File %s could not be Opened!\n",parmName); exit(EXIT_FAILURE); } fscanf(freqF,"%f\n",&f_s); fscanf(freqF,"%f\n",&shift); fscanf(freqF,"%f %f\n", &filtStart[0], &filtEnd[0]); fscanf(freqF,"%f %f\n", &filtStart[1], &filtEnd[1]); printf("\n"); printf("Input file is %s\n",inName); printf("Output file is %s.cpx\n",outName); printf("Parameter file is %s\n",parmName); printf("Filtering from frequencies %.2f to %.2f Hz and %.2f to %.2f in Azimuth\n",filtStart[0],filtEnd[0],filtStart[1],filtEnd[1]); printf("The sampling frequency is %f Hz\n",f_s); printf("Shifting the spectrum by %.2f Hz\n",shift); /* Get the number of lines and samples from the input meta file */ lines = inMeta->general->line_count; samps = inMeta->general->sample_count; chunk_size = BLOCK_SIZE; chunk_float = (float)lines/chunk_size; chunk_int = lines/chunk_size; last_chunk = (int)((chunk_float-(float)chunk_int) * (float)BLOCK_SIZE + 0.5); if( (2*TOSS_SIZE) > last_chunk) compensate_for_last_chunk=0; printf("Chunk Size is set to %d, the last chunk is %d lines\n", chunk_size, last_chunk); /* Compute the FFT length based on the number of lines. Must be a power of 2 */ i = (log10(chunk_size)/log10(2)) + 1; fftLen = pow(2,i); printf("FFT Length is %d\n",fftLen); cfft1d(fftLen,NULL,0); printf("The Input Image has %d lines and %d samples\n",lines,samps); /* Allocate the memory for all the buffers */ inBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*samps*fftLen); outBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*samps*fftLen); fftBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*fftLen); ampBuf = (float *)MALLOC(sizeof(float)*fftLen); phsBuf = (float *)MALLOC(sizeof(float)*fftLen); time_vector = (float *)MALLOC(sizeof(float)*lines); /* Open the Complex Image File */ if((inF=FOPEN(inName,"rb"))==NULL) { printf("Complex Image file %s could not be opened\n",inName); exit(EXIT_FAILURE); } strcat(outName,".cpx"); if((outF1=FOPEN(outName,"wb"))==NULL) { printf("Unable to write output %s\n",outName); exit(EXIT_FAILURE); } outMeta = meta_copy(inMeta); outMeta->general->line_count = chunk_int*(BLOCK_SIZE-2*TOSS_SIZE)+(last_chunk-2*TOSS_SIZE); outMeta->general->sample_count = samps; meta_write(outMeta, outName); /* Find the filter frequency index values */ df = f_s/fftLen; stop = 0; i = 0; while(stop<filtStart[0]) { f_lo1 = i; stop = df*i; i++; } stop = 0; i = 0; while(stop<filtStart[1]) { f_lo2=i; stop=df*i; i++; } i = fftLen; stop = df*i; while(stop>filtEnd[0]) { f_hi1 = i; stop = df*i; i--; } i = fftLen; stop = df*i; while(stop>filtEnd[1]) { f_hi2 = i; stop = df*i; i--; } /* Zero out all the arrays and begin processing data */ cur_time = 0; for(i=0; i<fftLen; i++) { ampBuf[i] = 0; phsBuf[i] = 0; } for(k=0; k<chunk_int+compensate_for_last_chunk; k++) { printf("\nProcessing Chunk %d of %d\n",k,lines/chunk_size); start_line = (k==0) ? 0 : (k*chunk_size)-(2*TOSS_SIZE); if (k==chunk_int) chunk_size=last_chunk; cur_time=start_line*(1/f_s); /* Read in the data chunk & put in proper endian order */ printf("Reading %d Lines Starting at Line %d\n",chunk_size,start_line); get_complexFloat_lines(inF, inMeta, start_line, chunk_size, inBuf); /* Process the each column */ printf("Performing the FFT and Filtering Operations\n"); for(x=0; x<samps; x++) { if(x%1000 == 0) printf("Processing Column %d\n",x); for(y=0;y<fftLen;y++) { fftBuf[y].real = 0; fftBuf[y].imag = 0; } for(y=0;y<chunk_size;y++) { fftBuf[y].real = inBuf[y*samps+x].real; fftBuf[y].imag = inBuf[y*samps+x].imag; } cfft1d(fftLen,fftBuf,-1); for (i=0; i<fftLen; i++) { ampBuf[i] = sqrt(fftBuf[i].real*fftBuf[i].real + fftBuf[i].imag*fftBuf[i].imag); if(fftBuf[i].imag!=0.0 || fftBuf[i].real!=0.0) phsBuf[i] = atan2(fftBuf[i].imag,fftBuf[i].real); else phsBuf[i] = 0; if(((i>f_lo1)&&(i<f_hi1)) || ((i>f_lo2) && (i<f_hi2))) { ampBuf[i] = 0; phsBuf[i] = 0; } fftBuf[i].real = ampBuf[i]*cos(phsBuf[i]); fftBuf[i].imag = ampBuf[i]*sin(phsBuf[i]); } cfft1d(fftLen,fftBuf,1); for(i=0;i<chunk_size;i++) { outBuf[i*samps+x].real = fftBuf[i].real; outBuf[i*samps+x].imag = fftBuf[i].imag; } } printf("Finished the FFT and Filtering Operations\n"); /* Perform the time-domain frequency shift */ if(shift != 0.0) { for(i=0; i<chunk_size; i++) time_vector[i] = cur_time+(1/f_s)*i; printf("\nPerforming time-domain frequency shift of %.2f Hz\n",shift); for(y=0; y<chunk_size; y++) { for(x=0; x<samps; x++) { A = outBuf[y*samps+x].real; B = outBuf[y*samps+x].imag; outBuf[y*samps+x].real = A*cos(2*pi*shift*time_vector[y]) - B*sin(2*pi*shift*time_vector[y]); outBuf[y*samps+x].imag = B*cos(2*pi*shift*time_vector[y]) + A*sin(2*pi*shift*time_vector[y]); } } } /* Write out the data file in big endian format */ printf("Writing the output lines %d to %d in file %s\n", start_line, start_line+chunk_size-TOSS_SIZE, outName); put_complexFloat_lines(outF1, outMeta, start_line, samps*(chunk_size-TOSS_SIZE), outBuf+(samps*TOSS_SIZE)); } printf("\n"); FCLOSE(outF1); StopWatch(); return 0; }
int main(int argc,char **argv) { int lines,samps,start_line; /* Number of image lines and samples */ int x,y,ii; /* Counters and the filter frequency indicies */ char *inName, *outName; /* Input filename */ char metaName[256]; /* Name of meta file */ complexFloat *inBuf; /* Input/Output Image Buffers */ complexFloat *fftBuf; /* FFT Buffer for the image */ float *ampBuf,*phsBuf; /* Amplitude and Phase Buffers */ float df, freq[FFT_LEN], f_s; /* Frequency Vector */ FILE *inF,*outF1; /* Input and Output file pointers */ meta_parameters *meta; /* Meta-file data pointer */ /* Usage is shown if the user doesn't give correct number of arguments */ if(argc!=4) { usage(argv[0]); } StartWatch(); /* Get the filename and filter start and end frequencies from the command line */ inName = argv[1]; outName = argv[2]; start_line = atoi(argv[3]); create_name(metaName, inName, ".meta"); /* Get the PRF and number of samples from the meta-file */ meta = meta_read(metaName); lines = FFT_LEN; samps = meta->general->sample_count; f_s = 1/(meta->sar->azimuth_time_per_pixel); printf("Sampling Frequency is %f\n",f_s); /* Compute the FFT length based on the number of lines. Must be a power of 2 */ printf("FFT Length is %d\n",FFT_LEN); cfft1d(FFT_LEN,NULL,0); /* Allocate the memory for all the buffers */ inBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*samps*FFT_LEN); fftBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*FFT_LEN); ampBuf = (float *)MALLOC(sizeof(float)*FFT_LEN); phsBuf = (float *)MALLOC(sizeof(float)*FFT_LEN); df = f_s/FFT_LEN; for(ii=0; ii<FFT_LEN; ii++) freq[ii] = df*ii; /* Open the Complex Image File */ if((inF=FOPEN(inName,"rb"))==NULL) { printf("Complex Image file %s could not be opened\n",inName); exit(EXIT_FAILURE); } /* Zero out all the arrays and begin processing data */ for(ii=0; ii<FFT_LEN; ii++) { ampBuf[ii]=0; phsBuf[ii]=0; } /* Read in the data chunk */ printf("Reading Data Starting at Line %d\n",start_line); get_complexFloat_lines(inF, meta, start_line, FFT_LEN, inBuf); /* Process the each column, take the average at the end */ printf("Performing the FFT\n"); for(x=0; x<samps; x++) { if(x%500 == 0) printf("Processing Column %d\n",x); for(y=0; y<FFT_LEN; y++) { fftBuf[y].real=0; fftBuf[y].imag=0; } for(y=0; y<lines; y++) { fftBuf[y].real=inBuf[y*samps+x].real; fftBuf[y].imag=inBuf[y*samps+x].imag; } cfft1d(FFT_LEN,fftBuf,-1); for (ii=0; ii<FFT_LEN; ii++) { ampBuf[ii] += sqrt(fftBuf[ii].real*fftBuf[ii].real + fftBuf[ii].imag*fftBuf[ii].imag); if(fftBuf[ii].imag!=0.0 || fftBuf[ii].real!=0.0) phsBuf[ii] += atan2(fftBuf[ii].imag, fftBuf[ii].real); } } printf("Finished the FFT\n"); /* Open and write output file */ strcat(outName,".spectra"); if((outF1=FOPEN(outName,"w"))==NULL) { printf("Unable to write output %s\n",outName); exit(EXIT_FAILURE); } for (ii=0; ii<FFT_LEN; ii++) { ampBuf[ii] /= samps; phsBuf[ii] /= samps; fprintf(outF1,"%f %f %f\n",freq[ii],ampBuf[ii],phsBuf[ii]); } /* Close up & leave */ meta_free(meta); FCLOSE(outF1); StopWatch(); return 0; }
static void get_uavsar_lines(ReadUavsarClientInfo *info, meta_parameters *meta, int row, int n, float *buf) { // wrapper for get_float_line() that multilooks if needed int j,ns=meta->general->sample_count; if (info->ml) { assert(meta->sar); int i,k; int nlooks = meta->sar->azimuth_look_count; row *= nlooks; // we fudged the line count in the metadata for the // viewer (which is displaying a multilooked image), we must // put the correct value back for the reader int lc = meta->general->line_count; meta->general->line_count = g_saved_line_count; float *tmp = MALLOC(sizeof(float)*ns); for (i=0; i<n; ++i) { float *this_row = buf + i*ns; get_float_line(info->fp, meta, row+i*nlooks, this_row); for (j=0; j<ns; ++j) ieee_big32(this_row[j]); int n_read = nlooks; for (k=1; k<nlooks; ++k) { if (row+n*nlooks+k >= meta->general->line_count) { --n_read; } else { get_float_line(info->fp, meta, row+i*nlooks+k, tmp); for (j=0; j<ns; ++j) ieee_big32(tmp[j]); for (j=0; j<ns; ++j) this_row[j] += tmp[j]; } } for (j=0; j<meta->general->sample_count; ++j) this_row[j] /= n_read; } free(tmp); // restore the fudged value meta->general->line_count = lc; } else { // no multilooking case if (info->is_complex) { complexFloat *cf_buf = MALLOC(sizeof(complexFloat)*ns*n); get_complexFloat_lines(info->fp, meta, row, n, cf_buf); for (j=0; j<n*ns; ++j) { ieee_big32(cf_buf[j].real); ieee_big32(cf_buf[j].imag); buf[j] = hypot(cf_buf[j].real, cf_buf[j].imag); //buf[j] = atan2_check(cf_buf[j].imag, cf_buf[j].real); } FREE(cf_buf); } else { get_float_lines(info->fp, meta, row, n, buf); for (j=0; j<n*ns; ++j) ieee_big32(buf[j]); } } }
void c2p_ext(const char *inDataName, const char *inMetaName, const char *outfile, int multilook, int banded) { meta_parameters *in_meta = meta_read(inMetaName); int data_type = in_meta->general->data_type; // the old code did this, but why?? in_meta->general->data_type = meta_polar2complex(data_type); // some sanity checks switch (data_type) { case COMPLEX_BYTE: case COMPLEX_INTEGER16: case COMPLEX_INTEGER32: case COMPLEX_REAL32: case COMPLEX_REAL64: break; default: asfPrintError("c2p: %s is not a complex image.\n", inDataName); } if (in_meta->general->band_count != 1) asfPrintError("c2p: %s is not a single-band image.\n", inDataName); if (!in_meta->sar) asfPrintError("c2p: %s is missing a SAR block.\n", inDataName); asfPrintStatus("Converting complex image to amplitude/phase...\n"); int nl = in_meta->general->line_count; int ns = in_meta->general->sample_count; // process 1 line at a time when not multilooking, otherwise grab nlooks int nlooks = multilook ? in_meta->sar->look_count : 1; if (nlooks == 1 && multilook) { asfPrintStatus("Not multilooking, look_count is 1.\n"); multilook = FALSE; } if (multilook) asfPrintStatus("Multilooking with %d looks.\n", nlooks); meta_parameters *out_meta = meta_read(inMetaName); out_meta->general->data_type = meta_complex2polar(data_type); // set up input/output files FILE *fin = fopenImage(inDataName, "rb"); // we either have 1 or 2 output files, per the "banded" flag. char *outfile_img = appendExt(outfile, ".img"); char *amp_name=NULL, *phase_name=NULL; FILE *fout_banded=NULL, *fout_amp=NULL, *fout_phase=NULL; if (banded) { asfPrintStatus("Output is 2-band image: %s\n", outfile_img); fout_banded = fopenImage(outfile_img, "wb"); } else { amp_name = appendToBasename(outfile_img, "_amp"); phase_name = appendToBasename(outfile_img, "_phase"); asfPrintStatus("Output amplitude file: %s\n", amp_name); asfPrintStatus("Output phase file: %s\n", phase_name); fout_amp = fopenImage(amp_name, "wb"); fout_phase = fopenImage(phase_name, "wb"); } if (banded) assert(fout_banded && !fout_amp && !fout_phase); else assert(!fout_banded && fout_amp && fout_phase); // get the metadata band_count correct, needed in the put_* calls if (banded) { out_meta->general->band_count = 2; strcpy(out_meta->general->bands, "AMP,PHASE"); } // input buffer complexFloat *cpx = MALLOC(sizeof(complexFloat)*ns*nlooks); // output buffers float *amp = MALLOC(sizeof(float)*ns*nlooks); float *phase = MALLOC(sizeof(float)*ns*nlooks); int line_in; // line in the input image int line_out=0; // line in the output image int samp; // sample #, loop index int l; // line loop index, iterates over the lines in the block out_meta->general->line_count = (int)ceil((double)nl/(double)nlooks); for (line_in=0; line_in<nl; line_in+=nlooks) { // lc = "line count" -- how many lines to read. normally we will read // nlooks lines, but near eof we might have to read fewer int lc = nlooks; if (line_in + lc > nl) lc = nl - line_in; // read "nlooks" (or possibly fewer, if near eof) lines of data int blockSize = get_complexFloat_lines(fin,in_meta,line_in,lc,cpx); if (blockSize != lc*ns) asfPrintError("bad blockSize: bs=%d nlooks=%d ns=%d\n", blockSize, nlooks, ns); // first, compute the power/phase for (l=0; l<lc; ++l) { for (samp=0; samp<ns; ++samp) { int k = l*ns + samp; // index into the block float re = cpx[k].real; float im = cpx[k].imag; if (re != 0.0 || im != 0.0) { amp[k] = re*re + im*im; phase[k] = atan2(im, re); } else { amp[k] = phase[k] = 0.0; } } } // now multilook, if requested if (multilook) { // put the multilooked data in the first "row" of amp,phase for (samp=0; samp<ns; ++samp) { float value = 0.0; for (l=0; l<lc; ++l) value += amp[l*ns + samp]; amp[samp] = sqrt(value/(float)lc); value = 0.0; for (l=0; l<lc; ++l) value += phase[l*ns + samp]; phase[samp] = value/(float)lc; } } else { for (samp=0; samp<ns*lc; ++samp) amp[samp] = sqrt(amp[samp]); } // write out a line (multilooked) or a bunch of lines (not multi) if (multilook) { if (banded) { put_band_float_line(fout_banded, out_meta, 0, line_out, amp); put_band_float_line(fout_banded, out_meta, 1, line_out, phase); } else { put_float_line(fout_amp, out_meta, line_out, amp); put_float_line(fout_phase, out_meta, line_out, phase); } ++line_out; } else { for (l=0; l<lc; ++l) { if (banded) { put_band_float_line(fout_banded, out_meta, 0, line_in+l, amp); put_band_float_line(fout_banded, out_meta, 1, line_in+l, phase); } else { put_float_line(fout_amp, out_meta, line_in+l, amp); put_float_line(fout_phase, out_meta, line_in+l, phase); } ++line_out; } } asfPercentMeter((float)line_in/(float)(nl)); } asfPercentMeter(1.0); fclose(fin); if (fout_banded) fclose(fout_banded); if (fout_amp) fclose(fout_amp); if (fout_phase) fclose(fout_phase); if (multilook) { if (out_meta->general->line_count != line_out) asfPrintError("Line counts don't match: %d != %d\n", out_meta->general->line_count, line_out); out_meta->general->y_pixel_size *= nlooks; out_meta->sar->azimuth_time_per_pixel *= nlooks; out_meta->sar->multilook = TRUE; } else assert(line_out == nl); // write out the metadata, different whether multi-banded or not if (banded) { assert(!amp_name && !phase_name); meta_write(out_meta, outfile); } else { assert(amp_name && phase_name); out_meta->general->image_data_type = AMPLITUDE_IMAGE; meta_write(out_meta, amp_name); out_meta->general->image_data_type = PHASE_IMAGE; meta_write(out_meta, phase_name); } if (multilook) asfPrintStatus("Original line count: %d, after multilooking: %d " "(%d looks)\n", nl, line_out, nlooks); meta_free(in_meta); meta_free(out_meta); FREE(amp); FREE(phase); FREE(cpx); FREE(outfile_img); FREE(amp_name); FREE(phase_name); }
int asf_igram_coh(int lookLine, int lookSample, int stepLine, int stepSample, char *masterFile, char *slaveFile, char *outBase, float *average) { char ampFile[255], phaseFile[255]; //, igramFile[512]; char cohFile[512], ml_ampFile[255], ml_phaseFile[255]; //, ml_igramFile[512]; FILE *fpMaster, *fpSlave, *fpAmp, *fpPhase, *fpCoh, *fpAmp_ml, *fpPhase_ml; int line, sample_count, line_count, count; float bin_high, bin_low, max=0.0, sum_a, sum_b, ampScale; double hist_sum=0.0, percent, percent_sum; long long hist_val[HIST_SIZE], hist_cnt=0; meta_parameters *inMeta,*outMeta, *ml_outMeta; complexFloat *master, *slave, *sum_igram, *sum_ml_igram; float *amp, *phase, *sum_cpx_a, *sum_cpx_b, *coh, *pCoh; float *ml_amp, *ml_phase; // FIXME: Processing flow with two-banded interferogram needed - backed out // for now create_name(ampFile, outBase,"_igram_amp.img"); create_name(phaseFile, outBase,"_igram_phase.img"); create_name(ml_ampFile, outBase,"_igram_ml_amp.img"); create_name(ml_phaseFile, outBase,"_igram_ml_phase.img"); //create_name(igramFile, outBase,"_igram.img"); //create_name(ml_igramFile, outBase, "_igram_ml.img"); //sprintf(cohFile, "coherence.img"); create_name(cohFile, outBase, "_coh.img"); // Read input meta file inMeta = meta_read(masterFile); line_count = inMeta->general->line_count; sample_count = inMeta->general->sample_count; ampScale = 1.0/(stepLine*stepSample); // Generate metadata for single-look images outMeta = meta_read(masterFile); outMeta->general->data_type = REAL32; // Write metadata for interferometric amplitude outMeta->general->image_data_type = AMPLITUDE_IMAGE; meta_write(outMeta, ampFile); // Write metadata for interferometric phase outMeta->general->image_data_type = PHASE_IMAGE; meta_write(outMeta, phaseFile); /* // Write metadata for interferogram outMeta->general->image_data_type = INTERFEROGRAM; outMeta->general->band_count = 2; strcpy(outMeta->general->bands, "IGRAM-AMP,IGRAM-PHASE"); meta_write(outMeta, igramFile); */ // Generate metadata for multilooked images ml_outMeta = meta_read(masterFile); ml_outMeta->general->data_type = REAL32; ml_outMeta->general->line_count = line_count/stepLine; ml_outMeta->general->sample_count = sample_count/stepSample; ml_outMeta->general->x_pixel_size *= stepSample; ml_outMeta->general->y_pixel_size *= stepLine; ml_outMeta->sar->multilook = 1; ml_outMeta->sar->line_increment *= stepLine; ml_outMeta->sar->sample_increment *= stepSample; // FIXME: This is the wrong increment but create_dem_grid does not know any // better at the moment. //ml_outMeta->sar->line_increment = 1; //ml_outMeta->sar->sample_increment = 1; // Write metadata for multilooked interferometric amplitude ml_outMeta->general->image_data_type = AMPLITUDE_IMAGE; meta_write(ml_outMeta, ml_ampFile); // Write metadata for multilooked interferometric phase ml_outMeta->general->image_data_type = PHASE_IMAGE; meta_write(ml_outMeta, ml_phaseFile); // Write metadata for coherence image ml_outMeta->general->image_data_type = COHERENCE_IMAGE; meta_write(ml_outMeta, cohFile); /* // Write metadata for multilooked interferogram ml_outMeta->general->image_data_type = INTERFEROGRAM; strcpy(ml_outMeta->general->bands, "IGRAM-AMP,IGRAM-PHASE"); ml_outMeta->general->band_count = 2; meta_write(ml_outMeta, ml_igramFile); */ // Allocate memory master = (complexFloat *) MALLOC(sizeof(complexFloat)*sample_count*lookLine); slave = (complexFloat *) MALLOC(sizeof(complexFloat)*sample_count*lookLine); amp = (float *) MALLOC(sizeof(float)*sample_count*lookLine); phase = (float *) MALLOC(sizeof(float)*sample_count*lookLine); ml_amp = (float *) MALLOC(sizeof(float)*sample_count/stepSample); ml_phase = (float *) MALLOC(sizeof(float)*sample_count/stepSample); coh = (float *) MALLOC(sizeof(float)*sample_count/stepSample); sum_cpx_a = (float *) MALLOC(sizeof(float)*sample_count); sum_cpx_b = (float *) MALLOC(sizeof(float)*sample_count); sum_igram = (complexFloat *) MALLOC(sizeof(complexFloat)*sample_count); sum_ml_igram = (complexFloat *) MALLOC(sizeof(complexFloat)*sample_count); // Open files fpMaster = FOPEN(masterFile,"rb"); fpSlave = FOPEN(slaveFile,"rb"); fpAmp = FOPEN(ampFile,"wb"); fpPhase = FOPEN(phaseFile,"wb"); fpAmp_ml = FOPEN(ml_ampFile,"wb"); fpPhase_ml = FOPEN(ml_phaseFile,"wb"); //FILE *fpIgram = FOPEN(igramFile, "wb"); //FILE *fpIgram_ml = FOPEN(ml_igramFile, "wb"); fpCoh = FOPEN(cohFile,"wb"); // Initialize histogram for (count=0; count<HIST_SIZE; count++) hist_val[count] = 0; asfPrintStatus(" Calculating interferogram and coherence ...\n\n"); for (line=0; line<line_count; line+=stepLine) { register int offset, row, column, limitLine; double igram_real, igram_imag; int inCol; limitLine=MIN(lookLine, line_count-line); printf("Percent completed %3.0f\r",(float)line/line_count*100.0); pCoh = coh; // Read in the next lines of data get_complexFloat_lines(fpMaster, inMeta, line, limitLine, master); get_complexFloat_lines(fpSlave, inMeta, line, limitLine, slave); // Add the remaining rows into sum vectors offset = sample_count; for (column=0; column<sample_count; column++) { offset = column; sum_cpx_a[column] = 0.0; sum_cpx_b[column] = 0.0; sum_igram[column].real = 0.0; sum_igram[column].imag = 0.0; sum_ml_igram[column].real = 0.0; sum_ml_igram[column].imag = 0.0; igram_real = 0.0; igram_imag = 0.0; for (row=0; row<limitLine; row++) { // Complex multiplication for interferogram generation igram_real = master[offset].real*slave[offset].real + master[offset].imag*slave[offset].imag; igram_imag = master[offset].imag*slave[offset].real - master[offset].real*slave[offset].imag; amp[offset] = sqrt(igram_real*igram_real + igram_imag*igram_imag); if (FLOAT_EQUIVALENT(igram_real, 0.0) || FLOAT_EQUIVALENT(igram_imag, 0.0)) phase[offset]=0.0; else phase[offset] = atan2(igram_imag, igram_real); sum_cpx_a[column] += AMP(master[offset])*AMP(master[offset]); sum_cpx_b[column] += AMP(slave[offset])*AMP(slave[offset]); sum_igram[column].real += igram_real; sum_igram[column].imag += igram_imag; if (line % stepLine == 0 && row < stepLine) { sum_ml_igram[column].real += igram_real; sum_ml_igram[column].imag += igram_imag; } offset += sample_count; } ml_amp[column] = sqrt(sum_ml_igram[column].real*sum_ml_igram[column].real + sum_ml_igram[column].imag*sum_ml_igram[column].imag)*ampScale; if (FLOAT_EQUIVALENT(sum_ml_igram[column].real, 0.0) || FLOAT_EQUIVALENT(sum_ml_igram[column].imag, 0.0)) ml_phase[column] = 0.0; else ml_phase[column] = atan2(sum_ml_igram[column].imag, sum_ml_igram[column].real); } // Write single-look and multilooked amplitude and phase put_float_lines(fpAmp, outMeta, line, stepLine, amp); put_float_lines(fpPhase, outMeta, line, stepLine, phase); put_float_line(fpAmp_ml, ml_outMeta, line/stepLine, ml_amp); put_float_line(fpPhase_ml, ml_outMeta, line/stepLine, ml_phase); //put_band_float_lines(fpIgram, outMeta, 0, line, stepLine, amp); //put_band_float_lines(fpIgram, outMeta, 1, line, stepLine, phase); //put_band_float_line(fpIgram_ml, ml_outMeta, 0, line/stepLine, ml_amp); //put_band_float_line(fpIgram_ml, ml_outMeta, 1, line/stepLine, ml_phase); // Calculate the coherence by adding from sum vectors for (inCol=0; inCol<sample_count; inCol+=stepSample) { register int limitSample = MIN(lookSample,sample_count-inCol); sum_a = 0.0; sum_b = 0.0; igram_real = 0.0; igram_imag = 0.0; // Step over multilook area and sum output columns for (column=0; column<limitSample; column++) { igram_real += sum_igram[inCol+column].real; igram_imag += sum_igram[inCol+column].imag; sum_a += sum_cpx_a[inCol+column]; sum_b += sum_cpx_b[inCol+column]; } if (FLOAT_EQUIVALENT((sum_a*sum_b), 0.0)) *pCoh = 0.0; else { *pCoh = (float) sqrt(igram_real*igram_real + igram_imag*igram_imag) / sqrt(sum_a * sum_b); if (*pCoh>1.0001) { printf(" coh = %f -- setting to 1.0\n",*pCoh); printf(" You shouldn't have seen this!\n"); printf(" Exiting.\n"); exit(EXIT_FAILURE); *pCoh=1.0; } } pCoh++; } // Write out values for coherence put_float_line(fpCoh, ml_outMeta, line/stepLine, coh); // Keep filling coherence histogram for (count=0; count<sample_count/stepSample; count++) { register int tmp; tmp = (int) (coh[count]*HIST_SIZE); /* Figure out which bin this value is in */ /* This shouldn't happen */ if(tmp >= HIST_SIZE) tmp = HIST_SIZE-1; if(tmp < 0) tmp = 0; hist_val[tmp]++; // Increment that bin for the histogram hist_sum += coh[count]; // Add up the values for the sum hist_cnt++; // Keep track of the total number of values if (coh[count]>max) max = coh[count]; // Calculate maximum coherence } } // End for line printf("Percent completed %3.0f\n",(float)line/line_count*100.0); // Sum and print the statistics percent_sum = 0.0; printf(" Coherence : Occurrences : Percent\n"); printf(" ---------------------------------------\n"); for (count=0; count<HIST_SIZE; count++) { bin_low = (float)(count)/(float)HIST_SIZE; bin_high = (float)(count+1)/(float)HIST_SIZE; percent = (double)hist_val[count]/(double)hist_cnt; percent_sum += (float)100*percent; printf(" %.2f -> %.2f : %.8lld %2.3f \n", bin_low,bin_high, (long long) hist_val[count],100*percent); } *average = (float)hist_sum/(float)hist_cnt; printf(" ---------------------------------------\n"); printf(" Maximum Coherence: %.3f\n", max); printf(" Average Coherence: %.3f (%.1f / %lld) %f\n", *average,hist_sum, hist_cnt, percent_sum); // Free and exit FREE(master); FREE(slave); FREE(amp); FREE(phase); FREE(ml_amp); FREE(ml_phase); FREE(coh); FCLOSE(fpMaster); FCLOSE(fpSlave); FCLOSE(fpAmp); FCLOSE(fpPhase); FCLOSE(fpAmp_ml); FCLOSE(fpPhase_ml); //FCLOSE(fpIgram); //FCLOSE(fpIgram_ml); FCLOSE(fpCoh); meta_free(inMeta); meta_free(outMeta); meta_free(ml_outMeta); return(0); }