source: trunk/src/read_get_field.m @ 402

Last change on this file since 402 was 402, checked in by sommeria, 12 years ago

bugs corrected and improved procedure for object projection

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