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