Index: trunk/src/series/sub_background.m
===================================================================
--- trunk/src/series/sub_background.m	(revision 374)
+++ trunk/src/series/sub_background.m	(revision 394)
@@ -65,14 +65,19 @@
     WaitbarPos=get(hseries.waitbar_frame,'Position');
 end
+filebase=fullfile(Param.InputTable{1,1},Param.InputTable{1,3});
+dir_images=Param.InputTable{1,1};
+NomType=Param.InputTable{1,4};
+FileExt=Param.InputTable{1,5};
 [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
 if size(filecell,1)>1
-    msgbox_uvmat('ERROR','This function use only one input image series')
-    return
-end
+    msgbox_uvmat('WARNING','This function uses only the first input image series')
+    return
+end
+
 %%% TODO: update with the new conventions%%%%%%%%%%%%%%%%%
 %% determine input image type
 FileType=[];%default
 MovieObject=[];
-FileExt=Series.FileExt;
+%FileExt=Series.FileExt;
 
 if isequal(lower(FileExt),'.avi')
@@ -86,13 +91,13 @@
 elseif isequal(lower(FileExt),'.vol')
     FileType='vol';
-else 
-   form=imformats(FileExt(2:end));
-   if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
-       if isequal(Series.NomType,'*');
-           FileType='multimage';
-       else
-           FileType='image';
-       end
-   end
+else
+    form=imformats(FileExt(2:end));
+    if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
+        if isequal(NomType,'*');
+            FileType='multimage';
+        else
+            FileType='image';
+        end
+    end
 end
 if isempty(FileType)
@@ -101,8 +106,8 @@
 end
 
-nbslice_i=Series.NbSlice; %number of slices 
-siz=size(num_i1);
+nbslice_i=Param.NbSlice; %number of slices
+siz=size(i1_series{1});
 nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts
-
+j1=[];%default
 
 %% apply the image rescaling function 'level' (avoid the blinking effects of bright particles)
@@ -112,5 +117,5 @@
 %% adjust the proposed number of images in the sliding average to include an integer number of bursts
 if siz(2)~=1
-    nbaver=floor(nbaver_init/siz(1)); % number of bursts used for the sliding background, 
+    nbaver=floor(nbaver_init/siz(1)); % number of bursts used for the sliding background,
     if isequal(floor(nbaver/2),nbaver)
         nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
@@ -118,8 +123,4 @@
     nbaver_init=nbaver*siz(1);%propose by default an integer number of bursts
 end
-
-filebase=fullfile(Series.RootPath,Series.RootFile);
-dir_images=Series.RootPath;
-nom_type=Series.NomType;
 
 %% create dir of the new images
@@ -130,10 +131,10 @@
     term='_b';
 end
-[pp,subdir_ima]=fileparts(Series.RootPath);
+[pp,subdir_ima]=fileparts(Param.InputTable{1,1});
 try
     mkdir([dir_images term]);
 catch ME
-            msgbox_uvmat('ERROR',ME.message);
-            return
+    msgbox_uvmat('ERROR',ME.message);
+    return
 end
 [xx,msg2] = fileattrib([dir_images term],'+w','g'); %yield writing access (+w) to user group (g)
@@ -142,9 +143,9 @@
     return
 end
-filebase_b=fullfile([dir_images term],Series.RootFile);
+filebase_b=fullfile([dir_images term],Param.InputTable{1,3});
 
 %% set processing parameters
 prompt = {'Number of images for the sliding background (MUST FIT IN COMPUETER MEMORY)';'The number of positions (laser slices)';'volume scan mode (Yes/No)';...
-        'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
+    'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
 dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' subdir_ima term];
 num_lines= 3;
@@ -157,15 +158,16 @@
 nbaver=ceil(nbaver_ima/siz(1));%number of bursts for the sliding background
 if isequal(floor(nbaver/2),nbaver)
-   nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
-end
-step=siz(1);%case of bursts: the sliding background is shifted by one burst 
+    nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
+end
+step=siz(1);%case of bursts: the sliding background is shifted by one burst
 vol_test=answer{3};
 if isequal(vol_test,'Yes')
     nbfield2=1;%case of volume: no consecutive series at a given level
-    nbslice_i=siz(1);%number of slices 
+    nbslice_i=siz(1);%number of slices
 else
     nbfield2=siz(1); %nb of consecutive images at each level(burst)
     if siz(2)>1
-       nbslice_i=str2num(answer{2})/(num_i1(1,2)-num_i1(1,1));% number of slices
+        % nbslice_i=str2num(answer{2})/(num_i1(1,2)-num_i1(1,1));% number of slices
+        nbslice_i=str2num(answer{2})/(i1_series{1}(1,2)-i1_series{1}(1,1));% number of slices
     else
         nbslice_i=1;
@@ -195,10 +197,11 @@
 
 %% prealocate memory for the sliding background
-first_image=name_generator(filebase,num_i1(1),num_j1(1),Series.FileExt,Series.NomType);
-Afirst=read_image(first_image,FileType,num_i1(1),MovieObject);
+% first_image=name_generator(filebase,num_i1(1),num_j1(1),FileExt,NomType);
+% Afirst=read_image(first_image,FileType,num_i1(1),MovieObject);
+Afirst=read_image(filecell{1,1},FileType,i1_series{1}(1),MovieObject);
 [npy,npx]=size(Afirst);
-try 
-Ak=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory
-Asort=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory
+try
+    Ak=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory
+    Asort=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory
 catch ME
     msgbox_uvmat('ERROR',ME.message)
@@ -215,11 +218,15 @@
     if isempty(uid_Heading)
         [t,uid_Heading]=add(t,1,'element','Heading');
-    end   
+    end
     uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
-    ImageName=name_generator(filebase_b,num_i1(1),num_j1(1),'.png',Series.NomType);
+    if ~isempty(j1_series{1})
+        j1=j1_series{1}(1);
+    end
+    ImageName=fullfile_uvmat([dir_images term],'',Param.InputTable{1,3},'.png',NomType,i1_series{1}(1),[],j1);
+    %     ImageName=name_generator(filebase_b,num_i1(1),num_j1(1),'.png',NomType);
     [pth,ImageName]=fileparts(ImageName);
     ImageName=[ImageName '.png'];
     if isempty(uid_ImageName)
-       [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
+        [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
     end
     uid_value=children(t,uid_ImageName);
@@ -228,13 +235,13 @@
     else
         t=set(t,uid_value(1),'value',ImageName);%indicate  name of the first image, with ;png extension
-    end  
-
+    end
+    
     %add information about image transform
     [t,new_uid]=add(t,1,'element','ImageTransform');
     [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
-    [t]=add(t,NameFunction_uid,'chardata','sub_background');      
+    [t]=add(t,NameFunction_uid,'chardata','sub_background');
     if test_level
-            [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
-            [t]=add(t,NameFunction_uid,'chardata','levels');
+        [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
+        [t]=add(t,NameFunction_uid,'chardata','levels');
     end
     [t,NbSlice_uid]=add(t,new_uid,'element','NbSlice');
@@ -243,5 +250,5 @@
     [t]=add(t,NbSlidingImages_uid,'chardata',num2str(nbaver));
     [t,LuminosityRank_uid]=add(t,new_uid,'element','RankBackground');
-    [t]=add(t,LuminosityRank_uid,'chardata',num2str(rank));% luminosity rank almong the nbaver sliding images 
+    [t]=add(t,LuminosityRank_uid,'chardata',num2str(rank));% luminosity rank almong the nbaver sliding images
     save(t,[filebase_b '.xml'])
 elseif exist([filebase '.civ'],'file')
@@ -250,5 +257,5 @@
 %copy the mask
 if exist([filebase '_1mask_1'],'file')
-     copyfile([filebase '_1mask_1'],[filebase_b '_1mask_1']);% copy the mask file
+    copyfile([filebase '_1mask_1'],[filebase_b '_1mask_1']);% copy the mask file
 end
 
@@ -261,28 +268,34 @@
             indselect(iburst,ifield)=((ifield-1)*nbslice_i+(islice-1))*nbfield2+iburst;
         end
-    end  
+    end
     
     %% read the first series of nbaver_ima images and sort by luminosity at each pixel
     for ifield = 1:nbaver_ima
         ifile=indselect(ifield);
-        filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),Series.FileExt,Series.NomType);
-        Aread=read_image(filename,FileType,num_i1(ifile),MovieObject);
-        Ak(:,:,ifield)=Aread;           
+        filename=filecell{1,ifile};
+        % filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),FileExt,NomType);
+        Aread=read_image(filename,FileType,i1_series{1}(ifile),MovieObject);
+        Ak(:,:,ifield)=Aread;
     end
     Asort=sort(Ak,3);%sort the luminosity of images at each point
     B=Asort(:,:,rank);%background image
-   display( 'first background image will be substracted')
+    display( 'first background image will be substracted')
     for ifield=1:nbfirst
-            Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image
-            Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
-            C=uint16(Acor);% set to integer 16 bits
-            ifile=indselect(ifield);
-            newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
-            if test_level
-                C=levels(C);
-                 imwrite(C,newname,'BitDepth',8); % save the new image
-            else
-                 imwrite(C,newname,'BitDepth',16); % save the new image
-            end
+        Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image
+        Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
+        C=uint16(Acor);% set to integer 16 bits
+        ifile=indselect(ifield);
+        %             newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',NomType)% makes the new file name
+        if ~isempty(j1_series{1})
+            j1=j1_series{1}(ifile);
+        end
+        newname=fullfile_uvmat([dir_images term],'',Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
+        %newname=name_generator(filebase_b,i1_series{1}(ifile),j1_series{1}(ifile),'.png',NomType);% makes the new file name
+        if test_level
+            C=levels(C);
+            imwrite(C,newname,'BitDepth',8); % save the new image
+        else
+            imwrite(C,newname,'BitDepth',16); % save the new image
+        end
     end
     
@@ -299,5 +312,5 @@
                 for iburst=1:step
                     ifile=indselect(ifield+step*floor(nbaver/2)+iburst-1);
-                    filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),Series.FileExt,Series.NomType);
+                    filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),FileExt,NomType);
                     Aread=read_image(filename,FileType,num_i1(ifile),MovieObject);
                     Ak(:,:,nbaver_ima-step+iburst)=Aread;
