source: trunk/src/get_field.m @ 771

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