source: trunk/src/read_field.m @ 580

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

bugs corrected for combining fields

File size: 6.5 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='';
36A=[];
37InputField={};
38check_colorvar=0;
39if isstruct(ParamIn)
40    if isfield(ParamIn,'FieldName')
41        if ischar(ParamIn.FieldName)
42            InputField={ParamIn.FieldName};
43        else
44            InputField= ParamIn.FieldName;
45        end
46    end
47    if isfield(ParamIn,'ColorVar')
48        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
49        check_colorvar=1;
50    end
51end
52
53%% distingush different input file types
54switch FileType
55    case 'civdata'
56        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
57        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
58        ParamOut.CivStage=Field.CivStage;
59    case 'civx'
60        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
61        [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
62        if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end
63        ParamOut.CivStage=Field.CivStage;
64    case 'netcdf'
65        ListVar={};
66        for ilist=1:numel(InputField)
67            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
68            if isempty(r)
69                ListVar=[ListVar InputField(ilist)];
70                Role{numel(ListVar)}='scalar';
71                %                     FieldRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
72            else
73                ListVar=[ListVar {r.UName,r.VName}];
74                Role{numel(ListVar)}='vector_y';
75                Role{numel(ListVar)-1}='vector_x';
76                %                    TODO; introduce that for unstructured coordinates
77                %                     switch r.Operator TODO; introduce that for unstructured coordinates
78                %                         case 'norm'
79                %                             FieldRequest{numel(ListVar)-1}='interp_lin';%scalar field (requires interpolation for plot)
80                %                             FieldRequest{numel(ListVar)}='interp_lin';
81                %                         otherwise
82                %                            FieldRequest{numel(ListVar)-1}='';
83                %                     end
84            end
85        end
86        if check_colorvar
87            Role{numel(ListVar)}='ancillary';% scalar used for color vector (not projected)
88        end
89        [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y)' ListVar]);
90        if isfield(Field,'Txt')
91            errormsg=Field.Txt;
92            return
93        end
94        for ivar=1:numel(ListVar)
95            Field.VarAttribute{ivar+2}.Role=Role{ivar};
96            %                 Field.VarAttribute{ivar+2}.FieldRequest=FieldRequest{ivar};
97        end
98       
99    case 'video'
100        if strcmp(class(ParamIn),'VideoReader')
101            A=read(ParamIn,num);
102        else
103            ParamOut=VideoReader(FileName);
104            A=read(ParamOut,num);
105        end
106    case 'mmreader'
107        if strcmp(class(ParamIn),'mmreader')
108            A=read(ParamIn,num);
109        else
110            ParamOut=mmreader(FileName);
111            A=read(ParamOut,num);
112        end
113    case 'vol'
114        A=imread(FileName);
115        Npz=size(A,1)/ParamIn.Npy;
116        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
117        A=permute(A,[3 2 1]);
118    case 'multimage'
119        warning 'off'
120        A=imread(FileName,num);
121    case 'image'
122        A=imread(FileName);
123end
124if ~isempty(errormsg)
125    errormsg=[FileType ' input: ' errormsg];
126    return
127end
128
129%% case of image
130if ~isempty(A)
131    if isstruct(ParamOut)
132        ParamOut.FieldName='image';
133    end
134    Npz=1;%default
135    npxy=size(A);
136    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
137    %     Rangy=[npxy(1)-0.5 0.5]; %
138    Field.NbDim=2;%default
139    Field.AName='image';
140    Field.ListVarName={'AY','AX','A'}; %
141    if ndims(A)==3
142        if Npz==1;%color
143            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
144            Field.AY=[npxy(1)-0.5 0.5];
145            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
146            if isstruct(ParamOut)
147                ParamOut.Npx=npxy(2);% display image size on the interface
148                ParamOut.Npy=npxy(1);
149            end
150            Field.VarAttribute{3}.Mesh=1;
151        else
152            Field.NbDim=3;
153            Field.ListVarName=['AZ' Field.ListVarName];
154            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
155            Field.AZ=[npxy(1)-0.5 0.5];
156            Field.AY=[npxy(2)-0.5 0.5];
157            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
158            if isstruct(ParamOut)
159                ParamOut.Npx=npxy(3);% display image size on the interface
160                ParamOut.Npy=npxy(2);
161            end
162            Field.VarAttribute{4}.Mesh=1;
163        end
164    else
165        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
166        Field.AY=[npxy(1)-0.5 0.5];
167        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
168        ParamOut.Npx=npxy(2);% display image size on the interface
169        ParamOut.Npy=npxy(1);
170        Field.VarAttribute{3}.Mesh=1;
171    end
172    Field.A=A;
173    Field.CoordUnit='pixel'; %used for mouse_motion
174end
175
176
177
Note: See TracBrowser for help on using the repository browser.