source: trunk/src/read_get_field.m @ 279

Last change on this file since 279 was 231, checked in by sommeria, 14 years ago

interactive test for piv introduced various bug corrections

File size: 34.4 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            if numel(DimCellA)<2
600                errormsg=['coordinates need to be defined for the scalar ' VarNameA];
601                return
602            end
603            coord_y_name=DimCellA{NbDim-1};
604            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
605            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
606            if testold
607                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
608            else
609                eval(['SubField.' coord_y_name '=[npxy(NbDim-1)-0.5 0.5];'])
610            end
611            if ~testold
612                coord_y_attr.units='index';
613            else
614                coord_y_attr.units='cm';
615            end
616            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
617        end
618        if empty_coord_z && NbDim==3
619            coord_z_name=DimCellA{NbDim-2};
620            SubField.ListVarName=[{coord_z_name} SubField.ListVarName];
621            SubField.VarDimName=[{coord_z_name} SubField.VarDimName];
622            if testold
623                eval(['SubField.' coord_z_name '=linspace(Coord_3(1),Coord_3(end),npxy(1));'])
624            else
625                eval(['SubField.' coord_z_name '=[0.5 npxy(NbDim-2)-0.5];'])
626            end
627            if ~testold
628                coord_z_attr.units='index';
629            else
630                coord_z_attr.units='cm';
631            end
632            SubField.VarAttribute=[{coord_z_attr} SubField.VarAttribute];   
633        end
634    end
635end
636
637%permute indices if coord_y is not the first matrix index: vector case
638if test_vector
639    VarNameU=Field.ListVarName{VarIndexU}; % name of u component variable
640    DimCellU=Field.VarDimName{VarIndexU}; % list of dimensions for u component 
641    eval(['npxy=size(SubField.' VarNameU ');']) % npxy= dimension values for the u component
642    SingleCellU={};
643    if numel(npxy) < numel(DimCellU)
644        SingleCellU=DimCellU(1:end-numel(npxy));
645        DimCellU=DimCellU(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
646    end
647    ind_single=find(npxy==1);%indices of singleton dimensions
648    if ind_single<=numel(DimCellU)
649        SingleCellU=[SingleCellU DimCellU(ind_single)];
650    end
651    ind_select=find(npxy~=1);%look for non singleton dimensions
652    if numel(DimCellU)>=ind_select
653        DimCellU=DimCellU(ind_select);
654    end
655    npxy=npxy(ind_select);
656    dimU=[];
657    if test_zdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
658        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
659            if strcmp(dimname_vec_z,SingleCellU{icoord})% a singleton dimension
660                errormsg=['the singleton dimension ' dimname_vec_z ' has been selected for z'];
661                return
662            end
663        end
664        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
665             if strcmp(dimname_vec_z,DimCellU{icoord})% a dimension variable
666                 dimU=[dimU icoord];
667                 break
668             end
669        end
670    end
671    if test_vec_y_dimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
672        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
673            if strcmp(dimname_vec_y,SingleCellU{icoord})% a singleton dimension
674                errormsg=['the singleton dimension ' dimname_vec_y ' has been selected for ordinate'];
675                return
676            end
677        end
678        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
679             if strcmp(dimname_vec_y,DimCellU{icoord})% a dimension variable
680                 dimU=[dimU icoord];
681                 break
682             end
683        end
684    end
685    if test_xdimvar
686        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
687            if strcmp(dimname_x,SingleCellU{icoord})% a singleton dimension
688                errormsg=['the singleton dimension ' dimname_vec_x ' has been selected for abscissa'];
689                return
690            end
691        end
692        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
693             if strcmp(dimname_vec_x,DimCellU{icoord})% a dimension variable
694                 dimU=[dimU icoord];
695                 break
696             end
697        end
698    end
699    if numel(DimCellU)>1
700        dimextra=(1:numel(DimCellU));
701        dimextra(dimU)=[]; %list of unselected dimension indices
702        DimCellU=DimCellU([dimU dimextra]);
703        eval(['SubField.' VarNameU '=permute(squeeze(SubField.' VarNameU '),[dimU dimextra]);'])
704        eval(['SubField.' VarNameV '=permute(squeeze(SubField.' VarNameV '),[dimU dimextra]);'])
705        SubField.VarDimName{VarSubIndexU}=DimCellU;
706        SubField.VarDimName{VarSubIndexV}=DimCellU;
707    end
708    %add default coord_x and/or coord_y if empty
709    if empty_coord_vec_x || empty_coord_vec_y
710        VarName=Field.ListVarName{field_var_index};
711        DimCell=Field.VarDimName{field_var_index};   
712        eval(['npxy=size(SubField.' VarName ');'])
713        if numel(npxy) < numel(DimCell)
714            DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
715        end
716        ind_select=find(npxy~=1) ;%look for non singleton dimensions
717        DimCell=DimCell(ind_select);
718        npxy=npxy(ind_select);
719        testold=0;
720    %old convention; use of coord_1 and Coord_2
721        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=field_var_index
722            if isfield(Field.VarAttribute{field_var_index},'Coord_2')&& isfield(Field.VarAttribute{field_var_index},'Coord_1')
723                 Coord_2=Field.VarAttribute{field_var_index}.Coord_2;
724                 Coord_1=Field.VarAttribute{field_var_index}.Coord_1;
725                testold=1;
726            end
727        end
728        if empty_coord_vec_x
729            if numel(DimCell)<2 
730                errormsg='undefined x coordinate';
731                return
732            end
733            coord_x_name=DimCell{2};
734            SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
735            SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
736            if testold
737                eval(['SubField.' coord_x_name '=linspace(Coord_2(1),Coord_2(end),npxy(2));'])
738            else
739                eval(['SubField.' coord_x_name '=[0.5 npxy(2)-0.5];'])
740            end
741           
742            if ~testold
743                coord_x_attr.units='index';
744            else
745                coord_x_attr.units='cm';
746            end
747            SubField.VarAttribute=[{coord_x_attr} SubField.VarAttribute]; 
748        end
749        if empty_coord_vec_y
750            coord_y_name=DimCell{1};
751            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
752            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
753            if testold
754                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
755            else
756                eval(['SubField.' coord_y_name '=[npxy(1)-0.5 0.5];'])
757            end
758            if ~testold
759                coord_y_attr.units='index';
760            else
761                coord_y_attr.units='cm';
762            end
763            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
764        end
765    end
766end
767if test_1Dplot
768    for ilist=1:numel(VarIndex_y)
769        VarName=Field.ListVarName{VarIndex_y(ilist)};
770        eval(['npxy=size(SubField.' VarName ');'])
771        ind_select=find(npxy~=1);
772        SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}(ind_select);
773        eval(['SubField.' VarName '=squeeze(SubField.' VarName ');'])%remove singleton dimensions
774        if testpermute(ilist)
775            eval(['SubField.' VarName '=permute(SubField.' VarName ',[2 1]);'])
776            SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}([2 1]);
777        end
778    end
779    if empty_coord_x
780        SubField.ListVarName=[{[coord_x_name '_index']} SubField.ListVarName];
781        SubField.VarDimName=[{coord_x_name } SubField.VarDimName];
782        VarName=Field.ListVarName{VarIndex_y(1)};
783        DimCell=Field.VarDimName{VarIndex_y(1)};   
784        eval(['npxy=size(SubField.' VarName ');'])
785        if numel(npxy) < numel(DimCell)
786%             DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
787        end
788        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=VarIndex_y(1) ...
789                             && isfield(Field.VarAttribute{VarIndex_y(1)},'Coord_1')
790%              Coord_1=Field.VarAttribute{VarIndex_y(1)}.Coord_1;%old convention; use of coord_1
791             eval(['SubField.' coord_x_name '_index=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
792        else
793            eval(['SubField.' coord_x_name '_index=linspace(0.5,npxy(1)-0.5,npxy(1));'])
794        end
795        struct.Role='coord_x';
796        SubField.VarAttribute=[{struct} SubField.VarAttribute];
797    end
798end
799
800%% remove time variable
801if get(handles.TimeDimension,'Value')
802
803%     TimeName=get(handles.TimeName,'String');
804%     time_index=find(strcmp(TimeName,SubField.ListVarName),1);
805%     SubField.ListVarName(TimeVarIndex)=[];
806%     SubField.VarDimName(TimeVarIndex)=[];
807%     SubField.VarAttribute(TimeVarIndex)=[];
808end
809
810
811%-------------------------------------------------
812% give index numbers of the strings str in the list ListvarName
813function VarIndex_y=name2index(cell_str,ListVarName)
814VarIndex_y=[];
815if ischar(cell_str)
816    for ivar=1:length(ListVarName)
817        varlist=ListVarName{ivar};
818        if isequal(varlist,cell_str)
819            VarIndex_y= ivar;
820            break
821        end
822    end
823elseif iscell(cell_str)
824    for isel=1:length(cell_str)
825        varsel=cell_str{isel};
826        for ivar=1:length(ListVarName)
827            varlist=ListVarName{ivar};
828            if isequal(varlist,varsel)
829                VarIndex_y=[VarIndex_y ivar];
830            end
831        end
832    end
833end
Note: See TracBrowser for help on using the repository browser.