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

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

bugs corrected to get an advancement bar with status

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