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

Last change on this file since 606 was 606, checked in by sommeria, 8 years ago

bug on compilation solved (still to test with transform_field fct). faster browser inrtroduced. Various bug corrections

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