-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetinv.m
1347 lines (1157 loc) · 33.9 KB
/
getinv.m
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
function [p,dr,ps,de,der]=getinv(di,p,ps,dr,iplot)
% function [p,dr,ps,de,der]=getinv(di,p,ps,dr,iplot)
% LADCP processing software version 5.0
%
% - solve linear inverse problem
%
% Martin Visbeck, LDEO, April-2000
%======================================================================
% G E T I N V . M
% doc: Thu Jun 17 15:36:21 2004
% dlm: Wed Sep 4 16:55:50 2019
% (c) 2004 ladcp@
% uE-Info: 35 55 NIL 0 0 72 0 2 8 NIL ofnI
%======================================================================
% CHANGE HISTORY:
%
% Jun 17, 2004: - removed ps.botfac = 0 on lack of magdecl
% Jun 27, 2004: - added warning about removed SADCP profiles
% Jul 6, 2004: - BUG: warning did not work because p was not accessible
% inside lainsadcp()
% Jul 17, 2004: - added warning about down/up mismatch (GPS problems)
% Jan 7, 2009: - tightened use of exist()
% Aug 25, 2010: - BUG: warning about low SADCP weight was produced
% (and corresponding velocities removed) even
% on sadcpfac=0, i.e. when SADCP data were only
% used for plotting
% Jun 29, 2011: - pass ps to geterr to allow controlling fig. 3
% Jan 6, 2012: - lesqchol removed because it does not deal with
% nearly singular matrices gracefully
% - BUG: replaced all imag() by new imagnan()
% Jul 28, 2014: - modified how to specify smallfac
% Aug 9, 2014: - modified how to specify dragfac (+ve for fixed; -ve for scaled Martin's default)
% Jul 23, 2015: - commented on bug below (#!#)
% - rounded default ps.dz (GK suggestion)
if nargin<5, iplot=0; end
%### start by defining all processing parameter which are not set by the user
disp('GETINV: compute best velocity profile')
ps=setdefv(ps,'dz',round(medianan(abs(diff(di.izm(:,1))))));
ps=setdefv(ps,'botfac',1);
ps=setdefv(ps,'sadcpfac',1);
ps=setdefv(ps,'barofac',1);
ps=setdefv(ps,'dragfac',0);
% negative values weigh Martin's original default
if (ps.dragfac < 0)
ps.dragfac = -ps.dragfac * tanh(p.maxdepth/3000)*0.2;
end
% how much do you want to smooth the ocean profile
ps=setdefv(ps,'smoofac',0);
% how much do you want to restrict large shears on short vertical wavelengths
% ps.smallfac(1) is the vertical-wavelength filter cutoff
% ps.smallfac(2) is the filter strength (larger implies more filtering)
%ps=setdefv(ps,'smallfac',[500 0.02]); % Martin's defaults
%ps=setdefv(ps,'smallfac',[200 0.02]); % works reasonably well for DoMORE1 data
ps=setdefv(ps,'smallfac',[99999 0]); % default: disable
imax = ceil(p.maxdepth/ps.smallfac(1));
smallfac = [0 0];
for i=1:imax
smallfac(i,1) = i;
smallfac(i,2) = ps.smallfac(2)/(1+abs(i-(imax/2)))*tanh(p.maxdepth/3000);
end
% do you want up/down cast seperately then set to 1
ps=setdefv(ps,'down_up',1);
% decide how to weight data
ps=setdefv(ps,'std_weight',1);
% taper small weights weight = weight. ^weightpower
ps=setdefv(ps,'weightpower',1);
ps=setdefv(ps,'solve',1);
% weight bottom track data with distance of bottom
% ps=setdefv(ps,'btrk_weight_nblen',[15 5]);
ps=setdefv(ps,'btrk_weight_nblen',[0 0]);
% restrict data to one instrument only 1: up+dn, 2:dn only 3:up only
ps=setdefv(ps,'up_dn_looker',1);
% Check for magnetic deviation
if existf(p,'drot')
if ~isfinite(p.drot)
warn=[' magnetic deviation given is NAN '];
p.warnp(size(p.warnp,1)+1,1:length(warn))=warn;
p.rot=0;
ps.sadcpfac=0;
ps.barofac=0;
% ps.botfac=0; %%% NOT NEEDED?!
ps.dragfac=0;
end
else
warn=[' NO magnetic deviation given '];
p.warnp(size(p.warnp,1)+1,1:length(warn))=warn;
p.rot=0;
ps.sadcpfac=0;
ps.barofac=0;
% ps.botfac=0; %%% NOT NEEDED?!
ps.dragfac=0;
end
% Barotropic velocity error due to navigation error
ps=setdefv(ps,'barvelerr',2*p.nav_error/p.dt_profile);
disp([' Barotropic velocity error ',num2str(2*p.nav_error/p.dt_profile),...
' [m/s]'])
% Super ensemble velocity error
nmax=min(length(di.izd),7);
sw=stdnan(di.rw(di.izd(1:nmax),:)); ii=find(sw>0);
sw=medianan(sw(ii))/tan(p.beamangle*pi/180);
disp([' super ensemble velocity error ',num2str(sw),' [m/s]'])
ps=setdefv(ps,'velerr',maxnan([sw,0.02]));
if exist('dr','var')
if existf(dr,'uerr')
if any(isfinite(dr.uerr))
ps.velerr=medianan(dr.uerr);
disp([' set velocity error to:',num2str(ps.velerr),' [m/s]'])
end
end
end
disp([' vertical resolution (ps.dz) is ',num2str(ps.dz),' [m]'])
[nbin,nt]=size(di.ru);
%### set up matrixes for inverse problem
%set up data arrays
% restrict to up/down looker
if ps.up_dn_looker==2
di.weight(di.izu,:)=nan;
disp(' restrict inversion to down looking instrument only')
elseif ps.up_dn_looker==3
di.weight(di.izd,:)=nan;
disp(' restrict inversion to up looking instrument only')
p=rmfield(p,'zbottom');
end
% velocities are complex
d=di.ru+sqrt(-1)*di.rv;
d=reshape(d,nbin*nt,1);
% bottom track velocity
if existf(p,'zbottom');
bvel=di.bvel(1,:)+sqrt(-1)*di.bvel(2,:);
bvels=sqrt(di.bvels(1,:).^2+di.bvels(2,:).^2);
if sum(ps.btrk_weight_nblen) > 0
% normalize btweight with gaussian centered at 5 binlenght from bottom
hbot=di.z+p.zbottom;
hm=abs(diff(di.izm([-1:0]+end,1)))*ps.btrk_weight_nblen(1);
hs=abs(diff(di.izm([-1:0]+end,1)))*ps.btrk_weight_nblen(2);
whbot=exp(-(hbot-hm).^2./(hs).^2);
bvels=bvels./whbot;
disp([' weighted bottom track with Gaussian centered ',...
int2str(ps.btrk_weight_nblen(1)),' bins above bottom and width of ',...
int2str(ps.btrk_weight_nblen(2)),' bins '])
end
dbot=meshgrid(bvel,1:nbin);
dbot=reshape(dbot,nt*nbin,1);
end
% bin depths
izv=reshape(-di.izm,nbin*nt,1);
% profile number
jprof=cumsum(ones(nbin,nt)')';
jprof=reshape(jprof,nbin*nt,1);
% bin number
jbin=meshgrid(1:nbin,1:nt)';
jbin=reshape(jbin,nbin*nt,1);
% data weight
if ps.std_weight~=1;
disp(' use correlation based weights')
% use correlation based estimator
wm=di.weight.^ps.weightpower;
% data with a d.weight smaller that weightmin are not used
ps=setdefv(ps,'weightmin',0.05);
else
% use super ensemble based estimator
disp([' use super ensemble std based weights normalized by ',...
num2str(ps.velerr),' m/s '])
wm=ps.velerr./di.ruvs+di.weight*0;
% data with a d.weight smaller that weightmin are not used
ps=setdefv(ps,'weightmin',0.05);
end
wm=reshape(wm,nt*nbin,1);
% other arrays used
dtiv=di.dt;
tim=di.time_jul;
zctd=di.z;
slat=di.slat;
slon=di.slon;
if isfinite(sum(slon+slat))
xship=(slon-slon(1))*60*1852*cos(meannan(slat)*pi/180);
yship=(slat-slat(1))*60*1852;
uship=diff(xship)./diff(tim)/(24*3600);
vship=diff(yship)./diff(tim)/(24*3600);
shipvel=uship+sqrt(-1)*vship;
shipvel=mean([shipvel([1,1:end]);shipvel([1:end,end])]);
% get a smooth ship surface velocity
tim1=tim-tim(1);
shipvelf=polyval(polyfit(tim1,shipvel,3),tim1);
i=0;
if length(shipvel)>5
while (i<50 & std(abs(shipvel-shipvelf))>0.15) | i==0
shipvel=mean([shipvel([3:end,end,end]);shipvel([2:end,end]);...
shipvel;shipvel([1,1:(end-1)]);shipvel([1,1,1:(end-2)])]);
i=i+1;
end
disp([' preaveraged GPS ships vel ',int2str(i),' times '])
end
else
% no GPS ship navigation time series, assume constant ships velocity
uship_a=p.uship+sqrt(-1)*p.vship;
shipvel=uship_a+di.z*0;
end
% mean CTD vertical velocity
wctd=meannan(di.rw);
if ps.dragfac>0
shipdragvel=shipvel;
if exist('dr','var')
% compute the ocean flow at the depth of the CTD
ut=interp1(dr.z,dr.u,-di.z);
vt=interp1(dr.z,dr.v,-di.z);
shipdragvel=shipvel-(ut+sqrt(-1)*vt);
end
% derive estimate for CTD velocity using ship velocity
% and vertical velocity and wireangle
% 0.2 of W-velocity project horizontal for 1 m/s
ps=setdefv(ps,'drag_tilt_vel',0.5);
tiltfac = ps.drag_tilt_vel*abs(shipdragvel).*(1-tanh(-di.z/8000));
% preject fration of vertical velocity in ships velocity direction
wctdf=-shipdragvel./(abs(shipdragvel)+0.001).*wctd.*tiltfac;
% make sure that this is not faster than ship velocity
wfac=max(abs(wctdf)./(abs(shipdragvel)+0.001),1+wctd*0);
wctdf=wctdf./wfac;
% average ctdvel back in time to take the inertia of wire into account
% smooth over max of 20 minutes for depth ~ 2000m
ps=setdefv(ps,'drag_lag_tim',20);
ps=setdefv(ps,'drag_lag_depth',2000);
for j=1:length(shipdragvel)
zfac=tanh(-di.z(j)/ps.drag_lag_depth);
% how many super ensembles need to be averaged
nsmooth=sum(di.time_jul>(di.time_jul(j)-ps.drag_lag_tim/24/60) & di.time_jul<di.time_jul(j));
ii=j-[0:fix(nsmooth*zfac)];
ii=ii(find(ii>0));
if length(ii)<2
ii=j;
end
% smooth w less in time
iw=ii(1:fix(end/10));
if length(iw)<2
iw=j;
end
% set the assumed ctd velocity as the sum between ship vel and projected w-vel
ctdvel(j)=meannan(shipvel(ii))+meannan(wctdf(iw));
end
else
ctdvel=shipvel*NaN;
end
% remove last data bin and one bin off the bottom and surface
if existf(p,'zbottom'),
zbottom=p.zbottom;
else
zbottom=1e32;
end
[jmax,jbott]=max(izv);
%#!# the following line is wrong, as izv>(ps.dz*(jmax-1) is never true;
%#!# the code is supposed to remove the "last data bin" (whatever that means)
%#!# but it does not do anything
ii=find(izv<(ps.dz) | izv>(ps.dz*(jmax-1)) | izv>(zbottom-ps.dz));
wm(ii)=0;
% remove bad or weakly constained data
jm=max(jprof);
disp([' remove ',int2str(sum(wm<ps.weightmin)),' constaints below minimum weight ']);
ii=find(isnan(d) | isnan(wm) | wm<ps.weightmin);
d(ii)=[];
if exist('dbot','var')
dbot(ii)=[];
end
izv(ii)=[];
jprof(ii)=[];
jbin(ii)=[];
wm(ii)=[];
% remove empty profiles at the end
ii=(max(jprof)+1):jm;
tim(ii)=[];
ctdvel(ii)=[];
shipvel(ii)=[];
zctd(ii)=[];
wctd(ii)=[];
slat(ii)=[];
slon(ii)=[];
dtiv(ii)=[];
if exist('bvel','var')
bvel(ii)=[];
bvels(ii)=[];
end
[jmax,jbott]=max(izv);
% prepare some output arrays
if existf(p,'name')
dr.name=p.name;
end
dr.date=gregoria(median(tim));
dr.lat=p.lat;
dr.lon=p.lon;
% set up main matrices for inversion
A1=lainseta(jprof,1);
A2=lainseta(izv,ps.dz);
[nt,nz]=size(A2);
% resulting depth vector
%z =([1:nz]'-.5)*ps.dz;
z =([1:nz]')*ps.dz;
A1o=A1;
A2o=A2;
do=d;
%### add weights to data
[A2,A1,d,idoc,iupc]=lainweig(A2,A1,d,wm);
% make sure time and depth dimension are different
if size(A2,2)==size(A1,2)
disp('change dimension of A2')
A2=[A2,A2(:,1)*0];
A2o=[A2o,A2o(:,1)*0];
[nt,nz]=size(A2);
z =([1:nz]')*ps.dz;
end
% save constraints
de.ocean_constraints=full(sum(abs(A2)));
de.ctd_constraints=full(sum(abs(A1)));
de.type_constraints='Velocity ';
%### smooth ocean and CTD velocity profiles
disp(' smooth Ocean velocity profile')
[A2,A1,d]=lainsmoo(A2,A1,d,ps.smoofac);
disp(' smooth CTD velocity profile')
[A1,A2,d]=lainsmoo(A1,A2,d,ps.smoofac);
de.ocean_constraints=[de.ocean_constraints;sum(abs(A2))-(de.ocean_constraints)];
de.ctd_constraints=[de.ctd_constraints;sum(abs(A1))-(de.ctd_constraints)];
de.type_constraints=[de.type_constraints;'Smoothing '];
%### add bottom track constraint
if exist('bvel','var')
if sum(isfinite(bvel))>0
btweight=ps.velerr./bvels;
[A2,A1,d,ubot,iubot]=lainbott(dbot.*wm,bvel,btweight,A2,A1,d,ps.botfac);
if length(ubot)>0
dr.zbot=z(iubot);
dr.ubot=real(ubot(:,1));
dr.vbot=imagnan(ubot(:,1));
dr.uerrbot=ubot(:,2);
% make length of array unique
while length(dr.zbot)==nt | length(dr.zbot)==nz
dr.zbot=[dr.zbot;nan];
dr.ubot=[dr.ubot;nan];
dr.vbot=[dr.vbot;nan];
dr.uerrbot=[dr.uerrbot;nan];
end
else
psbot=0;
end
if ps.botfac>0
disp([' weight for bottom track is (ps.botfac) ',num2str(ps.botfac)])
psbot=1;
else
disp(' not enought bottom track data')
psbot=0;
end
else
disp(' no bottom track data')
psbot=0;
end
else
psbot=0;
end
de.ocean_constraints=[de.ocean_constraints;sum(abs(A2))-sum(de.ocean_constraints)];
de.ctd_constraints=[de.ctd_constraints;sum(abs(A1))-sum(de.ctd_constraints)];
de.type_constraints=[de.type_constraints;'Bottomtrk '];
%### add ship adcp constraint
if existf(di,'svel')==1
if sum(isfinite(di.svel(:,1)))>2
[p,A2,A1,d,ds]=lainsadcp(p,di.svel,A2,A1,d,ps.dz,ps.sadcpfac,ps.velerr);
if length(ds.z_sadcp)>1
dr.z_sadcp=ds.z_sadcp;
dr.u_sadcp=ds.u_sadcp;
dr.v_sadcp=ds.v_sadcp;
dr.uerr_sadcp=ds.uerr_sadcp;
% make length of array unique
if existf(dr,'zbot'), lzbot=length(dr.zbot); else, lzbot=0; end
while length(dr.z_sadcp)==nt | length(dr.z_sadcp)==nz | ...
length(dr.z_sadcp)==lzbot
dr.z_sadcp=[dr.z_sadcp;nan];
dr.u_sadcp=[dr.u_sadcp;nan];
dr.v_sadcp=[dr.v_sadcp;nan];
dr.uerr_sadcp=[dr.uerr_sadcp;nan];
end
end
if ps.sadcpfac>0
disp([' weight for SADCP vel is (ps.sadcpfac) ',num2str(ps.sadcpfac)])
else
disp(' no sadcp used')
ps.sadcpfac=0;
end
else
disp(' not enough SADCP data')
ps.sadcpfac=0;
end
else
disp(' no SADCP data')
ps.sadcpfac=0;
end
de.ocean_constraints=[de.ocean_constraints;sum(abs(A2))-sum(de.ocean_constraints)];
de.ctd_constraints=[de.ctd_constraints;sum(abs(A1))-sum(de.ctd_constraints)];
de.type_constraints=[de.type_constraints;'Ship ADCP '];
%### add barotropic constraint
% check if position data exist
uship_a=p.uship+sqrt(-1)*p.vship;
if (abs(uship_a)==0 & p.lat==0 & p.lon==0) | ~isfinite(p.lon+p.lat)
disp(' no position data ');
ps.barofac=0;
ps.dragfac=0;
end
if ps.barofac>0
fac=ps.velerr/ps.barvelerr;
% need to increase if parts of profile are missing
ii=find(di.dt>3*mean(di.dt));
if length(ii)>1
facgap=sum(di.dt(ii))/sum(di.dt);
disp([' lainbaro: ',int2str(facgap*100),'% of profile have no useful data '])
fac=fac*(1-tanh(facgap/0.15));
end
if ~isfinite(fac), fac=1; end
[A2,A1,d]=lainbaro(A2,A1,d,uship_a,dtiv,ps.barofac*fac);
end
de.ocean_constraints=[de.ocean_constraints;sum(abs(A2))-sum(de.ocean_constraints)];
de.ctd_constraints=[de.ctd_constraints;sum(abs(A1))-sum(de.ctd_constraints)];
de.type_constraints=[de.type_constraints;'GPS naviga'];
%### small deep ocean velocity
if sum(smallfac(:,2))>0
[A2,A1,d]=lainsmal(A2,A1,d,smallfac);
end
de.ocean_constraints=[de.ocean_constraints;sum(abs(A2))-sum(de.ocean_constraints)];
de.ctd_constraints=[de.ctd_constraints;sum(abs(A1))-sum(de.ctd_constraints)];
de.type_constraints=[de.type_constraints;'Small flow'];
%### check if problem is well constrained
if (psbot==0 & ps.barofac==0 & ps.sadcpfac==0),
disp(' no bottom no barotropic no SADCP constrain => will set mean U,V to zero')
dr.onlyshear=1;
[A2,A1,d]=lainocean(A2,A1,d);
elseif existf(dr,'onlyshear')
dr=rmfield(dr,'onlyshear');
end
de.ocean_constraints=[de.ocean_constraints;sum(abs(A2))-sum(de.ocean_constraints)];
de.ctd_constraints=[de.ctd_constraints;sum(abs(A1))-sum(de.ctd_constraints)];
de.type_constraints=[de.type_constraints;'Zero mean '];
%### add CTD drag constraint
if ps.dragfac>0
disp([' weight for drag is (ps.dragfac) ',num2str(ps.dragfac)])
[A2,A1,d]=laindrag(A2,A1,d,ctdvel,ps.dragfac);
end
de.ocean_constraints=[de.ocean_constraints;sum(abs(A2))-sum(de.ocean_constraints)];
de.ctd_constraints=[de.ctd_constraints;sum(abs(A1))-sum(de.ctd_constraints)];
de.type_constraints=[de.type_constraints;'Drag Model'];
[ld,a1l]=size(A1);
[ld,a2l]=size(A2);
disp([' ready for inversion length of d: ',num3str(ld,6,0)])
disp([' (CTD vel) length of A1: ',num3str(a1l,6,0)])
disp([' (ocean vel) length of A2: ',num3str(a2l,6,0)])
[uocean,uctd]=lainsolv(A2,A1,d,ps.solve);
% save results in output array
dr.z=z;
dr.u=real(uocean(:,1));
dr.v=imagnan(uocean(:,1));
if size(uocean,2)>1
dr.uerr=(uocean(:,2));
end
dr.nvel=full(sum(A2o)');
dr.ubar=mean(dr.u);
dr.vbar=mean(dr.v);
dr.tim=tim;
dr.tim_hour=(tim-fix(tim(1)))*24;
if sum(isfinite(slat+slon))>0 | ps.dragfac>0
dr.shiplon=slon;
dr.shiplat=slat;
dr.xship=(slon-slon(1))*60*1852*cos(meannan(slat)*pi/180);
dr.yship=(slat-slat(1))*60*1852;
dr.uship=real(shipvel);
dr.vship=imagnan(shipvel);
end
dr.zctd=zctd;
dr.wctd=-wctd;
dr.uctd=-real(uctd(:,1))';
dr.vctd=-imagnan(uctd(:,1))';
if size(uctd,2)>1
dr.uctderr=(uctd(:,2))';
end
dt=diff(tim)*24*3600;
dt=mean([0,dt;dt,0]);
ctdpos=-cumsum(uctd(:,1).*dt').';
dr.xctd=real(ctdpos);
dr.yctd=imagnan(ctdpos);
figure(7)
clf
orient tall
tim=dr.tim-fix(dr.tim(1));
uctd_drag=real(ctdvel);
vctd_drag=imagnan(ctdvel);
% plot some of the drag fac results
subplot(421)
plot(tim,dr.uctd,'-b','linewidth',1.8)
hold on
if existf(dr,'uship')==1
plot(tim,dr.uship,'g-')
end
ut=interp1(dr.z,dr.u,-dr.zctd);
ii=find(isfinite(ut+dr.uctd));
if length(ii)>5
ii=ii(round((end*0.33):(end*0.67)));
co=corrcoef([ut(ii)',dr.uctd(ii)']);
ps.ucorr=co(1,2);
else
ps.ucorr=NaN;
end
plot(tim,ut,'-k','linewidth',1.2)
if ps.dragfac~=0
plot(tim,uctd_drag,'-r')
end
grid
title('ctd(-b) ship(-g) drag(-r) ocean(-k)')
ylabel(['U [m/s] corr: ',num2str(ps.ucorr)])
axis tight
subplot(422)
plot(tim,dr.vctd,'-b','linewidth',1.8)
hold on
grid
if existf(dr,'uship')==1
plot(tim,dr.vship,'g-')
end
vt=interp1(dr.z,dr.v,-dr.zctd);
ii=find(isfinite(vt+dr.vctd));
if length(ii)>5
ii=ii(round((end*0.33):(end*0.67)));
co=corrcoef([vt(ii)',dr.vctd(ii)']);
ps.vcorr=co(1,2);
else
ps.vcorr=NaN;
end
plot(tim,vt,'-k','linewidth',1.2)
if ps.dragfac~=0
plot(tim,vctd_drag,'-r')
end
ylabel(['V [m/s] corr: ',num2str(ps.vcorr)])
axis tight
if sum(shipvel)~=prod(shipvel)
subplot(423)
shippos=cumsum(shipvel.*dt);
ctddist=abs(shippos-ctdpos);
plot(tim,ctddist,'linewidth',2)
grid
ylabel('CTD distance from ship [m]')
xlabel('time in days')
axis tight
end
subplot(424)
plot(tim,dr.wctd,'linewidth',2)
grid
ylabel('CTD vertical velocity')
xlabel('time in days')
axis tight
subplot(212)
xctd=dr.xctd;
yctd=dr.yctd;
ii=fix(linspace(1,length(xctd),10));
[m,ib]=min(dr.zctd);
plot(xctd,yctd,'linewidth',2)
hold on
plot(xctd(ii),yctd(ii),'r.','markersize',10)
plot(xctd(ib),yctd(ib),'g+','markersize',9)
if existf(dr,'xship')
plot(dr.xship,dr.yship,'-g',dr.xship(ii),dr.yship(ii),'k.','markersize',10)
plot([xctd(ii);dr.xship(ii)],[yctd(ii); dr.yship(ii)],'-y','linewidth',0.5)
xlabel('CTD-position (blue) and ship (green) east-west [m]')
else
xlabel('CTD-position east-west [m]')
end
text(xctd(ib),yctd(ib),'bottom')
axis equal
axis tight
text(xctd(1),yctd(1),'start')
ylabel('north-south [m]')
title([p.name,' Results from Drag Fac : ',num2str(ps.dragfac)])
grid
set(gca,'fontsize',10)
streamer([p.name,' Figure 7']);
orient tall
pause(0.01)
% compute velcoity error
der=geterr(ps,dr,di,iplot);
if size(uocean,2)>1 % second pass: uerr is set
% note that dr.uerr is entirely re-scaled %%##%%
dr.uerr=dr.uerr/medianan(dr.uerr)*medianan(sqrt(der.u_oce_s.^2+der.v_oce_s.^2));
else % first pass: error is stddev of uocean(z)
dr.uerr= sqrt(der.u_oce_s.^2+der.v_oce_s.^2)';
end
% compute mean target strengt profile
% compute mean range profile
dr.range=dr.z*NaN;
if length(di.izu)>0
dr.range_do=dr.z*NaN;
dr.range_up=dr.z*NaN;
end
dr.ts=dr.z*NaN;
dr.ts_out=dr.z*NaN;
for i=1:length(dr.z)
ii=find(abs(dr.z(i)+di.z)<2*ps.dz);
if length(ii)>1
dr.ts(i)=mean(di.tsd(ii));
dr.ts_out(i)=mean(di.tsd_out(ii));
zd=abs(di.izm(di.izd,1)-di.z(1));
range=zd(sum(isfinite(di.rw(di.izd(2:end),ii)))+1);
dr.range(i)=medianan(range,ceil(length(ii)/4));
if length(di.izu)>0
zd=abs(di.izm(di.izu,1)-di.z(1));
range=zd(sum(isfinite(di.rw(di.izu(1:(end-1)),ii)))+1);
dr.range_up(i)=medianan(range,ceil(length(ii)/4));
dr.range_do(i)=dr.range(i);
dr.range(i)=dr.range_up(i)+dr.range_do(i);
end
end
end
if nargout>2
% prepare some output for error analysis
de.R=sum(abs(d)>0)/(nt+nz) ;
de.d=d;
de.wm=wm;
if exist('bvel','var')
de.bvel=bvel;
de.bvels=bvels;
end
de.uocean=uocean;
de.uctd=uctd;
de.dfit=[A2,A1]*[uocean(:,1);uctd(:,1)];
de.A=[A2,A1];
de.A1o=A1o;
de.A2o=A2o;
de.do=do;
de.jprof=jprof;
de.jbin=jbin;
end
%### solve up and down cast seperately
if ps.down_up
baroclinfac=10;
%down trace
disp(' solve only down trace')
A1s=A1(idoc,:);
ii=find(full(sum(A1s))==0);
A1s(:,ii)=[];
A2s=A2(idoc,:);
ds=d(idoc);
disp(' smooth Ocean velocity profile')
[A2s,A1s,ds]=lainsmoo(A2s,A1s,ds,ps.smoofac);
disp(' smooth CTD velocity profile')
[A1s,A2s,ds]=lainsmoo(A1s,A2s,ds,ps.smoofac);
% add zero mean constrain
A1s=[A1s;A1s(1,:)*0];
A2s=[A2s;A2s(1,:)*0+baroclinfac];
ds=[ds;0];
[uocean_do,uctd_do]=lainsolv(A2s,A1s,ds);
ii=find(abs(uocean_do(:,1))>5);
if length(ii)>0
disp([' found ',int2str(length(ii)),' big > 5m/s down cast UOCEAN:'])
uocean_do(ii)=NaN;
end
dr.u_do=real(uocean_do(:,1));
dr.v_do=imagnan(uocean_do(:,1));
if length(iupc)>5,
%up trace
disp(' solve only up trace')
A1s=A1(iupc,:);
ii=find(full(sum(A1s))==0);
A1s(:,ii)=[];
A2s=A2(iupc,:);
ds=d(iupc);
disp(' smooth Ocean velocity profile')
[A2s,A1s,ds]=lainsmoo(A2s,A1s,ds,ps.smoofac);
disp(' smooth CTD velocity profile')
[A1s,A2s,ds]=lainsmoo(A1s,A2s,ds,ps.smoofac);
% add zero mean constrain
A1s=[A1s;A1s(1,:)*0];
A2s=[A2s;A2s(1,:)*0+baroclinfac];
ds=[ds;0];
[uocean_up,uctd_up]=lainsolv(A2s,A1s,ds);
ii=find(abs(uocean_up(:,1))>5);
if length(ii)>0
disp([' found ',int2str(length(ii)),' big >5 m/s up cast UOCEAN:'])
uocean_up(ii)=NaN;
end
else
uocean_up=uocean_do*NaN;
end
dr.u_up=real(uocean_up(:,1));
dr.v_up=imagnan(uocean_up(:,1));
u_bias = meannan(dr.u_do-dr.u_up);
v_bias = meannan(dr.v_do-dr.v_up);
if abs(u_bias) > 0.02 | abs(v_bias) > 0.02
warn=(sprintf(' large up/down bias (u=%.2fm/s; v=%.2fm/s) --- GPS problems?',...
u_bias,v_bias));
p.warnp(size(p.warnp,1)+1,1:length(warn))=warn;
disp([' WARNING: ' warn]);
end
end
% compute pressure from depth
dr.p=press(dr.z);
% ----------------------------------------------------------
function [Ao,Ac,d]=lainbaro(Ao,Ac,d,uship,dt,w)
%function [Ao,Ac,d]=lainbaro(Ao,Ac,d,uship,dt,w)
%
% add barotropic constrain
% [uship] ship velocity over the cast from GPS positions
% w strength of constrain
%
%
[li,ljo]=size(Ao);
[li,ljc]=size(Ac);
if nargin<6, w=1; end
% normalize weights
% fac=li./(sum(dt)*ljc);
% fac=1/(sum(dt));
range=(full(sum(abs(Ac))));
fac=sqrt(sum(range));
disp([' normalized barotropic constrain weight: ',num2str(w)])
disp([' mean individual ctd velocity weight : ',num2str(mean(w*fac))])
Ac=[Ac;(1:ljc)*0+dt/sum(dt)*w*fac];
Ao(li+1,1)=0;
d=[d;-1*uship*w*fac];
% ----------------------------------------------------------
function [Ao,Ac,d]=lainocean(Ao,Ac,d,w)
%function [Ao,Ac,d]=lainocean(Ao,Ac,d,w)
%
% set barotropic constrain to be no mean ocean velocity
% w strength of constrain
%
%
[li,ljo]=size(Ao);
[li,ljc]=size(Ac);
if nargin<4, w=1; end
% normalize weights
fac=mean(sum(Ao));
disp(' add no mean flow constraint')
Ao=[Ao;(1:ljo)*0+w*fac];
Ac(li+1,1)=0;
d=[d;0];
%-------------------------------------------------------------------
function [Ao,Ac,d,ubot,ibot]=lainbott(dbvel,bvel,bvelw,Ao,Ac,d,botfacin)
% function [Ao,Ac,d,ubot,ibot]=lainbott(dbvel,bvel,bvelw,Ao,Ac,d,botfacin)
%
% add bottom track to the two parts of the A matrix
%
% dbvel = bottom track velocity array
% bvel = bottom track velocity
% bvelw = ratio between bottom track std and vel error
%
% d = cell velocity data
% Ao = ocean matrix
% Ac = ctd matrix
% botfac = nominal weight for bottom track
%
% also compute bottom track velocity
% ubot (:,1) = mean velocity
% ubot (:,2) = error
% ibot depth index
[li,ljo]=size(Ao);
if nargin<5, botfacin=1; end
ibot=find(isfinite(dbvel));
dbot=(d(ibot)-dbvel(ibot));
Aob=Ao(ibot,:);
if nargout>3
disp(' bottom inversion ')
% minimum number of estimates
nmin=3;
Ab=Aob;
s=sum(Ab>0)>=(nmin);
ibot=find(s==1);
% remove empty vertical depth
id=find(s==0);
Ab(:,id)=[];
if length(ibot)>1
% solve
[m,me]=lesqfit(dbot,Ab);
ubot=[full(m),abs(full(me))];
velerr=min(abs(full(me)));
botfac=botfacin./ubot(:,2)*velerr;
igood=find(ubot(:,2)<2*median(ubot(:,2)));
ubot=ubot(igood,:);
ibot=ibot(igood);
else
disp(' not enough bottom track data for bottom inversion ')
ubot=[];
botfac=botfacin;
end
end
% scaling needs more work
% range in fraction of total profile
% range=5*full(sum(Ac~=0))/size(Ac,2);
% use sqrt of contraints
iokbot=find(isfinite(bvel));
if length(iokbot>0)
range=sqrt(full(sum(abs(Ac))));
botfac=bvelw(iokbot)*botfacin.*range(iokbot);
Acb=zeros(length(botfac),size(Ac,2));
for i=1:length(botfac)
Acb(i,iokbot(i))=botfac(i);
db(i,1)=bvel(iokbot(i))*botfac(i);
end
d=[d;db];
Ac=[Ac;Acb];
Ao(length(d),1)=0;
disp([' ',int2str(length(botfac)),...
' bottom track ctd-vel weights of about : ',num2str(mean(botfac))])
else
disp('no finite bottom track velocities ')
end
%-------------------------------------------------------------------
function [p,Ao,Ac,d,ds]=lainsadcp(p,svel,Ao,Ac,d,dz,sadcpfac,velerr)
% function [p,Ao,Ac,d,ds]=lainsadcp(p,svel,Ao,Ac,d,dz,sadcpfac,velerr)
%
% add SADCP to the two parts of the A matrix
%
% svel = ship ADCP data velocity profile
%
% d = cell velocity data
% Ao = ocean matrix
% Ac = ctd matrix
% sadcpfac = weight for SADCP velocity
% velerr = velocity error for LADCP super ensemble
%
[li,ljo]=size(Ao);
if nargin<7, velerr=0; end
if nargin<6, sadcpfac=1; end
zsadcp=abs(svel(:,1));
dsadcp=svel(:,2)+sqrt(-1)*svel(:,3);
ii=find(isfinite(dsadcp));
zsadcp=zsadcp(ii);
dsadcp=dsadcp(ii);
verr=svel(ii,4);
% weight according to scatter in mean SADCP profile
% first replace small or no error with large error
ij=find(~isfinite(verr) | verr==0);
verr(ij)=maxnan(verr);
% If there are more than one profile use std for weights
if size(svel,1)>3
if ~(velerr>0)
fac0=sort(verr);
velerr=mean(fac0(1:ceil(end*0.2)));
end
fac=velerr./verr;
else
fac=dsadcp*0+1;
end
% Apply constraint
if sadcpfac>0
% weight down by factor of 2 because it directly influence velocity (up and down)
fac2=sqrt(full(sum(abs(Ao))))/2;
for i=1:length(dsadcp)
% sort to depth
jz=round(zsadcp(i)/dz);
jz=min(max(jz,1),ljo);
% fac(i)=fac(i)*fac2(jz);
Ao(li+i,jz)=sadcpfac*fac(i);
d=[d;dsadcp(i)*sadcpfac.*fac(i)];
end
Ac(length(d),1)=0;
disp([' mean sadcp weight : ',num2str(mean(sadcpfac.*fac))])
if nargout>3
iok=find(fac>0.1);
if length(iok) < length(fac)
disp(sprintf(' %d out of %d SADCP profiles removed because of low weight',...
length(fac)-length(iok),length(fac)));
end
if isempty(iok)
warn=(' all SADCP values removed because of low weight');
p.warnp(size(p.warnp,1)+1,1:length(warn))=warn;
disp([' WARNING: ' warn]);
end
ds.z_sadcp=zsadcp(iok);
ds.u_sadcp=real(dsadcp(iok));
ds.v_sadcp=imagnan(dsadcp(iok));
ds.uerr_sadcp=verr(iok);