Index: /trunk/src/series/extract_multitif.m
===================================================================
--- /trunk/src/series/extract_multitif.m	(revision 930)
+++ /trunk/src/series/extract_multitif.m	(revision 930)
@@ -0,0 +1,171 @@
+%'ima2netcdf': read image series and transform to netcdf
+%------------------------------------------------------------------------
+
+    
+% function ParamOut=ima2netcdf(Param)
+%
+%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%OUTPUT
+% ParamOut: sets options in the GUI series.fig needed for the function
+%
+%INPUT:
+% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
+% In batch mode, Param is the name of the corresponding xml file containing the same information
+% when Param.Action.RUN=0 (as activated when the current Action is selected
+% in series), the function ouput paramOut set the activation of the needed GUI elements
+%
+% Param contains the elements:(use the menu bar command 'export/GUI config' in series to 
+% see the current structure Param)
+%    .InputTable: cell of input file names, (several lines for multiple input)
+%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
+%    .OutputSubDir: name of the subdirectory for data outputs
+%    .OutputDirExt: directory extension for data outputs
+%    .Action: .ActionName: name of the current activated function
+%             .ActionPath:   path of the current activated function
+%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
+%             .RUN =0 for GUI input, =1 for function activation
+%             .RunMode='local','background', 'cluster': type of function  use
+%             
+%    .IndexRange: set the file or frame indices on which the action must be performed
+%    .FieldTransform: .TransformName: name of the selected transform function
+%                     .TransformPath:   path  of the selected transform function
+%    .InputFields: sub structure describing the input fields withfields
+%              .FieldName: name(s) of the field
+%              .VelType: velocity type
+%              .FieldName_1: name of the second field in case of two input series
+%              .VelType_1: velocity type of the second field in case of two input series
+%              .Coord_y: name of y coordinate variable
+%              .Coord_x: name of x coordinate variable
+%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%=======================================================================
+% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
+%   http://www.legi.grenoble-inp.fr
+%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
+%
+%     This file is part of the toolbox UVMAT.
+%
+%     UVMAT is free software; you can redistribute it and/or modify
+%     it under the terms of the GNU General Public License as published
+%     by the Free Software Foundation; either version 2 of the license,
+%     or (at your option) any later version.
+%
+%     UVMAT is distributed in the hope that it will be useful,
+%     but WITHOUT ANY WARRANTY; without even the implied warranty of
+%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+%     GNU General Public License (see LICENSE.txt) for more details.
+%=======================================================================
+
+function ParamOut=ima2netcdf(Param)
+
+%%%%%%%%%%%%%%%%%    INPUT PREPARATION MODE (no RUN)    %%%%%%%%%%%%%%%%%
+if isstruct(Param) && isequal(Param.Action.RUN,0)
+    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
+    ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
+    ParamOut.NbSlice='on'; % edit box nbre of slices made active
+    ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
+    ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
+    ParamOut.FieldTransform = 'off';%can use a transform function
+    ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
+    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
+    ParamOut.OutputDirExt='.png';%set the output dir extension
+    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
+    
+    %% root input file(s) and type
+    % check the existence of the first file in the series
+        first_j=[];% note that the function will propose to cover the whole range of indices
+    if isfield(Param.IndexRange,'MinIndex_j'); first_j=Param.IndexRange.MinIndex_j; end
+    last_j=[];
+    if isfield(Param.IndexRange,'MaxIndex_j'); last_j=Param.IndexRange.MaxIndex_j; end
+    PairString='';
+    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
+    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
+    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
+        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
+    if ~exist(FirstFileName,'file')
+        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
+    else
+        [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString);
+        LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
+        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
+        if ~exist(FirstFileName,'file')
+             msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist'])
+        end
+    end
+
+    %% check the validity of  input file types
+    ImageTypeOptions={'image','multimage','mmreader','video'};%allowed input file types(images)
+    FileInfo=get_file_info(FirstFileName);
+    FileType=FileInfo.FileType;
+    CheckImage=~isempty(find(strcmp(FileType,ImageTypeOptions), 1));% =1 for images
+    if ~CheckImage
+        msgbox_uvmat('ERROR',['invalid file type input: ' FileType ' not an image'])
+        return
+    end
+    
+    return
+end
+%%%%%%%%%%%%%%%%%    STOP HERE FOR PAMETER INPUT MODE   %%%%%%%%%%%%%%%%% 
+
+%% read input parameters from an xml file if input is a file name (batch mode)
+checkrun=1;
+RUNHandle=[];
+WaitbarHandle=[];
+if ischar(Param)
+    Param=xml2struct(Param);% read Param as input file (batch case)
+    checkrun=0;
+else
+hseries=findobj(allchild(0),'Tag','series');
+RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
+WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
+end
+
+%% list of input images
+DirImages=fullfile(Param.InputTable{1,1},Param.InputTable{1,2});
+ListStruct=dir(DirImages);   
+ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
+check_bad=strcmp('.',ListCells(1,:))|strcmp('..',ListCells(1,:));%detect the dir '.' to exclude it
+check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
+ListFile=ListCells(1,find(~check_dir & ~check_bad));
+
+% %% create the netcdf file with name flname in format NETCDF4
+ OutputDir=fullfile(Param.InputTable{1,1},[Param.OutputSubDir Param.OutputDirExt]);
+ 
+% cmode = netcdf.getConstant('NETCDF4');
+% cmode = bitor(cmode, netcdf.getConstant('CLASSIC_MODEL'));
+% cmode = bitor(cmode, netcdf.getConstant('CLOBBER'));
+% nc = netcdf.create(fullfile(OutputDir,'images.nc'), cmode); 
+
+%% Main loop
+ImagesPerLevel=100;%100;
+count=0;
+%count=933;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%CORRECTION EXP08: 67->_11_1
+%count=1934%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%CORRECTION EXP07: 
+%% loop on the files 
+for ifile=1:numel(ListFile)
+    update_waitbar(WaitbarHandle,ifile/numel(ListFile))
+    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
+        disp('program stopped by user')
+        break
+    end
+    ImageName=fullfile(DirImages,ListFile{ifile});
+    NbFrames=numel(imfinfo(ImageName));
+    % loop on the frames within the tiff file
+    for iframe=1:NbFrames      
+        A=imread(ImageName,iframe);
+        count=count+1;
+        if isequal(ImagesPerLevel,1)% mode series 
+            
+            OutputFile=fullfile(OutputDir,['img_' num2str(count) '.png']);
+        else % mode multilevel or volume (indices i and j)
+            i_index=fix(count/ImagesPerLevel)+1;
+            j_index=mod(count,ImagesPerLevel)+1;
+            OutputFile=fullfile(OutputDir,['img_' num2str(i_index) '_' num2str(j_index) '.png']);
+        end
+        imwrite(A,OutputFile,'BitDepth',16)
+    end
+end
+
+
Index: /trunk/src/series/extract_rdvision.m
===================================================================
--- /trunk/src/series/extract_rdvision.m	(revision 929)
+++ /trunk/src/series/extract_rdvision.m	(revision 930)
@@ -153,5 +153,5 @@
 end
 t=xmltree;
