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

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

corrections done in civ

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
[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)
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
[27]56end
57
[526]58%%%%%%%%%%%% STANDARD PART  %%%%%%%%%%%%
[592]59%% read input parameters from an xml file if input is a file name (batch mode)
60checkrun=1;
[457]61if ischar(Param)
[592]62    Param=xml2struct(Param);% read Param as input file (batch case)
63    checkrun=0;
[374]64end
[592]65
[462]66ParamOut=Param; %default output
[474]67OutputDir=[Param.OutputSubDir Param.OutputDirExt];
[457]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);
[374]75[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[526]76%%%%%%%%%%%%
77% The cell array filecell is the list of input file names, while
78% filecell{iview,fileindex}:
[457]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
[526]83%%%%%%%%%%%%
[457]84NbSlice=1;%default
85if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
86    NbSlice=Param.IndexRange.NbSlice;
[374]87end
[457]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
[374]94
[457]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')
[474]100        displ_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
[27]101        return
102    end
[457]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
[27]111end
112
[457]113%% calibration data and timing: read the ImaDoc files
[470]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
[457]116    diff_time=max(max(diff(time)));
117    if diff_time>0
[474]118        displ_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)],checkrun)
[457]119    end   
[531]120    time=time(1,:);% choose the time data from the first sequence
[457]121end
[27]122
[457]123%% coordinate transform or other user defined transform
[526]124transform_fct=[];%default
[478]125if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[474]126    addpath(Param.FieldTransform.TransformPath)
127    transform_fct=str2func(Param.FieldTransform.TransformName);
128    rmpath(Param.FieldTransform.TransformPath)
[457]129end
[474]130
[457]131%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
132 % EDIT FROM HERE
[27]133
[526]134%% check the validity of  ctinput file types
[457]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
[474]140    displ_uvmat('ERROR',['invalid file type input ' FileType{1}],checkrun)
[457]141    return
[27]142end
[457]143if nbview==2 && ~isequal(CheckImage{1},CheckImage{2})
[474]144        displ_uvmat('ERROR','input must be two image series or two netcdf file series',checkrun)
[259]145    return
146end
[457]147NomTypeOut='_1-2_1';% output file index will indicate the first and last ref index in the series
[478]148if checkrun==1
149    return % stop here for input checks
150end
[457]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
[27]169
[457]170%% Initiate output fields
171%initiate the output structure as a copy of the first input one (reproduce fields)
[464]172[DataOut,tild,errormsg] = read_field(filecell{1,1},FileType{1},InputFields{1},1);
[457]173if ~isempty(errormsg)
[474]174    displ_uvmat('ERROR',['error reading ' filecell{1,1} ': ' errormsg],checkrun)
[457]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
[190]198%% LOOP ON SLICES
[330]199nbmissing=0; %number of undetected files
[27]200for i_slice=1:NbSlice
[474]201    index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
[478]202    nbfile=0;
[474]203    nbmissing=0;
204   
205    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
[592]206    for index=index_slice 
[442]207        if checkrun
[592]208            stopstate=get(Param.RUNHandle,'BusyAction');
209            update_waitbar(Param.WaitbarHandle,index/nbfield)
[442]210        else
211            stopstate='queue';
212        end
[27]213        if isequal(stopstate,'queue')% enable STOP command
[531]214            Data=cell(1,nbview);%initiate the set Data;
[478]215            nbtime=0;
216            dt=[];
[526]217            %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
[330]218            for iview=1:nbview
[474]219                % reading input file(s)
[526]220                [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
[474]221                if ~isempty(errormsg)
[531]222                    errormsg=['time_series / read_field / ' errormsg];
[474]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
[536]230            if isempty(errormsg)
[526]231            Field=Data{1}; % default input field structure
[474]232            % coordinate transform (or other user defined transform)
233            if ~isempty(transform_fct)
[526]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});
[27]251                end
[474]252            end
[478]253           
[526]254            % calculate tps coefficients if needed
[583]255            if isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
[585]256                Field=tps_coeff_field(Field,check_proj_tps);
[474]257            end
[526]258           
259            %field projection on an object
[474]260            if Param.CheckObject
261                [Field,errormsg]=proj_field(Field,Param.ProjObject);
[526]262                if ~isempty(errormsg)
[531]263                    msgbox_uvmat('ERROR',['time_series / proj_field / ' errormsg])
[526]264                    return
265                end
[474]266            end
[478]267            nbfile=nbfile+1;
[474]268           
269            % initiate the time series at the first iteration
[478]270            if nbfile==1
[474]271                % stop program if the first field reading is in error
272                if ~isempty(errormsg)
[531]273                    displ_uvmat('ERROR',['time_series / sub_field / ' errormsg],checkrun)
[474]274                    return
[27]275                end
[474]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
[531]280                    displ_uvmat('ERROR','no input variable selected',checkrun)
[474]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
[27]291                            end
[474]292                            if isequal(var_role,'warnflag')
293                                testsum(ivar)=0;  % not recorded variable
294                                eval(['DataOut=rmfield(DataOut,''' Field.ListVarName{ivar} ''');']);%remove variable
[107]295                            end
[474]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
[107]299                            end
[27]300                        end
[474]301                        % check whether the variable ivar is a dimension variable
302                        DimCell=Field.VarDimName{ivar};
303                        if ischar(DimCell)
304                            DimCell={DimCell};
[27]305                        end
[474]306                        if numel(DimCell)==1 && isequal(Field.ListVarName{ivar},DimCell{1})%detect dimension variables
307                            testsum(ivar)=1;
308                        end
[27]309                    end
310                end
[474]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)
[478]327                                displ_uvmat('ERROR',['empty result at frame index ' num2str(i1_series{iview}(index))],checkrun)
[474]328                                return
[27]329                            end
[474]330                            VarVal=mean(VarVal,1);
[27]331                        end
[474]332                        VarVal=shiftdim(VarVal,-1); %shift dimension
333                        DataOut.(VarName)=cat(1,DataOut.(VarName),VarVal);%concanete the current field to the time series
[330]334                    else
[474]335                        DataOut.(VarName)=cat(1,DataOut.(VarName),0);% put each variable to 0 in case of input reading error
[330]336                    end
[474]337                elseif testsum(ivar)==1% variable representing fixed coordinates
[550]338                    VarInit=DataOut.(VarName);
[474]339                    if isempty(errormsg) && ~isequal(VarVal,VarInit)
340                        displ_uvmat('ERROR',['time series requires constant coordinates ' VarName],checkrun)
341                        return
342                    end
[27]343                end
[474]344            end
345           
346            % record the time:
[531]347            if isempty(time)% time not set by xml filer(s)
348                if isfield(Data{1},'Time')
[478]349                    DataOut.Time(nbfile,1)=Field.Time;
[474]350                else
[478]351                    DataOut.Time(nbfile,1)=index;%default
[330]352                end
[474]353            else % time from ImaDoc prevails  TODO: correct
[531]354                DataOut.Time(nbfile,1)=time(index);%
[27]355            end
[474]356           
357            % record the number of missing input fields
358            if ~isempty(errormsg)
359                nbmissing=nbmissing+1;
[478]360                display(['index=' num2str(index) ':' errormsg])
[474]361            end
[536]362            end
[27]363        end
364    end
[330]365    %%%%%%% END OF LOOP WITHIN A SLICE
[474]366   
[27]367    %remove time for global attributes if exists
[459]368    Time_index=find(strcmp('Time',DataOut.ListGlobalAttribute));
[447]369    if ~isempty(Time_index)
[459]370        DataOut.ListGlobalAttribute(Time_index)=[];
[27]371    end
[459]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
[27]376    end
[330]377   
[474]378    % add time dimension
[330]379    for ivar=1:length(Field.ListVarName)
380        DimCell=Field.VarDimName(ivar);
381        if testsum(ivar)==2%variable used as time series
[459]382            DataOut.VarDimName{ivar}=[{'Time'} DimCell];
[330]383        elseif testsum(ivar)==1
[459]384            DataOut.VarDimName{ivar}=DimCell;
[27]385        end
[330]386    end
[27]387    indexremove=find(~testsum);
388    if ~isempty(indexremove)
[459]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)=[];
[27]393        end
394    end
[330]395   
[27]396    %shift variable attributes
[459]397    if isfield(DataOut,'VarAttribute')
398        DataOut.VarAttribute=[{[]} DataOut.VarAttribute];
[330]399    end
[459]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)
[330]403    if ~test_time
[531]404        DataOut.Time=1:filecounter;
[330]405    end
[107]406   
[330]407    % display nbmissing
408    if ~isequal(nbmissing,0)
[474]409        displ_uvmat('WARNING',[num2str(nbmissing) ' files skipped: missing files or bad input, see command window display'],checkrun)
[330]410    end
411   
[27]412    %name of result file
[474]413    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(1),i1_series{1}(end),i_slice,[]);
[459]414    errormsg=struct2nc(OutputFile,DataOut); %save result file
[27]415    if isempty(errormsg)
[459]416        display([OutputFile ' written'])
[27]417    else
[474]418        displ_uvmat('ERROR',['error in Series/struct2nc: ' errormsg],checkrun)
[27]419    end
420end
[107]421
[330]422%% plot the time series (the last one in case of multislices)
[474]423if checkrun
[478]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)
[474]434end
[27]435
436
Note: See TracBrowser for help on using the repository browser.