1 | %'merge_proj': concatene several fields from series, can project them on a regular grid in phys coordinates |
---|
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 | function ParamOut=merge_proj(Param) |
---|
42 | |
---|
43 | %% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed |
---|
44 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
45 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
46 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
47 | ParamOut.NbSlice='off'; %nbre of slices ('off' by default) |
---|
48 | ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
49 | ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
50 | ParamOut.FieldTransform = 'on';%can use a transform function |
---|
51 | ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only) |
---|
52 | ParamOut.ProjObject='on';%can use projection object(option 'off'/'on', |
---|
53 | ParamOut.Mask='on';%can use mask option (option 'off'/'on', 'off' by default) |
---|
54 | ParamOut.OutputDirExt='.mproj';%set the output dir extension |
---|
55 | 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 |
---|
56 | filecell=get_file_series(Param);%check existence of the first input file |
---|
57 | if ~exist(filecell{1,1},'file') |
---|
58 | msgbox_uvmat('WARNING','the first input file does not exist') |
---|
59 | elseif isequal(size(Param.InputTable,1),1) && ~isfield(Param,'ProjObject') |
---|
60 | msgbox_uvmat('WARNING','You may need a projection object of type plane for merge_proj') |
---|
61 | end |
---|
62 | return |
---|
63 | end |
---|
64 | |
---|
65 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
66 | ParamOut=[]; %default output |
---|
67 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
68 | checkrun=1; |
---|
69 | if ischar(Param) |
---|
70 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
71 | checkrun=0; |
---|
72 | end |
---|
73 | hseries=findobj(allchild(0),'Tag','series'); |
---|
74 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
75 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
76 | |
---|
77 | %% define the directory for result file (with path=RootPath{1}) |
---|
78 | OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files |
---|
79 | |
---|
80 | if ~isfield(Param,'InputFields') |
---|
81 | Param.InputFields.FieldName=''; |
---|
82 | end |
---|
83 | |
---|
84 | %% root input file type |
---|
85 | RootPath=Param.InputTable(:,1); |
---|
86 | RootFile=Param.InputTable(:,3); |
---|
87 | SubDir=Param.InputTable(:,2); |
---|
88 | NomType=Param.InputTable(:,4); |
---|
89 | FileExt=Param.InputTable(:,5); |
---|
90 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
91 | %%%%%%%%%%%% |
---|
92 | % The cell array filecell is the list of input file names, while |
---|
93 | % filecell{iview,fileindex}: |
---|
94 | % iview: line in the table corresponding to a given file series |
---|
95 | % fileindex: file index within the file series, |
---|
96 | % 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 |
---|
97 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
98 | %%%%%%%%%%%% |
---|
99 | % NbSlice=1;%default |
---|
100 | % if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
101 | % NbSlice=Param.IndexRange.NbSlice; |
---|
102 | % end |
---|
103 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
104 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
105 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
106 | NbField=NbField_j*NbField_i; %total number of fields |
---|
107 | |
---|
108 | %% determine the file type on each line from the first input file |
---|
109 | ImageTypeOptions={'image','multimage','mmreader','video'}; |
---|
110 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
111 | for iview=1:NbView |
---|
112 | if ~exist(filecell{iview,1}','file') |
---|
113 | disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun) |
---|
114 | return |
---|
115 | end |
---|
116 | [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1}); |
---|
117 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
118 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
119 | if ~isempty(j1_series{iview}) |
---|
120 | frame_index{iview}=j1_series{iview}; |
---|
121 | else |
---|
122 | frame_index{iview}=i1_series{iview}; |
---|
123 | end |
---|
124 | end |
---|
125 | |
---|
126 | %% calibration data and timing: read the ImaDoc files |
---|
127 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
128 | if size(time,1)>1 |
---|
129 | diff_time=max(max(diff(time))); |
---|
130 | if diff_time>0 |
---|
131 | disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': time of first series chosen in result'],checkrun) |
---|
132 | end |
---|
133 | end |
---|
134 | |
---|
135 | %% coordinate transform or other user defined transform |
---|
136 | transform_fct='';%default fct handle |
---|
137 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
138 | currentdir=pwd; |
---|
139 | cd(Param.FieldTransform.TransformPath) |
---|
140 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
141 | cd (currentdir) |
---|
142 | end |
---|
143 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
144 | % EDIT FROM HERE |
---|
145 | |
---|
146 | %% check the validity of input file types |
---|
147 | for iview=1:NbView |
---|
148 | if ~isequal(CheckImage{iview},1)&&~isequal(CheckNc{iview},1) |
---|
149 | disp_uvmat('ERROR','input set of input series: need either netcdf either image series',checkrun) |
---|
150 | return |
---|
151 | end |
---|
152 | end |
---|
153 | |
---|
154 | %% output file type |
---|
155 | if min(cell2mat(CheckImage))==1 && (~Param.CheckObject || strcmp(Param.ProjObject.Type,'plane')) |
---|
156 | FileExtOut='.png'; %image output (input and proj result = image) |
---|
157 | for iview=1:NbView |
---|
158 | BitDepth(iview)=FileInfo{iview}.BitDepth; |
---|
159 | end |
---|
160 | BitDepth=max(BitDepth); |
---|
161 | else |
---|
162 | FileExtOut='.nc'; %netcdf output |
---|
163 | end |
---|
164 | NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series |
---|
165 | RootFileOut=RootFile{1}; |
---|
166 | for iview=2:NbView |
---|
167 | if ~strcmp(RootFile{iview},RootFile{1}) |
---|
168 | RootFileOut='mproj'; |
---|
169 | break |
---|
170 | end |
---|
171 | end |
---|
172 | |
---|
173 | %% mask (TODO: case of multilevels) |
---|
174 | MaskData=cell(NbView,1); |
---|
175 | if Param.CheckMask |
---|
176 | for iview=1:numel(Param.MaskTable) |
---|
177 | if exist(Param.MaskTable{iview},'file') |
---|
178 | [MaskData{iview},tild,errormsg] = read_field(Param.MaskTable{iview},'image'); |
---|
179 | if ~isempty(transform_fct) && nargin(transform_fct)>=2 |
---|
180 | MaskData{iview}=transform_fct(MaskData{iview},XmlData{iview}); |
---|
181 | end |
---|
182 | end |
---|
183 | end |
---|
184 | end |
---|
185 | |
---|
186 | %% Set field names and velocity types |
---|
187 | %use Param.InputFields for all views |
---|
188 | |
---|
189 | %% MAIN LOOP ON FIELDS |
---|
190 | %%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
191 | % for i_slice=1:NbSlice |
---|
192 | % index_slice=i_slice:NbSlice:NbField;% select file indices of the slice |
---|
193 | % NbFiles=0; |
---|
194 | % nbmissing=0; |
---|
195 | |
---|
196 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
197 | for index=1:NbField |
---|
198 | update_waitbar(WaitbarHandle,index/NbField) |
---|
199 | if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
200 | disp('program stopped by user') |
---|
201 | return |
---|
202 | end |
---|
203 | |
---|
204 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
205 | Data=cell(1,NbView);%initiate the set Data |
---|
206 | nbtime=0; |
---|
207 | for iview=1:NbView |
---|
208 | %% reading input file(s) |
---|
209 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},Param.InputFields,frame_index{iview}(index)); |
---|
210 | if ~isempty(errormsg) |
---|
211 | disp(['ERROR in merge_proj/read_field/' errormsg]) |
---|
212 | return |
---|
213 | end |
---|
214 | timeread(iview)=0; |
---|
215 | if isfield(Data{iview},'Time') |
---|
216 | timeread(iview)=Data{iview}.Time; |
---|
217 | nbtime=nbtime+1; |
---|
218 | end |
---|
219 | if ~isempty(NbSlice_calib) |
---|
220 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
221 | end |
---|
222 | |
---|
223 | %% transform the input field (e.g; phys) if requested (no transform involving two input fields) |
---|
224 | if ~isempty(transform_fct) |
---|
225 | if nargin(transform_fct)>=2 |
---|
226 | Data{iview}=transform_fct(Data{iview},XmlData{iview}); |
---|
227 | else |
---|
228 | Data{iview}=transform_fct(Data{iview}); |
---|
229 | end |
---|
230 | end |
---|
231 | |
---|
232 | %% calculate tps coefficients if needed |
---|
233 | check_proj_tps= isfield(Param,'ProjObject')&&~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'interp_tps')&&~isfield(Data{iview},'Coord_tps'); |
---|
234 | Data{iview}=tps_coeff_field(Data{iview},check_proj_tps); |
---|
235 | |
---|
236 | %% projection on object (gridded plane) |
---|
237 | if Param.CheckObject |
---|
238 | [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject); |
---|
239 | if ~isempty(errormsg) |
---|
240 | disp(['ERROR in merge_proge/proj_field: ' errormsg]) |
---|
241 | return |
---|
242 | end |
---|
243 | end |
---|
244 | |
---|
245 | %% mask |
---|
246 | if Param.CheckMask && ~isempty(MaskData{iview}) |
---|
247 | [Data{iview},errormsg]=mask_proj(Data{iview},MaskData{iview}); |
---|
248 | end |
---|
249 | end |
---|
250 | %%%%%%%%%%%%%%%% END LOOP ON VIEWS %%%%%%%%%%%%%%%% |
---|
251 | |
---|
252 | %% merge the NbView fields |
---|
253 | MergeData=merge_field(Data); |
---|
254 | if isfield(MergeData,'Txt') |
---|
255 | disp(MergeData.Txt); |
---|
256 | return |
---|
257 | end |
---|
258 | |
---|
259 | %% time of the merged field: take the average of the different views |
---|
260 | if ~isempty(time)% time defined from ImaDoc |
---|
261 | timeread=time(:,index); |
---|
262 | end |
---|
263 | timeread=mean(timeread); |
---|
264 | |
---|
265 | %% generating the name of the merged field |
---|
266 | i1=i1_series{1}(index); |
---|
267 | if ~isempty(i2_series{end}) |
---|
268 | i2=i2_series{end}(index); |
---|
269 | else |
---|
270 | i2=i1; |
---|
271 | end |
---|
272 | j1=1; |
---|
273 | j2=1; |
---|
274 | if ~isempty(j1_series{1}) |
---|
275 | j1=j1_series{1}(index); |
---|
276 | if ~isempty(j2_series{end}) |
---|
277 | j2=j2_series{end}(index); |
---|
278 | else |
---|
279 | j2=j1; |
---|
280 | end |
---|
281 | end |
---|
282 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFileOut,FileExtOut,NomType{1},i1,i2,j1,j2); |
---|
283 | |
---|
284 | %% recording the merged field |
---|
285 | if strcmp(FileExtOut,'.png') %output as image |
---|
286 | if BitDepth==8 |
---|
287 | imwrite(uint8(MergeData.A),OutputFile,'BitDepth',8) |
---|
288 | else |
---|
289 | imwrite(uint16(MergeData.A),OutputFile,'BitDepth',16) |
---|
290 | end |
---|
291 | if index==1 |
---|
292 | %write xml calibration file, using the first file |
---|
293 | siz=size(MergeData.A); |
---|
294 | npy=siz(1); |
---|
295 | npx=siz(2); |
---|
296 | if isfield(MergeData,'coord_x') && isfield(MergeData,'coord_y') |
---|
297 | Rangx=MergeData.coord_x; |
---|
298 | Rangy=MergeData.coord_y; |
---|
299 | elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY') |
---|
300 | Rangx=[MergeData.AX(1) MergeData.AX(end)]; |
---|
301 | Rangy=[MergeData.AY(1) MergeData.AY(end)]; |
---|
302 | else |
---|
303 | Rangx=[0.5 npx-0.5]; |
---|
304 | Rangy=[npy-0.5 0.5];%default |
---|
305 | end |
---|
306 | pxcmx=(npx-1)/(Rangx(2)-Rangx(1)); |
---|
307 | pxcmy=(npy-1)/(Rangy(1)-Rangy(2)); |
---|
308 | T_x=-pxcmx*Rangx(1)+0.5; |
---|
309 | T_y=-pxcmy*Rangy(2)+0.5; |
---|
310 | GeometryCal.CalibrationType='rescale'; |
---|
311 | GeometryCal.CoordUnit=MergeData.CoordUnit; |
---|
312 | GeometryCal.focal=1; |
---|
313 | GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1]; |
---|
314 | GeometryCal.Tx_Ty_Tz=[T_x T_y 1]; |
---|
315 | ImaDoc.GeometryCalib=GeometryCal; |
---|
316 | t=struct2xml(ImaDoc); |
---|
317 | t=set(t,1,'name','ImaDoc'); |
---|
318 | save(t,[fileparts(OutputFile) '.xml']) |
---|
319 | end |
---|
320 | |
---|
321 | else |
---|
322 | MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'}; |
---|
323 | MergeData.Conventions='uvmat'; |
---|
324 | MergeData.nb_coord=2; |
---|
325 | MergeData.nb_dim=2; |
---|
326 | dt=[]; |
---|
327 | if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt) |
---|
328 | dt=Data{1}.dt; |
---|
329 | end |
---|
330 | for iview =2:numel(Data) |
---|
331 | if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt)) |
---|
332 | dt=[];%dt not the same for all fields |
---|
333 | end |
---|
334 | end |
---|
335 | if isempty(dt) |
---|
336 | MergeData.ListGlobalAttribute(6)=[]; |
---|
337 | else |
---|
338 | MergeData.dt=dt; |
---|
339 | end |
---|
340 | MergeData.Time=timeread; |
---|
341 | error=struct2nc(OutputFile,MergeData);%save result file |
---|
342 | if isempty(error) |
---|
343 | display(['output file ' OutputFile ' written']) |
---|
344 | else |
---|
345 | display(error) |
---|
346 | end |
---|
347 | end |
---|
348 | end |
---|
349 | |
---|
350 | |
---|
351 | %'merge_field': concatene fields |
---|
352 | %------------------------------------------------------------------------ |
---|
353 | function MergeData=merge_field(Data) |
---|
354 | %% default output |
---|
355 | if isempty(Data)||~iscell(Data) |
---|
356 | MergeData=[]; |
---|
357 | return |
---|
358 | end |
---|
359 | error=0; |
---|
360 | MergeData=Data{1};% merged field= first field by default, reproduces the glabal attributes of the first field |
---|
361 | NbView=length(Data); |
---|
362 | if NbView==1 |
---|
363 | return |
---|
364 | end |
---|
365 | |
---|
366 | %% group the variables (fields of 'Data') in cells of variables with the same dimensions |
---|
367 | [CellInfo,NbDim,errormsg]=find_field_cells(Data{1}); |
---|
368 | |
---|
369 | %LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
370 | for icell=1:length(CellInfo) |
---|
371 | if NbDim(icell)~=1 % skip field cells which are of dim 1 |
---|
372 | switch CellInfo{icell}.CoordType |
---|
373 | case 'scattered' %case of input fields with unstructured coordinates: just concacene data |
---|
374 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
375 | VarName=Data{1}.ListVarName{ivar}; |
---|
376 | %MergeData=Data{1};% merged field= first field by default, reproduces the glabal attributes of the first field |
---|
377 | for iview=2:NbView |
---|
378 | MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)]; |
---|
379 | end |
---|
380 | end |
---|
381 | case 'grid' %case of fields defined on a structured grid |
---|
382 | FFName=''; |
---|
383 | if ~isempty(CellInfo{icell}.VarIndex_errorflag) |
---|
384 | FFName=Data{1}.ListVarName{CellInfo{icell}.VarIndex_errorflag};% name of errorflag variable |
---|
385 | end |
---|
386 | % select good data on each view |
---|
387 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
388 | VarName=Data{1}.ListVarName{ivar}; |
---|
389 | for iview=1:NbView |
---|
390 | if isempty(FFName) |
---|
391 | check_bad=isnan(Data{iview}.(VarName));%=0 for NaN data values, 1 else |
---|
392 | else |
---|
393 | check_bad=isnan(Data{iview}.(VarName)) | Data{iview}.(FFName)~=0;%=0 for NaN or error flagged data values, 1 else |
---|
394 | end |
---|
395 | Data{iview}.(VarName)(check_bad)=0; %set to zero NaN or masked data |
---|
396 | if iview==1 |
---|
397 | MergeData.(VarName)=Data{1}.(VarName);% correct the field of MergeData |
---|
398 | NbAver=~check_bad;% initiate NbAver: the nbre of good data for each point |
---|
399 | else |
---|
400 | MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);%add data |
---|
401 | NbAver=NbAver + ~check_bad;% add 1 for good data, 0 else |
---|
402 | end |
---|
403 | end |
---|
404 | MergeData.(VarName)(NbAver~=0)=MergeData.(VarName)(NbAver~=0)./NbAver(NbAver~=0);% take average of defined data at each point |
---|
405 | end |
---|
406 | end |
---|
407 | if isempty(FFName) |
---|
408 | FFName='FF'; |
---|
409 | end |
---|
410 | MergeData.(FFName)(NbAver~=0)=0;% flag to 1 undefined summed data |
---|
411 | MergeData.(FFName)(NbAver==0)=1;% flag to 1 undefined summed data |
---|
412 | end |
---|
413 | end |
---|
414 | |
---|
415 | |
---|
416 | |
---|