source: trunk/src/series/merge_proj_special.m @ 783

Last change on this file since 783 was 783, checked in by sommeria, 10 years ago

-pb of mulitple tiff reading solved
-modif of output parma in get-file_type and find_file_series

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