source: trunk/src/get_field.m @ 861

Last change on this file since 861 was 811, checked in by g7moreau, 10 years ago
  • Update header OUPUT -> OUTPUT and so on...
File size: 42.5 KB
Line 
1%'get_field': display variables and attributes from a Netcdf file, and OK selected fields
2%------------------------------------------------------------------------
3% GetFieldData=get_field(FileName,ParamIn)
4% associated with the GUI get_field.fig
5%
6% OUTPUT:
7% GetFieldData: structure containing the information on the selected
8%      fields, obtained by applying the fct red_GUI to the GUI get_field
9%   .FieldOption='vectors': variables are used for vector plot
10%                  'scalar': variables are used for scalar plot,
11%                  '1Dplot': variables are used for usual x-y plot,
12%                  'civdata...': go back to automatic reading of civ data
13%   .PanelVectors: sub-structure variables used as vector components
14%   .PanelScalar:
15% INPUT:
16% FileName: name (including path) of the netcdf file to open
17
18%=======================================================================
19% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
20%   http://www.legi.grenoble-inp.fr
21%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
22%
23%     This file is part of the toolbox UVMAT.
24%
25%     UVMAT is free software; you can redistribute it and/or modify
26%     it under the terms of the GNU General Public License as published
27%     by the Free Software Foundation; either version 2 of the license,
28%     or (at your option) any later version.
29%
30%     UVMAT is distributed in the hope that it will be useful,
31%     but WITHOUT ANY WARRANTY; without even the implied warranty of
32%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33%     GNU General Public License (see LICENSE.txt) for more details.
34%=======================================================================
35
36function varargout = get_field(varargin)
37
38% Last Modified by GUIDE v2.5 24-Apr-2014 22:45:34
39
40% Begin initialization code - DO NOT EDIT
41gui_Singleton = 1;
42gui_State = struct('gui_Name',       mfilename, ...
43                   'gui_Singleton',  gui_Singleton, ...
44                   'gui_OpeningFcn', @get_field_OpeningFcn, ...
45                   'gui_OutputFcn',  @get_field_OutputFcn, ...
46                   'gui_LayoutFcn',  [] , ...
47                   'gui_Callback',   []);
48if nargin && ischar(varargin{1})&& ~isempty(regexp(varargin{1},'_Callback','once'))
49    gui_State.gui_Callback = str2func(varargin{1});
50end
51
52if nargout
53    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
54else
55    gui_mainfcn(gui_State, varargin{:});
56end
57% End initialization code - DO NOT EDIT
58     
59%------------------------------------------------------------------------
60% --- Executes just before get_field is made visible.
61%------------------------------------------------------------------------
62function get_field_OpeningFcn(hObject, eventdata, handles,filename,ParamIn)
63
64%% GUI settings
65handles.output = 'Cancel';
66guidata(hObject, handles);
67set(hObject,'WindowButtonDownFcn',{'mouse_down'}) % allows mouse action with right button (zoom for uicontrol display)
68
69%% enter input data
70if ischar(filename) % input file name
71    set(handles.inputfile,'String',filename)% fill the input file name
72    [Field,tild,tild,errormsg]=nc2struct(filename,[]);% reads the  field structure, without the variables
73else
74    msgbox_uvmat('ERROR','get_field requires a file name as input')% display error message for input file reading
75    return
76end
77if ~isempty(errormsg)
78    msgbox_uvmat('ERROR',['get_field/nc2struct/' errormsg])% display error message for input file reading
79    return
80end
81if ~isfield(Field,'ListVarName')
82    msgbox_uvmat('ERROR',['no variable found in ' filename])% display error message for input file reading
83    return
84end
85if ~exist('ParamIn','var')
86    ParamIn=[];
87end
88
89%% look at singletons and variables with a single dimension
90Field.Display=Field;
91Field.Check0D=zeros(size(Field.ListVarName));% =1 for arrays with a single value
92NbVar=numel(Field.VarDimName);%nbre of variables in the input data
93for ilist=1:NbVar
94    if ischar(Field.VarDimName{ilist})
95        Field.VarDimName{ilist}={Field.VarDimName{ilist}}; %transform string into cell
96    end
97    NbDim=numel(Field.VarDimName{ilist});
98    check_singleton=false(1,NbDim);%  check singleton, false by default
99    for idim=1:NbDim
100        dim_index=strcmp(Field.VarDimName{ilist}{idim},Field.ListDimName);%index in the list of dimensions
101        check_singleton(idim)=isequal(Field.DimValue(dim_index),1);%check_singleton=1 for singleton
102    end
103    Field.Check0D(ilist)=(isequal(check_singleton,ones(1,NbDim)))||(~isequal(Field.VarType(ilist),4)&&~isequal(Field.VarType(ilist),5)&&~isequal(Field.VarType(ilist),6));% =1 if the variable reduces to a single value
104    if ~Field.Check0D(ilist)
105    Field.Display.VarDimName{ilist}=Field.VarDimName{ilist}(~check_singleton);% eliminate singletons in the list of variable dimensions
106    end
107end
108if ~isfield(Field,'VarAttribute')
109    Field.VarAttribute={};
110end
111if numel(Field.VarAttribute)<NbVar% complement VarAttribute by blanjs if neded
112    Field.VarAttribute(numel(Field.VarAttribute)+1:NbVar)=cell(1,NbVar-numel(Field.VarAttribute));
113end
114% Field.Display = list of variables and corresponding properties obtained after removal of variables with a single value and singleton dimensions
115Field.Display.ListVarName=Field.ListVarName(~Field.Check0D); %list of variables available for plots, after eliminating variables with a single value
116Field.Display.VarAttribute=Field.VarAttribute(~Field.Check0D);
117Field.Display.VarDimName=Field.Display.VarDimName(~Field.Check0D);
118Field.Display.ListDimName=Field.ListDimName(Field.DimValue~=1);% list of non singleton dimension names
119Field.Display.DimValue=Field.DimValue(Field.DimValue~=1);% corresponding list of non singleton dimension values
120
121
122%% analyse the input field cells
123[CellInfo,NbDim,errormsg]=find_field_cells(Field.Display);
124if ~isempty(errormsg)
125    msgbox_uvmat('ERROR',['get_field / Field_input / find_field_cells: ' errormsg])
126    return
127end
128[Field.MaxDim,imax]=max(NbDim);
129
130%% set time mode
131ListSwitchVarIndexTime={'file index'};% default setting: the time is the file index
132% look at global attributes with numerical values
133check_numvalue=false(1,numel(Field.ListGlobalAttribute));
134for ilist=1:numel(Field.ListGlobalAttribute)
135    Value=Field.(Field.ListGlobalAttribute{ilist});
136    check_numvalue(ilist)=isnumeric(Value);
137end
138Field.Display.ListGlobalAttribute=Field.ListGlobalAttribute(check_numvalue);% select the attributes with float numerical value
139if ~isempty(Field.Display.ListGlobalAttribute)
140    ListSwitchVarIndexTime=[ListSwitchVarIndexTime; {'attribute'}];% the time can be chosen as a global attribute
141end
142if Field.MaxDim>=2
143    ListSwitchVarIndexTime=[ListSwitchVarIndexTime;{'variable'};{'matrix index'}];% the time can be chosen as a dim index
144end
145
146%% select the Time attribute from input
147if isfield(ParamIn,'TimeAttrName')
148    time_index=find(strcmp(ParamIn.TimeAttrName,Field.Display.ListGlobalAttribute),1);
149else
150    time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);% look for global attribute containing name 'Time'
151end
152if ~isempty(time_index)
153    set(handles.SwitchVarIndexTime,'Value',2);
154    set(handles.TimeName,'UserData',time_index)
155else
156    set(handles.SwitchVarIndexTime,'Value',1);
157end
158set(handles.SwitchVarIndexTime,'String',ListSwitchVarIndexTime)
159set(handles.get_field,'UserData',Field);% record the finput field structure
160SwitchVarIndexTime_Callback([], [], handles)
161
162%% set vector menu (priority) if detected or scalar menu for space dim >=2, or usual (x,y) plot for 1D fields
163set(handles.vector_x,'String',Field.Display.ListVarName)% fill the menu of x vector components
164set(handles.vector_y,'String',Field.Display.ListVarName)% fill the menu of y vector components
165set(handles.vector_z,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
166set(handles.vec_color,'String',[{''} Field.Display.ListVarName])% fill the menu of y vector components
167set(handles.scalar,'Value',1)% fill the menu of y vector components
168set(handles.scalar,'String',Field.Display.ListVarName)% fill the menu for scalar
169set(handles.ordinate,'Value',1)% fill the menu of y vector components
170set(handles.ordinate,'String',Field.Display.ListVarName)% fill the menu of y coordinate for 1D plots
171checkseries=0;
172if isfield(ParamIn,'SeriesInput') && ParamIn.SeriesInput% case of call by series
173    set(handles.FieldOption,'value',1)
174    if isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
175    set(handles.FieldOption,'String',{'scalar';'vectors';'civdata...'})
176    else
177       set(handles.FieldOption,'String',{'scalar';'vectors'})
178    end
179    checkseries=1;
180    set(handles.scalar,'Max',2)
181elseif isfield(Field,'Conventions')&& strcmp(Field.Conventions,'uvmat/civdata')
182    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors';'civdata...'})% provides the possibility to come back to civdata
183    set(handles.scalar,'Max',1)
184else
185    set(handles.FieldOption,'String',{'1D plot';'scalar';'vectors'})
186    set(handles.scalar,'Max',1)
187end
188if Field.MaxDim>=2 && ~checkseries% case of 2D (or 3D) fields
189    check_vec_input=0;
190    if isfield(ParamIn,'vector_x')&& isfield(ParamIn,'vector_y')
191        ichoice_x=find(strcmp(ParamIn.vector_x,Field.Display.ListVarName),1);
192        ichoice_y=find(strcmp(ParamIn.vector_y,Field.Display.ListVarName),1);
193        if ~isempty(ichoice_x)&&~isempty(ichoice_y)
194            set(handles.vector_x,'UserData',ichoice_x)
195            set(handles.vector_y,'UserData',ichoice_y)
196            check_vec_input=1;
197        end
198    end
199    if ~check_vec_input && isfield(CellInfo{imax},'VarIndex_vector_x') &&  isfield(CellInfo{imax},'VarIndex_vector_y')
200        set(handles.vector_x,'UserData',CellInfo{imax}.VarIndex_vector_x(1))
201        set(handles.vector_y,'UserData',CellInfo{imax}.VarIndex_vector_y(1))
202        check_vec_input=1;
203    end
204    if check_vec_input
205        set(handles.FieldOption,'Value',3)% set vector selection option
206    else
207        set(handles.FieldOption,'Value',2)% set scalar selection option
208    end
209else % case of 1D fields
210    set(handles.FieldOption,'Value',1)
211end
212
213%% fill the general list of dimensions, variables, attributes
214if isfield(Field,'ListDimName')&&~isempty(Field.ListDimName)
215    Tabcell(:,1)=Field.ListDimName;
216    for iline=1:length(Field.ListDimName)
217        Tabcell{iline,2}=num2str(Field.DimValue(iline));
218    end
219    Tabchar=cell2tab(Tabcell,' = ');
220    set(handles.dimensions,'String',Tabchar)
221end
222
223%% fill menus for coordinates and time
224FieldOption_Callback(handles.variables,[], handles)% list the global attributes
225
226%% Make choices of coordinates from input
227if isfield(CellInfo{imax},'CoordIndex')
228    CoordIndex=CellInfo{imax}.CoordIndex;
229    if numel(CoordIndex)==2
230        YName=Field.ListVarName{CoordIndex(1)};
231        XName=Field.ListVarName{CoordIndex(2)};
232        ListCoord=get(handles.Coord_x,'String');
233        XIndex=find(strcmp(XName,ListCoord));
234        if ~isempty(XIndex)
235            set(handles.Coord_x,'Value',XIndex)
236        end
237        YIndex=find(strcmp(YName,ListCoord));
238        if ~isempty(YIndex)
239            set(handles.Coord_y,'Value',YIndex)
240        end
241    end
242end
243
244%% put the GUI on the lower right of the sceen
245set(hObject,'Unit','pixel')
246pos_view_field=get(hObject,'Position');
247set(0,'Unit','pixels')
248ScreenSize=get(0,'ScreenSize');
249pos_view_field(1)=ScreenSize(1)+ScreenSize(3)-pos_view_field(3);
250pos_view_field(2)=ScreenSize(2);
251set(hObject,'Position',pos_view_field)
252set(handles.get_field,'WindowStyle','modal')% Make the GUI modal
253if isfield(ParamIn,'Title')
254    set(hObject,'Name',ParamIn.Title)
255end
256
257%% set z coordinate menu if relevant
258if Field.MaxDim>=3
259    set(handles.Check3D,'Value',1)
260else
261    set(handles.Check3D,'Value',0)
262end
263Check3D_Callback(hObject, eventdata, handles)
264set(handles.variables,'Value',1)
265set(handles.variables,'String',[{'*'} Field.ListVarName])
266variables_Callback(handles.variables,[], handles)% list the global attributes
267drawnow
268uiwait(handles.get_field);
269
270%------------------------------------------------------------------------
271% --- Outputs from this function are returned to the command line.
272%------------------------------------------------------------------------
273function varargout = get_field_OutputFcn(hObject, eventdata, handles)
274
275varargout{1} = handles.output;
276delete(handles.get_field)
277
278%------------------------------------------------------------------------
279% --- Executes when user attempts to close get_field.
280%------------------------------------------------------------------------
281function get_field_CloseRequestFcn(hObject, eventdata, handles)
282
283if isequal(get(handles.get_field, 'waitstatus'), 'waiting')
284    % The GUI is still in UIWAIT, us UIRESUME
285    uiresume(handles.get_field);
286else
287    % The GUI is no longer waiting, just close it
288    delete(handles.get_field);
289end
290
291%------------------------------------------------------------------------
292% --- Executes on button press in OK.
293%------------------------------------------------------------------------
294function OK_Callback(hObject, eventdata, handles)
295
296handles.output=read_GUI(handles.get_field);
297guidata(hObject, handles);% Update handles structure
298uiresume(handles.get_field);
299drawnow
300
301% -----------------------------------------------------------------------
302% --- Activated by selection in the list of variables
303% ----------------------------------------------------------------------
304function variables_Callback(hObject, eventdata, handles)
305
306Tabchar={''};%default
307Tabcell=[];
308hselect_field=get(handles.variables,'parent');
309Field=get(handles.get_field,'UserData');
310index=get(handles.variables,'Value');%index in the list 'variables'
311
312%% list global TimeAttribute names and values if index=1 (blank TimeVariable display) is selected
313if isequal(index,1)
314    set(handles.attributes_txt,'String','global attributes')
315    if isfield(Field,'ListGlobalAttribute') && ~isempty(Field.ListGlobalAttribute)
316        for iline=1:length(Field.ListGlobalAttribute)
317            Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
318            if isfield(Field, Field.ListGlobalAttribute{iline})
319                val=Field.(Field.ListGlobalAttribute{iline});
320                if ischar(val);% attribute value is char string
321                    Tabcell{iline,2}=val;
322                elseif size(val,1)==1 %attribute value is a number or matlab vector
323                    Tabcell{iline,2}=num2str(val);
324                end
325            end
326        end
327        Tabchar=cell2tab(Tabcell,'=');
328    end
329    %% list Attribute names and values associated to the Variable # index-1
330else
331    list_var=get(handles.variables,'String');
332    if index>numel(list_var)
333        return
334    end
335    var_select=list_var{index};
336    set(handles.attributes_txt,'String', ['attributes of ' var_select])
337    if isfield(Field,'VarAttribute')&& length(Field.VarAttribute)>=index-1
338        %         nbline=0;
339        VarAttr=Field.VarAttribute{index-1};
340        if isstruct(VarAttr)
341            attr_list=fieldnames(VarAttr);
342            for iline=1:length(attr_list)
343                Tabcell{iline,1}=attr_list{iline};
344                val=VarAttr.(attr_list{iline}) ;
345                if ischar(val);
346                    Tabcell{iline,2}=val;
347                else
348                    Tabcell{iline,2}=num2str(val);
349                end
350            end
351        end
352    end
353end
354if ~isempty(Tabcell)
355    Tabchar=cell2tab(Tabcell,'=');
356    %     Tabchar=[{''};Tabchar];
357end
358set(handles.attributes,'Value',1);% select the first item
359set(handles.attributes,'String',Tabchar);
360
361%% update dimensions;
362if isfield(Field,'ListDimName')
363    Tabdim={};%default
364    if isequal(index,1)%list all dimensions
365        dim_indices=1:length(Field.ListDimName);
366        set(handles.dimensions_txt,'String', 'dimensions')
367    else
368        DimCell=Field.VarDimName{index-1};
369        if ischar(DimCell)
370            DimCell={DimCell};
371        end
372        dim_indices=[];
373        for idim=1:length(DimCell)
374            dim_index=strcmp(DimCell{idim},Field.ListDimName);%vector with size of Field.ListDimName, =0
375            dim_index=find(dim_index,1);
376            dim_indices=[dim_indices dim_index];
377        end
378        set(handles.dimensions_txt,'String', ['dimensions of ' var_select])
379    end
380    for iline=1:length(dim_indices)
381        Tabdim{iline,1}=Field.ListDimName{dim_indices(iline)};
382        Tabdim{iline,2}=num2str(Field.DimValue(dim_indices(iline)));
383    end
384    Tabchar=cell2tab(Tabdim,' = ');
385    Tabchar=[{''} ;Tabchar];
386    set(handles.dimensions,'Value',1)
387    set(handles.dimensions,'String',Tabchar)
388end
389
390%------------------------------------------------------------------------
391% --- Executes on selection change in FieldOption.
392%------------------------------------------------------------------------
393function FieldOption_Callback(hObject, eventdata, handles)
394
395Field=get(handles.get_field,'UserData');
396FieldList=get(handles.FieldOption,'String');
397FieldOption=FieldList{get(handles.FieldOption,'Value')};
398switch FieldOption
399   
400    case '1D plot'
401        set(handles.Coordinates,'Visible','on')
402        set(handles.PanelOrdinate,'Visible','on')
403        pos=get(handles.PanelOrdinate,'Position');
404        pos(1)=2;
405        pos_coord=get(handles.Coordinates,'Position');
406        pos(2)=pos_coord(2)-pos(4)-2;
407        set(handles.PanelOrdinate,'Position',pos)
408        set(handles.PanelScalar,'Visible','off')
409        set(handles.PanelVectors,'Visible','off')
410        set(handles.Coord_y,'Visible','off')
411        set(handles.Y_title,'Visible','off')
412        set(handles.Coord_z,'Visible','off')
413        set(handles.Z_title,'Visible','off')
414        ordinate_Callback(hObject, eventdata, handles)
415       
416    case {'scalar'}
417        set(handles.Coordinates,'Visible','on')
418        set(handles.PanelOrdinate,'Visible','off')
419        set(handles.PanelScalar,'Visible','on')
420        set(handles.PanelVectors,'Visible','off')
421        pos=get(handles.PanelScalar,'Position');
422        pos(1)=2;
423        pos_coord=get(handles.Coordinates,'Position');
424        pos(2)=pos_coord(2)-pos(4)-2;
425        set(handles.PanelScalar,'Position',pos)
426        set(handles.Coord_y,'Visible','on')
427        set(handles.Y_title,'Visible','on')
428        %default scalar selection
429        test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
430        for ilist=1:numel(Field.Display.VarDimName)
431            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
432                Role=Field.Display.VarAttribute{ilist}.Role;
433                if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
434                    test_coord(ilist)=1;
435                end
436            end
437            dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
438            if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
439                test_coord(ilist)=1;
440            end
441        end
442        scalar_index=find(~test_coord,1);%get the first variable not a coordinate
443        if isempty(scalar_index)
444            set(handles.scalar,'Value',1)
445        else
446            set(handles.scalar,'Value',scalar_index)
447        end       
448        scalar_Callback(hObject, eventdata, handles)
449             
450    case 'vectors'
451        set(handles.PanelVectors,'Visible','on')
452        set(handles.Coordinates,'Visible','on')
453        set(handles.PanelOrdinate,'Visible','off')
454        set(handles.PanelScalar,'Visible','off')
455        pos=get(handles.PanelVectors,'Position');
456        pos(1)=2;
457        pos_coord=get(handles.Coordinates,'Position');
458        pos(2)=pos_coord(2)-pos(4)-2;
459        set(handles.PanelVectors,'Position',pos)
460        set(handles.Coord_y,'Visible','on')
461        set(handles.Y_title,'Visible','on')
462        %default vector selection
463        vector_x_value=get(handles.vector_x,'UserData');
464        vector_y_value=get(handles.vector_y,'UserData');
465        if ~isempty(vector_x_value)&&~isempty(vector_y_value)
466            set(handles.vector_x,'Value',vector_x_value)
467            set(handles.vector_y,'Value',vector_y_value)
468        else
469            test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordinate
470            for ilist=1:numel(Field.Display.VarDimName)
471                if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ilist && isfield(Field.Display.VarAttribute{ilist},'Role')
472                    Role=Field.Display.VarAttribute{ilist}.Role;
473                    if strcmp(Role,'coord_x')||strcmp(Role,'coord_y')
474                        test_coord(ilist)=1;
475                    end
476                end
477                dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
478                if numel(dimnames)==1 && strcmp(dimnames{1},Field.Display.ListVarName{ilist})%dimension variable
479                    test_coord(ilist)=1;
480                end
481            end
482            vector_index=find(~test_coord,2);%get the two first variables not a coordinate
483            if isempty(vector_index)
484                set(handles.vector_x,'Value',1)
485                set(handles.vector_y,'Value',2)
486            else
487                set(handles.vector_x,'Value',vector_index(1))
488                set(handles.vector_y,'Value',vector_index(2))
489            end
490        end
491        vector_Callback(handles)
492       
493    case 'civdata...'
494        set(handles.PanelOrdinate,'Visible','off')
495        set(handles.PanelScalar,'Visible','off')
496        set(handles.PanelVectors,'Visible','off')
497        set(handles.Coordinates,'Visible','off')
498end
499
500%------------------------------------------------------------------------
501function ordinate_Callback(hObject, eventdata, handles)
502%------------------------------------------------------------------------
503Field=get(handles.get_field,'UserData');
504y_index=get(handles.ordinate,'Value');
505y_menu=get(handles.ordinate,'String');
506if isempty(y_menu)
507    return
508else
509YName=y_menu{y_index};
510end
511
512%% set list of possible coordinates
513test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
514test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
515ListCoord={''};
516dim_var=Field.Display.VarDimName{y_index};%list of dimensions of the selected variable
517
518for ilist=1:numel(Field.Display.VarDimName)
519    dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
520    if isequal(dimnames,dim_var)
521        test_component(ilist)=1;
522    elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
523        test_coord(ilist)=1;
524    end
525end
526var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
527var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
528ListCoord=Field.Display.ListVarName([var_component var_coord]);
529
530%% set default coord selection
531if numel(find(test_coord))>3
532     SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
533    if numel(SwitchVarIndexTime)<3
534        SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
535        set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
536    end
537    set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
538    SwitchVarIndexTime_Callback([], [], handles)
539end
540if numel(var_component)<2
541    if numel(test_coord)<2
542        ListCoord={''};
543    else
544        set(handles.Coord_x,'Value',2)
545        set(handles.Coord_y,'Value',1)
546    end
547else
548    coord_val=1;
549    for ilist=1:numel(var_component)
550        ivar=var_component(ilist);
551        if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
552            Role=Field.Display.VarAttribute{ivar}.Role;
553            if strcmp(Role,'coord_x')
554                coord_val=ilist;
555            end
556        end
557    end
558    set(handles.Coord_x,'Value',coord_val)
559end
560set(handles.Coord_x,'String',ListCoord)
561
562
563%% set list of time coordinates
564menu=get(handles.SwitchVarIndexTime,'String');
565TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
566switch TimeOption
567    case 'variable'
568        if numel(find(test_coord))<3
569            ListTime={''};
570        else
571            ListTime=Field.Display.ListVarName(find(test_coord,end));
572        end
573        set(handles.TimeName,'Value',1)
574        set(handles.TimeName,'String',ListTime)
575    case 'matrix index'
576        if numel(find(test_coord))<3
577            ListTime={''};
578        else
579            ListTime=Field.Display.VarDimName{find(test_coord,end)};
580        end
581        set(handles.TimeName,'Value',1)
582        set(handles.TimeName,'String',ListTime)
583end 
584update_field(handles,YName)
585         
586%------------------------------------------------------------------------
587% --- Executes on selection change in scalar menu.
588%------------------------------------------------------------------------
589function scalar_Callback(hObject, eventdata, handles)
590
591Field=get(handles.get_field,'UserData');
592scalar_index=get(handles.scalar,'Value');
593scalar_menu=get(handles.scalar,'String');
594ScalarName=scalar_menu{scalar_index};
595
596%% set list of possible coordinates
597test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
598test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
599dim_var=Field.Display.VarDimName{scalar_index};%list of dimensions of the selected variable
600if ~get(handles.CheckDimensionX,'Value') 
601    %look for coordinate variables among the other variables
602    for ilist=1:numel(Field.Display.VarDimName)
603        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
604        if isequal(dimnames,dim_var)
605            test_component(ilist)=1;% the listed variable has the same dimension as the selected scalar-> possibly chosen as unstructured coordinate
606        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var), 1))%variable ilist is a 1D array which can be coordinate variable
607            test_coord(ilist)=1;
608        end
609    end
610end
611var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
612var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
613ListCoord=Field.Display.ListVarName([var_component var_coord]);
614
615%% set default coord selection
616% if numel(find(test_coord))>3
617%     SwitchVarIndexTime=get(handles.SwitchVarIndexTime,'String');
618%     if numel(SwitchVarIndexTime)<3
619%         SwitchVarIndexTime=[SwitchVarIndexTime;'matrix_index'];
620%         set(handles.SwitchVarIndexTime,'String',SwitchVarIndexTime)
621%     end
622%     set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
623%     SwitchVarIndexTime_Callback([], [], handles)
624% end
625
626coord_val=[0 0];
627% look for labelled unstructured coordinates
628for ilist=1:numel(var_component)
629    ivar=var_component(ilist);
630    if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
631        Role=Field.Display.VarAttribute{ivar}.Role;
632        if strcmp(Role,'coord_x')
633            coord_val(1)=ilist;
634        elseif strcmp(Role,'coord_y')
635            coord_val(2)=ilist;
636        end
637    end
638end
639if numel(find(coord_val))<2
640    if numel(var_coord)>=2
641        coord_val=[numel(var_component)+2 numel(var_component)+1];
642    else
643        coord_val=[1 2];
644    end
645end
646if  get(handles.CheckDimensionX,'Value')
647    set(handles.Coord_x,'Value',2)
648    set(handles.Coord_x,'String',dim_var')
649else
650    set(handles.Coord_x,'Value',coord_val(1))
651    set(handles.Coord_x,'String',ListCoord)
652end
653if  get(handles.CheckDimensionX,'Value')
654    set(handles.Coord_y,'Value',1)
655    set(handles.Coord_y,'String',dim_var')
656else
657    set(handles.Coord_y,'Value',coord_val(2))
658    set(handles.Coord_y,'String',ListCoord)
659end
660if  get(handles.CheckDimensionX,'Value')
661    set(handles.Coord_z,'Value',1)
662    set(handles.Coord_z,'String',dim_var')
663else
664    set(handles.Coord_z,'Value',coord_val(2))
665    set(handles.Coord_z,'String',ListCoord)
666end
667
668%% set list of time coordinates
669menu=get(handles.SwitchVarIndexTime,'String');
670TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
671switch TimeOption
672    case 'variable'
673        if numel(find(test_coord))<3
674            ListTime={''};
675        else
676            ListTime=Field.Display.ListVarName(find(test_coord,end));
677        end
678        set(handles.TimeName,'Value',1)
679        set(handles.TimeName,'String',ListTime)
680    case 'dim index'
681        if numel(find(test_coord))<3
682            ListTime={''};
683        else
684            ListTime=Field.Display.VarDimName{find(test_coord,end)};
685        end
686        set(handles.TimeName,'Value',1)
687        set(handles.TimeName,'String',ListTime)
688end 
689update_field(handles,ScalarName)
690
691% --- Executes on button press in check_rgb.
692function check_rgb_Callback(hObject, eventdata, handles)
693
694
695%------------------------------------------------------------------------
696% --- Executes on selection change in vector_x.
697%------------------------------------------------------------------------
698function vector_x_Callback(hObject, eventdata, handles)
699
700vector_x_menu=get(handles.vector_x,'String');
701vector_x_index=get(handles.vector_x,'Value');
702vector_x=vector_x_menu{vector_x_index};
703vector_Callback(handles)
704update_field(handles,vector_x)
705
706%------------------------------------------------------------------------
707% --- Executes on selection change in vector_x.
708function vector_y_Callback(hObject, eventdata, handles)
709%------------------------------------------------------------------------
710vector_y_menu=get(handles.vector_x,'String');
711vector_y_index=get(handles.vector_x,'Value');
712vector_y=vector_y_menu{vector_y_index};
713vector_Callback(handles)
714update_field(handles,vector_y)
715
716%------------------------------------------------------------------------
717% --- Executes on selection change in vector_z.
718function vector_z_Callback(hObject, eventdata, handles)
719%------------------------------------------------------------------------
720vector_z_menu=get(handles.vector_z,'String');
721vector_z_index=get(handles.vector_z,'Value');
722vector_z=vector_z_menu{vector_z_index};
723vector_Callback(handles)
724update_field(handles,vector_z)
725
726%------------------------------------------------------------------------
727% --- Executes on selection change in vec_color.
728function vec_color_Callback(hObject, eventdata, handles)
729%------------------------------------------------------------------------
730index=get(handles.vec_color,'Value');
731string=get(handles.vec_color,'String');
732VarName=string{index};
733vector_Callback(handles)
734update_field(handles,VarName)
735
736%------------------------------------------------------------------------
737% --- Executes on selection change in vector_x or vector_y
738function vector_Callback( handles)
739%------------------------------------------------------------------------
740Field=get(handles.get_field,'UserData');
741vector_x_index=get(handles.vector_x,'Value');
742vector_y_index=get(handles.vector_y,'Value');
743vec_color_index=get(handles.vec_color,'Value');
744
745%% set list of possible coordinates
746test_component=zeros(size(Field.Display.VarDimName));%=1 when variable #ilist is eligible as unstructured coordinate
747test_coord=zeros(size(Field.Display.VarDimName)); %=1 when variable #ilist is eligible as structured coordiante
748check_consistent=1;%check that the selected vector components (and possibly color var) have the same dimensiosn
749ListCoord={''};
750dim_var=Field.Display.VarDimName{vector_x_index};%list of dimensions of the selected variable
751if ~isequal(dim_var,Field.Display.VarDimName{vector_y_index})
752    check_consistent=0;
753elseif vec_color_index~=1 && ~isequal(dim_var,Field.Display.VarDimName{vec_color_index})
754    check_consistent=0;
755end
756% the two vector components have consistent dimensions
757if check_consistent
758    for ilist=1:numel(Field.Display.VarDimName)
759        dimnames=Field.Display.VarDimName{ilist}; %list of dimensions for variable #ilist
760        if isequal(dimnames,dim_var)
761            test_component(ilist)=1;
762        elseif numel(dimnames)==1 && ~isempty(find(strcmp(dimnames{1},dim_var)))%variable ilist is a 1D array which can be coordinate variable
763            test_coord(ilist)=1;
764        end
765    end
766    var_component=find(test_component);% list of variable indices elligible as unstructured coordinates
767    var_coord=find(test_coord);% % list of variable indices elligible as structured coordinates
768    ListCoord=Field.Display.ListVarName([var_component var_coord]);
769   
770    %% set default coord selection
771    if numel(find(test_coord))>3
772        set(handles.SwitchVarIndexTime,'Value',3)% the last dim must be considered as time
773    end
774    if numel(var_component)<2
775        if numel(test_coord)<2
776            ListCoord={''};
777        else
778            set(handles.Coord_x,'Value',2)
779            set(handles.Coord_y,'Value',1)
780        end
781    else
782        coord_val=[0 0];
783        for ilist=1:numel(var_component)
784            ivar=var_component(ilist);
785            if isfield(Field.Display,'VarAttribute') && numel(Field.Display.VarAttribute)>=ivar && isfield(Field.Display.VarAttribute{ivar},'Role')
786                Role=Field.Display.VarAttribute{ivar}.Role;
787                if strcmp(Role,'coord_x')
788                    coord_val(1)=ilist;
789                elseif strcmp(Role,'coord_y')
790                    coord_val(2)=ilist;
791                end
792            end
793        end
794        if isempty(find(coord_val))
795            coord_val=var_coord;% case of dimension coordinates
796        end
797        if numel(find(coord_val))<2
798            %coord_val=[numel(var_component)+2 numel(var_component)+1];
799            coord_val=[1 2];
800        end
801        set(handles.Coord_x,'Value',coord_val(1))
802        set(handles.Coord_y,'Value',coord_val(2))
803    end
804end
805set(handles.Coord_y,'String',ListCoord)
806set(handles.Coord_x,'String',ListCoord)
807
808
809%% set list of time coordinates
810menu=get(handles.SwitchVarIndexTime,'String');
811TimeOption=menu{get(handles.SwitchVarIndexTime,'Value')};
812switch TimeOption
813    case 'variable'
814        if numel(find(test_coord))<3
815            ListTime={''};
816        else
817            ListTime=Field.Display.ListVarName(find(test_coord,end));
818        end
819        set(handles.TimeName,'Value',1)
820        set(handles.TimeName,'String',ListTime)
821    case 'dim index'
822        if numel(find(test_coord))<3
823            ListTime={''};
824        else
825            ListTime=Field.Display.VarDimName{find(test_coord,end)};
826        end
827        set(handles.TimeName,'Value',1)
828        set(handles.TimeName,'String',ListTime)
829end 
830
831%------------------------------------------------------------------------
832% --- Executes on selection change in SwitchVarIndexX.
833%------------------------------------------------------------------------
834function SwitchVarIndexX_Callback(hObject, eventdata, handles)
835
836%------------------------------------------------------------------------
837% --- Executes on selection change in Coord_x.
838%------------------------------------------------------------------------
839function Coord_x_Callback(hObject, eventdata, handles)
840
841index=get(handles.Coord_x,'Value');
842string=get(handles.Coord_x,'String');
843VarName=string{index};
844update_field(handles,VarName)
845
846%------------------------------------------------------------------------
847% --- Executes on selection change in Coord_y.
848%------------------------------------------------------------------------
849function Coord_y_Callback(hObject, eventdata, handles)
850
851index=get(handles.Coord_y,'Value');
852string=get(handles.Coord_y,'String');
853VarName=string{index};
854update_field(handles,VarName)
855
856%------------------------------------------------------------------------
857% --- Executes on selection change in Coord_z.
858%------------------------------------------------------------------------
859function Coord_z_Callback(hObject, eventdata, handles)
860
861index=get(handles.Coord_z,'Value');
862string=get(handles.Coord_z,'String');
863VarName=string{index};
864update_field(handles,VarName)
865
866%------------------------------------------------------------------------
867% --- Executes on selection change in SwitchVarIndexTime.
868%------------------------------------------------------------------------
869
870function SwitchVarIndexTime_Callback(hObject, eventdata, handles)
871
872Field=get(handles.get_field,'UserData');
873menu=get(handles.SwitchVarIndexTime,'String');
874option=menu{get(handles.SwitchVarIndexTime,'Value')};
875
876switch option
877    case 'file index'
878        set(handles.TimeName, 'Visible','off')% the time is taken as the file index
879    case 'attribute'
880        set(handles.TimeName, 'Visible','on')% timeName menu represents the available attributes
881        time_index=get(handles.TimeName,'UserData');    %select the input data
882        if isempty(time_index)
883            PreviousList=get(handles.TimeName, 'String');
884            if ~isempty(PreviousList)
885                PreviousAttr=PreviousList{get(handles.TimeName, 'Value')};
886                index=find(strcmp(PreviousAttr,Field.Display.ListGlobalAttributes),1);
887            end
888        end
889        if isempty(time_index)
890            time_index=find(~cellfun('isempty',regexp(Field.Display.ListGlobalAttribute,'Time')),1);% index of the attributes containing the string 'Time'
891        end     
892        if ~isempty(time_index)
893            set(handles.TimeName,'Value',time_index)
894        else
895            set(handles.TimeName,'Value',1)
896        end
897        set(handles.TimeName, 'String',Field.Display.ListGlobalAttribute)
898
899    case 'variable'% TimeName menu represents the available variables
900        set(handles.TimeName, 'Visible','on')
901        VarNbDim=cellfun('length',Field.Display.VarDimName); % check the nbre of dimensions of each input variable
902        TimeVarName=Field.Display.ListVarName(VarNbDim==1);% list of variables with a single dimension (candidate for time)
903        List=get(handles.TimeName,'String');% list of names on the menu for time
904        if isempty(List)
905            ind=1;
906        else
907            option=List{get(handles.TimeName,'Value')};% previous selected option
908            ind=find(strcmp(option,TimeVarName)); %check whether the previous selection is available in the newlist
909            if isempty(ind)
910                ind=1;
911            end
912        end
913        if ~isempty(TimeVarName)
914            set(handles.TimeName, 'Value',ind);% select first value in the menu if the option is not found
915            set(handles.TimeName, 'String',TimeVarName)% update the menu for time name
916        end
917    case 'matrix index'% TimeName menu represents the available dimensions
918        set(handles.TimeName, 'Visible','on')     
919        set(handles.TimeName, 'Value',1);
920        set(handles.TimeName, 'String',Field.Display.ListDimName)
921end
922TimeName_Callback(hObject, [], handles)
923
924%-----------------------------------------------------------------------
925function update_field(handles,VarName)
926%-----------------------------------------------------------------------
927Field=get(handles.get_field,'UserData');
928index=name2index(VarName,Field.ListVarName);
929if ~isempty(index)
930    set(handles.variables,'Value',index+1)
931    variables_Callback(handles.variables, [], handles)
932end
933
934%------------------------------------------------------------------------
935% --- give index numbers of the strings str in the list ListvarName
936% -----------------------------------------------------------------------
937function VarIndex_y=name2index(cell_str,ListVarName)
938
939VarIndex_y=[];
940if ischar(cell_str)
941    for ivar=1:length(ListVarName)
942        varlist=ListVarName{ivar};
943        if isequal(varlist,cell_str)
944            VarIndex_y= ivar;
945            break
946        end
947    end
948elseif iscell(cell_str)
949    for isel=1:length(cell_str)
950        varsel=cell_str{isel};
951        for ivar=1:length(ListVarName)
952            varlist=ListVarName{ivar};
953            if isequal(varlist,varsel)
954                VarIndex_y=[VarIndex_y ivar];
955            end
956        end
957    end
958end
959
960% --- Executes on button press in CheckDimensionY.
961function CheckDimensionX_Callback(hObject, eventdata, handles)
962FieldList=get(handles.FieldOption,'String');
963FieldOption=FieldList{get(handles.FieldOption,'Value')};
964switch FieldOption
965    case '1D plot'
966       
967    case {'scalar'}
968       scalar_Callback(hObject, eventdata, handles)
969    case 'vectors'
970end
971
972% % --- Executes on button press in CheckDimensionY.
973% function CheckDimensionY_Callback(hObject, eventdata, handles)
974% FieldList=get(handles.FieldOption,'String');
975% FieldOption=FieldList{get(handles.FieldOption,'Value')};
976% switch FieldOption
977%     case '1D plot'
978%         
979%     case {'scalar','pick variables'}
980%        scalar_Callback(hObject, eventdata, handles)
981%     case 'vectors'
982% end
983%
984%
985% % --- Executes on button press in CheckDimensionZ.
986% function CheckDimensionZ_Callback(hObject, eventdata, handles)
987% FieldList=get(handles.FieldOption,'String');
988% FieldOption=FieldList{get(handles.FieldOption,'Value')};
989% switch FieldOption
990%     case '1D plot'
991%         
992%     case 'scalar'
993%        scalar_Callback(hObject, eventdata, handles)
994%     case 'vectors'
995% end
996
997% --- Executes on selection change in TimeName.
998function TimeName_Callback(hObject, eventdata, handles)
999Field=get(handles.get_field,'UserData');
1000index=get(handles.SwitchVarIndexTime,'Value');
1001MenuIndex=get(handles.TimeName,'Value');
1002string=get(handles.TimeName,'String');
1003TimeName='';%default
1004if ~isempty(string)&&iscell(string)
1005TimeName=string{MenuIndex};
1006end
1007switch index
1008    case 1
1009        set(handles.num_TimeDimension,'String','')
1010        set(handles.TimeUnit,'String','index')
1011    case 2
1012        set(handles.num_TimeDimension,'String','')
1013        attr_index=find(strcmpi([TimeName 'Unit'],Field.ListGlobalAttribute));% look for time unit
1014        if ~isempty(attr_index)
1015            AttrName=Field.ListGlobalAttribute{attr_index};
1016            set(handles.TimeUnit,'String',Field.(AttrName))
1017        else
1018            set(handles.TimeUnit,'String','')
1019        end
1020    case {3 ,4}
1021        if index==3  % TimeName is used to chose a variable
1022            VarIndex=name2index(TimeName,Field.ListVarName);
1023            DimName=Field.VarDimName{VarIndex};
1024            DimIndex=name2index(DimName,Field.ListDimName);
1025            DimValue=Field.DimValue(DimIndex);
1026            set(handles.num_TimeDimension,'String',num2str(DimValue))
1027            unit='';
1028            if isfield(Field,'VarAttribute')&& isfield(Field.VarAttribute{VarIndex},'Unit')
1029                unit=Field.VarAttribute{VarIndex}.Unit;
1030            end
1031            set(handles.TimeUnit,'String',unit)
1032            update_field(handles,TimeName)
1033        elseif index==4% TimeName is used to chose a dimension
1034            DimName=string{MenuIndex};
1035            DimIndex=name2index(DimName,Field.ListDimName);
1036            DimValue=Field.DimValue(DimIndex);
1037            set(handles.num_TimeDimension,'String',num2str(DimValue))
1038            set(handles.TimeUnit,'String','index')
1039        end
1040end
1041
1042
1043% --- Executes on button press in Check3D.
1044function Check3D_Callback(hObject, eventdata, handles)
1045if get(handles.Check3D,'Value')
1046    status='on';
1047else
1048    status='off';
1049end
1050set(handles.Coord_z,'Visible',status)
1051% set(handles.CheckDimensionZ,'Visible',status)
1052set(handles.Z_title,'Visible',status)
1053set(handles.vector_z,'Visible',status)
1054set(handles.W_title,'Visible',status)   
Note: See TracBrowser for help on using the repository browser.