├── CalcGamma.m ├── CalcGamma_raterem.m ├── FilterFlat_s1a.m ├── README.md ├── allps2stamps.m ├── avgrect.m ├── bandPass_dates.m ├── build_Gint.m ├── build_Gint_s1a.m ├── calamp.m ├── calc_rate_residual.m ├── check_ints_s1a.m ├── choose_ints.m ├── choose_ints_km.m ├── compare_unw.m ├── crop_edges.m ├── crop_edges_dates.m ├── filter_diff.m ├── filter_diff_iter.m ├── filter_diff_iter_km.m ├── filter_diff_iter_old.m ├── filter_diff_new.m ├── filter_diff_remove_restore.m ├── filter_rlooks.m ├── filter_unw_diff.m ├── filtflat0.m ├── filtflat1.m ├── filtflat2.m ├── filtflat3.m ├── fitramp_date.m ├── fitramp_int.m ├── fitramp_int_s1a.m ├── flat_ints.m ├── geocode.m ├── geocode_dates.m ├── get_int_offsets_s1a.m ├── getstuff.m ├── gps_reference.m ├── gps_surface.m ├── hi ├── init_dirs.m ├── invert_dates.m ├── invert_dates_height.m ├── invert_dates_s1a.m ├── invert_rates.m ├── invert_rates_s1a.m ├── lf_power.m ├── load_allps_unw.m ├── load_data.m ├── makeGamma.m ├── make_all_raw.m ├── make_dates_struct.m ├── make_dates_struct_s1a.m ├── make_filtrate.m ├── make_frame_gmt.m ├── make_ints.m ├── make_mask.m ├── make_ramps.m ├── make_slcs.m ├── mask_unwrapped.m ├── master_int.m ├── my_ICA.m ├── mysys.m ├── old ├── invert_dates.m └── invert_rates.m ├── par_ps_interp.m ├── parraw.m ├── plot_all_pts.m ├── plot_geo.m ├── plot_images.m ├── plot_pts_geo.m ├── plot_std_profile.m ├── plot_ts_pts.m ├── ps_interp.m ├── ps_interp_par.m ├── ps_interp_runpar.m ├── ps_unwrap_allps.m ├── read_dopbase.m ├── read_dopbase_km.m ├── rect_slcs.m ├── rect_slcs_km.m ├── replace_dates_struct.m ├── replace_ints_struct.m ├── run_all.m ├── run_all_tsx.m ├── search_data.m ├── setup_init.m ├── smart_rlooks.m ├── smart_rlooks_2.m ├── smart_rlooks_par.m ├── smart_rlooks_runpar.m ├── undo_fitramp.m ├── unw2png_km.m ├── unw2png_km1.m ├── unw2png_km_s1a.m ├── unwrap_filtered_rlooks.m ├── unwrap_flat.m ├── unwrap_rlooks.m ├── unwrap_watermask.m ├── use_rdf.m ├── write_paramfile.m ├── write_paramfile_s1a.m ├── write_slc_proc.m ├── write_snaphu_conf.m ├── xml2rsc.m └── zzr.m /CalcGamma.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile) 3 | 4 | for i=1:nints 5 | filename = [ints(i).flat '_diff']; 6 | fid(i) = fopen(filename,'r','native'); 7 | end 8 | 9 | im = sqrt(-1); 10 | 11 | % if(exist(gammafile)) 12 | % disp([gammafile 'already made']) 13 | % else 14 | fidout = fopen(gammafile,'w','native'); 15 | 16 | for j=1:ny 17 | tic 18 | tmpcpx = complex(zeros(nints,nx),zeros(nints,nx)); %a chunk nints by nx 19 | nonz = zeros(1,nx); %a row of zeros 20 | for i=1:nints 21 | [dat,count] = fread(fid(i),nx,'real*4'); 22 | tmpcpx(i,1:count) = exp(im*dat); %diff complex number 23 | goodid = dat~=0; 24 | nonz(goodid) = nonz(goodid)+1; %adds a one for every int that has a nonzero 25 | end 26 | %phsvar=-2*log(abs(sum(tmpcpx,1)./nonz)); 27 | phsvar=abs(sum(tmpcpx,1)./nonz); %like coherence 28 | phsvar(nonzpi 82 | theta=theta-2*pi; 83 | end 84 | 85 | rotm=[cos(theta),sin(theta); -sin(theta),cos(theta)]; 86 | xy=xy'; 87 | xynew=rotm*xy; % rotate so that scene axes approx align with x=0 and y=0 88 | if max(xynew(1,:))-min(xynew(1,:))0,2); 33 | mag = sum(band1,2)./good; 34 | mag(isnan(mag))=0; 35 | m2 = band1-repmat(mag,1,nints); 36 | mstd = sum(m2.^2,2)./good; 37 | mstd(isnan(mstd))=0; 38 | 39 | fwrite(fidout,mag,'real*4'); 40 | end 41 | 42 | for i=1:nints 43 | fclose(fidin(i)); 44 | end 45 | fclose(fidout); 46 | 47 | else 48 | disp('stacking slc magnitudes') 49 | [nx,ny]=load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 50 | 51 | for i=1:ndates 52 | fidin(i)=fopen(dates(i).rectslc,'r','native'); 53 | end 54 | 55 | for j=1:ny 56 | band1=zeros(nx,ndates); 57 | for i=1:ndates 58 | [tmp,count]=fread(fidin(i),nx*2,'real*4'); 59 | if(count==nx*2) 60 | rl=tmp(1:2:end); 61 | ig=tmp(2:2:end); 62 | cpx=rl+im*ig; 63 | amp=abs(cpx); 64 | band1(:,i)=amp/dates(i).ampmed; 65 | end 66 | end 67 | band1(isnan(band1))=0; 68 | good = sum(band1>0,2); 69 | mag = sum(band1,2)./good; 70 | mag(isnan(mag))=0; 71 | m2 = band1-repmat(mag,1,ndates); 72 | mstd = sum(m2.^2,2)./good; 73 | mstd(isnan(mstd))=0; 74 | 75 | fwrite(fidout,mag,'real*4'); 76 | end 77 | 78 | for i=1:ndates 79 | fclose(fidin(i)); 80 | end 81 | fclose(fidout); 82 | 83 | end 84 | 85 | %% Plot the avgrect file 86 | fid = fopen('TS/avgslc.r4','r','native'); 87 | [rmg,count] = fread(fid,[nx,ny],'real*4'); 88 | status = fclose(fid); 89 | %downlook by taking every 10th pixel 90 | phs = (rmg(1:10:nx,1:10:ny))'; 91 | 92 | figure 93 | imagesc(phs,[0 5]) 94 | colorbar 95 | title('Average of SLCs') 96 | kylestyle 97 | 98 | 99 | -------------------------------------------------------------------------------- /bandPass_dates.m: -------------------------------------------------------------------------------- 1 | %bandpass image filter 2 | % function bandPass_dates(fstrength) 3 | set_params 4 | for fstrength=[1 5 20 80 160]; 5 | for ii=1:ndates 6 | % Specify inputs and outputs 7 | % maskfile = maskfilerlk{1}; 8 | % fid2 = fopen([infile '_filt'],'w'); 9 | 10 | % Load Phase 11 | infile = [dates(ii).unwrlk '_corrected']; 12 | outfile = [dates(ii).unwrlk '_filt']; 13 | fid1 = fopen(infile,'r'); 14 | [a,count] = fread(fid1,[newnx,newny],'real*4'); 15 | phs=a'; 16 | phs=fliplr(phs); 17 | % Load Mask 18 | fid5 = fopen(maskfile,'r'); 19 | [a,count] = fread(fid5,[newnx,newny],'real*4'); 20 | a = a==1; 21 | gam = a'; 22 | bad = ~gam; 23 | 24 | % Apply mask to phase 25 | pmask=phs; 26 | pmask(bad) =nan; 27 | 28 | % Transform phase to frequency domain 29 | phs_freq = fft2(phs); 30 | phs_freq = fftshift(phs_freq); 31 | 32 | figure;fftshow(phs_freq,'log') 33 | title('Fourier Spectrum of Image');colorbar 34 | % 35 | % Make filters 36 | % gauss_width1 = 200; 37 | % fig=figure(22); 38 | % set(fig,'position',[1 1 2000 600],'Color',[.1 .1 .1] );kylestyle 39 | 40 | % for ii = 1:3%:40; 41 | 42 | 43 | xoff=floor(newnx/2); 44 | yoff=floor(newny/2); 45 | [X,Y] = meshgrid(1-xoff:newnx-xoff,1-yoff:newny-yoff); 46 | 47 | fstrength=5; 48 | g1=fstrength/2; 49 | gauss1 = exp((-X.^2-Y.^2)/g1); 50 | g2=fstrength; 51 | gauss2 = exp((-X.^2-Y.^2)/(g2^2)); 52 | gauss3 = gauss2-gauss1; 53 | 54 | 55 | figure 56 | subplot(1,3,1);imagesc(gauss1);colorbar;title('filter 1') 57 | xlim([(newnx/2-10) (newnx/2+10)]); 58 | ylim([(newny/2-10) (newny/2+10)]); 59 | subplot(1,3,2);imagesc(gauss2);colorbar;title('filter 2') 60 | xlim([(newnx/2-10) (newnx/2+10)]); 61 | ylim([(newny/2-10) (newny/2+10)]); 62 | subplot(1,3,3);imagesc(gauss3);colorbar;title('filter 3') 63 | xlim([(newnx/2-10) (newnx/2+10)]); 64 | ylim([(newny/2-10) (newny/2+10)]); 65 | 66 | % Apply filter to freq domain 67 | phs_freq_filt = gauss3.*phs_freq; 68 | phs_freq_filt = ifftshift(phs_freq_filt); 69 | r=range(phs_freq_filt(:)); 70 | fl = log(1+abs(r)) 71 | fm = max(fl(:)); 72 | im2uint8(fl/fm) 73 | phs_filt = ifft2(phs_freq_filt); 74 | phs_filt = real(phs_filt); 75 | 76 | phs_diff=phs-phs_filt; 77 | 78 | % Plot masked phase 79 | fig=figure(1) 80 | subplot(1,3,1);imagesc(phs);colorbar;title('Original Phase');caxis([-25 100]); 81 | subplot(1,3,2);imagesc(phs_filt);colorbar;title('Filtered Phase');caxis([-25 100]); 82 | subplot(1,3,3);imagesc(phs_diff);colorbar;title('Phase minus filtered');caxis([-25 100]); 83 | colormap('jet') 84 | if ~exist('filt_figs','dir') 85 | !mkdir filt_figs 86 | end 87 | drawnow 88 | Mov(ii)=getframe; 89 | frame=getframe(fig); 90 | im=frame2im(frame); 91 | [imind,cm] = rgb2ind(im,256); 92 | imwrite(imind,cm,['filt_figs/filt_' num2str(fstrength) '.png'],'png'); 93 | 94 | fido=fopen(outfile,'w'); 95 | fwrite(fido,phs_diff','real*4'); 96 | fclose(fido); 97 | 98 | end 99 | end 100 | -------------------------------------------------------------------------------- /build_Gint.m: -------------------------------------------------------------------------------- 1 | function [G,Gg,R,N]=build_Gint 2 | set_params 3 | load(ts_paramfile) 4 | 5 | ndates=length(dates); 6 | nints=length(ints); 7 | 8 | dn = [dates.dn]; 9 | dn = dn-dn(id); 10 | 11 | i1=[ints.i1]; 12 | i2=[ints.i2]; 13 | 14 | G=zeros(nints,ndates); 15 | for i=1:nints 16 | G(i,i1(i)) = -1; 17 | G(i,i2(i)) = 1; 18 | end 19 | G(end+1,id)=1; %master date has zero def. 20 | 21 | %find un-resolved time periodsp=rank(G); 22 | p = rank(G); 23 | [U,S,V] = svd(G); 24 | Rm = V(:,1:p)*inv(S(1:p,1:p))*U(:,1:p)'*U(:,1:p)*S(1:p,1:p)*V(:,1:p)'; %truncates resolution matrix 25 | badid = find(diag(Rm)<0.9); %thresholds should perhaps be set differently 26 | goodid = find(diag(Rm)>=0.9); 27 | 28 | Gline = dn(goodid)'; 29 | Ggline = inv(Gline'*Gline)*Gline'; 30 | for j=1:length(badid) 31 | G(end+1,goodid)=Ggline*dn(badid(j)); 32 | G(end,badid(j))=-1; 33 | end 34 | 35 | Gg = inv(G'*G)*G'; 36 | N = G*Gg; 37 | R = Gg*G; 38 | 39 | plotflag=1; 40 | if(plotflag) 41 | res=diag(N); 42 | minres=min(res); 43 | maxres=max(res); 44 | dres=maxres-minres; 45 | 46 | figure 47 | c=colormap; 48 | dnpair=[dates(i1).dn;dates(i2).dn]; 49 | bppair=[dates(i1).bp;dates(i2).bp]; 50 | 51 | plot([dates.dn],[dates.bp],'k.') 52 | hold on 53 | for i=1:nints 54 | deltad=floor((res(i)-minres)/dres*63)+1; 55 | plot(dnpair(:,i),bppair(:,i),'-','Color',c(deltad,:)) 56 | 57 | end 58 | grid on 59 | datetick 60 | % print('-depsc','figs/baseline_R.eps'); 61 | end 62 | -------------------------------------------------------------------------------- /build_Gint_s1a.m: -------------------------------------------------------------------------------- 1 | function [G,Gg,R,N]=build_Gint 2 | set_params 3 | load(ts_paramfile) 4 | id=1; 5 | ndates=length(dates); 6 | nints=length(ints); 7 | 8 | dn = [dates.dn]; 9 | dn = dn-dn(id); 10 | 11 | i1=[ints.i1]; 12 | i2=[ints.i2]; 13 | 14 | G=zeros(nints,ndates); 15 | for i=1:nints 16 | G(i,i1(i)) = -1; 17 | G(i,i2(i)) = 1; 18 | end 19 | G(end+1,id)=1; %master date has zero def. 20 | 21 | %find un-resolved time periodsp=rank(G); 22 | p = rank(G); 23 | [U,S,V] = svd(G); 24 | Rm = V(:,1:p)*inv(S(1:p,1:p))*U(:,1:p)'*U(:,1:p)*S(1:p,1:p)*V(:,1:p)'; %truncates resolution matrix 25 | badid = find(diag(Rm)<0.9); %thresholds should perhaps be set differently 26 | goodid = find(diag(Rm)>=0.9); 27 | 28 | Gline = dn(goodid)'; 29 | Ggline = inv(Gline'*Gline)*Gline'; 30 | for j=1:length(badid) 31 | G(end+1,goodid)=Ggline*dn(badid(j)); 32 | G(end,badid(j))=-1; 33 | end 34 | 35 | Gg = inv(G'*G)*G'; 36 | N = G*Gg; 37 | R = Gg*G; 38 | 39 | plotflag=0; 40 | if(plotflag) 41 | res=diag(N); 42 | minres=min(res); 43 | maxres=max(res); 44 | dres=maxres-minres; 45 | 46 | figure 47 | c=colormap; 48 | dnpair=[dates(i1).dn;dates(i2).dn]; 49 | bppair=[dates(i1).bp;dates(i2).bp]; 50 | 51 | plot([dates.dn],[dates.bp],'k.') 52 | hold on 53 | for i=1:nints 54 | deltad=floor((res(i)-minres)/dres*63)+1; 55 | plot(dnpair(:,i),bppair(:,i),'-','Color',c(deltad,:)) 56 | 57 | end 58 | grid on 59 | datetick 60 | print('-depsc','figs/baseline_R.eps'); 61 | end 62 | -------------------------------------------------------------------------------- /calamp.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | ndates=length(dates); 4 | buffer=1000; 5 | 6 | 7 | if strcmp(sat,'S1A') 8 | [nx,ny]=load_rscs(ints(id).flat,'WIDTH','FILE_LENGTH'); 9 | nbuf=ceil(ny/buffer); 10 | for i=1:nints 11 | 12 | i 13 | fidin=fopen(ints(i).flat,'r','native'); 14 | med=0; 15 | totcount=0; 16 | 17 | for j=1:nbuf 18 | [tmp,count]=fread(fidin,nx*2*buffer,'real*4'); 19 | totcount=totcount+count/2; 20 | amp=abs(complex(tmp(1:2:count),tmp(2:2:count))); 21 | med=med+sum(amp); 22 | end 23 | fclose(fidin); 24 | ints(i).ampmed=med/totcount; 25 | end 26 | 27 | if(exist('ints','var')) 28 | save(ts_paramfile,'dates','ints'); 29 | else 30 | save(ts_paramfile,'dates'); 31 | end 32 | 33 | else 34 | [nx,ny]=load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 35 | nbuf=ceil(ny/buffer); 36 | for i=1:ndates 37 | i 38 | fidin=fopen(dates(i).rectslc,'r','native'); 39 | med=0; 40 | totcount=0; 41 | 42 | for j=1:nbuf 43 | [tmp,count]=fread(fidin,nx*2*buffer,'real*4'); 44 | totcount=totcount+count/2; 45 | amp=abs(complex(tmp(1:2:count),tmp(2:2:count))); 46 | med=med+sum(amp); 47 | end 48 | fclose(fidin); 49 | dates(i).ampmed=med/totcount; 50 | end 51 | 52 | if(exist('ints','var')) 53 | save(ts_paramfile,'dates','ints'); 54 | else 55 | save(ts_paramfile,'dates'); 56 | end 57 | 58 | end 59 | 60 | 61 | -------------------------------------------------------------------------------- /calc_rate_residual.m: -------------------------------------------------------------------------------- 1 | 2 | set_params 3 | % 4 | % ndates = length(dates); 5 | % nints = length(ints); 6 | % [nx,ny,lambda] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH','WAVELENGTH'); 7 | % 8 | % newnx = floor(nx./rlooks) 9 | % newny = floor(ny./alooks); 10 | 11 | for l=1:length(rlooks) 12 | fidr = fopen(['rates_' num2str(rlooks(l))],'r'); 13 | 14 | for i=1:nints 15 | fidi(i) = fopen(ints(i).unwrlk,'r'); 16 | intrms(i) = 0; 17 | end 18 | 19 | for j=1:newny(l) 20 | fidr = fopen(['rates_' num2str(rlooks(l))],'r'); 21 | rate = fread(fidr,newnx(l),'real*4')/lambda*(4*pi)/1000/365;%conver rate back to radians/day 22 | rate(isnan(rate))=0; 23 | for i=1:nints 24 | if(fidi(i)>0) 25 | unw = fread(fidi(i),newnx(l),'real*4'); 26 | res = unw-ints(i).dt*rate; 27 | intrms(i) = intrms(i) + sum(res.^2); 28 | end 29 | end 30 | end 31 | for i=1:nints 32 | ints(i).rms=intrms(i)/newnx(l)/newny(l); 33 | end 34 | end 35 | 36 | intrms = [ints.rms]; 37 | badid = find(intrms==max(intrms)) 38 | disp(['worst interferogram is ' num2str(badid)]); 39 | 40 | save(ts_paramfile,'dates','ints'); 41 | fclose('all') 42 | 43 | plotflag=1; 44 | if(plotflag) 45 | 46 | i1=[ints.i1]; 47 | i2=[ints.i2]; 48 | 49 | minres=min(intrms); 50 | maxres=max(intrms); 51 | dres=maxres-minres; 52 | 53 | figure 54 | c=colormap; 55 | dnpair=[dates(i1).dn;dates(i2).dn]; 56 | bppair=[dates(i1).bp;dates(i2).bp]; 57 | 58 | plot([dates.dn],[dates.bp],'k.') 59 | hold on 60 | for i=1:nints 61 | deltad=floor((intrms(i)-minres)/dres*63)+1; 62 | plot(dnpair(:,i),bppair(:,i),'-','Color',c(deltad,:)) 63 | 64 | end 65 | text([dates.dn],[dates.bp],num2str([1:ndates]')) 66 | grid on 67 | datetick 68 | end 69 | -------------------------------------------------------------------------------- /check_ints_s1a.m: -------------------------------------------------------------------------------- 1 | %checks the ints struct to make sure we have int dirs for all of them 2 | clear all;close all 3 | 4 | set_params 5 | badids=[]; 6 | for i=1:nints 7 | if(~exist(ints(i).flat)) 8 | disp([ints(i).flat 'does not exist (number ' num2str(i) ')']) 9 | badids= [badids;i]; 10 | end 11 | end 12 | 13 | if(exist('badids')) 14 | disp('') 15 | reply = input('Remove these from ints struct? Y/N [Y]: ', 's'); 16 | if(isempty(reply)) 17 | reply='Y'; 18 | end 19 | switch reply 20 | case {'Y','Yes','y','YES'} 21 | disp('Deleting missing pairs from structure!') 22 | disp('Process more ints to bridge the gap.') 23 | 24 | ints([badids])=[]; 25 | case {'No','n','N','NO'} 26 | disp('Process missing ints before continuing!') 27 | return 28 | end 29 | save(ts_paramfile,'dates','ints') 30 | else 31 | disp('No missing pairs :)') 32 | end -------------------------------------------------------------------------------- /choose_ints.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | ndates=length(dates); 4 | 5 | dn=[dates.dn]; 6 | bp=[dates.bp]; 7 | deltadn=repmat(dn,ndates,1)-repmat(dn',1,ndates); 8 | deltabp=repmat(bp,ndates,1)-repmat(bp',1,ndates); 9 | 10 | 11 | %Change doppler and baseline thresholds depending on satellite. 12 | switch sat 13 | case 'ENVI' 14 | dn_thresh = 300; 15 | bp_thresh = 200; 16 | [i1,i2]=find(and(and(deltadn>0,deltadn0,deltadn0,deltadn0,deltadn2 44 | a=find(i1==i); 45 | 46 | a3=i2(a); 47 | a1=sort(abs(deltabp(a3,i))); 48 | 49 | a2(1)=find(abs(deltabp(:,i))==a1(1)); 50 | a2(2)=find(abs(deltabp(:,i))==a1(2)); 51 | 52 | a4=a3(find(and(i2(a)~=a2(1),i2(a)~=a2(2)))) ; 53 | for j=1:length(a4) 54 | rempair(j,:)=[i,a4(j)]; 55 | end 56 | bad=ismember([i1 i2],rempair,'rows'); 57 | 58 | i1=i1(~bad); 59 | i2=i2(~bad); 60 | end 61 | end 62 | %remove pairs (by date index) 63 | if(~isempty(removepairs)) 64 | badid=ismember([i1 i2],removepairs,'rows'); 65 | i1=i1(~badid); 66 | i2=i2(~badid); 67 | end 68 | 69 | % if(~isempty(rp)) 70 | % badid=ismember([i1 i2],rp,'rows'); 71 | % i1=i1(~badid); 72 | % i2=i2(~badid); 73 | % end 74 | 75 | %add others here 76 | if(~isempty(addpairs)) 77 | i1=[i1;addpairs(:,1)]; 78 | i2=[i2;addpairs(:,2)]; 79 | end 80 | 81 | 82 | i1=[ints.i1]; 83 | i2=[ints.i2]; 84 | 85 | if(plotflag) 86 | dnpair=[dates(i1).dn;dates(i2).dn]; 87 | bppair=[dates(i1).bp;dates(i2).bp]; 88 | 89 | figure 90 | text(dn,bp,num2str([1:ndates]')); hold on 91 | plot(dnpair,bppair); 92 | grid on 93 | datetick 94 | title(masterdir) 95 | xlabel('Years'); ylabel('Baseline (m)') 96 | kylestyle 97 | end 98 | 99 | %save int structure and params, build Gint for later inversion 100 | clear ints 101 | nints=length(i1); 102 | 103 | for i=1:nints 104 | ints(i).i1=i1(i); 105 | ints(i).i2=i2(i); 106 | ints(i).dt=dates(i2(i)).dn-dates(i1(i)).dn; 107 | ints(i).name=[dates(i1(i)).name '-' dates(i2(i)).name]; 108 | ints(i).int=[intdir ints(i).name '.int']; 109 | ints(i).flat=[intdir 'flat_' ints(i).name '.int']; 110 | for j=1:length(rlooks) 111 | ints(i).flatrlk{j}=[rlkdir{j} 'flat_' ints(i).name '_' num2str(rlooks(j)) 'rlks.int']; 112 | ints(i).unwrlk{j}=[rlkdir{j} 'flat_' ints(i).name '_' num2str(rlooks(j)) 'rlks.unw']; 113 | ints(i).unwmsk{j}=[rlkdir{j} 'ramp_' ints(i).name '_' num2str(rlooks(j)) 'rlks.unw']; 114 | end 115 | for j=1:7 116 | ints(i).bvec(j)=dates(i2(i)).bvec(j)-dates(i1(i)).bvec(j); 117 | end 118 | ints(i).bp=ints(i).bvec(7); %for convenience 119 | end 120 | save(ts_paramfile,'dates','ints'); 121 | [G,Gg,R,N]=build_Gint; 122 | 123 | % clear input 124 | % 125 | % reply = input('Want to add/remove pairs? Y/n [Y]: ', 's'); 126 | % if(isempty(reply)) 127 | % reply='Y'; 128 | % end 129 | % 130 | % switch reply 131 | % case {'Y','Yes','y','YES'} 132 | % disp('add or remove pairs in set_params file and rerun') 133 | % case {'No','n','NO','N'} 134 | % % Finds a good interferogram pair to make the primary interferogram, and 135 | % % makes the first date of that pair the primary date. 136 | % intid_o=intid; 137 | % id_o=id; 138 | % 139 | % baseline_sums=abs([ints.dt])+abs([ints.bp]); %sum each int's spatial and temporal baseline 140 | % [~,b]=sort(baseline_sums); %find the lowest value from above 141 | % intid=b(1); %make that the master int 142 | % id=ints(intid).i1; %make the master date the first date in the master int from above 143 | % 144 | % disp(['Making ' ints(intid).name ' the primary interferogram.']); 145 | % disp([num2str(ints(intid).i1) ' and ' num2str(ints(intid).i2)]); 146 | % disp(['Making ' dates(id).name ' the primary date/']); 147 | % disp(['Writing new primary date and int to set_params file']); 148 | % 149 | % % Write the new ids to set_params 150 | % file=[masterdir 'set_params.m']; 151 | % fid=fopen(file,'a+'); 152 | % fprintf(fid,['id = ' num2str(id) ' ;\n']); 153 | % fprintf(fid,['intid = ' num2str(intid) ' ;\n']); 154 | % 155 | % 156 | % %check if ids changed 157 | % if(intid~=intid_o & id~=id_o) 158 | % disp('ids have changed. Rerunning from setup_init') 159 | % setup_init 160 | % choose_ints_km 161 | % else 162 | % disp('ids have not changed. Continue to make_slcs') 163 | % end 164 | % end 165 | % 166 | -------------------------------------------------------------------------------- /compare_unw.m: -------------------------------------------------------------------------------- 1 | 2 | set_params 3 | load(ts_paramfile); 4 | ndates=length(dates); 5 | nints=length(ints) 6 | 7 | [nx,ny]=load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 8 | newnx=nx./rlooks; 9 | newny=ny./alooks; 10 | unwres=zeros(nints,newnx(1),newny(1)); 11 | for l=1 12 | for i=1:nints 13 | fid1=fopen(ints(i).unwrlk{l},'r'); 14 | fid2=fopen([ints(i).unwrlk{l} '_rot'],'r'); 15 | tmp1=fread(fid1,[newnx(l),newny(l)*2],'real*4'); 16 | tmp2=fread(fid2,[newnx(l),newny(l)*2],'real*4'); 17 | fclose(fid1); 18 | fclose(fid2); 19 | tmp1=tmp1(:,2:2:end); 20 | tmp2=tmp2(:,2:2:end); 21 | res=tmp1-tmp2; 22 | resstd(i)=std(res(isfinite(res))); 23 | unwres(i,:,:)=res; 24 | end 25 | end -------------------------------------------------------------------------------- /crop_edges.m: -------------------------------------------------------------------------------- 1 | function crop_edges(mask) 2 | %%%acts on int file 3 | %%% mask = 1x4 vector of left,right, top,bottom to mask, 0 or larger 4 | if(mask<0) 5 | disp('mask values must be between 0 and nx,ny. Use full res width/length') 6 | return 7 | end 8 | if(length(mask)~=4) 9 | disp('mask must have 4 values'); 10 | return 11 | end 12 | 13 | masklft=mask(1); 14 | maskrgt=mask(2); 15 | masktop=mask(3); 16 | maskbot=mask(4); 17 | 18 | set_params 19 | 20 | if(or(or(or(masklft>nx,maskrgt>nx),masktop>ny),maskbot>ny)) 21 | disp('masks must be smaller than size of image') 22 | return 23 | end 24 | 25 | for l=1:length(rlooks) 26 | id1=ceil(masklft/rlooks); 27 | id2=floor((newnx-maskrgt/rlooks)); 28 | id3=ceil(masktop/alooks); 29 | id4=floor(newny-maskbot/alooks); 30 | for k=1:nints 31 | if(exist([ints(k).flatrlk],'file')) 32 | fidi=fopen([ints(k).flatrlk],'r'); 33 | fido=fopen([ints(k).flatrlk 'tmp'],'w'); 34 | for j=1:newny 35 | tmp=fread(fidi,newnx,'real*4'); 36 | tmp(1:id1)=0; 37 | tmp(id2:end)=0; 38 | if(and(j>=id3,j<=id4)) 39 | fwrite(fido,tmp,'real*4'); 40 | else 41 | fwrite(fido,zeros(1,newnx),'real*4'); 42 | end 43 | end 44 | fclose(fido); 45 | fclose(fidi); 46 | movefile([ints(k).flatrlk 'tmp'],[ints(k).flatrlk]); 47 | 48 | else 49 | disp([ints(k).flatrlk does not exist']); 50 | end 51 | end 52 | end 53 | 54 | -------------------------------------------------------------------------------- /crop_edges_dates.m: -------------------------------------------------------------------------------- 1 | function crop_edges_dates(mask) 2 | %%%acts on int file 3 | %%% mask = 1x4 vector of left,right, top,bottom to mask, 0 or larger 4 | if(mask<0) 5 | disp('mask values must be between 0 and nx,ny. Use full res width/length') 6 | return 7 | end 8 | if(length(mask)~=4) 9 | disp('mask must have 4 values'); 10 | return 11 | end 12 | 13 | masklft=mask(1); 14 | maskrgt=mask(2); 15 | masktop=mask(3); 16 | maskbot=mask(4); 17 | 18 | set_params 19 | load(ts_paramfile); 20 | 21 | ndates = length(dates); 22 | nints = length(ints); 23 | 24 | if strcmp(sat,'S1A') 25 | nx=ints(id).width; 26 | ny=ints(id).length; 27 | else 28 | [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 29 | 30 | end 31 | newnx = floor(nx./rlooks) 32 | newny = floor(ny./alooks); 33 | 34 | if(or(or(or(masklft>nx,maskrgt>nx),masktop>ny),maskbot>ny)) 35 | disp('masks must be smaller than size of image') 36 | return 37 | end 38 | 39 | for l=1:length(rlooks) 40 | id1=ceil(masklft/rlooks(l));%x2 for cpx files 41 | id2=floor((newnx(l)-maskrgt/rlooks(l))); 42 | id3=ceil(masktop/alooks(l)); 43 | id4=floor(newny(l)-maskbot/alooks(l)); 44 | for k=1%:ndates 45 | if(exist(dates(k).unwrlk{1},'file')) 46 | fidi=fopen(dates(k).unwrlk{1},'r'); 47 | fido=fopen([dates(k).unwrlk{1} 'tmp'],'w'); 48 | for j=1:newny(l) 49 | tmp=fread(fidi,newnx(l),'real*4'); 50 | tmp(1:id1)=0; 51 | tmp(id2:end)=0; 52 | if(and(j>=id3,j<=id4)) 53 | fwrite(fido,tmp,'real*4'); 54 | else 55 | fwrite(fido,zeros(1,newnx(l)),'real*4'); 56 | end 57 | end 58 | fclose(fido); 59 | fclose(fidi); 60 | movefile([dates(k).unwrlk{1} 'tmp'],dates(k).unwrlk{1}); 61 | end 62 | end 63 | end 64 | 65 | -------------------------------------------------------------------------------- /filter_diff.m: -------------------------------------------------------------------------------- 1 | function filter_diff(infile,filtfile,difffile,nx,ny,rx,ry) 2 | 3 | buffer = 2000; %lines 4 | nbuf = 1+ceil((ny-(buffer+ry))/buffer); 5 | 6 | fid1 = fopen(infile,'r'); 7 | fid3 = fopen(filtfile,'w'); 8 | fid4 = fopen(difffile,'w'); 9 | im = sqrt(-1); 10 | 11 | rea_buff = zeros(buffer+ry*2,nx); 12 | ima_buff = zeros(buffer+ry*2,nx); 13 | 14 | %h=fspecial('gaussian',[ry rx],1); 15 | tot=0; 16 | for l=1:nbuf 17 | % for l=1:4 18 | disp(['reading in buffer ' num2str(l) '/' num2str(nbuf)]); 19 | rbuff = buffer; 20 | keep = buffer+1; 21 | stopat = buffer+ry; 22 | tot = tot+buffer; 23 | if(l==1) 24 | rbuff = buffer+ry; 25 | keep = buffer+ry+1; 26 | elseif(tot>ny) 27 | stopat =rem(ny,buffer)+ry; 28 | end 29 | 30 | int_in = zeros(nx,rbuff); 31 | [tmp,count] = fread(fid1,[nx,rbuff],'real*4'); 32 | int_in(1:count) = tmp; 33 | 34 | clear tmp 35 | rea_buff=[rea_buff(keep:end,:);cos(int_in')]; 36 | ima_buff=[ima_buff(keep:end,:);sin(int_in')]; 37 | clear int_in 38 | 39 | disp('Processing'); 40 | 41 | rfilt=imgaussfilt(rea_buff,[ry rx]); 42 | ifilt=imgaussfilt(ima_buff,[ry rx]); 43 | 44 | cpx0 = rea_buff+im*ima_buff; 45 | cpxf = rfilt +im*ifilt; 46 | cpx0 = cpx0./abs(cpx0); 47 | cpxf = cpxf./abs(cpxf); 48 | phsdiff = cpx0.*conj(cpxf); 49 | fwrite(fid3,angle(cpxf(ry+1:stopat,:))','real*4'); 50 | fwrite(fid4,angle(phsdiff(ry+1:stopat,:))','real*4'); 51 | end 52 | fclose('all'); 53 | 54 | end 55 | 56 | -------------------------------------------------------------------------------- /filter_diff_iter.m: -------------------------------------------------------------------------------- 1 | function filter_diff_iter(infile,filtfile,difffile,nx,ny,rx,ry,gammafile,gammathresh) 2 | if(~exist(difffile,'file')) 3 | buffer = 2000; %lines 4 | nbuf = 1+ceil((ny-(buffer+ry))/buffer); 5 | 6 | fid1 = fopen(infile,'r'); 7 | fid3 = fopen(filtfile,'w'); 8 | fid4 = fopen(difffile,'w'); 9 | fid5 = fopen(gammafile,'r'); 10 | im = sqrt(-1); 11 | 12 | gam_buff = zeros(buffer+ry*2,nx); 13 | rea_buff = zeros(buffer+ry*2,nx); 14 | ima_buff = zeros(buffer+ry*2,nx); 15 | 16 | tot=0; 17 | for l=1:nbuf 18 | disp(['reading in buffer ' num2str(l) '/' num2str(nbuf)]); 19 | rbuff = buffer; 20 | keep = buffer+1; 21 | stopat = buffer+ry; 22 | tot = tot+buffer; 23 | if(l==1) 24 | rbuff = buffer+ry; 25 | keep = buffer+ry+1; 26 | elseif(tot>ny) 27 | stopat =rem(ny,buffer)+ry; 28 | end 29 | 30 | int_in = zeros(nx,rbuff); 31 | [tmp,count] = fread(fid1,[nx,rbuff],'real*4'); 32 | int_in(1:count) = tmp; 33 | 34 | clear tmp 35 | rea_buff=[rea_buff(keep:end,:);cos(int_in')]; 36 | ima_buff=[ima_buff(keep:end,:);sin(int_in')]; 37 | 38 | gam_in = zeros(nx,rbuff); 39 | [tmp,count] = fread(fid5,[nx,rbuff],'real*4'); 40 | gam_in(1:count) = tmp; 41 | gam_in = gam_in>gammathresh; %keep high gamma values (high coherence) 42 | gam_buff = [gam_buff(keep:end,:);gam_in']; 43 | 44 | disp('Processing'); 45 | bad = ~gam_buff; %bad are all pixels that are higher than threshold variance. make these 0 magnitude complex vectors. 46 | rea_buff(bad) = 0; 47 | ima_buff(bad) = 0; 48 | 49 | rfilt = imgaussfilt(rea_buff,[ry rx]); 50 | ifilt = imgaussfilt(ima_buff,[ry rx]); 51 | mfilt = imgaussfilt(gam_buff,[ry rx]); %filtering gamma image as well 52 | 53 | rfilt = rfilt./mfilt; %high phase variance is penalized 54 | ifilt = ifilt./mfilt; 55 | 56 | cpx0 = rea_buff+im*ima_buff; 57 | cpxf = rfilt +im*ifilt; 58 | % cpx0 = cpx0./abs(cpx0); %this makes the complex vector a unit vector? 59 | % cpxf = cpxf./abs(cpxf); 60 | phsdiff = cpx0.*conj(cpxf); 61 | fwrite(fid3,angle(cpxf(ry+1:stopat,:))','real*4'); 62 | fwrite(fid4,angle(phsdiff(ry+1:stopat,:))','real*4'); 63 | end 64 | fclose('all'); 65 | 66 | else 67 | disp(['filtering ' infile ' already done']); 68 | end 69 | 70 | -------------------------------------------------------------------------------- /filter_diff_iter_km.m: -------------------------------------------------------------------------------- 1 | function filter_diff_iter_km(infile,filtfile,difffile,nx,ny,rx,ry,gammafile,gammathresh) 2 | if(~exist(difffile,'file')) 3 | buffer = 2000; %lines 4 | nbuf = 1+ceil((ny-(buffer+ry))/buffer); 5 | 6 | fid1 = fopen(infile,'r'); 7 | fid3 = fopen(filtfile,'w'); 8 | fid4 = fopen(difffile,'w'); 9 | fid5 = fopen(gammafile,'r'); 10 | 11 | im = sqrt(-1); 12 | 13 | z = zeros(1,nx); 14 | rea_buff = zeros(buffer+ry*2,nx); 15 | ima_buff = zeros(buffer+ry*2,nx); 16 | 17 | tot=0; 18 | for l=1:nbuf 19 | disp(['reading in buffer ' num2str(l) '/' num2str(nbuf)]); 20 | rbuff = buffer; 21 | keep = buffer+1; 22 | stopat = buffer+ry; 23 | tot = tot+buffer; 24 | if(l==1) 25 | rbuff = buffer+ry; 26 | keep = buffer+ry+1; 27 | elseif(tot>ny) 28 | stopat =rem(ny,buffer)+ry; 29 | end 30 | 31 | % disp(['buffer ' num2str(l) ' ' num2str(stopat) ' ' num2str(tot)]) 32 | a = zeros(nx,rbuff); 33 | [tmp,count] = fread(fid1,[nx,rbuff],'real*4'); 34 | a(1:count) = tmp; 35 | 36 | clear tmp 37 | rea_buff=[rea_buff(keep:end,:);cos(a')]; 38 | ima_buff=[ima_buff(keep:end,:);sin(a')]; 39 | clear a 40 | 41 | a = zeros(nx,rbuff); 42 | [tmp,count] = fread(fid5,[nx,rbuff],'real*4'); 43 | a(1:count) = tmp; 44 | a = any) 31 | stopat =rem(ny,buffer)+ry; 32 | end 33 | 34 | %disp(['rbuf ' num2str(rbuff) ' keep ' num2str(keep) ' stopat ' num2str(stopat) ' ' num2str(tot)]) 35 | a = zeros(nx,rbuff); 36 | [tmp,count] = fread(fid1,[nx,rbuff],'real*4'); 37 | a(1:count) = tmp; 38 | 39 | clear tmp 40 | phs_buff = [phs_buff(keep:end,:);a']; 41 | rea = cos(phs_buff); 42 | ima = sin(phs_buff); 43 | 44 | a = zeros(nx,rbuff); 45 | [tmp,count] = fread(fid5,[nx,rbuff],'real*4'); 46 | a(1:count) = tmp; 47 | a = any) 31 | stopat =rem(ny,buffer)+ry; 32 | end 33 | 34 | disp(['buffer ' num2str(l) ' ' num2str(stopat) ' ' num2str(tot)]) 35 | a = zeros(nx,rbuff); 36 | [tmp,count] = fread(fid1,[nx,rbuff],'real*4'); 37 | a(1:count) = tmp; 38 | 39 | clear tmp 40 | rea_buff=[rea_buff(keep:end,:);cos(a')]; 41 | ima_buff=[ima_buff(keep:end,:);sin(a')]; 42 | clear a 43 | 44 | disp('Processing'); 45 | % rfilt = imfilter(rea_buff,gaus,'same'); 46 | % ifilt = imfilter(ima_buff,gaus,'same'); 47 | % rfilt=imgaussfilt(rea_buff,[ry rx]); 48 | % ifilt=imgaussfilt(ima_buff,[ry rx]); 49 | %rfilt=medfilt2(rea_buff); 50 | %ifilt=medfilt2(ima_buff); 51 | rfilt=wiener2(rea_buff); 52 | ifilt=wiener2(ima_buff); 53 | % rfilt = imfilter(rea_buff,h,'same'); 54 | % ifilt = imfilter(ima_buff,h,'same'); 55 | 56 | cpx0 = rea_buff+im*ima_buff; 57 | cpxf = rfilt +im*ifilt; 58 | cpx0 = cpx0./abs(cpx0); 59 | cpxf = cpxf./abs(cpxf); 60 | phsdiff = cpx0.*conj(cpxf); 61 | % fwrite(fid3,rea_buff(ry+1:stopat,:)','real*4'); 62 | % fwrite(fid4,rfilt(ry+1:stopat,:)','real*4'); 63 | fwrite(fid3,angle(cpxf(ry+1:stopat,:))','real*4'); 64 | fwrite(fid4,angle(phsdiff(ry+1:stopat,:))','real*4'); %need to fix so that doesn't run over ny 65 | end 66 | fclose('all'); 67 | 68 | end 69 | 70 | -------------------------------------------------------------------------------- /filter_diff_remove_restore.m: -------------------------------------------------------------------------------- 1 | function filter_diff_remove_restore(infile,filtratefile,filtfile,difffile,rateremfile,nx,ny,rx,ry,dt,lambda) 2 | 3 | buffer = 2000; %lines 4 | nbuf = 1+ceil((ny-(buffer+ry))/buffer); 5 | 6 | fid1 = fopen(infile,'r'); 7 | fid2 = fopen(filtratefile,'r'); 8 | fid3 = fopen(filtfile,'w'); 9 | fid4 = fopen(difffile,'w'); 10 | fid5 = fopen(rateremfile,'w'); 11 | im = sqrt(-1); 12 | 13 | z = zeros(1,nx); 14 | rea_buff = zeros(buffer+ry*2,nx); 15 | ima_buff = zeros(buffer+ry*2,nx); 16 | rate_buff = zeros(buffer+ry*2,nx); 17 | gausx=exp(-[-rx:rx].^2/2/rx^2); 18 | gausy=exp(-(-ry:ry).^2/2/ry^2); 19 | gaus=gausy'*gausx; 20 | gaus=gaus/sum(gaus(:)); 21 | %h=fspecial('gaussian',[ry rx],1); 22 | tot=0; 23 | for l=1:nbuf 24 | disp(['reading in buffer ' num2str(l) '/' num2str(nbuf)]); 25 | rbuff = buffer; 26 | keep = buffer+1; 27 | stopat = buffer+ry; 28 | tot = tot+buffer; 29 | if(l==1) 30 | rbuff = buffer+ry; 31 | keep = buffer+ry+1; 32 | elseif(tot>ny) 33 | stopat =rem(ny,buffer)+ry; 34 | end 35 | 36 | disp(['buffer ' num2str(l) ' ' num2str(stopat) ' ' num2str(tot)]) 37 | a = zeros(nx,rbuff); 38 | [tmp,count] = fread(fid1,[nx,rbuff],'real*4'); 39 | a(1:count) = tmp; 40 | 41 | clear tmp 42 | rea_buff=[rea_buff(keep:end,:);cos(a')]; 43 | ima_buff=[ima_buff(keep:end,:);sin(a')]; 44 | clear a 45 | 46 | a = zeros(nx,rbuff); 47 | [tmp,count] = fread(fid2,[nx,rbuff],'real*4'); 48 | tmp= tmp*(4*pi*dt)/(100*365*lambda); %radians/time interval (dt) 49 | a(1:count) = tmp; 50 | ratebuff=[rate_buff(keep:end,:);a']; 51 | 52 | 53 | cpx=rea_buff+im*ima_buff; 54 | cpxrate=exp(im*ratebuff); 55 | cpxdiff=cpx.*conj(cpxrate); 56 | 57 | rea_diff=real(cpxdiff); 58 | ima_diff=imag(cpxdiff); 59 | 60 | disp('Processing'); 61 | % rfilt = imfilter(rea_buff,gaus,'same'); 62 | % ifilt = imfilter(ima_buff,gaus,'same'); 63 | rfilt=imgaussfilt(rea_diff,[ry rx]); 64 | ifilt=imgaussfilt(ima_diff,[ry rx]); 65 | %rfilt=medfilt2(rea_buff); 66 | %ifilt=medfilt2(ima_buff); 67 | % rfilt=wiener2(rea_buff); 68 | % ifilt=wiener2(ima_buff); 69 | % rfilt = imfilter(rea_buff,h,'same'); 70 | % ifilt = imfilter(ima_buff,h,'same'); 71 | 72 | %cpx0 = rea_buff+im*ima_buff; 73 | cpxf = rfilt +im*ifilt; 74 | cpx0 = cpxdiff./abs(cpxdiff); 75 | cpxf = cpxf./abs(cpxf); 76 | phsdiff = cpx0.*conj(cpxf); 77 | % fwrite(fid3,rea_buff(ry+1:stopat,:)','real*4'); 78 | % fwrite(fid4,rfilt(ry+1:stopat,:)','real*4'); 79 | fwrite(fid3,angle(cpxf(ry+1:stopat,:))','real*4'); 80 | fwrite(fid4,angle(phsdiff(ry+1:stopat,:))','real*4'); %need to fix so that doesn't run over ny 81 | fwrite(fid5,angle(cpxdiff(ry+1:stopat,:))','real*4'); 82 | end 83 | fclose('all'); 84 | 85 | end 86 | 87 | -------------------------------------------------------------------------------- /filter_rlooks.m: -------------------------------------------------------------------------------- 1 | function filter_rlooks(filter_strength) 2 | 3 | % filter int 4 | % unwrap filtered int 5 | % subtract unwrapped from filtered int - this should give you just near-integers *2pi (it won't be exact, though) 6 | % add 2pi*n field to unfiltered int. 7 | 8 | set_params 9 | load(ts_paramfile); 10 | 11 | ndates = length(dates); 12 | nints = length(ints); 13 | 14 | if strcmp(sat,'S1A') 15 | nx=ints(id).width; 16 | ny=ints(id).length; 17 | else 18 | [nx,ny,lambda] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH','WAVELENGTH'); 19 | 20 | end 21 | newnx = floor(nx./rlooks) 22 | newny = floor(ny./alooks); 23 | 24 | if strcmp(sat,'ALOS') 25 | mysys(['cp /data/kdm95/ALOS_T211_650/TS/looks4/4rlks.rsc ' rlkdir{1}]) 26 | end 27 | 28 | %make a 4 rlks rsc file 29 | file=[rlkdir{1} '4rlks.rsc']; 30 | fid=fopen(file,'w'); 31 | fprintf(fid,[ 'WAVELENGTH ' num2str(lambda) ' \n' ]); 32 | fprintf(fid,[ 'XMIN ' num2str(0) ' \n' ]); 33 | fprintf(fid,[ 'XMAX ' num2str(newnx) ' \n' ]); 34 | fprintf(fid,[ 'WIDTH ' num2str(newnx) ' \n' ]); 35 | fprintf(fid,[ 'YMIN ' num2str(0) ' \n' ]); 36 | fprintf(fid,[ 'YMAX ' num2str(newny) ' \n' ]); 37 | 38 | for i=1:length(ints) 39 | ints(i).filtrlk= [rlkdir{1} 'filt_flat_' ints(i).name '_' num2str(rlooks) 'rlks.int']; 40 | ints(i).filtunwrlk= [rlkdir{1} 'filt_flat_' ints(i).name '_' num2str(rlooks) 'rlks.unw']; 41 | 42 | in=ints(i).flatrlk{1}; 43 | mysys(['cp ' rlkdir{1} '4rlks.rsc ' in '.rsc']) 44 | mysys(['cp ' in ' orig_' in]) 45 | out=[ints(i).filtrlk]; 46 | command=['filter.pl ' in(1:end-4) ' ' num2str(filter_strength) ' psfilt ' out(1:end-4)]; 47 | mysys(command); 48 | end 49 | 50 | save(ts_paramfile,'ints','dates') 51 | 52 | 53 | %Now unwrap the filtered ints 54 | for l=1:1%length(rlooks) 55 | for k=1:nints 56 | % if(~exist(ints(k).unwrlk{1},'file')) 57 | command=['snaphu -s ' ints(k).filtrlk ' ' num2str(newnx(l)) ' -o ' ints(k).unwrlk{1} ' -c ' rlkdir{l} 'mask.cor --tile 6 6 100 100']; 58 | mysys(command); 59 | % end 60 | end 61 | end 62 | 63 | %now load in the filtered int and the filtered unwrapped int and difference 64 | %them to get 2pi*integer 65 | -------------------------------------------------------------------------------- /filter_unw_diff.m: -------------------------------------------------------------------------------- 1 | function filter_unw_diff(filter_strength) 2 | % filter_strength = 2; 3 | % filter int 4 | % unwrap filtered int 5 | % subtract unwrapped from filtered int - this should give you just near-integers *2pi (it won't be exact, though) 6 | % add 2pi*n field to unfiltered int. 7 | 8 | set_params 9 | load(ts_paramfile); 10 | 11 | ndates = length(dates); 12 | nints = length(ints); 13 | 14 | if strcmp(sat,'S1A') 15 | nx=ints(id).width; 16 | ny=ints(id).length; 17 | else 18 | [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 19 | 20 | end 21 | newnx = floor(nx./rlooks) 22 | newny = floor(ny./alooks); 23 | % 24 | % if strcmp(sat,'ALOS') 25 | % mysys(['cp /data/kdm95/ALOS_T211_650/TS/looks4/4rlks.rsc ' rlkdir{1}]) 26 | % end 27 | 28 | %write the .rsc file 29 | out=[rlkdir{1} '4rlks.rsc']; 30 | fid = fopen(out,'wt+'); 31 | fprintf(fid,'WIDTH %s\n',num2str(newnx)); 32 | fprintf(fid,'FILE_LENGTH %s\n',num2str(newny)); 33 | fprintf(fid,'XMIN %s\n',num2str(0)); 34 | fprintf(fid,'XMAX %s\n',num2str(newnx)); 35 | fprintf(fid,'YMIN %s\n',num2str(0)); 36 | fprintf(fid,'YMAX %s\n',num2str(newny)); 37 | fclose(fid) 38 | 39 | 40 | for i=1:length(ints) 41 | ints(i).filtrlk= [rlkdir{1} 'filt_flat_' ints(i).name '_' num2str(rlooks) 'rlks.int']; 42 | ints(i).filtunwrlk= [rlkdir{1} 'filt_flat_' ints(i).name '_' num2str(rlooks) 'rlks.unw']; 43 | 44 | in=ints(i).flatrlk{1}; 45 | mysys(['cp ' rlkdir{1} '4rlks.rsc ' in '.rsc']) 46 | mysys(['cp ' in ' orig_' in]) 47 | out=[ints(i).filtrlk]; 48 | command=['filter.pl ' in(1:end-4) ' ' num2str(filter_strength) ' psfilt ' out(1:end-4)]; 49 | mysys(command); 50 | end 51 | 52 | save(ts_paramfile,'ints','dates') 53 | 54 | 55 | %Now unwrap the filtered ints 56 | for l=1:length(rlooks) 57 | for k=1:nints 58 | % if(~exist(ints(k).unwrlk{1},'file')) 59 | command=['snaphu -s ' ints(k).filtrlk ' ' num2str(newnx(l)) ' -o ' ints(k).filtunwrlk ' -c ' rlkdir{l} 'mask.cor --tile 6 6 100 100']; 60 | mysys(command); 61 | % end 62 | end 63 | end 64 | 65 | %now load in the filtered int and the filtered unwrapped int and difference 66 | %them to get 2pi*integer 67 | for k=1:nints 68 | un_filt=ints(k).flatrlk{1}; 69 | filt_int=ints(k).filtrlk; 70 | filt_unw=ints(k).filtunwrlk; 71 | %split into just phs 72 | mysys(['cpx2mag_phs ' un_filt ' mag0 phs0 ' num2str(newnx)]) 73 | mysys(['cpx2mag_phs ' filt_int ' mag1 phs1 ' num2str(newnx)]) 74 | mysys(['rmg2mag_phs ' filt_unw ' mag2 phs2 ' num2str(newnx)]) 75 | fid0=fopen('phs0','r','native'); 76 | fid1=fopen('phs1','r','native'); 77 | fid2=fopen('phs2','r','native'); 78 | if ~exist([ints(k).unwrlk{1} '_orig']) 79 | command=['mv ' ints(k).unwrlk{1} ' ' ints(k).unwrlk{1} '_orig']; 80 | mysys(command) 81 | command=['rm ' ints(k).unwrlk{1}]; 82 | mysys(command) 83 | end 84 | fid3=fopen('phs','w'); 85 | 86 | f0=fread(fid0,[newnx,newny/2],'real*4'); 87 | f1=fread(fid1,[newnx,newny/2],'real*4'); 88 | f2=fread(fid2,[newnx,newny/2],'real*4'); 89 | f3=f2-f1; 90 | f3(find(f3<1))=0; 91 | f4=f0+f3; 92 | fwrite(fid3,f4,'real*4') 93 | mysys(['mag_phs2rmg mag0 phs ' ints(k).unwrlk{1} ' ' num2str(newnx)]) 94 | fclose('all') 95 | !rm mag* phs* 96 | end 97 | %plot the last one as example 98 | figure 99 | subplot(2,2,1);imagesc(f0);axis image;title('unfiltered interferogram');colorbar 100 | subplot(2,2,2);imagesc(f1);axis image;title('filtered interferogram');colorbar 101 | subplot(2,2,3);imagesc(f2);axis image;title('unwrapped filtered interferogram');colorbar 102 | subplot(2,2,4);imagesc(f3);axis image;title('difference');colorbar 103 | figure 104 | imagesc(f4);axis image;title('unfiltered interferogram + 2pi*n values');colorbar -------------------------------------------------------------------------------- /filtflat0.m: -------------------------------------------------------------------------------- 1 | function filtflat0(ii,nx,ny,rx,ry) 2 | set_params 3 | tmp=dir(ints(ii).flat); %is output from diffnsim with phase+amp, we just want phase. 4 | if(tmp.bytes==nx*ny*4) 5 | disp([ints(ii).flat ' already split to one band']) 6 | elseif(tmp.bytes==nx*ny*8) 7 | disp(['splitting ' ints(ii).flat ' into just phs']) 8 | command=['cpx2mag_phs ' ints(ii).flat ' mag ' ints(ii).flat '_phs ' num2str(nx)]; 9 | mysys(command); 10 | command=['mv ' ints(ii).flat '_phs ' ints(ii).flat]; 11 | mysys(command); 12 | else 13 | disp([ints(ii).flat ' wrong size?']) 14 | return 15 | end 16 | % filter_diff(ints(ii).flat,[ints(ii).flat '_filtrate'],[ints(ii).flat '_filt'],[ints(ii).flat '_diff'],[ints(ii).flat 'raterem'], nx,ny,rx,ry); 17 | filter_diff(ints(ii).flat,[ints(ii).flat '_filt'],[ints(ii).flat '_diff'], nx,ny,rx,ry); -------------------------------------------------------------------------------- /filtflat1.m: -------------------------------------------------------------------------------- 1 | function filtflat1(i,nx,ny,rx,ry) 2 | getstuff 3 | system(['rm ' ints(i).flat '_filt ' ints(i).flat '_diff']); 4 | filter_diff_iter(ints(i).flat,[ints(i).flat '_filt'],[ints(i).flat '_diff'], nx,ny,rx,ry,gammafile,1.5); 5 | snapnow; -------------------------------------------------------------------------------- /filtflat2.m: -------------------------------------------------------------------------------- 1 | function filtflat2(i,nx,ny,rx,ry) 2 | %called by FilterFlat.m 3 | getstuff 4 | % if(exist([ints(i).flat '_raterem'],'var')) 5 | % disp(['skipping ' ints(i).name]); 6 | % else 7 | tmp=dir(ints(i).flat); %is output from diffnsim with phase+amp, we just want phase. 8 | if(tmp.bytes==nx*ny*4) 9 | disp([ints(i).flat ' already split to one band']) 10 | elseif(tmp.bytes==nx*ny*8) 11 | disp(['splitting ' ints(i).flat ' into just phs']) 12 | command=['cpx2mag_phs ' ints(i).flat ' mag phs ' num2str(nx)]; 13 | mysys(command); 14 | command=['mv phs ' ints(i).flat]; 15 | mysys(command); 16 | else 17 | disp([ints(i).flat ' wrong size?']) 18 | return 19 | end 20 | filter_diff_remove_restore(ints(i).flat,'rate_phs',[ints(i).flat '_filt'],[ints(i).flat '_diff'],[ints(i).flat '_raterem'], nx,ny,rx,ry,ints(i).dt,lambda); 21 | % end -------------------------------------------------------------------------------- /filtflat3.m: -------------------------------------------------------------------------------- 1 | function filtflat3(i,nx,ny,rx,ry) 2 | getstuff 3 | system(['rm ' ints(i).flat '_filt ' ints(i).flat '_diff']); 4 | filter_diff_iter([ints(i).flat '_raterem'],[ints(i).flat '_filt'],[ints(i).flat '_diff'], nx,ny,rx,ry,gammafile,1.5); 5 | snapnow; -------------------------------------------------------------------------------- /fitramp_int_s1a.m: -------------------------------------------------------------------------------- 1 | function fitramp(thresh,edge,waterheight,topoflag,boxedge,degree)%boxedge and edge must be defined in full res units 2 | %degree: 0=just offset, 1=planar ramp, 2=quadratic 3 | 4 | %topoflag can be 0 because no trees. if topoflag is set, the ramp 5 | %includes topo 6 | % thresh = 1; %this currently needs to be "big" for the first round, then iterate once or twice with smaller values. 7 | % edge = [10 10 10 10]; %pixels from left, right, top, bottom edges to mask 8 | % waterheight = []; %mask all pixels with height < waterheight. 9 | % topoflag = []; 10 | % boxedge = [0 0 0 0];%[2437 2636 1357 1582]*4; 11 | % degree = 2; 12 | 13 | 14 | set_params 15 | load(ts_paramfile); 16 | 17 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 18 | 19 | if(isempty(edge)) 20 | edge=[0 0 0 0]; %offset from left, right, top bottom 21 | end 22 | if(isempty(waterheight)) 23 | waterheight=-3000; 24 | end 25 | if(isempty(boxedge)) 26 | boxedge=[0 0 0 0]; 27 | end 28 | % fullresheightfile=[oldintdir 'radar.hgt']; 29 | % if(~exist(fullresheightfile)) 30 | % disp(['full res height file should exist: ' fullresheightfile]) 31 | % return 32 | % end 33 | 34 | 35 | for l=1:length(rlooks) 36 | % ratefile=['rates_' num2str(rlooks(l))]; 37 | % if(exist(ratefile)) 38 | % disp('removing rate file as well in ramp fit') 39 | % userate = 1; 40 | % fidrate = fopen(ratefile,'r'); 41 | % tmp = fread(fidrate,[newnx(l),newny(l)],'real*4'); 42 | % ratemap = tmp'*lambda*(4*pi)/1000/365;%conver rate back to radians/day 43 | % fclose(fidrate); 44 | % else 45 | % userate =0; 46 | % end 47 | userate=0; %realized that after first iteration, will introduce whatever ramp is in rate file 48 | [X,Y]=meshgrid(1:newnx(l),1:newny(l)); 49 | 50 | % lookheightfile=[oldintdir 'radar_' num2str(rlooks(l)) 'rlks.hgt']; 51 | % if(~exist(lookheightfile)) 52 | % 53 | % %---------------------------------------------------------- 54 | % %KM Edit: There is no rsc file for the look.pl command (looking for 55 | % %radar.hgt.rsc) 56 | % command1=['cp ' oldintdir 'reference.hgt.rsc ' oldintdir 'radar.hgt.rsc']; 57 | % mysys(command1); 58 | % %---------------------------------------------------------- 59 | % 60 | % 61 | % command=['look.pl ' fullresheightfile ' ' num2str(rlooks(l)) ' ' num2str(rlooks(l)*pixel_ratio)]; 62 | % mysys(command); 63 | % end 64 | dem_file=[ints(intid).dir '/dem.crop']; 65 | fiddem = fopen(dem_file,'r'); 66 | tmp = fread(fiddem,[newnx(l),newny(l)],'int16'); 67 | dem = tmp'; 68 | fclose(fiddem); 69 | 70 | fidmask = fopen(['res_'],'r'); 71 | tmp = fread(fidmask,[newnx(l),newny(l)],'real*4'); 72 | stdmask = tmp'; 73 | stdmask(stdmask>thresh)=NaN; 74 | fclose(fidmask); 75 | 76 | id1=floor(edge(1)/rlooks(l)); 77 | id2=floor(edge(2)/rlooks(l)); 78 | id3=floor(edge(3)/alooks(l)); 79 | id4=floor(edge(4)/alooks(l)); 80 | alooks(l); 81 | 82 | edgemask = ones(size(stdmask)); 83 | edgemask(:,1:1+id1)=NaN; %mask left 84 | edgemask(:,end-id2:end)=NaN; %mask right 85 | edgemask(1:1+id3,:)=NaN; %mask top 86 | edgemask(end-id4:end,:)=NaN; %mask bottom 87 | 88 | id1=ceil(boxedge(1)/rlooks(l)); 89 | id2=floor(boxedge(2)/rlooks(l)); 90 | id3=ceil(boxedge(3)/alooks(l)); 91 | id4=floor(boxedge(4)/alooks(l)); 92 | 93 | % boxmask = ones(size(stdmask)); 94 | % boxmask(:,1:id1)=NaN; %mask left 95 | % boxmask(:,id2:end)=NaN; %mask right 96 | % boxmask(1:1+id3,:)=NaN; %mask top 97 | % boxmask(id4:end,:)=NaN; %mask bottom 98 | 99 | % boxmask=isnan(boxmask); %reverse sense of boxmask 100 | 101 | watermask = dem; 102 | watermask(watermask0) 35 | tmp = fread(fidr1,nx*2,'real*4'); 36 | a1 = tmp(1:nx); 37 | r1 = tmp(nx+[1:nx]); 38 | mr1 = a1==0; %mask out areas where ramp 1 is 0 39 | else %no ramp file (i.e., is master date) 40 | r1 = zeros(nx,1); 41 | mr1 = false(size(r1)); 42 | end 43 | 44 | if(fidr2>0) 45 | tmp = fread(fidr2,nx*2,'real*4'); 46 | a2 = tmp(1:nx); 47 | r2 = tmp(nx+[1:nx]); 48 | mr2 = a2==0; 49 | else %no ramp file (i.e., is master date) 50 | r2 = zeros(nx,1); 51 | mr2 = false(size(r2)); 52 | end 53 | 54 | mask = (mint | mr1 | mr2); %should be true where masked 55 | 56 | dr = r2-r1; 57 | dr = exp(im*dr); 58 | newint = int.*conj(dr); 59 | 60 | newint = angle(newint); 61 | newint(mask)=0; 62 | 63 | fwrite(fido,newint,'real*4'); 64 | end 65 | fclose('all'); 66 | 67 | else 68 | disp([ints(i).flat ' already made']); 69 | end 70 | end 71 | 72 | 73 | -------------------------------------------------------------------------------- /geocode.m: -------------------------------------------------------------------------------- 1 | function geocode(unw_in, out_2lks) 2 | 3 | getstuff 4 | if rlooks==2 5 | % system('cp int_????????_????????/????????-????????_2rlks.cor.rsc rates_2.rsc') 6 | % lookup_file = 'GEO/geomap_2rlks.trans'; 7 | % system(['geocode.pl ' lookup_file ' rates_2 ' out_2lks]); 8 | 9 | 10 | froot=unw_in; 11 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 12 | 13 | [nx,ny,lambda] = load_rscs(dates(id).slc,'WIDTH','YMAX','WAVELENGTH'); 14 | newnx = floor(nx./rlooks) 15 | newny = floor(ny./alooks); 16 | 17 | %Step 1. Create rmg file 18 | amp_img = unw_in; 19 | phs_img = unw_in; 20 | rmg_in = [froot '_4rlks.unw']; 21 | 22 | mysys(['mag_phs2rmg ' amp_img ' ' phs_img ' ' rmg_in ' ' num2str(newnx)]) 23 | [nx2,ny2] = load_rscs([oldintdir 'radar_2rlks.hgt'],'WIDTH','FILE_LENGTH'); 24 | 25 | aff1=1; 26 | aff2=aff1/pixel_ratio; 27 | 28 | %Step 2. Create a 2-looks version 29 | %first, write out a rect_back.in file 30 | fid=fopen('rect_back.in','w'); 31 | 32 | fprintf(fid,['Input Image File Name (-) = ' rmg_in ' ! dimension of file to be rectified\n']); 33 | fprintf(fid,['Output Image File Name (-) = rates_2.unw ! dimension of output\n']); 34 | fprintf(fid,['Input Dimensions (-) = ' num2str(newnx) ' ' num2str(newny) ' ! across, down\n']); 35 | fprintf(fid,['Output Dimensions (-) = ' num2str(nx2) ' ' num2str(ny2) ' ! across, down\n']); 36 | fprintf(fid,['Affine Matrix Row 1 (-) = ' num2str(aff1) ' 0 ! a b\n']); 37 | fprintf(fid,['Affine Matrix Row 2 (-) = 0 ' num2str(aff2) ' ! c d\n']); 38 | fprintf(fid,['Affine Offset Vector (-) = 0 0 ! e f\n']); 39 | fprintf(fid,['File Type (-) = RMG ! [RMG, COMPLEX]\n']); 40 | fprintf(fid,['Interpolation Method (-) = NN ! [NN, Bilinear, Sinc]\n']); 41 | 42 | %now, use rect rect_back.in to up look the 4looks rates file 43 | mysys(['rect rect_back.in']) 44 | %now we have a rates_2.unw to geocode 45 | 46 | %Step 3. Geocode 47 | system('cp int_????????_????????/????????-????????_2rlks.cor.rsc rates_2.unw.rsc') 48 | lookup_file = 'GEO/geomap_2rlks.trans'; 49 | command = ['geocode.pl ' lookup_file ' rates_2.unw ' out_2lks]; 50 | mysys(command) 51 | 52 | else 53 | 54 | %Geocode a 4rlks unw file 55 | froot=unw_in; 56 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 57 | 58 | [nx,ny,lambda] = load_rscs(dates(id).slc,'WIDTH','YMAX','WAVELENGTH'); 59 | newnx = floor(nx./rlooks) 60 | newny = floor(ny./alooks); 61 | 62 | %Step 1. Create rmg file 63 | amp_img = unw_in; 64 | phs_img = unw_in; 65 | rmg_in = [froot '_4rlks.unw']; 66 | 67 | mysys(['mag_phs2rmg ' amp_img ' ' phs_img ' ' rmg_in ' ' num2str(newnx)]) 68 | [nx2,ny2] = load_rscs([oldintdir 'radar_2rlks.hgt'],'WIDTH','FILE_LENGTH'); 69 | 70 | aff1=.5; 71 | aff2=aff1/pixel_ratio; 72 | 73 | %Step 2. Create a 2-looks version 74 | %first, write out a rect_back.in file 75 | fid=fopen('rect_back.in','w'); 76 | 77 | fprintf(fid,['Input Image File Name (-) = ' rmg_in ' ! dimension of file to be rectified\n']); 78 | fprintf(fid,['Output Image File Name (-) = rates_2.unw ! dimension of output\n']); 79 | fprintf(fid,['Input Dimensions (-) = ' num2str(newnx) ' ' num2str(newny) ' ! across, down\n']); 80 | fprintf(fid,['Output Dimensions (-) = ' num2str(nx2) ' ' num2str(ny2) ' ! across, down\n']); 81 | fprintf(fid,['Affine Matrix Row 1 (-) = ' num2str(aff1) ' 0 ! a b\n']); 82 | fprintf(fid,['Affine Matrix Row 2 (-) = 0 ' num2str(aff2) ' ! c d\n']); 83 | fprintf(fid,['Affine Offset Vector (-) = 0 0 ! e f\n']); 84 | fprintf(fid,['File Type (-) = RMG ! [RMG, COMPLEX]\n']); 85 | fprintf(fid,['Interpolation Method (-) = NN ! [NN, Bilinear, Sinc]\n']); 86 | 87 | %now, use rect rect_back.in to up look the 4looks rates file 88 | mysys(['rect rect_back.in']) 89 | %now we have a rates_2.unw to geocode 90 | 91 | %Step 3. Geocode 92 | system('cp int_????????_????????/????????-????????_2rlks.cor.rsc rates_2.unw.rsc') 93 | lookup_file = 'GEO/geomap_2rlks.trans'; 94 | command = ['geocode.pl ' lookup_file ' rates_2.unw ' out_2lks]; 95 | mysys(command) 96 | 97 | end -------------------------------------------------------------------------------- /geocode_dates.m: -------------------------------------------------------------------------------- 1 | clear all;close all 2 | getstuff 3 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 4 | 5 | % crop_edges_dates([1*4 1*4 30*8 1*8]); %full res location of crop (so that it works for multiple rlooks, left right, top, bottom. RLBT? 6 | 7 | for ii=1:length(dates) 8 | froot=[rlkdir{1} dates(ii).name]; 9 | unw_in=[froot '_' num2str(rlooks) 'rlks.r4']; 10 | 11 | %Step 1. Create rmg file 12 | amp_img = unw_in; 13 | phs_img = unw_in; 14 | rmg_out = [froot '_' num2str(rlooks) 'rlks_in.unw']; 15 | 16 | system(['mag_phs2rmg ' amp_img ' ' phs_img ' ' rmg_out ' ' num2str(newnx)]); 17 | [nx2,ny2] = load_rscs([oldintdir 'radar_2rlks.hgt'],'WIDTH','FILE_LENGTH'); 18 | 19 | aff1=1; %.5 20 | aff2=.5; %aff1/pixel_ratio; 21 | 22 | %Step 2. Create a 2-looks version 23 | %first, write out a rect_back.in file 24 | fid=fopen('rect_back.in','w'); 25 | 26 | fprintf(fid,['Input Image File Name (-) = ' rmg_out ' ! dimension of file to be rectified\n']); 27 | fprintf(fid,['Output Image File Name (-) = ' froot '_2rlks.unw ! dimension of output\n']); 28 | fprintf(fid,['Input Dimensions (-) = ' num2str(newnx) ' ' num2str(newny) ' ! across, down\n']); 29 | fprintf(fid,['Output Dimensions (-) = ' num2str(nx2) ' ' num2str(ny2) ' ! across, down\n']); 30 | fprintf(fid,['Affine Matrix Row 1 (-) = ' num2str(aff1) ' 0 ! a b\n']); 31 | fprintf(fid,['Affine Matrix Row 2 (-) = 0 ' num2str(aff2) ' ! c d\n']); 32 | fprintf(fid,['Affine Offset Vector (-) = 0 0 ! e f\n']); 33 | fprintf(fid,['File Type (-) = RMG ! [RMG, COMPLEX]\n']); 34 | fprintf(fid,['Interpolation Method (-) = NN ! [NN, Bilinear, Sinc]\n']); 35 | 36 | %Use rect rect_back.in to up look the 4looks rates file 37 | system(['rect rect_back.in']); 38 | 39 | %Step 3. Geocode 40 | %we need to copy a rsc file 41 | system(['cp ' masterdir 'int_????????_????????/????????-????????_2rlks.cor.rsc ' froot '_2rlks.unw.rsc']); 42 | lookup_file = 'GEO/geomap_2rlks.trans'; 43 | system(['geocode.pl ' lookup_file ' ' froot '_2rlks.unw ' froot '_geo_2rlks.unw']); 44 | % system(['look.pl ' froot '_geo_2rlks.unw' 12]); 45 | end -------------------------------------------------------------------------------- /get_int_offsets_s1a.m: -------------------------------------------------------------------------------- 1 | %Find offsets from ISCE output (in x and y) 2 | 3 | %find the middle values from offset files. Look at files in the 4 | %int_date_date/coarse_offsets/overlaps directory. We will take the value in 5 | %the files azimuth_bot_01_02.off and range_bot_01_02.off. Just need to 6 | %read in the first line and take the median value. 7 | clear all;close all 8 | set_params 9 | load(ts_paramfile) 10 | ndates=length(dates); 11 | nints=length(ints); 12 | 13 | int{1} = 0; 14 | del(1,1)=0; 15 | del(1,2)=0; 16 | 17 | int{1} = ints(id).name; %name of ints 18 | fid1=fopen([masterdir 'int_' int{1} '/fine_offsets/azimuth_03.off'],'r'); 19 | azoff=fread(fid1,3000,'real*4'); 20 | maz(1)=median(azoff); 21 | fid2=fopen([masterdir 'int_' int{i} '/fine_offsets/range_03.off'],'r'); 22 | rgoff = fread(fid2,3000,'real*4'); 23 | mrg(i) = median(rgoff); 24 | fclose('all') 25 | 26 | for i=2:ndates-2 27 | inti=find([ints.i1]==i+1 & [ints.i2]==i); 28 | int{i} = ints(inti).name; %name of ints 29 | 30 | fid1=fopen([masterdir 'int_' int{i} '/fine_offsets/azimuth_03.off'],'r'); 31 | azoff=fread(fid1,3000,'real*4'); 32 | maz(i)=median(azoff); 33 | if abs(maz(i))>99999 34 | maz(i)=0; 35 | end 36 | del(i,1)=del(i-1,1)+maz(i); 37 | 38 | fid2=fopen([masterdir 'int_' int{i} '/fine_offsets/range_03.off'],'r'); 39 | rgoff = fread(fid2,3000,'real*4'); 40 | mrg(i) = median(rgoff); 41 | if abs(mrg(i))>99999 42 | mrg(i)=0; 43 | end 44 | del(i,2)=del(i-1,2)+mrg(i); 45 | 46 | fclose('all'); 47 | end 48 | 49 | 50 | 51 | %now calculate offset for each int 52 | for k=1:nints-2 53 | ints(k).azoff = del(ints(k).i2,1); 54 | ints(k).rgoff = del(ints(k).i2,2); 55 | end 56 | 57 | -------------------------------------------------------------------------------- /getstuff.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile) 3 | ndates = length(dates); 4 | nints = length(ints); 5 | if strcmp(sat,'S1A') 6 | nx=ints(id).width; 7 | ny=ints(id).length; 8 | 9 | else 10 | [nx,ny,lambda]= load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH','WAVELENGTH'); 11 | 12 | end 13 | l=1; 14 | im=sqrt(-1); 15 | newnx = floor(nx./rlooks); 16 | newny =floor(ny./alooks); -------------------------------------------------------------------------------- /gps_reference.m: -------------------------------------------------------------------------------- 1 | 2 | % GPS InSAR referencing 3 | % Kyle Murray 4 | % July, 2017 5 | % Description: 6 | % Loads all available UNR GPS time series in a given lat/lon range with 7 | % get_gps_ts script. All time series are put into a directory GPS 8 | % Finds reference date in all GPS sites. Displacement should be zero at 9 | % this date in all GPS TS's 10 | % 11 | clear all; close all 12 | getstuff 13 | 14 | dl_files=0; 15 | % dl_files=1: Download new files 16 | % dl_files=0: Just load the velocities 17 | [enu_vels,llh_vels,sites_vels]=get_gps_ts_vels(dl_files); 18 | 19 | % Plot horizontal 20 | figure;quiver(llh_vels(:,2),llh_vels(:,1),enu_vels(:,1),enu_vels(:,2)) 21 | title('Horizontal GPS velocities');axis image; kylestyle 22 | xlabel('Longitude');ylabel('latitude') 23 | 24 | % Plot Vertical 25 | figure;quiver(llh_vels(:,2),llh_vels(:,1),zeros(length(enu_vels(:,3)),1),enu_vels(:,3)) 26 | title('Vertical GPS velocities');axis image; kylestyle 27 | xlabel('Longitude');ylabel('latitude');hold on 28 | text(llh_vels(:,2),llh_vels(:,1),sites_vels) 29 | 30 | % %off map 31 | offmap={'WGPP'; 'P782';'P602'; 'P578';'P571';'P567';'P546';'P540' 32 | 'P539';'P522';'P518';'GDEC';'EDPP';'CUHS';'BCWR';'ARM2';'ARM1' }; 33 | 34 | % [~,~,bad_ids] = intersect(offmap(:),sites_vels(:)); 35 | % llh_vels(bad_ids,:)=[]; 36 | % sites_vels(bad_ids)=[]; 37 | % enu_vels(bad_ids,:)=[]; 38 | % % Give it a vector of indices 39 | % site_idx=1:length(sites_vels); 40 | % plot_pts_geo(llh_vels(site_idx,1),llh_vels(site_idx,2),sites_vels(site_idx)) 41 | 42 | % main subsidence lat lon 43 | a=[35.93, -119.4]; 44 | % lost hills lat lon 45 | b=[35.43, -119.7]; 46 | 47 | subsidence=[a;b]; 48 | 49 | 50 | % Just do a subset of the better GPS sites 51 | % good_subset={'P565';'P564';'P563';'P547';'P545';'P544';'P543';'P541';'P538' 52 | % 'P537';'P536';'P056';'CRCN';'BVPP';'BKR2';'BKR1';'BAK1'}; 53 | good_subset={'CRCN'}; 54 | [~,~,good_ids] = intersect(good_subset(:),sites_vels(:)); 55 | plot_all_pts(llh_vels(good_ids,1),llh_vels(good_ids,2),sites_vels(good_ids)) %plots insar and gps 56 | % plot_pts_geo(subsidence(:,1),subsidence(:,2)); %This also plots profiles 57 | 58 | 59 | %ideas: 60 | % use GPS in decorrelated areas, and interpolate points around it with good 61 | % insar dates 62 | 63 | % interpolate all GPS displacements to make a priori guesses for each 64 | % interferogram before unwrapping? 65 | 66 | % find areas of low deformation 67 | 68 | -------------------------------------------------------------------------------- /gps_surface.m: -------------------------------------------------------------------------------- 1 | % Description: 2 | % Loads each date, removes a model of GPS surface, remove low freq, add 3 | % back GPS, save dates 4 | % 5 | clear all; close all 6 | getstuff 7 | 8 | dl_files=0; 9 | % dl_files=1: Download new files 10 | % dl_files=0: Just load the velocities 11 | [enu_vels,llh_vels,sites_vels]=get_gps_ts_vels(dl_files); 12 | sites_vels(find(enu_vels(:,6)>0.003))=[]; 13 | llh_vels(find(enu_vels(:,6)>0.003),:)=[]; 14 | enu_vels(find(enu_vels(:,6)>0.003),:)=[]; 15 | 16 | % Plot Vertical 17 | figure;quiver(llh_vels(:,2),llh_vels(:,1),zeros(length(enu_vels(:,3)),1),enu_vels(:,3)) 18 | title('Vertical GPS velocities');axis image; kylestyle 19 | xlabel('Longitude');ylabel('latitude');hold on 20 | text(llh_vels(:,2),llh_vels(:,1),sites_vels) 21 | 22 | % Get the lat and lon vectors from geocoded date 23 | geo_int=[rlkdir{1} dates(1).name '_geo_2rlks.unw']; 24 | rate_struct=load_any_data(geo_int,'11N'); 25 | phs_X=rate_struct.X; 26 | phs_Y=rate_struct.Y; 27 | [lat_vec,lon_vec]=utm2ll(phs_X(:),phs_Y(:),11,'wgs84'); 28 | phs_grd=rate_struct.phs; 29 | 30 | % [xq,yq] = meshgrid(min_lon:.005:max_lon, min_lat:.005:max_lat); %one pixel at 2rlks is ~1/2000 degree 31 | lat_grd=reshape(lat_vec,size(phs_grd)); 32 | lon_grd=reshape(lon_vec,size(phs_grd)); 33 | x=llh_vels(:,2); 34 | y=llh_vels(:,1); 35 | v=enu_vels(:,3)*100; 36 | dv=enu_vels(:,6)*100; 37 | 38 | % Use griddata. Try the Biharmonic spline interpolation 'v4' or 'cubic' 39 | vq = griddata(x,y,v,lon_grd,lat_grd,'cubic'); %cm/yr 40 | 41 | % Use krig 42 | % V='1 Sph(50)'; %not sure what to use for this 43 | % [vq,d_var,lambda,K,k,inhood]=krig([x y],[v dv],[lon_vec lat_vec],V); 44 | 45 | % Plot the gridded data as a mesh and the scattered data as dots. 46 | % figure 47 | % mesh(lon_grd,lat_grd,vq) 48 | % hold on 49 | % plot3(x,y,v,'o') 50 | 51 | % Convert velocity to displacements for each inverted time interval 52 | % Get the time intervals 53 | datenumbers=char(dates.name); 54 | d=datenum(datenumbers,'yyyymmdd'); 55 | dy=d./365.25; 56 | 57 | %make copies of original geocoded dates 58 | for k=1:ndates 59 | if(~exist([rlkdir{1} dates(k).name '_geo_2rlks_orig.unw'],'file')) 60 | copyfile([rlkdir{1} dates(k).name '_geo_2rlks.unw'],[rlkdir{1} dates(k).name '_geo_2rlks_orig.unw']) 61 | copyfile([rlkdir{1} dates(k).name '_geo_2rlks.unw.rsc'],[rlkdir{1} dates(k).name '_geo_2rlks_orig.unw.rsc']) 62 | 63 | disp(['copying ' rlkdir{1} dates(k).name '_geo_2rlks.unw to ' rlkdir{1} dates(k).name '_geo_2rlks_orig.unw']); 64 | end 65 | end 66 | 67 | % Load each date, remove GPS, remove low freq, add back GPS, save 68 | for ii=1:ndates 69 | %first 70 | geo_int=[rlkdir{1} dates(ii).name '_geo_2rlks_orig.unw']; 71 | rate_struct=load_any_data(geo_int,'11N'); 72 | phs_X=rate_struct.X; 73 | phs_Y=rate_struct.Y; 74 | [lat_vec,lon_vec]=utm2ll(phs_X(:),phs_Y(:),11,'wgs84'); 75 | phs_grd=rate_struct.phs*lambda/(4*pi)*100; %convert to cm 76 | %second 77 | % geo_int=[rlkdir{1} dates(ii).name '_geo_2rlks_orig.unw']; 78 | % rate_struct=load_any_data(geo_int,'11N'); 79 | % phs_grd_two=-rate_struct.phs*lambda/(4*pi)*100; %convert to cm 80 | % %difference 81 | % phs_grd=phs_grd_two-phs_grd_one; 82 | phs_grd=phs_grd/cosd(25); 83 | time_interval=dy(ii)-dy(id); %time interval in years 84 | gps_disp = time_interval * vq; %this is the model to be removed 85 | gps_disp(isnan(gps_disp))=0; 86 | 87 | phs_removed=phs_grd-gps_disp; %check these for transpose 88 | phs_removed(isnan(phs_removed))=0; 89 | % Remove low pass 90 | % Transform phase to frequency domain 91 | phs_freq = fft2(phs_removed); 92 | phs_freq = fftshift(phs_freq); 93 | 94 | % Make filter 95 | xoff=floor(newnx/2); 96 | yoff=floor(newny/2); 97 | % [X,Y] = meshgrid(1-xoff:newnx-xoff,1-yoff:newny-yoff); 98 | fstrength=1; 99 | g2=(2+fstrength^1.5); 100 | [X,Y] = meshgrid(1:length(lon_grd(1,:)),1:length(lon_grd(:,1))); 101 | 102 | gauss2 = exp((-X.^2-Y.^2)/(g2^2)); 103 | 104 | 105 | % Apply filter 106 | phs_freq_filt = gauss2.*phs_freq; 107 | phs_freq_filt = ifftshift(phs_freq_filt); 108 | phs_filt = ifft2(phs_freq_filt); 109 | phs_filt = real(phs_filt); 110 | 111 | % Subtract filtered phase 112 | phs_diff=phs_removed-phs_filt; 113 | 114 | % Add back in GPS model 115 | phs_final = phs_diff+gps_disp; 116 | 117 | % Write output 118 | outfile = [rlkdir{1} dates(ii).name '_geo_2rlks_filt.unw']; 119 | fido=fopen('phs','w'); 120 | fwrite(fido,flipud(phs_final)','real*4'); 121 | fclose(fido); 122 | system(['mag_phs2rmg phs phs ' outfile ' ' num2str(length(phs_final(1,:)))]); 123 | 124 | % copyfile([rlkdir{1} dates(ii).name '_geo_2rlks_orig.unw.rsc'],[rlkdir{1} dates(ii).name '_geo_2rlks_filt.unw.rsc']); 125 | 126 | 127 | end 128 | % for ii=1:ndates 129 | % movefile([rlkdir{1} dates(ii).name '_geo_2rlks_filt.unw'],[rlkdir{1} dates(ii).name '_geo_2rlks.unw']); 130 | % end 131 | 132 | 133 | -------------------------------------------------------------------------------- /hi: -------------------------------------------------------------------------------- 1 | git commit bandPass_dates.m -m 'minor changes to filter design' 2 | git commit fitramp_int.m -m 'minor tweaks' 3 | git commit getstuff.m -m 'suppress newnx and add l=1' 4 | git commit invert_rates.m -m 'cleaned up excess' 5 | git commit lf_power.m -m 'saves svg' 6 | git commit plot_all_pts.m -m 'fixed major bug that plotted the wrong GPS component. outputs SVG.' 7 | git commit plot_pts_geo.m -m 'saves svg' 8 | git commit run_all.m -m 'minor changes' 9 | git commit unwrap_rlooks.m -m 'minor changes' 10 | git commit gps_surface.m -m 'New script that removes model of GPS surface before correcting for long wavelenth, then adds it back' 11 | git commit plot_std_profile.m -m 'plots a profile of the standard deviations of the rates' 12 | -------------------------------------------------------------------------------- /init_dirs.m: -------------------------------------------------------------------------------- 1 | function init_dirs 2 | if(exist('set_params.m','file')) % for some reason need this since init_dirs is not recognizing new set_params.m files. 3 | end 4 | 5 | set_params 6 | if(~exist('figs','dir')) 7 | mkdir('figs'); 8 | end 9 | 10 | if(~exist('baddatedir','dir')) 11 | mkdir(baddatedir); 12 | end 13 | if(~exist('TSdir','dir')) 14 | mkdir(TSdir); 15 | end 16 | if(~exist(rectdir,'dir')) 17 | mkdir(rectdir); 18 | end 19 | if(~exist([rectdir 'rectfiles/'],'dir')) 20 | mkdir([rectdir 'rectfiles/']); 21 | end 22 | if(~exist(intdir,'dir')) 23 | mkdir(intdir); 24 | end 25 | 26 | if(~exist(DEMdir,'dir')) 27 | mkdir(DEMdir); 28 | end 29 | 30 | for i=1:length(rlooks) 31 | if(~exist(rlkdir,'dir')) 32 | mkdir(rlkdir); 33 | end 34 | end 35 | 36 | 37 | -------------------------------------------------------------------------------- /invert_dates.m: -------------------------------------------------------------------------------- 1 | function invert_dates(topoflag) 2 | % topoflag=0; 3 | %0 looks for unwrlk{l}, 1 adds _topo.unw to name. 4 | set_params 5 | 6 | [G,Gg,R,N]=build_Gint; 7 | [m,n]=size(Gg); 8 | 9 | for l=1:length(rlooks) 10 | for i=1:nints 11 | if(topoflag) 12 | infile=[ints(i).unwrlk '_topo.unw']; 13 | else 14 | infile=[ints(i).unwrlk]; 15 | end 16 | if(~exist(infile)) 17 | disp([infile ' does not exist']) 18 | return 19 | end 20 | % mysys(['rmg2mag_phs ' infile ' mag phs ' num2str(newnx)]) 21 | fidi(i)=fopen(infile,'r'); 22 | % !rm phs 23 | % fidi(i)=fopen(infile,'r'); 24 | end 25 | for i=1:ndates 26 | 27 | fido(i)=fopen([rlkdir dates(i).name '_' num2str(rlooks) 'rlks.r4'],'w'); 28 | end 29 | fido2=fopen(['res_' num2str(rlooks(l))],'w'); 30 | 31 | 32 | 33 | for j=1:newny(l) 34 | tmpdat=zeros(n,newnx(l)); %data for n-nints = zeros 35 | for i=1:nints 36 | jnk = fread(fidi(i),newnx(l),'real*4'); 37 | tmpdat(i,1:length(jnk))=jnk; 38 | end 39 | 40 | mod = Gg * tmpdat; 41 | synth = G * mod; 42 | res = tmpdat-synth; 43 | resstd = std(res,1); 44 | fwrite(fido2,resstd,'real*4'); 45 | for i=1:ndates 46 | fwrite(fido(i),mod(i,:),'real*4'); 47 | end 48 | end 49 | fclose('all'); 50 | end 51 | 52 | %This script writes a file called res_# in addition to the inverted dates. 53 | %Each pixel is the standard deviation of the stack of residuals. The 54 | %residual is the difference between the phase of each unw int and the phase 55 | %predicted by the model (the model is the inverted date). So the std will 56 | %be high in places where the model isn't doing a good job predicting the 57 | %deformation. This will happen if there is a lot of noise in a particular 58 | %int (like atm delays), decorrelated areas, areas with unwrapping errors 59 | %or areas with aliased deformation signals. -------------------------------------------------------------------------------- /invert_dates_height.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | 4 | ndates = length(dates); 5 | nints = length(ints); 6 | [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 7 | 8 | 9 | [G,Gg,R,N]=build_Gint; 10 | [n,m]=size(G); 11 | %n is number of constraints -> #ints + #unconstrained intervals 12 | %m is number of intervals -> #dates-1 13 | 14 | G2=[ints.bp]'; 15 | Gg2 = inv(G2'*G2)*G2'; 16 | close; 17 | 18 | newnx=floor(nx./rlooks) 19 | newny=floor(ny./alooks); 20 | 21 | %for l=1:1 22 | for l=1:length(rlooks) 23 | for i=1:nints 24 | fidi(i)=fopen(ints(i).unwrlk{l},'r'); 25 | end 26 | for i=1:ndates 27 | fido(i)=fopen(dates(i).unwrlk{l},'w'); 28 | end 29 | fido2=fopen(['res_' num2str(rlooks(l))],'w'); 30 | fido3=fopen(['height_' num2str(rlooks(l))],'w'); 31 | 32 | for j=1:newny(l) 33 | tmpdat=zeros(n,newnx(l)); %data for n-nints = zeros 34 | for i=1:nints 35 | jnk = fread(fidi(i),newnx(l),'real*4'); 36 | jnk = fread(fidi(i),newnx(l),'real*4'); 37 | tmpdat(i,1:length(jnk))=jnk; 38 | end 39 | height = Gg2*tmpdat(1:nints,:); 40 | synth = G2*height; 41 | tmpdat = tmpdat-[synth;zeros((n-nints),newnx(l))]; 42 | 43 | mod = Gg*tmpdat; 44 | synth = G*mod; 45 | res = tmpdat-synth; 46 | resstd = std(res,1); 47 | fwrite(fido2,resstd,'real*4'); 48 | for i=1:ndates 49 | fwrite(fido(i),mod(i,:),'real*4'); 50 | end 51 | fwrite(fido3,height,'real*4'); 52 | 53 | end 54 | fclose('all'); 55 | end 56 | -------------------------------------------------------------------------------- /invert_dates_s1a.m: -------------------------------------------------------------------------------- 1 | topoflag=0; 2 | set_params 3 | load(ts_paramfile); 4 | 5 | [G,Gg,R,N]=build_Gint_s1a; 6 | [m,n]=size(Gg); 7 | 8 | 9 | for l=1:length(rlooks) 10 | for i=1:nints 11 | 12 | if(topoflag) 13 | infile=[ints(i).unwrlk '_topo.unw']; 14 | else 15 | infile=ints(i).unwrlk; 16 | end 17 | if(~exist(infile)) 18 | disp([infile ' does not exist']) 19 | return 20 | end 21 | fidi(i)=fopen(infile,'r'); 22 | 23 | end 24 | for i=1:ndates 25 | fido(i)=fopen(dates(i).unwrlk,'w'); 26 | end 27 | fido2=fopen(['res_'],'w'); 28 | 29 | for j=1:newny(l) 30 | tmpdat=nan(n,newnx(l)); %data for n-nints = zeros 31 | for i=1:nints 32 | jnk = fread(fidi(i),newnx(l),'real*4'); 33 | jnk = fread(fidi(i),newnx(l),'real*4'); 34 | 35 | tmpdat(i,1:length(jnk))=jnk; 36 | 37 | 38 | end 39 | 40 | mod = Gg*tmpdat; 41 | synth = G*mod; 42 | res = tmpdat-synth; 43 | resstd = std(res,1); 44 | fwrite(fido2,resstd,'real*4'); 45 | for i=1:ndates 46 | fwrite(fido(i),mod(i,:),'real*4'); 47 | end 48 | end 49 | 50 | fclose('all'); 51 | end 52 | 53 | -------------------------------------------------------------------------------- /invert_rates.m: -------------------------------------------------------------------------------- 1 | clear all;close all 2 | do_rates=1; 3 | mcov_flag=0; 4 | set_params 5 | dn = [dates.dn]; 6 | dn = dn-dn(1); 7 | 8 | if do_rates 9 | alld=zeros(ndates,newny,newnx); 10 | for i=1:ndates 11 | fid=fopen([dates(i).unwrlk '_corrected'],'r'); 12 | tmp=fread(fid,[newnx,newny],'real*4'); 13 | alld(i,:,:)=tmp'; 14 | end 15 | 16 | G = [ones(length(dn),1) dn']; 17 | Gg = inv(G'*G)*G'; 18 | 19 | alld = reshape(alld,[ndates,newnx*newny]); 20 | 21 | mod = Gg*alld; 22 | offs = reshape(mod(1,:),[newny newnx]); 23 | rates = reshape(mod(2,:),[newny,newnx])*lambda/(4*pi)*100*365; %cm/yr 24 | synth = G*mod; 25 | res = (alld-synth)*lambda/(4*pi)*100; %cm 26 | clear synth mod 27 | resstd = std(res,1); 28 | resstd = reshape(resstd,[newny newnx]); 29 | save('rates','rates') 30 | else 31 | load 'rates' 32 | end 33 | 34 | if(mcov_flag) 35 | for jj=1:length(alld(1,:)) 36 | co=cov(alld(:,jj)); 37 | mcov=diag(Gg*co*Gg'); 38 | rate_uncertainty(jj)=1.96*mcov(2)^.5; 39 | end 40 | rate_uncertainty=reshape(rate_uncertainty,[newny,newnx])*lambda/(4*pi)*100*365; %cm/yr 41 | figure 42 | plot(-rates(484:end,2575),'k.');hold on 43 | plot(-rates(484:end,2575)+rate_uncertainty(484:end,2575),'b--');hold on 44 | plot(-rates(484:end,2575)-rate_uncertainty(484:end,2575),'b--');hold on 45 | end 46 | 47 | %mask 48 | fidin = fopen(['TS/looks' num2str(rlooks) '/mask_' num2str(rlooks) 'rlks.r4'],'r','native'); 49 | msk=fread(fidin,[newnx,newny],'real*4'); 50 | msk=msk'; 51 | % rates(watermask'==0)=0; 52 | % rates(:,2389:end)=0; 53 | rates(msk<0.25)=nan; 54 | % rates=tmp>0.1; 55 | % fwrite(fidout,out,'integer*1'); 56 | % crop=logical(zeros(size(rates))); 57 | % crop(:,1:70)=1; 58 | % crop(:,(newnx-30):newnx)=1; 59 | % crop(1:3,:)=1;crop((newny-3):newny,:)=1; 60 | % rates(crop)=0; 61 | 62 | msk=zeros(size(rates)); 63 | msk(30:newny-30,30:newnx-30)=1; 64 | rates(msk==0)=nan; 65 | 66 | fout1=fopen(['rates_' num2str(rlooks)],'w'); 67 | fout2=fopen(['ratestd_' num2str(rlooks)],'w'); 68 | for i=1:newny 69 | fwrite(fout1,rates(i,:),'real*4'); 70 | % fwrite(fout2,resstd(i,:),'real*4'); 71 | end 72 | fclose('all') 73 | 74 | figure 75 | imagesc(-rates) 76 | % caxis([-20 20]) 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /invert_rates_s1a.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load('ts_paramfile'); 3 | 4 | ndates = length(dates); 5 | nints = length(ints); 6 | 7 | 8 | [X,Y] = meshgrid(1:newnx,1:newny); 9 | 10 | dn = [dates.dn]; 11 | dn = dn-dn(1); 12 | 13 | for l=1:length(rlooks) 14 | %for l=1:1 15 | [X,Y]=meshgrid(1:newnx(l),1:newny(l)); 16 | 17 | alld=zeros(ndates,newny(l),newnx(l)); 18 | for i=1:ndates 19 | fid=fopen(dates(i).unwrlk,'r'); 20 | tmp=fread(fid,[newnx(l),newny(l)],'real*4'); 21 | alld(i,:,:)=tmp'; 22 | end 23 | 24 | G = [ones(length(dn),1) dn']; 25 | Gg = inv(G'*G)*G'; 26 | 27 | alld = reshape(alld,[ndates,newnx(l)*newny(l)]); 28 | mod = Gg*alld; 29 | offs = reshape(mod(1,:),[newny(l) newnx(l)]); 30 | rates = reshape(mod(2,:),[newny(l),newnx(l)])*lambda/(4*pi)*1000*365; %mm/yr 31 | synth = G*mod; 32 | res = (alld-synth)*lambda/(4*pi)*1000; %mm 33 | resstd = std(res,1); 34 | resstd = reshape(resstd,[newny(l) newnx(l)]); 35 | 36 | %mask 37 | % fidin = fopen('TS/looks4/mask_4rlks.r4','r','native'); 38 | % msk=fread(fidin,[newnx,newny],'real*4'); 39 | % msk=msk'; 40 | % rates(msk<0.3)=0; 41 | % rates=tmp>0.1; 42 | % fwrite(fidout,out,'integer*1'); 43 | 44 | 45 | 46 | fout1=fopen(['rates_' num2str(rlooks(l))],'w'); 47 | fout2=fopen(['ratestd_' num2str(rlooks(l))],'w'); 48 | for i=1:newny(l) 49 | fwrite(fout1,rates(i,:),'real*4'); 50 | fwrite(fout2,resstd(i,:),'real*4'); 51 | end 52 | fclose('all') 53 | end 54 | 55 | figure 56 | imagesc(-rates) 57 | caxis([-30 30]) -------------------------------------------------------------------------------- /lf_power.m: -------------------------------------------------------------------------------- 1 | function low_freq_power = lf_power(num_lf_bands) 2 | % Kyle Murray 3 | % July 2017 4 | % Calculates low frequency power of stack of inverted dates and writes to a 5 | % file called lowFreqPower, and saves .mat file called lf_power.mat 6 | % num_lf_bands is the number of low frequency bands to sum to get the 7 | % power. Should be around 2 or 3? Higher numbers will include more high 8 | % frequency power in the final number. A strong secular rate + seasonal 9 | % signal would have high power at 1 cycle/year or less 10 | 11 | set_params 12 | % Load in stack of dates and find FFT at each pixel 13 | alld=zeros(ndates,newny,newnx); 14 | for i=1:ndates 15 | if(~exist(dates(i).unwrlk)) 16 | display([dates(i).unwrlk{1} ' does not exist']) 17 | return 18 | else 19 | fid=fopen([dates(i).unwrlk '_corrected'],'r'); 20 | tmp=fread(fid,[newnx,newny],'real*4'); 21 | alld(i,:,:)=tmp'; 22 | end 23 | end 24 | alld = reshape(alld,[ndates,newnx*newny]); 25 | 26 | % Calculate the frequency 27 | freqs=fft(alld,[],1); %transform to freq domain at each pixel 28 | clear alld tmp 29 | P2 = abs(freqs/ndates); %power spectrum 30 | clear freqs 31 | P1 = P2(1:ndates/2+1,:); %one sided power spectrum 32 | P1(2:end-1,:) = 2*P1(2:end-1,:); %one sided power spectrum 33 | ss = sum(P2(1:num_lf_bands,:),1); %sum the power at the lowest 3 freq bands 34 | low_freq_power = reshape(ss,[newny,newnx]); % 35 | 36 | % Plot resulting LF power image and write to file 37 | figure;imagesc(low_freq_power);colorbar;caxis([0 40]); 38 | saveas(gcf,['lf_power_raw'],'svg'); 39 | 40 | fido=fopen(['lowFreqPower' num2str(rlooks)],'w'); 41 | fwrite(fido,low_freq_power,'real*4');fclose(fido); 42 | save('lf_power','low_freq_power'); -------------------------------------------------------------------------------- /load_allps_unw.m: -------------------------------------------------------------------------------- 1 | %plot the ALLPS unwrapped ints to compare with StaMPS 2 | 3 | getstuff 4 | 5 | % allps_u=zeros(n_ps,nints); 6 | for ii=1:nints 7 | fid=fopen([ints(ii).unwrlk{1}],'r'); 8 | unw=fread(fid,[newnx, newny*2],'real*4'); 9 | phs = fliplr((unw(1:newnx,2:2:end))'); 10 | % allps_u(:,ii)=phs(msk); 11 | fclose(fid); 12 | end 13 | 14 | % save('allps_u','allps_u'); 15 | -------------------------------------------------------------------------------- /load_data.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile) 3 | 4 | switch sat 5 | case {'ENVI','ERS'} 6 | uname = 'rlohman'; 7 | passw = 'starfish'; 8 | case {'ALOS' , 'S1A'} 9 | %make cookies file 10 | %EOSDIS password info 11 | uname = 'rlohman'; 12 | passw = 'Rolohman1'; 13 | 14 | end 15 | parfor ii=1:length(dn) 16 | tic 17 | if ~exist(dates(i).raw,'file') 18 | parraw(ii,sat,searchresults,dn,sortdn,uname,passw) 19 | else 20 | disp(['already raw']) 21 | end 22 | toc 23 | end 24 | clear badid c 25 | c=[1]; 26 | badid=[]; 27 | for ii=1:length(dates) 28 | if ~exist(dates(ii).raw) 29 | badid(c)=ii 30 | c=c+1; 31 | end 32 | end 33 | 34 | if(badid) 35 | for i=1:length(badid) 36 | disp(dates(badid(i)).name) 37 | end 38 | 39 | reply = input(['Could not make .raw files for above dates. They probably do not have orbit files available. Remove them from dates struct? y/n [y] '],'s') 40 | if(isempty(reply)) 41 | reply='Y'; 42 | end 43 | switch reply 44 | case {'Y','Yes','y','YES'} 45 | disp('Moving dates to baddate dir, editing date struct') 46 | olddates = dates; 47 | baddates = dates(badid); 48 | dates(badid)=[]; 49 | newid = find(strcmp({dates.name},olddates(id).name));%new id for master date if others thrown out(change in set_params or run_all.m) 50 | for i=1:length(baddates) 51 | disp(['moving date ' baddates(i).name]); 52 | movefile(baddates(i).name,baddatedir); 53 | end 54 | disp(['change master date id in set_params to: ' num2str(newid)]) 55 | disp('run fom setup_init if changing master date'); 56 | disp('Otherwise just rerun read_dopbase'); 57 | 58 | save(ts_paramfile,'dates'); 59 | 60 | case {'No','n','NO','N'} 61 | disp('not changing anything.') 62 | return 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /makeGamma.m: -------------------------------------------------------------------------------- 1 | function makeGamma(iter) 2 | %iter: 0 first iteration 3 | %iter: 1 next iteration 4 | %iter: 2 remove restore rate map 5 | %iter: 3 remove restore next iteration 6 | 7 | 8 | set_params 9 | nx=ints(id).width; 10 | ny=ints(id).length; 11 | im = sqrt(-1); 12 | rx = 4; %should perhaps be set in set_params instead 13 | ry = rx*pixel_ratio; 14 | 15 | switch iter 16 | case 0 17 | display('Doing first iteration') 18 | tic 19 | parfor ii=1:nints 20 | filtflat0(ii,nx,ny,rx,ry) 21 | end 22 | toc 23 | display('Making gamma0.r4') 24 | CalcGamma 25 | 26 | case 1 27 | display('Using TS/gamma0.r4 file for a subsequent iteration') 28 | tic 29 | for i=1:nints 30 | % filtflat1(i,nx,ny,rx,ry) 31 | system(['rm ' ints(i).flat '_filt ' ints(i).flat '_diff']); 32 | filter_diff_iter(ints(i).flat,[ints(i).flat '_filt'],[ints(i).flat '_diff'], nx,ny,rx,ry,gammafile,.2); 33 | end 34 | toc 35 | if ~exist('TS/gamma0_orig.r4','file') 36 | !mv TS/gamma0.r4 TS/gamma0_orig.r4 37 | end 38 | display('Making next iteration of gamma0.r4') 39 | CalcGamma 40 | case 2 41 | if ~exist('rates.unw','file') 42 | make_filtrate 43 | end 44 | tic 45 | for i=1:nints 46 | % filtflat2(i,nx,ny,rx,ry) 47 | tmp=dir(ints(i).flat); %is output from diffnsim with phase+amp, we just want phase. 48 | if(tmp.bytes==nx*ny*4) 49 | disp([ints(i).flat ' already split to one band']) 50 | elseif(tmp.bytes==nx*ny*8) 51 | disp(['splitting ' ints(i).flat ' into just phs']) 52 | command=['cpx2mag_phs ' ints(i).flat ' mag phs ' num2str(nx)]; 53 | mysys(command); 54 | command=['mv phs ' ints(i).flat]; 55 | mysys(command); 56 | else 57 | disp([ints(i).flat ' wrong size?']) 58 | return 59 | end 60 | filter_diff_remove_restore2(ints(i).flat,'rate_phs',[ints(i).flat '_filt'],[ints(i).flat '_diff'],[ints(i).flat '_raterem'], nx,ny,rx,ry,ints(i).dt,lambda); 61 | end 62 | toc 63 | display('Making gamm0.r4_raterem') 64 | CalcGamma_raterem 65 | case 3 66 | display('Using TS/gamma0.r4 file for a subsequent iteration') 67 | tic 68 | parfor i=1:nints 69 | filtflat3(i,nx,ny,rx,ry) 70 | end 71 | toc 72 | !mv TS/gamma0.r4_raterem TS/gamma0_orig.r4_raterem 73 | display('Making next iteration of gamm0.r4_raterem') 74 | CalcGamma_raterem 75 | end -------------------------------------------------------------------------------- /make_all_raw.m: -------------------------------------------------------------------------------- 1 | function make_all_raw 2 | set_params 3 | load(ts_paramfile); 4 | ndates=length(dates); 5 | 6 | for i=1:ndates 7 | chdir(dates(i).dir) 8 | if(~exist(dates(i).raw)) 9 | switch sat 10 | 11 | case 'ERS' 12 | %!gunzip *.gz 13 | !tar -xzvf *.gz 14 | !mv DAT_01.001 IMAGERY 15 | !ln -s LEA_01.001 SARLEADER 16 | command=['make_raw.pl PRC SARLEADER ' dates(i).name]; 17 | mysys(command); 18 | case 'ENVI' 19 | command=['make_raw_envi.pl ASA* DOR ' dates(i).name]; 20 | mysys(command); 21 | case 'ALOS' 22 | 23 | types=dir('IMG*HV*'); %Check to see if this is FBD or FBS 24 | if(isempty(types)) 25 | command=['make_raw_alos.pl IMG ' dates(i).name]; 26 | else 27 | command=['make_raw_alos.pl IMG ' dates(i).name ' FBD2FBS']; 28 | end 29 | [status,result]=mysys(command); 30 | end 31 | else 32 | disp([dates(i).raw ' already exists']) 33 | end 34 | end 35 | chdir(masterdir); 36 | -------------------------------------------------------------------------------- /make_dates_struct.m: -------------------------------------------------------------------------------- 1 | function make_dates_struct(dn,results) 2 | set_params 3 | 4 | if(isempty(dn)) %assumes you've already downloaded dates 5 | output = dir; 6 | for i=1:length(output) 7 | name=output(i).name; 8 | test=regexp(name,'^\d{8}$'); %name made up of just 8 digits, nothing else 9 | if(test) 10 | dn(end+1)=datenum(name,'yyyymmdd'); 11 | else 12 | end 13 | end 14 | end 15 | for i=1:length(dn) 16 | 17 | dates(i).name = datestr(dn(i),'yyyymmdd'); 18 | dates(i).dn = dn(i); 19 | dates(i).dir = [masterdir dates(i).name '/']; 20 | dates(i).slc = [dates(i).dir dates(i).name '.slc']; 21 | dates(i).raw = [dates(i).dir dates(i).name '.raw']; 22 | dates(i).rectslc = [rectdir 'rect_' dates(i).name '.slc']; 23 | for j=1:length(rlooks) 24 | dates(i).unwrlk{j} = [rlkdir{j} dates(i).name '_' num2str(rlooks(j)) 'rlks.r4']; 25 | end 26 | if(~exist(dates(i).dir,'dir')) 27 | mkdir(dates(i).dir); 28 | end 29 | 30 | if(isstruct(results)) 31 | dates(i).searchresults = results(i); 32 | end 33 | end 34 | save(ts_paramfile,'dates'); 35 | 36 | 37 | -------------------------------------------------------------------------------- /make_dates_struct_s1a.m: -------------------------------------------------------------------------------- 1 | function make_dates_struct_s1a(dn,results) 2 | % set_params 3 | !mkdir r4_files 4 | if(isempty(dn)) %assumes you've already downloaded dates 5 | output = dir; 6 | for i=1:length(output) 7 | name=output(i).name; 8 | test=regexp(name,'^\d{8}$'); %name made up of just 8 digits, nothing else 9 | if(test) 10 | dn(end+1)=datenum(name,'yyyymmdd'); 11 | else 12 | end 13 | end 14 | end 15 | for i=1:length(dn) 16 | 17 | dates(i).name = datestr(dn(i),'yyyymmdd'); 18 | dates(i).dn = dn(i); 19 | dates(i).bp = 0; 20 | 21 | dates(i).unwrlk = ['r4_files/' dates(i).name '_rlks.r4']; 22 | 23 | 24 | if(isstruct(results)) 25 | dates(i).searchresults = results(i); 26 | end 27 | end 28 | save('ts_paramfile','dates'); 29 | 30 | 31 | -------------------------------------------------------------------------------- /make_filtrate.m: -------------------------------------------------------------------------------- 1 | %make a full resolution rate file from rates_4 and filter it 2 | unw_in='rates_4' 3 | %Geocode a 4rlks unw file 4 | froot=unw_in; 5 | set_params 6 | load(ts_paramfile); 7 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 8 | 9 | [nx,ny,lambda] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH','WAVELENGTH'); 10 | 11 | newnx = floor(nx./rlooks) 12 | newny = floor(ny./alooks); 13 | %Step 1. Create rmg file 14 | amp_img = unw_in; 15 | phs_img = unw_in; 16 | rmg_in = [froot '_4rlks.unw']; 17 | 18 | mysys(['mag_phs2rmg ' amp_img ' ' phs_img ' ' rmg_in ' ' num2str(newnx)]) 19 | % [nx2,ny2] = load_rscs([oldintdir 'radar.hgt'],'WIDTH','FILE_LENGTH'); 20 | 21 | aff1=1/rlooks; 22 | aff2=aff1/pixel_ratio; 23 | 24 | %Step 2. Create a 2-looks version 25 | %first, write out a rect_back.in file 26 | fid=fopen('rect_back.in','w'); 27 | 28 | fprintf(fid,['Input Image File Name (-) = ' rmg_in ' ! dimension of file to be rectified\n']); 29 | fprintf(fid,['Output Image File Name (-) = rates.unw ! dimension of output\n']); 30 | fprintf(fid,['Input Dimensions (-) = ' num2str(newnx) ' ' num2str(newny) ' ! across, down\n']); 31 | fprintf(fid,['Output Dimensions (-) = ' num2str(nx) ' ' num2str(ny) ' ! across, down\n']); 32 | fprintf(fid,['Affine Matrix Row 1 (-) = ' num2str(aff1) ' 0 ! a b\n']); 33 | fprintf(fid,['Affine Matrix Row 2 (-) = 0 ' num2str(aff2) ' ! c d\n']); 34 | fprintf(fid,['Affine Offset Vector (-) = 0 0 ! e f\n']); 35 | fprintf(fid,['File Type (-) = RMG ! [RMG, COMPLEX]\n']); 36 | fprintf(fid,['Interpolation Method (-) = Bilinear ! [NN, Bilinear, Sinc]\n']); 37 | 38 | %now, use rect rect_back.in to up look the 4looks rates file 39 | mysys(['rect rect_back.in']) 40 | %now we have a rates.unw to geocode 41 | 42 | -------------------------------------------------------------------------------- /make_frame_gmt.m: -------------------------------------------------------------------------------- 1 | %makes a frame to be used in gmt 2 | set_params 3 | frame_gmt=[[frames.lon]' [frames.lat]'; [frames.lon(1)] [frames.lat(1)] ]; 4 | dlmwrite([sat '_T' num2str(track) '_' num2str(frame) '.gmtframe'],frame_gmt,'delimiter',' ','precision','%.6f') 5 | -------------------------------------------------------------------------------- /make_ints.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | ndates=length(dates); 4 | nints=length(ints); 5 | 6 | [nx,ny]=load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 7 | 8 | for i=1% %now only need to make one? Perhaps not that in future 9 | if(~exist([ints(i).int '.rsc'],'file')) 10 | disp(['making ' ints(i).int]) 11 | fid1=fopen(dates(ints(i).i1).rectslc,'r'); 12 | fid2=fopen(dates(ints(i).i2).rectslc,'r'); 13 | fidout=fopen(ints(i).int,'w'); 14 | for j=1:ny 15 | tmp=fread(fid1,nx*2,'real*4'); 16 | slc1=complex(tmp(1:2:end),tmp(2:2:end)); 17 | tmp=fread(fid2,nx*2,'real*4'); 18 | slc2=complex(tmp(1:2:end),tmp(2:2:end)); 19 | int=slc1.*conj(slc2); 20 | tmp(1:2:end)=real(int); 21 | tmp(2:2:end)=imag(int); 22 | fwrite(fidout,tmp,'real*4'); 23 | end 24 | fclose(fidout); 25 | fclose(fid1); 26 | fclose(fid2); 27 | end 28 | copyfile([dates(ints(i).i1).rectslc '.rsc'],[ints(i).int '.rsc']); 29 | end 30 | 31 | -------------------------------------------------------------------------------- /make_mask.m: -------------------------------------------------------------------------------- 1 | function make_mask(thresh) 2 | 3 | set_params 4 | load(ts_paramfile); 5 | 6 | ndates=length(dates); 7 | nints=length(ints); 8 | 9 | % if strcmp(sat,'S1A') 10 | % nx=ints(id).width; 11 | % ny=ints(id).length; 12 | % else 13 | % [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 14 | % end 15 | 16 | newnx = floor(nx./rlooks) 17 | newny = floor(ny./alooks); 18 | 19 | fidin = fopen(gammafile,'r','native'); 20 | fidout = fopen(maskfile,'w','native'); 21 | 22 | for i=1:ny 23 | tmp=fread(fidin,nx,'real*4'); 24 | out=tmp>thresh; 25 | fwrite(fidout,out,'integer*1'); 26 | end 27 | fclose(fidin); 28 | fclose(fidout); 29 | 30 | for l=1:length(rlooks) 31 | fidin = fopen(maskfile,'r','native'); 32 | fidout = fopen(maskfilerlk,'w'); 33 | for i=1:newny(l) 34 | tmp=zeros(nx,alooks(l)); 35 | [jnk,count]=fread(fidin,[nx alooks(l)],'integer*1'); 36 | tmp(1:count)=jnk; 37 | tmp = sum(tmp,2); %sum along alooks dir 38 | 39 | tmp = reshape(tmp(1:rlooks(l)*newnx(l)),rlooks(l),newnx(l)); 40 | tmp = sum(tmp,1); %sum along rlooks dir 41 | 42 | tmp=tmp/rlooks(l)/alooks(l); 43 | %tmp(tmp<.25)=0; 44 | fwrite(fidout,tmp,'real*4'); 45 | 46 | 47 | end 48 | fclose(fidin); 49 | fclose(fidout); 50 | system(['mag_phs2rmg ' maskfilerlk ' ' maskfilerlk ' ' rlkdir 'mask.cor ' num2str(newnx(l))]); 51 | 52 | end 53 | 54 | % fid=fopen('TS/gamma0.r4','r','native'); 55 | % mm=fread(fid,[nx,ny],'real*4')'; 56 | % figure;imagesc(mm); 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /make_ramps.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | ndates = length(dates); 4 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 5 | diff_file = [oldintdir 'diffnsim_flat_HDR_' dates(ints(intid).i1).name '-' dates(ints(intid).i2).name '.int.in']; 6 | 7 | [nx,ny]=load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 8 | 9 | for i=[1:id-1 id+1:ndates] 10 | rampname=[intdir 'ramp_' dates(i).name '.unw']; 11 | newdiff=[intdir 'diffnsim_' dates(i).name '.in']; 12 | if(~exist(newdiff,'file')) 13 | for j=1:7 14 | bvec(j)=dates(i).bvec(j)-dates(id).bvec(j); 15 | end 16 | copyfile(diff_file,newdiff); 17 | input(1).name='Ramped input interferogram'; 18 | input(1).val=ints(1).int; 19 | input(2).name='Differential output interferogram'; 20 | input(2).val='jnk'; 21 | input(3).name='DEM in radar coordinates'; 22 | input(3).val=[oldintdir 'radar.hgt']; 23 | input(4).name='Simulated output DEM interferogram'; 24 | input(4).val=rampname; 25 | input(5).name='Cross Track Baseline, Rate & Acceleration'; 26 | input(5).val=num2str(bvec(1:3),10); 27 | input(6).name='Vertical Baseline, Rate, & Acceleration'; 28 | input(6).val=num2str(bvec(4:6),10); 29 | input(7).name='Platform Altitude, Rate, & Acceleration'; 30 | input(7).val=num2str(dates(id).hgtvec,10); 31 | input(8).name='Platform Velocity'; 32 | input(8).val=num2str(dates(id).vel,10); 33 | input(9).name='Number of pixels down, across'; 34 | input(9).val=[num2str(ny) ' ' num2str(nx)]; 35 | input(10).name='Starting Range for SLCs'; 36 | input(10).val=[num2str(dates(id).startrange,10) ' ' num2str(dates(i).startrange,10)]; 37 | use_rdf(newdiff,'write',input); 38 | end 39 | end 40 | 41 | for i=[1:id-1 id+1:ndates] 42 | newdiff=[intdir 'diffnsim_' dates(i).name '.in']; 43 | rampname=[intdir 'ramp_' dates(i).name '.unw']; 44 | if(~exist(rampname,'file')) 45 | command=['$INT_BIN/diffnsim ' newdiff]; 46 | mysys(command); 47 | else 48 | disp([rampname ' already made']) 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /make_slcs.m: -------------------------------------------------------------------------------- 1 | % set_params 2 | % load(ts_paramfile) 3 | % ndates=length(dates); 4 | % 5 | % for i=1:ndates 6 | % if(~exist(dates(i).slc,'file')) 7 | % chdir(dates(i).dir); 8 | % 9 | % command=['roi ' dates(i).name '_roi.in > ' dates(i).name '_roi.out']; 10 | % [status,result]=mysys(command); 11 | % 12 | % command=['length.pl ' dates(i).name '.slc']; 13 | % [status,result]=mysys(command); 14 | % 15 | % chdir(masterdir); 16 | % end 17 | % 18 | % end 19 | 20 | 21 | 22 | 23 | home=pwd; 24 | set_params 25 | load(ts_paramfile) 26 | ndates=length(dates); 27 | 28 | for i=1:ndates 29 | if(~exist(dates(i).slc,'file')) 30 | chdir(dates(i).dir); 31 | 32 | command=['roi ' dates(i).name '_roi.in > ' dates(i).name '_roi.out']; 33 | [status,result]=mysys(command); 34 | chdir(home); 35 | end 36 | 37 | end 38 | 39 | -------------------------------------------------------------------------------- /mask_unwrapped.m: -------------------------------------------------------------------------------- 1 | %Mask Unwrapped 2 | clear all;close all 3 | gammathresh=.1; 4 | set_params 5 | 6 | %load mask file 7 | mask_file=maskfilerlk; 8 | 9 | fid=fopen(mask_file,'r','native'); 10 | msk=fread(fid,[newnx,newny],'real*4'); 11 | fclose(fid); 12 | figure;imagesc(msk);colorbar 13 | 14 | ii=4; 15 | filename=[ints(ii).unwrlk]; 16 | display(['Masking ' ints(ii).unwrlk '_no_filt']) 17 | fid = fopen(filename,'r','native'); 18 | [phs,count] = fread(fid,[newnx,newny],'real*4'); 19 | fclose(fid); 20 | fid=fopen('phs_unmasked','w','native'); 21 | fwrite(fid,phs,'real*4') 22 | fclose(fid) 23 | 24 | mask=ones(size(phs)); 25 | mask(find(msk>matlog']); 4 | [status, result]=system(command); -------------------------------------------------------------------------------- /old/invert_dates.m: -------------------------------------------------------------------------------- 1 | function invert_dates(topoflag) 2 | %0 looks for unwrlk{l}, 1 adds _topo.unw to name. 3 | topoflag=0; 4 | set_params 5 | load(ts_paramfile); 6 | 7 | ndates = length(dates); 8 | nints = length(ints); 9 | [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 10 | newnx = floor(nx./rlooks) 11 | newny = floor(ny./alooks); 12 | 13 | [G,Gg,R,N]=build_Gint; 14 | [m,n]=size(Gg); 15 | 16 | for l=1:length(rlooks) 17 | for i=1:nints 18 | if(topoflag) 19 | infile=[ints(i).unwrlk{l} '_topo.unw']; 20 | else 21 | infile=ints(i).unwrlk{l}; 22 | end 23 | if(~exist(infile)) 24 | disp([infile ' does not exist']) 25 | return 26 | end 27 | fidi(i)=fopen(infile,'r'); 28 | end 29 | for i=1:ndates 30 | fido(i)=fopen(dates(i).unwrlk{l},'w'); 31 | end 32 | fido2=fopen(['res_' num2str(rlooks(l))],'w'); 33 | 34 | for j=1:newny(l) 35 | tmpdat=zeros(n,newnx(l)); %data for n-nints = zeros 36 | 37 | 38 | for i=1:nints 39 | jnk = fread(fidi(i),newnx(l),'real*4'); 40 | tmpdat(i,1:length(jnk))=jnk; 41 | end 42 | 43 | mod = Gg*tmpdat; 44 | synth = G*mod; 45 | res = tmpdat-synth; 46 | resstd = std(res,1); 47 | fwrite(fido2,resstd,'real*4'); 48 | for i=1:ndates 49 | fwrite(fido(i),mod(i,:),'real*4'); 50 | end 51 | end 52 | fclose('all'); 53 | end 54 | -------------------------------------------------------------------------------- /old/invert_rates.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | 4 | ndates = length(dates); 5 | nints = length(ints); 6 | [nx,ny,lambda] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH','WAVELENGTH'); 7 | newnx = floor(nx./rlooks) 8 | newny = floor(ny./alooks); 9 | [X,Y] = meshgrid(1:newnx,1:newny); 10 | 11 | dn = [dates.dn]; 12 | dn = dn-dn(1); 13 | 14 | for l=1:length(rlooks) 15 | %for l=1:1 16 | [X,Y]=meshgrid(1:newnx(l),1:newny(l)); 17 | 18 | alld=zeros(ndates,newny(l),newnx(l)); 19 | for i=1:ndates 20 | fid=fopen(dates(i).unwrlk{l},'r'); 21 | tmp=fread(fid,[newnx(l),newny(l)],'real*4'); 22 | alld(i,:,:)=tmp'; 23 | end 24 | 25 | G = [ones(length(dn),1) dn']; 26 | Gg = inv(G'*G)*G'; 27 | 28 | alld = reshape(alld,[ndates,newnx(l)*newny(l)]); 29 | mod = Gg*alld; 30 | offs = reshape(mod(1,:),[newny(l) newnx(l)]); 31 | rates = reshape(mod(2,:),[newny(l),newnx(l)])*lambda/(4*pi)*1000*365; %mm/yr 32 | synth = G*mod; 33 | res = (alld-synth)*lambda/(4*pi)*1000; %mm 34 | resstd = std(res,1); 35 | resstd = reshape(resstd,[newny(l) newnx(l)]); 36 | 37 | %mask 38 | % fidin = fopen('TS/looks4/mask_4rlks.r4','r','native'); 39 | % msk=fread(fidin,[newnx,newny],'real*4'); 40 | % msk=msk'; 41 | % rates(msk<0.3)=0; 42 | % rates=tmp>0.1; 43 | % fwrite(fidout,out,'integer*1'); 44 | 45 | 46 | 47 | fout1=fopen(['rates_' num2str(rlooks(l))],'w'); 48 | fout2=fopen(['ratestd_' num2str(rlooks(l))],'w'); 49 | for i=1:newny(l) 50 | fwrite(fout1,rates(i,:),'real*4'); 51 | fwrite(fout2,resstd(i,:),'real*4'); 52 | end 53 | fclose('all') 54 | end 55 | 56 | figure 57 | imagesc(-rates) 58 | caxis([-20 20]) -------------------------------------------------------------------------------- /par_ps_interp.m: -------------------------------------------------------------------------------- 1 | function par_ps_interp(ii,msk,gamma,alpha,rx,ry) 2 | set_params 3 | load(ts_paramfile) 4 | im = sqrt(-1); 5 | 6 | 7 | display(['filtering ' ints(ii).name]) 8 | %load int phase and correlation 9 | intfile=[ints(ii).flatrlk]; 10 | fid=fopen(intfile,'r','native'); 11 | [phs1,count] = fread(fid,[newnx,newny],'real*4'); 12 | fclose(fid); 13 | 14 | corfile=[ints(ii).flatrlk '.cor']; 15 | fid=fopen(corfile,'r','native'); 16 | [cor,count] = fread(fid,[newnx,newny],'real*4'); 17 | fclose(fid); 18 | 19 | 20 | %mask int to leave PS 21 | mask = mskgamma | cor>alpha; %high values are good 22 | 23 | real(~mask)=0; 24 | imag(~mask)=0; 25 | 26 | % Do the interpolation 27 | 28 | win_dimension=R*2+1; 29 | win=ones(win_dimension);%make an odd window of ones 30 | weight=zeros(size(win)); 31 | for i=1:win_dimension 32 | for j=1:win_dimension 33 | r(i,j)=sqrt(((R+1)-i)^2+((R+1)-j)^2); %distance from center of window 34 | weight(i,j)=exp((-r(i,j)^2)/(2*R)); %distance weighting 35 | end 36 | end 37 | 38 | weight=weight/sum(weight(:)); 39 | % % 40 | rea_f=zeros(size(mask)); 41 | ima_f=zeros(size(mask)); 42 | 43 | for j=1:newny-win_dimension 44 | rea_f(j:j+win_dimension-1,:)=conv2(real(j:j+win_dimension-1,:),weight,'same'); 45 | ima_f(j:j+win_dimension-1,:)=conv2(imag(j:j+win_dimension-1,:),weight,'same'); 46 | end 47 | 48 | 49 | real_final=rea_f+real; 50 | imag_final=ima_f+imag; 51 | 52 | phs = angle(real_final+im*imag_final); 53 | 54 | display([num2str(100*(sum(mask(:))/(newnx*newny))) '% of points left after masking']) 55 | fid=fopen('phs','w'); 56 | fwrite(fid,flipud(phs)','real*4'); 57 | fclose(fid); 58 | system(['mag_phs2cpx ' maskfilerlk{1} ' phs ' ints(ii).flatrlk{1} '_interp ' num2str(newnx)]); -------------------------------------------------------------------------------- /ps_interp_runpar.m: -------------------------------------------------------------------------------- 1 | %load in an unw int, interpolate, and unwrap 2 | %test 3 | clear all;close all 4 | 5 | %Define these things:______________________________________________________ 6 | gamma=0.3; %threshold for gamma0 mask 7 | alpha=0.5; %threshold for correlation in individual int 8 | %(high values for gamma and alpha are good) 9 | R=5; %define radius of filter 10 | %__________________________________________________________________________ 11 | 12 | getstuff; 13 | im = sqrt(-1); 14 | system(['mkdir ' rlkdir{1} 'orig']); 15 | %load mask.cor 16 | mskfile=[rlkdir{1} 'mask_4rlks.r4']; 17 | fid=fopen( maskfilerlk{1},'r','native'); 18 | msk=fread(fid,[newnx,newny],'real*4'); 19 | msk=flipud(msk'); 20 | fclose(fid); 21 | tic 22 | parfor ii=1:nints 23 | ps_interp_par(ii,gamma,alpha,R,im,msk) 24 | end 25 | toc 26 | 27 | % system(['mv ' rlkdir{1} '*_orig ' rlkdir{1} 'orig/']); 28 | 29 | %move orig files back 30 | % for kk=1:nints 31 | % system(['mv ' rlkdir{1} 'flat_' ints(kk).name '_4rlks.int_orig ' ints(ii).flatrlk{1}]); 32 | % end -------------------------------------------------------------------------------- /ps_unwrap_allps.m: -------------------------------------------------------------------------------- 1 | %ps_unwrap_allps 2 | % function []=ps_unwrap() 3 | %PS_UNWRAP unwrap phase using the 3-D cost function phase unwrapping algorithm 4 | % 5 | % Andy Hooper, Jun 2006 6 | % 7 | % ====================================================================== 8 | % 03/2008 AH: Fixed bug to make prefilter='n' option work 9 | % 03/2009 AH: Use smoothed scla instead of scla files 10 | % 06/2009 AH: Orbital ramps option added 11 | % 08/2009 AH: Goldstein alpha value added to options 12 | % 02/2010 AH: Replace unwrap_ifg_index with drop_ifg_index 13 | % ====================================================================== 14 | % logit; 15 | close all;clear all 16 | fprintf('Phase-unwrapping...\n') 17 | 18 | small_baseline_flag=getparm('small_baseline_flag',1); 19 | unwrap_patch_phase=getparm('unwrap_patch_phase',1); 20 | scla_deramp=getparm('scla_deramp',1); 21 | 22 | if exist('psver.mat','file') 23 | load psver 24 | else 25 | psver=1; 26 | end 27 | save('psver','psver') 28 | 29 | psname=['ps',num2str(psver)]; 30 | rcname=['rc',num2str(psver)]; 31 | pmname=['pm',num2str(psver)]; 32 | bpname=['bp',num2str(psver)]; 33 | if ~strcmpi(small_baseline_flag,'y') 34 | sclaname=['scla_smooth',num2str(psver)]; 35 | apsname=['aps',num2str(psver)]; 36 | phuwname=['phuw',num2str(psver)]; 37 | else 38 | sclaname=['scla_smooth_sb',num2str(psver)]; 39 | apsname=['aps_sb',num2str(psver)]; 40 | phuwname=['phuw_sb',num2str(psver)]; 41 | end 42 | 43 | ps=load(psname); 44 | 45 | drop_ifg_index=getparm('drop_ifg_index',1); 46 | unwrap_ifg_index=setdiff([1:ps.n_ifg],drop_ifg_index); 47 | 48 | if strcmpi(unwrap_patch_phase,'y') 49 | pm=load(pmname); 50 | ph_w=pm.ph_patch./abs(pm.ph_patch); 51 | clear pm 52 | if ~strcmpi(small_baseline_flag,'y') 53 | ph_w=[ph_w(:,1:ps.master_ix-1),zeros(ps.n_ps,1),ph_w(:,ps.master_ix:end)]; 54 | end 55 | else 56 | rc=load(rcname); 57 | ph_w=rc.ph_rc; 58 | clear rc; 59 | end 60 | 61 | ix=ph_w~=0; 62 | ph_w(ix)=ph_w(ix)./abs(ph_w(ix)); % normalize, to avoid high freq artifacts being introduced in adaptive filtering 63 | 64 | 65 | if exist(['./',bpname,'.mat'],'file') 66 | bp=load(bpname); 67 | else 68 | bperp=ps.bperp; 69 | if ~strcmpi(small_baseline_flag,'y') 70 | bperp=bperp([1:ps.master_ix-1,ps.master_ix+1:end]); 71 | end 72 | bp.bperp_mat=repmat(bperp',ps.n_ps,1); 73 | end 74 | 75 | scla_subtracted_sw=0; 76 | ramp_subtracted_sw=0; 77 | 78 | if ~strcmpi(small_baseline_flag,'y') & exist([sclaname,'.mat'],'file') 79 | fprintf(' subtracting scla and master aoe...\n') 80 | bperp_mat=[bp.bperp_mat(:,1:ps.master_ix-1),zeros(ps.n_ps,1,'single'),bp.bperp_mat(:,ps.master_ix:end)]; 81 | scla=load(sclaname); 82 | if size(scla.K_ps_uw,1)==ps.n_ps 83 | scla_subtracted_sw=1; 84 | ph_w=ph_w.*exp(-j*repmat(scla.K_ps_uw,1,ps.n_ifg).*bperp_mat); % subtract spatially correlated look angle error 85 | ph_w=ph_w.*repmat(exp(-j*scla.C_ps_uw),1,ps.n_ifg); % subtract master APS 86 | if strcmpi(scla_deramp,'y') & isfield(scla,'ph_ramp') & size(scla.ph_ramp,1)==ps.n_ps 87 | ramp_subtracted_sw=1; 88 | ph_w=ph_w.*exp(-j*scla.ph_ramp); % subtract orbital ramps 89 | end 90 | else 91 | fprintf(' wrong number of PS in scla - subtraction skipped...\n') 92 | delete([sclaname,'.mat']) 93 | end 94 | clear scla 95 | end 96 | 97 | if strcmpi(small_baseline_flag,'y') & exist([sclaname,'.mat'],'file') 98 | fprintf(' subtracting scla...\n') 99 | bperp_mat=bp.bperp_mat; 100 | scla=load(sclaname); 101 | if size(scla.K_ps_uw,1)==ps.n_ps 102 | scla_subtracted_sw=1; 103 | ph_w=ph_w.*exp(-j*repmat(scla.K_ps_uw,1,ps.n_ifg).*bperp_mat); % subtract spatially correlated look angle error 104 | if strcmpi(scla_deramp,'y') & isfield(scla,'ph_ramp') & size(scla.ph_ramp,1)==ps.n_ps 105 | ramp_subtracted_sw=1; 106 | ph_w=ph_w.*exp(-j*scla.ph_ramp); % subtract orbital ramps 107 | end 108 | else 109 | fprintf(' wrong number of PS in scla - subtraction skipped...\n') 110 | delete([sclaname,'.mat']) 111 | end 112 | clear scla 113 | end 114 | 115 | clear bp 116 | 117 | if exist([apsname,'.mat'],'file') 118 | fprintf(' subtracting slave aps...\n') 119 | aps=load(apsname); 120 | ph_w=ph_w.*exp(-j*aps.ph_aps_slave); 121 | clear aps 122 | end 123 | 124 | 125 | options=struct('master_day',ps.master_day); 126 | options.time_win=getparm('unwrap_time_win',1); 127 | options.unwrap_method=getparm('unwrap_method',1); 128 | options.grid_size=getparm('unwrap_grid_size',1); 129 | options.prefilt_win=getparm('unwrap_gold_n_win',1); 130 | options.goldfilt_flag=getparm('unwrap_prefilter_flag',1); 131 | options.gold_alpha=getparm('unwrap_gold_alpha',1); 132 | 133 | if strcmpi(small_baseline_flag,'y') 134 | %options.lowfilt_flag='y'; 135 | options.lowfilt_flag='n'; 136 | ifgday_ix=ps.ifgday_ix; 137 | day=ps.day-ps.master_day; 138 | else 139 | lowfilt_flag='n'; 140 | ifgday_ix=[]; 141 | master_ix=sum(ps.master_day>ps.day)+1; 142 | unwrap_ifg_index=setdiff(unwrap_ifg_index,master_ix); % leave master ifg (which is only noise) out 143 | day=ps.day(unwrap_ifg_index)-ps.master_day; 144 | end 145 | 146 | [ph_uw_some,msd_some]=uw_3d(ph_w(:,unwrap_ifg_index),ps.xy,day,ifgday_ix,options); 147 | 148 | ph_uw=zeros(ps.n_ps,ps.n_ifg,'single'); 149 | msd=zeros(ps.n_ifg,1,'single'); 150 | ph_uw(:,unwrap_ifg_index)=ph_uw_some; 151 | msd(unwrap_ifg_index)=msd_some; 152 | 153 | if scla_subtracted_sw & ~strcmpi(small_baseline_flag,'y') 154 | fprintf('Adding back SCLA and master AOE...\n') 155 | scla=load(sclaname); 156 | ph_uw=ph_uw+(repmat(scla.K_ps_uw,1,ps.n_ifg).*bperp_mat); % add back spatially correlated look angle error 157 | ph_uw=ph_uw+repmat(scla.C_ps_uw,1,ps.n_ifg); % add back master APS 158 | if ramp_subtracted_sw 159 | ph_uw=ph_uw+scla.ph_ramp; % add back orbital ramps 160 | end 161 | clear bp scla 162 | end 163 | 164 | if scla_subtracted_sw & strcmpi(small_baseline_flag,'y') 165 | fprintf('Adding back SCLA...\n') 166 | scla=load(sclaname); 167 | ph_uw=ph_uw+(repmat(scla.K_ps_uw,1,ps.n_ifg).*bperp_mat); % add back spatially correlated look angle error 168 | if ramp_subtracted_sw 169 | ph_uw=ph_uw+scla.ph_ramp; % add back orbital ramps 170 | end 171 | clear bp scla 172 | end 173 | 174 | if exist([apsname,'.mat'],'file') 175 | fprintf('Adding back slave APS...\n') 176 | aps=load(apsname); 177 | ph_uw=ph_uw+aps.ph_aps_slave; 178 | clear aps 179 | end 180 | 181 | if strcmpi(unwrap_patch_phase,'y') 182 | pm=load(pmname); 183 | ph_w=pm.ph_patch./abs(pm.ph_patch); 184 | clear pm 185 | if ~strcmpi(small_baseline_flag,'y') 186 | ph_w=[ph_w(:,1:ps.master_ix-1),zeros(ps.n_ps,1),ph_w(:,ps.master_ix:end)]; 187 | end 188 | rc=load(rcname); 189 | ph_uw=ph_uw+angle(rc.ph_rc.*conj(ph_w)); 190 | end 191 | 192 | ph_uw(:,setdiff([1:ps.n_ifg],unwrap_ifg_index))=0; 193 | 194 | save(phuwname,'ph_uw','msd') 195 | logit(1); 196 | -------------------------------------------------------------------------------- /rect_slcs.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | ndates = length(dates); 4 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 5 | 6 | %run master_int to get starting line of any offsets. 7 | resamp_file = [oldintdir dates(ints(intid).i1).name '-' dates(ints(intid).i2).name '_resamp.in']; 8 | input(1).name ='Starting Line, Number of Lines, and First Line Offset'; 9 | info = use_rdf(resamp_file,'read',input); 10 | info = str2num(info.val); 11 | startline = info(1); 12 | ny = info(2); 13 | 14 | 15 | %initoff = 50; %this was used in read_dopbase to shift the master date backwards 16 | lambda = load_rscs(dates(id).raw,'WAVELENGTH'); 17 | %[dop0,dop1,dop2,dop3,azres,squint]=load_rscs('all.dop.rsc','DOPPLER_RANGE0','DOPPLER_RANGE1','DOPPLER_RANGE2','DOPPLER_RANGE3','AZIMUTH_RESOLUTION','SQUINT'); 18 | [nx1,ny1]=load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 19 | command=['$INT_SCR/use_rsc.pl ' dates(id).slc ' write FILE_LENGTH ' num2str(ny)]; 20 | mysys(command); 21 | 22 | %crop master date if necessary 23 | if(startline>1) 24 | rectin=[rectdir 'rectfiles/rect_' dates(i).name '.in']; 25 | fid=fopen(rectin,'w'); 26 | fprintf(fid,'Input Image File Name (-) = %s ! dimension of file to be rectified\n',dates(id).slc); 27 | fprintf(fid,'Output Image File Name (-) = %s ! dimension of output\n',dates(id).rectslc); 28 | fprintf(fid,'Input Dimensions (-) = %d %d ! across, down\n',nx1,ny1); 29 | fprintf(fid,'Output Dimensions (-) = %d %d! across, down\n',nx1,ny); 30 | fprintf(fid,'Affine Matrix Row 1 (-) = 1 0 ! a b\n'); 31 | fprintf(fid,'Affine Matrix Row 2 (-) = 0 1 ! c d\n'); 32 | fprintf(fid,'Affine Offset Vector (-) = 0 %d ! e f\n',startline); 33 | fprintf(fid,'File Type (-) = COMPLEX ! [RMG, COMPLEX]\n'); 34 | fprintf(fid,'Interpolation Method (-) = Bilinear ! [NN, Bilinear, Sinc]\n'); 35 | fclose(fid); 36 | 37 | command=['rect ' rectin]; 38 | mysys(command); 39 | 40 | copyfile([dates(id).slc '.rsc'],[dates(id).rectslc '.rsc']); 41 | 42 | else 43 | copyfile([dates(id).slc],[dates(id).rectslc]); 44 | copyfile([dates(id).slc '.rsc'],[dates(id).rectslc '.rsc']); 45 | end 46 | dates(id).aff=[1 0 1 0 0 0]; 47 | 48 | for i=[1:id-1 id+1:ndates] 49 | [nx2,ny2]=load_rscs(dates(i).slc,'WIDTH','FILE_LENGTH'); 50 | 51 | offname=[rectdir 'rectfiles/' dates(i).name]; 52 | if(~exist([offname '_fitoff.out'])) 53 | command=['$INT_SCR/offset.pl ' dates(id).slc ' ' dates(i).slc ' ' offname ' 2 cpx ' num2str([dates(i).rgoff dates(i).azoff noff noff offwin searchwin])]; 54 | mysys(command); 55 | command=['$MY_BIN/fitoff_quad ' offname '.off ' offname '_cull.off 1.5 0.2 100 > ' offname '_fitoff.out']; 56 | mysys(command); 57 | end 58 | end 59 | 60 | if(plotflag) 61 | %check offsets 62 | f1 = figure('Name','X Offsets'); 63 | f2 = figure('Name','Y Offsets'); 64 | a = floor(sqrt(ndates)); 65 | b = ceil(ndates/a); 66 | ax = [1 nx1 1 ny1]; 67 | for i=[1:id-1 id+1:ndates] 68 | tmp=load([rectdir 'rectfiles/' dates(i).name '_cull.off']); 69 | figure(f1); 70 | subplot(a,b,i) 71 | scatter(tmp(:,1),tmp(:,3),12,tmp(:,2),'filled'); 72 | axis(ax); 73 | colorbar('h') 74 | title(dates(i).name) 75 | figure(f2); 76 | subplot(a,b,i) 77 | scatter(tmp(:,1),tmp(:,3),12,tmp(:,4),'filled'); 78 | axis(ax); 79 | colorbar('h') 80 | title(dates(i).name) 81 | end 82 | end 83 | 84 | %Run rect 85 | for i=[1:id-1 id+1:ndates] 86 | if(~exist(dates(i).rectslc)) 87 | command=['grep WARNING ' offname '_fitoff.out']; 88 | [status,result]=mysys(command); 89 | offname=[rectdir 'rectfiles/' dates(i).name]; 90 | 91 | 92 | if(status) 93 | command=['$OUR_SCR/find_affine_quad.pl ' offname '_fitoff.out']; 94 | [junk,aff]=mysys(command); 95 | aff=str2num(aff); 96 | [dop0,dop1,dop2,dop3,azres,squint]=load_rscs(dates(i).slc,'DOPPLER_RANGE0','DOPPLER_RANGE1','DOPPLER_RANGE2','DOPPLER_RANGE3','AZIMUTH_RESOLUTION','SQUINT'); 97 | rangepx = load_rscs(dates(i).raw,'RANGE_PIXEL_SIZE'); 98 | 99 | resampin=[rectdir 'rectfiles/resamp_' dates(i).name '.in']; 100 | fid=fopen(resampin,'w'); 101 | fprintf(fid,'Image Offset File Name (-) = %s\n',[offname '_cull.off']); 102 | fprintf(fid,'Display Fit Statistics to Screen (-) = No Fit Stats\n'); 103 | fprintf(fid,'Number of Fit Coefficients (-) = 6\n'); 104 | fprintf(fid,'SLC Image File 1 (-) = %s\n',dates(id).slc); 105 | fprintf(fid,'Number of Range Samples Image 1 (-) = %d\n',nx1); 106 | fprintf(fid,'SLC Image File 2 (-) = %s\n',dates(i).slc); 107 | fprintf(fid,'Number of Range Samples Image 2 (-) = %d\n',nx2); 108 | fprintf(fid,'Starting Line, Number of Lines, and First Line Offset (-) = %d %d 1\n',startline,ny); 109 | fprintf(fid,'Doppler Cubic Fit Coefficients - PRF Units (-) = %12.8g %12.8g %12.8g 0\n',dop0,dop1,dop2); 110 | fprintf(fid,'Radar Wavelength (m) = %12.8g\n',lambda); 111 | fprintf(fid,'Slant Range Pixel Spacing (m) = %12.8g\n',rangepx); 112 | fprintf(fid,'Number of Range and Azimuth Looks (-) = 1 1\n'); 113 | fprintf(fid,'Flatten with offset fit? (-) = No \n'); 114 | fprintf(fid,'Resampled SLC 1 File (-) = %s\n',dates(id).rectslc); 115 | fprintf(fid,'Resampled SLC 2 File (-) = %s\n',dates(i).rectslc); 116 | fprintf(fid,'Output Interferogram File (-) = jnkint\n'); 117 | fprintf(fid,'Multi-look Amplitude File (-) = jnkamp\n'); 118 | fprintf(fid,'END\n'); 119 | fclose(fid); 120 | 121 | command=['$MY_BIN/resamp_roi_nofilter ' resampin]; 122 | mysys(command); 123 | copyfile([dates(i).slc '.rsc'],[dates(i).rectslc '.rsc']); 124 | command=['$INT_SCR/use_rsc.pl ' dates(i).rectslc ' write WIDTH ' num2str(nx1)]; 125 | mysys(command); 126 | command=['$INT_SCR/use_rsc.pl ' dates(i).rectslc ' write FILE_LENGTH ' num2str(ny)]; 127 | mysys(command); 128 | dates(i).aff=aff; 129 | else 130 | disp(result) 131 | end 132 | end 133 | end 134 | 135 | 136 | 137 | 138 | 139 | 140 | if(exist('ints','var')) 141 | save(ts_paramfile,'dates','ints'); 142 | else 143 | save(ts_paramfile,'dates'); 144 | end 145 | -------------------------------------------------------------------------------- /rect_slcs_km.m: -------------------------------------------------------------------------------- 1 | clear all;close all 2 | 3 | set_params 4 | load(ts_paramfile); 5 | ndates = length(dates); 6 | 7 | 8 | %master int must have been run first, to determine any starting line offset. 9 | resampname=[masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/' ints(intid).name '_resamp.in']; 10 | if(~exist(resampname)) 11 | disp(['need to run master int first: ' resampname]); 12 | return 13 | end 14 | 15 | input(1).name='Starting Line, Number of Lines, and First Line Offset'; 16 | tmp=use_rdf(resampname,'read',input); 17 | tmp=str2num(tmp.val); 18 | startline=tmp(1); 19 | disp(['starting line is: ' num2str(startline) ': may need to be 1']) 20 | 21 | rangepx = load_rscs(dates(id).raw,'RANGE_PIXEL_SIZE'); 22 | lambda = load_rscs(dates(id).raw,'WAVELENGTH'); 23 | if(regexp(sat,'TSX')) 24 | initoff = 0; 25 | dop0 = 0; 26 | dop1 = 0; 27 | dop2 = 0; 28 | dop3 = 0; 29 | squint = 0; 30 | azres=load_rscs(dates(1).raw,'AZIMUTH_PIXEL_SIZE'); 31 | else 32 | [dop0,dop1,dop2,dop3,azres,squint]=load_rscs('all.dop.rsc','DOPPLER_RANGE0','DOPPLER_RANGE1','DOPPLER_RANGE2','DOPPLER_RANGE3','AZIMUTH_RESOLUTION','SQUINT'); 33 | end 34 | copyfile([dates(id).slc '.rsc'],[dates(id).rectslc '.rsc']); 35 | dates(id).aff=[1 0 1 0 0 0]; 36 | 37 | [nx1,ny1]=load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 38 | 39 | for i=[1:id-1 id+1:ndates] 40 | % for i=1 41 | [nx2,ny2]=load_rscs(dates(i).slc,'WIDTH','FILE_LENGTH'); %number of lines 42 | offname=[rectdir 'rectfiles/' dates(i).name]; %Save the date name as offname 43 | 44 | if(~exist([offname '_fitoff.out'])) 45 | command=['$INT_SCR/offset.pl ' dates(id).slc ' ' dates(i).slc ' ' offname ' 2 cpx ' num2str([dates(i).rgoff dates(i).azoff noff noff offwin searchwin])] 46 | mysys(command); 47 | command=['$MY_BIN/fitoff_quad ' offname '.off ' offname '_cull.off 1.5 0.5 50 > ' offname '_fitoff.out'] 48 | mysys(command); 49 | end 50 | end 51 | 52 | if(plotflag) 53 | %check offsets 54 | f1 = figure('Name','X Offsets'); 55 | f2 = figure('Name','Y Offsets'); 56 | a = floor(sqrt(ndates)); 57 | b = ceil(ndates/a); 58 | ax = [1 nx1 1 ny1]; 59 | for i=[1:id-1 id+1:ndates] 60 | % for i=1 61 | tmp=load([rectdir 'rectfiles/' dates(i).name '_cull.off']); 62 | 63 | figure(f1); 64 | subplot(a,b,i) 65 | scatter(tmp(:,1),tmp(:,3),12,tmp(:,2),'filled'); 66 | axis(ax); 67 | colorbar('h') 68 | title(dates(i).name) 69 | 70 | figure(f2); 71 | subplot(a,b,i) 72 | scatter(tmp(:,1),tmp(:,3),12,tmp(:,4),'filled'); 73 | axis(ax); 74 | colorbar('h') 75 | title(dates(i).name) 76 | end 77 | title(masterdir) 78 | % kylestyle 79 | 80 | % figure(f1) 81 | % print('-depsc',['figs/Xoff.eps ' masterdir]); 82 | % figure(f2) 83 | % print('-depsc',['figs/Yoff.eps ' masterdir]); 84 | 85 | end 86 | 87 | %Run rect 88 | for i=[1:id-1 id+1:ndates] 89 | % for i=1 90 | if(~exist(dates(i).rectslc)) 91 | command=['grep WARNING ' offname '_fitoff.out']; 92 | [status,result]=mysys(command); 93 | offname=[rectdir 'rectfiles/' dates(i).name]; 94 | 95 | 96 | if(status) 97 | command=['$OUR_SCR/find_affine_quad.pl ' offname '_fitoff.out']; 98 | [junk,aff]=mysys(command); 99 | aff=str2num(aff); 100 | 101 | resampin=[rectdir 'rectfiles/resamp_' dates(i).name '.in']; 102 | fid=fopen(resampin,'w'); 103 | fprintf(fid,'Image Offset File Name (-) = %s\n',[offname '_cull.off']); 104 | fprintf(fid,'Display Fit Statistics to Screen (-) = No Fit Stats\n'); 105 | fprintf(fid,'Number of Fit Coefficients (-) = 6\n'); 106 | fprintf(fid,'SLC Image File 1 (-) = %s\n',dates(id).slc); 107 | fprintf(fid,'Number of Range Samples Image 1 (-) = %d\n',nx1); 108 | fprintf(fid,'SLC Image File 2 (-) = %s\n',dates(i).slc); 109 | fprintf(fid,'Number of Range Samples Image 2 (-) = %d\n',nx2); 110 | fprintf(fid,'Starting Line, Number of Lines, and First Line Offset (-) = %d %d 1\n',startline,ny1); 111 | fprintf(fid,'Doppler Cubic Fit Coefficients - PRF Units (-) = %12.8g %12.8g %12.8g 0\n',dop0,dop1,dop2); 112 | fprintf(fid,'Radar Wavelength (m) = %12.8g\n',lambda); 113 | fprintf(fid,'Slant Range Pixel Spacing (m) = %12.8g\n',rangepx); 114 | fprintf(fid,'Number of Range and Azimuth Looks (-) = 1 1\n'); 115 | fprintf(fid,'Flatten with offset fit? (-) = No \n'); 116 | fprintf(fid,'Resampled SLC 1 File (-) = %s\n',dates(id).rectslc); 117 | fprintf(fid,'Resampled SLC 2 File (-) = %s\n',dates(i).rectslc); 118 | fprintf(fid,'Output Interferogram File (-) = jnkint\n'); 119 | fprintf(fid,'Multi-look Amplitude File (-) = jnkamp\n'); 120 | fprintf(fid,'END\n'); 121 | fclose(fid); 122 | 123 | command=['$MY_BIN/resamp_roi_nofilter ' resampin]; 124 | mysys(command); 125 | copyfile([dates(i).slc '.rsc'],[dates(i).rectslc '.rsc']); 126 | command=['$INT_SCR/use_rsc.pl ' dates(i).rectslc ' write WIDTH ' num2str(nx1)]; 127 | mysys(command); 128 | command=['$INT_SCR/use_rsc.pl ' dates(i).rectslc ' write FILE_LENGTH ' num2str(ny1)]; 129 | mysys(command); 130 | dates(i).aff=aff; 131 | else 132 | disp(result) 133 | end 134 | end 135 | end 136 | % %************************************************************************ 137 | %Make sure the rect file is not 0 size. If it is, then this will delete 138 | %all associated files. 139 | for i=[1:id-1 id+1:ndates] 140 | s=dir([rectdir 'rect_' dates(i).name '.slc']); 141 | if size(s)==[0 1] 142 | s(1).bytes=1; 143 | end 144 | filesize=s.bytes; 145 | if filesize < 1e9 %if less than 1Gb 146 | system(['rm ' rectdir '*' dates(i).name '*']); 147 | system(['rm ' rectdir 'rectfiles/*' dates(i).name '*']); 148 | removed(i)=dates(i).name; 149 | display([dates(i).name ' was bad and deleted.Find offsets manually and/or increase window size. Then rerun this script']) 150 | end 151 | end 152 | % %************************************************************************ 153 | % 154 | 155 | if(exist('ints','var')) 156 | save(ts_paramfile,'dates','ints'); 157 | else 158 | save(ts_paramfile,'dates'); 159 | end 160 | -------------------------------------------------------------------------------- /replace_dates_struct.m: -------------------------------------------------------------------------------- 1 | function replace_dates_struct(baddatesids, mode) 2 | %Mode=1: This will remove badintids from ints structure 3 | %Mode=2: This will change ints structure back to the original 4 | set_params 5 | load(ts_paramfile) 6 | 7 | switch mode 8 | case 1 9 | 10 | if(~exist([ts_paramfile '_orig'])) 11 | command=['mv ' ts_paramfile ' ' ts_paramfile '_orig'] 12 | mysys(command) 13 | end 14 | 15 | dates([baddatesids])=[]; 16 | save(ts_paramfile,'dates','ints') 17 | 18 | case 2 19 | command=['mv ' ts_paramfile '_orig ' ts_paramfile] 20 | mysys(command) 21 | end 22 | 23 | disp('MAY NEED TO CHANGE THE MASTER INT!') 24 | choose_ints_km 25 | set_params 26 | load(ts_paramfile); -------------------------------------------------------------------------------- /replace_ints_struct.m: -------------------------------------------------------------------------------- 1 | function replace_ints_struct(badintids, mode) 2 | %Mode=1: This will remove badintids from ints structure 3 | %Mode=2: This will change ints structure back to the original 4 | set_params 5 | load(ts_paramfile) 6 | 7 | switch mode 8 | case 1 9 | 10 | if(~exist([ts_paramfile '_orig'])) 11 | command=['mv ' ts_paramfile ' ' ts_paramfile '_orig'] 12 | mysys(command) 13 | end 14 | 15 | ints([badintids])=[]; 16 | save(ts_paramfile,'dates','ints') 17 | 18 | case 2 19 | command=['mv ' ts_paramfile '_orig ' ts_paramfile] 20 | mysys(command) 21 | end 22 | 23 | disp('MAY NEED TO CHANGE THE MASTER INT!') 24 | 25 | 26 | choose_ints_km 27 | set_params 28 | load(ts_paramfile); -------------------------------------------------------------------------------- /run_all.m: -------------------------------------------------------------------------------- 1 | %id: master date id. Change based on plot from search_data or your own intuition. 2 | clear all;close all 3 | track=163; 4 | frame=729; 5 | sat='ENVI'; 6 | plotflag=1; 7 | id=1; 8 | mkdir('figs') 9 | masterdir = [pwd '/'];%['/data/kdm95/' sat '/' num2str(track) '_' num2str(frame) '/']; %For now, set this by hand, make sure it exists, and run from this dir. 10 | 11 | 12 | 13 | [dn,footprints,searchresults,sortresults,sortdn,apiCall]=search_data(track,frame,sat,1,[]); 14 | % [dn,searchresults,sortdn,sortresults]=I_subset([50:56],dn,searchresults,sortdn,sortresults); %do only these date ids 15 | write_paramfile(sat,masterdir,id,footprints,1,pixel_ratio,rlk,track,frame); % (sat,masterdir,id,footprints,plotflag,pixel_ratio,track,frame) 16 | init_dirs; %initializes timeseries directories 17 | make_dates_struct(sortdn,sortresults); %makes dates structure, stores in ts_params.mat 18 | load_data; %After: Check to see if each dir now has data files 19 | setup_init 20 | % read_dopbase_km %selects dates that don't violate doppler, baseline, az off 21 | read_dopbase 22 | %new 23 | write_slc_proc 24 | %do 2 times more 25 | setup_init 26 | read_dopbase 27 | choose_ints_km %chooses set of interferograms, add or remove pairs by editing set_params.m 28 | 29 | % note: check azoff 30 | 31 | %% 32 | make_slcs 33 | master_int %This needs to be run before rect_slcs, to determine any offsets of the master relative to SIM 34 | 35 | % % Remove Bad Dates? 36 | % baddateids=[33]; 37 | % replace_dates_struct(baddateids,1) 38 | 39 | rect_slcs %If this does not result in rectified SLCs, you have a problem and must tinker by and in TS/rect/rectfiles. Requires resamp_roi_nofilter compiled. 40 | calamp %calculates mean amplitude in each slc 41 | avgrect %generates TS/avgslc.r4. Good to view this as quick check on rectification quality. Should look very sharp. 42 | 43 | make_ints 44 | make_ramps 45 | flat_ints 46 | makeGamma(0) %0, 1, 2, or 3 47 | makeGamma(1) 48 | make_mask(.2) 49 | smart_rlooks_2 50 | crop_edges([30*rlooks 30*rlooks 400*alooks 30*alooks]); %full res location of crop (so that it works for multiple rlooks, left right, top, bottom. 51 | ps_interp 52 | unwrap_rlooks %uses interp files now, does snaphu with tiles. 53 | 54 | % mask_unwrapped %masks using the gamma_4rlks.r4 file. first band is unmasked, second band is masked 55 | unw2png_km1(10,20) %(mode,wraprate,scale%) make a .png image of each unw_4lks in TS/looks4/png_files/ 56 | % disp('Examine the unwrapped ints and decide if filtering is required.') 57 | % 58 | % return 59 | %__________________________________________________________________________ 60 | %Try filtering, unwrapping, subtract unw from filtered, then add 2pi*n to 61 | %unfiltered int: 62 | % filter_unw_diff(1) 63 | %Or try filtering and unwrapping normally: 64 | % filter_rlooks 65 | % unwrap_filtered 66 | % unwfilt2png_km1(2,30,15) %(mode,wraprate,scale%) make a .png image of each unw_4lks in TS/looks4/png_files/ 67 | %__________________________________________________________________________ 68 | 69 | %% 70 | % filter_unw_diff(1) 71 | 72 | %remove Bad interferograms?________________________________________________ 73 | % badintids=[2 5 7 8 9 10 11]; 74 | % replace_ints_struct(badintids,1);set_params;load(ts_paramfile); 75 | % Mode=1: This will remove badintids from ints structure 76 | % Mode=2: This will change ints structure back to the original 77 | %__________________________________________________________________________ 78 | 79 | %iterate over this chunk 80 | invert_dates(0); %1 looks for .unw_topo.unw files (not made til fitramp_int), 0 looks for normal unw files. 81 | %%%this generates TS/looks#/date.r4 files and res#. Res# will have a ramp if not alligned. Choose thresh accordingly 82 | thresh =4; %this currently needs to be "big" for the first round, then iterate once or twice with smaller values. 83 | edge = [100 100 10 10*8]; %pixels from left, right, top, bottom edges to mask 84 | waterheight = [-10]; %mask all pixels with height < waterheight. 85 | topoflag = []; 86 | boxedge = [0 0 0 0];%[2437 2636 1357 1582]*4; 87 | fitramp_date(thresh,edge,waterheight,topoflag,boxedge,0); %topoflag=1 removes topo and writes to *_topo.unw files, 0 is normal. Now uses rates file, if it exists! 88 | 89 | invert_dates(0); 90 | geocode_dates; 91 | gps_reference; 92 | 93 | lf_power(2); 94 | invert_rates 95 | geocode('rates_2','geo_rates_2.unw') %Geocodes the rates_4 file and makes geo_rates_2.unw 96 | geocode('ratestd_2','geo_std_2.unw') %Geocodes the rates_4 file and makes geo_rates_2.unw 97 | plot_std_profile 98 | toc 99 | unwrap_flat %this can or can not be used - will reunwrap the now-flatter interferograms. Then iterate as above. 100 | 101 | %% 102 | %invert dates once with final set of flattened interferograms 103 | invert_dates(0); %1 looks for .unw_topo.unw files,0 looks for normal unw files. 104 | invert_rates %this uses the dates_nlooks.r4 files, however constructed 105 | calc_rate_residual %adds "rms" field to ints structure. can be used to decide whether to get rid of some ints. Then rerun choose_ints, and rerun from invert_dates. 106 | 107 | rp=[]; 108 | rms_thresh=100 %choose rms cutoff 109 | rp=[rp;[ints(abs([ints.rms])>rms_thresh).i1; ints(abs([ints.rms])>rms_thresh).i2]'] 110 | 111 | lf_power(5); %arg: # of freq bands 112 | 113 | %Geocode and make .kml file 114 | geocode('rates_2','geo_rates_2.unw') %Geocodes the rates_4 file and makes geo_rates_2.unw 115 | geocode('ratestd_2','geo_rates_2.unw') %Geocodes the rates_4 file and makes geo_rates_2.unw 116 | plot_std_profile 117 | unw2png_km('geo_rates_2.unw',[sat '_T' num2str(track) '_' num2str(frame) '.png'],2,10,50) %infile, outfile, mode(1 is amp, 2 is phs),wraprate,scale(% resize) 118 | make_frame_gmt 119 | 120 | mysys('unw2grd.pl geo_rates_2.unw ENVI_T485_2889.grd') 121 | 122 | 123 | -------------------------------------------------------------------------------- /run_all_tsx.m: -------------------------------------------------------------------------------- 1 | %%%set all of these! 2 | setenv('GFORTRAN_STDIN_UNIT','5') 3 | setenv('GFORTRAN_STDOUT_UNIT','6') 4 | setenv('GFORTRAN_STDERR_UNIT','0') 5 | setenv('DYLD_LIBRARY_PATH', '/usr/local/bin:/opt/local/lib:') 6 | 7 | masterdir = '/data/TSX/Kansas/T52/'; %For now, set this by hand, make sure it exists, and run from this dir. 8 | track = 52; %track for ERS/ENVI, path for ALOS 9 | frame = [820]; %frame for ERS/ENVI, row for ALOS. Can be vector of multiple adjacent frames 10 | sat = 'TSX'; %ERS,ENVI,ALOS or TSX 11 | % 12 | % track = 213; 13 | % frame = [2907]; 14 | % sat = 'ENVI'; %ENV1, ERS1, ERS2 15 | 16 | %%%two modes - either you've downloaded all the data yourself, into a 17 | %%%series of YYYYMMDD directories, or you are going to use the ASF/UNAVCO 18 | %%%api system to search the catalogs and download them. 19 | 20 | id = 1; %master date id. Change based on plot from search_data or your own intuition. 21 | datamode = 2; %1 = download, 2=already downloaded, in masterdir 22 | plotflag = 1; 23 | 24 | if (datamode==1) 25 | [dn,footprints,searchresults,sortresults,sortdn]=search_data(track,frame,sat,plotflag); 26 | write_paramfile(sat,masterdir,id,footprints,plotflag); %writes set_params.m, which you can edit. 27 | 28 | init_dirs; %initializes timeseries directories 29 | make_dates_struct(sortdn,sortresults); %makes dates structure, stores in ts_params.mat 30 | load_data(sat,searchresults,dn,sortdn); %After: Check to see if each dir now has data files 31 | 32 | elseif (datamode==2) 33 | %download and sort data into directories on your own, each with name YYYYMMDD 34 | write_paramfile(sat,masterdir,id,[],1); 35 | init_dirs; 36 | make_dates_struct([],[]); %makes dates structure, stores in ts_params.mat 37 | else 38 | disp('datamode must be 1 (to search and download new data) or 2 (data already exists, in YYYYMMDD dirs)') 39 | end 40 | 41 | make_all_raw %runs make_raw. Note: if the diffnsim_flat_HDR_20070526-20071011.int.in file doeesn't have "SLC relative line offset"=1, you have a problem. 42 | setup_init %runs process_2pass through baseline computation relative to master 43 | read_dopbase %selects dates that don't violate doppler, baseline, az off 44 | 45 | %%%Note - I THINK choose_ints can be run anytime from here on out, and 46 | %%%rerun if chosen ints change, i.e., to throw them out. And if you add 47 | %%%them, it should go through the remaining scripts (except calc_gamma.m) 48 | %%%and only change what it has to. 49 | choose_ints %chooses set of interferograms, add or remove pairs by editing set_params.m 50 | 51 | make_slcs %This really shouldn't fail. 52 | rect_slcs %If this does not result in rectified SLCs, you have a problem and must tinker by and in TS/rect/rectfiles. Requires resamp_roi_nofilter compiled. 53 | master_int %This needs to be run before flat_ints, can be run in parallel with rect_slcs. Load own dem first into DEM/tmp.dem, or will try loading SRTM. 54 | 55 | calamp %calculates mean amplitude in each slc 56 | avgrect %generates TS/avgslc.r4. Good to view this as quick check on rectification quality. Should look very sharp. 57 | 58 | %note - we don't make interferograms anymore- go straight to flattened after ramps. 59 | make_ramps 60 | flat_ints 61 | FilterFlat 62 | CalcGamma 63 | 64 | make_mask(gammathresh) %outputs not really used right now... 65 | crop_edges([207 20 13 1]); %full res location of crop (so that it works for multiple rlooks, left right, top, bottom. 66 | smart_rlooks %buffers could make this faster. Lots of I/O 67 | unwrap_rlooks %does snaphu with tiles. Make wierd output files on jackalope that need to be deleted. 68 | 69 | 70 | %iterate over this chunk 71 | invert_dates(0) %this generates TS/looks#/date.r4 files and res#. Res# will have a ramp if not alligned. Choose thresh accordingly 72 | thresh = 4; %this currently needs to be "big" for the first round, then iterate once or twice with smaller values. 73 | edge = [1843*4 4726 898*4 1517]; %pixels from left and right, top and bottom edge to mask 74 | waterheight = []; %mask all pixels with height < waterheight. 75 | topoflag = []; 76 | boxedge = [2437 2636 1357 1582]*4; 77 | fitramp_int(thresh,edge,waterheight,topoflag,boxedge,0) 78 | unwrap_flat %this can or can not be used - will reunwrap the now-flatter interferograms. Then iterate as above. 79 | 80 | %invert dates once with final set of flattened interferograms 81 | invert_dates 82 | 83 | invert_rates 84 | 85 | calc_rate_residual %adds "rms" field to ints structure. can be used to decide whether to get rid of some ints. Then rerun choose_ints, and rerun from invert_dates. 86 | -------------------------------------------------------------------------------- /setup_init.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | ndates=length(dates); 4 | 5 | roifile = [dates(1).dir dates(1).name '_roi.in']; 6 | if(exist(roifile,'file')) 7 | delete(roifile); 8 | end 9 | 10 | for i=[1:id-1 id+1:ndates] %make all relative to date "id" 11 | %check if raw exists 12 | infile=[dates(i).name '/' dates(i).name '.raw']; 13 | if(~exist(infile,'file')) 14 | disp([infile ' not found']); 15 | else 16 | roifile = [dates(i).dir dates(i).name '_roi.in']; 17 | if(exist(roifile,'file')) 18 | delete(roifile); 19 | end 20 | fid=fopen('tmp.proc','w'); 21 | 22 | fprintf(fid,['SarDir1=' masterdir dates(id).name '\n']); 23 | fprintf(fid,['SarDir2=' masterdir dates(i).name '\n']); 24 | fprintf(fid,['IntDir=intbase\n']); 25 | fprintf(fid,['OrbitType=HDR\n']); 26 | fprintf(fid,['pixel_ratio=1\n']); 27 | fprintf(fid,'\n'); 28 | 29 | fclose(fid); 30 | command=('process_2pass.pl tmp.proc raw orbbase'); 31 | [status(i), result]=mysys(command); 32 | end 33 | end 34 | delete('tmp.proc'); 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /smart_rlooks.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | 4 | ndates = length(dates); 5 | nints = length(ints); 6 | 7 | if strcmp(sat,'S1A') 8 | nx=ints(id).width; 9 | ny=ints(id).length; 10 | else 11 | [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 12 | end 13 | 14 | fid1=fopen(gammafile,'r'); 15 | im=sqrt(-1); 16 | thresh=2; 17 | 18 | newnx=floor(nx./rlooks) 19 | newny=floor(ny./alooks); 20 | 21 | for l=1:length(rlooks) 22 | for k=2:nints 23 | if(~exist(ints(k).flatrlk{1},'file')); 24 | fid2 = fopen(ints(k).flat,'r'); 25 | fid3 = fopen(ints(k).flatrlk{1},'w'); 26 | fid4 = fopen([ints(k).flatrlk{1} '.cor'],'w'); 27 | fid5 = fopen(maskfilerlk{l},'r'); 28 | for j=1:newny(l) 29 | gamma = NaN(alooks(l),nx); 30 | int = zeros(alooks(l),nx); 31 | mask = zeros(1,newnx(l)); 32 | 33 | for i=1:alooks(l) 34 | tmp=fread(fid1,nx,'real*4'); 35 | gamma(i,1:length(tmp))=tmp; 36 | 37 | tmp=fread(fid2,nx,'real*4'); 38 | int(i,1:length(tmp))=exp(im*tmp); 39 | end 40 | 41 | 42 | gamma(isnan(gamma))=1000; 43 | 44 | %read old mask file (downlooked) 45 | tmp = fread(fid5,newnx(l),'real*4'); 46 | mask(1:length(tmp))=tmp; 47 | 48 | cor = zeros(1,newnx(l)); 49 | for i=1:newnx(l) 50 | ids = (i-1)*rlooks(l)+[1:rlooks(l)]; 51 | tempi = int(:,ids); 52 | phs = angle(tempi); 53 | 54 | tempm = gamma(:,ids); 55 | tempm = 1./tempm; 56 | tempm(isnan(tempm))=0; 57 | 58 | 59 | goodid = find((phs~=0) & (isfinite(phs))); 60 | if(length(goodid)>1) 61 | cor(i) = abs(sum(tempi(goodid)))/length(goodid); 62 | meani = mean(tempi(goodid).*tempm(goodid)); 63 | fwrite(fid3,[real(meani) imag(meani)],'real*4'); 64 | fwrite(fid4,cor(i),'real*4'); 65 | else 66 | cor(i) = 0; 67 | fwrite(fid3,[0 0],'real*4'); 68 | fwrite(fid4,[0],'real*4'); 69 | end 70 | end 71 | cor(cor<.75)=0; 72 | fwrite(fid4,max(cor,mask),'real*4'); 73 | end 74 | fclose(fid2); 75 | fclose(fid3); 76 | fclose(fid4); 77 | fclose(fid5); 78 | frewind(fid1); 79 | else 80 | disp(['skipping ' ints(k).name]); 81 | end 82 | end 83 | end 84 | fclose(fid1); 85 | 86 | -------------------------------------------------------------------------------- /smart_rlooks_2.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile) 3 | % getstuff 4 | win=ones(alooks,rlooks); 5 | win=win/sum(win(:)); 6 | rangevec=[0:newnx-1]*rlooks+1; 7 | 8 | 9 | for l=1:length(rlooks) 10 | parfor k=1:nints 11 | im=sqrt(-1); 12 | fid1 = fopen(gammafile,'r'); 13 | fid2 = fopen(ints(k).flat,'r'); 14 | fid3 = fopen(ints(k).flatrlk,'w'); 15 | fid4 = fopen([ints(k).flatrlk '.cor'],'w'); 16 | 17 | for j=1:newny(l) 18 | mask = zeros(alooks(l),nx); 19 | int = zeros(alooks(l),nx); 20 | 21 | for i=1:alooks(l) 22 | gam = fread(fid1,nx,'real*4'); 23 | if(length(gam)==nx) 24 | mask(i,:)=gam(1:nx); 25 | else 26 | mask(i,:)=0; 27 | end 28 | mask(i,isnan(mask(i,:)))=0; 29 | tmp=fread(fid2,nx,'real*4'); 30 | if(length(tmp)==nx) 31 | int(i,:)=exp(im*tmp); 32 | else 33 | int(i,:)=zeros(1,nx); 34 | end 35 | end 36 | 37 | rea=real(int); 38 | ima=imag(int); 39 | 40 | rea_filt0 = conv2(rea,win,'valid'); 41 | ima_filt0 = conv2(ima,win,'valid'); 42 | 43 | rea=rea.*mask; 44 | ima=ima.*mask; 45 | 46 | rea_filt = conv2(rea,win,'valid'); 47 | ima_filt = conv2(ima,win,'valid'); 48 | mag_filt = conv2(mask,win,'valid'); 49 | 50 | phs = atan2(ima_filt(rangevec),rea_filt(rangevec)); 51 | phscor = sqrt(ima_filt0(rangevec).^2+rea_filt0(rangevec).^2); %abs value of average phase vector. 52 | %phssig=sqrt(-2*log(phscor)); 53 | 54 | fwrite(fid3,phs,'real*4'); 55 | fwrite(fid4,phscor,'real*4'); 56 | end 57 | fclose('all'); 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /smart_rlooks_par.m: -------------------------------------------------------------------------------- 1 | function smart_rlooks_par(k,newnx,newny,win,rangevec,intcorthresh,l,fid1) 2 | getstuff 3 | %if(~exist(ints(k).flatrlk{l},'file')); 4 | if(1) 5 | fid2 = fopen(ints(k).flat,'r'); 6 | fid3 = fopen('phs','w'); 7 | fid4 = fopen('cor','w'); 8 | fid5 = fopen([ints(k).flat '_diff'],'r'); 9 | for j=1:newny(l) 10 | mask = zeros(alooks(l),nx); 11 | int = zeros(alooks(l),nx); 12 | intcor=zeros(alooks(l),nx); 13 | for i=1:alooks(l) 14 | tmp_gamma=fread(fid1,nx,'real*4'); 15 | if(length(tmp_gamma)==nx) 16 | mask(i,:)=tmp_gamma(1:nx); 17 | else 18 | mask(i,:)=0; 19 | end 20 | mask(i,isnan(mask(i,:)))=0; 21 | tmp_int=fread(fid2,nx,'real*4'); 22 | if(length(tmp_int)==nx) 23 | int(i,:)=exp(im*tmp_int); 24 | else 25 | int(i,:)=zeros(1,nx); 26 | end 27 | tmp_int_diff=fread(fid5,nx,'real*4'); 28 | if(length(tmp_int_diff)==nx) 29 | intcor(i,:)=cos(tmp_int_diff); 30 | else 31 | intcor(i,:)=0; 32 | end 33 | end 34 | goodcorid=and(intcor>mask,intcor>intcorthresh); 35 | mask(goodcorid)=intcor(goodcorid); %this will add any good values specific to the individual int to the mask(gamma) file 36 | 37 | rea=real(int); 38 | ima=imag(int); 39 | 40 | rea_filt0 = conv2(rea,win,'valid'); 41 | ima_filt0 = conv2(ima,win,'valid'); 42 | 43 | rea=rea.*mask; %multiplies by the value from 0 to 1 in the mask 44 | ima=ima.*mask; 45 | 46 | rea_filt = conv2(rea,win,'valid'); 47 | ima_filt = conv2(ima,win,'valid'); 48 | %mag_filt = conv2(mask,win,'valid'); 49 | 50 | phs = atan2(ima_filt(rangevec),rea_filt(rangevec)); 51 | phscor = sqrt(ima_filt0(rangevec).^2+rea_filt0(rangevec).^2); %abs value of average phase vector. 52 | %phssig=sqrt(-2*log(phscor)); 53 | 54 | fwrite(fid3,phs,'real*4'); 55 | fwrite(fid4,phscor,'real*4'); 56 | end 57 | 58 | fclose(fid2); 59 | fclose(fid3); 60 | system(['mag_phs2cpx ' maskfilerlk{l} ' phs ' ints(k).flatrlk{l} ' ' num2str(newnx(l))]); 61 | system(['mag_phs2rmg cor cor ' ints(k).flatrlk{l} '_cor ' num2str(newnx(l))]); 62 | end -------------------------------------------------------------------------------- /smart_rlooks_runpar.m: -------------------------------------------------------------------------------- 1 | clear all;close all 2 | getstuff 3 | 4 | fid1=fopen(gammafile,'r'); 5 | im=sqrt(-1); 6 | thresh=2; 7 | 8 | newnx=floor(nx./rlooks) 9 | newny=floor(ny./alooks); 10 | 11 | 12 | win=ones(alooks,rlooks); 13 | win=win/sum(win(:)); 14 | rangevec=[0:newnx-1]*rlooks+1; 15 | intcorthresh=0.7; 16 | l=1; 17 | 18 | tic 19 | parfor k=1:nints 20 | smart_rlooks_par(k,newnx,newny,win,rangevec,intcorthresh,l,fid1) 21 | end 22 | toc 23 | 24 | frewind(fid1); 25 | fclose(fid1); 26 | -------------------------------------------------------------------------------- /undo_fitramp.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | 4 | nints=length(ints); 5 | 6 | for l=1:length(rlooks) 7 | for i=1:nints 8 | 9 | movefile([ints(i).unwrlk{l} '_old'],ints(i).unwrlk{l}); 10 | 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /unw2png_km.m: -------------------------------------------------------------------------------- 1 | function unw2png_km(infile,outfile,mode,wraprate,scale) 2 | 3 | set_params 4 | load(ts_paramfile); 5 | [nx,ny,lambda] = load_rscs([infile '.rsc'],'WIDTH','FILE_LENGTH','WAVELENGTH'); 6 | 7 | command = ['rmg2mag_phs ' infile ' mag phs ' num2str(nx)]; 8 | mysys(command) 9 | 10 | if mode==1 11 | command=['mdx mag -r4 -ponly ' num2str(nx)]; 12 | mysys(command); 13 | else 14 | command=['mdx phs -r4 -cmap cmy -wrap ' num2str(wraprate) ' -ponly ' num2str(nx)]; 15 | mysys(command); 16 | end 17 | 18 | !rm mag phs 19 | 20 | % read first pixel 21 | % command = ['convert out.ppm -crop 1x1+0+0 txt:->tmp']; 22 | % mysys(command); 23 | % 24 | % color = 'cyan'; 25 | % !rm tmp 26 | 27 | command = ['convert out.ppm -resize ' num2str(scale) '%' ' -transparent cyan ' outfile]; 28 | system(command); 29 | 30 | command = ['importIMGtoKML.pl ' outfile ' ' infile ' ' outfile '.png.kml']; 31 | mysys(command); 32 | 33 | 34 | % outfile='ENVI_T120_675'; 35 | % %make a grd file for GMT 36 | % command = ['unw2grd.pl geo_rates_2.unw ' outfile '.grd 1']; 37 | % mysys(command); 38 | 39 | end 40 | -------------------------------------------------------------------------------- /unw2png_km1.m: -------------------------------------------------------------------------------- 1 | function unw2png_km1(wraprate,scale) 2 | getstuff 3 | chdir(['TS/looks' num2str(rlooks) '/']) 4 | !mkdir png_files 5 | for k=1:nints 6 | command=['mdx ' ints(k).unwrlk{1} ' -r4 -cmap cmy -wrap ' num2str(wraprate) ' -ponly ' num2str(newnx)]; 7 | mysys(command); 8 | 9 | command = ['convert out.ppm -resize ' num2str(scale) '%' ' -transparent cyan ' ints(k).name '.png']; 10 | system(command); 11 | !mv *.png png_files/ 12 | end 13 | 14 | chdir(masterdir) 15 | 16 | -------------------------------------------------------------------------------- /unw2png_km_s1a.m: -------------------------------------------------------------------------------- 1 | function unw2png_km_s1a(infile,outfile,mode,wraprate,scale) 2 | % infile = 'rates_1'; 3 | % outfile = ['s1a_' num2str(track) '_' num2str(frame) '.png']; 4 | % mode = 2; 5 | % wraprate = 30; 6 | % scale= 50; 7 | l=1; 8 | set_params 9 | load(ts_paramfile); 10 | % [nx,ny,lambda] = load_rscs([infile '.rsc'],'WIDTH','FILE_LENGTH','WAVELENGTH'); 11 | 12 | 13 | fid=fopen(infile,'r') 14 | tmp=fread(fid,[newnx newny*2],'real*4'); 15 | phs = tmp'; 16 | fclose(fid); 17 | 18 | 19 | fid=fopen('phs','w') 20 | for i=1:newny(l) 21 | fwrite(fid,phs(i,:),'real*4'); 22 | end 23 | fclose(fid); 24 | 25 | 26 | if mode==1 27 | command=['mdx mag -r4 -ponly ' num2str(newnx)]; 28 | mysys(command); 29 | else 30 | command=['mdx phs -r4 -cmap cmy -wrap ' num2str(wraprate) ' -ponly ' num2str(newnx)]; 31 | mysys(command); 32 | end 33 | 34 | 35 | 36 | % read first pixel 37 | % command = ['convert out.ppm -crop 1x1+0+0 txt:->tmp']; 38 | % mysys(command); 39 | % 40 | % color = 'cyan'; 41 | % !rm tmp 42 | 43 | command = ['convert out.ppm -resize ' num2str(scale) '%' ' -transparent cyan ' outfile]; 44 | system(command); 45 | 46 | command = ['importIMGtoKML.pl ' outfile ' ' infile ' ' outfile '.kml']; 47 | mysys(command); 48 | 49 | % end 50 | -------------------------------------------------------------------------------- /unwrap_filtered_rlooks.m: -------------------------------------------------------------------------------- 1 | set_params 2 | load(ts_paramfile); 3 | 4 | ndates = length(dates); 5 | nints = length(ints); 6 | 7 | if strcmp(sat,'S1A') 8 | nx=ints(id).width; 9 | ny=ints(id).length; 10 | else 11 | [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 12 | 13 | end 14 | newnx = floor(nx./rlooks) 15 | newny = floor(ny./alooks); 16 | 17 | % %mask using mask.cor 18 | % fid=fopen([rlkdir{1} 'flat_' ints(id).name '_8rlks.rect.cor' ],'r'); 19 | % msk=fread(fid,[newnx,newny*2],'real*4'); 20 | % 21 | % msk=msk'; 22 | % msk=msk(1:2:newny,:); 23 | % fclose(fid) 24 | % 25 | % for l=1:length(rlooks) 26 | % for i=1:1 27 | % fid=fopen([ints(i).flatrlk{l}],'r'); 28 | % tmp = fread(fid,[newnx,newny],'real*4'); 29 | % tmp(find(msk<.5))=0; 30 | % fidout=fopen([ints(i).flatrlk{l} '_new'],'w','native'); 31 | % fwrite(fidout,tmp,'real*4') 32 | % end 33 | % end 34 | 35 | 36 | 37 | for l=1:1%length(rlooks) 38 | for k=1:nints 39 | % if(~exist(ints(k).unwrlk{1},'file')) 40 | command=['snaphu -s ' ints(k).filtrlk ' ' num2str(newnx(l)) ' -o ' ints(k).unwrlk{1} ' -c ' rlkdir{l} 'mask.cor --tile 6 6 100 100']; 41 | mysys(command); 42 | % end 43 | end 44 | end 45 | 46 | -------------------------------------------------------------------------------- /unwrap_flat.m: -------------------------------------------------------------------------------- 1 | getstuff 2 | l=1;k=1; 3 | 4 | 5 | if(exist([ints(k).unwrlk{l} '_flat'],'file')) 6 | reply = input('*.unw_flat already exist. Want to use them to make new .unw files? ', 's') 7 | if(isempty(reply)) 8 | reply='Y'; 9 | end 10 | switch reply 11 | case {'Y','yes','Yes','y','YES','yep'} 12 | disp('Overwriting *unw files, using *unw_flat. Using tiles') 13 | flag=1; 14 | case {'No','no','N','n','NO','nope'} 15 | disp('Doing nothing..') 16 | flag=0; 17 | return 18 | end 19 | else 20 | disp('*.unw_flat do not exist yet. Tagging .unw with _flat, and reunwrapping the _flat files.') 21 | flag=1; 22 | end 23 | 24 | 25 | if(flag==1) 26 | for l=1:length(rlooks) 27 | for k=1%:nints 28 | if(~exist([ints(k).unwrlk{l} '_flat'],'file')) 29 | movefile([ints(k).unwrlk{l}],[ints(k).unwrlk{l} '_flat']); %save original version 30 | end 31 | command=['snaphu -s ' ints(k).unwrlk{l} '_flat -u ' num2str(newnx(l)) ' -o ' ints(k).unwrlk{l} ' -c ' rlkdir{l} 'mask.cor --mcf --tile 7 7 150 150']; 32 | mysys(command); 33 | 34 | end 35 | end 36 | end 37 | 38 | 39 | % l=1 40 | % for k=1:nints 41 | % if(exist([ints(k).unwrlk{l} '_flat'],'file')) 42 | % 43 | % movefile([ints(k).unwrlk{l} '_flat'],[ints(k).unwrlk{l}]); %save original version 44 | % end 45 | % end 46 | 47 | 48 | 49 | 50 | 51 | %command=['snaphu -s ' ints(k).unwrlk{l} '_flat -u ' num2str(newnx(l)) ' -o ' ints(k).unwrlk{l} ' -c ' rlkdir{l} 'mask.cor --tile 7 7 150 150']; 52 | % -------------------------------------------------------------------------------- /unwrap_rlooks.m: -------------------------------------------------------------------------------- 1 | % Unwrap_rlooks 2 | %<^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^ 3 | % Kyle Murray; July 2017 4 | % Takes *bell ints and outputs r4 .unw files. Uses parallel 5 | % processing and tiles 6 | %<^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^><^ 7 | 8 | clear all;close all 9 | set_params 10 | 11 | % Specify snaphu options 12 | ntilerow=30; 13 | ntilecol=30; 14 | nproc=38; 15 | 16 | 17 | % Write snaphu configuration files 18 | write_snaphu_conf(ntilerow,ntilecol,nproc); % Uses parallel processing and tiles 19 | 20 | % Saves originals 21 | for k=1:nints 22 | if(~exist([ints(k).unwrlk '_orig'],'file')) 23 | if(exist([ints(k).unwrlk],'file')) 24 | copyfile([ints(k).unwrlk],[ints(k).unwrlk '_orig']) 25 | disp(['moving ' ints(k).unwrlk ' to ' ints(k).unwrlk '_orig']); 26 | end 27 | end 28 | end 29 | 30 | % Run snaphu 31 | for k=1:nints 32 | if(~exist([ints(k).unwrlk],'file')) 33 | disp(['unwrapping ' ints(k).flatrlk]); 34 | 35 | system(['snaphu -f ' ints(k).unwrlk '_snaphu.conf >> tmp_log']); 36 | else 37 | disp('unw files already exist. Unwrapping ints and writing over it. Modify write_snaphu_conf() to reunwrap .unw files.') 38 | disp(['unwrapping ' ints(k).flatrlk]); 39 | system(['snaphu -f ' ints(k).unwrlk '_snaphu.conf']); 40 | 41 | end 42 | end 43 | 44 | 45 | 46 | 47 | 48 | % Uncomment and run to copy originals back to the .unw name. 49 | % for k=1:nints 50 | % if(exist([ints(k).unwrlk '_orig'],'file')) 51 | % if(exist([ints(k).unwrlk],'file')) 52 | % copyfile([ints(k).unwrlk '_orig'],[ints(k).unwrlk]) 53 | % disp(['moving ' ints(k).unwrlk '_orig to ' ints(k).unwrlk]); 54 | % end 55 | % else 56 | % disp([ints(k).unwrlk '_orig does not exist']) 57 | % end 58 | % end 59 | -------------------------------------------------------------------------------- /unwrap_watermask.m: -------------------------------------------------------------------------------- 1 | function unwrap_watermask(waterheight) 2 | waterheight=-5; 3 | if(isempty(waterheight)) 4 | waterheight=-3000; 5 | end 6 | 7 | set_params 8 | load(ts_paramfile); 9 | 10 | ndates = length(dates); 11 | nints = length(ints); 12 | 13 | if strcmp(sat,'S1A') 14 | nx=ints(id).width; 15 | ny=ints(id).length; 16 | else 17 | [nx,ny] = load_rscs(dates(id).slc,'WIDTH','FILE_LENGTH'); 18 | 19 | end 20 | 21 | newnx = floor(nx./rlooks) 22 | newny = floor(ny./alooks); 23 | 24 | 25 | %__________________________________________________________________________ 26 | %mask out ocean 27 | if ~strcmp(sat,'S1A') 28 | oldintdir = [masterdir 'int_' dates(ints(intid).i1).name '_' dates(ints(intid).i2).name '/']; 29 | lookheightfile=[oldintdir 'radar_' num2str(rlooks(1)) 'rlks.hgt']; 30 | 31 | if(~exist(lookheightfile)) 32 | %---------------------------------------------------------- 33 | %KM Edit: There is no rsc file for the look.pl command (looking for 34 | %radar.hgt.rsc) 35 | command1=['cp ' oldintdir 'reference.hgt.rsc ' oldintdir 'radar.hgt.rsc']; 36 | mysys(command1); 37 | %---------------------------------------------------------- 38 | command=['look.pl ' fullresheightfile ' ' num2str(rlooks(l)) ' ' num2str(rlooks(l)*pixel_ratio)]; 39 | mysys(command); 40 | end 41 | 42 | fiddem = fopen(lookheightfile,'r'); 43 | tmp = fread(fiddem,[newnx,newny*2],'real*4'); 44 | dem = tmp(:,2:2:end); 45 | fclose(fiddem); 46 | watermask = dem; 47 | watermask(watermask0) 11 | tmp=b{i}; 12 | tmp=tmp(1:bid{i}-1); 13 | b{i}=tmp; 14 | end 15 | end 16 | c=content{3};%comments/units 17 | 18 | for j=1:length(input) 19 | string=input(j).name; 20 | string=upper(string); 21 | m=regexp(a,string); 22 | id=find(cellfun(@length,m)); 23 | if(length(id)==1) 24 | output(j).name=input(j).name; 25 | output(j).val=b{id}; 26 | else 27 | disp('Variable not found or found more than once'); 28 | end 29 | end 30 | fclose(fid); 31 | case 'write' 32 | for j=1:length(input) 33 | fid1=fopen(filename,'r'); 34 | fid2=fopen('tmp','w'); 35 | found=0; 36 | while 1 37 | tline = fgetl(fid1); 38 | if ~ischar(tline) 39 | break 40 | else 41 | if(regexp(tline,input(j).name)) 42 | found=1; 43 | fprintf(fid2,'%s (-) = %s !\n',input(j).name,input(j).val); 44 | else 45 | fprintf(fid2,'%s\n',tline); 46 | 47 | end 48 | end 49 | end 50 | if(found) 51 | else 52 | fprintf(fid2,'%s (-) = %s !\n',input(j).name,input(j).val); 53 | end 54 | fclose(fid1); 55 | fclose(fid2); 56 | movefile('tmp',filename); 57 | end 58 | 59 | end 60 | 61 | -------------------------------------------------------------------------------- /write_paramfile.m: -------------------------------------------------------------------------------- 1 | function write_paramfile(sat,masterdir,id,footprints,plotflag,pixel_ratio,rlk,track,frame) 2 | 3 | file=[masterdir 'set_params.m']; 4 | 5 | % disp('') 6 | % reply = input('overwrite existing set_params file? Y/N [Y]: ', 's'); 7 | % if(isempty(reply)) 8 | % reply='Y'; 9 | % end 10 | % switch reply 11 | % case {'Y','Yes','y','YES'} 12 | disp('overwriting') 13 | fid=fopen(file,'w'); 14 | 15 | fprintf(fid,'setenv(''GFORTRAN_STDIN_UNIT'',''5'')\n'); 16 | fprintf(fid,'setenv(''GFORTRAN_STDOUT_UNIT'',''6'')\n'); 17 | fprintf(fid,'setenv(''GFORTRAN_STDERR_UNIT'',''0'')\n\n'); 18 | 19 | baddatedir = [masterdir 'baddates/']; 20 | TSdir = [masterdir 'TS/']; 21 | rectdir = [TSdir 'rect/']; 22 | DEMdir = [masterdir 'DEM/']; 23 | intdir = [TSdir 'int/']; 24 | ts_paramfile = [TSdir 'ts_params.mat']; 25 | 26 | fprintf(fid,['sat = ''' sat ''';\n']); 27 | fprintf(fid,['track = ''' num2str(track) ''';\n']); 28 | fprintf(fid,['frame = ''' num2str(frame) ''';\n']); 29 | 30 | fprintf(fid,['masterdir = ''' masterdir ''';\n']); 31 | fprintf(fid,['baddatedir = ''' baddatedir ''';\n']); 32 | fprintf(fid,['TSdir = ''' TSdir ''';\n']); 33 | fprintf(fid,['rectdir = ''' rectdir ''';\n']); 34 | fprintf(fid,['DEMdir = ''' DEMdir ''';\n']); 35 | fprintf(fid,['intdir = ''' intdir ''';\n']); 36 | fprintf(fid,['ts_paramfile = ''' ts_paramfile ''';\n\n']); 37 | 38 | if nonzeros(footprints.lat) 39 | if(isstruct(footprints)) 40 | for j=1:length(footprints) 41 | fprintf(fid,['frames(' num2str(j) ').lat=[']); 42 | for i=1:4 43 | fprintf(fid,[num2str(footprints(j).lat(i)) ' ']); 44 | end 45 | fprintf(fid,'];\n'); 46 | fprintf(fid,['frames(' num2str(j) ').lon=[']); 47 | for i=1:4 48 | fprintf(fid,[num2str(footprints(j).lon(i)) ' ']); 49 | end 50 | fprintf(fid,'];\n'); 51 | end 52 | else 53 | fprintf(fid,'frames(1).lat=[0 0 0 0];\n'); 54 | fprintf(fid,'frames(1).lon=[0 0 0 0];\n'); 55 | end 56 | end 57 | fprintf(fid,'\n'); 58 | fprintf(fid,['id=' num2str(id) '; %%Master date id\n']); 59 | fprintf(fid,'intid=1; %%Master int id\n'); 60 | fprintf(fid,'noff=100; %%number of offsets in ampcor in x and y\n'); 61 | fprintf(fid,'offwin=64; %%ampcor offset window size\n'); 62 | fprintf(fid,'searchwin = 64; %%ampcor search window size\n\n'); 63 | 64 | fprintf(fid,'dopcutoff=400;\n'); 65 | fprintf(fid,'azcutoff=8000;\n\n'); 66 | 67 | switch sat 68 | case ALOS 69 | fprintf(fid,'rlooks = [4]; %%can be larger vector\n'); 70 | fprintf(fid,['pixel_ratio = 2 ;\n']); 71 | case ENVI 72 | fprintf(fid,'rlooks = [2]; %%can be larger vector\n'); 73 | fprintf(fid,['pixel_ratio = 5 ;\n']); 74 | fprintf(fid,'alooks = rlooks*pixel_ratio;\n'); 75 | end 76 | 77 | 78 | fprintf(fid,['plotflag=' num2str(plotflag) ';%%0 suppresses plots\n\n']); 79 | 80 | fprintf(fid,'for i=1:length(rlooks)\n'); 81 | fprintf(fid,'rlkdir{i} = [TSdir ''looks'' num2str(rlooks(i)) ''/''];\n'); 82 | fprintf(fid,'maskfilerlk{i}= [rlkdir{i} ''mask_'' num2str(rlooks(i)) ''rlks.r4''];\n'); 83 | fprintf(fid,'end\n\n'); 84 | 85 | fprintf(fid,'avgslcfile = [TSdir ''avgslc.r4''];\n'); 86 | fprintf(fid,'gammafile = [TSdir ''gamma0.r4''];\n'); 87 | fprintf(fid,'maskfile = [TSdir ''mask.i1''];\n\n'); 88 | 89 | fprintf(fid,'badbase={};\n'); 90 | fprintf(fid,'addpairs=[]; %%rows of i1,i2\n'); 91 | fprintf(fid,'removepairs=[]; %%rows of i1,i2\n\n'); 92 | 93 | 94 | 95 | 96 | fprintf(fid,'frame_gmt=[[frames.lon]'' [frames.lat]''; [frames.lon(1)] [frames.lat(1)] ];\n'); 97 | % fprintf(fid,'dlmwrite([sat ''_T'' num2str(track) ''_'' num2str(frame) ''.gmtframe''],frame_gmt,''delimiter'','' '',''precision'',''%.6f]'')\n'); 98 | 99 | fclose(fid); 100 | % case {'No','No','n','NO'} 101 | % disp('not overwriting') 102 | % return 103 | end 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /write_paramfile_s1a.m: -------------------------------------------------------------------------------- 1 | function write_paramfile_s1a(sat,masterdir,id,intid,pixel_ratio,rlooks) 2 | 3 | file=[masterdir 'set_params.m']; 4 | 5 | disp('') 6 | reply = input('overwrite existing set_params file? Y/N [Y]: ', 's'); 7 | if(isempty(reply)) 8 | reply='Y'; 9 | end 10 | switch reply 11 | case {'Y','Yes','y','YES'} 12 | disp('overwriting') 13 | fid=fopen(file,'w'); 14 | 15 | fprintf(fid,'setenv(''GFORTRAN_STDIN_UNIT'',''5'')\n'); 16 | fprintf(fid,'setenv(''GFORTRAN_STDOUT_UNIT'',''6'')\n'); 17 | fprintf(fid,'setenv(''GFORTRAN_STDERR_UNIT'',''0'')\n\n'); 18 | fprintf(fid,['masterdir = ''' masterdir ''';\n']); 19 | 20 | baddatedir = [masterdir 'baddates/']; 21 | TSdir = [masterdir 'TS/']; 22 | rectdir = [TSdir 'rect/']; 23 | DEMdir = [masterdir 'dem/']; 24 | intdir = [TSdir 'int/']; 25 | ts_paramfile = [TSdir 'ts_params.mat']; 26 | 27 | fprintf(fid,['sat = ''' sat ''';\n']); 28 | fprintf(fid,['baddatedir = ''' baddatedir ''';\n']); 29 | fprintf(fid,['TSdir = ''' TSdir ''';\n']); 30 | fprintf(fid,['rectdir = ''' rectdir ''';\n']); 31 | fprintf(fid,['DEMdir = ''' DEMdir ''';\n']); 32 | fprintf(fid,['intdir = ''' intdir ''';\n']); 33 | 34 | ts_paramfile = [masterdir 'ts_params.mat']; 35 | fprintf(fid,['ts_paramfile = ''' ts_paramfile ''';\n\n']); 36 | 37 | fprintf(fid,'\n'); 38 | fprintf(fid,['id=' num2str(id) '; %%Master date id\n']); 39 | fprintf(fid,['intid=' num2str(intid) '; %%Master date id\n']); 40 | fprintf(fid,'noff=150; %%number of offsets in ampcor in x and y\n'); 41 | fprintf(fid,'offwin=50; %%ampcor offset window size\n'); 42 | fprintf(fid,'searchwin = 10; %%ampcor search window size\n\n'); 43 | 44 | % CAUTION - Requesting very large search window pull in 45 | % Reference Window Size is 32 line pixels 46 | % Number of Search Pixels is 16 line pixels 47 | % The rule of thumb is that the search window pull in is at least 5 48 | % pixels and is less than the reference window size divided by 5. 49 | % Suggested Number of Search Pixels is 5 line pixels 50 | 51 | fprintf(fid,['lambda = 0.05546576; %%0.05546576 for S1A \n']); 52 | 53 | fprintf(fid,['rlooks = ' num2str(rlooks) '; %%can be larger vector\n']); 54 | fprintf(fid,['pixel_ratio = ' num2str(pixel_ratio) ';\n']); 55 | fprintf(fid,'alooks = rlooks*pixel_ratio;\n'); 56 | 57 | fprintf(fid,['plotflag=1; %%0 suppresses plots\n\n']); 58 | 59 | fprintf(fid,['if exist(ts_paramfile,''file'')\n']); 60 | fprintf(fid,[' load(ts_paramfile);\n']); 61 | fprintf(fid,[' ndates = length(dates);\n']); 62 | fprintf(fid,[' nints = length(ints);\n']); 63 | fprintf(fid,['end\n']); 64 | 65 | fprintf(fid,'for i=1:length(rlooks)\n'); 66 | fprintf(fid,'rlkdir = [TSdir ''looks'' num2str(rlooks) ''/''];\n'); 67 | fprintf(fid,'maskfilerlk= [rlkdir ''mask_'' num2str(rlooks) ''rlks.r4''];\n'); 68 | fprintf(fid,'end\n\n'); 69 | 70 | fprintf(fid,'avgslcfile = [TSdir ''avgslc.r4''];\n'); 71 | fprintf(fid,'gammafile = [TSdir ''gamma0.r4''];\n'); 72 | fprintf(fid,'maskfile = [TSdir ''mask.i1''];\n\n'); 73 | 74 | fclose(fid); 75 | case {'No','No','n','NO'} 76 | disp('not overwriting') 77 | return 78 | end -------------------------------------------------------------------------------- /write_slc_proc.m: -------------------------------------------------------------------------------- 1 | shifts=0; 2 | patches=0; % if set to 1, only does 1 patch (for testing) 3 | set_params 4 | load(ts_paramfile) 5 | ndates=length(dates); 6 | 7 | 8 | for i=1:ndates 9 | dateprocfile = [dates(i).dir dates(i).name '.proc']; %used by roi_prep to crop 10 | azoff=round(dates(i).azoff); 11 | rgoff=round(dates(i).rgoff); 12 | 13 | if(~exist(dateprocfile,'file')) 14 | %first round 15 | dates(i).rgoff_orig = azoff; 16 | dates(i).azoff_orig = rgoff; 17 | %orig values 18 | [wavel,startrange,prf,samp_frq,pulse,AntennaLen,velocity,xmin,xmax]=load_rscs(dates(i).raw,'WAVELENGTH','STARTING_RANGE','PRF','RANGE_SAMPLING_FREQUENCY','PULSE_LENGTH','ANTENNA_LENGTH','VELOCITY','XMIN','XMAX'); 19 | speed_of_light = 299792458; 20 | 21 | rg_frac=0.4; 22 | az_frac=0.8; 23 | chirp_samps=ceil(samp_frq*pulse); 24 | near_rng_ext=ceil(chirp_samps*rg_frac); 25 | far_rng_ext=near_rng_ext; 26 | 27 | rng_pixel_size=speed_of_light/samp_frq/2; 28 | out_pixel=ceil(xmax/2-xmin/2-chirp_samps+near_rng_ext+far_rng_ext); 29 | slc_far_range=startrange+out_pixel*rng_pixel_size; 30 | synth_apert_samps = ceil(wavel*slc_far_range*prf/AntennaLen/velocity); 31 | before_z_ext=ceil(az_frac*synth_apert_samps); 32 | after_z_ext=before_z_ext; 33 | 34 | dates(i).before_z=before_z_ext; 35 | dates(i).after_z=after_z_ext; 36 | dates(i).near_r=near_rng_ext; 37 | dates(i).far_r=far_rng_ext; 38 | 39 | 40 | fid=fopen([dates(i).dir dates(i).name '.proc'],'w'); 41 | 42 | fprintf(fid,'before_z_ext = %d\n',before_z_ext-azoff+shifts); 43 | fprintf(fid,'after_z_ext = %d\n',after_z_ext+azoff+shifts); 44 | fprintf(fid,'near_rng_ext = %d\n',near_rng_ext-rgoff+shifts); 45 | fprintf(fid,'far_rng_ext = %d\n',far_rng_ext+rgoff+shifts); 46 | if(patches) 47 | fprintf(fid,'number_of_patches = 1\n'); 48 | end 49 | 50 | fclose(fid); 51 | 52 | else 53 | disp([dateprocfile ' already made, not changing. Shifts = ' num2str(rgoff) ' rg ' num2str(azoff) ' az']); 54 | end 55 | end 56 | 57 | save(ts_paramfile,'dates'); 58 | -------------------------------------------------------------------------------- /write_snaphu_conf.m: -------------------------------------------------------------------------------- 1 | function write_snaphu_conf(ntilerow,ntilecol,nproc) 2 | % ntilerow: how many tiles rows 3 | % ntilecol: how many tiles cols 4 | % nproc: how many processors to split the tile processes on (Mongoose has 5 | % 40 cores, and packrat has 32 6 | set_params 7 | 8 | for ii=1:nints 9 | 10 | conf =[char(ints(ii).unwrlk) '_snaphu.conf']; 11 | fid=fopen(conf,'w'); 12 | 13 | fprintf(fid,['# Input \n']); 14 | fprintf(fid,['INFILE ' [ints(ii).flatrlk '_bell'] '\n']); 15 | % fprintf(fid,['UNWRAPPED_IN TRUE \n']); 16 | fprintf(fid,['# Input file line length \n']); 17 | fprintf(fid,['LINELENGTH ' num2str(newnx) '\n']); 18 | fprintf(fid,[' \n']); 19 | fprintf(fid,['# Output file name \n']); 20 | fprintf(fid,['OUTFILE ' [char(ints(ii).unwrlk)] '\n']); 21 | fprintf(fid,[' \n']); 22 | fprintf(fid,['# Correlation file name \n']); 23 | fprintf(fid,['CORRFILE ' maskfilerlk '\n']); 24 | fprintf(fid,[' \n']); 25 | fprintf(fid,['# Statistical-cost mode (TOPO, DEFO, SMOOTH, or NOSTATCOSTS) \n']); 26 | fprintf(fid,['STATCOSTMODE SMOOTH \n']); 27 | fprintf(fid,[' \n']); 28 | fprintf(fid,[' \n']); 29 | fprintf(fid,[' \n']); 30 | fprintf(fid,['INFILEFORMAT FLOAT_DATA \n']); 31 | fprintf(fid,['UNWRAPPEDINFILEFORMAT FLOAT_DATA \n']); 32 | fprintf(fid,['OUTFILEFORMAT FLOAT_DATA \n']); 33 | fprintf(fid,['CORRFILEFORMAT FLOAT_DATA \n']); 34 | fprintf(fid,[' \n']); 35 | fprintf(fid,['NTILEROW ' num2str(ntilerow) ' \n']); 36 | fprintf(fid,['NTILECOL ' num2str(ntilecol) ' \n']); 37 | fprintf(fid,['# Maximum number of child processes to start for parallel tile \n']); 38 | fprintf(fid,['# unwrapping. \n']); 39 | fprintf(fid,['NPROC ' num2str(nproc) ' \n']); 40 | fprintf(fid,['ROWOVRLP 100 \n']); 41 | fprintf(fid,['COLOVRLP 100 \n']); 42 | fprintf(fid,['RMTMPTILE TRUE \n']); 43 | fclose(fid); 44 | 45 | 46 | end 47 | 48 | disp(['unwrapping on ' num2str(nproc) ' cores, with ' num2str(ntilerow) 'x' num2str(ntilecol) ' tiles']) -------------------------------------------------------------------------------- /xml2rsc.m: -------------------------------------------------------------------------------- 1 | %Creates .rsc files for each flat.flat int in the ints structure 2 | clear all;close all 3 | 4 | set_params 5 | load(ts_paramfile); 6 | 7 | 8 | 9 | for i=1:nints 10 | %get length and width 11 | xmlfilename =[ints(i).flat '.xml']; 12 | system(['sed -n "/width/{n;p;}" ',xmlfilename,' > flat.flat.xml.txt']) 13 | system(['sed -n "/length/{n;p;}" ',xmlfilename,' >> flat.flat.xml.txt']) 14 | [status, width]=system('i=`awk ''NR==2 {print $1}'' flat.flat.xml.txt`;echo ${i:7:5}') 15 | [status, length]=system('i=`awk ''NR==5 {print $1}'' flat.flat.xml.txt`;echo ${i:7:5}') 16 | width(strfind(width,'<'))=[]; 17 | length(strfind(length,'<'))=[]; 18 | 19 | %write the .rsc file 20 | out=[ints(i).flat '.rsc']; 21 | fid = fopen(out,'wt+'); 22 | fprintf(fid,'WIDTH %s\n',num2str(width)); 23 | fprintf(fid,'FILE_LENGTH %s\n',num2str(length)); 24 | fprintf(fid,'XMIN %s\n',num2str(0)); 25 | fprintf(fid,'XMAX %s\n',num2str(width)); 26 | fprintf(fid,'YMIN %s\n',num2str(0)); 27 | fprintf(fid,'YMAX %s\n',num2str(length)); 28 | fprintf(fid,'RLOOKS %s\n',num2str(1)); 29 | fprintf(fid,'ALOOKS %s\n',num2str(1)); 30 | fprintf(fid,'FILE_START %s\n',num2str(1)); 31 | fprintf(fid,'DELTA_LINE_UTC %s\n',num2str(0.00374400023961602)); 32 | fclose(fid) 33 | ints(i).width=str2num(width); 34 | ints(i).length=str2num(length); 35 | ints(i).width_lks=floor(str2num(width)/rlooks); 36 | ints(i).length_lks=floor(str2num(length)/alooks); 37 | save(ts_paramfile,'ints','dates') 38 | end 39 | --------------------------------------------------------------------------------