[169] | 1 | %'aver_stat': calculate field average, used with series.fig |
---|
[457] | 2 | % this function can be used as a template for applying a global operation (here averaging) on a series of input fields |
---|
[169] | 3 | %------------------------------------------------------------------------ |
---|
[457] | 4 | % function ParamOut=aver_stat(Param) |
---|
[169] | 5 | % |
---|
[447] | 6 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
[457] | 7 | % |
---|
| 8 | % This function is used in four modes by the GUI series: |
---|
| 9 | % 1) config GUI: with no input argument, the function determine the suitable GUI configuration |
---|
| 10 | % 2) interactive input: the function is used to interactively introduce input parameters, and then stops |
---|
| 11 | % 3) RUN: the function itself runs, when an appropriate input structure Param has been introduced. |
---|
| 12 | % 4) BATCH: the function itself proceeds in BATCH mode, using an xml file 'Param' as input. |
---|
| 13 | % |
---|
| 14 | % This function is used in four modes by the GUI series: |
---|
| 15 | % 1) config GUI: with no input argument, the function determine the suitable GUI configuration |
---|
| 16 | % 2) interactive input: the function is used to interactively introduce input parameters, and then stops |
---|
| 17 | % 3) RUN: the function itself runs, when an appropriate input structure Param has been introduced. |
---|
| 18 | % 4) BATCH: the function itself proceeds in BATCH mode, using an xml file 'Param' as input. |
---|
| 19 | % |
---|
[169] | 20 | %OUTPUT |
---|
| 21 | % GUI_input=list of options in the GUI series.fig needed for the function |
---|
| 22 | % |
---|
| 23 | %INPUT: |
---|
[447] | 24 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
---|
| 25 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
---|
| 26 | % In the absence of input (as activated when the current Action is selected |
---|
| 27 | % in series), the function ouput GUI_input set the activation of the needed GUI elements |
---|
[169] | 28 | % |
---|
[447] | 29 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to see the current structure Param) |
---|
| 30 | % .InputTable: cell of input file names, (several lines for multiple input) |
---|
| 31 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
---|
| 32 | % .OutputSubDir: name of the subdirectory for data outputs |
---|
[474] | 33 | % .OutputDirExt: directory extension for data outputs |
---|
[447] | 34 | % .Action: .ActionName: name of the current activated function |
---|
| 35 | % .ActionPath: path of the current activated function |
---|
| 36 | % .IndexRange: set the file or frame indices on which the action must be performed |
---|
| 37 | % .FieldTransform: .TransformName: name of the selected transform function |
---|
| 38 | % .TransformPath: path of the selected transform function |
---|
| 39 | % .TransformHandle: corresponding function handle |
---|
| 40 | % .InputFields: sub structure describing the input fields withfields |
---|
| 41 | % .FieldName: name of the field |
---|
| 42 | % .VelType: velocity type |
---|
| 43 | % .FieldName_1: name of the second field in case of two input series |
---|
| 44 | % .VelType_1: velocity type of the second field in case of two input series |
---|
| 45 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
---|
| 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 47 | |
---|
[457] | 48 | function ParamOut=aver_stat(Param) |
---|
[447] | 49 | |
---|
| 50 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName |
---|
| 51 | if ~exist('Param','var') % case with no input parameter |
---|
[474] | 52 | ParamOut={'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default) |
---|
[457] | 53 | 'WholeIndexRange';'off';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
[27] | 54 | 'NbSlice';'on'; ...%nbre of slices ('off' by default) |
---|
[447] | 55 | 'VelType';'two';...% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
| 56 | 'FieldName';'two';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
| 57 | 'FieldTransform'; 'on';...%can use a transform function |
---|
| 58 | 'ProjObject';'on';...%can use projection object(option 'off'/'on', |
---|
| 59 | 'Mask';'off';...%can use mask option (option 'off'/'on', 'off' by default) |
---|
| 60 | 'OutputDirExt';'.stat';...%set the output dir extension |
---|
[27] | 61 | ''}; |
---|
| 62 | return |
---|
| 63 | end |
---|
| 64 | |
---|
[474] | 65 | %%%%%%%%%%%% STANDARD PART %%%%%%%%%%%% |
---|
[457] | 66 | %% select different modes, RUN, parameter input, BATCH |
---|
[447] | 67 | % BATCH case: read the xml file for batch case |
---|
[457] | 68 | if ischar(Param) |
---|
| 69 | Param=xml2struct(Param); |
---|
| 70 | checkrun=0; |
---|
| 71 | % RUN case: parameters introduced as the input structure Param |
---|
| 72 | else |
---|
[462] | 73 | if isfield(Param,'Specific')&& strcmp(Param.Specific,'?') |
---|
| 74 | checkrun=1;% will only search interactive input parameters (preparation of BATCH mode) |
---|
| 75 | else |
---|
| 76 | checkrun=2; % indicate the RUN option is used |
---|
| 77 | end |
---|
[478] | 78 | hseries=guidata(Param.hseries);%handles of the GUI series |
---|
[361] | 79 | end |
---|
[462] | 80 | ParamOut=Param; %default output |
---|
[474] | 81 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
---|
| 82 | |
---|
[457] | 83 | %% root input file(s) and type |
---|
| 84 | RootPath=Param.InputTable(:,1); |
---|
| 85 | RootFile=Param.InputTable(:,3); |
---|
| 86 | SubDir=Param.InputTable(:,2); |
---|
| 87 | NomType=Param.InputTable(:,4); |
---|
| 88 | FileExt=Param.InputTable(:,5); |
---|
[374] | 89 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
[474] | 90 | %%%%%%%%%%%% |
---|
| 91 | % The cell array filecell is the list of input file names, while |
---|
| 92 | % filecell{iview,fileindex}: |
---|
[447] | 93 | % iview: line in the table corresponding to a given file series |
---|
| 94 | % fileindex: file index within the file series, |
---|
| 95 | % 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 |
---|
| 96 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
[474] | 97 | %%%%%%%%%%%% |
---|
[447] | 98 | NbSlice=1;%default |
---|
[457] | 99 | if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
[447] | 100 | NbSlice=Param.IndexRange.NbSlice; |
---|
[27] | 101 | end |
---|
[454] | 102 | nbview=numel(i1_series);%number of input file series (lines in InputTable) |
---|
[457] | 103 | nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
| 104 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index |
---|
| 105 | nbfield=nbfield_j*nbfield_i; %total number of fields |
---|
[454] | 106 | nbfield_i=floor(nbfield/NbSlice);%total number of indexes in a slice (adjusted to an integer number of slices) |
---|
| 107 | nbfield=nbfield_i*NbSlice; %total number of fields after adjustement |
---|
[43] | 108 | |
---|
[447] | 109 | %determine the file type on each line from the first input file |
---|
| 110 | ImageTypeOptions={'image','multimage','mmreader','video'}; |
---|
| 111 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
[27] | 112 | for iview=1:nbview |
---|
[451] | 113 | if ~exist(filecell{iview,1}','file') |
---|
| 114 | msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist']) |
---|
| 115 | return |
---|
| 116 | end |
---|
| 117 | [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1}); |
---|
[447] | 118 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
| 119 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
[454] | 120 | if ~isempty(j1_series{iview}) |
---|
| 121 | frame_index{iview}=j1_series{iview}; |
---|
| 122 | else |
---|
| 123 | frame_index{iview}=i1_series{iview}; |
---|
| 124 | end |
---|
[27] | 125 | end |
---|
| 126 | |
---|
[451] | 127 | %% calibration data and timing: read the ImaDoc files |
---|
[470] | 128 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
| 129 | if size(time,1)>1 |
---|
[27] | 130 | diff_time=max(max(diff(time))); |
---|
| 131 | if diff_time>0 |
---|
[447] | 132 | msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)]) |
---|
[27] | 133 | end |
---|
| 134 | end |
---|
| 135 | |
---|
[447] | 136 | %% coordinate transform or other user defined transform |
---|
| 137 | transform_fct='';%default |
---|
[478] | 138 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
[474] | 139 | addpath(Param.FieldTransform.TransformPath) |
---|
| 140 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
| 141 | rmpath(Param.FieldTransform.TransformPath) |
---|
[27] | 142 | end |
---|
[474] | 143 | |
---|
[447] | 144 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
| 145 | % EDIT FROM HERE |
---|
| 146 | |
---|
| 147 | %% check the validity of input file types |
---|
| 148 | if CheckImage{1} |
---|
| 149 | FileExtOut='.png'; % write result as .png images for image inputs |
---|
| 150 | elseif CheckNc{1} |
---|
| 151 | FileExtOut='.nc';% write result as .nc files for netcdf inputs |
---|
| 152 | else |
---|
| 153 | msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}]) |
---|
| 154 | return |
---|
| 155 | end |
---|
| 156 | if nbview==2 && ~isequal(CheckImage{1},CheckImage{2}) |
---|
| 157 | msgbox_uvmat('ERROR','input must be two image series or two netcdf file series') |
---|
| 158 | return |
---|
| 159 | end |
---|
| 160 | NomTypeOut='_1-2_1';% output file index will indicate the first and last ref index in the series |
---|
[474] | 161 | if checkrun==1 |
---|
| 162 | return % stop here for input checks |
---|
[218] | 163 | end |
---|
[442] | 164 | |
---|
[447] | 165 | %% Set field names and velocity types |
---|
| 166 | InputFields{1}=[];%default (case of images) |
---|
| 167 | if isfield(Param,'InputFields') |
---|
| 168 | InputFields{1}=Param.InputFields; |
---|
| 169 | end |
---|
| 170 | if nbview==2 |
---|
| 171 | InputFields{2}=[];%default (case of images) |
---|
| 172 | if isfield(Param,'InputFields') |
---|
| 173 | InputFields{2}=Param.InputFields{1};%default |
---|
| 174 | if isfield(Param.InputFields,'FieldName_1') |
---|
| 175 | InputFields{2}.FieldName=Param.InputFields.FieldName_1; |
---|
| 176 | if isfield(Param.InputFields,'VelType_1') |
---|
| 177 | InputFields{2}.VelType=Param.InputFields.VelType_1; |
---|
| 178 | end |
---|
| 179 | end |
---|
[255] | 180 | end |
---|
| 181 | end |
---|
[447] | 182 | |
---|
| 183 | %% MAIN LOOP ON SLICES |
---|
[27] | 184 | for i_slice=1:NbSlice |
---|
[447] | 185 | index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice |
---|
[255] | 186 | nbfiles=0; |
---|
| 187 | nbmissing=0; |
---|
[474] | 188 | |
---|
[447] | 189 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
| 190 | for index=index_slice |
---|
| 191 | if checkrun |
---|
[478] | 192 | update_waitbar(hseries.Waitbar,index/(nbfield)) |
---|
[447] | 193 | stopstate=get(hseries.RUN,'BusyAction'); |
---|
| 194 | else |
---|
| 195 | stopstate='queue'; |
---|
| 196 | end |
---|
[526] | 197 | if isequal(stopstate,'queue')% enable STOP command |
---|
| 198 | |
---|
[447] | 199 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
| 200 | for iview=1:nbview |
---|
[255] | 201 | % reading input file(s) |
---|
[464] | 202 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index)); |
---|
[447] | 203 | if ~isempty(errormsg) |
---|
| 204 | errormsg=['error of input reading: ' errormsg]; |
---|
| 205 | break |
---|
[255] | 206 | end |
---|
[447] | 207 | if ~isempty(NbSlice_calib) |
---|
[454] | 208 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
[447] | 209 | end |
---|
| 210 | end |
---|
[526] | 211 | else |
---|
| 212 | errormsg='stop'; |
---|
| 213 | end |
---|
[447] | 214 | %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
| 215 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
| 216 | % EDIT FROM HERE |
---|
[526] | 217 | |
---|
[462] | 218 | if isempty(errormsg) |
---|
[526] | 219 | Field=Data{1}; % default input field structure |
---|
| 220 | %% coordinate transform (or other user defined transform) |
---|
[255] | 221 | if ~isempty(transform_fct) |
---|
[526] | 222 | switch nargin(transform_fct) |
---|
| 223 | case 4 |
---|
| 224 | if length(Data)==2 |
---|
| 225 | Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2}); |
---|
| 226 | else |
---|
| 227 | Field=transform_fct(Data{1},XmlData{1}); |
---|
| 228 | end |
---|
| 229 | case 3 |
---|
| 230 | if length(Data)==2 |
---|
| 231 | Field=transform_fct(Data{1},XmlData{1},Data{2}); |
---|
| 232 | else |
---|
| 233 | Field=transform_fct(Data{1},XmlData{1}); |
---|
| 234 | end |
---|
| 235 | case 2 |
---|
| 236 | Field=transform_fct(Data{1},XmlData{1}); |
---|
| 237 | case 1 |
---|
| 238 | Field=transform_fct(Data{1}); |
---|
[27] | 239 | end |
---|
[255] | 240 | end |
---|
| 241 | |
---|
[526] | 242 | %% calculate tps coefficients if needed |
---|
| 243 | if isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'filter') |
---|
| 244 | Field=calc_tps(Field,check_proj_tps); |
---|
[494] | 245 | end |
---|
[526] | 246 | |
---|
[451] | 247 | %field projection on an object |
---|
[447] | 248 | if Param.CheckObject |
---|
[462] | 249 | [Field,errormsg]=proj_field(Field,Param.ProjObject); |
---|
[255] | 250 | if ~isempty(errormsg) |
---|
[27] | 251 | msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg]) |
---|
| 252 | return |
---|
| 253 | end |
---|
[255] | 254 | end |
---|
| 255 | nbfiles=nbfiles+1; |
---|
[447] | 256 | |
---|
| 257 | %%%%%%%%%%%% MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
| 258 | %update sum |
---|
[462] | 259 | if nbfiles==1 %first field |
---|
| 260 | time_1=[]; |
---|
| 261 | if isfield(Field,'Time') |
---|
| 262 | time_1=Field.Time(1); |
---|
[255] | 263 | end |
---|
[462] | 264 | DataOut=Field;%default |
---|
| 265 | for ivar=1:length(Field.ListVarName) |
---|
| 266 | VarName=Field.ListVarName{ivar}; |
---|
| 267 | DataOut.(VarName)=double(DataOut.(VarName)); |
---|
| 268 | end |
---|
| 269 | else %current field |
---|
| 270 | for ivar=1:length(Field.ListVarName) |
---|
| 271 | VarName=Field.ListVarName{ivar}; |
---|
| 272 | sizmean=size(DataOut.(VarName)); |
---|
| 273 | siz=size(Field.(VarName)); |
---|
| 274 | if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean) |
---|
| 275 | msgbox_uvmat('ERROR',['unequal size of input field ' VarName ', need to project on a grid']) |
---|
| 276 | return |
---|
| 277 | else |
---|
| 278 | DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum |
---|
| 279 | end |
---|
| 280 | end |
---|
[255] | 281 | end |
---|
[447] | 282 | %%%%%%%%%%%% END MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
| 283 | else |
---|
[462] | 284 | display(errormsg) |
---|
[447] | 285 | end |
---|
[255] | 286 | end |
---|
[447] | 287 | %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%% |
---|
| 288 | |
---|
[27] | 289 | for ivar=1:length(Field.ListVarName) |
---|
| 290 | VarName=Field.ListVarName{ivar}; |
---|
[447] | 291 | DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean |
---|
[27] | 292 | end |
---|
| 293 | if nbmissing~=0 |
---|
| 294 | msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted']) |
---|
| 295 | end |
---|
[462] | 296 | if isempty(time) % time is read from files |
---|
[27] | 297 | if isfield(Field,'Time') |
---|
| 298 | time_end=Field.Time(1);%last time read |
---|
| 299 | if ~isempty(time_1) |
---|
[447] | 300 | DataOut.Time=time_1; |
---|
| 301 | DataOut.Time_end=time_end; |
---|
[27] | 302 | end |
---|
| 303 | end |
---|
[457] | 304 | else % time from ImaDoc prevails if it exists |
---|
| 305 | j1=1;%default |
---|
| 306 | if ~isempty(j1_series{1}) |
---|
| 307 | j1=j1_series{1}; |
---|
| 308 | end |
---|
| 309 | DataOut.Time=time(1,i1_series{1}(1),j1filexml); |
---|
[454] | 310 | DataOut.Time_end=time(end,i1_series{end}(end),j1_series{end}(end)); |
---|
[27] | 311 | end |
---|
[255] | 312 | |
---|
[526] | 313 | %writting the result file |
---|
[474] | 314 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(1),i1_series{1}(end),i_slice,[]); |
---|
[447] | 315 | if CheckImage{1} %case of images |
---|
| 316 | if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16)) |
---|
| 317 | DataOut.A=uint16(DataOut.A); |
---|
| 318 | imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images |
---|
[27] | 319 | else |
---|
[447] | 320 | DataOut.A=uint8(DataOut.A); |
---|
| 321 | imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images |
---|
[27] | 322 | end |
---|
[447] | 323 | display([OutputFile ' written']); |
---|
[55] | 324 | else %case of netcdf input file , determine global attributes |
---|
[447] | 325 | errormsg=struct2nc(OutputFile,DataOut); %save result file |
---|
[27] | 326 | if isempty(errormsg) |
---|
[447] | 327 | display([OutputFile ' written']); |
---|
[27] | 328 | else |
---|
| 329 | msgbox_uvmat('ERROR',['error in writting result file: ' errormsg]) |
---|
| 330 | display(errormsg) |
---|
| 331 | end |
---|
[255] | 332 | end % end averaging loop |
---|
[447] | 333 | end |
---|
| 334 | %%%%%%%%%%%%%%%% end loop on slices %%%%%%%%%%%%%%%% |
---|
[255] | 335 | |
---|
[451] | 336 | %% open the result file with uvmat (in RUN mode) |
---|
| 337 | if checkrun |
---|
[526] | 338 | % hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI |
---|
| 339 | % delete(hget_field) |
---|
[451] | 340 | uvmat(OutputFile)% open the last result file with uvmat |
---|
| 341 | end |
---|