@@ -311,6 +324,9 @@
                     C=uint16(Acor);
                     ifile=indselect(ifield+iburst-1);
-                    [newname]=...
-                        name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',Series.NomType) % makes the new file name
+                    if ~isempty(j1_series{1})
+                        j1=j1_series{1}(ifile);
+                    end
+                    newname=fullfile_uvmat([dir_images term],'',Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
+                    %[newname]=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',NomType) % makes the new file name
                     if test_level
                         C=levels(C);
@@ -325,22 +341,26 @@
         end
     end
-
-%% substract the background from the last images
+    
+    %% substract the background from the last images
     display('last background image will be substracted')
-     ifield=nbfield_slice-(step*ceil(nbaver/2))+1:nbfield_slice;
-     for ifield=nbfield_slice-(step*floor(nbaver/2))+1:nbfield_slice
-         index=ifield-nbfield_slice+step*(2*floor(nbaver/2)+1);
-         Acor=double(Ak(:,:,index))-double(B);
-         Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
-         C=uint16(Acor);
-         ifile=indselect(ifield);
-         newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
-         if test_level
-             C=levels(C);
-             imwrite(C,newname,'BitDepth',8); % save the new image
-         else
-             imwrite(C,newname,'BitDepth',16); % save the new image
-         end
-     end
+    ifield=nbfield_slice-(step*ceil(nbaver/2))+1:nbfield_slice;
+    for ifield=nbfield_slice-(step*floor(nbaver/2))+1:nbfield_slice
+        index=ifield-nbfield_slice+step*(2*floor(nbaver/2)+1);
+        Acor=double(Ak(:,:,index))-double(B);
+        Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
+        C=uint16(Acor);
+        ifile=indselect(ifield);
+        if ~isempty(j1_series{1})
+            j1=j1_series{1}(ifile);
+        end
+        newname=fullfile_uvmat([dir_images term],'',Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
+        %newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',NomType);% makes the new file name
+        if test_level
+            C=levels(C);
+            imwrite(C,newname,'BitDepth',8); % save the new image
+        else
+            imwrite(C,newname,'BitDepth',16); % save the new image
+        end
+    end
 end
 
