source: trunk/src/read_field.m @ 497

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

cleaning and small bug repair.
pb of histogram for filter data solved
display of uicontrol by right mouse selection improved

File size: 10.2 KB
RevLine 
[497]1%'read_field': read the fields from files in different formats (netcdf files, images, video)
[181]2%--------------------------------------------------------------------------
[450]3%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[181]4%
5% OUTPUT:
6% Field: matlab structure representing the field
7% ParamOut: structure representing parameters:
8%        .FieldName; field name
9%        .FieldList: menu of possible fields
10%        .VelType
11%        .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2  (fix1)....     
12%        .Npx,.Npy: for images, nbre of pixels in x and y
13% errormsg: error message, ='' by default
14%
15%INPUT
[466]16% FileName: name of the input file
[497]17% FileType: type of file, as determined by the function get_file_type.m
[450]18% ParamIn: movie object or Matlab structure of input parameters
[445]19%     .FieldName: name (char string) of the input field (for Civx data)
20%     .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...)
[181]21%     .ColorVar: variable used for vector color
22%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
[466]23% num: frame number for movies
[497]24%
25% see also read_image.m,read_civxdata.m,read_civdata.m,
[466]26
[450]27function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[181]28Field=[];
[354]29if ~exist('num','var')
30    num=1;
31end
32if ~exist('ParamIn','var')
33    ParamIn=[];
34end
35ParamOut=ParamIn;%default
[181]36errormsg='';
[466]37% if isfield(ParamIn,'VelType')
38% VelType=ParamIn.VelType;
39% end
[334]40A=[];
41%% distingush different input file types
[445]42try
43    switch FileType
44        case {'civx','civdata','netcdf'}  %read the first nc field
45            %         ParamOut.FieldName=ParamIn.FieldName;
46            GUIName='get_field'; %default name of the GUI get_field
47            if isfield(ParamIn,'GUIName')
48                GUIName=ParamIn.GUIName;
49            end
50            CivStage=0;
51            if ~strcmp(ParamIn.FieldName,'get_field...')% if get_field is not requested, look for Civx data
52                FieldList=calc_field;%list of possible fields for Civx data
53                ParamOut.ColorVar='';%default
[472]54                if ischar(ParamIn.FieldName)
55                    FieldName=ParamIn.FieldName;
56                else
57                    FieldName=ParamIn.FieldName{1};
58                end
59                field_index=strcmp(FieldName,FieldList);%look for ParamIn.FieldName in the list of possible fields for Civx data
[445]60                if isempty(find(field_index,1))% ParamIn.FieldName is not in the list, check whether Civx data exist
[450]61                    Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ','CivStage');
[445]62                    % case of new civdata conventions
63                    if isequal(Data.Conventions,'uvmat/civdata')
64                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
65                        ParamOut.ColorVar='ima_cor';
66                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
[450]67                        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType,Data.CivStage);
[472]68                        if ~isempty(errormsg),errormsg=['read_civdata:' errormsg];return,end
[445]69                        CivStage=Field.CivStage;
70                        ParamOut.CivStage=Field.CivStage;
71                        %case of old civx conventions
72                    elseif ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0)
73                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
74                        ParamOut.ColorVar='ima_cor';
75                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
[450]76                        [Field,ParamOut.VelType]=read_civxdata(FileName,InputField,ParamIn.VelType);
[472]77                        if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
[445]78                        CivStage=Field.CivStage;
79                        ParamOut.CivStage=Field.CivStage;
80                        % not cvix file, fields will be chosen through the GUI get_field
81                    else
82                        ParamOut.FieldName='get_field...';
83                        hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
84                        if ~isempty(hget_field)
85                            delete(hget_field)%delete  get_field for reinitialisation
86                        end
87                    end
[472]88                else             
89                    InputField=ParamOut.FieldName;
90                    if ischar(InputField)
91                        InputField={InputField};
92                    end
[445]93                    if isfield(ParamIn,'ColorVar')
94                        ParamOut.ColorVar=ParamIn.ColorVar;
95                        InputField=[InputField {ParamOut.ColorVar}];
96                    end
[450]97                    [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
[472]98                    if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
[404]99                    CivStage=Field.CivStage;
[405]100                    ParamOut.CivStage=Field.CivStage;
[334]101                end
[445]102                ParamOut.FieldList=[{'image'};FieldList;{'get_field...'}];
103            end
104            if CivStage==0% read the field names on the interface get_field.
105                hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
106                if isempty(hget_field)% open the GUI get_field if it is not found
[450]107                    hget_field= get_field(FileName);%open the get_field GUI
[445]108                    set(hget_field,'Name',GUIName)%update the name of get_field (e.g. get_field_1)
[334]109                end
[445]110                hhget_field=guidata(hget_field);
111                %% update  the get_field GUI
[450]112                set(hhget_field.inputfile,'String',FileName)
[445]113                set(hhget_field.list_fig,'Value',1)
114                if exist('num','var')&&~isnan(num)
115                    set(hhget_field.TimeIndexValue,'String',num2str(num))
[334]116                end
[445]117                funct_list=get(hhget_field.ACTION,'UserData');
118                funct_index=get(hhget_field.ACTION,'Value');
119                funct=funct_list{funct_index};%select  the current action in get_field, e;g. PLOT
120                Field=funct(hget_field); %%activate the current action selected in get_field, e;g.read the names of the variables to plot
121                Tabchar={''};%default
122                Tabcell=[];
[450]123                set(hhget_field.inputfile,'String',FileName)
[445]124                if isfield(Field,'ListGlobalAttribute')&& ~isempty(Field.ListGlobalAttribute)
125                    for iline=1:length(Field.ListGlobalAttribute)
126                        Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
127                        if isfield(Field, Field.ListGlobalAttribute{iline})
[472]128                            val=Field.(Field.ListGlobalAttribute{iline});
[445]129                            if ischar(val);
130                                Tabcell{iline,2}=val;
131                            else
132                                Tabcell{iline,2}=num2str(val);
133                            end
[334]134                        end
[181]135                    end
[445]136                    if ~isempty(Tabcell)
137                        Tabchar=cell2tab(Tabcell,'=');
138                        Tabchar=[{''};Tabchar];
139                    end
[181]140                end
[445]141                ParamOut.CivStage=0;
142                ParamOut.VelType=[];
143                if isfield(Field,'TimeIndex')
144                    ParamOut.TimeIndex=Field.TimeIndex;
[334]145                end
[445]146                if isfield(Field,'TimeValue')
147                    ParamOut.TimeValue=Field.TimeValue;
148                end
149                ParamOut.FieldList={'get_field...'};
[181]150            end
[493]151        case 'video'
152            if strcmp(class(ParamIn),'VideoReader')
153                A=read(ParamIn,num);
154            else
155                ParamOut=VideoReader(FileName);
156                A=read(ParamOut,num);
157            end
158        case 'mmreader'
159            if strcmp(class(ParamIn),'mmreader')
160                A=read(ParamIn,num);
161            else
162                ParamOut=mmreader(FileName);
163                A=read(ParamOut,num);
164            end
[445]165        case 'vol'
[450]166            A=imread(FileName);
[445]167            Npz=size(A,1)/ParamIn.Npy;
168            A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
169            A=permute(A,[3 2 1]);
170        case 'multimage'
171            warning 'off'
[450]172            A=imread(FileName,num);
[445]173        case 'image'
[450]174            A=imread(FileName);
[445]175    end
176catch ME
[472]177    errormsg=[FileType ' input: ' ME.message];
[445]178    return
[334]179end
[445]180
[334]181%% case of image
182if ~isempty(A)
[452]183    if isstruct(ParamOut)
[181]184    ParamOut.FieldName='image';
185    ParamOut.FieldList={'image'};
[452]186    end
[182]187    Npz=1;%default
[181]188    npxy=size(A);
[397]189%     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
190%     Rangy=[npxy(1)-0.5 0.5]; %
[181]191    Field.NbDim=2;%default
192    Field.AName='image';
193    Field.ListVarName={'AY','AX','A'}; %
194    if ndims(A)==3
195        if Npz==1;%color
196            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
197            Field.AY=[npxy(1)-0.5 0.5];
198            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[452]199            if isstruct(ParamOut)
[182]200            ParamOut.Npx=npxy(2);% display image size on the interface
201            ParamOut.Npy=npxy(1);
[452]202            end
[186]203            Field.VarAttribute{3}.Mesh=1;
[181]204        else
205            Field.NbDim=3;
206            Field.ListVarName=['AZ' Field.ListVarName];
207            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
208            Field.AZ=[npxy(1)-0.5 0.5];
209            Field.AY=[npxy(2)-0.5 0.5];
210            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
[452]211            if isstruct(ParamOut)
[182]212            ParamOut.Npx=npxy(3);% display image size on the interface
213            ParamOut.Npy=npxy(2);
[452]214             end
[186]215            Field.VarAttribute{4}.Mesh=1;
[181]216        end
217    else
218        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
219        Field.AY=[npxy(1)-0.5 0.5];
220        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[182]221        ParamOut.Npx=npxy(2);% display image size on the interface
222        ParamOut.Npy=npxy(1);
[186]223        Field.VarAttribute{3}.Mesh=1;
[181]224    end
225    Field.A=A;
226    Field.CoordUnit='pixel'; %used for mouse_motion
227end
228
229
[334]230
Note: See TracBrowser for help on using the repository browser.