source: trunk/src/read_get_field.m @ 230

Last change on this file since 230 was 227, checked in by sommeria, 14 years ago

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 size: 34.3 KB
Line 
1%'read_get_field': read the list of selected variables from the GUI get_field
2
3% OUTPUT:
4% SubField: structure with fields
5   %  .ListVarName: list of selected variables
6   %  .VarDimName: cells with the  corresponding dimension names
7   %  .Field1, 2...: if an input file has been opened by get_field
8% errormsg: error message (=[] when no error)
9
10% INPUT:
11% hget_field: handles of the GUI get_field
12
13function [SubField,errormsg]=read_get_field(hget_field)
14%---------------------------------------------------------
15SubField=[];%default
16errormsg=[]; %default
17handles=guidata(hget_field);%handles of GUI elements in get_field
18Field=get(hget_field,'UserData');% read the current field Structure in the get_field interface
19if isfield(Field,'VarAttribute')
20    VarAttribute=Field.VarAttribute;
21else
22    VarAttribute={};
23end
24
25% select the indices of field variables for 2D plots
26test_1Dplot=get(handles.check_1Dplot,'Value');
27test_scalar=get(handles.check_scalar,'Value');
28test_vector=get(handles.check_vector,'Value');
29
30nbvar=0;
31empty_coord_x=0;
32empty_coord_y=0;
33%dimname_y={};
34ListVarName={};
35VarDimName={};
36SubVarAttribute={};
37%dim_x=0;
38%dim_y=0;
39dim_z=0;
40%dim_vec_x=0;
41%dim_vec_y=0;
42%dim_vec_z=0;
43%c_index=[];
44
45%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46%  ordinary (1D) plot
47if test_1Dplot
48    % select ordinate variable(s)
49    inputlist=get(handles.ordinate,'String');
50    val=get(handles.ordinate,'Value');% selection(s) for ordinate
51    VarNameCell=inputlist(val); %names of the variable(s) in the list
52    VarIndex_y=[];
53    dim_ordinate={};
54    testpermute=[];
55    subvarindex=[];
56    for ilist=1:length(VarNameCell)
57        VarIndex_y(ilist)=name2index(VarNameCell{ilist},Field.ListVarName);%index of the variable in ListVarName
58        dim_ordinate{ilist}=Field.VarDimName{VarIndex_y(ilist)};% name of the corresponding dimension
59        testpermute(ilist)=0;%default
60        nbvar=nbvar+1;
61        ListVarName{nbvar}=Field.ListVarName{VarIndex_y(ilist)};
62        VarDimName{nbvar}=Field.VarDimName{VarIndex_y(ilist)};
63        subvarindex(ilist)=nbvar;
64        if numel(VarAttribute)>=VarIndex_y(ilist)
65            SubVarAttribute{nbvar}=VarAttribute{VarIndex_y(ilist)};
66        end
67        SubVarAttribute{nbvar}.Role='scalar';           
68    end
69   
70    % select abscissa variable
71    inputlist=get(handles.abscissa,'String');
72    val=get(handles.abscissa,'Value');% a single selection is expected for abscissa
73    VarName=inputlist{val}; %name of the variable in the list
74    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
75    if isempty(VarIndex)% default abscissa = matrix index
76        coord_x_name=dim_ordinate{1};% name of the x coordinate = dimension of the plotted quantity
77        if iscell(coord_x_name)
78            coord_x_name=coord_x_name{1};
79        end
80        empty_coord_x=1;
81    else
82        dimname_x=Field.VarDimName{VarIndex};
83        if iscell(dimname_x) && numel(dimname_x)~=1
84            errormsg='abscissa must be a one-dimensional variable';
85            return
86        end
87        nbvar=nbvar+1;
88        ListVarName{nbvar}=Field.ListVarName{VarIndex};
89        VarDimName{nbvar}=Field.VarDimName{VarIndex};
90        if numel(VarAttribute)>=VarIndex
91            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
92        end
93        SubVarAttribute{nbvar}.Role='coord_x';
94         %check consistency of ordinate dimensions
95        for ilist=1:length(VarNameCell)
96            if iscell(dim_ordinate{ilist})
97                if ~strcmp(dim_ordinate{ilist}{1},dimname_x)
98                    if strcmp(dim_ordinate{ilist}{2},dimname_x)
99                        testpermute(ilist)=1;
100                    else
101                        errormsg='inconsistent dimensions for ordinate and abscissa';
102                        return
103                    end
104                end
105            end
106        end
107    end
108end
109test3D=strcmp(get(handles.coord_z_scalar,'Visible'),'on')||strcmp(get(handles.coord_z_vectors,'Visible'),'on');
110VarSubIndexA=[];
111
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113%scalar field
114test_xdimvar=0;%default
115test_ydimvar=0;%default
116test_zdimvar=0;%defaul
117dimname_x=[];
118dimname_y=[];
119dimname_z=[];
120if test_scalar
121    inputlist=get(handles.scalar,'String');
122    if isempty(inputlist)
123        errormsg='empty input field';
124        return
125    end
126    val=get(handles.scalar,'Value');%selected index for the scalar variable
127    VarNameScalar=inputlist{val}; %name of the variable
128    VarIndexA=name2index(VarNameScalar,Field.ListVarName);%index of the variable in ListVarName
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
141    nbvar=nbvar+1;
142    ListVarName{nbvar}=Field.ListVarName{VarIndexA};
143    VarSubIndexA=nbvar;
144    VarDimName{nbvar}=dimname_A;
145    if numel(VarAttribute)>=VarIndexA
146        SubVarAttribute{nbvar}=VarAttribute{VarIndexA};
147    end
148    SubVarAttribute{nbvar}.Role='scalar';
149    field_var_index=VarIndexA; %store the last variable index to determine the absissa dimension if not defiend
150
151     % select x variable
152    inputlist=get(handles.coord_x_scalar,'String');
153    val=get(handles.coord_x_scalar,'Value');% a single selection is expected for abscissa
154    VarName=inputlist{val}; %name of the variable in the list
155    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
156    if isempty(VarIndex)% default abscissa = matrix index
157        empty_coord_x=1;
158    else
159        dimname_x=Field.VarDimName{VarIndex};
160        nbvar=nbvar+1;
161        ListVarName{nbvar}=Field.ListVarName{VarIndex};
162        VarDimName{nbvar}=dimname_x;
163        if numel(VarAttribute)>=VarIndex
164            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
165        end
166         %check consistency of dimensions
167        if ~isequal(dimname_x,dimname_A)% case of dimension variables
168            if iscell(dimname_x)
169                if numel(dimname_x)==1
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
174                else
175                    errormsg='invalid x coordinate selection in get_field';
176                    return
177                end
178            end
179            test_xdimvar=1;
180            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
181        else
182            SubVarAttribute{nbvar}.Role='coord_x';%abcissa with unstructured coordinates
183        end
184    end
185   
186    % select y variable
187    inputlist=get(handles.coord_y_scalar,'String');
188    val=get(handles.coord_y_scalar,'Value');% a single selection is expected for abscissa
189    VarName=inputlist{val}; %name of the variable in the list   
190    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
191    if isempty(VarIndex)% default abscissa = matrix index
192        empty_coord_y=1;
193    else
194        dimname_y=Field.VarDimName{VarIndex};
195        nbvar=nbvar+1;
196        ListVarName{nbvar}=Field.ListVarName{VarIndex};
197        VarDimName{nbvar}=dimname_y;
198        if numel(VarAttribute)>=VarIndex
199            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
200        end
201         %check consistency of dimensions
202        if ~isequal(dimname_y,dimname_A)% case of dimension variables
203             if iscell(dimname_y)
204                if numel(dimname_y)==1
205                    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
209                else
210                    errormsg='invalid y coordinate selection in get_field';
211                    return
212                end
213             end
214            test_ydimvar=1;
215            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
216        else
217            SubVarAttribute{nbvar}.Role='coord_y';%abcissa with unstructured coordinates
218        end
219    end
220
221        % select z variable
222   if test3D % TODO: Lire z comme x et y
223        inputlist=get(handles.coord_z_scalar,'String');
224        val=get(handles.coord_z_scalar,'Value');% a single selection is expected for abscissa
225        VarName=inputlist{val}; %name of the variable in the list   
226        VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
227        if isempty(VarIndex)% default abscissa = matrix index
228            empty_coord_z=1;
229        else
230            dimname_z=Field.VarDimName{VarIndex};
231            nbvar=nbvar+1;
232            ListVarName{nbvar}=Field.ListVarName{VarIndex};
233            VarDimName{nbvar}=dimname_z;
234            if numel(VarAttribute)>=VarIndex
235                SubVarAttribute{nbvar}=VarAttribute{VarIndex};
236            end
237            %check consistency of dimensions
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
242                    else
243                        errormsg='invalid z coordinate selection in get_field';
244                        return
245                    end
246                end
247                test_zdimvar=1;
248                SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
249            else
250                SubVarAttribute{nbvar}.Role='coord_z';%abcissa with unstructured coordinates
251            end
252        end
253   end
254end
255
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257% vectors
258test_vec_x_dimvar=0;%default
259test_vec_y_dimvar=0;%default
260% test_vec_z_dimvar=0;%defaul
261dimname_vec_x=[];
262dimname_vec_y=[];
263dimname_vec_z=[];
264if test_vector
265    %select u variable
266    inputlist=get(handles.vector_x,'String');
267    if isempty(inputlist)
268        errormsg='empty input field';
269        return
270    end
271    val=get(handles.vector_x,'Value');%selected indices in the ordinate listbox
272    VarNameU=inputlist{val}; %name of the variable in the list
273    VarIndexU=name2index(VarNameU,Field.ListVarName);%index of the variable in ListVarName
274    nbvar=nbvar+1;
275    VarSubIndexU=nbvar;
276    ListVarName{nbvar}=Field.ListVarName{VarIndexU};
277    dimname_u=Field.VarDimName{VarIndexU};
278    VarDimName{nbvar}=dimname_u;
279    if numel(VarAttribute)>=VarIndexU
280        SubVarAttribute{nbvar}=VarAttribute{VarIndexU};
281    end
282    SubVarAttribute{nbvar}.Role='vector_x';
283    field_var_index=VarIndexU; %store the last variable index to determine the absissa dimension if not defiend
284   
285    %scalar v variable
286    inputlist=get(handles.vector_y,'String');
287    val=get(handles.vector_y,'Value');%selected indices in the ordinate listbox
288    VarNameV=inputlist{val}; %name of the variable in the list
289    VarIndexV=name2index(VarNameV,Field.ListVarName);%index of the variable in ListVarName
290     %check consistency of dimensions with u
291    dimname_v=Field.VarDimName{VarIndexV};
292    if ~isequal(dimname_v,dimname_u)
293       errormsg='inconsistent dimensions for u and v';
294        return
295    end
296    nbvar=nbvar+1;
297    VarSubIndexV=nbvar;
298    ListVarName{nbvar}=Field.ListVarName{VarIndexV};
299    VarDimName{nbvar}=dimname_u;
300    if numel(VarAttribute)>=VarIndexV
301        SubVarAttribute{nbvar}=VarAttribute{VarIndexV};
302    end
303    SubVarAttribute{nbvar}.Role='vector_y';   
304 
305    % select x variable for vector
306    inputlist=get(handles.coord_x_vectors,'String');
307    val=get(handles.coord_x_vectors,'Value');% a single selection is expected for abscissa
308    VarName=inputlist{val}; %name of the variable in the list
309    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
310    if isempty(VarIndex)% default abscissa = matrix indexTODO like scalar
311        empty_coord_vec_x=1;
312    else
313        empty_coord_vec_x=0;
314        dimname_vec_x=Field.VarDimName{VarIndex};
315        nbvar=nbvar+1;
316        ListVarName{nbvar}=Field.ListVarName{VarIndex};
317        VarDimName{nbvar}=dimname_vec_x;
318        if numel(VarAttribute)>=VarIndex
319            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
320        end
321         %check consistency of dimensions
322        if ~isequal(dimname_vec_x,dimname_u)% case of dimension variables
323            if iscell(dimname_vec_x)
324                if numel(dimname_vec_x)==1
325                    dimname_vec_x=dimname_vec_x{1};%transform to char chain
326                else
327                    errormsg='invalid x coordinate selection in get_field';
328                    return
329                end
330            end
331             test_vec_x_dimvar=1;
332            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
333        else
334            SubVarAttribute{nbvar}.Role='coord_x';%abcissa with unstructured coordinates
335        end
336    end
337       
338     % select y variable for vector
339    inputlist=get(handles.coord_y_vectors,'String');
340    val=get(handles.coord_y_vectors,'Value');% a single selection is expected for abscissa
341    VarName=inputlist{val}; %name of the variable in the list
342    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
343    if isempty(VarIndex)% default abscissa = matrix indexTODO like scalar
344        empty_coord_vec_y=1;
345    else
346        empty_coord_vec_y=0;
347        dimname_vec_y=Field.VarDimName{VarIndex};
348        nbvar=nbvar+1;
349        ListVarName{nbvar}=Field.ListVarName{VarIndex};
350        VarDimName{nbvar}=dimname_vec_y;
351        if numel(VarAttribute)>=VarIndex
352            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
353        end
354         %check consistency of dimensions
355        if ~isequal(dimname_vec_y,dimname_u)% case of dimension variables
356            if iscell(dimname_vec_y)
357                if numel(dimname_vec_y)==1
358                    dimname_vec_y=dimname_vec_y{1};%transform to char chain
359                else
360                    errormsg='invalid y coordinate selection in get_field';
361                    return
362                end
363            end
364             test_vec_y_dimvar=1;
365            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
366        else
367            SubVarAttribute{nbvar}.Role='coord_y';%abcissa with unstructured coordinates
368        end
369    end   
370       
371     % select z variable for vector
372    if test3D
373        inputlist=get(handles.coord_z_vectors,'String');
374        val=get(handles.coord_z_vectors,'Value');% a single selection is expected for abscissa
375        VarName=inputlist{val}; %name of the variable in the list
376        VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
377        if isempty(VarIndex)% default abscissa = matrix indexTODO like scalar
378    %         coord_x_name=dimname_u{2};% name of the x coordinate = dimension of the plotted quantity
379%             empty_coord_vec_z=1;
380        else
381            dimname_vec_z=Field.VarDimName{VarIndex};
382            nbvar=nbvar+1;
383            ListVarName{nbvar}=Field.ListVarName{VarIndex};
384            VarDimName{nbvar}=dimname_vec_z;
385            if numel(VarAttribute)>=VarIndex
386                SubVarAttribute{nbvar}=VarAttribute{VarIndex};
387            end
388             %check consistency of dimensions
389            if ~isequal(dimname_vec_z,dimname_u)% case of dimension variables
390                if iscell(dimname_vec_z)
391                    if numel(dimname_vec_z)==1
392                        dimname_vec_z=dimname_vec_y{1};%transform to char chain
393                    else
394                        errormsg='invalid y coordinate selection in get_field';
395                        return
396                    end
397                end
398%                 test_vec_z_dimvar=1;
399                SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
400            else
401                SubVarAttribute{nbvar}.Role='coord_z';%abcissa with unstructured coordinates
402            end
403        end       
404    end   
405           
406    if test3D %  (a revoir) 
407        %scalar w variable
408        inputlist=get(handles.vector_z,'String');
409        val=get(handles.vector_z,'Value');%selected indices in the ordinate listbox
410        VarNameW=inputlist{val}; %name of the variable in the list
411        VarIndex=name2index(VarNameW,Field.ListVarName);%index of the variable in ListVarName
412        %check consistency of dimensions with u
413        if ~isempty( VarIndex)
414            dimname_w=Field.VarDimName{VarIndex};
415            if ~isequal(dimname_w,dimname_u)
416                errormsg='inconsistent dimensions for u and w';
417                return
418            end
419            nbvar=nbvar+1;
420            %         w_index=nbvar;
421            ListVarName{nbvar}=Field.ListVarName{VarIndex};
422            VarDimName{nbvar}=dimname_u;
423            if numel(VarAttribute)>=VarIndex
424                SubVarAttribute{nbvar}=VarAttribute{VarIndex};
425            end
426            SubVarAttribute{nbvar}.Role='vector_z';
427        end
428    end 
429   
430    % select color variable
431    inputlist=get(handles.vec_color,'String');
432    val=get(handles.vec_color,'Value');% a single selection is expected for abscissa
433    VarNameC=inputlist{val}; %name of the variable in the list
434    VarIndex=name2index(VarNameC,Field.ListVarName);%index of the variable in ListVarName
435       %check consistency of dimensions with u
436    if ~isempty(VarIndex)
437        if ~isequal(Field.VarDimName{VarIndex},dimname_u)
438            errormsg='inconsistent dimensions for u and v';
439            return
440        end
441        nbvar=nbvar+1;
442%         c_index=nbvar;
443        ListVarName{nbvar}=Field.ListVarName{VarIndex};
444        VarDimName{nbvar}=Field.VarDimName{VarIndex};
445        if numel(VarAttribute)>=VarIndex
446            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
447        end
448        SubVarAttribute{nbvar}.Role='scalar';
449    end
450end
451
452
453
454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455%% get the input field
456inputfield=get(handles.inputfile,'String');
457if exist(inputfield,'file')% read the input data corresponding to the list of selected varaibles
458    SubField=nc2struct(inputfield,ListVarName);
459else  % subfield stored in memory
460    SubField.ListGlobalAttribute={};
461    SubField.ListVarName=ListVarName;
462    SubField.VarDimName=VarDimName;
463end
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];
479SubField.InputFile=get(handles.inputfile,'String');
480SubField.VarAttribute=SubVarAttribute;
481
482%permute indices if coord_y is not the first matrix index: scalar case
483NbDim=2; %default
484if test_scalar
485    VarNameA=Field.ListVarName{VarIndexA};%name of the scalar variable
486    DimCellA=Field.VarDimName{VarIndexA};  %dimension names for the scalar variable
487    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
492    SingleCellA={};
493    if numel(npxy) < numel(DimCellA)
494        SingleCellA=DimCellA(1:end-numel(npxy));
495        DimCellA=DimCellA(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
496    end
497    ind_select=find(npxy~=1);%look for non singleton dimensions
498    DimCellA=DimCellA(ind_select);%dimension names for the scalar variable, after removing singletons
499    npxy=npxy(ind_select);
500%     if ~isempty(TimeVarIndex)%
501%         DimCellA(TimeVarIndex)=[];%suppress the time dimension
502%          npxy(TimeVarIndex)=[];
503%     end
504    NbDim=numel(npxy);
505    dimA=[];
506    if test_zdimvar
507        NbDim=3;% field considered as 3D if a z coordinate is defined (to distinguish for instance from 2D color images with 3 components)
508        ind_singleton=find(strcmp(dimname_z,SingleCellA),1);% look for coincidence of dimension with one of the singleton dimensions
509        if ~isempty(ind_singleton)
510             errormsg=['the singleton dimension ' dimname_z ' has been selected for z'];
511             return
512        end
513        icoord=find(strcmp(dimname_z,DimCellA),1);% a dimension variable
514        dimA=[dimA icoord];
515    end
516    if test_ydimvar
517        ind_singleton=find(strcmp(dimname_y,SingleCellA),1);% look for coincidence of dimension with one of the singleton dimensions
518        if ~isempty(ind_singleton)
519             errormsg=['the singleton dimension ' dimname_y ' has been selected for ordinate'];
520             return
521        end
522        icoord=find(strcmp(dimname_y,DimCellA),1);% a dimension variable
523        dimA=[dimA icoord];
524    end
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)         
528             errormsg=['the singleton dimension ' dimname_x ' has been selected for ordinate'];
529             return
530        end
531        icoord=find(strcmp(dimname_x{1},DimCellA),1);% a dimension variable
532        dimA=[dimA icoord];
533    end
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
559    DimCellA=DimCellA([dimA dimextra]);
560    SubField.VarDimName{VarSubIndexA}=DimCellA; 
561    %add default coord_x and/or coord_y if empty
562    if empty_coord_x || empty_coord_y
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
568        end
569        ind_select=find(npxy~=1) ;%look for non singleton dimensions
570        DimCellA=DimCellA(ind_select);%list of dimension names for the scalar, after singleton removal
571        npxy=npxy(ind_select);
572        testold=0;
573    %old convention; use of coord_1 and Coord_2
574        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=field_var_index
575            if isfield(Field.VarAttribute{field_var_index},'Coord_2')&& isfield(Field.VarAttribute{field_var_index},'Coord_1')
576                 Coord_2=Field.VarAttribute{field_var_index}.Coord_2;
577                 Coord_1=Field.VarAttribute{field_var_index}.Coord_1;
578                testold=1;
579            end
580        end
581        if empty_coord_x       
582                coord_x_name=DimCellA{NbDim};
583                SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
584                SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
585                if testold
586                    eval(['SubField.' coord_x_name '=linspace(Coord_2(1),Coord_2(end),npxy(2));'])
587                else
588                    eval(['SubField.' coord_x_name '=[0.5 npxy(NbDim)-0.5];'])
589                end
590           
591            if ~testold
592                coord_x_attr.units='index';
593            else
594                coord_x_attr.units='cm';
595            end
596            SubField.VarAttribute=[{coord_x_attr} SubField.VarAttribute]; 
597        end
598        if empty_coord_y
599            coord_y_name=DimCellA{NbDim-1};
600            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
601            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
602            if testold
603                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
604            else
605                eval(['SubField.' coord_y_name '=[npxy(NbDim-1)-0.5 0.5];'])
606            end
607            if ~testold
608                coord_y_attr.units='index';
609            else
610                coord_y_attr.units='cm';
611            end
612            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];   
629        end
630    end
631end
632
633%permute indices if coord_y is not the first matrix index: vector case
634if test_vector
635    VarNameU=Field.ListVarName{VarIndexU}; % name of u component variable
636    DimCellU=Field.VarDimName{VarIndexU}; % list of dimensions for u component 
637    eval(['npxy=size(SubField.' VarNameU ');']) % npxy= dimension values for the u component
638    SingleCellU={};
639    if numel(npxy) < numel(DimCellU)
640        SingleCellU=DimCellU(1:end-numel(npxy));
641        DimCellU=DimCellU(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
642    end
643    ind_single=find(npxy==1);%indices of singleton dimensions
644    if ind_single<=numel(DimCellU)
645        SingleCellU=[SingleCellU DimCellU(ind_single)];
646    end
647    ind_select=find(npxy~=1);%look for non singleton dimensions
648    if numel(DimCellU)>=ind_select
649        DimCellU=DimCellU(ind_select);
650    end
651    npxy=npxy(ind_select);
652    dimU=[];
653    if test_zdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
654        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
655            if strcmp(dimname_vec_z,SingleCellU{icoord})% a singleton dimension
656                errormsg=['the singleton dimension ' dimname_vec_z ' has been selected for z'];
657                return
658            end
659        end
660        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
661             if strcmp(dimname_vec_z,DimCellU{icoord})% a dimension variable
662                 dimU=[dimU icoord];
663                 break
664             end
665        end
666    end
667    if test_vec_y_dimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
668        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
669            if strcmp(dimname_vec_y,SingleCellU{icoord})% a singleton dimension
670                errormsg=['the singleton dimension ' dimname_vec_y ' has been selected for ordinate'];
671                return
672            end
673        end
674        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
675             if strcmp(dimname_vec_y,DimCellU{icoord})% a dimension variable
676                 dimU=[dimU icoord];
677                 break
678             end
679        end
680    end
681    if test_xdimvar
682        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
683            if strcmp(dimname_x,SingleCellU{icoord})% a singleton dimension
684                errormsg=['the singleton dimension ' dimname_vec_x ' has been selected for abscissa'];
685                return
686            end
687        end
688        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
689             if strcmp(dimname_vec_x,DimCellU{icoord})% a dimension variable
690                 dimU=[dimU icoord];
691                 break
692             end
693        end
694    end
695    if numel(DimCellU)>1
696        dimextra=(1:numel(DimCellU));
697        dimextra(dimU)=[]; %list of unselected dimension indices
698        DimCellU=DimCellU([dimU dimextra]);
699        eval(['SubField.' VarNameU '=permute(squeeze(SubField.' VarNameU '),[dimU dimextra]);'])
700        eval(['SubField.' VarNameV '=permute(squeeze(SubField.' VarNameV '),[dimU dimextra]);'])
701        SubField.VarDimName{VarSubIndexU}=DimCellU;
702        SubField.VarDimName{VarSubIndexV}=DimCellU;
703    end
704    %add default coord_x and/or coord_y if empty
705    if empty_coord_vec_x || empty_coord_vec_y
706        VarName=Field.ListVarName{field_var_index};
707        DimCell=Field.VarDimName{field_var_index};   
708        eval(['npxy=size(SubField.' VarName ');'])
709        if numel(npxy) < numel(DimCell)
710            DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
711        end
712        ind_select=find(npxy~=1) ;%look for non singleton dimensions
713        DimCell=DimCell(ind_select);
714        npxy=npxy(ind_select);
715        testold=0;
716    %old convention; use of coord_1 and Coord_2
717        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=field_var_index
718            if isfield(Field.VarAttribute{field_var_index},'Coord_2')&& isfield(Field.VarAttribute{field_var_index},'Coord_1')
719                 Coord_2=Field.VarAttribute{field_var_index}.Coord_2;
720                 Coord_1=Field.VarAttribute{field_var_index}.Coord_1;
721                testold=1;
722            end
723        end
724        if empty_coord_vec_x
725            if numel(DimCell)<2 
726                errormsg='undefined x coordinate';
727                return
728            end
729            coord_x_name=DimCell{2};
730            SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
731            SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
732            if testold
733                eval(['SubField.' coord_x_name '=linspace(Coord_2(1),Coord_2(end),npxy(2));'])
734            else
735                eval(['SubField.' coord_x_name '=[0.5 npxy(2)-0.5];'])
736            end
737           
738            if ~testold
739                coord_x_attr.units='index';
740            else
741                coord_x_attr.units='cm';
742            end
743            SubField.VarAttribute=[{coord_x_attr} SubField.VarAttribute]; 
744        end
745        if empty_coord_vec_y
746            coord_y_name=DimCell{1};
747            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
748            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
749            if testold
750                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
751            else
752                eval(['SubField.' coord_y_name '=[npxy(1)-0.5 0.5];'])
753            end
754            if ~testold
755                coord_y_attr.units='index';
756            else
757                coord_y_attr.units='cm';
758            end
759            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
760        end
761    end
762end
763if test_1Dplot
764    for ilist=1:numel(VarIndex_y)
765        VarName=Field.ListVarName{VarIndex_y(ilist)};
766        eval(['npxy=size(SubField.' VarName ');'])
767        ind_select=find(npxy~=1);
768        SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}(ind_select);
769        eval(['SubField.' VarName '=squeeze(SubField.' VarName ');'])%remove singleton dimensions
770        if testpermute(ilist)
771            eval(['SubField.' VarName '=permute(SubField.' VarName ',[2 1]);'])
772            SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}([2 1]);
773        end
774    end
775    if empty_coord_x
776        SubField.ListVarName=[{[coord_x_name '_index']} SubField.ListVarName];
777        SubField.VarDimName=[{coord_x_name } SubField.VarDimName];
778        VarName=Field.ListVarName{VarIndex_y(1)};
779        DimCell=Field.VarDimName{VarIndex_y(1)};   
780        eval(['npxy=size(SubField.' VarName ');'])
781        if numel(npxy) < numel(DimCell)
782%             DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
783        end
784        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=VarIndex_y(1) ...
785                             && isfield(Field.VarAttribute{VarIndex_y(1)},'Coord_1')
786%              Coord_1=Field.VarAttribute{VarIndex_y(1)}.Coord_1;%old convention; use of coord_1
787             eval(['SubField.' coord_x_name '_index=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
788        else
789            eval(['SubField.' coord_x_name '_index=linspace(0.5,npxy(1)-0.5,npxy(1));'])
790        end
791        struct.Role='coord_x';
792        SubField.VarAttribute=[{struct} SubField.VarAttribute];
793    end
794end
795
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
807%-------------------------------------------------
808% give index numbers of the strings str in the list ListvarName
809function VarIndex_y=name2index(cell_str,ListVarName)
810VarIndex_y=[];
811if ischar(cell_str)
812    for ivar=1:length(ListVarName)
813        varlist=ListVarName{ivar};
814        if isequal(varlist,cell_str)
815            VarIndex_y= ivar;
816            break
817        end
818    end
819elseif iscell(cell_str)
820    for isel=1:length(cell_str)
821        varsel=cell_str{isel};
822        for ivar=1:length(ListVarName)
823            varlist=ListVarName{ivar};
824            if isequal(varlist,varsel)
825                VarIndex_y=[VarIndex_y ivar];
826            end
827        end
828    end
829end
Note: See TracBrowser for help on using the repository browser.