source: trunk/src/series/civ_series.m @ 597

Last change on this file since 597 was 597, checked in by sommeria, 11 years ago

civ_series added

File size: 15.7 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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%civ_series
40
41function ParamOut=civ_series(Param)
42
43%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
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='off';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
49    ParamOut.FieldName='off';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
50    ParamOut.FieldTransform = 'off';...%can use a transform function
51    ParamOut.ProjObject='off';...%can use projection object(option 'off'/'on',
52    ParamOut.Mask='off';...%can use mask option   (option 'off'/'on', 'off' by default)
53    ParamOut.OutputDirExt='.civ';%set the output dir extension
54return
55end
56
57%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
58
59%% read input parameters from an xml file if input is a file name (batch mode)
60checkrun=1;
61if ischar(Param)
62    Param=xml2struct(Param);% read Param as input file (batch case)
63    checkrun=0;
64end
65
66ParamOut=Param; %default output
67if ~isfield(Param,'InputFields')
68    Param.InputFields.FieldName='';
69end
70OutputSubDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
71
72%% root input file type
73RootPath=Param.InputTable(:,1);
74RootFile=Param.InputTable(:,3);
75SubDir=Param.InputTable(:,2);
76NomType=Param.InputTable(:,4);
77FileExt=Param.InputTable(:,5);
78[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
79%%%%%%%%%%%%
80% The cell array filecell is the list of input file names, while
81% filecell{iview,fileindex}:
82%        iview: line in the table corresponding to a given file series
83%        fileindex: file index within  the file series,
84% 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
85% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
86%%%%%%%%%%%%
87NbSlice=1;%default
88if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
89    NbSlice=Param.IndexRange.NbSlice;
90end
91nbview=numel(i1_series);%number of input file series (lines in InputTable)
92nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
93nbfield_i=size(i1_series{1},2); %nb of fields for the i index
94nbfield=nbfield_j*nbfield_i; %total number of fields
95nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
96nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
97
98%determine the file type on each line from the first input file
99ImageTypeOptions={'image','multimage','mmreader','video'};
100NcTypeOptions={'netcdf','civx','civdata'};
101for iview=1:nbview
102    if ~exist(filecell{iview,1}','file')
103        displ_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
104        return
105    end
106    [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1});
107    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
108    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
109    if ~isempty(j1_series{iview})
110        frame_index{iview}=j1_series{iview};
111    else
112        frame_index{iview}=i1_series{iview};
113    end
114end
115
116
117%% calibration data and timing: read the ImaDoc files
118[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
119if size(time,1)>1
120    diff_time=max(max(diff(time)));
121    if diff_time>0
122        displ_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)],checkrun)
123    end   
124end
125
126%% coordinate transform or other user defined transform
127
128%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
129 % EDIT FROM HERE
130
131%% check the validity of  input file types
132if CheckImage{1}
133    FileExtOut='.png'; % write result as .png images for image inputs
134elseif CheckNc{1}
135    FileExtOut='.nc';% write result as .nc files for netcdf inputs
136else
137    displ_uvmat('ERROR',['invalid file type input ' FileType{1}],checkrun)
138    return
139end
140for iview=1:nbview
141        if ~isequal(CheckImage{iview},CheckImage{1})||~isequal(CheckNc{iview},CheckNc{1})
142        displ_uvmat('ERROR','input set of input series: need  either netcdf either image series',checkrun)
143    return
144    end
145end
146NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series
147% if checkrun==1
148%     ParamOut.Specific=[];%no specific parameter
149%     return %stop here for interactive input (option Param.Specific='?')
150% end
151
152%% Set field names and velocity types
153%use Param.InputFields for all views
154
155%% MAIN LOOP ON SLICES
156%%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
157
158index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
159nbfiles=0;
160nbmissing=0;
161
162%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
163%TODO: call civ_matlab
164for index=index_slice
165    if checkrun
166        stopstate=get(Param.RUNHandle,'BusyAction');
167        update_waitbar(Param.WaitbarHandle,index/nbfield)
168    else
169        stopstate='queue';
170    end
171    if ~isequal(stopstate,'queue')% enable STOP command
172        return
173    end
174    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
175    Data=cell(1,nbview);%initiate the set Data
176    nbtime=0;
177    for iview=1:nbview
178        %% reading input file(s)
179        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},Param.InputFields,frame_index{iview}(index));
180        if ~isempty(errormsg)
181            errormsg=['merge_proj/read_field/' errormsg];
182            display(errormsg)
183            break
184        end
185        timeread(iview)=0;
186        if isfield(Data{iview},'Time')
187            timeread(iview)=Data{iview}.Time;
188            nbtime=nbtime+1;
189        end
190        if ~isempty(NbSlice_calib)
191            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
192        end
193       
194        %% transform the input field (e.g; phys) if requested
195        if ~isempty(transform_fct)
196            if nargin(transform_fct)>=2
197                Data{iview}=transform_fct(Data{iview},XmlData{iview});
198            else
199                Data{iview}=transform_fct(Data{iview});
200            end
201        end
202       
203        %% check whether tps is needed, then calculate tps coefficients if needed
204        check_tps=0;
205        if isfield(Param.InputFields,'FieldName')
206            if ischar(Param.InputFields.FieldName)
207                Param.InputFields.FieldName={Param.InputFields.FieldName};
208            end
209        else
210            Param.InputFields.FieldName={};
211        end
212        for ilist=1:numel(Param.InputFields.FieldName)
213            switch Param.InputFields.FieldName{ilist}
214                case {'vort','div','strain'}
215                    check_tps=1;
216            end
217        end
218       
219        %% calculate tps coeff if needed
220        check_proj_tps= ~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'filter')&&~isfield(Data{iview},'Coord_tps');
221        Data{iview}=tps_coeff_field(Data{iview},check_proj_tps);
222       
223        %% projection on object (gridded plane)
224        if Param.CheckObject
225            [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject);
226            if ~isempty(errormsg)
227                displ_uvmat('ERROR',['error in merge_proge/proj_field: ' errormsg],checkrun)
228                return
229            end
230        end
231    end
232    %----------END LOOP ON VIEWS----------------------
233   
234    %% merge the nbview fields
235    MergeData=merge_field(Data);
236    if isfield(MergeData,'Txt')
237        displ_uvmat('ERROR',MergeData.Txt,checkrun)
238        return
239    end
240   
241    % time of the merged field:
242    if ~isempty(time)% time defined from ImaDoc
243        timeread=time(:,index);
244    end
245    timeread=mean(timeread);
246   
247    % generating the name of the merged field
248    i1=i1_series{iview}(index);
249    if ~isempty(i2_series{iview})
250        i2=i2_series{iview}(index);
251    else
252        i2=i1;
253    end
254    j1=1;
255    j2=1;
256    if ~isempty(j1_series{iview})
257        j1=j1_series{iview}(index);
258        if ~isempty(j2_series{iview})
259            j2=j2_series{iview}(index);
260        else
261            j2=j1;
262        end
263    end
264    OutputFile=fullfile_uvmat(RootPath{1},OutputSubDir,RootFile{1},FileExtOut,NomType{1},i1,i2,j1,j2);
265   
266    % recording the merged field
267    if CheckImage{1}    %in case of input images an image is produced
268        if isa(MergeData.A,'uint8')
269            bitdepth=8;
270        elseif isa(MergeData.A,'uint16')
271            bitdepth=16;
272        end
273        imwrite(MergeData.A,OutputFile,'BitDepth',bitdepth);
274        %write xml calibration file
275        siz=size(MergeData.A);
276        npy=siz(1);
277        npx=siz(2);
278        if isfield(MergeData,'VarAttribute')&&isfield(MergeData.VarAttribute{1},'Coord_2')&&isfield(MergeData.VarAttribute{1},'Coord_1')
279            Rangx=MergeData.VarAttribute{1}.Coord_2;
280            Rangy=MergeData.VarAttribute{1}.Coord_1;
281        elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY')
282            Rangx=[MergeData.AX(1) MergeData.AX(end)];
283            Rangy=[MergeData.AY(1) MergeData.AY(end)];
284        else
285            Rangx=[0.5 npx-0.5];
286            Rangy=[npy-0.5 0.5];%default
287        end
288        pxcmx=(npx-1)/(Rangx(2)-Rangx(1));
289        pxcmy=(npy-1)/(Rangy(1)-Rangy(2));
290        T_x=-pxcmx*Rangx(1)+0.5;
291        T_y=-pxcmy*Rangy(2)+0.5;
292        GeometryCal.focal=1;
293        GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1];
294        GeometryCal.Tx_Ty_Tz=[T_x T_y 1];
295        ImaDoc.GeometryCalib=GeometryCal;
296        %             t=struct2xml(ImaDoc);
297        %             t=set(t,1,'name','ImaDoc');
298        %             save(t,[filebase_merge '.xml'])
299        %             display([filebase_merge '.xml saved'])
300    else
301        MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'};
302        MergeData.Conventions='uvmat';
303        MergeData.nb_coord=2;
304        MergeData.nb_dim=2;
305        dt=[];
306        if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt)
307            dt=Data{1}.dt;
308        end
309        for iview =2:numel(Data)
310            if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt))
311                dt=[];%dt not the same for all fields
312            end
313        end
314        if isempty(dt)
315            MergeData.ListGlobalAttribute(6)=[];
316        else
317            MergeData.dt=dt;
318        end
319        MergeData.Time=timeread;
320        error=struct2nc(OutputFile,MergeData);%save result file
321        if isempty(error)
322            display(['output file ' OutputFile ' written'])
323        else
324            display(error)
325        end
326    end
327end
328
329%'merge_field': concatene fields
330%------------------------------------------------------------------------
331function MergeData=merge_field(Data)
332%% default output
333if isempty(Data)||~iscell(Data)
334    MergeData=[];
335    return
336end
337MergeData=Data{1};%default
338error=0;
339nbview=length(Data);
340if nbview==1
341    return
342end
343
344%% group the variables (fields of 'Data') in cells of variables with the same dimensions
345[CellVarIndex,NbDim,VarTypeCell]=find_field_cells(Data{1});
346%LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS
347% CellVarIndex=cells of variable index arrays
348for icell=1:length(CellVarIndex)
349    if NbDim(icell)==1
350        continue
351    end
352    VarIndex=CellVarIndex{icell};%  indices of the selected variables in the list FieldData.ListVarName
353    VarType=VarTypeCell{icell};
354    ivar_X=VarType.coord_x;
355    ivar_Y=VarType.coord_y;
356    ivar_FF=VarType.errorflag;
357    if isempty(ivar_X)
358        test_grid=1;%test for input data on regular grid (e.g. image)coordinates
359    else
360        if length(ivar_Y)~=1
361                displ_uvmat('ERROR','y coordinate missing in proj_field.m',checkrun)
362                return
363        end
364        test_grid=0;
365    end
366    %case of input fields with unstructured coordinates
367    if ~test_grid
368        for ivar=VarIndex
369            VarName=MergeData.ListVarName{ivar};
370            for iview=1:nbview
371                MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)];
372            end
373        end
374    %case of fields defined on a structured  grid
375    else 
376        testFF=0;
377        for iview=2:nbview
378            for ivar=VarIndex
379                VarName=MergeData.ListVarName{ivar};
380                if isfield(MergeData,'VarAttribute')
381                    if length(MergeData.VarAttribute)>=ivar && isfield(MergeData.VarAttribute{ivar},'Role') && isequal(MergeData.VarAttribute{ivar}.Role,'errorflag')
382                        testFF=1;
383                    end
384                end
385                MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);
386            end
387        end
388        if testFF
389            nbaver=nbview-MergeData.FF;
390            indgood=find(nbaver>0);
391            for ivar=VarIndex
392                VarName=MergeData.ListVarName{ivar};
393                MergeData.(VarName)(indgood)=double(MergeData.(VarName)(indgood))./nbaver(indgood);
394            end
395        else
396            for ivar=VarIndex
397                VarName=MergeData.ListVarName{ivar};
398                MergeData.(VarName)=double(MergeData.(VarName))./nbview;
399            end   
400        end
401    end
402end
403
404   
Note: See TracBrowser for help on using the repository browser.