[573] | 1 | %'merge_proj': concatene several fields from series, can project them on a regular grid in phys coordinates |
---|
[457] | 2 | %------------------------------------------------------------------------ |
---|
[464] | 3 | % function ParamOut=merge_proj(Param) |
---|
[457] | 4 | %------------------------------------------------------------------------ |
---|
| 5 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
[169] | 6 | % |
---|
| 7 | %OUTPUT |
---|
[596] | 8 | % ParamOut: sets options in the GUI series.fig needed for the function |
---|
[169] | 9 | % |
---|
| 10 | %INPUT: |
---|
[457] | 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 |
---|
[596] | 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 |
---|
[169] | 15 | % |
---|
[596] | 16 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to |
---|
| 17 | % see the current structure Param) |
---|
[457] | 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 |
---|
[474] | 21 | % .OutputDirExt: directory extension for data outputs |
---|
[457] | 22 | % .Action: .ActionName: name of the current activated function |
---|
| 23 | % .ActionPath: path of the current activated function |
---|
[596] | 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 | % |
---|
[457] | 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 |
---|
[596] | 32 | % .FieldName: name(s) of the field |
---|
[457] | 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 |
---|
[596] | 36 | % .Coord_y: name of y coordinate variable |
---|
| 37 | % .Coord_x: name of x coordinate variable |
---|
[457] | 38 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
---|
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 40 | |
---|
[810] | 41 | %======================================================================= |
---|
[1126] | 42 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[810] | 43 | % http://www.legi.grenoble-inp.fr |
---|
[1127] | 44 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
[810] | 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 | |
---|
[462] | 59 | function ParamOut=merge_proj(Param) |
---|
[29] | 60 | |
---|
[606] | 61 | %% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed |
---|
[592] | 62 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
[606] | 63 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
[632] | 64 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
[606] | 65 | ParamOut.NbSlice='off'; %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='one';% 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 |
---|
[1134] | 69 | %%%%% list of possible transform functions (needed only for compilation) |
---|
[1137] | 70 | if 0==1 %never satisfied but trigger compilation with the appropriate transform functions ('eval' inactive for compilation) |
---|
| 71 | phys |
---|
| 72 | phys_polar |
---|
| 73 | sub_field |
---|
| 74 | end |
---|
[1135] | 75 | |
---|
[1134] | 76 | ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions |
---|
| 77 | %%%%%%%% |
---|
[606] | 78 | ParamOut.ProjObject='on';%can use projection object(option 'off'/'on', |
---|
[635] | 79 | ParamOut.Mask='on';%can use mask option (option 'off'/'on', 'off' by default) |
---|
[592] | 80 | ParamOut.OutputDirExt='.mproj';%set the output dir extension |
---|
[605] | 81 | 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 |
---|
[716] | 82 | %check the input files |
---|
[905] | 83 | ParamOut.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1) |
---|
[716] | 84 | first_j=[]; |
---|
| 85 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end |
---|
| 86 | PairString=''; |
---|
| 87 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
| 88 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
| 89 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
| 90 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
| 91 | if ~exist(FirstFileName,'file') |
---|
| 92 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
---|
[605] | 93 | end |
---|
[606] | 94 | return |
---|
[29] | 95 | end |
---|
[1134] | 96 | |
---|
[457] | 97 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
[632] | 98 | ParamOut=[]; %default output |
---|
[905] | 99 | RUNHandle=[]; |
---|
| 100 | WaitbarHandle=[]; |
---|
[592] | 101 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
| 102 | checkrun=1; |
---|
[457] | 103 | if ischar(Param) |
---|
[592] | 104 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
| 105 | checkrun=0; |
---|
[905] | 106 | else |
---|
| 107 | hseries=findobj(allchild(0),'Tag','series'); |
---|
| 108 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
| 109 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
[374] | 110 | end |
---|
[959] | 111 | |
---|
[624] | 112 | %% define the directory for result file (with path=RootPath{1}) |
---|
| 113 | OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files |
---|
| 114 | |
---|
[550] | 115 | if ~isfield(Param,'InputFields') |
---|
| 116 | Param.InputFields.FieldName=''; |
---|
| 117 | end |
---|
[457] | 118 | |
---|
[960] | 119 | %% root input file(s) name, type and index series |
---|
[457] | 120 | RootPath=Param.InputTable(:,1); |
---|
| 121 | RootFile=Param.InputTable(:,3); |
---|
| 122 | SubDir=Param.InputTable(:,2); |
---|
| 123 | FileExt=Param.InputTable(:,5); |
---|
[960] | 124 | |
---|
| 125 | hdisp=disp_uvmat('WAITING...','checking the file series',checkrun); |
---|
| 126 | % gives the series of input file names and indices set by the input parameters: |
---|
[374] | 127 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
[474] | 128 | % filecell{iview,fileindex}: |
---|
[457] | 129 | % iview: line in the table corresponding to a given file series |
---|
[960] | 130 | % fileindex: file index with i and j reshaped as a 1D array |
---|
[457] | 131 | % 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 |
---|
| 132 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
[1134] | 133 | if ~isempty(hdisp),delete(hdisp),end %end the waiting display |
---|
[960] | 134 | |
---|
[606] | 135 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
| 136 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
| 137 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
| 138 | NbField=NbField_j*NbField_i; %total number of fields |
---|
[457] | 139 | |
---|
[673] | 140 | %% determine the file type on each line from the first input file |
---|
[1134] | 141 | NcTypeOptions={'netcdf','civx','civdata','pivdata_fluidimage'}; |
---|
[606] | 142 | for iview=1:NbView |
---|
[462] | 143 | if ~exist(filecell{iview,1}','file') |
---|
[668] | 144 | disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun) |
---|
[462] | 145 | return |
---|
| 146 | end |
---|
[784] | 147 | [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1}); |
---|
[783] | 148 | FileType{iview}=FileInfo{iview}.FileType; |
---|
[1030] | 149 | if strcmp(FileType{iview},'civdata')&& ~isfield(Param.InputFields,'VelType') |
---|
| 150 | FileType{iview}='netcdf'; |
---|
| 151 | end |
---|
[1033] | 152 | CheckImage{iview}=strcmp(FileInfo{iview}.FieldType,'image');% =1 for images |
---|
[880] | 153 | if CheckImage{iview} |
---|
| 154 | ParamIn{iview}=MovieObject{iview}; |
---|
| 155 | else |
---|
| 156 | ParamIn{iview}=Param.InputFields; |
---|
| 157 | end |
---|
[462] | 158 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
| 159 | if ~isempty(j1_series{iview}) |
---|
| 160 | frame_index{iview}=j1_series{iview}; |
---|
| 161 | else |
---|
| 162 | frame_index{iview}=i1_series{iview}; |
---|
| 163 | end |
---|
[457] | 164 | end |
---|
[739] | 165 | if NbView >1 && max(cell2mat(CheckImage))>0 && ~isfield(Param,'ProjObject') |
---|
| 166 | 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) |
---|
| 167 | return |
---|
| 168 | end |
---|
[457] | 169 | |
---|
| 170 | %% calibration data and timing: read the ImaDoc files |
---|
[470] | 171 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
| 172 | if size(time,1)>1 |
---|
[457] | 173 | diff_time=max(max(diff(time))); |
---|
[474] | 174 | if diff_time>0 |
---|
[676] | 175 | disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun) |
---|
[457] | 176 | end |
---|
| 177 | end |
---|
[676] | 178 | if ~isempty(errormsg) |
---|
[829] | 179 | disp_uvmat('WARNING',errormsg,checkrun) |
---|
[676] | 180 | end |
---|
| 181 | time=mean(time,1); %averaged time taken for the merged field |
---|
[457] | 182 | |
---|
[409] | 183 | %% coordinate transform or other user defined transform |
---|
[606] | 184 | transform_fct='';%default fct handle |
---|
[1134] | 185 | checksub=0; |
---|
[478] | 186 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
[986] | 187 | currentdir=pwd; |
---|
| 188 | cd(Param.FieldTransform.TransformPath) |
---|
[1137] | 189 | if strcmp(Param.FieldTransform.TransformName,'sub_field') |
---|
| 190 | checksub=2;% the two into field series will be subtracted |
---|
| 191 | end |
---|
[986] | 192 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
| 193 | cd (currentdir) |
---|
| 194 | if isfield(Param,'TransformInput') |
---|
| 195 | for iview=1:NbView |
---|
[883] | 196 | XmlData{iview}.TransformInput=Param.TransformInput; |
---|
[986] | 197 | end |
---|
[1134] | 198 | end |
---|
| 199 | if checksub>2 && NbView>2 |
---|
| 200 | disp_uvmat('WARNING',['only the two first input file series will be combined by ' Param.FieldTransform.TransformName],checkrun) |
---|
| 201 | end |
---|
[374] | 202 | end |
---|
[457] | 203 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
| 204 | % EDIT FROM HERE |
---|
[116] | 205 | |
---|
[457] | 206 | %% check the validity of input file types |
---|
[606] | 207 | for iview=1:NbView |
---|
[673] | 208 | if ~isequal(CheckImage{iview},1)&&~isequal(CheckNc{iview},1) |
---|
[668] | 209 | disp_uvmat('ERROR','input set of input series: need either netcdf either image series',checkrun) |
---|
[632] | 210 | return |
---|
[464] | 211 | end |
---|
[457] | 212 | end |
---|
[673] | 213 | |
---|
| 214 | %% output file type |
---|
| 215 | if min(cell2mat(CheckImage))==1 && (~Param.CheckObject || strcmp(Param.ProjObject.Type,'plane')) |
---|
| 216 | FileExtOut='.png'; %image output (input and proj result = image) |
---|
| 217 | else |
---|
| 218 | FileExtOut='.nc'; %netcdf output |
---|
| 219 | end |
---|
[880] | 220 | if isempty(j1_series{1}) |
---|
| 221 | NomTypeOut='_1'; |
---|
| 222 | else |
---|
| 223 | NomTypeOut='_1_1'; |
---|
| 224 | end |
---|
[867] | 225 | %NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series |
---|
[673] | 226 | RootFileOut=RootFile{1}; |
---|
| 227 | for iview=2:NbView |
---|
| 228 | if ~strcmp(RootFile{iview},RootFile{1}) |
---|
| 229 | RootFileOut='mproj'; |
---|
| 230 | break |
---|
| 231 | end |
---|
| 232 | end |
---|
[457] | 233 | |
---|
[642] | 234 | |
---|
[457] | 235 | %% Set field names and velocity types |
---|
[462] | 236 | %use Param.InputFields for all views |
---|
[457] | 237 | |
---|
[640] | 238 | %% MAIN LOOP ON FIELDS |
---|
[457] | 239 | %%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
[474] | 240 | |
---|
[457] | 241 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
[880] | 242 | tstart=tic; %used to record the computing time |
---|
[938] | 243 | CheckOverwrite=1;%default |
---|
| 244 | if isfield(Param,'CheckOverwrite') |
---|
| 245 | CheckOverwrite=Param.CheckOverwrite; |
---|
| 246 | end |
---|
[1096] | 247 | OutputPath=fullfile(Param.OutputPath,num2str(Param.Experiment),num2str(Param.Device)); |
---|
[1094] | 248 | |
---|
[606] | 249 | for index=1:NbField |
---|
[626] | 250 | update_waitbar(WaitbarHandle,index/NbField) |
---|
[642] | 251 | if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
[624] | 252 | disp('program stopped by user') |
---|
[606] | 253 | return |
---|
| 254 | end |
---|
[938] | 255 | |
---|
| 256 | %% generating the name of the merged field |
---|
| 257 | i1=i1_series{1}(index); |
---|
| 258 | if ~isempty(i2_series{end}) |
---|
| 259 | i2=i2_series{end}(index); |
---|
| 260 | else |
---|
| 261 | i2=i1; |
---|
| 262 | end |
---|
| 263 | j1=1; |
---|
| 264 | j2=1; |
---|
| 265 | if ~isempty(j1_series{1}) |
---|
| 266 | j1=j1_series{1}(index); |
---|
| 267 | if ~isempty(j2_series{end}) |
---|
| 268 | j2=j2_series{end}(index); |
---|
| 269 | else |
---|
| 270 | j2=j1; |
---|
| 271 | end |
---|
| 272 | end |
---|
[1090] | 273 | OutputFile=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,FileExtOut,NomTypeOut,i1,i2,j1,j2); |
---|
[938] | 274 | if ~CheckOverwrite && exist(OutputFile,'file') |
---|
| 275 | disp(['existing output file ' OutputFile ' already exists, skip to next field']) |
---|
| 276 | continue% skip iteration if the mode overwrite is desactivated and the result file already exists |
---|
| 277 | end |
---|
| 278 | |
---|
[606] | 279 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
| 280 | Data=cell(1,NbView);%initiate the set Data |
---|
[676] | 281 | timeread=zeros(1,NbView); |
---|
[606] | 282 | for iview=1:NbView |
---|
[880] | 283 | %% reading input file(s) |
---|
| 284 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},ParamIn{iview},frame_index{iview}(index)); |
---|
[1062] | 285 | if isempty(errormsg) |
---|
[1084] | 286 | disp([filecell{iview,index} ' read OK']) |
---|
[1062] | 287 | else |
---|
[746] | 288 | disp_uvmat('ERROR',['ERROR in merge_proj/read_field/' errormsg],checkrun) |
---|
[614] | 289 | return |
---|
[457] | 290 | end |
---|
[938] | 291 | |
---|
[857] | 292 | ListVar=Data{iview}.ListVarName; |
---|
| 293 | for ilist=1:numel(ListVar) |
---|
| 294 | Data{iview}.(ListVar{ilist})=double(Data{iview}.(ListVar{ilist}));% transform all fields in double before all operations |
---|
| 295 | end |
---|
[676] | 296 | % get the time defined in the current file if not already defined from the xml file |
---|
[975] | 297 | if isempty(time) && isfield(Data{iview},'Time') |
---|
[606] | 298 | timeread(iview)=Data{iview}.Time; |
---|
[595] | 299 | end |
---|
[606] | 300 | if ~isempty(NbSlice_calib) |
---|
| 301 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
| 302 | end |
---|
[642] | 303 | |
---|
[1134] | 304 | %% transform the input field iview (e.g; phys) if requested (no transform involving two input fields at this stage) |
---|
[1137] | 305 | if ~isempty(transform_fct)&& checksub==0 |
---|
| 306 | if nargin(transform_fct)>=2 |
---|
[606] | 307 | Data{iview}=transform_fct(Data{iview},XmlData{iview}); |
---|
[1137] | 308 | else |
---|
[606] | 309 | Data{iview}=transform_fct(Data{iview}); |
---|
[29] | 310 | end |
---|
[606] | 311 | end |
---|
[632] | 312 | |
---|
[642] | 313 | %% calculate tps coefficients if needed |
---|
[1030] | 314 | |
---|
| 315 | check_proj_tps= strcmp(FileType{iview},'civdata') && isfield(Param,'ProjObject')&&~isempty(Param.ProjObject)... |
---|
| 316 | && strcmp(Param.ProjObject.ProjMode,'interp_tps')&&~isfield(Data{iview},'Coord_tps'); |
---|
[1078] | 317 | if check_proj_tps |
---|
[606] | 318 | Data{iview}=tps_coeff_field(Data{iview},check_proj_tps); |
---|
[1078] | 319 | end |
---|
[642] | 320 | |
---|
[606] | 321 | %% projection on object (gridded plane) |
---|
| 322 | if Param.CheckObject |
---|
| 323 | [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject); |
---|
| 324 | if ~isempty(errormsg) |
---|
[746] | 325 | disp_uvmat('ERROR',['ERROR in merge_proge/proj_field: ' errormsg],checkrun) |
---|
[606] | 326 | return |
---|
[29] | 327 | end |
---|
[464] | 328 | end |
---|
[640] | 329 | |
---|
| 330 | %% mask |
---|
[1147] | 331 | if Param.CheckMask % introduce multilevel mask like for civ |
---|
| 332 | NbSlice=Param.MaskTable{iview,2}; |
---|
| 333 | [RootPath_mask,SubDir_mask,RootFile_mask,i1_mask,i2_mask,j1_mask,j2_mask,Ext_mask]=fileparts_uvmat(Param.MaskTable{iview,1}); |
---|
| 334 | i1_mask=mod(i1-1,NbSlice)+1; |
---|
| 335 | maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask); |
---|
| 336 | if ~exist(maskname,'file') |
---|
| 337 | maskname=Param.MaskTable{iview,1}; |
---|
| 338 | end |
---|
| 339 | [MaskData,~,errormsg] = read_field(maskname,'image'); |
---|
[1152] | 340 | if ~isempty(NbSlice_calib) |
---|
| 341 | MaskData.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
| 342 | end |
---|
[1147] | 343 | if ~isempty(transform_fct) && nargin(transform_fct)>=2 |
---|
| 344 | MaskData=transform_fct(MaskData,XmlData{iview}); |
---|
| 345 | end |
---|
| 346 | if ~isempty(MaskData) |
---|
| 347 | [Data{iview},errormsg]=mask_proj(Data{iview},MaskData); |
---|
| 348 | end |
---|
[640] | 349 | end |
---|
[606] | 350 | end |
---|
[673] | 351 | %%%%%%%%%%%%%%%% END LOOP ON VIEWS %%%%%%%%%%%%%%%% |
---|
[606] | 352 | |
---|
[1134] | 353 | %% merge the NbView fields |
---|
[1137] | 354 | if checksub==0 |
---|
[1134] | 355 | [MergeData,errormsg]=merge_field(Data);%concatene all the input field series by fct merge_data |
---|
[1137] | 356 | else |
---|
| 357 | MergeData=transform_fct(Data{1},XmlData{1},Data{2}); %combine the two input file series |
---|
| 358 | % else |
---|
| 359 | % MergeData=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});%combine the two input file series with calibration parameters |
---|
| 360 | end |
---|
[739] | 361 | if ~isempty(errormsg) |
---|
| 362 | disp_uvmat('ERROR',errormsg,checkrun); |
---|
[606] | 363 | return |
---|
| 364 | end |
---|
| 365 | |
---|
[673] | 366 | %% time of the merged field: take the average of the different views |
---|
[676] | 367 | if ~isempty(time) |
---|
| 368 | timeread=time(index); |
---|
| 369 | elseif ~isempty(find(timeread))% time defined from ImaDoc |
---|
| 370 | timeread=mean(timeread(timeread~=0));% take average over times form the files (when defined) |
---|
| 371 | else |
---|
| 372 | timeread=index;% take time=file index |
---|
[606] | 373 | end |
---|
| 374 | |
---|
[938] | 375 | |
---|
[673] | 376 | %% recording the merged field |
---|
| 377 | if strcmp(FileExtOut,'.png') %output as image |
---|
| 378 | if index==1 |
---|
[956] | 379 | if strcmp(class(MergeData.A),'uint8') |
---|
| 380 | BitDepth=8; |
---|
| 381 | else |
---|
| 382 | BitDepth=16; |
---|
| 383 | end |
---|
[673] | 384 | %write xml calibration file, using the first file |
---|
| 385 | siz=size(MergeData.A); |
---|
| 386 | npy=siz(1); |
---|
| 387 | npx=siz(2); |
---|
[880] | 388 | if isfield(MergeData,'Coord_x') && isfield(MergeData,'Coord_y') |
---|
| 389 | Rangx=MergeData.Coord_x; |
---|
| 390 | Rangy=MergeData.Coord_y; |
---|
[673] | 391 | elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY') |
---|
| 392 | Rangx=[MergeData.AX(1) MergeData.AX(end)]; |
---|
| 393 | Rangy=[MergeData.AY(1) MergeData.AY(end)]; |
---|
| 394 | else |
---|
| 395 | Rangx=[0.5 npx-0.5]; |
---|
| 396 | Rangy=[npy-0.5 0.5];%default |
---|
| 397 | end |
---|
| 398 | pxcmx=(npx-1)/(Rangx(2)-Rangx(1)); |
---|
| 399 | pxcmy=(npy-1)/(Rangy(1)-Rangy(2)); |
---|
| 400 | T_x=-pxcmx*Rangx(1)+0.5; |
---|
| 401 | T_y=-pxcmy*Rangy(2)+0.5; |
---|
| 402 | GeometryCal.CalibrationType='rescale'; |
---|
| 403 | GeometryCal.CoordUnit=MergeData.CoordUnit; |
---|
| 404 | GeometryCal.focal=1; |
---|
| 405 | GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1]; |
---|
| 406 | GeometryCal.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 407 | ImaDoc.GeometryCalib=GeometryCal; |
---|
| 408 | t=struct2xml(ImaDoc); |
---|
| 409 | t=set(t,1,'name','ImaDoc'); |
---|
| 410 | save(t,[fileparts(OutputFile) '.xml']) |
---|
| 411 | end |
---|
[956] | 412 | if BitDepth==8 |
---|
| 413 | imwrite(uint8(MergeData.A),OutputFile,'BitDepth',8) |
---|
| 414 | else |
---|
| 415 | imwrite(uint16(MergeData.A),OutputFile,'BitDepth',16) |
---|
| 416 | end |
---|
[937] | 417 | else %output as netcdf files |
---|
| 418 | MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','NbCoord','NbDim'}; |
---|
[606] | 419 | MergeData.Conventions='uvmat'; |
---|
[937] | 420 | MergeData.NbCoord=2; |
---|
| 421 | MergeData.NbDim=2; |
---|
| 422 | % time interval of PIV |
---|
| 423 | Dt=[]; |
---|
| 424 | if isfield(Data{1},'Dt')&& isnumeric(Data{1}.Dt) |
---|
| 425 | Dt=Data{1}.Dt; |
---|
[606] | 426 | end |
---|
| 427 | for iview =2:numel(Data) |
---|
[937] | 428 | if ~(isfield(Data{iview},'Dt')&& isequal(Data{iview}.Dt,Dt)) |
---|
| 429 | Dt=[];%dt not the same for all fields |
---|
[464] | 430 | end |
---|
| 431 | end |
---|
[676] | 432 | if ~isempty(timeread) |
---|
| 433 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'Time'}]; |
---|
| 434 | MergeData.Time=timeread; |
---|
| 435 | end |
---|
[937] | 436 | % position of projection plane |
---|
| 437 | if isfield(Data{1},'ProjObjectCoord')&& isfield(Data{1},'ProjObjectAngle') |
---|
| 438 | ProjObjectCoord=Data{1}.ProjObjectCoord; |
---|
| 439 | ProjObjectAngle=Data{1}.ProjObjectAngle; |
---|
| 440 | for iview =2:numel(Data) |
---|
| 441 | if ~(isfield(Data{iview},'ProjObjectCoord')&& isequal(Data{iview}.ProjObjectCoord,ProjObjectCoord))... |
---|
| 442 | ||~(isfield(Data{iview},'ProjObjectAngle')&& isequal(Data{iview}.ProjObjectAngle,ProjObjectAngle)) |
---|
| 443 | ProjObjectCoord=[];%dt not the same for all fields |
---|
| 444 | end |
---|
| 445 | end |
---|
| 446 | if ~isempty(ProjObjectCoord) |
---|
| 447 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'ProjObjectCoord'} {'ProjObjectAngle'}]; |
---|
| 448 | MergeData.ProjObjectCoord=ProjObjectCoord; |
---|
| 449 | MergeData.ProjObjectAngle=ProjObjectAngle; |
---|
| 450 | end |
---|
[29] | 451 | end |
---|
[937] | 452 | % coord unit |
---|
| 453 | if isfield(Data{1},'CoordUnit') |
---|
| 454 | CoordUnit=Data{1}.CoordUnit; |
---|
| 455 | for iview =2:numel(Data) |
---|
| 456 | if ~(isfield(Data{iview},'CoordUnit')&& isequal(Data{iview}.CoordUnit,CoordUnit)) |
---|
| 457 | CoordUnit=[];%CoordUnit not the same for all fields |
---|
| 458 | end |
---|
| 459 | end |
---|
| 460 | if ~isempty(CoordUnit) |
---|
| 461 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'CoordUnit'}]; |
---|
| 462 | MergeData.CoordUnit=CoordUnit; |
---|
| 463 | end |
---|
| 464 | end |
---|
| 465 | % time unit |
---|
| 466 | if isfield(Data{1},'TimeUnit') |
---|
| 467 | TimeUnit=Data{1}.TimeUnit; |
---|
| 468 | for iview =2:numel(Data) |
---|
| 469 | if ~(isfield(Data{iview},'TimeUnit')&& isequal(Data{iview}.TimeUnit,TimeUnit)) |
---|
| 470 | TimeUnit=[];%TimeUnit not the same for all fields |
---|
| 471 | end |
---|
| 472 | end |
---|
| 473 | if ~isempty(TimeUnit) |
---|
| 474 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'TimeUnit'}]; |
---|
| 475 | MergeData.TimeUnit=TimeUnit; |
---|
| 476 | end |
---|
| 477 | end |
---|
[606] | 478 | error=struct2nc(OutputFile,MergeData);%save result file |
---|
| 479 | if isempty(error) |
---|
[739] | 480 | disp(['output file ' OutputFile ' written']) |
---|
[606] | 481 | else |
---|
[739] | 482 | disp(error) |
---|
[606] | 483 | end |
---|
[29] | 484 | end |
---|
| 485 | end |
---|
[905] | 486 | ellapsed_time=toc(tstart); |
---|
| 487 | disp(['total ellapsed time ' num2str(ellapsed_time/60,2) ' minutes']) |
---|
| 488 | disp([ num2str(ellapsed_time/(60*NbField),3) ' minutes per iteration']) |
---|
[29] | 489 | |
---|
[222] | 490 | %'merge_field': concatene fields |
---|
| 491 | %------------------------------------------------------------------------ |
---|
[739] | 492 | function [MergeData,errormsg]=merge_field(Data) |
---|
[222] | 493 | %% default output |
---|
[29] | 494 | if isempty(Data)||~iscell(Data) |
---|
| 495 | MergeData=[]; |
---|
| 496 | return |
---|
| 497 | end |
---|
[739] | 498 | errormsg=''; |
---|
[867] | 499 | MergeData=Data{1};% merged field= first field by default, reproduces the global attributes of the first field |
---|
[606] | 500 | NbView=length(Data); |
---|
[867] | 501 | if NbView==1% if there is only one field, just reproduce it in MergeData |
---|
| 502 | return |
---|
[29] | 503 | end |
---|
[222] | 504 | |
---|
[522] | 505 | %% group the variables (fields of 'Data') in cells of variables with the same dimensions |
---|
[668] | 506 | [CellInfo,NbDim,errormsg]=find_field_cells(Data{1}); |
---|
[739] | 507 | if ~isempty(errormsg) |
---|
| 508 | return |
---|
| 509 | end |
---|
[670] | 510 | |
---|
[89] | 511 | %LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
[668] | 512 | for icell=1:length(CellInfo) |
---|
[670] | 513 | if NbDim(icell)~=1 % skip field cells which are of dim 1 |
---|
| 514 | switch CellInfo{icell}.CoordType |
---|
[867] | 515 | case 'scattered' %case of input fields with unstructured coordinates: just concatene data |
---|
[670] | 516 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
| 517 | VarName=Data{1}.ListVarName{ivar}; |
---|
| 518 | for iview=2:NbView |
---|
| 519 | MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)]; |
---|
[89] | 520 | end |
---|
| 521 | end |
---|
[670] | 522 | case 'grid' %case of fields defined on a structured grid |
---|
| 523 | FFName=''; |
---|
[676] | 524 | if isfield(CellInfo{icell},'VarIndex_errorflag') && ~isempty(CellInfo{icell}.VarIndex_errorflag) |
---|
[670] | 525 | FFName=Data{1}.ListVarName{CellInfo{icell}.VarIndex_errorflag};% name of errorflag variable |
---|
[867] | 526 | MergeData.ListVarName(CellInfo{icell}.VarIndex_errorflag)=[];%remove error flag variable in MergeData (will use NaN instead) |
---|
| 527 | MergeData.VarDimName(CellInfo{icell}.VarIndex_errorflag)=[]; |
---|
| 528 | MergeData.VarAttribute(CellInfo{icell}.VarIndex_errorflag)=[]; |
---|
[670] | 529 | end |
---|
| 530 | % select good data on each view |
---|
| 531 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
| 532 | VarName=Data{1}.ListVarName{ivar}; |
---|
| 533 | for iview=1:NbView |
---|
| 534 | if isempty(FFName) |
---|
| 535 | check_bad=isnan(Data{iview}.(VarName));%=0 for NaN data values, 1 else |
---|
| 536 | else |
---|
| 537 | check_bad=isnan(Data{iview}.(VarName)) | Data{iview}.(FFName)~=0;%=0 for NaN or error flagged data values, 1 else |
---|
| 538 | end |
---|
[867] | 539 | Data{iview}.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag |
---|
[670] | 540 | if iview==1 |
---|
[867] | 541 | %MergeData.(VarName)=Data{1}.(VarName);% initiate MergeData with the first field |
---|
[874] | 542 | MergeData.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag |
---|
[670] | 543 | NbAver=~check_bad;% initiate NbAver: the nbre of good data for each point |
---|
[739] | 544 | elseif size(Data{iview}.(VarName))~=size(MergeData.(VarName)) |
---|
| 545 | errormsg='sizes of the input matrices do not agree, need to interpolate on a common grid using a projection object'; |
---|
| 546 | return |
---|
[867] | 547 | else |
---|
| 548 | MergeData.(VarName)=MergeData.(VarName) +double(Data{iview}.(VarName));%add data |
---|
[670] | 549 | NbAver=NbAver + ~check_bad;% add 1 for good data, 0 else |
---|
| 550 | end |
---|
| 551 | end |
---|
| 552 | MergeData.(VarName)(NbAver~=0)=MergeData.(VarName)(NbAver~=0)./NbAver(NbAver~=0);% take average of defined data at each point |
---|
[867] | 553 | MergeData.(VarName)(NbAver==0)=NaN;% set to NaN the points with no good data |
---|
[670] | 554 | end |
---|
[89] | 555 | end |
---|
| 556 | end |
---|
| 557 | end |
---|
[222] | 558 | |
---|
[670] | 559 | |
---|
[810] | 560 | |
---|