source: trunk/src/series/time_series.m @ 596

Last change on this file since 596 was 596, checked in by sommeria, 12 years ago

corrections done in civ

File size: 18.9 KB
Line 
1%'time_series': extract a time series after projection on an object (points , line..)
2% this function can be used as a template for applying a global operation on a series of input fields
3%------------------------------------------------------------------------
4% function GUI_input=time_series(Param)
5%
6%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
7%
8%OUTPUT
9% ParamOut: sets options in the GUI series.fig needed for the function
10%
11%INPUT:
12% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
13% In batch mode, Param is the name of the corresponding xml file containing the same information
14% when Param.Action.RUN=0 (as activated when the current Action is selected
15% in series), the function ouput paramOut set the activation of the needed GUI elements
16%
17% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
18% see the current structure Param)
19%    .InputTable: cell of input file names, (several lines for multiple input)
20%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
21%    .OutputSubDir: name of the subdirectory for data outputs
22%    .OutputDirExt: directory extension for data outputs
23%    .Action: .ActionName: name of the current activated function
24%             .ActionPath:   path of the current activated function
25%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
26%             .RUN =0 for GUI input, =1 for function activation
27%             .RunMode='local','background', 'cluster': type of function  use
28%             
29%    .IndexRange: set the file or frame indices on which the action must be performed
30%    .FieldTransform: .TransformName: name of the selected transform function
31%                     .TransformPath:   path  of the selected transform function
32%    .InputFields: sub structure describing the input fields withfields
33%              .FieldName: name(s) of the field
34%              .VelType: velocity type
35%              .FieldName_1: name of the second field in case of two input series
36%              .VelType_1: velocity type of the second field in case of two input series
37%              .Coord_y: name of y coordinate variable
38%              .Coord_x: name of x coordinate variable
39%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41
42function ParamOut=time_series(Param)
43
44%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
45if isstruct(Param) && isequal(Param.Action.RUN,0)
46    ParamOut.AllowInputSort='off';...% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
47    ParamOut.WholeIndexRange='off';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
48    ParamOut.NbSlice='on'; ...%nbre of slices ('off' by default)
49    ParamOut.VelType='two';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
50    ParamOut.FieldName='two';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
51    ParamOut.FieldTransform = 'on';...%can use a transform function
52    ParamOut.ProjObject='on';...%can use projection object(option 'off'/'on',
53    ParamOut.Mask='off';...%can use mask option   (option 'off'/'on', 'off' by default)
54    ParamOut.OutputDirExt='.tseries';%set the output dir extension
55return
56end
57
58%%%%%%%%%%%% STANDARD PART  %%%%%%%%%%%%
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
67OutputDir=[Param.OutputSubDir Param.OutputDirExt];
68
69%% root input file(s) and type
70RootPath=Param.InputTable(:,1);
71RootFile=Param.InputTable(:,3);
72SubDir=Param.InputTable(:,2);
73NomType=Param.InputTable(:,4);
74FileExt=Param.InputTable(:,5);
75[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
76%%%%%%%%%%%%
77% The cell array filecell is the list of input file names, while
78% filecell{iview,fileindex}:
79%        iview: line in the table corresponding to a given file series
80%        fileindex: file index within  the file series,
81% 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
82% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
83%%%%%%%%%%%%
84NbSlice=1;%default
85if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
86    NbSlice=Param.IndexRange.NbSlice;
87end
88nbview=numel(i1_series);%number of input file series (lines in InputTable)
89nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
90nbfield_i=size(i1_series{1},2); %nb of fields for the i index
91nbfield=nbfield_j*nbfield_i; %total number of fields
92nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
93nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
94
95%determine the file type on each line from the first input file
96ImageTypeOptions={'image','multimage','mmreader','video'};
97NcTypeOptions={'netcdf','civx','civdata'};
98for iview=1:nbview
99    if ~exist(filecell{iview,1}','file')
100        displ_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
101        return
102    end
103    [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1});
104    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
105    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
106    if ~isempty(j1_series{iview})
107        frame_index{iview}=j1_series{iview};
108    else
109        frame_index{iview}=i1_series{iview};
110    end
111end
112
113%% calibration data and timing: read the ImaDoc files
114[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
115if size(time,1)>1
116    diff_time=max(max(diff(time)));
117    if diff_time>0
118        displ_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)],checkrun)
119    end   
120    time=time(1,:);% choose the time data from the first sequence
121end
122
123%% coordinate transform or other user defined transform
124transform_fct=[];%default
125if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
126    addpath(Param.FieldTransform.TransformPath)
127    transform_fct=str2func(Param.FieldTransform.TransformName);
128    rmpath(Param.FieldTransform.TransformPath)
129end
130
131%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
132 % EDIT FROM HERE
133
134%% check the validity of  ctinput file types
135if CheckImage{1}
136    FileExtOut='.png'; % write result as .png images for image inputs
137elseif CheckNc{1}
138    FileExtOut='.nc';% write result as .nc files for netcdf inputs
139else
140    displ_uvmat('ERROR',['invalid file type input ' FileType{1}],checkrun)
141    return
142end
143if nbview==2 && ~isequal(CheckImage{1},CheckImage{2})
144        displ_uvmat('ERROR','input must be two image series or two netcdf file series',checkrun)
145    return
146end
147NomTypeOut='_1-2_1';% output file index will indicate the first and last ref index in the series
148if checkrun==1
149    return % stop here for input checks
150end
151
152%% Set field names and velocity types
153InputFields{1}=[];%default (case of images)
154if isfield(Param,'InputFields')
155    InputFields{1}=Param.InputFields;
156end
157if nbview==2
158    InputFields{2}=[];%default (case of images)
159    if isfield(Param,'InputFields')
160        InputFields{2}=Param.InputFields{1};%default
161        if isfield(Param.InputFields,'FieldName_1')
162            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
163            if isfield(Param.InputFields,'VelType_1')
164                InputFields{2}.VelType=Param.InputFields.VelType_1;
165            end
166        end
167    end
168end
169
170%% Initiate output fields
171%initiate the output structure as a copy of the first input one (reproduce fields)
172[DataOut,tild,errormsg] = read_field(filecell{1,1},FileType{1},InputFields{1},1);
173if ~isempty(errormsg)
174    displ_uvmat('ERROR',['error reading ' filecell{1,1} ': ' errormsg],checkrun)
175    return
176end
177time_1=[];
178if isfield(DataOut,'Time')
179    time_1=DataOut.Time(1);
180end
181if CheckNc{iview}
182    if isempty(strcmp('Conventions',DataOut.ListGlobalAttribute))
183        DataOut.ListGlobalAttribute=['Conventions' DataOut.ListGlobalAttribute];
184    end
185    DataOut.Conventions='uvmat';
186    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {Param.Action}];
187    ActionKey='Action';
188    while isfield(DataOut,ActionKey)
189        ActionKey=[ActionKey '_1'];
190    end
191    DataOut.(ActionKey)=Param.Action;
192    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {ActionKey}];
193    if isfield(DataOut,'Time')
194        DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {'Time','Time_end'}];
195    end
196end
197
198%% LOOP ON SLICES
199nbmissing=0; %number of undetected files
200for i_slice=1:NbSlice
201    index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
202    nbfile=0;
203    nbmissing=0;
204   
205    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
206    for index=index_slice 
207        if checkrun
208            stopstate=get(Param.RUNHandle,'BusyAction');
209            update_waitbar(Param.WaitbarHandle,index/nbfield)
210        else
211            stopstate='queue';
212        end
213        if isequal(stopstate,'queue')% enable STOP command
214            Data=cell(1,nbview);%initiate the set Data;
215            nbtime=0;
216            dt=[];
217            %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
218            for iview=1:nbview
219                % reading input file(s)
220                [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
221                if ~isempty(errormsg)
222                    errormsg=['time_series / read_field / ' errormsg];
223                    display(errormsg)
224                    break
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            end
230            if isempty(errormsg)
231            Field=Data{1}; % default input field structure
232            % coordinate transform (or other user defined transform)
233            if ~isempty(transform_fct)
234                switch nargin(transform_fct)
235                    case 4
236                        if length(Data)==2
237                            Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
238                        else
239                            Field=transform_fct(Data{1},XmlData{1});
240                        end
241                    case 3
242                        if length(Data)==2
243                            Field=transform_fct(Data{1},XmlData{1},Data{2});
244                        else
245                            Field=transform_fct(Data{1},XmlData{1});
246                        end
247                    case 2
248                        Field=transform_fct(Data{1},XmlData{1});
249                    case 1
250                        Field=transform_fct(Data{1});
251                end
252            end
253           
254            % calculate tps coefficients if needed
255            if isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
256                Field=tps_coeff_field(Field,check_proj_tps);
257            end
258           
259            %field projection on an object
260            if Param.CheckObject
261                [Field,errormsg]=proj_field(Field,Param.ProjObject);
262                if ~isempty(errormsg)
263                    msgbox_uvmat('ERROR',['time_series / proj_field / ' errormsg])
264                    return
265                end
266            end
267            nbfile=nbfile+1;
268           
269            % initiate the time series at the first iteration
270            if nbfile==1
271                % stop program if the first field reading is in error
272                if ~isempty(errormsg)
273                    displ_uvmat('ERROR',['time_series / sub_field / ' errormsg],checkrun)
274                    return
275                end
276                DataOut=Field;%default
277                DataOut.NbDim=Field.NbDim+1; %add the time dimension for plots
278                nbvar=length(Field.ListVarName);
279                if nbvar==0
280                    displ_uvmat('ERROR','no input variable selected',checkrun)
281                    return
282                end
283                testsum=2*ones(1,nbvar);%initiate flag for action on each variable
284                if isfield(Field,'VarAttribute') % look for coordinate and flag variables
285                    for ivar=1:nbvar
286                        if length(Field.VarAttribute)>=ivar && isfield(Field.VarAttribute{ivar},'Role')
287                            var_role=Field.VarAttribute{ivar}.Role;%'role' of the variable
288                            if isequal(var_role,'errorflag')
289                                displ_uvmat('ERROR','do not handle error flags in time series',checkrun)
290                                return
291                            end
292                            if isequal(var_role,'warnflag')
293                                testsum(ivar)=0;  % not recorded variable
294                                eval(['DataOut=rmfield(DataOut,''' Field.ListVarName{ivar} ''');']);%remove variable
295                            end
296                            if isequal(var_role,'coord_x')| isequal(var_role,'coord_y')|...
297                                    isequal(var_role,'coord_z')|isequal(var_role,'coord')
298                                testsum(ivar)=1; %constant coordinates, record without time evolution
299                            end
300                        end
301                        % check whether the variable ivar is a dimension variable
302                        DimCell=Field.VarDimName{ivar};
303                        if ischar(DimCell)
304                            DimCell={DimCell};
305                        end
306                        if numel(DimCell)==1 && isequal(Field.ListVarName{ivar},DimCell{1})%detect dimension variables
307                            testsum(ivar)=1;
308                        end
309                    end
310                end
311                for ivar=1:nbvar
312                    if testsum(ivar)==2
313                        eval(['DataOut.' Field.ListVarName{ivar} '=[];'])
314                    end
315                end
316                DataOut.ListVarName=[{'Time'} DataOut.ListVarName];
317            end
318           
319            % add data to the current field
320            for ivar=1:length(Field.ListVarName)
321                VarName=Field.ListVarName{ivar};
322                VarVal=Field.(VarName);
323                if testsum(ivar)==2% test for recorded variable
324                    if isempty(errormsg)
325                        if isequal(Param.ProjObject.ProjMode,'inside')% take the average in the domain for 'inside' mode
326                            if isempty(VarVal)
327                                displ_uvmat('ERROR',['empty result at frame index ' num2str(i1_series{iview}(index))],checkrun)
328                                return
329                            end
330                            VarVal=mean(VarVal,1);
331                        end
332                        VarVal=shiftdim(VarVal,-1); %shift dimension
333                        DataOut.(VarName)=cat(1,DataOut.(VarName),VarVal);%concanete the current field to the time series
334                    else
335                        DataOut.(VarName)=cat(1,DataOut.(VarName),0);% put each variable to 0 in case of input reading error
336                    end
337                elseif testsum(ivar)==1% variable representing fixed coordinates
338                    VarInit=DataOut.(VarName);
339                    if isempty(errormsg) && ~isequal(VarVal,VarInit)
340                        displ_uvmat('ERROR',['time series requires constant coordinates ' VarName],checkrun)
341                        return
342                    end
343                end
344            end
345           
346            % record the time:
347            if isempty(time)% time not set by xml filer(s)
348                if isfield(Data{1},'Time')
349                    DataOut.Time(nbfile,1)=Field.Time;
350                else
351                    DataOut.Time(nbfile,1)=index;%default
352                end
353            else % time from ImaDoc prevails  TODO: correct
354                DataOut.Time(nbfile,1)=time(index);%
355            end
356           
357            % record the number of missing input fields
358            if ~isempty(errormsg)
359                nbmissing=nbmissing+1;
360                display(['index=' num2str(index) ':' errormsg])
361            end
362            end
363        end
364    end
365    %%%%%%% END OF LOOP WITHIN A SLICE
366   
367    %remove time for global attributes if exists
368    Time_index=find(strcmp('Time',DataOut.ListGlobalAttribute));
369    if ~isempty(Time_index)
370        DataOut.ListGlobalAttribute(Time_index)=[];
371    end
372    DataOut.Conventions='uvmat';
373    for ivar=1:numel(DataOut.ListVarName)
374        VarName=DataOut.ListVarName{ivar};
375        eval(['DataOut.' VarName '=squeeze(DataOut.' VarName ');']) %remove singletons
376    end
377   
378    % add time dimension
379    for ivar=1:length(Field.ListVarName)
380        DimCell=Field.VarDimName(ivar);
381        if testsum(ivar)==2%variable used as time series
382            DataOut.VarDimName{ivar}=[{'Time'} DimCell];
383        elseif testsum(ivar)==1
384            DataOut.VarDimName{ivar}=DimCell;
385        end
386    end
387    indexremove=find(~testsum);
388    if ~isempty(indexremove)
389        DataOut.ListVarName(1+indexremove)=[];
390        DataOut.VarDimName(indexremove)=[];
391        if isfield(DataOut,'Role') && ~isempty(DataOut.Role{1})%generaliser aus autres attributs
392            DataOut.Role(1+indexremove)=[];
393        end
394    end
395   
396    %shift variable attributes
397    if isfield(DataOut,'VarAttribute')
398        DataOut.VarAttribute=[{[]} DataOut.VarAttribute];
399    end
400    DataOut.VarDimName=[{'Time'} DataOut.VarDimName];
401    DataOut.Action=Param.Action;%name of the processing programme
402    test_time=diff(DataOut.Time)>0;% test that the readed time is increasing (not constant)
403    if ~test_time
404        DataOut.Time=1:filecounter;
405    end
406   
407    % display nbmissing
408    if ~isequal(nbmissing,0)
409        displ_uvmat('WARNING',[num2str(nbmissing) ' files skipped: missing files or bad input, see command window display'],checkrun)
410    end
411   
412    %name of result file
413    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(1),i1_series{1}(end),i_slice,[]);
414    errormsg=struct2nc(OutputFile,DataOut); %save result file
415    if isempty(errormsg)
416        display([OutputFile ' written'])
417    else
418        displ_uvmat('ERROR',['error in Series/struct2nc: ' errormsg],checkrun)
419    end
420end
421
422%% plot the time series (the last one in case of multislices)
423if checkrun
424    figure
425    haxes=axes;
426    plot_field(DataOut,haxes)
427       
428    %% display the result file using the GUI get_field
429    hget_field=findobj(allchild(0),'name','get_field');
430    if ~isempty(hget_field)
431        delete(hget_field)
432    end
433    get_field(OutputFile,DataOut)
434end
435
436
Note: See TracBrowser for help on using the repository browser.