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

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