forked from bminchew/vector_disp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_disp.f90
1036 lines (924 loc) · 36.9 KB
/
vector_disp.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
program vector_disp
!****************************************************************
!****************************************************************
!**
!** FILE NAME: vector_disp_bm.f
!**
!** DATE WRITTEN: September 2011
!**
!** PROGRAMMER:
!** Brent Minchew
!** California Institute of Technology
!**
!** FUNCTIONAL DESCRIPTION: 3D vector inversion estimated using
!** weighted least squares from unwrapped phase or
!** line of sight displacement files
!**
!** INPUTS: Phase or LOS displacement, correlation, LOS file
!** (LOS must be pixel-by-pixel interleave in order ENU)
!**
!** OUTPUTS: 1) Velocity field in component order and reference
!** frame as given in LOS file
!** 2) [optional] error estimation for each component
!** 3) [optional] mean square error for each pixel
!**
!** ROUTINES CALLED: dggglm, dgetri, dgetrf
!**
!** NOTES: must be called with appropriate command file
!**
!** UPDATE LOG:
!**
!** Date Changed Reason Changed
!** ------------ ----------------
!** Jan 8, 2013 Replaced MIGS inversion with LAPACK routines: dggglm, dgetri
!** Jun 3, 2013 Added rank(G) >= 3 check in subroutine getrank(G,nonul,rnk)
!**
!*****************************************************************
implicit none
character*250 cmdfile,otpref,otfold,eastnm,nornm,upnm,msenm,gtgnm
character*250 str,str2,label,dumname,vmagnm,numnm,acornm,gdopnm
character*1 errstr
character(len=250),dimension(500):: unwnm,cornm,losnm,aznm
integer ii,jj,kk,mm,zz,ierr,ios,ar,r,k,ns,iid,dumi,tp,tf,alaz
integer numscenes,master,cnt,doline,errest,nonul,maxcol,vm
integer stv(13),today(3),now(3),indx(3),mseout,scncnt,eoc,getwdp
integer info,lwork,nrhs,corout,gdopest,gtgout,gtgoff,ocoff,rnk
real*4 lambda,timeconv,distconv,pi,dum1,dum2,lne,nulo
integer,allocatable:: lines(:),cols(:),colsrt(:),azlogic(:),ipiv(:)
real*4,allocatable:: unw(:,:),cor(:,:),los(:,:),east(:),north(:),up(:)
real*4,allocatable:: dumv1(:),dumv2(:),azoff(:,:),ehdg(:),nhdg(:),hdg(:)
real*4,allocatable:: erre(:),errn(:),erru(:),nulaz(:),gdopvec(:)
real*4,allocatable:: latin(:),lonin(:),latspc(:),lonspc(:),nulu(:),nulc(:),nullos(:)
real*4,allocatable:: nlooks(:),r2dsp(:),msev(:),vmag(:),numout(:),avecor(:)
real*4,allocatable:: gtgeast(:),gtgnorth(:),gtgup(:),gtgoen(:),gtgonu(:),gtgoeu(:)
real*4,allocatable:: ocoen(:),oconu(:),ocoeu(:),wdop(:)
real*8,allocatable:: dgmat(:,:),ddvec(:),dvmat(:,:),dbmat(:,:),dxvec(:),dyvec(:)
real*8,allocatable:: ccoef(:),work(:)
real*4 gtg(3,3),gtgc(3,3)
real*8 dgtg(3,3),gdopmat(3,3),mse,dcor,dscor,sdum,rnktol,radconv
integer,allocatable:: scol(:),acol(:),bcol(:),latoff(:),lonoff(:),eline(:),sline(:)
integer,allocatable:: elinesrt(:),logic(:)
integer,allocatable:: losid(:),unwid(:),corid(:),azid(:) ! file ids
type :: parstr
real*4 pi,fpii,r2d
end type
type(parstr) par
character(len=11),parameter:: fu='unformatted'
character(len=6), parameter:: di='direct'
character(len=7), parameter:: re='replace'
print*,' ';print*,' '
if (iargc().eq.1) goto 900
print*,' ~~~ 3D Displacement Inversion ~~~ '
print*,' '
print*,' Usage vector_disp_bm input_command_file '
print*,' '
print*,' '
stop
900 continue
call getarg(1,cmdfile)
open(3,file=cmdfile,status='old')
rnktol = 1.d-2
ios = 0
lne = 1.d0
errest = 0
timeconv = 1.d0
distconv = 1.d0
pi = 4.d0*atan(1.d0)
numscenes = 5000
ns = 0
mseout = 0
eoc = 0
vm = 0
alaz = 0
corout = 0
gdopest = 0
gtgout = 0
gtgoff = 0
ocoff = 0
getwdp = 0
scncnt = 0
do while (ios.eq.0.and.ns.le.numscenes.and.scncnt.le.numscenes.and.eoc.eq.0)
read(3,'(A)',iostat=ios) str
k = scan(str,'::')
label = trim(adjustl(str(1:k-1)))
str=str(k+2:)
select case (label)
case('Number of Scenes (-)')
read(str,*,iostat=ios) numscenes
if (numscenes.lt.3) then
print*,'You must have at least 3 scenes to do the inversion'
stop
endif
allocate(lines(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in lines'
allocate(cols(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in cols'
allocate(latin(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in latin'
allocate(lonin(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in lonin'
allocate(latspc(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in latspc'
allocate(lonspc(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in lonspc'
allocate(nlooks(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in nlooks'
allocate(nulc(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in nulc'
allocate(nulu(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in nulu'
allocate(nullos(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in nullos'
allocate(nulaz(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in nulaz'
allocate(r2dsp(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in r2dsp'
allocate(azlogic(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in azlogic'
allocate(ehdg(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in ehdg'
allocate(nhdg(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in nhdg'
allocate(hdg(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in hdg'
azlogic = 0
case('Master Scene (-)')
read(str,*,iostat=ios) master
case('Radar Wavelength (in preferred output distance units)')
read(str,*,iostat=ios) lambda
case('Time converstion factor (applied to all scenes equally)')
if (trim(adjustl(str)).ne.'none') read(str,*,iostat=ios) timeconv
case('Distance converstion factor (applied to all scenes equally)')
if (trim(adjustl(str)).ne.'none') read(str,*,iostat=ios) distconv
case('Output file prefix')
otpref = str
case('Output folder')
otfold = str
case('Output error estimates')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
errest = 1
endif
case('Output diag(G^T*W*G)^-1)')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
errest = 1
endif
case('Output model covariance (G^TWG)^-1)')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
errest = 1
endif
case('Output offdiag(G^T*W*G)^-1)')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
ocoff = 1
endif
case('Output diag(G^TG)^-1')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
gtgout = 1
endif
case('Output diag(G^T*G)^-1')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
gtgout = 1
endif
case('Output offdiag(G^T*G)^-1')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
gtgoff = 1
endif
case('Output offdiag(G^TG)^-1')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
gtgoff = 1
endif
case('Output sqrt(tr((G^T*W*G)^-1))')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
getwdp = 1
endif
case('Output GDOP sqrt(tr((G^TG)^-1))')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
gdopest = 1
endif
case('Output mean square error estimate')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
mseout = 1
endif
case('Output velocity magnitude')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
vm = 1
endif
case('Output average correlation')
errstr = trim(adjustl(str))
if (errstr.eq.'y'.or.errstr.eq.'Y') then
corout = 1
endif
case('Output null value')
if (trim(adjustl(str)).eq.'NaN'.or.trim(adjustl(str)).eq.'nan') then
lne = 0.d0
nulo = 0.d0
elseif (trim(adjustl(str)).eq.'Inf'.or.trim(adjustl(str)).eq.'inf') then
lne = 0.d0
nulo = 1.d0
elseif (trim(adjustl(str)).eq.'-Inf'.or.trim(adjustl(str)).eq.'-inf') then
lne = 0.d0
nulo = -1.d0
else
read(str,*,iostat=ios) nulo
lne = 1.d0
endif
nulo = nulo/lne
case('Rank (normalized) tolerance')
read(str,*,iostat=ios) rnktol
case('Scene')
read(str,*,iostat=ios) ns
case('Samples in Unwrapped or LOS displacement (-)')
if (ns.gt.0) then
read(str,*,iostat=ios) cols(ns)
scncnt = scncnt + 1
endif
case('Unwrapped or LOS displacement file')
if (ns.gt.0) unwnm(ns) = str
case('Type of unwrapped or LOS displacement file (enter phase or disp)')
if (trim(adjustl(str)).eq.'phase'.and.ns.le.numscenes.and.ns.gt.0) then
r2dsp(ns) = 1.0d0
elseif (trim(adjustl(str)).eq.'disp'.and.ns.le.numscenes.and.ns.gt.0) then
r2dsp(ns) = 4.d0*pi/lambda
elseif (ns.gt.numscenes.or.ns.le.0) then
continue
else
print *,'Invalid type of displacement file in scene ',ns
print *,'Stopping...'
stop
endif
case('Correlation file')
if (ns.gt.0) cornm(ns) = str
case('LOS file (must be by-pixel interleave in order ENU)')
if (ns.gt.0) losnm(ns) = str
case('Azimuth offset file (enter none if unavailable)')
if (trim(adjustl(str)).ne.'none'.and.trim(adjustl(str)).ne.'None' &
& .and.trim(adjustl(str)).ne.'NONE'.and.ns.gt.0) then
aznm(ns) = str
azlogic(ns) = 1
alaz = 1
endif
case('Peg heading (only needed if azimuth offset file is provided)')
if(azlogic(ns).eq.1) read(str,*,iostat=ios) hdg(ns)
case('Azimuth offset null value')
if(azlogic(ns).eq.1) read(str,*,iostat=ios) nulaz(ns)
case('Upper left corner Latitude (deg)')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) latin(ns)
case('Upper left corner Longitude (deg)')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) lonin(ns)
case('Latitude Spacing (deg/pixel)')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) latspc(ns)
case('Longitude Spacing (deg/pixel)')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) lonspc(ns)
case('Correlation null value')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) nulc(ns)
case('Phase or displacement null value')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) nulu(ns)
case('LOS null value')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) nullos(ns)
case('Number of looks')
if(ns.le.numscenes.and.ns.gt.0) read(str,*,iostat=ios) nlooks(ns)
case('End of command file')
eoc = 1
case default
continue
end select
enddo
allocate(colsrt(numscenes))
colsrt = cols
call qsort(colsrt,numscenes)
maxcol = colsrt(numscenes)
deallocate(colsrt)
! now allocate the rest
allocate(acol(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in acol'
allocate(bcol(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in bcol'
allocate(scol(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in scol'
allocate(eline(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in eline'
allocate(sline(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in sline'
allocate(latoff(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in latoff'
allocate(lonoff(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in lonoff'
allocate(losid(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in losid'
allocate(unwid(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in unwid'
allocate(corid(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in corid'
allocate(ccoef(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in ccoef'
allocate(elinesrt(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in elinesrt'
allocate(logic(numscenes),stat=ierr)
if(ierr.ne.0) print*,'allocation error in logic'
allocate(dumv1(10*maxcol),stat=ierr)
if(ierr.ne.0) print*,'allocation error in dumv1'
allocate(dumv2(maxcol),stat=ierr)
if(ierr.ne.0) print*,'allocation error in dumv2'
allocate(unw(numscenes,cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in unw'
allocate(cor(numscenes,cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in cor'
allocate(los(numscenes,3*cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in los'
allocate(east(cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in east'
allocate(north(cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in north'
allocate(up(cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in up'
allocate(erre(cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in erre'
allocate(errn(cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in errn'
allocate(erru(cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in erru'
allocate(numout(cols(master)),stat=ierr)
if(ierr.ne.0) print*,'allocation error in numout'
if (mseout.eq.1) allocate(msev(cols(master)))
if (vm.eq.1) allocate(vmag(cols(master)))
if (gdopest.eq.1) allocate(gdopvec(cols(master)))
if (corout.eq.1) allocate(avecor(cols(master)))
if (getwdp.eq.1) allocate(wdop(cols(master)))
if (gtgout.eq.1) then
allocate(gtgeast(cols(master)))
allocate(gtgnorth(cols(master)))
allocate(gtgup(cols(master)))
endif
if (ocoff.eq.1) then
allocate(ocoen(cols(master)))
allocate(oconu(cols(master)))
allocate(ocoeu(cols(master)))
endif
if (gtgoff.eq.1) then
allocate(gtgoen(cols(master)))
allocate(gtgonu(cols(master)))
allocate(gtgoeu(cols(master)))
endif
if (alaz.eq.1) then
allocate(azoff(numscenes,cols(master)))
allocate(azid(numscenes))
endif
! open output files
otpref = adjustl(otpref)
otfold = adjustl(otfold)
tp = len_trim(otpref)
if (otpref(tp:tp).eq.'.') tp = tp-1
tf = len_trim(otfold)
if (otfold(tf:tf).eq.'/') tf = tf-1
eastnm = otfold(1:tf)//'/'//otpref(1:tp)//'.east'
nornm = otfold(1:tf)//'/'//otpref(1:tp)//'.north'
upnm = otfold(1:tf)//'/'//otpref(1:tp)//'.up'
numnm = otfold(1:tf)//'/'//otpref(1:tp)//'.num'
open(11,file=eastnm,access=di,form=fu,status=re,recl=4*cols(master))
open(12,file=nornm,access=di,form=fu,status=re,recl=4*cols(master))
open(13,file=upnm,access=di,form=fu,status=re,recl=4*cols(master))
open(19,file=numnm,access=di,form=fu,status=re,recl=4*cols(master))
if (errest.eq.1) then
eastnm = otfold(1:tf)//'/'//otpref(1:tp)//'.obscov.east'
nornm = otfold(1:tf)//'/'//otpref(1:tp)//'.obscov.north'
upnm = otfold(1:tf)//'/'//otpref(1:tp)//'.obscov.up'
open(14,file=eastnm,access=di,form=fu,status=re,recl=4*cols(master))
open(15,file=nornm,access=di,form=fu,status=re,recl=4*cols(master))
open(16,file=upnm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (mseout.eq.1) then
msenm = otfold(1:tf)//'/'//otpref(1:tp)//'.mse'
open(17,file=msenm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (vm.eq.1) then
vmagnm = otfold(1:tf)//'/'//otpref(1:tp)//'.mag'
open(18,file=vmagnm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (corout.eq.1) then
acornm = otfold(1:tf)//'/'//otpref(1:tp)//'.avecor'
open(20,file=acornm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (gdopest.eq.1) then
gdopnm = otfold(1:tf)//'/'//otpref(1:tp)//'.gdop'
open(21,file=gdopnm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (gtgout.eq.1) then
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.gtgi.east'
open(22,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.gtgi.north'
open(23,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.gtgi.up'
open(24,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (gtgoff.eq.1) then
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.gtgi.en'
open(25,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.gtgi.nu'
open(26,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.gtgi.eu'
open(27,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (ocoff.eq.1) then
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.obscov.en'
open(28,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.obscov.nu'
open(29,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.obscov.eu'
open(30,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
endif
if (getwdp.eq.1) then
gtgnm = otfold(1:tf)//'/'//otpref(1:tp)//'.obscov.dop'
open(31,file=gtgnm,access=di,form=fu,status=re,recl=4*cols(master))
endif
! set up each scene
iid = 41 ! starting input file id
do ii=1,numscenes
unwid(ii) = iid
931 iid = iid + 1
if (iid.ge.900.and.iid.le.940) goto 931
if (iid.ge.100.and.iid.le.110) goto 931
corid(ii) = iid
932 iid = iid + 1
if (iid.ge.900.and.iid.le.940) goto 932
if (iid.ge.100.and.iid.le.110) goto 932
losid(ii) = iid
933 iid = iid + 1
if (iid.ge.900.and.iid.le.940) goto 933
if (iid.ge.100.and.iid.le.110) goto 933
dumname = trim(adjustl(unwnm(ii)))
open(unwid(ii),file=dumname,access=di,form=fu,status='old',recl=4*cols(ii))
call stat(dumname,stv)
lines(ii) = stv(8)/(4*cols(ii))
dumname = trim(adjustl(cornm(ii)))
open(corid(ii),file=dumname,access=di,form=fu,status='old',recl=4*cols(ii))
dumname = trim(adjustl(losnm(ii)))
open(losid(ii),file=dumname,access=di,form=fu,status='old',recl=3*4*cols(ii))
if (azlogic(ii).eq.1) then
azid(ii) = iid
934 iid = iid + 1
if (iid.ge.900.and.iid.le.940) goto 934
if (iid.ge.100.and.iid.le.110) goto 934
dumname = trim(adjustl(aznm(ii)))
open(azid(ii),file=dumname,access=di,form=fu,status='old',recl=4*cols(ii))
azoff(ii,:) = nulaz(ii)
ehdg(ii) = sin(hdg(ii)*pi/180.d0)
nhdg(ii) = cos(hdg(ii)*pi/180.d0)
endif
! calculate pixel offsets for each scene relative to the master
dumi = 0
dum1 = (latin(master) - latin(ii))/latspc(ii)
if (abs(dum1-int(dum1)) .ge. 0.5d0) dumi = dum1/abs(dum1)
latoff(ii) = -(int(dum1) + dumi) ! positive southward
dumi = 0
dum1 = (lonin(master) - lonin(ii))/lonspc(ii)
if (abs(dum1-int(dum1)) .ge. 0.5d0) dumi = dum1/abs(dum1)
lonoff(ii) = -(int(dum1) + dumi) ! positive eastward
eline(ii) = lines(ii) + latoff(ii)
sline(ii) = -latoff(ii)
! set column offset conditions
if (lonoff(ii).le.0) then ! shifted left or not at all relative to master
bcol(ii) = 0 ! no buffer on left
scol(ii) = -lonoff(ii) + 1 ! starting column
dum1 = cols(ii) - scol(ii) + 1
if (dum1.lt.cols(master)) then ! needs null-valued buffer on the right
acol(ii) = cols(master) - dum1
else ! no buffer on the right
acol(ii) = 0
endif
else ! shifted right relative to master
bcol(ii) = lonoff(ii) ! number of null-valued columns to buffer on left
scol(ii) = 1
dum1 = cols(ii)
if (dum1.lt.cols(master)) then
acol(ii) = cols(master) - dum1 ! number of null-valued columns to buffer onthe right
else
acol(ii) = 0 ! no buffer on the right
endif
endif
ccoef(ii) = 2.d0*nlooks(ii)
enddo
elinesrt = eline
call qsort(elinesrt,numscenes)
if (lines(master).le.elinesrt(numscenes-2)) then
doline = lines(master)
else
doline = elinesrt(numscenes-2)
endif
print*,' '
print*,' ~~~ Weighted least squares velocity inversion ~~~'
print*,' '
print*,' Total lines = ',doline
print*,' Total samples = ',cols(master)
print*,' Total scenes = ',numscenes
print*,' ';print*,' '
! Let's do it
dumv2 = 0.d0
radconv = lambda/(4.d0*pi)*timeconv*distconv
do ii=1,doline
if(mod(ii,100).eq.0.or.ii.eq.doline.or.ii.eq.1) then
write(*,"(A1,A,t8,I8)",advance='no') achar(13),'Line = ',ii
endif
cnt = 0
do mm=1,numscenes
if (ii.ge.latoff(mm).and.ii.le.eline(mm).and.(sline(mm)+ii).gt.0) then
dumv1 = nulu(mm)
read(unwid(mm),rec=ii+sline(mm)) dumv1(bcol(mm)+1:bcol(mm)+cols(mm))
unw(mm,:) = dumv1(scol(mm):cols(master)+scol(mm)-1)
dumv1 = nulc(mm)
read(corid(mm),rec=ii+sline(mm)) dumv1(bcol(mm)+1:bcol(mm)+cols(mm))
cor(mm,:) = dumv1(scol(mm):cols(master)+scol(mm)-1)
dumv1 = nullos(mm)
read(losid(mm),rec=ii+sline(mm)) dumv1(3*bcol(mm)+1:3*(bcol(mm)+cols(mm)))
los(mm,:) = dumv1(3*(scol(mm)-1)+1:3*(cols(master)+scol(mm)-1))
if(azlogic(mm).eq.1) then
dumv1 = nulaz(mm)
read(azid(mm),rec=ii+sline(mm)) dumv1(bcol(mm)+1:bcol(mm)+cols(mm))
azoff(mm,:) = dumv1(scol(mm):cols(master)+scol(mm)-1)
endif
cnt = cnt + 1
endif
enddo
east = nulo
north = nulo
up = nulo
erre = nulo
errn = nulo
erru = nulo
numout = nulo
if (mseout.eq.1) msev = nulo
if (vm.eq.1) vmag = nulo
if (corout.eq.1) avecor = nulo
if (gdopest.eq.1) gdopvec = nulo
if (getwdp.eq.1) wdop = nulo
if (gtgout.eq.1) then
gtgeast = nulo
gtgnorth = nulo
gtgup = nulo
endif
if (gtgoff.eq.1) then
gtgoen = nulo
gtgonu = nulo
gtgoeu = nulo
endif
if (ocoff.eq.1) then
ocoen = nulo
oconu = nulo
ocoeu = nulo
endif
if (cnt.ge.3) then
!$omp parallel do &
!$omp default(private) &
!$omp shared(los,cor,unw,ccoef,r2dsp,nullos,nulu,nulc) &
!$omp shared(azlogic,azoff,nulaz,ehdg,nhdg,radconv) &
!$omp shared(east,north,up,erre,errn,erru,numout,mseout,msev) &
!$omp shared(vm,vmag,corout,avecor,gdopest,gdopvec,getwdp,wdop) &
!$omp shared(gtgout,gtgeast,gtgnorth,gtgup,gtgoff,gtgoen,gtgonu,gtgoeu) &
!$omp shared(errest,ocoff,ocoen,oconu,ocoeu,cols,master,numscenes,rnktol)
do jj=1,cols(master)
nonul = 0
logic = 0
do mm=1,numscenes
dum1 = product(los(mm,(1+(jj-1)*3):(3+(jj-1)*3))-nullos(mm))*cor(mm,jj) ! test null vals
if(unw(mm,jj).ne.nulu(mm).and.dum1.ne.0.d0.and.cor(mm,jj).ne.nulc(mm).and.&
& unw(mm,jj).eq.unw(mm,jj).and.dum1.eq.dum1) then
nonul = nonul + 1
logic(mm) = 1
if (azlogic(mm).eq.1.and.azoff(mm,jj).ne.nulaz(mm).and.azoff(mm,jj).eq.azoff(mm,jj)) nonul = nonul + 1
endif
enddo
numout(jj) = nonul
if (nonul.ge.3) then
lwork = 3+nonul+nonul
allocate(dgmat(nonul,3))
allocate(dvmat(nonul,nonul))
allocate(dbmat(nonul,nonul))
allocate(ddvec(nonul))
allocate(dxvec(3))
allocate(dyvec(nonul))
allocate(work(lwork))
dvmat = 0.d0
dbmat = 0.d0
dscor = 0.d0
kk = 1
do mm=1,numscenes
if (logic(mm).eq.1) then
dcor = (dble(cor(mm,jj)))**2.d0
dvmat(kk,kk)= ccoef(mm)*dcor/(1-dcor)
dbmat(kk,kk)= 1.d0/dvmat(kk,kk)
dgmat(kk,1) = dble(los(mm,(1+(jj-1)*3)))
dgmat(kk,2) = dble(los(mm,(2+(jj-1)*3)))
dgmat(kk,3) = dble(los(mm,(3+(jj-1)*3)))
ddvec(kk) = dble(unw(mm,jj)*r2dsp(mm))
dscor = dscor + dble(cor(mm,jj))
kk = kk + 1
if(azlogic(mm).eq.1.and.azoff(mm,jj).ne.nulaz(mm)) then
dvmat(kk,kk) = ccoef(mm)*dcor/(1-dcor)
dbmat(kk,kk) = 1.d0/dvmat(kk,kk)
dgmat(kk,1) = dble(ehdg(mm))
dgmat(kk,2) = dble(nhdg(mm))
dgmat(kk,3) = 0.d0
ddvec(kk) = dble(azoff(mm,jj)*r2dsp(mm))
dscor = dscor + dble(cor(mm,jj))
kk = kk + 1
endif
endif
enddo
call getrank(dgmat,nonul,rnk,rnktol) ! don't bother with rank < 3
if (rnk.gt.2) then
dgtg = matmul(matmul(transpose(dgmat),dvmat),dgmat)
gdopmat = matmul(transpose(dgmat),dgmat)
call dggglm(nonul,3,nonul,dgmat,nonul,dbmat,nonul,ddvec,dxvec,dyvec,work,lwork,info)
! dggglm is the LAPACK general least squares routine
east(jj) = sngl(dxvec(1)*radconv)
north(jj) = sngl(dxvec(2)*radconv)
up(jj) = sngl(dxvec(3)*radconv)
if (corout.eq.1) then
avecor(jj) = sngl(dscor/dble(nonul))
endif
if (vm.eq.1) then
vmag(jj) = sngl(radconv*dsqrt(dxvec(1)**2+dxvec(2)**2+dxvec(3)**2))
endif
if (gtgout.eq.1.or.gdopest.eq.1.or.gtgoff.eq.1) then
call dinv(3,gdopmat)
if (gtgout.eq.1) then
gtgeast(jj) = sngl(gdopmat(1,1))
gtgnorth(jj) = sngl(gdopmat(2,2))
gtgup(jj) = sngl(gdopmat(3,3))
endif
if (gtgoff.eq.1) then
gtgoen(jj) = sngl(gdopmat(1,2))
gtgonu(jj) = sngl(gdopmat(2,3))
gtgoeu(jj) = sngl(gdopmat(1,3))
endif
if (gdopest.eq.1) then
gdopvec(jj) = sngl(dsqrt(gdopmat(1,1) + gdopmat(2,2) + gdopmat(3,3)))
endif
endif
if (errest.eq.1.or.ocoff.eq.1.or.getwdp.eq.1) then
call dinv(3,dgtg)
if (errest.eq.1) then
erre(jj) = sngl(dgtg(1,1))
errn(jj) = sngl(dgtg(2,2))
erru(jj) = sngl(dgtg(3,3))
endif
if (ocoff.eq.1) then
ocoen(jj) = sngl(dgtg(1,2))
oconu(jj) = sngl(dgtg(2,3))
ocoeu(jj) = sngl(dgtg(1,3))
endif
if (getwdp.eq.1) then
wdop(jj) = sngl(dsqrt(dgtg(1,1) + dgtg(2,2) + dgtg(3,3)))
endif
endif
if (mseout.eq.1) then
if (nonul.gt.2) then
mse = sum(dyvec**2)/dble(nonul)
else
mse = 0.d0
endif
msev(jj) = sngl(mse)
endif
endif
deallocate(dgmat,dvmat,dbmat,ddvec,work,dxvec,dyvec)
endif
enddo
!$omp end parallel do
endif
write(11,rec=ii) (east(mm),mm=1,cols(master))
write(12,rec=ii) (north(mm),mm=1,cols(master))
write(13,rec=ii) (up(mm),mm=1,cols(master))
write(19,rec=ii) (numout(mm),mm=1,cols(master))
if (errest.eq.1) then
write(14,rec=ii) (erre(mm),mm=1,cols(master))
write(15,rec=ii) (errn(mm),mm=1,cols(master))
write(16,rec=ii) (erru(mm),mm=1,cols(master))
endif
if (mseout.eq.1) write(17,rec=ii) (msev(mm),mm=1,cols(master))
if (vm.eq.1) write(18,rec=ii) (vmag(mm),mm=1,cols(master))
if (corout.eq.1) write(20,rec=ii) (avecor(mm),mm=1,cols(master))
if (gdopest.eq.1) write(21,rec=ii) (gdopvec(mm),mm=1,cols(master))
if (gtgout.eq.1) then
write(22,rec=ii) (gtgeast(mm),mm=1,cols(master))
write(23,rec=ii) (gtgnorth(mm),mm=1,cols(master))
write(24,rec=ii) (gtgup(mm),mm=1,cols(master))
endif
if (gtgoff.eq.1) then
write(25,rec=ii) (gtgoen(mm),mm=1,cols(master))
write(26,rec=ii) (gtgonu(mm),mm=1,cols(master))
write(27,rec=ii) (gtgoeu(mm),mm=1,cols(master))
endif
if (ocoff.eq.1) then
write(28,rec=ii) (ocoen(mm),mm=1,cols(master))
write(29,rec=ii) (oconu(mm),mm=1,cols(master))
write(30,rec=ii) (ocoeu(mm),mm=1,cols(master))
endif
if (getwdp.eq.1) write(31,rec=ii) (wdop(mm),mm=1,cols(master))
enddo
print*,' ';print*,' '
print*,'Done!'
print*,' ';print*,' '
100 format(a8,i6)
110 format(a15,f9.2)
end program
! *************************************************************************************
! *************************************************************************************
subroutine getrank(a,n,rnk,st)
! returns rank(A) for a double precision nX3 matrix A
implicit none
integer n,rnk,info,i
real*8 a(n,3),pa(n,3),s(3),u(n,3),vt(n,3),work(100),st
pa = a
call dgesvd('N','N',n,3,pa,n,s,u,n,vt,n,work,30,info)
if (info.ne.0) then
print *,'Something is wrong in getrank; info = ',info
else
s = s/dsqrt(sum(s**2))
rnk = 0
do i=1,3
if (s(i).gt.st) rnk = rnk + 1
enddo
endif
end subroutine
! *************************************************************************************
! *************************************************************************************
subroutine dinv(n,mat)
! returns inv(mat) for a double precision nXn matrix mat
implicit none
integer n,info,ipiv(n)
real*8 mat(n,n), work(n*n)
call dgetrf(n,n,mat,n,ipiv,info)
call dgetri(n,mat,n,ipiv,work,n*n,info)
end subroutine
! *************************************************************************************
! *************************************************************************************
subroutine getmse(v,b,g,gtg,m,n,mse)
! calculates mean square error scalar mse
!
! inputs: v = inverse covariance matrix
! b = data vector
! g = linear operator
! gtg = inv(transpose(g)*inv(v)*g)
! m = number of observations
! n = size of model (will be 3 for 3D vector field)
implicit none
integer m,n
real*4 gtg(n,n),v(m,m),g(m,n),b(m)
real*4 va(m,n),vagtg(m,n),at(n,m),atv(n,m)
real*4 big(m,m),par1(m),mse
if (m.eq.n) then
mse = 0
return
endif
at = transpose(g)
va = matmul(v,g)
vagtg = matmul(va,gtg)
atv = matmul(at,v)
big = v - matmul(vagtg,atv)
par1(1) = b(1)*big(1,1) + b(2)*big(2,1) + b(3)*big(3,1)
par1(2) = b(1)*big(1,2) + b(2)*big(2,2) + b(3)*big(3,2)
par1(3) = b(1)*big(1,3) + b(2)*big(2,3) + b(3)*big(3,3)
mse = (par1(1)*b(1) + par1(2)*b(2) + par1(3)*b(3))/(m-n)
end subroutine
! *************************************************************************************
! *************************************************************************************
SUBROUTINE qsort(COUNT,N)
! Quicksort
!C .. Scalar Arguments ..
INTEGER N
!C ..
!C .. Array Arguments ..
REAL COUNT(*)
!C ..
!C .. Local Scalars ..
REAL AV,X
INTEGER I,IF,IFEND,IFK,IFKA,IK,IP,IS,IS1,IY,J,K,K1,LA,LNGTH,M,MLOOP
!C ..
!C .. Local Arrays ..
INTEGER MARK(100)
!C ..
!C .. Executable Statements ..
!C CHECK THAT A TRIVIAL CASE HAS NOT BEEN ENTERED
IF (N.EQ.1) GO TO 280
IF (N.GE.1) GO TO 110
WRITE (6,FMT=100)
100 FORMAT (/,/,/,20X,' ***KB05A*** ','NO NUMBERS TO BE SORTED ** RETURN TO CALLING PROGRAM')
GO TO 280
!C 'M' IS THE LENGTH OF SEGMENT WHICH IS SHORT ENOUGH TO ENTER
!C THE FINAL SORTING ROUTINE. IT MAY BE EASILY CHANGED.
110 M = 12
!C SET UP INITIAL VALUES.
LA = 2
IS = 1
IF = N
DO 270 MLOOP = 1,N
!C IF SEGMENT IS SHORT ENOUGH SORT WITH FINAL SORTING ROUTINE .
IFKA = IF - IS
IF ((IFKA+1).GT.M) GO TO 140
!C********* FINAL SORTING ***
!C ( A SIMPLE BUBBLE SORT )
IS1 = IS + 1
DO 130 J = IS1,IF
I = J
120 IF (COUNT(I-1).LE.COUNT(I)) GO TO 130
AV = COUNT(I-1)
COUNT(I-1) = COUNT(I)
COUNT(I) = AV
I = I - 1
IF (I.GT.IS) GO TO 120
130 CONTINUE
LA = LA - 2
GO TO 260
!C ******* QUICKSORT ********
!C SELECT THE NUMBER IN THE CENTRAL POSITION IN THE SEGMENT AS
!C THE TEST NUMBER.REPLACE IT WITH THE NUMBER FROM THE SEGMENT'S
!C HIGHEST ADDRESS.
140 IY = (IS+IF)/2
X = COUNT(IY)
COUNT(IY) = COUNT(IF)
!C THE MARKERS 'I' AND 'IFK' ARE USED FOR THE BEGINNING AND END
!C OF THE SECTION NOT SO FAR TESTED AGAINST THE PRESENT VALUE
!C OF X .
K = 1
IFK = IF
!C WE ALTERNATE BETWEEN THE OUTER LOOP THAT INCREASES I AND THE
!C INNER LOOP THAT REDUCES IFK, MOVING NUMBERS AS NECESSARY,
!C UNTIL THEY MEET .
DO 160 I = IS,IF
IF (X.GT.COUNT(I)) GO TO 160
IF (I.GE.IFK) GO TO 170
COUNT(IFK) = COUNT(I)
K1 = K
DO 150 K = K1,IFKA
IFK = IF - K
IF (COUNT(IFK).GE.X) GO TO 150
IF (I.GE.IFK) GO TO 180
COUNT(I) = COUNT(IFK)
GO TO 160
150 CONTINUE
GO TO 170
160 CONTINUE
!C RETURN THE TEST NUMBER TO THE POSITION MARKED BY THE MARKER
!C WHICH DID NOT MOVE LAST. IT DIVIDES THE INITIAL SEGMENT INTO
!C 2 PARTS. ANY ELEMENT IN THE FIRST PART IS LESS THAN ANY ELEMENT
!C IN THE SECOND PART, AND THEY MAY NOW BE SORTED INDEPENDENTLY.
170 COUNT(IFK) = X
IP = IFK
GO TO 190
180 COUNT(I) = X
IP = I
!C STORE THE LONGER SUBDIVISION IN WORKSPACE.
190 IF ((IP-IS).GT. (IF-IP)) GO TO 200
MARK(LA) = IF
MARK(LA-1) = IP + 1
IF = IP - 1
GO TO 210
200 MARK(LA) = IP - 1
MARK(LA-1) = IS
IS = IP + 1
!C FIND THE LENGTH OF THE SHORTER SUBDIVISION.