source: trunk/src/read_get_field.m @ 35

Last change on this file since 35 was 34, checked in by sommeria, 15 years ago

get_field: list of actions put as functions in subdir get_field (like done for series), store and retrieve the latest opened files.
series: bug repair for action functions
read_get_field: bug repair for the case of vectors with unstructured coordinates

File size: 31.1 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 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    SingleCellA=[SingleCellA DimCellA(ind_single)];
468    ind_select=find(npxy~=1);%look for non singleton dimensions
469    DimCellA=DimCellA(ind_select);
470    npxy=npxy(ind_select);
471    dimA=[];
472    if test_zdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
473        for icoord=1:numel(SingleCellA)% look for coincidence of dimension with one of the dimensions of the scalar
474            if strcmp(dimname_z,SingleCellA{icoord})% a singleton dimension
475                errormsg=['the singleton dimension ' dimname_z ' has been selected for z'];
476                return
477            end
478        end
479        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
480             if strcmp(dimname_z,DimCellA{icoord})% a dimension variable
481                 dimA=[dimA icoord];
482                 break
483             end
484        end
485    end
486    if test_ydimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
487        for icoord=1:numel(SingleCellA)% look for coincidence of dimension with one of the dimensions of the scalar
488            if strcmp(dimname_y,SingleCellA{icoord})% a singleton dimension
489                errormsg=['the singleton dimension ' dimname_y ' has been selected for ordiante'];
490                return
491            end
492        end
493        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
494             if strcmp(dimname_y,DimCellA{icoord})% a dimension variable
495                 dimA=[dimA icoord];
496                 break
497             end
498        end
499    end
500    if test_xdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
501        for icoord=1:numel(SingleCellA)% look for coincidence of dimension with one of the dimensions of the scalar
502            if strcmp(dimname_x,SingleCellA{icoord})% a singleton dimension
503                errormsg=['the singleton dimension ' dimname_x ' has been selected for abscissa'];
504                return
505            end
506        end
507        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
508             if strcmp(dimname_x,DimCellA{icoord})% a dimension variable
509                 dimA=[dimA icoord];
510                 break
511             end
512        end
513    end
514    dimextra=(1:numel(DimCellA));
515    dimextra(dimA)=[]; %list of unselected dimension indices
516    DimCellA=DimCellA([dimA dimextra]);
517    eval(['SubField.' VarNameA '=permute(squeeze(SubField.' VarNameA '),[dimA dimextra]);'])
518    SubField.VarDimName{VarSubIndexA}=DimCellA; 
519    %add default coord_x and/or coord_y if empty
520    if empty_coord_x || empty_coord_y
521        VarName=Field.ListVarName{field_var_index};
522        DimCell=Field.VarDimName{field_var_index};   
523        eval(['npxy=size(SubField.' VarName ')'])
524        if numel(npxy) < numel(DimCell)
525            DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
526        end
527        ind_select=find(npxy~=1) ;%look for non singleton dimensions
528        DimCell=DimCell(ind_select);
529        npxy=npxy(ind_select);
530        testold=0;
531    %old convention; use of coord_1 and Coord_2
532        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=field_var_index
533            if isfield(Field.VarAttribute{field_var_index},'Coord_2')&& isfield(Field.VarAttribute{field_var_index},'Coord_1')
534%                 Coord_2=Field.VarAttribute{field_var_index}.Coord_2;
535%                 Coord_1=Field.VarAttribute{field_var_index}.Coord_1;
536                testold=1;
537            end
538        end
539        if empty_coord_x       
540                coord_x_name=DimCell{2};
541                SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
542                SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
543                if testold
544                    eval(['SubField.' coord_x_name '=linspace(Coord_2(1),Coord_2(end),npxy(2));'])
545                else
546                    eval(['SubField.' coord_x_name '=[0.5 npxy(2)-0.5];'])
547                end
548           
549            if ~testold
550                coord_x_attr.units='index';
551            else
552                coord_x_attr.units='cm';
553            end
554            SubField.VarAttribute=[{coord_x_attr} SubField.VarAttribute]; 
555        end
556        if empty_coord_y
557            coord_y_name=DimCell{1};
558            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
559            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
560            if testold
561                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
562            else
563                eval(['SubField.' coord_y_name '=[npxy(1)-0.5 0.5];'])
564            end
565            if ~testold
566                coord_y_attr.units='index';
567            else
568                coord_y_attr.units='cm';
569            end
570            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
571        end
572    end
573end
574
575%permute indices if coord_y is not the first matrix index: vector case
576if test_vector
577    VarNameU=Field.ListVarName{VarIndexU}; % name of u component variable
578    DimCellU=Field.VarDimName{VarIndexU}; % list of dimensions for u component 
579    eval(['npxy=size(SubField.' VarNameU ')']) % npxy= dimension values for the u component
580    SingleCellU={};
581    if numel(npxy) < numel(DimCellU)
582        SingleCellU=DimCellU(1:end-numel(npxy));
583        DimCellU=DimCellU(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
584    end
585    ind_single=find(npxy==1);%indices of singleton dimensions
586    if ind_single<=numel(DimCellU)
587        SingleCellU=[SingleCellU DimCellU(ind_single)];
588    end
589    ind_select=find(npxy~=1);%look for non singleton dimensions
590    DimCellU=DimCellU(ind_select);
591    npxy=npxy(ind_select);
592    dimU=[];
593    if test_zdimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
594        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
595            if strcmp(dimname_vec_z,SingleCellU{icoord})% a singleton dimension
596                errormsg=['the singleton dimension ' dimname_vec_z ' has been selected for z'];
597                return
598            end
599        end
600        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
601             if strcmp(dimname_vec_z,DimCellU{icoord})% a dimension variable
602                 dimU=[dimU icoord];
603                 break
604             end
605        end
606    end
607    if test_ydimvar%dim_x && dim_y && ~isempty(VarSubIndexA)
608        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
609            if strcmp(dimname_vec_y,SingleCellU{icoord})% a singleton dimension
610                errormsg=['the singleton dimension ' dimname_vec_y ' has been selected for ordinate'];
611                return
612            end
613        end
614        for icoord=1:numel(DimCellU)% look for coincidence of dimension with one of the dimensions of the scalar
615             if strcmp(dimname_vec_y,DimCellU{icoord})% a dimension variable
616                 dimU=[dimU icoord];
617                 break
618             end
619        end
620    end
621    if test_xdimvar
622        for icoord=1:numel(SingleCellU)% look for coincidence of dimension with one of the dimensions of the scalar
623            if strcmp(dimname_x,SingleCellU{icoord})% a singleton dimension
624                errormsg=['the singleton dimension ' dimname_vec_x ' has been selected for abscissa'];
625                return
626            end
627        end
628        for icoord=1:numel(DimCellA)% look for coincidence of dimension with one of the dimensions of the scalar
629             if strcmp(dimname_vec_x,DimCellU{icoord})% a dimension variable
630                 dimU=[dimU icoord];
631                 break
632             end
633        end
634    end
635    if numel(DimCellU)>1
636        dimextra=(1:numel(DimCellU));
637        dimextra(dimU)=[]; %list of unselected dimension indices
638        DimCellU=DimCellU([dimU dimextra]);
639        eval(['SubField.' VarNameU '=permute(squeeze(SubField.' VarNameU '),[dimU dimextra]);'])
640        eval(['SubField.' VarNameV '=permute(squeeze(SubField.' VarNameV '),[dimU dimextra]);'])
641        SubField.VarDimName{VarSubIndexU}=DimCellU;
642        SubField.VarDimName{VarSubIndexV}=DimCellU;
643    end
644    %add default coord_x and/or coord_y if empty
645    if empty_coord_vec_x || empty_coord_vec_y
646        VarName=Field.ListVarName{field_var_index};
647        DimCell=Field.VarDimName{field_var_index};   
648        eval(['npxy=size(SubField.' VarName ')'])
649        if numel(npxy) < numel(DimCell)
650            DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
651        end
652        ind_select=find(npxy~=1) ;%look for non singleton dimensions
653        DimCell=DimCell(ind_select);
654        npxy=npxy(ind_select);
655        testold=0;
656    %old convention; use of coord_1 and Coord_2
657        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=field_var_index
658            if isfield(Field.VarAttribute{field_var_index},'Coord_2')&& isfield(Field.VarAttribute{field_var_index},'Coord_1')
659%                 Coord_2=Field.VarAttribute{field_var_index}.Coord_2;
660%                 Coord_1=Field.VarAttribute{field_var_index}.Coord_1;
661                testold=1;
662            end
663        end
664        if empty_coord_vec_x       
665                coord_x_name=DimCell{2};
666                SubField.ListVarName=[{coord_x_name} SubField.ListVarName];
667                SubField.VarDimName=[{coord_x_name} SubField.VarDimName]; 
668                if testold
669                    eval(['SubField.' coord_x_name '=linspace(Coord_2(1),Coord_2(end),npxy(2));'])
670                else
671                    eval(['SubField.' coord_x_name '=[0.5 npxy(2)-0.5];'])
672                end
673           
674            if ~testold
675                coord_x_attr.units='index';
676            else
677                coord_x_attr.units='cm';
678            end
679            SubField.VarAttribute=[{coord_x_attr} SubField.VarAttribute]; 
680        end
681        if empty_coord_vec_y
682            coord_y_name=DimCell{1};
683            SubField.ListVarName=[{coord_y_name} SubField.ListVarName];
684            SubField.VarDimName=[{coord_y_name} SubField.VarDimName];
685            if testold
686                eval(['SubField.' coord_y_name '=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
687            else
688                eval(['SubField.' coord_y_name '=[npxy(1)-0.5 0.5];'])
689            end
690            if ~testold
691                coord_y_attr.units='index';
692            else
693                coord_y_attr.units='cm';
694            end
695            SubField.VarAttribute=[{coord_y_attr} SubField.VarAttribute];       
696        end
697    end
698end
699if test_1Dplot
700    for ilist=1:numel(VarIndex_y)
701        VarName=Field.ListVarName{VarIndex_y(ilist)};
702        eval(['npxy=size(SubField.' VarName ');'])
703        ind_select=find(npxy~=1);
704        SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}(ind_select);
705        eval(['SubField.' VarName '=squeeze(SubField.' VarName ');'])%remove singleton dimensions
706        if testpermute(ilist)
707            eval(['SubField.' VarName '=permute(SubField.' VarName ',[2 1]);'])
708            subvarindex(ilist)
709            SubField.VarDimName{subvarindex(ilist)}
710            SubField.VarDimName{subvarindex(ilist)}=SubField.VarDimName{subvarindex(ilist)}([2 1]);
711        end
712    end
713    if empty_coord_x
714        SubField.ListVarName=[{[coord_x_name '_index']} SubField.ListVarName];
715        SubField.VarDimName=[{coord_x_name } SubField.VarDimName];
716        VarName=Field.ListVarName{VarIndex_y(1)};
717        DimCell=Field.VarDimName{VarIndex_y(1)};   
718        eval(['npxy=size(SubField.' VarName ')'])
719        if numel(npxy) < numel(DimCell)
720%             DimCell=DimCell(end-numel(npxy)+1:end); %suppress the first singletons) dimensions
721        end
722%         ind_select=find(npxy~=1) ;%look for non singleton dimensions
723%         DimCell=DimCell(ind_select);
724%         npxy=npxy(ind_select);
725        if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=VarIndex_y(1) ...
726                             && isfield(Field.VarAttribute{VarIndex_y(1)},'Coord_1')
727%              Coord_1=Field.VarAttribute{VarIndex_y(1)}.Coord_1;%old convention; use of coord_1
728             eval(['SubField.' coord_x_name '_index=linspace(Coord_1(1),Coord_1(end),npxy(1));'])
729        else
730            eval(['SubField.' coord_x_name '_index=linspace(0.5,npxy(1)-0.5,npxy(1));'])
731        end
732        struct.Role='coord_x';
733        SubField.VarAttribute=[{struct} SubField.VarAttribute];
734    end
735end
736
737%-------------------------------------------------
738% give index numbers of the strings str in the list ListvarName
739function VarIndex_y=name2index(cell_str,ListVarName)
740VarIndex_y=[];
741if ischar(cell_str)
742    for ivar=1:length(ListVarName)
743        varlist=ListVarName{ivar};
744        if isequal(varlist,cell_str)
745            VarIndex_y= ivar;
746            break
747        end
748    end
749elseif iscell(cell_str)
750    for isel=1:length(cell_str)
751        varsel=cell_str{isel};
752        for ivar=1:length(ListVarName)
753            varlist=ListVarName{ivar};
754            if isequal(varlist,varsel)
755                VarIndex_y=[VarIndex_y ivar];
756            end
757        end
758    end
759end
Note: See TracBrowser for help on using the repository browser.