source: trunk/src/read_civdata.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: 8.9 KB
Line 
1%'read_civdata': reads new civ data from netcdf files
2%------------------------------------------------------------------
3%
4% function [Field,VelTypeOut]=read_civdata(filename,FieldNames,VelType)
5%
6% OUTPUT:
7% Field: structure representing the selected field, containing
8%            .Txt: (char string) error message if any
9%            .ListGlobalAttribute: list of global attributes containing:
10%                    .NbCoord: number of vector components
11%                    .NbDim: number of dimensions (=2 or 3)
12%                    .Dt: time interval for the corresponding image pair
13%                    .Time: absolute time (average of the initial image pair)
14%                    .CivStage: =0,
15%                       =1, civ1 has been performed only
16%                       =2, fix1 has been performed
17%                       =3, pacth1 has been performed
18%                       =4, civ2 has been performed
19%                       =5, fix2 has been performed
20%                       =6, pacth2 has been performed
21%                     .CoordUnit: 'pixel'
22%             .ListVarName: {'X'  'Y'  'U'  'V'  'F'  'FF'}
23%                   .X, .Y, .Z: set of vector coordinates
24%                    .U,.V,.W: corresponding set of vector components
25%                    .F: warning flags
26%                    .FF: error flag, =0 for good vectors
27%                     .C: scalar associated with velocity (used for vector colors)
28%
29% VelTypeOut: velocity type corresponding to the selected field: ='civ1','interp1','interp2','civ2'....
30%
31% INPUT:
32% filename: file name (string).
33% FieldNames =cell of field names to get, which can contain the strings:
34%             'ima_cor': image correlation, vec_c or vec2_C
35%             'vort','div','strain': requires velocity derivatives DUDX...
36%             'error': error estimate (vec_E or vec2_E)
37%             
38% VelType : character string indicating the types of velocity fields to read ('civ1','civ2'...)
39%            if vel_type=[] or'*', a  priority choice, given by vel_type_out{1,2}, is done depending
40%            if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)
41
42
43% FUNCTIONS called:
44% 'varcivx_generator':, sets the names of vaiables to read in the netcdf file
45% 'nc2struct': reads a netcdf file
46
47function [Field,VelTypeOut,errormsg]=read_civdata(filename,FieldNames,VelType)
48
49%% default input
50if ~exist('VelType','var')
51    VelType='';
52end
53if isequal(VelType,'*')
54    VelType='';
55end
56if isempty(VelType)
57    VelType='';
58end
59if ~exist('FieldNames','var')
60    FieldNames=[]; %default
61end
62errormsg='';
63if ischar(FieldNames), FieldNames={FieldNames}; end;
64FieldRequest='';
65for ilist=1:length(FieldNames)
66    if ~isempty(FieldNames{ilist})
67        switch FieldNames{ilist}
68            case{'U','V','norm(U,V)'}
69                FieldRequest='interp_lin';
70            case {'curl(U,V)','div(U,V)','strain(U,V)'}
71                FieldRequest='interp_tps';
72        end
73    end
74end
75
76%% reading data
77Data=nc2struct(filename,'ListGlobalAttribute','CivStage');
78[varlist,role,VelTypeOut]=varcivx_generator(FieldRequest,VelType,Data.CivStage);
79if isempty(varlist)
80    erromsg=['error in read_civdata: unknow velocity type ' VelType];
81    return
82else
83    [Field,vardetect]=nc2struct(filename,varlist);%read the variables in the netcdf file
84end
85if isfield(Field,'Txt')
86    errormsg=Field.Txt;
87    return
88end
89if vardetect(1)==0
90     errormsg=[ 'requested field not available in ' filename '/' VelType ': need to run patch'];
91     return
92end
93switch VelTypeOut
94    case{'civ1','filter1'}
95        if isfield(Field,'Patch1_SubDomain')
96            Field.SubDomain=Field.Patch1_SubDomain;
97            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
98        end     
99        Field.Dt=Field.Civ1_Dt;
100        Field.Time=Field.Civ1_Time;
101    case{'civ2','filter2'}
102        if isfield(Field,'Patch2_SubDomain')
103            Field.SubDomain=Field.Patch2_SubDomain;
104            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
105        end
106        Field.Dt=Field.Civ2_Dt;
107        Field.Time=Field.Civ2_Time;
108end
109Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'Dt','Time'}];
110ivar_U_tps=[];
111var_ind=find(vardetect);
112for ivar=1:numel(var_ind)
113    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
114    Field.VarAttribute{ivar}.FieldRequest=FieldRequest;
115    if strcmp(role{var_ind(ivar)},'vector_x')
116        Field.VarAttribute{ivar}.FieldName=FieldNames;
117        ivar_U=ivar;
118    end
119    if strcmp(role{var_ind(ivar)},'vector_x_tps')
120        Field.VarAttribute{ivar}.FieldName=FieldNames;
121        ivar_U_tps=ivar;
122    end
123%     Field.VarAttribute{ivar}.Unit=units{var_ind(ivar)};
124    Field.VarAttribute{ivar}.Mesh=0.1;%typical mesh for histograms O.1 pixel
125end
126if ~isempty(ivar_U_tps)
127    Field.VarAttribute{ivar_U}.VarIndex_tps=ivar_U_tps;
128end
129
130Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'NbCoord','NbDim','TimeUnit','CoordUnit'}];
131% %% update list of global attributes
132Field.NbCoord=2;
133Field.NbDim=2;
134Field.TimeUnit='s';
135Field.CoordUnit='pixel';
136
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
139%INPUT:
140% FieldNames =cell of field names to get, which can contain the strings:
141%             'ima_cor': image correlation, vec_c or vec2_C
142%             'vort','div','strain': requires velocity derivatives DUDX...
143%             'error': error estimate (vec_E or vec2_E)
144%             
145% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
146%            if vel_type=[] or'*', a  priority choice is done, civ2 considered better than civ1 )
147
148function [var,role,vel_type_out,errormsg]=varcivx_generator(FieldRequest,vel_type,CivStage)
149
150%% default input values
151if ~exist('vel_type','var'),vel_type='';end;
152if iscell(vel_type),vel_type=vel_type{1}; end;%transform cell to string if needed
153errormsg='';
154
155%% select the priority order for automatic vel_type selection
156if strcmp(vel_type,'civ2') && strcmp(FieldRequest,'derivatives')
157    vel_type='filter2';
158elseif strcmp(vel_type,'civ1') && strcmp(FieldRequest,'derivatives')
159    vel_type='filter1';
160end
161if isempty(vel_type)||strcmp(vel_type,'*')
162    switch CivStage
163        case {6} %filter2 available
164            vel_type='filter2';
165        case {4,5}% civ2 available but not filter2
166            if strcmp(FieldRequest,'derivatives')% derivatives needed
167                vel_type='filter1';
168            else
169                vel_type='civ2';
170            end
171        case 3
172            vel_type='filter1';
173        case {1,2}% civ1 available but not filter1
174            vel_type='civ1';
175    end
176end
177
178var={};
179switch vel_type
180    case 'civ1'
181        var={'X','Y','Z','U','V','W','C','F','FF';...
182            'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U','Civ1_V','Civ1_W','Civ1_C','Civ1_F','Civ1_FF'};
183        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'};
184    %    units={'pixel','pixel','pixel','pixel','pixel','pixel','','',''};
185    case 'filter1'
186        var={'X','Y','Z','U','V','W','C','F','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbSites';...
187            'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U_smooth','Civ1_V_smooth','Civ1_W','Civ1_C','Civ1_F','Civ1_FF',...
188            'Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps','Civ1_W_tps','Civ1_SubRange','Civ1_NbSites'};
189        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag','coord_tps','vector_x_tps',...
190            'vector_y_tps','vector_z_tps','ancillary','ancillary'};
191     %   units={'pixel','pixel','pixel','pixel','pixel','pixel','','','','pixel','pixel','pixel','pixel','pixel',''};
192    case 'civ2'
193        var={'X','Y','Z','U','V','W','C','F','FF';...
194            'Civ2_X','Civ2_Y','Civ2_Z','Civ2_U','Civ2_V','Civ2_W','Civ2_C','Civ2_F','Civ2_FF'};
195        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'};
196      %  units={'pixel','pixel','pixel','pixel','pixel','pixel','','',''};
197    case 'filter2'
198        var={'X','Y','Z','U','V','W','C','F','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbSites';...
199            'Civ2_X','Civ2_Y','Civ2_Z','Civ2_U_smooth','Civ2_V_smooth','Civ2_W','Civ2_C','Civ2_F','Civ2_FF',...
200            'Civ2_Coord_tps','Civ2_U_tps','Civ2_V_tps','','Civ2_SubRange','Civ2_NbSites'};
201        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag','coord_tps','vector_x_tps',...
202            'vector_y_tps','vector_z_tps','ancillary','ancillary'};
203       % units={'pixel','pixel','pixel','pixel','pixel','pixel','','','','pixel','pixel','pixel','pixel','pixel',''};
204end
205if ~strcmp(FieldRequest,'interp_tps')
206    var=var(:,1:9);%suppress tps if not needed
207end
208vel_type_out=vel_type;
209
210
211
212
213
Note: See TracBrowser for help on using the repository browser.