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

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