Ignore:
Timestamp:
Jun 17, 2012, 10:52:04 PM (12 years ago)
Author:
sommeria
Message:

merg_i_j transformed to the new standards.
Various improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/series/time_series.m

    r447 r457  
    11%'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
    23%------------------------------------------------------------------------
    34% 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.
    419%
    520%OUTPUT
     
    722%
    823%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 %i2_series: 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
     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
    1428%
    15 function GUI_input=time_series(Param)
     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)
    1649
    1750%% requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
    1851if ~exist('Param','var')
    19     GUI_input={'RootPath';'two';...%nbre of possible input series (options 'on'/'two'/'many', default:'one')
     52    ParamOut={'RootPath';'two';...%nbre of possible input series (options 'on'/'two'/'many', default:'one')
    2053        'SubDir';'on';... % subdirectory of derived files (PIV fields), ('on' by default)
    2154        'RootFile';'on';... %root input file name ('on' by default)
     
    3467end
    3568
    36 %% input parameters
    37 % read the xml file for batch case
    38 if ischar(Param) && ~isempty(find(regexp('Param','.xml$')))
    39     Param=xml2struct(Param);
    40     checkrun=0;
    41 else %  RUN case: parameters introduced as the input structure Param
     69%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
     70%% select different modes,  RUN, parameter input, BATCH
     71% BATCH  case: read the xml file for batch case
     72ParamOut=Param; %default output
     73if ischar(Param)
     74    if strcmp(Param,'input?')
     75        checkrun=1;% will inly search input parameters (preparation of BATCH mode)
     76    else
     77        Param=xml2struct(Param);
     78        checkrun=0;
     79    end
     80% RUN case: parameters introduced as the input structure Param
     81else
    4282    hseries=guidata(Param.hseries);%handles of the GUI series
    43     WaitbarPos=get(hseries.waitbar_frame,'Position');
    44     checkrun=1;
    45 end
     83    WaitbarPos=get(hseries.waitbar_frame,'Position');%position of the waitbar on the GUI series
     84    checkrun=2; % indicate the RUN option is used
     85end
     86
     87%% root input file(s) and type
     88RootPath=Param.InputTable(:,1);
     89RootFile=Param.InputTable(:,3);
     90SubDir=Param.InputTable(:,2);
     91NomType=Param.InputTable(:,4);
     92FileExt=Param.InputTable(:,5);
     93
     94% get the set of input file names (cell array filecell), and the lists of
     95% input file or frame indices i1_series,i2_series,j1_series,j2_series
    4696[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
     97% filecell{iview,fileindex}: cell array representing the list of file names
     98%        iview: line in the table corresponding to a given file series
     99%        fileindex: file index within  the file series,
     100% 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
     101% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
     102% set of frame indices used for movie or multimage input
     103% numbers of slices and file indices
     104
     105NbSlice=1;%default
     106if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
     107    NbSlice=Param.IndexRange.NbSlice;
     108end
     109nbview=numel(i1_series);%number of input file series (lines in InputTable)
     110nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
     111nbfield_i=size(i1_series{1},2); %nb of fields for the i index
     112nbfield=nbfield_j*nbfield_i; %total number of fields
     113nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
     114nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
     115
     116%determine the file type on each line from the first input file
     117ImageTypeOptions={'image','multimage','mmreader','video'};
     118NcTypeOptions={'netcdf','civx','civdata'};
     119for iview=1:nbview
     120    if ~exist(filecell{iview,1}','file')
     121        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
     122        return
     123    end
     124    [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1});
     125    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
     126    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
     127    if ~isempty(j1_series{iview})
     128        frame_index{iview}=j1_series{iview};
     129    else
     130        frame_index{iview}=i1_series{iview};
     131    end
     132end
     133
     134%% calibration data and timing: read the ImaDoc files
     135mode=''; %default
     136timecell={};
     137itime=0;
     138NbSlice_calib={};
     139XmlData=cell(1,nbview);%initiate the structures containing the data from the xml file (calibration and timing)
     140for iview=1:nbview%Loop on views
     141    SubDirBase=regexprep(SubDir{iview},'\..*','');%take the root part of SubDir, before the first dot '.'
     142    filexml=[fullfile(RootPath{iview},SubDirBase) '.xml'];%new convention: xml at the level of the image folder
     143    if ~exist(filexml,'file')
     144        filexml=[fullfile(RootPath{iview},SubDir{iview},RootFile{iview}) '.xml']; % old convention: xml inside the image folder
     145        if ~exist(filexml,'file')
     146            filexml=[fullfile(RootPath{iview},SubDir{iview},RootFile{iview}) '.civ']; % very old convention: .civ file
     147            if ~exist(filexml,'file')
     148                filexml='';
     149            end
     150        end
     151    end
     152    if ~isempty(filexml)
     153        [XmlData{iview},error]=imadoc2struct(filexml);
     154    end
     155    if isfield(XmlData{iview},'Time')
     156        itime=itime+1;
     157        timecell{itime}=XmlData{iview}.Time;
     158    end
     159    if isfield(XmlData{iview},'GeometryCalib') && isfield(XmlData{iview}.GeometryCalib,'SliceCoord')
     160        NbSlice_calib{iview}=size(XmlData{iview}.GeometryCalib.SliceCoord,1);%nbre of slices for Zindex in phys transform
     161        if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
     162            msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
     163        end
     164    end
     165end
     166
     167%% check coincidence in time for several input file series
     168multitime=0;
     169if isempty(timecell)
     170    time=[];
     171elseif length(timecell)==1
     172    time=timecell{1};
     173elseif length(timecell)>1
     174    multitime=1;
     175    for icell=1:length(timecell)
     176        if ~isequal(size(timecell{icell}),size(timecell{1}))
     177            msgbox_uvmat('WARNING','inconsistent time array dimensions in ImaDoc fields, the time for the first series is used')
     178            time=timecell{1};
     179            multitime=0;
     180            break
     181        end
     182    end
     183end
     184if multitime
     185    for icell=1:length(timecell)
     186        time(icell,:,:)=timecell{icell};
     187    end
     188    diff_time=max(max(diff(time)));
     189    if diff_time>0
     190        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
     191    end   
     192end
     193if size(time,2) < i2_series{1}(end) ||( ~isempty(j2_series{1}) && size(time,3) < j2_series{1}(end))% time array absent or too short in ImaDoc xml file'
     194    time=[];
     195end
    47196
    48197%% coordinate transform or other user defined transform
    49198transform_fct='';%default
    50 if isfield(Param,'FieldTransform')&&isfield(Param.FieldTransform,'fct_handle')
    51     transform_fct=Param.FieldTransform.fct_handle;
    52 end
    53 
    54 %% projection object
    55 test_object=get(hseries.GetObject,'Value');
    56 if test_object
    57     hset_object=findobj(allchild(0),'tag','set_object');
    58     ProjObject=read_GUI(hset_object);
    59     answeryes=msgbox_uvmat('INPUT_Y-N',['field series projected on ' ProjObject.Type]);
    60     if ~isequal(answeryes,'Yes')
     199if isfield(Param,'FieldTransform')&&isfield(Param.FieldTransform,'TransformHandle')
     200    transform_fct=Param.FieldTransform.TransformHandle;
     201end
     202%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
     203 % EDIT FROM HERE
     204
     205%% check the validity of  input file types
     206if CheckImage{1}
     207    FileExtOut='.png'; % write result as .png images for image inputs
     208elseif CheckNc{1}
     209    FileExtOut='.nc';% write result as .nc files for netcdf inputs
     210else
     211    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
     212    return
     213end
     214if nbview==2 && ~isequal(CheckImage{1},CheckImage{2})
     215        msgbox_uvmat('ERROR','input must be two image series or two netcdf file series')
     216    return
     217end
     218NomTypeOut='_1-2_1';% output file index will indicate the first and last ref index in the series
     219if NbSlice~=nbfield_j
     220    answer=msgbox_uvmat('INPUT_Y-N',['will not average slice by slice: for so cancel and set NbSlice= ' num2str(nbfield_j)]);
     221    if ~strcmp(answer,'Yes')
    61222        return
    62223    end
    63 else
    64     msgbox_uvmat('ERROR','a projection object is needed');
    65     return
    66 end
    67 
    68 %% features of the input fields
    69 RootPath=Param.InputTable(:,1);
    70 RootFile=Param.InputTable(:,3);
    71 % SubDir=Param.InputTable(:,2);
    72 NomType=Param.InputTable(:,4);
    73 FileExt=Param.InputTable(:,5);
    74 % ext=FileExt{1};
    75 % form=imformats(ext(2:end));%test valid Matlab image formats
    76 nbfield=size(i1_series{1},1)*size(i1_series{1},2); %number of fields in the time series
    77 
    78 %% determine image type
    79 hhh=which('mmreader');
    80 testnetcdf=0;
    81 nbview=length(RootPath);%Number of input series: this function  accepts only one or two input file series (sub_field is used in the latter case)
    82 for iview=1:nbview
    83     if isequal(FileExt{iview},'.nc')||isequal(FileExt{iview},'.cdf')
    84         FileType{iview}='netcdf';
    85         testnetcdf=1;
    86     elseif isequal(lower(FileExt{iview}),'.avi')
    87         if ~isequal(hhh,'')%&& mmreader.isPlatformSupported()
    88             MovieObject{iview}=mmreader(fullfile(RootPath{iview},[RootFile{iview} FileExt{iview}]));
    89             FileType{iview}='movie';
    90         else
    91             FileType{iview}='avi';
    92         end
    93     elseif isequal(lower(FileExt{iview}),'.vol')
    94         FileType{iview}='vol';
    95     else
    96        form=imformats(FileExt{iview}(2:end));
    97        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
    98            if isequal(NomType{iview},'*');
    99                FileType{iview}='multimage';
    100            else
    101                FileType{iview}='image';
    102            end
    103        end
    104     end
    105 end
    106 filebase{1}=fullfile(RootPath{1},RootFile{1});
    107 
    108 %% number of slices
    109 NbSlice=Param.NbSlice;
    110 
    111 %% Field and velocity type (the same for the two views)
    112 FieldName={''};
    113 
    114 if isfield(Param,'InputFields')&&isfield(Param.InputFields,'FieldMenu') 
    115     FieldName=Param.InputFields.FieldMenu;%the same set of fields for all views
    116     VelType{1}=Param.InputFields.VelTypeMenu;
    117 end
    118 if isempty(FieldName) && testnetcdf
    119     msgbox_uvmat('ERROR','A field must be defined as input')
    120     return
    121 end
     224end
     225
     226%% Set field names and velocity types
     227InputFields{1}=[];%default (case of images)
     228if isfield(Param,'InputFields')
     229    InputFields{1}=Param.InputFields;
     230end
     231if nbview==2
     232    InputFields{2}=[];%default (case of images)
     233    if isfield(Param,'InputFields')
     234        InputFields{2}=Param.InputFields{1};%default
     235        if isfield(Param.InputFields,'FieldName_1')
     236            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
     237            if isfield(Param.InputFields,'VelType_1')
     238                InputFields{2}.VelType=Param.InputFields.VelType_1;
     239            end
     240        end
     241    end
     242end
     243%%% TO UPDATE
    122244if isequal(FieldName,'get_field...')
    123245    hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
     
    140262    end
    141263end
     264%%%%%%%
     265
     266%% Initiate output fields
     267%initiate the output structure as a copy of the first input one (reproduce fields)
     268[DataOut,ParamOut,errormsg] = read_field(filecell{1,1},FileType{1},InputFields{1},1);
     269if ~isempty(errormsg)
     270    msgbox_uvmat('ERROR',['error reading ' filecell{1,1} ': ' errormsg])
     271    return
     272end
     273time_1=[];
     274if isfield(DataOut,'Time')
     275    time_1=DataOut.Time(1);
     276end
     277if CheckNc{iview}
     278    if isempty(strcmp('Conventions',DataOut.ListGlobalAttribute))
     279        DataOut.ListGlobalAttribute=['Conventions' DataOut.ListGlobalAttribute];
     280    end
     281    DataOut.Conventions='uvmat';
     282    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {Param.Action}];
     283    ActionKey='Action';
     284    while isfield(DataOut,ActionKey)
     285        ActionKey=[ActionKey '_1'];
     286    end
     287    DataOut.(ActionKey)=Param.Action;
     288    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {ActionKey}];
     289    if isfield(DataOut,'Time')
     290        DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {'Time','Time_end'}];
     291    end
     292end
     293
     294
     295
    142296
    143297%% detect whether the two files are 'images' or 'netcdf'
     
    155309%     VelType{2}=VelType_str{VelType_val};
    156310% end
    157 
    158 %% Calibration data and timing: read the ImaDoc files
    159 % mode=''; %default
    160 timecell={};
    161 XmlData={};
    162 itime=0;
    163 NbSlice_calib={};
    164 for iview=1:nbview%Loop on views
    165     XmlData{iview}=[];%default
    166     filebase{iview}=fullfile(RootPath{iview},RootFile{iview});
    167     if exist([filebase{iview} '.xml'],'file')
    168         [XmlData{iview},error]=imadoc2struct([filebase{iview} '.xml']);
    169         if isfield(XmlData{iview},'Time')
    170             itime=itime+1;
    171             timecell{itime}=XmlData{iview}.Time;
    172         end
    173         if isfield(XmlData{iview},'GeometryCalib') && isfield(XmlData{iview}.GeometryCalib,'SliceCoord')
    174             NbSlice_calib{iview}=size(XmlData{iview}.GeometryCalib.SliceCoord,1);%nbre of slices for Zindex in phys transform
    175             if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
    176                 msgbox_uvmat('WARNING','inconsistent number of Z indices for the field series');
    177             end
    178         end
    179     elseif exist([filebase{iview} '.civ'],'file')%old convention .civ text file
    180         [error,time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([filebase{iview} '.civ']);
    181         itime=itime+1;
    182         timecell{itime}=time;
    183         XmlData{iview}.Time=time;
    184         GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
    185         GeometryCalib.Tx=0;
    186         GeometryCalib.Ty=0;
    187         GeometryCalib.Tz=1;
    188         GeometryCalib.dpx=1;
    189         GeometryCalib.dpy=1;
    190         GeometryCalib.sx=1;
    191         GeometryCalib.Cx=0;
    192         GeometryCalib.Cy=0;
    193         GeometryCalib.f=1;
    194         GeometryCalib.kappa1=0;
    195         GeometryCalib.CoordUnit='cm';
    196         XmlData{iview}.GeometryCalib=GeometryCalib;
    197         if error==1
    198             msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
    199         end
    200     end
    201 end
    202 time=[];%default
    203 if ~isempty(timecell)
    204     if numel(timecell{1})<nbfield
    205        msgbox_uvmat('WARNING','time array from ImaDoc to short')
    206     else
    207         time=timecell{1}; %time defined from ImaDoc file (image series)
    208     end
    209 end
    210 
    211 %% check coincidence in time
    212 if length(timecell)>1
    213     for icell=2:length(timecell)
    214         if isequal(size(timecell{icell}),size(time))
    215             diff_time=max(abs(timecell{icell}-time));
    216             if diff_time>0
    217                 msgbox_uvmat('WARNING',['times of series differ by more than ' num2str(diff_time)])
    218                 break
    219             end
    220         else
    221             msgbox_uvmat('WARNING','inconsistent time array dimensions in ImaDoc fields, the time for the first series is used')
    222             break
    223         end
    224     end
    225 end
    226 if ~isempty(time)
    227     display(['time is read from ' filebase{iview} '.xml'])
    228 end
    229 
    230 %%  Root name of output files (TO GENERALISE FOR TWO INPUT SERIES)
    231 subdir_result='time_series';
    232 pathdir=fullfile(RootPath{1},subdir_result);
    233 while exist(pathdir,'dir')
    234     subdir_result=[subdir_result '.0'];
    235     pathdir=fullfile(RootPath{1},subdir_result);
    236 end
    237 [m1,m2,m3]=mkdir(pathdir);
    238 if ~isequal(m2,'')
    239      msgbox_uvmat('CONFIRMATION',m2);%error message for directory creation
    240 end
    241 [xx,msg2] = fileattrib(pathdir,'+w','g'); %yield writing access (+w) to user group (g)
    242 if ~strcmp(msg2,'')
    243     msgbox_uvmat('ERROR',['pb of permission for ' pathdir ': ' msg2])%error message for directory creation
    244     return
    245 end
    246 filebase_out=filebase{1};
    247  i21=i1_series{end}(end);
    248  if ~isempty(i2_series{end})
    249      i21=i2_series{end}(end)-i1_series{1}(1);
    250  end
    251  j21=1;
    252  if ~isempty(j1_series{1})
    253      j21=j1_series{end}(end);
    254       if ~isempty(j2_series{end})
    255           j21=j2_series{end}(end)-j21;
    256       end
    257  end
    258 NomTypeOut=nomtype2pair(NomType{1},i21,j21);
    259 
    260 
    261 %% velocity type
    262 VelType_str=get(hseries.VelTypeMenu,'String');
    263 VelType_val=get(hseries.VelTypeMenu,'Value');
    264 VelType{1}=VelType_str{VelType_val};
    265 if nbview==2
    266     VelType_str=get(hseries.VelTypeMenu_1,'String');
    267     VelType_val=get(hseries.VelTypeMenu_1,'Value');
    268     VelType{2}=VelType_str{VelType_val};
    269 end
    270311
    271312%% LOOP ON SLICES
Note: See TracChangeset for help on using the changeset viewer.