source: trunk/src/read_field.m @ 520

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

new bugs corrected

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