source: trunk/src/read_get_field.m @ 32

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

-subfunction find_file_indices put back in series (it had been extracted by mistake to check_files
-aver_stat: name of output file rationalized
-read_get_field : pb with vector fields, yet to solve

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