source: trunk/src/read_field.m @ 466

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

bugs corrected

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