source: trunk/src/series/histo_diff.m @ 868

Last change on this file since 868 was 868, checked in by sommeria, 6 years ago

histo_diff upadated

File size: 16.5 KB
Line 
1%'histo_diff': calculate the histogram of the difference between two fields
2%------------------------------------------------------------------------
3% function ParamOut=histo_diff(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-2014, LEGI UMR 5519 / CNRS UJF 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
59function ParamOut=histo_diff(Param)
60
61%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
62if isstruct(Param) && isequal(Param.Action.RUN,0)% function activated from the GUI series but not RUN
63    ParamOut.AllowInputSort='off';% 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='two';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
67    ParamOut.FieldName='two';% 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.ProjObject='on';%can use projection object(option 'off'/'on',
70    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
71    ParamOut.OutputDirExt='.histodif';%set the output dir extension
72    ParamOut.OutputFileMode='NbSlice';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice
73    % check the existence of the first file in the series
74    first_j=[];
75    if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
76    last_j=[];
77    if isfield(Param.IndexRange,'last_j'); last_j=Param.IndexRange.last_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    else
86        [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString);
87        LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
88        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
89        if ~exist(FirstFileName,'file')
90             msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist'])
91        end
92    end
93    return
94end
95
96%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
97ParamOut=[];%default output
98%% read input parameters from an xml file if input is a file name (batch mode)
99checkrun=1;
100if ischar(Param)
101    Param=xml2struct(Param);% read Param as input file (batch case)
102    checkrun=0;
103end
104hseries=findobj(allchild(0),'Tag','series');
105RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
106WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
107
108%% define the directory for result file (with path=RootPath{1})
109OutputDir=[Param.OutputSubDir Param.OutputDirExt];
110   
111%% root input file(s) name, type and index series
112RootPath=Param.InputTable(:,1);
113RootFile=Param.InputTable(:,3);
114SubDir=Param.InputTable(:,2);
115NomType=Param.InputTable(:,4);
116FileExt=Param.InputTable(:,5);
117hdisp=disp_uvmat('WAITING...','checking the file series',checkrun);
118[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
119if ~isempty(hdisp),delete(hdisp),end;
120%%%%%%%%%%%%
121% The cell array filecell is the list of input file names, while
122% filecell{iview,fileindex}:
123%        iview: line in the table corresponding to a given file series
124%        fileindex: file index within  the file series,
125% 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
126% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
127%%%%%%%%%%%%
128NbView=numel(i1_series);%number of input file series (lines in InputTable)
129NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
130NbField_i=size(i1_series{1},2); %nb of fields for the i index
131NbField=NbField_j*NbField_i; %total number of fields
132
133%% determine the file type on each line from the first input file
134ImageTypeOptions={'image','multimage','mmreader','video'};
135NcTypeOptions={'netcdf','civx','civdata'};
136for iview=1:NbView
137    if ~exist(filecell{iview,1}','file')
138        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
139        return
140    end
141    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
142    FileType{iview}=FileInfo{iview}.FileType;
143    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
144    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
145    if ~isempty(j1_series{iview})
146        frame_index{iview}=j1_series{iview};
147    else
148        frame_index{iview}=i1_series{iview};
149    end
150end
151
152%% calibration data and timing: read the ImaDoc files
153[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
154if size(time,1)>1
155    diff_time=max(max(diff(time)));
156    if diff_time>0
157        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
158    end   
159end
160
161%% coordinate transform or other user defined transform
162transform_fct='';%default
163if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
164    addpath(Param.FieldTransform.TransformPath)
165    transform_fct=str2func(Param.FieldTransform.TransformName);
166    rmpath(Param.FieldTransform.TransformPath)
167end
168if ~(isfield(Param,'ProjObject')&& ismember(Param.ProjObject.Type,{'polygon','rectangle','ellipse'}))
169    disp_uvmat('ERROR','projection in a closed domain: polygon, rectangle or ellipse is needed for histograms',checkrun)
170end
171%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
172 % EDIT FROM HERE
173
174%% check the validity of  input file types
175if CheckImage{1}
176    FileExtOut='.png'; % write result as .png images for image inputs
177elseif CheckNc{1}
178    FileExtOut='.nc';% write result as .nc files for netcdf inputs
179else
180    disp_uvmat('ERROR',['invalid file type input ' FileType{1}],checkrun)
181    return
182end
183if NbView==2 && ~isequal(CheckImage{1},CheckImage{2})
184    msgbox_uvmat('ERROR','input must be two image series or two netcdf file series')
185    return
186end
187
188%% settings for the output file
189NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
190first_i=i1_series{1}(1);
191last_i=i1_series{1}(end);
192if isempty(j1_series{1})% if there is no second index j
193    first_j=1;last_j=1;
194else
195    first_j=j1_series{1}(1);
196    last_j=j1_series{1}(end);
197end
198
199%% Set field names and velocity types
200InputFields{1}=[];%default (case of images)
201if isfield(Param,'InputFields')
202    InputFields{1}=Param.InputFields;
203end
204if NbView==2
205    InputFields{2}=[];%default (case of images)
206    if isfield(Param,'InputFields')
207        InputFields{2}=InputFields{1};%default
208        if isfield(Param.InputFields,'FieldName_1')
209            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
210            if isfield(Param.InputFields,'VelType_1')
211                InputFields{2}.VelType=Param.InputFields.VelType_1;
212            end
213        end
214    end
215end
216
217% for i_slice=1:NbSlice
218% index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
219% nbfiles=0;
220% nbmissing=0;
221
222%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
223for index=1:NbField
224    update_waitbar(WaitbarHandle,index/NbField)
225    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
226        disp('program stopped by user')
227        break
228    end
229   
230    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
231    for iview=1:NbView
232        % reading input file(s)
233        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
234        if ~isempty(errormsg)
235            errormsg=['error of input reading: ' errormsg];
236            break
237        end
238        if ~isempty(NbSlice_calib)
239            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
240        end
241    end
242    %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
243    %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
244    % EDIT FROM HERE
245   
246    if isempty(errormsg)
247        Field=Data{1}; % default input field structure
248        %% coordinate transform (or other user defined transform)
249        if ~isempty(transform_fct)
250            switch nargin(transform_fct)
251                case 2
252                    Field=transform_fct(Data{1},XmlData{1});
253                case 1
254                    Field=transform_fct(Data{1});
255            end
256        end
257       
258        %% projection of the second (ref) field on the first one
259        % calculate tps coefficients if needed (spatial derivatives)
260%         if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
261%             Field=tps_coeff_field(Field,check_proj_tps);
262%         end       
263          Field.ListVarName={'X','Y','U','V'};
264          Field.VarDimName={'nb_vector','nb_vector','nb_vector','nb_vector'};
265          F.U=scatteredInterpolant(Data{2}.X,Data{2}.Y,Data{2}.U,'linear');
266          Field.U=Field.U-F.U(Field.X,Field.Y);%substract the interpolated ref to U
267          F.V=scatteredInterpolant(Data{2}.X,Data{2}.Y,Data{2}.V,'linear');
268          Field.V=Field.V-F.V(Field.X,Field.Y);%substract the interpolated ref to V
269         
270        %field projection on a close object yielding histogram
271            [Histo,errormsg]=proj_field(Field,Param.ProjObject);% calculate histogram
272            if ~isempty(errormsg)
273                disp_uvmat('ERROR',['error in histo_diff/proj_field:' errormsg],checkrun)
274                return
275            end
276%         nbfiles=nbfiles+1;
277       
278        %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
279        if index==1 %first field
280            time_1=[];
281            if isfield(Field,'Time')
282                time_1=Field.Time(1);
283            end
284            DataOut=Field;%default
285            DataOut.Conventions='uvmat'; %suppress Conventions='uvmat/civdata' for civ input files
286            errorvar=zeros(numel(Field.ListVarName));%index of errorflag associated to each variable
287            for ivar=1:numel(Field.ListVarName)
288                VarName=Field.ListVarName{ivar};
289                DataOut.(VarName)=zeros(size(DataOut.(VarName)));% initiate each field to zero
290                NbData.(VarName)=zeros(size(DataOut.(VarName)));% initiate the nbre of good data to zero
291                for iivar=1:length(Field.VarAttribute)
292                    if isequal(Field.VarDimName{iivar},Field.VarDimName{ivar})&& isfield(Field.VarAttribute{iivar},'Role')...
293                            && strcmp(Field.VarAttribute{iivar}.Role,'errorflag')
294                        errorvar(ivar)=iivar; % index of the errorflag variable corresponding to ivar
295                    end
296                end
297            end
298            DataOut.ListVarName(errorvar(errorvar~=0))=[]; %remove errorflag from result
299            DataOut.VarDimName(errorvar(errorvar~=0))=[]; %remove errorflag from result
300            DataOut.VarAttribute(errorvar(errorvar~=0))=[]; %remove errorflag from result
301        end   %current field
302        for ivar=1:length(DataOut.ListVarName)
303            VarName=Field.ListVarName{ivar};
304            sizmean=size(DataOut.(VarName));
305            siz=size(Field.(VarName));
306            if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean)
307                disp_uvmat('ERROR',['unequal size of input field ' VarName ', need to project  on a grid'],checkrun)
308                return
309            else
310                if errorvar(ivar)==0
311                    check_bad=isnan(Field.(VarName));%=0 for NaN data values, 1 else
312                else
313                    check_bad=isnan(Field.(VarName)) | Field.(Field.ListVarName{errorvar(ivar)})~=0;%=0 for NaN or error flagged data values, 1 else
314                end
315                Field.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag
316                DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
317                NbData.(VarName)=NbData.(VarName)+ ~check_bad;% records the number of data for each point
318            end
319        end
320        %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
321    else
322        disp(errormsg)
323    end
324end
325%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
326
327for ivar=1:length(Field.ListVarName)
328    VarName=Field.ListVarName{ivar};
329    DataOut.(VarName)=DataOut.(VarName)./NbData.(VarName); % normalize the mean
330end
331if isempty(time) % time is read from files
332    if isfield(Field,'Time')
333        time_end=Field.Time(1);%last time read
334        if ~isempty(time_1)
335            DataOut.Time=time_1;
336            DataOut.Time_end=time_end;
337        end
338    end
339else  % time from ImaDoc prevails if it exists
340    DataOut.Time=time(1);
341    DataOut.Time_end=time(end);
342end
343
344%% writing the result file
345OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j);
346errormsg=struct2nc(OutputFile,DataOut); %save result file
347if isempty(errormsg)
348    disp([OutputFile ' written']);
349else
350    disp(['error in writting result file: ' errormsg])
351end
352
353%% open the result file with uvmat (in RUN mode)
354if checkrun
355    uvmat(OutputFile)% open the last result file with uvmat
356end
Note: See TracBrowser for help on using the repository browser.