source: trunk/src/read_field.m @ 656

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

various bugs corrected

File size: 7.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%        .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')
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        for ilist=1:numel(InputField)
72            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
73            if isempty(r)%  no operator used
74                ListVar=[ListVar InputField(ilist)];
75                if check_colorvar(ilist)
76                                    Role{numel(ListVar)}='ancillary';% not projected with interpolation
77                ProjModeRequest{numel(ListVar)}='';
78                else
79                Role{numel(ListVar)}='scalar';
80                ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
81                end
82            else  % an operator 'vec' or 'norm' is used
83                ListVar=[ListVar {r.UName,r.VName}];
84                Role{numel(ListVar)}='vector_y';
85                Role{numel(ListVar)-1}='vector_x';
86                      if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
87                                    ProjModeRequest{numel(ListVar)-1}='interp_lin';%scalar field (requires interpolation for plot)
88                                    ProjModeRequest{numel(ListVar)}='interp_lin';
89                      else
90                                   ProjModeRequest{numel(ListVar)-1}='';
91                                   ProjModeRequest{numel(ListVar)}='';
92                            end
93            end
94        end
95        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
96            [Field,var_detect,ichoice]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
97        else
98        [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]);
99        end
100        if isfield(Field,'Txt')
101            errormsg=Field.Txt;
102            return
103        end
104        for ivar=1:numel(ListVar)
105            Field.VarAttribute{ivar+2}.Role=Role{ivar};
106            if isfield(ParamIn,'FieldName')
107                Field.VarAttribute{ivar+2}.FieldName=ParamIn.FieldName;
108            end
109            Field.VarAttribute{ivar+2}.ProjModeRequest=ProjModeRequest{ivar};
110        end       
111    case 'video'
112        if strcmp(class(ParamIn),'VideoReader')
113            A=read(ParamIn,num);
114        else
115            ParamOut=VideoReader(FileName);
116            A=read(ParamOut,num);
117        end
118    case 'mmreader'
119        if strcmp(class(ParamIn),'mmreader')
120            A=read(ParamIn,num);
121        else
122            ParamOut=mmreader(FileName);
123            A=read(ParamOut,num);
124        end
125    case 'vol'
126        A=imread(FileName);
127        Npz=size(A,1)/ParamIn.Npy;
128        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
129        A=permute(A,[3 2 1]);
130    case 'multimage'
131        warning 'off'
132        A=imread(FileName,num);
133    case 'image'
134        A=imread(FileName);
135end
136if ~isempty(errormsg)
137    errormsg=[FileType ' input: ' errormsg];
138    return
139end
140
141%% case of image
142if ~isempty(A)
143    if isstruct(ParamOut)
144        ParamOut.FieldName='image';
145    end
146    Npz=1;%default
147    npxy=size(A);
148    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
149    %     Rangy=[npxy(1)-0.5 0.5]; %
150    Field.NbDim=2;%default
151    Field.AName='image';
152    Field.ListVarName={'AY','AX','A'}; %
153    if ndims(A)==3
154        if Npz==1;%color
155            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
156            Field.AY=[npxy(1)-0.5 0.5];
157            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
158            if isstruct(ParamOut)
159                ParamOut.Npx=npxy(2);% display image size on the interface
160                ParamOut.Npy=npxy(1);
161            end
162            Field.VarAttribute{3}.Mesh=1;
163        else
164            Field.NbDim=3;
165            Field.ListVarName=['AZ' Field.ListVarName];
166            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
167            Field.AZ=[npxy(1)-0.5 0.5];
168            Field.AY=[npxy(2)-0.5 0.5];
169            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
170            if isstruct(ParamOut)
171                ParamOut.Npx=npxy(3);% display image size on the interface
172                ParamOut.Npy=npxy(2);
173            end
174            Field.VarAttribute{4}.Mesh=1;
175        end
176    else
177        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
178        Field.AY=[npxy(1)-0.5 0.5];
179        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
180        ParamOut.Npx=npxy(2);% display image size on the interface
181        ParamOut.Npy=npxy(1);
182        Field.VarAttribute{3}.Mesh=1;
183    end
184    Field.A=A;
185    Field.CoordUnit='pixel'; %used for mouse_motion
186end
187
188
189
Note: See TracBrowser for help on using the repository browser.