NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/DPWavesProc/trunk/DPWavesProc/adcp_matlab/radialtouvw.m

Revision 229 (checked in by gdusek, 15 years ago)

--

Line 
1 function [ID,SM,EP]=radialtouvw(pressure,range,orbit,sysinfo,data_type)
2
3
4 %This code takes the loaded orbit, pressure and system info data from an
5 %ADCP waves output and computes the uvw velocities in earth coordinates.
6 %It also interpolates to remove bad data points and prepares the data
7 %structure required for running the DIWASP program.  The output will be in
8 %the structures ID, SM and EP.
9
10 %data_type is:
11 %1 to use uvw and pressure to generate the data structures,
12 %2 to use ranges
13 %3 to use radial velocity data
14
15
16
17 %make sure to load: pressure=load('pressure data'),
18 %orbit=load('orbital data'),sysinfo=load('sysinfo data') and range
19
20 %what is the transducer face height off the bottom?
21 adcpheight=0.4;
22
23 %whats the magnetic variation
24 magvar=-10;
25
26 %set up sysinfo file
27 samplesInBurst=sysinfo(3,:);
28 bin1height=sysinfo(9,:);
29 bin2height=sysinfo(10,:);
30 bin3height=sysinfo(11,:);
31 bin4height=sysinfo(12,:);
32 bin5height=sysinfo(13,:);
33 heading=sysinfo(18,:);
34 pitch=sysinfo(19,:);
35 roll=sysinfo(20,:);
36
37 %set up pressure
38 press=pressure/1000;
39
40 %find the average depth
41 avgdepth=mean(press)+adcpheight;
42
43 %set up range
44 range=range/1000;
45 meanrange=mean(range);
46 meanrange=repmat(meanrange,samplesInBurst,1);
47 dmrange=range-meanrange;
48 std_range=std(dmrange);
49
50 % Take out any bad data points in orbital data
51 ibad=find(orbit < -32000);
52 orbit(ibad) = NaN;
53 orbit=orbit/1000;
54 orbitnew=orbit;
55 %interpolate to take out any NaNs and QC for bad data in orbital data
56 std_orbit=ones(1,20);
57 for i=1:20
58    
59     %first take out any points outside of 4 std deviations
60     std_orbit(i)=nanstd(orbit(:,i));
61     ibad_std=find(abs(orbit(:,i)) > 4*std_orbit(i));
62     orbit(ibad_std,i)=NaN;
63 end
64 %find the avg std deviation for each group 4 beams
65 for i=4:4:20
66     avgstd_orbit(i-3:i)=mean(std_orbit(i-3:i));
67 end
68
69 %now remove the points outside 4avg std dev and interp
70 for i=1:20
71     ibad_std=find(abs(orbit(:,i)) > 4*avgstd_orbit(i));
72     orbit(ibad_std,i)=NaN;
73    
74     time=[1:1:length(orbit(:,i))];
75     NaNs=isnan(orbit(:,i));
76     igood=find(NaNs == 0);
77     ibad=find(NaNs == 1);
78
79     intValues= interp1(time(igood),orbit(igood,i),time(ibad),'linear','extrap');
80     orbitnew(ibad,i)=intValues;   
81 end
82
83 % Do similar QC for the range data
84 rangenew=dmrange;
85 for i=1:4
86     ibad=find(abs(dmrange(:,i)) > 4*std_range(:,i));
87     dmrange(ibad,i)=NaN;
88     time=[1:1:length(range(:,i))];
89     NaNs=isnan(dmrange(:,i));
90     igood=find(NaNs == 0);
91     ibad=find(NaNs == 1);
92
93     intValues= interp1(time(igood),dmrange(igood,i),time(ibad),'linear','extrap');
94     rangenew(ibad,i)=intValues;
95 end
96
97 dmrange=rangenew;
98    
99 %change heading, pitch, roll into degrees
100      
101 heading = heading/100;
102 pitch=pitch/100;
103 roll=roll/100;
104 pitch_out=pitch;
105 roll_out=roll;
106
107 %When converting to earth coordinates, the directions will be wrong unless we
108 %correct for bottom/up facing ADCPs, simply by rotating the roll by 180   
109 roll=roll+180;
110
111 %Set up geometry for transformation from beam to instrument
112 C1 = 1;
113 A1 = 1/(2*sin(20*pi/180));
114 B1 = 1/(4*cos(20*pi/180));
115 D1 = A1/sqrt(2);
116
117 % coordinate transformation matrix
118 CH = cos((heading+magvar)*pi/180);
119 SH = sin((heading+magvar)*pi/180);
120 CP = cos((pitch)*pi/180);
121 SP = sin((pitch)*pi/180);
122 CR = cos((roll)*pi/180);
123 SR = sin((roll)*pi/180);
124
125 %  let the matrix elements be ( a b c; d e f; g h j);
126 a = CH.*CR + SH.*SP.*SR;  b = SH.*CP; c = CH.*SR - SH.*SP.*CR;
127 d = -SH.*CR + CH.*SP.*SR; e = CH.*CP; f = -SH.*SR - CH.*SP.*CR;
128 g = -CP.*SR;              h = SP;     j = CP.*CR;
129 uno=1;
130
131 %reshape the orbital matrix to 3 dimensions
132
133 radial=reshape(orbitnew,samplesInBurst,4,5);
134    
135 %  Compute uvw velocities and change to earth coordinates
136  
137 u = C1*A1*(radial(:,1,:)-radial(:,2,:));
138 v = C1*A1*(radial(:,4,:)-radial(:,3,:));
139 w = B1*(radial(:,1,:)+radial(:,2,:)+radial(:,3,:)+radial(:,4,:));
140 error_vel = D1*(radial(:,1,:)+radial(:,2,:)-radial(:,3,:)-radial(:,4,:));
141    
142 u=reshape(u,samplesInBurst,5);
143 v=reshape(v,samplesInBurst,5);
144 w=reshape(w,samplesInBurst,5);
145 error_vel=reshape(error_vel,samplesInBurst,5);
146
147 [m,n] = size(u);
148 uno = ones(m,5);
149 unew = u.*(uno*a) + v.*(uno*b) + w.*(uno*c);
150 vnew = u.*(uno*d) + v.*(uno*e) + w.*(uno*f);
151 wnew = u.*(uno*g) + v.*(uno*h) + w.*(uno*j);
152
153 u_all = unew;v_all=vnew;w_all=wnew;
154 error_vel_all = error_vel;
155
156 %compute the original x,y,z positions for each beam for each bin to be accurate we need to take out the adcpheight
157
158 xyzpos=ones(3,4,5);
159 heights=[bin1height bin2height bin3height bin4height bin5height avgdepth]-adcpheight;
160 pos=heights*tan(20*pi/180);
161 for i=1:5
162     xyzpos(:,1,i)=[pos(i),0,heights(i)];
163     xyzpos(:,2,i)=[-pos(i),0,heights(i)];
164     xyzpos(:,3,i)=[0,pos(i),heights(i)];
165     xyzpos(:,4,i)=[0,-pos(i),heights(i)];
166 end
167
168 % set up the new coordinate transformation matrix
169 CH = cos((heading+magvar)*pi/180);
170 SH = sin((heading+magvar)*pi/180);
171 CP = cos((pitch_out)*pi/180);
172 SP = sin((pitch_out)*pi/180);
173 CR = cos((-roll_out)*pi/180);
174 SR = sin((-roll_out)*pi/180);
175
176 %  let the matrix elements be ( a b c; d e f; g h j), a slightly different
177 %  matrix from before;
178 a = CH.*CR - SH.*SP.*SR;  b = SH.*CP; c = -CH.*SR - SH.*SP.*CR;
179 d = -SH.*CR - CH.*SP.*SR; e = CH.*CP; f = SH.*SR - CH.*SP.*CR;
180 g = CP.*SR;              h = SP;     j = CP.*CR;
181
182 %transform the original x,y,z positions to the new positions accounting for
183 %heading, pitch and roll... we also add adcpheight back in
184 new_xyzpos=ones(3,4,5);
185 new_xyzpos(1,:,:)=xyzpos(1,:,:)*a+xyzpos(2,:,:)*b+xyzpos(3,:,:)*c;
186 new_xyzpos(2,:,:)=xyzpos(1,:,:)*d+xyzpos(2,:,:)*e+xyzpos(3,:,:)*f;
187 new_xyzpos(3,:,:)=xyzpos(1,:,:)*g+xyzpos(2,:,:)*h+xyzpos(3,:,:)*j+adcpheight;
188
189 xyzpositions=new_xyzpos;
190
191
192 %now we need to figure out the xyz positions at the surface for the range
193 %sin45=0.7071
194
195 binheight=heights(:,6);
196    
197 %beam 3
198 bearing=(heading+magvar)*(pi/180);
199 distfromz=binheight*tan((20-pitch_out)*(pi/180));
200 xpos=sin(bearing)*distfromz;
201 ypos=cos(bearing)*distfromz;
202
203 distroll=binheight*tan(roll_out*(pi/180));
204 beam3xpos=xpos+0.7071*distroll;
205 beam3ypos=ypos+0.7071*distroll;
206    
207 %beam 4
208 bearing=bearing+pi;
209 distfromz=binheight*tan((20+pitch_out)*(pi/180));
210 xpos=sin(bearing)*distfromz;
211 ypos=cos(bearing)*distfromz;
212
213 distroll=binheight*tan(roll_out*(pi/180));
214 beam4xpos=xpos+0.7071*distroll;
215 beam4ypos=ypos+0.7071*distroll;
216
217 %beam 1
218 bearing=bearing-pi/2;
219 distfromz=binheight*tan((20+roll_out)*(pi/180));
220 xpos=sin(bearing)*distfromz;
221 ypos=cos(bearing)*distfromz;
222
223 distroll=binheight*tan(pitch_out*(pi/180));
224 beam1xpos=xpos+0.7071*distroll;
225 beam1ypos=ypos+0.7071*distroll;
226
227 %beam2
228 bearing=bearing+pi;
229 distfromz=binheight*tan((20-roll_out)*(pi/180));
230 xpos=sin(bearing)*distfromz;
231 ypos=cos(bearing)*distfromz;
232
233 distroll=binheight*tan(pitch_out*(pi/180));
234 beam2xpos=xpos+0.7071*distroll;
235 beam2ypos=ypos+0.7071*distroll;
236
237 xypositions(:,:)=[beam1xpos beam2xpos beam3xpos beam4xpos; beam1ypos beam2ypos beam3ypos beam4ypos];
238    
239 %Put into structures for DIWASP
240
241
242 %sampling frequency
243 ID.fs=2;
244 %depth
245 ID.depth=(adcpheight+mean(press));
246 %the spectral matrix structure
247 SM.freqs=[0.01:0.01:0.4];
248 SM.dirs=[0:2:360];
249 SM.xaxisdir= 90;
250 %the estimation parameter
251 EP.method= 'EMEP';
252 EP.iter=100;
253
254
255 if data_type == 1
256     %For uvw and pressure for bins 2,3,4
257     % the datatypes
258     ID.datatypes={'pres' 'velx' 'vely' 'velz' 'velx' 'vely' 'velz' 'velx' 'vely' 'velz'};
259     % the layout
260     ID.layout = [ 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0;
261     adcpheight bin2height bin2height bin2height bin3height bin3height bin3height bin4height bin4height bin4height];
262     % the data
263     ID.data = horzcat(press, u_all(:,2), v_all(:,2), w_all(:,2), u_all(:,3), v_all(:,3), w_all(:,3), u_all(:,4), v_all(:,4), w_all(:,4));
264    
265 elseif data_type == 2
266     %For the ranges of each beam
267     % the datatypes
268     ID.datatypes={'elev' 'elev' 'elev' 'elev'};
269     % the layout
270     ID.layout = [xypositions(1,1) xypositions(1,2) xypositions(1,3) xypositions(1,4);
271     xypositions(2,1) xypositions(2,2) xypositions(2,3) xypositions(2,4);
272     avgdepth avgdepth avgdepth avgdepth];
273     % the data
274     ID.data = horzcat(dmrange(:,1), dmrange(:,2), dmrange(:,3), dmrange(:,4));
275
276 elseif data_type == 3
277     % For the radial velocities bins 2,3,4
278     % the datatypes
279     ID.datatypes={'radial' 'radial' 'radial' 'radial' 'radial' 'radial' 'radial' 'radial' 'radial' 'radial' 'radial' 'radial'};
280     % the layout
281     ID.layout = [xyzpositions(1,1,2) xyzpositions(1,2,2) xyzpositions(1,3,2) xyzpositions(1,4,2) xyzpositions(1,1,3) xyzpositions(1,2,3) xyzpositions(1,3,3) xyzpositions(1,4,3) xyzpositions(1,1,4) xyzpositions(1,2,4) xyzpositions(1,3,4) xyzpositions(1,4,4);
282     xyzpositions(2,1,2) xyzpositions(2,2,2) xyzpositions(2,3,2) xyzpositions(2,4,2) xyzpositions(2,1,3) xyzpositions(2,2,3) xyzpositions(2,3,3) xyzpositions(2,4,3) xyzpositions(2,1,4) xyzpositions(2,2,4) xyzpositions(2,3,4) xyzpositions(2,4,4);
283     xyzpositions(3,1,2) xyzpositions(3,2,2) xyzpositions(3,3,2) xyzpositions(3,4,2) xyzpositions(3,1,3) xyzpositions(3,2,3) xyzpositions(3,3,3) xyzpositions(3,4,3) xyzpositions(3,1,4) xyzpositions(3,2,4) xyzpositions(3,3,4) xyzpositions(3,4,4)];
284     % the data
285     ID.data = horzcat(orbitnew(:,5),orbitnew(:,6),orbitnew(:,7),orbitnew(:,8),orbitnew(:,9),orbitnew(:,10),orbitnew(:,11),orbitnew(:,12),orbitnew(:,13),orbitnew(:,14),orbitnew(:,15),orbitnew(:,16));
286
287     % the layout using bins 3,4,5
288     %ID.layout = [xyzpositions(1,1,5) xyzpositions(1,2,5) xyzpositions(1,3,5) xyzpositions(1,4,5) xyzpositions(1,1,4) xyzpositions(1,2,4) xyzpositions(1,3,4) xyzpositions(1,4,4) xyzpositions(1,1,3) xyzpositions(1,2,3) xyzpositions(1,3,3) xyzpositions(1,4,3);
289    % xyzpositions(2,1,5) xyzpositions(2,2,5) xyzpositions(2,3,5) xyzpositions(2,4,5) xyzpositions(2,1,4) xyzpositions(2,2,4) xyzpositions(2,3,4) xyzpositions(2,4,4) xyzpositions(2,1,3) xyzpositions(2,2,3) xyzpositions(2,3,3) xyzpositions(2,4,3);
290    % xyzpositions(3,1,5) xyzpositions(3,2,5) xyzpositions(3,3,5) xyzpositions(3,4,5) xyzpositions(3,1,4) xyzpositions(3,2,4) xyzpositions(3,3,4) xyzpositions(3,4,4) xyzpositions(3,1,3) xyzpositions(3,2,3) xyzpositions(3,3,3) xyzpositions(3,4,3)];
291     % the data
292    % ID.data = horzcat(orbitnew(:,17),orbitnew(:,18),orbitnew(:,19),orbitnew(:,20),orbitnew(:,13),orbitnew(:,14),orbitnew(:,15),orbitnew(:,16),orbitnew(:,9),orbitnew(:,10),orbitnew(:,11),orbitnew(:,12));
293
294 end
295
296
297
298
299  
300
301
302
303
304
305
Note: See TracBrowser for help on using the browser.