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

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

functions upadated in series

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