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