source: trunk/src/read_field.m @ 516

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

various bug corrections

File size: 10.2 KB
Line 
1%'read_field': read the fields from files in different formats (netcdf files, images, video)
2%--------------------------------------------------------------------------
3%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
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
16% FileName: name of the input file
17% FileType: type of file, as determined by the function get_file_type.m
18% ParamIn: movie object or Matlab structure of input parameters
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'...)
21%     .ColorVar: variable used for vector color
22%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
23% num: frame number for movies
24%
25% see also read_image.m,read_civxdata.m,read_civdata.m,
26
27function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
28Field=[];
29if ~exist('num','var')
30    num=1;
31end
32if ~exist('ParamIn','var')
33    ParamIn=[];
34end
35ParamOut=ParamIn;%default
36errormsg='';
37% if isfield(ParamIn,'VelType')
38% VelType=ParamIn.VelType;
39% end
40A=[];
41%% distingush different input file types
42try
43    switch FileType
44        case {'civx','civdata','netcdf'}  %read the first nc field
45            GUIName='get_field'; %default name of the GUI get_field
46            if isfield(ParamIn,'GUIName')
47                GUIName=ParamIn.GUIName;
48            end
49            CivStage=0;
50            if ~strcmp(ParamIn.FieldName,'get_field...')% if get_field is not requested, look for Civx data
51                FieldList=calc_field;%list of possible fields for Civx data
52                ParamOut.ColorVar='';%default
53                if ischar(ParamIn.FieldName)
54                    FieldName=ParamIn.FieldName;
55                else
56                    FieldName=ParamIn.FieldName{1};
57                end
58                field_index=strcmp(FieldName,FieldList);%look for ParamIn.FieldName in the list of possible fields for Civx data
59                if isempty(find(field_index,1))% ParamIn.FieldName is not in the list, check whether Civx data exist
60                    Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ','CivStage');
61                    % case of new civdata conventions
62                    if isequal(Data.Conventions,'uvmat/civdata')
63                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
64                        ParamOut.ColorVar='ima_cor';
65                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
66                        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType,Data.CivStage);
67                        if ~isempty(errormsg),errormsg=['read_civdata:' errormsg];return,end
68                        CivStage=Field.CivStage;
69                        ParamOut.CivStage=Field.CivStage;
70                        %case of old civx conventions
71                    elseif ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0)
72                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
73                        ParamOut.ColorVar='ima_cor';
74                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
75                        [Field,ParamOut.VelType]=read_civxdata(FileName,InputField,ParamIn.VelType);
76                        if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
77                        CivStage=Field.CivStage;
78                        ParamOut.CivStage=Field.CivStage;
79                        % not cvix file, fields will be chosen through the GUI get_field
80                    else
81                        ParamOut.FieldName='get_field...';
82                        hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
83                        if ~isempty(hget_field)
84                            delete(hget_field)%delete  get_field for reinitialisation
85                        end
86                    end
87                else             
88                    InputField=ParamOut.FieldName;
89                    if ischar(InputField)
90                        InputField={InputField};
91                    end
92                    if isfield(ParamIn,'ColorVar')
93                        ParamOut.ColorVar=ParamIn.ColorVar;
94                        InputField=[InputField {ParamOut.ColorVar}];
95                    end
96                    [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
97                    if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
98                    CivStage=Field.CivStage;
99                    ParamOut.CivStage=Field.CivStage;
100                end
101                ParamOut.FieldList=[{'image'};FieldList;{'get_field...'}];
102            end
103            if CivStage==0% read the field names on the interface get_field.
104                hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
105                if isempty(hget_field)% open the GUI get_field if it is not found
106                    hget_field= get_field(FileName);%open the get_field GUI
107                    set(hget_field,'Name',GUIName)%update the name of get_field (e.g. get_field_1)
108                end
109                hhget_field=guidata(hget_field);
110                %% update  the get_field GUI
111                set(hhget_field.inputfile,'String',FileName)
112                set(hhget_field.list_fig,'Value',1)
113                if exist('num','var')&&~isnan(num)
114                    set(hhget_field.TimeIndexValue,'String',num2str(num))
115                end
116%                 funct_list=get(hhget_field.ACTION,'UserData');
117%                 funct_index=get(hhget_field.ACTION,'Value');
118%                 funct=funct_list{funct_index};%select  the current action in get_field, e;g. PLOT
119%                 Field=funct(hget_field); %%activate the current action selected in get_field, e;g.read the names of the variables to plot
120                [Field,errormsg]=read_get_field(hget_field);
121                Tabchar={''};%default
122                Tabcell=[];
123                set(hhget_field.inputfile,'String',FileName)
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})
128                            val=Field.(Field.ListGlobalAttribute{iline});
129                            if ischar(val);
130                                Tabcell{iline,2}=val;
131                            else
132                                Tabcell{iline,2}=num2str(val);
133                            end
134                        end
135                    end
136                    if ~isempty(Tabcell)
137                        Tabchar=cell2tab(Tabcell,'=');
138                        Tabchar=[{''};Tabchar];
139                    end
140                end
141                ParamOut.CivStage=0;
142                ParamOut.VelType=[];
143                if isfield(Field,'TimeIndex')
144                    ParamOut.TimeIndex=Field.TimeIndex;
145                end
146                if isfield(Field,'TimeValue')
147                    ParamOut.TimeValue=Field.TimeValue;
148                end
149                ParamOut.FieldList={'get_field...'};
150            end
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
165        case 'vol'
166            A=imread(FileName);
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'
172            A=imread(FileName,num);
173        case 'image'
174            A=imread(FileName);
175    end
176catch ME
177    errormsg=[FileType ' input: ' ME.message];
178    return
179end
180
181%% case of image
182if ~isempty(A)
183    if isstruct(ParamOut)
184    ParamOut.FieldName='image';
185    ParamOut.FieldList={'image'};
186    end
187    Npz=1;%default
188    npxy=size(A);
189%     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
190%     Rangy=[npxy(1)-0.5 0.5]; %
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
199            if isstruct(ParamOut)
200            ParamOut.Npx=npxy(2);% display image size on the interface
201            ParamOut.Npy=npxy(1);
202            end
203            Field.VarAttribute{3}.Mesh=1;
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
211            if isstruct(ParamOut)
212            ParamOut.Npx=npxy(3);% display image size on the interface
213            ParamOut.Npy=npxy(2);
214             end
215            Field.VarAttribute{4}.Mesh=1;
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
221        ParamOut.Npx=npxy(2);% display image size on the interface
222        ParamOut.Npy=npxy(1);
223        Field.VarAttribute{3}.Mesh=1;
224    end
225    Field.A=A;
226    Field.CoordUnit='pixel'; %used for mouse_motion
227end
228
229
230
Note: See TracBrowser for help on using the repository browser.