source: trunk/src/read_civdata.m @ 1048

Last change on this file since 1048 was 1033, checked in by sommeria, 6 years ago

miscellaneous updates

File size: 10.6 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% FUNCTIONS called:
43% 'varcivx_generator':, sets the names of vaiables to read in the netcdf file
44% 'nc2struct': reads a netcdf file
45
46%=======================================================================
47% Copyright 2008-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
48%   http://www.legi.grenoble-inp.fr
49%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
50%
51%     This file is part of the toolbox UVMAT.
52%
53%     UVMAT is free software; you can redistribute it and/or modify
54%     it under the terms of the GNU General Public License as published
55%     by the Free Software Foundation; either version 2 of the license,
56%     or (at your option) any later version.
57%
58%     UVMAT is distributed in the hope that it will be useful,
59%     but WITHOUT ANY WARRANTY; without even the implied warranty of
60%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
61%     GNU General Public License (see LICENSE.txt) for more details.
62%=======================================================================
63
64function [Field,VelTypeOut,errormsg]=read_civdata(FileName,FieldNames,VelType)
65
66%% default input
67if ~exist('VelType','var')
68    VelType='';
69end
70if isequal(VelType,'*')
71    VelType='';
72end
73if isempty(VelType)
74    VelType='';
75end
76if ~exist('FieldNames','var')
77    FieldNames=[]; %default
78end
79Field=[];
80VelTypeOut=VelType;
81errormsg='';
82if ischar(FieldNames), FieldNames={FieldNames}; end;
83ProjModeRequest='';
84for ilist=1:length(FieldNames)
85    if ~isempty(FieldNames{ilist})
86        switch FieldNames{ilist}
87            case {'U','V','norm(U,V)'}
88                if ~strcmp(FieldNames{1},'vec(U,V)')% if the scalar is not used as color of vectors
89                ProjModeRequest='interp_lin';
90                end
91            case {'curl(U,V)','div(U,V)','strain(U,V)'}
92                ProjModeRequest='interp_tps';
93        end
94    end
95end
96
97%% reading data
98Data=nc2struct(FileName,'ListGlobalAttribute','CivStage');
99if isfield(Data,'Txt')
100     errormsg=['error in read_civdata: ' Data.Txt];
101    return
102end
103% set the list of variables to read and their role
104[varlist,role,VelTypeOut]=varcivx_generator(ProjModeRequest,VelType,Data.CivStage);
105if isempty(varlist)
106    errormsg=['error in read_civdata: unknow velocity type ' VelType];
107    return
108else
109    [Field,vardetect]=nc2struct(FileName,varlist);%read the variables in the netcdf file
110end
111if isfield(Field,'Txt')
112    errormsg=Field.Txt;
113    return
114end
115if vardetect(1)==0
116     errormsg=[ 'requested field not available in ' FileName '/' VelType ': need to run patch'];
117     return
118end
119switch VelTypeOut
120    case {'civ1','filter1'}
121        if isfield(Field,'Patch1_SubDomain')
122            Field.SubDomain=Field.Patch1_SubDomain;
123            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
124        end
125        if isfield(Field,'Civ1_Dt')
126            Field.Dt=Field.Civ1_Dt;
127        end
128        if isfield(Field,'Civ1_Time')
129            Field.Time=Field.Civ1_Time;
130        end
131    case {'civ2','filter2'}
132        if isfield(Field,'Patch2_SubDomain')
133            Field.SubDomain=Field.Patch2_SubDomain;
134            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
135        end
136        if isfield(Field,'Civ2_Dt')
137        Field.Dt=Field.Civ2_Dt;
138        end
139        if isfield(Field,'Civ2_Time')
140        Field.Time=Field.Civ2_Time;
141        end
142end
143Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'Dt','Time'}];
144ivar_U_tps=[];
145ivar_V_tps=[];
146var_ind=find(vardetect);
147for ivar=1:numel(var_ind)
148    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
149    %Field.VarAttribute{ivar}.Mesh=0.025;%typical mesh for histograms O.025 pixel (used in series)
150    Field.VarAttribute{ivar}.ProjModeRequest=ProjModeRequest;
151    if strcmp(role{var_ind(ivar)},'vector_x')
152        Field.VarAttribute{ivar}.FieldName=FieldNames;
153        ivar_U=ivar;
154    end
155    if strcmp(role{var_ind(ivar)},'vector_x_tps')
156        Field.VarAttribute{ivar}.FieldName=FieldNames;
157        ivar_U_tps=ivar;
158    end
159    if strcmp(role{var_ind(ivar)},'vector_y')
160        Field.VarAttribute{ivar}.FieldName=FieldNames;
161        ivar_V=ivar;
162    end
163    if strcmp(role{var_ind(ivar)},'vector_y_tps')
164        Field.VarAttribute{ivar}.FieldName=FieldNames;
165        ivar_V_tps=ivar;
166    end
167end
168if ~isempty(ivar_U_tps)
169    Field.VarAttribute{ivar_U}.VarIndex_tps=ivar_U_tps;
170end
171if ~isempty(ivar_V_tps)
172    Field.VarAttribute{ivar_V}.VarIndex_tps=ivar_V_tps;
173end
174
175%% update list of global attributes
176Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'NbCoord','NbDim','TimeUnit','CoordUnit'}];
177Field.NbCoord=2;
178Field.NbDim=2;
179Field.TimeUnit='s';
180Field.CoordUnit='pixel';
181
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
184%INPUT:
185% FieldNames =cell of field names to get, which can contain the strings:
186%             'ima_cor': image correlation, vec_c or vec2_C
187%             'vort','div','strain': requires velocity derivatives DUDX...
188%             'error': error estimate (vec_E or vec2_E)
189%             
190% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
191%            if vel_type=[] or'*', a  priority choice is done, civ2 considered better than civ1 )
192
193function [var,role,vel_type_out,errormsg]=varcivx_generator(ProjModeRequest,vel_type,CivStage)
194
195%% default input values
196if ~exist('vel_type','var'),vel_type='';end;
197if iscell(vel_type),vel_type=vel_type{1}; end;%transform cell to string if needed
198errormsg='';
199
200%% select the priority order for automatic vel_type selection
201if strcmp(vel_type,'civ2') && strcmp(ProjModeRequest,'derivatives')
202    vel_type='filter2';
203elseif strcmp(vel_type,'civ1') && strcmp(ProjModeRequest,'derivatives')
204    vel_type='filter1';
205end
206if isempty(vel_type)||strcmp(vel_type,'*')
207    vel_type='filter2';% case CivStage >=6
208    switch CivStage
209        case {4,5}% civ2 available but not filter2
210            if strcmp(ProjModeRequest,'derivatives')% derivatives needed
211                vel_type='filter1';
212            else
213                vel_type='civ2';
214            end
215        case 3
216            vel_type='filter1';
217        case {1,2}% civ1 available but not filter1
218            vel_type='civ1';
219    end
220end
221
222var={};
223switch vel_type
224    case 'civ1'
225        var={'X','Y','Z','U','V','W','C','F','FF';...
226            'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U','Civ1_V','Civ1_W','Civ1_C','Civ1_F','Civ1_FF'};
227        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'};
228    %    units={'pixel','pixel','pixel','pixel','pixel','pixel','','',''};
229    case 'filter1'
230        var={'X','Y','Z','U','V','W','C','F','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbCentre','NbCentre','NbCentre';...
231            'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U_smooth','Civ1_V_smooth','Civ1_W','Civ1_C','Civ1_F','Civ1_FF',...
232            'Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps','Civ1_W_tps','Civ1_SubRange','Civ1_NbCentre','Civ1_NbCentres','Civ1_NbSites'};
233        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag','coord_tps','vector_x_tps',...
234            'vector_y_tps','vector_z_tps','ancillary','ancillary','ancillary','ancillary'};
235     %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
236    case 'civ2'
237        var={'X','Y','Z','U','V','W','C','F','FF';...
238            'Civ2_X','Civ2_Y','Civ2_Z','Civ2_U','Civ2_V','Civ2_W','Civ2_C','Civ2_F','Civ2_FF'};
239        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'};
240    case 'filter2'
241        var={'X','Y','Z','U','V','W','C','F','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbCentre','NbCentre','NbCentre';...
242            'Civ2_X','Civ2_Y','Civ2_Z','Civ2_U_smooth','Civ2_V_smooth','Civ2_W','Civ2_C','Civ2_F','Civ2_FF',...
243            'Civ2_Coord_tps','Civ2_U_tps','Civ2_V_tps','','Civ2_SubRange','Civ2_NbCentre','Civ2_NbCentres','Civ2_NbSites'};
244        role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag','coord_tps','vector_x_tps',...
245            'vector_y_tps','vector_z_tps','ancillary','ancillary','ancillary','ancillary'};
246end
247if ~strcmp(ProjModeRequest,'interp_tps')
248    var=var(:,1:9);%suppress tps if not needed
249end
250vel_type_out=vel_type;
251
252
253
254
255
Note: See TracBrowser for help on using the repository browser.