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

Last change on this file since 190 was 190, checked in by sommeria, 14 years ago

various bug repairs and cleaning, pb of dates in french with check_files

File size: 22.8 KB
Line 
1%'time_series': extract a time series, used with series.fig
2%------------------------------------------------------------------------
3% function GUI_input=time_series(num_i1,num_i2,num_j1,num_j2,Series)
4%
5%OUTPUT
6% GUI_input=list of options in the GUI series.fig needed for the function
7%
8%INPUT:
9%num_i1: series of first indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
10%num_i2: series of second indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
11%num_j1: series of first indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ )
12%num_j2: series of second indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ)
13%Series: Matlab structure containing information set by the series interface
14%
15function GUI_input=time_series(num_i1,num_i2,num_j1,num_j2,Series)
16
17%requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
18if ~exist('num_i1','var')
19    GUI_input={'RootPath';'two';...%nbre of possible input series (options 'on'/'two'/'many', default:'one')
20        'SubDir';'on';... % subdirectory of derived files (PIV fields), ('on' by default)
21        'RootFile';'on';... %root input file name ('on' by default)
22        'FileExt';'on';... %input file extension ('on' by default)
23        'NomType';'on';...%type of file indexing ('on' by default)
24        'NbSlice';'on'; ...%nbre of slices ('off' by default)
25        'VelTypeMenu';'two';...% menu for selecting the velocity type (civ1,..) options 'off'/'one'/'two', 'off' by default)
26        'FieldMenu';'two';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
27        'CoordType';'on';...%can use a transform function 'off' by default
28        'GetObject';'on';...%can use projection object ,'off' by default
29        %'GetMask';'on'...%can use mask option   ,'off' by default
30        %'PARAMETER'; options: name of the user defined parameter',repeat a line for each parameter
31               ''};
32    return %exit the function
33end
34
35%------------------------------------------------------
36hseries=guidata(Series.hseries);%handles in the GUI series
37WaitbarPos=get(hseries.waitbar_frame,'Position'); %position of the waitbar frame
38
39%% projection object
40test_object=get(hseries.GetObject,'Value');
41if test_object
42    hset_object=findobj(allchild(0),'tag','set_object');
43    ProjObject=read_set_object(guidata(hset_object));
44    %answeryes=questdlg({['field series projected on ' Series.ProjObject.Style]});
45    answeryes=msgbox_uvmat('INPUT_Y-N',['field series projected on ' ProjObject.Style]);
46    if ~isequal(answeryes,'Yes')
47        return
48    end
49else
50    msgbox_uvmat('ERROR','a projection object is needed');
51    return
52end
53
54%% root names
55if iscell(Series.RootPath)
56    RootPath=Series.RootPath;
57    RootFile=Series.RootFile;
58    SubDir=Series.SubDir;
59    FileExt=Series.FileExt;
60    NomType=Series.NomType;
61else
62    RootPath={Series.RootPath};
63    RootFile={Series.RootFile};
64    SubDir={Series.SubDir};
65    FileExt={Series.FileExt};
66    NomType={Series.NomType};
67    num_i1={num_i1};
68    num_i2={num_i2};
69    num_j1={num_j1};
70    num_j2={num_j2};
71end
72ext=FileExt{1};
73form=imformats(ext([2:end]));%test valid Matlab image formats
74testima=0;
75if ~isempty(form)||isequal(lower(ext),'.avi')
76    testima=1;
77end
78nbview=length(RootPath);%number of series (1 or 2)
79nbfield=size(num_i1{1},1)*size(num_i1{1},2); %number of fields in the time series
80
81%Number of input series: this function  accepts only one or two input file series (sub_field is used in the latter case)
82nbview=length(RootPath);
83
84%determine image type
85hhh=which('mmreader');
86for iview=1:nbview
87    if isequal(FileExt{iview},'.nc')||isequal(FileExt{iview},'.cdf')
88        FileType{iview}='netcdf';
89    elseif isequal(lower(FileExt{iview}),'.avi')
90        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
91            MovieObject{iview}=mmreader(fullfile(RootPath{iview},[RootFile{iview} FileExt{iview}]));
92            FileType{iview}='movie';
93        else
94            FileType{iview}='avi';
95        end
96    elseif isequal(lower(FileExt{iview}),'.vol')
97        FileType{iview}='vol';
98    else
99       form=imformats(FileExt{iview}(2:end));
100       if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
101           if isequal(NomType{iview},'*');
102               FileType{iview}='multimage';
103           else
104               FileType{iview}='image';
105           end
106       end
107    end
108end
109filebase{1}=fullfile(RootPath{1},RootFile{1});
110
111% number of slices
112NbSlice=str2num(get(hseries.NbSlice,'String'));
113if isempty(NbSlice)
114    NbSlice=1;
115end
116NbSlice_name=num2str(NbSlice);
117
118% Field and velocity type (the same for the two views)
119if isfield(Series,'Field')
120    FieldName=Series.Field;%the same set of fields for all views
121else
122    FieldName={''};
123end
124if isequal(FieldName,{'get_field...'})
125    hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
126    if numel(hget_field)>1
127        delete(hget_field(2:end)) % delete multiple occurerence of the GUI get_fioeld
128    elseif isempty(hget_field)
129       filename=name_generator(filebase{1},num_i1{1}(1),num_j1{1}(1),FileExt{1},NomType{1},1,num_i2{1}(1),num_j2{1}(1),SubDir{1});
130       idetect(iview)=exist(filename,'file');
131       hget_field=get_field(filename);
132       return
133    end
134    SubField=read_get_field(hget_field); %read the names of the variables to plot in the get_field GUI
135    if isempty(SubField)
136        delete(hget_field)
137       filename=name_generator(filebase{1},num_i1{1}(1),num_j1{1}(1),FileExt{1},NomType{1},1,num_i2{1}(1),num_j2{1}(1),SubDir{1});
138        hget_field=get_field(filename);
139        SubField=read_get_field(hget_field); %read the names of the variables to plot in the get_field GUI
140    end
141end
142
143%detect whether the two files are 'images' or 'netcdf'
144
145testcivx=0;
146FileExt=get(hseries.FileExt,'String');
147if ~isequal(FieldName,{'get_field...'})
148    testcivx=isequal(FileType{1},'netcdf');
149end
150
151VelType_str=get(hseries.VelTypeMenu,'String');
152VelType_val=get(hseries.VelTypeMenu,'Value');
153VelType{1}=VelType_str{VelType_val};
154if nbview==2
155    VelType_str=get(hseries.VelTypeMenu_1,'String');
156    VelType_val=get(hseries.VelTypeMenu_1,'Value');
157    VelType{2}=VelType_str{VelType_val};
158end
159
160%% Calibration data and timing: read the ImaDoc files
161mode=''; %default
162timecell={};
163XmlData={};
164itime=0;
165NbSlice_calib={};
166for iview=1:nbview%Loop on views
167    XmlData{iview}=[];%default
168    filebase{iview}=fullfile(RootPath{iview},RootFile{iview});
169    if exist([filebase{iview} '.xml'],'file')
170        [XmlData{iview},error]=imadoc2struct([filebase{iview} '.xml']);
171        if isfield(XmlData{iview},'Time')
172            itime=itime+1;
173            timecell{itime}=XmlData{iview}.Time;
174        end
175        if isfield(XmlData{iview},'GeometryCalib') && isfield(XmlData{iview}.GeometryCalib,'SliceCoord')
176            NbSlice_calib{iview}=size(XmlData{iview}.GeometryCalib.SliceCoord,1);%nbre of slices for Zindex in phys transform
177            if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
178                msgbox_uvmat('WARNING','inconsistent number of Z indices for the field series');
179            end
180        end
181    elseif exist([filebase{iview} '.civ'],'file')
182        [error,time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([filebase{iview} '.civ']);
183        itime=itime+1;
184        timecell{itime}=time;
185        XmlData{iview}.Time=time;
186        GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
187        GeometryCalib.Tx=0;
188        GeometryCalib.Ty=0;
189        GeometryCalib.Tz=1;
190        GeometryCalib.dpx=1;
191        GeometryCalib.dpy=1;
192        GeometryCalib.sx=1;
193        GeometryCalib.Cx=0;
194        GeometryCalib.Cy=0;
195        GeometryCalib.f=1;
196        GeometryCalib.kappa1=0;
197        GeometryCalib.CoordUnit='cm';
198        XmlData{iview}.GeometryCalib=GeometryCalib;
199        if error==1
200            msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
201        end
202    end
203end
204
205%% check coincidence in time
206multitime=0;
207if length(timecell)==0
208    time=[];
209elseif length(timecell)==1
210    time=timecell{1};
211elseif length(timecell)>1
212    multitime=1;
213    for icell=1:length(timecell)
214        if ~isequal(size(timecell{icell}),size(timecell{1}))
215            msgbox_uvmat('WARNING','inconsistent time array dimensions in ImaDoc fields, the time for the first series is used')
216            time=timecell{1};
217            multitime=0;
218            break
219        end
220    end
221end
222if multitime
223    for icell=1:length(timecell)
224        time(icell,:,:)=timecell{icell};
225    end
226    diff_time=max(max(diff(time)));
227    if diff_time>0
228        msgbox_uvmat('WARNING',['times of series differ by more than ' num2str(diff_time)])
229    end   
230end
231if size(time,2) < num_i2{1}(end) || size(time,3) < num_j2{1}(end)% ime array absent or too short in ImaDoc xml file'
232    time=[];
233end
234
235%%  Root name of output files (TO GENERALISE FOR TWO INPUT SERIES)
236subdir_result='time_series';
237pathdir=fullfile(RootPath{1},subdir_result);
238while exist(pathdir,'dir')
239    pathdir=[pathdir '.0'];
240end
241[m1,m2,m3]=mkdir(pathdir);
242if ~isequal(m2,'')
243     msgbox_uvmat('CONFIRMATION',m2);%error message for directory creation
244end
245[xx,msg2] = fileattrib(pathdir,'+w','g'); %yield writing access (+w) to user group (g)
246if ~strcmp(msg2,'')
247    msgbox_uvmat('ERROR',['pb of permission for ' subdir_result ': ' msg2])%error message for directory creation
248    return
249end
250
251filebase_out=filebase{1};
252NomTypeOut=nomtype2pair(NomType{1},num_i2{end}(end)-num_i1{1}(1),num_j2{end}(end)-num_j1{1}(1));
253
254%% coordinate transform or other user defined transform
255transform_fct=[];%default
256if isfield(Series,'transform_fct')
257    transform_fct=Series.transform_fct;
258end
259
260%% velocity type
261VelType_str=get(hseries.VelTypeMenu,'String');
262VelType_val=get(hseries.VelTypeMenu,'Value');
263VelType{1}=VelType_str{VelType_val};
264if nbview==2
265    VelType_str=get(hseries.VelTypeMenu_1,'String');
266    VelType_val=get(hseries.VelTypeMenu_1,'Value');
267    VelType{2}=VelType_str{VelType_val};
268end
269
270%% LOOP ON SLICES
271for i_slice=1:NbSlice
272     dt=[];
273     nbmissing=0; %number of undetected files
274     nbfiles=0;
275    %%%%%%%%%%%%%%%%%%%%%%%%%%%%LOOP ON FIELDS IN  A SLICE
276    for ifile=i_slice:NbSlice:nbfield 
277        stopstate=get(hseries.RUN,'BusyAction');
278        if isequal(stopstate,'queue')% enable STOP command
279             update_waitbar(hseries.waitbar,WaitbarPos,ifile/nbfield)
280             for iview=1:nbview
281                filename=...
282                           name_generator(filebase{iview},num_i1{iview}(ifile),num_j1{iview}(ifile),FileExt{iview},NomType{iview},1,num_i2{iview}(ifile),num_j2{iview}(ifile),SubDir{iview});
283                idetect(iview)=exist(filename,'file');
284                Data{iview}=[]; %default     
285                if ~isequal(FileType{iview},'netcdf')               
286                    Data{iview}.ListVarName={'A'};
287                    Data{iview}.AName='image';
288                    switch FileType{iview}
289                        case 'movie'
290                            A=read(MovieObject{iview},num_i1{iview}(ifile));
291                        case 'avi'
292                            mov=aviread(filename,num_i1{iview}(ifile));
293                            A=frame2im(mov(1));
294                        case 'vol'
295                            A=imread(filename);
296                        case 'multimage'
297                            A=imread(filename,num_i1{iview}(ifile));
298                        case 'image'
299                            A=imread(filename);
300                    end
301                    Data{iview}.ListVarName={'AY','AX','A'}; %
302                    npy=size(A,1);
303                    npx=size(A,2);
304                    nbcolor=size(A,3);
305                    if nbcolor==3
306                         Data{iview}.VarDimName={'AY','AX',{'AY','AX','rgb'}};
307                    else
308                         Data{iview}.VarDimName={'AY','AX',{'AY','AX'}};
309                    end 
310                    Data{iview}.AY=[npy-0.5 0.5];
311                    Data{iview}.AX=[0.5 npx-0.5];
312                    Data{iview}.A=double(A);
313                    Data{iview}.CoordUnit='pixel';
314                elseif testcivx
315                    [Data{iview},VelTypeOut]=read_civxdata(filename,FieldName,VelType);
316                else
317                    [Data{iview},var_detect]=nc2struct(filename,SubField.ListVarName); %read the corresponding input data               
318                    Data{iview}.VarAttribute=SubField.VarAttribute;
319                end
320                if ~isempty(NbSlice_calib)  % z index
321                    Data{iview}.ZIndex=mod(num_i1{iview}(ifile)-1,NbSlice_calib{1})+1;
322                end
323             end
324             
325             % coordinate transform (or other user defined transform)
326            if ~isempty(transform_fct)
327                 % z index
328                if ~isempty(NbSlice_calib)
329                    Data{iview}.ZIndex=mod(num_i1{iview}(ifile)-1,NbSlice_calib{1})+1;%Zindex for phys transform
330                end
331                if nbview==2
332                    [Data{1},Data{2}]=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
333                    if isempty(Data{2})
334                        Data(2)=[];
335                    end
336                else
337                    Data{1}=transform_fct(Data{1},XmlData{1});
338                end
339            end     
340            if testcivx
341                    Data{iview}=calc_field(FieldName,Data{iview});%calculate field (vort..)
342            end
343            if length(Data)==2
344                [Field,errormsg]=sub_field(Data{1},Data{2}); %substract the two fields
345                if ~isempty(errormsg)
346                    msgbox_uvmat('ERROR',['error in time_series/sub_field:' errormsg])
347                    return
348                end
349            else
350                Field=Data{1};
351            end
352            if test_object
353                [Field,errormsg]=proj_field(Field,ProjObject);
354                if ~isempty(errormsg)
355                    msgbox_uvmat('ERROR',['error in time_series/proj_field:' errormsg])
356                    return
357                end
358            end
359            if min(idetect)>=1% the input file(s) have been detected         
360                nbfiles=nbfiles+1;
361                if nbfiles==1 %first field: initiate the time series
362                    RecordData=Field;%default
363                    RecordData.NbDim=Field.NbDim+1; %add the time dimension for plots         
364                    nbvar=length(Field.ListVarName);
365                    if nbvar==0
366                        msgbox_uvmat('ERROR','no input variable selected in get_field')
367                        return
368                    end
369                    testsum=2*ones(1,nbvar);%initiate flag for action on each variable
370                    indexfalse=0;
371                    CoordName={};
372                    indexremove=[];
373                    if isfield(Field,'VarAttribute') % look for coordinate and flag variables   
374                        for ivar=1:nbvar
375                            if length(Field.VarAttribute)>=ivar && isfield(Field.VarAttribute{ivar},'Role')
376                               %Field.ListVarName{ivar}
377                                var_role=Field.VarAttribute{ivar}.Role;%'role' of the variable
378                                if isequal(var_role,'errorflag')
379                                    msgbox_uvmat('ERROR','do not handle error flags in time series')
380                                    return                                               
381                                end
382                                if isequal(var_role,'warnflag')                       
383                                    testsum(ivar)=0;  % not recorded variable
384                                    eval(['RecordData=rmfield(RecordData,''' Field.ListVarName{ivar} ''');']);%remove variable
385                                end                 
386                                if isequal(var_role,'coord_x')| isequal(var_role,'coord_y')|...
387                                    isequal(var_role,'coord_z')|isequal(var_role,'coord')
388                                    testsum(ivar)=1; %constant coordinates, record without time evolution
389                                end
390                            end
391                            % check whether the variable ivar is a dimension variable
392                            DimCell=Field.VarDimName{ivar};
393                            if ischar(DimCell)
394                                DimCell={DimCell};
395                            end
396                            if numel(DimCell)==1 && isequal(Field.ListVarName{ivar},DimCell{1})%detect dimension variables
397                               testsum(ivar)=1;
398                            end
399                        end
400                    end
401                    for ivar=1:nbvar
402                        if testsum(ivar)==2                     
403                            eval(['RecordData.' Field.ListVarName{ivar} '=[];'])
404                        end
405                    end
406                    RecordData.ListVarName=[{'Time'} RecordData.ListVarName];
407                end
408                for ivar=1:length(Field.ListVarName)
409                    VarName=Field.ListVarName{ivar};
410                    eval(['VarVal=Field.' VarName ';']);
411                    if testsum(ivar)==2% test for recorded variable
412                        eval(['VarVal=Field.' VarName ';']);
413                        if isequal(ProjObject.ProjMode,'inside')% take the average in the domain for 'inside' mode
414                            if isempty(VarVal)
415                                msgbox_uvmat('ERROR',['empty result at frame index ' num2str(num_i1{iview}(ifile))])
416                                return                             
417                            end
418                            VarVal=mean(VarVal,1);
419                        end
420                        VarVal=shiftdim(VarVal,-1); %shift dimension
421                        eval(['RecordData.' VarName '=cat(1,RecordData.' VarName ',VarVal);']);%concanete the current field to the time series   
422                    elseif testsum(ivar)==1% variable representing fixed coordinates
423                        eval(['VarInit=RecordData.' VarName ';']);
424                        if ~isequal(VarVal,VarInit)
425                            msgbox_uvmat('ERROR',['time series requires constant coordinates ' VarName])
426                            return
427                        end
428                    end                 
429                end
430                % time:
431                if isempty(time)% time read in ncfiles
432                   if isfield(Field,'Time')
433                       RecordData.Time(nbfiles,1)=Field.Time;
434                   else
435                       RecordData.Time(nbfiles,1)=nbfiles;%default
436                   end
437                else % time from ImaDoc prevails
438                    RecordData.Time(nbfiles,1)=(time(1,num_i1{1}(ifile),num_j1{1}(ifile))+time(end,num_i2{end}(ifile),num_j2{end}(ifile)))/2;
439                end
440            else
441                nbmissing=nbmissing+1;
442            end
443        end
444    end
445    %remove time for global attributes if exists
446    for iattr=1:numel(RecordData.ListGlobalAttribute)
447        if strcmp(RecordData.ListGlobalAttribute{iattr},'Time')
448            RecordData.ListGlobalAttribute(iattr)=[];
449            break
450        end
451    end
452    for ivar=1:numel(RecordData.ListVarName)
453        VarName=RecordData.ListVarName{ivar};
454        eval(['RecordData.' VarName '=squeeze(RecordData.' VarName ');']) %remove singletons
455    end
456        % add time dimension and update VarDimIndex:
457        for ivar=1:length(Field.ListVarName)
458             DimCell=Field.VarDimName(ivar);
459             if testsum(ivar)==2%variable used as time series
460                  RecordData.VarDimName{ivar}=[{'Time'} DimCell];
461             elseif testsum(ivar)==1
462                 RecordData.VarDimName{ivar}=DimCell;
463             end
464        end
465    indexremove=find(~testsum);
466    if ~isempty(indexremove)
467        RecordData.ListVarName(1+indexremove)=[];
468        RecordData.VarDimName(indexremove)=[];
469        if isfield(RecordData,'Role')&~isempty(RecordData.Role{1})%generaliser aus autres attributs
470            RecordData.Role(1+indexremove)=[];
471        end
472    end
473
474    %shift variable attributes
475    if isfield(RecordData,'VarAttribute')
476        RecordData.VarAttribute=[{[]} RecordData.VarAttribute];
477    end
478    RecordData.VarDimName=[{'Time'} RecordData.VarDimName];
479    RecordData.Action=Series.Action;%name of the processing programme
480   
481    %name of result file
482    [filemean]=...
483               name_generator(filebase_out,num_i1{1}(i_slice),num_j1{1}(i_slice),'.nc','_i1-i2_j1-j2',1,num_i2{end}(ifile),num_j2{end}(ifile),subdir_result);
484    errormsg=struct2nc(filemean,RecordData); %save result file
485    if isempty(errormsg)
486        display([filemean ' written'])
487    else
488        msgbox_uvmat('ERROR',['error in Series/struct2nc' errormsg])
489    end
490end
491
492figure
493haxes=axes;
494
495plot_field(RecordData,haxes)
496hget_field=findobj(allchild(0),'name','get_field');
497if ~isempty(hget_field)
498    delete(hget_field)
499end
500
501get_field(filemean,RecordData)
502   
503%-----------------------------------------------------------------------
504% --- Executes on selection change in CoordType.
505function CoordType_Callback(hObject, eventdata, handles)
506menu_str=get(handles.CoordType,'String');
507ind_coord=get(handles.CoordType,'Value');
508coord_option=menu_str{ind_coord};
509if isequal(coord_option,'more...');
510    fct_name='';
511    if exist('./TMP/current_usr_fct.mat','file')% if a file is found
512        h=load('./TMP/current_usr_fct.mat');
513        if isfield(h,'fct_name');
514            fct_name=h.fct_name;
515        end
516    end
517    prompt = {'Enter the name of the transform function'};
518    dlg_title = 'user defined transform';
519    num_lines= 1;
520    [FileName, PathName, filterindex] = uigetfile( ...
521       {'*.m', ' (*.m)';
522        '*.m',  '.m files '; ...
523        '*.*', 'All Files (*.*)'}, ...
524        'Pick a file', fct_name);
525    fct_name=fullfile(PathName,FileName);
526    addpath(PathName);%add the path to the selected fct
527    [errormsg,date_str]=check_functions;%check whether new functions can oversed the uvmat package A UTILISER
528    if ~exist(fct_name,'file')
529           warndlg(['image procesing fct ' fct_name ' not found'])
530    else
531        transform=FileName(1:end-2);%
532        update_menu(handles.CoordType,transform)%add the selected fct to the menu
533  %      set(handles.mouse_coord,'String',menu([1:end-1])')%update the mouse coord menu
534      %save ('./TMP/current_usr_fct.mat','fct_name');
535    end   
536end
537ind_coord=get(handles.CoordType,'Value');   
538
539%---------------------------------------------------------------------
540% % --- Executes on selection change in ProjObject.
541% function ProjObject_Callback(hObject, eventdata, handles)
542%
543% list_object=get(handles.ProjObject,'String');
544% index=get(handles.ProjObject,'Value');
545% hseries=get(handles.ProjObject,'Parent');
546% SeriesData=get(hseries,'UserData');
547% Obj=SeriesData.ProjObject{index};
548% [SeriesData.hset_object,SeriesData.sethandles]=set_object(SeriesData.ProjObject{index});
549% set(hseries,'UserData',SeriesData);
550
Note: See TracBrowser for help on using the repository browser.