-%%% A REMETTREE %%%%%%%%%%%%%%%%%%%%%
+
 save(t,fullfile(RootPath,'Running.xml'))%create an xml file to indicate that processing takes place
 
@@ -194,17 +194,18 @@
         return
     end
-    [XmlData,errormsg]=imadoc2struct(filexml);
+ 
     newxml=fullfile(RootPath,Param.InputTable{iview,3});
     newxml=regexprep(newxml,'_Master_Dalsa_4M180$','');%suppress '_Master_Dalsa_4M180'
     newxml=[newxml '.xml'];
     
-    [success,errormsg] = copyfile(filexml,newxml); %copy the xml file in the upper folder
-       
-    nbfield2=size(XmlData.Time,2)-1;
-    if nbfield2>1
-        NomTypeNew='_1_1';
-    else
-        NomTypeNew='_1';
-    end
+    %copyfile_modif(filexml,newxml); %copy the xml file in the upper folder
+    
+    %[XmlData,errormsg]=imadoc2struct(newxml);
+%     nbfield2=size(XmlData.Time,2)-1;
+%     if nbfield2>1
+%         NomTypeNew='_1_1';
+%     else
+%         NomTypeNew='_1';
+%     end
     %% get the names of .seq and .sqb files
     switch Param.InputTable{iview,5}
@@ -212,5 +213,5 @@
             filename_seq=fullfile(RootPath,Param.InputTable{iview,2},[Param.InputTable{iview,3} '.seq']);
             filename_sqb=fullfile(RootPath,Param.InputTable{iview,2},[Param.InputTable{iview,3} '.sqb']);
