source: trunk/src/read_get_field.m @ 534

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

datatree_browse introduced (still work to do)
get_field revised to introduce new transform fcts, like FFT

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