Ignore:
Timestamp:
Mar 31, 2011, 1:42:51 PM (13 years ago)
Author:
sommeria
Message:

add function sub_field_series to apply the sub_field operation to a series of fileds (for instance subtracting a background to an image series)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/read_get_field.m

    r206 r227  
    124124        return
    125125    end
    126     val=get(handles.scalar,'Value');%selected indices in the ordinate listbox
    127     VarNameScalar=inputlist{val}; %name of the variable in the list
     126    val=get(handles.scalar,'Value');%selected index for the scalar variable
     127    VarNameScalar=inputlist{val}; %name of the variable
    128128    VarIndexA=name2index(VarNameScalar,Field.ListVarName);%index of the variable in ListVarName
    129     dimname_A=Field.VarDimName{VarIndexA};
     129    dimname_A=Field.VarDimName{VarIndexA};% list of dimension names for the scalar variable (cell array)
     130    %remove time dimension if defined
     131    TimeVarIndex=[]; %default
     132    if strcmp(get(handles.TimeDimensionMenu,'Visible'),'on')
     133        dim_list=get(handles.TimeDimensionMenu,'String');
     134        dim_index=get(handles.TimeDimensionMenu,'Value');
     135        Time_dim=dim_list{dim_index};
     136        TimeVarIndex=find(strcmp(Time_dim,dimname_A),1);
     137        if ~isempty(TimeVarIndex)
     138            dimname_A(TimeVarIndex)=[];
     139        end
     140    end
    130141    nbvar=nbvar+1;
    131142    ListVarName{nbvar}=Field.ListVarName{VarIndexA};
     
    157168            if iscell(dimname_x)
    158169                if numel(dimname_x)==1
    159                     dimname_x=dimname_x{1};%transform to char chain
     170                    %dimname_x=dimname_x{1};%transform to char chain
     171                elseif numel(dimname_x)==2
     172                    index1_x = find(strcmp(dimname_x{1},dimname_A));%index of diname_x(1) in dimname_A
     173                    index2_x = find(strcmp(dimname_x{2},dimname_A));%index of diname_x(2) in dimname_A
    160174                else
    161175                    errormsg='invalid x coordinate selection in get_field';
     
    190204                if numel(dimname_y)==1
    191205                    dimname_y=dimname_y{1};%transform to char chain
     206                elseif numel(dimname_y)==2
     207                    index1_y = find(strcmp(dimname_y{1},dimname_A));%index of diname_x(1) in dimname_A
     208                    index2_y = find(strcmp(dimname_y{2},dimname_A));%index of diname_x(2) in dimname_A
    192209                else
    193210                    errormsg='invalid y coordinate selection in get_field';
     
    214231            nbvar=nbvar+1;
    215232            ListVarName{nbvar}=Field.ListVarName{VarIndex};
    216             VarDimName{nbvar}=dimname_y;
     233            VarDimName{nbvar}=dimname_z;
    217234            if numel(VarAttribute)>=VarIndex
    218235                SubVarAttribute{nbvar}=VarAttribute{VarIndex};
    219236            end
    220237            %check consistency of dimensions
    221             if ~isequal(dimname_y,dimname_A)% case of dimension variables
    222                 if iscell(dimname_y)
    223                     if numel(dimname_y)==1
    224                         dimname_y=dimname_y{1};%transform to char chain
     238            if ~isequal(dimname_z,dimname_A)% case of dimension variables
     239                if iscell(dimname_z)
     240                    if numel(dimname_z)==1
     241                        dimname_z=dimname_z{1};%transform to char chain
    225242                    else
    226                         errormsg='invalid y coordinate selection in get_field';
     243                        errormsg='invalid z coordinate selection in get_field';
    227244                        return
    228245                    end
     
    432449    end
    433450end
     451
     452
     453
    434454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    435 
    436 % get the input field
     455%% get the input field
    437456inputfield=get(handles.inputfile,'String');
    438457if exist(inputfield,'file')% read the input data corresponding to the list of selected varaibles
     
    443462    SubField.VarDimName=VarDimName;
    444463end
    445 SubField.ListGlobalAttribute=['InputFile' SubField.ListGlobalAttribute];
     464if strcmp(get(handles.TimeIndexValue,'Visible'),'on') && ~isempty(get(handles.TimeIndexValue,'String'))
     465    SubField.ListGlobalAttribute=[{'TimeIndex'} SubField.ListGlobalAttribute];
     466    SubField.TimeIndex=str2double(get(handles.TimeIndexValue,'String')); 
     467end
     468TimeValue=NaN;
     469if strcmp(get(handles.TimeVarValue,'Visible'),'on')
     470    TimeValue=str2double(get(handles.TimeVarValue,'String'));
     471elseif strcmp(get(handles.TimeValue,'Visible'),'on')
     472    TimeValue=str2double(get(handles.TimeValue,'String'));
     473end
     474if ~isnan(TimeValue)   
     475    SubField.ListGlobalAttribute=[{'TimeValue'} SubField.ListGlobalAttribute];
     476    SubField.TimeValue=str2double(get(handles.TimeVarValue,'String')); 
     477end
     478SubField.ListGlobalAttribute=[{'InputFile'} SubField.ListGlobalAttribute];
    446479SubField.InputFile=get(handles.inputfile,'String');
    447480SubField.VarAttribute=SubVarAttribute;
     
    453486    DimCellA=Field.VarDimName{VarIndexA};  %dimension names for the scalar variable
    454487    eval(['npxy=size(SubField.' VarNameA ');'])%zize of the scalar variable
     488%     if ~isempty(TimeVarIndex)%
     489%         DimCellA(TimeVarIndex)=[];%suppress the time dimension
     490%         npxy(TimeVarIndex)=[];
     491%     end
    455492    SingleCellA={};
    456493    if numel(npxy) < numel(DimCellA)
     
    461498    DimCellA=DimCellA(ind_select);%dimension names for the scalar variable, after removing singletons
    462499    npxy=npxy(ind_select);
     500%     if ~isempty(TimeVarIndex)%
     501%         DimCellA(TimeVarIndex)=[];%suppress the time dimension
     502%          npxy(TimeVarIndex)=[];
     503%     end
     504    NbDim=numel(npxy);
    463505    dimA=[];
    464     if test_zdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
     506    if test_zdimvar
    465507        NbDim=3;% field considered as 3D if a z coordinate is defined (to distinguish for instance from 2D color images with 3 components)
    466508        ind_singleton=find(strcmp(dimname_z,SingleCellA),1);% look for coincidence of dimension with one of the singleton dimensions
     
    472514        dimA=[dimA icoord];
    473515    end
    474     if test_ydimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
     516    if test_ydimvar
    475517        ind_singleton=find(strcmp(dimname_y,SingleCellA),1);% look for coincidence of dimension with one of the singleton dimensions
    476518        if ~isempty(ind_singleton)
     
    481523        dimA=[dimA icoord];
    482524    end
    483     if test_xdimvar
    484         ind_singleton=find(strcmp(dimname_x,SingleCellA),1);% look for coincidence of dimension with one of the singleton dimensions
    485         if ~isempty(ind_singleton)
     525    if test_xdimvar% a varible has been selected for x coordinate
     526        ind_singleton=find(strcmp(dimname_x{1},SingleCellA),1);% look for coincidence of dimension with one of the singleton dimensions
     527        if ~isempty(ind_singleton)         
    486528             errormsg=['the singleton dimension ' dimname_x ' has been selected for ordinate'];
    487529             return
    488530        end
    489         icoord=find(strcmp(dimname_x,DimCellA),1);% a dimension variable
     531        icoord=find(strcmp(dimname_x{1},DimCellA),1);% a dimension variable
    490532        dimA=[dimA icoord];
    491533    end
    492     dimextra=(1:numel(DimCellA));
    493     dimextra(dimA)=[]; %list of unselected dimension indices
     534    dimextra=(1:numel(DimCellA)); %list of initial dimension indices
     535    if isempty(TimeVarIndex)%
     536        dimextra(dimA)=[]; %list of unselected dimension indices
     537        DimCellA=DimCellA([dimA dimextra]);
     538        eval(['SubField.' VarNameA '=squeeze(SubField.' VarNameA ');'])
     539        if ~isempty(dimA)&& ~isempty(dimextra)
     540        eval(['SubField.' VarNameA '=permute(SubField.' VarNameA ',[dimA dimextra]);'])
     541        end
     542    else
     543        time_index=str2double(get(handles.TimeIndexValue,'String'));
     544        dimextra([dimA TimeVarIndex])=[];
     545        %DimCellA=DimCellA([dimA dimextra TimeVarIndex])%put the time dimension at the end
     546        eval(['SubField.' VarNameA '=permute(squeeze(SubField.' VarNameA '),[dimA dimextra TimeVarIndex]);'])%put time variable as the last dimension
     547        nbdimA=numel(dimA)+numel(dimextra);
     548        switch nbdimA
     549            case 1
     550                colon_str='(:';
     551            case 2
     552                colon_str='(:,:';
     553            case 3
     554                colon_str='(:,:,:';
     555        end
     556        eval(['SubField.' VarNameA '=SubField.' VarNameA  colon_str ',time_index);'])
     557        NbDim=NbDim-1;
     558    end
    494559    DimCellA=DimCellA([dimA dimextra]);
    495     eval(['SubField.' VarNameA '=permute(squeeze(SubField.' VarNameA '),[dimA dimextra]);'])
    496560    SubField.VarDimName{VarSubIndexA}=DimCellA; 
    497561    %add default coord_x and/or coord_y if empty
    498562    if empty_coord_x || empty_coord_y
    499         VarName=Field.ListVarName{field_var_index};
    500         DimCell=Field.VarDimName{field_var_index};   
    501         eval(['npxy=size(SubField.' VarName ');'])
    502         if numel(npxy) < numel(DimCell)
    503             DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
     563        %VarName=Field.ListVarName{field_var_index}
     564        %DimCell=Field.VarDimName{field_var_index}   
     565        eval(['npxy=size(SubField.' VarNameA ')'])
     566        if numel(npxy) < numel(DimCellA)
     567            DimCellA=DimCellA(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
    504568        end
    505569        ind_select=find(npxy~=1) ;%look for non singleton dimensions
    506         DimCell=DimCell(ind_select);%list of dimension names for the scalar, after singleton removal
     570        DimCellA=DimCellA(ind_select);%list of dimension names for the scalar, after singleton removal
    507571        npxy=npxy(ind_select);
    508572        testold=0;
     
    516580        end
    517581        if empty_coord_x       
    518                 coord_x_name=DimCell{NbDim};
     582                coord_x_name=DimCellA{NbDim};
    519583                SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
    520584                SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
     
    533597        end
    534598        if empty_coord_y
    535             coord_y_name=DimCell{NbDim-1};
     599            coord_y_name=DimCellA{NbDim-1};
    536600            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
    537601            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
     
    547611            end
    548612            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
     613        end
     614        if empty_coord_z && NbDim==3
     615            coord_z_name=DimCellA{NbDim-2};
     616            SubField.ListVarName=[{coord_z_name} SubField.ListVarName];
     617            SubField.VarDimName=[{coord_z_name} SubField.VarDimName];
     618            if testold
     619                eval(['SubField.' coord_z_name '=linspace(Coord_3(1),Coord_3(end),npxy(1));'])
     620            else
     621                eval(['SubField.' coord_z_name '=[0.5 npxy(NbDim-2)-0.5];'])
     622            end
     623            if ~testold
     624                coord_z_attr.units='index';
     625            else
     626                coord_z_attr.units='cm';
     627            end
     628            SubField.VarAttribute=[{coord_z_attr} SubField.VarAttribute];   
    549629        end
    550630    end
     
    714794end
    715795
     796%% remove time variable
     797if get(handles.TimeDimension,'Value')
     798
     799%     TimeName=get(handles.TimeName,'String');
     800%     time_index=find(strcmp(TimeName,SubField.ListVarName),1);
     801%     SubField.ListVarName(TimeVarIndex)=[];
     802%     SubField.VarDimName(TimeVarIndex)=[];
     803%     SubField.VarAttribute(TimeVarIndex)=[];
     804end
     805
     806
    716807%-------------------------------------------------
    717808% give index numbers of the strings str in the list ListvarName
Note: See TracChangeset for help on using the changeset viewer.