-            logdir=[Param.InputTable{1,2} .extract];
+            logdir=[Param.OutputSubDir Param.OutputDirExt];
             [success,errormsg] = copyfile(filename_seq,[fullfile(RootPath,logdir,Param.InputTable{iview,3}) '.seq']); %copy the seq file in the upper folder
             [success,errormsg] = copyfile(filename_sqb,[fullfile(RootPath,logdir,Param.InputTable{iview,3}) '.sqb']); %copy the sqb file in the upper folder
@@ -240,11 +241,5 @@
         SeqData.binrepertoire=[SeqData.binrepertoire DirExt];
     end
-    
-    %% checking consistency with the xml file
-    [npi,npj]=size(XmlData.Time);
-    if ~isequal(SeqData.nb_frames,(npi-1)*(npj-1))
-        disp_uvmat('ERRROR',['inconsistent number of images ' num2str(SeqData.nb_frames) ' with respect to the xml file: ' num2str((npi-1)*(npj-1))] ,checkrun);
-        return
-    end
+   
     
     %% reading the .sqb file
@@ -273,27 +268,28 @@
     for ii=1: numel(m.Data)
         timestamp(ii)=m.Data(ii).timestamp;
-        j1=1;
-        if ~isequal(nbfield2,1)
-            j1=mod(ii-1,nbfield2)+1;
-        end
-        i1=floor((ii-1)/nbfield2)+1;
-        diff_time(i1,j1)= timestamp(ii)-XmlData.Time(i1+1,j1+1);
-    end
-    time_diff_max=max(diff_time');
-    time_diff_min=min(diff_time');
-    if max(time_diff_max)>0.005
-        disp(['WARNING:timestamps exceeds xml time by' num2str(max(time_diff_max))])
-        checkpreserve=1;
-    elseif min(time_diff_min)<-0.005
-        disp(['timestamps is lower than xml time by' num2str(min(time_diff_min))])
-        checkpreserve=1;
+%         j1=1;
+%         if ~isequal(nbfield2,1)
+%             j1=mod(ii-1,nbfield2)+1;
+%         end
+%         i1=floor((ii-1)/nbfield2)+1;
+        %diff_time(i1,j1)= timestamp(ii)-XmlData.Time(i1+1,j1+1);
+    end
+    [nbfield2,msg]=copyfile_modif(filexml,timestamp,newxml); %copy the xml file in the upper folder
+    [XmlData,errormsg]=imadoc2struct(newxml);% check reading of the new xml file
+    if ~isempty(errormsg)
+        disp(errormsg)
+        return
+    end
+    difftime=XmlData.Time(2:end,2:end)-(reshape(timestamp,nbfield2,[]))';
+    disp(['time from xml and timestamp differ by ' num2str(max(max(abs(difftime))))])
+    if max(abs(difftime))>0.01
+        checkpreserve=1;% will not erase the initial files, possibility of error
+    end
+    if nbfield2>1
+        NomTypeNew='_1_1';
     else
-        disp('CONFIRMATION:time from xml file correct within better than 5 ms')
-    end
-    if checkpreserve
-          disp(  'max and min of timestamp-xml time for each index i:')
-          disp(time_diff_max)
-          disp(time_diff_min)            
-    end
+        NomTypeNew='_1';
+    end
+
     [BinList,errormsg]=binread_rdv_series(RootPath,SeqData,m.Data,nbfield2,NomTypeNew);
     if ~isempty(errormsg)
@@ -301,4 +297,5 @@
         return
     end
+    
     % check the existence of the expected output image files (from the xml)
     FileDir=SeqData.sequencename;
@@ -314,8 +311,9 @@
 %% remove binary files if transfer OK
     if ~checkpreserve
-        for ibin=1:numel(BinList)
-            delete(BinList{ibin})
+        for iview=1:size(Param.InputTable,1)
+         fullfile(RootPath,Param.InputTable{iview,2})
+
+        [SUCCESS,MESSAGE]=rmdir(fullfile(RootPath,Param.InputTable{iview,2}),'s')
         end
-        rmdir(fullfile(RootPath,Param.InputTable{iview,2}))
     end
 delete(fullfile(RootPath,'Running.xml'))%delete the  xml file to indicate that processing is finished
@@ -323,4 +321,5 @@
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %--------- reads a series of bin files
+
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 function [BinList,errormsg]=binread_rdv_series(PathDir,SeqData,SqbData,nbfield2,NomTypeNew)
@@ -349,4 +348,6 @@
 fid=0;
 errormsg='';
+BinList={};
+
 classname=sprintf('uint%d',SeqData.bytesperpixel*8);
 
@@ -424,354 +425,70 @@
 
 
-% for ifile=1:nbfield
-%             update_waitbar(WaitbarHandle,ifile/nbfield)
-%     if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
-%         disp('program stopped by user')
-%         break
-%     end
-%     [A,FileInfo,timestamps]=read_rdvision(filename,ifile);
-%     if ifile==1
-%         classA=class(A);
-%         if strcmp(classA,'uint8')
-%             BitDepth=8;
-%         else
-%         BitDepth=16;
-%         end
-%     end
-%     j1=[];
-%     if ~isequal(nbfield2,1)
-%     j1=mod(ifile-1+first_label,nbfield2)+1;
-%     end
-%     i1=floor((ifile-1+first_label)/nbfield2)+1;
-%     OutputFile=fullfile_uvmat(RootPath{1},OutputDir,'img','.png',NomTypeNew,i1,[],j1);
-%     try
-%         imwrite(A,OutputFile,'BitDepth',BitDepth) % case of 16 bit images
-%     disp([OutputFile ' written']);
-%         [s,errormsg] = fileattrib(OutputFile,'-w','a'); %set images to read only '-w' for all users ('a')
-%         if ~s
-%             disp_uvmat('ERROR',errormsg,checkrun);
-%             return
-%         end
-%     catch ME
-%         disp_uvmat('ERROR',ME.message,checkrun);
-%         return
-%     end
-%
-% end
-
-%'imadoc2struct_special': reads the xml file for image documentation
-%------------------------------------------------------------------------
-% function [s,errormsg]=imadoc2struct_special(ImaDoc,option)
-%
-% OUTPUT:
-% s: structure representing ImaDoc
-%   s.Heading: information about the data hierarchical structure
-%   s.Time: matrix of times
-%   s.TimeUnit
-%  s.GeometryCalib: substructure containing the parameters for geometric calibration
-% errormsg: error message
-%
-% INPUT:
-% ImaDoc: full name of the xml input file with head key ImaDoc
-% option: ='GeometryCalib': read  the data of GeometryCalib, including source point coordinates
-
-function [s,errormsg]=imadoc2struct_special(ImaDoc,option) 
-
-%% default input and output
-if ~exist('option','var')
-    option='*';
-end
-errormsg=[];%default
-s.Heading=[];%default
-s.Time=[]; %default
-s.TimeUnit=[]; %default
-s.GeometryCalib=[];
-tsai=[];%default
-
-%% opening the xml file
-if exist(ImaDoc,'file')~=2, errormsg=[ ImaDoc ' does not exist']; return;end;%input file does not exist
-try
-    t=xmltree(ImaDoc);
-catch
-    errormsg={[ImaDoc ' is not a valid xml file']; lasterr};
-    display(errormsg);
-    return
-end
-uid_root=find(t,'/ImaDoc');
-if isempty(uid_root), errormsg=[ImaDoc ' is not an image documentation file ImaDoc']; return; end;%not an ImaDoc .xml file
-
-
-%% Heading
-uid_Heading=find(t,'/ImaDoc/Heading');
-if ~isempty(uid_Heading), 
-    uid_Campaign=find(t,'/ImaDoc/Heading/Campaign');
-    uid_Exp=find(t,'/ImaDoc/Heading/Experiment');
-    uid_Device=find(t,'/ImaDoc/Heading/Device');
-    uid_Record=find(t,'/ImaDoc/Heading/Record');
-    uid_FirstImage=find(t,'/ImaDoc/Heading/ImageName');
-    s.Heading.Campaign=get(t,children(t,uid_Campaign),'value');
-    s.Heading.Experiment=get(t,children(t,uid_Exp),'value');
-    s.Heading.Device=get(t,children(t,uid_Device),'value');
-    if ~isempty(uid_Record)
-        s.Heading.Record=get(t,children(t,uid_Record),'value');
-    end
-    s.Heading.ImageName=get(t,children(t,uid_FirstImage),'value');
-end
-
-%% Camera  and timing
-if strcmp(option,'*') || strcmp(option,'Camera')
-    uid_Camera=find(t,'/ImaDoc/Camera');
-    if ~isempty(uid_Camera)
-        uid_ImageSize=find(t,'/ImaDoc/Camera/ImageSize');
-        if ~isempty(uid_ImageSize);
-            ImageSize=get(t,children(t,uid_ImageSize),'value');
-            xindex=findstr(ImageSize,'x');
-            if length(xindex)>=2
-                s.Npx=str2double(ImageSize(1:xindex(1)-1));
-                s.Npy=str2double(ImageSize(xindex(1)+1:xindex(2)-1));
-            end
-        end
-        uid_TimeUnit=find(t,'/ImaDoc/Camera/TimeUnit');
-        if ~isempty(uid_TimeUnit)
-            s.TimeUnit=get(t,children(t,uid_TimeUnit),'value');
-        end
-        uid_BurstTiming=find(t,'/ImaDoc/Camera/BurstTiming');
-        if ~isempty(uid_BurstTiming)
-            for k=1:length(uid_BurstTiming)
-                subt=branch(t,uid_BurstTiming(k));%subtree under BurstTiming
-                % reading Dtk
-                Frequency=get_value(subt,'/BurstTiming/FrameFrequency',1);
-                Dtj=get_value(subt,'/BurstTiming/Dtj',[]);
-                Dtj=Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
-                NbDtj=get_value(subt,'/BurstTiming/NbDtj',[]);
-                %%%% correction RDvision %%%%
-%                 NbDtj=NbDtj/numel(Dtj);
-%                 s.NbDtj=NbDtj;
-%                 %%%%
-                Dti=get_value(subt,'/BurstTiming/Dti',[]);
-                NbDti=get_value(subt,'/BurstTiming/NbDti',1);
-                 %%%% correction RDvision %%%%
-                if isempty(Dti)% series 
-                     Dti=Dtj;
-                      NbDti=NbDtj;
-                     Dtj=[];
-                     s.Dti=Dti;
-                     s.NbDti=NbDti;
-                else
-                    % NbDtj=NbDtj/numel(Dtj);%bursts
-                    if ~isempty(NbDtj)
-                    s.NbDtj=NbDtj/numel(Dtj);%bursts;
-                    else
-                        s.NbDtj=1;
-                    end
-                end
-                %%%% %%%%
-                Dti=Dti/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
-
-                Time_val=get_value(subt,'/BurstTiming/Time',0);%time in TimeUnit
-                if ~isempty(Dti)
-                    Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
-                    Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals  Dti
-                end
-                if ~isempty(Dtj)
-                    Dtj=reshape(Dtj'*ones(1,s.NbDtj),1,s.NbDtj*numel(Dtj)); %concatene Dtj vector NbDtj times
-                    Dtj=[0 Dtj];
-                    Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
-                end
-                % reading Dtk
-                Dtk=get_value(subt,'/BurstTiming/Dtk',[]);
-                NbDtk=get_value(subt,'/BurstTiming/NbDtk',1);
-                %%%% correction RDvision %%%%
-                if ~isequal(NbDtk,1)
-                    NbDtk=-1+(NbDtk+1)/(NbDti+1);
-                end
-                s.NbDtk=NbDtk;
-                %%%%%
-                if isempty(Dtk)
-                    s.Time=[s.Time;Time_val];
-                else
-                    for kblock=1:NbDtk+1
-                        Time_val_k=Time_val+(kblock-1)*Dtk;
-                        s.Time=[s.Time;Time_val_k];
-                    end
-                end
-            end
-        end
-    end
-end
-
-%% motor
-if strcmp(option,'*') || strcmp(option,'GeometryCalib')
-    uid_subtree=find(t,'/ImaDoc/TranslationMotor');
-    if length(uid_subtree)==1
-        subt=branch(t,uid_subtree);%subtree under GeometryCalib
-       [s.TranslationMotor,errormsg]=read_subtree(subt,{'Nbslice','ZStart','ZEnd'},[1 1 1],[1 1 1]);
-    end 
-end
-%%  geometric calibration
-if strcmp(option,'*') || strcmp(option,'GeometryCalib')
-    uid_GeometryCalib=find(t,'/ImaDoc/GeometryCalib');
-    if ~isempty(uid_GeometryCalib)
-        if length(uid_GeometryCalib)>1
-            errormsg=['More than one GeometryCalib in ' filecivxml];
-            return
-        end
-        subt=branch(t,uid_GeometryCalib);%subtree under GeometryCalib
-        cont=get(subt,1,'contents');
-        if ~isempty(cont)
-            uid_CalibrationType=find(subt,'/GeometryCalib/CalibrationType');
-            if isequal(length(uid_CalibrationType),1)
-                tsai.CalibrationType=get(subt,children(subt,uid_CalibrationType),'value');
-            end
-            uid_CoordUnit=find(subt,'/GeometryCalib/CoordUnit');
-            if isequal(length(uid_CoordUnit),1)
-                tsai.CoordUnit=get(subt,children(subt,uid_CoordUnit),'value');
-            end
-            uid_fx_fy=find(subt,'/GeometryCalib/fx_fy');
-            focal=[];%default fro old convention (Reg Wilson)
-            if isequal(length(uid_fx_fy),1)
-                tsai.fx_fy=str2num(get(subt,children(subt,uid_fx_fy),'value'));
-            else %old convention (Reg Wilson)
-                uid_focal=find(subt,'/GeometryCalib/focal');
-                uid_dpx_dpy=find(subt,'/GeometryCalib/dpx_dpy');
-                uid_sx=find(subt,'/GeometryCalib/sx');
-                if ~isempty(uid_focal) && ~isempty(uid_dpx_dpy) && ~isempty(uid_sx)
-                    dpx_dpy=str2num(get(subt,children(subt,uid_dpx_dpy),'value'));
-                    sx=str2num(get(subt,children(subt,uid_sx),'value'));
-                    focal=str2num(get(subt,children(subt,uid_focal),'value'));
-                    tsai.fx_fy(1)=sx*focal/dpx_dpy(1);
-                    tsai.fx_fy(2)=focal/dpx_dpy(2);
-                end
-            end
-            uid_Cx_Cy=find(subt,'/GeometryCalib/Cx_Cy');
-            if ~isempty(uid_Cx_Cy)
-                tsai.Cx_Cy=str2num(get(subt,children(subt,uid_Cx_Cy),'value'));
-            end
-            uid_kc=find(subt,'/GeometryCalib/kc');
-            if ~isempty(uid_kc)
-                tsai.kc=str2double(get(subt,children(subt,uid_kc),'value'));
-            else %old convention (Reg Wilson)
-                uid_kappa1=find(subt,'/GeometryCalib/kappa1');
-                if ~isempty(uid_kappa1)&& ~isempty(focal)
-                    kappa1=str2double(get(subt,children(subt,uid_kappa1),'value'));
-                    tsai.kc=-kappa1*focal*focal;
-                end
-            end
-            uid_Tx_Ty_Tz=find(subt,'/GeometryCalib/Tx_Ty_Tz');
-            if ~isempty(uid_Tx_Ty_Tz)
-                tsai.Tx_Ty_Tz=str2num(get(subt,children(subt,uid_Tx_Ty_Tz),'value'));
-            end
-            uid_R=find(subt,'/GeometryCalib/R');
-            if ~isempty(uid_R)
-                RR=get(subt,children(subt,uid_R),'value');
-                if length(RR)==3
-                    tsai.R=[str2num(RR{1});str2num(RR{2});str2num(RR{3})];
-                end
-            end
-            
-            %look for laser plane definitions
-            uid_Angle=find(subt,'/GeometryCalib/PlaneAngle');
-            uid_Pos=find(subt,'/GeometryCalib/SliceCoord');
-            if isempty(uid_Pos)
-                uid_Pos=find(subt,'/GeometryCalib/PlanePos');%old convention
-            end
-            if ~isempty(uid_Angle)
-                tsai.PlaneAngle=str2num(get(subt,children(subt,uid_Angle),'value'));
-            end
-            if ~isempty(uid_Pos)
-                for j=1:length(uid_Pos)
-                    tsai.SliceCoord(j,:)=str2num(get(subt,children(subt,uid_Pos(j)),'value'));
-                end
-                uid_DZ=find(subt,'/GeometryCalib/SliceDZ');
-                uid_NbSlice=find(subt,'/GeometryCalib/NbSlice');
-                if ~isempty(uid_DZ) && ~isempty(uid_NbSlice)
-                    DZ=str2double(get(subt,children(subt,uid_DZ),'value'));
-                    NbSlice=get(subt,children(subt,uid_NbSlice),'value');
-                    if isequal(NbSlice,'volume')
-                        tsai.NbSlice='volume';
-                        NbSlice=NbDtj+1;
-                    else
-                        tsai.NbSlice=str2double(NbSlice);
-                    end
-                    tsai.SliceCoord=ones(NbSlice,1)*tsai.SliceCoord+DZ*(0:NbSlice-1)'*[0 0 1];
-                end
-            end   
-            tsai.SliceAngle=get_value(subt,'/GeometryCalib/SliceAngle',[0 0 0]);
-            tsai.VolumeScan=get_value(subt,'/GeometryCalib/VolumeScan','n');
-            tsai.InterfaceCoord=get_value(subt,'/GeometryCalib/InterfaceCoord',[0 0 0]);
-            tsai.RefractionIndex=get_value(subt,'/GeometryCalib/RefractionIndex',1);
-            
-            if strcmp(option,'GeometryCalib')
-                tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
-            end
-            s.GeometryCalib=tsai;
-        end
-    end
-end
-
-%--------------------------------------------------
-%  read a subtree
-% INPUT: 
-% t: xltree
-% head_element: head elelemnt of the subtree
-% Data, structure containing 
-%    .Key: element name
-%    .Type: type of element ('charg', 'float'....)
-%    .NbOccur: nbre of occurrence, NaN for un specified number 
-function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
-%--------------------------------------------------
-s=[];%default
-errormsg='';
-head_element=get(subt,1,'name');
-    cont=get(subt,1,'contents');
-    if ~isempty(cont)
-        for ilist=1:length(Data)
-            uid_key=find(subt,[head_element '/' Data{ilist}]);
-            if ~isequal(length(uid_key),NbOccur(ilist))
-                errormsg=['wrong number of occurence for ' Data{ilist}];
-                return
-            end
-            for ival=1:length(uid_key)
-                val=get(subt,children(subt,uid_key(ival)),'value');
-                if ~NumTest(ilist)
-                    eval(['s.' Data{ilist} '=val;']);
-                else
-                    eval(['s.' Data{ilist} '=str2double(val);'])
-                end
-            end
-        end
-    end
-
-
-%--------------------------------------------------
-%  read an xml element
-function val=get_value(t,label,default)
-%--------------------------------------------------
-val=default;
-uid=find(t,label);%find the element iud(s)
-if ~isempty(uid) %if the element named label exists
-   uid_child=children(t,uid);%find the children 
-   if ~isempty(uid_child)
-       data=get(t,uid_child,'type');%get the type of child
-       if iscell(data)% case of multiple element
-           for icell=1:numel(data)
-               val_read=str2num(get(t,uid_child(icell),'value'));
-               if ~isempty(val_read)
-                   val(icell,:)=val_read;
-               end
-           end
-%           val=val';
-       else % case of unique element value
-           val_read=str2num(get(t,uid_child,'value'));
-           if ~isempty(val_read)
-               val=val_read;
-           else
-              val=get(t,uid_child,'value');%char string data
-           end
-       end
-   end
-end
-
-
-
-
+function [nbfield2,msg]=copyfile_modif(filexml,timestamp,newxml)
+msg='';
+t=xmltree(filexml);
+
+%% correct NbDtj
+uid_NbDtj=find(t,'ImaDoc/Camera/BurstTiming/NbDtj');
+uid_content=get(t,uid_NbDtj,'contents');
+t=set(t,uid_content,'value','1');% set NbDtj to 1 (correct error in the xml file)
+
+%% check Dtj
+uid_Dtj=find(t,'ImaDoc/Camera/BurstTiming/Dtj');
+uid_content=get(t,uid_Dtj,'contents');
+Dtj=str2num(get(t,uid_content,'value'));
+nbfield2=numel(Dtj)+1;
+timestamp=(reshape(timestamp,nbfield2,[]))';
+diff_Dtj=diff(timestamp(1,:))-Dtj;
+if max(abs(diff_Dtj))>min(Dtj)/1000
+    disp(['Dtj from xml file differs from time stamp by ' num2str(max(abs(diff_Dtj))) ', '])%'
+else
+    disp('Dtj OK');
+end
+
+%% correct NbDti
+NbDti=size(timestamp,1); %default for series or burst
+uid_motor_nbslice=find(t,'ImaDoc/TranslationMotor/Nbslice');
+if ~isempty(uid_motor_nbslice)
+    uid_content=get(t,uid_motor_nbslice,'contents');
+    NbSlice=str2num(get(t,uid_content,'value'));
+    NbDti=NbSlice-1;
+uid_NbDti=find(t,'ImaDoc/Camera/BurstTiming/NbDti');
+uid_content=get(t,uid_NbDti,'contents');
+t=set(t,uid_content,'value',num2str(NbDti));
+end
+
+%% adjust Dti
+uid_Dti=find(t,'ImaDoc/Camera/BurstTiming/Dti');
+uid_content=get(t,uid_Dti,'contents');
+Dti=str2num(get(t,uid_content,'value'));
+Dti_stamp=(timestamp(1+NbDti,1)-timestamp(1,1))/NbDti;
+if abs(Dti_stamp-Dti)>Dti/1000
+    disp([msg 'Dti from xml file corrected by ' num2str(Dti_stamp-Dti) ', ']);%'
+else
+    disp('Dti OK')
+end
+t=set(t,uid_content,'value',num2str(Dti_stamp));
+
+%% adjust Dtk
+uid_Dtk=find(t,'ImaDoc/Camera/BurstTiming/Dtk');
+if ~isempty(uid_Dtk)
+uid_content_Dtk=get(t,uid_Dtk,'contents');
+Dtk=str2num(get(t,uid_content_Dtk,'value'));
+uid_NbDtk=find(t,'ImaDoc/Camera/BurstTiming/NbDtk');
+uid_content_NbDtk=get(t,uid_NbDtk,'contents');
+NbDtk=str2num(get(t,uid_content_NbDtk,'value'));
+Dtk_stamp=(timestamp(end-NbDti,1)-timestamp(1,1))/NbDtk;
+if abs(Dtk_stamp-Dtk)>Dtk/1000
+    disp([msg 'Dtk from xml file corrected by ' num2str(Dtk_stamp-Dtk)]);
+else
+    disp('Dtk OK')
+end
+t=set(t,uid_content_Dtk,'value',num2str(Dtk_stamp));
+end
+
+save(t,newxml)
+
+
+
+
Index: /trunk/src/struct2nc.m
===================================================================
--- /trunk/src/struct2nc.m	(revision 929)
+++ /trunk/src/struct2nc.m	(revision 930)
@@ -35,18 +35,18 @@
 %=======================================================================
 
-function errormsg=struct2nc(flname,Data)
-if ~ischar(flname)
-    errormsg='invalid input for the netcf file name';
-    return
-end
+function [errormsg,nc]=struct2nc(flname,Data,action)
+nc=[];
+% if ~ischar(flname)
+%     errormsg='invalid input for the netcf file name';
+%     return
+% end
 if ~exist('Data','var')
      errormsg='no data  input for the netcdf file';
     return
 end 
-FilePath=fileparts(flname);
-if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
-    errormsg=['directory ' FilePath ' needs to be created'];
-    return
-end
+if ~exist('action','var')
+    action='one_input'; %fill the file with data and close it
+end
+
 
 %% check the validity of the input field structure
@@ -59,8 +59,17 @@
 
 %% create the netcdf file with name flname in format NETCDF4
-cmode = netcdf.getConstant('NETCDF4');
-cmode = bitor(cmode, netcdf.getConstant('CLASSIC_MODEL'));
-cmode = bitor(cmode, netcdf.getConstant('CLOBBER'));
-nc = netcdf.create(flname, cmode); 
+if ischar(flname)
+    FilePath=fileparts(flname);
+    if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
+        errormsg=['directory ' FilePath ' needs to be created'];
+        return
+    end
+    cmode = netcdf.getConstant('NETCDF4');
+    cmode = bitor(cmode, netcdf.getConstant('CLASSIC_MODEL'));
+    cmode = bitor(cmode, netcdf.getConstant('CLOBBER'));
+    nc = netcdf.create(flname, cmode);
+else
+    nc=flname;
+end
 
 %% write global constants
@@ -159,6 +168,7 @@
     end
 end
+if strcmp(action,'one_input')
 netcdf.close(nc)
-
+end
 
 %'check_field_structure': check the validity of the field struture representation consistant with the netcdf format
Index: /trunk/src/xml2struct.m
===================================================================
--- /trunk/src/xml2struct.m	(revision 929)
+++ /trunk/src/xml2struct.m	(revision 930)
@@ -13,5 +13,6 @@
 
 %=======================================================================
-% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
+% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France/.fsnet/project/coriolis/2016/16CIRCUMPOLAR/EXP07/2016-03-08T
+
 %   http://www.legi.grenoble-inp.fr
 %   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
