source: trunk/src/get_field.m @ 762

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