1 | %'merge_proj': concatene several fields from series, project on a polar grid |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % function ParamOut=merge_proj(Param) |
---|
4 | %------------------------------------------------------------------------ |
---|
5 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
6 | % |
---|
7 | %OUTPUT |
---|
8 | % ParamOut: sets options in the GUI series.fig needed for the function |
---|
9 | % |
---|
10 | %INPUT: |
---|
11 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
---|
12 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
---|
13 | % when Param.Action.RUN=0 (as activated when the current Action is selected |
---|
14 | % in series), the function ouput paramOut set the activation of the needed GUI elements |
---|
15 | % |
---|
16 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to |
---|
17 | % see the current structure Param) |
---|
18 | % .InputTable: cell of input file names, (several lines for multiple input) |
---|
19 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
---|
20 | % .OutputSubDir: name of the subdirectory for data outputs |
---|
21 | % .OutputDirExt: directory extension for data outputs |
---|
22 | % .Action: .ActionName: name of the current activated function |
---|
23 | % .ActionPath: path of the current activated function |
---|
24 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct |
---|
25 | % .RUN =0 for GUI input, =1 for function activation |
---|
26 | % .RunMode='local','background', 'cluster': type of function use |
---|
27 | % |
---|
28 | % .IndexRange: set the file or frame indices on which the action must be performed |
---|
29 | % .FieldTransform: .TransformName: name of the selected transform function |
---|
30 | % .TransformPath: path of the selected transform function |
---|
31 | % .InputFields: sub structure describing the input fields withfields |
---|
32 | % .FieldName: name(s) of the field |
---|
33 | % .VelType: velocity type |
---|
34 | % .FieldName_1: name of the second field in case of two input series |
---|
35 | % .VelType_1: velocity type of the second field in case of two input series |
---|
36 | % .Coord_y: name of y coordinate variable |
---|
37 | % .Coord_x: name of x coordinate variable |
---|
38 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
---|
39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
40 | |
---|
41 | %======================================================================= |
---|
42 | % Copyright 2008-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
43 | % http://www.legi.grenoble-inp.fr |
---|
44 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
45 | % |
---|
46 | % This file is part of the toolbox UVMAT. |
---|
47 | % |
---|
48 | % UVMAT is free software; you can redistribute it and/or modify |
---|
49 | % it under the terms of the GNU General Public License as published |
---|
50 | % by the Free Software Foundation; either version 2 of the license, |
---|
51 | % or (at your option) any later version. |
---|
52 | % |
---|
53 | % UVMAT is distributed in the hope that it will be useful, |
---|
54 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
55 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
56 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
57 | %======================================================================= |
---|
58 | |
---|
59 | function ParamOut=merge_proj_polar(Param) |
---|
60 | |
---|
61 | %% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed |
---|
62 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
63 | ParamOut.AllowInputSort='on';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
64 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
65 | ParamOut.NbSlice='on'; %nbre of slices ('off' by default) |
---|
66 | ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
67 | ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
68 | ParamOut.FieldTransform = 'on';%can use a transform function |
---|
69 | ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only) |
---|
70 | ParamOut.ProjObject='off';%can use projection object(option 'off'/'on', |
---|
71 | ParamOut.Mask='off';%can use mask option (option 'off'/'on', 'off' by default) |
---|
72 | ParamOut.OutputDirExt='.polar';%set the output dir extension |
---|
73 | ParamOut.OutputFileMode='NbInput';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice |
---|
74 | %check the input files |
---|
75 | ParamOut.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1) |
---|
76 | first_j=[]; |
---|
77 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end |
---|
78 | PairString=''; |
---|
79 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
80 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
81 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
82 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
83 | if ~exist(FirstFileName,'file') |
---|
84 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
---|
85 | end |
---|
86 | return |
---|
87 | end |
---|
88 | |
---|
89 | %%%% specific input parameters |
---|
90 | % calculate the positions on which to interpolate |
---|
91 | radius_ref=450;% radius of the mountain top |
---|
92 | radius_shifted=-130:2:130;% radius shifted by the radius of the origin at the topography summit |
---|
93 | radius=radius_ref+radius_shifted;%radius from centre of the tank |
---|
94 | azimuth_arclength=(-150:2:400);%azimuth in arc length at origin position |
---|
95 | azimuth=pi/2-azimuth_arclength/radius_ref;%azimuth in radian |
---|
96 | [Radius,Azimuth]=meshgrid(radius,azimuth); |
---|
97 | XI=Radius.*cos(Azimuth);% set of x axis of the points where interpolqtion needs to be done |
---|
98 | YI=Radius.*sin(Azimuth)-radius_ref;% set of y axis of the points where interpolqtion needs to be done |
---|
99 | FieldNames={'vec(U,V)';'curl(U,V)';'div(U,V)'}; |
---|
100 | HeadData.ListVarName= {'radius','azimuth'} ; |
---|
101 | HeadData.VarDimName={'radius','azimuth'}; |
---|
102 | HeadData.VarAttribute={'coord_y','coord_x'} ; |
---|
103 | HeadData.radius=radius_shifted; |
---|
104 | HeadData.azimuth=azimuth_arclength; |
---|
105 | thresh2=16; % square of the interpolation range |
---|
106 | |
---|
107 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
108 | ParamOut=[]; %default output |
---|
109 | RUNHandle=[]; |
---|
110 | WaitbarHandle=[]; |
---|
111 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
112 | checkrun=1; |
---|
113 | if ischar(Param) |
---|
114 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
115 | checkrun=0; |
---|
116 | else |
---|
117 | hseries=findobj(allchild(0),'Tag','series'); |
---|
118 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
119 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
120 | end |
---|
121 | |
---|
122 | %% root input file type |
---|
123 | RootPath=Param.InputTable(:,1); |
---|
124 | RootFile=Param.InputTable(:,3); |
---|
125 | SubDir=Param.InputTable(:,2); |
---|
126 | %NomType=Param.InputTable(:,4); |
---|
127 | FileExt=Param.InputTable(:,5); |
---|
128 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
129 | %%%%%%%%%%%% |
---|
130 | % The cell array filecell is the list of input file names, while |
---|
131 | % filecell{iview,fileindex}: |
---|
132 | % iview: line in the table corresponding to a given file series |
---|
133 | % fileindex: file index within the file series, |
---|
134 | % i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j |
---|
135 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
136 | %%%%%%%%%%%% |
---|
137 | % NbSlice=1;%default |
---|
138 | % if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
139 | % NbSlice=Param.IndexRange.NbSlice; |
---|
140 | % end |
---|
141 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
142 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
143 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
144 | NbField=NbField_j*NbField_i; %total number of fields |
---|
145 | |
---|
146 | %% define the name for result file (with path=RootPath{1}) |
---|
147 | OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files |
---|
148 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},'.nc','_1',i1_series{1}(1)); |
---|
149 | CheckOverwrite=1;%default |
---|
150 | if isfield(Param,'CheckOverwrite') |
---|
151 | CheckOverwrite=Param.CheckOverwrite; |
---|
152 | end |
---|
153 | if ~CheckOverwrite && exist(OutputFile,'file') |
---|
154 | disp(['existing output file ' OutputFile ' already exists, skip to next field']) |
---|
155 | return% skip iteration if the mode overwrite is desactivated and the result file already exists |
---|
156 | end |
---|
157 | |
---|
158 | if ~isfield(Param,'InputFields') |
---|
159 | Param.InputFields.FieldName=''; |
---|
160 | end |
---|
161 | |
---|
162 | %% prepare output file content |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | %% determine the file type on each line from the first input file |
---|
167 | ImageTypeOptions={'image','multimage','mmreader','video','cine_phantom'}; |
---|
168 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
169 | for iview=1:NbView |
---|
170 | if ~exist(filecell{iview,1}','file') |
---|
171 | disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun) |
---|
172 | return |
---|
173 | end |
---|
174 | [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1}); |
---|
175 | FileType{iview}=FileInfo{iview}.FileType; |
---|
176 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
177 | if CheckImage{iview} |
---|
178 | ParamIn{iview}=MovieObject{iview}; |
---|
179 | else |
---|
180 | ParamIn{iview}=Param.InputFields; |
---|
181 | end |
---|
182 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
183 | if ~isempty(j1_series{iview}) |
---|
184 | frame_index{iview}=j1_series{iview}; |
---|
185 | else |
---|
186 | frame_index{iview}=i1_series{iview}; |
---|
187 | end |
---|
188 | end |
---|
189 | if NbView >1 && max(cell2mat(CheckImage))>0 && ~isfield(Param,'ProjObject') |
---|
190 | disp_uvmat('ERROR','projection on a common grid is needed to concatene images: use a Projection Object of type ''plane'' with ProjMode=''interp_lin''',checkrun) |
---|
191 | return |
---|
192 | end |
---|
193 | |
---|
194 | %% calibration data and timing: read the ImaDoc files |
---|
195 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
196 | if size(time,1)>1 |
---|
197 | diff_time=max(max(diff(time))); |
---|
198 | if diff_time>0 |
---|
199 | disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun) |
---|
200 | end |
---|
201 | end |
---|
202 | if ~isempty(errormsg) |
---|
203 | disp_uvmat('WARNING',errormsg,checkrun) |
---|
204 | end |
---|
205 | time=mean(time,1); %averaged time taken for the merged field |
---|
206 | |
---|
207 | %% height z |
---|
208 | % position of projection plane |
---|
209 | |
---|
210 | ProjObjectCoord=XmlData{1}.GeometryCalib.SliceCoord; |
---|
211 | CoordUnit=XmlData{1}.GeometryCalib.CoordUnit; |
---|
212 | for iview =2:numel(XmlData) |
---|
213 | if ~(isfield(XmlData{iview},'GeometryCalib')&& isequal(XmlData{iview}.GeometryCalib.SliceCoord,ProjObjectCoord))... |
---|
214 | disp('error: geometric calibration missing or inconsistent plane positions') |
---|
215 | return |
---|
216 | end |
---|
217 | end |
---|
218 | |
---|
219 | |
---|
220 | %% coordinate transform or other user defined transform |
---|
221 | transform_fct='';%default fct handle |
---|
222 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
223 | currentdir=pwd; |
---|
224 | cd(Param.FieldTransform.TransformPath) |
---|
225 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
226 | cd (currentdir) |
---|
227 | if isfield(Param,'TransformInput') |
---|
228 | for iview=1:NbView |
---|
229 | XmlData{iview}.TransformInput=Param.TransformInput; |
---|
230 | end |
---|
231 | end |
---|
232 | end |
---|
233 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
234 | % EDIT FROM HERE |
---|
235 | |
---|
236 | %% check the validity of input file types |
---|
237 | for iview=1:NbView |
---|
238 | if ~isequal(CheckNc{iview},1) |
---|
239 | disp_uvmat('ERROR','input files needs to be in netcdf (extension .nc)',checkrun) |
---|
240 | return |
---|
241 | end |
---|
242 | end |
---|
243 | |
---|
244 | % %% output file type |
---|
245 | if isempty(j1_series{1}) |
---|
246 | NomTypeOut='_1'; |
---|
247 | else |
---|
248 | NomTypeOut='_1_1'; |
---|
249 | end |
---|
250 | RootFileOut=RootFile{1}; |
---|
251 | for iview=2:NbView |
---|
252 | if ~strcmp(RootFile{iview},RootFile{1}) |
---|
253 | RootFileOut='mproj'; |
---|
254 | break |
---|
255 | end |
---|
256 | end |
---|
257 | |
---|
258 | |
---|
259 | %% MAIN LOOP ON FIELDS |
---|
260 | %%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
261 | % for i_slice=1:NbSlice |
---|
262 | % index_slice=i_slice:NbSlice:NbField;% select file indices of the slice |
---|
263 | % NbFiles=0; |
---|
264 | % nbmissing=0; |
---|
265 | |
---|
266 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
267 | tstart=tic; %used to record the computing time |
---|
268 | TimeData=[]; |
---|
269 | |
---|
270 | for index=1:NbField |
---|
271 | disp(['index=' num2str(index)]) |
---|
272 | disp(['ellapsed time ' num2str(toc(tstart)/60,4) ' minutes']) |
---|
273 | update_waitbar(WaitbarHandle,index/NbField) |
---|
274 | if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
275 | disp('program stopped by user') |
---|
276 | return |
---|
277 | end |
---|
278 | |
---|
279 | %% generating the name of the merged field |
---|
280 | i1=i1_series{1}(index); |
---|
281 | if ~isempty(i2_series{end}) |
---|
282 | i2=i2_series{end}(index); |
---|
283 | else |
---|
284 | i2=i1; |
---|
285 | end |
---|
286 | j1=1; |
---|
287 | j2=1; |
---|
288 | if ~isempty(j1_series{1}) |
---|
289 | j1=j1_series{1}(index); |
---|
290 | if ~isempty(j2_series{end}) |
---|
291 | j2=j2_series{end}(index); |
---|
292 | else |
---|
293 | j2=j1; |
---|
294 | end |
---|
295 | end |
---|
296 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFileOut,'.nc',NomTypeOut,i1,i2,j1,j2); |
---|
297 | |
---|
298 | |
---|
299 | %% z position |
---|
300 | ZIndex=mod(i1_series{1}(index)-1,NbSlice_calib{1})+1;%Zindex for phys transform |
---|
301 | ZPosNew=ProjObjectCoord(ZIndex,3); |
---|
302 | if index==1 |
---|
303 | ZPos=ZPosNew; |
---|
304 | else |
---|
305 | if ZPosNew~=ZPos |
---|
306 | disp('inconsistent z positions in the series') |
---|
307 | return |
---|
308 | end |
---|
309 | end |
---|
310 | % radius of the topography section at z position |
---|
311 | ind_mask=[]; |
---|
312 | if ZPos<20 |
---|
313 | TopoRadius=40*sin(acos((20+ZPos)/40)); |
---|
314 | ind_mask=(XI'.*XI'+YI'.*YI')<TopoRadius*TopoRadius;% indidces of data to mask |
---|
315 | end |
---|
316 | if ~CheckOverwrite && exist(OutputFile,'file') |
---|
317 | disp(['existing output file ' OutputFile ' already exists, skip to next field']) |
---|
318 | continue% skip iteration if the mode overwrite is desactivated and the result file already exists |
---|
319 | end |
---|
320 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
321 | Data=cell(1,NbView);%initiate the set Data |
---|
322 | timeread=zeros(1,NbView); |
---|
323 | for iview=1:NbView |
---|
324 | %% reading input file(s) |
---|
325 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},ParamIn{iview},frame_index{iview}(index)); |
---|
326 | if ~isempty(errormsg) |
---|
327 | disp_uvmat('ERROR',['ERROR in merge_proj/read_field/' errormsg],checkrun) |
---|
328 | return |
---|
329 | end |
---|
330 | ListVar=Data{iview}.ListVarName; |
---|
331 | for ilist=1:numel(ListVar) |
---|
332 | Data{iview}.(ListVar{ilist})=double(Data{iview}.(ListVar{ilist}));% transform all fields in double before all operations |
---|
333 | end |
---|
334 | % get the time defined in the current file if not already defined from the xml file |
---|
335 | if ~isempty(time) && isfield(Data{iview},'Time') |
---|
336 | timeread(iview)=Data{iview}.Time; |
---|
337 | end |
---|
338 | if ~isempty(NbSlice_calib) |
---|
339 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
340 | end |
---|
341 | |
---|
342 | %% transform the input field (e.g; phys) if requested (no transform involving two input fields) |
---|
343 | if ~isempty(transform_fct) |
---|
344 | if nargin(transform_fct)>=2 |
---|
345 | Data{iview}=transform_fct(Data{iview},XmlData{iview}); |
---|
346 | else |
---|
347 | Data{iview}=transform_fct(Data{iview}); |
---|
348 | end |
---|
349 | end |
---|
350 | |
---|
351 | %% calculate tps coefficients |
---|
352 | Data{iview}=tps_coeff_field(Data{iview},1); |
---|
353 | |
---|
354 | %% projection on the polar grid |
---|
355 | [DataOut,VarAttribute,errormsg]=calc_field_tps(Data{iview}.Coord_tps,Data{iview}.NbCentre,Data{iview}.SubRange,... |
---|
356 | cat(3,Data{iview}.U_tps,Data{iview}.V_tps),FieldNames,cat(3,XI,YI)); |
---|
357 | % set to NaN interpolation points which are too far from any initial data (more than 2 CoordMesh) |
---|
358 | Coord=permute(Data{iview}.Coord_tps,[1 3 2]); |
---|
359 | Coord=reshape(Coord,size(Coord,1)*size(Coord,2),2); |
---|
360 | if exist('scatteredInterpolant','file')%recent Matlab versions |
---|
361 | F=scatteredInterpolant(Coord,Coord(:,1),'nearest'); |
---|
362 | G=scatteredInterpolant(Coord,Coord(:,2),'nearest'); |
---|
363 | else |
---|
364 | F=TriScatteredInterp(Coord,Coord(:,1),'nearest'); |
---|
365 | G=TriScatteredInterp(Coord,Coord(:,2),'nearest'); |
---|
366 | end |
---|
367 | Distx=F(XI,YI)-XI;% diff of x coordinates with the nearest measurement point |
---|
368 | Disty=G(XI,YI)-YI;% diff of y coordinates with the nearest measurement point |
---|
369 | Dist=Distx.*Distx+Disty.*Disty; |
---|
370 | ListVarName=(fieldnames(DataOut))'; |
---|
371 | VarDimName=cell(size(ListVarName)); |
---|
372 | ProjData{iview}=HeadData; |
---|
373 | ProjData{iview}.ListVarName= [ProjData{iview}.ListVarName ListVarName]; |
---|
374 | ProjData{iview}.VarDimName={'radius','azimuth'}; |
---|
375 | ProjData{iview}.VarAttribute=[{'coord_x'} {'coord_y'} VarAttribute]; |
---|
376 | for ivar=1:numel(ListVarName) |
---|
377 | ProjData{iview}.VarDimName{ivar+2}={'radius','azimuth'}; |
---|
378 | VarName=ListVarName{ivar}; |
---|
379 | if ~isempty(thresh2) |
---|
380 | DataOut.(VarName)(Dist>thresh2)=NaN;% put to NaN interpolated positions further than RangeInterp from initial data |
---|
381 | end |
---|
382 | ProjData{iview}.(VarName)=(DataOut.(VarName))'; |
---|
383 | end |
---|
384 | |
---|
385 | end |
---|
386 | %%%%%%%%%%%%%%%% END LOOP ON VIEWS %%%%%%%%%%%%%%%% |
---|
387 | |
---|
388 | %% merge the NbView fields |
---|
389 | [MergeData,errormsg]=merge_field(ProjData); |
---|
390 | if ~isempty(errormsg) |
---|
391 | disp_uvmat('ERROR',errormsg,checkrun); |
---|
392 | return |
---|
393 | end |
---|
394 | |
---|
395 | |
---|
396 | %% time of the merged field: take the average of the different views |
---|
397 | if ~isempty(time) |
---|
398 | timeread=time(index); |
---|
399 | elseif ~isempty(find(timeread))% time defined from ImaDoc |
---|
400 | timeread=mean(timeread(timeread~=0));% take average over times form the files (when defined) |
---|
401 | else |
---|
402 | timeread=index;% take time=file index |
---|
403 | end |
---|
404 | |
---|
405 | %% rotating the velocity vectors to the local axis of the polatr coordinates |
---|
406 | Unew=MergeData.U.*sin(Azimuth')-MergeData.V.*cos(Azimuth'); |
---|
407 | Vnew=MergeData.U.*cos(Azimuth')+MergeData.V.*sin(Azimuth'); |
---|
408 | if ~isempty(ind_mask) |
---|
409 | Unew(ind_mask)=NaN; |
---|
410 | Vnew(ind_mask)=NaN; |
---|
411 | MergeData.curl(ind_mask)=NaN; |
---|
412 | MergeData.div(ind_mask)=NaN; |
---|
413 | end |
---|
414 | [npy,npx]=size(Unew); |
---|
415 | |
---|
416 | %% create the output file for the first iteration of the loop |
---|
417 | if isempty(TimeData)% initialize |
---|
418 | TimeData.ListGlobalAttribute={'Conventions','Project','CoordUnit','TimeUnit','ZPos','Time'}; |
---|
419 | TimeData.Conventions='uvmat'; |
---|
420 | TimeData.Project='2016_Circumpolar'; |
---|
421 | TimeData.CoordUnit='cm'; |
---|
422 | TimeData.TimeUnit='s'; |
---|
423 | TimeData.ZPos=ZPos; |
---|
424 | TimeData.ListVarName={'radius','azimuth','U','V','curl','div'}; |
---|
425 | TimeData.VarDimName={'radius','azimuth',{'radius','azimuth'},{'radius','azimuth'}... |
---|
426 | {'radius','azimuth'},{'radius','azimuth'}}; |
---|
427 | TimeData.VarAttribute{1}.Role=''; |
---|
428 | TimeData.VarAttribute{2}.Role=''; |
---|
429 | TimeData.VarAttribute{3}.Role='vector_x'; |
---|
430 | TimeData.VarAttribute{4}.Role='vector_y'; |
---|
431 | TimeData.VarAttribute{5}.Role='scalar'; |
---|
432 | TimeData.VarAttribute{6}.Role='scalar'; |
---|
433 | |
---|
434 | TimeData.radius=radius_shifted; |
---|
435 | TimeData.azimuth=azimuth_arclength; |
---|
436 | end |
---|
437 | |
---|
438 | %% append data to the netcdf file for next iterations |
---|
439 | TimeData.Time=timeread; |
---|
440 | TimeData.U=Unew; |
---|
441 | TimeData.V=Vnew; |
---|
442 | TimeData.curl=MergeData.curl; |
---|
443 | TimeData.div=MergeData.div; |
---|
444 | |
---|
445 | error=struct2nc(OutputFile,TimeData);%save result file |
---|
446 | if isempty(error) |
---|
447 | disp(['output file ' OutputFile ' written']) |
---|
448 | else |
---|
449 | disp(error) |
---|
450 | end |
---|
451 | ellapsed_time=toc(tstart); |
---|
452 | disp(['total ellapsed time ' num2str(ellapsed_time/60,2) ' minutes']) |
---|
453 | end |
---|
454 | |
---|
455 | ellapsed_time=toc(tstart); |
---|
456 | disp(['total ellapsed time ' num2str(ellapsed_time/60,2) ' minutes']) |
---|
457 | disp([ num2str(ellapsed_time/(60*NbField),3) ' minutes per iteration']) |
---|
458 | |
---|
459 | %'merge_field': concatene fields |
---|
460 | %------------------------------------------------------------------------ |
---|
461 | function [MergeData,errormsg]=merge_field(Data) |
---|
462 | %% default output |
---|
463 | if isempty(Data)||~iscell(Data) |
---|
464 | MergeData=[]; |
---|
465 | return |
---|
466 | end |
---|
467 | errormsg=''; |
---|
468 | MergeData=Data{1};% merged field= first field by default, reproduces the global attributes of the first field |
---|
469 | NbView=length(Data); |
---|
470 | if NbView==1% if there is only one field, just reproduce it in MergeData |
---|
471 | return |
---|
472 | end |
---|
473 | |
---|
474 | %% group the variables (fields of 'Data') in cells of variables with the same dimensions |
---|
475 | [CellInfo,NbDim,errormsg]=find_field_cells(Data{1}); |
---|
476 | if ~isempty(errormsg) |
---|
477 | return |
---|
478 | end |
---|
479 | |
---|
480 | %LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
481 | for icell=1:length(CellInfo) |
---|
482 | if NbDim(icell)~=1 % skip field cells which are of dim 1 |
---|
483 | switch CellInfo{icell}.CoordType |
---|
484 | case 'scattered' %case of input fields with unstructured coordinates: just concatene data |
---|
485 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
486 | VarName=Data{1}.ListVarName{ivar}; |
---|
487 | for iview=2:NbView |
---|
488 | MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)]; |
---|
489 | end |
---|
490 | end |
---|
491 | case 'grid' %case of fields defined on a structured grid |
---|
492 | FFName=''; |
---|
493 | if isfield(CellInfo{icell},'VarIndex_errorflag') && ~isempty(CellInfo{icell}.VarIndex_errorflag) |
---|
494 | FFName=Data{1}.ListVarName{CellInfo{icell}.VarIndex_errorflag};% name of errorflag variable |
---|
495 | MergeData.ListVarName(CellInfo{icell}.VarIndex_errorflag)=[];%remove error flag variable in MergeData (will use NaN instead) |
---|
496 | MergeData.VarDimName(CellInfo{icell}.VarIndex_errorflag)=[]; |
---|
497 | MergeData.VarAttribute(CellInfo{icell}.VarIndex_errorflag)=[]; |
---|
498 | end |
---|
499 | % select good data on each view |
---|
500 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
501 | VarName=Data{1}.ListVarName{ivar}; |
---|
502 | for iview=1:NbView |
---|
503 | if isempty(FFName) |
---|
504 | check_bad=isnan(Data{iview}.(VarName));%=0 for NaN data values, 1 else |
---|
505 | else |
---|
506 | check_bad=isnan(Data{iview}.(VarName)) | Data{iview}.(FFName)~=0;%=0 for NaN or error flagged data values, 1 else |
---|
507 | end |
---|
508 | Data{iview}.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag |
---|
509 | if iview==1 |
---|
510 | %MergeData.(VarName)=Data{1}.(VarName);% initiate MergeData with the first field |
---|
511 | MergeData.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag |
---|
512 | NbAver=~check_bad;% initiate NbAver: the nbre of good data for each point |
---|
513 | elseif size(Data{iview}.(VarName))~=size(MergeData.(VarName)) |
---|
514 | errormsg='sizes of the input matrices do not agree, need to interpolate on a common grid using a projection object'; |
---|
515 | return |
---|
516 | else |
---|
517 | MergeData.(VarName)=MergeData.(VarName) +double(Data{iview}.(VarName));%add data |
---|
518 | NbAver=NbAver + ~check_bad;% add 1 for good data, 0 else |
---|
519 | end |
---|
520 | end |
---|
521 | MergeData.(VarName)(NbAver~=0)=MergeData.(VarName)(NbAver~=0)./NbAver(NbAver~=0);% take average of defined data at each point |
---|
522 | MergeData.(VarName)(NbAver==0)=NaN;% set to NaN the points with no good data |
---|
523 | end |
---|
524 | end |
---|
525 | % if isempty(FFName) |
---|
526 | % FFName='FF'; |
---|
527 | % end |
---|
528 | % MergeData.(FFName)(NbAver~=0)=0;% flag to 1 undefined summed data |
---|
529 | % MergeData.(FFName)(NbAver==0)=1;% flag to 1 undefined summed data |
---|
530 | end |
---|
531 | end |
---|
532 | |
---|
533 | |
---|
534 | |
---|