source: trunk/src/read_field.m @ 684

Last change on this file since 684 was 684, checked in by sommeria, 11 years ago

corrections in proj_field: reducing the field of an image, and read_field: reading norm of vectors on gridded mesh

File size: 10.3 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%        .VelType
10%        .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2  (fix1)....     
11%        .Npx,.Npy: for images, nbre of pixels in x and y
12% errormsg: error message, ='' by default
13%
14%INPUT
15% FileName: name of the input file
16% FileType: type of file, as determined by the function get_file_type.m
17% ParamIn: movie object or Matlab structure of input parameters
18%     .FieldName: name (char string) of the input field (for Civx data)
19%     .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...)
20%     .ColorVar: variable used for vector color
21%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
22% num: frame number for movies
23%
24% see also read_image.m,read_civxdata.m,read_civdata.m,
25
26function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
27Field=[];
28if ~exist('num','var')
29    num=1;
30end
31if ~exist('ParamIn','var')
32    ParamIn=[];
33end
34ParamOut=ParamIn;%default
35errormsg='';
36if ~exist(FileName,'file')
37    erromsg=['input file ' FileName ' does not exist'];
38    return
39end
40A=[];
41InputField={};
42check_colorvar=0;
43if isstruct(ParamIn)
44    if isfield(ParamIn,'FieldName')
45        if ischar(ParamIn.FieldName)
46            InputField={ParamIn.FieldName};
47        else
48            InputField= ParamIn.FieldName;
49        end
50    end
51    check_colorvar=zeros(size(InputField));
52    if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar)
53        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
54        check_colorvar(numel(InputField))=1;
55    end
56end
57
58%% distingush different input file types
59switch FileType
60    case 'civdata'
61        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
62        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
63        ParamOut.CivStage=Field.CivStage;
64    case 'civx'
65        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
66        [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
67        if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end
68        ParamOut.CivStage=Field.CivStage;
69    case 'netcdf'
70        ListVar={};
71        Role={};
72        ProjModeRequest={};
73        ListInputField={};
74        ListOperator={};
75        checkU=0;
76        checkV=0;
77        for ilist=1:numel(InputField)
78            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
79            if isempty(r)%  no operator used
80                if isempty(find(strcmp(InputField{ilist},ListVar)))
81                    ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list
82                    ListInputField=[ListInputField InputField(ilist)];
83                    ListOperator=[ListOperator {''}];
84                end
85                if check_colorvar(ilist)
86                    Role{numel(ListVar)}='ancillary';% not projected with interpolation
87                    ProjModeRequest{numel(ListVar)}='';
88                else
89                    Role{numel(ListVar)}='scalar';
90                    ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
91                end
92            else  % an operator 'vec' or 'norm' is used
93                %Operator=r.Operator;
94                if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
95                    ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot)
96                else
97                    ProjModeRequestVar='';
98                end
99                ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName
100                ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName
101                if isempty(ind_var_U)
102                    ListVar=[ListVar {r.UName}]; % append the variable in the list if not previously listed
103                    Role=[Role {'vector_x'}];
104                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
105                    ListInputField=[ListInputField InputField(ilist)];
106                    %ListOperator=[ListOperator {[r.Operator '_U']}];
107                else
108                    checkU=1;
109                end
110                if isempty(ind_var_V)
111                    ListVar=[ListVar {r.VName}];% append the variable in the list if not previously listed
112                    Role=[Role {'vector_y'}];
113                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
114                    ListInputField=[ListInputField {''}];
115                    %ListOperator=[ListOperator {[r.Operator '_V']}];
116                else
117                    checkV=1;
118                end
119            end
120        end
121        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
122            [Field,var_detect,ichoice]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
123        else
124            [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
125        end
126        if isfield(Field,'Txt')
127            errormsg=Field.Txt;
128            return
129        end
130        for ilist=3:numel(Field.VarDimName)
131            if isequal(Field.VarDimName{1},Field.VarDimName{ilist})
132                Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates
133                Field.VarAttribute{2}.Role='coord_y';
134                break
135            end
136        end
137        NormName='';
138        UName='';
139        VName='';
140        for ilist=1:numel(ListVar)
141            Field.VarAttribute{ilist+2}.Role=Role{ilist};
142            Field.VarAttribute{ilist+2}.ProjModeRequest=ProjModeRequest{ilist};
143            if isfield(ParamIn,'FieldName')
144                Field.VarAttribute{ilist+2}.FieldName=ListInputField{ilist};
145            end
146            r=regexp(ListInputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
147            if ~isempty(r)&& strcmp(r.Operator,'norm')
148                NormName='norm';
149                if ~isempty(find(strcmp(ListVar,'norm')))
150                    NormName='norm_1';
151                end
152                Field.ListVarName=[Field.ListVarName {NormName}];
153                ilistmax=numel(Field.ListVarName);
154                Field.VarDimName{ilistmax}=Field.VarDimName{ilist+2};
155                Field.VarAttribute{ilistmax}.Role='scalar';
156                Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName);
157                Field.(NormName)=sqrt(Field.(NormName));
158                UName=r.UName;
159                VName=r.VName;
160            end
161        end
162        if ~isempty(NormName)% remove U and V if norm has been calculated and U and V are not needed as variables
163            ind_var_U=find(strcmp(UName,ListVar));%check previous listing of variable r.UName
164            ind_var_V=find(strcmp(VName,ListVar));%check previous listing of variable r.VName
165             if ~checkU && ~checkV
166                    Field.ListVarName([ind_var_U+2 ind_var_V+2])=[];
167                    Field.VarDimName([ind_var_U+2 ind_var_V+2])=[];
168                    Field.VarAttribute([ind_var_U+2 ind_var_V+2])=[];
169                elseif ~checkU
170                    Field.ListVarName(ind_var_U+2)=[];
171                    Field.VarDimName(ind_var_U+2)=[];
172                    Field.VarAttribute(ind_var_U+2 )=[];
173                elseif ~checkV
174                    Field.ListVarName(ind_var_V+2)=[];
175                    Field.VarDimName(ind_var_V+2)=[];
176                    Field.VarAttribute(ind_var_V+2 )=[];
177             end
178        end
179    case 'video'
180        if strcmp(class(ParamIn),'VideoReader')
181            A=read(ParamIn,num);
182        else
183            ParamOut=VideoReader(FileName);
184            A=read(ParamOut,num);
185        end
186    case 'mmreader'
187        if strcmp(class(ParamIn),'mmreader')
188            A=read(ParamIn,num);
189        else
190            ParamOut=mmreader(FileName);
191            A=read(ParamOut,num);
192        end
193    case 'vol'
194        A=imread(FileName);
195        Npz=size(A,1)/ParamIn.Npy;
196        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
197        A=permute(A,[3 2 1]);
198    case 'multimage'
199        warning 'off'
200        A=imread(FileName,num);
201    case 'image'
202        A=imread(FileName);
203end
204if ~isempty(errormsg)
205    errormsg=[FileType ' input: ' errormsg];
206    return
207end
208
209%% case of image
210if ~isempty(A)
211    if isstruct(ParamOut)
212        ParamOut.FieldName='image';
213    end
214    Npz=1;%default
215    npxy=size(A);
216    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
217    %     Rangy=[npxy(1)-0.5 0.5]; %
218    Field.NbDim=2;%default
219    Field.AName='image';
220    Field.ListVarName={'AY','AX','A'}; %
221    if ndims(A)==3
222        if Npz==1;%color
223            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
224            Field.AY=[npxy(1)-0.5 0.5];
225            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
226            if isstruct(ParamOut)
227                ParamOut.Npx=npxy(2);% display image size on the interface
228                ParamOut.Npy=npxy(1);
229            end
230            Field.VarAttribute{3}.Mesh=1;
231        else
232            Field.NbDim=3;
233            Field.ListVarName=['AZ' Field.ListVarName];
234            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
235            Field.AZ=[npxy(1)-0.5 0.5];
236            Field.AY=[npxy(2)-0.5 0.5];
237            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
238            if isstruct(ParamOut)
239                ParamOut.Npx=npxy(3);% display image size on the interface
240                ParamOut.Npy=npxy(2);
241            end
242            Field.VarAttribute{4}.Mesh=1;
243        end
244    else
245        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
246        Field.AY=[npxy(1)-0.5 0.5];
247        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
248        ParamOut.Npx=npxy(2);% display image size on the interface
249        ParamOut.Npy=npxy(1);
250        Field.VarAttribute{3}.Mesh=1;
251    end
252    Field.A=A;
253    Field.CoordUnit='pixel'; %used for mouse_motion
254end
255
256
257
Note: See TracBrowser for help on using the repository browser.