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

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

commit the series fcts with the new form

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