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

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

bugs corrected; Posiibility of usual 1D plots introduced with the new way of using get_field. time_series and aver_stat updated

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