source: trunk/src/read_get_field.m @ 102

Last change on this file since 102 was 89, checked in by sommeria, 14 years ago

many bug corrections and cleaning. Activation of the BW option in uvmat. Improvement of the interaction of get_field with uvmat.

File size: 31.2 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 indices in the ordinate listbox
127    VarNameScalar=inputlist{val}; %name of the variable in the list
128    VarIndexA=name2index(VarNameScalar,Field.ListVarName);%index of the variable in ListVarName
129    dimname_A=Field.VarDimName{VarIndexA};
130    nbvar=nbvar+1;
131    ListVarName{nbvar}=Field.ListVarName{VarIndexA};
132    VarSubIndexA=nbvar;
133    VarDimName{nbvar}=dimname_A;
134    if numel(VarAttribute)>=VarIndexA
135        SubVarAttribute{nbvar}=VarAttribute{VarIndexA};
136    end
137    SubVarAttribute{nbvar}.Role='scalar';
138    field_var_index=VarIndexA; %store the last variable index to determine the absissa dimension if not defiend
139
140     % select x variable
141    inputlist=get(handles.coord_x_scalar,'String');
142    val=get(handles.coord_x_scalar,'Value');% a single selection is expected for abscissa
143    VarName=inputlist{val}; %name of the variable in the list
144    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
145    if isempty(VarIndex)% default abscissa = matrix index
146        empty_coord_x=1;
147    else
148        dimname_x=Field.VarDimName{VarIndex};
149        nbvar=nbvar+1;
150        ListVarName{nbvar}=Field.ListVarName{VarIndex};
151        VarDimName{nbvar}=dimname_x;
152        if numel(VarAttribute)>=VarIndex
153            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
154        end
155         %check consistency of dimensions
156        if ~isequal(dimname_x,dimname_A)% case of dimension variables
157            if iscell(dimname_x)
158                if numel(dimname_x)==1
159                    dimname_x=dimname_x{1};%transform to char chain
160                else
161                    errormsg='invalid x coordinate selection in get_field';
162                    return
163                end
164            end
165            test_xdimvar=1;
166            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
167        else
168            SubVarAttribute{nbvar}.Role='coord_x';%abcissa with unstructured coordinates
169        end
170    end
171   
172    % select y variable
173    inputlist=get(handles.coord_y_scalar,'String');
174    val=get(handles.coord_y_scalar,'Value');% a single selection is expected for abscissa
175    VarName=inputlist{val}; %name of the variable in the list   
176    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
177    if isempty(VarIndex)% default abscissa = matrix index
178        empty_coord_y=1;
179    else
180        dimname_y=Field.VarDimName{VarIndex};
181         %check consistency of dimensions
182        nbvar=nbvar+1;
183        ListVarName{nbvar}=Field.ListVarName{VarIndex};
184        VarDimName{nbvar}=dimname_y;
185        if numel(VarAttribute)>=VarIndex
186            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
187        end
188         %check consistency of dimensions
189        if ~isequal(dimname_y,dimname_A)% case of dimension variables
190             if iscell(dimname_y)
191                if numel(dimname_y)==1
192                    dimname_y=dimname_y{1};%transform to char chain
193                else
194                    errormsg='invalid y coordinate selection in get_field';
195                    return
196                end
197             end
198            test_ydimvar=1;
199            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
200        else
201            SubVarAttribute{nbvar}.Role='coord_y';%abcissa with unstructured coordinates
202        end
203%         if isequal(dimname_y,dimname_x)
204%             errormsg='identical x and y coordinates selected in get_field';
205%             return
206%         end
207    end
208
209        % select z variable
210   if test3D % TODO: Lire z comme x et y
211        inputlist=get(handles.coord_z_scalar,'String');
212        val=get(handles.coord_z_scalar,'Value');% a single selection is expected for abscissa
213        VarName=inputlist{val}; %name of the variable in the list   
214        VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
215        if isempty(VarIndex)% default abscissa = matrix index
216%             coord_z_name=dimname_A{1};% name of the x coordinate = dimension of the plotted quantity
217%            empty_coord_z=1;
218        else
219            dimname_z=Field.VarDimName{VarIndex};
220             %check consistency of dimensions
221            if ~isequal(dimname_z,dimname_A)
222                for icoord=1:numel(dimname_A)
223                    if strcmp(dimname_z,dimname_A{icoord})%  a dimension variable
224                         dim_z=icoord;
225                        break
226                    end
227                end
228                if ~dim_z
229                    errormsg='inconsistent dimensions for coordinate z';
230                    return
231                end
232            end
233            nbvar=nbvar+1;
234            ListVarName{nbvar}=Field.ListVarName{VarIndex};
235            VarDimName{nbvar}=dimname_z;
236            if numel(VarAttribute)>=VarIndex
237                SubVarAttribute{nbvar}=VarAttribute{VarIndex};
238            end
239            if dim_z
240                SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
241            else
242                SubVarAttribute{nbvar}.Role='coord_z';%z coordinate with unstructured coordinates
243            end
244        end
245   end
246end
247
248%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249% vectors
250% test_vec_x_dimvar=0;%default
251% test_vec_y_dimvar=0;%default
252% test_vec_z_dimvar=0;%defaul
253dimname_vec_x=[];
254dimname_vec_y=[];
255dimname_vec_z=[];
256if test_vector
257    %select u variable
258    inputlist=get(handles.vector_x,'String');
259    if isempty(inputlist)
260        errormsg='empty input field';
261        return
262    end
263    val=get(handles.vector_x,'Value');%selected indices in the ordinate listbox
264    VarNameU=inputlist{val}; %name of the variable in the list
265    VarIndexU=name2index(VarNameU,Field.ListVarName);%index of the variable in ListVarName
266    nbvar=nbvar+1;
267    VarSubIndexU=nbvar;
268    ListVarName{nbvar}=Field.ListVarName{VarIndexU};
269    dimname_u=Field.VarDimName{VarIndexU};
270    VarDimName{nbvar}=dimname_u;
271    if numel(VarAttribute)>=VarIndexU
272        SubVarAttribute{nbvar}=VarAttribute{VarIndexU};
273    end
274    SubVarAttribute{nbvar}.Role='vector_x';
275    field_var_index=VarIndexU; %store the last variable index to determine the absissa dimension if not defiend
276   
277    %scalar v variable
278    inputlist=get(handles.vector_y,'String');
279    val=get(handles.vector_y,'Value');%selected indices in the ordinate listbox
280    VarNameV=inputlist{val}; %name of the variable in the list
281    VarIndexV=name2index(VarNameV,Field.ListVarName);%index of the variable in ListVarName
282     %check consistency of dimensions with u
283    dimname_v=Field.VarDimName{VarIndexV};
284    if ~isequal(dimname_v,dimname_u)
285       errormsg='inconsistent dimensions for u and v';
286        return
287    end
288    nbvar=nbvar+1;
289    VarSubIndexV=nbvar;
290    ListVarName{nbvar}=Field.ListVarName{VarIndexV};
291    VarDimName{nbvar}=dimname_u;
292    if numel(VarAttribute)>=VarIndexV
293        SubVarAttribute{nbvar}=VarAttribute{VarIndexV};
294    end
295    SubVarAttribute{nbvar}.Role='vector_y';   
296 
297    % select x variable for vector
298    inputlist=get(handles.coord_x_vectors,'String');
299    val=get(handles.coord_x_vectors,'Value');% a single selection is expected for abscissa
300    VarName=inputlist{val}; %name of the variable in the list
301    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
302    if isempty(VarIndex)% default abscissa = matrix indexTODO like scalar
303        empty_coord_vec_x=1;
304    else
305        empty_coord_vec_x=0;
306        dimname_vec_x=Field.VarDimName{VarIndex};
307        nbvar=nbvar+1;
308        ListVarName{nbvar}=Field.ListVarName{VarIndex};
309        VarDimName{nbvar}=dimname_vec_x;
310        if numel(VarAttribute)>=VarIndex
311            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
312        end
313         %check consistency of dimensions
314        if ~isequal(dimname_vec_x,dimname_u)% case of dimension variables
315            if iscell(dimname_vec_x)
316                if numel(dimname_vec_x)==1
317                    dimname_vec_x=dimname_vec_x{1};%transform to char chain
318                else
319                    errormsg='invalid x coordinate selection in get_field';
320                    return
321                end
322            end
323%             test_vec_x_dimvar=1;
324            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
325        else
326            SubVarAttribute{nbvar}.Role='coord_x';%abcissa with unstructured coordinates
327        end
328    end
329       
330     % select y variable for vector
331    inputlist=get(handles.coord_y_vectors,'String');
332    val=get(handles.coord_y_vectors,'Value');% a single selection is expected for abscissa
333    VarName=inputlist{val}; %name of the variable in the list
334    VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
335    if isempty(VarIndex)% default abscissa = matrix indexTODO like scalar
336        empty_coord_vec_y=1;
337    else
338        empty_coord_vec_y=0;
339        dimname_vec_y=Field.VarDimName{VarIndex};
340        nbvar=nbvar+1;
341        ListVarName{nbvar}=Field.ListVarName{VarIndex};
342        VarDimName{nbvar}=dimname_vec_y;
343        if numel(VarAttribute)>=VarIndex
344            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
345        end
346         %check consistency of dimensions
347        if ~isequal(dimname_vec_y,dimname_u)% case of dimension variables
348            if iscell(dimname_vec_y)
349                if numel(dimname_vec_y)==1
350                    dimname_vec_y=dimname_vec_y{1};%transform to char chain
351                else
352                    errormsg='invalid y coordinate selection in get_field';
353                    return
354                end
355            end
356%             test_vec_y_dimvar=1;
357            SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
358        else
359            SubVarAttribute{nbvar}.Role='coord_y';%abcissa with unstructured coordinates
360        end
361    end   
362       
363     % select z variable for vector
364    if test3D
365        inputlist=get(handles.coord_z_vectors,'String');
366        val=get(handles.coord_z_vectors,'Value');% a single selection is expected for abscissa
367        VarName=inputlist{val}; %name of the variable in the list
368        VarIndex=name2index(VarName,Field.ListVarName);%index of the variable in ListVarName
369        if isempty(VarIndex)% default abscissa = matrix indexTODO like scalar
370    %         coord_x_name=dimname_u{2};% name of the x coordinate = dimension of the plotted quantity
371%             empty_coord_vec_z=1;
372        else
373            dimname_vec_z=Field.VarDimName{VarIndex};
374            nbvar=nbvar+1;
375            ListVarName{nbvar}=Field.ListVarName{VarIndex};
376            VarDimName{nbvar}=dimname_vec_z;
377            if numel(VarAttribute)>=VarIndex
378                SubVarAttribute{nbvar}=VarAttribute{VarIndex};
379            end
380             %check consistency of dimensions
381            if ~isequal(dimname_vec_z,dimname_u)% case of dimension variables
382                if iscell(dimname_vec_z)
383                    if numel(dimname_vec_z)==1
384                        dimname_vec_z=dimname_vec_y{1};%transform to char chain
385                    else
386                        errormsg='invalid y coordinate selection in get_field';
387                        return
388                    end
389                end
390%                 test_vec_z_dimvar=1;
391                SubVarAttribute{nbvar}.Role='dimvar';% dimension variable
392            else
393                SubVarAttribute{nbvar}.Role='coord_z';%abcissa with unstructured coordinates
394            end
395        end       
396    end   
397           
398    if test3D %  (a revoir) 
399         %scalar w variable
400        inputlist=get(handles.vector_z,'String');
401        val=get(handles.vector_z,'Value');%selected indices in the ordinate listbox
402        VarNameW=inputlist{val}; %name of the variable in the list
403        VarIndex=name2index(VarNameW,Field.ListVarName);%index of the variable in ListVarName
404         %check consistency of dimensions with u
405        dimname_w=Field.VarDimName{VarIndex};
406        if ~isequal(dimname_w,dimname_u)
407           errormsg='inconsistent dimensions for u and v';
408            return
409        end
410        nbvar=nbvar+1;
411%         w_index=nbvar;
412        ListVarName{nbvar}=Field.ListVarName{VarIndex};
413        VarDimName{nbvar}=dimname_u;
414        if numel(VarAttribute)>=VarIndex
415            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
416        end
417        SubVarAttribute{nbvar}.Role='vector_z';
418    end 
419   
420    % select color variable
421    inputlist=get(handles.vec_color,'String');
422    val=get(handles.vec_color,'Value');% a single selection is expected for abscissa
423    VarNameC=inputlist{val}; %name of the variable in the list
424    VarIndex=name2index(VarNameC,Field.ListVarName);%index of the variable in ListVarName
425       %check consistency of dimensions with u
426    if ~isempty(VarIndex)
427        if ~isequal(Field.VarDimName{VarIndex},dimname_u)
428            errormsg='inconsistent dimensions for u and v';
429            return
430        end
431        nbvar=nbvar+1;
432%         c_index=nbvar;
433        ListVarName{nbvar}=Field.ListVarName{VarIndex};
434        VarDimName{nbvar}=Field.VarDimName{VarIndex};
435        if numel(VarAttribute)>=VarIndex
436            SubVarAttribute{nbvar}=VarAttribute{VarIndex};
437        end
438        SubVarAttribute{nbvar}.Role='scalar';
439    end
440end
441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442
443% get the input field
444inputfield=get(handles.inputfile,'String');
445if exist(inputfield,'file')% read the input data corresponding to the list of selected varaibles
446    SubField=nc2struct(inputfield,ListVarName);
447else  % subfield stored in memory
448    SubField.ListGlobalAttribute={};
449    SubField.ListVarName=ListVarName;
450    SubField.VarDimName=VarDimName;
451end
452SubField.ListGlobalAttribute=['InputFile' SubField.ListGlobalAttribute];
453SubField.InputFile=get(handles.inputfile,'String');
454SubField.VarAttribute=SubVarAttribute;
455
456%permute indices if coord_y is not the first matrix index: scalar case
457if test_scalar
458    VarNameA=Field.ListVarName{VarIndexA};
459    DimCellA=Field.VarDimName{VarIndexA};   
460    eval(['npxy=size(SubField.' VarNameA ')'])
461    SingleCellA={};
462    if numel(npxy) < numel(DimCellA)
463        SingleCellA=DimCellA(1:end-numel(npxy));
464        DimCellA=DimCellA(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
465    end
466    ind_single=find(npxy==1);
467    if ~isempty(ind_single)
468    %SingleCellA=[SingleCellA DimCellA(ind_single)];TO CHECK
469    end
470    ind_select=find(npxy~=1);%look for non singleton dimensions
471    DimCellA=DimCellA(ind_select);
472    npxy=npxy(ind_select);
473    dimA=[];
474    if test_zdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
475        for icoord=1:numel(SingleCellA)% look for coincidence of dimension with one of the dimensions of the scalar
476            if strcmp(dimname_z,SingleCellA{icoord})% a singleton dimension
477                errormsg=['the singleton dimension ' dimname_z ' has been selected for z'];
478                return
479            end
480        end
481        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
482             if strcmp(dimname_z,DimCellA{icoord})% a dimension variable
483                 dimA=[dimA icoord];
484                 break
485             end
486        end
487    end
488    if test_ydimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
489        for icoord=1:numel(SingleCellA)% look for coincidence of dimension with one of the dimensions of the scalar
490            if strcmp(dimname_y,SingleCellA{icoord})% a singleton dimension
491                errormsg=['the singleton dimension ' dimname_y ' has been selected for ordiante'];
492                return
493            end
494        end
495        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
496             if strcmp(dimname_y,DimCellA{icoord})% a dimension variable
497                 dimA=[dimA icoord];
498                 break
499             end
500        end
501    end
502    if test_xdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
503        for icoord=1:numel(SingleCellA)% look for coincidence of dimension with one of the dimensions of the scalar
504            if strcmp(dimname_x,SingleCellA{icoord})% a singleton dimension
505                errormsg=['the singleton dimension ' dimname_x ' has been selected for abscissa'];
506                return
507            end
508        end
509        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
510             if strcmp(dimname_x,DimCellA{icoord})% a dimension variable
511                 dimA=[dimA icoord];
512                 break
513             end
514        end
515    end
516    dimextra=(1:numel(DimCellA));
517    dimextra(dimA)=[]; %list of unselected dimension indices
518    DimCellA=DimCellA([dimA dimextra]);
519   % eval(['SubField.' VarNameA '=permute(squeeze(SubField.' VarNameA '),[dimA dimextra]);'])TO CHECK
520    SubField.VarDimName{VarSubIndexA}=DimCellA; 
521    %add default coord_x and/or coord_y if empty
522    if empty_coord_x || empty_coord_y
523        VarName=Field.ListVarName{field_var_index};
524        DimCell=Field.VarDimName{field_var_index};   
525        eval(['npxy=size(SubField.' VarName ')'])
526        if numel(npxy) < numel(DimCell)
527            DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
528        end
529        ind_select=find(npxy~=1) ;%look for non singleton dimensions
530        DimCell=DimCell(ind_select);
531        npxy=npxy(ind_select);
532        testold=0;
533    %old convention; use of coord_1 and Coord_2
534        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=field_var_index
535            if isfield(Field.VarAttribute{field_var_index},'Coord_2')&& isfield(Field.VarAttribute{field_var_index},'Coord_1')
536                 Coord_2=Field.VarAttribute{field_var_index}.Coord_2;
537                 Coord_1=Field.VarAttribute{field_var_index}.Coord_1;
538                testold=1;
539            end
540        end
541        if empty_coord_x       
542                coord_x_name=DimCell{2};
543                SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
544                SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
545                if testold
546                    eval(['SubField.' coord_x_name '=linspace(Coord_2(1),Coord_2(end),npxy(2));'])
547                else
548                    eval(['SubField.' coord_x_name '=[0.5 npxy(2)-0.5];'])
549                end
550           
551            if ~testold
552                coord_x_attr.units='index';
553            else
554                coord_x_attr.units='cm';
555            end
556            SubField.VarAttribute=[{coord_x_attr} SubField.VarAttribute]; 
557        end
558        if empty_coord_y
559            coord_y_name=DimCell{1};
560            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
561            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
562            if testold
563                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
564            else
565                eval(['SubField.' coord_y_name '=[npxy(1)-0.5 0.5];'])
566            end
567            if ~testold
568                coord_y_attr.units='index';
569            else
570                coord_y_attr.units='cm';
571            end
572            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
573        end
574    end
575end
576
577%permute indices if coord_y is not the first matrix index: vector case
578if test_vector
579    VarNameU=Field.ListVarName{VarIndexU}; % name of u component variable
580    DimCellU=Field.VarDimName{VarIndexU}; % list of dimensions for u component 
581    eval(['npxy=size(SubField.' VarNameU ')']) % npxy= dimension values for the u component
582    SingleCellU={};
583    if numel(npxy) < numel(DimCellU)
584        SingleCellU=DimCellU(1:end-numel(npxy));
585        DimCellU=DimCellU(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
586    end
587    ind_single=find(npxy==1);%indices of singleton dimensions
588    if ind_single<=numel(DimCellU)
589        SingleCellU=[SingleCellU DimCellU(ind_single)];
590    end
591    ind_select=find(npxy~=1);%look for non singleton dimensions
592    if numel(DimCellU)>=ind_select
593        DimCellU=DimCellU(ind_select);
594    end
595    npxy=npxy(ind_select);
596    dimU=[];
597    if test_zdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
598        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
599            if strcmp(dimname_vec_z,SingleCellU{icoord})% a singleton dimension
600                errormsg=['the singleton dimension ' dimname_vec_z ' has been selected for z'];
601                return
602            end
603        end
604        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
605             if strcmp(dimname_vec_z,DimCellU{icoord})% a dimension variable
606                 dimU=[dimU icoord];
607                 break
608             end
609        end
610    end
611    if test_ydimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
612        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
613            if strcmp(dimname_vec_y,SingleCellU{icoord})% a singleton dimension
614                errormsg=['the singleton dimension ' dimname_vec_y ' has been selected for ordinate'];
615                return
616            end
617        end
618        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
619             if strcmp(dimname_vec_y,DimCellU{icoord})% a dimension variable
620                 dimU=[dimU icoord];
621                 break
622             end
623        end
624    end
625    if test_xdimvar
626        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
627            if strcmp(dimname_x,SingleCellU{icoord})% a singleton dimension
628                errormsg=['the singleton dimension ' dimname_vec_x ' has been selected for abscissa'];
629                return
630            end
631        end
632        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
633             if strcmp(dimname_vec_x,DimCellU{icoord})% a dimension variable
634                 dimU=[dimU icoord];
635                 break
636             end
637        end
638    end
639    if numel(DimCellU)>1
640        dimextra=(1:numel(DimCellU));
641        dimextra(dimU)=[]; %list of unselected dimension indices
642        DimCellU=DimCellU([dimU dimextra]);
643        eval(['SubField.' VarNameU '=permute(squeeze(SubField.' VarNameU '),[dimU dimextra]);'])
644        eval(['SubField.' VarNameV '=permute(squeeze(SubField.' VarNameV '),[dimU dimextra]);'])
645        SubField.VarDimName{VarSubIndexU}=DimCellU;
646        SubField.VarDimName{VarSubIndexV}=DimCellU;
647    end
648    %add default coord_x and/or coord_y if empty
649    if empty_coord_vec_x || empty_coord_vec_y
650        VarName=Field.ListVarName{field_var_index};
651        DimCell=Field.VarDimName{field_var_index};   
652        eval(['npxy=size(SubField.' VarName ')'])
653        if numel(npxy) < numel(DimCell)
654            DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
655        end
656        ind_select=find(npxy~=1) ;%look for non singleton dimensions
657        DimCell=DimCell(ind_select);
658        npxy=npxy(ind_select);
659        testold=0;
660    %old convention; use of coord_1 and Coord_2
661        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=field_var_index
662            if isfield(Field.VarAttribute{field_var_index},'Coord_2')&& isfield(Field.VarAttribute{field_var_index},'Coord_1')
663                 Coord_2=Field.VarAttribute{field_var_index}.Coord_2;
664                 Coord_1=Field.VarAttribute{field_var_index}.Coord_1;
665                testold=1;
666            end
667        end
668        if empty_coord_vec_x
669            if numel(DimCell)<2 
670                errormsg='undefined x coordinate';
671                return
672            end
673            coord_x_name=DimCell{2};
674            SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
675            SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
676            if testold
677                eval(['SubField.' coord_x_name '=linspace(Coord_2(1),Coord_2(end),npxy(2));'])
678            else
679                eval(['SubField.' coord_x_name '=[0.5 npxy(2)-0.5];'])
680            end
681           
682            if ~testold
683                coord_x_attr.units='index';
684            else
685                coord_x_attr.units='cm';
686            end
687            SubField.VarAttribute=[{coord_x_attr} SubField.VarAttribute]; 
688        end
689        if empty_coord_vec_y
690            coord_y_name=DimCell{1};
691            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
692            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
693            if testold
694                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
695            else
696                eval(['SubField.' coord_y_name '=[npxy(1)-0.5 0.5];'])
697            end
698            if ~testold
699                coord_y_attr.units='index';
700            else
701                coord_y_attr.units='cm';
702            end
703            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
704        end
705    end
706end
707if test_1Dplot
708    for ilist=1:numel(VarIndex_y)
709        VarName=Field.ListVarName{VarIndex_y(ilist)};
710        eval(['npxy=size(SubField.' VarName ');'])
711        ind_select=find(npxy~=1);
712        SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}(ind_select);
713        eval(['SubField.' VarName '=squeeze(SubField.' VarName ');'])%remove singleton dimensions
714        if testpermute(ilist)
715            eval(['SubField.' VarName '=permute(SubField.' VarName ',[2 1]);'])
716            SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}([2 1]);
717        end
718    end
719    if empty_coord_x
720        SubField.ListVarName=[{[coord_x_name '_index']} SubField.ListVarName];
721        SubField.VarDimName=[{coord_x_name } SubField.VarDimName];
722        VarName=Field.ListVarName{VarIndex_y(1)};
723        DimCell=Field.VarDimName{VarIndex_y(1)};   
724        eval(['npxy=size(SubField.' VarName ')'])
725        if numel(npxy) < numel(DimCell)
726%             DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
727        end
728%         ind_select=find(npxy~=1) ;%look for non singleton dimensions
729%         DimCell=DimCell(ind_select);
730%         npxy=npxy(ind_select);
731        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=VarIndex_y(1) ...
732                             && isfield(Field.VarAttribute{VarIndex_y(1)},'Coord_1')
733%              Coord_1=Field.VarAttribute{VarIndex_y(1)}.Coord_1;%old convention; use of coord_1
734             eval(['SubField.' coord_x_name '_index=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
735        else
736            eval(['SubField.' coord_x_name '_index=linspace(0.5,npxy(1)-0.5,npxy(1));'])
737        end
738        struct.Role='coord_x';
739        SubField.VarAttribute=[{struct} SubField.VarAttribute];
740    end
741end
742
743%-------------------------------------------------
744% give index numbers of the strings str in the list ListvarName
745function VarIndex_y=name2index(cell_str,ListVarName)
746VarIndex_y=[];
747if ischar(cell_str)
748    for ivar=1:length(ListVarName)
749        varlist=ListVarName{ivar};
750        if isequal(varlist,cell_str)
751            VarIndex_y= ivar;
752            break
753        end
754    end
755elseif iscell(cell_str)
756    for isel=1:length(cell_str)
757        varsel=cell_str{isel};
758        for ivar=1:length(ListVarName)
759            varlist=ListVarName{ivar};
760            if isequal(varlist,varsel)
761                VarIndex_y=[VarIndex_y ivar];
762            end
763        end
764    end
765end
Note: See TracBrowser for help on using the repository browser.