source: trunk/src/read_get_field.m @ 19

Last change on this file since 19 was 19, checked in by gostiaux, 14 years ago

the private files have been moved down to the root folder

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