source: trunk/src/read_field.m @ 517

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

various bugs corrected. get_field now used in a passive way from uvmat: variable names are transferred from get_field to uvmat.

File size: 11.7 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 'civdata'
45%             ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
46%                         ParamOut.ColorVar='ima_cor';
47                        InputField=[{ParamIn.FieldName} {ParamIn.ColorVar}];
48                        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
49                        if ~isempty(errormsg),errormsg=['read_civdata:' errormsg];return,end
50                        ParamOut.CivStage=Field.CivStage;
51         case 'civx'
52            ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
53                        ParamOut.ColorVar='ima_cor';
54                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
55                        [Field,ParamOut.VelType]=read_civxdata(FileName,InputField,ParamIn.VelType);
56                        if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
57                        ParamOut.CivStage=Field.CivStage;
58        case 'netcdf'
59            r=regexp(ParamIn.FieldName,'(^vec|^norm)\((?<UName>.+),(?<VName>.+)\)$','names');
60            if isempty(r)
61                ListVar={ParamIn.FieldName};
62                input='scalar';
63            else
64                ListVar={r.UName,r.VName};
65                input='vectors';
66            end
67            if ~isempty(ParamIn.ColorVar)
68                r=regexp(ParamIn.ColorVar,'(^vec|^norm)\((?<UName>.+),(?<VName>.+)\)$','names');
69                if isempty(r)
70                    ListVar=[ListVar {ParamIn.ColorVar}];
71                else
72                    ListVar=[ListVar {r.UName,r.VName}];
73                end
74            end
75                [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.CoordName ListVar]);
76                if strcmp(input,'vectors')
77                    Field.VarAttribute{3}.Role='vector_x';
78                    Field.VarAttribute{4}.Role='vector_y';
79                else
80                    Field.VarAttribute{3}.Role='scalar';
81                end
82%             GUIName='get_field'; %default name of the GUI get_field
83%             if isfield(ParamIn,'GUIName')
84%                 GUIName=ParamIn.GUIName;
85%             end
86%             CivStage=0;
87% %             if ~strcmp(ParamIn.FieldName,'get_field...')% if get_field is not requested, look for Civx data
88%                 FieldList=calc_field;%list of possible fields for Civx data
89%                 ParamOut.ColorVar='';%default
90%                 if ischar(ParamIn.FieldName)
91%                     FieldName=ParamIn.FieldName;
92%                 else
93%                     FieldName=ParamIn.FieldName{1};
94%                 end
95%                 field_index=strcmp(FieldName,FieldList);%look for ParamIn.FieldName in the list of possible fields for Civx data
96%                 if isempty(find(field_index,1))% ParamIn.FieldName is not in the list, check whether Civx data exist
97%                     Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ','CivStage');
98%                     % case of new civdata conventions
99%                     if isequal(Data.Conventions,'uvmat/civdata')
100%                         
101%                         %case of old civx conventions
102%                     elseif ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0)
103%                         ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
104%                         ParamOut.ColorVar='ima_cor';
105%                         InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
106%                         [Field,ParamOut.VelType]=read_civxdata(FileName,InputField,ParamIn.VelType);
107%                         if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
108%                         CivStage=Field.CivStage;
109%                         ParamOut.CivStage=Field.CivStage;
110%                         % not cvix file, fields will be chosen through the GUI get_field
111%                     else
112%                         ParamOut.FieldName='get_field...';
113%                         hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
114%                         if ~isempty(hget_field)
115%                             delete(hget_field)%delete  get_field for reinitialisation
116%                         end
117%                     end
118%                 else             
119%                     InputField=ParamOut.FieldName;
120%                     if ischar(InputField)
121%                         InputField={InputField};
122%                     end
123%                     if isfield(ParamIn,'ColorVar')
124%                         ParamOut.ColorVar=ParamIn.ColorVar;
125%                         InputField=[InputField {ParamOut.ColorVar}];
126%                     end
127%                     [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
128%                     if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
129%                     CivStage=Field.CivStage;
130%                     ParamOut.CivStage=Field.CivStage;
131%                 end
132%                 ParamOut.FieldList=[{'image'};FieldList;{'get_field...'}];
133%             end
134%             if CivStage==0% read the field names on the interface get_field.
135%                 hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
136%                 if isempty(hget_field)% open the GUI get_field if it is not found
137%                     hget_field= get_field(FileName);%open the get_field GUI
138%                     set(hget_field,'Name',GUIName)%update the name of get_field (e.g. get_field_1)
139%                 end
140%                 hhget_field=guidata(hget_field);
141%                 %% update  the get_field GUI
142%                 set(hhget_field.inputfile,'String',FileName)
143%                 set(hhget_field.list_fig,'Value',1)
144%                 if exist('num','var')&&~isnan(num)
145%                     set(hhget_field.TimeIndexValue,'String',num2str(num))
146%                 end
147% %                 funct_list=get(hhget_field.ACTION,'UserData');
148% %                 funct_index=get(hhget_field.ACTION,'Value');
149% %                 funct=funct_list{funct_index};%select  the current action in get_field, e;g. PLOT
150% %                 Field=funct(hget_field); %%activate the current action selected in get_field, e;g.read the names of the variables to plot
151%                 [Field,errormsg]=read_get_field(hget_field);
152%                 Tabchar={''};%default
153%                 Tabcell=[];
154%                 set(hhget_field.inputfile,'String',FileName)
155%                 if isfield(Field,'ListGlobalAttribute')&& ~isempty(Field.ListGlobalAttribute)
156%                     for iline=1:length(Field.ListGlobalAttribute)
157%                         Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
158%                         if isfield(Field, Field.ListGlobalAttribute{iline})
159%                             val=Field.(Field.ListGlobalAttribute{iline});
160%                             if ischar(val);
161%                                 Tabcell{iline,2}=val;
162%                             else
163%                                 Tabcell{iline,2}=num2str(val);
164%                             end
165%                         end
166%                     end
167%                     if ~isempty(Tabcell)
168%                         Tabchar=cell2tab(Tabcell,'=');
169%                         Tabchar=[{''};Tabchar];
170%                     end
171%                 end
172%                 ParamOut.CivStage=0;
173%                 ParamOut.VelType=[];
174%                 if isfield(Field,'TimeIndex')
175%                     ParamOut.TimeIndex=Field.TimeIndex;
176%                 end
177%                 if isfield(Field,'TimeValue')
178%                     ParamOut.TimeValue=Field.TimeValue;
179%                 end
180%                 ParamOut.FieldList={'get_field...'};
181
182        case 'video'
183            if strcmp(class(ParamIn),'VideoReader')
184                A=read(ParamIn,num);
185            else
186                ParamOut=VideoReader(FileName);
187                A=read(ParamOut,num);
188            end
189        case 'mmreader'
190            if strcmp(class(ParamIn),'mmreader')
191                A=read(ParamIn,num);
192            else
193                ParamOut=mmreader(FileName);
194                A=read(ParamOut,num);
195            end
196        case 'vol'
197            A=imread(FileName);
198            Npz=size(A,1)/ParamIn.Npy;
199            A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
200            A=permute(A,[3 2 1]);
201        case 'multimage'
202            warning 'off'
203            A=imread(FileName,num);
204        case 'image'
205            A=imread(FileName);
206    end
207catch ME
208    errormsg=[FileType ' input: ' ME.message];
209    return
210end
211
212%% case of image
213if ~isempty(A)
214    if isstruct(ParamOut)
215    ParamOut.FieldName='image';
216    ParamOut.FieldList={'image'};
217    end
218    Npz=1;%default
219    npxy=size(A);
220%     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
221%     Rangy=[npxy(1)-0.5 0.5]; %
222    Field.NbDim=2;%default
223    Field.AName='image';
224    Field.ListVarName={'AY','AX','A'}; %
225    if ndims(A)==3
226        if Npz==1;%color
227            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
228            Field.AY=[npxy(1)-0.5 0.5];
229            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
230            if isstruct(ParamOut)
231            ParamOut.Npx=npxy(2);% display image size on the interface
232            ParamOut.Npy=npxy(1);
233            end
234            Field.VarAttribute{3}.Mesh=1;
235        else
236            Field.NbDim=3;
237            Field.ListVarName=['AZ' Field.ListVarName];
238            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
239            Field.AZ=[npxy(1)-0.5 0.5];
240            Field.AY=[npxy(2)-0.5 0.5];
241            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
242            if isstruct(ParamOut)
243            ParamOut.Npx=npxy(3);% display image size on the interface
244            ParamOut.Npy=npxy(2);
245             end
246            Field.VarAttribute{4}.Mesh=1;
247        end
248    else
249        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
250        Field.AY=[npxy(1)-0.5 0.5];
251        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
252        ParamOut.Npx=npxy(2);% display image size on the interface
253        ParamOut.Npy=npxy(1);
254        Field.VarAttribute{3}.Mesh=1;
255    end
256    Field.A=A;
257    Field.CoordUnit='pixel'; %used for mouse_motion
258end
259
260
261
Note: See TracBrowser for help on using the repository browser.